Skip to content

Commit 359952c

Browse files
committed
git: add SkipHooks option to commit and amend
1 parent 581070a commit 359952c

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
`SetRefIfMatches`, and `CreateRef`.
1414
- `git.RefMutation` has a new `IsNoop` method to make it easier to check for
1515
the zero value.
16+
- `git.CommitOptions` and `git.AmendOptions` have a new field: `SkipHooks`.
1617

1718
### Changed
1819

api.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,9 @@ type CommitOptions struct {
523523
AuthorTime time.Time
524524
Committer object.User
525525
CommitTime time.Time
526+
527+
// If SkipHooks is true, pre-commit and commit-msg hooks will be skipped.
528+
SkipHooks bool
526529
}
527530

528531
func (opts CommitOptions) addToEnv(env []string) []string {
@@ -543,13 +546,20 @@ func (opts CommitOptions) addToEnv(env []string) []string {
543546
return env
544547
}
545548

549+
func (opts CommitOptions) addToArgs(args []string) []string {
550+
if opts.SkipHooks {
551+
args = append(args, "--no-verify")
552+
}
553+
return args
554+
}
555+
546556
// Commit creates a new commit on HEAD with the staged content.
547557
// The message will be used exactly as given.
548558
func (g *Git) Commit(ctx context.Context, message string, opts CommitOptions) error {
549559
out := new(bytes.Buffer)
550560
w := &limitWriter{w: out, n: errorOutputLimit}
551561
err := g.runner.RunGit(ctx, &Invocation{
552-
Args: []string{"commit", "--quiet", "--file=-", "--cleanup=verbatim"},
562+
Args: opts.addToArgs([]string{"commit", "--quiet", "--file=-", "--cleanup=verbatim"}),
553563
Dir: g.dir,
554564
Env: opts.addToEnv(nil),
555565
Stdin: strings.NewReader(message),
@@ -568,7 +578,7 @@ func (g *Git) CommitAll(ctx context.Context, message string, opts CommitOptions)
568578
out := new(bytes.Buffer)
569579
w := &limitWriter{w: out, n: errorOutputLimit}
570580
err := g.runner.RunGit(ctx, &Invocation{
571-
Args: []string{"commit", "--quiet", "--file=-", "--cleanup=verbatim", "--all"},
581+
Args: opts.addToArgs([]string{"commit", "--quiet", "--file=-", "--cleanup=verbatim", "--all"}),
572582
Dir: g.dir,
573583
Env: opts.addToEnv(nil),
574584
Stdin: strings.NewReader(message),
@@ -612,7 +622,9 @@ func (g *Git) CommitFiles(ctx context.Context, message string, pathspecs []Paths
612622
}
613623
}
614624
}
615-
args := []string{"commit", "--quiet", "--file=-", "--cleanup=verbatim", "--only", "--allow-empty", "--"}
625+
args := []string{"commit", "--quiet", "--file=-", "--cleanup=verbatim", "--only", "--allow-empty"}
626+
args = opts.addToArgs(args)
627+
args = append(args, "--")
616628
for _, spec := range pathspecs {
617629
args = append(args, spec.String())
618630
}
@@ -650,15 +662,21 @@ type AmendOptions struct {
650662
// If CommitTime is not zero, then it will be used as the commit time
651663
// instead of now.
652664
CommitTime time.Time
665+
666+
// If SkipHooks is true, pre-commit and commit-msg hooks will be skipped.
667+
SkipHooks bool
653668
}
654669

655-
func (opts AmendOptions) addAuthorToArgs(args []string) []string {
670+
func (opts AmendOptions) addToArgs(args []string) []string {
656671
if opts.Author != "" {
657672
args = append(args, "--author="+string(opts.Author))
658673
}
659674
if !opts.AuthorTime.IsZero() {
660675
args = append(args, "--date="+opts.AuthorTime.Format(time.RFC3339))
661676
}
677+
if opts.SkipHooks {
678+
args = append(args, "--no-verify")
679+
}
662680
return args
663681
}
664682

@@ -688,7 +706,7 @@ func (g *Git) Amend(ctx context.Context, opts AmendOptions) error {
688706
out := new(bytes.Buffer)
689707
w := &limitWriter{w: out, n: errorOutputLimit}
690708
err := g.runner.RunGit(ctx, &Invocation{
691-
Args: opts.addAuthorToArgs([]string{
709+
Args: opts.addToArgs([]string{
692710
"commit",
693711
"--amend",
694712
"--quiet",
@@ -722,7 +740,7 @@ func (g *Git) AmendAll(ctx context.Context, opts AmendOptions) error {
722740
out := new(bytes.Buffer)
723741
w := &limitWriter{w: out, n: errorOutputLimit}
724742
err := g.runner.RunGit(ctx, &Invocation{
725-
Args: opts.addAuthorToArgs([]string{
743+
Args: opts.addToArgs([]string{
726744
"commit",
727745
"--amend",
728746
"--all",
@@ -784,7 +802,7 @@ func (g *Git) AmendFiles(ctx context.Context, pathspecs []Pathspec, opts AmendOp
784802
}
785803
}
786804
}
787-
args := opts.addAuthorToArgs([]string{
805+
args := opts.addToArgs([]string{
788806
"commit",
789807
"--amend",
790808
"--only",

0 commit comments

Comments
 (0)