Skip to content

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

Fix `primary_remote()` failing when non-remote config keys match the regex. (#1908)

Kevin Menard87c575b9653c
diff --git a/src/git/repository/remotes.rs b/src/git/repository/remotes.rs
index 1f6c1374c..8f5947706 100644
--- a/src/git/repository/remotes.rs
+++ b/src/git/repository/remotes.rs
@@ -125,9 +125,13 @@ impl Repository {
let output = self
.run_command(&["config", "--get-regexp", r"remote\..+\.url"])
.unwrap_or_default();
- let first_remote = output.lines().next().and_then(|line| {
+ let first_remote = output.lines().find_map(|line| {
// Parse "remote.<name>.url <value>" format
// Use ".url " as delimiter to handle remote names with dots (e.g., "my.remote")
+ // Use find_map (not next + parse) because the unanchored regex matches
+ // any config key containing "remote.<something>.url" — not just actual
+ // remote entries. For example, includeIf.hasconfig:remote.*.url:... keys
+ // match and can appear before the first real remote URL.
line.strip_prefix("remote.")
.and_then(|s| s.split_once(".url "))
.map(|(name, _)| name)
diff --git a/tests/integration_tests/default_branch.rs b/tests/integration_tests/default_branch.rs
index e106b6003..f169940f3 100644
--- a/tests/integration_tests/default_branch.rs
+++ b/tests/integration_tests/default_branch.rs
@@ -1,5 +1,6 @@
use crate::common::{TestRepo, repo, repo_with_remote};
use rstest::rstest;
+use std::fs;
use worktrunk::git::{GitRemoteUrl, Repository};
#[rstest]
@@ -113,6 +114,30 @@ fn test_primary_remote_detects_custom_remote(mut repo: TestRepo) {
assert_eq!(remote, "upstream");
}
+#[rstest]
+fn test_primary_remote_skips_includeif_lines(repo: TestRepo) {
+ // `git config --get-regexp remote\..+\.url` uses an unanchored regex, so it matches
+ // any config key containing "remote.<something>.url" — not just actual remote entries.
+ // For example, `includeIf.hasconfig:remote.*.url:...` keys match and can appear before
+ // the first real remote URL. primary_remote() must skip these non-remote lines.
+ //
+ // We prepend an includeIf section to the local .git/config so it appears before the
+ // [remote "origin"] section in git's output (git emits config entries in file order
+ // within each scope, and global config entries appear before local ones).
+ let git_config = repo.root_path().join(".git/config");
+ let original = fs::read_to_string(&git_config).unwrap();
+ let patched = format!(
+ "[includeIf \"hasconfig:remote.*.url:https://github.com/example/other.git\"]\n\
+ \tpath = /dev/null\n{}",
+ original
+ );
+ fs::write(&git_config, patched).unwrap();
+
+ let git_repo = Repository::at(repo.root_path()).unwrap();
+ let remote = git_repo.primary_remote().unwrap();
+ assert_eq!(remote, "origin");
+}
+
#[rstest]
fn test_branch_exists_with_custom_remote(mut repo: TestRepo) {
repo.setup_custom_remote("upstream", "main");