Browse Source

Improve memory usage when reaching diff limits (#2990)

Signed-off-by: Duncan Ogilvie <mr.exodia.tpodt@gmail.com>
Duncan Ogilvie 2 years ago
parent
commit
c80d147fa9
1 changed files with 20 additions and 12 deletions
  1. 20 12
      models/git_diff.go

+ 20 - 12
models/git_diff.go

@@ -252,19 +252,27 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D
252 252
 	input := bufio.NewReader(reader)
253 253
 	isEOF := false
254 254
 	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
261 273
 			}
262 274
 		}
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()
268 276
 
269 277
 		if strings.HasPrefix(line, "+++ ") || strings.HasPrefix(line, "--- ") || len(line) == 0 {
270 278
 			continue
@@ -295,7 +303,7 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D
295 303
 		lineCount++
296 304
 
297 305
 		// Diff data too large, we only show the first about maxLines lines
298
-		if curFileLinesCount >= maxLines || len(line) >= maxLineCharacters {
306
+		if curFileLinesCount >= maxLines {
299 307
 			curFile.IsIncomplete = true
300 308
 		}
301 309