A way to get last commit date for the whole repo?

I’d love to show on my site the date the site was last updated, and I can’t seem to find a way to do that.

.Site.LastChange only shows the last modification of pages (and contrary to what the doc says, not just any pages, but only in .Site.Params.MainSections it looks like).

.Site.Home.Lastmod seems to give the date of the commit that last touched files immediately in content/, excluding subdirs (is this how it is supposed to work, by the way?)

.Site.Lastmod doesn’t work at all (and neither does .Site.GitInfo, which seems to be a logical place to look for this information).

Is there some place else?

1 Like

May be iterate on ALL your pages and keep the latest date ?

Thought about it, but I want to also reflect non-page changes (i.e. changes to templates, CSS, etc.)

I had a similar need recently (wanted to display the last commit hash for the entire site) and resorted to using an external process to create a data template which my site footer could consume. Rather kludgy, but it works smoothly in absence of a way to get that cleanly within Hugo.

Or use an environment variable.

LAST_COMMIT_DATE=$(git log -1 --format=%cI) hugo server
{{ with (getenv "LAST_COMMIT_DATE") }}
  <p>Last updated: {{ time . | dateFormat "2 Jan 2006" }}</p>
{{ end }}

You could also set it up as a permanent alias:

alias hugo='LAST_COMMIT_DATE=$(git log -1 --format=%cI) hugo'

Lovely, thanks, I didn’t know there was a way to get environment variables in Hugo templates.

Still, it looks kludgy. I wish there was a clean way…

  • .Site.LastChange chould show the most recent non-zero .LastMod. If this isn’t the case, it’s a bug and I will fix it.
  • .Site.Home.LastMod will (if memory serves me right) be the same as Site.LastChange if you have no content file for the home page, else it will get that date.

Yes, it actually does just that. My confusion in original post was due to languages getting in the way.

This is not the behavior I get on my site, but I can’t share the code of that, and I can’t reproduce my issues on a newly created site. I’ll dig in more.

Thank you for clarifications!

See https://github.com/gohugoio/hugo/issues/7310

I will check this today. I’m doing a “mostly bug fix” release in a day or two.

1 Like

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