Skip to content

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

Make worktree-path optional in config (#379)

Maximilian Roos2c5000de8aa7
diff --git a/dev/config.example.toml b/dev/config.example.toml
index e630a1528..bf33dd206 100644
--- a/dev/config.example.toml
+++ b/dev/config.example.toml
@@ -30,16 +30,16 @@
# Worktree Path Template
# Variables:
-# {{ main_worktree }} - Main worktree directory name (e.g., "myproject")
+# {{ repo }} - Repository directory name (e.g., "myproject")
# {{ branch }} - Raw branch name (e.g., "feature/auth")
# {{ branch | sanitize }} - Branch name with / and \ replaced by - (e.g., "feature-auth")
#
# Paths are relative to the main worktree root (original repository directory).
#
-# Example paths created (main worktree at /Users/dev/myproject, branch feature/auth):
-# "../{{ main_worktree }}.{{ branch | sanitize }}" → /Users/dev/myproject.feature-auth
-# ".worktrees/{{ branch | sanitize }}" → /Users/dev/myproject/.worktrees/feature-auth
-worktree-path = "../{{ main_worktree }}.{{ branch | sanitize }}"
+# Example paths created (repo at /Users/dev/myproject, branch feature/auth):
+# "../{{ repo }}.{{ branch | sanitize }}" → /Users/dev/myproject.feature-auth
+# ".worktrees/{{ branch | sanitize }}" → /Users/dev/myproject/.worktrees/feature-auth
+worktree-path = "../{{ repo }}.{{ branch | sanitize }}"
# Alternative: Inside repo (useful for bare repos)
# worktree-path = ".worktrees/{{ branch | sanitize }}"
diff --git a/docs/content/config.md b/docs/content/config.md
index 277ba5809..79d0d6c0f 100644
--- a/docs/content/config.md
+++ b/docs/content/config.md
@@ -317,16 +317,16 @@ Creates `~/.config/worktrunk/config.toml` with the following content:
# Worktree Path Template
# Variables:
-# {{ main_worktree }} - Main worktree directory name (e.g., "myproject")
+# {{ repo }} - Repository directory name (e.g., "myproject")
# {{ branch }} - Raw branch name (e.g., "feature/auth")
# {{ branch | sanitize }} - Branch name with / and \ replaced by - (e.g., "feature-auth")
#
# Paths are relative to the main worktree root (original repository directory).
#
-# Example paths created (main worktree at /Users/dev/myproject, branch feature/auth):
-# "../{{ main_worktree }}.{{ branch | sanitize }}" → /Users/dev/myproject.feature-auth
-# ".worktrees/{{ branch | sanitize }}" → /Users/dev/myproject/.worktrees/feature-auth
-worktree-path = "../{{ main_worktree }}.{{ branch | sanitize }}"
+# Example paths created (repo at /Users/dev/myproject, branch feature/auth):
+# "../{{ repo }}.{{ branch | sanitize }}" → /Users/dev/myproject.feature-auth
+# ".worktrees/{{ branch | sanitize }}" → /Users/dev/myproject/.worktrees/feature-auth
+worktree-path = "../{{ repo }}.{{ branch | sanitize }}"
# Alternative: Inside repo (useful for bare repos)
# worktree-path = ".worktrees/{{ branch | sanitize }}"
diff --git a/src/commands/config.rs b/src/commands/config.rs
index d9e07d580..f668062d5 100644
--- a/src/commands/config.rs
+++ b/src/commands/config.rs
@@ -281,13 +281,9 @@ fn render_user_config(out: &mut String) -> anyhow::Result<()> {
out,
"{}",
hint_message(cformat!(
- "Not found (using defaults); to create one, run <bright-black>wt config create</>"
+ "Not found; to create one, run <bright-black>wt config create</>"
))
)?;
- writeln!(out)?;
- let default_config =
- "# Default configuration:\nworktree-path = \"../{{ repo }}.{{ branch | sanitize }}\"";
- write!(out, "{}", format_toml(default_config))?;
return Ok(());
}
diff --git a/src/config/mod.rs b/src/config/mod.rs
index 158cbac08..d0cde036c 100644
--- a/src/config/mod.rs
+++ b/src/config/mod.rs
@@ -45,16 +45,29 @@ mod tests {
fn test_config_serialization() {
let config = WorktrunkConfig::default();
let toml = toml::to_string(&config).unwrap();
- assert!(toml.contains("worktree-path"));
- assert!(toml.contains("../{{ repo }}.{{ branch | sanitize }}"));
+ // worktree-path is not serialized when None (uses built-in default)
+ assert!(!toml.contains("worktree-path"));
assert!(toml.contains("commit-generation"));
}
+ #[test]
+ fn test_config_serialization_with_worktree_path() {
+ let config = WorktrunkConfig {
+ worktree_path: Some("custom/{{ branch }}".to_string()),
+ ..Default::default()
+ };
+ let toml = toml::to_string(&config).unwrap();
+ assert!(toml.contains("worktree-path"));
+ assert!(toml.contains("custom/{{ branch }}"));
+ }
+
#[test]
fn test_default_config() {
let config = WorktrunkConfig::default();
+ // worktree_path is None by default, but the getter returns the default
+ assert!(config.worktree_path.is_none());
assert_eq!(
- config.worktree_path,
+ config.worktree_path(),
"../{{ repo }}.{{ branch | sanitize }}"
);
assert_eq!(config.commit_generation.command, None);
@@ -64,7 +77,7 @@ mod tests {
#[test]
fn test_format_worktree_path() {
let config = WorktrunkConfig {
- worktree_path: "{{ main_worktree }}.{{ branch }}".to_string(),
+ worktree_path: Some("{{ main_worktree }}.{{ branch }}".to_string()),
..Default::default()
};
assert_eq!(
@@ -76,7 +89,7 @@ mod tests {
#[test]
fn test_format_worktree_path_custom_template() {
let config = WorktrunkConfig {
- worktree_path: "{{ main_worktree }}-{{ branch }}".to_string(),
+ worktree_path: Some("{{ main_worktree }}-{{ branch }}".to_string()),
..Default::default()
};
assert_eq!(
@@ -88,7 +101,7 @@ mod tests {
#[test]
fn test_format_worktree_path_only_branch() {
let config = WorktrunkConfig {
- worktree_path: ".worktrees/{{ main_worktree }}/{{ branch }}".to_string(),
+ worktree_path: Some(".worktrees/{{ main_worktree }}/{{ branch }}".to_string()),
..Default::default()
};
assert_eq!(
@@ -101,7 +114,7 @@ mod tests {
fn test_format_worktree_path_with_slashes() {
// Use {{ branch | sanitize }} to replace slashes with dashes
let config = WorktrunkConfig {
- worktree_path: "{{ main_worktree }}.{{ branch | sanitize }}".to_string(),
+ worktree_path: Some("{{ main_worktree }}.{{ branch | sanitize }}".to_string()),
..Default::default()
};
assert_eq!(
@@ -113,7 +126,9 @@ mod tests {
#[test]
fn test_format_worktree_path_with_multiple_slashes() {
let config = WorktrunkConfig {
- worktree_path: ".worktrees/{{ main_worktree }}/{{ branch | sanitize }}".to_string(),
+ worktree_path: Some(
+ ".worktrees/{{ main_worktree }}/{{ branch | sanitize }}".to_string(),
+ ),
..Default::default()
};
assert_eq!(
@@ -126,7 +141,9 @@ mod tests {
fn test_format_worktree_path_with_backslashes() {
// Windows-style path separators should also be sanitized
let config = WorktrunkConfig {
- worktree_path: ".worktrees/{{ main_worktree }}/{{ branch | sanitize }}".to_string(),
+ worktree_path: Some(
+ ".worktrees/{{ main_worktree }}/{{ branch | sanitize }}".to_string(),
+ ),
..Default::default()
};
assert_eq!(
@@ -139,7 +156,7 @@ mod tests {
fn test_format_worktree_path_raw_branch() {
// {{ branch }} without filter gives raw branch name
let config = WorktrunkConfig {
- worktree_path: "{{ main_worktree }}.{{ branch }}".to_string(),
+ worktree_path: Some("{{ main_worktree }}.{{ branch }}".to_string()),
..Default::default()
};
assert_eq!(
diff --git a/src/config/user.rs b/src/config/user.rs
index 3101dbe6d..477dfca5a 100644
--- a/src/config/user.rs
+++ b/src/config/user.rs
@@ -124,10 +124,14 @@ pub enum StageMode {
///
/// Environment variables can override config file settings using `WORKTRUNK_` prefix with
/// `__` separator for nested fields (e.g., `WORKTRUNK_COMMIT_GENERATION__COMMAND`).
-#[derive(Debug, Serialize, Deserialize)]
+#[derive(Debug, Default, Serialize, Deserialize)]
pub struct WorktrunkConfig {
- #[serde(rename = "worktree-path", default = "default_worktree_path")]
- pub worktree_path: String,
+ #[serde(
+ rename = "worktree-path",
+ default,
+ skip_serializing_if = "Option::is_none"
+ )]
+ pub(crate) worktree_path: Option<String>,
#[serde(default, rename = "commit-generation")]
pub commit_generation: CommitGenerationConfig,
@@ -293,28 +297,19 @@ pub struct MergeConfig {
pub verify: Option<bool>,
}
-/// Default worktree path template (used by serde)
+/// Default worktree path template
fn default_worktree_path() -> String {
"../{{ repo }}.{{ branch | sanitize }}".to_string()
}
-impl Default for WorktrunkConfig {
- fn default() -> Self {
- Self {
- worktree_path: default_worktree_path(),
- commit_generation: CommitGenerationConfig::default(),
- projects: std::collections::BTreeMap::new(),
- list: None,
- commit: None,
- merge: None,
- hooks: HooksConfig::default(),
- skip_shell_integration_prompt: false,
- unknown: std::collections::HashMap::new(),
- }
+impl WorktrunkConfig {
+ /// Returns the worktree path template, falling back to the default if not set.
+ pub fn worktree_path(&self) -> String {
+ self.worktree_path
+ .clone()
+ .unwrap_or_else(default_worktree_path)
}
-}
-impl WorktrunkConfig {
/// Load configuration from config file and environment variables.
///
/// Configuration is loaded in the following order (later sources override earlier ones):
@@ -324,8 +319,10 @@ impl WorktrunkConfig {
pub fn load() -> Result<Self, ConfigError> {
let defaults = Self::default();
+ // Note: worktree-path has no default set here - it's handled by the getter
+ // which returns the default when None. This allows us to distinguish
+ // "user explicitly set this" from "using default".
let mut builder = Config::builder()
- .set_default("worktree-path", defaults.worktree_path)?
.set_default(
"commit-generation.command",
defaults.commit_generation.command.unwrap_or_default(),
@@ -364,14 +361,16 @@ impl WorktrunkConfig {
let config: Self = builder.build()?.try_deserialize()?;
- // Validate worktree path
- if config.worktree_path.is_empty() {
- return Err(ConfigError::Message("worktree-path cannot be empty".into()));
- }
- if std::path::Path::new(&config.worktree_path).is_absolute() {
- return Err(ConfigError::Message(
- "worktree-path must be relative, not absolute".into(),
- ));
+ // Validate worktree path (only if explicitly set - default is always valid)
+ if let Some(ref path) = config.worktree_path {
+ if path.is_empty() {
+ return Err(ConfigError::Message("worktree-path cannot be empty".into()));
+ }
+ if std::path::Path::new(path).is_absolute() {
+ return Err(ConfigError::Message(
+ "worktree-path must be relative, not absolute".into(),
+ ));
+ }
}
// Validate commit generation config
@@ -415,7 +414,7 @@ impl WorktrunkConfig {
vars.insert("main_worktree", main_worktree);
vars.insert("repo", main_worktree);
vars.insert("branch", branch);
- expand_template(&self.worktree_path, &vars, false)
+ expand_template(&self.worktree_path(), &vars, false)
}
/// Check if a command is approved for the given project
@@ -653,7 +652,11 @@ impl WorktrunkConfig {
} else {
// No existing file, create from scratch using toml_edit for consistent formatting
let mut doc = toml_edit::DocumentMut::new();
- doc["worktree-path"] = toml_edit::value(&self.worktree_path);
+
+ // Only write worktree-path if explicitly set (not the default)
+ if let Some(ref path) = self.worktree_path {
+ doc["worktree-path"] = toml_edit::value(path);
+ }
// skip-shell-integration-prompt (only if true)
if self.skip_shell_integration_prompt {
@@ -891,8 +894,10 @@ rename-tab = "echo 'switched'"
#[test]
fn test_worktrunk_config_default() {
let config = WorktrunkConfig::default();
+ // worktree_path is None by default, but the getter returns the default
+ assert!(config.worktree_path.is_none());
assert_eq!(
- config.worktree_path,
+ config.worktree_path(),
"../{{ repo }}.{{ branch | sanitize }}"
);
assert!(config.projects.is_empty());
@@ -934,7 +939,7 @@ rename-tab = "echo 'switched'"
#[test]
fn test_worktrunk_config_format_path_custom_template() {
let config = WorktrunkConfig {
- worktree_path: ".worktrees/{{ branch }}".to_string(),
+ worktree_path: Some(".worktrees/{{ branch }}".to_string()),
..Default::default()
};
let path = config.format_path("myrepo", "feature").unwrap();
diff --git a/tests/integration_tests/approval_save.rs b/tests/integration_tests/approval_save.rs
index 3debcd937..c8efceb3d 100644
--- a/tests/integration_tests/approval_save.rs
+++ b/tests/integration_tests/approval_save.rs
@@ -33,8 +33,6 @@ fn test_approval_saves_to_disk() {
// Verify TOML structure
let toml_content = fs::read_to_string(&config_path).unwrap();
assert_snapshot!(toml_content, @r#"
- worktree-path = "../{{ repo }}.{{ branch | sanitize }}"
-
[commit-generation]
args = []
@@ -89,8 +87,6 @@ fn test_duplicate_approvals_not_saved_twice() {
// Verify file contains only one entry
let toml_content = fs::read_to_string(&config_path).unwrap();
assert_snapshot!(toml_content, @r#"
- worktree-path = "../{{ repo }}.{{ branch | sanitize }}"
-
[commit-generation]
args = []
@@ -141,8 +137,6 @@ fn test_multiple_project_approvals() {
// Verify file structure
let toml_content = fs::read_to_string(&config_path).unwrap();
assert_snapshot!(toml_content, @r#"
- worktree-path = "../{{ repo }}.{{ branch | sanitize }}"
-
[commit-generation]
args = []
@@ -229,12 +223,10 @@ fn test_yes_flag_does_not_save_approval() {
// Load the config and verify it's still empty (no approvals added)
let saved_config = fs::read_to_string(&config_path).unwrap();
- assert_snapshot!(saved_config, @r#"
- worktree-path = "../{{ repo }}.{{ branch | sanitize }}"
-
+ assert_snapshot!(saved_config, @"
[commit-generation]
args = []
- "#);
+ ");
}
/// Test that approval saving logic handles missing config gracefully
@@ -264,8 +256,6 @@ fn test_approval_saves_to_new_config_file() {
// Verify content
let content = fs::read_to_string(&config_path).unwrap();
assert_snapshot!(content, @r#"
- worktree-path = "../{{ repo }}.{{ branch | sanitize }}"
-
[commit-generation]
args = []
@@ -619,13 +609,12 @@ fn test_skip_shell_integration_prompt_saves_to_disk() {
// Verify TOML structure
let toml_content = fs::read_to_string(&config_path).unwrap();
- assert_snapshot!(toml_content, @r#"
- worktree-path = "../{{ repo }}.{{ branch | sanitize }}"
+ assert_snapshot!(toml_content, @"
skip-shell-integration-prompt = true
[commit-generation]
args = []
- "#);
+ ");
}
/// Test that set_skip_shell_integration_prompt is idempotent
diff --git a/tests/integration_tests/config_show.rs b/tests/integration_tests/config_show.rs
index 6539bcd4e..9833aa1af 100644
--- a/tests/integration_tests/config_show.rs
+++ b/tests/integration_tests/config_show.rs
@@ -591,6 +591,28 @@ fn test_config_show_whitespace_only_project_config(mut repo: TestRepo, temp_home
});
}
+/// Test `wt config show` when user config file doesn't exist
+///
+/// Should show a hint about creating the config and display the default configuration.
+#[rstest]
+fn test_config_show_no_user_config(mut repo: TestRepo, temp_home: TempDir) {
+ // Setup mock gh/glab for deterministic BINARIES output
+ repo.setup_mock_ci_tools_unauthenticated();
+
+ // Don't create any user config file - temp_home is empty
+
+ let settings = setup_snapshot_settings_with_home(&repo, &temp_home);
+ settings.bind(|| {
+ let mut cmd = wt_command();
+ repo.configure_wt_cmd(&mut cmd);
+ repo.configure_mock_commands(&mut cmd);
+ cmd.arg("config").arg("show").current_dir(repo.root_path());
+ set_temp_home_env(&mut cmd, temp_home.path());
+
+ assert_cmd_snapshot!(cmd);
+ });
+}
+
/// Test `wt config show` shows warning for unmatched candidates (potential false negatives)
///
/// When a shell config contains `wt` at a word boundary but it's NOT detected as
diff --git a/tests/snapshots/integration__integration_tests__config_show__config_show_no_user_config.snap b/tests/snapshots/integration__integration_tests__config_show__config_show_no_user_config.snap
new file mode 100644
index 000000000..31d188f2e
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__config_show__config_show_no_user_config.snap
@@ -0,0 +1,47 @@
+---
+source: tests/integration_tests/config_show.rs
+info:
+ program: wt
+ args:
+ - config
+ - show
+ env:
+ APPDATA: "[TEST_CONFIG_HOME]"
+ CLICOLOR_FORCE: "1"
+ COLUMNS: "150"
+ 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"
+ USERPROFILE: "[TEST_HOME]"
+ WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"
+ XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
+---
+success: true
+exit_code: 0
+----- stdout -----
+
+----- stderr -----
+USER CONFIG ~/.config/worktrunk/config.toml
+↳ Not found; to create one, run wt config create
+
+PROJECT CONFIG _REPO_/.config/wt.toml
+↳ Not found
+
+SHELL INTEGRATION
+○ Skipped bash; ~/.bashrc not found
+○ Skipped zsh; ~/.zshrc not found
+○ Skipped fish; ~/.config/fish/conf.d not found
+
+RUNTIME
+  wt [VERSION]
+↳ Shell integration not active
diff --git a/tests/snapshots/integration__integration_tests__help__help_config_create.snap b/tests/snapshots/integration__integration_tests__help__help_config_create.snap
index 8834b98e8..4eb231e47 100644
--- a/tests/snapshots/integration__integration_tests__help__help_config_create.snap
+++ b/tests/snapshots/integration__integration_tests__help__help_config_create.snap
@@ -77,16 +77,16 @@ Creates ~/.config/worktrunk/config.toml with the following content:

# Worktree Path Template
# Variables:
- # {{ main_worktree }} - Main worktree directory name (e.g., "myproject")
+ # {{ repo }} - Repository directory name (e.g., "myproject")
# {{ branch }} - Raw branch name (e.g., "feature/auth")
# {{ branch | sanitize }} - Branch name with / and \ replaced by - (e.g., "feature-auth")
#
# Paths are relative to the main worktree root (original repository directory).
#
- # Example paths created (main worktree at /Users/dev/myproject, branch feature/auth):
- # "../{{ main_worktree }}.{{ branch | sanitize }}" → /Users/dev/myproject.feature-auth
- # ".worktrees/{{ branch | sanitize }}" → /Users/dev/myproject/.worktrees/feature-auth
- worktree-path = "../{{ main_worktree }}.{{ branch | sanitize }}"
+ # Example paths created (repo at /Users/dev/myproject, branch feature/auth):
+ # "../{{ repo }}.{{ branch | sanitize }}" → /Users/dev/myproject.feature-auth
+ # ".worktrees/{{ branch | sanitize }}" → /Users/dev/myproject/.worktrees/feature-auth
+ worktree-path = "../{{ repo }}.{{ branch | sanitize }}"

# Alternative: Inside repo (useful for bare repos)
# worktree-path = ".worktrees/{{ branch | sanitize }}"