@@ -357,92 +357,58 @@ func (r *FileDiffReader) ReadExtendedHeaders() ([]string, error) {
357
357
// handleEmpty detects when FileDiff was an empty diff and will not have any hunks
358
358
// that follow. It updates fd fields from the parsed extended headers.
359
359
func handleEmpty (fd * FileDiff ) (wasEmpty bool ) {
360
- var err error
361
360
lineCount := len (fd .Extended )
362
361
if lineCount > 0 && ! strings .HasPrefix (fd .Extended [0 ], "diff --git " ) {
363
362
return false
364
363
}
365
- switch {
366
- case (lineCount == 3 || lineCount == 4 && strings .HasPrefix (fd .Extended [3 ], "Binary files " ) || lineCount > 4 && strings .HasPrefix (fd .Extended [3 ], "GIT binary patch" )) &&
367
- strings .HasPrefix (fd .Extended [1 ], "old mode " ) && strings .HasPrefix (fd .Extended [2 ], "new mode " ):
368
364
369
- names := strings .SplitN (fd .Extended [0 ][len ("diff --git " ):], " " , 2 )
370
- fd .OrigName , err = strconv .Unquote (names [0 ])
371
- if err != nil {
372
- fd .OrigName = names [0 ]
373
- }
374
- fd .NewName , err = strconv .Unquote (names [1 ])
375
- if err != nil {
376
- fd .NewName = names [1 ]
377
- }
378
- return true
379
- case (lineCount == 3 || lineCount == 4 && strings .HasPrefix (fd .Extended [3 ], "Binary files " ) || lineCount > 4 && strings .HasPrefix (fd .Extended [3 ], "GIT binary patch" )) &&
380
- strings .HasPrefix (fd .Extended [1 ], "new file mode " ):
365
+ lineHasPrefix := func (idx int , prefix string ) bool {
366
+ return strings .HasPrefix (fd .Extended [idx ], prefix )
367
+ }
368
+
369
+ linesHavePrefixes := func (idx1 int , prefix1 string , idx2 int , prefix2 string ) bool {
370
+ return lineHasPrefix (idx1 , prefix1 ) && lineHasPrefix (idx2 , prefix2 )
371
+ }
372
+
373
+ isRename := (lineCount == 4 && linesHavePrefixes (2 , "rename from " , 3 , "rename to " )) ||
374
+ (lineCount == 6 && linesHavePrefixes (2 , "rename from " , 3 , "rename to " ) && lineHasPrefix (5 , "Binary files " )) ||
375
+ (lineCount == 6 && linesHavePrefixes (1 , "old mode " , 2 , "new mode " ) && linesHavePrefixes (4 , "rename from " , 5 , "rename to " ))
376
+
377
+ isDeletedFile := (lineCount == 3 || lineCount == 4 && lineHasPrefix (3 , "Binary files " ) || lineCount > 4 && lineHasPrefix (3 , "GIT binary patch" )) &&
378
+ lineHasPrefix (1 , "deleted file mode " )
379
+
380
+ isNewFile := (lineCount == 3 || lineCount == 4 && lineHasPrefix (3 , "Binary files " ) || lineCount > 4 && lineHasPrefix (3 , "GIT binary patch" )) &&
381
+ lineHasPrefix (1 , "new file mode " )
382
+
383
+ isModeChange := lineCount == 3 && linesHavePrefixes (1 , "old mode " , 2 , "new mode " )
384
+
385
+ isBinaryPatch := lineCount == 3 && lineHasPrefix (2 , "Binary files " ) || lineCount > 3 && lineHasPrefix (2 , "GIT binary patch" )
386
+
387
+ if ! isModeChange && ! isRename && ! isBinaryPatch && ! isNewFile && ! isDeletedFile {
388
+ return false
389
+ }
390
+
391
+ names := strings .SplitN (fd .Extended [0 ][len ("diff --git " ):], " " , 2 )
392
+
393
+ var err error
394
+ fd .OrigName , err = strconv .Unquote (names [0 ])
395
+ if err != nil {
396
+ fd .OrigName = names [0 ]
397
+ }
398
+ fd .NewName , err = strconv .Unquote (names [1 ])
399
+ if err != nil {
400
+ fd .NewName = names [1 ]
401
+ }
381
402
382
- names := strings . SplitN ( fd . Extended [ 0 ][ len ( "diff --git " ):], " " , 2 )
403
+ if isNewFile {
383
404
fd .OrigName = "/dev/null"
384
- fd .NewName , err = strconv .Unquote (names [1 ])
385
- if err != nil {
386
- fd .NewName = names [1 ]
387
- }
388
- return true
389
- case (lineCount == 3 || lineCount == 4 && strings .HasPrefix (fd .Extended [3 ], "Binary files " ) || lineCount > 4 && strings .HasPrefix (fd .Extended [3 ], "GIT binary patch" )) &&
390
- strings .HasPrefix (fd .Extended [1 ], "deleted file mode " ):
405
+ }
391
406
392
- names := strings .SplitN (fd .Extended [0 ][len ("diff --git " ):], " " , 2 )
393
- fd .OrigName , err = strconv .Unquote (names [0 ])
394
- if err != nil {
395
- fd .OrigName = names [0 ]
396
- }
407
+ if isDeletedFile {
397
408
fd .NewName = "/dev/null"
398
- return true
399
- case lineCount == 4 && strings .HasPrefix (fd .Extended [2 ], "rename from " ) && strings .HasPrefix (fd .Extended [3 ], "rename to " ):
400
- names := strings .SplitN (fd .Extended [0 ][len ("diff --git " ):], " " , 2 )
401
- fd .OrigName , err = strconv .Unquote (names [0 ])
402
- if err != nil {
403
- fd .OrigName = names [0 ]
404
- }
405
- fd .NewName , err = strconv .Unquote (names [1 ])
406
- if err != nil {
407
- fd .NewName = names [1 ]
408
- }
409
- return true
410
- case lineCount == 6 && strings .HasPrefix (fd .Extended [2 ], "rename from " ) && strings .HasPrefix (fd .Extended [3 ], "rename to " ) && strings .HasPrefix (fd .Extended [5 ], "Binary files " ):
411
- names := strings .SplitN (fd .Extended [0 ][len ("diff --git " ):], " " , 2 )
412
- fd .OrigName , err = strconv .Unquote (names [0 ])
413
- if err != nil {
414
- fd .OrigName = names [0 ]
415
- }
416
- fd .NewName , err = strconv .Unquote (names [1 ])
417
- if err != nil {
418
- fd .NewName = names [1 ]
419
- }
420
- return true
421
- case lineCount == 6 && strings .HasPrefix (fd .Extended [4 ], "rename from " ) && strings .HasPrefix (fd .Extended [5 ], "rename to " ):
422
- names := strings .SplitN (fd .Extended [0 ][len ("diff --git " ):], " " , 2 )
423
- fd .OrigName , err = strconv .Unquote (names [0 ])
424
- if err != nil {
425
- fd .OrigName = names [0 ]
426
- }
427
- fd .NewName , err = strconv .Unquote (names [1 ])
428
- if err != nil {
429
- fd .NewName = names [1 ]
430
- }
431
- return true
432
- case lineCount == 3 && strings .HasPrefix (fd .Extended [2 ], "Binary files " ) || lineCount > 3 && strings .HasPrefix (fd .Extended [2 ], "GIT binary patch" ):
433
- names := strings .SplitN (fd .Extended [0 ][len ("diff --git " ):], " " , 2 )
434
- fd .OrigName , err = strconv .Unquote (names [0 ])
435
- if err != nil {
436
- fd .OrigName = names [0 ]
437
- }
438
- fd .NewName , err = strconv .Unquote (names [1 ])
439
- if err != nil {
440
- fd .NewName = names [1 ]
441
- }
442
- return true
443
- default :
444
- return false
445
409
}
410
+
411
+ return true
446
412
}
447
413
448
414
var (
0 commit comments