hemsby commited on
Commit
1e6f50f
Β·
1 Parent(s): 57d9df5

gates: sync to final DECODE hardening pattern

Browse files

Same shape as prior ports. Brings this branch up to PBI_team_utils/master
(post-fix set including stage-invariant reviewer-hash).

Hardening files byte-identical. Kleo's videoextraction_app "## This
repo" section preserved: Streamlit + HF Spaces deploy, supported video
formats, no-video-material commit rule.

Reviewer subagent ran before commit and reported clean.

.claude/hooks/block-dangerous-git.sh CHANGED
@@ -25,11 +25,14 @@ if [ -z "$COMMAND" ]; then
25
  exit 0
26
  fi
27
 
28
- # Detect tier
 
 
 
29
  TIER="data"
30
- if [ -f "package.json" ] && [ ! -f "pyproject.toml" ] && [ ! -f "setup.py" ]; then
31
  TIER="product"
32
- elif [ -f "pom.xml" ] || [ -f "build.gradle" ] || [ -f "build.gradle.kts" ]; then
33
  TIER="product"
34
  fi
35
 
@@ -43,9 +46,14 @@ BLOCKED_ALL=(
43
  "git[[:space:]]+push[[:space:]]+-f([[:space:]]|$)"
44
  "git[[:space:]]+reset[[:space:]]+--hard"
45
  "git[[:space:]]+clean[[:space:]]+-[a-z]*f"
46
- "git[[:space:]]+branch[[:space:]]+-D"
47
- "(^|[^a-zA-Z0-9_/])rm[[:space:]]+-[a-z]*r[a-z]*f[a-z]*[[:space:]]+/(\s|$)"
48
- "(^|[^a-zA-Z0-9_/])rm[[:space:]]+-[a-z]*r[a-z]*f[a-z]*[[:space:]]+~/?(\s|$)"
 
 
 
 
 
49
  )
50
 
51
  # Patterns blocked only in PRODUCT tier
@@ -64,7 +72,7 @@ Command: $COMMAND
64
 
65
  Destructive operations require explicit user sign-off. If the user asked
66
  for this, the model should surface the command in chat for confirmation
67
- before invoking the tool. See decode-claude-wiki/docs/rules for rule 08.
68
  EOF
69
  exit 2
70
  fi
 
25
  exit 0
26
  fi
27
 
28
+ # Detect tier. Use CLAUDE_PROJECT_DIR if available so a subdirectory cwd
29
+ # can't silently downgrade a product-tier repo to data-tier by missing
30
+ # package.json in the current working directory.
31
+ TIER_BASE="${CLAUDE_PROJECT_DIR:-$PWD}"
32
  TIER="data"
33
+ if [ -f "$TIER_BASE/package.json" ] && [ ! -f "$TIER_BASE/pyproject.toml" ] && [ ! -f "$TIER_BASE/setup.py" ]; then
34
  TIER="product"
35
+ elif [ -f "$TIER_BASE/pom.xml" ] || [ -f "$TIER_BASE/build.gradle" ] || [ -f "$TIER_BASE/build.gradle.kts" ]; then
36
  TIER="product"
37
  fi
38
 
 
46
  "git[[:space:]]+push[[:space:]]+-f([[:space:]]|$)"
47
  "git[[:space:]]+reset[[:space:]]+--hard"
48
  "git[[:space:]]+clean[[:space:]]+-[a-z]*f"
49
+ # branch delete-with-force: -D shorthand, combined short flags,
50
+ # longform pairs, and mixed-order combinations.
51
+ "git[[:space:]]+branch[[:space:]]+-[a-zA-Z]*D"
52
+ "git[[:space:]]+branch[[:space:]]+-[a-zA-Z]*(df|fd)[a-zA-Z]*([[:space:]]|$)"
53
+ "git[[:space:]]+branch[[:space:]]+.*(-d[[:space:]]|--delete[[:space:]]).*(-f([[:space:]]|$)|--force([[:space:]]|$))"
54
+ "git[[:space:]]+branch[[:space:]]+.*(-f[[:space:]]|--force[[:space:]]).*(-d([[:space:]]|$)|--delete([[:space:]]|$))"
55
+ "(^|[^a-zA-Z0-9_/])rm[[:space:]]+-[a-z]*r[a-z]*f[a-z]*[[:space:]]+/([[:space:]]|$)"
56
+ "(^|[^a-zA-Z0-9_/])rm[[:space:]]+-[a-z]*r[a-z]*f[a-z]*[[:space:]]+~/?([[:space:]]|$)"
57
  )
