Source File
source.go
Belonging Package
golang.org/x/pkgsite/internal/source
package source
import (
)
func ( *Info) () string {
return .DirectoryURL("")
}
func ( *Info) ( string, int) string {
if == nil {
return ""
}
, := path.Split()
return expand(.templates.Line, map[string]string{
"repo": .repoURL,
"importPath": path.Join(strings.TrimPrefix(.repoURL, "https://"), ),
"commit": .commit,
"file": path.Join(.moduleDir, ),
"dir": ,
"base": ,
"line": strconv.Itoa(),
})
}
var urlTemplatesByKind = map[string]urlTemplates{
"github": githubURLTemplates,
"gitlab": githubURLTemplates, // preserved for backwards compatibility (DB still has source_info->Kind = "gitlab")
"bitbucket": bitbucketURLTemplates,
}
Kind string `json:",omitempty"`
Templates *urlTemplates `json:",omitempty"`
}
for , := range urlTemplatesByKind {
if .templates == {
.Kind =
break
}
if .Kind == "gitlab" {
.Kind = "github"
}
if .Kind == "" && .templates != (urlTemplates{}) {
.Templates = &.templates
}
return json.Marshal()
}
func ( *Info) ( []byte) ( error) {
defer derrors.Wrap(&, "UnmarshalJSON(data)")
var jsonInfo
if := json.Unmarshal(, &); != nil {
return
}
.repoURL = trimVCSSuffix(.RepoURL)
.moduleDir = .ModuleDir
.commit = .Commit
if .Kind != "" {
.templates = urlTemplatesByKind[.Kind]
} else if .Templates != nil {
.templates = *.Templates
}
return nil
}
httpClient *http.Client
}
func ( *Client) ( context.Context, , string, bool) ( *http.Response, error) {
defer derrors.Wrap(&, "doURL(ctx, client, %q, %q)", , )
if == nil || .httpClient == nil {
return nil, fmt.Errorf("c.httpClient cannot be nil")
}
, := http.NewRequest(, , nil)
if != nil {
return nil,
}
, := ctxhttp.Do(, .httpClient, )
if != nil {
return nil,
}
if && .StatusCode != 200 {
.Body.Close()
return nil, fmt.Errorf("status %s", .Status)
}
return , nil
}
if strings.HasPrefix(, "example.com/") {
return NewGitHubInfo("https://"+, "", ), nil
}
if == stdlib.ModulePath {
return newStdlibInfo()
}
, , , , := matchStatic()
if != nil {
, = moduleInfoDynamic(, , , )
if != nil {
return nil,
}
} else {
, := commitFromVersion(, )
if != nil {
= (, )
}
= &Info{
repoURL: trimVCSSuffix("https://" + ),
moduleDir: ,
commit: ,
templates: ,
}
}
if != nil {
adjustVersionedModuleDirectory(, , )
}
}
func ( string) ( *Info, error) {
defer derrors.Wrap(&, "newStdlibInfo(%q)", )
, := stdlib.TagForVersion()
if != nil {
return nil,
}
:= csopensourceTemplates
.Raw = "https://github.com/golang/go/raw/{commit}/{file}"
return &Info{
repoURL: stdlib.GoSourceRepoURL,
moduleDir: stdlib.Directory(),
commit: ,
templates: ,
}, nil
}
func ( string) (, string, urlTemplates, transformCommitFunc, error) {
for , := range patterns {
:= .re.FindStringSubmatch()
if == nil {
continue
}
var string
for , := range .re.SubexpNames() {
if == "repo" {
= []
break
}
if strings.HasPrefix(, "blitiri.com.ar/") {
= strings.Replace(, "/go/", "/git/r/", 1)
}
= strings.TrimPrefix(, [0])
= strings.TrimPrefix(, "/")
return , , .templates, .transformCommit, nil
}
return "", "", urlTemplates{}, nil, derrors.NotFound
}
:= .repoURL
if == (urlTemplates{}) {
var string
, _, , , _ = matchStatic(removeHTTPScheme(.dirTemplate))
if == (urlTemplates{}) {
if == nil {
, = matchLegacyTemplates(, )
= strings.TrimSuffix(, ".git")
} else {
log.Infof(, "no templates for repo URL %q from meta tag: err=%v", .repoURL, )
}
= "https://" +
}
}
:= strings.TrimPrefix(strings.TrimPrefix(, .repoRootPrefix), "/")
, := commitFromVersion(, )
if != nil {
= (, )
}
return &Info{
repoURL: strings.TrimSuffix(, "/"),
moduleDir: ,
commit: ,
templates: ,
}, nil
}
var legacyTemplateMatches = []struct {
fileRegexp *regexp.Regexp
templates urlTemplates
transformCommit transformCommitFunc
}{
{
regexp.MustCompile(`/src/branch/\w+\{/dir\}/\{file\}#L\{line\}$`),
giteaURLTemplates, giteaTransformCommit,
},
{
regexp.MustCompile(`/src/\w+\{/dir\}/\{file\}#L\{line\}$`),
giteaURLTemplates, nil,
},
{
regexp.MustCompile(`/-/blob/\w+\{/dir\}/\{file\}#L\{line\}$`),
gitlab2URLTemplates, nil,
},
{
regexp.MustCompile(`/tree\{/dir\}/\{file\}#n\{line\}$`),
fdioURLTemplates, fdioTransformCommit,
},
}
func ( context.Context, *sourceMeta) ( urlTemplates, transformCommitFunc) {
if .fileTemplate == "" {
return urlTemplates{}, nil
}
for , := range legacyTemplateMatches {
if .fileRegexp.MatchString(.fileTemplate) {
return .templates, .transformCommit
}
}
log.Infof(, "matchLegacyTemplates: no matches for repo URL %q; replacing", .repoURL)
:= strings.NewReplacer(
"{/dir}/{file}", "/{file}",
"{dir}/{file}", "{file}",
"{/dir}", "/{dir}")
:= .Replace(.fileTemplate)
:=
if := strings.LastIndexByte(, '#'); > 0 {
= [:]
}
return urlTemplates{
Repo: .repoURL,
Directory: .Replace(.dirTemplate),
File: ,
Line: ,
}, nil
}
var patterns = []struct {
pattern string // uncompiled regexp
templates urlTemplates
transformCommit transformCommitFunc
}{
{
pattern: `^(?P<repo>github\.com/[a-z0-9A-Z_.\-]+/[a-z0-9A-Z_.\-]+)`,
templates: githubURLTemplates,
},
{
pattern: `^(?P<repo>bitbucket\.org/[a-z0-9A-Z_.\-]+/[a-z0-9A-Z_.\-]+)`,
templates: bitbucketURLTemplates,
},
{
pattern: `^(?P<repo>gitlab\.com/[a-z0-9A-Z_.\-]+/[a-z0-9A-Z_.\-]+)`,
templates: githubURLTemplates,
},
pattern: `^(?P<repo>gitlab\.[a-z0-9A-Z.-]+/[a-z0-9A-Z_.\-]+/[a-z0-9A-Z_.\-]+)(\.git|$)`,
templates: githubURLTemplates,
},
{
pattern: `^(?P<repo>gitee\.com/[a-z0-9A-Z_.\-]+/[a-z0-9A-Z_.\-]+)(\.git|$)`,
templates: githubURLTemplates,
},
{
pattern: `^(?P<repo>git\.sr\.ht/~[a-z0-9A-Z_.\-]+/[a-z0-9A-Z_.\-]+)`,
templates: urlTemplates{
Directory: "{repo}/tree/{commit}/{dir}",
File: "{repo}/tree/{commit}/{file}",
Line: "{repo}/tree/{commit}/{file}#L{line}",
Raw: "{repo}/blob/{commit}/{file}",
},
},
{
pattern: `^(?P<repo>git\.fd\.io/[a-z0-9A-Z_.\-]+)`,
templates: fdioURLTemplates,
transformCommit: fdioTransformCommit,
},
{
pattern: `^(?P<repo>git\.pirl\.io/[a-z0-9A-Z_.\-]+/[a-z0-9A-Z_.\-]+)`,
templates: gitlab2URLTemplates,
},
{
pattern: `^(?P<repo>gitea\.com/[a-z0-9A-Z_.\-]+/[a-z0-9A-Z_.\-]+)(\.git|$)`,
templates: giteaURLTemplates,
transformCommit: giteaTransformCommit,
},
pattern: `^(?P<repo>gitea\.[a-z0-9A-Z.-]+/[a-z0-9A-Z_.\-]+/[a-z0-9A-Z_.\-]+)(\.git|$)`,
templates: giteaURLTemplates,
transformCommit: giteaTransformCommit,
},
{
pattern: `^(?P<repo>go\.isomorphicgo\.org/[a-z0-9A-Z_.\-]+/[a-z0-9A-Z_.\-]+)(\.git|$)`,
templates: giteaURLTemplates,
transformCommit: giteaTransformCommit,
},
{
pattern: `^(?P<repo>git\.openprivacy\.ca/[a-z0-9A-Z_.\-]+/[a-z0-9A-Z_.\-]+)(\.git|$)`,
templates: giteaURLTemplates,
transformCommit: giteaTransformCommit,
},
{
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}",
},
},
{
pattern: `^(?P<repo>blitiri\.com\.ar/go/.+)$`,
templates: urlTemplates{
Repo: "{repo}",
Directory: "{repo}/b/master/t/{dir}",
File: "{repo}/b/master/t/{dir}f={file}.html",
Line: "{repo}/b/master/t/{dir}f={file}.html#line-{line}",
},
},
{
pattern: `^(?P<repo>[^.]+\.googlesource\.com/[^.]+)(\.git|$)`,
templates: googlesourceURLTemplates,
},
{
pattern: `^(?P<repo>git\.apache\.org/[^.]+)(\.git|$)`,
templates: githubURLTemplates,
{
pattern: `(?P<repo>([a-z0-9.\-]+\.)+[a-z0-9.\-]+(:[0-9]+)?(/~?[A-Za-z0-9_.\-]+)+?)\.(bzr|fossil|git|hg|svn)`,
templates: urlTemplates{},
},
}
func () {
for := range patterns {
if {
return "commit/" +
}
return "tag/" +
}
type urlTemplates struct {
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 (
githubURLTemplates = urlTemplates{
Directory: "{repo}/tree/{commit}/{dir}",
File: "{repo}/blob/{commit}/{file}",
Line: "{repo}/blob/{commit}/{file}#L{line}",
Raw: "{repo}/raw/{commit}/{file}",
}
bitbucketURLTemplates = urlTemplates{
Directory: "{repo}/src/{commit}/{dir}",
File: "{repo}/src/{commit}/{file}",
Line: "{repo}/src/{commit}/{file}#lines-{line}",
Raw: "{repo}/raw/{commit}/{file}",
}
giteaURLTemplates = urlTemplates{
Directory: "{repo}/src/{commit}/{dir}",
File: "{repo}/src/{commit}/{file}",
Line: "{repo}/src/{commit}/{file}#L{line}",
Raw: "{repo}/raw/{commit}/{file}",
}
googlesourceURLTemplates = urlTemplates{
Directory: "{repo}/+/{commit}/{dir}",
File: "{repo}/+/{commit}/{file}",
}
gitlab2URLTemplates = urlTemplates{
Directory: "{repo}/-/tree/{commit}/{dir}",
File: "{repo}/-/blob/{commit}/{file}",
Line: "{repo}/-/blob/{commit}/{file}#L{line}",
Raw: "{repo}/-/raw/{commit}/{file}",
}
fdioURLTemplates = urlTemplates{
Directory: "{repo}/tree/{dir}?{commit}",
File: "{repo}/tree/{file}?{commit}",
Line: "{repo}/tree/{file}?{commit}#n{line}",
Raw: "{repo}/plain/{file}?{commit}",
}
csopensourceTemplates = urlTemplates{
Directory: "{repo}/+/{commit}:{dir}",
File: "{repo}/+/{commit}:{file}",
}
)
:= strings.TrimSuffix(, "+incompatible")
:= removeVersionSuffix()
if != "" {
return + "/" + , false
}
return , false
}
}
func (, , string) *Info {
return &Info{
repoURL: trimVCSSuffix(),
moduleDir: ,
commit: ,
templates: githubURLTemplates,
}
}
func ( string) *Info {
, := newStdlibInfo()
if != nil {
panic()
}
return
![]() |
The pages are generated with Golds v0.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. |