twpayne / chezmoi

Manage your dotfiles across multiple diverse machines, securely.
https://www.chezmoi.io/
MIT License
12.9k stars 478 forks source link

Variables supported by commitMessageTemplate #3321

Closed lgersman closed 10 months ago

lgersman commented 10 months ago

Is your feature request related to a problem? Please describe.

At first : I love chezmoi since I first used it - it's a great piece of software !

I would like to use the commitMessageTemplate feature of chezmoi to have the machine name (or whatever available as environment variable) in the commit message. This is useful to detect on which machine what commit was created.

I could not find any hint if this is possible right know.

Describe the solution you'd like

Support the environment variable references in commitMessageTemplate

twpayne commented 10 months ago

Thanks for the kind words!

You can use the env template function in your git.commitMessageTemplate string, for example:

[git]
    commitMessageTemplate = "commit with HOME={{ env \"HOME\" }}"

I'll add some functionality to improve the configurability of the commit message.

lgersman commented 10 months ago

Thank you for your fast response!

Please consider updating the docs (at least with an example git.commitMessageTemplate using the Sprig env function or a reference to the supported Sprig features). I was not aware that I can already use env variables in git.commitMessageTemplate :+1:

One more question : I am not (yet) a Go developer, but I have seen in the Sprig documentation that there exists a getHostByName template function.

Can I use this function also in git.commitMessageTemplate to automatically inject the host name in the commit message (thats actually my use case) ?

Like so ?

[git]
    commitMessageTemplate = "commit from host {{ getHostByName \"127.0.0.1\" }}"
twpayne commented 10 months ago

Please consider updating the docs (at least with an example git.commitMessageTemplate using the Sprig env function or a reference to the supported Sprig features). I was not aware that I can already use env variables in git.commitMessageTemplate 👍

The documentation states that git.commitMessageTemplate is a template. There is no indication that this template is any different from any other template in chezmoi, so I don't think such an example is necessary [^1].

[^1]: In fact, commit message templates were a bit special, but #3322 makes them much more like normal templates in chezmoi.

One more question : I am not (yet) a Go developer, but I have seen in the Sprig documentation that there exists a getHostByName template function.

Can I use this function also in git.commitMessageTemplate to automatically inject the host name in the commit message (thats actually my use case) ?

Yes, you can, but once #3322 is merged and a new version of chezmoi is released, I would strongly recommend using {{ .chezmoi.hostname }} or {{ .chezmoi.fqdnHostname }} instead, since these variables return the correct value across a wider range of machines.

lgersman commented 10 months ago

Thanks for your explanation!

Can't wait to get the new chezmoi release with #3322 being merged 👌

twpayne commented 10 months ago

3322 is now merged, so I'll close this. The next chezmoi release should be within two weeks. Thanks again for the discussion and ideas :)