]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
Handle GitInfo for modules where Origin is not set when running go list
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Wed, 25 Feb 2026 13:26:50 +0000 (14:26 +0100)
committerGitHub <noreply@github.com>
Wed, 25 Feb 2026 13:26:50 +0000 (14:26 +0100)
This is an upstream bug, but this workaround seem to work fine.

Fixes #14564

hugolib/gitinfo.go
hugolib/gitinfo_github_test.go
hugolib/hugo_sites.go
modules/client.go
modules/module.go

index 07832fbdcf067d7647f66b6e83f9e2add1df9a67..013b8e66641b46f86f42e62b949bc6ffcd5b6aa5 100644 (file)
@@ -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
                }
index 49e2a0f840b2f0c1872852eeec30e8ed210072be..39c9ea42f749ea4b964557364245155e6532a2d9 100644 (file)
@@ -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|",
+       )
+}
index 8b2eb46360f9b11cd5861edd262302f539cb646a..694dd705176aa021775a5e193498818e01684d92 100644 (file)
@@ -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
                }
index 2c5faf41486095a4c04953f74f675bc9b3a6004c..d28aace7fe800f9b854296dbbf8bd4dce9877b8f 100644 (file)
@@ -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 {
index 89219cefb876273454c6c260f19116efc7d2896b..993e76286e1e7d0a4e0fdb40a8f205cdc8bdbf00 100644 (file)
@@ -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 {