Move duplicated functions (#33977)

Remove duplicated functions `IsExist`, `IsFile` and `IsDir` in package
`modules/git` and use the exists functions in `modules/util`.
pull/34013/head^2
Lunny Xiao 2025-03-25 07:40:31 -07:00 committed by GitHub
parent 356b707dde
commit d6e94fa4e4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 35 additions and 35 deletions

View File

@ -51,15 +51,26 @@ func GetHook(repoPath, name string) (*Hook, error) {
name: name,
path: filepath.Join(repoPath, "hooks", name+".d", name),
}
samplePath := filepath.Join(repoPath, "hooks", name+".sample")
if isFile(h.path) {
isFile, err := util.IsFile(h.path)
if err != nil {
return nil, err
}
if isFile {
data, err := os.ReadFile(h.path)
if err != nil {
return nil, err
}
h.IsActive = true
h.Content = string(data)
} else if isFile(samplePath) {
return h, nil
}
samplePath := filepath.Join(repoPath, "hooks", name+".sample")
isFile, err = util.IsFile(samplePath)
if err != nil {
return nil, err
}
if isFile {
data, err := os.ReadFile(samplePath)
if err != nil {
return nil, err
@ -77,7 +88,11 @@ func (h *Hook) Name() string {
// Update updates hook settings.
func (h *Hook) Update() error {
if len(strings.TrimSpace(h.Content)) == 0 {
if isExist(h.path) {
exist, err := util.IsExist(h.path)
if err != nil {
return err
}
if exist {
err := util.Remove(h.path)
if err != nil {
return err
@ -101,7 +116,10 @@ func (h *Hook) Update() error {
// ListHooks returns a list of Git hooks of given repository.
func ListHooks(repoPath string) (_ []*Hook, err error) {
if !isDir(filepath.Join(repoPath, "hooks")) {
exist, err := util.IsDir(filepath.Join(repoPath, "hooks"))
if err != nil {
return nil, err
} else if !exist {
return nil, errors.New("hooks path does not exist")
}

View File

@ -49,7 +49,12 @@ func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) {
repoPath, err := filepath.Abs(repoPath)
if err != nil {
return nil, err
} else if !isDir(repoPath) {
}
exist, err := util.IsDir(repoPath)
if err != nil {
return nil, err
}
if !exist {
return nil, util.NewNotExistErrorf("no such file or directory")
}

View File

@ -47,7 +47,12 @@ func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) {
repoPath, err := filepath.Abs(repoPath)
if err != nil {
return nil, err
} else if !isDir(repoPath) {
}
exist, err := util.IsDir(repoPath)
if err != nil {
return nil, err
}
if !exist {
return nil, util.NewNotExistErrorf("no such file or directory")
}

View File

@ -8,7 +8,6 @@ import (
"encoding/hex"
"fmt"
"io"
"os"
"strconv"
"strings"
"sync"
@ -41,33 +40,6 @@ func (oc *ObjectCache[T]) Get(id string) (T, bool) {
return obj, has
}
// isDir returns true if given path is a directory,
// or returns false when it's a file or does not exist.
func isDir(dir string) bool {
f, e := os.Stat(dir)
if e != nil {
return false
}
return f.IsDir()
}
// isFile returns true if given path is a file,
// or returns false when it's a directory or does not exist.
func isFile(filePath string) bool {
f, e := os.Stat(filePath)
if e != nil {
return false
}
return !f.IsDir()
}
// isExist checks whether a file or directory exists.
// It returns false when the file or directory does not exist.
func isExist(path string) bool {
_, err := os.Stat(path)
return err == nil || os.IsExist(err)
}
// ConcatenateError concatenats an error with stderr string
func ConcatenateError(err error, stderr string) error {
if len(stderr) == 0 {