Skip to content

Commit 424a57e

Browse files
committed
add a new config option
Signed-off-by: a1012112796 <[email protected]>
1 parent cf151a5 commit 424a57e

File tree

5 files changed

+38
-14
lines changed

5 files changed

+38
-14
lines changed

custom/conf/app.example.ini

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,12 @@ ROUTER = console
691691
;; Disables highlight of added and removed changes
692692
;DISABLE_DIFF_HIGHLIGHT = false
693693
;;
694+
;; max size of one file that can be full highlight on git diff ui.
695+
;; if the file is too big, it will be renderd line by line,
696+
;; sometimes it will lead a not good highlight result.
697+
;;
698+
;MAX_DIFF_HIGHLIGHT_FILE_SIZE = 5 * 1024
699+
;;
694700
;; Max number of lines allowed in a single file in diff view
695701
;MAX_GIT_DIFF_LINES = 1000
696702
;;

docs/content/doc/administration/config-cheat-sheet.en-us.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,7 @@ Default templates for project boards:
10811081
- `HOME_PATH`: **%(APP_DATA_PATH)s/home**: The HOME directory for Git.
10821082
This directory will be used to contain the `.gitconfig` and possible `.gnupg` directories that Gitea's git calls will use. If you can confirm Gitea is the only application running in this environment, you can set it to the normal home directory for Gitea user.
10831083
- `DISABLE_DIFF_HIGHLIGHT`: **false**: Disables highlight of added and removed changes.
1084+
- `MAX_DIFF_HIGHLIGHT_FILE_SIZE`: **5120** max size of one file that can be full highlight on git diff ui.
10841085
- `MAX_GIT_DIFF_LINES`: **1000**: Max number of lines allowed of a single file in diff view.
10851086
- `MAX_GIT_DIFF_LINE_CHARACTERS`: **5000**: Max character count per line highlighted in diff view.
10861087
- `MAX_GIT_DIFF_FILES`: **100**: Max number of files shown in diff view.

modules/setting/git.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ var Git = struct {
4040
Pull int
4141
GC int `ini:"GC"`
4242
} `ini:"git.timeout"`
43+
MaxDiffHighlightFileSize int64
4344
}{
4445
Reflog: struct {
4546
Enabled bool
@@ -76,6 +77,7 @@ var Git = struct {
7677
Pull: 300,
7778
GC: 60,
7879
},
80+
MaxDiffHighlightFileSize: 5 * 1024,
7981
}
8082

8183
func loadGitFrom(rootCfg ConfigProvider) {

routers/web/repo/compare.go

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -932,20 +932,31 @@ func getExcerptLines(commit *git.Commit, filePath string, idxLeft, idxRight, chu
932932
return nil, err
933933
}
934934

935-
content, err := io.ReadAll(reader)
936-
_ = reader.Close()
937-
if err != nil {
938-
log.Error("io.ReadAll: %v", err)
939-
return nil, err
940-
}
935+
var (
936+
scanner *bufio.Scanner
937+
highlightedContent []string
938+
)
941939

942-
highlightedContent, _, err := highlight.File(filePath, "", content)
943-
if err != nil {
944-
log.Error("highlight.File: %v", err)
945-
return nil, err
940+
if blob.Size() >= setting.Git.MaxDiffHighlightFileSize {
941+
defer reader.Close()
942+
scanner = bufio.NewScanner(reader)
943+
} else {
944+
content, err := io.ReadAll(reader)
945+
_ = reader.Close()
946+
if err != nil {
947+
log.Error("io.ReadAll: %v", err)
948+
return nil, err
949+
}
950+
951+
highlightedContent, _, err = highlight.File(filePath, "", content)
952+
if err != nil {
953+
log.Error("highlight.File: %v", err)
954+
return nil, err
955+
}
956+
957+
scanner = bufio.NewScanner(bytes.NewBuffer(content))
946958
}
947959

948-
scanner := bufio.NewScanner(bytes.NewBuffer(content))
949960
var diffLines []*gitdiff.DiffLine
950961
for line := 0; line < idxRight+chunkSize; line++ {
951962
if ok := scanner.Scan(); !ok {
@@ -960,9 +971,13 @@ func getExcerptLines(commit *git.Commit, filePath string, idxLeft, idxRight, chu
960971
RightIdx: line + 1,
961972
Type: gitdiff.DiffLinePlain,
962973
Content: " " + lineText,
963-
HighlightContent: highlightedContent[line],
964-
HasHighlightContent: true,
965974
}
975+
976+
if highlightedContent != nil {
977+
diffLine.HighlightContent = highlightedContent[line]
978+
diffLine.HasHighlightContent = true
979+
}
980+
966981
diffLines = append(diffLines, diffLine)
967982
}
968983
return diffLines, nil

services/gitdiff/gitdiff.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1116,7 +1116,7 @@ func GetDiff(gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff
11161116
return nil, ""
11171117
}
11181118

1119-
if entry.Blob().Size() >= setting.UI.MaxDisplayFileSize/2 {
1119+
if entry.Blob().Size() >= setting.Git.MaxDiffHighlightFileSize {
11201120
return nil, ""
11211121
}
11221122

0 commit comments

Comments
 (0)