summaryrefslogtreecommitdiff
path: root/plugins/security-guidance/hooks/gitutil.py
AgeCommit message (Collapse)Author
6 dayssecurity-guidance: skip other repositories whose settings change what git runsmhegazy/sg-other-repo-settingsClaude
The hooks run git in the session's repository and in those the session moves to. A repository other than the session's own is now skipped when its own config has settings that change what git runs. An index copy made in another repository holds only the entries for files. Co-authored-by: mhegazy <mhegazy@anthropic.com>
7 daysMerge remote-tracking branch 'origin/main' into mhegazy/sg-commit-diff-memoryClaude
Co-authored-by: mhegazy <mhegazy@anthropic.com> # Conflicts: # plugins/security-guidance/hooks/security_reminder_hook.py
9 dayssecurity-guidance: read commit and push diffs without holding themClaude
The commit and push reviews captured, decoded and split the whole `git show -p` / `git diff` before looking at the number of files. An import of 5,800 files and 4.6 M lines took 6.4 GB, and the review was then skipped for its file count. Read git's output as it arrives and keep what the review uses: the characters of each file the prompt takes, a count of the added lines after them, and past the file cut-off only the file names. Co-authored-by: mhegazy <mhegazy@anthropic.com>
9 dayssecurity-guidance: leave renamed key lists out of the reviewmhegazy/sg-renamed-into-codeClaude
A text file whose name or folder says it holds secrets (api_keys.txt, tokens.txt, secrets/prod.md) was read whole when it was renamed to a code name. It now stays out of the review, and so does a file that recent commits renamed from such a name. Co-authored-by: mhegazy <mhegazy@anthropic.com>
12 dayssecurity-guidance: review text files renamed to code namesClaude
git shows no content for a renamed file, so a text or document file renamed to a code name (notes.txt to payload.py) was never read by the commit and push reviews. They now read it whole, behind the files they already review and within a small share of the prompt. Other renames are reviewed as before. Part of #4852 Co-authored-by: mhegazy <mhegazy@anthropic.com>
12 daysKeep git out of folders without a .git entry on older git versionsmhegazy/sg-ordinary-checkoutsClaude
Git before 2.38 has no safe.bareRepository. Check before the prompt and Stop hooks run git, following the same search git uses to find a repository, and let a work tree kept elsewhere through core.worktree through. Co-authored-by: mhegazy <mhegazy@anthropic.com>
13 dayssecurity-guidance: only run git in ordinary checkoutsClaude
Treat a folder as a repository to run git in only when it has its own .git entry or the caller named the repository through GIT_DIR, and set safe.bareRepository=explicit on the hook's git commands. Linked worktrees and ordinary checkouts work as before. Co-authored-by: mhegazy <mhegazy@anthropic.com>
14 daysMerge pull request #7047 from anthropics/mhegazy/sg-push-merge-baseMohamed Hegazy
security-guidance: push review of a merge reviews what the push added
2026-11-09security-guidance: push review of a merge reviews what the push addedClaude
The reviewed prefix could end on the merged-in branch, so the diff held everything the first parent has that the branch lacks. Keep the base on HEAD's first-parent line. A merge whose history is all reviewed is sent as its combined diff, so only its conflict resolutions and hand edits are read. Co-authored-by: mhegazy <mhegazy@anthropic.com>
2026-11-07security-guidance: count commits that no commit hook sawmhegazy/sg-unseen-commit-countMohamed Hegazy
A commit review starts from a hook on the Bash tool, so a commit made any other way starts none. The Stop row now carries turn_commits: how many commits the turn made, how many have no review, and how many of those no commit hook saw. Count only; what is reviewed does not change, and no hook entry is added. Co-authored-by: mhegazy <mhegazy@anthropic.com>
2026-11-05Merge main into mhegazy/sg-index-copy-all-gitlinksmhegazy/sg-index-copy-all-gitlinksClaude
Co-authored-by: mhegazy <mhegazy@anthropic.com>
2026-11-05Merge pull request #6835 from anthropics/mhegazy/sg-secret-rename-pairingMohamed Hegazy
security-guidance: keep a renamed file paired with its old name in the Stop review
2026-11-05Merge pull request #6922 from anthropics/mhegazy/sg-prompt-snapshotMohamed Hegazy
security-guidance: take the turn baseline without git stash create
2026-11-05Merge pull request #6919 from anthropics/mhegazy/sg-add-failure-fixMohamed Hegazy
security-guidance: keep new files in the Stop review when git add -N fails
2026-11-05Merge pull request #7002 from anthropics/mhegazy/sg-git-log-signature-offMohamed Hegazy
security-guidance: read git log output without signature checks
2026-11-04security-guidance: name new files the Stop review could not seeMohamed Hegazy
When git add -N fails on both tries or times out, wake Claude once per set of files with their names, so the review no longer ends as "no changes". A timed-out add now keeps the tracked files' diff, so a timeout on every turn cannot stop their reviews. Private object directory cleanup no longer chmods through a link. Fixes #6996 Co-authored-by: mhegazy <mhegazy@anthropic.com>
2026-11-04security-guidance: drop every gitlink from the index copyMohamed Hegazy
Whether git starts something in a gitlink's directory is decided when git runs, so the check for a .git there beforehand can go stale. Remove all of them from the copy. On git older than 2.34 the baseline for a submodule that is not checked out falls back to HEAD. Co-authored-by: mhegazy <mhegazy@anthropic.com>
2026-11-04security-guidance: start git by its full path on Windowsmhegazy/sg-git-program-from-pathMohamed Hegazy
A bare program name on Windows is looked up in the working directory before PATH, and the hooks run in the project. Name the first git.exe on PATH, leaving out relative entries, the working directory and folders inside the project, as the claude lookup already avoids that search. Co-authored-by: mhegazy <mhegazy@anthropic.com>
2026-11-04security-guidance: read git log output without signature checksmhegazy/sg-git-log-signature-offMohamed Hegazy
With log.showSignature set, git log and git show verify every signed commit, which adds lines to the output the hooks parse and starts gpg.program. Set it off for the hooks' git and for the commit reviewer's environment. Co-authored-by: mhegazy <mhegazy@anthropic.com>
2026-10-27security-guidance: take the turn baseline without git stash createmhegazy/sg-prompt-snapshotMohamed Hegazy
stash create checks the work tree on one thread. add -u into the throwaway index copy, write-tree and commit-tree produce the same commit and use git's index preload instead, which matters where each file status is slow. See #6854.
2026-10-27security-guidance: keep new files in the Stop review when git add -N failsMohamed Hegazy
Retry the add with a private object directory for users who cannot write the object store. Drop the pathspec settings git refuses alongside literal pathspecs, which hid new files since 2.0.16. When a failure stays and nothing is reviewed, keep the files for a later turn.
2026-10-27Merge remote-tracking branch 'origin/main' into mhegazy/sg-secret-rename-pairingmhegazy/sg-secret-rename-pairingMohamed Hegazy
# Conflicts: # plugins/security-guidance/hooks/diffstate.py
2026-10-26Merge pull request #6861 from anthropics/mhegazy/sg-add-fail-metricMohamed Hegazy
security-guidance: count failed git add -N calls in the Stop review
2026-10-23security-guidance: count failed git add -N calls in the Stop reviewmhegazy/sg-add-fail-metricMohamed Hegazy
A failed add leaves new files out of the diff with no sign of it. Measure only; behaviour is unchanged.
2026-10-22security-guidance: report where a slow prompt's time goesmhegazy/sg-prompt-capture-timeMohamed Hegazy
Prompts that wait a second or more in the UserPromptSubmit hook send one metrics row: total, baseline and untracked listing time, index size, and whether the repository is on a Windows drive under WSL. Measures only; capture is unchanged. See #6854.
2026-10-22Merge remote-tracking branch 'origin/main' into mhegazy/sg-secret-rename-pairingMohamed Hegazy
2026-10-21Merge branch 'main' into mhegazy/sg-index-copy-gitlinksClaude
2026-10-20security-guidance: keep nested repositories out of the index copyClaude
The hooks run `git stash create` (the turn baseline) and `git add --intent-to-add` (untracked files) on a throwaway copy of the index. A nested repository that the index records is a gitlink entry there. `git stash create` takes no --ignore-submodules option and `git add` reads the gitlinks its paths cover, so both started a second git inside that directory. Before anything runs on the copy, remove the gitlink entry of every nested repository from the copy, never from the real index. If that removal fails, do not use the copy at all: the baseline falls back to HEAD and runs no stash create, and the diff reads the real index with --ignore-submodules=all. A gitlink whose directory has no `.git` (a submodule that is not checked out) stays; git starts nothing there. Run the intent-to-add `git add` with literal pathspecs, in that one call's environment only. The index file is scanned for a gitlink's mode bytes first, so a repository without one starts no extra git process.
2026-10-20security-guidance: keep a renamed file paired with its old name in the Stop ↵Mohamed Hegazy
review The Stop review limits its diff to the changed paths. For a rename that git already knows about (git mv, or a rename committed during the turn) that list held only the new name, so the diff showed a new file and the parsers could not apply the rule that leaves out a file renamed away from a secret-shaped name. The old name now goes into the list beside the new one, as it already did for a plain mv. The file listings themselves are unchanged.
2026-10-20Merge branch 'main' into mhegazy/sg-stop-pathspecMohamed Hegazy
The batched path commands now read each diff through the bounded reader that main added, so both changes apply to get_git_diff.
2026-10-20Merge pull request #6808 from anthropics/mhegazy/sg-secret-named-telemetryMohamed Hegazy
security-guidance: count secret-named data files in the Stop review set
2026-10-20Merge pull request #6807 from anthropics/mhegazy/sg-stop-diff-memoryMohamed Hegazy
security-guidance: read the Stop review's git diff without holding it whole
2026-10-20Merge pull request #6721 from anthropics/mhegazy/sg-claude-code-credential-filesMohamed Hegazy
security-guidance: keep Claude Code's own credential files out of reviews
2026-10-20Merge pull request #6810 from anthropics/mhegazy/sg-stop-baseline-checkoutMohamed Hegazy
security-guidance: do not review against a baseline from another checkout
2026-10-20Merge main into mhegazy/sg-stop-diff-memorymhegazy/sg-stop-diff-memoryMohamed Hegazy
Conflict in get_git_diff: main added IGNORE_SUBMODULES to the diff command on the line where this branch resets its read counters. Both are kept.
2026-10-19security-guidance: stay out of repositories nested in the work treemhegazy/sg-nested-reposClaude
A directory with its own .git inside the work tree (a submodule, a vendored checkout, an unpacked archive) was added to the throwaway index as a submodule entry, and git then started a second git inside it to see whether it had changed. Nothing in such a directory is reviewed. git status and work-tree git diff now ignore submodule entries, an untracked nested repository is no longer listed as a changed file or added to the index copy, and a commit that moves a submodule is always shown as its two commit ids.
2026-10-19security-guidance: keep added lines past the cut, hold the diff timeoutMohamed Hegazy
A reviewable file past the per-file cap now keeps as much again of its added lines, so a rewritten large file still shows what is new, and its diff ends with a line saying it was cut. The read runs on a thread so the 30 s limit holds while a process git started keeps the pipe open; a read that breaks is an error instead of a short diff. SG_MAX_FILE_DIFF_BYTES of zero or less means the default. The parser and the reader share one test for which files are reviewed.
2026-10-19security-guidance: do not review against a baseline from another checkoutmhegazy/sg-stop-baseline-checkoutMohamed Hegazy
The turn baseline (stash SHA, HEAD, untracked snapshot) is kept per session, and the Stop review used it in whatever checkout the turn ended in. After the session entered another worktree, that worktree's older commits looked like commits made this turn and its files were diffed against the first checkout's, so code committed long before was reviewed and reported again (anthropics/claude-code#102567). The prompt hook now stores the repo root and the prompt time beside the baseline. When the Stop review runs in a different checkout it leaves the baseline out and uses what that checkout can say for itself: its own HEAD when the turn began (from its reflog) for the commit range, HEAD for file contents, and file times to leave out uncommitted files last written before the turn. A baseline taken after a finding records the checkout it was taken in. A prompt in a folder that is not a repo no longer replaces the untracked snapshot of the kept baseline with an empty one, which made every untracked file in the repo look new at the next Stop. Stop rows carry `bl_origin`: 0 same checkout, 1 another worktree, 2 another repository, 3 a folder that is gone, 4 no baseline; plus 10 or 20 when the baseline is an earlier turn's.
2026-10-19security-guidance: count secret-named data files in the Stop review setmhegazy/sg-secret-named-telemetryMohamed Hegazy
Data files named like secrets.yml or client_secret.json are reviewed like any other file. The Stop hook now reports how many are in its review set, as a count only (secret_named_reviewed, emitted when nonzero), so the share of reviews that include one can be read from telemetry. No change to what is reviewed. Refs anthropics/claude-code#96276
2026-10-19security-guidance: read the Stop review's git diff without holding it wholeMohamed Hegazy
The Stop review captured the whole diff of the turn before its per-file caps applied, so a turn that wrote large text files cost the hook several times their size in memory. The output is now read as it arrives: files the review drops keep only their header line, and one file's diff is cut at 4 MiB (SG_MAX_FILE_DIFF_BYTES). The git command is unchanged. Adds count-only metrics, nonzero only: diff_mb, diff_cut_files, diff_timeout.
2026-10-18security-guidance: keep secret renames out and report the right skip when ↵Mohamed Hegazy
paths do not fit one command When the changed paths are too long for one git command: - secret-shaped paths gone from the work tree go into every batch, so a rename away from such a name is still paired and its content stays out of the review - the batch size follows the system's own argument limit when it is lower - giving up is reported as skip 8 and a set with no source files as skip 7, instead of 6 (no changes) MAX_REVIEW_SET_PATHS counts only reviewable paths, read from the repo root, so an unignored dependency directory does not hide an edited source file.
2026-10-18Merge remote-tracking branch 'origin/main' into mhegazy/sg-stop-pathspecMohamed Hegazy
2026-10-16security-guidance: keep Claude Code's own credential files out of reviewsmhegazy/sg-claude-code-credential-filesMohamed Hegazy
When the Claude Code config directory is a git repo, .credentials.json and .claude.json show up as changed files each time Claude Code rewrites them, and their contents (sign-in tokens, a saved API key, MCP server secrets) were sent to the Stop review. Treat both names as secret-shaped, like .netrc and .npmrc. Refs anthropics/claude-code#90010
2026-10-15Merge remote-tracking branch 'origin/main' into mhegazy/sg-diff-prefix-configmhegazy/sg-diff-prefix-configMohamed Hegazy
2026-10-15security-guidance: keep secret-shaped files out of reviews (#6537)Mohamed Hegazy
* security-guidance: keep secret-shaped files out of reviews The Stop review covers every dirty file in the work tree, including ones the user changed and Claude never opened. Key files, credential files and encrypted dotfile sources were sent along with the code. Skip files that are secret stores by naming convention: .env variants, SSH private keys, common credential files, and chezmoi private/encrypted sources. * security-guidance: report how many secret-shaped files were skipped The Stop hook now reports secret_shaped_skipped (a count, never names, only when nonzero) so the skip can be confirmed after release and watched for over-matching.
2026-10-13security-guidance: keep the Stop review working when the changed paths do ↵Mohamed Hegazy
not fit one command The Stop review passed every changed path to `git diff` as arguments. With enough of them (an unignored cache, a repo at $HOME) the OS refused to start git, the diff failed twice, and the turn's real edits were never reviewed. When the OS refuses the argument list, keep only the paths the review can use (reviewable source files) and send them in a few commands under the same time limit. A path list that fits one command is handled exactly as before. Skip review sets of more than 100,000 paths before any diff. Fixes #4824
2026-10-13security-guidance: read diffs the same whatever the user's prefix settingsMohamed Hegazy
With diff.mnemonicPrefix or diff.noprefix set in the user's git config, diff headers no longer read `a/<path> b/<path>`, the parser matched no file, and the review was skipped with nothing shown. mnemonicPrefix switched off the Stop review; noprefix switched off the commit and push reviews as well. Pass the default prefix settings on every git call the hook makes. Fixes #4569
2026-10-12security-guidance: count diff headers the parser could not readmhegazy/sg-diff-parsingMohamed Hegazy
A file whose header cannot be parsed is left out of the review without a trace. The Stop hook now reports diff_hdr_unparsed (a count, only when nonzero) so a header shape the parser misses shows up in metrics.
2026-10-12security-guidance: parse diff sections by header lineMohamed Hegazy
The diff parser split on the text 'diff --git ' anywhere, so a file whose own content contains it was cut short, and the rest of that file was never reviewed. It also dropped files whose names git quotes (a double quote, backslash or control character in the name). Split only where a line starts with the header, and decode quoted names.
2026-10-07security-guidance: remove stash indexes older releases left in the git directorymhegazy/sg-reclaim-after-hook-killMohamed Hegazy
Releases before 2.0.10 ran `git stash create` on the real index. When that git was killed, it left an `index.stash.<pid>` file the size of the index in the git directory, and nothing removes those: a clone can hold hundreds. Before it copies the index, the hook now deletes such files once they are older than 30 minutes. It only touches regular files of the current user whose name is exactly `index.stash.<digits>`, and never `index.lock`. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QR7edfw5HHkxJvJVgZiWpM