From 4ddf1d962d8252f2762d59211dbcf4bccb9be67e Mon Sep 17 00:00:00 2001 From: Billy Keyes Date: Wed, 13 Mar 2019 23:04:01 -0700 Subject: [PATCH] Implement fragment header parsing Use a regexp for simplicity, unlike the direct parsing in Git. --- gitdiff/gitdiff.go | 8 +++- gitdiff/parser.go | 40 +++++++++++++++++++- gitdiff/parser_test.go | 84 ++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 126 insertions(+), 6 deletions(-) diff --git a/gitdiff/gitdiff.go b/gitdiff/gitdiff.go index b1c5d4c..e0d0a3c 100644 --- a/gitdiff/gitdiff.go +++ b/gitdiff/gitdiff.go @@ -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 +} diff --git a/gitdiff/parser.go b/gitdiff/parser.go index d88499f..074c567 100644 --- a/gitdiff/parser.go +++ b/gitdiff/parser.go @@ -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 diff --git a/gitdiff/parser_test.go b/gitdiff/parser_test.go index 0439460..60a98bc 100644 --- a/gitdiff/parser_test.go +++ b/gitdiff/parser_test.go @@ -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) + } + }) + } +}