From e357907ee6c8c11aa603b41a00cb8b79454043bd Mon Sep 17 00:00:00 2001 From: Bot Date: Wed, 9 Sep 2026 13:47:14 -0700 Subject: [PATCH] fix(git-guard): add unmerged-path guard + concrete remediation syntax for every failure branch Adds the unmerged-conflict guard flagged in PR review: before the dirty-tree flow can run, check `git ls-files -u` and refuse to auto-commit if unresolved merge conflict markers are present (e.g. left behind by a prior run's failed `git stash pop`). Without this, a re-run's `git add -A` would silently stage literal <<<<<<< / ======= / >>>>>>> markers into a real commit and push them to origin/main. Also expands every failure-path message (stash-push failure, stash-pop conflict vs. untracked-file-collision, resync failure, true divergence, manual-abort cases) to include concrete, copy-pasteable remediation command sequences with multiple options (resolve-in-place vs. abandon-and-reapply vs. drop-if-unneeded), rather than a single generic hint. The true-divergence message now also tells the operator to check `git stash list` first, so a stash created by the new dirty+stale path isn't confused with a second manually-created one. No change to the common dirty-but-current or clean-but-stale behavior. --- deploy/git-guard.sh | 171 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 147 insertions(+), 24 deletions(-) diff --git a/deploy/git-guard.sh b/deploy/git-guard.sh index 4fed859..0426ce1 100644 --- a/deploy/git-guard.sh +++ b/deploy/git-guard.sh @@ -7,6 +7,8 @@ # - Clean + behind (ff-only) -> auto `git pull --ff-only`, then pass # - Clean + ahead only -> interactive: offer to push; non-interactive: BLOCK # (unpushed) +# - Unresolved merge conflict -> REFUSE immediately. Never auto-commits over +# markers present conflict markers. Prints remediation options. # - Dirty + local in sync -> offer to commit + push right now # with origin (auto in non-interactive/CI runs, after a # secret-pattern scan of the staged diff) @@ -15,8 +17,8 @@ # origin using the same behind/ahead/diverged # rules as the clean-tree case, then reapply # the stash and re-run. On any failure the -# stash is preserved and printed for manual -# recovery. +# stash is preserved and remediation options +# (with exact commands) are printed. # - Diverged (local AND -> REFUSE. Never auto-resolves. Prints the # remote both moved) backup/stash/reset recovery steps and exits. # @@ -76,7 +78,8 @@ resync_with_origin() { echo "==> Fast-forwarded to $(git rev-parse --short main). OK to deploy." return 0 else - echo "Aborting - pull manually, then retry." + echo "Aborting - pull manually, then retry:" + echo " cd $DIR && git pull --ff-only origin main" return 1 fi fi @@ -97,7 +100,10 @@ resync_with_origin() { echo "==> Pushed. OK to deploy." return 0 else - echo "Aborting. Review with: git log origin/main..main" + echo "Aborting. Review with:" + echo " cd $DIR && git log origin/main..main" + echo "Then push manually when ready:" + echo " git push origin main" return 1 fi fi @@ -113,28 +119,74 @@ resync_with_origin() { git log --oneline "$base_sha..origin/main" | sed 's/^/!! /' echo "!!" echo "!! This requires a human decision - git-guard will NOT auto-resolve this." + echo "!!" + echo "!! Before stashing anything new, check whether a git-guard safety stash" + echo "!! ALREADY exists from this same run (avoids confusing duplicate stashes):" + echo "!! git stash list" + echo "!!" echo "!! Recommended recovery:" echo "!! 1. tar backup: tar czf /volume1/docker/compose-files-backup-\$(date +%Y%m%d-%H%M%S).tar.gz -C /volume1/docker compose-files" echo "!! 2. name the branch: git branch backup/pre-reset-\$(date +%Y%m%d)" - echo "!! 3. stash all state: git stash push -u -m 'pre-reset-snapshot'" + echo "!! 3. stash any NEW uncommitted state only if 'git stash list' above" + echo "!! didn't already show one for this run:" + echo "!! git stash push -u -m 'pre-reset-snapshot'" echo "!! 4. reset to origin: git reset --hard origin/main" - echo "!! 5. selectively restore needed files from the stash/backup branch" + echo "!! 5. selectively restore needed files from the stash/backup branch:" + echo "!! git stash list" + echo "!! git stash show -p stash@{N}" + echo "!! git stash apply stash@{N} # 'apply' keeps the stash as a backup; use 'pop' to also drop it" echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" return 1 } +# ---- Case: unresolved merge conflict already present ---- +# Can happen if a PRIOR git-guard run's `git stash pop` conflicted and the +# resulting conflict markers were never resolved before the next deploy +# attempt. Must be checked BEFORE the dirty-tree commit flow below, because +# an unmerged path shows up as "dirty" too, and `git add -A` would silently +# stage the literal <<<<<<< / ======= / >>>>>>> markers into a real commit. +if git ls-files -u | grep -q .; then + echo "ERROR: unresolved merge conflict markers present in the working tree." + echo "Refusing to auto-commit over a conflict — this would push literal" + echo "<<<<<<< / ======= / >>>>>>> markers to origin/main." + echo + echo "Conflicted paths:" + git diff --name-only --diff-filter=U | sed 's/^/ /' + echo + echo "Remediation options:" + echo " A) Resolve the conflict by hand, then commit and push:" + echo " cd $DIR" + echo " git status # see conflicted paths" + echo " git diff # inspect the conflict markers" + echo " \$EDITOR # remove markers, keep correct content" + echo " git add " + echo " git commit -m 'resolve git-guard stash-pop conflict'" + echo " git push origin main" + echo " B) Discard the conflicted merge attempt entirely and start clean from" + echo " origin/main, then decide separately whether to re-apply anything" + echo " from a prior safety stash:" + echo " cd $DIR" + echo " git checkout -- ." + echo " git reset --hard origin/main" + echo " git stash list # look for a git-guard-safety-stash-* entry" + echo " git stash show -p stash@{N} # inspect before deciding" + echo " C) Once resolved (via A or B) and no longer needed, clean up the stash:" + echo " git stash drop stash@{N}" + exit 1 +fi + # ---- Case: dirty tracked changes ---- if [ "$DIRTY" -eq 1 ]; then echo "!! WORKING TREE DIRTY — uncommitted changes detected:" git status --short echo - # NEW: if local is ALSO stale/diverged from origin, committing right now - # would create a doomed commit on top of a base that's about to be - # rejected on push (this is exactly what caused a real incident: a stray - # on-disk edit sat on a checkout that was 4 commits behind, git-guard - # auto-committed anyway, then the push bounced). Stash first, resync - # safely using the same rules as the clean-tree path, then reapply. + # If local is ALSO stale/diverged from origin, committing right now would + # create a doomed commit on top of a base that's about to be rejected on + # push (this is exactly what caused a real incident: a stray on-disk edit + # sat on a checkout that was 4 commits behind, git-guard auto-committed + # anyway, then the push bounced). Stash first, resync safely using the + # same rules as the clean-tree path, then reapply. if [ "$LOCAL" != "$REMOTE" ]; then echo "!! Local main is ALSO stale/diverged from origin/main." echo " Refusing to commit on top of a stale base — stashing the dirty" @@ -142,7 +194,24 @@ if [ "$DIRTY" -eq 1 ]; then echo STASH_MSG="git-guard-safety-stash-$(date -u +%Y%m%dT%H%M%SZ)" - git stash push -u -m "$STASH_MSG" + if ! git stash push -u -m "$STASH_MSG"; then + echo "ERROR: 'git stash push' itself failed (disk full, permissions, or" + echo " some other git error). Your changes are still on disk," + echo " uncommitted — nothing has been lost, but git-guard cannot" + echo " proceed safely until this is resolved." + echo + echo "Remediation options:" + echo " A) Check disk space and permissions, then retry the deploy:" + echo " df -h $DIR" + echo " ls -la $DIR" + echo " B) Identify and manually move aside whatever is blocking the stash," + echo " then retry:" + echo " cd $DIR" + echo " git status --short # find the offending path(s)" + echo " mv .bak-\$(date +%s)" + echo " C) Inspect the raw git error above for specifics before proceeding." + exit 1 + fi echo "==> Stashed as: $STASH_MSG" if resync_with_origin "$LOCAL" "$REMOTE" "$BASE"; then @@ -151,19 +220,68 @@ if [ "$DIRTY" -eq 1 ]; then echo "==> Stash reapplied cleanly. Re-checking sync state..." exec "$0" "$@" else - echo "ERROR: stash pop produced conflicts. Your changes are safe in the" - echo " stash but could not be auto-reapplied on the resynced base." - echo "Resolve manually:" - echo " cd $DIR" - echo " git status" - echo " git stash list # look for: $STASH_MSG" - echo " # resolve conflicts, then: git stash drop" + echo "ERROR: 'git stash pop' did not complete successfully." + echo + if git ls-files -u | grep -q .; then + echo "This is a MERGE CONFLICT — your stashed changes were partially" + echo "applied and conflict markers (<<<<<<< / ======= / >>>>>>>) are now" + echo "in the working tree. The stash itself is still preserved as a backup." + echo + echo "Remediation options:" + echo " A) Resolve the conflict by hand, then commit and push:" + echo " cd $DIR" + echo " git status # see conflicted paths" + echo " git diff # inspect the markers" + echo " \$EDITOR # remove markers, keep correct content" + echo " git add " + echo " git commit -m 'resolve git-guard stash-pop conflict'" + echo " git push origin main" + echo " git stash list # confirm which entry is: $STASH_MSG" + echo " git stash drop stash@{N} # once confirmed no longer needed" + echo " B) Abandon this merge attempt and fall back to a clean, resynced" + echo " tree, then re-apply the change manually with full visibility:" + echo " cd $DIR" + echo " git checkout -- ." + echo " git reset --hard origin/main # now matches origin, no conflict" + echo " git stash list # find: $STASH_MSG" + echo " git stash show -p stash@{N} # review the content" + echo " git stash apply stash@{N} # 'apply' keeps the backup; use 'pop' to also drop it" + else + echo "This looks like an UNTRACKED-FILE COLLISION, not a merge conflict" + echo "(a file added upstream shares a path with an untracked file in your" + echo "stash). No conflict markers were written; the stash was NOT applied" + echo "and remains fully intact." + echo + echo "Remediation options:" + echo " A) Move the colliding upstream file aside, pop, then reconcile:" + echo " cd $DIR" + echo " git status --short # identify the colliding path" + echo " mv .upstream-\$(date +%s)" + echo " git stash pop" + echo " diff .upstream-* # reconcile manually, then remove the .upstream-* backup" + echo " B) Inspect the stash without applying, and hand-merge the needed" + echo " pieces instead:" + echo " git stash list # find N" + echo " git stash show -p stash@{N}" + fi + echo + echo "Your stash reference for this run: $STASH_MSG" exit 1 fi else echo "ERROR: could not safely resync with origin/main." echo "Your uncommitted changes are preserved in the stash: $STASH_MSG" - echo "Resolve manually, then run: git stash pop" + echo + echo "Remediation options:" + echo " A) Follow the manual recovery steps printed above (from the" + echo " behind/ahead/diverged case), THEN reapply your change:" + echo " cd $DIR" + echo " git stash list # find: $STASH_MSG" + echo " git stash apply stash@{N} # or 'pop' to also drop it once resynced" + echo " B) If the stashed change is no longer needed (e.g. it's already" + echo " represented in a since-merged PR), verify then drop it:" + echo " git stash show -p stash@{N}" + echo " git stash drop stash@{N}" exit 1 fi fi @@ -180,7 +298,8 @@ if [ "$DIRTY" -eq 1 ]; then if git diff --cached | grep -Eiq "$SECRET_PATTERN"; then echo "ERROR: possible secret detected in staged changes. Refusing to auto-commit." - echo "Review manually: git diff --cached" + echo "Review manually:" + echo " cd $DIR && git diff --cached" git reset exit 1 fi @@ -192,11 +311,15 @@ if [ "$DIRTY" -eq 1 ]; then exec "$0" "$@" else echo "ERROR: push failed (likely diverged from origin). Aborting deploy." - echo "Run: cd $DIR && git status" + echo "Run:" + echo " cd $DIR && git status" exit 1 fi else - echo "Aborting deploy - commit or stash changes manually, then retry." + echo "Aborting deploy - commit or stash changes manually, then retry:" + echo " cd $DIR" + echo " git add -A && git commit -m 'your message' && git push origin main" + echo " # or: git stash push -u -m 'manual-stash'" exit 1 fi fi