Skip to content

Commit

Permalink
Implement fragment header parsing
Browse files Browse the repository at this point in the history
Use a regexp for simplicity, unlike the direct parsing in Git.
  • Loading branch information
bluekeyes committed Mar 14, 2019
1 parent f41707b commit 4ddf1d9
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 6 deletions.
8 changes: 7 additions & 1 deletion gitdiff/gitdiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
40 changes: 39 additions & 1 deletion gitdiff/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bufio"
"fmt"
"io"
"regexp"
"strconv"
"strings"
)

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
84 changes: 80 additions & 4 deletions gitdiff/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
})
}
}

0 comments on commit 4ddf1d9

Please sign in to comment.