diff --git a/api/internal/git/cloner.go b/api/internal/git/cloner.go index 6716195556..63fcaa21e8 100644 --- a/api/internal/git/cloner.go +++ b/api/internal/git/cloner.go @@ -27,90 +27,28 @@ func ClonerUsingGitExec(repoSpec *RepoSpec) error { if err != nil { return err } - cmd := exec.Command( - gitProgram, - "init", - repoSpec.Dir.String()) - var out bytes.Buffer - cmd.Stdout = &out - cmd.Stderr = &out - err = cmd.Run() - if err != nil { - log.Printf("Error initializing empty git repo: %s", out.String()) - return errors.Wrapf( - err, - "trouble initializing empty git repo in %s", - repoSpec.Dir.String()) - } - cmd = exec.Command( - gitProgram, - "remote", - "add", - "origin", - repoSpec.CloneSpec()) - cmd.Stdout = &out - cmd.Stderr = &out - cmd.Dir = repoSpec.Dir.String() - err = cmd.Run() - if err != nil { - log.Printf("Error setting git remote: %s", out.String()) - return errors.Wrapf( - err, - "trouble adding remote %s", - repoSpec.CloneSpec()) - } if repoSpec.Ref == "" { repoSpec.Ref = "master" } - cmd = exec.Command( + cmd := exec.Command( gitProgram, - "fetch", + "clone", "--depth=1", - "origin", - repoSpec.Ref) - cmd.Stdout = &out - cmd.Stderr = &out - cmd.Dir = repoSpec.Dir.String() - err = cmd.Run() - if err != nil { - cmd = exec.Command( - gitProgram, - "pull", - "origin", - "master") - var out bytes.Buffer - cmd.Stdout = &out - cmd.Dir = repoSpec.Dir.String() - err := cmd.Run() - if err != nil { - return errors.Wrapf(err, "trouble pulling %s", repoSpec.OrgRepo) - } - if repoSpec.Ref == "" { - repoSpec.Ref = "master" - } - cmd = exec.Command(gitProgram, "checkout", repoSpec.Ref) - cmd.Dir = repoSpec.Dir.String() - err = cmd.Run() - if err != nil { - return errors.Wrapf( - err, "trouble checking out href %s", repoSpec.Ref) - } - } - - cmd = exec.Command( - gitProgram, - "reset", - "--hard", - "FETCH_HEAD") + repoSpec.CloneSpec(), + "-b", + repoSpec.Ref, + repoSpec.Dir.String()) + var out bytes.Buffer cmd.Stdout = &out cmd.Stderr = &out - cmd.Dir = repoSpec.Dir.String() err = cmd.Run() if err != nil { - log.Printf("Error performing git reset: %s", out.String()) + log.Printf("Error cloning git repo: %s", out.String()) return errors.Wrapf( - err, "trouble hard resetting empty repository to %s", repoSpec.Ref) + err, + "trouble cloning git repo %v in %s", + repoSpec.CloneSpec(), repoSpec.Dir.String()) } cmd = exec.Command( @@ -120,10 +58,11 @@ func ClonerUsingGitExec(repoSpec *RepoSpec) error { "--init", "--recursive") cmd.Stdout = &out + cmd.Stderr = &out cmd.Dir = repoSpec.Dir.String() err = cmd.Run() if err != nil { - return errors.Wrapf(err, "trouble fetching submodules for %s", repoSpec.Ref) + return errors.Wrapf(err, "trouble fetching submodules for %s", repoSpec.CloneSpec()) } return nil