Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

fix: exclude VCS metadata directories from copy-ignored (#1250)

worktrunk-bot070e23f6e39a
diff --git a/docs/content/step.md b/docs/content/step.md
index 95d15d703..cd09ce116 100644
--- a/docs/content/step.md
+++ b/docs/content/step.md
@@ -299,7 +299,7 @@ target/
- Handles nested `.gitignore` files, global excludes, and `.git/info/exclude`
- Skips existing files by default (safe to re-run)
- `--force` overwrites existing files in the destination
-- Skips `.git` entries and other worktrees
+- Skips `.git` entries, VCS metadata directories (`.jj`, `.hg`, etc.), and other worktrees
### Performance
diff --git a/skills/worktrunk/reference/step.md b/skills/worktrunk/reference/step.md
index a21aaccce..34d068247 100644
--- a/skills/worktrunk/reference/step.md
+++ b/skills/worktrunk/reference/step.md
@@ -279,7 +279,7 @@ target/
- Handles nested `.gitignore` files, global excludes, and `.git/info/exclude`
- Skips existing files by default (safe to re-run)
- `--force` overwrites existing files in the destination
-- Skips `.git` entries and other worktrees
+- Skips `.git` entries, VCS metadata directories (`.jj`, `.hg`, etc.), and other worktrees
### Performance
diff --git a/src/cli/step.rs b/src/cli/step.rs
index 71b1d41a7..f091640e1 100644
--- a/src/cli/step.rs
+++ b/src/cli/step.rs
@@ -260,7 +260,7 @@ target/
- Handles nested `.gitignore` files, global excludes, and `.git/info/exclude`
- Skips existing files by default (safe to re-run)
- `--force` overwrites existing files in the destination
-- Skips `.git` entries and other worktrees
+- Skips `.git` entries, VCS metadata directories (`.jj`, `.hg`, etc.), and other worktrees
## Performance
diff --git a/src/commands/step_commands.rs b/src/commands/step_commands.rs
index a0cc2f2cf..dd0a2a513 100644
--- a/src/commands/step_commands.rs
+++ b/src/commands/step_commands.rs
@@ -542,13 +542,21 @@ pub fn step_diff(target: Option<&str>, extra_args: &[String]) -> anyhow::Result<
Ok(())
}
+/// VCS metadata directories that should never be copied between worktrees.
+///
+/// These directories contain internal state tied to a specific working directory.
+/// Git's own `.git` is implicitly excluded (git ls-files never reports it), but
+/// other VCS tools colocated with git need explicit exclusion.
+const VCS_METADATA_DIRS: &[&str] = &[".jj", ".hg", ".svn", ".sl", ".bzr", ".pijul"];
+
/// List gitignored entries in a worktree, filtered by `.worktreeinclude` and excluding
-/// entries that contain nested worktrees.
+/// VCS metadata directories and entries that contain nested worktrees.
///
-/// Combines three steps:
+/// Combines four steps:
/// 1. `list_ignored_entries()` — git ls-files for ignored entries
/// 2. `.worktreeinclude` filtering — only matching entries if the file exists
-/// 3. Nested worktree filtering — exclude entries containing other worktrees
+/// 3. VCS metadata filtering — exclude directories like `.jj`, `.hg`
+/// 4. Nested worktree filtering — exclude entries containing other worktrees
fn list_and_filter_ignored_entries(
worktree_path: &Path,
context: &str,
@@ -577,9 +585,22 @@ fn list_and_filter_ignored_entries(
ignored_entries
};
- // Filter out entries that contain other worktrees
+ // Filter out VCS metadata directories and entries that contain other worktrees
Ok(filtered
.into_iter()
+ .filter(|(entry_path, is_dir)| {
+ // Skip VCS metadata directories (.jj, .hg, etc.) — these contain
+ // internal state tied to a specific working directory
+ if *is_dir
+ && entry_path
+ .file_name()
+ .and_then(|n| n.to_str())
+ .is_some_and(|name| VCS_METADATA_DIRS.contains(&name))
+ {
+ return false;
+ }
+ true
+ })
.filter(|(entry_path, _)| {
!worktree_paths
.iter()
diff --git a/tests/integration_tests/step_copy_ignored.rs b/tests/integration_tests/step_copy_ignored.rs
index 72f75ada1..461bdf349 100644
--- a/tests/integration_tests/step_copy_ignored.rs
+++ b/tests/integration_tests/step_copy_ignored.rs
@@ -947,6 +947,56 @@ fn test_copy_ignored_error_includes_path_file(mut repo: TestRepo) {
);
}
+/// Test that VCS metadata directories are excluded from copy-ignored (GitHub issue #1249)
+///
+/// VCS metadata directories like `.jj` (Jujutsu), `.hg` (Mercurial) contain internal
+/// state tied to a specific working directory. Copying them between worktrees breaks
+/// the colocated VCS.
+#[rstest]
+fn test_copy_ignored_skips_vcs_metadata_dirs(mut repo: TestRepo) {
+ let feature_path = repo.add_worktree("feature");
+
+ // Create VCS metadata directories that are gitignored
+ let jj_dir = repo.root_path().join(".jj");
+ fs::create_dir_all(jj_dir.join("repo")).unwrap();
+ fs::write(jj_dir.join("repo/store"), "jj internal state").unwrap();
+
+ let hg_dir = repo.root_path().join(".hg");
+ fs::create_dir_all(&hg_dir).unwrap();
+ fs::write(hg_dir.join("dirstate"), "hg internal state").unwrap();
+
+ // Also create a regular ignored file that SHOULD be copied
+ fs::write(repo.root_path().join(".env"), "SECRET=value").unwrap();
+
+ fs::write(repo.root_path().join(".gitignore"), ".jj/\n.hg/\n.env\n").unwrap();
+
+ // Run copy-ignored
+ assert_cmd_snapshot!(make_snapshot_cmd(
+ &repo,
+ "step",
+ &["copy-ignored"],
+ Some(&feature_path),
+ ));
+
+ // Verify: .env was copied (regular ignored file)
+ assert!(
+ feature_path.join(".env").exists(),
+ ".env should be copied to destination"
+ );
+
+ // Verify: .jj was NOT copied (VCS metadata)
+ assert!(
+ !feature_path.join(".jj").exists(),
+ ".jj directory should NOT be copied (VCS metadata)"
+ );
+
+ // Verify: .hg was NOT copied (VCS metadata)
+ assert!(
+ !feature_path.join(".hg").exists(),
+ ".hg directory should NOT be copied (VCS metadata)"
+ );
+}
+
/// Test that worktrees nested inside the source are not copied (GitHub issue #641)
///
/// When worktree-path is configured to place worktrees inside the primary worktree
diff --git a/tests/snapshots/integration__integration_tests__step_copy_ignored__copy_ignored_skips_vcs_metadata_dirs.snap b/tests/snapshots/integration__integration_tests__step_copy_ignored__copy_ignored_skips_vcs_metadata_dirs.snap
new file mode 100644
index 000000000..8c3590561
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__step_copy_ignored__copy_ignored_skips_vcs_metadata_dirs.snap
@@ -0,0 +1,45 @@
+---
+source: tests/integration_tests/step_copy_ignored.rs
+info:
+ program: wt
+ args:
+ - step
+ - copy-ignored
+ env:
+ APPDATA: "[TEST_CONFIG_HOME]"
+ CLICOLOR_FORCE: "1"
+ COLUMNS: "500"
+ GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
+ GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
+ GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"
+ GIT_CONFIG_SYSTEM: /dev/null
+ GIT_EDITOR: ""
+ GIT_TERMINAL_PROMPT: "0"
+ HOME: "[TEST_HOME]"
+ LANG: C
+ LC_ALL: C
+ MOCK_CONFIG_DIR: "[MOCK_CONFIG_DIR]"
+ NO_COLOR: ""
+ PATH: "[PATH]"
+ PSModulePath: ""
+ RUST_LOG: warn
+ SHELL: ""
+ TERM: alacritty
+ USERPROFILE: "[TEST_HOME]"
+ WORKTRUNK_APPROVALS_PATH: "[TEST_APPROVALS]"
+ WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
+ WORKTRUNK_SYSTEM_CONFIG_PATH: "[TEST_SYSTEM_CONFIG]"
+ WORKTRUNK_TEST_CLAUDE_INSTALLED: "0"
+ WORKTRUNK_TEST_DELAYED_STREAM_MS: "-1"
+ WORKTRUNK_TEST_EPOCH: "1735776000"
+ WORKTRUNK_TEST_NUSHELL_ENV: "0"
+ WORKTRUNK_TEST_POWERSHELL_ENV: "0"
+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"
+ XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
+---
+success: true
+exit_code: 0
+----- stdout -----
+
+----- stderr -----
+✓ Copied 1 entry