8000 [VAULT-36201] pipeline(git): add Go git client by ryancragun · Pull Request #30645 · hashicorp/vault · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[VAULT-36201] pipeline(git): add Go git client #30645

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 34 additions & 15 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,39 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0
linters-settings:
depguard:
rules:
main:
list-mode: lax
files:
- "./sdk/**"
allow:
- "github.com/hashicorp/go-metrics/compat"
deny:
- pkg: "github.com/hashicorp/go-metrics"
desc: not allowed, use github.com/hashicorp/go-metrics/compat instead
- pkg: "github.com/armon/go-metrics"
desc: not allowed, use github.com/hashicorp/go-metrics/compat instead
# SPDX-License-Identifier: BUSL-1.1

version: "2"
linters:
enable:
- depguard
settings:
depguard:
rules:
main:
list-mode: lax
files:
- ./sdk/**
allow:
- github.com/hashicorp/go-metrics/compat
deny:
- pkg: github.com/hashicorp/go-metrics
desc: not allowed, use github.com/hashicorp/go-metrics/compat instead
- pkg: github.com/armon/go-metrics
desc: not allowed, use github.com/hashicorp/go-metrics/compat instead
exclusions:
generated: lax
presets:
- comments
- common-false-positives
- legacy
- std-error-handling
paths:
- third_party$
- builtin$
- examples$
formatters:
exclusions:
generated: lax
paths:
- third_party$
- builtin$
- examples$
115 changes: 115 additions & 0 deletions tools/pipeline/internal/pkg/git/apply.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// 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
}
216 changes: 216 additions & 0 deletions tools/pipeline/internal/pkg/git/branch.go
5D39
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1

package git

import (
"context"
"fmt"
"strings"
)

// BranchTrack are supported branch tracking options
type BranchTrack = string

const (
BranchTrackDirect BranchTrack = "direct"
BranchTrackInherit BranchTrack = "inherit"
)

// BranchOpts are the git branch flags and arguments
// See: https://git-scm.com/docs/git-branch
type BranchOpts struct {
// Options
Abbrev uint // --abbrev=<n>
All bool // --all
Contains string // --contains <commit>
Copy bool // --copy
CreateReflog bool // --create-reflog
Delete bool // --delete
Force bool // -f
Format string // --format <format>
IgnoreCase bool // --ignore-case
List bool // --list
Merged string // --merged <commit>
Move bool // --move
NoAbbrev bool // --no-abbrev
NoColor bool // --no-color
NoColumn bool // --no-column
NoContains string // --no-contains <commit>
NoMerged string // --no-merged <commit>
NoTrack bool // --no-track
OmitEmpty bool // --omit-empty
PointsAt string // --points-at <object>
Remotes bool // --remotes
Quiet bool // --quiet
SetUpstream bool // --set-upstream
SetUpstreamTo string // --set-upstream-to=<upstream>
ShowCurrent bool // --show-current
Sort string // --sort=<key>
Track BranchTrack // --track
UnsetUpstream bool // --unset-upstream

// Targets. The branch command has several different modules. Set the correct
// targets depending on which combination of options you're setting.
BranchName string // <branchname>
StartPoint string // <start-point>
OldBranch string // <oldbranch>
NewBranch string // <newbranch>
Pattern []string // <pattern>
}

// Branch runs the git branch command
func (c *Client) Branch(ctx context.Context, opts *BranchOpts) (*ExecResponse, error) {
return c.Exec(ctx, "branch", opts)
}

// String returns the options as a string
func (o *BranchOpts) String() string {
return strings.Join(o.Strings(), " ")
}

// Strings returns the options as a string slice
func (o *BranchOpts) Strings() []string {
if o == nil {
return nil
}

opts := []string{}

if o.Abbrev > 0 {
opts = append(opts, fmt.Sprintf("--abbrev=%d", o.Abbrev))
}

if o.All {
opts = append(opts, "--all")
}

if o.Contains != "" {
opts = append(opts, fmt.Sprintf("--contains=%s", string(o.Contains)))
}

if o.Copy {
opts = append(opts, "--copy")
}

if o.CreateReflog {
opts = append(opts, "--create-reflog")
}

if o.Delete {
opts = append(opts, "--delete")
}

if o.Force {
opts = append(opts, "--force")
}

if o.Format != "" {
opts = append(opts, fmt.Sprintf("--format=%s", string(o.Format)))
}

if o.IgnoreCase {
opts = append(opts, "--ignore-case")
}

if o.List {
opts = append(opts, "--list")
}

if o.Merged != "" {
opts = append(opts, fmt.Sprintf("--merged=%s", string(o.Merged)))
}

if o.Move {
opts = append(opts, "--move")
}

if o.NoAbbrev {
opts = append(opts, "--no-abbrev")
}

if o.NoColor {
opts = append(opts, "--no-color")
}

if o.NoColumn {
opts = append(opts, "--no-column")
}

if o.NoTrack {
opts = append(opts, "--no-track")
}

if o.NoContains != "" {
opts = append(opts, fmt.Sprintf("--no-contains=%s", string(o.NoContains)))
}

if o.NoMerged != "" {
opts = append(opts, fmt.Sprintf("--no-merged=%s", string(o.NoMerged)))
}

if o.OmitEmpty {
opts = append(opts, "--omit-empty")
}

if o.PointsAt != "" {
opts = append(opts, fmt.Sprintf("--points-at=%s", string(o.PointsAt)))
}

if o.Quiet {
opts = append(opts, "--quiet")
}

if o.Remotes {
opts = append(opts, "--remotes")
}

if o.SetUpstream {
opts = append(opts, "--set-upstream")
}

if o.SetUpstreamTo != "" {
opts = append(opts, fmt.Sprintf("--set-upstream-to=%s", string(o.SetUpstreamTo)))
}

if o.ShowCurrent {
opts = append(opts, "--show-current")
}

if o.Sort != "" {
opts = append(opts, fmt.Sprintf("--sort=%s", string(o.Sort)))
}

if o.Track != "" {
opts = append(opts, fmt.Sprintf("--track=%s", string(o.Track)))
}

if o.UnsetUpstream {
opts = append(opts, "--unset-upstream")
}

// Not all of these can be used at once but we try to put them in an order
// where we won't cause problems if the correct flags and targets are set.

if o.BranchName != "" {
opts = append(opts, o.BranchName)
}

if o.OldBranch != "" {
opts = append(opts, o.OldBranch)
}

if o.NewBranch != "" {
opts = append(opts, o.NewBranch)
}

if o.StartPoint != "" {
opts = append(opts, o.StartPoint)
}

if len(o.Pattern) > 0 {
opts = append(opts, o.Pattern...)
}

return opts
}
Loading
Loading
0