Skip to content

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

test: format fork remote URLs without spawning gh or glab (#4679)

Worktrunk Bot365539a266d3
diff --git a/src/git/remote_ref/github.rs b/src/git/remote_ref/github.rs
index ce6291706..db36780d4 100644
--- a/src/git/remote_ref/github.rs
+++ b/src/git/remote_ref/github.rs
@@ -8,7 +8,7 @@ use serde::Deserialize;
use super::{
CliApiRequest, PlatformData, RemoteRefInfo, cli_api_error, cli_config_value,
- extract_host_from_html_url, run_cli_api,
+ extract_host_from_html_url, host_remote_url, run_cli_api,
};
use crate::git::{ForgeKind, Repository};
use crate::shell_exec::Cmd;
@@ -232,32 +232,5 @@ pub fn is_authed_for(host: &str) -> bool {
/// Construct the remote URL for a fork repository.
pub fn fork_remote_url(host: &str, owner: &str, repo: &str) -> String {
- if use_ssh_protocol(host) {
- format!("git@{}:{}/{}.git", host, owner, repo)
- } else {
- format!("https://{}/{}/{}.git", host, owner, repo)
- }
-}
-
-#[cfg(test)]
-mod tests {
- use super::*;
-
- #[test]
- fn test_fork_remote_url_formats() {
- // Protocol depends on `gh config get git_protocol`, so just check format
- let url = fork_remote_url("github.com", "contributor", "repo");
- let valid_urls = [
- "[email protected]:contributor/repo.git",
- "https://github.com/contributor/repo.git",
- ];
- assert!(valid_urls.contains(&url.as_str()), "unexpected URL: {url}");
-
- let url = fork_remote_url("github.example.com", "org", "project");
- let valid_urls = [
- "[email protected]:org/project.git",
- "https://github.example.com/org/project.git",
- ];
- assert!(valid_urls.contains(&url.as_str()), "unexpected URL: {url}");
- }
+ host_remote_url(host, owner, repo, use_ssh_protocol(host))
}
diff --git a/src/git/remote_ref/gitlab.rs b/src/git/remote_ref/gitlab.rs
index b675a478d..2fefe3183 100644
--- a/src/git/remote_ref/gitlab.rs
+++ b/src/git/remote_ref/gitlab.rs
@@ -25,7 +25,8 @@ use anyhow::{Context, bail};
use serde::Deserialize;
use super::{
- CliApiRequest, PlatformData, RemoteRefInfo, cli_api_error, cli_config_value, run_cli_api,
+ CliApiRequest, PlatformData, RemoteRefInfo, cli_api_error, cli_config_value, host_remote_url,
+ run_cli_api,
};
use crate::git::{ForgeKind, Repository};
use crate::shell_exec::Cmd;
@@ -271,11 +272,7 @@ fn git_protocol(host: &str) -> String {
/// Construct the remote URL for a GitLab project, respecting protocol preference.
pub fn fork_remote_url(host: &str, owner: &str, repo: &str) -> String {
- if git_protocol(host) == "ssh" {
- format!("git@{host}:{owner}/{repo}.git")
- } else {
- format!("https://{host}/{owner}/{repo}.git")
- }
+ host_remote_url(host, owner, repo, git_protocol(host) == "ssh")
}
#[cfg(test)]
@@ -283,24 +280,6 @@ mod tests {
use super::*;
use crate::git::remote_ref::RemoteRefInfo;
- #[test]
- fn test_fork_remote_url_formats() {
- // Protocol depends on `glab config get git_protocol`, so just check format
- let url = fork_remote_url("gitlab.com", "contributor", "repo");
- let valid_urls = [
- "[email protected]:contributor/repo.git",
- "https://gitlab.com/contributor/repo.git",
- ];
- assert!(valid_urls.contains(&url.as_str()), "unexpected URL: {url}");
-
- let url = fork_remote_url("gitlab.example.com", "org", "project");
- let valid_urls = [
- "[email protected]:org/project.git",
- "https://gitlab.example.com/org/project.git",
- ];
- assert!(valid_urls.contains(&url.as_str()), "unexpected URL: {url}");
- }
-
#[test]
fn test_fetch_gitlab_project_urls_rejects_github_ref() {
let github_info = RemoteRefInfo {
diff --git a/src/git/remote_ref/mod.rs b/src/git/remote_ref/mod.rs
index ccf4796de..c935243d7 100644
--- a/src/git/remote_ref/mod.rs
+++ b/src/git/remote_ref/mod.rs
@@ -220,6 +220,15 @@ pub(super) fn extract_host_from_html_url(html_url: &str) -> anyhow::Result<Strin
.with_context(|| format!("Failed to parse host from PR URL: {html_url}"))
}
+/// The clone URL for `owner/repo` on `host`, over SSH or HTTPS.
+pub(super) fn host_remote_url(host: &str, owner: &str, repo: &str, ssh: bool) -> String {
+ if ssh {
+ format!("git@{host}:{owner}/{repo}.git")
+ } else {
+ format!("https://{host}/{owner}/{repo}.git")
+ }
+}
+
/// `<tool> config get <args>`, trimmed; `None` if the CLI can't answer.
pub(super) fn cli_config_value(tool: &str, args: &[&str]) -> Option<String> {
Cmd::new(tool)
@@ -532,6 +541,18 @@ fn azure_repo_segments(
mod tests {
use super::*;
+ #[test]
+ fn host_remote_url_formats_each_protocol() {
+ assert_eq!(
+ host_remote_url("github.com", "contributor", "repo", true),
+ "[email protected]:contributor/repo.git"
+ );
+ assert_eq!(
+ host_remote_url("gitlab.example.com", "org", "project", false),
+ "https://gitlab.example.com/org/project.git"
+ );
+ }
+
#[test]
fn api_url_host_uses_the_network_host_after_userinfo() {
assert_eq!(