Skip to content

Commit 7de148a

Browse files
committed
split GetDiff
1 parent abc63e9 commit 7de148a

File tree

5 files changed

+32
-19
lines changed

5 files changed

+32
-19
lines changed

routers/api/v1/repo/pull.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1592,7 +1592,7 @@ func GetPullRequestFiles(ctx *context.APIContext) {
15921592

15931593
// FIXME: If there are too many files in the repo, may cause some unpredictable issues.
15941594
// FIXME: it doesn't need to call "GetDiff" to do various parsing and highlighting
1595-
diff, err := gitdiff.GetDiff(ctx, baseGitRepo,
1595+
diff, err := gitdiff.GetDiffForAPI(ctx, baseGitRepo,
15961596
&gitdiff.DiffOptions{
15971597
BeforeCommitID: startCommitID,
15981598
AfterCommitID: endCommitID,

routers/web/repo/commit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ func Diff(ctx *context.Context) {
314314
maxLines, maxFiles = -1, -1
315315
}
316316

317-
diff, err := gitdiff.GetDiff(ctx, gitRepo, &gitdiff.DiffOptions{
317+
diff, err := gitdiff.GetDiffForRender(ctx, gitRepo, &gitdiff.DiffOptions{
318318
AfterCommitID: commitID,
319319
SkipTo: ctx.FormString("skip-to"),
320320
MaxLines: maxLines,

routers/web/repo/compare.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ func PrepareCompareDiff(
614614

615615
fileOnly := ctx.FormBool("file-only")
616616

617-
diff, err := gitdiff.GetDiff(ctx, ci.HeadGitRepo,
617+
diff, err := gitdiff.GetDiffForRender(ctx, ci.HeadGitRepo,
618618
&gitdiff.DiffOptions{
619619
BeforeCommitID: beforeCommitID,
620620
AfterCommitID: headCommitID,

routers/web/repo/pull.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi
749749
diffOptions.BeforeCommitID = startCommitID
750750
}
751751

752-
diff, err := gitdiff.GetDiff(ctx, gitRepo, diffOptions, files...)
752+
diff, err := gitdiff.GetDiffForRender(ctx, gitRepo, diffOptions, files...)
753753
if err != nil {
754754
ctx.ServerError("GetDiff", err)
755755
return

services/gitdiff/gitdiff.go

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ type DiffHTMLOperation struct {
106106
const BlobExcerptChunkSize = 20
107107

108108
// MaxDiffHighlightEntireFileSize is the maximum file size that will be highlighted with "entire file diff"
109-
const MaxDiffHighlightEntireFileSize = 500 * 1024
109+
const MaxDiffHighlightEntireFileSize = 1 * 1024 * 1024
110110

111111
// GetType returns the type of DiffLine.
112112
func (d *DiffLine) GetType() int {
@@ -1160,20 +1160,21 @@ func guessBeforeCommitForDiff(gitRepo *git.Repository, beforeCommitID string, af
11601160
return actualBeforeCommit, actualBeforeCommitID, nil
11611161
}
11621162

1163-
// GetDiff builds a Diff between two commits of a repository.
1163+
// getDiffBasic builds a Diff between two commits of a repository.
11641164
// Passing the empty string as beforeCommitID returns a diff from the parent commit.
11651165
// The whitespaceBehavior is either an empty string or a git flag
1166-
func GetDiff(ctx context.Context, gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff, error) {
1166+
// Returned beforeCommit could be nil if the afterCommit doesn't have parent commit
1167+
func getDiffBasic(ctx context.Context, gitRepo *git.Repository, opts *DiffOptions, files ...string) (_ *Diff, beforeCommit, afterCommit *git.Commit, err error) {
11671168
repoPath := gitRepo.Path
11681169

1169-
afterCommit, err := gitRepo.GetCommit(opts.AfterCommitID)
1170+
afterCommit, err = gitRepo.GetCommit(opts.AfterCommitID)
11701171
if err != nil {
1171-
return nil, err
1172+
return nil, nil, nil, err
11721173
}
11731174

1174-
actualBeforeCommit, actualBeforeCommitID, err := guessBeforeCommitForDiff(gitRepo, opts.BeforeCommitID, afterCommit)
1175+
beforeCommit, beforeCommitID, err := guessBeforeCommitForDiff(gitRepo, opts.BeforeCommitID, afterCommit)
11751176
if err != nil {
1176-
return nil, err
1177+
return nil, nil, nil, err
11771178
}
11781179

11791180
cmdDiff := git.NewCommand().
@@ -1189,7 +1190,7 @@ func GetDiff(ctx context.Context, gitRepo *git.Repository, opts *DiffOptions, fi
11891190
parsePatchSkipToFile = ""
11901191
}
11911192

1192-
cmdDiff.AddDynamicArguments(actualBeforeCommitID.String(), opts.AfterCommitID)
1193+
cmdDiff.AddDynamicArguments(beforeCommitID.String(), opts.AfterCommitID)
11931194
cmdDiff.AddDashesAndList(files...)
11941195

11951196
cmdCtx, cmdCancel := context.WithCancel(ctx)
@@ -1219,12 +1220,25 @@ func GetDiff(ctx context.Context, gitRepo *git.Repository, opts *DiffOptions, fi
12191220
// Ensure the git process is killed if it didn't exit already
12201221
cmdCancel()
12211222
if err != nil {
1222-
return nil, fmt.Errorf("unable to ParsePatch: %w", err)
1223+
return nil, nil, nil, fmt.Errorf("unable to ParsePatch: %w", err)
12231224
}
12241225
diff.Start = opts.SkipTo
1226+
return diff, beforeCommit, afterCommit, nil
1227+
}
1228+
1229+
func GetDiffForAPI(ctx context.Context, gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff, error) {
1230+
diff, _, _, err := getDiffBasic(ctx, gitRepo, opts, files...)
1231+
return diff, err
1232+
}
12251233

1226-
checker, deferable := gitRepo.CheckAttributeReader(opts.AfterCommitID)
1227-
defer deferable()
1234+
func GetDiffForRender(ctx context.Context, gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff, error) {
1235+
diff, beforeCommit, afterCommit, err := getDiffBasic(ctx, gitRepo, opts, files...)
1236+
if err != nil {
1237+
return nil, err
1238+
}
1239+
1240+
checker, deferrable := gitRepo.CheckAttributeReader(opts.AfterCommitID)
1241+
defer deferrable()
12281242

12291243
for _, diffFile := range diff.Files {
12301244
isVendored := optional.None[bool]()
@@ -1244,7 +1258,7 @@ func GetDiff(ctx context.Context, gitRepo *git.Repository, opts *DiffOptions, fi
12441258

12451259
// Populate Submodule URLs
12461260
if diffFile.SubmoduleDiffInfo != nil {
1247-
diffFile.SubmoduleDiffInfo.PopulateURL(diffFile, actualBeforeCommit, afterCommit)
1261+
diffFile.SubmoduleDiffInfo.PopulateURL(diffFile, beforeCommit, afterCommit)
12481262
}
12491263

12501264
if !isVendored.Has() {
@@ -1256,14 +1270,12 @@ func GetDiff(ctx context.Context, gitRepo *git.Repository, opts *DiffOptions, fi
12561270
isGenerated = optional.Some(analyze.IsGenerated(diffFile.Name))
12571271
}
12581272
diffFile.IsGenerated = isGenerated.Value()
1259-
tailSection, limitedContent := diffFile.GetTailSectionAndLimitedContent(actualBeforeCommit, afterCommit)
1273+
tailSection, limitedContent := diffFile.GetTailSectionAndLimitedContent(beforeCommit, afterCommit)
12601274
if tailSection != nil {
12611275
diffFile.Sections = append(diffFile.Sections, tailSection)
12621276
}
12631277

12641278
if !setting.Git.DisableDiffHighlight {
1265-
// FIXME: it's not right to highlight code here for all cases, because this function is also used for non-rendering purpose
1266-
// For example: API
12671279
if limitedContent.LeftContent != nil && limitedContent.LeftContent.buf.Len() < MaxDiffHighlightEntireFileSize {
12681280
diffFile.highlightedOldLines = highlightCodeLines(diffFile, limitedContent.LeftContent.buf.String())
12691281
}
@@ -1272,6 +1284,7 @@ func GetDiff(ctx context.Context, gitRepo *git.Repository, opts *DiffOptions, fi
12721284
}
12731285
}
12741286
}
1287+
12751288
return diff, nil
12761289
}
12771290

0 commit comments

Comments
 (0)