]> source.dussan.org Git - gitblit.git/commitdiff
Improve relative path determination using Java 7 Paths 04/204/1
authorJames Moger <james.moger@gitblit.com>
Fri, 10 Oct 2014 16:04:39 +0000 (12:04 -0400)
committerJames Moger <james.moger@gitblit.com>
Fri, 10 Oct 2014 16:04:39 +0000 (12:04 -0400)
src/main/java/com/gitblit/utils/FileUtils.java

index a1eb5bba78d1699836d973bf2718966dd3886b77..27caa7eab20c6b929b2e3335f3bc4e6b49fdd3ab 100644 (file)
@@ -26,6 +26,8 @@ import java.io.IOException;
 import java.io.InputStreamReader;\r
 import java.io.OutputStreamWriter;\r
 import java.nio.charset.Charset;\r
+import java.nio.file.Path;\r
+import java.nio.file.Paths;\r
 \r
 /**\r
  * Common file utilities.\r
@@ -291,20 +293,10 @@ public class FileUtils {
         * @return a relative path from basePath to path\r
         */\r
        public static String getRelativePath(File basePath, File path) {\r
-               File exactBase = getExactFile(basePath);\r
-               File exactPath = getExactFile(path);\r
-               if (path.getAbsolutePath().startsWith(basePath.getAbsolutePath())) {\r
-                       // absolute base-path match\r
-                       return StringUtils.getRelativePath(basePath.getAbsolutePath(), path.getAbsolutePath());\r
-               } else if (exactPath.getPath().startsWith(exactBase.getPath())) {\r
-                       // canonical base-path match\r
-                       return StringUtils.getRelativePath(exactBase.getPath(), exactPath.getPath());\r
-               } else if (exactPath.getPath().startsWith(basePath.getAbsolutePath())) {\r
-                       // mixed path match\r
-                       return StringUtils.getRelativePath(basePath.getAbsolutePath(), exactPath.getPath());\r
-               } else if (path.getAbsolutePath().startsWith(exactBase.getPath())) {\r
-                       // mixed path match\r
-                       return StringUtils.getRelativePath(exactBase.getPath(), path.getAbsolutePath());\r
+               Path exactBase = Paths.get(getExactFile(basePath).toURI());\r
+               Path exactPath = Paths.get(getExactFile(path).toURI());\r
+               if (exactPath.startsWith(exactBase)) {\r
+                       return exactBase.relativize(exactPath).toString();\r
                }\r
                // no relative relationship\r
                return null;\r