summaryrefslogtreecommitdiff
path: root/plugins/security-guidance/hooks/diffstate.py
diff options
context:
space:
mode:
authorClaude <noreply@anthropic.com>2026-11-20 20:52:52 +0000
committerClaude <noreply@anthropic.com>2026-11-20 20:52:52 +0000
commit4e7fef8cacbc5d051fbcb94dfba9f14f285216dc (patch)
tree58ed0d877ed34991db52f3ee8779bf6a68ecaa1f /plugins/security-guidance/hooks/diffstate.py
parentf87ea647eab3b3aa765ed79dbc1eaeb1450d18d2 (diff)
parentd586c3f10a8c2366179bf46b097b4f652622ffc6 (diff)
Merge remote-tracking branch 'origin/main' into mhegazy/sg-count-dropped-changesmhegazy/sg-count-dropped-changes
Diffstat (limited to 'plugins/security-guidance/hooks/diffstate.py')
-rw-r--r--plugins/security-guidance/hooks/diffstate.py23
1 files changed, 14 insertions, 9 deletions
diff --git a/plugins/security-guidance/hooks/diffstate.py b/plugins/security-guidance/hooks/diffstate.py
index 3d26274d..3fde36ec 100644
--- a/plugins/security-guidance/hooks/diffstate.py
+++ b/plugins/security-guidance/hooks/diffstate.py
@@ -19,7 +19,7 @@ from _base import debug_log, make_state_dir, portable_output, _PV
import gitutil
from gitutil import (
GIT_CMD,
- _git_dir, _git_toplevel, _found_through_bare_folder, _git_status_porcelain,
+ _git_dir, _git_toplevel, _stay_out, _git_status_porcelain,
_git_head_at, _git_rev_parse_head, _is_ancestor, _git_name_only, _temp_index,
_secret_rename_origins, index_entry_count,
)
@@ -435,8 +435,8 @@ def dropped_changes(state, sha, baseline_fail, root, head, untracked):
since, kind = state.get("stop_at", 0), state["stop_left"]
else:
return 0
- if baseline_fail or state.get("baseline_fail"):
- return -1 # HEAD in place of a snapshot
+ if old != sha and (baseline_fail or state.get("baseline_fail")):
+ return -1 # HEAD in place of one of the snapshots
deadline = time.monotonic() + _DROPPED_WAIT_S
paths = []
@@ -445,7 +445,8 @@ def dropped_changes(state, sha, baseline_fail, root, head, untracked):
r = subprocess.run(
[*GIT_CMD, "diff", gitutil.IGNORE_SUBMODULES, "--name-only", "--no-renames", "-z",
old, sha, "--"],
- cwd=root, capture_output=True, timeout=_DROPPED_WAIT_S)
+ cwd=root, capture_output=True, timeout=_DROPPED_WAIT_S,
+ env=gitutil.no_download_env())
except (OSError, subprocess.SubprocessError):
return -1
if r.returncode != 0:
@@ -567,6 +568,8 @@ def get_baseline_file_content(session_id, file_path, cwd):
# Windows for any file that is not directly in the session
# directory.
rel_path = rel_path.replace(os.sep, "/")
+ if _stay_out(git_cwd or cwd_abs):
+ return None
result = subprocess.run(
[*GIT_CMD, "show", f"{baseline_sha}:./{rel_path}"],
cwd=git_cwd, capture_output=True, timeout=5
@@ -628,7 +631,9 @@ def _snapshot_commit(cwd, env, head_tree):
# No pathspec: `add -u` has staged the whole work tree, whatever directory
# of it `cwd` is, since git 2.0. `-- :/` would say the same but fails
# outright under a user's GIT_LITERAL_PATHSPECS.
- add = run(["add", "-u"])
+ # Not for files in a directory that a sparse index stores as one entry.
+ sparse = gitutil.may_be_sparse(env["GIT_INDEX_FILE"])
+ add = run([*(gitutil.SPARSE_AS_IT_IS if sparse else []), "add", "-u"])
if add.returncode != 0:
debug_log(f"Snapshot: add -u failed: {_stderr_of(add)}")
return None
@@ -698,8 +703,8 @@ def capture_git_baseline(cwd):
global last_baseline_fail, last_index_entries
last_baseline_fail = 0
last_index_entries = None
- if _found_through_bare_folder(cwd):
- debug_log("Not an ordinary checkout, skipping baseline capture")
+ if _stay_out(cwd):
+ debug_log("Not a checkout to run git in, skipping baseline capture")
return None
try:
# HEAD's commit and its tree in one call: the commit is the answer
@@ -1500,7 +1505,7 @@ def _list_untracked(cwd):
gitutil.py already follow the lenient pattern; this function and
capture_git_baseline / _git_name_only / _git_status_porcelain were
the holdouts."""
- if _found_through_bare_folder(cwd):
+ if _stay_out(cwd):
return {}
try:
repo = _git_toplevel(cwd) or cwd
@@ -1575,7 +1580,7 @@ def compute_v2_review_set(cwd, baseline_sha, head_at_capture, untracked_at_basel
if not isinstance(untracked_at_baseline, dict):
untracked_at_baseline = {}
- tracked_dirty, untracked = (None, None) if _found_through_bare_folder(cwd) else _git_status_porcelain(repo)
+ tracked_dirty, untracked = (None, None) if _stay_out(cwd) else _git_status_porcelain(repo)
if tracked_dirty is None:
return [], "HEAD", repo, [], {"dirty_now_count": -1, "changed_since_count": -1, "review_set_count": 0}