diff --git a/src/loopzero/cli.py b/src/loopzero/cli.py index 635cfa1b..3ff9b65f 100644 --- a/src/loopzero/cli.py +++ b/src/loopzero/cli.py @@ -700,7 +700,7 @@ def cmd_pr(args: argparse.Namespace) -> int: raise CliError( f"PR head {pr.head_sha[:12]} is not in this worktree; fetch and integrate it first" ) - force_lease = not _is_ancestor(wt, pr.head_sha, head) + force_lease = pr.head_sha != head and not _is_ancestor(wt, pr.head_sha, head) if force_lease: previous_heads = _git(wt, "reflog", "show", "--format=%H", f"refs/heads/{branch}").splitlines() if not any(_is_ancestor(wt, pr.head_sha, previous) for previous in previous_heads): @@ -1216,10 +1216,10 @@ def _load_review( def _pr_readiness( - wt: Path, config: Config, *, allow_merged: bool = False, workflow_wait: bool = False + wt: Path, config: Config, branch: str, *, allow_merged: bool = False, workflow_wait: bool = False ) -> tuple[github.PR, github.Readiness | None]: """PR plus readiness; readiness is None only for an already merged PR (allow_merged).""" - branch, head = worktree.branch(wt), worktree.head(wt) + head = worktree.head(wt) _require_task_branch(config, branch) pr = _require_pr(config, branch, allow_merged=allow_merged) _require_pushed(pr, head) @@ -1247,7 +1247,7 @@ def _cleanup(wt: Path, pr: github.PR) -> None: def cmd_ready(args: argparse.Namespace) -> int: wt, config = _context(args, pin_checks=True) - pr, readiness = _pr_readiness(wt, config, workflow_wait=args.wait is not None) + pr, readiness = _pr_readiness(wt, config, worktree.branch(wt), workflow_wait=args.wait is not None) assert readiness is not None waiting = _draft_checks_waiting(config, pr, readiness) kicked = bool(waiting) @@ -1675,7 +1675,7 @@ def cmd_merge(args: argparse.Namespace) -> int: print(f"requested removal of PR #{pr.number} from Mergify") return 0 pr, readiness = _pr_readiness( - wt, config, allow_merged=True, workflow_wait=args.wait is not None + wt, config, branch, allow_merged=True, workflow_wait=args.wait is not None ) started = time.monotonic() deadline = None if args.wait is None else started + args.wait diff --git a/tests/test_cli.py b/tests/test_cli.py index 5c136d5d..dd28cf13 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1111,12 +1111,14 @@ def test_progress_output_precedes_the_stderr_verdict_in_one_file(tmp_path: Path) @pytest.mark.parametrize("corrupt_report", [False, True], ids=["no-report", "corrupt-report"]) -def test_pr_updates_existing_open_pr(wt, gh, capsys, corrupt_report): +def test_pr_updates_existing_open_pr(wt, gh, capsys, corrupt_report, git_calls): if corrupt_report: (wt / ".loopzero" / "checks.json").write_text('{"results": "nope"}') arm_pr_body_patch(gh, head_of(wt)) + git_calls.clear() code, out, err = run(capsys, "pr") + assert not any(call[:2] == ["merge-base", "--is-ancestor"] for call in git_calls) assert code == 0 and out == URL + "\n" assert ("warning: ignoring unreadable" in err) == corrupt_report @@ -1136,6 +1138,7 @@ def test_pr_aborts_when_ancestry_cannot_be_verified(wt, gh, fake_tool, tmp_path, pushes = trace_pushes(fake_tool, tmp_path) else: arm_pr_body_patch(gh, head_of(wt)) + git(wt, "commit", "--allow-empty", "-m", "Advance local head") pushes = tmp_path / "pushes" fake_tool("git", f'''\ if [ "$1" = "merge-base" ] && [ "$2" = "--is-ancestor" ]; then {probe}; fi @@ -3708,7 +3711,7 @@ def test_merge_queued_prints_and_keeps_worktree(wt: Path, gh: FakeGh, capsys, he @pytest.mark.parametrize("attested", [True, False]) @pytest.mark.parametrize("merge_state", ["BEHIND", "CLEAN"]) def test_mergify_merge_requests_once_for_behind_head( - wt: Path, gh: FakeGh, capsys, monkeypatch, attested, merge_state + wt: Path, gh: FakeGh, capsys, monkeypatch, attested, merge_state, git_calls ) -> None: head = commit_mergify_configuration(wt) arm_approved_ready_pr(gh, head, wt, isDraft=False, mergeStateStatus=merge_state) @@ -3717,7 +3720,9 @@ def test_mergify_merge_requests_once_for_behind_head( stub_mergify_admission(monkeypatch, configured=None) monkeypatch.setattr(mergify, "request", lambda *args: requested.append(args)) + git_calls.clear() code, out, err = run(capsys, "merge") + assert git_calls.count(["branch", "--show-current"]) == 1 if attested: assert (code, err) == (0, "")