Skip to content

Commit

Permalink
feat: Make commit message templates regular templates
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Nov 6, 2023
1 parent dbc9054 commit c6688d2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
4 changes: 4 additions & 0 deletions assets/templates/COMMIT_MESSAGE.tmpl
@@ -1,3 +1,5 @@
{{- with .chezmoi.status }}

{{- range .Ordinary -}}
{{ if and (eq .X 'A') (eq .Y '.') -}}Add {{ .Path | targetRelPath }}
{{ else if and (eq .X 'D') (eq .Y '.') -}}Remove {{ .Path | targetRelPath }}
Expand All @@ -19,3 +21,5 @@
{{- range .Untracked -}}
{{ fail "untracked files" }}
{{- end -}}

{{- end -}}
14 changes: 10 additions & 4 deletions internal/cmd/config.go
Expand Up @@ -1448,11 +1448,11 @@ func (c *Config) gitAutoAdd() (*git.Status, error) {

// gitAutoCommit commits all changes in the git index, including generating a
// commit message from status.
func (c *Config) gitAutoCommit(status *git.Status) error {
func (c *Config) gitAutoCommit(cmd *cobra.Command, status *git.Status) error {
if status.Empty() {
return nil
}
funcMap := maps.Clone(sprig.TxtFuncMap())
funcMap := maps.Clone(c.templateFuncs)
maps.Copy(funcMap, map[string]any{
"promptBool": c.promptBoolInteractiveTemplateFunc,
"promptChoice": c.promptChoiceInteractiveTemplateFunc,
Expand Down Expand Up @@ -1498,7 +1498,13 @@ func (c *Config) gitAutoCommit(status *git.Status) error {
if err != nil {
return err
}
commitMessage, err := commitMessageTmpl.Execute(status)
sourceState, err := c.getSourceState(cmd.Context(), cmd)
if err != nil {
return err
}
templateDataMap := sourceState.TemplateData()
templateDataMap["chezmoi"].(map[string]any)["status"] = status //nolint:forcetypeassert
commitMessage, err := commitMessageTmpl.Execute(templateDataMap)
if err != nil {
return err
}
Expand Down Expand Up @@ -1803,7 +1809,7 @@ func (c *Config) persistentPostRunRootE(cmd *cobra.Command, args []string) error
}
}
if c.Git.AutoCommit || c.Git.AutoPush {
if err := c.gitAutoCommit(status); err != nil {
if err := c.gitAutoCommit(cmd, status); err != nil {
return err
}
}
Expand Down
10 changes: 6 additions & 4 deletions internal/cmd/testdata/scripts/autocommit.txtar
Expand Up @@ -37,7 +37,7 @@ exec git --git-dir=$CHEZMOISOURCEDIR/.git show HEAD
stdout 'Remove \.file'

# test that chezmoi edit uses a custom commit message template
appendline $CHEZMOICONFIGDIR/chezmoi.toml ' commitMessageTemplate = "feat: my commit message"'
appendline $CHEZMOICONFIGDIR/chezmoi.toml ' commitMessageTemplate = "{{ .prefix }}my commit message"'
exec chezmoi edit $HOME${/}.dir${/}file
exec git --git-dir=$CHEZMOISOURCEDIR/.git show HEAD
stdout 'feat: my commit message'
Expand All @@ -46,16 +46,18 @@ stdout 'feat: my commit message'
appendline $CHEZMOICONFIGDIR/chezmoi.toml ' commitMessageTemplateFile = ".COMMIT_MESSAGE.tmpl"'
! exec chezmoi edit $HOME${/}.dir${/}file
stderr 'cannot specify both git.commitMessageTemplate and git.commitMessageTemplateFile'
removeline $CHEZMOICONFIGDIR/chezmoi.toml ' commitMessageTemplate = "feat: my commit message"'
removeline $CHEZMOICONFIGDIR/chezmoi.toml ' commitMessageTemplate = "{{ .prefix }}my commit message"'

# test that chezmoi edit uses a custom commit message template file
exec chezmoi edit $HOME${/}.dir${/}file
exec git --git-dir=$CHEZMOISOURCEDIR/.git show HEAD
stdout 'my commit message file'
stdout 'feat: my commit message file'
removeline $CHEZMOICONFIGDIR/chezmoi.toml ' commitMessageTemplateFile = ".COMMIT_MESSAGE.tmpl"'

-- home/user/.config/chezmoi/chezmoi.toml --
[data]
prefix = "feat: "
[git]
autoCommit = true
-- home/user/.local/share/chezmoi/.COMMIT_MESSAGE.tmpl --
my commit message file
{{ .prefix }}my commit message file

0 comments on commit c6688d2

Please sign in to comment.