58
 
59
  # Patterns blocked only in PRODUCT tier
 
72
 
73
  Destructive operations require explicit user sign-off. If the user asked
74
  for this, the model should surface the command in chat for confirmation
75
+ before invoking the tool.
76
  EOF
77
  exit 2
78
  fi
.claude/hooks/block-env-writes.sh CHANGED
@@ -25,7 +25,7 @@ BASENAME=$(basename "$FILE_PATH")
25
 
26
  # Denylist patterns (exact basename or glob-style)
27
  case "$BASENAME" in
28
- .env|.env.local|.env.*|credentials|credentials.*|*.pem|*.key|*.pfx|id_rsa|id_rsa.*|service-account.json)
29
  cat >&2 <<EOF
30
  BLOCKED: write to '$FILE_PATH' denied.
31
 
 
25
 
26
  # Denylist patterns (exact basename or glob-style)
27
  case "$BASENAME" in
28
+ .env|.env.local|.env.*|credentials|credentials.*|*.pem|*.key|*.pfx|id_rsa|id_rsa.*|*service-account*.json)
29
  cat >&2 <<EOF
30
  BLOCKED: write to '$FILE_PATH' denied.
31
 
.claude/hooks/ensure-setup.sh ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env bash
2
+ #
3
+ # DECODE SessionStart hook.
4
+ # Ensures .githooks is wired to git via core.hooksPath so the commit-msg
5
+ # and pre-commit gates fire on every commit β€” whether invoked from
6
+ # Claude Code or the terminal.
7
+ #
8
+ # Runs once per Claude Code session start. Idempotent. Silent on success;
9
+ # warns (non-blocking) if setup.sh fails so the user can debug.
10
+
11
+ set -euo pipefail
12
+
13
+ if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
14
+ exit 0
15
+ fi
16
+
17
+ cd "$(git rev-parse --show-toplevel)"
18
+
19
+ if [ ! -x "scripts/setup.sh" ]; then
20
+ exit 0
21
+ fi
22
+
23
+ CURRENT=$(git config --local --get core.hooksPath 2>/dev/null || true)
24
+
25
+ if [ "$CURRENT" = ".githooks" ]; then
26
+ # Already wired to our hooks β€” nothing to do.
27
+ exit 0
28
+ fi
29
+
30
+ if [ -n "$CURRENT" ]; then
31
+ # core.hooksPath is set to something other than .githooks. Don't
32
+ # silently overwrite β€” warn the user and exit. Our hardening gates
33
+ # (commit-msg, pre-commit) will NOT fire from their current path.
34
+ echo >&2 "[ensure-setup] WARN: core.hooksPath is set to '$CURRENT', not '.githooks'."
35
+ echo >&2 "[ensure-setup] This repo's commit-msg and pre-commit gates will not fire."
36
+ echo >&2 "[ensure-setup] Either unset it (\`git config --unset core.hooksPath\`) or"
37
+ echo >&2 "[ensure-setup] make sure '$CURRENT' routes to the hooks in .githooks/."
38
+ exit 0
39
+ fi
40
+
41
+ # core.hooksPath is unset β€” wire it via setup.sh.
42
+ if ! bash scripts/setup.sh >/dev/null 2>&1; then
43
+ echo >&2 "[ensure-setup] WARN: scripts/setup.sh failed β€” git hooks may not be wired."
44
+ echo >&2 "[ensure-setup] Run 'bash scripts/setup.sh' manually to debug."
45
+ fi
46
+
47
+ exit 0
.claude/hooks/reviewer-stop.sh CHANGED
@@ -15,12 +15,13 @@ if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
15
  exit 0
