]> source.dussan.org Git - gitea.git/commitdiff
Change git.cmd to RunWithContext (#18693)
authorMartin Scholz <martin.scholz83@outlook.com>
Fri, 11 Feb 2022 12:47:22 +0000 (13:47 +0100)
committerGitHub <noreply@github.com>
Fri, 11 Feb 2022 12:47:22 +0000 (13:47 +0100)
Change all `cmd...Pipeline` commands to `cmd.RunWithContext`.

#18553

Co-authored-by: Martin Scholz <martin.scholz@versasec.com>
28 files changed:
integrations/pull_merge_test.go
modules/git/batch_reader.go
modules/git/commit.go
modules/git/diff.go
modules/git/log_name_status.go
modules/git/pipeline/catfile.go
modules/git/pipeline/lfs_nogogit.go
modules/git/pipeline/namerev.go
modules/git/pipeline/revlist.go
modules/git/repo.go
modules/git/repo_archive.go
modules/git/repo_attribute.go
modules/git/repo_branch_nogogit.go
modules/git/repo_commit.go
modules/git/repo_compare.go
modules/git/repo_index.go
modules/git/repo_object.go
modules/git/repo_ref_nogogit.go
modules/git/repo_stats.go
modules/git/repo_tree.go
modules/gitgraph/graph.go
routers/private/hook_verification.go
services/mirror/mirror_pull.go
services/pull/merge.go
services/pull/patch.go
services/pull/patch_unmerged.go
services/pull/temp_repo.go
services/repository/files/temp_repo.go

index 57a9868678e977bc007b2eaa2eb03c61756aa1e8..8aded910d4b256735fe115ccee572cd15d84f11a 100644 (file)
@@ -274,7 +274,13 @@ func TestCantMergeUnrelated(t *testing.T) {
 
                stdin := bytes.NewBufferString("Unrelated File")
                var stdout strings.Builder
-               err = git.NewCommand(git.DefaultContext, "hash-object", "-w", "--stdin").RunInDirFullPipeline(path, &stdout, nil, stdin)
+               err = git.NewCommand(git.DefaultContext, "hash-object", "-w", "--stdin").RunWithContext(&git.RunContext{
+                       Timeout: -1,
+                       Dir:     path,
+                       Stdin:   stdin,
+                       Stdout:  &stdout,
+               })
+
                assert.NoError(t, err)
                sha := strings.TrimSpace(stdout.String())
 
@@ -301,7 +307,14 @@ func TestCantMergeUnrelated(t *testing.T) {
                _, _ = messageBytes.WriteString("\n")
 
                stdout.Reset()
-               err = git.NewCommand(git.DefaultContext, "commit-tree", treeSha).RunInDirTimeoutEnvFullPipeline(env, -1, path, &stdout, nil, messageBytes)
+               err = git.NewCommand(git.DefaultContext, "commit-tree", treeSha).
+                       RunWithContext(&git.RunContext{
+                               Env:     env,
+                               Timeout: -1,
+                               Dir:     path,
+                               Stdin:   messageBytes,
+                               Stdout:  &stdout,
+                       })
                assert.NoError(t, err)
                commitSha := strings.TrimSpace(stdout.String())
 
index 4cd6cb1217713f88f526effed7138b2d198b6c03..66ca118de5a4916e35a6c6de23bf93794f128aa6 100644 (file)
@@ -34,7 +34,11 @@ func EnsureValidGitRepository(ctx context.Context, repoPath string) error {
        stderr := strings.Builder{}
        err := NewCommand(ctx, "rev-parse").
                SetDescription(fmt.Sprintf("%s rev-parse [repo_path: %s]", GitExecutable, repoPath)).
-               RunInDirFullPipeline(repoPath, nil, &stderr, nil)
+               RunWithContext(&RunContext{
+                       Timeout: -1,
+                       Dir:     repoPath,
+                       Stderr:  &stderr,
+               })
        if err != nil {
                return ConcatenateError(err, (&stderr).String())
        }
@@ -61,7 +65,13 @@ func CatFileBatchCheck(ctx context.Context, repoPath string) (WriteCloserError,
                stderr := strings.Builder{}
                err := NewCommand(ctx, "cat-file", "--batch-check").
                        SetDescription(fmt.Sprintf("%s cat-file --batch-check [repo_path: %s] (%s:%d)", GitExecutable, repoPath, filename, line)).
-                       RunInDirFullPipeline(repoPath, batchStdoutWriter, &stderr, batchStdinReader)
+                       RunWithContext(&RunContext{
+                               Timeout: -1,
+                               Dir:     repoPath,
+                               Stdin:   batchStdinReader,
+                               Stdout:  batchStdoutWriter,
+                               Stderr:  &stderr,
+                       })
                if err != nil {
                        _ = batchStdoutWriter.CloseWithError(ConcatenateError(err, (&stderr).String()))
                        _ = batchStdinReader.CloseWithError(ConcatenateError(err, (&stderr).String()))
@@ -100,7 +110,13 @@ func CatFileBatch(ctx context.Context, repoPath string) (WriteCloserError, *bufi
                stderr := strings.Builder{}
                err := NewCommand(ctx, "cat-file", "--batch").
                        SetDescription(fmt.Sprintf("%s cat-file --batch [repo_path: %s] (%s:%d)", GitExecutable, repoPath, filename, line)).
-                       RunInDirFullPipeline(repoPath, batchStdoutWriter, &stderr, batchStdinReader)
+                       RunWithContext(&RunContext{
+                               Timeout: -1,
+                               Dir:     repoPath,
+                               Stdin:   batchStdinReader,
+                               Stdout:  batchStdoutWriter,
+                               Stderr:  &stderr,
+                       })
                if err != nil {
                        _ = batchStdoutWriter.CloseWithError(ConcatenateError(err, (&stderr).String()))
                        _ = batchStdinReader.CloseWithError(ConcatenateError(err, (&stderr).String()))
index 77ba3c0eb22bf22aeede4bdf78754b6a49e1e3dc..340a7e21dd643342943c3353e731722e25f596a0 100644 (file)
@@ -486,7 +486,12 @@ func GetCommitFileStatus(ctx context.Context, repoPath, commitID string) (*Commi
        stderr := new(bytes.Buffer)
        args := []string{"log", "--name-status", "-c", "--pretty=format:", "--parents", "--no-renames", "-z", "-1", commitID}
 
-       err := NewCommand(ctx, args...).RunInDirPipeline(repoPath, w, stderr)
+       err := NewCommand(ctx, args...).RunWithContext(&RunContext{
+               Timeout: -1,
+               Dir:     repoPath,
+               Stdout:  w,
+               Stderr:  stderr,
+       })
        w.Close() // Close writer to exit parsing goroutine
        if err != nil {
                return nil, ConcatenateError(err, stderr.String())
index 2d85db475396fa890adab89f634825d96dcaef1e..621878f620401d6137dc39950dd0a5246a6161f6 100644 (file)
@@ -301,9 +301,12 @@ func GetAffectedFiles(repo *Repository, oldCommitID, newCommitID string, env []s
 
        // Run `git diff --name-only` to get the names of the changed files
        err = NewCommand(repo.Ctx, "diff", "--name-only", oldCommitID, newCommitID).
-               RunInDirTimeoutEnvFullPipelineFunc(env, -1, repo.Path,
-                       stdoutWriter, nil, nil,
-                       func(ctx context.Context, cancel context.CancelFunc) error {
+               RunWithContext(&RunContext{
+                       Env:     env,
+                       Timeout: -1,
+                       Dir:     repo.Path,
+                       Stdout:  stdoutWriter,
+                       PipelineFunc: func(ctx context.Context, cancel context.CancelFunc) error {
                                // Close the writer end of the pipe to begin processing
                                _ = stdoutWriter.Close()
                                defer func() {
@@ -320,7 +323,8 @@ func GetAffectedFiles(repo *Repository, oldCommitID, newCommitID string, env []s
                                        affectedFiles = append(affectedFiles, path)
                                }
                                return scanner.Err()
-                       })
+                       },
+               })
        if err != nil {
                log.Error("Unable to get affected files for commits from %s to %s in %s: %v", oldCommitID, newCommitID, repo.Path, err)
        }
index 7720d53db19cc81aef6f9e8e18b1e233c7cf02cd..0571a4dd20c652373a02c046c2cd69d84f3f5929 100644 (file)
@@ -55,7 +55,12 @@ func LogNameStatusRepo(ctx context.Context, repository, head, treepath string, p
 
        go func() {
                stderr := strings.Builder{}
-               err := NewCommand(ctx, args...).RunInDirFullPipeline(repository, stdoutWriter, &stderr, nil)
+               err := NewCommand(ctx, args...).RunWithContext(&RunContext{
+                       Timeout: -1,
+                       Dir:     repository,
+                       Stdout:  stdoutWriter,
+                       Stderr:  &stderr,
+               })
                if err != nil {
                        _ = stdoutWriter.CloseWithError(ConcatenateError(err, (&stderr).String()))
                } else {
index 15c3396ff5fa3246e36bbbd6fd5328ae33b2fec5..6948131e460d4aae5dfd58d9a9d85bb10776232e 100644 (file)
@@ -27,7 +27,13 @@ func CatFileBatchCheck(ctx context.Context, shasToCheckReader *io.PipeReader, ca
        stderr := new(bytes.Buffer)
        var errbuf strings.Builder
        cmd := git.NewCommand(ctx, "cat-file", "--batch-check")
-       if err := cmd.RunInDirFullPipeline(tmpBasePath, catFileCheckWriter, stderr, shasToCheckReader); err != nil {
+       if err := cmd.RunWithContext(&git.RunContext{
+               Timeout: -1,
+               Dir:     tmpBasePath,
+               Stdin:   shasToCheckReader,
+               Stdout:  catFileCheckWriter,
+               Stderr:  stderr,
+       }); err != nil {
                _ = catFileCheckWriter.CloseWithError(fmt.Errorf("git cat-file --batch-check [%s]: %v - %s", tmpBasePath, err, errbuf.String()))
        }
 }
@@ -40,7 +46,12 @@ func CatFileBatchCheckAllObjects(ctx context.Context, catFileCheckWriter *io.Pip
        stderr := new(bytes.Buffer)
        var errbuf strings.Builder
        cmd := git.NewCommand(ctx, "cat-file", "--batch-check", "--batch-all-objects")
-       if err := cmd.RunInDirPipeline(tmpBasePath, catFileCheckWriter, stderr); err != nil {
+       if err := cmd.RunWithContext(&git.RunContext{
+               Timeout: -1,
+               Dir:     tmpBasePath,
+               Stdout:  catFileCheckWriter,
+               Stderr:  stderr,
+       }); err != nil {
                log.Error("git cat-file --batch-check --batch-all-object [%s]: %v - %s", tmpBasePath, err, errbuf.String())
                err = fmt.Errorf("git cat-file --batch-check --batch-all-object [%s]: %v - %s", tmpBasePath, err, errbuf.String())
                _ = catFileCheckWriter.CloseWithError(err)
@@ -56,7 +67,13 @@ func CatFileBatch(ctx context.Context, shasToBatchReader *io.PipeReader, catFile
 
        stderr := new(bytes.Buffer)
        var errbuf strings.Builder
-       if err := git.NewCommand(ctx, "cat-file", "--batch").RunInDirFullPipeline(tmpBasePath, catFileBatchWriter, stderr, shasToBatchReader); err != nil {
+       if err := git.NewCommand(ctx, "cat-file", "--batch").RunWithContext(&git.RunContext{
+               Timeout: -1,
+               Dir:     tmpBasePath,
+               Stdout:  catFileBatchWriter,
+               Stdin:   shasToBatchReader,
+               Stderr:  stderr,
+       }); err != nil {
                _ = shasToBatchReader.CloseWithError(fmt.Errorf("git rev-list [%s]: %v - %s", tmpBasePath, err, errbuf.String()))
        }
 }
index 90ffef16bbe5f7547e92a1652c2257c2cb787e57..1d43080a5a01e52b10ce6d16df6639e030b560e4 100644 (file)
@@ -53,7 +53,12 @@ func FindLFSFile(repo *git.Repository, hash git.SHA1) ([]*LFSResult, error) {
 
        go func() {
                stderr := strings.Builder{}
-               err := git.NewCommand(repo.Ctx, "rev-list", "--all").RunInDirPipeline(repo.Path, revListWriter, &stderr)
+               err := git.NewCommand(repo.Ctx, "rev-list", "--all").RunWithContext(&git.RunContext{
+                       Timeout: -1,
+                       Dir:     repo.Path,
+                       Stdout:  revListWriter,
+                       Stderr:  &stderr,
+               })
                if err != nil {
                        _ = revListWriter.CloseWithError(git.ConcatenateError(err, (&stderr).String()))
                } else {
index 84006e9005d85d7db385572391c61da68c7b185b..357322070ecea0836876c620ece5b9d538b6ad55 100644 (file)
@@ -23,7 +23,13 @@ func NameRevStdin(ctx context.Context, shasToNameReader *io.PipeReader, nameRevS
 
        stderr := new(bytes.Buffer)
        var errbuf strings.Builder
-       if err := git.NewCommand(ctx, "name-rev", "--stdin", "--name-only", "--always").RunInDirFullPipeline(tmpBasePath, nameRevStdinWriter, stderr, shasToNameReader); err != nil {
+       if err := git.NewCommand(ctx, "name-rev", "--stdin", "--name-only", "--always").RunWithContext(&git.RunContext{
+               Timeout: -1,
+               Dir:     tmpBasePath,
+               Stdout:  nameRevStdinWriter,
+               Stdin:   shasToNameReader,
+               Stderr:  stderr,
+       }); err != nil {
                _ = shasToNameReader.CloseWithError(fmt.Errorf("git name-rev [%s]: %v - %s", tmpBasePath, err, errbuf.String()))
        }
 }
index 75dc676f367be8fa4ffba84dc397e03ce03059a3..a1f8f079f959306083f7c5d910244e46c28fdd0f 100644 (file)
@@ -25,7 +25,12 @@ func RevListAllObjects(ctx context.Context, revListWriter *io.PipeWriter, wg *sy
        stderr := new(bytes.Buffer)
        var errbuf strings.Builder
        cmd := git.NewCommand(ctx, "rev-list", "--objects", "--all")
-       if err := cmd.RunInDirPipeline(basePath, revListWriter, stderr); err != nil {
+       if err := cmd.RunWithContext(&git.RunContext{
+               Timeout: -1,
+               Dir:     basePath,
+               Stdout:  revListWriter,
+               Stderr:  stderr,
+       }); err != nil {
                log.Error("git rev-list --objects --all [%s]: %v - %s", basePath, err, errbuf.String())
                err = fmt.Errorf("git rev-list --objects --all [%s]: %v - %s", basePath, err, errbuf.String())
                _ = revListWriter.CloseWithError(err)
@@ -40,7 +45,12 @@ func RevListObjects(ctx context.Context, revListWriter *io.PipeWriter, wg *sync.
        stderr := new(bytes.Buffer)
        var errbuf strings.Builder
        cmd := git.NewCommand(ctx, "rev-list", "--objects", headSHA, "--not", baseSHA)
-       if err := cmd.RunInDirPipeline(tmpBasePath, revListWriter, stderr); err != nil {
+       if err := cmd.RunWithContext(&git.RunContext{
+               Timeout: -1,
+               Dir:     tmpBasePath,
+               Stdout:  revListWriter,
+               Stderr:  stderr,
+       }); err != nil {
                log.Error("git rev-list [%s]: %v - %s", tmpBasePath, err, errbuf.String())
                errChan <- fmt.Errorf("git rev-list [%s]: %v - %s", tmpBasePath, err, errbuf.String())
        }
index ff704138a7b101a429e7f3c9038b4be4b95c4e36..79a540209c5d1ccb1b30cc83df5e07c76d30ca4d 100644 (file)
@@ -204,7 +204,13 @@ func Push(ctx context.Context, repoPath string, opts PushOptions) error {
                opts.Timeout = -1
        }
 
-       err := cmd.RunInDirTimeoutEnvPipeline(opts.Env, opts.Timeout, repoPath, &outbuf, &errbuf)
+       err := cmd.RunWithContext(&RunContext{
+               Env:     opts.Env,
+               Timeout: opts.Timeout,
+               Dir:     repoPath,
+               Stdout:  &outbuf,
+               Stderr:  &errbuf,
+       })
        if err != nil {
                if strings.Contains(errbuf.String(), "non-fast-forward") {
                        return &ErrPushOutOfDate{
index cf09bba0db3445cda748f1efd7bd7fb0238a924d..b7c339c271fd7a94f0c60e1e7b924dfcc49d640a 100644 (file)
@@ -57,7 +57,12 @@ func (repo *Repository) CreateArchive(ctx context.Context, format ArchiveType, t
        )
 
        var stderr strings.Builder
-       err := NewCommand(ctx, args...).RunInDirPipeline(repo.Path, target, &stderr)
+       err := NewCommand(ctx, args...).RunWithContext(&RunContext{
+               Timeout: -1,
+               Dir:     repo.Path,
+               Stdout:  target,
+               Stderr:  &stderr,
+       })
        if err != nil {
                return ConcatenateError(err, stderr.String())
        }
index c63dfacdfc2bdd4d1e09277ebcbdf5b401b20c47..d31203aabd04964041c02acedefb6fedf5e05bf2 100644 (file)
@@ -76,7 +76,13 @@ func (repo *Repository) CheckAttribute(opts CheckAttributeOpts) (map[string]map[
 
        cmd := NewCommand(repo.Ctx, cmdArgs...)
 
-       if err := cmd.RunInDirTimeoutEnvPipeline(env, -1, repo.Path, stdOut, stdErr); err != nil {
+       if err := cmd.RunWithContext(&RunContext{
+               Env:     env,
+               Timeout: -1,
+               Dir:     repo.Path,
+               Stdout:  stdOut,
+               Stderr:  stdErr,
+       }); err != nil {
                return nil, fmt.Errorf("failed to run check-attr: %v\n%s\n%s", err, stdOut.String(), stdErr.String())
        }
 
@@ -182,9 +188,17 @@ func (c *CheckAttributeReader) Run() error {
                _ = c.Close()
        }()
        stdErr := new(bytes.Buffer)
-       err := c.cmd.RunInDirTimeoutEnvFullPipelineFunc(c.env, -1, c.Repo.Path, c.stdOut, stdErr, c.stdinReader, func(_ context.Context, _ context.CancelFunc) error {
-               close(c.running)
-               return nil
+       err := c.cmd.RunWithContext(&RunContext{
+               Env:     c.env,
+               Timeout: -1,
+               Dir:     c.Repo.Path,
+               Stdin:   c.stdinReader,
+               Stdout:  c.stdOut,
+               Stderr:  stdErr,
+               PipelineFunc: func(_ context.Context, _ context.CancelFunc) error {
+                       close(c.running)
+                       return nil
+               },
        })
        if err != nil && c.ctx.Err() != nil && err.Error() != "signal: killed" {
                return fmt.Errorf("failed to run attr-check. Error: %w\nStderr: %s", err, stdErr.String())
index 2e9335a31636c399c8ebbacb06fcd72fedca3e2f..66990add6fd98a7483f673bf6c0d537cda5defa7 100644 (file)
@@ -96,7 +96,12 @@ func walkShowRef(ctx context.Context, repoPath, arg string, skip, limit int, wal
                if arg != "" {
                        args = append(args, arg)
                }
-               err := NewCommand(ctx, args...).RunInDirPipeline(repoPath, stdoutWriter, stderrBuilder)
+               err := NewCommand(ctx, args...).RunWithContext(&RunContext{
+                       Timeout: -1,
+                       Dir:     repoPath,
+                       Stdout:  stdoutWriter,
+                       Stderr:  stderrBuilder,
+               })
                if err != nil {
                        if stderrBuilder.Len() == 0 {
                                _ = stdoutWriter.Close()
index 5ccc42a383eb57b58b3518c99cae273928e301d4..8e059ce0ea252aee3d44fcb8055100c0a9b3f08e 100644 (file)
@@ -211,7 +211,12 @@ func (repo *Repository) CommitsByFileAndRange(revision, file string, page int) (
                err := NewCommand(repo.Ctx, "log", revision, "--follow",
                        "--max-count="+strconv.Itoa(setting.Git.CommitsRangeSize*page),
                        prettyLogFormat, "--", file).
-                       RunInDirPipeline(repo.Path, stdoutWriter, &stderr)
+                       RunWithContext(&RunContext{
+                               Timeout: -1,
+                               Dir:     repo.Path,
+                               Stdout:  stdoutWriter,
+                               Stderr:  &stderr,
+                       })
                if err != nil {
                        _ = stdoutWriter.CloseWithError(ConcatenateError(err, (&stderr).String()))
                } else {
index dddc158dcc4537dc39ef987a4383bbd632fb3f1a..aa8015af14e503b1ea0775925473ab274e2130d8 100644 (file)
@@ -147,13 +147,23 @@ func (repo *Repository) GetDiffNumChangedFiles(base, head string, directComparis
        }
 
        if err := NewCommand(repo.Ctx, "diff", "-z", "--name-only", base+separator+head).
-               RunInDirPipeline(repo.Path, w, stderr); err != nil {
+               RunWithContext(&RunContext{
+                       Timeout: -1,
+                       Dir:     repo.Path,
+                       Stdout:  w,
+                       Stderr:  stderr,
+               }); err != nil {
                if strings.Contains(stderr.String(), "no merge base") {
                        // git >= 2.28 now returns an error if base and head have become unrelated.
                        // previously it would return the results of git diff -z --name-only base head so let's try that...
                        w = &lineCountWriter{}
                        stderr.Reset()
-                       if err = NewCommand(repo.Ctx, "diff", "-z", "--name-only", base, head).RunInDirPipeline(repo.Path, w, stderr); err == nil {
+                       if err = NewCommand(repo.Ctx, "diff", "-z", "--name-only", base, head).RunWithContext(&RunContext{
+                               Timeout: -1,
+                               Dir:     repo.Path,
+                               Stdout:  w,
+                               Stderr:  stderr,
+                       }); err == nil {
                                return w.numLines, nil
                        }
                }
@@ -238,28 +248,46 @@ func (repo *Repository) GetDiffOrPatch(base, head string, w io.Writer, patch, bi
 
 // GetDiff generates and returns patch data between given revisions, optimized for human readability
 func (repo *Repository) GetDiff(base, head string, w io.Writer) error {
-       return NewCommand(repo.Ctx, "diff", "-p", base, head).
-               RunInDirPipeline(repo.Path, w, nil)
+       return NewCommand(repo.Ctx, "diff", "-p", base, head).RunWithContext(&RunContext{
+               Timeout: -1,
+               Dir:     repo.Path,
+               Stdout:  w,
+       })
 }
 
 // GetDiffBinary generates and returns patch data between given revisions, including binary diffs.
 func (repo *Repository) GetDiffBinary(base, head string, w io.Writer) error {
        if CheckGitVersionAtLeast("1.7.7") == nil {
-               return NewCommand(repo.Ctx, "diff", "-p", "--binary", "--histogram", base, head).
-                       RunInDirPipeline(repo.Path, w, nil)
+               return NewCommand(repo.Ctx, "diff", "-p", "--binary", "--histogram", base, head).RunWithContext(&RunContext{
+                       Timeout: -1,
+                       Dir:     repo.Path,
+                       Stdout:  w,
+               })
        }
-       return NewCommand(repo.Ctx, "diff", "-p", "--binary", "--patience", base, head).
-               RunInDirPipeline(repo.Path, w, nil)
+       return NewCommand(repo.Ctx, "diff", "-p", "--binary", "--patience", base, head).RunWithContext(&RunContext{
+               Timeout: -1,
+               Dir:     repo.Path,
+               Stdout:  w,
+       })
 }
 
 // GetPatch generates and returns format-patch data between given revisions, able to be used with `git apply`
 func (repo *Repository) GetPatch(base, head string, w io.Writer) error {
        stderr := new(bytes.Buffer)
        err := NewCommand(repo.Ctx, "format-patch", "--binary", "--stdout", base+"..."+head).
-               RunInDirPipeline(repo.Path, w, stderr)
+               RunWithContext(&RunContext{
+                       Timeout: -1,
+                       Dir:     repo.Path,
+                       Stdout:  w,
+                       Stderr:  stderr,
+               })
        if err != nil && bytes.Contains(stderr.Bytes(), []byte("no merge base")) {
                return NewCommand(repo.Ctx, "format-patch", "--binary", "--stdout", base, head).
-                       RunInDirPipeline(repo.Path, w, nil)
+                       RunWithContext(&RunContext{
+                               Timeout: -1,
+                               Dir:     repo.Path,
+                               Stdout:  w,
+                       })
        }
        return err
 }
@@ -268,7 +296,12 @@ func (repo *Repository) GetPatch(base, head string, w io.Writer) error {
 func (repo *Repository) GetDiffFromMergeBase(base, head string, w io.Writer) error {
        stderr := new(bytes.Buffer)
        err := NewCommand(repo.Ctx, "diff", "-p", "--binary", base+"..."+head).
-               RunInDirPipeline(repo.Path, w, stderr)
+               RunWithContext(&RunContext{
+                       Timeout: -1,
+                       Dir:     repo.Path,
+                       Stdout:  w,
+                       Stderr:  stderr,
+               })
        if err != nil && bytes.Contains(stderr.Bytes(), []byte("no merge base")) {
                return repo.GetDiffBinary(base, head, w)
        }
index 8e76c5e466095476eb04fede988019280d000785..53de0f1cb8ec1fcc6f5c81608a2784cc196c7dbe 100644 (file)
@@ -106,7 +106,13 @@ func (repo *Repository) RemoveFilesFromIndex(filenames ...string) error {
                        buffer.WriteByte('\000')
                }
        }
-       return cmd.RunInDirFullPipeline(repo.Path, stdout, stderr, bytes.NewReader(buffer.Bytes()))
+       return cmd.RunWithContext(&RunContext{
+               Timeout: -1,
+               Dir:     repo.Path,
+               Stdin:   bytes.NewReader(buffer.Bytes()),
+               Stdout:  stdout,
+               Stderr:  stderr,
+       })
 }
 
 // AddObjectToIndex adds the provided object hash to the index at the provided filename
index a9ab66b28f17ea30c82d35ebe14f39374d7972c4..378e657ce4f4a0d6f9a5cf4abd17fd2bad32bf5b 100644 (file)
@@ -45,7 +45,13 @@ func (repo *Repository) hashObject(reader io.Reader) (string, error) {
        cmd := NewCommand(repo.Ctx, "hash-object", "-w", "--stdin")
        stdout := new(bytes.Buffer)
        stderr := new(bytes.Buffer)
-       err := cmd.RunInDirFullPipeline(repo.Path, stdout, stderr, reader)
+       err := cmd.RunWithContext(&RunContext{
+               Timeout: -1,
+               Dir:     repo.Path,
+               Stdin:   reader,
+               Stdout:  stdout,
+               Stderr:  stderr,
+       })
        if err != nil {
                return "", err
        }
index e17d23eb9c418d9b4efc32c4877df813f25a88ff..42295e43ac580f729d5cdc4923265c695a965205 100644 (file)
@@ -23,7 +23,12 @@ func (repo *Repository) GetRefsFiltered(pattern string) ([]*Reference, error) {
 
        go func() {
                stderrBuilder := &strings.Builder{}
-               err := NewCommand(repo.Ctx, "for-each-ref").RunInDirPipeline(repo.Path, stdoutWriter, stderrBuilder)
+               err := NewCommand(repo.Ctx, "for-each-ref").RunWithContext(&RunContext{
+                       Timeout: -1,
+                       Dir:     repo.Path,
+                       Stdout:  stdoutWriter,
+                       Stderr:  stderrBuilder,
+               })
                if err != nil {
                        _ = stdoutWriter.CloseWithError(ConcatenateError(err, stderrBuilder.String()))
                } else {
index 6f5973ebe5f24556fd5fb7cb572bf3755bc70de3..598ec37a2c61ced5394296f35e2d9c5821faf2e3 100644 (file)
@@ -67,12 +67,14 @@ func (repo *Repository) GetCodeActivityStats(fromTime time.Time, branch string)
        }
 
        stderr := new(strings.Builder)
-       err = NewCommand(repo.Ctx, args...).RunInDirTimeoutEnvFullPipelineFunc(
-               nil, -1, repo.Path,
-               stdoutWriter, stderr, nil,
-               func(ctx context.Context, cancel context.CancelFunc) error {
+       err = NewCommand(repo.Ctx, args...).RunWithContext(&RunContext{
+               Env:     []string{},
+               Timeout: -1,
+               Dir:     repo.Path,
+               Stdout:  stdoutWriter,
+               Stderr:  stderr,
+               PipelineFunc: func(ctx context.Context, cancel context.CancelFunc) error {
                        _ = stdoutWriter.Close()
-
                        scanner := bufio.NewScanner(stdoutReader)
                        scanner.Split(bufio.ScanLines)
                        stats.CommitCount = 0
@@ -103,11 +105,7 @@ func (repo *Repository) GetCodeActivityStats(fromTime time.Time, branch string)
                                case 4: // E-mail
                                        email := strings.ToLower(l)
                                        if _, ok := authors[email]; !ok {
-                                               authors[email] = &CodeActivityAuthor{
-                                                       Name:    author,
-                                                       Email:   email,
-                                                       Commits: 0,
-                                               }
+                                               authors[email] = &CodeActivityAuthor{Name: author, Email: email, Commits: 0}
                                        }
                                        authors[email].Commits++
                                default: // Changed file
@@ -128,7 +126,6 @@ func (repo *Repository) GetCodeActivityStats(fromTime time.Time, branch string)
                                        }
                                }
                        }
-
                        a := make([]*CodeActivityAuthor, 0, len(authors))
                        for _, v := range authors {
                                a = append(a, v)
@@ -137,14 +134,13 @@ func (repo *Repository) GetCodeActivityStats(fromTime time.Time, branch string)
                        sort.Slice(a, func(i, j int) bool {
                                return a[i].Commits > a[j].Commits
                        })
-
                        stats.AuthorCount = int64(len(authors))
                        stats.ChangedFiles = int64(len(files))
                        stats.Authors = a
-
                        _ = stdoutReader.Close()
                        return nil
-               })
+               },
+       })
        if err != nil {
                return nil, fmt.Errorf("Failed to get GetCodeActivityStats for repository.\nError: %w\nStderr: %s", err, stderr)
        }
index 9efacc8dfe00ced7ed8f95a9782c7330f7ba6115..3219b569a53d05d0b8325aa256dfe3b76b804e99 100644 (file)
@@ -60,7 +60,14 @@ func (repo *Repository) CommitTree(author, committer *Signature, tree *Tree, opt
 
        stdout := new(bytes.Buffer)
        stderr := new(bytes.Buffer)
-       err = cmd.RunInDirTimeoutEnvFullPipeline(env, -1, repo.Path, stdout, stderr, messageBytes)
+       err = cmd.RunWithContext(&RunContext{
+               Env:     env,
+               Timeout: -1,
+               Dir:     repo.Path,
+               Stdin:   messageBytes,
+               Stdout:  stdout,
+               Stderr:  stderr,
+       })
 
        if err != nil {
                return SHA1{}, ConcatenateError(err, stderr.String())
index c0618152d8a8be2d14ce18e27b02b8ff737e2a0d..e15441b8831d7801b85e8d9f3db9355f13d7801d 100644 (file)
@@ -64,57 +64,63 @@ func GetCommitGraph(r *git.Repository, page, maxAllowedColors int, hidePRRefs bo
 
        scanner := bufio.NewScanner(stdoutReader)
 
-       if err := graphCmd.RunInDirTimeoutEnvFullPipelineFunc(nil, -1, r.Path, stdoutWriter, stderr, nil, func(ctx context.Context, cancel context.CancelFunc) error {
-               _ = stdoutWriter.Close()
-               defer stdoutReader.Close()
-               parser := &Parser{}
-               parser.firstInUse = -1
-               parser.maxAllowedColors = maxAllowedColors
-               if maxAllowedColors > 0 {
-                       parser.availableColors = make([]int, maxAllowedColors)
-                       for i := range parser.availableColors {
-                               parser.availableColors[i] = i + 1
-                       }
-               } else {
-                       parser.availableColors = []int{1, 2}
-               }
-               for commitsToSkip > 0 && scanner.Scan() {
-                       line := scanner.Bytes()
-                       dataIdx := bytes.Index(line, []byte("DATA:"))
-                       if dataIdx < 0 {
-                               dataIdx = len(line)
+       if err := graphCmd.RunWithContext(&git.RunContext{
+               Timeout: -1,
+               Dir:     r.Path,
+               Stdout:  stdoutWriter,
+               Stderr:  stderr,
+               PipelineFunc: func(ctx context.Context, cancel context.CancelFunc) error {
+                       _ = stdoutWriter.Close()
+                       defer stdoutReader.Close()
+                       parser := &Parser{}
+                       parser.firstInUse = -1
+                       parser.maxAllowedColors = maxAllowedColors
+                       if maxAllowedColors > 0 {
+                               parser.availableColors = make([]int, maxAllowedColors)
+                               for i := range parser.availableColors {
+                                       parser.availableColors[i] = i + 1
+                               }
+                       } else {
+                               parser.availableColors = []int{1, 2}
                        }
-                       starIdx := bytes.IndexByte(line, '*')
-                       if starIdx >= 0 && starIdx < dataIdx {
-                               commitsToSkip--
+                       for commitsToSkip > 0 && scanner.Scan() {
+                               line := scanner.Bytes()
+                               dataIdx := bytes.Index(line, []byte("DATA:"))
+                               if dataIdx < 0 {
+                                       dataIdx = len(line)
+                               }
+                               starIdx := bytes.IndexByte(line, '*')
+                               if starIdx >= 0 && starIdx < dataIdx {
+                                       commitsToSkip--
+                               }
+                               parser.ParseGlyphs(line[:dataIdx])
                        }
-                       parser.ParseGlyphs(line[:dataIdx])
-               }
 
-               row := 0
+                       row := 0
+
+                       // Skip initial non-commit lines
+                       for scanner.Scan() {
+                               line := scanner.Bytes()
+                               if bytes.IndexByte(line, '*') >= 0 {
+                                       if err := parser.AddLineToGraph(graph, row, line); err != nil {
+                                               cancel()
+                                               return err
+                                       }
+                                       break
+                               }
+                               parser.ParseGlyphs(line)
+                       }
 
-               // Skip initial non-commit lines
-               for scanner.Scan() {
-                       line := scanner.Bytes()
-                       if bytes.IndexByte(line, '*') >= 0 {
+                       for scanner.Scan() {
+                               row++
+                               line := scanner.Bytes()
                                if err := parser.AddLineToGraph(graph, row, line); err != nil {
                                        cancel()
                                        return err
                                }
-                               break
-                       }
-                       parser.ParseGlyphs(line)
-               }
-
-               for scanner.Scan() {
-                       row++
-                       line := scanner.Bytes()
-                       if err := parser.AddLineToGraph(graph, row, line); err != nil {
-                               cancel()
-                               return err
                        }
-               }
-               return scanner.Err()
+                       return scanner.Err()
+               },
        }); err != nil {
                return graph, err
        }
index 565cb273e790fac86ac3623c57aa846b67ba6482..683ed8d071c621455a6f4f6858073abc8b32dd00 100644 (file)
@@ -45,9 +45,12 @@ func verifyCommits(oldCommitID, newCommitID string, repo *git.Repository, env []
 
        // This is safe as force pushes are already forbidden
        err = git.NewCommand(repo.Ctx, "rev-list", oldCommitID+"..."+newCommitID).
-               RunInDirTimeoutEnvFullPipelineFunc(env, -1, repo.Path,
-                       stdoutWriter, nil, nil,
-                       func(ctx context.Context, cancel context.CancelFunc) error {
+               RunWithContext(&git.RunContext{
+                       Env:     env,
+                       Timeout: -1,
+                       Dir:     repo.Path,
+                       Stdout:  stdoutWriter,
+                       PipelineFunc: func(ctx context.Context, cancel context.CancelFunc) error {
                                _ = stdoutWriter.Close()
                                err := readAndVerifyCommitsFromShaReader(stdoutReader, repo, env)
                                if err != nil {
@@ -56,7 +59,8 @@ func verifyCommits(oldCommitID, newCommitID string, repo *git.Repository, env []
                                }
                                _ = stdoutReader.Close()
                                return err
-                       })
+                       },
+               })
        if err != nil && !isErrUnverifiedCommit(err) {
                log.Error("Unable to check commits from %s to %s in %s: %v", oldCommitID, newCommitID, repo.Path, err)
        }
@@ -89,9 +93,12 @@ func readAndVerifyCommit(sha string, repo *git.Repository, env []string) error {
        hash := git.MustIDFromString(sha)
 
        return git.NewCommand(repo.Ctx, "cat-file", "commit", sha).
-               RunInDirTimeoutEnvFullPipelineFunc(env, -1, repo.Path,
-                       stdoutWriter, nil, nil,
-                       func(ctx context.Context, cancel context.CancelFunc) error {
+               RunWithContext(&git.RunContext{
+                       Env:     env,
+                       Timeout: -1,
+                       Dir:     repo.Path,
+                       Stdout:  stdoutWriter,
+                       PipelineFunc: func(ctx context.Context, cancel context.CancelFunc) error {
                                _ = stdoutWriter.Close()
                                commit, err := git.CommitFromReader(repo, hash, stdoutReader)
                                if err != nil {
@@ -105,7 +112,8 @@ func readAndVerifyCommit(sha string, repo *git.Repository, env []string) error {
                                        }
                                }
                                return nil
-                       })
+                       },
+               })
 }
 
 type errUnverifiedCommit struct {
index 4710f9642da3091bd469d2909066866c49f09a5d..c86f15efc0210266891ee9f42c1e34db8734cd73 100644 (file)
@@ -161,7 +161,12 @@ func pruneBrokenReferences(ctx context.Context,
        stdoutBuilder.Reset()
        pruneErr := git.NewCommand(ctx, "remote", "prune", m.GetRemoteName()).
                SetDescription(fmt.Sprintf("Mirror.runSync %ssPrune references: %s ", wiki, m.Repo.FullName())).
-               RunInDirTimeoutPipeline(timeout, repoPath, stdoutBuilder, stderrBuilder)
+               RunWithContext(&git.RunContext{
+                       Timeout: timeout,
+                       Dir:     repoPath,
+                       Stdout:  stdoutBuilder,
+                       Stderr:  stderrBuilder,
+               })
        if pruneErr != nil {
                stdout := stdoutBuilder.String()
                stderr := stderrBuilder.String()
@@ -203,7 +208,12 @@ func runSync(ctx context.Context, m *repo_model.Mirror) ([]*mirrorSyncResult, bo
        stderrBuilder := strings.Builder{}
        if err := git.NewCommand(ctx, gitArgs...).
                SetDescription(fmt.Sprintf("Mirror.runSync: %s", m.Repo.FullName())).
-               RunInDirTimeoutPipeline(timeout, repoPath, &stdoutBuilder, &stderrBuilder); err != nil {
+               RunWithContext(&git.RunContext{
+                       Timeout: timeout,
+                       Dir:     repoPath,
+                       Stdout:  &stdoutBuilder,
+                       Stderr:  &stderrBuilder,
+               }); err != nil {
                stdout := stdoutBuilder.String()
                stderr := stderrBuilder.String()
 
@@ -226,7 +236,12 @@ func runSync(ctx context.Context, m *repo_model.Mirror) ([]*mirrorSyncResult, bo
                                stdoutBuilder.Reset()
                                if err = git.NewCommand(ctx, gitArgs...).
                                        SetDescription(fmt.Sprintf("Mirror.runSync: %s", m.Repo.FullName())).
-                                       RunInDirTimeoutPipeline(timeout, repoPath, &stdoutBuilder, &stderrBuilder); err != nil {
+                                       RunWithContext(&git.RunContext{
+                                               Timeout: timeout,
+                                               Dir:     repoPath,
+                                               Stdout:  &stdoutBuilder,
+                                               Stderr:  &stderrBuilder,
+                                       }); err != nil {
                                        stdout := stdoutBuilder.String()
                                        stderr := stderrBuilder.String()
 
@@ -282,7 +297,12 @@ func runSync(ctx context.Context, m *repo_model.Mirror) ([]*mirrorSyncResult, bo
                stdoutBuilder.Reset()
                if err := git.NewCommand(ctx, "remote", "update", "--prune", m.GetRemoteName()).
                        SetDescription(fmt.Sprintf("Mirror.runSync Wiki: %s ", m.Repo.FullName())).
-                       RunInDirTimeoutPipeline(timeout, wikiPath, &stdoutBuilder, &stderrBuilder); err != nil {
+                       RunWithContext(&git.RunContext{
+                               Timeout: timeout,
+                               Dir:     wikiPath,
+                               Stdout:  &stdoutBuilder,
+                               Stderr:  &stderrBuilder,
+                       }); err != nil {
                        stdout := stdoutBuilder.String()
                        stderr := stderrBuilder.String()
 
@@ -314,7 +334,12 @@ func runSync(ctx context.Context, m *repo_model.Mirror) ([]*mirrorSyncResult, bo
 
                                        if err = git.NewCommand(ctx, "remote", "update", "--prune", m.GetRemoteName()).
                                                SetDescription(fmt.Sprintf("Mirror.runSync Wiki: %s ", m.Repo.FullName())).
-                                               RunInDirTimeoutPipeline(timeout, wikiPath, &stdoutBuilder, &stderrBuilder); err != nil {
+                                               RunWithContext(&git.RunContext{
+                                                       Timeout: timeout,
+                                                       Dir:     wikiPath,
+                                                       Stdout:  &stdoutBuilder,
+                                                       Stderr:  &stderrBuilder,
+                                               }); err != nil {
                                                stdout := stdoutBuilder.String()
                                                stderr := stderrBuilder.String()
                                                stderrMessage = sanitizer.Replace(stderr)
index 62c502011a32e8b83b96eb6f379b33728eb9fe23..cb857cc60dbc751d268a9008ec6743f323f4a5f7 100644 (file)
@@ -188,35 +188,65 @@ func rawMerge(ctx context.Context, pr *models.PullRequest, doer *user_model.User
        }
 
        // Switch off LFS process (set required, clean and smudge here also)
-       if err := gitConfigCommand().AddArguments("filter.lfs.process", "").RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil {
+       if err := gitConfigCommand().AddArguments("filter.lfs.process", "").
+               RunWithContext(&git.RunContext{
+                       Timeout: -1,
+                       Dir:     tmpBasePath,
+                       Stdout:  &outbuf,
+                       Stderr:  &errbuf,
+               }); err != nil {
                log.Error("git config [filter.lfs.process -> <> ]: %v\n%s\n%s", err, outbuf.String(), errbuf.String())
                return "", fmt.Errorf("git config [filter.lfs.process -> <> ]: %v\n%s\n%s", err, outbuf.String(), errbuf.String())
        }
        outbuf.Reset()
        errbuf.Reset()
 
-       if err := gitConfigCommand().AddArguments("filter.lfs.required", "false").RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil {
+       if err := gitConfigCommand().AddArguments("filter.lfs.required", "false").
+               RunWithContext(&git.RunContext{
+                       Timeout: -1,
+                       Dir:     tmpBasePath,
+                       Stdout:  &outbuf,
+                       Stderr:  &errbuf,
+               }); err != nil {
                log.Error("git config [filter.lfs.required -> <false> ]: %v\n%s\n%s", err, outbuf.String(), errbuf.String())
                return "", fmt.Errorf("git config [filter.lfs.required -> <false> ]: %v\n%s\n%s", err, outbuf.String(), errbuf.String())
        }
        outbuf.Reset()
        errbuf.Reset()
 
-       if err := gitConfigCommand().AddArguments("filter.lfs.clean", "").RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil {
+       if err := gitConfigCommand().AddArguments("filter.lfs.clean", "").
+               RunWithContext(&git.RunContext{
+                       Timeout: -1,
+                       Dir:     tmpBasePath,
+                       Stdout:  &outbuf,
+                       Stderr:  &errbuf,
+               }); err != nil {
                log.Error("git config [filter.lfs.clean -> <> ]: %v\n%s\n%s", err, outbuf.String(), errbuf.String())
                return "", fmt.Errorf("git config [filter.lfs.clean -> <> ]: %v\n%s\n%s", err, outbuf.String(), errbuf.String())
        }
        outbuf.Reset()
        errbuf.Reset()
 
-       if err := gitConfigCommand().AddArguments("filter.lfs.smudge", "").RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil {
+       if err := gitConfigCommand().AddArguments("filter.lfs.smudge", "").
+               RunWithContext(&git.RunContext{
+                       Timeout: -1,
+                       Dir:     tmpBasePath,
+                       Stdout:  &outbuf,
+                       Stderr:  &errbuf,
+               }); err != nil {
                log.Error("git config [filter.lfs.smudge -> <> ]: %v\n%s\n%s", err, outbuf.String(), errbuf.String())
                return "", fmt.Errorf("git config [filter.lfs.smudge -> <> ]: %v\n%s\n%s", err, outbuf.String(), errbuf.String())
        }
        outbuf.Reset()
        errbuf.Reset()
 
-       if err := gitConfigCommand().AddArguments("core.sparseCheckout", "true").RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil {
+       if err := gitConfigCommand().AddArguments("core.sparseCheckout", "true").
+               RunWithContext(&git.RunContext{
+                       Timeout: -1,
+                       Dir:     tmpBasePath,
+                       Stdout:  &outbuf,
+                       Stderr:  &errbuf,
+               }); err != nil {
                log.Error("git config [core.sparseCheckout -> true ]: %v\n%s\n%s", err, outbuf.String(), errbuf.String())
                return "", fmt.Errorf("git config [core.sparsecheckout -> true]: %v\n%s\n%s", err, outbuf.String(), errbuf.String())
        }
@@ -224,7 +254,13 @@ func rawMerge(ctx context.Context, pr *models.PullRequest, doer *user_model.User
        errbuf.Reset()
 
        // Read base branch index
-       if err := git.NewCommand(ctx, "read-tree", "HEAD").RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil {
+       if err := git.NewCommand(ctx, "read-tree", "HEAD").
+               RunWithContext(&git.RunContext{
+                       Timeout: -1,
+                       Dir:     tmpBasePath,
+                       Stdout:  &outbuf,
+                       Stderr:  &errbuf,
+               }); err != nil {
                log.Error("git read-tree HEAD: %v\n%s\n%s", err, outbuf.String(), errbuf.String())
                return "", fmt.Errorf("Unable to read base branch in to the index: %v\n%s\n%s", err, outbuf.String(), errbuf.String())
        }
@@ -279,7 +315,13 @@ func rawMerge(ctx context.Context, pr *models.PullRequest, doer *user_model.User
                fallthrough
        case repo_model.MergeStyleRebaseMerge:
                // Checkout head branch
-               if err := git.NewCommand(ctx, "checkout", "-b", stagingBranch, trackingBranch).RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil {
+               if err := git.NewCommand(ctx, "checkout", "-b", stagingBranch, trackingBranch).
+                       RunWithContext(&git.RunContext{
+                               Timeout: -1,
+                               Dir:     tmpBasePath,
+                               Stdout:  &outbuf,
+                               Stderr:  &errbuf,
+                       }); err != nil {
                        log.Error("git checkout base prior to merge post staging rebase [%s:%s -> %s:%s]: %v\n%s\n%s", pr.HeadRepo.FullName(), pr.HeadBranch, pr.BaseRepo.FullName(), pr.BaseBranch, err, outbuf.String(), errbuf.String())
                        return "", fmt.Errorf("git checkout base prior to merge post staging rebase  [%s:%s -> %s:%s]: %v\n%s\n%s", pr.HeadRepo.FullName(), pr.HeadBranch, pr.BaseRepo.FullName(), pr.BaseBranch, err, outbuf.String(), errbuf.String())
                }
@@ -287,7 +329,13 @@ func rawMerge(ctx context.Context, pr *models.PullRequest, doer *user_model.User
                errbuf.Reset()
 
                // Rebase before merging
-               if err := git.NewCommand(ctx, "rebase", baseBranch).RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil {
+               if err := git.NewCommand(ctx, "rebase", baseBranch).
+                       RunWithContext(&git.RunContext{
+                               Timeout: -1,
+                               Dir:     tmpBasePath,
+                               Stdout:  &outbuf,
+                               Stderr:  &errbuf,
+                       }); err != nil {
                        // Rebase will leave a REBASE_HEAD file in .git if there is a conflict
                        if _, statErr := os.Stat(filepath.Join(tmpBasePath, ".git", "REBASE_HEAD")); statErr == nil {
                                var commitSha string
@@ -335,7 +383,13 @@ func rawMerge(ctx context.Context, pr *models.PullRequest, doer *user_model.User
                }
 
                // Checkout base branch again
-               if err := git.NewCommand(ctx, "checkout", baseBranch).RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil {
+               if err := git.NewCommand(ctx, "checkout", baseBranch).
+                       RunWithContext(&git.RunContext{
+                               Timeout: -1,
+                               Dir:     tmpBasePath,
+                               Stdout:  &outbuf,
+                               Stderr:  &errbuf,
+                       }); err != nil {
                        log.Error("git checkout base prior to merge post staging rebase [%s:%s -> %s:%s]: %v\n%s\n%s", pr.HeadRepo.FullName(), pr.HeadBranch, pr.BaseRepo.FullName(), pr.BaseBranch, err, outbuf.String(), errbuf.String())
                        return "", fmt.Errorf("git checkout base prior to merge post staging rebase  [%s:%s -> %s:%s]: %v\n%s\n%s", pr.HeadRepo.FullName(), pr.HeadBranch, pr.BaseRepo.FullName(), pr.BaseBranch, err, outbuf.String(), errbuf.String())
                }
@@ -375,7 +429,14 @@ func rawMerge(ctx context.Context, pr *models.PullRequest, doer *user_model.User
                }
                sig := pr.Issue.Poster.NewGitSig()
                if signArg == "" {
-                       if err := git.NewCommand(ctx, "commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email), "-m", message).RunInDirTimeoutEnvPipeline(env, -1, tmpBasePath, &outbuf, &errbuf); err != nil {
+                       if err := git.NewCommand(ctx, "commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email), "-m", message).
+                               RunWithContext(&git.RunContext{
+                                       Env:     env,
+                                       Timeout: -1,
+                                       Dir:     tmpBasePath,
+                                       Stdout:  &outbuf,
+                                       Stderr:  &errbuf,
+                               }); err != nil {
                                log.Error("git commit [%s:%s -> %s:%s]: %v\n%s\n%s", pr.HeadRepo.FullName(), pr.HeadBranch, pr.BaseRepo.FullName(), pr.BaseBranch, err, outbuf.String(), errbuf.String())
                                return "", fmt.Errorf("git commit [%s:%s -> %s:%s]: %v\n%s\n%s", pr.HeadRepo.FullName(), pr.HeadBranch, pr.BaseRepo.FullName(), pr.BaseBranch, err, outbuf.String(), errbuf.String())
                        }
@@ -384,7 +445,14 @@ func rawMerge(ctx context.Context, pr *models.PullRequest, doer *user_model.User
                                // add trailer
                                message += fmt.Sprintf("\nCo-authored-by: %s\nCo-committed-by: %s\n", sig.String(), sig.String())
                        }
-                       if err := git.NewCommand(ctx, "commit", signArg, fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email), "-m", message).RunInDirTimeoutEnvPipeline(env, -1, tmpBasePath, &outbuf, &errbuf); err != nil {
+                       if err := git.NewCommand(ctx, "commit", signArg, fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email), "-m", message).
+                               RunWithContext(&git.RunContext{
+                                       Env:     env,
+                                       Timeout: -1,
+                                       Dir:     tmpBasePath,
+                                       Stdout:  &outbuf,
+                                       Stderr:  &errbuf,
+                               }); err != nil {
                                log.Error("git commit [%s:%s -> %s:%s]: %v\n%s\n%s", pr.HeadRepo.FullName(), pr.HeadBranch, pr.BaseRepo.FullName(), pr.BaseBranch, err, outbuf.String(), errbuf.String())
                                return "", fmt.Errorf("git commit [%s:%s -> %s:%s]: %v\n%s\n%s", pr.HeadRepo.FullName(), pr.HeadBranch, pr.BaseRepo.FullName(), pr.BaseBranch, err, outbuf.String(), errbuf.String())
                        }
@@ -448,7 +516,13 @@ func rawMerge(ctx context.Context, pr *models.PullRequest, doer *user_model.User
        }
 
        // Push back to upstream.
-       if err := pushCmd.RunInDirTimeoutEnvPipeline(env, -1, tmpBasePath, &outbuf, &errbuf); err != nil {
+       if err := pushCmd.RunWithContext(&git.RunContext{
+               Env:     env,
+               Timeout: -1,
+               Dir:     tmpBasePath,
+               Stdout:  &outbuf,
+               Stderr:  &errbuf,
+       }); err != nil {
                if strings.Contains(errbuf.String(), "non-fast-forward") {
                        return "", &git.ErrPushOutOfDate{
                                StdOut: outbuf.String(),
@@ -475,12 +549,26 @@ func rawMerge(ctx context.Context, pr *models.PullRequest, doer *user_model.User
 func commitAndSignNoAuthor(ctx context.Context, pr *models.PullRequest, message, signArg, tmpBasePath string, env []string) error {
        var outbuf, errbuf strings.Builder
        if signArg == "" {
-               if err := git.NewCommand(ctx, "commit", "-m", message).RunInDirTimeoutEnvPipeline(env, -1, tmpBasePath, &outbuf, &errbuf); err != nil {
+               if err := git.NewCommand(ctx, "commit", "-m", message).
+                       RunWithContext(&git.RunContext{
+                               Env:     env,
+                               Timeout: -1,
+                               Dir:     tmpBasePath,
+                               Stdout:  &outbuf,
+                               Stderr:  &errbuf,
+                       }); err != nil {
                        log.Error("git commit [%s:%s -> %s:%s]: %v\n%s\n%s", pr.HeadRepo.FullName(), pr.HeadBranch, pr.BaseRepo.FullName(), pr.BaseBranch, err, outbuf.String(), errbuf.String())
                        return fmt.Errorf("git commit [%s:%s -> %s:%s]: %v\n%s\n%s", pr.HeadRepo.FullName(), pr.HeadBranch, pr.BaseRepo.FullName(), pr.BaseBranch, err, outbuf.String(), errbuf.String())
                }
        } else {
-               if err := git.NewCommand(ctx, "commit", signArg, "-m", message).RunInDirTimeoutEnvPipeline(env, -1, tmpBasePath, &outbuf, &errbuf); err != nil {
+               if err := git.NewCommand(ctx, "commit", signArg, "-m", message).
+                       RunWithContext(&git.RunContext{
+                               Env:     env,
+                               Timeout: -1,
+                               Dir:     tmpBasePath,
+                               Stdout:  &outbuf,
+                               Stderr:  &errbuf,
+                       }); err != nil {
                        log.Error("git commit [%s:%s -> %s:%s]: %v\n%s\n%s", pr.HeadRepo.FullName(), pr.HeadBranch, pr.BaseRepo.FullName(), pr.BaseBranch, err, outbuf.String(), errbuf.String())
                        return fmt.Errorf("git commit [%s:%s -> %s:%s]: %v\n%s\n%s", pr.HeadRepo.FullName(), pr.HeadBranch, pr.BaseRepo.FullName(), pr.BaseBranch, err, outbuf.String(), errbuf.String())
                }
@@ -490,7 +578,12 @@ func commitAndSignNoAuthor(ctx context.Context, pr *models.PullRequest, message,
 
 func runMergeCommand(pr *models.PullRequest, mergeStyle repo_model.MergeStyle, cmd *git.Command, tmpBasePath string) error {
        var outbuf, errbuf strings.Builder
-       if err := cmd.RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil {
+       if err := cmd.RunWithContext(&git.RunContext{
+               Timeout: -1,
+               Dir:     tmpBasePath,
+               Stdout:  &outbuf,
+               Stderr:  &errbuf,
+       }); err != nil {
                // Merge will leave a MERGE_HEAD file in the .git folder if there is a conflict
                if _, statErr := os.Stat(filepath.Join(tmpBasePath, ".git", "MERGE_HEAD")); statErr == nil {
                        // We have a merge conflict error
@@ -523,7 +616,13 @@ func getDiffTree(ctx context.Context, repoPath, baseBranch, headBranch string) (
        getDiffTreeFromBranch := func(repoPath, baseBranch, headBranch string) (string, error) {
                var outbuf, errbuf strings.Builder
                // Compute the diff-tree for sparse-checkout
-               if err := git.NewCommand(ctx, "diff-tree", "--no-commit-id", "--name-only", "-r", "-z", "--root", baseBranch, headBranch, "--").RunInDirPipeline(repoPath, &outbuf, &errbuf); err != nil {
+               if err := git.NewCommand(ctx, "diff-tree", "--no-commit-id", "--name-only", "-r", "-z", "--root", baseBranch, headBranch, "--").
+                       RunWithContext(&git.RunContext{
+                               Timeout: -1,
+                               Dir:     repoPath,
+                               Stdout:  &outbuf,
+                               Stderr:  &errbuf,
+                       }); err != nil {
                        return "", fmt.Errorf("git diff-tree [%s base:%s head:%s]: %s", repoPath, baseBranch, headBranch, errbuf.String())
                }
                return outbuf.String(), nil
index a2c8345326f0bf88f38cc318524cd715363347fa..f401b85345ebe8b2ae86a47532b1a1a05a0e5740 100644 (file)
@@ -383,10 +383,11 @@ func checkConflicts(ctx context.Context, pr *models.PullRequest, gitRepo *git.Re
        // 7. Run the check command
        conflict = false
        err = git.NewCommand(gitRepo.Ctx, args...).
-               RunInDirTimeoutEnvFullPipelineFunc(
-                       nil, -1, tmpBasePath,
-                       nil, stderrWriter, nil,
-                       func(ctx context.Context, cancel context.CancelFunc) error {
+               RunWithContext(&git.RunContext{
+                       Timeout: -1,
+                       Dir:     tmpBasePath,
+                       Stderr:  stderrWriter,
+                       PipelineFunc: func(ctx context.Context, cancel context.CancelFunc) error {
                                // Close the writer end of the pipe to begin processing
                                _ = stderrWriter.Close()
                                defer func() {
@@ -444,7 +445,8 @@ func checkConflicts(ctx context.Context, pr *models.PullRequest, gitRepo *git.Re
                                }
 
                                return nil
-                       })
+                       },
+               })
 
        // 8. If there is a conflict the `git apply` command will return a non-zero error code - so there will be a positive error.
        if err != nil {
index 65264f9865ae0d0e0bd62d6ed3314683110d1158..abd54b07cf1229205af86962bc35d3f07c6edcf1 100644 (file)
@@ -63,10 +63,12 @@ func readUnmergedLsFileLines(ctx context.Context, tmpBasePath string, outputChan
 
        stderr := &strings.Builder{}
        err = git.NewCommand(ctx, "ls-files", "-u", "-z").
-               RunInDirTimeoutEnvFullPipelineFunc(
-                       nil, -1, tmpBasePath,
-                       lsFilesWriter, stderr, nil,
-                       func(_ context.Context, _ context.CancelFunc) error {
+               RunWithContext(&git.RunContext{
+                       Timeout: -1,
+                       Dir:     tmpBasePath,
+                       Stdout:  lsFilesWriter,
+                       Stderr:  stderr,
+                       PipelineFunc: func(_ context.Context, _ context.CancelFunc) error {
                                _ = lsFilesWriter.Close()
                                defer func() {
                                        _ = lsFilesReader.Close()
@@ -102,8 +104,8 @@ func readUnmergedLsFileLines(ctx context.Context, tmpBasePath string, outputChan
                                        toemit.path = split[2][2 : len(split[2])-1]
                                        outputChan <- toemit
                                }
-                       })
-
+                       },
+               })
        if err != nil {
                outputChan <- &lsFileLine{err: fmt.Errorf("git ls-files -u -z: %v", git.ConcatenateError(err, stderr.String()))}
        }
index e9c227d79422badb706942a0d8c99c11525efdd7..831d98745e4ca8b3f848d17895863a664f7de111 100644 (file)
@@ -93,7 +93,13 @@ func createTemporaryRepo(ctx context.Context, pr *models.PullRequest) (string, e
        }
 
        var outbuf, errbuf strings.Builder
-       if err := git.NewCommand(ctx, "remote", "add", "-t", pr.BaseBranch, "-m", pr.BaseBranch, "origin", baseRepoPath).RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil {
+       if err := git.NewCommand(ctx, "remote", "add", "-t", pr.BaseBranch, "-m", pr.BaseBranch, "origin", baseRepoPath).
+               RunWithContext(&git.RunContext{
+                       Timeout: -1,
+                       Dir:     tmpBasePath,
+                       Stdout:  &outbuf,
+                       Stderr:  &errbuf,
+               }); err != nil {
                log.Error("Unable to add base repository as origin [%s -> %s]: %v\n%s\n%s", pr.BaseRepo.FullName(), tmpBasePath, err, outbuf.String(), errbuf.String())
                if err := models.RemoveTemporaryPath(tmpBasePath); err != nil {
                        log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err)
@@ -103,7 +109,13 @@ func createTemporaryRepo(ctx context.Context, pr *models.PullRequest) (string, e
        outbuf.Reset()
        errbuf.Reset()
 
-       if err := git.NewCommand(ctx, "fetch", "origin", "--no-tags", "--", pr.BaseBranch+":"+baseBranch, pr.BaseBranch+":original_"+baseBranch).RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil {
+       if err := git.NewCommand(ctx, "fetch", "origin", "--no-tags", "--", pr.BaseBranch+":"+baseBranch, pr.BaseBranch+":original_"+baseBranch).
+               RunWithContext(&git.RunContext{
+                       Timeout: -1,
+                       Dir:     tmpBasePath,
+                       Stdout:  &outbuf,
+                       Stderr:  &errbuf,
+               }); err != nil {
                log.Error("Unable to fetch origin base branch [%s:%s -> base, original_base in %s]: %v:\n%s\n%s", pr.BaseRepo.FullName(), pr.BaseBranch, tmpBasePath, err, outbuf.String(), errbuf.String())
                if err := models.RemoveTemporaryPath(tmpBasePath); err != nil {
                        log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err)
@@ -113,7 +125,13 @@ func createTemporaryRepo(ctx context.Context, pr *models.PullRequest) (string, e
        outbuf.Reset()
        errbuf.Reset()
 
-       if err := git.NewCommand(ctx, "symbolic-ref", "HEAD", git.BranchPrefix+baseBranch).RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil {
+       if err := git.NewCommand(ctx, "symbolic-ref", "HEAD", git.BranchPrefix+baseBranch).
+               RunWithContext(&git.RunContext{
+                       Timeout: -1,
+                       Dir:     tmpBasePath,
+                       Stdout:  &outbuf,
+                       Stderr:  &errbuf,
+               }); err != nil {
                log.Error("Unable to set HEAD as base branch [%s]: %v\n%s\n%s", tmpBasePath, err, outbuf.String(), errbuf.String())
                if err := models.RemoveTemporaryPath(tmpBasePath); err != nil {
                        log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err)
@@ -131,7 +149,13 @@ func createTemporaryRepo(ctx context.Context, pr *models.PullRequest) (string, e
                return "", fmt.Errorf("Unable to head base repository to temporary repo [%s -> tmpBasePath]: %v", pr.HeadRepo.FullName(), err)
        }
 
-       if err := git.NewCommand(ctx, "remote", "add", remoteRepoName, headRepoPath).RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil {
+       if err := git.NewCommand(ctx, "remote", "add", remoteRepoName, headRepoPath).
+               RunWithContext(&git.RunContext{
+                       Timeout: -1,
+                       Dir:     tmpBasePath,
+                       Stdout:  &outbuf,
+                       Stderr:  &errbuf,
+               }); err != nil {
                log.Error("Unable to add head repository as head_repo [%s -> %s]: %v\n%s\n%s", pr.HeadRepo.FullName(), tmpBasePath, err, outbuf.String(), errbuf.String())
                if err := models.RemoveTemporaryPath(tmpBasePath); err != nil {
                        log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err)
@@ -151,7 +175,13 @@ func createTemporaryRepo(ctx context.Context, pr *models.PullRequest) (string, e
        } else {
                headBranch = pr.GetGitRefName()
        }
-       if err := git.NewCommand(ctx, "fetch", "--no-tags", remoteRepoName, headBranch+":"+trackingBranch).RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil {
+       if err := git.NewCommand(ctx, "fetch", "--no-tags", remoteRepoName, headBranch+":"+trackingBranch).
+               RunWithContext(&git.RunContext{
+                       Timeout: -1,
+                       Dir:     tmpBasePath,
+                       Stdout:  &outbuf,
+                       Stderr:  &errbuf,
+               }); err != nil {
                if err := models.RemoveTemporaryPath(tmpBasePath); err != nil {
                        log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err)
                }
index b89d51601afe6c5337dda05a94feaaaf801cd8dd..2223e1c8fda3fe7009c6a4b794774e7fa8a4973b 100644 (file)
@@ -97,7 +97,13 @@ func (t *TemporaryUploadRepository) LsFiles(filenames ...string) ([]string, erro
                }
        }
 
-       if err := git.NewCommand(t.ctx, cmdArgs...).RunInDirPipeline(t.basePath, stdOut, stdErr); err != nil {
+       if err := git.NewCommand(t.ctx, cmdArgs...).
+               RunWithContext(&git.RunContext{
+                       Timeout: -1,
+                       Dir:     t.basePath,
+                       Stdout:  stdOut,
+                       Stderr:  stdErr,
+               }); err != nil {
                log.Error("Unable to run git ls-files for temporary repo: %s (%s) Error: %v\nstdout: %s\nstderr: %s", t.repo.FullName(), t.basePath, err, stdOut.String(), stdErr.String())
                err = fmt.Errorf("Unable to run git ls-files for temporary repo of: %s Error: %v\nstdout: %s\nstderr: %s", t.repo.FullName(), err, stdOut.String(), stdErr.String())
                return nil, err
@@ -124,7 +130,14 @@ func (t *TemporaryUploadRepository) RemoveFilesFromIndex(filenames ...string) er
                }
        }
 
-       if err := git.NewCommand(t.ctx, "update-index", "--remove", "-z", "--index-info").RunInDirFullPipeline(t.basePath, stdOut, stdErr, stdIn); err != nil {
+       if err := git.NewCommand(t.ctx, "update-index", "--remove", "-z", "--index-info").
+               RunWithContext(&git.RunContext{
+                       Timeout: -1,
+                       Dir:     t.basePath,
+                       Stdin:   stdIn,
+                       Stdout:  stdOut,
+                       Stderr:  stdErr,
+               }); err != nil {
                log.Error("Unable to update-index for temporary repo: %s (%s) Error: %v\nstdout: %s\nstderr: %s", t.repo.FullName(), t.basePath, err, stdOut.String(), stdErr.String())
                return fmt.Errorf("Unable to update-index for temporary repo: %s Error: %v\nstdout: %s\nstderr: %s", t.repo.FullName(), err, stdOut.String(), stdErr.String())
        }
@@ -136,7 +149,14 @@ func (t *TemporaryUploadRepository) HashObject(content io.Reader) (string, error
        stdOut := new(bytes.Buffer)
        stdErr := new(bytes.Buffer)
 
-       if err := git.NewCommand(t.ctx, "hash-object", "-w", "--stdin").RunInDirFullPipeline(t.basePath, stdOut, stdErr, content); err != nil {
+       if err := git.NewCommand(t.ctx, "hash-object", "-w", "--stdin").
+               RunWithContext(&git.RunContext{
+                       Timeout: -1,
+                       Dir:     t.basePath,
+                       Stdin:   content,
+                       Stdout:  stdOut,
+                       Stderr:  stdErr,
+               }); err != nil {
                log.Error("Unable to hash-object to temporary repo: %s (%s) Error: %v\nstdout: %s\nstderr: %s", t.repo.FullName(), t.basePath, err, stdOut.String(), stdErr.String())
                return "", fmt.Errorf("Unable to hash-object to temporary repo: %s Error: %v\nstdout: %s\nstderr: %s", t.repo.FullName(), err, stdOut.String(), stdErr.String())
        }
@@ -254,7 +274,15 @@ func (t *TemporaryUploadRepository) CommitTreeWithDate(author, committer *user_m
 
        stdout := new(bytes.Buffer)
        stderr := new(bytes.Buffer)
-       if err := git.NewCommand(t.ctx, args...).RunInDirTimeoutEnvFullPipeline(env, -1, t.basePath, stdout, stderr, messageBytes); err != nil {
+       if err := git.NewCommand(t.ctx, args...).
+               RunWithContext(&git.RunContext{
+                       Env:     env,
+                       Timeout: -1,
+                       Dir:     t.basePath,
+                       Stdin:   messageBytes,
+                       Stdout:  stdout,
+                       Stderr:  stderr,
+               }); err != nil {
                log.Error("Unable to commit-tree in temporary repo: %s (%s) Error: %v\nStdout: %s\nStderr: %s",
                        t.repo.FullName(), t.basePath, err, stdout, stderr)
                return "", fmt.Errorf("Unable to commit-tree in temporary repo: %s Error: %v\nStdout: %s\nStderr: %s",
@@ -304,15 +332,21 @@ func (t *TemporaryUploadRepository) DiffIndex() (*gitdiff.Diff, error) {
        var finalErr error
 
        if err := git.NewCommand(t.ctx, "diff-index", "--src-prefix=\\a/", "--dst-prefix=\\b/", "--cached", "-p", "HEAD").
-               RunInDirTimeoutEnvFullPipelineFunc(nil, 30*time.Second, t.basePath, stdoutWriter, stderr, nil, func(ctx context.Context, cancel context.CancelFunc) error {
-                       _ = stdoutWriter.Close()
-                       diff, finalErr = gitdiff.ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, stdoutReader, "")
-                       if finalErr != nil {
-                               log.Error("ParsePatch: %v", finalErr)
-                               cancel()
-                       }
-                       _ = stdoutReader.Close()
-                       return finalErr
+               RunWithContext(&git.RunContext{
+                       Timeout: 30 * time.Second,
+                       Dir:     t.basePath,
+                       Stdout:  stdoutWriter,
+                       Stderr:  stderr,
+                       PipelineFunc: func(ctx context.Context, cancel context.CancelFunc) error {
+                               _ = stdoutWriter.Close()
+                               diff, finalErr = gitdiff.ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, stdoutReader, "")
+                               if finalErr != nil {
+                                       log.Error("ParsePatch: %v", finalErr)
+                                       cancel()
+                               }
+                               _ = stdoutReader.Close()
+                               return finalErr
+                       },
                }); err != nil {
                if finalErr != nil {
                        log.Error("Unable to ParsePatch in temporary repo %s (%s). Error: %v", t.repo.FullName(), t.basePath, finalErr)