Adding Last Modified Git Status to Pages?

I’m making a few assumptions here, so correct me if I’m barking up the wrong tree.

But I’ve noticed that on some of the Hugo Docs pages there is a “Last modified…” style message at the bottom of the page, giving the date the page was last modified and also what looks like the relevant Git commit message. For example; on the page: https://gohugo.io/getting-started/configuration-markup

at the bottom, we find this:

So, a couple of assumptions/questions:

  1. I’m assuming adding this info to the pages is somehow done automatically and not by hand each time a page is modified. If so:

    • Is this a function built-in to Hugo or hand-rolled? Either way, could someone point me to how it’s done?
  2. Assumption no.2. I’m also thinking access to this feature may be contingent on the Hugo site in question being hosted on Github pages, which I’m assuming [Assumption no.3] the Hugo Docs pages are.

    • If all previous assumptions are true, does that mean a feature like this can only be implement with Github Pages based sites?

See https://gohugo.io/variables/git/.

Example (snippet placed in layouts/_default/single.html):

"{{ .Title }}" was last updated on {{ .Lastmod.Format "January 2, 2006" }}: {{ .GitInfo.Subject }} ({{ .GitInfo.AbbreviatedHash }})

I’m also thinking access to this feature may be contingent on the Hugo site in question being hosted on Github pages

No, this is not required unless you want to provide public access to your commits (as is the case in the example you cited).

Ha! That simple, eh? Thanks!

Done and implemented. Now all I need to ‘memo to self’ is to make sure and write descriptive commit messages all the time, instead of my habitual “fixed some crap” or “deleted some bollox”

1 Like

Dammit. I think I spoke too soon. I’ve run into a weird situation here.

If I add the following to my page:

<p>LAST MODIFIED:{{ .Lastmod.Format "02 Jan 2006" }} </p>

then run hugo server the site builds without any problems. However, if I add a .GitInfo variable:

<p> LAST MODIFIED: {{ .Lastmod.Format "02 Jan 2006" }} &mdash; BECAUSE: "{{.GitInfo.Subject }}"</p>

Then hugo server bails out with this twice repeated error:

Building sites … 
ERROR 2020/05/10 14:46:19 render of "page" failed: "/Users/madra/Sites/stiobhart.net/themes/stiobhart.net/layouts/_default/single.html:43:147": execute of template failed: template: _default/single.html:43:147: executing "_default/single.html" at <.GitInfo.Subject>: nil pointer evaluating *gitmap.GitInfo.Subject
ERROR 2020/05/10 14:46:19 render of "page" failed: "/Users/madra/Sites/stiobhart.net/themes/stiobhart.net/layouts/_default/single.html:43:147": execute of template failed: template: _default/single.html:43:147: executing "_default/single.html" at <.GitInfo.Subject>: nil pointer evaluating *gitmap.GitInfo.Subject

and the site fails to build. Yet, if I use the first option above, then run hugo server so the site builds and only then add the {{.GitInfo.Subject }} part into the template and re-save the template file, Hugo’s live reload reloads whichever page I’m viewing with that {{ .GitInfo.Subject }} data correctly displayed on the page.

So how can Hugo read the content of the {{.GitInfo.Subject }} variable after a live reload but [if I’m interpreting the error correctly] seems to be finding the variable unset when building from scratch?

I have set the enableGitInfo = true option in my site’s config.toml file.

EDIT:

I’m now thinking that this ‘double error’ above maybe actually be 2 single errors relating to two particular pages in my site which [for whatever reason] have no associated Git info and therefore Hugo is bailing out when it tries to render that meta info for those pages. Unfortunately [if that is the case] the error doesn’t pinpoint which pages are affected.

Obvious remedy is to add in a check for the presence of the .GitInfo.Subject variable value, before trying to print it to the page. But, here again, I’m running into probs:

First I tried an isset

{{ if (isset .GitInfo "Subject") }}
 {{.GitInfo.Subject }}
{{end}}

But Hugo doesn’t like that because the .gitmap stuff is a pointer:

WARNING: calling IsSet with unsupported type "ptr" (*gitmap.GitInfo) will always return false.

So I tried again using with instead, which is meant to be the recommended way to handle variables which may not exist

{{with .GitInfo.Subject }}
{{.GitInfo.Subject }}
{{end}}

But that gives me the following error:

ERROR 2020/05/10 15:10:50 render of "page" failed: 
<snip>
can't evaluate field GitInfo in type string

So, how would I go about handling writing a conditional so I only try to display {{.GitInfo.Subject }} if it actually exists for a particular page?

First, when using with, the context (aka “the dot”) is rebound to its scope. For example:

{{- $var := "foo: -}}
{{- with $var -}}
  {{ . }}
{{- end -}}

will emit foo.

See:
https://gohugo.io/templates/introduction/#the-dot
https://gohugo.io/functions/with/

Second, neither with nor isset will do what you need, but this will:

{{- if $.GitInfo -}}
  {{ .GitInfo.Subject }}
{{- end -}}

This was not intuitive. I found it here: https://github.com/gohugoio/hugo/issues/4184.

Finally, at least for me, this is a non-issue if all of my content is under source control. It sounds like some of yours is not.

1 Like

Thanks again. That seems to have done it.

I had also tried wrapping the .GitInfo data in a {{with .GitInfo }} following discussion in this thread but that also gave the same errors as wrapping it in {{with .GitInfo.Subject }}

I’m curious as to the significance of the {{- in your example? I’ve seen and used {{ and {{% in my Hugo templates and shortcodes, but never seen the {{- version before. What does that do?

My site is under source control. My working method is LAPTOP —push—> GITHUB —pull—> SERVER and has been for many years now. So I don’t know how any of the pages couldn’t have version control info attached by now, given the amount of changes I’ve made over the years, since originally porting the site from Tumblr to Hugo, way back in 2014.

I agree, it does seem the obvious explanation though. I just can’t fathom how anything on the site can have ‘slipped through the gaps’ like that, after so many updates, rewrites and general tinkerings. T’is a mystery!

https://golang.org/pkg/text/template/#hdr-Text_and_spaces

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.