Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify git cloner logic #2080

Merged
merged 1 commit into from
Jan 13, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 13 additions & 74 deletions api/internal/git/cloner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down