summaryrefslogtreecommitdiff
path: root/plugins/security-guidance/hooks/diffstate.py
diff options
context:
space:
mode:
authorMohamed Hegazy <mhegazy@anthropic.com>2026-10-05 09:53:54 -0700
committerMohamed Hegazy <mhegazy@anthropic.com>2026-10-05 09:53:54 -0700
commita22217bb450a6cc00d7a161566e7cc7d96dbf63a (patch)
treed805eb1ed3ec8a89ea6466bad1da7e8007bae3db /plugins/security-guidance/hooks/diffstate.py
parentd182ca456ca09d31d139f7d3818d1d333b103cce (diff)
security-guidance: leave nothing behind when git is killed (2.0.9 → 2.0.10)mhegazy/sg-temp-index-cleanup
With a very large git index the hook's git calls hit their time limits and are killed, so git cannot clean up. Each kill could leave an index-sized file in the temp directory, index.stash.<pid> files in .git, and a dead .git/index.lock that fails the user's next git add or commit. - `git stash create` and the work-tree diff run on a throwaway index copy. - Every git the hook spawns gets GIT_OPTIONAL_LOCKS=0. - Copies live in a private per-user folder and are removed together with the files git writes next to them. - Leftovers older than 30 minutes are swept, including existing ones. Co-Authored-By: Claude <noreply@anthropic.com>
Diffstat (limited to 'plugins/security-guidance/hooks/diffstate.py')
-rw-r--r--plugins/security-guidance/hooks/diffstate.py26
1 files changed, 19 insertions, 7 deletions
diff --git a/plugins/security-guidance/hooks/diffstate.py b/plugins/security-guidance/hooks/diffstate.py
index 8d0665dd..be839212 100644
--- a/plugins/security-guidance/hooks/diffstate.py
+++ b/plugins/security-guidance/hooks/diffstate.py
@@ -13,7 +13,7 @@ from _base import debug_log, _PV
from gitutil import (
GIT_CMD,
_git_dir, _git_toplevel, _git_status_porcelain,
- _git_rev_parse_head, _is_ancestor, _git_name_only,
+ _git_rev_parse_head, _is_ancestor, _git_name_only, _temp_index,
)
from session_state import with_locked_state
@@ -188,6 +188,13 @@ def capture_git_baseline(cwd):
Falls back to HEAD if the working tree is clean.
Returns the SHA string, or None if not in a git repo or if the repo has no commits.
+ `stash create` runs on a throwaway copy of the index (GIT_INDEX_FILE), so
+ its `index.lock` and `index.stash.<pid>` files land next to the copy, not
+ in `.git`: it cannot collide with a concurrent git command, and a timeout
+ kill cannot leave a lock that blocks the user's next one.
+ GIT_OPTIONAL_LOCKS does not cover stash. Copying the real index without a
+ lock is safe: git replaces it by atomic rename.
+
NOTE: `git stash create` does NOT capture untracked files. UPS pairs this
SHA with a `_list_untracked()` snapshot stored as `untracked_at_baseline`,
and `compute_v2_review_set` subtracts that set so pre-existing untracked
@@ -209,11 +216,16 @@ def capture_git_baseline(cwd):
debug_log("No commits in repo, skipping baseline capture")
return None
- result = subprocess.run(
- [*GIT_CMD, "stash", "create"],
- cwd=cwd, capture_output=True, timeout=15
- )
- sha = (result.stdout or b"").decode("utf-8", errors="replace").strip()
+ sha = ""
+ # untracked_paths=[]: a plain copy of the index, no `add -N` pass.
+ with _temp_index(cwd, untracked_paths=[]) as env:
+ # env is None when there is no index file to copy: treat as clean.
+ if env is not None:
+ result = subprocess.run(
+ [*GIT_CMD, "stash", "create"],
+ cwd=cwd, capture_output=True, timeout=15, env=env
+ )
+ sha = (result.stdout or b"").decode("utf-8", errors="replace").strip()
if sha:
return sha
@@ -450,7 +462,7 @@ def compute_v2_review_set(cwd, baseline_sha, head_at_capture, untracked_at_basel
dirty_now |= _git_name_only(repo, f"{head_at_capture}..HEAD") or set()
diff_base = head_at_capture
- # changed_since: tracked files vs the stash baseline (no temp index — the
+ # changed_since: tracked files vs the stash baseline (no `add -N` — the
# stash never contained untracked files anyway), then union with
# currently-untracked. The previous `include_untracked=True` arm cost a
# full `git add -N .` (slow in large repos) per call to surface