Five git runners in tools/ and hermes_cli/ each spelled out the same subprocess.run call: output captured as UTF-8 text with errors replaced, stdin closed, no raise on a non-zero exit, and the caller's cwd, env and timeout. utils/git.py now holds that call as run_git, and the five runners call it. Each runner keeps its own argv hardening, environment, return shape and error handling, so behaviour does not change. worktree_gc's private _run lost its only caller and is removed.
40 lines
1.0 KiB
Python
40 lines
1.0 KiB
Python
"""Run one git command and return its result.
|
|
|
|
The caller owns everything that makes the call safe for a given repository: the argv
|
|
hardening and the environment (``hermes_cli._subprocess_compat`` builds both), and which
|
|
``git`` executable to run.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import subprocess
|
|
from collections.abc import Mapping, Sequence
|
|
|
|
|
|
def run_git(
|
|
args: Sequence[str],
|
|
*,
|
|
cwd: str | os.PathLike[str] | None,
|
|
env: Mapping[str, str] | None,
|
|
timeout: float,
|
|
git: str = "git",
|
|
) -> subprocess.CompletedProcess[str]:
|
|
"""Run ``git *args`` with output captured as UTF-8 text and stdin closed.
|
|
|
|
A non-zero exit is returned, not raised. ``OSError`` (git missing, ``cwd`` gone) and
|
|
``subprocess.TimeoutExpired`` propagate to the caller.
|
|
"""
|
|
return subprocess.run(
|
|
[git, *args],
|
|
cwd=cwd,
|
|
env=env,
|
|
capture_output=True,
|
|
text=True,
|
|
encoding="utf-8",
|
|
errors="replace",
|
|
timeout=timeout,
|
|
stdin=subprocess.DEVNULL,
|
|
check=False,
|
|
)
|