Skip to content

Commit c54639b

Browse files
authored
Repare and Improve GetDiffRangeWithWhitespaceBehavior (#16894) (#16895)
fix pipe leak
1 parent 49a71a6 commit c54639b

File tree

5 files changed

+26
-37
lines changed

5 files changed

+26
-37
lines changed

Diff for: routers/web/repo/commit.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,8 @@ func Diff(ctx *context.Context) {
268268
repoName := ctx.Repo.Repository.Name
269269
commitID := ctx.Params(":sha")
270270
var (
271-
gitRepo *git.Repository
272-
err error
273-
repoPath string
271+
gitRepo *git.Repository
272+
err error
274273
)
275274

276275
if ctx.Data["PageIsWiki"] != nil {
@@ -279,10 +278,9 @@ func Diff(ctx *context.Context) {
279278
ctx.ServerError("Repo.GitRepo.GetCommit", err)
280279
return
281280
}
282-
repoPath = ctx.Repo.Repository.WikiPath()
281+
defer gitRepo.Close()
283282
} else {
284283
gitRepo = ctx.Repo.GitRepo
285-
repoPath = models.RepoPath(userName, repoName)
286284
}
287285

288286
commit, err := gitRepo.GetCommit(commitID)
@@ -306,7 +304,7 @@ func Diff(ctx *context.Context) {
306304
ctx.Data["CommitStatus"] = models.CalcCommitStatus(statuses)
307305
ctx.Data["CommitStatuses"] = statuses
308306

309-
diff, err := gitdiff.GetDiffCommitWithWhitespaceBehavior(repoPath,
307+
diff, err := gitdiff.GetDiffCommitWithWhitespaceBehavior(gitRepo,
310308
commitID, setting.Git.MaxGitDiffLines,
311309
setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles,
312310
gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)))

Diff for: routers/web/repo/compare.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ func PrepareCompareDiff(
526526
return true
527527
}
528528

529-
diff, err := gitdiff.GetDiffRangeWithWhitespaceBehavior(models.RepoPath(headUser.Name, headRepo.Name),
529+
diff, err := gitdiff.GetDiffRangeWithWhitespaceBehavior(headGitRepo,
530530
compareInfo.MergeBase, headCommitID, setting.Git.MaxGitDiffLines,
531531
setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, whitespaceBehavior)
532532
if err != nil {
@@ -618,11 +618,15 @@ func getBranchesAndTagsForRepo(user *models.User, repo *models.Repository) (bool
618618
// CompareDiff show different from one commit to another commit
619619
func CompareDiff(ctx *context.Context) {
620620
headUser, headRepo, headGitRepo, compareInfo, baseBranch, headBranch := ParseCompareInfo(ctx)
621+
defer func() {
622+
if headGitRepo != nil {
623+
headGitRepo.Close()
624+
}
625+
}()
621626

622627
if ctx.Written() {
623628
return
624629
}
625-
defer headGitRepo.Close()
626630

627631
nothingToCompare := PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, compareInfo, baseBranch, headBranch,
628632
gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)))

Diff for: routers/web/repo/pull.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -587,10 +587,9 @@ func ViewPullFiles(ctx *context.Context) {
587587
pull := issue.PullRequest
588588

589589
var (
590-
diffRepoPath string
591590
startCommitID string
592591
endCommitID string
593-
gitRepo *git.Repository
592+
gitRepo = ctx.Repo.GitRepo
594593
)
595594

596595
var prInfo *git.CompareInfo
@@ -607,9 +606,6 @@ func ViewPullFiles(ctx *context.Context) {
607606
return
608607
}
609608

610-
diffRepoPath = ctx.Repo.GitRepo.Path
611-
gitRepo = ctx.Repo.GitRepo
612-
613609
headCommitID, err := gitRepo.GetRefCommitID(pull.GetGitRefName())
614610
if err != nil {
615611
ctx.ServerError("GetRefCommitID", err)
@@ -623,7 +619,7 @@ func ViewPullFiles(ctx *context.Context) {
623619
ctx.Data["Reponame"] = ctx.Repo.Repository.Name
624620
ctx.Data["AfterCommitID"] = endCommitID
625621

626-
diff, err := gitdiff.GetDiffRangeWithWhitespaceBehavior(diffRepoPath,
622+
diff, err := gitdiff.GetDiffRangeWithWhitespaceBehavior(gitRepo,
627623
startCommitID, endCommitID, setting.Git.MaxGitDiffLines,
628624
setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles,
629625
gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)))

Diff for: services/gitdiff/gitdiff.go

+7-22
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"regexp"
2121
"sort"
2222
"strings"
23+
"time"
2324

2425
"code.gitea.io/gitea/models"
2526
"code.gitea.io/gitea/modules/charset"
@@ -1205,31 +1206,20 @@ func readFileName(rd *strings.Reader) (string, bool) {
12051206
return name[2:], ambiguity
12061207
}
12071208

1208-
// GetDiffRange builds a Diff between two commits of a repository.
1209-
// passing the empty string as beforeCommitID returns a diff from the
1210-
// parent commit.
1211-
func GetDiffRange(repoPath, beforeCommitID, afterCommitID string, maxLines, maxLineCharacters, maxFiles int) (*Diff, error) {
1212-
return GetDiffRangeWithWhitespaceBehavior(repoPath, beforeCommitID, afterCommitID, maxLines, maxLineCharacters, maxFiles, "")
1213-
}
1214-
12151209
// GetDiffRangeWithWhitespaceBehavior builds a Diff between two commits of a repository.
12161210
// Passing the empty string as beforeCommitID returns a diff from the parent commit.
12171211
// The whitespaceBehavior is either an empty string or a git flag
1218-
func GetDiffRangeWithWhitespaceBehavior(repoPath, beforeCommitID, afterCommitID string, maxLines, maxLineCharacters, maxFiles int, whitespaceBehavior string) (*Diff, error) {
1219-
gitRepo, err := git.OpenRepository(repoPath)
1220-
if err != nil {
1221-
return nil, err
1222-
}
1223-
defer gitRepo.Close()
1212+
func GetDiffRangeWithWhitespaceBehavior(gitRepo *git.Repository, beforeCommitID, afterCommitID string, maxLines, maxLineCharacters, maxFiles int, whitespaceBehavior string) (*Diff, error) {
1213+
repoPath := gitRepo.Path
12241214

12251215
commit, err := gitRepo.GetCommit(afterCommitID)
12261216
if err != nil {
12271217
return nil, err
12281218
}
12291219

1230-
// FIXME: graceful: These commands should likely have a timeout
1231-
ctx, cancel := context.WithCancel(git.DefaultContext)
1220+
ctx, cancel := context.WithTimeout(git.DefaultContext, time.Duration(setting.Git.Timeout.Default)*time.Second)
12321221
defer cancel()
1222+
12331223
var cmd *exec.Cmd
12341224
if (len(beforeCommitID) == 0 || beforeCommitID == git.EmptySHA) && commit.ParentCount() == 0 {
12351225
diffArgs := []string{"diff", "--src-prefix=\\a/", "--dst-prefix=\\b/", "-M"}
@@ -1303,15 +1293,10 @@ func GetDiffRangeWithWhitespaceBehavior(repoPath, beforeCommitID, afterCommitID
13031293
return diff, nil
13041294
}
13051295

1306-
// GetDiffCommit builds a Diff representing the given commitID.
1307-
func GetDiffCommit(repoPath, commitID string, maxLines, maxLineCharacters, maxFiles int) (*Diff, error) {
1308-
return GetDiffRangeWithWhitespaceBehavior(repoPath, "", commitID, maxLines, maxLineCharacters, maxFiles, "")
1309-
}
1310-
13111296
// GetDiffCommitWithWhitespaceBehavior builds a Diff representing the given commitID.
13121297
// The whitespaceBehavior is either an empty string or a git flag
1313-
func GetDiffCommitWithWhitespaceBehavior(repoPath, commitID string, maxLines, maxLineCharacters, maxFiles int, whitespaceBehavior string) (*Diff, error) {
1314-
return GetDiffRangeWithWhitespaceBehavior(repoPath, "", commitID, maxLines, maxLineCharacters, maxFiles, whitespaceBehavior)
1298+
func GetDiffCommitWithWhitespaceBehavior(gitRepo *git.Repository, commitID string, maxLines, maxLineCharacters, maxFiles int, whitespaceBehavior string) (*Diff, error) {
1299+
return GetDiffRangeWithWhitespaceBehavior(gitRepo, "", commitID, maxLines, maxLineCharacters, maxFiles, whitespaceBehavior)
13151300
}
13161301

13171302
// CommentAsDiff returns c.Patch as *Diff

Diff for: services/gitdiff/gitdiff_test.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"testing"
1414

1515
"code.gitea.io/gitea/models"
16+
"code.gitea.io/gitea/modules/git"
1617
"code.gitea.io/gitea/modules/highlight"
1718
"code.gitea.io/gitea/modules/setting"
1819
jsoniter "github.com/json-iterator/go"
@@ -513,8 +514,13 @@ func TestDiffLine_GetCommentSide(t *testing.T) {
513514
}
514515

515516
func TestGetDiffRangeWithWhitespaceBehavior(t *testing.T) {
517+
gitRepo, err := git.OpenRepository("./testdata/academic-module")
518+
if !assert.NoError(t, err) {
519+
return
520+
}
521+
defer gitRepo.Close()
516522
for _, behavior := range []string{"-w", "--ignore-space-at-eol", "-b", ""} {
517-
diffs, err := GetDiffRangeWithWhitespaceBehavior("./testdata/academic-module", "559c156f8e0178b71cb44355428f24001b08fc68", "bd7063cc7c04689c4d082183d32a604ed27a24f9",
523+
diffs, err := GetDiffRangeWithWhitespaceBehavior(gitRepo, "559c156f8e0178b71cb44355428f24001b08fc68", "bd7063cc7c04689c4d082183d32a604ed27a24f9",
518524
setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffFiles, behavior)
519525
assert.NoError(t, err, fmt.Sprintf("Error when diff with %s", behavior))
520526
for _, f := range diffs.Files {

0 commit comments

Comments
 (0)