Skip to content
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

refactor: git package #2790

Merged
merged 2 commits into from
Aug 1, 2024
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
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 @@
spinner := message.NewProgressSpinner("Discovering images in %s", repo)
defer spinner.Stop()

gitPath, err := helm.DownloadChartFromGitToTemp(ctx, repo, spinner)
gitPath, err := helm.DownloadChartFromGitToTemp(ctx, repo)

Check warning on line 536 in src/extensions/bigbang/bigbang.go

View check run for this annotation

Codecov / codecov/patch

src/extensions/bigbang/bigbang.go#L536

Added line #L536 was not covered by tests
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 {

Check warning on line 17 in src/internal/git/fallback.go

View check run for this annotation

Codecov / codecov/patch

src/internal/git/fallback.go#L17

Added line #L17 was not covered by tests
// 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}

Check warning on line 20 in src/internal/git/fallback.go

View check run for this annotation

Codecov / codecov/patch

src/internal/git/fallback.go#L20

Added line #L20 was not covered by tests

// 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")

Check warning on line 26 in src/internal/git/fallback.go

View check run for this annotation

Codecov / codecov/patch

src/internal/git/fallback.go#L23-L26

Added lines #L23 - L26 were not covered by tests
}

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

Check warning on line 31 in src/internal/git/fallback.go

View check run for this annotation

Codecov / codecov/patch

src/internal/git/fallback.go#L30-L31

Added lines #L30 - L31 were not covered by tests
}

cloneExecConfig := exec.Config{
Stdout: io.Discard,
Stderr: io.Discard,

Check warning on line 36 in src/internal/git/fallback.go

View check run for this annotation

Codecov / codecov/patch

src/internal/git/fallback.go#L34-L36

Added lines #L34 - L36 were not covered by tests
}
_, _, err := exec.CmdWithContext(ctx, cloneExecConfig, "git", cloneArgs...)
if err != nil {
return err

Check warning on line 40 in src/internal/git/fallback.go

View check run for this annotation

Codecov / codecov/patch

src/internal/git/fallback.go#L38-L40

Added lines #L38 - L40 were not covered by tests
}

// 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,

Check warning on line 49 in src/internal/git/fallback.go

View check run for this annotation

Codecov / codecov/patch

src/internal/git/fallback.go#L44-L49

Added lines #L44 - L49 were not covered by tests
}
_, _, err := exec.CmdWithContext(ctx, fetchExecConfig, "git", fetchArgs...)
if err != nil {
return err

Check warning on line 53 in src/internal/git/fallback.go

View check run for this annotation

Codecov / codecov/patch

src/internal/git/fallback.go#L51-L53

Added lines #L51 - L53 were not covered by tests
}
}

return nil

Check warning on line 57 in src/internal/git/fallback.go

View check run for this annotation

Codecov / codecov/patch

src/internal/git/fallback.go#L57

Added line #L57 was not covered by tests
}
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