diff --git a/bin/git_cli/epic_branch.py b/bin/git_cli/epic_branch.py index 491735c3..c9f7dc80 100644 --- a/bin/git_cli/epic_branch.py +++ b/bin/git_cli/epic_branch.py @@ -9,9 +9,11 @@ def levelled(branch: str, default: str, cwd: str | None = None, timeout: float | """Brings origin's branch level with default, the default branch's ref, such as `origin/main`, and fetches it: what it did, as a sentence. Where origin has no such branch, it makes one at default. A call to origin still running after timeout seconds fails. again is False in the second try `merged` makes after a push that failed.""" - held = fetched(cwd, timeout, run) or on_origin(branch, cwd, timeout, run) - if isinstance(held, Failure): - return held + # Asked before the fetch: a branch another chunk makes after the fetch would be on origin and missing here. + held = on_origin(branch, cwd, timeout, run) + failed = held if isinstance(held, Failure) else fetched(cwd, timeout, run) + if isinstance(failed, Failure): + return failed if not held: return pushed(default, branch, cwd, timeout, run) or f"Made origin/{branch} off {default}." return merged(branch, default, cwd, timeout, run, again) diff --git a/bin/git_cli/test_epic_branch.py b/bin/git_cli/test_epic_branch.py index a3951638..a56a70b8 100644 --- a/bin/git_cli/test_epic_branch.py +++ b/bin/git_cli/test_epic_branch.py @@ -67,16 +67,29 @@ class Levelled(unittest.TestCase): self.assertEqual(sh(self.origin, "rev-parse", "ABC-1"), sh(self.origin, "rev-parse", "main"), "not the commits the deleted branch held") self.assertEqual(sh(self.clone, "rev-parse", "origin/ABC-1"), sh(self.origin, "rev-parse", "main")) + def test_a_branch_another_chunk_makes_between_its_two_calls_to_origin_is_no_failure(self) -> None: + calls: list[tuple[str, ...]] = [] + + def raced(*args: str, **options: object) -> str | Failure: + answer = git_repo.git(*args, **options) + calls.append(args) + if len(calls) == 1: + sh(self.origin, "branch", "--quiet", "ABC-1", "main") + return answer + + self.assertEqual(levelled("ABC-1", "origin/main", cwd=str(self.clone), run=raced), "Made origin/ABC-1 off origin/main.") + self.assertEqual(sh(self.clone, "rev-parse", "origin/ABC-1"), sh(self.origin, "rev-parse", "main")) + def test_asks_origin_for_exactly_the_branch(self) -> None: calls: list[tuple[str, ...]] = [] levelled("ABC-1", "origin/main", run=scripted(calls)) - self.assertEqual(calls[1], ("ls-remote", "--heads", "origin", "refs/heads/ABC-1")) + self.assertEqual(calls[0], ("ls-remote", "--heads", "origin", "refs/heads/ABC-1")) def test_an_origin_that_cannot_say_whether_it_has_the_branch_fails_with_gits_reason(self) -> None: calls: list[tuple[str, ...]] = [] unreachable = Failure("git ls-remote --heads origin refs/heads/ABC-1: fatal: unable to access origin") self.assertEqual(levelled("ABC-1", "origin/main", run=scripted(calls, ls_remote=unreachable)), unreachable) - self.assertEqual([args[0] for args in calls], ["fetch", "ls-remote"], "makes no branch on an origin it could not ask") + self.assertEqual([args[0] for args in calls], ["ls-remote"], "makes no branch on an origin it could not ask") def test_leaves_a_branch_that_holds_the_default_branch(self) -> None: self.commit("ABC-1", "b.txt", "epic\n") @@ -148,11 +161,11 @@ class Levelled(unittest.TestCase): calls: list[tuple[str, ...]] = [] declined = Failure("git push --quiet origin c0mm17:refs/heads/ABC-1: ! [remote rejected] c0mm17 -> ABC-1 (protected branch hook declined)") self.assertEqual(levelled("ABC-1", "origin/main", run=scripted(calls, push=declined)), declined) - self.assertEqual([args[0] for args in calls], ["fetch", "ls-remote", "rev-list", "merge-tree", "commit-tree", "push"] * 2) + self.assertEqual([args[0] for args in calls], ["ls-remote", "fetch", "rev-list", "merge-tree", "commit-tree", "push"] * 2) def test_a_fetch_that_fails_is_tried_once_more_since_a_chunk_starting_beside_it_may_hold_the_ref(self) -> None: merge = ["rev-list", "merge-tree", "commit-tree", "push"] - for fetches, names in (([LOCKED, ""], ["fetch", "fetch", "ls-remote", *merge, "fetch"]), (["", LOCKED, ""], ["fetch", "ls-remote", *merge, "fetch", "fetch"])): + for fetches, names in (([LOCKED, ""], ["ls-remote", "fetch", "fetch", *merge, "fetch"]), (["", LOCKED, ""], ["ls-remote", "fetch", *merge, "fetch", "fetch"])): with self.subTest(fetches=fetches): calls: list[tuple[str, ...]] = [] self.assertEqual(levelled("ABC-1", "origin/main", run=scripted(calls, fetch=list(fetches))), "Merged origin/main into origin/ABC-1.") @@ -161,10 +174,10 @@ class Levelled(unittest.TestCase): def test_a_fetch_that_fails_twice_fails_with_gits_reason(self) -> None: calls: list[tuple[str, ...]] = [] self.assertEqual(levelled("ABC-1", "origin/main", run=scripted(calls, fetch=LOCKED)), LOCKED) - self.assertEqual([args[0] for args in calls], ["fetch", "fetch"]) + self.assertEqual([args[0] for args in calls], ["ls-remote", "fetch", "fetch"]) def test_every_call_to_origin_runs_in_the_checkout_and_stops_after_the_timeout_given(self) -> None: - for answers, origin_calls in (({"ls_remote": ""}, ["fetch", "ls-remote", "push", "fetch"]), ({"push": Failure("git push: rejected")}, ["fetch", "ls-remote", "push", "fetch", "ls-remote", "push"]), ({"fetch": [LOCKED, ""], "ls_remote": ""}, ["fetch", "fetch", "ls-remote", "push", "fetch"]), ({"fetch": ["", LOCKED, ""], "ls_remote": ""}, ["fetch", "ls-remote", "push", "fetch", "fetch"])): + for answers, origin_calls in (({"ls_remote": ""}, ["ls-remote", "fetch", "push", "fetch"]), ({"push": Failure("git push: rejected")}, ["ls-remote", "fetch", "push", "ls-remote", "fetch", "push"]), ({"fetch": [LOCKED, ""], "ls_remote": ""}, ["ls-remote", "fetch", "fetch", "push", "fetch"]), ({"fetch": ["", LOCKED, ""], "ls_remote": ""}, ["ls-remote", "fetch", "push", "fetch", "fetch"])): with self.subTest(origin_calls=origin_calls): timed: list[tuple[str, float | None, str | None]] = [] answering = scripted([], **answers)