Merge pull request #3306 from thatsmydoing/single-fetch

Don't fetch default branch if ref is specified
This commit is contained in:
Jeff Regan
2020-12-07 13:10:26 -08:00
committed by GitHub

View File

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