mirror of
				https://github.com/go-gitea/gitea
				synced 2025-10-31 11:28:24 +00:00 
			
		
		
		
	Refactor legacy code (#35708)
And by the way, remove the legacy TODO, split large functions into small ones, and add more tests
This commit is contained in:
		| @@ -47,30 +47,16 @@ func GetHook(repoPath, name string) (*Hook, error) { | ||||
| 		name: name, | ||||
| 		path: filepath.Join(repoPath, filepath.Join("hooks", name+".d", name)), | ||||
| 	} | ||||
| 	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 | ||||
| 		} | ||||
| 	if data, err := os.ReadFile(h.path); err == nil { | ||||
| 		h.IsActive = true | ||||
| 		h.Content = string(data) | ||||
| 		return h, nil | ||||
| 	} else if !os.IsNotExist(err) { | ||||
| 		return nil, err | ||||
| 	} | ||||
|  | ||||
| 	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 | ||||
| 		} | ||||
| 	if data, err := os.ReadFile(samplePath); err == nil { | ||||
| 		h.Sample = string(data) | ||||
| 	} | ||||
| 	return h, nil | ||||
|   | ||||
| @@ -202,11 +202,11 @@ func NewConfigProviderFromFile(file string) (ConfigProvider, error) { | ||||
| 	loadedFromEmpty := true | ||||
|  | ||||
| 	if file != "" { | ||||
| 		isFile, err := util.IsFile(file) | ||||
| 		isExist, err := util.IsExist(file) | ||||
| 		if err != nil { | ||||
| 			return nil, fmt.Errorf("unable to check if %q is a file. Error: %v", file, err) | ||||
| 			return nil, fmt.Errorf("unable to check if %q exists: %v", file, err) | ||||
| 		} | ||||
| 		if isFile { | ||||
| 		if isExist { | ||||
| 			if err = cfg.Append(file); err != nil { | ||||
| 				return nil, fmt.Errorf("failed to load config file %q: %v", file, err) | ||||
| 			} | ||||
|   | ||||
| @@ -115,15 +115,10 @@ func IsDir(dir string) (bool, error) { | ||||
| 	return false, err | ||||
| } | ||||
|  | ||||
| // 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, error) { | ||||
| 	f, err := os.Stat(filePath) | ||||
| func IsRegularFile(filePath string) (bool, error) { | ||||
| 	f, err := os.Lstat(filePath) | ||||
| 	if err == nil { | ||||
| 		return !f.IsDir(), nil | ||||
| 	} | ||||
| 	if os.IsNotExist(err) { | ||||
| 		return false, nil | ||||
| 		return f.Mode().IsRegular(), nil | ||||
| 	} | ||||
| 	return false, err | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user