OverviewFilesCommitsIssuesDocsReleasesPeers

hyper-tps / tools / project_godot_guard.sh

59 lines · bash ref:
#!/usr/bin/env bash
# Godot's editor rewrites project.godot and CANNOT keep comments.
#
# ConfigFile's writer emits keys and values only, so every `;` line in that
# file is destroyed the moment the editor saves project settings -- which
# dev-run.sh does routinely. It also prunes keys sitting at their default and
# reorders sections. A ~28-line comment block explaining the Jolt physics
# experiment was lost exactly this way and nearly committed.
#
# This restores that damage, and ONLY that damage.
#
#   check    exit 1 if comments have been stripped
#   restore  restore from git IF the only changes are removals/reordering
#
# The safety rule is a subset test: every non-comment line in the CURRENT file
# must already exist in the committed one. Comment stripping, key reordering
# and default-pruning all satisfy that. A setting you actually changed in the
# editor introduces a line that is NOT in the committed file, and restore
# refuses and shows you the diff instead of throwing your change away.
set -uo pipefail
cd "$(dirname "$0")/.." || exit 2
F=project.godot
MODE="${1:-check}"

git diff --quiet -- "$F" && { echo "project-godot-guard: clean"; exit 0; }

CANON=$(mktemp); CUR=$(mktemp)
trap 'rm -f "$CANON" "$CUR"' EXIT
git show "HEAD:$F" > "$CANON" 2>/dev/null || { echo "guard: $F not in HEAD"; exit 2; }
cp "$F" "$CUR"

norm() { grep -vE '^\s*;|^\s*$' "$1" | sort; }
ADDED=$(comm -13 <(norm "$CANON") <(norm "$CUR"))
LOST_COMMENTS=$(( $(grep -cE '^\s*;' "$CANON") - $(grep -cE '^\s*;' "$F") ))

if [ -n "$ADDED" ]; then
    echo "project-godot-guard: REAL CHANGES present, not just editor churn:"
    echo "$ADDED" | sed 's/^/    + /'
    [ "$LOST_COMMENTS" -gt 0 ] && echo "  (…and $LOST_COMMENTS comment lines were also stripped)"
    echo "  Refusing to restore. Re-add the comment block by hand, or commit"
    echo "  the settings change and then restore comments from an older rev."
    exit 1
fi

if [ "$LOST_COMMENTS" -le 0 ]; then
    echo "project-godot-guard: changed, but no comments lost — leaving it alone"
    git diff --stat -- "$F"
    exit 0
fi

if [ "$MODE" = "restore" ]; then
    git checkout -- "$F"
    echo "project-godot-guard: restored $F ($LOST_COMMENTS comment lines the editor stripped)"
    exit 0
fi
echo "project-godot-guard: $LOST_COMMENTS comment lines stripped by the editor."
echo "  Run: tools/project_godot_guard.sh restore"
exit 1