diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 1d67cb81..881090cd 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -269,7 +269,7 @@ Git configuration so URL rewrites cannot change its destination. After confirmed remote deletion or absence, cleanup deletes the matching `origin/lz/` tracking ref only if it still points to the merged head. An already-merged PR can resume cleanup from its task worktree. Local removal -requires a clean worktree at the merged PR head. +requires a clean worktree at the merged PR head with no ignored user files. Hosted eligibility trusts the configured publisher's projection and live identity, state, head and latest ordering. It cannot recompute private raw or defeat a coherent forgery by a trusted publisher/editor; native claims do not attest the diff --git a/src/loopzero/worktree.py b/src/loopzero/worktree.py index 008a7cde..0c3909fc 100644 --- a/src/loopzero/worktree.py +++ b/src/loopzero/worktree.py @@ -260,12 +260,23 @@ def merges_since(wt: Path, sha: str) -> list[str]: def cleanup(wt: Path) -> None: - """Remove the worktree and its branch. Refuses when the worktree is dirty.""" + """Remove the worktree and branch if there are no local user files.""" wt = Path(wt).absolute() if not wt.exists(): raise WorktreeError(f"no such worktree: {wt}") if is_dirty(wt): raise WorktreeError(f"worktree is dirty, refusing to remove: {wt}") + ignored = _git( + wt, "ls-files", "--others", "--ignored", "--exclude-standard", "-z", preserve_newlines=True + ) + for path in ignored.split("\0")[:-1]: + if ( + (path in {".loopzero/task.md", ".loopzero/checks.json"} + or re.fullmatch(r"\.loopzero/review-[0-9a-f]{12}-(primary|delta)\.json", path)) + and not (wt / path).is_symlink() + ): + continue + raise WorktreeError(f"worktree contains ignored file {path!r}, refusing to remove: {wt}") name, gd = branch(wt), (wt / _git(wt, "rev-parse", "--git-common-dir")).resolve() _git(gd, f"--git-dir={gd}", "worktree", "remove", str(wt)) # never the primary checkout try: diff --git a/tests/test_cli.py b/tests/test_cli.py index fda149c0..9c1dd5f8 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -4715,7 +4715,7 @@ def interrupt(staged, dest): def test_already_merged_finishes_partial_cleanup(wt, repo, gh, capsys, monkeypatch, head): - (wt / ".loopzero" / "review-existing.json").write_text("{broken") + cli._review_file(wt, head, "primary").write_text("{broken") arm_pr(gh, head, isDraft=False, state="MERGED") gh.append(f"api repos/{REPO}/pulls/7", merge_json(state="MERGED", head=head, sha="c" * 40)) ref = f"api repos/{REPO}/git/ref/heads/lz/t1" diff --git a/tests/test_worktree.py b/tests/test_worktree.py index 230adc02..5bb9f003 100644 --- a/tests/test_worktree.py +++ b/tests/test_worktree.py @@ -133,13 +133,11 @@ def interrupt_after_add(cwd, *args, **kwargs): def test_start_excludes_worktrees_and_task_dir(repo: Path) -> None: exclude = repo / ".git" / "info" / "exclude" - exclude.parent.mkdir(parents=True, exist_ok=True) exclude.write_text("*.log") # no trailing newline on purpose 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() @@ -238,12 +236,14 @@ def test_cleanup_removes_worktree_and_branch(repo: Path, monkeypatch) -> None: assert not wt.exists() and not git(repo, "branch", "--list", "lz/done") and "done" not in git(repo, "worktree", "list") -def test_cleanup_refuses_dirty(repo: Path) -> None: +@pytest.mark.parametrize("file", ["scratch.txt", "local.env", ".loopzero/user-notes.md"]) +def test_cleanup_preserves_user_files(repo: Path, file: str) -> None: + (repo / ".git/info/exclude").write_text("local.env\n") wt = worktree.start(repo, "dirty", "main") - (wt / "scratch.txt").write_text("x") - with pytest.raises(worktree.WorktreeError, match="dirty"): + (wt / file).write_text("x") + with pytest.raises(worktree.WorktreeError, match="worktree is dirty" if file == "scratch.txt" else "ignored file"): worktree.cleanup(wt) - assert wt.exists() and "lz/dirty" in git(repo, "branch", "--list", "lz/dirty") + assert (wt / file).read_text() == "x" and "lz/dirty" in git(repo, "branch", "--list", "lz/dirty") def test_cleanup_missing_worktree(repo: Path) -> None: