Skip to content

Commit

Permalink
refactor: git package (zarf-dev#2790)
Browse files Browse the repository at this point in the history
Signed-off-by: Philip Laine <philip.laine@gmail.com>
Co-authored-by: Austin Abro <37223396+AustinAbro321@users.noreply.github.com>
Signed-off-by: Tim Seagren <timseagren@defenseunicorns.com>
  • Loading branch information
2 people authored and chaospuppy committed Aug 5, 2024
1 parent 22b55db commit 54f46c8
Show file tree
Hide file tree
Showing 17 changed files with 514 additions and 527 deletions.
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ require (
github.com/distribution/reference v0.5.0
github.com/fairwindsops/pluto/v5 v5.18.4
github.com/fatih/color v1.17.0
github.com/fluxcd/gitkit v0.6.0
github.com/fluxcd/helm-controller/api v1.0.1
github.com/fluxcd/pkg/apis/meta v1.5.0
github.com/fluxcd/source-controller/api v1.3.0
Expand Down Expand Up @@ -68,6 +69,7 @@ require (
require (
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/gofrs/uuid v4.2.0+incompatible // indirect
)

require (
Expand Down Expand Up @@ -247,7 +249,7 @@ require (
github.com/go-chi/chi v4.1.2+incompatible // indirect
github.com/go-errors/errors v1.4.2 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.5.0 // indirect
github.com/go-git/go-billy/v5 v5.5.0
github.com/go-gorp/gorp/v3 v3.1.0 // indirect
github.com/go-ini/ini v1.67.0 // indirect
github.com/go-jose/go-jose/v3 v3.0.3 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,8 @@ github.com/felixge/fgprof v0.9.3 h1:VvyZxILNuCiUCSXtPtYmmtGvb65nqXh2QFWc0Wpf2/g=
github.com/felixge/fgprof v0.9.3/go.mod h1:RdbpDgzqYVh/T9fPELJyV7EYJuHB55UTEULNun8eiPw=
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/fluxcd/gitkit v0.6.0 h1:iNg5LTx6ePo+Pl0ZwqHTAkhbUHxGVSY3YCxCdw7VIFg=
github.com/fluxcd/gitkit v0.6.0/go.mod h1:svOHuKi0fO9HoawdK4HfHAJJseZDHHjk7I3ihnCIqNo=
github.com/fluxcd/helm-controller/api v1.0.1 h1:Gn9qEVuif6D5+gHmVwTEZkR4+nmLOcOhKx4Sw2gL2EA=
github.com/fluxcd/helm-controller/api v1.0.1/go.mod h1:/6AD5a2qjo/ttxVM8GR33syLZwqigta60DCLdy8GrME=
github.com/fluxcd/pkg/apis/acl v0.3.0 h1:UOrKkBTOJK+OlZX7n8rWt2rdBmDCoTK+f5TY2LcZi8A=
Expand Down Expand Up @@ -841,6 +843,8 @@ github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk=
github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gofrs/flock v0.8.1 h1:+gYjHKf32LDeiEEFhQaotPbLuUXjY5ZqxKgXy7n59aw=
github.com/gofrs/flock v0.8.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU=
github.com/gofrs/uuid v4.2.0+incompatible h1:yyYWMnhkhrKwwr8gAOcOCYxOOscHgDS9yZgBrnJfGa0=
github.com/gofrs/uuid v4.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
Expand Down
2 changes: 1 addition & 1 deletion src/extensions/bigbang/bigbang.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ func findImagesforBBChartRepo(ctx context.Context, repo string, values chartutil
spinner := message.NewProgressSpinner("Discovering images in %s", repo)
defer spinner.Stop()

gitPath, err := helm.DownloadChartFromGitToTemp(ctx, repo, spinner)
gitPath, err := helm.DownloadChartFromGitToTemp(ctx, repo)
if err != nil {
return images, err
}
Expand Down
58 changes: 58 additions & 0 deletions src/internal/git/fallback.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors

// Package git contains functions for interacting with git repositories.
package git

import (
"context"
"io"

"github.com/go-git/go-git/v5/plumbing"

"github.com/zarf-dev/zarf/src/pkg/utils/exec"
)

// gitCloneFallback is a fallback if go-git fails to clone a repo.
func (r *Repository) gitCloneFallback(ctx context.Context, gitURL string, ref plumbing.ReferenceName, shallow bool) error {
// If we can't clone with go-git, fallback to the host clone
// Only support "all tags" due to the azure clone url format including a username
cloneArgs := []string{"clone", "--origin", onlineRemoteName, gitURL, r.path}

// Don't clone all tags / refs if we're cloning a specific tag or branch.
if ref.IsTag() || ref.IsBranch() {
cloneArgs = append(cloneArgs, "--no-tags")
cloneArgs = append(cloneArgs, "-b", ref.Short())
cloneArgs = append(cloneArgs, "--single-branch")
}

// If this is a shallow clone set the depth to 1
if shallow {
cloneArgs = append(cloneArgs, "--depth", "1")
}

cloneExecConfig := exec.Config{
Stdout: io.Discard,
Stderr: io.Discard,
}
_, _, err := exec.CmdWithContext(ctx, cloneExecConfig, "git", cloneArgs...)
if err != nil {
return err
}

// If we're cloning the whole repo, we need to also fetch the other branches besides the default.
if ref == emptyRef {
fetchArgs := []string{"fetch", "--tags", "--update-head-ok", onlineRemoteName, "refs/*:refs/*"}
fetchExecConfig := exec.Config{
Stdout: io.Discard,
Stderr: io.Discard,
Dir: r.path,
}
_, _, err := exec.CmdWithContext(ctx, fetchExecConfig, "git", fetchArgs...)
if err != nil {
return err
}
}

return nil
}
26 changes: 26 additions & 0 deletions src/internal/git/git.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors

// Package git contains functions for interacting with git repositories.
package git

import (
"fmt"
"strings"

"github.com/go-git/go-git/v5/plumbing"
)

const onlineRemoteName = "online-upstream"
const offlineRemoteName = "offline-downstream"
const emptyRef = ""

// ParseRef parses the provided ref into a ReferenceName if it's not a hash.
func ParseRef(r string) plumbing.ReferenceName {
// If not a full ref, assume it's a tag at this point.
if !plumbing.IsHash(r) && !strings.HasPrefix(r, "refs/") {
r = fmt.Sprintf("refs/tags/%s", r)
}
// Set the reference name to the provided ref.
return plumbing.ReferenceName(r)
}
40 changes: 40 additions & 0 deletions src/internal/git/git_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors

package git

import (
"testing"

"github.com/go-git/go-git/v5/plumbing"
"github.com/stretchr/testify/require"
)

func TestParseRef(t *testing.T) {
t.Parallel()

tests := []struct {
name string
refPlain string
expectedRef plumbing.ReferenceName
}{
{
name: "basic",
refPlain: "v1.0.0",
expectedRef: plumbing.ReferenceName("refs/tags/v1.0.0"),
},
{
name: "basic",
refPlain: "refs/heads/branchname",
expectedRef: plumbing.ReferenceName("refs/heads/branchname"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

ref := ParseRef(tt.refPlain)
require.Equal(t, tt.expectedRef, ref)
})
}
}
Loading

0 comments on commit 54f46c8

Please sign in to comment.