16
  fi
17
 
18
- cd "$(git rev-parse --show-toplevel)"
 
19
 
20
- PATTERNS='\.(py|ts|tsx|js|jsx|R|r|java|sql)$|^scripts/|^output/|^dist/'
21
 
22
  MODIFIED=$( {
23
- git diff --name-only --diff-filter=ACMRD HEAD 2>/dev/null || true
24
  git ls-files --others --exclude-standard 2>/dev/null || true
25
  git diff --cached --name-only --diff-filter=ACMR 2>/dev/null || true
26
  } | sort -u )
@@ -30,15 +31,17 @@ if ! echo "$MODIFIED" | grep -qE "$PATTERNS"; then
30
  fi
31
 
32
  # Marker format (one line): <HEAD-SHA> <DIFF-HASH> <ISO-8601-UTC-timestamp>
33
- # DIFF-HASH = git hash-object --stdin < (git diff HEAD). Binding to the
34
- # diff content β€” not just HEAD β€” means any edit made after the reviewer
35
- # cleared invalidates the marker.
36
- MARKER=".decode-reviewer-clean"
37
- if [ -f "$MARKER" ]; then
 
 
38
  MARKED_SHA=$(awk 'NR==1 {print $1}' "$MARKER" 2>/dev/null || echo "")
39
  MARKED_DIFF=$(awk 'NR==1 {print $2}' "$MARKER" 2>/dev/null || echo "")
40
  CURRENT_SHA=$(git rev-parse HEAD 2>/dev/null || echo "initial")
41
- CURRENT_DIFF=$(git diff HEAD 2>/dev/null | git hash-object --stdin 2>/dev/null || echo "")
42
  if [ -n "$MARKED_SHA" ] && [ -n "$MARKED_DIFF" ] \
43
  && [ "$MARKED_SHA" = "$CURRENT_SHA" ] \
44
  && [ "$MARKED_DIFF" = "$CURRENT_DIFF" ]; then
 
15
  exit 0
16
  fi
17
 
18
+ REPO_ROOT="$(git rev-parse --show-toplevel)"
19
+ cd "$REPO_ROOT"
20
 
21
+ PATTERNS='\.(py|ts|tsx|js|jsx|R|r|java|sql)$|^scripts/'
22
 
23
  MODIFIED=$( {
24
+ git diff --name-only --diff-filter=ACMR HEAD 2>/dev/null || true
25
  git ls-files --others --exclude-standard 2>/dev/null || true
26
  git diff --cached --name-only --diff-filter=ACMR 2>/dev/null || true
27
  } | sort -u )
 
31
  fi
32
 
33
  # Marker format (one line): <HEAD-SHA> <DIFF-HASH> <ISO-8601-UTC-timestamp>
34
+ # DIFF-HASH is computed by scripts/reviewer-hash.sh, which covers both
35
+ # tracked diffs (git diff HEAD) AND untracked file contents. Binding to
36
+ # the hash means any edit made after the reviewer cleared β€” whether to
37
+ # tracked OR untracked files β€” invalidates the marker.
38
+ MARKER="$REPO_ROOT/.decode-reviewer-clean"
39
+ HASH_SCRIPT="$REPO_ROOT/scripts/reviewer-hash.sh"
40
+ if [ -f "$MARKER" ] && [ -x "$HASH_SCRIPT" ]; then
41
  MARKED_SHA=$(awk 'NR==1 {print $1}' "$MARKER" 2>/dev/null || echo "")
42
  MARKED_DIFF=$(awk 'NR==1 {print $2}' "$MARKER" 2>/dev/null || echo "")
43
  CURRENT_SHA=$(git rev-parse HEAD 2>/dev/null || echo "initial")
44
+ CURRENT_DIFF=$(bash "$HASH_SCRIPT" 2>/dev/null || echo "")
45
  if [ -n "$MARKED_SHA" ] && [ -n "$MARKED_DIFF" ] \
46
  && [ "$MARKED_SHA" = "$CURRENT_SHA" ] \
