summaryrefslogtreecommitdiff
path: root/plugins/security-guidance/hooks/gitutil.py
diff options
context:
space:
mode:
authorMohamed Hegazy <mhegazy@anthropic.com>2026-10-07 08:40:40 -0700
committerMohamed Hegazy <mhegazy@anthropic.com>2026-10-07 08:40:40 -0700
commitf711194a9ecdf004b655dbc5006610fbb9e7c7bb (patch)
tree8684355bbc2e54fa4332077788b780aafd492390 /plugins/security-guidance/hooks/gitutil.py
parentbfe21649b9ccdbc67599d6647ff139a4c34e4458 (diff)
security-guidance: remove stash indexes older releases left in the git directorymhegazy/sg-reclaim-after-hook-kill
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
Diffstat (limited to 'plugins/security-guidance/hooks/gitutil.py')
-rw-r--r--plugins/security-guidance/hooks/gitutil.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/plugins/security-guidance/hooks/gitutil.py b/plugins/security-guidance/hooks/gitutil.py
index 5c4d2327..2036b45e 100644
--- a/plugins/security-guidance/hooks/gitutil.py
+++ b/plugins/security-guidance/hooks/gitutil.py
@@ -211,6 +211,41 @@ def _sweep_stale_indexes(dirpath, uid):
pass
+def _sweep_stale_stash_indexes(real_index):
+ """Delete `<index>.stash.<pid>` files a killed `git stash create` left
+ next to the real index. Releases before 2.0.10 ran it there, so a clone
+ can hold hundreds, each the size of the index, and nothing else removes
+ them. Only our own regular files past the age limit go: git keeps one for
+ seconds. `index.lock` is never touched; a live git may hold it."""
+ import stat
+ import time
+ uid = os.getuid() if hasattr(os, "getuid") else None
+ cutoff = time.time() - _TEMP_INDEX_STALE_S
+ deadline = time.monotonic() + _SWEEP_BUDGET_S
+ dirpath = os.path.dirname(real_index)
+ stale = re.compile(re.escape(os.path.basename(real_index)) + r"\.stash\.\d+$")
+ try:
+ names = os.listdir(dirpath)
+ except OSError:
+ return
+ for name in names:
+ if not stale.match(name):
+ continue
+ p = os.path.join(dirpath, name)
+ try:
+ st = os.lstat(p)
+ if not stat.S_ISREG(st.st_mode):
+ continue
+ if uid is not None and st.st_uid != uid:
+ continue
+ if max(st.st_mtime, st.st_ctime) < cutoff:
+ os.unlink(p)
+ if time.monotonic() > deadline:
+ return
+ except OSError:
+ pass
+
+
# Index copies this process has made and not yet removed.
_live_copies = set()
@@ -360,6 +395,7 @@ def _temp_index(cwd, untracked_paths=None):
yield None
return
+ _sweep_stale_stash_indexes(real_index)
tmpdir = _hook_tmpdir()
try:
index_bytes = os.path.getsize(real_index)