Skip to content

Commit 6e43c89

Browse files
committed
Simplify expressions
1 parent 3277587 commit 6e43c89

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

diff/parse.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -340,9 +340,13 @@ func (r *FileDiffReader) ReadExtendedHeaders() ([]string, error) {
340340
// that follow. It updates fd fields from the parsed extended headers.
341341
func handleEmpty(fd *FileDiff) (wasEmpty bool) {
342342
var err error
343+
lineCount := len(fd.Extended)
344+
if lineCount > 0 && !strings.HasPrefix(fd.Extended[0], "diff --git ") {
345+
return false
346+
}
343347
switch {
344-
case (len(fd.Extended) == 3 || len(fd.Extended) == 4 && strings.HasPrefix(fd.Extended[3], "Binary files ") || len(fd.Extended) > 4 && strings.HasPrefix(fd.Extended[3], "GIT binary patch")) &&
345-
strings.HasPrefix(fd.Extended[1], "new file mode ") && strings.HasPrefix(fd.Extended[0], "diff --git "):
348+
case (lineCount == 3 || lineCount == 4 && strings.HasPrefix(fd.Extended[3], "Binary files ") || lineCount > 4 && strings.HasPrefix(fd.Extended[3], "GIT binary patch")) &&
349+
strings.HasPrefix(fd.Extended[1], "new file mode "):
346350

347351
names := strings.SplitN(fd.Extended[0][len("diff --git "):], " ", 2)
348352
fd.OrigName = "/dev/null"
@@ -351,8 +355,8 @@ func handleEmpty(fd *FileDiff) (wasEmpty bool) {
351355
fd.NewName = names[1]
352356
}
353357
return true
354-
case (len(fd.Extended) == 3 || len(fd.Extended) == 4 && strings.HasPrefix(fd.Extended[3], "Binary files ") || len(fd.Extended) > 4 && strings.HasPrefix(fd.Extended[3], "GIT binary patch")) &&
355-
strings.HasPrefix(fd.Extended[1], "deleted file mode ") && strings.HasPrefix(fd.Extended[0], "diff --git "):
358+
case (lineCount == 3 || lineCount == 4 && strings.HasPrefix(fd.Extended[3], "Binary files ") || lineCount > 4 && strings.HasPrefix(fd.Extended[3], "GIT binary patch")) &&
359+
strings.HasPrefix(fd.Extended[1], "deleted file mode "):
356360

357361
names := strings.SplitN(fd.Extended[0][len("diff --git "):], " ", 2)
358362
fd.OrigName, err = strconv.Unquote(names[0])
@@ -361,7 +365,7 @@ func handleEmpty(fd *FileDiff) (wasEmpty bool) {
361365
}
362366
fd.NewName = "/dev/null"
363367
return true
364-
case len(fd.Extended) == 4 && strings.HasPrefix(fd.Extended[2], "rename from ") && strings.HasPrefix(fd.Extended[3], "rename to ") && strings.HasPrefix(fd.Extended[0], "diff --git "):
368+
case lineCount == 4 && strings.HasPrefix(fd.Extended[2], "rename from ") && strings.HasPrefix(fd.Extended[3], "rename to "):
365369
names := strings.SplitN(fd.Extended[0][len("diff --git "):], " ", 2)
366370
fd.OrigName, err = strconv.Unquote(names[0])
367371
if err != nil {
@@ -372,7 +376,7 @@ func handleEmpty(fd *FileDiff) (wasEmpty bool) {
372376
fd.NewName = names[1]
373377
}
374378
return true
375-
case (len(fd.Extended) == 3 && strings.HasPrefix(fd.Extended[2], "Binary files ") || len(fd.Extended) > 3 && strings.HasPrefix(fd.Extended[2], "GIT binary patch")) && strings.HasPrefix(fd.Extended[0], "diff --git "):
379+
case lineCount == 3 && strings.HasPrefix(fd.Extended[2], "Binary files ") || lineCount > 3 && strings.HasPrefix(fd.Extended[2], "GIT binary patch"):
376380
names := strings.SplitN(fd.Extended[0][len("diff --git "):], " ", 2)
377381
fd.OrigName, err = strconv.Unquote(names[0])
378382
if err != nil {

0 commit comments

Comments
 (0)