Add git commit date as last update date in hugo page
.GitInfo or .Page.Lastmod as last update date in hugo pageUse .GitInfo or .Page.Lastmod
Hugo have build-in support to get the last Git revision information for every content file through Page.Lastmod or .GitInfo variable.
Page.Lastmod is fetched from .GitInfo.AuthorDate.
To enable Hugo get git commit info,
first need change enableGitInfo to true in config.toml:
# config.toml
enableGitInfo = true
Then in any page template can use .GitInfo or .Page.Lastmod to get current file’s the latest git commit info.
Last Update:
<time datetime="{{ .Page.Lastmod.Format "Mon Jan 10 17:13:38 2020 -0700" }}" class="text-muted">
{{ $.Page.Lastmod.Format "January 02, 2006" }}
</time>
Troubleshooting
.GitInfo is null in local hugo serve
You need commit file to make .GitInfo have validate data.
Per hugo document:
The .GitInfo feature must be enabled in your Hugo project by passing –enableGitInfo flag on the command line or by setting enableGitInfo to true in your site’s configuration file.
This is not accurate, setting enableGitInfo to true in your site’s configuration file is not enough. If you run a dev server by hugo serve, the .GitInfo also is null.
You have to add --enableGitInfo to get .GitInfo:
$ hugo serve --enableGitInfo
Check the content of .GitInfo
You may use jsonify to serialize .GitInfo and display it on page:
{{ .GitInfo | jsonify }}
Here is an example output of {{ .GitInfo | jsonify }}:
{
"hash":"f9ede6c53eed4753abe9f4ad5303a5c0fffc7b5a",
"abbreviatedHash":"f9ede6c",
"subject":"show last update through git commit info",
"authorName":"...",
"authorEmail":"...",
"authorDate":"2020-02-13T15:01:40-08:00",
"commitDate":"2020-02-13T15:01:40-08:00"
}
Example usage of .GitInfo:
{{- if .GitInfo }}
<p style="font-size: small; color: grey;">Last commit: {{ .GitInfo.AbbreviatedHash }} {{.GitInfo.CommitDate}}</p>
{{- end }}
or use with:
{{ with .GitInfo }}
<p style="font-size: small; color: grey;">Page version: {{ .AbbreviatedHash }} {{ .CommitDate.Format "2006-01-02"}}</p>
{{- end }}
Note
Have to check null of .GitInfo use if or with, because newly created page donot have git info yet. Otherwise will get following error:
executing "main" at <.GitInfo.AbbreviatedHash>: nil pointer evaluating *gitmap.GitInfo.AbbreviatedHash
Related pages:
- Demystifying Hugo Shortcodes: Your Ultimate Guide
- Hugo: How to escape shortcode in markdown
- Add comments to Static blog like Hugo, Jekyll with Isso
- Tips on Hugo SEO
- Custom hugo RSS template
- Hugo troubleshooting: execute of template failed: can't give argument to non-function
References
OmniLock - Block / Hide App on iOS
Block distractive apps from appearing on the Home Screen and App Library, enhance your focus and reduce screen time.
DNS Firewall for iOS and Mac OS
Encrypted your DNS to protect your privacy and firewall to block phishing, malicious domains, block ads in all browsers and apps