Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix(copy-ignored): let directory patterns cover files inside the directory (#4406)
Worktrunk Botf1ddabe7f0dc
diff --git a/src/commands/step/shared.rs b/src/commands/step/shared.rsindex ca529a0de..58d40d5e9 100644--- a/src/commands/step/shared.rs+++ b/src/commands/step/shared.rs@@ -130,7 +130,11 @@ pub(super) fn list_and_filter_ignored_entries(};ignored_entries.into_iter()- .filter(|(path, is_dir)| include_matcher.matched(path, *is_dir).is_ignore())+ .filter(|(path, is_dir)| {+ include_matcher+ .matched_path_or_any_parents(relative_entry(path, worktree_path), *is_dir)+ .is_ignore()+ }).collect()} else {ignored_entries@@ -162,11 +166,12 @@ pub(super) fn list_and_filter_ignored_entries(.into_iter().filter(|(path, is_dir)| {// Skip entries matching configured exclude patterns- if let Some(ref matcher) = exclude_matcher {- let relative = path.strip_prefix(worktree_path).unwrap_or(path.as_path());- if matcher.matched(relative, *is_dir).is_ignore() {- return false;- }+ if let Some(ref matcher) = exclude_matcher+ && matcher+ .matched_path_or_any_parents(relative_entry(path, worktree_path), *is_dir)+ .is_ignore()+ {+ return false;}// Skip built-in excluded directories (.jj, .hg, .worktrees, etc.)if *is_dir@@ -189,6 +194,18 @@ pub(super) fn list_and_filter_ignored_entries(.collect())}+/// An entry's path relative to its worktree, for gitignore matching.+///+/// Matching walks the entry's parents (`matched_path_or_any_parents`) because+/// `git ls-files --directory` lists an ignored file individually when its+/// directory also holds tracked files, so a `config/` pattern has to reach+/// `config/local.yml` through its parent, as it would in a `.gitignore`.+/// Every entry is `worktree_path.join(..)` (see `list_ignored_entries`), so the+/// strip always succeeds.+fn relative_entry<'a>(path: &'a Path, worktree_path: &Path) -> &'a Path {+ path.strip_prefix(worktree_path).unwrap_or(path)+}+/// List ignored entries using git ls-files////// Uses `git ls-files -z --ignored --exclude-standard -o --directory` which:diff --git a/tests/integration_tests/step_copy_ignored.rs b/tests/integration_tests/step_copy_ignored.rsindex f2e66f10a..a53a1a1a9 100644--- a/tests/integration_tests/step_copy_ignored.rs+++ b/tests/integration_tests/step_copy_ignored.rs@@ -213,6 +213,58 @@ fn test_copy_ignored_skips_built_in_excluded_dirs(mut repo: TestRepo) {);}+/// Plant an ignored `config/local.yml` beside a tracked `config/app.yml`.+/// With a tracked sibling, `git ls-files --directory` lists the ignored file+/// itself rather than collapsing it to `config/`, so a `config/` pattern has+/// to reach it through its parent directory.+fn setup_ignored_file_in_tracked_dir(repo: &mut TestRepo) -> PathBuf {+ let config_dir = repo.root_path().join("config");+ fs::create_dir_all(&config_dir).unwrap();+ fs::write(config_dir.join("app.yml"), "tracked").unwrap();+ fs::write(repo.root_path().join(".gitignore"), "config/local.yml\n").unwrap();+ repo.run_git(&["add", "config/app.yml", ".gitignore"]);+ repo.run_git(&["commit", "-m", "Add tracked config"]);+ fs::write(config_dir.join("local.yml"), "secret").unwrap();+ repo.add_worktree("feature")+}++/// A `.worktreeinclude` directory pattern covers the ignored files inside+/// that directory, as it would in a `.gitignore`.+#[rstest]+fn test_copy_ignored_worktreeinclude_directory_pattern_covers_files(mut repo: TestRepo) {+ let feature_path = setup_ignored_file_in_tracked_dir(&mut repo);+ fs::write(repo.root_path().join(".worktreeinclude"), "config/\n").unwrap();++ run_copy_ignored_single_entry(&repo, &feature_path);++ assert_eq!(+ fs::read_to_string(feature_path.join("config/local.yml")).unwrap(),+ "secret"+ );+}++/// A `[step.copy-ignored].exclude` directory pattern skips the ignored files+/// inside that directory.+#[rstest]+fn test_copy_ignored_exclude_directory_pattern_covers_files(mut repo: TestRepo) {+ let feature_path = setup_ignored_file_in_tracked_dir(&mut repo);+ fs::write(repo.root_path().join(".env"), "SECRET=value").unwrap();+ fs::write(+ repo.root_path().join(".gitignore"),+ "config/local.yml\n.env\n",+ )+ .unwrap();+ repo.write_test_config("[step.copy-ignored]\nexclude = [\"config/\"]\n");++ run_copy_ignored_single_entry(&repo, &feature_path);++ assert!(feature_path.join(".env").exists(), ".env should be copied");+ assert!(+ !feature_path.join("config/local.yml").exists(),+ "config/local.yml should be excluded by the config/ pattern"+ );+}+/// Test error handling when .worktreeinclude has invalid syntax#[rstest]fn test_copy_ignored_invalid_worktreeinclude(mut repo: TestRepo) {