Skip to content

Commit

Permalink
Add a check for pre-normalize'd paths, to relativize
Browse files Browse the repository at this point in the history
Should fix:

* sbt/sbt#3286
* sbt/sbt#3276
  • Loading branch information
dwijnand committed Jul 10, 2017
1 parent 16c2e31 commit d2ec6df
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 d2ec6df

Please sign in to comment.