Skip to content
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.

Commit

Permalink
Fix #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 29, 2020
1 parent 440555f commit fa3b325
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 1 deletion.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@
</dependency>

<!-- Unit tests -->
<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest_2.11</artifactId>
<version>3.0.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.specs2</groupId>
<artifactId>specs2_${scala.version}</artifactId>
Expand Down
13 changes: 12 additions & 1 deletion 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/)?""".r ~> binaryFilePath <~ " and ") ~ ("""(?:b/)?""".r ~> binaryFilePath <~ " 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 @@ -103,6 +111,9 @@ object GitDiffParser extends RegexParsers {

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

// Match anything until " and " or " differ"
def binaryFilePath: Parser[String] = """.+?(?=(\sand\s)|(\sdiffer\r?\n))""".r

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

def hash: Parser[String] = """[0-9a-f]{7,}""".r
Expand Down
5 changes: 5 additions & 0 deletions src/test/resources/diffs/diff-binary-files.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
diff --git a/solutions/_templates/hybrid-solution/mvn/login/assets/finantix-logo.png b/solutions/_templates/hybrid/mvn/login/assets/finantix-logo.png
similarity index 100%
rename from solutions/_templates/hybrid-solution/mvn/login/assets/finantix-logo.png
rename to solutions/_templates/hybrid/mvn/login/assets/finantix-logo.png
Binary files a/solutions/_templates/hybrid-solution/mvn/login/assets/finantix-logo.png and b/solutions/_templates/hybrid/mvn/login/assets/finantix-logo.png differ
3 changes: 3 additions & 0 deletions src/test/resources/diffs/diff-excluded-file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
diff --git a/ui/pnpm-lock.yaml b/ui/pnpm-lock.yaml
index d1549e0..06b7b7d 100644
File excluded by pattern "pnpm-lock.yaml"
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ch.mibex.bitbucket.sonar.diff

import org.scalatest.FunSuite

class GitDiffParserTest extends FunSuite {
private def readFile(path: String) =
scala.io.Source.fromInputStream(getClass.getResourceAsStream(path)).mkString.replaceAll("\u0085", "")

test("The Binary files differ case should be correctly parsed") {
val response = GitDiffParser.parse(readFile("/diffs/diff-binary-files.txt"))
assert(response.isRight)
}

test("The File excluded by pattern case should be correctly parsed") {
val response = GitDiffParser.parse(readFile("/diffs/diff-excluded-file.txt"))
assert(response.isRight)
}
}

0 comments on commit fa3b325

Please sign in to comment.