Skip to content

Commit a0b33e3

Browse files
committed
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.
1 parent 3398cfa commit a0b33e3

File tree

4 files changed

+86
-83
lines changed

4 files changed

+86
-83
lines changed

gitdiff/gitdiff.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ type File struct {
2323
NewOIDPrefix string
2424
Score int
2525

26-
Fragments []*Fragment
26+
// TextFragments contains the fragments describing changes to a text file. It
27+
// may be empty if the file is empty or if only the mode changes.
28+
TextFragments []*TextFragment
2729
}
2830

29-
// Fragment describes changed lines starting at a specific line in a text file.
30-
type Fragment struct {
31+
// TextFragment describes changed lines starting at a specific line in a text file.
32+
type TextFragment struct {
3133
Comment string
3234

3335
OldPosition int64
@@ -42,20 +44,25 @@ type Fragment struct {
4244
LeadingContext int64
4345
TrailingContext int64
4446

45-
Lines []FragmentLine
47+
Lines []Line
4648
}
4749

48-
// FragmentLine is a line in a fragment.
49-
type FragmentLine struct {
50+
// Header returns the cannonical header of this fragment.
51+
func (f *TextFragment) Header() string {
52+
return fmt.Sprintf("@@ -%d,%d +%d,%d @@ %s", f.OldPosition, f.OldLines, f.NewPosition, f.NewLines, f.Comment)
53+
}
54+
55+
// Line is a line in a text fragment.
56+
type Line struct {
5057
Op LineOp
5158
Line string
5259
}
5360

54-
func (fl FragmentLine) String() string {
61+
func (fl Line) String() string {
5562
return fl.Op.String() + fl.Line
5663
}
5764

58-
// LineOp describes the type of a fragment line: context, added, or removed.
65+
// LineOp describes the type of a text fragment line: context, added, or removed.
5966
type LineOp int
6067

6168
const (
@@ -79,7 +86,3 @@ func (op LineOp) String() string {
7986
return "?"
8087
}
8188

82-
// Header returns the cannonical header of this fragment.
83-
func (f *Fragment) Header() string {
84-
return fmt.Sprintf("@@ -%d,%d +%d,%d @@ %s", f.OldPosition, f.OldLines, f.NewPosition, f.NewLines, f.Comment)
85-
}

gitdiff/parser.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func (p *parser) ParseTextFragments(f *File) (n int, err error) {
144144
return n, err
145145
}
146146

147-
f.Fragments = append(f.Fragments, frag)
147+
f.TextFragments = append(f.TextFragments, frag)
148148
n++
149149
}
150150
}
@@ -200,7 +200,7 @@ func (p *parser) Errorf(delta int64, msg string, args ...interface{}) error {
200200
return fmt.Errorf("gitdiff: line %d: %s", p.lineno+delta, fmt.Sprintf(msg, args...))
201201
}
202202

203-
func (p *parser) ParseTextFragmentHeader() (*Fragment, error) {
203+
func (p *parser) ParseTextFragmentHeader() (*TextFragment, error) {
204204
const (
205205
startMark = "@@ -"
206206
endMark = " @@"
@@ -215,7 +215,7 @@ func (p *parser) ParseTextFragmentHeader() (*Fragment, error) {
215215
return nil, p.Errorf(0, "invalid fragment header")
216216
}
217217

218-
f := &Fragment{}
218+
f := &TextFragment{}
219219
f.Comment = strings.TrimSpace(parts[1])
220220

221221
header := parts[0][len(startMark) : len(parts[0])-len(endMark)]
@@ -238,7 +238,7 @@ func (p *parser) ParseTextFragmentHeader() (*Fragment, error) {
238238
return f, nil
239239
}
240240

241-
func (p *parser) ParseTextChunk(frag *Fragment) error {
241+
func (p *parser) ParseTextChunk(frag *TextFragment) error {
242242
if p.Line(0) == "" {
243243
return p.Errorf(0, "no content following fragment header")
244244
}
@@ -266,17 +266,17 @@ func (p *parser) ParseTextChunk(frag *Fragment) error {
266266
} else {
267267
frag.TrailingContext++
268268
}
269-
frag.Lines = append(frag.Lines, FragmentLine{OpContext, data})
269+
frag.Lines = append(frag.Lines, Line{OpContext, data})
270270
case '-':
271271
oldLines--
272272
frag.LinesDeleted++
273273
frag.TrailingContext = 0
274-
frag.Lines = append(frag.Lines, FragmentLine{OpDelete, data})
274+
frag.Lines = append(frag.Lines, Line{OpDelete, data})
275275
case '+':
276276
newLines--
277277
frag.LinesAdded++
278278
frag.TrailingContext = 0
279-
frag.Lines = append(frag.Lines, FragmentLine{OpAdd, data})
279+
frag.Lines = append(frag.Lines, Line{OpAdd, data})
280280
default:
281281
// this may appear in middle of fragment if it's for a deleted line
282282
if isNoNewlineLine(line) {

gitdiff/parser_test.go

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ context line
130130
@@ -1 +1 @@
131131
`,
132132
Parse: func(p *parser) error {
133-
return p.ParseTextChunk(&Fragment{OldLines: 3, NewLines: 3})
133+
return p.ParseTextChunk(&TextFragment{OldLines: 3, NewLines: 3})
134134
},
135135
EndLine: "@@ -1 +1 @@\n",
136136
},
@@ -280,14 +280,14 @@ a wild fragment appears?
280280
}
281281

282282
func TestParse(t *testing.T) {
283-
expectedFragments := []*Fragment{
283+
expectedFragments := []*TextFragment{
284284
{
285285
OldPosition: 3,
286286
OldLines: 6,
287287
NewPosition: 3,
288288
NewLines: 8,
289289
Comment: "fragment 1",
290-
Lines: []FragmentLine{
290+
Lines: []Line{
291291
{OpContext, "context line\n"},
292292
{OpDelete, "old line 1\n"},
293293
{OpDelete, "old line 2\n"},
@@ -310,7 +310,7 @@ func TestParse(t *testing.T) {
310310
NewPosition: 33,
311311
NewLines: 2,
312312
Comment: "fragment 2",
313-
Lines: []FragmentLine{
313+
Lines: []Line{
314314
{OpContext, "context line\n"},
315315
{OpDelete, "old line 4\n"},
316316
{OpAdd, "new line 6\n"},
@@ -341,12 +341,12 @@ Date: Tue Apr 2 22:55:40 2019 -0700
341341
InputFile: "testdata/one_file.patch",
342342
Output: []*File{
343343
{
344-
OldName: "dir/file1.txt",
345-
NewName: "dir/file1.txt",
346-
OldMode: os.FileMode(0100644),
347-
OldOIDPrefix: "ebe9fa54",
348-
NewOIDPrefix: "fe103e1d",
349-
Fragments: expectedFragments,
344+
OldName: "dir/file1.txt",
345+
NewName: "dir/file1.txt",
346+
OldMode: os.FileMode(0100644),
347+
OldOIDPrefix: "ebe9fa54",
348+
NewOIDPrefix: "fe103e1d",
349+
TextFragments: expectedFragments,
350350
},
351351
},
352352
Preamble: expectedPreamble,
@@ -355,20 +355,20 @@ Date: Tue Apr 2 22:55:40 2019 -0700
355355
InputFile: "testdata/two_files.patch",
356356
Output: []*File{
357357
{
358-
OldName: "dir/file1.txt",
359-
NewName: "dir/file1.txt",
360-
OldMode: os.FileMode(0100644),
361-
OldOIDPrefix: "ebe9fa54",
362-
NewOIDPrefix: "fe103e1d",
363-
Fragments: expectedFragments,
358+
OldName: "dir/file1.txt",
359+
NewName: "dir/file1.txt",
360+
OldMode: os.FileMode(0100644),
361+
OldOIDPrefix: "ebe9fa54",
362+
NewOIDPrefix: "fe103e1d",
363+
TextFragments: expectedFragments,
364364
},
365365
{
366-
OldName: "dir/file2.txt",
367-
NewName: "dir/file2.txt",
368-
OldMode: os.FileMode(0100644),
369-
OldOIDPrefix: "417ebc70",
370-
NewOIDPrefix: "67514b7f",
371-
Fragments: expectedFragments,
366+
OldName: "dir/file2.txt",
367+
NewName: "dir/file2.txt",
368+
OldMode: os.FileMode(0100644),
369+
OldOIDPrefix: "417ebc70",
370+
NewOIDPrefix: "67514b7f",
371+
TextFragments: expectedFragments,
372372
},
373373
},
374374
Preamble: expectedPreamble,

0 commit comments

Comments
 (0)