How to set the git config email as archtype frontmatter

I’m using Hugo to build the documentation for a system at work. There will probably be more people contributing to the documentation over time, and I’d love for either

  1. the frontmatter to be automatcally populated with the git config email in author from archtype default, and, if possible
  2. update a changed .md-file with the current comitter’s git config email in author.

Is this possible?

Instead of relying on front matter, you can extract the data from each commit.

In your site configuration:

enableGitInfo = true

In your templates:

{{ with .GitInfo }}
  {{ .AuthorName }}
  {{ .AuthorEmail }}
  {{ .AuthorDate }}
{{ end }}

It is possible to populate a front matter field when creating (not updating) content, but each user would need to set environment variables, something like:

if git -v &> /dev/null; then
    HUGO_GIT_USER_NAME=$(git config user.name)
    HUGO_GIT_USER_EMAIL=$(git config user.email)
    export HUGO_GIT_USER_NAME
    export HUGO_GIT_USER_EMAIL
fi

In the archetype:

user = '{{ os.Getenv "HUGO_GIT_USER_NAME" }}'
2 Likes