Skip to content

Commit

Permalink
Merge pull request #56 from dwijnand/relativize-fix-3
Browse files Browse the repository at this point in the history
Add a check for pre-normalize'd paths, to relativize
  • Loading branch information
eed3si9n committed Jul 14, 2017
2 parents 16c2e31 + d2ec6df commit f55849d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion io/src/main/scala/sbt/io/IO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ object IO {
def relativize(base: File, file: File): Option[String] = {
val basePath = base.toPath
val filePath = file.toPath
if (filePath.normalize() startsWith basePath.normalize()) {
if ((filePath startsWith basePath) || (filePath.normalize() startsWith basePath.normalize())) {
val relativePath = catching(classOf[IllegalArgumentException]) opt (basePath relativize filePath)
relativePath map (_.toString)
} else None
Expand Down
11 changes: 11 additions & 0 deletions io/src/test/scala/sbt/io/IOSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,15 @@ class IOSpec extends FlatSpec with Matchers {
IO.relativize(rootDir.toFile, nestedFile) shouldBe Some("meh.file")
IO.relativize(relativeRootDir, nestedFile) shouldBe Some("../../meh.file")
}

it should "relativize . dirs" in {
val base = new File(".")
val file1 = new File("./.git")
val file2 = new File(".", ".git")
val file3 = new File(base, ".git")

IO.relativize(base, file1) shouldBe Some(".git")
IO.relativize(base, file2) shouldBe Some(".git")
IO.relativize(base, file3) shouldBe Some(".git")
}
}

0 comments on commit f55849d

Please sign in to comment.