summaryrefslogtreecommitdiff
path: root/plugins/security-guidance/hooks/gitutil.py
diff options
context:
space:
mode:
authorMohamed Hegazy <8000722+mhegazy@users.noreply.github.com>2026-11-09 14:59:32 -0800
committerGitHub <noreply@github.com>2026-11-09 14:59:32 -0800
commitea686bae367e20a4092b2fd4327ffd299bb60f24 (patch)
treecd64a87ba776838a0941e6e43d1b05d480ec8af5 /plugins/security-guidance/hooks/gitutil.py
parentfa5015cff6782d33feeeb31dd2753eea3be83568 (diff)
parent5d1c0f596415311a60adc55d0c0fa1d5f4ac27d8 (diff)
Merge pull request #7033 from anthropics/mhegazy/sg-unseen-commit-count
security-guidance: count commits that no commit hook saw
Diffstat (limited to 'plugins/security-guidance/hooks/gitutil.py')
-rw-r--r--plugins/security-guidance/hooks/gitutil.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/plugins/security-guidance/hooks/gitutil.py b/plugins/security-guidance/hooks/gitutil.py
index 4c363ef4..9b1e2c91 100644
--- a/plugins/security-guidance/hooks/gitutil.py
+++ b/plugins/security-guidance/hooks/gitutil.py
@@ -1097,6 +1097,51 @@ def _detect_main_branch(repo_root):
return None
+# Commits turn_commit_shas reads.
+TURN_COMMITS_MAX = 100
+
+
+def turn_commit_shas(repo_root, since, until, head_before=None):
+ """SHAs that `git commit` made in this checkout from `since` to `until`
+ (epoch seconds), newest first, from its HEAD reflog. None when git cannot
+ say. Not merges, picks, reverts or rebases: no review hook waits for
+ those.
+
+ Goes by the time of the reflog entry, so checking out or pulling
+ somebody's recent commit does not count as making one (a commit made
+ with GIT_COMMITTER_DATE set is dated by that). Those times are whole
+ seconds; `head_before`, what HEAD was at `since`, leaves out the commits
+ made earlier in that second."""
+ try:
+ # --date=unix makes %gd `HEAD@{<seconds>}`; %gs is free text, so last.
+ r = subprocess.run(
+ [*GIT_CMD, "log", "-g", "--no-show-signature", "--date=unix",
+ "--basic-regexp", "--grep-reflog=^commit", "-n", str(TURN_COMMITS_MAX),
+ "--format=%H %gd %gs", "HEAD", "--"],
+ cwd=repo_root, capture_output=True, timeout=5,
+ )
+ except (subprocess.TimeoutExpired, FileNotFoundError, OSError, ValueError):
+ return None
+ if r.returncode != 0:
+ # A repository without a commit has nothing to count.
+ return None if _git_rev_parse_head(repo_root) or not _git_toplevel(repo_root) else []
+ since = int(since)
+ entries = []
+ for line in r.stdout.decode("utf-8", errors="replace").splitlines():
+ # 40 digits only: the reviewed record knows no SHA-256 ids.
+ m = re.match(r"([0-9a-f]{40}) HEAD@\{(\d+)\} commit(?! \((?:merge|cherry-pick)\))", line)
+ if m and since <= int(m.group(2)) <= until:
+ entries.append((m.group(1), int(m.group(2))))
+ # Oldest match, as the turn itself can come back to where it started.
+ for i in range(len(entries) - 1, -1, -1):
+ if entries[i][1] > since:
+ break
+ if entries[i][0] == head_before:
+ del entries[i:]
+ break
+ return list(dict.fromkeys(sha for sha, _ in entries))
+
+
def _git_reflog_recent_commits(repo_root, max_age_s=120, max_n=5):
"""Return (fresh_commit_shas, stale_count) from the HEAD reflog.