From 1e0c49775fcc5a894d47746841e34d3e863d2a38 Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 08:05:25 +0100 Subject: [PATCH 1/2] Keep start from following redirected Git destinations Architecture-Review: unchanged; Start still fetches the configured base and creates the task checkout. It now binds both preparation writes to their intended Git destinations. Co-Authored-By: Codex --- src/loopzero/worktree.py | 19 +++++++++++++------ tests/test_worktree.py | 27 ++++++++++++++++++--------- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/loopzero/worktree.py b/src/loopzero/worktree.py index 008a7cde..c2c15567 100644 --- a/src/loopzero/worktree.py +++ b/src/loopzero/worktree.py @@ -102,10 +102,13 @@ def _branch_exists(repo_root: Path, branch: str) -> bool: def _ensure_excludes(repo_root: Path) -> None: - common = Path(_git(repo_root, "rev-parse", "--path-format=absolute", "--git-common-dir")) - exclude = common / "info" / "exclude" + common = Path(_git(repo_root, "rev-parse", "--path-format=absolute", "--git-common-dir", + preserve_whitespace=True)).resolve() + info = (common / "info").resolve() + exclude = (info / "exclude").resolve() + if not info.is_relative_to(common) or not exclude.is_relative_to(common): + raise WorktreeError(f"Git exclude is outside its Git directory: {exclude}") exclude.parent.mkdir(parents=True, exist_ok=True) - exclude = exclude.resolve() existing = exclude.read_text() if exclude.exists() else "" lines = set(existing.splitlines()) missing = [e for e in EXCLUDES if e not in lines] @@ -136,9 +139,13 @@ 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}" + remote_ref = f"refs/heads/{base_branch}" try: - _git(repo_root, "fetch", "--quiet", "origin", f"+refs/heads/{base_branch}:{base_ref}") + advertised = _git(repo_root, "ls-remote", "--exit-code", "--refs", "origin", remote_ref) + base, _, ref = advertised.partition("\t") + if ref != remote_ref: + raise WorktreeError(f"origin did not report a unique {remote_ref}") + _git(repo_root, "fetch", "--quiet", "--no-tags", "--no-write-fetch-head", "--refmap=", "origin", base) except GitError: if "origin" not in _git(repo_root, "remote").splitlines(): raise WorktreeError( @@ -147,7 +154,7 @@ def start(repo_root: Path, slug: str, base_branch: str) -> Path: "with `git remote rename origin`." ) from None raise - base = _git(repo_root, "rev-parse", "--verify", f"{base_ref}^{{commit}}") + _git(repo_root, "update-ref", "--no-deref", f"refs/remotes/origin/{base_branch}", base) if existing: existing_head = _resumable_head(repo_root, wt, branch) if existing_head is None: diff --git a/tests/test_worktree.py b/tests/test_worktree.py index 230adc02..708c5c6e 100644 --- a/tests/test_worktree.py +++ b/tests/test_worktree.py @@ -48,18 +48,20 @@ def test_branch_refuses_detached_head(repo: Path) -> None: worktree.branch(repo) -@pytest.mark.parametrize("branch", [None, "feature/ci+release"]) +@pytest.mark.parametrize("branch", [None, "feature/ci+release", "symbolic"]) 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: + if branch == "symbolic": + git(repo, "symbolic-ref", "refs/remotes/origin/main", "refs/heads/main") + elif 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 + invoke, local_head = worktree._git, worktree.head(repo) def interleave(path, *args, **kwargs): result = invoke(path, *args, **kwargs) if args[0] == "fetch": @@ -71,7 +73,8 @@ def interleave(path, *args, **kwargs): assert wt == repo / ".worktrees" / "my-task" assert (wt / "feature.txt").exists(), "fetched origin/main before branching" assert worktree.branch(wt) == "lz/my-task" - assert worktree.head(wt) == upstream + assert worktree.head(wt) == upstream and worktree.head(repo) == local_head + assert git(repo, "rev-parse", "refs/remotes/origin/main").strip() == upstream assert worktree.base_sha(wt) == upstream text = worktree.task_text(wt) assert text.startswith("# My task\n") @@ -131,15 +134,21 @@ def interrupt_after_add(cwd, *args, **kwargs): assert task.read_text().endswith("(filled by `loopzero review`)\n") -def test_start_excludes_worktrees_and_task_dir(repo: Path) -> None: +@pytest.mark.parametrize("redirect", [None, "file", "directory"]) +def test_start_excludes_worktrees_and_task_dir(repo: Path, redirect: str | None) -> None: exclude = repo / ".git" / "info" / "exclude" - exclude.parent.mkdir(parents=True, exist_ok=True) exclude.write_text("*.log") # no trailing newline on purpose + if redirect: + link = exclude if redirect == "file" else exclude.parent + link.symlink_to(link.rename(repo.parent / "outside")) + with pytest.raises(worktree.WorktreeError, match="outside its Git directory"): + worktree.start(repo, "t1", "main") + assert exclude.read_text() == "*.log" and not (repo / ".worktrees").exists() + return old_inode = exclude.stat().st_ino wt = worktree.start(repo, "t1", "main") assert exclude.stat().st_ino != old_inode - lines = exclude.read_text().splitlines() - assert lines == ["*.log", ".worktrees/", ".loopzero/"] + assert exclude.read_text().splitlines() == ["*.log", ".worktrees/", ".loopzero/"] assert not worktree.is_dirty(wt), "fresh worktree with task.md is clean" assert git(repo, "status", "--porcelain") == "" installed = exclude.stat() @@ -199,7 +208,7 @@ def test_start_explains_missing_origin(repo: Path, change: tuple[str, ...]) -> N def test_start_reports_git_failure_with_command(repo: Path) -> None: with pytest.raises(worktree.GitError) as info: worktree.start(repo, "nobase", "does-not-exist") - assert info.value.command[:2] == ("git", "fetch") + assert info.value.command[:2] == ("git", "ls-remote") assert info.value.tail From e19029cf23db1dc973fcf3ba41848b07e96ad611 Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 08:08:40 +0100 Subject: [PATCH 2/2] Explain when origin did not advertise the configured base Architecture-Review: unchanged; start still fetches the configured base before creating the checkout. Co-Authored-By: Codex --- src/loopzero/worktree.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/loopzero/worktree.py b/src/loopzero/worktree.py index c2c15567..1e6e60c6 100644 --- a/src/loopzero/worktree.py +++ b/src/loopzero/worktree.py @@ -140,11 +140,12 @@ def start(repo_root: Path, slug: str, base_branch: str) -> Path: if not existing and _branch_exists(repo_root, branch): raise WorktreeError(f"branch already exists: {branch}") remote_ref = f"refs/heads/{base_branch}" + lookup = ("ls-remote", "--refs", "origin", remote_ref) try: - advertised = _git(repo_root, "ls-remote", "--exit-code", "--refs", "origin", remote_ref) + advertised = _git(repo_root, *lookup) base, _, ref = advertised.partition("\t") if ref != remote_ref: - raise WorktreeError(f"origin did not report a unique {remote_ref}") + raise GitError(("git", *lookup), f"origin did not report a unique {remote_ref}") _git(repo_root, "fetch", "--quiet", "--no-tags", "--no-write-fetch-head", "--refmap=", "origin", base) except GitError: if "origin" not in _git(repo_root, "remote").splitlines():