]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
source: Expose Ancestor in GitInfo
authorJens Broekens <jens.broekens@protonmail.com>
Wed, 26 Mar 2025 19:12:48 +0000 (20:12 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 7 Jul 2025 12:49:27 +0000 (14:49 +0200)
Also updates docs and bumps bep/gitmap to v1.7.0

Closes #5693

Co-authored-by: bep <bjorn.erik.pedersen@gmail.com>
docs/content/en/methods/page/GitInfo.md
go.mod
go.sum
source/fileInfo.go
tpl/page/page_integration_test.go

index 5fde05b07c60582f385e160b42571fcb3a56f09b..8896823900889bf2b7de25d6c6a7ea65ed282b57 100644 (file)
@@ -117,6 +117,22 @@ hugo --enableGitInfo
 {{ end }}
 ```
 
+###### Ancestor
+
+(`*source.GitInfo`) The file-filtered ancestor commit, if any.
+
+```go-html-template
+{{ partial "inline/changelog.html" .GitInfo }} → 2023-10-09: Add tutorials
+                                                 2025-03-26: Edit GitInfo docs
+
+{{ define "_partials/inline/changelog.html" }}
+  {{ with . }}
+    {{ partial "inline/changelog.html" .Ancestor }}
+    {{ .CommitDate.Format "2006-01-02" }}: {{ .Subject }}<br>
+  {{ end }}
+{{ end }}
+```
+
 ## Last modified date
 
 By default, when `enableGitInfo` is `true`, the `Lastmod` method on a `Page` object returns the Git AuthorDate of the last commit that included the file.
diff --git a/go.mod b/go.mod
index 8ff323985de0709ab861dcf4cf4cc412301946a3..945dd5964f2f29bbf6ba78c01137cfa5fcf0600d 100644 (file)
--- a/go.mod
+++ b/go.mod
@@ -8,7 +8,7 @@ require (
        github.com/aws/aws-sdk-go-v2/service/cloudfront v1.44.10
        github.com/bep/clocks v0.5.0
        github.com/bep/debounce v1.2.0
-       github.com/bep/gitmap v1.6.0
+       github.com/bep/gitmap v1.7.0
        github.com/bep/goat v0.5.0
        github.com/bep/godartsass/v2 v2.5.0
        github.com/bep/golibsass v1.2.0
diff --git a/go.sum b/go.sum
index bb016326c90056295689599be5b590f30ff57dc3..3273b4f910509dde0eafc478ce39e1fa6fbe530f 100644 (file)
--- a/go.sum
+++ b/go.sum
@@ -141,8 +141,8 @@ github.com/bep/clocks v0.5.0 h1:hhvKVGLPQWRVsBP/UB7ErrHYIO42gINVbvqxvYTPVps=
 github.com/bep/clocks v0.5.0/go.mod h1:SUq3q+OOq41y2lRQqH5fsOoxN8GbxSiT6jvoVVLCVhU=
 github.com/bep/debounce v1.2.0 h1:wXds8Kq8qRfwAOpAxHrJDbCXgC5aHSzgQb/0gKsHQqo=
 github.com/bep/debounce v1.2.0/go.mod h1:H8yggRPQKLUhUoqrJC1bO2xNya7vanpDl7xR3ISbCJ0=
-github.com/bep/gitmap v1.6.0 h1:sDuQMm9HoTL0LtlrfxjbjgAg2wHQd4nkMup2FInYzhA=
-github.com/bep/gitmap v1.6.0/go.mod h1:n+3W1f/rot2hynsqEGxGMErPRgT41n9CkGuzPvz9cIw=
+github.com/bep/gitmap v1.7.0 h1:jvPnRQv5RG6IDPrwoDiwAhTE/DmdEkOW4poFeUYmjI8=
+github.com/bep/gitmap v1.7.0/go.mod h1:n+3W1f/rot2hynsqEGxGMErPRgT41n9CkGuzPvz9cIw=
 github.com/bep/goat v0.5.0 h1:S8jLXHCVy/EHIoCY+btKkmcxcXFd34a0Q63/0D4TKeA=
 github.com/bep/goat v0.5.0/go.mod h1:Md9x7gRxiWKs85yHlVTvHQw9rg86Bm+Y4SuYE8CTH7c=
 github.com/bep/godartsass/v2 v2.5.0 h1:tKRvwVdyjCIr48qgtLa4gHEdtRkPF8H1OeEhJAEv7xg=
index dfa5cda26af665cc55f7142b5b2efd0bb372a43e..7eff6fa23341a35588d5a2ac57079b2ff63a05c1 100644 (file)
@@ -155,7 +155,23 @@ func NewFileInfo(fi hugofs.FileMetaInfo) *File {
 }
 
 func NewGitInfo(info gitmap.GitInfo) GitInfo {
-       return GitInfo(info)
+       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.
@@ -176,10 +192,12 @@ type GitInfo struct {
        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.Hash == ""
+func (g *GitInfo) IsZero() bool {
+       return g == nil || g.Hash == ""
 }
index 0f48ecd28e47e2fec44e914d3fc2d842b192f319..623501f25decca45abc84452048b1e6a9dc26ed1 100644 (file)
@@ -220,3 +220,23 @@ disableLiveReload = true
 
        b.AssertFileContent("public/index.html", "1\n2\n3")
 }
+
+func TestThatPageGitInfoShouldBeZero(t *testing.T) {
+       t.Parallel()
+
+       files := `
+-- hugo.toml --
+disableKinds = ["taxonomy", "term"]
+-- content/p1.md --
+---
+title: "P1"
+---
+-- layouts/all.html --
+GitInfo: {{ with .GitInfo }}FAIL{{ end }}
+
+`
+
+       b := hugolib.Test(t, files)
+
+       b.AssertFileContent("public/p1/index.html", "! FAIL")
+}