Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Refactor: Improve `configure-shell` output and Fish shell detection
Maximilian Roos52e62ad39990
diff --git a/src/commands/configure_shell.rs b/src/commands/configure_shell.rsindex 8e14bdbff..471c57152 100644--- a/src/commands/configure_shell.rs+++ b/src/commands/configure_shell.rs@@ -7,6 +7,7 @@ pub struct ConfigureResult {pub shell: Shell,pub path: PathBuf,pub action: ConfigAction,+ pub config_line: String,}#[derive(Debug, PartialEq)]@@ -60,11 +61,24 @@ pub fn handle_configure_shell(// Find the first existing config filelet target_path = paths.iter().find(|p| p.exists());+ // For Fish, also check if the parent directory (conf.d/) exists+ // since we create the file there rather than modifying an existing one+ let has_config_location = if matches!(shell, Shell::Fish) {+ paths+ .first()+ .and_then(|p| p.parent())+ .map(|p| p.exists())+ .unwrap_or(false)+ || target_path.is_some()+ } else {+ target_path.is_some()+ };+// Track all checked paths for better error messageschecked_paths.extend(paths.iter().map(|p| (shell, p.clone())));- // Only configure if explicitly targeting this shell OR if config file exists- let should_configure = shell_filter.is_some() || target_path.is_some();+ // Only configure if explicitly targeting this shell OR if config file/location exists+ let should_configure = shell_filter.is_some() || has_config_location;if should_configure {let path = target_path.or_else(|| paths.first());@@ -144,6 +158,7 @@ fn configure_shell_file(shell,path: path.to_path_buf(),action: ConfigAction::AlreadyExists,+ config_line: config_content.to_string(),}));}}@@ -154,6 +169,7 @@ fn configure_shell_file(shell,path: path.to_path_buf(),action: ConfigAction::WouldAdd,+ config_line: config_content.to_string(),}));}@@ -171,6 +187,7 @@ fn configure_shell_file(shell,path: path.to_path_buf(),action: ConfigAction::Added,+ config_line: config_content.to_string(),}))} else {// File doesn't exist@@ -181,6 +198,7 @@ fn configure_shell_file(shell,path: path.to_path_buf(),action: ConfigAction::WouldCreate,+ config_line: config_content.to_string(),}));}@@ -199,6 +217,7 @@ fn configure_shell_file(shell,path: path.to_path_buf(),action: ConfigAction::Created,+ config_line: config_content.to_string(),}))} else {// Don't create config files for shells the user might not use@@ -222,23 +241,30 @@ fn configure_fish_file(let existing_content = fs::read_to_string(path).map_err(|e| format!("Failed to read {}: {}", path.display(), e))?;- // Check for specific patterns that indicate our integration is present- let function_pattern = format!("function {}", cmd_prefix);- let has_integration = existing_content.contains(&function_pattern);+ // Check for the init pattern (either old function-based or new eval-based)+ let init_pattern = format!("{} init", cmd_prefix);+ let has_integration = existing_content.contains(&init_pattern);if has_integration {return Ok(Some(ConfigureResult {shell,path: path.to_path_buf(),action: ConfigAction::AlreadyExists,+ config_line: content.to_string(),}));}}// File doesn't exist or doesn't have our integration- // Only create if explicitly targeting this shell (consistent with other shells)+ // For Fish, create if parent directory exists or if explicitly targeting this shell+ // This is different from other shells because Fish uses conf.d/ which may exist+ // even if the specific wt.fish file doesn'tif !explicit_shell && !path.exists() {- return Ok(None);+ // Check if parent directory exists+ let parent_exists = path.parent().map(|p| p.exists()).unwrap_or(false);+ if !parent_exists {+ return Ok(None);+ }}if dry_run {@@ -250,6 +276,7 @@ fn configure_fish_file(} else {ConfigAction::WouldCreate},+ config_line: content.to_string(),}));}@@ -259,13 +286,14 @@ fn configure_fish_file(.map_err(|e| format!("Failed to create directory {}: {}", parent.display(), e))?;}- // Write the full content- fs::write(path, content)+ // Write the config content with newline+ fs::write(path, format!("{}\n", content)).map_err(|e| format!("Failed to write to {}: {}", path.display(), e))?;Ok(Some(ConfigureResult {shell,path: path.to_path_buf(),action: ConfigAction::Created,+ config_line: content.to_string(),}))}diff --git a/src/main.rs b/src/main.rsindex 1489ee46d..c2fd6f871 100644--- a/src/main.rs+++ b/src/main.rs@@ -2,6 +2,7 @@ use clap::{CommandFactory, Parser, Subcommand};use std::process;use worktrunk::config::WorktrunkConfig;use worktrunk::git::GitError;+use worktrunk::styling::{dim_style, should_use_color};mod commands;mod display;@@ -163,6 +164,17 @@ fn main() {result.shell,result.path.display());+ // Indent each line of the config content with dim/gray color+ if should_use_color() {+ let style = dim_style();+ for line in result.config_line.lines() {+ println!(" {}{}{}", style.render(), line, style.render_reset());+ }+ } else {+ for line in result.config_line.lines() {+ println!(" {}", line);+ }+ }}}).map_err(GitError::CommandFailed),diff --git a/src/shell.rs b/src/shell.rsindex 0f92f27b6..6fd3de616 100644--- a/src/shell.rs+++ b/src/shell.rs@@ -122,17 +122,12 @@ impl Shell {/// Returns the line to add to the config file for shell integration///- /// For most shells, this is an eval statement. For Fish, this returns- /// the full integration code since it goes into a separate conf.d/ file.+ /// All shells use an eval-like pattern now.pub fn config_line(&self, cmd_prefix: &str) -> String {match self {Self::Fish => {- // Fish uses a separate file in conf.d/, so we generate the full content- ShellInit::new(*self, cmd_prefix.to_string())- .generate()- .unwrap_or_else(|_| {- format!("# Error generating fish config for {}", cmd_prefix)- })+ // Fish uses 'source' instead of 'eval'+ format!("{} init {} | source", cmd_prefix, self)}_ => {// All other shells use eval patterndiff --git a/tests/integration_tests/configure_shell.rs b/tests/integration_tests/configure_shell.rsindex 4cee75dd5..19e28871a 100644--- a/tests/integration_tests/configure_shell.rs+++ b/tests/integration_tests/configure_shell.rs@@ -159,8 +159,8 @@ fn test_configure_shell_fish() {let content = fs::read_to_string(&fish_config).unwrap();assert!(- content.contains("function wt"),- "Should contain wt function"+ content.contains("wt init fish | source"),+ "Should contain wt init command");}@@ -218,8 +218,8 @@ fn test_configure_shell_fish_custom_prefix() {let content = fs::read_to_string(&fish_config).unwrap();assert!(- content.contains("function worktree"),- "Should contain worktree function with custom prefix"+ content.contains("worktree init fish | source"),+ "Should contain worktree init command with custom prefix");}@@ -266,3 +266,140 @@ fn test_configure_shell_multiple_configs() {"Zsh config should be updated");}++/// Test configure-shell shows both shells needing updates and already configured shells+#[test]+fn test_configure_shell_mixed_states() {+ let repo = TestRepo::new();+ let temp_home = TempDir::new().unwrap();++ // Create bash config with wt already configured+ let bash_config_path = if cfg!(target_os = "macos") {+ temp_home.path().join(".bash_profile")+ } else {+ temp_home.path().join(".bashrc")+ };+ fs::write(+ &bash_config_path,+ "# Existing config\neval \"$(wt init bash)\"\n",+ )+ .unwrap();++ // Create zsh config without wt+ let zshrc_path = temp_home.path().join(".zshrc");+ fs::write(&zshrc_path, "# Existing zsh config\n").unwrap();++ let mut settings = Settings::clone_current();+ settings.set_snapshot_path("../snapshots");+ settings.add_filter(&temp_home.path().to_string_lossy(), "[TEMP_HOME]");++ settings.bind(|| {+ let mut cmd = Command::new(get_cargo_bin("wt"));+ repo.clean_cli_env(&mut cmd);+ cmd.arg("configure-shell")+ .env("HOME", temp_home.path())+ .current_dir(repo.root_path());++ assert_cmd_snapshot!("configure_shell_mixed_states", cmd);+ });++ // Verify bash was not modified (already configured)+ let bash_content = fs::read_to_string(&bash_config_path).unwrap();+ let bash_wt_count = bash_content.matches("wt init").count();+ assert_eq!(+ bash_wt_count, 1,+ "Bash should still have exactly one wt init line"+ );++ // Verify zsh was modified+ let zsh_content = fs::read_to_string(&zshrc_path).unwrap();+ assert!(+ zsh_content.contains("eval \"$(wt init zsh)\""),+ "Zsh config should be updated"+ );+}++/// Test configure-shell --dry-run shows both shells needing updates and already configured shells+#[test]+fn test_configure_shell_mixed_states_dry_run() {+ let repo = TestRepo::new();+ let temp_home = TempDir::new().unwrap();++ // Create bash config with wt already configured+ let bash_config_path = if cfg!(target_os = "macos") {+ temp_home.path().join(".bash_profile")+ } else {+ temp_home.path().join(".bashrc")+ };+ fs::write(+ &bash_config_path,+ "# Existing config\neval \"$(wt init bash)\"\n",+ )+ .unwrap();++ // Create zsh config without wt+ let zshrc_path = temp_home.path().join(".zshrc");+ fs::write(&zshrc_path, "# Existing zsh config\n").unwrap();++ let mut settings = Settings::clone_current();+ settings.set_snapshot_path("../snapshots");+ settings.add_filter(&temp_home.path().to_string_lossy(), "[TEMP_HOME]");++ settings.bind(|| {+ let mut cmd = Command::new(get_cargo_bin("wt"));+ repo.clean_cli_env(&mut cmd);+ cmd.arg("configure-shell")+ .arg("--dry-run")+ .env("HOME", temp_home.path())+ .current_dir(repo.root_path());++ assert_cmd_snapshot!("configure_shell_mixed_states_dry_run", cmd);+ });++ // Verify files were not modified in dry-run+ let bash_content = fs::read_to_string(&bash_config_path).unwrap();+ let bash_wt_count = bash_content.matches("wt init").count();+ assert_eq!(+ bash_wt_count, 1,+ "Bash should still have exactly one wt init line"+ );++ let zsh_content = fs::read_to_string(&zshrc_path).unwrap();+ assert!(+ !zsh_content.contains("eval \"$(wt init zsh)\""),+ "Zsh config should not be modified in dry-run"+ );+}++/// Test configure-shell detects Fish when conf.d directory exists+#[test]+fn test_configure_shell_fish_conf_d_exists() {+ let repo = TestRepo::new();+ let temp_home = TempDir::new().unwrap();++ // Create Fish conf.d directory (but not the wt.fish file)+ let fish_conf_d = temp_home.path().join(".config/fish/conf.d");+ fs::create_dir_all(&fish_conf_d).unwrap();++ let mut settings = Settings::clone_current();+ settings.set_snapshot_path("../snapshots");+ settings.add_filter(&temp_home.path().to_string_lossy(), "[TEMP_HOME]");++ settings.bind(|| {+ let mut cmd = Command::new(get_cargo_bin("wt"));+ repo.clean_cli_env(&mut cmd);+ cmd.arg("configure-shell")+ .arg("--dry-run")+ .env("HOME", temp_home.path())+ .current_dir(repo.root_path());++ assert_cmd_snapshot!("configure_shell_fish_conf_d_exists", cmd);+ });++ // Verify the fish file was not created in dry-run+ let fish_config = temp_home.path().join(".config/fish/conf.d/wt.fish");+ assert!(+ !fish_config.exists(),+ "Fish config file should not be created in dry-run"+ );+}diff --git a/tests/snapshots/integration__integration_tests__configure_shell__configure_shell_already_exists.snap b/tests/snapshots/integration__integration_tests__configure_shell__configure_shell_already_exists.snapindex 411bd0a91..d7151e69d 100644--- a/tests/snapshots/integration__integration_tests__configure_shell__configure_shell_already_exists.snap+++ b/tests/snapshots/integration__integration_tests__configure_shell__configure_shell_already_exists.snap@@ -13,7 +13,7 @@ info:GIT_CONFIG_GLOBAL: /dev/nullGIT_CONFIG_SYSTEM: /dev/nullGIT_EDITOR: ""- HOME: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpwfHd1M+ HOME: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpCE8ZcmLANG: CLC_ALL: CSOURCE_DATE_EPOCH: "1704067200"@@ -22,5 +22,6 @@ success: trueexit_code: 0----- stdout -----Already configured zsh [TEMP_HOME]/.zshrc+ [2meval "$(wt init zsh)"[0m----- stderr -----diff --git a/tests/snapshots/integration__integration_tests__configure_shell__configure_shell_custom_prefix.snap b/tests/snapshots/integration__integration_tests__configure_shell__configure_shell_custom_prefix.snapindex 9c6d1fc32..de1e99dd2 100644--- a/tests/snapshots/integration__integration_tests__configure_shell__configure_shell_custom_prefix.snap+++ b/tests/snapshots/integration__integration_tests__configure_shell__configure_shell_custom_prefix.snap@@ -15,7 +15,7 @@ info:GIT_CONFIG_GLOBAL: /dev/nullGIT_CONFIG_SYSTEM: /dev/nullGIT_EDITOR: ""- HOME: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpb4zFg2+ HOME: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpus29kRLANG: CLC_ALL: CSOURCE_DATE_EPOCH: "1704067200"@@ -24,5 +24,6 @@ success: trueexit_code: 0----- stdout -----Added bash [TEMP_HOME]/.bash_profile+ [2meval "$(worktree init bash)"[0m----- stderr -----diff --git a/tests/snapshots/integration__integration_tests__configure_shell__configure_shell_dry_run.snap b/tests/snapshots/integration__integration_tests__configure_shell__configure_shell_dry_run.snapindex f50241474..a52224bc5 100644--- a/tests/snapshots/integration__integration_tests__configure_shell__configure_shell_dry_run.snap+++ b/tests/snapshots/integration__integration_tests__configure_shell__configure_shell_dry_run.snap@@ -12,7 +12,7 @@ info:GIT_CONFIG_GLOBAL: /dev/nullGIT_CONFIG_SYSTEM: /dev/nullGIT_EDITOR: ""- HOME: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpnODeol+ HOME: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpsT0i6BLANG: CLC_ALL: CSOURCE_DATE_EPOCH: "1704067200"@@ -21,5 +21,6 @@ success: trueexit_code: 0----- stdout -----Would add to zsh [TEMP_HOME]/.zshrc+ [2meval "$(wt init zsh)"[0m----- stderr -----diff --git a/tests/snapshots/integration__integration_tests__configure_shell__configure_shell_fish.snap b/tests/snapshots/integration__integration_tests__configure_shell__configure_shell_fish.snapindex a7d5de0e8..6c4128a17 100644--- a/tests/snapshots/integration__integration_tests__configure_shell__configure_shell_fish.snap+++ b/tests/snapshots/integration__integration_tests__configure_shell__configure_shell_fish.snap@@ -13,7 +13,7 @@ info:GIT_CONFIG_GLOBAL: /dev/nullGIT_CONFIG_SYSTEM: /dev/nullGIT_EDITOR: ""- HOME: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpkyQDxi+ HOME: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpusgPLXLANG: CLC_ALL: CSOURCE_DATE_EPOCH: "1704067200"@@ -22,5 +22,6 @@ success: trueexit_code: 0----- stdout -----Created fish [TEMP_HOME]/.config/fish/conf.d/wt.fish+ [2mwt init fish | source[0m----- stderr -----diff --git a/tests/snapshots/integration__integration_tests__configure_shell__configure_shell_fish_conf_d_exists.snap b/tests/snapshots/integration__integration_tests__configure_shell__configure_shell_fish_conf_d_exists.snapnew file mode 100644index 000000000..5ff019549--- /dev/null+++ b/tests/snapshots/integration__integration_tests__configure_shell__configure_shell_fish_conf_d_exists.snap@@ -0,0 +1,26 @@+---+source: tests/integration_tests/configure_shell.rs+info:+ program: wt+ args:+ - configure-shell+ - "--dry-run"+ env:+ CLICOLOR_FORCE: "1"+ GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"+ GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"+ GIT_CONFIG_GLOBAL: /dev/null+ GIT_CONFIG_SYSTEM: /dev/null+ GIT_EDITOR: ""+ HOME: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpFrHXtL+ LANG: C+ LC_ALL: C+ SOURCE_DATE_EPOCH: "1704067200"+---+success: true+exit_code: 0+----- stdout -----+Would create fish [TEMP_HOME]/.config/fish/conf.d/wt.fish+ [2mwt init fish | source[0m++----- stderr -----diff --git a/tests/snapshots/integration__integration_tests__configure_shell__configure_shell_fish_custom_prefix.snap b/tests/snapshots/integration__integration_tests__configure_shell__configure_shell_fish_custom_prefix.snapindex af8676afa..8a3af9ca3 100644--- a/tests/snapshots/integration__integration_tests__configure_shell__configure_shell_fish_custom_prefix.snap+++ b/tests/snapshots/integration__integration_tests__configure_shell__configure_shell_fish_custom_prefix.snap@@ -15,7 +15,7 @@ info:GIT_CONFIG_GLOBAL: /dev/nullGIT_CONFIG_SYSTEM: /dev/nullGIT_EDITOR: ""- HOME: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmp34TZwb+ HOME: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmp7RffoALANG: CLC_ALL: CSOURCE_DATE_EPOCH: "1704067200"@@ -24,5 +24,6 @@ success: trueexit_code: 0----- stdout -----Created fish [TEMP_HOME]/.config/fish/conf.d/worktree.fish+ [2mworktree init fish | source[0m----- stderr -----diff --git a/tests/snapshots/integration__integration_tests__configure_shell__configure_shell_mixed_states.snap b/tests/snapshots/integration__integration_tests__configure_shell__configure_shell_mixed_states.snapnew file mode 100644index 000000000..88fd0fbfb--- /dev/null+++ b/tests/snapshots/integration__integration_tests__configure_shell__configure_shell_mixed_states.snap@@ -0,0 +1,27 @@+---+source: tests/integration_tests/configure_shell.rs+info:+ program: wt+ args:+ - configure-shell+ env:+ CLICOLOR_FORCE: "1"+ GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"+ GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"+ GIT_CONFIG_GLOBAL: /dev/null+ GIT_CONFIG_SYSTEM: /dev/null+ GIT_EDITOR: ""+ HOME: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpah5k5T+ LANG: C+ LC_ALL: C+ SOURCE_DATE_EPOCH: "1704067200"+---+success: true+exit_code: 0+----- stdout -----+Already configured bash [TEMP_HOME]/.bash_profile+ [2meval "$(wt init bash)"[0m+Added zsh [TEMP_HOME]/.zshrc+ [2meval "$(wt init zsh)"[0m++----- stderr -----diff --git a/tests/snapshots/integration__integration_tests__configure_shell__configure_shell_mixed_states_dry_run.snap b/tests/snapshots/integration__integration_tests__configure_shell__configure_shell_mixed_states_dry_run.snapnew file mode 100644index 000000000..dae43f462--- /dev/null+++ b/tests/snapshots/integration__integration_tests__configure_shell__configure_shell_mixed_states_dry_run.snap@@ -0,0 +1,28 @@+---+source: tests/integration_tests/configure_shell.rs+info:+ program: wt+ args:+ - configure-shell+ - "--dry-run"+ env:+ CLICOLOR_FORCE: "1"+ GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"+ GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"+ GIT_CONFIG_GLOBAL: /dev/null+ GIT_CONFIG_SYSTEM: /dev/null+ GIT_EDITOR: ""+ HOME: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmprQhDXj+ LANG: C+ LC_ALL: C+ SOURCE_DATE_EPOCH: "1704067200"+---+success: true+exit_code: 0+----- stdout -----+Already configured bash [TEMP_HOME]/.bash_profile+ [2meval "$(wt init bash)"[0m+Would add to zsh [TEMP_HOME]/.zshrc+ [2meval "$(wt init zsh)"[0m++----- stderr -----diff --git a/tests/snapshots/integration__integration_tests__configure_shell__configure_shell_multiple_configs.snap b/tests/snapshots/integration__integration_tests__configure_shell__configure_shell_multiple_configs.snapindex 1bf9c89d6..76b789859 100644--- a/tests/snapshots/integration__integration_tests__configure_shell__configure_shell_multiple_configs.snap+++ b/tests/snapshots/integration__integration_tests__configure_shell__configure_shell_multiple_configs.snap@@ -11,7 +11,7 @@ info:GIT_CONFIG_GLOBAL: /dev/nullGIT_CONFIG_SYSTEM: /dev/nullGIT_EDITOR: ""- HOME: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpybQMOW+ HOME: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpV1fR3fLANG: CLC_ALL: CSOURCE_DATE_EPOCH: "1704067200"@@ -20,6 +20,8 @@ success: trueexit_code: 0----- stdout -----Added bash [TEMP_HOME]/.bash_profile+ [2meval "$(wt init bash)"[0mAdded zsh [TEMP_HOME]/.zshrc+ [2meval "$(wt init zsh)"[0m----- stderr -----diff --git a/tests/snapshots/integration__integration_tests__configure_shell__configure_shell_zsh.snap b/tests/snapshots/integration__integration_tests__configure_shell__configure_shell_zsh.snapindex f93e83f37..1ab5ff25d 100644--- a/tests/snapshots/integration__integration_tests__configure_shell__configure_shell_zsh.snap+++ b/tests/snapshots/integration__integration_tests__configure_shell__configure_shell_zsh.snap@@ -13,7 +13,7 @@ info:GIT_CONFIG_GLOBAL: /dev/nullGIT_CONFIG_SYSTEM: /dev/nullGIT_EDITOR: ""- HOME: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpuxXUM5+ HOME: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpQbOOEvLANG: CLC_ALL: CSOURCE_DATE_EPOCH: "1704067200"@@ -22,5 +22,6 @@ success: trueexit_code: 0----- stdout -----Added zsh [TEMP_HOME]/.zshrc+ [2meval "$(wt init zsh)"[0m----- stderr -----