aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/URIPredicate.java8
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/PathResolver.java31
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/DefaultInputModuleHierarchy.java2
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/FileIndexer.java7
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputComponentStore.java2
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/ModuleFileSystemInitializer.java12
6 files changed, 40 insertions, 22 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/URIPredicate.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/URIPredicate.java
index edf241d7207..8d1df915802 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/URIPredicate.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/URIPredicate.java
@@ -24,10 +24,10 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
+import java.util.Optional;
import org.sonar.api.batch.fs.FileSystem.Index;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.scan.filesystem.PathResolver;
-import org.sonar.api.utils.PathUtils;
/**
* @since 6.6
@@ -50,11 +50,11 @@ class URIPredicate extends AbstractFilePredicate {
@Override
public Iterable<InputFile> get(Index index) {
Path path = Paths.get(uri);
- String relative = PathUtils.sanitize(PathResolver.relativePath(baseDir, path));
- if (relative == null) {
+ Optional<String> relative = PathResolver.relativize(baseDir, path);
+ if (!relative.isPresent()) {
return Collections.emptyList();
}
- InputFile f = index.inputFile(relative);
+ InputFile f = index.inputFile(relative.get());
return f != null ? Arrays.asList(f) : Collections.<InputFile>emptyList();
}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/PathResolver.java b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/PathResolver.java
index 692fd5427da..cd90998741a 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/PathResolver.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/PathResolver.java
@@ -24,6 +24,7 @@ import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
+import java.util.Optional;
import javax.annotation.CheckForNull;
import javax.annotation.concurrent.Immutable;
import org.apache.commons.io.FilenameUtils;
@@ -39,10 +40,7 @@ import static java.util.stream.Collectors.joining;
@Immutable
public class PathResolver {
- /**
- * Static since 6.6
- */
- public static File relativeFile(File dir, String path) {
+ public File relativeFile(File dir, String path) {
return dir.toPath().resolve(path).normalize().toFile();
}
@@ -79,11 +77,10 @@ public class PathResolver {
* <li>null is returned if file is not a child of dir
* <li>the resulting path is converted to use Unix separators
* </ul>
- * Static since 6.6
* @since 6.0
*/
@CheckForNull
- public static String relativePath(Path dir, Path file) {
+ public String relativePath(Path dir, Path file) {
Path baseDir = dir.normalize();
Path path = file.normalize();
if (!path.startsWith(baseDir)) {
@@ -97,6 +94,28 @@ public class PathResolver {
}
}
+ /**
+ * Similar to {@link Path#relativize(Path)} except that:
+ * <ul>
+ * <li>Empty is returned if file is not a child of dir
+ * <li>the resulting path is converted to use Unix separators
+ * </ul>
+ * @since 6.6
+ */
+ public static Optional<String> relativize(Path dir, Path file) {
+ Path baseDir = dir.normalize();
+ Path path = file.normalize();
+ if (!path.startsWith(baseDir)) {
+ return Optional.empty();
+ }
+ try {
+ Path relativized = baseDir.relativize(path);
+ return Optional.of(FilenameUtils.separatorsToUnix(relativized.toString()));
+ } catch (IllegalArgumentException e) {
+ return Optional.empty();
+ }
+ }
+
@CheckForNull
public String relativePath(File dir, File file) {
return relativePath(dir.toPath(), file.toPath());
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/DefaultInputModuleHierarchy.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/DefaultInputModuleHierarchy.java
index 915504ba16c..4aeb1c4ffc1 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/DefaultInputModuleHierarchy.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/DefaultInputModuleHierarchy.java
@@ -110,6 +110,6 @@ public class DefaultInputModuleHierarchy implements InputModuleHierarchy {
Path parentBaseDir = parent.getBaseDir();
Path moduleBaseDir = inputModule.getBaseDir();
- return PathResolver.relativePath(parentBaseDir, moduleBaseDir);
+ return PathResolver.relativize(parentBaseDir, moduleBaseDir).orElse(null);
}
}
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/FileIndexer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/FileIndexer.java
index ea2b20fc4bc..09f774a0d32 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/FileIndexer.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/FileIndexer.java
@@ -191,11 +191,8 @@ public class FileIndexer {
private String getParentRelativePath(Path filePath) {
Path parentDir = filePath.getParent();
- String relativePath = PathResolver.relativePath(module.getBaseDir(), parentDir);
- if (relativePath == null) {
- throw new IllegalStateException("Failed to compute relative path of file: " + parentDir);
- }
- return relativePath;
+ return PathResolver.relativize(module.getBaseDir(), parentDir)
+ .orElseThrow(() -> new IllegalStateException("Failed to compute relative path of file: " + parentDir));
}
private void indexFileAndParentDir(InputFile inputFile, String parentRelativePath) {
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputComponentStore.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputComponentStore.java
index ee1b34a8d95..cc9c3b34123 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputComponentStore.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputComponentStore.java
@@ -149,7 +149,7 @@ public class InputComponentStore {
}
private String getProjectRelativePath(DefaultInputDir dir) {
- return PathResolver.relativePath(getProjectBaseDir(), dir.path());
+ return PathResolver.relativize(getProjectBaseDir(), dir.path()).orElseThrow(() -> new IllegalStateException("Dir " + dir.path() + " should be relative to project baseDir"));
}
private Path getProjectBaseDir() {
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/ModuleFileSystemInitializer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/ModuleFileSystemInitializer.java
index f5828172008..8a3c2893dc6 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/ModuleFileSystemInitializer.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/ModuleFileSystemInitializer.java
@@ -26,6 +26,7 @@ import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
+import java.util.Optional;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import org.apache.commons.lang.StringUtils;
@@ -59,10 +60,11 @@ public class ModuleFileSystemInitializer {
private static List<Path> initSources(ProjectDefinition module, Path baseDir, String propertyKey, String logLabel) {
List<Path> result = new ArrayList<>();
+ PathResolver pathResolver = new PathResolver();
String srcPropValue = module.properties().get(propertyKey);
if (srcPropValue != null) {
for (String sourcePath : parseAsCsv(propertyKey, srcPropValue)) {
- File dirOrFile = PathResolver.relativeFile(module.getBaseDir(), sourcePath);
+ File dirOrFile = pathResolver.relativeFile(module.getBaseDir(), sourcePath);
if (dirOrFile.exists()) {
result.add(dirOrFile.toPath());
}
@@ -102,13 +104,13 @@ public class ModuleFileSystemInitializer {
StringBuilder sb = new StringBuilder(label);
for (Iterator<Path> it = paths.iterator(); it.hasNext();) {
Path file = it.next();
- String relativePathToBaseDir = PathResolver.relativePath(baseDir, file);
- if (relativePathToBaseDir == null) {
+ Optional<String> relativePathToBaseDir = PathResolver.relativize(baseDir, file);
+ if (!relativePathToBaseDir.isPresent()) {
sb.append(file);
- } else if (StringUtils.isBlank(relativePathToBaseDir)) {
+ } else if (StringUtils.isBlank(relativePathToBaseDir.get())) {
sb.append(".");
} else {
- sb.append(relativePathToBaseDir);
+ sb.append(relativePathToBaseDir.get());
}
if (it.hasNext()) {
sb.append(", ");