Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix: resolve default-branch project config by name, not cwd HEAD (#3466)
Worktrunk Bot5c6fbed809ba
diff --git a/src/git/repository/config.rs b/src/git/repository/config.rsindex 0a0fd1472..7e34a6ec3 100644--- a/src/git/repository/config.rs+++ b/src/git/repository/config.rs@@ -712,14 +712,17 @@ impl Repository {/// error-swallowing shape `alias.rs` uses for config resolution, and safe/// because the fallback only ever adds hooks, never risks data.///- /// The read uses `HEAD` rather than a resolved branch name: at the bare- /// root the repo's own `HEAD` is a symbolic ref to the default branch (it- /// is what local default-branch inference reads), so `HEAD:.config/wt.toml`- /// reads the default branch's committed config, always resolves in a bare- /// repo, and needs no separate branch lookup. The returned `PathBuf` is a- /// display-only label of the form `HEAD:.config/wt.toml` — a git revision- /// spec, not a filesystem path. Nothing is read from or written to it; it- /// only annotates diagnostics (e.g. a parse error) with the source.+ /// The read resolves the default branch **by name**+ /// (`<default-branch>:.config/wt.toml`), not via `HEAD`. `git show` resolves+ /// `HEAD` against the invocation cwd's per-worktree HEAD, and this runs with+ /// `discovery_path` as cwd — a linked worktree parked on some other branch+ /// when `wt` is invoked from inside one (the common agent case). Reading+ /// `HEAD` there would read that worktree's branch, dropping or mis-sourcing+ /// the default branch's config; an absolute branch ref is cwd-independent.+ /// The returned `PathBuf` is a display-only label of the form+ /// `<default-branch>:.config/wt.toml` — a git revision spec, not a+ /// filesystem path. Nothing is read from or written to it; it only+ /// annotates diagnostics (e.g. a parse error) with the object-store source.pub fn default_branch_project_config_content(&self) -> Option<(String, PathBuf)> {if !self.is_bare().unwrap_or(false) {return None;@@ -731,11 +734,11 @@ impl Repository {return None;}- let spec = "HEAD:.config/wt.toml";- match self.run_command_output(&["show", spec]) {+ let spec = format!("{}:.config/wt.toml", self.default_branch()?);+ match self.run_command_output(&["show", &spec]) {Ok(output) if output.status.success() => Some((String::from_utf8_lossy(&output.stdout).into_owned(),- PathBuf::from(spec),+ PathBuf::from(&spec),)),// A non-zero exit (typically 128, path absent from the tree) or a// rare spawn failure: treat as "no project config", the same resultdiff --git a/tests/integration_tests/bare_repository.rs b/tests/integration_tests/bare_repository.rsindex 9ac587c31..afc6f94ac 100644--- a/tests/integration_tests/bare_repository.rs+++ b/tests/integration_tests/bare_repository.rs@@ -968,6 +968,99 @@ fn test_bare_repo_no_project_config_when_primary_off_branch_and_none_present() {);}+#[test]+fn test_bare_repo_project_config_found_from_linked_worktree_when_primary_off_branch() {+ // Regression for the #3461 fix: the object-store fallback must resolve the+ // *default branch's* config by name, not via `HEAD`. `git show HEAD:...`+ // resolves HEAD against the invocation cwd's per-worktree HEAD, so when `wt`+ // runs from inside a linked worktree parked on another branch — the common+ // agent case — HEAD is that worktree's branch, not the default. Reading+ // HEAD there dropped the default branch's config and every project hook.+ //+ // This test runs `wt switch --create` from inside a *second* linked worktree+ // whose branch has no `.config/wt.toml` on disk, with the default branch+ // checked out nowhere, and asserts the default branch's committed hook still+ // fires. `git show HEAD:...` would read the invoking worktree's branch+ // (no config) and silently drop the hook; `git show <default>:...` finds it.+ let test = BareRepoTest::new();++ // Primary worktree on the default branch, carrying the project config.+ let main_worktree = test.create_worktree("main", "main");+ test.commit_in(&main_worktree, "Initial commit");++ let config_dir = main_worktree.join(".config");+ fs::create_dir_all(&config_dir).unwrap();+ let marker_path = test.bare_repo_path().join("hook-ran-from-linked.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();+ let output = test+ .git_command(&main_worktree)+ .args(["add", ".config/wt.toml"])+ .run()+ .unwrap();+ assert!(output.status.success());+ test.commit_in(&main_worktree, "Add project config");++ // A second linked worktree whose branch drops the config on disk, so+ // resolution there must fall through to the object-store read.+ let other_worktree = test.create_worktree("other", "other");+ let output = test+ .git_command(&other_worktree)+ .args(["rm", ".config/wt.toml"])+ .run()+ .unwrap();+ assert!(+ output.status.success(),+ "git rm failed: {}",+ String::from_utf8_lossy(&output.stderr)+ );+ test.commit_in(&other_worktree, "Remove project config on other branch");++ // Park the primary off the default branch, so `main` is checked out nowhere.+ let output = test+ .git_command(&main_worktree)+ .args(["checkout", "-b", "feature-x"])+ .run()+ .unwrap();+ assert!(+ output.status.success(),+ "checkout -b feature-x failed: {}",+ String::from_utf8_lossy(&output.stderr)+ );++ // Run `wt switch --create` from *inside* the `other` worktree (HEAD = other).+ let (cd_path, exec_path, _guard) = directive_files();+ let mut cmd = wt_command();+ test.configure_wt_cmd(&mut cmd);+ configure_directive_files(&mut cmd, &cd_path, &exec_path);+ cmd.args(["switch", "--create", "test-repro", "--yes"])+ .current_dir(&other_worktree);++ 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 default branch's committed hook must run even though `wt` was invoked+ // from a linked worktree whose own branch carries no config.+ wait_for_file_content(&marker_path);+ let content = fs::read_to_string(&marker_path).unwrap();+ assert!(+ content.contains("hook-executed"),+ "Project hook must resolve the default branch's config by name (not via \+ cwd `HEAD`) when invoked from a linked worktree. Marker 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