Skip to content

fix: keep the live config when returning to the base branch after a backup - #12

Merged
askb merged 1 commit into
askb:mainfrom
mmornati:upstream/keep-live-config-after-backup
Nov 4, 2026
Merged

askb merged 1 commit into
askb:mainfrom
mmornati:upstream/keep-live-config-after-backup

Conversation

@mmornati

Copy link
Copy Markdown
Contributor

Requested by Marco

Problem

After a run commits the drift on auto-backup/<timestamp> and pushes it, the EXIT trap cleanup_branch returns to the base branch with git checkout -q "$BASE_BRANCH".

The base branch doesn't have that drift yet (the PR isn't merged), so the checkout rewrites /config to the pre-backup state:

  • Every backed-up change disappears from disk until the next run pulls the merged PR (up to 24 h with the default schedule). For example, a HACS update to an integration goes back to the old version on disk, and UI edits are reverted.
  • Edits made in that window are applied on top of stale files, which then shows up as warning:stash_conflict or error:pull_rebase_failed on the next run.
  • If the backup PR is never merged, the drift is lost for good.

We hit this on a production instance: integration versions on disk kept bouncing between the old and new versions after every backup run.

Fix

Point HEAD back at the base branch without touching the working tree:

git symbolic-ref HEAD "refs/heads/${BASE_BRANCH}"
git reset -q   # index = base branch, working tree unchanged

The drift stays as uncommitted changes, and the next run's existing stash → pull → pop reconciles it with the merged PR.

Tests

tests/self_check.sh now asserts that:

  • after a backup run the live files are still the changed ones, and new files still exist (step 3);
  • once the backup branch is merged upstream, the next run pulls it and finds no drift, no conflict and no leftover stash, with the live files unchanged (step 3b).

Run in alpine:3.21: OK: all self-checks passed. With the current git checkout the new step 3 assertion fails.

Version bumped to 0.3.1, with a CHANGELOG entry. I'm also opening a separate PR for a related stash-conflict bug. Both bump to 0.3.1, so whichever lands second needs its version bumped again; I'm happy to rebase.

🤖 Generated with Claude Code

…ackup

cleanup_branch ran `git checkout <base>` after committing the drift on
the backup branch. The base branch does not have that drift yet, so the
checkout rewrote /config to the pre-backup state: backed-up changes left
the disk until the next run pulled the merged PR, edits made in between
hit stale files (stash_conflict / pull_rebase_failed), and drift from a
PR that was never merged was lost.

Move HEAD with symbolic-ref + mixed reset instead, so the working tree
is untouched and the drift is reconciled by the next stash/pull/pop.
The self-check now asserts the live files survive a backup and that the
run after the PR merges syncs cleanly.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: mmornati <[email protected]>
@askb
askb merged commit 16b44ed 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