diff --git a/cmd/clone.go b/cmd/clone.go index 73c30bb..80f1c63 100644 --- a/cmd/clone.go +++ b/cmd/clone.go @@ -756,6 +756,25 @@ func CloneAllRepos(git git.Gitter, cloneTargets []scm.Repo) { } } else { + if os.Getenv("GHORG_FETCH_ALL") == "true" { + err = git.FetchAll(repo) + + if err != nil { + e := fmt.Sprintf("Could not fetch remotes: %s Error: %v", repo.URL, err) + cloneErrors = append(cloneErrors, e) + return + } + } else { + err = git.FetchCloneBranch(repo) + + if err != nil { + e := fmt.Sprintf("Could not fetch remote: %s Error: %v", repo.URL, err) + cloneErrors = append(cloneErrors, e) + return + } + } + + err := git.Checkout(repo) if err != nil { e := fmt.Sprintf("Could not checkout out %s, branch may not exist or may not have any contents, no changes made on: %s Error: %v", repo.CloneBranch, repo.URL, err) @@ -789,16 +808,6 @@ func CloneAllRepos(git git.Gitter, cloneTargets []scm.Repo) { action = "pulling" pulledCount++ - - if os.Getenv("GHORG_FETCH_ALL") == "true" { - err = git.FetchAll(repo) - - if err != nil { - e := fmt.Sprintf("Could not fetch remotes: %s Error: %v", repo.URL, err) - cloneErrors = append(cloneErrors, e) - return - } - } } err = git.SetOrigin(repo) diff --git a/cmd/clone_test.go b/cmd/clone_test.go index 00ddc1a..929c3f8 100644 --- a/cmd/clone_test.go +++ b/cmd/clone_test.go @@ -97,6 +97,10 @@ func (g MockGitClient) FetchAll(repo scm.Repo) error { return nil } +func (g MockGitClient) FetchCloneBranch(repo scm.Repo) error { + return nil +} + func TestInitialClone(t *testing.T) { defer UnsetEnv("GHORG_")() dir, err := os.MkdirTemp("", "ghorg_test_initial") diff --git a/git/git.go b/git/git.go index c7f8e4a..d40bd9c 100644 --- a/git/git.go +++ b/git/git.go @@ -19,6 +19,7 @@ type Gitter interface { Checkout(scm.Repo) error UpdateRemote(scm.Repo) error FetchAll(scm.Repo) error + FetchCloneBranch(scm.Repo) error } type GitClient struct{} @@ -175,3 +176,19 @@ func (g GitClient) FetchAll(repo scm.Repo) error { } return cmd.Run() } + +func (g GitClient) FetchCloneBranch(repo scm.Repo) error { + args := []string{"fetch", "origin", repo.CloneBranch} + + if os.Getenv("GHORG_CLONE_DEPTH") != "" { + index := 1 + args = append(args[:index+1], args[index:]...) + args[index] = fmt.Sprintf("--depth=%v", os.Getenv("GHORG_CLONE_DEPTH")) + } + cmd := exec.Command("git", args...) + cmd.Dir = repo.HostPath + if os.Getenv("GHORG_DEBUG") != "" { + return printDebugCmd(cmd, repo) + } + return cmd.Run() +}