vault/tools/pipeline/internal/pkg/git/cherry-pick.go
Ryan Cragun cb321ce774
[VAULT-36201] pipeline(git): add Go git client (#30645)
As I was working on VAULT-34829 it became clear that we needed to solve
the problem of using Git from Go. Initially I tried to use the
go-git/go-git pure Go implementation of Git, but it lacked several
features that we needed.

The next best option seemed to be shelling out to Git. What started as a
simple way to execute Git commands with the requisite environment and
configuration led to the implementation.

A few notes:
- I did not add support for all flags for the implemented sub-commands
- I wanted it to handle automatic inject and configuration of PATs when
  operating against private remote repositories in Github.
- I did not want to rely on the local machine having been configured.
  The client that is built in is capable of configuring everything that
  we need via environment variables.
- I chose to go the environment variable route for configuration as it's
  the only way to not overwrite local configuration or set up our own
  cred store.

As the git client ended up being ~50% of the work for VAULT-34829, I
decided to extract it out into its own PR to reduce the review burden.

NOTE: While we don't use it in CI, there is .golanci.yml present in the
 the tree and it causes problems in editors that expect it to be V2. As
 such I migrated it.

Signed-off-by: Ryan Cragun <me@ryan.ec>
2025-05-16 12:41:31 -06:00

148 lines
3.6 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package git
import (
"context"
"fmt"
"strings"
)
// EmptyCommit are supported empty commit handling options
type EmptyCommit = string
const (
EmptyCommitDrop EmptyCommit = "drop"
EmptyCommitKeep EmptyCommit = "keep"
EmptyCommitStop EmptyCommit = "stop"
)
// CherryPickOpts are the git cherry-pick flags and arguments
// See: https://git-scm.com/docs/git-cherry-pick
type CherryPickOpts struct {
// Options
AllowEmpty bool // --allow-empty
AllowEmptyMessage bool // --allow-empty-message
Empty EmptyCommit // --empty=
FF bool // --ff
GPGSign bool // --gpgsign
GPGSignKeyID string // --gpgsign=<key-id>
Mainline string // --mainline
NoReReReAutoupdate bool // --no-rerere-autoupdate
Record bool // -x
ReReReAutoupdate bool // --rerere-autoupdate
Signoff bool // --signoff
Strategy MergeStrategy // --strategy
StrategyOptions []MergeStrategyOption // --strategy-option=<option>
// Target
Commit string // <commit>
// Sequences
Continue bool // --continue
Abort bool // --abort
Quit bool // --quit
}
// CherryPick runs the git cherry-pick command
func (c *Client) CherryPick(ctx context.Context, opts *CherryPickOpts) (*ExecResponse, error) {
return c.Exec(ctx, "cherry-pick", opts)
}
// CherryPickAbort aborts an in-progress cherry-pick
func (c *Client) CherryPickAbort(ctx context.Context) (*ExecResponse, error) {
return c.CherryPick(ctx, &CherryPickOpts{Abort: true})
}
// CherryPickContinue continues an in-progress cherry-pick
func (c *Client) CherryPickContinue(ctx context.Context) (*ExecResponse, error) {
return c.CherryPick(ctx, &CherryPickOpts{Continue: true})
}
// CherryPickQuit quits an in-progress cherry-pick
func (c *Client) CherryPickQuit(ctx context.Context) (*ExecResponse, error) {
return c.CherryPick(ctx, &CherryPickOpts{Quit: true})
}
// String returns the options as a string
func (o *CherryPickOpts) String() string {
return strings.Join(o.Strings(), " ")
}
// Strings returns the options as a string slice
func (o *CherryPickOpts) Strings() []string {
if o == nil {
return nil
}
switch {
case o.Abort:
return []string{"--abort"}
case o.Continue:
return []string{"--continue"}
case o.Quit:
return []string{"--quit"}
}
opts := []string{}
if o.AllowEmpty {
opts = append(opts, "--allow-empty")
}
if o.AllowEmptyMessage {
opts = append(opts, "--allow-empty-message")
}
if o.Empty != "" {
opts = append(opts, fmt.Sprintf("--empty=%s", string(o.Empty)))
}
if o.FF {
opts = append(opts, "--ff")
}
if o.GPGSign {
opts = append(opts, "--gpg-sign")
}
if o.GPGSignKeyID != "" {
opts = append(opts, fmt.Sprintf("--gpg-sign=%s", o.GPGSignKeyID))
}
if o.Mainline != "" {
opts = append(opts, fmt.Sprintf("--mainline=%s", o.Mainline))
}
if o.NoReReReAutoupdate {
opts = append(opts, "--no-rerere-autoupdate")
}
if o.Record {
opts = append(opts, "-x")
}
if o.ReReReAutoupdate {
opts = append(opts, "--rerere-autoupdate")
}
if o.Signoff {
opts = append(opts, "--signoff")
}
if o.Strategy != "" {
opts = append(opts, fmt.Sprintf("--strategy=%s", string(o.Strategy)))
}
for _, opt := range o.StrategyOptions {
opts = append(opts, fmt.Sprintf("--strategy-option=%s", string(opt)))
}
if o.Commit != "" {
opts = append(opts, o.Commit)
}
return opts
}