Bläddra i källkod

Improve relative path determination using Java 7 Paths

tags/v1.6.1
James Moger 9 år sedan
förälder
incheckning
a74ddc2454
1 ändrade filer med 6 tillägg och 14 borttagningar
  1. 6
    14
      src/main/java/com/gitblit/utils/FileUtils.java

+ 6
- 14
src/main/java/com/gitblit/utils/FileUtils.java Visa fil

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

Laddar…
Avbryt
Spara