Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a check for pre-normalize'd paths, to relativize #56

Merged
merged 1 commit into from
Jul 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())) {
Copy link
Member

@eed3si9n eed3si9n Jul 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this happening because JIO Path is unable to handle . and .git correctly?
What if you have ../foo and .git? Don't they start the same?

  1. It might make it easier for me to understand if the toPath and if expression part is factored out to an inner function named sharesPath.
  2. After resolving the symlinks, maybe you should convert it to an absolute URI instead?

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")
}
}