enhance: truncate but show long lines in diffs (#39279)

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
Sergio Benitez
2026-09-12 13:56:25 +02:00
committed by GitHub
parent da37b7916b
commit 1e13badb39
13 changed files with 430 additions and 222 deletions
+9 -1
View File
@@ -140,6 +140,14 @@ func isHeader(lof string, inHunk bool) bool {
return strings.HasPrefix(lof, cmdDiffHead) || (!inHunk && (strings.HasPrefix(lof, "---") || strings.HasPrefix(lof, "+++")))
}
func NewGitDiffScanner(r io.Reader) *bufio.Scanner {
// TODO: GIT-DIFF-PARSE-LONG-LINE: ideally it shouldn't use bufio.Scanner which has a limit.
// It will cause errors if a line is very long.
scanner := bufio.NewScanner(r)
scanner.Buffer(nil, max(512*1024, int(setting.UI.MaxDisplayFileSize/16)))
return scanner
}
// CutDiffAroundLine cuts a diff of a file in way that only the given line + numberOfLine above it will be shown
// it also recalculates hunks and adds the appropriate headers to the new diff.
// Warning: Only one-file diffs are allowed.
@@ -149,7 +157,7 @@ func CutDiffAroundLine(originalDiff io.Reader, line int64, old bool, numbersOfLi
return "", nil
}
scanner := bufio.NewScanner(originalDiff)
scanner := NewGitDiffScanner(originalDiff)
hunk := make([]string, 0)
// begin is the start of the hunk containing searched line