Skip to content

fix(scripts): detect the primary checkout from git's own repo layout - #451

Open
growlf wants to merge 3 commits into
mainfrom
450-bin-check-worktree-sh-reports-the-primary-checkout
Open

growlf wants to merge 3 commits into
mainfrom
450-bin-check-worktree-sh-reports-the-primary-checkout

Conversation

@growlf

@growlf growlf commented Oct 21, 2026 •

Copy link
Copy Markdown
Owner

Closes #450

What

bin/check-worktree.sh reported "in the primary checkout" from inside every linked worktree, so the guard's only meaningful signal — the primary checkout having drifted off main — was buried under noise that fired on every legitimate commit.

Root cause

REPO_ROOT was derived from the script's own location and reused as PRIMARY_PATH, then compared against each entry of git worktree list. That list always contains the worktree the caller is standing in, so the comparison matched unconditionally and IS_PRIMARY=1 was guaranteed.

The fix compares git rev-parse --absolute-git-dir with --git-common-dir: identical only in the primary checkout, distinct (<common>/worktrees/<name>) in a linked worktree. The word-splitting for line in $WORKTREE_LIST loop goes away with it, so checkout paths containing spaces no longer break detection.

A second defect the new tests found

GREEN was never defined in the script's colour block, so the success path — OK: In worktree '<path>' on branch '<branch>' — died under set -u with GREEN: unbound variable, exit 1. It had never been reached, because the broken detector always took the warning branch. One bug was hiding the other; fixing detection alone would have turned "false warning" into "fails on every legitimate commit".

Tests

New tests/test_check_worktree.py, 8 cases built on real repositories (stubbing git would have reproduced the bug's assumptions rather than catching them):

Situation Expected
worktree, feature branch exit 0, no "primary checkout"
worktree path containing a space resolves correctly
worktree, run from a subdirectory still detected as a worktree
primary, main exit 1, refusal
primary, main, --allow-main exit 0, override acknowledged
primary, feature branch exit 0, warning (the genuine case)
worktree on main exit 0, worktree-on-main warning
outside a git repository exit 1, clear error

Full suite: 1716 passed, 1 skipped.

Also in this branch

Two commits carried over from session work that would otherwise have been left uncommitted in the primary checkout — which is the very state this script exists to prevent:

  • docs(kb) — two findings that cost real diagnostic time: a Frappe site 500ing because its DB user was granted for a single container IP, and the Bitwarden CLI answering bw list from a local cache while locked, which can turn "not in the vault" into a confidently wrong answer. Container addresses use RFC 5737 documentation ranges.
  • fix(ansible) — render handler: redirect routes through their own branch in the Caddyfile template. No rendered-output change: the live deployed config is byte-identical to this template's render.

Deliberately not included: uncommitted edits to bin/bwunlock.sh in the primary checkout, which belong to another session still in flight there.

…450)

check-worktree.sh derived a 'primary path' from the script's own location and
compared it against every entry of `git worktree list`. That entry set always
contains the worktree the caller is standing in, so the comparison matched
unconditionally and every worktree commit printed 'WARNING: On branch X in the
primary checkout' - masking the one condition worth flagging.

Compare --absolute-git-dir against --git-common-dir instead: identical only in
the primary checkout, distinct in a linked worktree. This also removes the
word-splitting loop, so paths containing spaces no longer break detection.

The new tests immediately caught a second latent defect: GREEN was never
defined, so the success path died under 'set -u' with 'GREEN: unbound
variable'. A broken detector had hidden an unreachable success path.

Closes #450
- frappe-500-after-restart-db-grant: a site 500s after a container restart
  because its DB user was granted for one container IP; diagnose from the
  backend log, fix the grant for '%'.
- bitwarden-cli-serves-stale-cache: bw list answers from its local cache even
  while locked, so 'not in the vault' can be a wrong conclusion drawn from data
  older than the change being investigated; lastSync is the only freshness
  signal.

Container addresses in the first entry use RFC 5737 documentation ranges.
A route declaring handler: redirect only ever carried its raw extra lines
through the generic 'extra is defined' branch. Give redirects their own branch
so the intent is explicit and a future edit to the generic branch cannot
silently drop them.

No change to rendered output: the live deployed Caddyfile is byte-identical to
this template's render.
@growlf growlf self-assigned this Oct 21, 2026
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.

bin/check-worktree.sh reports the primary checkout from inside every worktree

1 participant