Prevent checkout failure on default branch change (#437)

This commit is contained in:
Darren Gibson
2024-07-16 21:00:40 -05:00
committed by GitHub
parent a8155223af
commit 150e326a65
3 changed files with 40 additions and 10 deletions

View File

@@ -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)

View File

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

View File

@@ -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()
}