47
  && [ "$MARKED_DIFF" = "$CURRENT_DIFF" ]; then
.claude/settings.json CHANGED
@@ -19,6 +19,8 @@
19
  "Bash(git diff)",
20
  "Bash(git diff:*)",
21
  "Bash(git rev-parse HEAD)",
 
 
22
  "Bash(git log)",
23
  "Bash(git log:*)",
24
  "Bash(git status)",
@@ -59,6 +61,16 @@
59
  }
60
  ]
61
  }
 
 
 
 
 
 
 
 
 
 
62
  ]
63
  }
64
  }
 
19
  "Bash(git diff)",
20
  "Bash(git diff:*)",
21
  "Bash(git rev-parse HEAD)",
22
+ "Bash(git hash-object:*)",
23
+ "Bash(bash scripts/reviewer-hash.sh)",
24
  "Bash(git log)",
25
  "Bash(git log:*)",
26
  "Bash(git status)",
 
61
  }
62
  ]
63
  }
64
+ ],
65
+ "SessionStart": [
66
+ {
67
+ "hooks": [
68
+ {
69
+ "type": "command",
70
+ "command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/ensure-setup.sh"
71
+ }
72
+ ]
73
+ }
74
  ]
75
  }
76
  }
.githooks/commit-msg CHANGED
@@ -33,7 +33,7 @@ fi
33
 
34
  # ----- Detect substantive changes -----
35
  STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACMR || true)
36
- SUBSTANTIVE_PATTERN='\.(py|ts|tsx|js|jsx|R|r|java|sql)$|^scripts/|^output/|^dist/'
37
  SUBSTANTIVE_FILES=""
38
  if [ -n "$STAGED_FILES" ]; then
39
  SUBSTANTIVE_FILES=$(echo "$STAGED_FILES" | grep -E "$SUBSTANTIVE_PATTERN" || true)
@@ -56,15 +56,17 @@ fi
56
 
57
  # ----- Reviewer marker check -----
58
  # Marker format (one line): <HEAD-SHA> <DIFF-HASH> <ISO-8601-UTC-timestamp>
59
- # where DIFF-HASH = git hash-object --stdin < (git diff HEAD).
60
- # Binding to the diff content β€” not just HEAD β€” prevents edits made
61
- # after the reviewer cleared from silently bypassing the gate.
 
62
  MARKER="$REPO_ROOT/.decode-reviewer-clean"
63
- if [ -f "$MARKER" ]; then
 
64
  MARKED_SHA=$(awk 'NR==1 {print $1}' "$MARKER" 2>/dev/null || echo "")
65
  MARKED_DIFF=$(awk 'NR==1 {print $2}' "$MARKER" 2>/dev/null || echo "")
66
  CURRENT_HEAD=$(git rev-parse HEAD 2>/dev/null || echo "initial")
67
- CURRENT_DIFF=$(git diff HEAD 2>/dev/null | git hash-object --stdin 2>/dev/null || echo "")
68
  if [ -n "$MARKED_SHA" ] && [ -n "$MARKED_DIFF" ] \
69
  && [ "$MARKED_SHA" = "$CURRENT_HEAD" ] \
70
  && [ "$MARKED_DIFF" = "$CURRENT_DIFF" ]; then
@@ -90,7 +92,6 @@ To proceed, ONE of:
90
  git commit -m "... [override-reviewer: quick doc typo]"
