tailscale/misc/git_hook/git-hook.go
Fernando Serboncini 514d7d28e7
misc/git_hook: extract shared githook package; auto-rebuild on version bump (#19440)
Pull the hook logic into a reusable githook library package so
tailscale/corp can share it via a thin wrapper main instead of
keeping a forked copy in sync.

The install flow also changes: a wrapper scripts now build the
binary and reinstall the git hooks. Pulling new shared code no
longer requires re-running the installer.

Updates tailscale/corp#39860

Change-Id: I4d606d11c8c883015c190c54e3387a7f9fe4dd32

Signed-off-by: Fernando Serboncini <fserb@tailscale.com>
2026-04-17 16:24:39 -04:00

67 lines
1.7 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/install-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 (
_ "embed"
"fmt"
"log"
"os"
"strings"
"tailscale.com/misc/git_hook/githook"
)
//go:embed HOOK_VERSION
var compiledHookVersion string
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(compiledHookVersion))
case "install":
err = githook.WriteHooks(hooks)
case "pre-commit":
err = githook.CheckForbiddenMarkers()
case "commit-msg":
err = githook.AddChangeID(args)
case "pre-push":
err = githook.CheckGoModReplaces(args, pushRemotes, nil)
}
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)
}
}