Skip to content

Commit

Permalink
Simplify expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
eseliger committed May 26, 2020
1 parent 3277587 commit 6e43c89
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions diff/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,13 @@ func (r *FileDiffReader) ReadExtendedHeaders() ([]string, error) {
// that follow. It updates fd fields from the parsed extended headers.
func handleEmpty(fd *FileDiff) (wasEmpty bool) {
var err error
lineCount := len(fd.Extended)
if lineCount > 0 && !strings.HasPrefix(fd.Extended[0], "diff --git ") {
return false
}
switch {
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")) &&
strings.HasPrefix(fd.Extended[1], "new file mode ") && strings.HasPrefix(fd.Extended[0], "diff --git "):
case (lineCount == 3 || lineCount == 4 && strings.HasPrefix(fd.Extended[3], "Binary files ") || lineCount > 4 && strings.HasPrefix(fd.Extended[3], "GIT binary patch")) &&
strings.HasPrefix(fd.Extended[1], "new file mode "):

names := strings.SplitN(fd.Extended[0][len("diff --git "):], " ", 2)
fd.OrigName = "/dev/null"
Expand All @@ -351,8 +355,8 @@ func handleEmpty(fd *FileDiff) (wasEmpty bool) {
fd.NewName = names[1]
}
return true
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")) &&
strings.HasPrefix(fd.Extended[1], "deleted file mode ") && strings.HasPrefix(fd.Extended[0], "diff --git "):
case (lineCount == 3 || lineCount == 4 && strings.HasPrefix(fd.Extended[3], "Binary files ") || lineCount > 4 && strings.HasPrefix(fd.Extended[3], "GIT binary patch")) &&
strings.HasPrefix(fd.Extended[1], "deleted file mode "):

names := strings.SplitN(fd.Extended[0][len("diff --git "):], " ", 2)
fd.OrigName, err = strconv.Unquote(names[0])
Expand All @@ -361,7 +365,7 @@ func handleEmpty(fd *FileDiff) (wasEmpty bool) {
}
fd.NewName = "/dev/null"
return true
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 "):
case lineCount == 4 && strings.HasPrefix(fd.Extended[2], "rename from ") && strings.HasPrefix(fd.Extended[3], "rename to "):
names := strings.SplitN(fd.Extended[0][len("diff --git "):], " ", 2)
fd.OrigName, err = strconv.Unquote(names[0])
if err != nil {
Expand All @@ -372,7 +376,7 @@ func handleEmpty(fd *FileDiff) (wasEmpty bool) {
fd.NewName = names[1]
}
return true
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 "):
case lineCount == 3 && strings.HasPrefix(fd.Extended[2], "Binary files ") || lineCount > 3 && strings.HasPrefix(fd.Extended[2], "GIT binary patch"):
names := strings.SplitN(fd.Extended[0][len("diff --git "):], " ", 2)
fd.OrigName, err = strconv.Unquote(names[0])
if err != nil {
Expand Down

0 comments on commit 6e43c89

Please sign in to comment.