Files
bluekeyes-go-gitdiff/gitdiff/parser.go
T
Billy Keyes 4b25230457 Check for expected input in parsing functions
Instead of checking that a line has a certain prefix before calling a
header parsing function, the functions now check this and return nil
objects when called on the wrong line type. If the line passes this
basic check but is still invalid, and error is returned as before.
2019-03-25 22:27:31 -07:00

202 lines
4.8 KiB
Go

package gitdiff
import (
"bufio"
"fmt"
"io"
"strconv"
"strings"
)
// Parse parses a patch with changes for one or more files. Any content
// preceding the first file header is ignored. If an error occurs while
// parsing, files will contain all files parsed before the error.
func Parse(r io.Reader) (files []*File, err error) {
p := &parser{r: bufio.NewReader(r)}
var file *File
for {
file, err = p.ParseNextFileHeader()
if err != nil {
return
}
if file == nil {
break
}
err = p.ParseFileChanges(file)
if err != nil {
return
}
files = append(files, file)
}
return files, nil
}
// TODO(bkeyes): consider exporting the parser type with configuration
// this would enable OID validation, p-value guessing, and prefix stripping
// by allowing users to set or override defaults
type parser struct {
r *bufio.Reader
eof bool
lineno int64
lines [3]string
}
// ParseNextFileHeader finds and parses the next file header in the stream. It
// returns nil if no headers are found before the end of the stream.
func (p *parser) ParseNextFileHeader() (*File, error) {
for {
if err := p.Next(); err != nil {
if err == io.EOF {
break
}
return nil, err
}
// check for disconnected fragment headers (corrupt patch)
frag, err := p.ParseFragmentHeader()
if err != nil {
// not a valid header, nothing to worry about
continue
}
if frag != nil {
return nil, p.Errorf(0, "patch fragment without header: %s", p.Line(0))
}
// check for a git-generated patch
file, err := p.ParseGitFileHeader()
if err != nil {
return nil, err
}
if file != nil {
return file, nil
}
// check for a "traditional" patch
file, err = p.ParseTraditionalFileHeader()
if err != nil {
return nil, err
}
if file != nil {
return file, nil
}
}
return nil, nil
}
// ParseFileChanges parses file changes until the next file header or the end
// of the stream and attaches them to the given file.
func (p *parser) ParseFileChanges(f *File) error {
panic("TODO(bkeyes): unimplemented")
}
// Next advances the parser by one line. It returns any error encountered while
// reading the line, including io.EOF when the end of stream is reached.
func (p *parser) Next() error {
if p.eof {
p.lines[0] = ""
return io.EOF
}
if p.lineno == 0 {
// on first call to next, need to shift in all lines
for i := 0; i < len(p.lines)-1; i++ {
if err := p.shiftLines(); err != nil && err != io.EOF {
return err
}
}
}
err := p.shiftLines()
if err == io.EOF {
p.eof = p.lines[1] == ""
} else if err != nil {
return err
}
p.lineno++
return nil
}
func (p *parser) shiftLines() (err error) {
for i := 0; i < len(p.lines)-1; i++ {
p.lines[i] = p.lines[i+1]
}
p.lines[len(p.lines)-1], err = p.r.ReadString('\n')
return
}
// Line returns a line from the parser without advancing it. A delta of 0
// returns the current line, while higher deltas return read-ahead lines. It
// returns an empty string if the delta is higher than the available lines,
// either because of the buffer size or because the parser reached the end of
// the input. Valid lines always contain at least a newline character.
func (p *parser) Line(delta uint) string {
return p.lines[delta]
}
// Errorf generates an error and appends the current line information.
func (p *parser) Errorf(delta int64, msg string, args ...interface{}) error {
return fmt.Errorf("gitdiff: line %d: %s", p.lineno+delta, fmt.Sprintf(msg, args...))
}
func (p *parser) ParseFragmentHeader() (*Fragment, error) {
const (
startMark = "@@ -"
endMark = " @@"
)
if !strings.HasPrefix(p.Line(0), startMark) {
return nil, nil
}
parts := strings.SplitAfterN(p.Line(0), endMark, 2)
if len(parts) < 2 {
return nil, fmt.Errorf("invalid fragment header")
}
f := &Fragment{}
f.Comment = strings.TrimSpace(parts[1])
header := parts[0][len(startMark) : len(parts[0])-len(endMark)]
ranges := strings.Split(header, " +")
if len(ranges) != 2 {
return nil, fmt.Errorf("invalid fragment header")
}
var err error
if f.OldPosition, f.OldLines, err = parseRange(ranges[0]); err != nil {
return nil, fmt.Errorf("invalid fragment header: %v", err)
}
if f.NewPosition, f.NewLines, err = parseRange(ranges[1]); err != nil {
return nil, fmt.Errorf("invalid fragment header: %v", err)
}
return f, nil
}
func parseRange(s string) (start int64, end int64, err error) {
parts := strings.SplitN(s, ",", 2)
if start, err = strconv.ParseInt(parts[0], 10, 64); err != nil {
nerr := err.(*strconv.NumError)
return 0, 0, fmt.Errorf("bad start of range: %s: %v", parts[0], nerr.Err)
}
if len(parts) > 1 {
if end, err = strconv.ParseInt(parts[1], 10, 64); err != nil {
nerr := err.(*strconv.NumError)
return 0, 0, fmt.Errorf("bad end of range: %s: %v", parts[1], nerr.Err)
}
} else {
end = 1
}
return
}