diff --git a/misc/git_hook/githook/HOOK_VERSION b/misc/git_hook/githook/HOOK_VERSION index b8626c4cff..7ed6ff82de 100644 --- a/misc/git_hook/githook/HOOK_VERSION +++ b/misc/git_hook/githook/HOOK_VERSION @@ -1 +1 @@ -4 +5 diff --git a/misc/git_hook/githook/largefiles.go b/misc/git_hook/githook/largefiles.go index ca12e5b5ca..4e53e67e9a 100644 --- a/misc/git_hook/githook/largefiles.go +++ b/misc/git_hook/githook/largefiles.go @@ -8,6 +8,7 @@ import ( "fmt" "os" "os/exec" + "slices" "strconv" "strings" ) @@ -20,8 +21,12 @@ const skipLargeFileCheckEnv = "TS_SKIP_LARGE_FILE_CHECK" // checkLargeBlobs rejects the push p if it adds or changes any blob // larger than maxSize bytes, comparing the tree being pushed against -// the remote's previous tree (or, for new refs, the merge base with the -// remote's default branch). The same tree diff logic runs in CI via the +// each available base tree: the remote's previous tree for the ref, +// and the merge base with the remote's default branch. A file is only +// flagged if it is a large addition relative to every base, so blobs +// that are already on the remote via its default branch (e.g. after +// rebasing past an unrelated large-file change on main) are not +// reported. The same tree diff logic runs in CI via the // check-git-accidental-large-file GitHub Action; this catches mistakes // before they permanently bloat the remote repo. func checkLargeBlobs(remoteName string, p push, maxSize int64) error { @@ -36,14 +41,22 @@ func checkLargeBlobs(remoteName string, p push, maxSize int64) error { if err != nil { return fmt.Errorf("resolving tree of %v: %v", p.localSHA, err) } - beforeTree := findBaseTree(remoteName, p) - if beforeTree == "" { + baseTrees := findBaseTrees(remoteName, p) + if len(baseTrees) == 0 { fmt.Fprintf(os.Stderr, "git-hook: pre-push: no base tree found for %s; skipping large file check\n", p.remoteRef) return nil } - large := appendLargeAdditions(nil, beforeTree, afterTree, "", maxSize) - if len(large) == 0 { - return nil + var large []largeFile + for i, baseTree := range baseTrees { + additions := appendLargeAdditions(nil, baseTree, afterTree, "", maxSize) + if i == 0 { + large = additions + } else { + large = intersectByPath(large, additions) + } + if len(large) == 0 { + return nil + } } var sb strings.Builder for _, f := range large { @@ -52,14 +65,20 @@ func checkLargeBlobs(remoteName string, p push, maxSize int64) error { return fmt.Errorf("push adds files larger than %d bytes:\n%sset %s=1 to push anyway", maxSize, sb.String(), skipLargeFileCheckEnv) } -// findBaseTree returns the tree hash to diff the push against, or the -// empty string if no suitable base is available locally. For updates to -// an existing remote ref it uses the remote's old commit. For new refs -// it falls back to the merge base with the remote's default branch. -func findBaseTree(remoteName string, p push) string { +// findBaseTrees returns the tree hashes to diff the push against, or +// nil if no suitable base is available locally. For updates to an +// existing remote ref it includes the remote's old commit. It also +// includes the merge base with the remote's default branch, which for +// new refs is the only base. +func findBaseTrees(remoteName string, p push) (trees []string) { + addTree := func(tree string) { + if !slices.Contains(trees, tree) { + trees = append(trees, tree) + } + } if p.remoteSHA != zeroRef { if tree, err := treeOf(p.remoteSHA); err == nil { - return tree + addTree(tree) } } for _, ref := range []string{ @@ -72,10 +91,26 @@ func findBaseTree(remoteName string, p push) string { continue } if tree, err := treeOf(strings.TrimSpace(string(out))); err == nil { - return tree + addTree(tree) + break } } - return "" + return trees +} + +// intersectByPath returns the entries of a whose paths also appear in b. +func intersectByPath(a, b []largeFile) []largeFile { + inB := make(map[string]bool, len(b)) + for _, f := range b { + inB[f.path] = true + } + var out []largeFile + for _, f := range a { + if inB[f.path] { + out = append(out, f) + } + } + return out } // treeOf resolves a git ref or commit to its tree hash. diff --git a/misc/git_hook/githook/largefiles_test.go b/misc/git_hook/githook/largefiles_test.go new file mode 100644 index 0000000000..1567679a0d --- /dev/null +++ b/misc/git_hook/githook/largefiles_test.go @@ -0,0 +1,110 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: BSD-3-Clause + +package githook + +import ( + "os" + "os/exec" + "strings" + "testing" +) + +// testMaxSize is the large-blob threshold used by the tests, chosen +// small so test fixtures stay tiny. +const testMaxSize = 100 + +func git(t *testing.T, args ...string) string { + t.Helper() + cmd := exec.Command("git", args...) + cmd.Env = append(cmd.Environ(), + "GIT_AUTHOR_NAME=test", "GIT_AUTHOR_EMAIL=test@example.com", + "GIT_COMMITTER_NAME=test", "GIT_COMMITTER_EMAIL=test@example.com", + ) + out, err := cmd.CombinedOutput() + if err != nil { + t.Fatalf("git %v: %v\n%s", args, err, out) + } + return strings.TrimSpace(string(out)) +} + +func writeAndCommit(t *testing.T, name, contents, msg string) string { + t.Helper() + if err := os.WriteFile(name, []byte(contents), 0o644); err != nil { + t.Fatal(err) + } + git(t, "add", "-A") + git(t, "commit", "-q", "-m", msg) + return git(t, "rev-parse", "HEAD") +} + +// setupRepo creates a scratch repo laying out the rebase scenario: +// +// c1 (initial) -- c2 (adds big.bin on main, origin/main) +// \ +// f1 (old branch tip, as previously pushed) +// +// It returns the three commit hashes. +func setupRepo(t *testing.T) (c1, c2, f1 string) { + t.Chdir(t.TempDir()) + git(t, "init", "-q", "-b", "main") + c1 = writeAndCommit(t, "small.txt", "hello", "initial") + + git(t, "checkout", "-q", "-b", "feature") + f1 = writeAndCommit(t, "feature.txt", "feature work", "feature commit") + + git(t, "checkout", "-q", "main") + c2 = writeAndCommit(t, "big.bin", strings.Repeat("x", 2*testMaxSize), "add big file on main") + git(t, "update-ref", "refs/remotes/origin/main", c2) + return c1, c2, f1 +} + +// TestCheckLargeBlobsRebasePastLargeFile verifies that pushing a +// branch rebased past an unrelated large-file change on main is not +// flagged: the large blob is already on the remote via main. +func TestCheckLargeBlobsRebasePastLargeFile(t *testing.T) { + t.Setenv(skipLargeFileCheckEnv, "") + _, _, f1 := setupRepo(t) + + git(t, "checkout", "-q", "feature") + git(t, "rebase", "-q", "main") + f2 := git(t, "rev-parse", "HEAD") + + err := checkLargeBlobs("origin", push{ + localRef: "refs/heads/feature", + localSHA: f2, + remoteRef: "refs/heads/feature", + remoteSHA: f1, + }, testMaxSize) + if err != nil { + t.Errorf("checkLargeBlobs = %v; want nil (big.bin is already on origin/main)", err) + } +} + +// TestCheckLargeBlobsNewLargeFile verifies that a genuinely new large +// blob on the branch is flagged, both for updates to an existing +// remote ref and for new refs. +func TestCheckLargeBlobsNewLargeFile(t *testing.T) { + t.Setenv(skipLargeFileCheckEnv, "") + _, _, f1 := setupRepo(t) + + git(t, "checkout", "-q", "feature") + git(t, "rebase", "-q", "main") + f2 := writeAndCommit(t, "mine.bin", strings.Repeat("y", 2*testMaxSize), "add my own big file") + + for _, remoteSHA := range []string{f1, zeroRef} { + err := checkLargeBlobs("origin", push{ + localRef: "refs/heads/feature", + localSHA: f2, + remoteRef: "refs/heads/feature", + remoteSHA: remoteSHA, + }, testMaxSize) + if err == nil { + t.Errorf("remoteSHA=%s: checkLargeBlobs = nil; want error flagging mine.bin", remoteSHA) + } else if !strings.Contains(err.Error(), "mine.bin") { + t.Errorf("remoteSHA=%s: error %v does not mention mine.bin", remoteSHA, err) + } else if strings.Contains(err.Error(), "big.bin") { + t.Errorf("remoteSHA=%s: error %v should not mention big.bin", remoteSHA, err) + } + } +}