Skip to content

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

test(switch): cover GitLab fork remote lookup across ssh/https (#2596)

Maximilian Roosa390d7e34ddf
diff --git a/src/commands/worktree/switch.rs b/src/commands/worktree/switch.rs
index b66394389..5965dc377 100644
--- a/src/commands/worktree/switch.rs
+++ b/src/commands/worktree/switch.rs
@@ -223,9 +223,8 @@ fn resolve_fork_ref(
ref_type.display(number)
)
})?;
- // TODO(gitlab-protocol): We only try the URL based on glab's git_protocol setting.
- // If the user's remote uses the other protocol (ssh vs https), we'll fail to find it.
- // Consider trying both ssh and https URLs before erroring.
+ // find_remote_by_url matches by (host, owner, repo); ssh vs https
+ // doesn't matter (test_find_remote_by_url_cross_protocol).
let remote = repo.find_remote_by_url(&target_url).ok_or_else(|| {
anyhow::anyhow!(
"No remote found for target project; \
diff --git a/tests/integration_tests/default_branch.rs b/tests/integration_tests/default_branch.rs
index 558424c3c..530c8724c 100644
--- a/tests/integration_tests/default_branch.rs
+++ b/tests/integration_tests/default_branch.rs
@@ -487,6 +487,41 @@ fn test_find_remote_for_repo_insteadof_multiple_remotes(repo: TestRepo) {
);
}
+/// find_remote_by_url matches a remote regardless of which transport
+/// protocol (ssh vs https) the lookup URL uses, because both URLs are parsed
+/// into (host, owner, repo) before matching. This is what lets `wt switch`
+/// resolve a GitLab fork target whether the user's remote is configured with
+/// SSH and glab returns HTTPS, or vice versa.
+#[rstest]
+fn test_find_remote_by_url_cross_protocol(repo: TestRepo) {
+ // Remote configured with HTTPS
+ repo.run_git(&["remote", "remove", "origin"]);
+ repo.run_git(&[
+ "remote",
+ "add",
+ "origin",
+ "https://gitlab.com/group/subgroup/proj.git",
+ ]);
+
+ let git_repo = Repository::at(repo.root_path()).unwrap();
+
+ // Lookup with SSH form should still find it
+ let found = git_repo.find_remote_by_url("[email protected]:group/subgroup/proj.git");
+ assert_eq!(found.as_deref(), Some("origin"));
+
+ // And vice versa: re-configure with SSH, look up with HTTPS
+ repo.run_git(&["remote", "remove", "origin"]);
+ repo.run_git(&[
+ "remote",
+ "add",
+ "origin",
+ "[email protected]:group/subgroup/proj.git",
+ ]);
+ let git_repo = Repository::at(repo.root_path()).unwrap();
+ let found = git_repo.find_remote_by_url("https://gitlab.com/group/subgroup/proj.git");
+ assert_eq!(found.as_deref(), Some("origin"));
+}
+
/// Test find_remote_by_url: resolves through insteadOf.
#[rstest]
fn test_find_remote_by_url_insteadof(repo: TestRepo) {