From: Bjørn Erik Pedersen Date: Wed, 25 Feb 2026 13:26:50 +0000 (+0100) Subject: Handle GitInfo for modules where Origin is not set when running go list X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=d98cd4aecf25b9df78d811759ea6135b0c7610f1;p=brevno-suite%2Fhugo Handle GitInfo for modules where Origin is not set when running go list This is an upstream bug, but this workaround seem to work fine. Fixes #14564 --- diff --git a/hugolib/gitinfo.go b/hugolib/gitinfo.go index 07832fbdc..013b8e666 100644 --- a/hugolib/gitinfo.go +++ b/hugolib/gitinfo.go @@ -114,7 +114,9 @@ func newGitInfo(cfg gitInfoConfig) (*gitInfo, error) { continue } hasGitModules = true - if mod.Owner() == nil { + if mod.Owner() == nil && !mod.Origin().IsZero() { + // TODO(bep) I'm not sure if this will ever be true. Needs some investigation, + // as I'm sure there will be cases where this could be useful. projectIsGitModule = true } } @@ -133,6 +135,9 @@ func newGitInfo(cfg gitInfoConfig) (*gitInfo, error) { gitRepo, err := mapLocalRepo(cfg) if err != nil { if hasGitModules { + // We added GitInfo module support in Hugo v0.157.0 and need some real world experience, + // but for now, don't fail if the local repo is not a Git repo, but there are Git modules. + // I'm not sure we should even warn, but let's do that for now. cfg.Logger.Warnf("Failed to read local Git log: %v", err) return g, nil } diff --git a/hugolib/gitinfo_github_test.go b/hugolib/gitinfo_github_test.go index 49e2a0f84..39c9ea42f 100644 --- a/hugolib/gitinfo_github_test.go +++ b/hugolib/gitinfo_github_test.go @@ -17,7 +17,7 @@ import ( "testing" ) -func TestGitInfoFromGitModule(t *testing.T) { +func TestGitInfoFromGitModuleWithVersionQuery(t *testing.T) { t.Parallel() files := ` @@ -63,3 +63,35 @@ List: {{ .Title }} "AuthorName: Bjørn Erik Pedersen|", ) } + +func TestGitInfoFromGitModuleWithGoMod(t *testing.T) { + t.Parallel() + + files := ` +-- hugo.toml -- +baseURL = "https://example.com/" +enableGitInfo = true + +[module] +[[module.imports]] +path = "github.com/bep/hugo-mod-testing-content" +[[module.imports.mounts]] +source = "content" +target = "content" +-- layouts/page.html -- +Title: {{ .Title }}| +GitInfo: {{ with .GitInfo }}Hash: {{ .Hash }}|Subject: {{ .Subject }}|AuthorName: {{ .AuthorName }}{{ end }}| +Content: {{ .Content }}| +-- layouts/_default/list.html -- +List: {{ .Title }} +-- go.mod -- +module hugotest +` + + b := Test(t, files, TestOptOsFs()) + + b.AssertFileContent("public/docs/functions/kurtosis/index.html", + "Hash: 668663b54d0937df05185d144765d13c3ffda489|", + "AuthorName: Bjørn Erik Pedersen|", + ) +} diff --git a/hugolib/hugo_sites.go b/hugolib/hugo_sites.go index 8b2eb4636..694dd7051 100644 --- a/hugolib/hugo_sites.go +++ b/hugolib/hugo_sites.go @@ -554,7 +554,7 @@ func (h *HugoSites) loadGitInfo() error { } gi, err := newGitInfo(cfg) if err != nil { - h.Log.Errorln("Failed to read Git log:", err) + return err } else { h.gitInfo = gi } diff --git a/modules/client.go b/modules/client.go index 2c5faf414..d28aace7f 100644 --- a/modules/client.go +++ b/modules/client.go @@ -564,6 +564,26 @@ func (c *Client) listGoMods() (goModules, error) { if m.Dir == "" { modulesToDownload = append(modulesToDownload, fmt.Sprintf("%s@%s", m.Path, m.Version)) } + + // See https://github.com/golang/go/issues/67363 + // Origin isn't always set. + if m.Origin == nil && m.GoMod != "" { + // There's sometimes an Info field with a JSON filename with this info, but that is also not always set. + // But we seem to always get the go.mod filename, so we can determine the info filename from that, + // just replace the .mod suffix with .info. + infoFilename := strings.TrimSuffix(m.GoMod, ".mod") + ".info" + // JSON on the form {"Version":"v0.0.0-20260225095909-668663b54d09","Time":"2026-02-25T09:59:09Z","Origin":{"VCS":"git","URL":"https://github.com/bep/hugo-mod-testing-content","Hash":"668663b54d0937df05185d144765d13c3ffda489"}} + if b, err := afero.ReadFile(c.fs, infoFilename); err == nil { + var info struct { + Version string + Time time.Time + Origin *goModuleOrigin + } + if err := json.Unmarshal(b, &info); err == nil { + m.Origin = info.Origin + } + } + } } if len(modulesToDownload) > 0 { diff --git a/modules/module.go b/modules/module.go index 89219cefb..993e76286 100644 --- a/modules/module.go +++ b/modules/module.go @@ -92,6 +92,10 @@ type ModuleOrigin struct { Ref string // e.g. "refs/tags/v3.0.1" } +func (o ModuleOrigin) IsZero() bool { + return o.URL == "" +} + type Modules []Module type moduleAdapter struct {