Don't fetch default branch if ref is specified

Currently, we always fetch the default branch with an initial git clone
and then fetch the ref after if it's specified. This changes it to only
make one fetch instead of two if a ref is specified.
This commit is contained in:
Thomas Dy
2020-12-01 18:28:20 +09:00
parent 35a19fb8a9
commit 6445e03d1a

View File

@@ -29,43 +29,57 @@ func ClonerUsingGitExec(repoSpec *RepoSpec) error {
cmd := exec.Command(
gitProgram,
"clone",
"--depth=1",
repoSpec.CloneSpec(),
"init",
repoSpec.Dir.String())
out, err := cmd.CombinedOutput()
if err != nil {
log.Printf("Error cloning git repo: %s", out)
log.Printf("Error initializing git repo: %s", out)
return errors.Wrapf(
err,
"trouble cloning git repo %v in %s",
repoSpec.CloneSpec(), repoSpec.Dir.String())
"trouble initializing git repo in %s",
repoSpec.Dir.String())
}
if repoSpec.Ref != "" {
cmd = exec.Command(
gitProgram,
"fetch",
"--depth=1",
"origin",
repoSpec.Ref)
cmd.Dir = repoSpec.Dir.String()
out, err = cmd.CombinedOutput()
if err != nil {
log.Printf("Error fetching ref: %s", out)
return errors.Wrapf(err, "trouble fetching %s", repoSpec.Ref)
}
cmd = exec.Command(
gitProgram,
"remote",
"add",
"origin",
repoSpec.CloneSpec())
cmd.Dir = repoSpec.Dir.String()
out, err = cmd.CombinedOutput()
if err != nil {
log.Printf("Error adding remote: %s", out)
return errors.Wrapf(err, "trouble adding remote %s", repoSpec.CloneSpec())
}
cmd = exec.Command(
gitProgram,
"checkout",
"FETCH_HEAD")
cmd.Dir = repoSpec.Dir.String()
out, err = cmd.CombinedOutput()
if err != nil {
log.Printf("Error checking out ref: %s", out)
return errors.Wrapf(err, "trouble checking out %s", repoSpec.Ref)
}
ref := "HEAD"
if repoSpec.Ref != "" {
ref = repoSpec.Ref
}
cmd = exec.Command(
gitProgram,
"fetch",
"--depth=1",
"origin",
ref)
cmd.Dir = repoSpec.Dir.String()
out, err = cmd.CombinedOutput()
if err != nil {
log.Printf("Error fetching ref: %s", out)
return errors.Wrapf(err, "trouble fetching %s", ref)
}
cmd = exec.Command(
gitProgram,
"checkout",
"FETCH_HEAD")
cmd.Dir = repoSpec.Dir.String()
out, err = cmd.CombinedOutput()
if err != nil {
log.Printf("Error checking out ref: %s", out)
return errors.Wrapf(err, "trouble checking out %s", ref)
}
cmd = exec.Command(