@@ -357,75 +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
+ }
381
368
382
- names := strings .SplitN (fd .Extended [0 ][len ("diff --git " ):], " " , 2 )
383
- 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 " ):
369
+ linesHavePrefixes := func (idx1 int , prefix1 string , idx2 int , prefix2 string ) bool {
370
+ return lineHasPrefix (idx1 , prefix1 ) && lineHasPrefix (idx2 , prefix2 )
371
+ }
391
372
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
- }
397
- 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 [5 ], "Binary files " ) && strings .HasPrefix (fd .Extended [2 ], "rename from " ) && strings .HasPrefix (fd .Extended [3 ], "rename to " ):
411
- names := strings .SplitN (fd .Extended [0 ][len ("diff --git " ):], " " , 2 )
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 {
412
396
fd .OrigName = names [0 ]
397
+ }
398
+ fd .NewName , err = strconv .Unquote (names [1 ])
399
+ if err != nil {
413
400
fd .NewName = names [1 ]
414
- return true
415
- case lineCount == 3 && strings .HasPrefix (fd .Extended [2 ], "Binary files " ) || lineCount > 3 && strings .HasPrefix (fd .Extended [2 ], "GIT binary patch" ):
416
- names := strings .SplitN (fd .Extended [0 ][len ("diff --git " ):], " " , 2 )
417
- fd .OrigName , err = strconv .Unquote (names [0 ])
418
- if err != nil {
419
- fd .OrigName = names [0 ]
420
- }
421
- fd .NewName , err = strconv .Unquote (names [1 ])
422
- if err != nil {
423
- fd .NewName = names [1 ]
424
- }
425
- return true
426
- default :
427
- return false
428
401
}
402
+
403
+ if isNewFile {
404
+ fd .OrigName = "/dev/null"
405
+ }
406
+
407
+ if isDeletedFile {
408
+ fd .NewName = "/dev/null"
409
+ }
410
+
411
+ return true
429
412
}
430
413
431
414
var (
0 commit comments