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

109 lines
2.6 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package git
import (
"context"
"fmt"
"strings"
)
type (
DiffAlgorithm = string
DiffMergeFormat = string
)
const (
DiffAlgorithmPatience DiffAlgorithm = "patience"
DiffAlgorithmMinimal DiffAlgorithm = "minimal"
DiffAlgorithmHistogram DiffAlgorithm = "histogram"
DiffAlgorithmMyers DiffAlgorithm = "myers"
DiffMergeFormatOff DiffMergeFormat = "off"
DiffMergeFormatNone DiffMergeFormat = "none"
DiffMergeFormatFirstParent DiffMergeFormat = "first-parent"
DiffMergeFormatSeparate DiffMergeFormat = "separate"
DiffMergeFormatCombined DiffMergeFormat = "combined"
DiffMergeFormatDenseCombined DiffMergeFormat = "dense-combined"
DiffMergeFormatRemerge DiffMergeFormat = "remerge"
)
// ShowOpts are the git show flags and arguments
// See: https://git-scm.com/docs/git-show
type ShowOpts struct {
// Options
DiffAlgorithm DiffAlgorithm // --diff-algorithm=<algo>
DiffMerges DiffMergeFormat // --diff-merges=<format>
Format string // --format <format>
NoColor bool // --no-color
NoPatch bool // --no-patch
Patch bool // --patch
Output string // --output=<file>
Raw bool // --raw
// Targets
Object string // <object>
PathSpec []string // <pathspec>
}
// Show runs the git show command
func (c *Client) Show(ctx context.Context, opts *ShowOpts) (*ExecResponse, error) {
return c.Exec(ctx, "show", opts)
}
// String returns the options as a string
func (o *ShowOpts) String() string {
return strings.Join(o.Strings(), " ")
}
// Strings returns the options as a string slice
func (o *ShowOpts) Strings() []string {
if o == nil {
return nil
}
opts := []string{}
if o.DiffAlgorithm != "" {
opts = append(opts, fmt.Sprintf("--diff-algorithm=%s", string(o.DiffAlgorithm)))
}
if o.DiffMerges != "" {
opts = append(opts, fmt.Sprintf("--diff-merges=%s", string(o.DiffMerges)))
}
if o.Format != "" {
opts = append(opts, fmt.Sprintf("--format=%s", string(o.Format)))
}
if o.NoColor {
opts = append(opts, "--no-color")
}
if o.NoPatch {
opts = append(opts, "--no-patch")
}
if o.Output != "" {
opts = append(opts, fmt.Sprintf("--output=%s", o.Output))
}
if o.Patch {
opts = append(opts, "--patch")
}
if o.Raw {
opts = append(opts, "--raw")
}
opts = append(opts, o.Object)
// If there's a pathspec, append the paths at the very end
if len(o.PathSpec) > 0 {
opts = append(append(opts, "--"), o.PathSpec...)
}
return opts
}