Skip to content

fix: clear the git index after a stash conflict - #13

Merged
askb merged 1 commit into
askb:mainfrom
mmornati:upstream/clear-index-after-stash-conflict
Nov 4, 2026
Merged

askb merged 1 commit into
askb:mainfrom
mmornati:upstream/clear-index-after-stash-conflict

Conversation

@mmornati

Copy link
Copy Markdown
Contributor

Requested by Marco

Problem

When git stash pop conflicts after the upstream sync (the live config and a merged PR changed the same file, typically a HACS update on the box), the script runs:

git checkout -q --theirs . 2>/dev/null || true
git stash drop -q
write_status warning stash_conflict "kept live config where conflicting"

checkout --theirs rewrites the files with the live version, which is correct, but the conflict stays in the index (UU entries in git status). From then on, every run fails at the first git stash:

custom_components/browser_mod/manifest.json: needs merge
www/community/lovelace-lawn-mower-card/lawn-mower-card.js: needs merge
error: could not write index
WARNING: Backup run failed (see log in config dir)

No backup and no upstream sync happens again until someone SSHes in and runs git reset by hand. On our instance it stayed stuck for more than a day after a browser_mod 3.4.0 → 3.4.1 HACS update, which also blocked merged config PRs from reaching the box.

Fix

  • Right after keeping the live files, git reset -q unstages everything. The kept files become plain drift and go out through the normal backup PR.
  • At the start of a run, if git ls-files -u is not empty (a repo already stuck because of an earlier version), log it and run git reset -q so the repo heals by itself.

Both only touch the index. The working tree, which is the live config, is never modified.

Tests

Two new cases in tests/self_check.sh:

  • 3c: upstream and live edit the same file. The run must keep the live version, report the stash conflict and leave no unmerged entries, and the next run must not fail.
  • 3d: a repo seeded with unmerged index entries must heal on the next run.

Run in alpine:3.21: OK: all self-checks passed. With the current script the new test fails (FAIL: unmerged index entries left after stash conflict).

Version bumped to 0.3.1, with a CHANGELOG entry. This is independent of #12 (rollback after backup), but that bug is a common source of these stash conflicts. Both bump to 0.3.1, so whichever lands second needs its version bumped again; I'm happy to rebase.

🤖 Generated with Claude Code

askb pushed a commit that referenced this pull request Nov 4, 2026
…ackup (#12)

Thanks Marco — confirmed on my own box: today's run reverted /config after pushing the branch. Merging. #13 will be rebased on top (version/changelog conflict) and a follow-up adds de-duplication of open backup PRs now that drift persists between runs.
After `git stash pop` conflicted, the script ran `git checkout --theirs .`
to keep the live files, but the conflict stayed in the index. Every later
run then failed at `git stash` with "needs merge" / "could not write
index", so no backup or upstream sync happened again until someone ran
`git reset` by hand.

Unstage everything right after keeping the live files, and heal a repo
already stuck that way at the start of a run. The working tree is never
touched. Self-check covers both cases (fails on 0.3.1).

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Signed-off-by: mmornati <[email protected]>
@askb
askb force-pushed the upstream/clear-index-after-stash-conflict branch from eaefcc5 to 5a918b1 Compare November 4, 2026 06:49
@askb

askb commented Nov 4, 2026

Copy link
Copy Markdown
Owner

Rebased onto main after #12 (kept both 0.3.1 changelog entries, no code conflict). Self-check 3a–3d and shellcheck pass locally. Merging once CI is green — thanks Marco.

@askb
askb merged commit 0376d9e into askb:main Nov 4, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants