From 2e6289f9fbf266a3396c7d17369237d8089cf717 Mon Sep 17 00:00:00 2001 From: Bee Date: Thu, 8 Oct 2026 07:49:57 +0000 Subject: [PATCH] fix(hooks): key the push-time suite on the remote ref, never on the local ref's shape (#7352) Since #7348 the pre-push hook ran scripts/pre-push-suite.sh only for stdin lines whose local ref matched refs/heads/*. For `git push origin HEAD` and `HEAD:refs/heads/x` git passes the literal local ref HEAD, so the suite never ran for those pushes: a Go change pushed that way landed with an empty bazel log, while the same change pushed as `git push origin br1` was refused. The hook now skips a line when the remote ref is refs/tags/* or the local sha is the zero sha (a deletion); everything else is a branch push, HEAD included. TestPrePushHookSuiteKeysOnRemoteRef feeds the real hook HEAD-sourced ref lines from a scratch repository with a stub suite that records whether it ran: both HEAD forms run it on a Go change; a docs-only push, a tag pushed by HEAD or by name, and a deletion do not. On main's hook the two HEAD cases fail. TestPrePushHookRunsSuiteOnlyForGoChanges indexed "pre-push-suite.sh", which the hook's header comment on line 3 also contains, so the ordering assertion held wherever the call block sat. It now indexes the call line; moving the suite block after the managed beads section fails it. Fixes #7351 Co-authored-by: Claude Fable 5.1 --- .githooks/pre-push | 11 ++- scripts/check-versions/pre_push_hook_test.go | 99 ++++++++++++++++++++ scripts/pre_push_suite_test.go | 6 +- 3 files changed, 110 insertions(+), 6 deletions(-) diff --git a/.githooks/pre-push b/.githooks/pre-push index fc57efb0b..4a20fb57d 100755 --- a/.githooks/pre-push +++ b/.githooks/pre-push @@ -61,12 +61,15 @@ fi # `bazel test //... --config=ci`, for branch pushes whose new commits change # Go or Bazel inputs. A tag push, a deletion, or a docs-only push skips it. # BD_PREPUSH_SUITE=go runs plain go test instead, under a banner. +# +# The decision keys on the remote ref and the shas, never on the local ref's +# shape: for `git push origin HEAD` and `HEAD:refs/heads/x` git passes the +# literal local ref `HEAD`, which a `refs/heads/*` match would skip (#7351). zero_sha=0000000000000000000000000000000000000000 suite_needed=0 -while read -r local_ref local_sha _remote_ref remote_sha; do - case "$local_ref" in - refs/heads/*) ;; - *) continue ;; +while read -r _local_ref local_sha remote_ref remote_sha; do + case "$remote_ref" in + refs/tags/*) continue ;; esac if [ "$local_sha" = "$zero_sha" ] || ! git cat-file -e "$local_sha^{commit}" 2>/dev/null; then continue diff --git a/scripts/check-versions/pre_push_hook_test.go b/scripts/check-versions/pre_push_hook_test.go index 573f32aec..de6f60647 100644 --- a/scripts/check-versions/pre_push_hook_test.go +++ b/scripts/check-versions/pre_push_hook_test.go @@ -140,6 +140,105 @@ func TestPrePushHookDriftRefusalPointsAtCheckerRemedy(t *testing.T) { } } +// The push-time suite decision keys on the remote ref and the shas, never on +// the local ref's shape: for `git push origin HEAD` and `HEAD:refs/heads/x` +// git passes the literal local ref `HEAD`, which a `refs/heads/*` match +// skipped (#7351). A scratch repository stands in for the checkout and a stub +// scripts/pre-push-suite.sh records whether the hook decided to run it, so +// this also runs under Bazel. +func TestPrePushHookSuiteKeysOnRemoteRef(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("Windows does not provide the oldest supported /bin/bash") + } + + const bash = "/bin/bash" + if _, err := os.Stat(bash); err != nil { + t.Skipf("system Bash unavailable: %v", err) + } + git, err := exec.LookPath("git") + if err != nil { + t.Fatalf("git is required to exercise the pre-push hook: %v", err) + } + + hook := filepath.Join(bazeltest.RepoRoot(t), ".githooks", "pre-push") + scratch := t.TempDir() + suiteLog := filepath.Join(scratch, "suite.log") + stub := "#!/bin/sh\nprintf 'ran\\n' >> '" + suiteLog + "'\n" + if err := os.MkdirAll(filepath.Join(scratch, "scripts"), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(scratch, "scripts", "pre-push-suite.sh"), []byte(stub), 0o755); err != nil { + t.Fatal(err) + } + + gitIn := func(args ...string) string { + t.Helper() + cmd := exec.Command(git, append([]string{"-c", "maintenance.auto=false", "-c", "gc.auto=0"}, args...)...) + cmd.Dir = scratch + cmd.Env = append(os.Environ(), + "GIT_CONFIG_NOSYSTEM=1", "GIT_CONFIG_GLOBAL="+os.DevNull, + "GIT_AUTHOR_NAME=t", "GIT_AUTHOR_EMAIL=t@example.com", + "GIT_COMMITTER_NAME=t", "GIT_COMMITTER_EMAIL=t@example.com", + ) + out, err := cmd.CombinedOutput() + if err != nil { + t.Fatalf("git %v: %v\n%s", args, err, out) + } + return strings.TrimSpace(string(out)) + } + commit := func(name, message string) string { + t.Helper() + if err := os.WriteFile(filepath.Join(scratch, name), []byte(name+"\n"), 0o644); err != nil { + t.Fatal(err) + } + gitIn("add", name) + gitIn("commit", "-q", "-m", message) + return gitIn("rev-parse", "HEAD") + } + gitIn("init", "-q", "-b", "main") + docsBase := commit("README.md", "docs base") + goChange := commit("change.go", "a Go change") + docsOnly := commit("NOTES.md", "a docs-only change") + // A new remote branch is compared against main's merge-base. + gitIn("update-ref", "refs/remotes/origin/main", docsBase) + + const zero = "0000000000000000000000000000000000000000" + for _, tc := range []struct { + name string + line string + ran bool + }{ + {"git push origin HEAD (existing branch)", "HEAD " + goChange + " refs/heads/x " + docsBase, true}, + {"git push origin HEAD:refs/heads/x (new branch)", "HEAD " + goChange + " refs/heads/x " + zero, true}, + {"git push origin x (branch-named local ref)", "refs/heads/x " + goChange + " refs/heads/x " + docsBase, true}, + {"docs-only push", "HEAD " + docsOnly + " refs/heads/x " + goChange, false}, + {"tag push by HEAD", "HEAD " + goChange + " refs/tags/t " + zero, false}, + {"tag push", "refs/tags/t " + goChange + " refs/tags/t " + zero, false}, + {"branch deletion", "refs/heads/x " + zero + " refs/heads/x " + goChange, false}, + } { + t.Run(tc.name, func(t *testing.T) { + if err := os.RemoveAll(suiteLog); err != nil { + t.Fatal(err) + } + command := exec.Command(bash, hook, "origin", "https://example.invalid/repo.git") + command.Dir = scratch + command.Env = append(os.Environ(), "PATH="+filepath.Dir(git)+":/bin:/usr/bin") + command.Stdin = strings.NewReader(tc.line + "\n") + if output, err := command.CombinedOutput(); err != nil { + t.Fatalf("pre-push hook on %q: %v\n%s", tc.line, err, output) + } + _, err := os.Stat(suiteLog) + ran := err == nil + if !ran && !errors.Is(err, os.ErrNotExist) { + t.Fatal(err) + } + if ran != tc.ran { + t.Errorf("suite ran = %v on %q, want %v", ran, tc.line, tc.ran) + } + }) + } +} + // fakeGoBuildDir returns a directory holding a stand-in `go` whose `build ... // -o OUT ...` copies the Bazel-built checker (BEADS_TEST_CHECK_VERSIONS) to // OUT, for check-versions.sh under Bazel, where there is no module checkout diff --git a/scripts/pre_push_suite_test.go b/scripts/pre_push_suite_test.go index 9e4a96703..172df23a3 100644 --- a/scripts/pre_push_suite_test.go +++ b/scripts/pre_push_suite_test.go @@ -141,7 +141,7 @@ func TestPrePushHookRunsSuiteOnlyForGoChanges(t *testing.T) { body := readPolicyFile(t, sourceRepoRoot(t), ".githooks/pre-push") for _, want := range []string{ `"$repo_root/scripts/pre-push-suite.sh" managed { t.Errorf(".githooks/pre-push must run the suite before the managed beads section") }