Files
tailscale/misc/git_hook/git-hook.go
Brad Fitzpatrick 8c98d2a417 misc/git_hook: reject pushes that add large files
Add a large blob check to the pre-push hook, using the same git tree
diff logic as corp's check-file-size CI workflow (the
check-git-accidental-large-file GitHub Action): diff the pushed tree
against the remote's old tree (or the merge base with the remote's
default branch for new refs) and reject any new or changed blob over
1.5 MB. Unlike the CI check, which only guards PRs into main, the hook
runs before pushing to any branch, catching mistakes before they
permanently bloat the remote repo.

Set TS_SKIP_LARGE_FILE_CHECK=1 to push a large file intentionally,
mirroring the skip-large-file-check commit message tag honored by CI.

This folds the go.mod replace check and the new check into a single
CheckPrePush entry point so both share one read of the hook's stdin;
corp's git-hook.go needs the matching call site update when it next
bumps its tailscale.com dependency.

Updates tailscale/corp#9863

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
Change-Id: I1c8cf2a277ce854d45c0ea809bed7c06b3295374
2026-07-24 08:22:19 -07:00

71 lines
1.9 KiB
Go

// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
// The git-hook command is Tailscale's git hook binary, built and
// installed under .git/hooks/ts-git-hook-bin by the launcher at
// .git/hooks/ts-git-hook. misc/add-git-hooks.go writes the initial
// launcher; subsequent HOOK_VERSION bumps trigger self-rebuilds.
//
// # Adding your own hooks
//
// To add your own hook alongside one we already hook, create an executable
// file .git/hooks/<hook-name>.local (e.g. pre-commit.local). It runs after
// the built-in hook succeeds.
package main
import (
"fmt"
"log"
"os"
"strings"
"tailscale.com/misc/git_hook/githook"
)
// maxPushBlobSize is the largest new or changed blob allowed in a
// push. It matches the 1.5 MB limit enforced by the check-file-size CI
// workflow. Set TS_SKIP_LARGE_FILE_CHECK=1 to override.
const maxPushBlobSize = 1_500_000
var pushRemotes = []string{
"git@github.com:tailscale/tailscale",
"git@github.com:tailscale/tailscale.git",
"https://github.com/tailscale/tailscale",
"https://github.com/tailscale/tailscale.git",
}
// hooks are the hook names this binary handles. Used by install to
// write per-hook wrappers; must stay in sync with the dispatcher below.
var hooks = []string{"pre-commit", "commit-msg", "pre-push"}
func main() {
log.SetFlags(0)
if len(os.Args) < 2 {
return
}
cmd, args := os.Args[1], os.Args[2:]
var err error
switch cmd {
case "version":
fmt.Print(strings.TrimSpace(githook.HookVersion) + ":0")
case "install":
err = githook.WriteHooks(hooks)
case "pre-commit":
err = githook.CheckForbiddenMarkers()
case "commit-msg":
err = githook.AddChangeID(args)
case "pre-push":
err = githook.CheckPrePush(args, githook.PrePushConfig{
WatchedRemotes: pushRemotes,
MaxBlobSize: maxPushBlobSize,
})
}
if err != nil {
log.Fatalf("git-hook: %v: %v", cmd, err)
}
if err := githook.RunLocalHook(cmd, args); err != nil {
log.Fatalf("git-hook: %v", err)
}
}