From 7c36ae7668bf4e6d93c91db5e4ef041e6324dbd8 Mon Sep 17 00:00:00 2001 From: Zhaoqi Xu Date: Mon, 31 Aug 2026 19:29:18 +0800 Subject: [PATCH] fix(git): keep IPv6 brackets in submodule web links (#39186) Co-authored-by: wxiaoguang --- modules/git/url/url.go | 10 +++++++--- modules/git/url/url_test.go | 8 ++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/modules/git/url/url.go b/modules/git/url/url.go index d73a4fbcbf6..fdb6a1cf84d 100644 --- a/modules/git/url/url.go +++ b/modules/git/url/url.go @@ -171,11 +171,15 @@ func MakeRepositoryWebLink(repoURL *RepositoryURL) string { case "http", "https": return strings.TrimSuffix(repoURL.GitURL.String(), ".git") case "ssh", "git+ssh": - hostname, _, _ := net.SplitHostPort(repoURL.GitURL.Host) - hostname = util.IfZero(hostname, repoURL.GitURL.Host) + // only get the hostname part (with IPv6 square brackets) + host, _, _ := net.SplitHostPort(repoURL.GitURL.Host) + host = strings.TrimSuffix(net.JoinHostPort(host, "dummy-port"), ":dummy-port") + // if failed to parse the host, use it as is + host = util.IfZero(host, repoURL.GitURL.Host) + urlPath := strings.TrimSuffix(repoURL.GitURL.Path, ".git") urlPath = strings.TrimPrefix(urlPath, "/") - urlFull := fmt.Sprintf("https://%s/%s", hostname, urlPath) + urlFull := fmt.Sprintf("https://%s/%s", host, urlPath) urlFull = strings.TrimSuffix(urlFull, "/") return urlFull } diff --git a/modules/git/url/url_test.go b/modules/git/url/url_test.go index 3a6dcf9632e..093eb53c19c 100644 --- a/modules/git/url/url_test.go +++ b/modules/git/url/url_test.go @@ -264,4 +264,12 @@ func TestMakeRepositoryBaseLink(t *testing.T) { u, err = ParseRepositoryURL(t.Context(), "git+ssh://other:123/owner/repo.git") assert.NoError(t, err) assert.Equal(t, "https://other/owner/repo", MakeRepositoryWebLink(u)) + + u, err = ParseRepositoryURL(t.Context(), "git+ssh://[::1]/owner/repo.git") + assert.NoError(t, err) + assert.Equal(t, "https://[::1]/owner/repo", MakeRepositoryWebLink(u)) + + u, err = ParseRepositoryURL(t.Context(), "git+ssh://[::1]:2222/owner/repo.git") + assert.NoError(t, err) + assert.Equal(t, "https://[::1]/owner/repo", MakeRepositoryWebLink(u)) }