Skip to content

Commit 1996e45

Browse files
committed
Add support for pre-commit-commands
Signed-off-by: Knut Ahlers <knut@ahlers.me>
1 parent 66c64e5 commit 1996e45

File tree

5 files changed

+70
-4
lines changed

5 files changed

+70
-4
lines changed

assets/git_changerelease.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,16 @@ match_major:
2323
# which will be used to add the tag to.
2424
release_commit_message: "prepare release {{.Version}}"
2525

26+
# Commands to run before committing the changelog and adding the tag.
27+
# Therefore these can add content to be included into the release-
28+
# commit. These commands have access to the `TAG_VERSION` variable
29+
# which contains the tag to be applied after the commit. If the
30+
# command specified here is prefixed with a `-` sign, the exit status
31+
# will not fail the release process. If it is not prefixed with a `-`
32+
# a non-zero exit status will terminate the release process. The
33+
# commands will be run from the repostory root, so sub-dirs MUST be
34+
# specified. All commands are run as `bash -ec "..."` so you can use
35+
# bash inside the commands.
36+
pre_commit_commands: []
37+
2638
...

config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ type configFile struct {
1919
ReleaseCommitMessage string `yaml:"release_commit_message"`
2020

2121
IgnoreMessages []string `yaml:"ignore_messages"`
22+
23+
PreCommitCommands []string `yaml:"pre_commit_commands"`
2224
}
2325

2426
func loadConfig(configFiles ...string) (*configFile, error) {

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/Luzifer/git-changerelease
33
go 1.21
44

55
require (
6+
github.com/Luzifer/go_helpers/v2 v2.21.0
67
github.com/Luzifer/rconfig/v2 v2.4.0
78
github.com/Masterminds/semver/v3 v3.2.1
89
github.com/mitchellh/go-homedir v1.1.0

go.sum

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/Luzifer/go_helpers/v2 v2.21.0 h1:kR0kdpTkYpkou3qOr2E+sXh0FxG85Mof4BlRhfSB790=
2+
github.com/Luzifer/go_helpers/v2 v2.21.0/go.mod h1:cIIqMPu3NT8/6kHke+03hVznNDLLKVGA74Lz47CWJyA=
13
github.com/Luzifer/rconfig/v2 v2.4.0 h1:MAdymTlExAZ8mx5VG8xOFAtFQSpWBipKYQHPOmYTn9o=
24
github.com/Luzifer/rconfig/v2 v2.4.0/go.mod h1:hWF3ZVSusbYlg5bEvCwalEyUSY+0JPJWUiIu7rBmav8=
35
github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0rYXWg0=
@@ -7,8 +9,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
79
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
810
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
911
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
10-
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
11-
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
12+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
13+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
1214
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
1315
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
1416
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -18,8 +20,9 @@ github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVs
1820
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
1921
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
2022
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
21-
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
2223
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
24+
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
25+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
2326
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
2427
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
2528
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
@@ -30,5 +33,6 @@ gopkg.in/validator.v2 v2.0.1 h1:xF0KWyGWXm/LM2G1TrEjqOu4pa6coO9AlWSf3msVfDY=
3033
gopkg.in/validator.v2 v2.0.1/go.mod h1:lIUZBlB3Im4s/eYp39Ry/wkR02yOPhZ9IwIRBjuPuG8=
3134
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
3235
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
33-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
3436
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
37+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
38+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

main.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/mitchellh/go-homedir"
1616
"github.com/sirupsen/logrus"
1717

18+
"github.com/Luzifer/go_helpers/v2/env"
1819
"github.com/Luzifer/rconfig/v2"
1920
)
2021

@@ -152,6 +153,12 @@ func main() {
152153
logrus.WithError(err).Fatal("writing changelog")
153154
}
154155

156+
for _, pc := range config.PreCommitCommands {
157+
if err = runUserCommand(pc, map[string]string{"TAG_VERSION": newVersion.String()}); err != nil {
158+
logrus.WithError(err).WithField("cmd", pc).Fatal("executing pre-commit-commands")
159+
}
160+
}
161+
155162
// Write the tag
156163
if err = applyTag("v" + newVersion.String()); err != nil {
157164
logrus.WithError(err).Fatal("applying tag")
@@ -260,3 +267,43 @@ func renderTemplate(name string, tplSrc []byte, values any) ([]byte, error) {
260267

261268
return buf.Bytes(), nil
262269
}
270+
271+
func runUserCommand(command string, extraEnv map[string]string) error {
272+
if len(command) == 0 {
273+
return errors.New("empty command specified")
274+
}
275+
276+
// Check whether command is prefixed with a minus: If so the error
277+
// will only be logged and is not fatal to the changerelease run
278+
fatal := command[0] != '-'
279+
if !fatal {
280+
command = command[1:]
281+
}
282+
283+
logrus.WithField("fatal", fatal).WithField("command", command).Trace("running pre_commit_commands")
284+
285+
envVars := env.ListToMap(os.Environ())
286+
for k, v := range extraEnv {
287+
envVars[k] = v
288+
}
289+
290+
wd, err := filenameInGitRoot(".")
291+
if err != nil {
292+
return fmt.Errorf("determining workdir for commands: %w", err)
293+
}
294+
295+
cmd := exec.Command("/usr/bin/env", "bash", "-ec", command)
296+
cmd.Stderr = os.Stderr
297+
cmd.Stdout = os.Stdout
298+
cmd.Env = env.MapToList(envVars)
299+
cmd.Dir = wd
300+
301+
if err = cmd.Run(); err != nil {
302+
if fatal {
303+
return fmt.Errorf("command had error: %w", err)
304+
}
305+
logrus.WithError(err).WithField("cmd", command).Warn("command had error, marked as non-fatal")
306+
}
307+
308+
return nil
309+
}

0 commit comments

Comments
 (0)