Implement fragment header parsing

Use a regexp for simplicity, unlike the direct parsing in Git.
This commit is contained in:
Billy Keyes
2019-03-13 23:04:01 -07:00
parent f41707bf2a
commit 4ddf1d962d
3 changed files with 126 additions and 6 deletions
+7 -1
View File
@@ -7,4 +7,10 @@ type File struct {
}
// Fragment describes changed lines starting at a specific line in a text file.
type Fragment struct{}
type Fragment struct {
OldPosition int64
OldLines int64
NewPosition int64
NewLines int64
}
+39 -1
View File
@@ -4,6 +4,8 @@ import (
"bufio"
"fmt"
"io"
"regexp"
"strconv"
"strings"
)
@@ -48,6 +50,11 @@ const (
newFilePrefix = "+++ "
)
var (
// TODO(bkeyes): are the boundary conditions necessary?
fragmentHeaderRegexp = regexp.MustCompile(`^@@ -(\d+),(\d+) \+(\d+)(?:,(\d+))? @@.*\n`)
)
// 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 *File, err error) {
@@ -128,7 +135,38 @@ func (p *parser) ParseTraditionalFileHeader(f *File, oldFile, newFile string) er
}
func (p *parser) ParseFragmentHeader(f *Fragment, header string) error {
panic("unimplemented")
match := fragmentHeaderRegexp.FindStringSubmatch(header)
if len(match) < 5 {
return p.Errorf("invalid fragment header")
}
parseInt := func(s string, v *int64) (err error) {
if *v, err = strconv.ParseInt(s, 10, 64); err != nil {
nerr := err.(*strconv.NumError)
return p.Errorf("invalid fragment header value: %s: %v", s, nerr.Err)
}
return
}
if err := parseInt(match[1], &f.OldPosition); err != nil {
return err
}
if err := parseInt(match[2], &f.OldLines); err != nil {
return err
}
if err := parseInt(match[3], &f.NewPosition); err != nil {
return err
}
f.NewLines = 1
if match[4] != "" {
if err := parseInt(match[4], &f.NewLines); err != nil {
return err
}
}
return nil
}
// Line reads and returns the next line. The first call to Line after a call to
+80 -4
View File
@@ -2,18 +2,19 @@ package gitdiff
import (
"bufio"
"bytes"
"reflect"
"strings"
"testing"
)
func TestLineOperations(t *testing.T) {
content := []byte(`the first line
content := `the first line
the second line
the third line
`)
`
newParser := func() *parser {
return &parser{r: bufio.NewReader(bytes.NewBuffer(content))}
return &parser{r: bufio.NewReader(strings.NewReader(content))}
}
t.Run("readLine", func(t *testing.T) {
@@ -56,3 +57,78 @@ the third line
}
})
}
func TestParseFragmentHeader(t *testing.T) {
tests := []struct {
Name string
Input string
Expected *Fragment
Invalid bool
}{
{
Name: "shortest",
Input: "@@ -0,0 +1 @@\n",
Expected: &Fragment{
OldPosition: 0,
OldLines: 0,
NewPosition: 1,
NewLines: 1,
},
},
{
Name: "standard",
Input: "@@ -21,5 +28,9 @@\n",
Expected: &Fragment{
OldPosition: 21,
OldLines: 5,
NewPosition: 28,
NewLines: 9,
},
},
{
Name: "trailingWhitespace",
Input: "@@ -21,5 +28,9 @@ \r\n",
Expected: &Fragment{
OldPosition: 21,
OldLines: 5,
NewPosition: 28,
NewLines: 9,
},
},
{
Name: "incomplete",
Input: "@@ -12,3 +2\n",
Invalid: true,
},
{
Name: "badNumbers",
Input: "@@ -1a,2b +3c,4d @@\n",
Invalid: true,
},
}
for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
p := &parser{r: bufio.NewReader(strings.NewReader(test.Input))}
line, _ := p.Line()
var frag Fragment
err := p.ParseFragmentHeader(&frag, line)
if test.Invalid {
if err == nil {
t.Fatalf("expected error parsing header, but got nil")
}
return
}
if err != nil {
t.Fatalf("error parsing header: %v", err)
}
if !reflect.DeepEqual(*test.Expected, frag) {
t.Fatalf("incorrect fragment\nexpected: %+v\nactual: %+v", *test.Expected, frag)
}
})
}
}