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

Fallback to host-provided git on clone failure during package create #471

Merged
merged 5 commits into from
May 2, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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 examples/gitops-data/zarf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ components:
repos:
# Do a tag-provided Git Repo mirror
- https://github.com/defenseunicorns/[email protected]
# Do a tag-provided Git Repo mirror with the default branch of main
# Do a tag-provided Git Repo mirror with the default branch of main
- https://repo1.dso.mil/platform-one/big-bang/apps/security-tools/[email protected]
# Do a full Git Repo Mirror
- https://github.com/stefanprodan/podinfo.git
# Clone an azure repo that breaks in go-git and has to fall back to the host git
- https://[email protected]/me0515/zarf-public-test/_git/zarf-public-test
21 changes: 17 additions & 4 deletions src/internal/git/pull.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package git

import (
"context"

"github.com/defenseunicorns/zarf/src/internal/message"
"github.com/defenseunicorns/zarf/src/internal/utils"
"github.com/go-git/go-git/v5"
Expand All @@ -20,13 +22,13 @@ func DownloadRepoToTemp(gitUrl string, spinner *message.Spinner) string {
return path
}

func Pull(gitUrl string, targetFolder string, spinner *message.Spinner) string {
func Pull(gitUrl, targetFolder string, spinner *message.Spinner) string {
path := targetFolder + "/" + transformURLtoRepoName(gitUrl)
pull(gitUrl, path, spinner)
return path
}

func pull(gitUrl string, targetFolder string, spinner *message.Spinner) {
func pull(gitUrl, targetFolder string, spinner *message.Spinner) {
spinner.Updatef("Processing git repo %s", gitUrl)

gitCred := FindAuthForHost(gitUrl)
Expand Down Expand Up @@ -54,7 +56,19 @@ func pull(gitUrl string, targetFolder string, spinner *message.Spinner) {
if err == git.ErrRepositoryAlreadyExists {
spinner.Debugf("Repo already cloned")
} else if err != nil {
YrrepNoj marked this conversation as resolved.
Show resolved Hide resolved
spinner.Fatalf(err, "Not a valid git repo or unable to clone")
message.Infof("Falling back to host git for %s", gitUrl)

// 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
stdOut, stdErr, err := utils.ExecCommandWithContext(context.TODO(), false, "git", "clone", "--origin", onlineRemoteName, gitUrl, targetFolder)
spinner.Updatef(stdOut)
spinner.Debugf(stdErr)

if err != nil {
spinner.Fatalf(err, "Not a valid git repo or unable to clone")
}

return
}

if !fetchAllTags {
Expand All @@ -80,6 +94,5 @@ func pull(gitUrl string, targetFolder string, spinner *message.Spinner) {

fetchTag(targetFolder, tag)
CheckoutTagAsBranch(targetFolder, tag, trunkBranchName)

}
}
6 changes: 4 additions & 2 deletions src/internal/git/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package git

import (
"fmt"
"path/filepath"
"strings"

"github.com/defenseunicorns/zarf/src/config"
Expand Down Expand Up @@ -30,9 +31,10 @@ func PushAllDirectories(localPath string) {
defer spinner.Stop()

for _, path := range paths {
spinner.Updatef("Pushing git repo %s", localPath)
basename := filepath.Base(path)
spinner.Updatef("Pushing git repo %s", basename)
if err := push(path, spinner); err != nil {
spinner.Fatalf(err, "Unable to push the git repo %s", localPath)
spinner.Fatalf(err, "Unable to push the git repo %s", basename)
}

// Add the read-only user to this repo
Expand Down