aboutsummaryrefslogtreecommitdiff
path: root/internal/source/source.go
diff options
context:
space:
mode:
authorJonathan Amsterdam <jba@google.com>2021-02-01 17:41:51 -0500
committerJonathan Amsterdam <jba@google.com>2021-02-02 14:36:21 +0000
commitff8cbf2add81b8cf5cd63b0c6f9744dfd79b8de1 (patch)
tree1ed582475d6329e3241e0f9f647a68a4b9b953af /internal/source/source.go
parenta9a10d0c1d62ec4a2d2266dafd4737a475cf74d7 (diff)
downloadgo-x-pkgsite-ff8cbf2add81b8cf5cd63b0c6f9744dfd79b8de1.tar.xz
internal/source: trim .git suffix from repo URLs
Remove the ".git" from the repo URL of source templates when we know it will result in invalid URLs. The ".git" is understood by the Go command, but not by some code hosting sites. To be safe, only remove it on code hosting sites we've tested. For now, that's GitHub and GitLab. Remove the suffix in three places: 1. When we construct a SourceInfo from the repo URL and meta tags. This will fix newly processed modules. 2. When we unmarshal a SourceInfo from the DB. This will fix it during rendering for already-processed modules. It will save us from having to reprocess or backfill. 3. In the function we use in tests. Fixes golang/go#44032 Change-Id: Ic6d48d0422dc12ce8dbc4586f8d6c586f463531e Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/288612 Trust: Jonathan Amsterdam <jba@google.com> Run-TryBot: Jonathan Amsterdam <jba@google.com> TryBot-Result: kokoro <noreply+kokoro@google.com> Reviewed-by: Jamal Carvalho <jamal@golang.org>
Diffstat (limited to 'internal/source/source.go')
-rw-r--r--internal/source/source.go26
1 files changed, 23 insertions, 3 deletions
diff --git a/internal/source/source.go b/internal/source/source.go
index 456d6318..f207478f 100644
--- a/internal/source/source.go
+++ b/internal/source/source.go
@@ -192,7 +192,7 @@ func (i *Info) UnmarshalJSON(data []byte) (err error) {
if err := json.Unmarshal(data, &ji); err != nil {
return err
}
- i.repoURL = ji.RepoURL
+ i.repoURL = trimVCSSuffix(ji.RepoURL)
i.moduleDir = ji.ModuleDir
i.commit = ji.Commit
if ji.Kind != "" {
@@ -275,7 +275,7 @@ func ModuleInfo(ctx context.Context, client *Client, modulePath, version string)
commit = transformCommit(commit, isHash)
}
info = &Info{
- repoURL: "https://" + repo,
+ repoURL: trimVCSSuffix("https://" + repo),
moduleDir: relativeModulePath,
commit: commit,
templates: templates,
@@ -755,6 +755,26 @@ func commitFromVersion(vers, relativeModulePath string) (commit string, isHash b
}
}
+// trimVCSSuffix removes a VCS suffix from a repo URL in selected cases.
+//
+// The Go command allows a VCS suffix on a repo, like github.com/foo/bar.git. But
+// some code hosting sites don't support all paths constructed from such URLs.
+// For example, GitHub will redirect github.com/foo/bar.git to github.com/foo/bar,
+// but will 404 on github.com/goo/bar.git/tree/master and any other URL with a
+// non-empty path.
+//
+// To be conservative, we remove the suffix only in cases where we know it's
+// wrong.
+func trimVCSSuffix(repoURL string) string {
+ if !strings.HasSuffix(repoURL, ".git") {
+ return repoURL
+ }
+ if strings.HasPrefix(repoURL, "https://github.com/") || strings.HasPrefix(repoURL, "https://gitlab.com/") {
+ return strings.TrimSuffix(repoURL, ".git")
+ }
+ return repoURL
+}
+
// The following code copied from cmd/go/internal/get:
// expand rewrites s to replace {k} with match[k] for each key k in match.
@@ -773,7 +793,7 @@ func expand(s string, match map[string]string) string {
// It is for testing only.
func NewGitHubInfo(repoURL, moduleDir, commit string) *Info {
return &Info{
- repoURL: repoURL,
+ repoURL: trimVCSSuffix(repoURL),
moduleDir: moduleDir,
commit: commit,
templates: githubURLTemplates,