decode_tools / scripts /reviewer-hash.sh
hemsby's picture
fix(gates): make reviewer-hash stage-invariant
c612276
Raw History Blame Contribute Delete
1.81 kB
#!/usr/bin/env bash
#
# DECODE reviewer hash (stage-invariant) — single source of truth for the
# DIFF_HASH captured in the .decode-reviewer-clean marker.
#
# Used identically by:
# - the reviewer procedure (Claude writing the marker, per CLAUDE.md)
# - the Stop hook (.claude/hooks/reviewer-stop.sh)
# - the commit-msg hook (.githooks/commit-msg)
#
# Produces the same hash regardless of whether changes are staged or
# unstaged. That means "run reviewer, write marker, then git add, then
# commit" works correctly — the hash the commit-msg hook computes matches
# the hash the reviewer wrote into the marker.
#
# Why the old implementation was stage-dependent: it hashed
# `git diff HEAD` (which doesn't include untracked files) plus untracked
# file contents (via ls-files --others). A new file would move between
# those two sections on `git add`, changing the text fed to hash-object
# even though the actual content was identical.
#
# This version takes the UNION of files-differing-from-HEAD and
# untracked-non-ignored, iterates that set in sorted order, and emits
# uniform framing for each file — stage-invariant by construction.
set -euo pipefail
REPO_ROOT="$(git rev-parse --show-toplevel 2>/dev/null || pwd)"
cd "$REPO_ROOT"
{
# core.quotepath=false keeps paths with spaces / non-ASCII from being
# emitted as "quoted\\040path" which the while-read loop can't test/cat.
{ git -c core.quotepath=false diff --name-only HEAD 2>/dev/null || true
git -c core.quotepath=false ls-files --others --exclude-standard 2>/dev/null || true
} | sort -u | while IFS= read -r f; do
[ -n "$f" ] || continue
if [ -f "$f" ]; then
printf 'FILE %s\n' "$f"
cat "$f"
printf '\n'
else
printf 'DELETED %s\n' "$f"
fi
done
} | git hash-object --stdin