Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
| ... | ... |
@@ -32,7 +32,7 @@ func Clone(remoteURL string) (string, error) {
|
| 32 | 32 |
return cloneGitRepo(repo) |
| 33 | 33 |
} |
| 34 | 34 |
|
| 35 |
-func cloneGitRepo(repo gitRepo) (string, error) {
|
|
| 35 |
+func cloneGitRepo(repo gitRepo) (checkoutDir string, err error) {
|
|
| 36 | 36 |
fetch := fetchArgs(repo.remote, repo.ref) |
| 37 | 37 |
|
| 38 | 38 |
root, err := ioutil.TempDir("", "docker-build-git")
|
| ... | ... |
@@ -40,6 +40,12 @@ func cloneGitRepo(repo gitRepo) (string, error) {
|
| 40 | 40 |
return "", err |
| 41 | 41 |
} |
| 42 | 42 |
|
| 43 |
+ defer func() {
|
|
| 44 |
+ if err != nil {
|
|
| 45 |
+ os.RemoveAll(root) |
|
| 46 |
+ } |
|
| 47 |
+ }() |
|
| 48 |
+ |
|
| 43 | 49 |
if out, err := gitWithinDir(root, "init"); err != nil {
|
| 44 | 50 |
return "", errors.Wrapf(err, "failed to init repo at %s: %s", root, out) |
| 45 | 51 |
} |
| ... | ... |
@@ -54,7 +60,7 @@ func cloneGitRepo(repo gitRepo) (string, error) {
|
| 54 | 54 |
return "", errors.Wrapf(err, "error fetching: %s", output) |
| 55 | 55 |
} |
| 56 | 56 |
|
| 57 |
- root, err = checkoutGit(root, repo.ref, repo.subdir) |
|
| 57 |
+ checkoutDir, err = checkoutGit(root, repo.ref, repo.subdir) |
|
| 58 | 58 |
if err != nil {
|
| 59 | 59 |
return "", err |
| 60 | 60 |
} |
| ... | ... |
@@ -66,7 +72,7 @@ func cloneGitRepo(repo gitRepo) (string, error) {
|
| 66 | 66 |
return "", errors.Wrapf(err, "error initializing submodules: %s", output) |
| 67 | 67 |
} |
| 68 | 68 |
|
| 69 |
- return root, nil |
|
| 69 |
+ return checkoutDir, nil |
|
| 70 | 70 |
} |
| 71 | 71 |
|
| 72 | 72 |
func parseRemoteURL(remoteURL string) (gitRepo, error) {
|
| ... | ... |
@@ -234,17 +234,17 @@ func TestCheckoutGit(t *testing.T) {
|
| 234 | 234 |
if c.fail {
|
| 235 | 235 |
assert.Error(t, err) |
| 236 | 236 |
continue |
| 237 |
- } else {
|
|
| 237 |
+ } |
|
| 238 |
+ require.NoError(t, err) |
|
| 239 |
+ defer os.RemoveAll(r) |
|
| 240 |
+ if c.submodule {
|
|
| 241 |
+ b, err := ioutil.ReadFile(filepath.Join(r, "sub/subfile")) |
|
| 238 | 242 |
require.NoError(t, err) |
| 239 |
- if c.submodule {
|
|
| 240 |
- b, err := ioutil.ReadFile(filepath.Join(r, "sub/subfile")) |
|
| 241 |
- require.NoError(t, err) |
|
| 242 |
- assert.Equal(t, "subcontents", string(b)) |
|
| 243 |
- } else {
|
|
| 244 |
- _, err := os.Stat(filepath.Join(r, "sub/subfile")) |
|
| 245 |
- require.Error(t, err) |
|
| 246 |
- require.True(t, os.IsNotExist(err)) |
|
| 247 |
- } |
|
| 243 |
+ assert.Equal(t, "subcontents", string(b)) |
|
| 244 |
+ } else {
|
|
| 245 |
+ _, err := os.Stat(filepath.Join(r, "sub/subfile")) |
|
| 246 |
+ require.Error(t, err) |
|
| 247 |
+ require.True(t, os.IsNotExist(err)) |
|
| 248 | 248 |
} |
| 249 | 249 |
|
| 250 | 250 |
b, err := ioutil.ReadFile(filepath.Join(r, "Dockerfile")) |