mirror of
https://github.com/hashicorp/vault.git
synced 2025-08-06 14:47:01 +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>
95 lines
1.9 KiB
Go
95 lines
1.9 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package git
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
)
|
|
|
|
// ResetMode is is mode to use when resetting the repository
|
|
type ResetMode string
|
|
|
|
const (
|
|
ResetModeSoft ResetMode = "soft"
|
|
ResetModeMixed ResetMode = "mixed"
|
|
ResetModeHard ResetMode = "hard"
|
|
ResetModeMerge ResetMode = "merge"
|
|
ResetModeKeep ResetMode = "keep"
|
|
)
|
|
|
|
// ResetOpts are the git reset flags and arguments
|
|
// See: https://git-scm.com/docs/git-reset
|
|
type ResetOpts struct {
|
|
// Options
|
|
Mode ResetMode // [--soft, --hard, etc..]
|
|
NoRefresh bool // --no-refresh
|
|
Patch bool // --patch
|
|
Quiet bool // --quiet
|
|
Refresh bool // --refresh
|
|
|
|
// Targets
|
|
Commit string // <commit>
|
|
Treeish string // <tree-ish>
|
|
PathSpec []string // <pathspec>
|
|
}
|
|
|
|
// Reset runs the git reset command
|
|
func (c *Client) Reset(ctx context.Context, opts *ResetOpts) (*ExecResponse, error) {
|
|
return c.Exec(ctx, "reset", opts)
|
|
}
|
|
|
|
// String returns the options as a string
|
|
func (o *ResetOpts) String() string {
|
|
return strings.Join(o.Strings(), " ")
|
|
}
|
|
|
|
// Strings returns the options as a string slice
|
|
func (o *ResetOpts) Strings() []string {
|
|
if o == nil {
|
|
return nil
|
|
}
|
|
|
|
opts := []string{}
|
|
|
|
// Do mode before flags if it set
|
|
if o.Mode != "" {
|
|
opts = append(opts, "--"+string(o.Mode))
|
|
}
|
|
|
|
// Flags
|
|
if o.NoRefresh {
|
|
opts = append(opts, "--no-refresh")
|
|
}
|
|
|
|
if o.Quiet {
|
|
opts = append(opts, "--quiet")
|
|
}
|
|
|
|
if o.Refresh {
|
|
opts = append(opts, "--refresh")
|
|
}
|
|
|
|
// Do Patch after flags but before our targets
|
|
if o.Patch {
|
|
opts = append(opts, "--patch")
|
|
}
|
|
|
|
// Do our targets
|
|
if o.Commit != "" {
|
|
opts = append(opts, o.Commit)
|
|
}
|
|
|
|
if o.Treeish != "" {
|
|
opts = append(opts, o.Treeish)
|
|
}
|
|
|
|
// 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
|
|
}
|