From 5cb0c9aa0d7eed087055b1efca79628957207d36 Mon Sep 17 00:00:00 2001 From: zeripath Date: Wed, 19 Jan 2022 23:26:57 +0000 Subject: Propagate context and ensure git commands run in request context (#17868) This PR continues the work in #17125 by progressively ensuring that git commands run within the request context. This now means that the if there is a git repo already open in the context it will be used instead of reopening it. Signed-off-by: Andrew Thornton --- modules/git/diff.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'modules/git/diff.go') diff --git a/modules/git/diff.go b/modules/git/diff.go index f90f911be0..02ed2c60c4 100644 --- a/modules/git/diff.go +++ b/modules/git/diff.go @@ -30,17 +30,17 @@ const ( ) // GetRawDiff dumps diff results of repository in given commit ID to io.Writer. -func GetRawDiff(repoPath, commitID string, diffType RawDiffType, writer io.Writer) error { - return GetRawDiffForFile(repoPath, "", commitID, diffType, "", writer) +func GetRawDiff(ctx context.Context, repoPath, commitID string, diffType RawDiffType, writer io.Writer) error { + return GetRawDiffForFile(ctx, repoPath, "", commitID, diffType, "", writer) } // GetRawDiffForFile dumps diff results of file in given commit ID to io.Writer. -func GetRawDiffForFile(repoPath, startCommit, endCommit string, diffType RawDiffType, file string, writer io.Writer) error { - repo, err := OpenRepository(repoPath) +func GetRawDiffForFile(ctx context.Context, repoPath, startCommit, endCommit string, diffType RawDiffType, file string, writer io.Writer) error { + repo, closer, err := RepositoryFromContextOrOpen(ctx, repoPath) if err != nil { return fmt.Errorf("OpenRepository: %v", err) } - defer repo.Close() + defer closer.Close() return GetRepoRawDiffForFile(repo, startCommit, endCommit, diffType, file, writer) } @@ -276,7 +276,7 @@ func CutDiffAroundLine(originalDiff io.Reader, line int64, old bool, numbersOfLi } // GetAffectedFiles returns the affected files between two commits -func GetAffectedFiles(oldCommitID, newCommitID string, env []string, repo *Repository) ([]string, error) { +func GetAffectedFiles(repo *Repository, oldCommitID, newCommitID string, env []string) ([]string, error) { stdoutReader, stdoutWriter, err := os.Pipe() if err != nil { log.Error("Unable to create os.Pipe for %s", repo.Path) @@ -290,7 +290,7 @@ func GetAffectedFiles(oldCommitID, newCommitID string, env []string, repo *Repos affectedFiles := make([]string, 0, 32) // Run `git diff --name-only` to get the names of the changed files - err = NewCommand("diff", "--name-only", oldCommitID, newCommitID). + err = NewCommandContext(repo.Ctx, "diff", "--name-only", oldCommitID, newCommitID). RunInDirTimeoutEnvFullPipelineFunc(env, -1, repo.Path, stdoutWriter, nil, nil, func(ctx context.Context, cancel context.CancelFunc) error { -- cgit v1.2.3