diff --git a/nixos/home/laptop-repo-keeper.sh b/nixos/home/laptop-repo-keeper.sh index 2c97278ee9..e48288e555 100755 --- a/nixos/home/laptop-repo-keeper.sh +++ b/nixos/home/laptop-repo-keeper.sh @@ -13,6 +13,12 @@ # Untracked files are only counted here; the daily # laptop-untracked-review classifies them (oversized ones belong # to the backup scripts) +# A snapshot is only ever committed on an up-to-date base: if +# the primary is behind origin, committing would put it ahead +# AND behind at once (diverged), after which this keeper can +# only report it and pm-git-sync can no longer fast-forward — +# the 2026-11-02 code#4464 divergence. Behind + dirty is +# reported loudly and left uncommitted for reconciliation. # - pulls fast-forward when behind, pushes when ahead; never # force-pushes, never merges, never rebases # Worktrees are counted for the report but never touched: they are @@ -95,6 +101,13 @@ for d in "${REPOS[@]}"; do [ -n "$big" ] && say "- $name: LARGE TRACKED FILES: $big" fi wt="$(g worktree list | wc -l)" + # Ahead/behind is needed BEFORE the snapshot step (see header): + # the counts are recomputed after it for the pull/push steps. + counts="0 0" + if [ "$has_origin" -eq 1 ]; then + counts="$(g rev-list --left-right --count "$db...origin/$db" 2>/dev/null || echo '0 0')" + fi + read -r ahead behind <<<"$counts" if [ "$br" != "$db" ]; then if [ "$dirty" -eq 0 ]; then if g checkout -q "$db"; then @@ -111,7 +124,9 @@ for d in "${REPOS[@]}"; do fi if [ "$dirty" -gt 0 ]; then residue="$(g ls-files | grep -Ei "$RESIDUE_RE" || true)" - if [ -n "$residue" ]; then + if [ "${behind:-0}" -gt 0 ]; then + say "- $name: DIRTY ($dirty tracked entries) NOT committed — $behind commit(s) behind origin/$db; a snapshot on a stale base would diverge the primary (code#4464) — reconcile first" + elif [ -n "$residue" ]; then residue_list="$(printf '%s' "$residue" | tr '\n' ' ')" say "- $name: TRACKED RESIDUE NOT committed — move these paths to a dated residue directory first: $residue_list" elif g status --porcelain | grep -v '^??' | grep -Eiq "$SECRET_RE"; then diff --git a/nixos/home/laptop-repo-keeper.test.sh b/nixos/home/laptop-repo-keeper.test.sh index 072fa35cd7..543ce75584 100755 --- a/nixos/home/laptop-repo-keeper.test.sh +++ b/nixos/home/laptop-repo-keeper.test.sh @@ -82,6 +82,45 @@ check 'clean case: tracked snapshot committed' '2' "$(git -C "$R2" rev-list --co grep -Fq -- 'committed 1 tracked entries on main' "$TMP_ROOT/clean.log" || fail 'clean case: report missing successful snapshot' +# A dirty primary that is BEHIND origin must not be snapshot-committed: +# the commit would land on a stale base, putting the primary ahead AND +# behind (diverged) at once, after which neither the keeper nor +# pm-git-sync can move it (code#4464). +ORIGIN_BARE="$TMP_ROOT/origin.git" +git init -q --bare -b main "$ORIGIN_BARE" +ADV="$TMP_ROOT/advancer" +git clone -q "$ORIGIN_BARE" "$ADV" +printf 'before\n' >"$ADV/tracked.txt" +git -C "$ADV" add -- tracked.txt +git -C "$ADV" commit -q -m initial +git -C "$ADV" push -q origin main + +H3="$TMP_ROOT/behind-home" +R3="$H3/code" +git clone -q "$ORIGIN_BARE" "$R3" +printf 'upstream\n' >>"$ADV/tracked.txt" +git -C "$ADV" commit -q -am upstream +git -C "$ADV" push -q origin main +printf 'local edit\n' >>"$R3/tracked.txt" +run_keeper "$H3" "$TMP_ROOT/behind.log" +check 'behind case: no snapshot commit on stale base' '1' "$(git -C "$R3" rev-list --count HEAD)" +check 'behind case: tracked edit remains dirty' \ + 'tracked.txt' "$(git -C "$R3" diff --name-only)" +grep -Fq -- 'NOT committed' "$TMP_ROOT/behind.log" || + fail 'behind case: report missing stale-base refusal' +grep -Fq -- 'behind origin/main' "$TMP_ROOT/behind.log" || + fail 'behind case: report missing behind count' + +# The same dirty tree on an up-to-date base still snapshots and pushes. +H4="$TMP_ROOT/current-home" +R4="$H4/code" +git clone -q "$ORIGIN_BARE" "$R4" +printf 'local edit\n' >>"$R4/tracked.txt" +run_keeper "$H4" "$TMP_ROOT/current.log" +check 'current case: snapshot committed on up-to-date base' '3' "$(git -C "$R4" rev-list --count HEAD)" +check 'current case: snapshot pushed to origin' \ + "$(git -C "$R4" rev-parse HEAD)" "$(git --git-dir="$ORIGIN_BARE" rev-parse main)" + if [ "$failures" -gt 0 ]; then echo "$failures failure(s)" >&2 exit 1