Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix: skip accidental config in bare repo root directory (#1697)
worktrunk-botd51ee8754925
diff --git a/src/config/project.rs b/src/config/project.rsindex 9d95ea972..21a2a14ac 100644--- a/src/config/project.rs+++ b/src/config/project.rs@@ -134,19 +134,12 @@ impl ProjectConfig {repo: &crate::git::Repository,write_hints: bool,) -> Result<Option<Self>, ConfigError> {- let repo_root = repo- .current_worktree()- .root()- .map_err(|e| ConfigError::Message(format!("Failed to get worktree root: {}", e)))?;- let config_path = repo_root.join(".config").join("wt.toml");-- // For bare repos, if the current location doesn't have a config file,- // fall back to the primary worktree (default branch worktree). This- // handles the common case where the user runs commands from the bare- // repo root but the config lives in the primary worktree (#1691).- let config_path = if config_path.exists() {- config_path- } else if repo.is_bare().unwrap_or(false) {+ // For bare repos, go straight to the primary worktree for config.+ // `root()` falls back to the bare repo directory when `--show-toplevel`+ // fails, which would incorrectly pick up a `.config/wt.toml` placed in+ // the bare repo root — that's an accidental side effect, not a real+ // worktree root (#1691).+ let config_path = if repo.is_bare().unwrap_or(false) {let primary = repo.primary_worktree().ok()@@ -158,7 +151,16 @@ impl ProjectConfig {None => return Ok(None),}} else {- return Ok(None);+ let repo_root = repo+ .current_worktree()+ .root()+ .map_err(|e| ConfigError::Message(format!("Failed to get worktree root: {}", e)))?;+ let config_path = repo_root.join(".config").join("wt.toml");+ if config_path.exists() {+ config_path+ } else {+ return Ok(None);+ }};// Load directly with toml crate to preserve insertion order (with preserve_order feature)diff --git a/tests/integration_tests/bare_repository.rs b/tests/integration_tests/bare_repository.rsindex 5da81fbbe..5f8b65e58 100644--- a/tests/integration_tests/bare_repository.rs+++ b/tests/integration_tests/bare_repository.rs@@ -8,6 +8,8 @@ use rstest::rstest;use std::fs;use std::path::{Path, PathBuf};use std::process::Command;+use std::thread;+use std::time::Duration;#[test]fn test_bare_repo_list_worktrees() {@@ -632,6 +634,52 @@ fn test_bare_repo_project_config_found_with_dash_c_flag() {);}+#[test]+fn test_bare_repo_ignores_config_in_bare_root() {+ // Regression test for #1691: a `.config/wt.toml` placed in the bare repo root+ // directory should NOT be picked up. Only the primary worktree's config matters.+ let test = BareRepoTest::new();++ // Create main worktree (the primary worktree for bare repos) — no config here+ let main_worktree = test.create_worktree("main", "main");+ test.commit_in(&main_worktree, "Initial commit");++ // Place config in the bare repo root (NOT in a worktree)+ let config_dir = test.bare_repo_path().join(".config");+ fs::create_dir_all(&config_dir).unwrap();++ let marker_path = test.bare_repo_path().join("hook-should-not-run.marker");+ let marker_str = marker_path.to_str().unwrap().replace('\\', "/");+ fs::write(+ config_dir.join("wt.toml"),+ format!("post-start = \"echo bad > '{}'\"\n", marker_str),+ )+ .unwrap();++ // Run `wt switch --create feature` from the bare repo root+ let (directive_path, _guard) = directive_file();+ let mut cmd = wt_command();+ test.configure_wt_cmd(&mut cmd);+ configure_directive_file(&mut cmd, &directive_path);+ cmd.args(["switch", "--create", "feature", "--yes"])+ .current_dir(test.bare_repo_path());++ let output = cmd.output().unwrap();+ assert!(+ output.status.success(),+ "wt switch failed:\nstdout: {}\nstderr: {}",+ String::from_utf8_lossy(&output.stdout),+ String::from_utf8_lossy(&output.stderr)+ );++ // The hook from the bare root config should NOT have executed+ thread::sleep(Duration::from_millis(500));+ assert!(+ !marker_path.exists(),+ "Config in bare repo root should be ignored — only primary worktree config should be used"+ );+}+#[test]fn test_bare_repo_slashed_branch_with_sanitize() {// Test that slashed branch names work with bare repos and the sanitize filter