Files
skillshare/scripts/hooks/git-e2e.sh
Rusty Shackleford 1a2389dea2 feat(hooks): manage Git config hooks (#310)
Git 2.54 added named config hooks, but keeping their registrations and
helper paths consistent across machines still required manual config
edits. Bring them into the same preview, sync, and recovery workflow as
Agent hooks.

- Global and project scope, with linked worktrees sharing the common dir
- Portable helper files, collision detection, and ownership tracking of
  generated files and include.path entries
- Backups and restore preserve unrelated settings and conditional includes
- Management never executes hooks; Git below 2.54 is reported as inactive
  while outputs are still generated
- CLI/API inventory, raw YAML editing in the dashboard, localized docs
2026-10-02 02:56:54 +08:00

135 lines
6.4 KiB
Bash

#!/usr/bin/env bash
# The only Git hook execution proof. Run in a fresh devcontainer ssenv, never on the host.
set -euo pipefail
HOOKS_WORKSPACE=$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd -P)
[[ "$HOOKS_WORKSPACE" == /workspace && -f /.dockerenv ]] || {
printf 'Git hook execution requires the /workspace devcontainer.\n' >&2; exit 1;
}
[[ -n "${SSENV_ACTIVE:-}" && -n "${SSENV_ORIG_HOME:-}" ]] || {
printf 'Enter a fresh ssenv before running this proof.\n' >&2; exit 1;
}
HOOKS_ENV_ROOT=${SSENV_ROOT:-$SSENV_ORIG_HOME/.ss-envs}
[[ "$(realpath "$HOME")" == "$(realpath "$HOOKS_ENV_ROOT/$SSENV_ACTIVE")" && "$HOME" != "$SSENV_ORIG_HOME" ]]
command -v python3 >/dev/null
command -v jq >/dev/null
HOOKS_BINARY=${HOOKS_BINARY:-/workspace/bin/skillshare}
[[ -x "$HOOKS_BINARY" ]]
HOOKS_VERSION=$(git version | sed -E 's/^git version ([0-9]+\.[0-9]+\.[0-9]+).*$/\1/')
[[ "$(printf '%s\n' 2.54.0 "$HOOKS_VERSION" | sort -V | head -1)" == 2.54.0 ]]
HOOKS_CASE=$(mktemp -d "$HOME/git-hooks-e2e.XXXXXX")
export XDG_CONFIG_HOME="$HOOKS_CASE/config" XDG_STATE_HOME="$HOOKS_CASE/state"
export GIT_CONFIG_GLOBAL="$HOOKS_CASE/gitconfig" GIT_CONFIG_NOSYSTEM=1
export SKILLSHARE_CONFIG="$HOOKS_CASE/skillshare.yaml" HOOKS_LOG="$HOOKS_CASE/log.jsonl"
unset GIT_DIR GIT_WORK_TREE GIT_COMMON_DIR GIT_INDEX_FILE GIT_OBJECT_DIRECTORY GIT_ALTERNATE_OBJECT_DIRECTORIES GIT_CONFIG_PARAMETERS GIT_CONFIG_COUNT
unset CLAUDE_CONFIG_DIR CODEX_HOME COPILOT_HOME PI_CODING_AGENT_DIR
while IFS= read -r HOOKS_ENV_KEY; do
unset "$HOOKS_ENV_KEY"
done < <(compgen -v | sed -n '/^GIT_CONFIG_\(KEY\|VALUE\)_/p')
printf 'targets: {}\n' > "$SKILLSHARE_CONFIG"
: > "$GIT_CONFIG_GLOBAL"
: > "$HOOKS_LOG"
git init -q --initial-branch=main "$HOOKS_CASE/repo"
git init -q --bare "$HOOKS_CASE/remote.git"
cd "$HOOKS_CASE/repo"
git config user.name 'Hook proof'
git config user.email 'hook-proof@example.invalid'
git config commit.gpgsign false
git config core.hooksPath .git/hooks
git remote add origin "$HOOKS_CASE/remote.git"
printf 'baseline\n' > tracked
git add tracked
git commit -qm baseline
# No script redirects stdin before the recorder reads it.
cat > "$HOOKS_CASE/entry.yaml" <<'YAML'
bindings:
git:
commands:
proof.commit: {events: [post-commit], command: "{files}/record config-commit"}
proof.push-a: {events: [pre-push], command: "{files}/record config-push-a"}
proof.push-b: {events: [pre-push], command: "{files}/record config-push-b"}
proof.rewrite: {events: [post-rewrite], command: "{files}/record config-rewrite"}
proof.verify: {events: [pre-commit], command: "{files}/record config-verify"}
files:
record: |
#!/bin/sh
exec python3 -c 'import json,os,sys; print(json.dumps({"label":sys.argv[1],"argv":sys.argv[2:],"stdin":sys.stdin.read()}),file=open(os.environ["HOOKS_LOG"],"a"))' "$@"
YAML
"$HOOKS_BINARY" hooks add proof --file "$HOOKS_CASE/entry.yaml" --sync -g >/dev/null
HOOKS_RECORDER="$XDG_CONFIG_HOME/git/skillshare/files/proof/record"
# These hookdir files are e2e fixtures, never generated by management.
printf '#!/bin/sh\nexec "%s" hookdir-commit "$@"\n' "$HOOKS_RECORDER" > .git/hooks/post-commit
printf '#!/bin/sh\nexec "%s" hookdir-push "$@"\n' "$HOOKS_RECORDER" > .git/hooks/pre-push
chmod +x .git/hooks/post-commit .git/hooks/pre-push
git commit -qm ordinary --allow-empty
git commit -qm amended --amend --allow-empty
git push -q origin main
HOOKS_VERIFY_BEFORE=$(grep -c '"label": "config-verify"' "$HOOKS_LOG")
git commit -qm skip-verify --allow-empty --no-verify
[[ "$(grep -c '"label": "config-verify"' "$HOOKS_LOG")" == "$HOOKS_VERIFY_BEFORE" ]]
# core.hooksPath is relative to each worktree; only global config hooks are asserted here.
git worktree add -qb linked "$HOOKS_CASE/linked"
(cd "$HOOKS_CASE/linked" && git commit -qm linked --allow-empty)
python3 - "$HOOKS_LOG" <<'PY'
import json, sys
rows = [json.loads(line) for line in open(sys.argv[1])]
labels = [r['label'] for r in rows]
assert labels.count('config-commit') == 4, labels
assert labels.count('config-verify') == 3, labels
for i, label in enumerate(labels):
if label == 'hookdir-commit':
assert labels[i-1] == 'config-commit', labels
push = [r for r in rows if 'push' in r['label']]
assert [r['label'] for r in push] == ['config-push-a', 'config-push-b', 'hookdir-push'], push
assert all(r['argv'][0] == 'origin' and len(r['argv']) == 2 for r in push), push
assert push[0]['stdin'] and all(r['stdin'] == push[0]['stdin'] for r in push), push
rewrite = [r for r in rows if r['label'] == 'config-rewrite']
assert len(rewrite) == 1 and rewrite[0]['argv'] == ['amend'], rewrite
assert len(rewrite[0]['stdin'].split()) == 2, rewrite
PY
# Portable Roborev wrappers intentionally do not forward Git's positional arguments.
mkdir -p "$HOOKS_CASE/bin"
cat > "$HOOKS_CASE/bin/roborev" <<'SH'
#!/bin/sh
exec python3 -c 'import json,os,sys; print(json.dumps({"label":"roborev","argv":sys.argv[1:],"stdin":sys.stdin.read()}),file=open(os.environ["HOOKS_LOG"],"a"))' "$@"
SH
chmod +x "$HOOKS_CASE/bin/roborev"
export PATH="$HOOKS_CASE/bin:$PATH"
cat > "$HOOKS_CASE/roborev.yaml" <<'YAML'
bindings:
git:
commands:
roborev.post-rewrite: {events: [post-rewrite], command: "{files}/post-rewrite"}
roborev.pre-push: {events: [pre-push], command: "{files}/pre-push"}
files:
post-rewrite: |
#!/bin/sh
PATH="$PATH:/opt/homebrew/bin:/home/linuxbrew/.linuxbrew/bin:/usr/local/bin"
command -v roborev >/dev/null 2>&1 || exit 0
exec roborev remap --quiet 2>/dev/null
pre-push: |
#!/bin/sh
PATH="$PATH:/opt/homebrew/bin:/home/linuxbrew/.linuxbrew/bin:/usr/local/bin"
command -v roborev >/dev/null 2>&1 || exit 0
roborev post-commit --flush-push 2>/dev/null || true
YAML
"$HOOKS_BINARY" hooks add roborev --file "$HOOKS_CASE/roborev.yaml" --sync -g >/dev/null
git commit -qm stub-amended --amend --allow-empty
git push -q --force origin main
python3 - "$HOOKS_LOG" <<'PY'
import json, sys
rows = [json.loads(line) for line in open(sys.argv[1])]
calls = [r for r in rows if r['label'] == 'roborev']
assert [r['argv'] for r in calls] == [['remap', '--quiet'], ['post-commit', '--flush-push']], calls
rewrite = [r for r in rows if r['label'] == 'config-rewrite'][-1]
push = [r for r in rows if r['label'] == 'config-push-a'][-1]
assert calls[0]['stdin'] == rewrite['stdin'] and calls[0]['stdin'], calls
assert calls[1]['stdin'] == push['stdin'] and calls[1]['stdin'], calls
PY
printf '{"case":"git-config-execution","passed":true,"evidence":"%s"}\n' "$HOOKS_CASE"