this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

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.

+86 -83
+15 -12
gitdiff/gitdiff.go
··· 23 23 NewOIDPrefix string 24 24 Score int 25 25 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 27 29 } 28 30 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 { 31 33 Comment string 32 34 33 35 OldPosition int64 ··· 42 44 LeadingContext int64 43 45 TrailingContext int64 44 46 45 - Lines []FragmentLine 47 + Lines []Line 48 + } 49 + 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) 46 53 } 47 54 48 - // FragmentLine is a line in a fragment. 49 - type FragmentLine struct { 55 + // Line is a line in a text fragment. 56 + type Line struct { 50 57 Op LineOp 51 58 Line string 52 59 } 53 60 54 - func (fl FragmentLine) String() string { 61 + func (fl Line) String() string { 55 62 return fl.Op.String() + fl.Line 56 63 } 57 64 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. 59 66 type LineOp int 60 67 61 68 const ( ··· 79 86 return "?" 80 87 } 81 88 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 - }
+7 -7
gitdiff/parser.go
··· 144 144 return n, err 145 145 } 146 146 147 - f.Fragments = append(f.Fragments, frag) 147 + f.TextFragments = append(f.TextFragments, frag) 148 148 n++ 149 149 } 150 150 } ··· 200 200 return fmt.Errorf("gitdiff: line %d: %s", p.lineno+delta, fmt.Sprintf(msg, args...)) 201 201 } 202 202 203 - func (p *parser) ParseTextFragmentHeader() (*Fragment, error) { 203 + func (p *parser) ParseTextFragmentHeader() (*TextFragment, error) { 204 204 const ( 205 205 startMark = "@@ -" 206 206 endMark = " @@" ··· 215 215 return nil, p.Errorf(0, "invalid fragment header") 216 216 } 217 217 218 - f := &Fragment{} 218 + f := &TextFragment{} 219 219 f.Comment = strings.TrimSpace(parts[1]) 220 220 221 221 header := parts[0][len(startMark) : len(parts[0])-len(endMark)] ··· 238 238 return f, nil 239 239 } 240 240 241 - func (p *parser) ParseTextChunk(frag *Fragment) error { 241 + func (p *parser) ParseTextChunk(frag *TextFragment) error { 242 242 if p.Line(0) == "" { 243 243 return p.Errorf(0, "no content following fragment header") 244 244 } ··· 266 266 } else { 267 267 frag.TrailingContext++ 268 268 } 269 - frag.Lines = append(frag.Lines, FragmentLine{OpContext, data}) 269 + frag.Lines = append(frag.Lines, Line{OpContext, data}) 270 270 case '-': 271 271 oldLines-- 272 272 frag.LinesDeleted++ 273 273 frag.TrailingContext = 0 274 - frag.Lines = append(frag.Lines, FragmentLine{OpDelete, data}) 274 + frag.Lines = append(frag.Lines, Line{OpDelete, data}) 275 275 case '+': 276 276 newLines-- 277 277 frag.LinesAdded++ 278 278 frag.TrailingContext = 0 279 - frag.Lines = append(frag.Lines, FragmentLine{OpAdd, data}) 279 + frag.Lines = append(frag.Lines, Line{OpAdd, data}) 280 280 default: 281 281 // this may appear in middle of fragment if it's for a deleted line 282 282 if isNoNewlineLine(line) {
+22 -22
gitdiff/parser_test.go
··· 130 130 @@ -1 +1 @@ 131 131 `, 132 132 Parse: func(p *parser) error { 133 - return p.ParseTextChunk(&Fragment{OldLines: 3, NewLines: 3}) 133 + return p.ParseTextChunk(&TextFragment{OldLines: 3, NewLines: 3}) 134 134 }, 135 135 EndLine: "@@ -1 +1 @@\n", 136 136 }, ··· 280 280 } 281 281 282 282 func TestParse(t *testing.T) { 283 - expectedFragments := []*Fragment{ 283 + expectedFragments := []*TextFragment{ 284 284 { 285 285 OldPosition: 3, 286 286 OldLines: 6, 287 287 NewPosition: 3, 288 288 NewLines: 8, 289 289 Comment: "fragment 1", 290 - Lines: []FragmentLine{ 290 + Lines: []Line{ 291 291 {OpContext, "context line\n"}, 292 292 {OpDelete, "old line 1\n"}, 293 293 {OpDelete, "old line 2\n"}, ··· 310 310 NewPosition: 33, 311 311 NewLines: 2, 312 312 Comment: "fragment 2", 313 - Lines: []FragmentLine{ 313 + Lines: []Line{ 314 314 {OpContext, "context line\n"}, 315 315 {OpDelete, "old line 4\n"}, 316 316 {OpAdd, "new line 6\n"}, ··· 341 341 InputFile: "testdata/one_file.patch", 342 342 Output: []*File{ 343 343 { 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, 350 350 }, 351 351 }, 352 352 Preamble: expectedPreamble, ··· 355 355 InputFile: "testdata/two_files.patch", 356 356 Output: []*File{ 357 357 { 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, 364 364 }, 365 365 { 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, 372 372 }, 373 373 }, 374 374 Preamble: expectedPreamble,
+42 -42
gitdiff/parser_text_test.go
··· 9 9 func TestParseTextFragmentHeader(t *testing.T) { 10 10 tests := map[string]struct { 11 11 Input string 12 - Output *Fragment 12 + Output *TextFragment 13 13 Err bool 14 14 }{ 15 15 "shortest": { 16 16 Input: "@@ -1 +1 @@\n", 17 - Output: &Fragment{ 17 + Output: &TextFragment{ 18 18 OldPosition: 1, 19 19 OldLines: 1, 20 20 NewPosition: 1, ··· 23 23 }, 24 24 "standard": { 25 25 Input: "@@ -21,5 +28,9 @@\n", 26 - Output: &Fragment{ 26 + Output: &TextFragment{ 27 27 OldPosition: 21, 28 28 OldLines: 5, 29 29 NewPosition: 28, ··· 32 32 }, 33 33 "trailingComment": { 34 34 Input: "@@ -21,5 +28,9 @@ func test(n int) {\n", 35 - Output: &Fragment{ 35 + Output: &TextFragment{ 36 36 Comment: "func test(n int) {", 37 37 OldPosition: 21, 38 38 OldLines: 5, ··· 75 75 func TestParseTextChunk(t *testing.T) { 76 76 tests := map[string]struct { 77 77 Input string 78 - Fragment Fragment 78 + Fragment TextFragment 79 79 80 - Output *Fragment 80 + Output *TextFragment 81 81 Err bool 82 82 }{ 83 83 "addWithContext": { ··· 86 86 +new line 2 87 87 context line 88 88 `, 89 - Fragment: Fragment{ 89 + Fragment: TextFragment{ 90 90 OldLines: 2, 91 91 NewLines: 4, 92 92 }, 93 - Output: &Fragment{ 93 + Output: &TextFragment{ 94 94 OldLines: 2, 95 95 NewLines: 4, 96 - Lines: []FragmentLine{ 96 + Lines: []Line{ 97 97 {OpContext, "context line\n"}, 98 98 {OpAdd, "new line 1\n"}, 99 99 {OpAdd, "new line 2\n"}, ··· 110 110 -old line 2 111 111 context line 112 112 `, 113 - Fragment: Fragment{ 113 + Fragment: TextFragment{ 114 114 OldLines: 4, 115 115 NewLines: 2, 116 116 }, 117 - Output: &Fragment{ 117 + Output: &TextFragment{ 118 118 OldLines: 4, 119 119 NewLines: 2, 120 - Lines: []FragmentLine{ 120 + Lines: []Line{ 121 121 {OpContext, "context line\n"}, 122 122 {OpDelete, "old line 1\n"}, 123 123 {OpDelete, "old line 2\n"}, ··· 134 134 +new line 1 135 135 context line 136 136 `, 137 - Fragment: Fragment{ 137 + Fragment: TextFragment{ 138 138 OldLines: 3, 139 139 NewLines: 3, 140 140 }, 141 - Output: &Fragment{ 141 + Output: &TextFragment{ 142 142 OldLines: 3, 143 143 NewLines: 3, 144 - Lines: []FragmentLine{ 144 + Lines: []Line{ 145 145 {OpContext, "context line\n"}, 146 146 {OpDelete, "old line 1\n"}, 147 147 {OpAdd, "new line 1\n"}, ··· 160 160 +new line 1 161 161 context line 162 162 `, 163 - Fragment: Fragment{ 163 + Fragment: TextFragment{ 164 164 OldLines: 4, 165 165 NewLines: 4, 166 166 }, 167 - Output: &Fragment{ 167 + Output: &TextFragment{ 168 168 OldLines: 4, 169 169 NewLines: 4, 170 - Lines: []FragmentLine{ 170 + Lines: []Line{ 171 171 {OpContext, "context line\n"}, 172 172 {OpDelete, "old line 1\n"}, 173 173 {OpContext, "context line\n"}, ··· 186 186 +new line 1 187 187 \ No newline at end of file 188 188 `, 189 - Fragment: Fragment{ 189 + Fragment: TextFragment{ 190 190 OldLines: 2, 191 191 NewLines: 2, 192 192 }, 193 - Output: &Fragment{ 193 + Output: &TextFragment{ 194 194 OldLines: 2, 195 195 NewLines: 2, 196 - Lines: []FragmentLine{ 196 + Lines: []Line{ 197 197 {OpContext, "context line\n"}, 198 198 {OpDelete, "old line 1\n"}, 199 199 {OpAdd, "new line 1"}, ··· 209 209 \ No newline at end of file 210 210 +new line 1 211 211 `, 212 - Fragment: Fragment{ 212 + Fragment: TextFragment{ 213 213 OldLines: 2, 214 214 NewLines: 2, 215 215 }, 216 - Output: &Fragment{ 216 + Output: &TextFragment{ 217 217 OldLines: 2, 218 218 NewLines: 2, 219 - Lines: []FragmentLine{ 219 + Lines: []Line{ 220 220 {OpContext, "context line\n"}, 221 221 {OpDelete, "old line 1"}, 222 222 {OpAdd, "new line 1\n"}, ··· 231 231 +new line 2 232 232 +new line 3 233 233 `, 234 - Fragment: Fragment{ 234 + Fragment: TextFragment{ 235 235 OldLines: 0, 236 236 NewLines: 3, 237 237 }, 238 - Output: &Fragment{ 238 + Output: &TextFragment{ 239 239 OldLines: 0, 240 240 NewLines: 3, 241 - Lines: []FragmentLine{ 241 + Lines: []Line{ 242 242 {OpAdd, "new line 1\n"}, 243 243 {OpAdd, "new line 2\n"}, 244 244 {OpAdd, "new line 3\n"}, ··· 251 251 -old line 2 252 252 -old line 3 253 253 `, 254 - Fragment: Fragment{ 254 + Fragment: TextFragment{ 255 255 OldLines: 3, 256 256 NewLines: 0, 257 257 }, 258 - Output: &Fragment{ 258 + Output: &TextFragment{ 259 259 OldLines: 3, 260 260 NewLines: 0, 261 - Lines: []FragmentLine{ 261 + Lines: []Line{ 262 262 {OpDelete, "old line 1\n"}, 263 263 {OpDelete, "old line 2\n"}, 264 264 {OpDelete, "old line 3\n"}, ··· 272 272 +new line 273 273 context line 274 274 `, 275 - Fragment: Fragment{ 275 + Fragment: TextFragment{ 276 276 OldLines: 3, 277 277 NewLines: 4, 278 278 }, 279 - Output: &Fragment{ 279 + Output: &TextFragment{ 280 280 OldLines: 3, 281 281 NewLines: 4, 282 - Lines: []FragmentLine{ 282 + Lines: []Line{ 283 283 {OpContext, "context line\n"}, 284 284 {OpContext, "\n"}, 285 285 {OpAdd, "new line\n"}, ··· 299 299 ?wat line 300 300 context line 301 301 `, 302 - Fragment: Fragment{ 302 + Fragment: TextFragment{ 303 303 OldLines: 3, 304 304 NewLines: 3, 305 305 }, ··· 311 311 +new line 1 312 312 context line 313 313 `, 314 - Fragment: Fragment{ 314 + Fragment: TextFragment{ 315 315 OldLines: 2, 316 316 NewLines: 5, 317 317 }, ··· 347 347 Input string 348 348 File File 349 349 350 - Fragments []*Fragment 350 + Fragments []*TextFragment 351 351 Err bool 352 352 }{ 353 353 "multipleChanges": { ··· 367 367 +new line 3 368 368 context line 369 369 `, 370 - Fragments: []*Fragment{ 370 + Fragments: []*TextFragment{ 371 371 { 372 372 OldPosition: 1, 373 373 OldLines: 3, 374 374 NewPosition: 1, 375 375 NewLines: 2, 376 - Lines: []FragmentLine{ 376 + Lines: []Line{ 377 377 {OpContext, "context line\n"}, 378 378 {OpDelete, "old line 1\n"}, 379 379 {OpContext, "context line\n"}, ··· 387 387 OldLines: 3, 388 388 NewPosition: 7, 389 389 NewLines: 3, 390 - Lines: []FragmentLine{ 390 + Lines: []Line{ 391 391 {OpContext, "context line\n"}, 392 392 {OpDelete, "old line 2\n"}, 393 393 {OpAdd, "new line 1\n"}, ··· 403 403 OldLines: 3, 404 404 NewPosition: 14, 405 405 NewLines: 4, 406 - Lines: []FragmentLine{ 406 + Lines: []Line{ 407 407 {OpContext, "context line\n"}, 408 408 {OpDelete, "old line 3\n"}, 409 409 {OpAdd, "new line 2\n"}, ··· 461 461 } 462 462 463 463 for i, frag := range test.Fragments { 464 - if !reflect.DeepEqual(frag, file.Fragments[i]) { 465 - t.Errorf("incorrect fragment at position %d\nexpected: %+v\nactual: %+v", i, frag, file.Fragments[i]) 464 + if !reflect.DeepEqual(frag, file.TextFragments[i]) { 465 + t.Errorf("incorrect fragment at position %d\nexpected: %+v\nactual: %+v", i, frag, file.TextFragments[i]) 466 466 } 467 467 } 468 468 })