Skip to content

Commit

Permalink
Merge pull request #857 from pst/fastergitclone
Browse files Browse the repository at this point in the history
Reduce time required for cloning remote bases
  • Loading branch information
k8s-ci-robot authored Mar 7, 2019
2 parents a6f6514 + 4f429d6 commit a59577c
Showing 1 changed file with 42 additions and 5 deletions.
47 changes: 42 additions & 5 deletions pkg/git/cloner.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,61 @@ func ClonerUsingGitExec(repoSpec *RepoSpec) error {
}
cmd := exec.Command(
gitProgram,
"clone",
repoSpec.CloneSpec(),
"init",
repoSpec.cloneDir.String())
var out bytes.Buffer
cmd.Stdout = &out
err = cmd.Run()
if err != nil {
return errors.Wrapf(err, "trouble cloning %s", repoSpec.raw)
return errors.Wrapf(
err,
"trouble initializing empty git repo in %s",
repoSpec.cloneDir.String())
}

cmd = exec.Command(
gitProgram,
"remote",
"add",
"origin",
repoSpec.CloneSpec())
cmd.Stdout = &out
cmd.Dir = repoSpec.cloneDir.String()
err = cmd.Run()
if err != nil {
return errors.Wrapf(
err,
"trouble adding remote %s",
repoSpec.CloneSpec())
}

if repoSpec.ref == "" {
return nil
}
cmd = exec.Command(gitProgram, "checkout", repoSpec.ref)
cmd = exec.Command(
gitProgram,
"fetch",
"--depth=1",
"origin",
repoSpec.ref)
cmd.Stdout = &out
cmd.Dir = repoSpec.cloneDir.String()
err = cmd.Run()
if err != nil {
return errors.Wrapf(err, "trouble fetching %s", repoSpec.ref)
}

cmd = exec.Command(
gitProgram,
"reset",
"--hard",
"FETCH_HEAD")
cmd.Stdout = &out
cmd.Dir = repoSpec.cloneDir.String()
err = cmd.Run()
if err != nil {
return errors.Wrapf(
err, "trouble checking out href %s", repoSpec.ref)
err, "trouble hard resetting empty repository to %s", repoSpec.ref)
}
return nil
}
Expand Down

0 comments on commit a59577c

Please sign in to comment.