Rename types in preparation for binary parsing

Fragment is now TextFragment to distinguish from a future
BinaryFragment. Also rename FragmentLine to Line, since the
text-orientation is implied by the name.
This commit is contained in:
Billy Keyes
2019-04-07 20:33:21 -07:00
parent 3398cfa890
commit a0b33e37b4
4 changed files with 86 additions and 83 deletions
+15 -12
View File
@@ -23,11 +23,13 @@ type File struct {
NewOIDPrefix string
Score int
Fragments []*Fragment
// TextFragments contains the fragments describing changes to a text file. It
// may be empty if the file is empty or if only the mode changes.
TextFragments []*TextFragment
}
// Fragment describes changed lines starting at a specific line in a text file.
type Fragment struct {
// TextFragment describes changed lines starting at a specific line in a text file.
type TextFragment struct {
Comment string
OldPosition int64
@@ -42,20 +44,25 @@ type Fragment struct {
LeadingContext int64
TrailingContext int64
Lines []FragmentLine
Lines []Line
}
// FragmentLine is a line in a fragment.
type FragmentLine struct {
// Header returns the cannonical header of this fragment.
func (f *TextFragment) Header() string {
return fmt.Sprintf("@@ -%d,%d +%d,%d @@ %s", f.OldPosition, f.OldLines, f.NewPosition, f.NewLines, f.Comment)
}
// Line is a line in a text fragment.
type Line struct {
Op LineOp
Line string
}
func (fl FragmentLine) String() string {
func (fl Line) String() string {
return fl.Op.String() + fl.Line
}
// LineOp describes the type of a fragment line: context, added, or removed.
// LineOp describes the type of a text fragment line: context, added, or removed.
type LineOp int
const (
@@ -79,7 +86,3 @@ func (op LineOp) String() string {
return "?"
}
// Header returns the cannonical header of this fragment.
func (f *Fragment) Header() string {
return fmt.Sprintf("@@ -%d,%d +%d,%d @@ %s", f.OldPosition, f.OldLines, f.NewPosition, f.NewLines, f.Comment)
}
+7 -7
View File
@@ -144,7 +144,7 @@ func (p *parser) ParseTextFragments(f *File) (n int, err error) {
return n, err
}
f.Fragments = append(f.Fragments, frag)
f.TextFragments = append(f.TextFragments, frag)
n++
}
}
@@ -200,7 +200,7 @@ 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) ParseTextFragmentHeader() (*Fragment, error) {
func (p *parser) ParseTextFragmentHeader() (*TextFragment, error) {
const (
startMark = "@@ -"
endMark = " @@"
@@ -215,7 +215,7 @@ func (p *parser) ParseTextFragmentHeader() (*Fragment, error) {
return nil, p.Errorf(0, "invalid fragment header")
}
f := &Fragment{}
f := &TextFragment{}
f.Comment = strings.TrimSpace(parts[1])
header := parts[0][len(startMark) : len(parts[0])-len(endMark)]
@@ -238,7 +238,7 @@ func (p *parser) ParseTextFragmentHeader() (*Fragment, error) {
return f, nil
}
func (p *parser) ParseTextChunk(frag *Fragment) error {
func (p *parser) ParseTextChunk(frag *TextFragment) error {
if p.Line(0) == "" {
return p.Errorf(0, "no content following fragment header")
}
@@ -266,17 +266,17 @@ func (p *parser) ParseTextChunk(frag *Fragment) error {
} else {
frag.TrailingContext++
}
frag.Lines = append(frag.Lines, FragmentLine{OpContext, data})
frag.Lines = append(frag.Lines, Line{OpContext, data})
case '-':
oldLines--
frag.LinesDeleted++
frag.TrailingContext = 0
frag.Lines = append(frag.Lines, FragmentLine{OpDelete, data})
frag.Lines = append(frag.Lines, Line{OpDelete, data})
case '+':
newLines--
frag.LinesAdded++
frag.TrailingContext = 0
frag.Lines = append(frag.Lines, FragmentLine{OpAdd, data})
frag.Lines = append(frag.Lines, Line{OpAdd, data})
default:
// this may appear in middle of fragment if it's for a deleted line
if isNoNewlineLine(line) {
+22 -22
View File
@@ -130,7 +130,7 @@ context line
@@ -1 +1 @@
`,
Parse: func(p *parser) error {
return p.ParseTextChunk(&Fragment{OldLines: 3, NewLines: 3})
return p.ParseTextChunk(&TextFragment{OldLines: 3, NewLines: 3})
},
EndLine: "@@ -1 +1 @@\n",
},
@@ -280,14 +280,14 @@ a wild fragment appears?
}
func TestParse(t *testing.T) {
expectedFragments := []*Fragment{
expectedFragments := []*TextFragment{
{
OldPosition: 3,
OldLines: 6,
NewPosition: 3,
NewLines: 8,
Comment: "fragment 1",
Lines: []FragmentLine{
Lines: []Line{
{OpContext, "context line\n"},
{OpDelete, "old line 1\n"},
{OpDelete, "old line 2\n"},
@@ -310,7 +310,7 @@ func TestParse(t *testing.T) {
NewPosition: 33,
NewLines: 2,
Comment: "fragment 2",
Lines: []FragmentLine{
Lines: []Line{
{OpContext, "context line\n"},
{OpDelete, "old line 4\n"},
{OpAdd, "new line 6\n"},
@@ -341,12 +341,12 @@ Date: Tue Apr 2 22:55:40 2019 -0700
InputFile: "testdata/one_file.patch",
Output: []*File{
{
OldName: "dir/file1.txt",
NewName: "dir/file1.txt",
OldMode: os.FileMode(0100644),
OldOIDPrefix: "ebe9fa54",
NewOIDPrefix: "fe103e1d",
Fragments: expectedFragments,
OldName: "dir/file1.txt",
NewName: "dir/file1.txt",
OldMode: os.FileMode(0100644),
OldOIDPrefix: "ebe9fa54",
NewOIDPrefix: "fe103e1d",
TextFragments: expectedFragments,
},
},
Preamble: expectedPreamble,
@@ -355,20 +355,20 @@ Date: Tue Apr 2 22:55:40 2019 -0700
InputFile: "testdata/two_files.patch",
Output: []*File{
{
OldName: "dir/file1.txt",
NewName: "dir/file1.txt",
OldMode: os.FileMode(0100644),
OldOIDPrefix: "ebe9fa54",
NewOIDPrefix: "fe103e1d",
Fragments: expectedFragments,
OldName: "dir/file1.txt",
NewName: "dir/file1.txt",
OldMode: os.FileMode(0100644),
OldOIDPrefix: "ebe9fa54",
NewOIDPrefix: "fe103e1d",
TextFragments: expectedFragments,
},
{
OldName: "dir/file2.txt",
NewName: "dir/file2.txt",
OldMode: os.FileMode(0100644),
OldOIDPrefix: "417ebc70",
NewOIDPrefix: "67514b7f",
Fragments: expectedFragments,
OldName: "dir/file2.txt",
NewName: "dir/file2.txt",
OldMode: os.FileMode(0100644),
OldOIDPrefix: "417ebc70",
NewOIDPrefix: "67514b7f",
TextFragments: expectedFragments,
},
},
Preamble: expectedPreamble,
+42 -42
View File
@@ -9,12 +9,12 @@ import (
func TestParseTextFragmentHeader(t *testing.T) {
tests := map[string]struct {
Input string
Output *Fragment
Output *TextFragment
Err bool
}{
"shortest": {
Input: "@@ -1 +1 @@\n",
Output: &Fragment{
Output: &TextFragment{
OldPosition: 1,
OldLines: 1,
NewPosition: 1,
@@ -23,7 +23,7 @@ func TestParseTextFragmentHeader(t *testing.T) {
},
"standard": {
Input: "@@ -21,5 +28,9 @@\n",
Output: &Fragment{
Output: &TextFragment{
OldPosition: 21,
OldLines: 5,
NewPosition: 28,
@@ -32,7 +32,7 @@ func TestParseTextFragmentHeader(t *testing.T) {
},
"trailingComment": {
Input: "@@ -21,5 +28,9 @@ func test(n int) {\n",
Output: &Fragment{
Output: &TextFragment{
Comment: "func test(n int) {",
OldPosition: 21,
OldLines: 5,
@@ -75,9 +75,9 @@ func TestParseTextFragmentHeader(t *testing.T) {
func TestParseTextChunk(t *testing.T) {
tests := map[string]struct {
Input string
Fragment Fragment
Fragment TextFragment
Output *Fragment
Output *TextFragment
Err bool
}{
"addWithContext": {
@@ -86,14 +86,14 @@ func TestParseTextChunk(t *testing.T) {
+new line 2
context line
`,
Fragment: Fragment{
Fragment: TextFragment{
OldLines: 2,
NewLines: 4,
},
Output: &Fragment{
Output: &TextFragment{
OldLines: 2,
NewLines: 4,
Lines: []FragmentLine{
Lines: []Line{
{OpContext, "context line\n"},
{OpAdd, "new line 1\n"},
{OpAdd, "new line 2\n"},
@@ -110,14 +110,14 @@ func TestParseTextChunk(t *testing.T) {
-old line 2
context line
`,
Fragment: Fragment{
Fragment: TextFragment{
OldLines: 4,
NewLines: 2,
},
Output: &Fragment{
Output: &TextFragment{
OldLines: 4,
NewLines: 2,
Lines: []FragmentLine{
Lines: []Line{
{OpContext, "context line\n"},
{OpDelete, "old line 1\n"},
{OpDelete, "old line 2\n"},
@@ -134,14 +134,14 @@ func TestParseTextChunk(t *testing.T) {
+new line 1
context line
`,
Fragment: Fragment{
Fragment: TextFragment{
OldLines: 3,
NewLines: 3,
},
Output: &Fragment{
Output: &TextFragment{
OldLines: 3,
NewLines: 3,
Lines: []FragmentLine{
Lines: []Line{
{OpContext, "context line\n"},
{OpDelete, "old line 1\n"},
{OpAdd, "new line 1\n"},
@@ -160,14 +160,14 @@ func TestParseTextChunk(t *testing.T) {
+new line 1
context line
`,
Fragment: Fragment{
Fragment: TextFragment{
OldLines: 4,
NewLines: 4,
},
Output: &Fragment{
Output: &TextFragment{
OldLines: 4,
NewLines: 4,
Lines: []FragmentLine{
Lines: []Line{
{OpContext, "context line\n"},
{OpDelete, "old line 1\n"},
{OpContext, "context line\n"},
@@ -186,14 +186,14 @@ func TestParseTextChunk(t *testing.T) {
+new line 1
\ No newline at end of file
`,
Fragment: Fragment{
Fragment: TextFragment{
OldLines: 2,
NewLines: 2,
},
Output: &Fragment{
Output: &TextFragment{
OldLines: 2,
NewLines: 2,
Lines: []FragmentLine{
Lines: []Line{
{OpContext, "context line\n"},
{OpDelete, "old line 1\n"},
{OpAdd, "new line 1"},
@@ -209,14 +209,14 @@ func TestParseTextChunk(t *testing.T) {
\ No newline at end of file
+new line 1
`,
Fragment: Fragment{
Fragment: TextFragment{
OldLines: 2,
NewLines: 2,
},
Output: &Fragment{
Output: &TextFragment{
OldLines: 2,
NewLines: 2,
Lines: []FragmentLine{
Lines: []Line{
{OpContext, "context line\n"},
{OpDelete, "old line 1"},
{OpAdd, "new line 1\n"},
@@ -231,14 +231,14 @@ func TestParseTextChunk(t *testing.T) {
+new line 2
+new line 3
`,
Fragment: Fragment{
Fragment: TextFragment{
OldLines: 0,
NewLines: 3,
},
Output: &Fragment{
Output: &TextFragment{
OldLines: 0,
NewLines: 3,
Lines: []FragmentLine{
Lines: []Line{
{OpAdd, "new line 1\n"},
{OpAdd, "new line 2\n"},
{OpAdd, "new line 3\n"},
@@ -251,14 +251,14 @@ func TestParseTextChunk(t *testing.T) {
-old line 2
-old line 3
`,
Fragment: Fragment{
Fragment: TextFragment{
OldLines: 3,
NewLines: 0,
},
Output: &Fragment{
Output: &TextFragment{
OldLines: 3,
NewLines: 0,
Lines: []FragmentLine{
Lines: []Line{
{OpDelete, "old line 1\n"},
{OpDelete, "old line 2\n"},
{OpDelete, "old line 3\n"},
@@ -272,14 +272,14 @@ func TestParseTextChunk(t *testing.T) {
+new line
context line
`,
Fragment: Fragment{
Fragment: TextFragment{
OldLines: 3,
NewLines: 4,
},
Output: &Fragment{
Output: &TextFragment{
OldLines: 3,
NewLines: 4,
Lines: []FragmentLine{
Lines: []Line{
{OpContext, "context line\n"},
{OpContext, "\n"},
{OpAdd, "new line\n"},
@@ -299,7 +299,7 @@ func TestParseTextChunk(t *testing.T) {
?wat line
context line
`,
Fragment: Fragment{
Fragment: TextFragment{
OldLines: 3,
NewLines: 3,
},
@@ -311,7 +311,7 @@ func TestParseTextChunk(t *testing.T) {
+new line 1
context line
`,
Fragment: Fragment{
Fragment: TextFragment{
OldLines: 2,
NewLines: 5,
},
@@ -347,7 +347,7 @@ func TestParseTextFragments(t *testing.T) {
Input string
File File
Fragments []*Fragment
Fragments []*TextFragment
Err bool
}{
"multipleChanges": {
@@ -367,13 +367,13 @@ func TestParseTextFragments(t *testing.T) {
+new line 3
context line
`,
Fragments: []*Fragment{
Fragments: []*TextFragment{
{
OldPosition: 1,
OldLines: 3,
NewPosition: 1,
NewLines: 2,
Lines: []FragmentLine{
Lines: []Line{
{OpContext, "context line\n"},
{OpDelete, "old line 1\n"},
{OpContext, "context line\n"},
@@ -387,7 +387,7 @@ func TestParseTextFragments(t *testing.T) {
OldLines: 3,
NewPosition: 7,
NewLines: 3,
Lines: []FragmentLine{
Lines: []Line{
{OpContext, "context line\n"},
{OpDelete, "old line 2\n"},
{OpAdd, "new line 1\n"},
@@ -403,7 +403,7 @@ func TestParseTextFragments(t *testing.T) {
OldLines: 3,
NewPosition: 14,
NewLines: 4,
Lines: []FragmentLine{
Lines: []Line{
{OpContext, "context line\n"},
{OpDelete, "old line 3\n"},
{OpAdd, "new line 2\n"},
@@ -461,8 +461,8 @@ func TestParseTextFragments(t *testing.T) {
}
for i, frag := range test.Fragments {
if !reflect.DeepEqual(frag, file.Fragments[i]) {
t.Errorf("incorrect fragment at position %d\nexpected: %+v\nactual: %+v", i, frag, file.Fragments[i])
if !reflect.DeepEqual(frag, file.TextFragments[i]) {
t.Errorf("incorrect fragment at position %d\nexpected: %+v\nactual: %+v", i, frag, file.TextFragments[i])
}
}
})