pub const SHIM: &str = #!/bin/sh
# git-templates hook shim → the greengate binary.
#
# One shim serves every hook: it passes its own filename through, so the binary
# knows which hook to run. Dispatchers (pre-commit, pre-push) and ported leaf
# hooks use the identical file.
# Author: https://github.com/fredericrous
#
# POSIX sh, not zsh: this must run wherever git does, including Git for Windows
# (which ships sh but not zsh).
#
# Resolution order matters more than it looks. Git hooks do NOT inherit an
# interactive shell's PATH: GUI clients (VS Code, Tower, JetBrains) launch git
# with an environment that often lacks ~/.local/bin or ~/.cargo/bin. A shim that
# only did `exec greengate` would work in the terminal and fail in the GUI. So:
# 1. $GIT_HOOKS_BIN — escape hatch; also how `make test` points at target/debug
# 2. the baked path — an absolute path written into this shim by
# `greengate install`. The placeholder token is NOT
# spelled out in this comment: substitution is a plain
# global replace, so a comment containing the token gets
# rewritten too, and the explanation turns into a machine
# path. It did, for as long as this said otherwise.
# ABSOLUTE, and checked for it below before it is used.
# Git runs a hook with the working tree as the current
# directory, so testing an UNSUBSTITUTED token with
# `[ -x ]` asks the REPOSITORY whether it has a file by
# that name — and a clone shipping an executable so
# named would then be run here, on the first commit,
# before any check and before any trust decision. The
# unbaked shim is a supported state (3 below), so the
# value has to be rejected outright rather than asked
# about on disk.
# 3. $HOME/.local/bin — the default install location, resolved at RUNTIME so
# this template stays machine-agnostic. Without it, a
# repo created by `git init` from a template dir that is
# the git checkout itself (the usual setup here — the
# XDG path symlinks to the repo) never gets that token
# substituted and would depend on PATH after all.
# greengate first, then its old name amont, the alias
# a 2.x install left; each with and without .exe:
# Windows builds greengate.exe, and `[ -x .../greengate ]`
# is false for it — which left
# Windows with PATH as its ONLY fallback, in exactly the
# GUI-client case this list exists to survive. Windows
# needs no symlink for any of this: point
# init.templateDir straight at the checkout.
# 4. PATH — last resort, greengate then amont
# and if none resolve, FAIL LOUDLY. Never skip a check silently: a hook that
# quietly does nothing is worse than one that is missing.
set -e
HOOKS_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
HOOK_NAME=$(basename -- "$0")
# The baked path, and nothing relative — see 2 above. Absolute means POSIX `/…`
# or a Windows drive path: `greengate install` bakes `C:\Users\…\greengate.exe` on
# Git for Windows. An unbaked shim fails this and falls through to 3, which is
# exactly what it should do.
BAKED="__GREENGATE_BIN__"
case "$BAKED" in
/*|?:/*|?:\\*) ;;
*) BAKED= ;;
esac
if [ -n "$GIT_HOOKS_BIN" ] && [ -x "$GIT_HOOKS_BIN" ]; then
BIN="$GIT_HOOKS_BIN"
elif [ -n "$BAKED" ] && [ -x "$BAKED" ]; then
BIN="$BAKED"
elif [ -x "$HOME/.local/bin/greengate" ]; then
BIN="$HOME/.local/bin/greengate"
elif [ -x "$HOME/.local/bin/greengate.exe" ]; then
BIN="$HOME/.local/bin/greengate.exe"
elif [ -x "$HOME/.local/bin/amont" ]; then
BIN="$HOME/.local/bin/amont"
elif [ -x "$HOME/.local/bin/amont.exe" ]; then
BIN="$HOME/.local/bin/amont.exe"
elif command -v greengate > /dev/null 2>&1; then
BIN=greengate
elif command -v amont > /dev/null 2>&1; then
BIN=amont
else
printf ' \033[38;5;160m✗\033[0m greengate binary not found — hooks cannot run.\n' >&2
printf ' Looked at: $GIT_HOOKS_BIN, the baked path, ~/.local/bin, then PATH.\n' >&2
printf ' Reinstall it the way it arrived:\n' >&2
printf ' npm/pnpm repo: \033[38;5;208mnpm install\033[0m (prepare re-bakes the hooks)\n' >&2
printf ' curl installer: \033[38;5;208mcurl -fsSL https://raw.githubusercontent.com/greenharness/greengate/main/install/install.sh | sh\033[0m\n' >&2
printf ' cargo / brew: \033[38;5;208mcargo install greengate\033[0m / \033[38;5;208mbrew install fredericrous/tap/greengate\033[0m\n' >&2
exit 1
fi
exec "$BIN" --hooks-dir "$HOOKS_DIR" "$HOOK_NAME" "$@"
;Expand description
The one shim. All four git-invoked hooks are the same file — it passes its
own filename through — and shims_on_disk_match_the_embedded_one keeps the
repository’s templates/hooks/ honest against this copy.
The canonical text lives INSIDE this crate, and that is a packaging
constraint rather than a preference. It used to be
include_str!("../../../templates/hooks/pre-commit"), reaching up to the
repository root — which works in a checkout and cannot work in a published
crate, because cargo package tars up this directory and nothing above it.
The tarball compiled nowhere: couldn't read src/../../../templates/hooks/ pre-commit. crates.io is immutable, so that would have been a broken
release that could only be yanked, never fixed in place.
The repository’s templates/hooks/ still holds the four installable copies
— that directory IS the product for anyone pointing init.templateDir at a
clone, and it has to be real files rather than symlinks because Git for
Windows materialises those as text files containing a path.