From 90d397b14299b1cb03a6b3a3e9e1ce6dfc36cdad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Mon, 7 Jul 2025 13:03:57 +0200 Subject: [PATCH] Remove the internal GitInfo type and make Page.GitInf() return a pointer See #5693 --- hugolib/gitinfo.go | 6 ++-- hugolib/hugo_sites.go | 6 ++-- hugolib/page.go | 2 +- hugolib/page__common.go | 2 +- hugolib/page__meta.go | 2 +- resources/page/page.go | 2 +- resources/page/page_nop.go | 4 +-- resources/page/testhelpers_test.go | 4 +-- source/fileInfo.go | 48 +----------------------------- tpl/page/page_integration_test.go | 2 +- 10 files changed, 16 insertions(+), 62 deletions(-) diff --git a/hugolib/gitinfo.go b/hugolib/gitinfo.go index 6b5261084..4786a9509 100644 --- a/hugolib/gitinfo.go +++ b/hugolib/gitinfo.go @@ -30,14 +30,14 @@ type gitInfo struct { repo *gitmap.GitRepo } -func (g *gitInfo) forPage(p page.Page) source.GitInfo { +func (g *gitInfo) forPage(p page.Page) *source.GitInfo { name := strings.TrimPrefix(filepath.ToSlash(p.File().Filename()), g.contentDir) name = strings.TrimPrefix(name, "/") gi, found := g.repo.Files[name] if !found { - return source.GitInfo{} + return nil } - return source.NewGitInfo(*gi) + return gi } func newGitInfo(d *deps.Deps) (*gitInfo, error) { diff --git a/hugolib/hugo_sites.go b/hugolib/hugo_sites.go index 0b68af2ec..a79a77d36 100644 --- a/hugolib/hugo_sites.go +++ b/hugolib/hugo_sites.go @@ -230,13 +230,13 @@ func (h *HugoSites) RegularPages() page.Pages { return v } -func (h *HugoSites) gitInfoForPage(p page.Page) (source.GitInfo, error) { +func (h *HugoSites) gitInfoForPage(p page.Page) (*source.GitInfo, error) { if _, err := h.init.gitInfo.Do(context.Background()); err != nil { - return source.GitInfo{}, err + return nil, err } if h.gitInfo == nil { - return source.GitInfo{}, nil + return nil, nil } return h.gitInfo.forPage(p), nil diff --git a/hugolib/page.go b/hugolib/page.go index dc0d9f4bc..5387c021d 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -238,7 +238,7 @@ func (p *pageState) ApplyFilterToHeadings(ctx context.Context, fn func(*tableofc } } -func (p *pageState) GitInfo() source.GitInfo { +func (p *pageState) GitInfo() *source.GitInfo { return p.gitInfo } diff --git a/hugolib/page__common.go b/hugolib/page__common.go index f6f01bbe2..0b9cf0200 100644 --- a/hugolib/page__common.go +++ b/hugolib/page__common.go @@ -86,7 +86,7 @@ type pageCommon struct { targetPathDescriptor page.TargetPathDescriptor // Set if feature enabled and this is in a Git repo. - gitInfo source.GitInfo + gitInfo *source.GitInfo codeowners []string // Positional navigation diff --git a/hugolib/page__meta.go b/hugolib/page__meta.go index 1af489f18..3c694ab41 100644 --- a/hugolib/page__meta.go +++ b/hugolib/page__meta.go @@ -405,7 +405,7 @@ func (p *pageState) setMetaPostParams() error { } var gitAuthorDate time.Time - if !p.gitInfo.IsZero() { + if p.gitInfo != nil { gitAuthorDate = p.gitInfo.AuthorDate } diff --git a/resources/page/page.go b/resources/page/page.go index cbcfad557..490afd890 100644 --- a/resources/page/page.go +++ b/resources/page/page.go @@ -135,7 +135,7 @@ type GetPageProvider interface { // GitInfoProvider provides Git info. type GitInfoProvider interface { // GitInfo returns the Git info for this object. - GitInfo() source.GitInfo + GitInfo() *source.GitInfo // CodeOwners returns the code owners for this object. CodeOwners() []string } diff --git a/resources/page/page_nop.go b/resources/page/page_nop.go index 398a7df02..0fc9628c9 100644 --- a/resources/page/page_nop.go +++ b/resources/page/page_nop.go @@ -178,8 +178,8 @@ func (p *nopPage) GetTerms(taxonomy string) Pages { return nil } -func (p *nopPage) GitInfo() source.GitInfo { - return source.GitInfo{} +func (p *nopPage) GitInfo() *source.GitInfo { + return nil } func (p *nopPage) CodeOwners() []string { diff --git a/resources/page/testhelpers_test.go b/resources/page/testhelpers_test.go index 1d2ee6223..5e1ec9a58 100644 --- a/resources/page/testhelpers_test.go +++ b/resources/page/testhelpers_test.go @@ -225,8 +225,8 @@ func (p *testPage) GetInternalRelatedDocsHandler() *RelatedDocsHandler { return relatedDocsHandler } -func (p *testPage) GitInfo() source.GitInfo { - return source.GitInfo{} +func (p *testPage) GitInfo() *source.GitInfo { + return nil } func (p *testPage) CodeOwners() []string { diff --git a/source/fileInfo.go b/source/fileInfo.go index 7eff6fa23..fff78e2fe 100644 --- a/source/fileInfo.go +++ b/source/fileInfo.go @@ -16,7 +16,6 @@ package source import ( "path/filepath" "sync" - "time" "github.com/bep/gitmap" "github.com/gohugoio/hugo/common/hashing" @@ -154,50 +153,5 @@ func NewFileInfo(fi hugofs.FileMetaInfo) *File { } } -func NewGitInfo(info gitmap.GitInfo) GitInfo { - gi := GitInfo{ - Hash: info.Hash, - AbbreviatedHash: info.AbbreviatedHash, - Subject: info.Subject, - AuthorName: info.AuthorName, - AuthorEmail: info.AuthorEmail, - AuthorDate: info.AuthorDate, - CommitDate: info.CommitDate, - Body: info.Body, - } - - if info.Ancestor != nil { - anc := NewGitInfo(*info.Ancestor) - gi.Ancestor = &anc - } - - return gi -} - // GitInfo provides information about a version controlled source file. -type GitInfo struct { - // Commit hash. - Hash string `json:"hash"` - // Abbreviated commit hash. - AbbreviatedHash string `json:"abbreviatedHash"` - // The commit message's subject/title line. - Subject string `json:"subject"` - // The author name, respecting .mailmap. - AuthorName string `json:"authorName"` - // The author email address, respecting .mailmap. - AuthorEmail string `json:"authorEmail"` - // The author date. - AuthorDate time.Time `json:"authorDate"` - // The commit date. - CommitDate time.Time `json:"commitDate"` - // The commit message's body. - Body string `json:"body"` - // The file-filtered ancestor commit, if any. - Ancestor *GitInfo `json:"ancestor"` -} - -// IsZero returns true if the GitInfo is empty, -// meaning it will also be falsy in the Go templates. -func (g *GitInfo) IsZero() bool { - return g == nil || g.Hash == "" -} +type GitInfo = gitmap.GitInfo diff --git a/tpl/page/page_integration_test.go b/tpl/page/page_integration_test.go index 623501f25..f96c87f98 100644 --- a/tpl/page/page_integration_test.go +++ b/tpl/page/page_integration_test.go @@ -221,7 +221,7 @@ disableLiveReload = true b.AssertFileContent("public/index.html", "1\n2\n3") } -func TestThatPageGitInfoShouldBeZero(t *testing.T) { +func TestThatPageGitInfoShouldBeNil(t *testing.T) { t.Parallel() files := `