Skip to content

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

fix: `wt step copy-ignored` fails in bare repository setups (#600)

Maximilian Roos6a7d73b557cd
diff --git a/src/commands/step_commands.rs b/src/commands/step_commands.rs
index 62c925147..c2c5d5f0b 100644
--- a/src/commands/step_commands.rs
+++ b/src/commands/step_commands.rs
@@ -441,11 +441,20 @@ pub fn step_copy_ignored(
})?;
(path, branch.to_string())
}
- None => (
- repo.worktree_base()?,
- repo.default_branch()
- .ok_or_else(|| anyhow::anyhow!("Cannot determine default branch"))?,
- ),
+ None => {
+ // Default source is the default branch's worktree.
+ // For bare repos, worktree_base() returns the bare directory (which has no
+ // working tree for git ls-files). We need the actual worktree path.
+ let default_branch = repo
+ .default_branch()
+ .ok_or_else(|| anyhow::anyhow!("Cannot determine default branch"))?;
+ let path = repo.worktree_for_branch(&default_branch)?.ok_or_else(|| {
+ worktrunk::git::GitError::WorktreeNotFound {
+ branch: default_branch.clone(),
+ }
+ })?;
+ (path, default_branch)
+ }
};
let dest_path = match to {
diff --git a/tests/integration_tests/step_copy_ignored.rs b/tests/integration_tests/step_copy_ignored.rs
index 0b293efa1..bee36a2da 100644
--- a/tests/integration_tests/step_copy_ignored.rs
+++ b/tests/integration_tests/step_copy_ignored.rs
@@ -534,3 +534,67 @@ fn test_copy_ignored_to_nonexistent_worktree(repo: TestRepo) {
None,
));
}
+
+/// Test copy-ignored when default branch has no worktree
+///
+/// When the default branch (main) has no worktree, copy-ignored should error clearly
+/// rather than failing cryptically with git ls-files errors.
+#[rstest]
+fn test_copy_ignored_no_default_branch_worktree(mut repo: TestRepo) {
+ // Create a feature worktree and switch main worktree to a different branch
+ let feature_path = repo.add_worktree("feature");
+ repo.switch_primary_to("develop"); // main worktree is now on 'develop', not 'main'
+
+ // Now 'main' has no worktree. Try to copy from default (main).
+ assert_cmd_snapshot!(make_snapshot_cmd(
+ &repo,
+ "step",
+ &["copy-ignored"],
+ Some(&feature_path),
+ ));
+}
+
+/// Test copy-ignored in a bare repository setup
+///
+/// This test reproduces GitHub issue #598: `wt step copy-ignored` fails in bare repo
+/// with error "git ls-files failed: fatal: this operation must be run in a work tree"
+#[test]
+fn test_copy_ignored_bare_repo() {
+ use crate::common::{BareRepoTest, TestRepoBase, setup_temp_snapshot_settings, wt_command};
+
+ let test = BareRepoTest::new();
+
+ // Create main worktree (default branch)
+ let main_worktree = test.create_worktree("main", "main");
+ test.commit_in(&main_worktree, "Initial commit on main");
+
+ // Create a feature worktree
+ let feature_worktree = test.create_worktree("feature", "feature");
+ test.commit_in(&feature_worktree, "Feature work");
+
+ // Create .env file in main (source worktree)
+ fs::write(main_worktree.join(".env"), "SECRET=value").unwrap();
+
+ // Add .env to .gitignore in main
+ fs::write(main_worktree.join(".gitignore"), ".env\n").unwrap();
+
+ // Create .worktreeinclude in main
+ fs::write(main_worktree.join(".worktreeinclude"), ".env\n").unwrap();
+
+ // Run copy-ignored from feature worktree (copies from main by default)
+ let settings = setup_temp_snapshot_settings(test.temp_path());
+ settings.bind(|| {
+ let mut cmd = wt_command();
+ test.configure_wt_cmd(&mut cmd);
+ cmd.args(["step", "copy-ignored"])
+ .current_dir(&feature_worktree);
+
+ insta_cmd::assert_cmd_snapshot!(cmd);
+ });
+
+ // Verify file was copied
+ assert!(
+ feature_worktree.join(".env").exists(),
+ ".env should be copied to feature worktree"
+ );
+}
diff --git a/tests/snapshots/integration__integration_tests__step_copy_ignored__copy_ignored_bare_repo.snap b/tests/snapshots/integration__integration_tests__step_copy_ignored__copy_ignored_bare_repo.snap
new file mode 100644
index 000000000..4b6d95d37
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__step_copy_ignored__copy_ignored_bare_repo.snap
@@ -0,0 +1,31 @@
+---
+source: tests/integration_tests/step_copy_ignored.rs
+info:
+ program: wt
+ args:
+ - step
+ - copy-ignored
+ env:
+ CLICOLOR_FORCE: ""
+ 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"
+ LANG: C
+ LC_ALL: C
+ NO_COLOR: ""
+ RUST_LOG: warn
+ SOURCE_DATE_EPOCH: "1735776000"
+ TERM: alacritty
+ WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"
+---
+success: true
+exit_code: 0
+----- stdout -----
+
+----- stderr -----
+✓ Copied 1 entry
diff --git a/tests/snapshots/integration__integration_tests__step_copy_ignored__copy_ignored_no_default_branch_worktree.snap b/tests/snapshots/integration__integration_tests__step_copy_ignored__copy_ignored_no_default_branch_worktree.snap
new file mode 100644
index 000000000..8a2bcbae6
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__step_copy_ignored__copy_ignored_no_default_branch_worktree.snap
@@ -0,0 +1,35 @@
+---
+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
+ PATH: "[PATH]"
+ RUST_LOG: warn
+ SOURCE_DATE_EPOCH: "1735776000"
+ TERM: alacritty
+ USERPROFILE: "[TEST_HOME]"
+ WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"
+ XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
+---
+success: false
+exit_code: 1
+----- stdout -----
+
+----- stderr -----
+✗ No worktree found for branch main