File size: 1,806 Bytes
2eee090
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/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