fix(hooks): skip post-checkout rebuild when PREV_HEAD equals NEW_HEAD (#2421)

git checkout -b <new> reports a branch switch (flag=1) but passes the same SHA
for prev and new HEAD, so the post-checkout hook fired a full changed_paths-less
rebuild — the most expensive graphify op — on essentially every new branch. Add
a POSIX-sh guard that exits 0 when PREV_HEAD == NEW_HEAD; a real branch switch
(PREV != NEW) still rebuilds.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
nothariharan
2026-08-17 17:47:56 +01:00
committed by safishamsi
co-authored by Claude Opus 4.8
parent e7cb399ac3
commit 5c88af7617
2 changed files with 13 additions and 0 deletions
+4
View File
@@ -355,6 +355,10 @@ if [ "$BRANCH_SWITCH" != "1" ]; then
exit 0
fi
# A no-op checkout (e.g. `git checkout -b` with no start point) reports a
# branch switch but leaves the tree unchanged ΓÇö nothing to rebuild (#2421).
[ "$PREV_HEAD" = "$NEW_HEAD" ] && exit 0
# Only run if graphify-out/ exists (graph has been built before)
if [ ! -d "graphify-out" ]; then
exit 0
+9
View File
@@ -614,6 +614,15 @@ def test_hooks_honor_skip_env(name, script):
)
def test_checkout_hook_skips_same_head_noop():
"""`git checkout -b` with no start point passes identical PREV/NEW heads.
Rebuild must short-circuit ΓÇö PREV_HEAD/NEW_HEAD were previously assigned
and never read (#2421 / leftover from #1809)."""
assert "PREV_HEAD=$1" in _CHECKOUT_SCRIPT
assert "NEW_HEAD=$2" in _CHECKOUT_SCRIPT
assert '[ "$PREV_HEAD" = "$NEW_HEAD" ] && exit 0' in _CHECKOUT_SCRIPT
@pytest.mark.parametrize("name,script", _HOOK_SCRIPTS)
def test_hooks_skip_linked_worktrees(name, script):
"""Both hooks must short-circuit in a linked worktree (git-dir != common-dir),