Involved Source Filesmeta-tags.go
Package source constructs public URLs that link to the source files in a module. It
can be used to build references to Go source code, or to any other files in a
module.
Of course, the module zip file contains all the files in the module. This
package attempts to find the origin of the zip file, in a repository that is
publicly readable, and constructs links to that repo. While a module zip file
could in theory come from anywhere, including a non-public location, this
package recognizes standard module path patterns and construct repository
URLs from them, like the go command does.
Package-Level Type Names (total 6, in which 2 are exported)
jsonInfo is a Go struct describing the JSON structure of an INFO.
Commitstring
Store common templates efficiently by setting this to a short string
we look up in a map. If Kind != "", then Templates == nil.
ModuleDirstringRepoURLstringTemplates*urlTemplates
sourceMeta represents the values in a go-source meta tag, or as a fallback,
values from a go-import meta tag.
The go-source spec is at https://github.com/golang/gddo/wiki/Source-Code-Links.
The go-import spec is in "go help importpath".
The next two are only present in a go-source tag.
// URL template for a directory
// URL template for a file and line
// import path prefix corresponding to repo root
// URL of the repo root
func fetchMeta(ctx context.Context, client *Client, importPath string) (_ *sourceMeta, err error)
func parseMeta(importPath string, r io.Reader) (sm *sourceMeta, err error)
func matchLegacyTemplates(ctx context.Context, sm *sourceMeta) (_ urlTemplates, transformCommit transformCommitFunc)
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").
// URL template for a directory, with {repo}, {importPath}, {commit}, {dir}.
// URL template for a file, with {repo}, {importPath}, {commit}, {file}, {base}.
// URL template for a line, with {repo}, {importPath}, {commit}, {file}, {base}, {line}.
// Optional URL template for the raw contents of a file, with {repo}, {commit}, {file}.
// Optional URL template for the repository home page, with {repo}. If left empty, a default template "{repo}" is used.
func matchLegacyTemplates(ctx context.Context, sm *sourceMeta) (_ urlTemplates, transformCommit transformCommitFunc)
func matchStatic(moduleOrRepoPath string) (repo, relativeModulePath string, _ urlTemplates, transformCommit transformCommitFunc, _ error)
var bitbucketURLTemplates
var csopensourceTemplates
var fdioURLTemplates
var giteaURLTemplates
var githubURLTemplates
var gitlab2URLTemplates
var googlesourceURLTemplates
Package-Level Functions (total 21, in which 5 are exported)
ModuleInfo determines the repository corresponding to the module path. It
returns a URL to that repo, as well as the directory of the module relative
to the repo root.
ModuleInfo may fetch from arbitrary URLs, so it can be slow.
New constructs a *Client using the provided timeout.
NewClientForTesting returns a Client suitable for testing. It returns the
same results as an ordinary client for statically recognizable paths, but
always returns a nil *Info for dynamic paths (those requiring HTTP requests).
NewGitHubInfo creates a source.Info with GitHub URL templates.
It is for testing only.
NewStdlibInfo returns a source.Info for the standard library at the given
semantic version. It panics if the version does not correspond to a Go release
tag. It is for testing only.
adjustVersionedModuleDirectory changes info.moduleDir if necessary to
correctly reflect the repo structure. info.moduleDir will be wrong if it has
a suffix "/vN" for N > 1, and the repo uses the "major branch" convention,
where modules at version 2 and higher live on branches rather than
subdirectories. See https://research.swtch.com/vgo-module for a discussion of
the "major branch" vs. "major subdirectory" conventions for organizing a
repo.
commitFromVersion returns a string that refers to a commit corresponding to version.
It also reports whether the returned value is a commit hash.
The string may be a tag, or it may be the hash or similar unique identifier of a commit.
The second argument is the module path relative to the repo root.
expand rewrites s to replace {k} with match[k] for each key k in match.
fetchMeta retrieves go-import and go-source meta tag information, using the import path to construct
a URL as described in "go help importpath".
The importPath argument, as the name suggests, could be any package import
path. But we only pass module paths.
The discovery site only cares about linking to source, not fetching it (we
already have it in the module zip file). So we merge the go-import and
go-source meta tag information, preferring the latter.
giteaTransformCommit transforms commits for the Gitea code hosting system.
matchLegacyTemplates matches the templates from the go-source meta tag
against some known patterns to guess the version-aware URL templates. If it
can't find a match, it falls back using the go-source templates with some
small replacements. These will not be version-aware but will still serve
source at a fixed commit, which is better than nothing.
matchStatic matches the given module or repo path against a list of known
patterns. It returns the repo name, the module path relative to the repo
root, and URL templates if there is a match.
The relative module path may not be correct in all cases: it is wrong if it
ends in a version that is not part of the repo directory structure, because
the repo follows the "major branch" convention for versions 2 and above.
E.g. this function could return "foo/v2", but the module files live under "foo"; the
"/v2" is part of the module path (and the import paths of its packages) but
is not a subdirectory. This mistake is corrected in adjustVersionedModuleDirectory,
once we have all the information we need to fix it.
repo + "/" + relativeModulePath is often, but not always, equal to
moduleOrRepoPath. It is not when the argument is a module path that uses the
go command's general syntax, which ends in a ".vcs" (e.g. ".git", ".hg") that
is neither part of the repo nor the suffix. For example, if the argument is
github.com/a/b/c
then repo="github.com/a/b" and relativeModulePath="c"; together they make up the module path.
But if the argument is
example.com/a/b.git/c
then repo="example.com/a/b" and relativeModulePath="c"; the ".git" is omitted, since it is neither
part of the repo nor part of the relative path to the module within the repo.
moduleInfoDynamic uses the go-import and go-source meta tags to construct an Info.
removeHTTPScheme removes an initial "http://" or "https://" from url.
The result can be used to match against our static patterns.
If the URL uses a different scheme, it won't be removed and it won't
match any patterns, as intended.
removeVersionSuffix returns s with "/vN" removed if N is an integer > 1.
Otherwise it returns s.
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.
Package-Level Variables (total 10, none are exported)
List of template regexps and their corresponding likely templates,
used by matchLegacyTemplates below.
Patterns for determining repo and URL templates from module paths or repo
URLs. Each regexp must match a prefix of the target string, and must have a
group named "repo".
map of common urlTemplates
The pages are generated with Goldsv0.3.2-preview. (GOOS=darwin GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds.