Refactor GetRepoRawDiffForFile to avoid unnecessary pipe or goroutine (#36434)

This commit is contained in:
wxiaoguang
2026-01-23 10:10:11 +08:00
committed by GitHub
parent 5f91c51fa5
commit e42a1dbb6b
4 changed files with 63 additions and 47 deletions

View File

@@ -5,6 +5,7 @@ package git
import (
"bufio"
"context"
"fmt"
"io"
"regexp"
@@ -14,34 +15,64 @@ import (
"code.gitea.io/gitea/modules/git/gitcmd"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
)
// RawDiffType type of a raw diff.
// RawDiffType output format: diff or patch
type RawDiffType string
// RawDiffType possible values.
const (
RawDiffNormal RawDiffType = "diff"
RawDiffPatch RawDiffType = "patch"
)
// GetRawDiff dumps diff results of repository in given commit ID to io.Writer.
func GetRawDiff(repo *Repository, commitID string, diffType RawDiffType, writer io.Writer) error {
return GetRepoRawDiffForFile(repo, "", commitID, diffType, "", writer)
}
// GetRepoRawDiffForFile dumps diff results of file in given commit ID to io.Writer according given repository
func GetRepoRawDiffForFile(repo *Repository, startCommit, endCommit string, diffType RawDiffType, file string, writer io.Writer) error {
commit, err := repo.GetCommit(endCommit)
func GetRawDiff(repo *Repository, commitID string, diffType RawDiffType, writer io.Writer) (retErr error) {
diffOutput, diffFinish, err := getRepoRawDiffForFile(repo.Ctx, repo, "", commitID, diffType, "")
if err != nil {
return err
}
defer func() {
err := diffFinish()
if retErr == nil {
retErr = err // only return command's error if no previous error
}
}()
_, err = io.Copy(writer, diffOutput)
return err
}
// GetFileDiffCutAroundLine cuts the old or new part of the diff of a file around a specific line number
func GetFileDiffCutAroundLine(
repo *Repository, startCommit, endCommit, treePath string,
line int64, old bool, numbersOfLine int,
) (_ string, retErr error) {
diffOutput, diffFinish, err := getRepoRawDiffForFile(repo.Ctx, repo, startCommit, endCommit, RawDiffNormal, treePath)
if err != nil {
return "", err
}
defer func() {
err := diffFinish()
if retErr == nil {
retErr = err // only return command's error if no previous error
}
}()
return CutDiffAroundLine(diffOutput, line, old, numbersOfLine)
}
// getRepoRawDiffForFile returns an io.Reader for the diff results of file in given commit ID
// and a "finish" function to wait for the git command and clean up resources after reading is done.
func getRepoRawDiffForFile(ctx context.Context, repo *Repository, startCommit, endCommit string, diffType RawDiffType, file string) (io.Reader, func() gitcmd.RunStdError, error) {
commit, err := repo.GetCommit(endCommit)
if err != nil {
return nil, nil, err
}
var files []string
if len(file) > 0 {
files = append(files, file)
}
cmd := gitcmd.NewCommand()
cmd := gitcmd.NewCommand().WithDir(repo.Path)
switch diffType {
case RawDiffNormal:
if len(startCommit) != 0 {
@@ -53,7 +84,7 @@ func GetRepoRawDiffForFile(repo *Repository, startCommit, endCommit string, diff
} else {
c, err := commit.Parent(0)
if err != nil {
return err
return nil, nil, err
}
cmd.AddArguments("diff").
AddOptionFormat("--find-renames=%s", setting.Git.DiffRenameSimilarityThreshold).
@@ -68,18 +99,25 @@ func GetRepoRawDiffForFile(repo *Repository, startCommit, endCommit string, diff
} else {
c, err := commit.Parent(0)
if err != nil {
return err
return nil, nil, err
}
query := fmt.Sprintf("%s...%s", endCommit, c.ID.String())
cmd.AddArguments("format-patch", "--no-signature", "--stdout").AddDynamicArguments(query).AddDashesAndList(files...)
}
default:
return fmt.Errorf("invalid diffType: %s", diffType)
return nil, nil, util.NewInvalidArgumentErrorf("invalid diff type: %s", diffType)
}
return cmd.WithDir(repo.Path).
WithStdoutCopy(writer).
RunWithStderr(repo.Ctx)
stdoutReader, stdoutReaderClose := cmd.MakeStdoutPipe()
err = cmd.StartWithStderr(ctx)
if err != nil {
stdoutReaderClose()
return nil, nil, err
}
return stdoutReader, func() gitcmd.RunStdError {
stdoutReaderClose()
return cmd.WaitWithStderr()
}, nil
}
// ParseDiffHunkString parse the diff hunk content and return