Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Comment on lines 578 to 583
}

Expand Down
20 changes: 16 additions & 4 deletions git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down