diff --git a/git/git.go b/git/git.go index a5fcdfe..d80eda3 100644 --- a/git/git.go +++ b/git/git.go @@ -513,6 +513,22 @@ func RedactURL(u string) string { return u } +// reconstructSCP formats an Endpoint as an SCP-like URL (e.g. user@host:path or user@host:port:path). +func reconstructSCP(ep *transport.Endpoint) string { + var b strings.Builder + if ep.User != "" { + b.WriteString(ep.User) + b.WriteString("@") + } + b.WriteString(ep.Host) + if ep.Port != 0 { + fmt.Fprintf(&b, ":%d", ep.Port) + } + b.WriteString(":") + b.WriteString(strings.TrimPrefix(ep.Path, "/")) + return b.String() +} + // ResolveSubmoduleURL resolves a potentially relative submodule URL against a parent repository URL. func ResolveSubmoduleURL(parentURL, submoduleURL string) (string, error) { // If the submodule URL is absolute (contains ://) or doesn't start with ./ or ../, return it as-is @@ -561,6 +577,9 @@ func ResolveSubmoduleURL(parentURL, submoduleURL string) (string, error) { // Reconstruct the URL with the resolved path. parentEP.Path = path.Clean(currentPath) + if !strings.Contains(parentURL, "://") { + return reconstructSCP(parentEP), nil + } return parentEP.String(), nil } diff --git a/git/git_test.go b/git/git_test.go index f3febd3..2662b42 100644 --- a/git/git_test.go +++ b/git/git_test.go @@ -825,25 +825,37 @@ func TestResolveSubmoduleURL(t *testing.T) { name: "scpRelativeSibling", parentURL: "git@github.com:org/main.git", subURL: "../deps/lib.git", - expect: "ssh://git@github.com/org/deps/lib.git", + expect: "git@github.com:org/deps/lib.git", }, { name: "scpRelativeChild", parentURL: "git@github.com:org/main.git", subURL: "./extras/tool.git", - expect: "ssh://git@github.com/org/main.git/extras/tool.git", + expect: "git@github.com:org/main.git/extras/tool.git", }, { name: "scpMultiLevelUp", parentURL: "git@github.com:a/b/c/repo.git", subURL: "../../other/lib.git", - expect: "ssh://git@github.com/a/b/other/lib.git", + expect: "git@github.com:a/b/other/lib.git", }, { name: "scpWithPort", parentURL: "git@github.com:2222:org/main.git", subURL: "../deps/lib.git", - expect: "ssh://git@github.com:2222/org/deps/lib.git", + expect: "git@github.com:2222:org/deps/lib.git", + }, + { + name: "scpNoUserRelativeSibling", + parentURL: "github.com:org/main.git", + subURL: "../deps/lib.git", + expect: "github.com:org/deps/lib.git", + }, + { + name: "scpIPAddressRelativeSibling", + parentURL: "deploy@192.168.1.50:repos/main.git", + subURL: "../deps/lib.git", + expect: "deploy@192.168.1.50:repos/deps/lib.git", }, { name: "httpsMultiLevelUp",