Skip to content

Commit

Permalink
feat: Add git.commitMessageTemplateFile config variable
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Nov 6, 2023
1 parent 8d58c3e commit f7c3745
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ sections:
commitMessageTemplate:
type: 'string'
description: Commit message template
commitMessageTemplateFile:
type: 'string'
description: Commit message template file (relative to source directory)
gitHub:
refreshPeriod:
type: duration
Expand Down
10 changes: 10 additions & 0 deletions assets/chezmoi.io/docs/user-guide/daily-operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ message each time, use:
commitMessageTemplate = "{{ promptString \"Commit message\" }}"
```

If your commit message is longer than fits in a string then you can set
`git.commitMessageTemplateFile` to specify a path to the commit message template
relative to the source directory, for example:

```toml title="~/.config/chezmoi/chezmoi.toml"
[git]
autoCommit = true
commitMessageTemplateFile = ".commit_message.tmpl"
```

Be careful when using `autoPush`. If your dotfiles repo is public and you
accidentally add a secret in plain text, that secret will be pushed to your
public repo.
Expand Down
43 changes: 36 additions & 7 deletions internal/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,14 @@ func (c *Config) decodeConfigFile(configFileAbsPath chezmoi.AbsPath, configFile
return fmt.Errorf("%s: %w", configFileAbsPath, err)
}

if configFile.Git.CommitMessageTemplate != "" &&
configFile.Git.CommitMessageTemplateFile != "" {
return fmt.Errorf(
"%s: cannot specify both git.commitMessageTemplate and git.commitMessageTemplateFile",
configFileAbsPath,
)
}

return nil
}

Expand Down Expand Up @@ -1456,14 +1464,36 @@ func (c *Config) gitAutoCommit(status *git.Status) error {
String()
},
})
templateOptions := chezmoi.TemplateOptions{
Options: slices.Clone(c.Template.Options),
var name string
var commitMessageTemplateData []byte
switch {
case c.Git.CommitMessageTemplate != "":
name = "git.commitMessageTemplate"
commitMessageTemplateData = []byte(c.Git.CommitMessageTemplate)
case c.Git.CommitMessageTemplateFile != "":
if c.sourceDirAbsPathErr != nil {
return c.sourceDirAbsPathErr
}
commitMessageTemplateFileAbsPath := c.sourceDirAbsPath.JoinString(
c.Git.CommitMessageTemplateFile,
)
name = c.sourceDirAbsPath.String()
var err error
commitMessageTemplateData, err = c.baseSystem.ReadFile(commitMessageTemplateFileAbsPath)
if err != nil {
return err
}
default:
name = "COMMIT_MESSAGE"
commitMessageTemplateData = []byte(templates.CommitMessageTmpl)
}
commitMessageTmpl, err := chezmoi.ParseTemplate(
"commit_message",
[]byte(c.Git.CommitMessageTemplate),
name,
commitMessageTemplateData,
funcMap,
templateOptions,
chezmoi.TemplateOptions{
Options: slices.Clone(c.Template.Options),
},
)
if err != nil {
return err
Expand Down Expand Up @@ -2731,8 +2761,7 @@ func newConfigFile(bds *xdg.BaseDirectorySpecification) ConfigFile {
},
Format: writeDataFormatJSON,
Git: gitCmdConfig{
Command: "git",
CommitMessageTemplate: templates.CommitMessageTmpl,
Command: "git",
},
GitHub: gitHubConfig{
RefreshPeriod: 1 * time.Minute,
Expand Down
11 changes: 6 additions & 5 deletions internal/cmd/gitcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
)

type gitCmdConfig struct {
Command string `json:"command" mapstructure:"command" yaml:"command"`
AutoAdd bool `json:"autoadd" mapstructure:"autoadd" yaml:"autoadd"`
AutoCommit bool `json:"autocommit" mapstructure:"autocommit" yaml:"autocommit"`
AutoPush bool `json:"autopush" mapstructure:"autopush" yaml:"autopush"`
CommitMessageTemplate string `json:"commitMessageTemplate" mapstructure:"commitMessageTemplate" yaml:"commitMessageTemplate"`
Command string `json:"command" mapstructure:"command" yaml:"command"`
AutoAdd bool `json:"autoadd" mapstructure:"autoadd" yaml:"autoadd"`
AutoCommit bool `json:"autocommit" mapstructure:"autocommit" yaml:"autocommit"`
AutoPush bool `json:"autopush" mapstructure:"autopush" yaml:"autopush"`
CommitMessageTemplate string `json:"commitMessageTemplate" mapstructure:"commitMessageTemplate" yaml:"commitMessageTemplate"`
CommitMessageTemplateFile string `json:"commitMessageTemplateFile" mapstructure:"commitMessageTemplateFile" yaml:"commitMessageTemplateFile"`
}

func (c *Config) newGitCmd() *cobra.Command {
Expand Down
18 changes: 16 additions & 2 deletions internal/cmd/testdata/scripts/autocommit.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,25 @@ 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"'
appendline $CHEZMOICONFIGDIR/chezmoi.toml ' commitMessageTemplate = "feat: my commit message"'
exec chezmoi edit $HOME${/}.dir${/}file
exec git --git-dir=$CHEZMOISOURCEDIR/.git show HEAD
stdout 'feat: my commit'
stdout 'feat: my commit message'

# test that only one of git.commitMessageTemplate and git.commitMessageTemplateFile can be set
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"'

# 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'
removeline $CHEZMOICONFIGDIR/chezmoi.toml ' commitMessageTemplateFile = ".COMMIT_MESSAGE.tmpl"'

-- home/user/.config/chezmoi/chezmoi.toml --
[git]
autoCommit = true
-- home/user/.local/share/chezmoi/.COMMIT_MESSAGE.tmpl --
my commit message file

0 comments on commit f7c3745

Please sign in to comment.