Skip to content

The agent that grows with you

refactor(cli): clear _active_worktree once in _cleanup_worktree

kshitijk4poorf6465a307967
diff --git a/cli.py b/cli.py
index bc5feb3b954..c5c480e26ea 100644
--- a/cli.py
+++ b/cli.py
@@ -586,11 +586,11 @@ def _cleanup_worktree(info: Dict[str, str] = None) -> None:
wt_path, branch, repo_root = info["path"], info["branch"], info["repo_root"]
if not Path(wt_path).exists():
return
+ _active_worktree = None
if _worktree_is_dirty(wt_path, timeout=10):
# Uncommitted work: say so, and don't hint at `remove --force`, which would destroy it.
_cprint(f"\n\033[33m{_t('cli.worktree.uncommitted_keeping', path=wt_path)}\033[0m")
- _active_worktree = None
return
if _worktree_has_unpushed_commits(wt_path, timeout=10):
@@ -601,7 +601,6 @@ def _cleanup_worktree(info: Dict[str, str] = None) -> None:
else:
_cprint(f"\n\033[33m{_t('cli.worktree.unpushed_keeping', path=wt_path)}\033[0m")
print(f" {_t('cli.worktree.clean_up_manually', path=wt_path)}")
- _active_worktree = None
return
# Release the tree's language servers while the path still exists, then unlock so `remove`
@@ -610,8 +609,6 @@ def _cleanup_worktree(info: Dict[str, str] = None) -> None:
_git_quiet(["worktree", "unlock", wt_path], repo_root, log="git worktree unlock failed (non-fatal)")
_git_quiet(["worktree", "remove", wt_path, "--force"], repo_root, timeout=15, log="Failed to remove worktree")
_git_quiet(["branch", "-D", branch], repo_root, log=f"Failed to delete branch {branch}")
-
- _active_worktree = None
_cprint(f"\033[32m{_t('cli.worktree.cleaned_up', path=wt_path)}\033[0m")
diff --git a/tests/hermes_cli/test_worktree.py b/tests/hermes_cli/test_worktree.py
index b391da51f8d..5bdbcd85ffe 100644
--- a/tests/hermes_cli/test_worktree.py
+++ b/tests/hermes_cli/test_worktree.py
@@ -86,16 +86,9 @@ def test_exit_cleanup_preserves_dirty_worktree(git_repo, monkeypatch, capsys, re
(git_repo / "node_modules" / "dep.js").write_text("x\n")
(git_repo / ".worktreeinclude").write_text("node_modules\n")
(git_repo / ".git" / "info" / "exclude").write_text("node_modules/\n.worktreeinclude\n.worktrees/\n")
- worktree = git_repo / ".worktrees" / "session"
- worktree.parent.mkdir()
- subprocess.run(
- ["git", "worktree", "add", str(worktree), "-b", "hermes/session", "HEAD"],
- cwd=git_repo, check=True, capture_output=True,
- )
+ worktree = TestWorktreeLockReaping._mk(cli, git_repo, "session", dirty=real_untracked)
worktree_ops._copy_worktree_includes(str(git_repo), worktree)
assert (worktree / "node_modules").is_symlink()
- if real_untracked:
- (worktree / "untracked.txt").write_text("recover me\n")
monkeypatch.setattr(cli, "release_lsp_clients", lambda path: None)
printed = []
monkeypatch.setattr(cli, "_cprint", printed.append)
@@ -109,7 +102,7 @@ def test_exit_cleanup_preserves_dirty_worktree(git_repo, monkeypatch, capsys, re
out = "".join(printed) + capsys.readouterr().out
if real_untracked:
assert worktree.exists()
- assert (worktree / "untracked.txt").read_text() == "recover me\n"
+ assert (worktree / "dirty.txt").read_text() == "uncommitted"
assert "uncommitted changes" in out
assert "--force" not in out
else: