Skip to content

Commit d6e94fa

Browse files
authored
Move duplicated functions (#33977)
Remove duplicated functions `IsExist`, `IsFile` and `IsDir` in package `modules/git` and use the exists functions in `modules/util`.
1 parent 356b707 commit d6e94fa

File tree

4 files changed

+35
-35
lines changed

4 files changed

+35
-35
lines changed

modules/git/hook.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,26 @@ func GetHook(repoPath, name string) (*Hook, error) {
5151
name: name,
5252
path: filepath.Join(repoPath, "hooks", name+".d", name),
5353
}
54-
samplePath := filepath.Join(repoPath, "hooks", name+".sample")
55-
if isFile(h.path) {
54+
isFile, err := util.IsFile(h.path)
55+
if err != nil {
56+
return nil, err
57+
}
58+
if isFile {
5659
data, err := os.ReadFile(h.path)
5760
if err != nil {
5861
return nil, err
5962
}
6063
h.IsActive = true
6164
h.Content = string(data)
62-
} else if isFile(samplePath) {
65+
return h, nil
66+
}
67+
68+
samplePath := filepath.Join(repoPath, "hooks", name+".sample")
69+
isFile, err = util.IsFile(samplePath)
70+
if err != nil {
71+
return nil, err
72+
}
73+
if isFile {
6374
data, err := os.ReadFile(samplePath)
6475
if err != nil {
6576
return nil, err
@@ -77,7 +88,11 @@ func (h *Hook) Name() string {
7788
// Update updates hook settings.
7889
func (h *Hook) Update() error {
7990
if len(strings.TrimSpace(h.Content)) == 0 {
80-
if isExist(h.path) {
91+
exist, err := util.IsExist(h.path)
92+
if err != nil {
93+
return err
94+
}
95+
if exist {
8196
err := util.Remove(h.path)
8297
if err != nil {
8398
return err
@@ -101,7 +116,10 @@ func (h *Hook) Update() error {
101116

102117
// ListHooks returns a list of Git hooks of given repository.
103118
func ListHooks(repoPath string) (_ []*Hook, err error) {
104-
if !isDir(filepath.Join(repoPath, "hooks")) {
119+
exist, err := util.IsDir(filepath.Join(repoPath, "hooks"))
120+
if err != nil {
121+
return nil, err
122+
} else if !exist {
105123
return nil, errors.New("hooks path does not exist")
106124
}
107125

modules/git/repo_base_gogit.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,12 @@ func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) {
4949
repoPath, err := filepath.Abs(repoPath)
5050
if err != nil {
5151
return nil, err
52-
} else if !isDir(repoPath) {
52+
}
53+
exist, err := util.IsDir(repoPath)
54+
if err != nil {
55+
return nil, err
56+
}
57+
if !exist {
5358
return nil, util.NewNotExistErrorf("no such file or directory")
5459
}
5560

modules/git/repo_base_nogogit.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) {
4747
repoPath, err := filepath.Abs(repoPath)
4848
if err != nil {
4949
return nil, err
50-
} else if !isDir(repoPath) {
50+
}
51+
exist, err := util.IsDir(repoPath)
52+
if err != nil {
53+
return nil, err
54+
}
55+
if !exist {
5156
return nil, util.NewNotExistErrorf("no such file or directory")
5257
}
5358

modules/git/utils.go

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"encoding/hex"
99
"fmt"
1010
"io"
11-
"os"
1211
"strconv"
1312
"strings"
1413
"sync"
@@ -41,33 +40,6 @@ func (oc *ObjectCache[T]) Get(id string) (T, bool) {
4140
return obj, has
4241
}
4342

44-
// isDir returns true if given path is a directory,
45-
// or returns false when it's a file or does not exist.
46-
func isDir(dir string) bool {
47-
f, e := os.Stat(dir)
48-
if e != nil {
49-
return false
50-
}
51-
return f.IsDir()
52-
}
53-
54-
// isFile returns true if given path is a file,
55-
// or returns false when it's a directory or does not exist.
56-
func isFile(filePath string) bool {
57-
f, e := os.Stat(filePath)
58-
if e != nil {
59-
return false
60-
}
61-
return !f.IsDir()
62-
}
63-
64-
// isExist checks whether a file or directory exists.
65-
// It returns false when the file or directory does not exist.
66-
func isExist(path string) bool {
67-
_, err := os.Stat(path)
68-
return err == nil || os.IsExist(err)
69-
}
70-
7143
// ConcatenateError concatenats an error with stderr string
7244
func ConcatenateError(err error, stderr string) error {
7345
if len(stderr) == 0 {

0 commit comments

Comments
 (0)