Skip to content

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

fix: fence leading-dash remote names from git options (#4267)

DDL2014eb3f1c90
diff --git a/src/git/repository/config.rs b/src/git/repository/config.rs
index 725efdb21..0aac2b242 100644
--- a/src/git/repository/config.rs
+++ b/src/git/repository/config.rs
@@ -724,14 +724,18 @@ impl Repository {
// Private helpers for default_branch detection
fn local_default_branch(&self, remote: &str) -> anyhow::Result<String> {
- let stdout =
- self.run_command(&["rev-parse", "--abbrev-ref", &format!("{}/HEAD", remote)])?;
+ let stdout = self.run_command(&[
+ "rev-parse",
+ "--abbrev-ref",
+ "--verify",
+ &format!("refs/remotes/{remote}/HEAD"),
+ ])?;
DefaultBranchName::from_local(remote, &stdout).map(DefaultBranchName::into_string)
}
pub(super) fn query_remote_default_branch(&self, remote: &str) -> anyhow::Result<String> {
let stdout = self.run_command_bounded(
- &["ls-remote", "--symref", remote, "HEAD"],
+ &["ls-remote", "--symref", "--", remote, "HEAD"],
Some(REMOTE_DETECTION_TIMEOUT),
)?;
DefaultBranchName::from_remote(&stdout).map(DefaultBranchName::into_string)
diff --git a/src/git/repository/remotes.rs b/src/git/repository/remotes.rs
index b5a0d9843..acde0ed0e 100644
--- a/src/git/repository/remotes.rs
+++ b/src/git/repository/remotes.rs
@@ -130,7 +130,7 @@ impl Repository {
.effective_remote_urls
.entry(remote.to_string())
.or_insert_with(|| {
- self.run_command(&["remote", "get-url", remote])
+ self.run_command(&["remote", "get-url", "--", remote])
.ok()
.map(|url| url.trim().to_string())
.filter(|url| !url.is_empty())
diff --git a/tests/integration_tests/default_branch.rs b/tests/integration_tests/default_branch.rs
index 0f2009a7e..403e03362 100644
--- a/tests/integration_tests/default_branch.rs
+++ b/tests/integration_tests/default_branch.rs
@@ -179,6 +179,28 @@ fn test_get_default_branch_with_custom_remote(mut repo: TestRepo) {
assert_eq!(branch, "main");
}
+/// Git permits remote names that begin with `-`. They must remain operands in
+/// both the local remote-HEAD lookup and the fallback network query.
+#[rstest]
+fn test_get_default_branch_with_leading_dash_remote(#[from(repo_with_remote)] repo: TestRepo) {
+ repo.run_git(&["remote", "rename", "--", "origin", "-x"]);
+ let git_repo = Repository::at(repo.root_path()).unwrap();
+
+ assert_eq!(
+ git_repo.remote_head(),
+ Some(("-x".to_string(), "main".to_string()))
+ );
+
+ repo.run_git(&["update-ref", "-d", "refs/remotes/-x/HEAD"]);
+ repo.run_git(&["switch", "-c", "alpha"]);
+ repo.run_git(&["branch", "beta"]);
+ repo.run_git(&["branch", "-D", "--", "main"]);
+
+ let branch = git_repo.default_branch();
+
+ assert_eq!(branch.as_deref(), Some("main"));
+}
+
#[rstest]
fn test_primary_remote_detects_custom_remote(mut repo: TestRepo) {
// Remove origin (fixture has it) so upstream becomes the primary
@@ -489,6 +511,18 @@ fn test_effective_remote_url_without_insteadof(repo: TestRepo) {
);
}
+/// Effective URL lookup must not parse a leading-dash remote as an option.
+#[rstest]
+fn test_effective_remote_url_with_leading_dash_remote(repo: TestRepo) {
+ repo.run_git(&["remote", "rename", "--", "origin", "-x"]);
+ let git_repo = Repository::at(repo.root_path()).unwrap();
+
+ assert_eq!(
+ git_repo.effective_remote_url("-x"),
+ git_repo.remote_url("-x")
+ );
+}
+
/// Test effective_remote_url: returns None for nonexistent remote.
#[rstest]
fn test_effective_remote_url_nonexistent_remote(repo: TestRepo) {