From b877504b03a43dc5ed19109f1a00f08a343ebaba Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Thu, 31 Mar 2022 19:56:22 +0800 Subject: Refactor `git.Command.Run*`, introduce `RunWithContextString` and `RunWithContextBytes` (#19266) This follows * https://github.com/go-gitea/gitea/issues/18553 Introduce `RunWithContextString` and `RunWithContextBytes` to help the refactoring. Add related unit tests. They keep the same behavior to save stderr into err.Error() as `RunInXxx` before. Remove `RunInDirTimeoutPipeline` `RunInDirTimeoutFullPipeline` `RunInDirTimeout` `RunInDirTimeoutEnv` `RunInDirPipeline` `RunInDirFullPipeline` `RunTimeout`, `RunInDirTimeoutEnvPipeline`, `RunInDirTimeoutEnvFullPipeline`, `RunInDirTimeoutEnvFullPipelineFunc`. Then remaining `RunInDir` `RunInDirBytes` `RunInDirWithEnv` can be easily refactored in next PR with a simple search & replace: * before: `stdout, err := RunInDir(path)` * next: `stdout, _, err := RunWithContextString(&git.RunContext{Dir:path})` Other changes: 1. When `timeout <= 0`, use default. Because `timeout==0` is meaningless and could cause bugs. And now many functions becomes more simple, eg: `GitGcRepos` 9 lines to 1 line. `Fsck` 6 lines to 1 line. 2. Only set defaultCommandExecutionTimeout when the option `setting.Git.Timeout.Default > 0` --- modules/git/git.go | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) (limited to 'modules/git/git.go') diff --git a/modules/git/git.go b/modules/git/git.go index 14940d1f16..6b7dbfd169 100644 --- a/modules/git/git.go +++ b/modules/git/git.go @@ -124,7 +124,9 @@ func VersionInfo() string { func Init(ctx context.Context) error { DefaultContext = ctx - defaultCommandExecutionTimeout = time.Duration(setting.Git.Timeout.Default) * time.Second + if setting.Git.Timeout.Default > 0 { + defaultCommandExecutionTimeout = time.Duration(setting.Git.Timeout.Default) * time.Second + } if err := SetExecutablePath(setting.Git.Path); err != nil { return err @@ -295,10 +297,5 @@ func checkAndRemoveConfig(key, value string) error { // Fsck verifies the connectivity and validity of the objects in the database func Fsck(ctx context.Context, repoPath string, timeout time.Duration, args ...string) error { - // Make sure timeout makes sense. - if timeout <= 0 { - timeout = -1 - } - _, err := NewCommand(ctx, "fsck").AddArguments(args...).RunInDirTimeout(timeout, repoPath) - return err + return NewCommand(ctx, "fsck").AddArguments(args...).RunWithContext(&RunContext{Timeout: timeout, Dir: repoPath}) } -- cgit v1.2.3