Skip to content

Commit

Permalink
internal/source: add support for dmitri.shuralyov.com/... packages
Browse files Browse the repository at this point in the history
Add support for a set of packages that defer to another website to
display their source code. To be able to produce expected URLs, we
need to add two more URL template variables:

• {importPath} - Package import path ("example.com/myrepo/mypkg").
• {base}       - Base name of file containing the identifier,
                 including file extension ("file.go").

Also add a new optional Repo URL template for overriding the home
page of the repository, which is something that was possible with
the original go-source meta tag.

Document the existing URL template variables, so that it's easier
to understand how to use them.

For golang/go#40477.

Change-Id: I70b857155f69c5c3ed41e78daccb90153a927290
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/246239
Run-TryBot: Dmitri Shuralyov <[email protected]>
TryBot-Result: kokoro <[email protected]>
Reviewed-by: Jonathan Amsterdam <[email protected]>
  • Loading branch information
dmitshur committed Aug 28, 2020
1 parent ea96ad2 commit 21b6d17
Show file tree
Hide file tree
Showing 3 changed files with 1,421 additions and 1,067 deletions.
67 changes: 52 additions & 15 deletions internal/source/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,18 @@ type Info struct {
templates urlTemplates // for building URLs
}

// RepoURL returns a URL for the home page of the repository.
func (i *Info) RepoURL() string {
if i == nil {
return ""
}
return i.repoURL
if i.templates.Repo == "" {
// The default repo template is just "{repo}".
return i.repoURL
}
return expand(i.templates.Repo, map[string]string{
"repo": i.repoURL,
})
}

// ModuleURL returns a URL for the home page of the module.
Expand All @@ -67,9 +74,10 @@ func (i *Info) DirectoryURL(dir string) string {
return ""
}
return strings.TrimSuffix(expand(i.templates.Directory, map[string]string{
"repo": i.repoURL,
"commit": i.commit,
"dir": path.Join(i.moduleDir, dir),
"repo": i.repoURL,
"importPath": path.Join(strings.TrimPrefix(i.repoURL, "https://"), dir),
"commit": i.commit,
"dir": path.Join(i.moduleDir, dir),
}), "/")
}

Expand All @@ -78,10 +86,13 @@ func (i *Info) FileURL(pathname string) string {
if i == nil {
return ""
}
dir, base := path.Split(pathname)
return expand(i.templates.File, map[string]string{
"repo": i.repoURL,
"commit": i.commit,
"file": path.Join(i.moduleDir, pathname),
"repo": i.repoURL,
"importPath": path.Join(strings.TrimPrefix(i.repoURL, "https://"), dir),
"commit": i.commit,
"file": path.Join(i.moduleDir, pathname),
"base": base,
})
}

Expand All @@ -90,11 +101,14 @@ func (i *Info) LineURL(pathname string, line int) string {
if i == nil {
return ""
}
dir, base := path.Split(pathname)
return expand(i.templates.Line, map[string]string{
"repo": i.repoURL,
"commit": i.commit,
"file": path.Join(i.moduleDir, pathname),
"line": strconv.Itoa(line),
"repo": i.repoURL,
"importPath": path.Join(strings.TrimPrefix(i.repoURL, "https://"), dir),
"commit": i.commit,
"file": path.Join(i.moduleDir, pathname),
"base": base,
"line": strconv.Itoa(line),
})
}

Expand Down Expand Up @@ -521,6 +535,17 @@ var patterns = []struct {
// URLs anyway. See gogs/gogs#6242.
templates: giteaURLTemplates,
},

{
pattern: `^(?P<repo>dmitri\.shuralyov\.com\/.+)$`,
templates: urlTemplates{
Repo: "{repo}/...",
Directory: "https://gotools.org/{importPath}?rev={commit}",
File: "https://gotools.org/{importPath}?rev={commit}#{base}",
Line: "https://gotools.org/{importPath}?rev={commit}#{base}-L{line}",
},
},

