hugolib: Fix Page.GitInfo for modules with go.mod in a repo subdirectory

Map content files using their path relative to the git repo root by
prepending the module's Origin.Subdir to the lookup key.

Fixes #14942
This commit is contained in:
Bjørn Erik Pedersen
2026-05-25 22:50:09 +02:00
parent aeb9a5cc02
commit df5421918a
4 changed files with 51 additions and 8 deletions
+7
View File
@@ -80,6 +80,13 @@ func (g *gitInfo) gitInfoFromModuleRepo(p page.Page, mod modules.Module, repo *g
filePath := strings.TrimPrefix(filename, modDir)
filePath = strings.TrimPrefix(filePath, "/")
// If the module's go.mod lives in a subdirectory of the git repo,
// the blobless clone keys files by their path relative to the repo root,
// so we need to prepend the subdir.
if subdir := strings.Trim(mod.Origin().Subdir, "/"); subdir != "" {
filePath = subdir + "/" + filePath
}
gi, found := repo.Files[filePath]
if !found {
return nil
+33
View File
@@ -95,3 +95,36 @@ module hugotest
"AuthorName: Bjørn Erik Pedersen|",
)
}
// See issue 14942.
func TestGitInfoFromGitModuleWithGoModInSubdir(t *testing.T) {
t.Parallel()
files := `
-- hugo.toml --
baseURL = "https://example.com/"
enableGitInfo = true
[module]
[[module.imports]]
path = "github.com/bep/hugo-mod-misc/dummy-content"
version = "v0.3.0"
[[module.imports.mounts]]
source = "content"
target = "content"
-- layouts/page.html --
Title: {{ .Title }}|
GitInfo: {{ with .GitInfo }}Hash: {{ .Hash }}|Subject: {{ .Subject }}|AuthorName: {{ .AuthorName }}{{ end }}|
-- layouts/_default/list.html --
List: {{ .Title }}
-- go.mod --
module hugotest
`
b := Test(t, files, TestOptOsFs())
b.AssertFileContent("public/blog/music/satin-doll/index.html",
"Hash: 522ee6c486ff24b2e3d45e9b90061564e9427030|",
"AuthorName: Bjørn Erik Pedersen|",
)
}
+1
View File
@@ -991,6 +991,7 @@ type goModule struct {
type goModuleOrigin struct {
VCS string // version control system, e.g. "git"
URL string // repository URL, e.g. "https://github.com/bep/hugo-testing-git-versions"
Subdir string // subdirectory within the repo where the module lives, e.g. "site"
Hash string // commit hash
TagSum string
Ref string // e.g. "refs/tags/v3.0.1"
+10 -8
View File
@@ -86,10 +86,11 @@ type Module interface {
// ModuleOrigin contains origin info for a Go module.
type ModuleOrigin struct {
VCS string // version control system, e.g. "git"
URL string // repository URL, e.g. "https://github.com/bep/hugo-testing-git-versions"
Hash string // commit hash
Ref string // e.g. "refs/tags/v3.0.1"
VCS string // version control system, e.g. "git"
URL string // repository URL, e.g. "https://github.com/bep/hugo-testing-git-versions"
Subdir string // subdirectory within the repo where the module lives, e.g. "site"
Hash string // commit hash
Ref string // e.g. "refs/tags/v3.0.1"
}
func (o ModuleOrigin) IsZero() bool {
@@ -240,9 +241,10 @@ func (m *moduleAdapter) Origin() ModuleOrigin {
return ModuleOrigin{}
}
return ModuleOrigin{
VCS: m.gomod.Origin.VCS,
URL: m.gomod.Origin.URL,
Hash: m.gomod.Origin.Hash,
Ref: m.gomod.Origin.Ref,
VCS: m.gomod.Origin.VCS,
URL: m.gomod.Origin.URL,
Subdir: m.gomod.Origin.Subdir,
Hash: m.gomod.Origin.Hash,
Ref: m.gomod.Origin.Ref,
}
}