summaryrefslogtreecommitdiff
path: root/plugins/security-guidance/hooks/gitutil.py
diff options
context:
space:
mode:
authorMohamed Hegazy <8000722+mhegazy@users.noreply.github.com>2026-10-20 13:27:17 -0700
committerGitHub <noreply@github.com>2026-10-20 13:27:17 -0700
commit62bb122f3052d547bdc4e5e2d0e4029fdcb2e8e5 (patch)
tree43e45cb481a5fcd7afb1835c013be6ebd4e8d296 /plugins/security-guidance/hooks/gitutil.py
parent07b02ef8249ec166557a5bd4f045c57af6a62c3f (diff)
parent307800f0aaa7b285390bd1702055881f48098b2d (diff)
Merge pull request #6810 from anthropics/mhegazy/sg-stop-baseline-checkout
security-guidance: do not review against a baseline from another checkout
Diffstat (limited to 'plugins/security-guidance/hooks/gitutil.py')
-rw-r--r--plugins/security-guidance/hooks/gitutil.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/plugins/security-guidance/hooks/gitutil.py b/plugins/security-guidance/hooks/gitutil.py
index 28ded7d4..a2402d1e 100644
--- a/plugins/security-guidance/hooks/gitutil.py
+++ b/plugins/security-guidance/hooks/gitutil.py
@@ -506,6 +506,41 @@ def _git_toplevel(cwd):
return None
+def _git_toplevel_and_head(cwd):
+ """(repo root, HEAD SHA) for `cwd` from one git call, or (None, None)
+ when git cannot give both (not a work tree, no commits)."""
+ try:
+ r = subprocess.run(
+ [*GIT_CMD, "rev-parse", "--show-toplevel", "HEAD"],
+ cwd=cwd, capture_output=True, timeout=5,
+ )
+ except (subprocess.TimeoutExpired, FileNotFoundError, OSError):
+ return None, None
+ if r.returncode != 0:
+ return None, None
+ # See #2099: the first line is a PATH, so decode the bytes manually.
+ top, _, head = r.stdout.decode("utf-8", errors="replace").strip().rpartition("\n")
+ return (top.strip() or None), (head.strip() or None)
+
+
+def _git_head_at(cwd, epoch):
+ """The SHA this checkout's HEAD pointed at just before `epoch` (seconds),
+ read from its own reflog. A checkout created after `epoch` answers with
+ the commit it was created at. None when git cannot say (no reflog)."""
+ try:
+ # Reflog times are whole seconds: ask for the second before, so a
+ # commit made in the same second as `epoch` counts as after it.
+ r = subprocess.run(
+ [*GIT_CMD, "rev-parse", "--verify", "-q", f"HEAD@{{@{int(epoch) - 1}}}"],
+ cwd=cwd, capture_output=True, timeout=5,
+ )
+ except (subprocess.TimeoutExpired, FileNotFoundError, OSError,
+ TypeError, ValueError, OverflowError):
+ return None
+ sha = r.stdout.decode("utf-8", errors="replace").strip()
+ return sha if r.returncode == 0 and sha else None
+
+
def _git_dir(repo_root):
"""Absolute shared `.git` directory for repo_root.