Repository navigation
Keep start from following redirected Git destinations #1604
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -101,10 +101,12 @@ def _branch_exists(repo_root: Path, branch: str) -> bool: | |
|
|
||
|
|
||
| def _ensure_excludes(repo_root: Path) -> None: | ||
| common = Path(_git(repo_root, *_ABSOLUTE_COMMON_DIR)) | ||
| exclude = common / "info" / "exclude" | ||
| common = Path(_git(repo_root, *_ABSOLUTE_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 "" | ||
| missing = [entry for entry in EXCLUDES if entry not in existing.splitlines()] | ||
| if missing: | ||
|
|
@@ -134,9 +136,14 @@ 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}" | ||
| lookup = ("ls-remote", "--refs", "origin", remote_ref) | ||
| try: | ||
| _git(repo_root, "fetch", "--quiet", "origin", f"+refs/heads/{base_branch}:{base_ref}") | ||
| advertised = _git(repo_root, *lookup) | ||
| base, _, ref = advertised.partition("\t") | ||
| if ref != 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) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Fetching the ls-remote SHA can fail if origin moves between the two calls Start now looks up the tip with |
||
| except GitError: | ||
| if "origin" not in _git(repo_root, "remote").splitlines(): | ||
| raise WorktreeError( | ||
|
|
@@ -145,7 +152,7 @@ def start(repo_root: Path, slug: str, base_branch: str) -> Path: | |
| "with `git remote rename <name> 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: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: The interleaving fetch in the test is now dead weight
|
||
| 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 | ||
|
|
||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Missing-base message says 'unique' when origin has no such branch
When the base branch does not exist on origin,
ls-remotesucceeds with empty output, and the user seesorigin did not report a unique refs/heads/<base>. The common case is that the branch is missing, not that it is ambiguous, so 'unique' is misleading. Smallest fix: whenadvertisedis empty, report something likeorigin has no branch <base>; check [repo] base_branch, and keep the 'unique' wording only when several lines match.