git.py¶
clew/guard/git.py
Git diff parsing for change detection.
_rev_parse_head
str | None clew.guard.git._rev_parse_head(RunCommand|None run_command)
Get the current HEAD commit SHA.
Source · 5 lines
def _rev_parse_head(run_command: RunCommand | None) -> str | None:
result = run_git(["git", "rev-parse", "HEAD"], run_command)
return result.strip() if result is not None else None
Activity · 2 steps
Flows
get_branch_diff_range
str | None clew.guard.git.get_branch_diff_range(str target_branch="origin/main", RunCommand|None run_command=None)
Build a diff range string from merge-base to HEAD.
Source · 13 lines
def get_branch_diff_range(
target_branch: str = "origin/main",
run_command: RunCommand | None = None,
) -> str | None:
base = get_merge_base(target_branch, run_command)
if base is None:
return None
head = _rev_parse_head(run_command)
if head and base == head:
return None
return f"{base}...HEAD"
Activity · 7 steps
Requirements
Flows
get_changed_files
list[str] | None clew.guard.git.get_changed_files(str|None diff_range, bool staged, RunCommand|None run_command=None)
The files a revision range (or the index) changed.
Source · 13 lines
def get_changed_files(
diff_range: str | None,
staged: bool,
run_command: RunCommand | None = None,
) -> list[str] | None:
if diff_range and diff_range.startswith("-"):
logger.error("Refusing diff range that looks like an option: %r", diff_range)
return None
cmd = ["git", "diff", "--name-only", *(["--cached"] if staged else [diff_range or "HEAD"])]
out = run_git(cmd, run_command)
return None if out is None else [line for line in out.splitlines() if line.strip()]
Activity · 6 steps
Requirements
Flows
get_changed_lines_for_file
set[int] clew.guard.git.get_changed_lines_for_file(str file_path, RunCommand|None run_command=None)
Convenience function combining staged diff retrieval and parsing.
Called by none
Tested by
Source · 6 lines
def get_changed_lines_for_file(
file_path: str,
run_command: RunCommand | None = None,
) -> set[int]:
diff_output = get_staged_diff(file_path, run_command)
return parse_changed_lines(diff_output) if diff_output else set()1 more lines, not shown
Activity · 2 steps
Requirements
get_diff
str | None clew.guard.git.get_diff(str file_path, str diff_range, RunCommand|None run_command=None)
Run git diff for a file over a given revision range.
Source · 12 lines
def get_diff(
file_path: str,
diff_range: str,
run_command: RunCommand | None = None,
) -> str | None:
if diff_range.startswith("-"):
# git diff would parse this as an option; --output=<file> is an arbitrary write.
logger.error("Refusing diff range that looks like an option: %r", diff_range)
return None
return run_git(["git", "diff", "-U0", diff_range, "--", file_path], run_command)
Activity · 4 steps
Requirements
Flows
get_file_at_revision
str | None clew.guard.git.get_file_at_revision(str file_path, str revision="HEAD", RunCommand|None run_command=None)
Get a file's content as of a given revision.
Source · 8 lines
def get_file_at_revision(
file_path: str,
revision: str = "HEAD",
run_command: RunCommand | None = None,
) -> str | None:
return run_git(["git", "show", f"{revision}:./{file_path}"], run_command)
Activity · 1 steps
Requirements
Flows
get_merge_base
str | None clew.guard.git.get_merge_base(str target_branch="origin/main", RunCommand|None run_command=None)
Detect the merge-base between current HEAD and a target branch.
Source · 11 lines
def get_merge_base(
target_branch: str = "origin/main",
run_command: RunCommand | None = None,
) -> str | None:
result = run_git(["git", "merge-base", target_branch, "HEAD"], run_command)
if result is None:
logger.warning("Could not determine merge-base against %s", target_branch)
return None
return result.strip()
Activity · 5 steps
Requirements
Flows
get_staged_diff
str | None clew.guard.git.get_staged_diff(str file_path, RunCommand|None run_command=None)
Run git diff cached for a single file.
Source · 7 lines
def get_staged_diff(
file_path: str,
run_command: RunCommand | None = None,
) -> str | None:
return run_git(["git", "diff", "--cached", "-U0", "--", file_path], run_command)
Activity · 1 steps
Requirements
Flows
git_add
bool clew.guard.git.git_add(str|list[str] paths, RunCommand|None run_command=None)
Stage files for the next git commit.
Source · 9 lines
def git_add(
paths: str | list[str],
run_command: RunCommand | None = None,
) -> bool:
file_list = [paths] if isinstance(paths, str) else [str(p) for p in paths]
# "--" so a path beginning with "-" cannot be parsed as an option.
return run_git(["git", "add", "--", *file_list], run_command) is not None
Activity · 2 steps
Flows
parse_changed_lines
set[int] clew.guard.git.parse_changed_lines(str diff_output)
Extract the set of modified line numbers from a unified diff.
Source · 19 lines
def parse_changed_lines(diff_output: str) -> set[int]:
changed: set[int] = set()
hunk_re = re.compile(r"^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@", re.MULTILINE)
for match in hunk_re.finditer(diff_output):
start = int(match.group(1))
count = int(match.group(2)) if match.group(2) else 1
if count == 0:
# Pure deletion — no new lines added
continue
# Convert to 0-indexed
for line_num in range(start - 1, start - 1 + count):
changed.add(line_num)
return changed
Activity · 10 steps
Requirements
Flows
run_git
str | None clew.guard.git.run_git(list[str] cmd, RunCommand|None run_command=None)
Run a git command through the injectable runner, returning None on failure.
Called by _rev_parse_head, _tracked, get_changed_files, get_diff, get_file_at_revision, get_merge_base, get_staged_diff, git_add
Tested by
Calls git_argv
Source · 21 lines
def run_git(cmd: list[str], run_command: RunCommand | None = None) -> str | None:
runner = run_command or _default_run_command
## DISARMED HERE, AT THE ONE PLACE EVERY GATE GIT CALL PASSES THROUGH (1.1.0 audit, C1 —
## second half). A repository's own `.git/config` can name a command git then RUNS
## (`core.fsmonitor` on a diff, and its siblings), so a bare `["git", …]` argv is
## execution chosen by the tree being inspected. The gate is the worse context for that
## than the pipeline: it runs as the committing developer, on whatever branch they have
## checked out to review, before any of their own code runs.
##
## The call sites keep their readable literal lists and this rewrites them, rather than
## fourteen call sites each remembering a helper. An injected `run_command` gets the
## disarmed argv too, so a test double cannot observe an argv the real runner would not
## receive.
disarmed = git_argv(*cmd[1:]) if cmd and cmd[0] == "git" else cmd
try:
return runner(disarmed)
except GIT_ERRORS as e:
logger.warning("git %s failed: %s", " ".join(cmd[1:3]), e)
return None