From 9a91d4f2ea8637e48211c1053f7bc0c341e0288c Mon Sep 17 00:00:00 2001 From: Amolith Date: Thu, 29 Feb 2024 15:51:42 -0500 Subject: [PATCH] Improve RemoveRepo implementation --- git/git.go | 38 +++++++++----------------------------- 1 file changed, 9 insertions(+), 29 deletions(-) diff --git a/git/git.go b/git/git.go index 9464bfb..8a4d9aa 100644 --- a/git/git.go +++ b/git/git.go @@ -7,7 +7,6 @@ package git import ( "errors" "fmt" - "io" "net/url" "os" "strings" @@ -149,42 +148,23 @@ func RemoveRepo(url string) (err error) { return err } - // TODO: Check whether the two parent directories are empty and remove them if - // so - for i := 0; i < 2; i++ { - path = strings.TrimSuffix(path, "/") + path = path[:strings.LastIndex(path, "/")] + dirs := strings.Split(path, "/") + + for range dirs { if path == "data" { break } - empty, err := dirEmpty(path) + err = os.Remove(path) if err != nil { - return err - } - if empty { - err = os.Remove(path) - if err != nil { - return err - } + // This folder likely has data, so might as well save some time by + // not checking the parents we can't delete anyway. + break } path = path[:strings.LastIndex(path, "/")] } - return err -} - -// dirEmpty checks if a directory is empty. -func dirEmpty(name string) (empty bool, err error) { - f, err := os.Open(name) - if err != nil { - return false, err - } - defer f.Close() - - _, err = f.Readdirnames(1) - if err == io.EOF { - return true, nil - } - return false, err + return nil } // stringifyRepo accepts a repository URI string and the corresponding local