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
8 changes: 4 additions & 4 deletions src/loopzero/worktree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")

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: Fetch still overwrites the shared FETCH_HEAD

start no longer reads FETCH_HEAD, but its fetch still writes it. A user or another tool in the same checkout that runs git fetch X && git merge FETCH_HEAD can therefore have its FETCH_HEAD replaced by loopzero's base fetch, which is the same race in the other direction. candidate.py:358 already passes --no-write-fetch-head. Smallest fix: add --no-write-fetch-head to this fetch so both fetch sites behave the same way. Not blocking.

except GitError:
if "origin" not in _git(repo_root, "remote").splitlines():
raise WorktreeError(
Expand All @@ -140,16 +141,15 @@ def start(repo_root: Path, slug: str, base_branch: str) -> Path:
"with `git remote rename <name> 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:
raise WorktreeError(refusal)
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:
Expand Down
14 changes: 11 additions & 3 deletions tests/test_worktree.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,24 @@ 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"
origin = (repo.parent / "origin.git").as_uri()
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":

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: Interleave hook catches every _git call that starts with fetch

interleave runs the competing fetch after any _git(..., "fetch", ...). Today start makes only one fetch call, so the test is correct. If a second fetch is added later, the hook will quietly fire again. Matching the exact base fetch (for example args[:1] == ("fetch",) and f"refs/heads/main" in args[-1]) or asserting the hook fired exactly once would keep the test aimed at the race it reproduces. I confirmed the test fails on the old code: in both parametrizations side points at the pre-commit_on_origin main, so a FETCH_HEAD-based base would differ from upstream.

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"
Expand Down
Loading