Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/loopzero/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ def _is_ancestor(wt: Path, sha: str, head: str) -> bool:
["git", "merge-base", "--is-ancestor", sha, head],
cwd=wt, env_allowlist=worktree.GIT_ENV, timeout=GIT_TIMEOUT,
)
if done.exit_code not in (0, 1):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Stricter _is_ancestor also changes the delta-review message when the reviewed commit is missing

_is_ancestor now raises on any exit other than 0 or 1. That change also reaches _run_review (cli.py:984) and _delta_merge_recovery (cli.py:971). Before this change, a delta review whose reviewed SHA was missing from the local repo made git merge-base exit 128, which _is_ancestor read as 'not an ancestor'. The user then got the specific recovery guidance ("reviewed head … is not an ancestor of this branch. Find the original branch tip with git reflog …"). For example, this happens when another clone force-pushed the reviewed head and this repo never fetched it. Now the same case shows the generic "git merge-base --is-ancestor failed (exit 128): fatal: Not a valid commit name …". Failing closed is the right call, and both paths still abort. The cost is the actionable advice on that path, and no test covers the new review-side behaviour. Smallest fix: in _run_review, run the same cat-file -e <sha>^{commit} check used in cmd_pr before the ancestry probe and give a 'fetch it first' hint. Or add a test that pins the new message so the change is deliberate.

detail = _proc.tail(done.stderr or done.stdout, 1)
raise CliError(f"git merge-base --is-ancestor failed (exit {done.exit_code}): "
f"{detail or 'retry the ancestry check'}")
return done.exit_code == 0


Expand Down Expand Up @@ -667,17 +671,19 @@ def cmd_pr(args: argparse.Namespace) -> int:
)
existing = _sync_task_narrative(wt, pr.body or "") if pr is not None else None
body = _pr_body(wt, head, config, existing)
force_lease = pr is not None and pr.state == "OPEN" and not _is_ancestor(wt, pr.head_sha, head)
force_lease = False
push = ["push", "-u", "origin"]
if force_lease:
if pr is not None and pr.state == "OPEN":
# Only rewrite history this worktree has seen; an unknown remote head is someone else's.
known = _proc.run(["git", "cat-file", "-e", f"{pr.head_sha}^{{commit}}"], cwd=wt,
env_allowlist=worktree.GIT_ENV, timeout=GIT_TIMEOUT)
if known.exit_code != 0:
raise CliError(
f"PR head {pr.head_sha[:12]} is not in this worktree; fetch and integrate it first"
)
push.extend(("--porcelain", f"--force-with-lease={branch}:{pr.head_sha}"))
force_lease = not _is_ancestor(wt, pr.head_sha, head)
if force_lease:
push.extend(("--porcelain", f"--force-with-lease={branch}:{pr.head_sha}"))
push.append(branch)
_git_push(wt, push, force_lease=force_lease)
if pr is not None and pr.state == "OPEN":
Expand Down
37 changes: 37 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,43 @@ def test_pr_updates_existing_open_pr(wt: Path, gh: FakeGh, capsys) -> None:
assert "(no `loopzero check` run recorded)" in gh.calls[-1]["--input"]


@pytest.mark.parametrize(
("probe", "diagnostic"),
[
("kill -TERM $$", "exit -15"),
("echo 'fatal: unable to read commit' >&2; exit 128", "unable to read commit"),
],
)
def test_pr_aborts_on_ancestry_probe_error(wt, gh, fake_tool, tmp_path, capsys, probe, diagnostic):
gh.respond_pr_list([pr_json(headRefOid=head_of(wt))])
gh.respond(f"api repos/{REPO}/pulls/7 --method PATCH", {})
pushes = tmp_path / "pushes"
fake_tool("git", f'''\
if [ "$1" = "merge-base" ] && [ "$2" = "--is-ancestor" ]; then {probe}; fi
if [ "$1" = "push" ]; then touch "{pushes}"; exit 0; fi
exec /usr/bin/git "$@"
''')

code, out, err = run(capsys, "pr")

assert (code, out) == (1, "")
assert "git merge-base --is-ancestor" in err and diagnostic in err
assert not pushes.exists()
assert all("--method" not in call["argv"] for call in gh.calls)


def test_pr_aborts_on_missing_remote_head(wt, gh, fake_tool, tmp_path, capsys):
gh.respond_pr_list([pr_json(headRefOid="a" * 40)])
pushes = trace_pushes(fake_tool, tmp_path)

code, out, err = run(capsys, "pr")

assert (code, out) == (1, "")
assert "fetch and integrate it first" in err
assert not pushes.exists()
assert all("--method" not in call["argv"] for call in gh.calls)


@pytest.mark.parametrize("hook_fails", [False, True])
def test_pr_uses_lease_for_rewritten_open_pr(
wt: Path, repo: Path, gh: FakeGh, fake_tool, tmp_path: Path, capsys, hook_fails: bool
Expand Down
Loading