91
  Overrides are visible forever in \`git log --grep="override-reviewer"\`.
92
 
93
- See decode-claude-wiki/docs/rules for rule 03 (reviewer required).
94
  ================================================================
95
  EOF
96
  exit 1
 
33
 
34
  # ----- Detect substantive changes -----
35
  STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACMR || true)
36
+ SUBSTANTIVE_PATTERN='\.(py|ts|tsx|js|jsx|R|r|java|sql)$|^scripts/'
37
  SUBSTANTIVE_FILES=""
38
  if [ -n "$STAGED_FILES" ]; then
39
  SUBSTANTIVE_FILES=$(echo "$STAGED_FILES" | grep -E "$SUBSTANTIVE_PATTERN" || true)
 
56
 
57
  # ----- Reviewer marker check -----
58
  # Marker format (one line): <HEAD-SHA> <DIFF-HASH> <ISO-8601-UTC-timestamp>
59
+ # where DIFF-HASH is computed by scripts/reviewer-hash.sh and covers both
60
+ # tracked diffs (git diff HEAD) AND untracked file contents. Binding to
61
+ # the hash prevents edits made after the reviewer cleared β€” whether to
62
+ # tracked or untracked files β€” from silently bypassing the gate.
63
  MARKER="$REPO_ROOT/.decode-reviewer-clean"
64
+ HASH_SCRIPT="$REPO_ROOT/scripts/reviewer-hash.sh"
65
+ if [ -f "$MARKER" ] && [ -x "$HASH_SCRIPT" ]; then
66
  MARKED_SHA=$(awk 'NR==1 {print $1}' "$MARKER" 2>/dev/null || echo "")
67
  MARKED_DIFF=$(awk 'NR==1 {print $2}' "$MARKER" 2>/dev/null || echo "")
68
  CURRENT_HEAD=$(git rev-parse HEAD 2>/dev/null || echo "initial")
69
+ CURRENT_DIFF=$(bash "$HASH_SCRIPT" 2>/dev/null || echo "")
70
  if [ -n "$MARKED_SHA" ] && [ -n "$MARKED_DIFF" ] \
71
  && [ "$MARKED_SHA" = "$CURRENT_HEAD" ] \
72
  && [ "$MARKED_DIFF" = "$CURRENT_DIFF" ]; then
 
92
  git commit -m "... [override-reviewer: quick doc typo]"
93
  Overrides are visible forever in \`git log --grep="override-reviewer"\`.
94
 
 
95
  ================================================================
96
  EOF
97
  exit 1
.github/workflows/gate.yml CHANGED
@@ -45,21 +45,35 @@ jobs:
45
 
46
  override-audit:
47
  runs-on: ubuntu-latest
48
- if: github.event_name == 'pull_request'
49
  steps:
50
  - uses: actions/checkout@v4
51
  with:
52
  fetch-depth: 0
53
- - name: Report override-reviewer usage in PR commits
54
  env:
 
55
  BASE_REF: ${{ github.base_ref }}
 
56
  run: |
57
  set -euo pipefail
58
- OVERRIDES=$(git log --format='%H %s' "origin/${BASE_REF}"..HEAD | grep -E '\[override-reviewer:' || true)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  if [ -n "$OVERRIDES" ]; then
60
- echo "This PR contains reviewer overrides:"
61
  echo "$OVERRIDES"
62
- # Non-blocking β€” just surface it
63
  fi
64
 
65
  ci-fail-incident:
 
45
 
46
  override-audit:
47
  runs-on: ubuntu-latest
 
48
  steps:
49
  - uses: actions/checkout@v4
50
  with:
51
  fetch-depth: 0
52
+ - name: Report override-reviewer usage
53
  env:
54
+ EVENT_NAME: ${{ github.event_name }}
55
  BASE_REF: ${{ github.base_ref }}
56
+ BEFORE_SHA: ${{ github.event.before }}
57
  run: |
58
  set -euo pipefail
59
+ if [ "$EVENT_NAME" = "pull_request" ]; then
60
+ RANGE="origin/${BASE_REF}..HEAD"
61
+ LABEL="PR"
62
+ else
63
+ if [ -n "${BEFORE_SHA:-}" ] && [ "$BEFORE_SHA" != "0000000000000000000000000000000000000000" ]; then
64
+ RANGE="${BEFORE_SHA}..HEAD"
65
+ elif git rev-parse HEAD~1 >/dev/null 2>&1; then
66
+ RANGE="HEAD~1..HEAD"
67
+ else
68
+ # root commit with no parent
69
+ RANGE="HEAD"
70
+ fi
71
+ LABEL="push"
72
+ fi
73
+ OVERRIDES=$(git log --format='%H %s' "$RANGE" 2>/dev/null | grep -E '\[override-reviewer:' || true)
74
  if [ -n "$OVERRIDES" ]; then
75
+ echo "This $LABEL contains reviewer overrides:"
76
  echo "$OVERRIDES"
 
77
  fi
78
 
79
  ci-fail-incident:
CLAUDE.md CHANGED
@@ -23,7 +23,7 @@ Triggered by the Stop hook when the session modified substantive files and no fr
23
  1. Run `git diff HEAD` via Bash.
24
  2. Dispatch `feature-dev:code-reviewer` via the Agent tool. Pass the diff as context. Instruct: review only the changes, target latency under 90 seconds, report only blocking issues (confidence β‰₯ 80).
25
  3. When the reviewer returns:
26
- - **No blocking issues** β†’ compute `HEAD_SHA=$(git rev-parse HEAD)` and `DIFF_HASH=$(git diff HEAD | git hash-object --stdin)`, then Write `.decode-reviewer-clean` with exactly one line: `<HEAD-SHA> <DIFF-HASH> <ISO-8601-UTC-timestamp>`.
27
  - **Blocking issues** β†’ Write `.decode-reviewer-findings.md` with the list. Do NOT write `.decode-reviewer-clean`.
28
  4. Return.
29
 
@@ -62,14 +62,14 @@ Use it after a change already works and before commit when you want a final pass
62
 
63
  ## First-time setup in this repo
64
 
65
- If this is the first time you (or anyone) is committing in this repo after the hardening PR merged, run:
 
 
66
 
67
  ```bash
