Skip to content

Commit

Permalink
git: add SkipHooks option to commit and amend
Browse files Browse the repository at this point in the history
  • Loading branch information
zombiezen committed Feb 18, 2022
1 parent 581070a commit 359952c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
32 changes: 25 additions & 7 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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),
Expand All @@ -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),
Expand Down Expand Up @@ -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())
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 359952c

Please sign in to comment.