@@ -15,7 +15,6 @@ import (
15
15
"io"
16
16
"net/url"
17
17
"os"
18
- "os/exec"
19
18
"regexp"
20
19
"sort"
21
20
"strings"
@@ -30,7 +29,6 @@ import (
30
29
"code.gitea.io/gitea/modules/highlight"
31
30
"code.gitea.io/gitea/modules/lfs"
32
31
"code.gitea.io/gitea/modules/log"
33
- "code.gitea.io/gitea/modules/process"
34
32
"code.gitea.io/gitea/modules/setting"
35
33
36
34
"github.com/sergi/go-diff/diffmatchpatch"
@@ -1322,10 +1320,6 @@ func GetDiff(gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff
1322
1320
return nil , err
1323
1321
}
1324
1322
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
-
1329
1323
argsLength := 6
1330
1324
if len (opts .WhitespaceBehavior ) > 0 {
1331
1325
argsLength ++
@@ -1375,21 +1369,28 @@ func GetDiff(gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff
1375
1369
diffArgs = append (diffArgs , files ... )
1376
1370
}
1377
1371
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
+ }()
1382
1377
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
+ }
1387
1389
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 )
1391
1392
1392
- diff , err := ParsePatch (opts .MaxLines , opts .MaxLineCharacters , opts .MaxFiles , stdout , parsePatchSkipToFile )
1393
+ diff , err := ParsePatch (opts .MaxLines , opts .MaxLineCharacters , opts .MaxFiles , reader , parsePatchSkipToFile )
1393
1394
if err != nil {
1394
1395
return nil , fmt .Errorf ("unable to ParsePatch: %w" , err )
1395
1396
}
@@ -1408,7 +1409,7 @@ func GetDiff(gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff
1408
1409
IndexFile : indexFilename ,
1409
1410
WorkTree : worktree ,
1410
1411
}
1411
- ctx , cancel := context .WithCancel (ctx )
1412
+ ctx , cancel := context .WithCancel (gitRepo . Ctx )
1412
1413
if err := checker .Init (ctx ); err != nil {
1413
1414
log .Error ("Unable to open checker for %s. Error: %v" , opts .AfterCommitID , err )
1414
1415
} else {
@@ -1472,10 +1473,6 @@ func GetDiff(gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff
1472
1473
}
1473
1474
}
1474
1475
1475
- if err = cmd .Wait (); err != nil {
1476
- return nil , fmt .Errorf ("error during cmd.Wait: %w" , err )
1477
- }
1478
-
1479
1476
separator := "..."
1480
1477
if opts .DirectComparison {
1481
1478
separator = ".."
@@ -1485,12 +1482,12 @@ func GetDiff(gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff
1485
1482
if len (opts .BeforeCommitID ) == 0 || opts .BeforeCommitID == git .EmptySHA {
1486
1483
shortstatArgs = []string {git .EmptyTreeSHA , opts .AfterCommitID }
1487
1484
}
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 ... )
1489
1486
if err != nil && strings .Contains (err .Error (), "no merge base" ) {
1490
1487
// git >= 2.28 now returns an error if base and head have become unrelated.
1491
1488
// previously it would return the results of git diff --shortstat base head so let's try that...
1492
1489
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 ... )
1494
1491
}
1495
1492
if err != nil {
1496
1493
return nil , err
0 commit comments