diff options
| author | Mohamed Hegazy <mhegazy@anthropic.com> | 2026-10-07 08:40:40 -0700 |
|---|---|---|
| committer | Mohamed Hegazy <mhegazy@anthropic.com> | 2026-10-07 08:40:40 -0700 |
| commit | f711194a9ecdf004b655dbc5006610fbb9e7c7bb (patch) | |
| tree | 8684355bbc2e54fa4332077788b780aafd492390 /plugins | |
| parent | bfe21649b9ccdbc67599d6647ff139a4c34e4458 (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')
| -rw-r--r-- | plugins/security-guidance/hooks/gitutil.py | 36 | ||||
| -rw-r--r-- | plugins/security-guidance/tests/test_temp_index_cleanup.py | 55 |
2 files changed, 91 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) diff --git a/plugins/security-guidance/tests/test_temp_index_cleanup.py b/plugins/security-guidance/tests/test_temp_index_cleanup.py index c975848e..4ac19ea5 100644 --- a/plugins/security-guidance/tests/test_temp_index_cleanup.py +++ b/plugins/security-guidance/tests/test_temp_index_cleanup.py @@ -410,3 +410,58 @@ class TestCopySlots: assert diffstate.capture_git_baseline(str(repo)) == head assert diffstate.capture_git_baseline(str(repo)) != head assert _leftovers(tmproot) == [] + + +class TestStashIndexesLeftInGitDir: + """What releases before 2.0.10 left next to the real index.""" + + def test_old_ones_go_and_nothing_else_does(self, tmp_path, tmproot, monkeypatch): + repo = make_repo(tmp_path / "repo", {"app.py": "x = 1\n"}) + git_dir = repo / ".git" + index = (git_dir / "index").read_bytes() + old = [_write(str(git_dir / n)) for n in ("index.stash.123", "index.stash.99999")] + not_ours = [_write(str(git_dir / n)) for n in ( + "index.lock", "index.stash.12.bak", "index.stash.abc", "index.stash.", "myindex.stash.5")] + _advance_clock(monkeypatch, gitutil._TEMP_INDEX_STALE_S + 60) + fresh = _write(str(git_dir / "index.stash.456")) + now = time.time() # the moved clock + os.utime(fresh, (now, now)) + + with gitutil._temp_index(str(repo), untracked_paths=[]): + pass + + assert [p for p in old if os.path.exists(p)] == [] + assert [p for p in not_ours if not os.path.exists(p)] == [] + assert os.path.exists(fresh) + assert (git_dir / "index").read_bytes() == index + + def test_a_directory_or_link_by_that_name_stays(self, tmp_path, tmproot, monkeypatch): + repo = make_repo(tmp_path / "repo", {"app.py": "x = 1\n"}) + git_dir = repo / ".git" + target = _write(str(tmp_path / "elsewhere")) + os.mkdir(git_dir / "index.stash.1") + os.symlink(target, git_dir / "index.stash.2") + _advance_clock(monkeypatch, gitutil._TEMP_INDEX_STALE_S + 60) + + with gitutil._temp_index(str(repo), untracked_paths=[]): + pass + + assert os.path.isdir(git_dir / "index.stash.1") + assert os.path.islink(git_dir / "index.stash.2") + assert os.path.exists(target) + + def test_linked_worktree_has_its_own(self, tmp_path, tmproot, monkeypatch): + from conftest import git + repo = make_repo(tmp_path / "repo", {"app.py": "x = 1\n"}) + wt = tmp_path / "wt" + git(repo, "worktree", "add", "-q", str(wt), "-b", "side") + wt_git_dir = repo / ".git" / "worktrees" / "wt" + mine = _write(str(wt_git_dir / "index.stash.7")) + main = _write(str(repo / ".git" / "index.stash.7")) + _advance_clock(monkeypatch, gitutil._TEMP_INDEX_STALE_S + 60) + + with gitutil._temp_index(str(wt), untracked_paths=[]): + pass + + assert not os.path.exists(mine) + assert os.path.exists(main) |
