From 359952c5baae60d2229f94bd7949006bcf772e2f Mon Sep 17 00:00:00 2001 From: Ross Light Date: Fri, 18 Feb 2022 12:21:11 -0800 Subject: [PATCH] git: add SkipHooks option to commit and amend --- CHANGELOG.md | 1 + api.go | 32 +++++++++++++++++++++++++------- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d5ecb51..6969777 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 `SetRefIfMatches`, and `CreateRef`. - `git.RefMutation` has a new `IsNoop` method to make it easier to check for the zero value. +- `git.CommitOptions` and `git.AmendOptions` have a new field: `SkipHooks`. ### Changed diff --git a/api.go b/api.go index 3f36668..ae5b8ba 100644 --- a/api.go +++ b/api.go @@ -523,6 +523,9 @@ type CommitOptions struct { AuthorTime time.Time Committer object.User CommitTime time.Time + + // If SkipHooks is true, pre-commit and commit-msg hooks will be skipped. + SkipHooks bool } func (opts CommitOptions) addToEnv(env []string) []string { @@ -543,13 +546,20 @@ func (opts CommitOptions) addToEnv(env []string) []string { return env } +func (opts CommitOptions) addToArgs(args []string) []string { + if opts.SkipHooks { + args = append(args, "--no-verify") + } + return args +} + // Commit creates a new commit on HEAD with the staged content. // The message will be used exactly as given. func (g *Git) Commit(ctx context.Context, message string, opts CommitOptions) error { out := new(bytes.Buffer) w := &limitWriter{w: out, n: errorOutputLimit} err := g.runner.RunGit(ctx, &Invocation{ - Args: []string{"commit", "--quiet", "--file=-", "--cleanup=verbatim"}, + Args: opts.addToArgs([]string{"commit", "--quiet", "--file=-", "--cleanup=verbatim"}), Dir: g.dir, Env: opts.addToEnv(nil), Stdin: strings.NewReader(message), @@ -568,7 +578,7 @@ func (g *Git) CommitAll(ctx context.Context, message string, opts CommitOptions) out := new(bytes.Buffer) w := &limitWriter{w: out, n: errorOutputLimit} err := g.runner.RunGit(ctx, &Invocation{ - Args: []string{"commit", "--quiet", "--file=-", "--cleanup=verbatim", "--all"}, + Args: opts.addToArgs([]string{"commit", "--quiet", "--file=-", "--cleanup=verbatim", "--all"}), Dir: g.dir, Env: opts.addToEnv(nil), Stdin: strings.NewReader(message), @@ -612,7 +622,9 @@ func (g *Git) CommitFiles(ctx context.Context, message string, pathspecs []Paths } } } - args := []string{"commit", "--quiet", "--file=-", "--cleanup=verbatim", "--only", "--allow-empty", "--"} + args := []string{"commit", "--quiet", "--file=-", "--cleanup=verbatim", "--only", "--allow-empty"} + args = opts.addToArgs(args) + args = append(args, "--") for _, spec := range pathspecs { args = append(args, spec.String()) } @@ -650,15 +662,21 @@ type AmendOptions struct { // If CommitTime is not zero, then it will be used as the commit time // instead of now. CommitTime time.Time + + // If SkipHooks is true, pre-commit and commit-msg hooks will be skipped. + SkipHooks bool } -func (opts AmendOptions) addAuthorToArgs(args []string) []string { +func (opts AmendOptions) addToArgs(args []string) []string { if opts.Author != "" { args = append(args, "--author="+string(opts.Author)) } if !opts.AuthorTime.IsZero() { args = append(args, "--date="+opts.AuthorTime.Format(time.RFC3339)) } + if opts.SkipHooks { + args = append(args, "--no-verify") + } return args } @@ -688,7 +706,7 @@ func (g *Git) Amend(ctx context.Context, opts AmendOptions) error { out := new(bytes.Buffer) w := &limitWriter{w: out, n: errorOutputLimit} err := g.runner.RunGit(ctx, &Invocation{ - Args: opts.addAuthorToArgs([]string{ + Args: opts.addToArgs([]string{ "commit", "--amend", "--quiet", @@ -722,7 +740,7 @@ func (g *Git) AmendAll(ctx context.Context, opts AmendOptions) error { out := new(bytes.Buffer) w := &limitWriter{w: out, n: errorOutputLimit} err := g.runner.RunGit(ctx, &Invocation{ - Args: opts.addAuthorToArgs([]string{ + Args: opts.addToArgs([]string{ "commit", "--amend", "--all", @@ -784,7 +802,7 @@ func (g *Git) AmendFiles(ctx context.Context, pathspecs []Pathspec, opts AmendOp } } } - args := opts.addAuthorToArgs([]string{ + args := opts.addToArgs([]string{ "commit", "--amend", "--only",