Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix: load project config from primary worktree in bare repos (#1692)
worktrunk-bot73a220118eca
diff --git a/src/config/project.rs b/src/config/project.rsindex 363f2c45a..9d95ea972 100644--- a/src/config/project.rs+++ b/src/config/project.rs@@ -140,9 +140,26 @@ impl ProjectConfig {.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() {+ // 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) {+ let primary = repo+ .primary_worktree()+ .ok()+ .flatten()+ .map(|p| p.join(".config").join("wt.toml"))+ .filter(|p| p.exists());+ match primary {+ Some(path) => path,+ None => return Ok(None),+ }+ } else {return Ok(None);- }+ };// Load directly with toml crate to preserve insertion order (with preserve_order feature)let contents = std::fs::read_to_string(&config_path)diff --git a/tests/integration_tests/bare_repository.rs b/tests/integration_tests/bare_repository.rsindex 31d7caeaa..5da81fbbe 100644--- a/tests/integration_tests/bare_repository.rs+++ b/tests/integration_tests/bare_repository.rs@@ -1,7 +1,7 @@use crate::common::{BareRepoTest, TestRepo, TestRepoBase, canonicalize, configure_directive_file,configure_git_cmd, directive_file, repo, setup_temp_snapshot_settings, wait_for,- wait_for_file_count, wt_command,+ wait_for_file_content, wait_for_file_count, wt_command,};use insta_cmd::assert_cmd_snapshot;use rstest::rstest;@@ -500,6 +500,138 @@ fn test_bare_repo_background_logs_location() {);}+#[test]+fn test_bare_repo_project_config_found_from_bare_root() {+ // Regression test for #1691: project config in the primary worktree should be+ // found when running from the bare repo root directory, not just from within+ // a worktree that contains the config.+ let test = BareRepoTest::new();++ // Create main worktree (the primary worktree for bare repos)+ let main_worktree = test.create_worktree("main", "main");+ test.commit_in(&main_worktree, "Initial commit");++ // Place project config in the primary worktree's .config/wt.toml+ let config_dir = main_worktree.join(".config");+ fs::create_dir_all(&config_dir).unwrap();++ // Use a marker file to prove the hook ran+ let marker_path = test.bare_repo_path().join("hook-ran.marker");+ let marker_str = marker_path.to_str().unwrap().replace('\\', "/");+ fs::write(+ config_dir.join("wt.toml"),+ format!("post-start = \"echo hook-executed > '{}'\"\n", marker_str),+ )+ .unwrap();++ // Commit the config so it's part of the worktree+ let output = test+ .git_command(&main_worktree)+ .args(["add", ".config/wt.toml"])+ .output()+ .unwrap();+ assert!(output.status.success());+ test.commit_in(&main_worktree, "Add project config");++ // Now run `wt switch --create feature` from the bare repo root (NOT from main worktree)+ // This is the scenario described in #1691+ 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();++ if !output.status.success() {+ panic!(+ "wt switch failed:\nstdout: {}\nstderr: {}",+ String::from_utf8_lossy(&output.stdout),+ String::from_utf8_lossy(&output.stderr)+ );+ }++ // The hook from the primary worktree's config should have executed+ wait_for_file_content(&marker_path);+ let content = fs::read_to_string(&marker_path).unwrap();+ assert!(+ content.contains("hook-executed"),+ "Hook from primary worktree config should run when command is invoked from bare root. \+ Marker file content: {:?}",+ content+ );+}++#[test]+fn test_bare_repo_project_config_found_with_dash_c_flag() {+ // Regression test for #1691 (comment): project config in the primary worktree+ // should be found when using `-C <repo>` from an unrelated directory.+ let test = BareRepoTest::new();++ // Create main worktree (the primary worktree for bare repos)+ let main_worktree = test.create_worktree("main", "main");+ test.commit_in(&main_worktree, "Initial commit");++ // Place project config in the primary worktree's .config/wt.toml+ let config_dir = main_worktree.join(".config");+ fs::create_dir_all(&config_dir).unwrap();++ // Use a marker file to prove the hook ran+ let marker_path = test.bare_repo_path().join("hook-ran-c-flag.marker");+ let marker_str = marker_path.to_str().unwrap().replace('\\', "/");+ fs::write(+ config_dir.join("wt.toml"),+ format!("post-start = \"echo hook-executed > '{}'\"\n", marker_str),+ )+ .unwrap();++ // Commit the config so it's part of the worktree+ let output = test+ .git_command(&main_worktree)+ .args(["add", ".config/wt.toml"])+ .output()+ .unwrap();+ assert!(output.status.success());+ test.commit_in(&main_worktree, "Add project config");++ // Run from a completely unrelated directory using -C to point at the bare repo+ let unrelated_dir = tempfile::tempdir().unwrap();+ 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([+ "-C",+ test.bare_repo_path().to_str().unwrap(),+ "switch",+ "--create",+ "feature-c-flag",+ "--yes",+ ])+ .current_dir(unrelated_dir.path());++ let output = cmd.output().unwrap();++ if !output.status.success() {+ panic!(+ "wt switch -C failed:\nstdout: {}\nstderr: {}",+ String::from_utf8_lossy(&output.stdout),+ String::from_utf8_lossy(&output.stderr)+ );+ }++ // The hook from the primary worktree's config should have executed+ wait_for_file_content(&marker_path);+ let content = fs::read_to_string(&marker_path).unwrap();+ assert!(+ content.contains("hook-executed"),+ "Hook from primary worktree config should run when using -C flag. \+ Marker file content: {:?}",+ content+ );+}+#[test]fn test_bare_repo_slashed_branch_with_sanitize() {// Test that slashed branch names work with bare repos and the sanitize filter