Skip to content

Commit 500c677

Browse files
committed
feat: Add git-auto-commit and git-auto-push hooks
1 parent e25d60b commit 500c677

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

assets/chezmoi.io/docs/reference/configuration-file/hooks.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@ The following events are defined:
99
| Event | Trigger |
1010
| --------------------- | --------------------------------------------- |
1111
| *command*, e.g. `add` | Running `chezmoi command`, e.g. `chezmoi add` |
12+
| `git-auto-commit` | Generating an automatic git commit |
13+
| `git-auto-push` | Running an automatic git push |
1214
| `read-source-state` | Reading the source state |
1315

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

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

2325
!!! example

internal/cmd/config.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,15 +1583,33 @@ func (c *Config) gitAutoCommit(cmd *cobra.Command, status *chezmoigit.Status) er
15831583
if err != nil {
15841584
return err
15851585
}
1586-
return c.run(c.WorkingTreeAbsPath, c.Git.Command, []string{"commit", "--message", string(commitMessage)})
1586+
if err := c.runHookPre("git-auto-commit"); err != nil {
1587+
return err
1588+
}
1589+
if err := c.run(c.WorkingTreeAbsPath, c.Git.Command, []string{"commit", "--message", string(commitMessage)}); err != nil {
1590+
return err
1591+
}
1592+
if err := c.runHookPost("git-auto-commit"); err != nil {
1593+
return err
1594+
}
1595+
return nil
15871596
}
15881597

15891598
// gitAutoPush pushes all changes to the remote if status is not empty.
15901599
func (c *Config) gitAutoPush(status *chezmoigit.Status) error {
15911600
if status.Empty() {
15921601
return nil
15931602
}
1594-
return c.run(c.WorkingTreeAbsPath, c.Git.Command, []string{"push"})
1603+
if err := c.runHookPre("git-auto-push"); err != nil {
1604+
return err
1605+
}
1606+
if err := c.run(c.WorkingTreeAbsPath, c.Git.Command, []string{"push"}); err != nil {
1607+
return err
1608+
}
1609+
if err := c.runHookPost("git-auto-push"); err != nil {
1610+
return err
1611+
}
1612+
return nil
15951613
}
15961614

15971615
// gitCommitMessage returns the git commit message for the given status.

internal/cmd/testdata/scripts/autopush.txtar

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ exec chezmoi init file://$WORK/dotfiles.git
1010

1111
# test that chezmoi add creates and pushes a commit
1212
exec chezmoi add $HOME${/}.file
13+
stdout ^git-auto-commit-pre$
14+
stdout ^git-auto-commit-post$
15+
stdout ^git-auto-push-pre$
16+
stdout ^git-auto-push-post$
1317
exec git --git-dir=$WORK/dotfiles.git show HEAD
1418
stdout 'Add \.file'
1519

@@ -41,3 +45,16 @@ stdout 'Remove \.file'
4145
-- home/user/.config/chezmoi/chezmoi.toml --
4246
[git]
4347
autoPush = true
48+
[hooks.git-auto-commit.pre]
49+
command = "echo"
50+
args = ["git-auto-commit-pre"]
51+
[hooks.git-auto-commit.post]
52+
command = "echo"
53+
args = ["git-auto-commit-post"]
54+
[hooks.git-auto-push.pre]
55+
command = "echo"
56+
args = ["git-auto-push-pre"]
57+
[hooks.git-auto-push.post]
58+
command = "echo"
59+
args = ["git-auto-push-post"]
60+

0 commit comments

Comments
 (0)