diff --git a/src/loopzero/worktree.py b/src/loopzero/worktree.py index 8dce414f..93966fe4 100644 --- a/src/loopzero/worktree.py +++ b/src/loopzero/worktree.py @@ -130,8 +130,9 @@ def start(repo_root: Path, slug: str, base_branch: str) -> Path: raise WorktreeError(refusal) if not existing and _branch_exists(repo_root, branch): raise WorktreeError(f"branch already exists: {branch}") + base_ref = f"refs/remotes/origin/{base_branch}" try: - _git(repo_root, "fetch", "--quiet", "origin", f"refs/heads/{base_branch}") + _git(repo_root, "fetch", "--quiet", "origin", f"+refs/heads/{base_branch}:{base_ref}") except GitError: if "origin" not in _git(repo_root, "remote").splitlines(): raise WorktreeError( @@ -140,8 +141,7 @@ def start(repo_root: Path, slug: str, base_branch: str) -> Path: "with `git remote rename origin`." ) from None raise - base_ref = f"origin/{base_branch}" - base = _git(repo_root, "rev-parse", "--verify", "FETCH_HEAD^{commit}") + base = _git(repo_root, "rev-parse", "--verify", f"{base_ref}^{{commit}}") if existing: existing_head = _resumable_head(repo_root, wt, branch) if existing_head is None: @@ -149,7 +149,7 @@ def start(repo_root: Path, slug: str, base_branch: str) -> Path: if existing_head != base: raise WorktreeError( f"cannot resume start at {wt}: existing HEAD {existing_head} does not match " - f"{base_ref} at {base}; run loopzero start with a new slug" + f"origin/{base_branch} at {base}; run loopzero start with a new slug" ) _ensure_excludes(repo_root) if not existing: diff --git a/tests/test_worktree.py b/tests/test_worktree.py index 90460bac..14aa39e2 100644 --- a/tests/test_worktree.py +++ b/tests/test_worktree.py @@ -51,9 +51,9 @@ def test_branch_refuses_detached_head(repo: Path) -> None: @pytest.mark.parametrize("branch", [None, "feature/ci+release"]) -def test_start_creates_branch_worktree_and_task_file(repo: Path, branch: str | None) -> None: - if branch: - git(repo, "push", "-q", "origin", f"HEAD:refs/heads/{branch}") +def test_start_creates_branch_worktree_and_task_file(repo: Path, branch: str | None, monkeypatch) -> None: + side = branch or "side" + git(repo, "push", "-q", "origin", f"HEAD:refs/heads/{side}") upstream = commit_on_origin(repo, "feature.txt") if branch: checkout = repo.parent / "single-branch" @@ -61,6 +61,14 @@ def test_start_creates_branch_worktree_and_task_file(repo: Path, branch: str | N git(repo, "clone", "-q", "--single-branch", "--branch", branch, origin, str(checkout)) assert not git(checkout, "for-each-ref", "refs/heads/main", "refs/remotes/origin/main").strip() repo = checkout + invoke = worktree._git + def interleave(path, *args, **kwargs): + result = invoke(path, *args, **kwargs) + if args[0] == "fetch": + git(path, "fetch", "-q", "origin", f"refs/heads/{side}") + return result + + monkeypatch.setattr(worktree, "_git", interleave) wt = worktree.start(repo, "my-task", "main") assert wt == repo / ".worktrees" / "my-task" assert (wt / "feature.txt").exists(), "fetched origin/main before branching"