summaryrefslogtreecommitdiffstats
path: root/services
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2022-03-31 19:56:22 +0800
committerGitHub <noreply@github.com>2022-03-31 13:56:22 +0200
commitb877504b03a43dc5ed19109f1a00f08a343ebaba (patch)
treee1769eab011325f444b07fd388a7f7430abc85c6 /services
parentd4f84f1c937e24e71aa5f05c58d440cde741450f (diff)
downloadgitea-b877504b03a43dc5ed19109f1a00f08a343ebaba.tar.gz
gitea-b877504b03a43dc5ed19109f1a00f08a343ebaba.zip
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`
Diffstat (limited to 'services')
-rw-r--r--services/repository/check.go10
-rw-r--r--services/repository/fork.go4
2 files changed, 3 insertions, 11 deletions
diff --git a/services/repository/check.go b/services/repository/check.go
index 6962090f84..efce308f59 100644
--- a/services/repository/check.go
+++ b/services/repository/check.go
@@ -77,15 +77,7 @@ func GitGcRepos(ctx context.Context, timeout time.Duration, args ...string) erro
SetDescription(fmt.Sprintf("Repository Garbage Collection: %s", repo.FullName()))
var stdout string
var err error
- if timeout > 0 {
- var stdoutBytes []byte
- stdoutBytes, err = command.RunInDirTimeout(
- timeout,
- repo.RepoPath())
- stdout = string(stdoutBytes)
- } else {
- stdout, err = command.RunInDir(repo.RepoPath())
- }
+ stdout, _, err = command.RunWithContextString(&git.RunContext{Timeout: timeout, Dir: repo.RepoPath()})
if err != nil {
log.Error("Repository garbage collection failed for %v. Stdout: %s\nError: %v", repo, stdout, err)
diff --git a/services/repository/fork.go b/services/repository/fork.go
index 1a5d358408..220fbeb992 100644
--- a/services/repository/fork.go
+++ b/services/repository/fork.go
@@ -108,10 +108,10 @@ func ForkRepository(ctx context.Context, doer, owner *user_model.User, opts Fork
needsRollback = true
repoPath := repo_model.RepoPath(owner.Name, repo.Name)
- if stdout, err := git.NewCommand(txCtx,
+ if stdout, _, err := git.NewCommand(txCtx,
"clone", "--bare", oldRepoPath, repoPath).
SetDescription(fmt.Sprintf("ForkRepository(git clone): %s to %s", opts.BaseRepo.FullName(), repo.FullName())).
- RunInDirTimeout(10*time.Minute, ""); err != nil {
+ RunWithContextBytes(&git.RunContext{Timeout: 10 * time.Minute}); err != nil {
log.Error("Fork Repository (git clone) Failed for %v (from %v):\nStdout: %s\nError: %v", repo, opts.BaseRepo, stdout, err)
return fmt.Errorf("git clone: %v", err)
}