mirror of
https://github.com/hashicorp/vault.git
synced 2025-08-06 22:57:02 +02:00
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>
143 lines
3.4 KiB
Go
143 lines
3.4 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package git
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
type (
|
|
// IgnoredMode determines how to handle ignored files
|
|
IgnoredMode = string
|
|
// IgnoredMode determines how to handle changes to submodules
|
|
IgnoreSubmodulesWhen = string
|
|
)
|
|
|
|
const (
|
|
IgnoredModeTraditional IgnoredMode = "traditional"
|
|
IgnoredModeNo IgnoredMode = "no"
|
|
IgnoredModeMatching IgnoredMode = "matching"
|
|
|
|
IgnoreSubmodulesWhenNone IgnoreSubmodulesWhen = "none"
|
|
IgnoreSubmodulesWhenUntracked IgnoreSubmodulesWhen = "untracked"
|
|
IgnoreSubmodulesWhenDirty IgnoreSubmodulesWhen = "dirty"
|
|
IgnoreSubmodulesWhenAll IgnoreSubmodulesWhen = "all"
|
|
)
|
|
|
|
// StatusOpts are the git status flags and arguments
|
|
// See: https://git-scm.com/docs/git-status
|
|
type StatusOpts struct {
|
|
// Options
|
|
AheadBehind bool // --ahead-behind
|
|
Branch bool // --branch
|
|
Column string // --column=
|
|
FindRenames uint // --find-renames=
|
|
Ignored IgnoredMode // --ignored=
|
|
IgnoreSubmodules IgnoreSubmodulesWhen // --ignore-submodules=<when>
|
|
Long bool // --long
|
|
NoAheadBehind bool // --no-ahead-behind
|
|
NoColumn bool // --no-column
|
|
NoRenames bool // --no-renames
|
|
Porcelain bool // --porcelain
|
|
Renames bool // --renames
|
|
Short bool // --short
|
|
ShowStash bool // --show-stash
|
|
UntrackedFiles UntrackedFiles // --untracked-files=<mode>
|
|
Verbose bool // --verbose
|
|
|
|
// Targets
|
|
PathSpec []string // <pathspec>
|
|
}
|
|
|
|
// Status runs the git status command
|
|
func (c *Client) Status(ctx context.Context, opts *StatusOpts) (*ExecResponse, error) {
|
|
return c.Exec(ctx, "status", opts)
|
|
}
|
|
|
|
// String returns the options as a string
|
|
func (o *StatusOpts) String() string {
|
|
return strings.Join(o.Strings(), " ")
|
|
}
|
|
|
|
// Strings returns the options as a string slice
|
|
func (o *StatusOpts) Strings() []string {
|
|
if o == nil {
|
|
return nil
|
|
}
|
|
|
|
opts := []string{}
|
|
if o.AheadBehind {
|
|
opts = append(opts, "--ahead-behind")
|
|
}
|
|
|
|
if o.Branch {
|
|
opts = append(opts, "--branch")
|
|
}
|
|
|
|
if o.Column != "" {
|
|
opts = append(opts, fmt.Sprintf("--column=%s", o.Column))
|
|
}
|
|
|
|
if o.FindRenames > 0 {
|
|
opts = append(opts, fmt.Sprintf("--find-renames=%d", o.FindRenames))
|
|
}
|
|
|
|
if o.Ignored != "" {
|
|
opts = append(opts, fmt.Sprintf("--ignored=%s", string(o.Ignored)))
|
|
}
|
|
|
|
if o.IgnoreSubmodules != "" {
|
|
opts = append(opts, fmt.Sprintf("--ignore-submodules=%s", string(o.IgnoreSubmodules)))
|
|
}
|
|
|
|
if o.Long {
|
|
opts = append(opts, "--long")
|
|
}
|
|
|
|
if o.NoAheadBehind {
|
|
opts = append(opts, "--no-ahead-behind")
|
|
}
|
|
|
|
if o.NoColumn {
|
|
opts = append(opts, "--no-column")
|
|
}
|
|
|
|
if o.NoRenames {
|
|
opts = append(opts, "--no-renames")
|
|
}
|
|
|
|
if o.Porcelain {
|
|
opts = append(opts, "--porcelain")
|
|
}
|
|
|
|
if o.Renames {
|
|
opts = append(opts, "--renames")
|
|
}
|
|
|
|
if o.Short {
|
|
opts = append(opts, "--short")
|
|
}
|
|
|
|
if o.ShowStash {
|
|
opts = append(opts, "--show-stash")
|
|
}
|
|
|
|
if o.UntrackedFiles != "" {
|
|
opts = append(opts, fmt.Sprintf("--untracked-files=%s", string(o.UntrackedFiles)))
|
|
}
|
|
|
|
if o.Verbose {
|
|
opts = append(opts, "--verbose")
|
|
}
|
|
|
|
// 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
|
|
}
|