@@ -106,7 +106,7 @@ type DiffHTMLOperation struct {
106106const 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.
112112func (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