Repository navigation
Make an interrupted worktree checkout retryable #1934
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 |
|---|---|---|
|
|
@@ -2,7 +2,9 @@ | |
|
|
||
| from __future__ import annotations | ||
|
|
||
| import shlex | ||
| import subprocess | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
@@ -92,7 +94,7 @@ def interleave(argv, **kwargs): | |
| assert base_section.strip().splitlines() == [f"main @ {upstream}", f"Base: {upstream}"] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("state", ["same-base", "dirty", "moved-base"]) | ||
| @pytest.mark.parametrize("state", ["same-base", "dirty", "moved-base", "detached"]) | ||
| def test_start_retries_after_git_creates_worktree(repo, monkeypatch, state): | ||
| repo = repo.rename(repo.with_name("repo\rreturn")) | ||
| base = commit_on_origin(repo, "base-change") | ||
|
|
@@ -101,7 +103,8 @@ def test_start_retries_after_git_creates_worktree(repo, monkeypatch, state): | |
|
|
||
| def interrupt_after_add(cwd, *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: Test helper name no longer matches what it interrupts
|
||
| output = run_git(cwd, *args, **kwargs) | ||
| if args[:2] == ("worktree", "add"): | ||
| interrupted_step = ("worktree", "add") if state == "detached" else ("switch", "--quiet") | ||
| if args[:2] == interrupted_step: | ||
| raise KeyboardInterrupt | ||
| return output | ||
|
|
||
|
|
@@ -110,9 +113,10 @@ def interrupt_after_add(cwd, *args, **kwargs): | |
| with pytest.raises(KeyboardInterrupt): | ||
| worktree.start(repo, "my-task", "main") | ||
| task = worktree.task_file(wt) | ||
| assert (worktree.branch(wt), worktree.head(wt)) == ("lz/my-task", base) | ||
| current_branch = git(wt, "branch", "--show-current").strip() | ||
| assert (current_branch, worktree.head(wt)) == ("" if state == "detached" else "lz/my-task", base) | ||
| assert not task.exists() | ||
| if state == "same-base": | ||
| if state in {"same-base", "detached"}: | ||
| assert worktree.start(repo, "my-task", "main") == wt | ||
| assert worktree.base_sha(wt) == base | ||
| text = task.read_text() | ||
|
|
@@ -306,3 +310,24 @@ def test_abandoned_worktree_guide(repo: Path, tmp_path: Path, state: str) -> Non | |
| assert git(repo, "ls-remote", "origin", "refs/heads/lz/abandoned").split() == [ | ||
| head, "refs/heads/lz/abandoned" | ||
| ] | ||
|
|
||
|
|
||
| def test_start_retries_after_git_checkout_interrupt(repo, tmp_path): | ||
| (repo / ".gitattributes").write_text("README.md filter=paused\n") | ||
| (repo / "workflow.toml").write_text('[repo]\nname="owner/repo"\n') | ||
| git(repo, "add", ".gitattributes", "workflow.toml") | ||
| git(repo, "commit", "-qm", "add checkout barrier") | ||
| git(repo, "push", "-q", "origin", "main") | ||
| pid_file, entered = tmp_path / "starter.pid", tmp_path / "checkout-entered" | ||
| git(repo, "config", "filter.paused.smudge", | ||
| f'cat >/dev/null; : > {shlex.quote(str(entered))}; ' | ||
| f'kill -INT "$(cat {shlex.quote(str(pid_file))})"; exec sleep 30') | ||
| start = ("import os, sys; from pathlib import Path; from loopzero.cli import main; " | ||
| "Path(sys.argv[1]).write_text(str(os.getpid())); sys.exit(main(['start', 'paused']))") | ||
| command = [sys.executable, "-c", start, str(pid_file)] | ||
|
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: End-to-end interrupt test uses tight subprocess timeouts Each run of |
||
| first = subprocess.run(command, cwd=repo, capture_output=True, timeout=8, check=False) | ||
| assert entered.exists() and first.returncode in (-2, 130) | ||
| git(repo, "config", "--unset", "filter.paused.smudge") | ||
| resumed = subprocess.run(command, cwd=repo, capture_output=True, timeout=8, check=False) | ||
| assert resumed.returncode == 0, resumed.stderr | ||
| assert worktree.task_file(worktree.worktree_dir(repo, "paused")).is_file() | ||
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: An interrupt inside
git switch -ccan still leave the slug stuckgit switch -cdoes two separate things: it createsrefs/heads/<branch>, then it updates HEAD.defer_sigtermholds back SIGTERM, but not SIGINT or the 5 s SIGKILL fallback. A Ctrl-C that lands between those two steps leaves the worktree detached while the branch already exists. On retry,_resumable_checkoutreturns None because_branch_exists(repo_root, expected_branch)is true (line 297). The operator then gets the generic "worktree directory already exists" refusal, which is the stuck-slug problem this change is meant to fix. The same state follows a concurrent branch creation between the up-front_branch_existscheck andswitch -c. Before this change,worktree add -bfailed without creating the directory, so this path is new. The window is small. Smallest fix: in the detached case, accept an existing branch when it resolves to the detached HEAD and no other worktree has it checked out, then finish withgit switch <branch>instead of-c. Alternatively, give a specific refusal that says how to recover.