Improve RemoveRepo implementation

This commit is contained in:
Amolith 2024-02-29 15:51:42 -05:00
parent dde4c97802
commit 9a91d4f2ea
Signed by: Amolith
SSH Key Fingerprint: SHA256:JBKEeoO/72Fz03rtlzeO49PATFT2maMancH3opcT0h0
1 changed files with 9 additions and 29 deletions

View File

@ -7,7 +7,6 @@ package git
import ( import (
"errors" "errors"
"fmt" "fmt"
"io"
"net/url" "net/url"
"os" "os"
"strings" "strings"
@ -149,42 +148,23 @@ func RemoveRepo(url string) (err error) {
return err return err
} }
// TODO: Check whether the two parent directories are empty and remove them if path = path[:strings.LastIndex(path, "/")]
// so dirs := strings.Split(path, "/")
for i := 0; i < 2; i++ {
path = strings.TrimSuffix(path, "/") for range dirs {
if path == "data" { if path == "data" {
break break
} }
empty, err := dirEmpty(path) err = os.Remove(path)
if err != nil { 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.
if empty { break
err = os.Remove(path)
if err != nil {
return err
}
} }
path = path[:strings.LastIndex(path, "/")] path = path[:strings.LastIndex(path, "/")]
} }
return err return nil
}
// 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
} }
// stringifyRepo accepts a repository URI string and the corresponding local // stringifyRepo accepts a repository URI string and the corresponding local