6
0
mirror of https://github.com/grdl/git-get.git synced 2026-02-04 20:54:41 +00:00

Cleanup empty directories created by a failed git clone

This commit is contained in:
Grzegorz Dlugoszewski
2020-07-30 20:31:06 +02:00
parent b2d1d773f7
commit 5fe2ddb74f
4 changed files with 98 additions and 3 deletions

View File

@@ -4,6 +4,8 @@ import (
"fmt"
"git-get/pkg/run"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
)
@@ -54,6 +56,7 @@ func Clone(opts *CloneOpts) (*Repo, error) {
}
if err != nil {
cleanupFailedClone(opts.Path)
return nil, err
}
@@ -183,3 +186,21 @@ func (r *Repo) Remote() (string, error) {
func (r *Repo) Path() string {
return r.path
}
// cleanupFailedClone removes empty directories created by git when coning failed.
// Git itself will delete the final repo directory if a clone failed,
// but it won't delete all the parent dirs that it created when cloning the repo.
// eg:
// When operation like `git clone https://github.com/grdl/git-get /tmp/some/temp/dir/git-get` fails,
// git will only delete the final `git-get` dir in the path an will leave /tmp/some/temp/dir even if it just created them.
//
// os.Remove will only delete an empty dir so we traverse the path "upwards" and delete all directories
// until a non-empty one is reached.
func cleanupFailedClone(path string) {
for {
path = filepath.Dir(path)
if err := os.Remove(path); err != nil {
return
}
}
}

View File

@@ -1,9 +1,14 @@
package git
import (
"fmt"
"git-get/pkg/git/test"
"os"
"path/filepath"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func TestUncommitted(t *testing.T) {
@@ -299,3 +304,70 @@ func TestAheadBehind(t *testing.T) {
})
}
}
func TestCleanupFailedClone(t *testing.T) {
// Test dir structure:
// root
// └── a/
// ├── b/
// │ └── c/
// └── x/
// └── y/
// └── file.txt
tests := []struct {
path string // path to cleanup
wantGone string // this path should be deleted, if empty - nothing should be deleted
wantStay string // this path shouldn't be deleted
}{
{
path: "a/b/c/repo",
wantGone: "a/b/c/repo",
wantStay: "a",
}, {
path: "a/b/c/repo",
wantGone: "a/b",
wantStay: "a",
}, {
path: "a/b/repo",
wantGone: "",
wantStay: "a/b/c",
}, {
path: "a/x/y/repo",
wantGone: "",
wantStay: "a/x/y",
},
}
for i, test := range tests {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
root := createTestDirTree(t)
path := filepath.Join(root, test.path)
cleanupFailedClone(path)
if test.wantGone != "" {
wantGone := filepath.Join(root, test.wantGone)
assert.NoDirExists(t, wantGone, "%s dir should be deleted during the cleanup", wantGone)
}
if test.wantStay != "" {
wantLeft := filepath.Join(root, test.wantStay)
assert.DirExists(t, wantLeft, "%s dir should not be deleted during the cleanup", wantLeft)
}
})
}
}
func createTestDirTree(t *testing.T) string {
root := test.TempDir(t, "")
err := os.MkdirAll(filepath.Join(root, "a", "b", "c"), os.ModePerm)
err = os.MkdirAll(filepath.Join(root, "a", "x", "y"), os.ModePerm)
_, err = os.Create(filepath.Join(root, "a", "x", "y", "file.txt"))
if err != nil {
t.Fatal(err)
}
return root
}