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

123 lines
2.5 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package git
import (
"context"
"fmt"
"strings"
)
// CheckoutOpts are the git checkout flags and arguments
// See: https://git-scm.com/docs/git-checkout
type CheckoutOpts struct {
// Options
NewBranch string // -b
NewBranchForceCheckout string // -B
Detach bool // --detach
Force bool // -f
Guess bool // --guess
Progress bool // --progress
NoTrack bool // --no-track
Orphan string // --orphan
Ours bool // --ours
Quiet bool // --quiet
Theirs bool // --theirs
Track BranchTrack // --track
// Targets
Branch string // <new-branch>
StartPoint string // <start-point>
Treeish string // <tree-ish>
// Paths
PathSpec []string // -- <pathspec>
}
// Branch runs the git checkout command
func (c *Client) Checkout(ctx context.Context, opts *CheckoutOpts) (*ExecResponse, error) {
return c.Exec(ctx, "checkout", opts)
}
// String returns the options as a string
func (o *CheckoutOpts) String() string {
return strings.Join(o.Strings(), " ")
}
// Strings returns the options as a string slice
func (o *CheckoutOpts) Strings() []string {
if o == nil {
return nil
}
opts := []string{}
if o.NewBranch != "" {
opts = append(opts, "-b", o.NewBranch)
}
if o.NewBranchForceCheckout != "" {
opts = append(opts, "-B", o.NewBranchForceCheckout)
}
if o.Detach {
opts = append(opts, "--detach")
}
if o.Force {
opts = append(opts, "--force")
}
if o.Guess {
opts = append(opts, "--guess")
}
if o.NoTrack {
opts = append(opts, "--no-track")
}
if o.Orphan != "" {
opts = append(opts, "--orphan", string(o.Orphan))
}
if o.Ours {
opts = append(opts, "--ours")
}
if o.Progress {
opts = append(opts, "--progress")
}
if o.Quiet {
opts = append(opts, "--quiet")
}
if o.Theirs {
opts = append(opts, "--theirs")
}
if o.Track != "" {
opts = append(opts, fmt.Sprintf("--track=%s", string(o.Track)))
}
// Do the <branch>, <start-point>, and <tree-ish> before pathspec
if o.Branch != "" {
opts = append(opts, o.Branch)
}
if o.StartPoint != "" {
opts = append(opts, o.StartPoint)
}
if o.Treeish != "" {
opts = append(opts, o.Treeish)
}
// If there's a pathspec always set it last
if len(o.PathSpec) > 0 {
opts = append(append(opts, "--"), o.PathSpec...)
}
return opts
}