vault/tools/pipeline/internal/pkg/git/apply.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

116 lines
2.6 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package git
import (
"context"
"fmt"
"strings"
)
// ApplyWhitespaceAction are actions Git can take when encountering whitespace
// conflicts during apply.
type ApplyWhitespaceAction = string
const (
ApplyWhitespaceActionNoWarn ApplyWhitespaceAction = "nowarn"
ApplyWhitespaceActionWarn ApplyWhitespaceAction = "warn"
ApplyWhitespaceActionFix ApplyWhitespaceAction = "fix"
ApplyWhitespaceActionError ApplyWhitespaceAction = "error"
ApplyWhitespaceActionErrorAll ApplyWhitespaceAction = "error-all"
)
// ApplyOpts are the git apply flags and arguments
// See: https://git-scm.com/docs/git-apply
type ApplyOpts struct {
// Options
AllowEmpty bool // --allow-empty
Cached bool // --cached
Check bool // --check
Index bool // --index
Ours bool // --ours
Recount bool // --recount
Stat bool // --stat
Summary bool // --summary
Theirs bool // --theirs
ThreeWayMerge bool // -3way
Union bool // --union
Whitespace ApplyWhitespaceAction // --whitespace=<action>
// Targets, depending on which combination of options you're setting
Patch []string // <patch>
}
// Apply runs the git apply command
func (c *Client) Apply(ctx context.Context, opts *ApplyOpts) (*ExecResponse, error) {
return c.Exec(ctx, "apply", opts)
}
// String returns the options as a string
func (o *ApplyOpts) String() string {
return strings.Join(o.Strings(), " ")
}
// Strings returns the options as a string slice
func (o *ApplyOpts) Strings() []string {
if o == nil {
return nil
}
opts := []string{}
if o.AllowEmpty {
opts = append(opts, "--allow-empty")
}
if o.Cached {
opts = append(opts, "--cached")
}
if o.Check {
opts = append(opts, "--check")
}
if o.Index {
opts = append(opts, "--index")
}
if o.Ours {
opts = append(opts, "--ours")
}
if o.Recount {
opts = append(opts, "--recount")
}
if o.Stat {
opts = append(opts, "--stat")
}
if o.Summary {
opts = append(opts, "--summary")
}
if o.Theirs {
opts = append(opts, "--theirs")
}
if o.ThreeWayMerge {
opts = append(opts, "--3way")
}
if o.Union {
opts = append(opts, "--union")
}
if o.Whitespace != "" {
opts = append(opts, fmt.Sprintf("--whitespace=%s", string(o.Whitespace)))
}
if len(o.Patch) > 0 {
opts = append(opts, o.Patch...)
}
return opts
}