.GitInfo returns <nil>

I’ve just discovered Hugo supports Git variables and I thought of adding some into a partial (footer), you can see the tentative code here:

https://github.com/shaftoe/curriculum-vitae/blob/footer/layouts/partials/footer.html

Unfortunately on my macOs v10.14.6 workstation with Hugo v0.63.2 doesn’t work despite all the requirements seems to be satisfied:

  • .git is in the Hugo root folder
  • git executable is definitely in my PATH, I use it all the time
  • feature enabled in config.toml

When I try to access .GitInfo inside a template with {{ .GitInfo.<any var> }} it returns <nil>:

executing "partials/footer.html" at <.GitInfo.AbbreviatedHash>: nil pointer evaluating *gitmap.GitInfo.AbbreviatedHash

Ideas?

Thanks


Alex

2 Likes

It appears that hugo’s git variable, .GitInfo, is only available when generating pages from the ‘./content’ directory. The variable does not seem to be available when generating pages from other places like ./layout (i.e. custom site landing pages ). If your footer template is being evaluated in one of those “non-content” pages, then .GitInfo will be nil.

A possible workaround is to use .Site.GetPage to fetch a normal content page and get the variable from that.


Sam

Thanks! I almost forgot about this.