History

Always use git command but not os.Command (#18363)

Lunny Xiao committed 4 years ago

Commit 35fdefc1 parent f066b293 files · +46 −56Browse files

Showing the changes to services/gitdiff/gitdiff.go (1 of 3 files). Show all

Unified · Split

services/gitdiff/gitdiff.go+22−25
@@ -15,7 +15,6 @@ import ( · 14 unmodified lines
15 "io"15 "io"
16 "net/url"16 "net/url"
17 "os"17 "os"
18 "os/exec"
19 "regexp"18 "regexp"
20 "sort"19 "sort"
21 "strings"20 "strings"
@@ -30,7 +29,6 @@ import ( · 8 unmodified lines
30 "code.gitea.io/gitea/modules/highlight"29 "code.gitea.io/gitea/modules/highlight"
31 "code.gitea.io/gitea/modules/lfs"30 "code.gitea.io/gitea/modules/lfs"
32 "code.gitea.io/gitea/modules/log"31 "code.gitea.io/gitea/modules/log"
33 "code.gitea.io/gitea/modules/process"
34 "code.gitea.io/gitea/modules/setting"32 "code.gitea.io/gitea/modules/setting"
35 33
36 "github.com/sergi/go-diff/diffmatchpatch"34 "github.com/sergi/go-diff/diffmatchpatch"
@@ -1322,10 +1320,6 @@ func GetDiff(gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff · 1285 unmodified lines
1322 return nil, err1320 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 argsLength := 61323 argsLength := 6
1330 if len(opts.WhitespaceBehavior) > 0 {1324 if len(opts.WhitespaceBehavior) > 0 {
1331 argsLength++1325 argsLength++
@@ -1375,21 +1369,28 @@ func GetDiff(gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff · 43 unmodified lines
1375 diffArgs = append(diffArgs, files...)1369 diffArgs = append(diffArgs, files...)
1376 }1370 }
1377 1371
1378 cmd := exec.CommandContext(ctx, git.GitExecutable, diffArgs...)1372 reader, writer := io.Pipe()
1379 1373 defer func() {
1380 cmd.Dir = repoPath1374 _ = reader.Close()
1381 cmd.Stderr = os.Stderr1375 _ = writer.Close()
1376 }()
1382 1377
1383 stdout, err := cmd.StdoutPipe()1378 go func(ctx context.Context, diffArgs []string, repoPath string, writer *io.PipeWriter) {
1384 if err != nil {1379 cmd := git.NewCommandContextNoGlobals(ctx, diffArgs...)
1385 return nil, fmt.Errorf("error creating StdoutPipe: %w", err)1380 cmd.SetDescription(fmt.Sprintf("GetDiffRange [repo_path: %s]", repoPath))
1386 }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 {1390 _ = writer.Close()
1389 return nil, fmt.Errorf("error during Start: %w", err)1391 }(gitRepo.Ctx, diffArgs, repoPath, writer)
1390 }
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 if err != nil {1394 if err != nil {
1394 return nil, fmt.Errorf("unable to ParsePatch: %w", err)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 · 12 unmodified lines
1408 IndexFile: indexFilename,1409 IndexFile: indexFilename,
1409 WorkTree: worktree,1410 WorkTree: worktree,
1410 }1411 }
1411 ctx, cancel := context.WithCancel(ctx)1412 ctx, cancel := context.WithCancel(gitRepo.Ctx)
1412 if err := checker.Init(ctx); err != nil {1413 if err := checker.Init(ctx); err != nil {
1413 log.Error("Unable to open checker for %s. Error: %v", opts.AfterCommitID, err)1414 log.Error("Unable to open checker for %s. Error: %v", opts.AfterCommitID, err)
1414 } else {1415 } else {
@@ -1472,10 +1473,6 @@ func GetDiff(gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff · 57 unmodified lines
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 separator := "..."1476 separator := "..."
1480 if opts.DirectComparison {1477 if opts.DirectComparison {
1481 separator = ".."1478 separator = ".."
@@ -1485,12 +1482,12 @@ func GetDiff(gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff · 3 unmodified lines
1485 if len(opts.BeforeCommitID) == 0 || opts.BeforeCommitID == git.EmptySHA {1482 if len(opts.BeforeCommitID) == 0 || opts.BeforeCommitID == git.EmptySHA {
1486 shortstatArgs = []string{git.EmptyTreeSHA, opts.AfterCommitID}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 if err != nil && strings.Contains(err.Error(), "no merge base") {1486 if err != nil && strings.Contains(err.Error(), "no merge base") {
1490 // git >= 2.28 now returns an error if base and head have become unrelated.1487 // git >= 2.28 now returns an error if base and head have become unrelated.
1491 // previously it would return the results of git diff --shortstat base head so let's try that...1488 // previously it would return the results of git diff --shortstat base head so let's try that...
1492 shortstatArgs = []string{opts.BeforeCommitID, opts.AfterCommitID}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 if err != nil {1492 if err != nil {
1496 return nil, err1493 return nil, err

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t