hemsby commited on
Commit
c612276
·
1 Parent(s): c4fa9c5

fix(gates): make reviewer-hash stage-invariant

Browse files

Carryover from PBI_team_utils 5d75a2ad. Identical helper; see that
commit for the full rationale (stage-invariant union-of-changes hash
+ core.quotepath=false for space/non-ASCII paths).

Reviewer pass ran on the canonical commit in PBI_team_utils; this
port is a byte-identical file copy.

Files changed (1) hide show
  1. scripts/reviewer-hash.sh +28 -15
scripts/reviewer-hash.sh CHANGED
@@ -1,21 +1,27 @@
1
  #!/usr/bin/env bash
2
  #
3
- # DECODE reviewer hash — single source of truth for the DIFF_HASH captured
4
- # in the .decode-reviewer-clean marker.
5
  #
6
  # Used identically by:
7
  # - the reviewer procedure (Claude writing the marker, per CLAUDE.md)
8
  # - the Stop hook (.claude/hooks/reviewer-stop.sh)
9
  # - the commit-msg hook (.githooks/commit-msg)
10
  #
11
- # Captures both tracked-file diffs (via `git diff HEAD`) AND the contents
12
- # of untracked files (via `git ls-files --others --exclude-standard`).
13
- # Any edit to either surface invalidates the marker.
 
14
  #
15
- # Why this exists: the original design hashed only `git diff HEAD`, which
16
- # excludes untracked files. That left an attack path — agent creates a
17
- # new file, reviewer passes, agent silently edits the new file, marker
18
- # still validates. This helper closes the gap.
 
 
 
 
 
19
 
20
  set -euo pipefail
21
 
@@ -23,11 +29,18 @@ REPO_ROOT="$(git rev-parse --show-toplevel 2>/dev/null || pwd)"
23
  cd "$REPO_ROOT"
24
 
25
  {
26
- git diff HEAD 2>/dev/null || true
27
- git ls-files --others --exclude-standard 2>/dev/null | while IFS= read -r f; do
28
- [ -f "$f" ] || continue
29
- printf 'UNTRACKED %s\n' "$f"
30
- cat "$f"
31
- printf '\n'
 
 
 
 
 
 
 
32
  done
33
  } | git hash-object --stdin
 
1
  #!/usr/bin/env bash
2
  #
3
+ # DECODE reviewer hash (stage-invariant) — single source of truth for the
4
+ # DIFF_HASH captured in the .decode-reviewer-clean marker.
5
  #
6
  # Used identically by:
7
  # - the reviewer procedure (Claude writing the marker, per CLAUDE.md)
8
  # - the Stop hook (.claude/hooks/reviewer-stop.sh)
9
  # - the commit-msg hook (.githooks/commit-msg)
10
  #
11
+ # Produces the same hash regardless of whether changes are staged or
12
+ # unstaged. That means "run reviewer, write marker, then git add, then
13
+ # commit" works correctly — the hash the commit-msg hook computes matches
14
+ # the hash the reviewer wrote into the marker.
15
  #
16
+ # Why the old implementation was stage-dependent: it hashed
17
+ # `git diff HEAD` (which doesn't include untracked files) plus untracked
18
+ # file contents (via ls-files --others). A new file would move between
19
+ # those two sections on `git add`, changing the text fed to hash-object
20
+ # even though the actual content was identical.
21
+ #
22
+ # This version takes the UNION of files-differing-from-HEAD and
23
+ # untracked-non-ignored, iterates that set in sorted order, and emits
24
+ # uniform framing for each file — stage-invariant by construction.
25
 
26
  set -euo pipefail
27
 
 
29
  cd "$REPO_ROOT"
30
 
31
  {
32
+ # core.quotepath=false keeps paths with spaces / non-ASCII from being
33
+ # emitted as "quoted\\040path" which the while-read loop can't test/cat.
34
+ { git -c core.quotepath=false diff --name-only HEAD 2>/dev/null || true
35
+ git -c core.quotepath=false ls-files --others --exclude-standard 2>/dev/null || true
36
+ } | sort -u | while IFS= read -r f; do
37
+ [ -n "$f" ] || continue
38
+ if [ -f "$f" ]; then
39
+ printf 'FILE %s\n' "$f"
40
+ cat "$f"
41
+ printf '\n'
42
+ else
43
+ printf 'DELETED %s\n' "$f"
44
+ fi
45
  done
46
  } | git hash-object --stdin