Skip to content

Commit c80d147

Browse files
mrexodialafriks
authored andcommitted
Improve memory usage when reaching diff limits (#2990)
Signed-off-by: Duncan Ogilvie <[email protected]>
1 parent d39b88a commit c80d147

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

models/git_diff.go

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -252,19 +252,27 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D
252252
input := bufio.NewReader(reader)
253253
isEOF := false
254254
for !isEOF {
255-
line, err := input.ReadString('\n')
256-
if err != nil {
257-
if err == io.EOF {
258-
isEOF = true
259-
} else {
260-
return nil, fmt.Errorf("ReadString: %v", err)
255+
var linebuf bytes.Buffer
256+
for {
257+
b, err := input.ReadByte()
258+
if err != nil {
259+
if err == io.EOF {
260+
isEOF = true
261+
break
262+
} else {
263+
return nil, fmt.Errorf("ReadByte: %v", err)
264+
}
265+
}
266+
if b == '\n' {
267+
break
268+
}
269+
if linebuf.Len() < maxLineCharacters {
270+
linebuf.WriteByte(b)
271+
} else if linebuf.Len() == maxLineCharacters {
272+
curFile.IsIncomplete = true
261273
}
262274
}
263-
264-
if len(line) > 0 && line[len(line)-1] == '\n' {
265-
// Remove line break.
266-
line = line[:len(line)-1]
267-
}
275+
line := linebuf.String()
268276

269277
if strings.HasPrefix(line, "+++ ") || strings.HasPrefix(line, "--- ") || len(line) == 0 {
270278
continue
@@ -295,7 +303,7 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D
295303
lineCount++
296304

297305
// Diff data too large, we only show the first about maxLines lines
298-
if curFileLinesCount >= maxLines || len(line) >= maxLineCharacters {
306+
if curFileLinesCount >= maxLines {
299307
curFile.IsIncomplete = true
300308
}
301309

0 commit comments

Comments
 (0)