Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions internal/source/meta-tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
type sourceMeta struct {
repoRootPrefix string // import path prefix corresponding to repo root
repoURL string // URL of the repo root
repoSubdir string // subdirectory within the repo (since Go 1.25)
// The next two are only present in a go-source tag.
dirTemplate string // URL template for a directory
fileTemplate string // URL template for a file and line
Expand Down Expand Up @@ -98,8 +99,16 @@ metaScan:
}
switch nameAttr {
case "go-import":
if len(fields) != 3 {
errorMessage = "go-import meta tag content attribute does not have three fields"
repoSubdir := ""
switch len(fields) {
case 3:
// go-import meta tag support minium 3 fields (root-path vcs repo-url).
break // good to proceed
case 4:
// go-import meta tag support minium 3 fields (root-path vcs repo-url), and additionally "subdirectory" field since Go 1.25.
repoSubdir = fields[3]
default:
errorMessage = "go-import meta tag content attribute does not have three or four fields"
continue metaScan
}
if fields[1] == "mod" {
Expand All @@ -114,6 +123,7 @@ metaScan:
sm = &sourceMeta{
repoRootPrefix: repoRootPrefix,
repoURL: fields[2],
repoSubdir: repoSubdir,
}
// Keep going in the hope of finding a go-source tag.
case "go-source":
Expand All @@ -128,16 +138,23 @@ metaScan:
}
// If go-source repo is "_", then default to the go-import repo.
repoURL := fields[1]
// reuse subdirectory field from go-import if present
repoSubdir := ""
if sm != nil {
repoSubdir = sm.repoSubdir
}
if repoURL == "_" {
if sm == nil {
errorMessage = `go-source repo is "_", but no previous go-import tag`
break metaScan
}
repoURL = sm.repoURL
repoSubdir = sm.repoSubdir
}
sm = &sourceMeta{
repoRootPrefix: repoRootPrefix,
repoURL: repoURL,
repoSubdir: repoSubdir,
dirTemplate: fields[2],
fileTemplate: fields[3],
}
Expand Down
1 change: 1 addition & 0 deletions internal/source/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ func moduleInfoDynamic(ctx context.Context, client *Client, modulePath, version
}
}
dir := strings.TrimPrefix(strings.TrimPrefix(modulePath, sourceMeta.repoRootPrefix), "/")
dir = path.Join(sourceMeta.repoSubdir, dir)
commit, isHash := commitFromVersion(version, dir)
if transformCommit != nil {
commit = transformCommit(commit, isHash)
Expand Down
46 changes: 41 additions & 5 deletions internal/source/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,36 @@ func TestModuleInfoDynamic(t *testing.T) {
// empty templates
},
},
{
"slatedb.io/slatedb-go",
// Package with go-import meta tag, where there is subdirectory field (since Go 1.25).
&Info{
repoURL: "https://github.com/slatedb/slatedb",
moduleDir: "slatedb-go/go",
commit: "slatedb-go/go/v1.2.3",
templates: githubURLTemplates,
},
},
{
"edv1n.github.io/go-get-subdirectory-test",
// Package with go-import meta tag, where there is subdirectory field with value `gopkg`.
&Info{
repoURL: "https://github.com/edv1n/go-get-subdirectory-test",
moduleDir: "gopkg",
commit: "gopkg/v1.2.3",
templates: githubURLTemplates,
},
},
{
"edv1n.github.io/go-get-subdirectory-test/sub",
// Package with go-import meta tag, where there is subdirectory field with value `gopkg/sub`.
&Info{
repoURL: "https://github.com/edv1n/go-get-subdirectory-test",
moduleDir: "gopkg/sub",
commit: "gopkg/sub/v1.2.3",
templates: githubURLTemplates,
},
},
{
"azul3d.org/examples/abs",
// The go-source tag has a template that is handled incorrectly by godoc; but we
Expand Down Expand Up @@ -436,6 +466,12 @@ var testWeb = map[string]string{
<head><meta name="go-import" content="bob.com/bad/github git https://github.com/bob/bad/&quot;&gt;$">`,
"https://bob.com/bad/apache": `
<head><meta name="go-import" content="bob.com/bad/apache git https://git.apache.org/&gt;$">`,
// Package with go-import meta tag, where there is subdirectory field (since Go 1.25).
"https://slatedb.io/slatedb-go": `<head><meta name="go-import" content="slatedb.io/slatedb-go git https://github.com/slatedb/slatedb slatedb-go/go">`,
"https://edv1n.github.io/go-get-subdirectory-test": `<head>` +
`<meta name="go-import" content="edv1n.github.io/go-get-subdirectory-test git https://github.com/edv1n/go-get-subdirectory-test gopkg">`,
"https://edv1n.github.io/go-get-subdirectory-test/sub": `<head>` +
`<meta name="go-import" content="edv1n.github.io/go-get-subdirectory-test/sub git https://github.com/edv1n/go-get-subdirectory-test gopkg/sub">`,
// Package with go-source meta tag, where {file} appears on the right of '#' in the file field URL template.
"https://azul3d.org/examples/abs": `<!DOCTYPE html><html><head>` +
`<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>` +
Expand Down Expand Up @@ -534,27 +570,27 @@ func TestMatchLegacyTemplates(t *testing.T) {
wantTransformCommitNil bool
}{
{
sm: sourceMeta{"", "", "", "https://git.blindage.org/21h/hcloud-dns/src/branch/master{/dir}/{file}#L{line}"},
sm: sourceMeta{"", "", "", "", "https://git.blindage.org/21h/hcloud-dns/src/branch/master{/dir}/{file}#L{line}"},
wantTemplates: giteaURLTemplates,
wantTransformCommitNil: false,
},
{
sm: sourceMeta{"", "", "", "https://git.lastassault.de/sup/networkoverlap/-/blob/master{/dir}/{file}#L{line}"},
sm: sourceMeta{"", "", "", "", "https://git.lastassault.de/sup/networkoverlap/-/blob/master{/dir}/{file}#L{line}"},
wantTemplates: gitlabURLTemplates,
wantTransformCommitNil: true,
},
{
sm: sourceMeta{"", "", "", "https://git.borago.de/Marco/gqltest/src/master{/dir}/{file}#L{line}"},
sm: sourceMeta{"", "", "", "", "https://git.borago.de/Marco/gqltest/src/master{/dir}/{file}#L{line}"},
wantTemplates: giteaURLTemplates,
wantTransformCommitNil: true,
},
{
sm: sourceMeta{"", "", "", "https://git.zx2c4.com/wireguard-windows/tree{/dir}/{file}#n{line}"},
sm: sourceMeta{"", "", "", "", "https://git.zx2c4.com/wireguard-windows/tree{/dir}/{file}#n{line}"},
wantTemplates: fdioURLTemplates,
wantTransformCommitNil: false,
},
{
sm: sourceMeta{"", "", "unknown{/dir}", "unknown{/dir}/{file}#L{line}"},
sm: sourceMeta{"", "", "", "unknown{/dir}", "unknown{/dir}/{file}#L{line}"},
wantTemplates: urlTemplates{
Repo: "",
Directory: "unknown/{dir}",
Expand Down
27 changes: 27 additions & 0 deletions internal/tests/source/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,33 @@ func TestModuleInfo(t *testing.T) {
"https://blitiri.com.ar/git/r/log/b/master/t/f=go.mod.html#line-1",
"",
},
{
"slatedb.io/slatedb-go match github.com/slatedb/slatedb/slatedb-go/go",
"slatedb.io/slatedb-go", "v0.8.2", "go.mod",
"https://github.com/slatedb/slatedb",
"https://github.com/slatedb/slatedb/tree/slatedb-go/go/v0.8.2/slatedb-go/go",
"https://github.com/slatedb/slatedb/blob/slatedb-go/go/v0.8.2/slatedb-go/go/go.mod",
"https://github.com/slatedb/slatedb/blob/slatedb-go/go/v0.8.2/slatedb-go/go/go.mod#L1",
"https://github.com/slatedb/slatedb/raw/slatedb-go/go/v0.8.2/slatedb-go/go/go.mod",
},
{
"edv1n.github.io/go-get-subdirectory-test match github.com/edv1n/go-get-subdirectory-test with subdir gopkg",
"edv1n.github.io/go-get-subdirectory-test", "v0.0.4", "go.mod",
"https://github.com/edv1n/go-get-subdirectory-test",
"https://github.com/edv1n/go-get-subdirectory-test/tree/gopkg/v0.0.4/gopkg",
"https://github.com/edv1n/go-get-subdirectory-test/blob/gopkg/v0.0.4/gopkg/go.mod",
"https://github.com/edv1n/go-get-subdirectory-test/blob/gopkg/v0.0.4/gopkg/go.mod#L1",
"https://github.com/edv1n/go-get-subdirectory-test/raw/gopkg/v0.0.4/gopkg/go.mod",
},
{
"edv1n.github.io/go-get-subdirectory-test/sub match github.com/edv1n/go-get-subdirectory-test with subdir gopkg/sub",
"edv1n.github.io/go-get-subdirectory-test/sub", "v0.0.4", "go.mod",
"https://github.com/edv1n/go-get-subdirectory-test",
"https://github.com/edv1n/go-get-subdirectory-test/tree/gopkg/sub/v0.0.4/gopkg/sub",
"https://github.com/edv1n/go-get-subdirectory-test/blob/gopkg/sub/v0.0.4/gopkg/sub/go.mod",
"https://github.com/edv1n/go-get-subdirectory-test/blob/gopkg/sub/v0.0.4/gopkg/sub/go.mod#L1",
"https://github.com/edv1n/go-get-subdirectory-test/raw/gopkg/sub/v0.0.4/gopkg/sub/go.mod",
},
} {
t.Run(test.desc, func(t *testing.T) {
info, err := source.ModuleInfo(context.Background(), source.NewClient(client), test.modulePath, test.version)
Expand Down
Loading