diff options
| author | Claude <noreply@anthropic.com> | 2026-11-09 17:52:07 +0000 |
|---|---|---|
| committer | Claude <noreply@anthropic.com> | 2026-11-09 17:52:07 +0000 |
| commit | 67f8c646e7cb50b4e28e042d13932efca6dc5f50 (patch) | |
| tree | a38c9eedd041469f6c8576ac151a91304db3ea39 /plugins/security-guidance/hooks/gitutil.py | |
| parent | 0412be76f50ab4f4f8abff3753efa9269fbbca5b (diff) | |
security-guidance: push review of a merge reviews what the push added
The reviewed prefix could end on the merged-in branch, so the diff held
everything the first parent has that the branch lacks. Keep the base on
HEAD's first-parent line. A merge whose history is all reviewed is sent as
its combined diff, so only its conflict resolutions and hand edits are read.
Co-authored-by: mhegazy <mhegazy@anthropic.com>
Diffstat (limited to 'plugins/security-guidance/hooks/gitutil.py')
| -rw-r--r-- | plugins/security-guidance/hooks/gitutil.py | 55 |
1 files changed, 54 insertions, 1 deletions
diff --git a/plugins/security-guidance/hooks/gitutil.py b/plugins/security-guidance/hooks/gitutil.py index 4c363ef4..733762ef 100644 --- a/plugins/security-guidance/hooks/gitutil.py +++ b/plugins/security-guidance/hooks/gitutil.py @@ -1047,7 +1047,7 @@ def _git_rev_list_range(repo_root, base, head="HEAD"): # See #2099: stdout is ASCII SHAs, but stderr can carry git error # messages referencing non-ASCII filenames — keep bytes raw. r = subprocess.run( - [*GIT_CMD, "rev-list", "--reverse", f"{base}..{head}"], + [*GIT_CMD, "rev-list", "--reverse", "--topo-order", f"{base}..{head}"], cwd=repo_root, capture_output=True, timeout=10, ) if r.returncode != 0: @@ -1057,6 +1057,59 @@ def _git_rev_list_range(repo_root, base, head="HEAD"): return [] +def _git_range_parents(repo_root, base, head="HEAD"): + """{sha: [parent shas, first parent first]} for `base..head`. + + Empty on error: the caller then treats the range as linear, which reviews + more, never less. + """ + try: + r = subprocess.run( + [*GIT_CMD, "rev-list", "--parents", f"{base}..{head}"], + cwd=repo_root, capture_output=True, timeout=10, + ) + if r.returncode != 0: + return {} + rows = [line.split() for line in r.stdout.decode("utf-8", errors="replace").splitlines()] + return {row[0]: row[1:] for row in rows if row} + except (subprocess.TimeoutExpired, FileNotFoundError, OSError): + return {} + + +def _git_merge_resolved_files(repo_root, merge_sha): + """(path, combined-diff hunks) for each source file a merge changed itself, + or None on error. + + The combined diff shows only lines that match none of the merge's parents: + conflict resolutions and hand edits. A clean merge has none, and neither + does a conflict settled by taking one side whole. + """ + try: + r = subprocess.run( + [*GIT_CMD, "diff-tree", "--cc", "-M", "--no-commit-id", "-r", "--no-color", + "--no-ext-diff", "--no-textconv", merge_sha], + cwd=repo_root, capture_output=True, timeout=30, + ) + if r.returncode != 0: + return None + except (subprocess.TimeoutExpired, FileNotFoundError, OSError): + return None + files = [] + # Hunk lines start with one marker column per parent, so only a file + # header starts with "diff --cc " in the first column. + text = r.stdout.decode("utf-8", errors="replace") + for section in re.split(r"^diff --cc ", text, flags=re.M)[1:]: + header, _, body = section.partition("\n") + path = header + if header.startswith('"'): + quoted = _read_quoted_path(header, 0) + path = quoted[0] if quoted else None + at = body.find("@@@") + if path and at >= 0 and _section_is_reviewable(path, path): + files.append((path, body[at:].rstrip("\n"))) + return files + + def _git_diff_range(repo_root, base, head="HEAD"): """`git diff -p base head` as text on success, None on error. |
