forked from p85947160/gitea
Make ParsePatch more performant
This commit is contained in:
parent
d644e88107
commit
5b17f054c5
|
@ -247,31 +247,55 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D
|
||||||
lineCount int
|
lineCount int
|
||||||
curFileLinesCount int
|
curFileLinesCount int
|
||||||
curFileLFSPrefix bool
|
curFileLFSPrefix bool
|
||||||
|
|
||||||
|
input = bufio.NewReader(reader)
|
||||||
|
isEOF = false
|
||||||
)
|
)
|
||||||
|
|
||||||
input := bufio.NewReader(reader)
|
|
||||||
isEOF := false
|
|
||||||
for !isEOF {
|
for !isEOF {
|
||||||
var linebuf bytes.Buffer
|
var linebuf bytes.Buffer
|
||||||
for {
|
for {
|
||||||
b, err := input.ReadByte()
|
peek, err := input.Peek(maxLineCharacters)
|
||||||
|
if err != bufio.ErrBufferFull {
|
||||||
|
return nil, fmt.Errorf("PeekByte: %v", err)
|
||||||
|
}
|
||||||
|
newLine := bytes.Index(peek, []byte("\n"));
|
||||||
|
if newLine == -1 {
|
||||||
|
// Instead of reading things, and copying memory around,
|
||||||
|
// we simply discard them (which doesn't allocate memory)
|
||||||
|
curFile.IsIncomplete = true
|
||||||
|
// We already know that we can read `len(peek)` amount of bytes,
|
||||||
|
// hence no error-checking
|
||||||
|
input.Discard(len(peek))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if curFile.IsIncomplete {
|
||||||
|
// Since we get here without hiting the above case, we've found a newline
|
||||||
|
// and only discard that part.
|
||||||
|
input.Discard(newFile)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
buff := make([]byte, newLine)
|
||||||
|
n, err := input.Read(buff)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
isEOF = true
|
isEOF = true
|
||||||
break
|
break
|
||||||
} else {
|
|
||||||
return nil, fmt.Errorf("ReadByte: %v", err)
|
|
||||||
}
|
}
|
||||||
|
return nil, fmt.Errorf("Read: %v", err)
|
||||||
|
}
|
||||||
|
if n != newLine {
|
||||||
|
return nil, fmt.Errorf("Read: could not read enough bytes %d != %d", n, newLine)
|
||||||
|
}
|
||||||
|
n, err := linebuf.Write(buff)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("Write: %v", err)
|
||||||
|
}
|
||||||
|
if n != newLine {
|
||||||
|
return nil, fmt.Errorf("Write: could not write enough bytes %d != %d", n, newLine)
|
||||||
}
|
}
|
||||||
if b == '\n' {
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if linebuf.Len() < maxLineCharacters {
|
|
||||||
linebuf.WriteByte(b)
|
|
||||||
} else if linebuf.Len() == maxLineCharacters {
|
|
||||||
curFile.IsIncomplete = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
line := linebuf.String()
|
line := linebuf.String()
|
||||||
|
|
||||||
if strings.HasPrefix(line, "+++ ") || strings.HasPrefix(line, "--- ") || len(line) == 0 {
|
if strings.HasPrefix(line, "+++ ") || strings.HasPrefix(line, "--- ") || len(line) == 0 {
|
||||||
|
|
Loading…
Reference in New Issue