Answer architecture round 4: fail rule-hook overrides when the branch's reflog cannot be read or does not hold HEAD, so a git error never reads as a clean review
This commit is contained in:
+13
-4
@@ -25,7 +25,8 @@ default branch lacks, and those logged under the branch's own name.
|
|||||||
Run with no arguments in a terminal, it prints this help.
|
Run with no arguments in a terminal, it prints this help.
|
||||||
|
|
||||||
Exit 2 with the rule, why it holds and how to override it, when a rule blocks the call, or when an
|
Exit 2 with the rule, why it holds and how to override it, when a rule blocks the call, or when an
|
||||||
override cannot be logged; 0 otherwise. Exit 1, a non-blocking error to Claude Code, on input it
|
override cannot be logged; 0 otherwise. `rule-hook overrides` exits 1 when git cannot read the
|
||||||
|
checkout or the branch's reflog. Exit 1, a non-blocking error to Claude Code, on input it
|
||||||
cannot read.
|
cannot read.
|
||||||
"""
|
"""
|
||||||
import dataclasses
|
import dataclasses
|
||||||
@@ -46,6 +47,8 @@ from shell_command import simple_commands
|
|||||||
from utc_stamp import utc
|
from utc_stamp import utc
|
||||||
|
|
||||||
LOG = "rule-overrides.tsv"
|
LOG = "rule-overrides.tsv"
|
||||||
|
# A -c given here outranks the checkout's own config, which may allow one transport by name; GIT_NO_LAZY_FETCH needs git 2.44.
|
||||||
|
GUARDS = [option for setting in ["core.fsmonitor=false", "log.showSignature=false", "protocol.allow=never", *(f"protocol.{name}.allow=never" for name in ("ext", "file", "git", "http", "https", "ssh"))] for option in ("-c", setting)]
|
||||||
|
|
||||||
|
|
||||||
def main(argv: list[str], stdin: str) -> int:
|
def main(argv: list[str], stdin: str) -> int:
|
||||||
@@ -173,7 +176,13 @@ def print_overrides(directory: str) -> int:
|
|||||||
ref = branch if isinstance(branch, str) and branch else "HEAD"
|
ref = branch if isinstance(branch, str) and branch else "HEAD"
|
||||||
# The branch's reflog keeps a commit an amend or a rebase replaced, follows `git branch -m`, and holds no other branch's commit.
|
# The branch's reflog keeps a commit an amend or a rebase replaced, follows `git branch -m`, and holds no other branch's commit.
|
||||||
reflog = git(directory, "reflog", "show", "--format=%H", ref)
|
reflog = git(directory, "reflog", "show", "--format=%H", ref)
|
||||||
walked = set(reflog.split()) if isinstance(reflog, str) else set()
|
head = git(directory, "rev-parse", "HEAD")
|
||||||
|
# A reflog missing HEAD is off or expired, and would hide this chunk's overrides.
|
||||||
|
if isinstance(reflog, Failure) or isinstance(head, Failure) or head not in reflog.split():
|
||||||
|
reason = reflog.reason if isinstance(reflog, Failure) else head.reason if isinstance(head, Failure) else "it does not hold HEAD"
|
||||||
|
print(f"rule-hook overrides cannot read the branch's reflog, so it cannot tell which overrides are this chunk's: {reason}", file=sys.stderr)
|
||||||
|
return 1
|
||||||
|
walked = set(reflog.split())
|
||||||
default = default_branch(directory)
|
default = default_branch(directory)
|
||||||
log = pathlib.Path(common) / LOG
|
log = pathlib.Path(common) / LOG
|
||||||
for line in log.read_text(encoding="utf-8").splitlines() if log.is_file() else []:
|
for line in log.read_text(encoding="utf-8").splitlines() if log.is_file() else []:
|
||||||
@@ -194,9 +203,9 @@ def holds(directory: str, descendant: str, ancestor: str) -> bool:
|
|||||||
|
|
||||||
|
|
||||||
def git(directory: str, *args: str) -> str | Failure:
|
def git(directory: str, *args: str) -> str | Failure:
|
||||||
# The hook runs git in a checkout the call has not yet been allowed to touch, whose config could name a program: no fsmonitor, and no fetch of a partial clone's missing objects through its transport.
|
# The hook runs git in a checkout the call has not yet been allowed to touch, whose config could name a program: no fsmonitor, no signature check, and no fetch of a partial clone's missing objects through its transport.
|
||||||
try:
|
try:
|
||||||
ran = subprocess.run(["git", "-c", "core.fsmonitor=false", "-c", "protocol.allow=never", "-C", directory, *args], capture_output=True, env={**os.environ, "GIT_NO_LAZY_FETCH": "1"}, text=True)
|
ran = subprocess.run(["git", *GUARDS, "-C", directory, *args], capture_output=True, env={**os.environ, "GIT_NO_LAZY_FETCH": "1"}, text=True)
|
||||||
except OSError as failure:
|
except OSError as failure:
|
||||||
return Failure(f"git could not start: {failure}")
|
return Failure(f"git could not start: {failure}")
|
||||||
return ran.stdout.strip() if ran.returncode == 0 else Failure(f"git {' '.join(args)}: {ran.stderr.strip()}")
|
return ran.stdout.strip() if ran.returncode == 0 else Failure(f"git {' '.join(args)}: {ran.stderr.strip()}")
|
||||||
|
|||||||
@@ -170,6 +170,16 @@ class Hook(unittest.TestCase):
|
|||||||
git(self.repo, "branch", "-m", "renamed")
|
git(self.repo, "branch", "-m", "renamed")
|
||||||
self.assertIn("\timage-pin\t", self.listed(), "the reflog keeps the amended commit")
|
self.assertIn("\timage-pin\t", self.listed(), "the reflog keeps the amended commit")
|
||||||
|
|
||||||
|
def test_an_unreadable_reflog_fails_the_listing(self):
|
||||||
|
unborn = tempfile.mkdtemp()
|
||||||
|
self.addCleanup(subprocess.run, ["rm", "-rf", unborn])
|
||||||
|
git(unborn, "init", "-q", "-b", "chunk")
|
||||||
|
ran = subprocess.run([str(HOOK), "overrides"], capture_output=True, cwd=unborn, env=isolated_env(), text=True)
|
||||||
|
self.assertEqual(ran.returncode, 1)
|
||||||
|
self.assertIn("cannot read the branch's reflog", ran.stderr)
|
||||||
|
git(self.repo, "reflog", "expire", "--expire=now", "--all")
|
||||||
|
self.assertEqual(subprocess.run([str(HOOK), "overrides"], capture_output=True, cwd=self.repo, env=isolated_env(), text=True).returncode, 1, "an expired reflog holds no HEAD")
|
||||||
|
|
||||||
def test_a_sibling_branch_s_override_is_not_listed(self):
|
def test_a_sibling_branch_s_override_is_not_listed(self):
|
||||||
git(self.repo, "checkout", "-q", "-b", "sibling")
|
git(self.repo, "checkout", "-q", "-b", "sibling")
|
||||||
git(self.repo, "commit", "-q", "--allow-empty", "-m", "sibling work")
|
git(self.repo, "commit", "-q", "--allow-empty", "-m", "sibling work")
|
||||||
|
|||||||
Reference in New Issue
Block a user