Skip to content

Commit 853a3a8

Browse files
committed
feat: support SCP-like parent URLs with relative submodules (#492)
1 parent 7ac38c1 commit 853a3a8

2 files changed

Lines changed: 35 additions & 4 deletions

File tree

git/git.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,22 @@ func RedactURL(u string) string {
513513
return u
514514
}
515515

516+
// reconstructSCP formats an Endpoint as an SCP-like URL (e.g. user@host:path or user@host:port:path).
517+
func reconstructSCP(ep *transport.Endpoint) string {
518+
var b strings.Builder
519+
if ep.User != "" {
520+
b.WriteString(ep.User)
521+
b.WriteString("@")
522+
}
523+
b.WriteString(ep.Host)
524+
if ep.Port != 0 {
525+
fmt.Fprintf(&b, ":%d", ep.Port)
526+
}
527+
b.WriteString(":")
528+
b.WriteString(strings.TrimPrefix(ep.Path, "/"))
529+
return b.String()
530+
}
531+
516532
// ResolveSubmoduleURL resolves a potentially relative submodule URL against a parent repository URL.
517533
func ResolveSubmoduleURL(parentURL, submoduleURL string) (string, error) {
518534
// 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) {
561577

562578
// Reconstruct the URL with the resolved path.
563579
parentEP.Path = path.Clean(currentPath)
580+
if !strings.Contains(parentURL, "://") {
581+
return reconstructSCP(parentEP), nil
582+
}
564583
return parentEP.String(), nil
565584
}
566585

git/git_test.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -825,25 +825,37 @@ func TestResolveSubmoduleURL(t *testing.T) {
825825
name: "scpRelativeSibling",
826826
parentURL: "git@github.com:org/main.git",
827827
subURL: "../deps/lib.git",
828-
expect: "ssh://git@github.com/org/deps/lib.git",
828+
expect: "git@github.com:org/deps/lib.git",
829829
},
830830
{
831831
name: "scpRelativeChild",
832832
parentURL: "git@github.com:org/main.git",
833833
subURL: "./extras/tool.git",
834-
expect: "ssh://git@github.com/org/main.git/extras/tool.git",
834+
expect: "git@github.com:org/main.git/extras/tool.git",
835835
},
836836
{
837837
name: "scpMultiLevelUp",
838838
parentURL: "git@github.com:a/b/c/repo.git",
839839
subURL: "../../other/lib.git",
840-
expect: "ssh://git@github.com/a/b/other/lib.git",
840+
expect: "git@github.com:a/b/other/lib.git",
841841
},
842842
{
843843
name: "scpWithPort",
844844
parentURL: "git@github.com:2222:org/main.git",
845845
subURL: "../deps/lib.git",
846-
expect: "ssh://git@github.com:2222/org/deps/lib.git",
846+
expect: "git@github.com:2222:org/deps/lib.git",
847+
},
848+
{
849+
name: "scpNoUserRelativeSibling",
850+
parentURL: "github.com:org/main.git",
851+
subURL: "../deps/lib.git",
852+
expect: "github.com:org/deps/lib.git",
853+
},
854+
{
855+
name: "scpIPAddressRelativeSibling",
856+
parentURL: "deploy@192.168.1.50:repos/main.git",
857+
subURL: "../deps/lib.git",
858+
expect: "deploy@192.168.1.50:repos/deps/lib.git",
847859
},
848860
{
849861
name: "httpsMultiLevelUp",

0 commit comments

Comments
 (0)