Skip to content

Commit

Permalink
feat: Add git-auto-commit and git-auto-push hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Jan 28, 2025
1 parent e25d60b commit 500c677
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
8 changes: 5 additions & 3 deletions assets/chezmoi.io/docs/reference/configuration-file/hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ The following events are defined:
| Event | Trigger |
| --------------------- | --------------------------------------------- |
| *command*, e.g. `add` | Running `chezmoi command`, e.g. `chezmoi add` |
| `git-auto-commit` | Generating an automatic git commit |
| `git-auto-push` | Running an automatic git push |
| `read-source-state` | Reading the source state |

Each event can have a `.pre` and/or a `.post` command. The *event*.`pre` command
is executed before *event* occurs and the *event*`.post` command is executed
after *event* has occurred.

A command contains a `command` or `script` and an optional array of strings
`args`. `command`s are executed directly. `script`s are executed with
configured interpreter for the script's extension, see the [section on
A command contains a `command` or `script` and an optional array of strings
`args`. `command`s are executed directly. `script`s are executed with configured
interpreter for the script's extension, see the [section on
interpreters](interpreters.md).

!!! example
Expand Down
22 changes: 20 additions & 2 deletions internal/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1583,15 +1583,33 @@ func (c *Config) gitAutoCommit(cmd *cobra.Command, status *chezmoigit.Status) er
if err != nil {
return err
}
return c.run(c.WorkingTreeAbsPath, c.Git.Command, []string{"commit", "--message", string(commitMessage)})
if err := c.runHookPre("git-auto-commit"); err != nil {
return err
}
if err := c.run(c.WorkingTreeAbsPath, c.Git.Command, []string{"commit", "--message", string(commitMessage)}); err != nil {
return err
}
if err := c.runHookPost("git-auto-commit"); err != nil {
return err
}
return nil
}

// gitAutoPush pushes all changes to the remote if status is not empty.
func (c *Config) gitAutoPush(status *chezmoigit.Status) error {
if status.Empty() {
return nil
}
return c.run(c.WorkingTreeAbsPath, c.Git.Command, []string{"push"})
if err := c.runHookPre("git-auto-push"); err != nil {
return err
}
if err := c.run(c.WorkingTreeAbsPath, c.Git.Command, []string{"push"}); err != nil {
return err
}
if err := c.runHookPost("git-auto-push"); err != nil {
return err
}
return nil
}

// gitCommitMessage returns the git commit message for the given status.
Expand Down
17 changes: 17 additions & 0 deletions internal/cmd/testdata/scripts/autopush.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ exec chezmoi init file://$WORK/dotfiles.git

# test that chezmoi add creates and pushes a commit
exec chezmoi add $HOME${/}.file
stdout ^git-auto-commit-pre$
stdout ^git-auto-commit-post$
stdout ^git-auto-push-pre$
stdout ^git-auto-push-post$
exec git --git-dir=$WORK/dotfiles.git show HEAD
stdout 'Add \.file'

Expand Down Expand Up @@ -41,3 +45,16 @@ stdout 'Remove \.file'
-- home/user/.config/chezmoi/chezmoi.toml --
[git]
autoPush = true
[hooks.git-auto-commit.pre]
command = "echo"
args = ["git-auto-commit-pre"]
[hooks.git-auto-commit.post]
command = "echo"
args = ["git-auto-commit-post"]
[hooks.git-auto-push.pre]
command = "echo"
args = ["git-auto-push-pre"]
[hooks.git-auto-push.post]
command = "echo"
args = ["git-auto-push-post"]

0 comments on commit 500c677

Please sign in to comment.