Files
tailscale/misc/git_hook/githook/launcher.sh
T
Brad Fitzpatrick 1a3a1da16f misc/git_hook/githook: don't rebuild hooks after every OSS bump
The launcher found the shared HOOK_VERSION via `go list -m -f
'{{.Dir}}' tailscale.com`. But go list -m doesn't download modules, so
after a repo bumps its tailscale.com dependency to a version not yet in
the module cache, Dir is empty, SHARED_VER falls back to 0, and the
launcher rebuilds ts-git-hook-bin even though the shared HOOK_VERSION
didn't change. In corp, which can sometimes bump OSS a few times a
day, that meant a spurious "git-hook: rebuilding ts-git-hook-bin..."
(plus a module download) on the first git operation after most bumps.

If Dir is empty, run `go mod download tailscale.com` and look again.
The rebuild was going to download the module anyway.

Bump HOOK_VERSION so existing installs pick up the new launcher.

Updates tailscale/corp#40381

Change-Id: I6e4720702d7052df3b15dce0c60a8b313c6b48e3
Signed-off-by: Brad Fitzpatrick <[email protected]>
2026-11-25 08:54:24 -08:00

60 lines
1.9 KiB
Bash
Executable File

#!/usr/bin/env bash
# ts-git-hook launcher (installed at .git/hooks/ts-git-hook).
#
# Written by misc/add-git-hooks.go from the canonical copy embedded
# in tailscale.com/misc/git_hook/githook. On every invocation it:
#
# 1. Compares the binary's reported version against the shared
# githook HOOK_VERSION (resolved via `go list -m tailscale.com`)
# plus the repo-local HOOK_VERSION.
# 2. If stale or missing: rebuilds ts-git-hook-bin and runs
# `ts-git-hook-bin install`.
# 3. Execs the binary with the hook's args.
set -euo pipefail
REPO_ROOT="$(git rev-parse --show-toplevel 2>/dev/null)" || {
echo "git-hook: not in a git repo" >&2
exit 1
}
HOOK_DIR="$(git -C "$REPO_ROOT" rev-parse --git-path hooks)"
case "$HOOK_DIR" in
/*) ;;
*) HOOK_DIR="$REPO_ROOT/$HOOK_DIR" ;;
esac
# Windows (Git for Windows / MSYS2) needs .exe suffixes.
EXE=""
case "$(uname -s)" in MINGW* | MSYS* | CYGWIN*) EXE=".exe" ;; esac
BINARY="$HOOK_DIR/ts-git-hook-bin$EXE"
GO="$REPO_ROOT/tool/go$EXE"
if [ ! -x "$GO" ]; then GO=go; fi
oss_dir() {
(cd "$REPO_ROOT" && GOWORK=off "$GO" list -m -f '{{.Dir}}' tailscale.com 2>/dev/null) || true
}
OSS_DIR="$(oss_dir)"
if [ -z "$OSS_DIR" ]; then
# go list -m doesn't download; a fresh go.mod bump may not be cached yet.
(cd "$REPO_ROOT" && GOWORK=off "$GO" mod download tailscale.com >/dev/null 2>&1) || true
OSS_DIR="$(oss_dir)"
fi
SHARED_VER="$(cat "$OSS_DIR/misc/git_hook/githook/HOOK_VERSION" 2>/dev/null || echo 0)"
LOCAL_VER="$(cat "$REPO_ROOT/misc/git_hook/HOOK_VERSION" 2>/dev/null || echo 0)"
WANT="$SHARED_VER:$LOCAL_VER"
HAVE="$("$BINARY" version 2>/dev/null || echo none)"
if [ "$WANT" != "$HAVE" ]; then
echo "git-hook: rebuilding ts-git-hook-bin..." >&2
(cd "$REPO_ROOT" && GOWORK=off "$GO" build -o "$BINARY" ./misc/git_hook) || {
echo "git-hook: rebuild failed, run: ./tool/go run ./misc/add-git-hooks.go" >&2
exit 1
}
"$BINARY" install
fi
exec "$BINARY" "$@"