Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add git-auto-commit and git-auto-push hooks #4236

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"]

Loading