mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-31 10:56:10 +01:00 
			
		
		
		
	Move duplicated functions (#33977)
Remove duplicated functions `IsExist`, `IsFile` and `IsDir` in package `modules/git` and use the exists functions in `modules/util`.
This commit is contained in:
		| @@ -51,15 +51,26 @@ func GetHook(repoPath, name string) (*Hook, error) { | |||||||
| 		name: name, | 		name: name, | ||||||
| 		path: filepath.Join(repoPath, "hooks", name+".d", name), | 		path: filepath.Join(repoPath, "hooks", name+".d", name), | ||||||
| 	} | 	} | ||||||
| 	samplePath := filepath.Join(repoPath, "hooks", name+".sample") | 	isFile, err := util.IsFile(h.path) | ||||||
| 	if isFile(h.path) { | 	if err != nil { | ||||||
|  | 		return nil, err | ||||||
|  | 	} | ||||||
|  | 	if isFile { | ||||||
| 		data, err := os.ReadFile(h.path) | 		data, err := os.ReadFile(h.path) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return nil, err | 			return nil, err | ||||||
| 		} | 		} | ||||||
| 		h.IsActive = true | 		h.IsActive = true | ||||||
| 		h.Content = string(data) | 		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) | 		data, err := os.ReadFile(samplePath) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return nil, err | 			return nil, err | ||||||
| @@ -77,7 +88,11 @@ func (h *Hook) Name() string { | |||||||
| // Update updates hook settings. | // Update updates hook settings. | ||||||
| func (h *Hook) Update() error { | func (h *Hook) Update() error { | ||||||
| 	if len(strings.TrimSpace(h.Content)) == 0 { | 	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) | 			err := util.Remove(h.path) | ||||||
| 			if err != nil { | 			if err != nil { | ||||||
| 				return err | 				return err | ||||||
| @@ -101,7 +116,10 @@ func (h *Hook) Update() error { | |||||||
|  |  | ||||||
| // ListHooks returns a list of Git hooks of given repository. | // ListHooks returns a list of Git hooks of given repository. | ||||||
| func ListHooks(repoPath string) (_ []*Hook, err error) { | 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") | 		return nil, errors.New("hooks path does not exist") | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|   | |||||||
| @@ -49,7 +49,12 @@ func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) { | |||||||
| 	repoPath, err := filepath.Abs(repoPath) | 	repoPath, err := filepath.Abs(repoPath) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return nil, err | 		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") | 		return nil, util.NewNotExistErrorf("no such file or directory") | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|   | |||||||
| @@ -47,7 +47,12 @@ func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) { | |||||||
| 	repoPath, err := filepath.Abs(repoPath) | 	repoPath, err := filepath.Abs(repoPath) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return nil, err | 		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") | 		return nil, util.NewNotExistErrorf("no such file or directory") | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|   | |||||||
| @@ -8,7 +8,6 @@ import ( | |||||||
| 	"encoding/hex" | 	"encoding/hex" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"io" | 	"io" | ||||||
| 	"os" |  | ||||||
| 	"strconv" | 	"strconv" | ||||||
| 	"strings" | 	"strings" | ||||||
| 	"sync" | 	"sync" | ||||||
| @@ -41,33 +40,6 @@ func (oc *ObjectCache[T]) Get(id string) (T, bool) { | |||||||
| 	return obj, has | 	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 | // ConcatenateError concatenats an error with stderr string | ||||||
| func ConcatenateError(err error, stderr string) error { | func ConcatenateError(err error, stderr string) error { | ||||||
| 	if len(stderr) == 0 { | 	if len(stderr) == 0 { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user