Skip to content

The agent that grows with you

fix(worktree): don't count .worktreeinclude symlinks as uncommitted changes

kshitijk4poorebb876bb7468
diff --git a/hermes_cli/worktree_ops.py b/hermes_cli/worktree_ops.py
index b77160940bd..3837d8c5ccc 100644
--- a/hermes_cli/worktree_ops.py
+++ b/hermes_cli/worktree_ops.py
@@ -508,11 +508,54 @@ def _worktree_has_unpushed_commits(worktree_path: str, timeout: int = 10) -> boo
return True
+def _include_symlink_paths(worktree_path: str, timeout: int = 10) -> set:
+ """Relative paths in *worktree_path* that are ``.worktreeinclude`` directory symlinks.
+
+ ``_copy_worktree_includes`` symlinks included directories back to the main checkout. A
+ trailing-slash gitignore pattern (``node_modules/``) never matches a symlink, so git reports
+ each one as untracked. Only a symlink at a listed entry that resolves to that same entry in
+ the main repo qualifies — anything else is real user state.
+ """
+ common = _git_out(["rev-parse", "--path-format=absolute", "--git-common-dir"], worktree_path,
+ timeout=timeout)
+ if not common:
+ return set()
+ repo_root = Path(common).parent
+ include_file = repo_root / ".worktreeinclude"
+ if not include_file.is_file():
+ return set()
+ wt = Path(worktree_path)
+ paths = set()
+ for line in include_file.read_text(encoding="utf-8-sig", errors="replace").splitlines():
+ entry = line.strip().replace("\\", "/").rstrip("/")
+ if not entry or entry.startswith("#"):
+ continue
+ dst = wt / entry
+ try:
+ if dst.is_symlink() and dst.resolve() == (repo_root / entry).resolve():
+ paths.add(entry)
+ except (OSError, ValueError):
+ continue
+ return paths
+
+
def _worktree_is_dirty(worktree_path: str, timeout: int = 10) -> bool:
- """Whether a worktree has staged/unstaged/untracked changes. Fails SAFE toward True."""
+ """Whether a worktree has staged/unstaged/untracked changes. Fails SAFE toward True.
+
+ Untracked ``.worktreeinclude`` directory symlinks are ignored: they are our own scaffolding,
+ and counting them would keep every worktree of such a repo forever.
+ """
try:
- status = _git_out(["status", "--porcelain"], worktree_path, timeout=timeout)
- return status is None or bool(status)
+ result = _git(["status", "--porcelain", "-z"], worktree_path, timeout=timeout)
+ if result.returncode != 0:
+ return True
+ entries = [e for e in result.stdout.split("\0") if e]
+ if not entries:
+ return False
+ if any(not e.startswith("?? ") for e in entries):
+ return True
+ include_links = _include_symlink_paths(worktree_path, timeout=timeout)
+ return any(e[3:].rstrip("/") not in include_links for e in entries)
except Exception:
return True
diff --git a/tests/hermes_cli/test_worktree.py b/tests/hermes_cli/test_worktree.py
index 18ad157a0ec..b391da51f8d 100644
--- a/tests/hermes_cli/test_worktree.py
+++ b/tests/hermes_cli/test_worktree.py
@@ -73,17 +73,29 @@ def git_repo_no_remote(tmp_path):
# ---------------------------------------------------------------------------
-def test_exit_cleanup_preserves_dirty_worktree(git_repo, monkeypatch, capsys):
- """Exit cleanup must preserve local changes for manual recovery."""
[email protected]("real_untracked", [True, False], ids=["untracked-file-kept", "include-symlink-only-removed"])
+def test_exit_cleanup_preserves_dirty_worktree(git_repo, monkeypatch, capsys, real_untracked):
+ """Exit cleanup must preserve local changes, but not count .worktreeinclude symlinks as changes.
+
+ A trailing-slash ignore (``node_modules/``) never matches the include step's symlink, so git
+ lists it as untracked; treating that as dirty would keep every worktree of such a repo forever.
+ """
import cli
+ (git_repo / "node_modules").mkdir()
+ (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 / "untracked.txt").write_text("recover me\n")
+ 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)
@@ -94,11 +106,16 @@ def test_exit_cleanup_preserves_dirty_worktree(git_repo, monkeypatch, capsys):
"repo_root": str(git_repo),
})
- assert worktree.exists()
- assert (worktree / "untracked.txt").read_text() == "recover me\n"
out = "".join(printed) + capsys.readouterr().out
- assert "uncommitted changes" in out
- assert "--force" not in out
+ if real_untracked:
+ assert worktree.exists()
+ assert (worktree / "untracked.txt").read_text() == "recover me\n"
+ assert "uncommitted changes" in out
+ assert "--force" not in out
+ else:
+ assert not worktree.exists()
+ assert "cleaned up" in out
+ assert (git_repo / "node_modules" / "dep.js").exists()