68
  bash scripts/setup.sh
69
  ```
70
 
71
- This wires `.githooks/pre-commit` into git via `core.hooksPath`. One-time per clone.
72
-
73
  ## Destructive operations
74
 
75
  Destructive git operations (`push --force`, `reset --hard`, `clean -f`, `branch -D`, etc.) are blocked by the `block-dangerous-git.sh` PreToolUse hook. If you genuinely need one, ask the user to run it manually after explicit chat confirmation.
 
23
  1. Run `git diff HEAD` via Bash.
24
  2. Dispatch `feature-dev:code-reviewer` via the Agent tool. Pass the diff as context. Instruct: review only the changes, target latency under 90 seconds, report only blocking issues (confidence β‰₯ 80).
25
  3. When the reviewer returns:
26
+ - **No blocking issues** β†’ compute `HEAD_SHA=$(git rev-parse HEAD)` and `DIFF_HASH=$(bash scripts/reviewer-hash.sh)`, then Write `.decode-reviewer-clean` with exactly one line: `<HEAD-SHA> <DIFF-HASH> <ISO-8601-UTC-timestamp>`. The hash helper covers tracked-file diffs AND untracked file contents; do not substitute a plain `git diff | git hash-object` pipe β€” it misses untracked files and lets create-file β†’ pass-review β†’ silent-edit bypass the marker.
27
  - **Blocking issues** β†’ Write `.decode-reviewer-findings.md` with the list. Do NOT write `.decode-reviewer-clean`.
28
  4. Return.
29
 
 
62
 
63
  ## First-time setup in this repo
64
 
65
+ Setup runs automatically on Claude Code session start via the `ensure-setup.sh` SessionStart hook β€” it wires `.githooks` into git (`core.hooksPath`) so the commit-msg and pre-commit gates fire. Idempotent; no-ops once wired.
66
+
67
+ If you commit from a plain terminal in a fresh clone without ever opening a Claude Code session, the hooks will not fire until you run setup manually:
68
 
69
  ```bash
70
  bash scripts/setup.sh
71
  ```
72
 
 
 
73
  ## Destructive operations
74
 
75
  Destructive git operations (`push --force`, `reset --hard`, `clean -f`, `branch -D`, etc.) are blocked by the `block-dangerous-git.sh` PreToolUse hook. If you genuinely need one, ask the user to run it manually after explicit chat confirmation.
scripts/reviewer-hash.sh ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
28
+ REPO_ROOT="$(git rev-parse --show-toplevel 2>/dev/null || pwd)"
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