gitutils: remove checkout directory on error

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
Tonis Tiigi
2017-12-08 11:51:10 -08:00
parent e2cc22d076
commit 7bc503344a
2 changed files with 19 additions and 13 deletions

View File

@ -32,7 +32,7 @@ func Clone(remoteURL string) (string, error) {
return cloneGitRepo(repo)
}
func cloneGitRepo(repo gitRepo) (string, error) {
func cloneGitRepo(repo gitRepo) (checkoutDir string, err error) {
fetch := fetchArgs(repo.remote, repo.ref)
root, err := ioutil.TempDir("", "docker-build-git")
@ -40,6 +40,12 @@ func cloneGitRepo(repo gitRepo) (string, error) {
return "", err
}
defer func() {
if err != nil {
os.RemoveAll(root)
}
}()
if out, err := gitWithinDir(root, "init"); err != nil {
return "", errors.Wrapf(err, "failed to init repo at %s: %s", root, out)
}
@ -54,7 +60,7 @@ func cloneGitRepo(repo gitRepo) (string, error) {
return "", errors.Wrapf(err, "error fetching: %s", output)
}
root, err = checkoutGit(root, repo.ref, repo.subdir)
checkoutDir, err = checkoutGit(root, repo.ref, repo.subdir)
if err != nil {
return "", err
}
@ -66,7 +72,7 @@ func cloneGitRepo(repo gitRepo) (string, error) {
return "", errors.Wrapf(err, "error initializing submodules: %s", output)
}
return root, nil
return checkoutDir, nil
}
func parseRemoteURL(remoteURL string) (gitRepo, error) {