mirror of
https://github.com/outbackdingo/ghorg.git
synced 2026-01-27 10:19:03 +00:00
Prevent checkout failure on default branch change (#437)
This commit is contained in:
29
cmd/clone.go
29
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)
|
||||
|
||||
@@ -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")
|
||||
|
||||
17
git/git.go
17
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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user