@@ -15,7 +15,6 @@ import (
1515 "io"
1616 "net/url"
1717 "os"
18- "os/exec"
1918 "regexp"
2019 "sort"
2120 "strings"
@@ -30,7 +29,6 @@ import (
3029 "code.gitea.io/gitea/modules/highlight"
3130 "code.gitea.io/gitea/modules/lfs"
3231 "code.gitea.io/gitea/modules/log"
33- "code.gitea.io/gitea/modules/process"
3432 "code.gitea.io/gitea/modules/setting"
3533
3634 "github.com/sergi/go-diff/diffmatchpatch"
@@ -1322,10 +1320,6 @@ func GetDiff(gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff
13221320 return nil , err
13231321 }
13241322
1325- timeout := time .Duration (setting .Git .Timeout .Default ) * time .Second
1326- ctx , _ , finished := process .GetManager ().AddContextTimeout (gitRepo .Ctx , timeout , fmt .Sprintf ("GetDiffRange [repo_path: %s]" , repoPath ))
1327- defer finished ()
1328-
13291323 argsLength := 6
13301324 if len (opts .WhitespaceBehavior ) > 0 {
13311325 argsLength ++
@@ -1375,21 +1369,28 @@ func GetDiff(gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff
13751369 diffArgs = append (diffArgs , files ... )
13761370 }
13771371
1378- cmd := exec .CommandContext (ctx , git .GitExecutable , diffArgs ... )
1379-
1380- cmd .Dir = repoPath
1381- cmd .Stderr = os .Stderr
1372+ reader , writer := io .Pipe ()
1373+ defer func () {
1374+ _ = reader .Close ()
1375+ _ = writer .Close ()
1376+ }()
13821377
1383- stdout , err := cmd .StdoutPipe ()
1384- if err != nil {
1385- return nil , fmt .Errorf ("error creating StdoutPipe: %w" , err )
1386- }
1378+ go func (ctx context.Context , diffArgs []string , repoPath string , writer * io.PipeWriter ) {
1379+ cmd := git .NewCommandContextNoGlobals (ctx , diffArgs ... )
1380+ cmd .SetDescription (fmt .Sprintf ("GetDiffRange [repo_path: %s]" , repoPath ))
1381+ if err := cmd .RunWithContext (& git.RunContext {
1382+ Timeout : time .Duration (setting .Git .Timeout .Default ) * time .Second ,
1383+ Dir : repoPath ,
1384+ Stderr : os .Stderr ,
1385+ Stdout : writer ,
1386+ }); err != nil {
1387+ log .Error ("error during RunWithContext: %w" , err )
1388+ }
13871389
1388- if err = cmd .Start (); err != nil {
1389- return nil , fmt .Errorf ("error during Start: %w" , err )
1390- }
1390+ _ = writer .Close ()
1391+ }(gitRepo .Ctx , diffArgs , repoPath , writer )
13911392
1392- diff , err := ParsePatch (opts .MaxLines , opts .MaxLineCharacters , opts .MaxFiles , stdout , parsePatchSkipToFile )
1393+ diff , err := ParsePatch (opts .MaxLines , opts .MaxLineCharacters , opts .MaxFiles , reader , parsePatchSkipToFile )
13931394 if err != nil {
13941395 return nil , fmt .Errorf ("unable to ParsePatch: %w" , err )
13951396 }
@@ -1408,7 +1409,7 @@ func GetDiff(gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff
14081409 IndexFile : indexFilename ,
14091410 WorkTree : worktree ,
14101411 }
1411- ctx , cancel := context .WithCancel (ctx )
1412+ ctx , cancel := context .WithCancel (gitRepo . Ctx )
14121413 if err := checker .Init (ctx ); err != nil {
14131414 log .Error ("Unable to open checker for %s. Error: %v" , opts .AfterCommitID , err )
14141415 } else {
@@ -1472,10 +1473,6 @@ func GetDiff(gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff
14721473 }
14731474 }
14741475
1475- if err = cmd .Wait (); err != nil {
1476- return nil , fmt .Errorf ("error during cmd.Wait: %w" , err )
1477- }
1478-
14791476 separator := "..."
14801477 if opts .DirectComparison {
14811478 separator = ".."
@@ -1485,12 +1482,12 @@ func GetDiff(gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff
14851482 if len (opts .BeforeCommitID ) == 0 || opts .BeforeCommitID == git .EmptySHA {
14861483 shortstatArgs = []string {git .EmptyTreeSHA , opts .AfterCommitID }
14871484 }
1488- diff .NumFiles , diff .TotalAddition , diff .TotalDeletion , err = git .GetDiffShortStat (ctx , repoPath , shortstatArgs ... )
1485+ diff .NumFiles , diff .TotalAddition , diff .TotalDeletion , err = git .GetDiffShortStat (gitRepo . Ctx , repoPath , shortstatArgs ... )
14891486 if err != nil && strings .Contains (err .Error (), "no merge base" ) {
14901487 // git >= 2.28 now returns an error if base and head have become unrelated.
14911488 // previously it would return the results of git diff --shortstat base head so let's try that...
14921489 shortstatArgs = []string {opts .BeforeCommitID , opts .AfterCommitID }
1493- diff .NumFiles , diff .TotalAddition , diff .TotalDeletion , err = git .GetDiffShortStat (ctx , repoPath , shortstatArgs ... )
1490+ diff .NumFiles , diff .TotalAddition , diff .TotalDeletion , err = git .GetDiffShortStat (gitRepo . Ctx , repoPath , shortstatArgs ... )
14941491 }
14951492 if err != nil {
14961493 return nil , err
0 commit comments