Skip to content

Commit

Permalink
Fallback to host-provided git on clone failure during package create (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jeff-mccoy authored May 2, 2022
1 parent 6dd158c commit 612346e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
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
22 changes: 18 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,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 {
Expand All @@ -80,6 +95,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

0 comments on commit 612346e

Please sign in to comment.