// Patterns that match the general go command pattern, where they must have
// a ".git" repo suffix in an import path. If matching a repo URL from a meta tag,
// there is no ".git".
Expand Down Expand Up @@ -577,11 +602,23 @@ func giteaTransformCommit(commit string, isHash bool) string {

// urlTemplates describes how to build URLs from bits of source information.
// The fields are exported for JSON encoding.
//
// The template variables are:
//
// • {repo} - Repository URL with "https://" prefix ("https://example.com/myrepo").
// • {importPath} - Package import path ("example.com/myrepo/mypkg").
// • {commit} - Tag name or commit hash corresponding to version ("v0.1.0" or "1234567890ab").
// • {dir} - Path to directory of the package, relative to repo root ("mypkg").
// • {file} - Path to file containing the identifier, relative to repo root ("mypkg/file.go").
// • {base} - Base name of file containing the identifier, including file extension ("file.go").
// • {line} - Line number for the identifier ("41").
//
type urlTemplates struct {
Directory string // URL template for a directory, with {repo}, {commit} and {dir}
File string // URL template for a file, with {repo}, {commit} and {file}
Line string // URL template for a line, with {repo}, {commit}, {file} and {line}
Raw string // URL template for the raw contents of a file, with {repo}, {repoPath}, {commit} and {file}
Repo string `json:",omitempty"` // Optional URL template for the repository home page, with {repo}. If left empty, a default template "{repo}" is used.
Directory string // URL template for a directory, with {repo}, {importPath}, {commit}, {dir}.
File string // URL template for a file, with {repo}, {importPath}, {commit}, {file}, {base}.
Line string // URL template for a line, with {repo}, {importPath}, {commit}, {file}, {base}, {line}.
Raw string // Optional URL template for the raw contents of a file, with {repo}, {commit}, {file}.
}

var (
Expand Down
32 changes: 28 additions & 4 deletions internal/source/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,14 +333,34 @@ func TestModuleInfo(t *testing.T) {
"https://github.com/gonum/gonum/blob/v0.6.1/doc.go#L1",
"https://github.com/gonum/gonum/raw/v0.6.1/doc.go",
},
{
"custom with gotools at repo root",
"dmitri.shuralyov.com/gpu/mtl", "v0.0.0-20191203043605-d42048ed14fd", "mtl.go",

"https://dmitri.shuralyov.com/gpu/mtl/...",
"https://gotools.org/dmitri.shuralyov.com/gpu/mtl?rev=d42048ed14fd",
"https://gotools.org/dmitri.shuralyov.com/gpu/mtl?rev=d42048ed14fd#mtl.go",
"https://gotools.org/dmitri.shuralyov.com/gpu/mtl?rev=d42048ed14fd#mtl.go-L1",
"",
},
{
"custom with gotools in subdir",
"dmitri.shuralyov.com/gpu/mtl", "v0.0.0-20191203043605-d42048ed14fd", "example/movingtriangle/internal/coreanim/coreanim.go",

"https://dmitri.shuralyov.com/gpu/mtl/...",
"https://gotools.org/dmitri.shuralyov.com/gpu/mtl?rev=d42048ed14fd",
"https://gotools.org/dmitri.shuralyov.com/gpu/mtl/example/movingtriangle/internal/coreanim?rev=d42048ed14fd#coreanim.go",
"https://gotools.org/dmitri.shuralyov.com/gpu/mtl/example/movingtriangle/internal/coreanim?rev=d42048ed14fd#coreanim.go-L1",
"",
},
} {
t.Run(test.desc, func(t *testing.T) {
info, err := ModuleInfo(context.Background(), &Client{client}, test.modulePath, test.version)
if err != nil {
t.Fatal(err)
}

check(t, "repo", info.repoURL, test.wantRepo)
check(t, "repo", info.RepoURL(), test.wantRepo)
check(t, "module", info.ModuleURL(), test.wantModule)
check(t, "file", info.FileURL(test.file), test.wantFile)
check(t, "line", info.LineURL(test.file, 1), test.wantLine)
Expand Down Expand Up @@ -837,6 +857,10 @@ func TestJSON(t *testing.T) {
&Info{repoURL: "r", moduleDir: "m", commit: "c", templates: urlTemplates{File: "f"}},
`{"RepoURL":"r","ModuleDir":"m","Commit":"c","Templates":{"Directory":"","File":"f","Line":"","Raw":""}}`,
},
{
&Info{repoURL: "r", moduleDir: "m", commit: "c", templates: urlTemplates{Repo: "r", File: "f"}},
`{"RepoURL":"r","ModuleDir":"m","Commit":"c","Templates":{"Repo":"r","Directory":"","File":"f","Line":"","Raw":""}}`,
},
} {
bytes, err := json.Marshal(&test.in)
if err != nil {
Expand Down Expand Up @@ -877,9 +901,9 @@ func TestURLTemplates(t *testing.T) {
}
}

check(p.templates.Directory, "commit", "dir")
check(p.templates.File, "commit", "file")
check(p.templates.Line, "commit", "file", "line")
check(p.templates.Directory, "commit")
check(p.templates.File, "commit")
check(p.templates.Line, "commit", "line")
check(p.templates.Raw, "commit", "file")
}
}
Loading

0 comments on commit 21b6d17

Please sign in to comment.