Skip to content

Commit bb4cc87

Browse files
authored
Repare and Improve GetDiffRangeWithWhitespaceBehavior (#16894)
* repare and improve GetDiffRangeWithWhitespaceBehavior * Context with Timeout
1 parent f2b4b0f commit bb4cc87

File tree

5 files changed

+26
-37
lines changed

5 files changed

+26
-37
lines changed

routers/web/repo/commit.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,8 @@ func Diff(ctx *context.Context) {
259259
repoName := ctx.Repo.Repository.Name
260260
commitID := ctx.Params(":sha")
261261
var (
262-
gitRepo *git.Repository
263-
err error
264-
repoPath string
262+
gitRepo *git.Repository
263+
err error
265264
)
266265

267266
if ctx.Data["PageIsWiki"] != nil {
@@ -270,10 +269,9 @@ func Diff(ctx *context.Context) {
270269
ctx.ServerError("Repo.GitRepo.GetCommit", err)
271270
return
272271
}
273-
repoPath = ctx.Repo.Repository.WikiPath()
272+
defer gitRepo.Close()
274273
} else {
275274
gitRepo = ctx.Repo.GitRepo
276-
repoPath = models.RepoPath(userName, repoName)
277275
}
278276

279277
commit, err := gitRepo.GetCommit(commitID)
@@ -297,7 +295,7 @@ func Diff(ctx *context.Context) {
297295
ctx.Data["CommitStatus"] = models.CalcCommitStatus(statuses)
298296
ctx.Data["CommitStatuses"] = statuses
299297

300-
diff, err := gitdiff.GetDiffCommitWithWhitespaceBehavior(repoPath,
298+
diff, err := gitdiff.GetDiffCommitWithWhitespaceBehavior(gitRepo,
301299
commitID, setting.Git.MaxGitDiffLines,
302300
setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles,
303301
gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)))

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 {
@@ -616,11 +616,15 @@ func getBranchesAndTagsForRepo(user *models.User, repo *models.Repository) (bool
616616
// CompareDiff show different from one commit to another commit
617617
func CompareDiff(ctx *context.Context) {
618618
headUser, headRepo, headGitRepo, compareInfo, baseBranch, headBranch := ParseCompareInfo(ctx)
619+
defer func() {
620+
if headGitRepo != nil {
621+
headGitRepo.Close()
622+
}
623+
}()
619624

620625
if ctx.Written() {
621626
return
622627
}
623-
defer headGitRepo.Close()
624628

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

routers/web/repo/pull.go

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

601601
var (
602-
diffRepoPath string
603602
startCommitID string
604603
endCommitID string
605-
gitRepo *git.Repository
604+
gitRepo = ctx.Repo.GitRepo
606605
)
607606

608607
var prInfo *git.CompareInfo
@@ -619,9 +618,6 @@ func ViewPullFiles(ctx *context.Context) {
619618
return
620619
}
621620

622-
diffRepoPath = ctx.Repo.GitRepo.Path
623-
gitRepo = ctx.Repo.GitRepo
624-
625621
headCommitID, err := gitRepo.GetRefCommitID(pull.GetGitRefName())
626622
if err != nil {
627623
ctx.ServerError("GetRefCommitID", err)
@@ -635,7 +631,7 @@ func ViewPullFiles(ctx *context.Context) {
635631
ctx.Data["Reponame"] = ctx.Repo.Repository.Name
636632
ctx.Data["AfterCommitID"] = endCommitID
637633

638-
diff, err := gitdiff.GetDiffRangeWithWhitespaceBehavior(diffRepoPath,
634+
diff, err := gitdiff.GetDiffRangeWithWhitespaceBehavior(gitRepo,
639635
startCommitID, endCommitID, setting.Git.MaxGitDiffLines,
640636
setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles,
641637
gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)))

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

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/json"
1819
"code.gitea.io/gitea/modules/setting"
@@ -514,8 +515,13 @@ func TestDiffLine_GetCommentSide(t *testing.T) {
514515
}
515516

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

0 commit comments

Comments
 (0)