Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix(git): tolerate non-git paths in WorkingTree::is_linked (#2625)
Worktrunk Botab45e004e57a
diff --git a/src/git/repository/config.rs b/src/git/repository/config.rsindex 2b73e3ad7..370b3eb50 100644--- a/src/git/repository/config.rs+++ b/src/git/repository/config.rs@@ -465,7 +465,15 @@ impl Repository {// - Linked worktrees: HEAD points to CURRENT branch, so skip this heuristic// - Normal repos: HEAD points to CURRENT branch, so skip this heuristiclet is_bare = self.is_bare()?;- let in_linked_worktree = self.current_worktree().is_linked()?;+ // Anchor the linked-worktree probe to this Repository's own+ // discovery path rather than the process CWD (`current_worktree()`).+ // The two are identical for `Repository::current()`, but+ // `Repository::at(p)` is also used for tests and for non-CWD callers+ // like `wt list`'s pipeline; using CWD there leaks process state+ // into a query that should be answered relative to `self` (#2624).+ let in_linked_worktree = self+ .worktree_at(self.discovery_path().to_path_buf())+ .is_linked()?;if ((is_bare && !in_linked_worktree) || branches.is_empty())&& let Ok(head_ref) = self.run_command(&["symbolic-ref", "HEAD"])&& let Some(branch) = head_ref.trim().strip_prefix("refs/heads/")diff --git a/tests/integration_tests/default_branch.rs b/tests/integration_tests/default_branch.rsindex 530c8724c..9e09cdbbc 100644--- a/tests/integration_tests/default_branch.rs+++ b/tests/integration_tests/default_branch.rs@@ -1,4 +1,4 @@-use crate::common::{TestRepo, repo, repo_with_remote};+use crate::common::{BareRepoTest, TestRepo, TestRepoBase, repo, repo_with_remote};use rstest::rstest;use std::fs;use worktrunk::git::{GitRemoteUrl, Repository};@@ -588,3 +588,82 @@ fn test_github_push_url_unknown_host_non_github_insteadof(repo: TestRepo) {let git_repo = Repository::at(repo.root_path()).unwrap();assert!(git_repo.branch("main").github_push_url().is_none());}++// --- `-C` and discovery-path independence ---+//+// `infer_default_branch_locally` previously called `current_worktree().is_linked()`,+// which probes `base_path()` (the process CWD or `-C` target). When tests+// constructed `Repository::at(test_path)` from a process CWD that wasn't+// inside any git repo, `is_linked()` errored on the missing `.git` and+// propagated, making `default_branch()` return `None` (#2624). Anchoring+// the probe to `self.discovery_path()` makes the answer depend on the+// repo we're asking about, not on whatever the process CWD happens to be.+//+// `-C` itself doesn't trigger the original bug — it sets `base_path()` to+// the same path `Repository::current()` adopts as `discovery_path()`, so+// both probes hit the same directory. These tests still exercise it+// end-to-end so the binary's behavior under `-C` is locked in alongside+// the unit-level fix.++/// `wt -C <repo>` from a non-repo CWD resolves the default branch via+/// local inference (no remote, empty cache).+#[rstest]+fn test_default_branch_via_c_flag_from_non_repo_cwd(repo: TestRepo) {+ // Force the local-inference path. The standard fixture has an origin+ // remote with origin/HEAD set, so `default_branch()` would short-circuit+ // through `detect_from_remote` before ever reaching the formerly-buggy+ // local probe.+ repo.run_git(&["remote", "remove", "origin"]);++ let non_repo_cwd = tempfile::tempdir().unwrap();+ let mut cmd = repo.wt_command();+ cmd.current_dir(non_repo_cwd.path()).args([+ "-C",+ repo.root_path().to_str().unwrap(),+ "config",+ "state",+ "default-branch",+ ]);++ let output = cmd.output().unwrap();+ assert!(+ output.status.success(),+ "wt -C <repo> config state default-branch from non-repo CWD failed:\nstdout: {}\nstderr: {}",+ String::from_utf8_lossy(&output.stdout),+ String::from_utf8_lossy(&output.stderr),+ );+ assert_eq!(String::from_utf8_lossy(&output.stdout).trim(), "main");+}++/// `wt -C <linked-worktree-of-bare>` from a non-repo CWD resolves the+/// default branch correctly. The maintainer's specific concern in #2625+/// review was: "Presumably if we were in a bare worktree with `-C` in a+/// linked worktree, the existing code would be wrong?" — this test pins+/// down the answer (it isn't), so the combination stays covered.+#[test]+fn test_default_branch_via_c_flag_to_linked_worktree_of_bare_repo() {+ let test = BareRepoTest::new();+ let main_worktree = test.create_worktree("main", "main");++ // Make sure there's a commit so refs resolve cleanly.+ test.commit_in(&main_worktree, "init");++ let non_repo_cwd = tempfile::tempdir().unwrap();+ let mut cmd = test.wt_command();+ cmd.current_dir(non_repo_cwd.path()).args([+ "-C",+ main_worktree.to_str().unwrap(),+ "config",+ "state",+ "default-branch",+ ]);++ let output = cmd.output().unwrap();+ assert!(+ output.status.success(),+ "wt -C <linked-wt-of-bare> config state default-branch from non-repo CWD failed:\nstdout: {}\nstderr: {}",+ String::from_utf8_lossy(&output.stdout),+ String::from_utf8_lossy(&output.stderr),+ );+ assert_eq!(String::from_utf8_lossy(&output.stdout).trim(), "main");+}