Skip to content

Commit

Permalink
Fix mibexsoftware#102 add renamed binary files and excluded file cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Laura Cameran committed Jun 22, 2020
1 parent 440555f commit 6979fb3
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/main/scala/ch/mibex/bitbucket/sonar/diff/GitDiffParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ object GitDiffParser extends RegexParsers {

case class FileChange(oldFile: String, newFile: String)

case class ExcludePattern(pattern: String)

sealed trait Diff
case class BinaryDiff() extends Diff
case class GitDiff(gitDiffHeader: FileChange, header: ExtendedDiffHeader, hunks: List[Hunk]) extends Diff {
Expand Down Expand Up @@ -57,10 +59,16 @@ object GitDiffParser extends RegexParsers {

def readUpToNextDiffOrEnd = """(?s).+?(?=((?:diff --git)|$))\n?""".r

def binaryDiff: Parser[BinaryDiff] = gitDiffHeader ~ extendedDiffHeader ~ "GIT binary patch" ~ readUpToNextDiffOrEnd ^^ {
def binaryDiff: Parser[BinaryDiff] = gitDiffHeader ~ extendedDiffHeader ~ (("GIT binary patch" ~ readUpToNextDiffOrEnd) | binaryFilesDiff | excludedFile) ^^ {
_ => BinaryDiff()
}

def binaryFilesDiff: Parser[FileChange] = "Binary files " ~> (("a/" ~> filePath) ~ (" and b/" ~> filePath)) <~ " differ" <~ nl ^^ {
case oldF ~ newF => FileChange(oldF, newF)
}

def excludedFile: Parser[ExcludePattern] = "File excluded by pattern " ~> """"(.+?)"""".r <~ nl ^^ { p => ExcludePattern(p) }

def gitDiff: Parser[GitDiff] = gitDiffHeader ~ extendedDiffHeader ~ hunks ^^ {
case fc ~ h ~ hs => GitDiff(fc, h, hs)
}
Expand Down Expand Up @@ -101,7 +109,7 @@ object GitDiffParser extends RegexParsers {

def fileMode: Parser[Int] = """[0-7]{6}""".r ^^ { _.toInt }

def filePath: Parser[String] = """.+?(?=(\sb/)|(\r?\n))""".r
def filePath: Parser[String] = """.+?(?=((\sand)?\sb/)|((\sdiffer)?\r?\n))""".r

def similarity: Parser[Int] = """\d{1,3}""".r ^^ { _.toInt }

Expand Down

0 comments on commit 6979fb3

Please sign in to comment.