Skip to content

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

fix(switch): name the configured worktree path in the bare-repo warning (#4491)

Worktrunk Bot00cf0ed9ea4c
diff --git a/src/commands/worktree/resolve.rs b/src/commands/worktree/resolve.rs
index 00636f8fc..87a923def 100644
--- a/src/commands/worktree/resolve.rs
+++ b/src/commands/worktree/resolve.rs
@@ -10,6 +10,7 @@ use std::path::PathBuf;
use color_print::cformat;
use normalize_path::NormalizePath;
+use path_slash::PathExt as _;
use worktrunk::config::UserConfig;
use worktrunk::git::Repository;
use worktrunk::path::{format_path_for_display, paths_match};
@@ -175,11 +176,19 @@ pub fn offer_bare_repo_worktree_path_fix(
.and_then(|n| n.to_str())
.unwrap_or("project");
- // Use the actual branch being switched to in example paths. Calling
- // `sanitize_branch_name` keeps the displayed example aligned with what
- // the `sanitize` Jinja filter applies inside `BARE_REPO_WORKTREE_PATH`.
+ // Use the actual branch being switched to in example paths. The bad
+ // example is where the configured template puts it, shown relative to
+ // the project's parent when it lands there. Calling
+ // `sanitize_branch_name` keeps the good example aligned with what the
+ // `sanitize` Jinja filter applies inside `BARE_REPO_WORKTREE_PATH`.
+ let bad_path = compute_worktree_path(repo, branch, config)?;
+ let example_bad = repo_path
+ .parent()
+ .and_then(|p| p.parent())
+ .and_then(|root| bad_path.strip_prefix(root).ok())
+ .map(|rel| rel.to_slash_lossy().into_owned())
+ .unwrap_or_else(|| format_path_for_display(&bad_path).to_string());
let sanitized = worktrunk::config::sanitize_branch_name(branch);
- let example_bad = format!("{parent_name}/{repo_name}.{sanitized}");
let example_good = format!("{parent_name}/{sanitized}");
let config_path_display = worktrunk::config::config_path_for_display();
diff --git a/tests/integration_tests/bare_repository.rs b/tests/integration_tests/bare_repository.rs
index 8f1247c94..9182ad454 100644
--- a/tests/integration_tests/bare_repository.rs
+++ b/tests/integration_tests/bare_repository.rs
@@ -2031,6 +2031,36 @@ fn test_bare_repo_worktree_path_prompt_non_interactive_warning() {
});
}
+/// The warning names where the configured template puts the worktree, not
+/// the default template's `{{ repo }}.{{ branch }}` shape.
+#[test]
+fn test_bare_repo_worktree_path_warning_uses_configured_template() {
+ let test = setup_unconfigured_nested_bare_repo();
+ let main_worktree = test.project_path().join("main");
+ fs::write(
+ test.config_path(),
+ "skip-shell-integration-prompt = true\nworktree-path = \"{{ repo_path }}/../{{ repo }}-wt/{{ branch | sanitize }}\"\n",
+ )
+ .unwrap();
+
+ let mut cmd = wt_command();
+ test.configure_wt_cmd(&mut cmd);
+ cmd.args(["switch", "--create", "feature", "--no-cd"])
+ .current_dir(&main_worktree);
+ let output = cmd.output().unwrap();
+ let stderr = String::from_utf8_lossy(&output.stderr);
+ assert!(output.status.success(), "{stderr}");
+
+ assert!(
+ stderr.contains("worktrees will be at project/.git-wt/feature"),
+ "warning should name the configured path, got: {stderr}"
+ );
+ assert!(
+ test.project_path().join(".git-wt").join("feature").exists(),
+ "worktree should be where the warning said"
+ );
+}
+
/// Symbolic identifiers (-, @, pr:N) are passed before branch resolution, so
/// the example paths in the prompt would show the raw symbol. The function
/// must return early without prompting.