From 612346ebc0efce2470eac647e6e769e30e232f3d Mon Sep 17 00:00:00 2001 From: Minimind <882485+jeff-mccoy@users.noreply.github.com> Date: Mon, 2 May 2022 16:03:35 -0500 Subject: [PATCH] Fallback to host-provided git on clone failure during package create (#471) --- examples/gitops-data/zarf.yaml | 4 +++- src/internal/git/pull.go | 22 ++++++++++++++++++---- src/internal/git/push.go | 6 ++++-- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/examples/gitops-data/zarf.yaml b/examples/gitops-data/zarf.yaml index fc67d1493a..e90411a995 100644 --- a/examples/gitops-data/zarf.yaml +++ b/examples/gitops-data/zarf.yaml @@ -11,7 +11,9 @@ components: repos: # Do a tag-provided Git Repo mirror - https://github.com/defenseunicorns/zarf.git@v0.15.0 - # 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/twistlock.git@0.0.9-bb.0 # 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://me0515@dev.azure.com/me0515/zarf-public-test/_git/zarf-public-test diff --git a/src/internal/git/pull.go b/src/internal/git/pull.go index 1c07eaea8b..7f566b4557 100644 --- a/src/internal/git/pull.go +++ b/src/internal/git/pull.go @@ -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" @@ -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) @@ -54,7 +56,20 @@ func pull(gitUrl string, targetFolder string, spinner *message.Spinner) { if err == git.ErrRepositoryAlreadyExists { spinner.Debugf("Repo already cloned") } else if err != nil { - spinner.Fatalf(err, "Not a valid git repo or unable to clone") + spinner.Debugf("Failed to clone repo: %s", err) + 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 { @@ -80,6 +95,5 @@ func pull(gitUrl string, targetFolder string, spinner *message.Spinner) { fetchTag(targetFolder, tag) CheckoutTagAsBranch(targetFolder, tag, trunkBranchName) - } } diff --git a/src/internal/git/push.go b/src/internal/git/push.go index 76b152d5cd..948006cf19 100644 --- a/src/internal/git/push.go +++ b/src/internal/git/push.go @@ -2,6 +2,7 @@ package git import ( "fmt" + "path/filepath" "strings" "github.com/defenseunicorns/zarf/src/config" @@ -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