gohugoio / hugo

The world’s fastest framework for building websites.
https://gohugo.io
Apache License 2.0
74.4k stars 7.43k forks source link

Allow access to git config (user.name and user.email) during new content creation from archetype #10902

Open imomaliev opened 1 year ago

imomaliev commented 1 year ago

I would like to have the ability to use .GitInfo in archetype templates to provide information about the author. It seems like .GitInfo is directly bound to the content file's git metadata, so this may not be a good candidate. But maybe there is another way to autopopulate information about author by using current git user. This will allow us to use author: {{ .GitInfo.AuthorName }} in archetype template frontmatter. There will be multiple authors for the project so I need information from git, not from config.toml

Example

Add author: {{ .GitInfo.AuthorName }} to archetypes/default.md

---
title: "{{ replace .Name "-" " " | title }}"
date: {{ .Date }}
draft: true
author: {{ .GitInfo.AuthorName }}
---

Current behavior

We get an error.

$ hugo new test.md
Error: failed to execute archetype template: template: archetype.md:5:19: executing "archetype.md" at <.GitInfo.AuthorName>: can't evaluate field GitInfo in type *hugolib.archetypeFileData: template: archetype.md:5:19: executing "archetype.md" at <.GitInfo.AuthorName>: can't evaluate field GitInfo in type *hugolib.archetypeFileData

Expected behavior

New test.md content file is created

---
title: "Test"
date: 2023-04-18T16:15:43+07:00
draft: true
author: "John Doe"
---
jmooring commented 1 year ago

What is the use case? Why do you need to store this value in front matter instead of accessing the value during build?

bep commented 1 year ago

The current .GitInfo is tied to directly to the last Git commit. When you create a new content file there's no commit yet, so I don't see how this would work on the technical level.

imomaliev commented 1 year ago

What is the use case? Why do you need to store this value in front matter instead of accessing the value during build?

We are trying to use Hugo to host ADRs (Architectural Decision Records). It would be useful to take a value of an author from the git on hugo new command call and extract it into the document, it will be imprinted in the document's frontmatter the same way date does.

When you create a new content file there's no commit yet, so I don't see how this would work on the technical level.

Yes, I assumed that and wrote in my original post that .GitInfo may not fit my request because it is bound to the git history of the document and doesn't have access to the general git configuration of the repository.

Maybe there is some other way to provide this info for the archetype template?

jmooring commented 1 year ago

@imomaliev

It would be useful to take a value...

I understand what you want to do, but I am asking why you want to do it instead of extracting .GitInfo.AuthorName during the build?

imomaliev commented 1 year ago

but I am asking why you want to do it instead of extracting .GitInfo.AuthorName during the build?

I want to have author to be viewable from gitlab UI or source as well. If it will be available only after build, we wouldn't be able to just open ADR as markdown file and be able to tell who the author was

jmooring commented 1 year ago

If you do this from the command line:

HUGO_GIT_USER_NAME=$(git config user.name) hugo new something

You can do this in your archetype:

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

Or export the env var from .bashrc, etc.

if git -v &> /dev/null; then
  export HUGO_GIT_USER_NAME=$(git config user.name)
fi
imomaliev commented 1 year ago

@jmooring Thank you. I will try this.