aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine/src
diff options
context:
space:
mode:
authorDuarte Meneses <duarte.meneses@sonarsource.com>2018-10-19 14:25:51 +0200
committerSonarTech <sonartech@sonarsource.com>2018-11-07 20:21:02 +0100
commit945beaac05d9a6b77b300ce877d4046f5918eded (patch)
treea7807f43e19a3777911b16c10fa27a1b90cd0bc5 /sonar-scanner-engine/src
parent647a6bbfd4ec768467ba75c82977e88bea1e8e2e (diff)
downloadsonarqube-945beaac05d9a6b77b300ce877d4046f5918eded.tar.gz
sonarqube-945beaac05d9a6b77b300ce877d4046f5918eded.zip
SONAR-11367 skip CPD for unchanged files in SLB
Diffstat (limited to 'sonar-scanner-engine/src')
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/cpd/CpdExecutor.java16
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/cpd/CpdExecutorTest.java9
2 files changed, 19 insertions, 6 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/cpd/CpdExecutor.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/cpd/CpdExecutor.java
index 464241ab670..1e7f07ec041 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/cpd/CpdExecutor.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/cpd/CpdExecutor.java
@@ -49,6 +49,7 @@ import org.sonar.scanner.protocol.output.ScannerReport;
import org.sonar.scanner.protocol.output.ScannerReport.Duplicate;
import org.sonar.scanner.protocol.output.ScannerReport.Duplication;
import org.sonar.scanner.report.ReportPublisher;
+import org.sonar.scanner.scan.branch.BranchConfiguration;
import org.sonar.scanner.scan.filesystem.InputComponentStore;
import org.sonar.scanner.util.ProgressReport;
@@ -67,21 +68,24 @@ public class CpdExecutor {
private final SonarCpdBlockIndex index;
private final ReportPublisher publisher;
private final InputComponentStore componentStore;
+ private final BranchConfiguration branchConfiguration;
private final ProgressReport progressReport;
private final CpdSettings settings;
private final ExecutorService executorService;
private int count = 0;
private int total;
- public CpdExecutor(CpdSettings settings, SonarCpdBlockIndex index, ReportPublisher publisher, InputComponentStore inputComponentCache) {
- this(settings, index, publisher, inputComponentCache, Executors.newSingleThreadExecutor());
+ public CpdExecutor(CpdSettings settings, SonarCpdBlockIndex index, ReportPublisher publisher, InputComponentStore inputComponentCache, BranchConfiguration branchConfiguration) {
+ this(settings, index, publisher, inputComponentCache, branchConfiguration, Executors.newSingleThreadExecutor());
}
- public CpdExecutor(CpdSettings settings, SonarCpdBlockIndex index, ReportPublisher publisher, InputComponentStore inputComponentCache, ExecutorService executorService) {
+ public CpdExecutor(CpdSettings settings, SonarCpdBlockIndex index, ReportPublisher publisher, InputComponentStore inputComponentCache,
+ BranchConfiguration branchConfiguration, ExecutorService executorService) {
this.settings = settings;
this.index = index;
this.publisher = publisher;
this.componentStore = inputComponentCache;
+ this.branchConfiguration = branchConfiguration;
this.progressReport = new ProgressReport("CPD computation", TimeUnit.SECONDS.toMillis(10));
this.executorService = executorService;
}
@@ -98,7 +102,7 @@ public class CpdExecutor {
while (it.hasNext()) {
ResourceBlocks resourceBlocks = it.next();
Optional<FileBlocks> fileBlocks = toFileBlocks(resourceBlocks.resourceId(), resourceBlocks.blocks());
- if (!fileBlocks.isPresent() || fileBlocks.get().getInputFile().status() == InputFile.Status.SAME) {
+ if (!fileBlocks.isPresent() || shouldSkip(fileBlocks.get().getInputFile())) {
continue;
}
components.add(fileBlocks.get());
@@ -125,6 +129,10 @@ public class CpdExecutor {
}
}
+ private boolean shouldSkip(DefaultInputFile inputFile) {
+ return branchConfiguration.isShortOrPullRequest() && inputFile.status() == InputFile.Status.SAME;
+ }
+
private static String pluralize(int files) {
return files == 1 ? "file" : "files";
}
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/cpd/CpdExecutorTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/cpd/CpdExecutorTest.java
index 929afe6a7c0..ce60085a323 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/cpd/CpdExecutorTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/cpd/CpdExecutorTest.java
@@ -77,6 +77,7 @@ public class CpdExecutorTest {
private ExecutorService executorService = mock(ExecutorService.class);
private CpdSettings settings = mock(CpdSettings.class);
private ReportPublisher publisher = mock(ReportPublisher.class);
+ private BranchConfiguration branchConfiguration = mock(BranchConfiguration.class);
private SonarCpdBlockIndex index = new SonarCpdBlockIndex(publisher, settings);
private ScannerReportReader reader;
private DefaultInputFile batchComponent1;
@@ -94,7 +95,7 @@ public class CpdExecutorTest {
DefaultInputModule inputModule = TestInputFileBuilder.newDefaultInputModule("foo", baseDir);
componentStore = new InputComponentStore(inputModule, mock(BranchConfiguration.class));
- executor = new CpdExecutor(settings, index, publisher, componentStore, executorService);
+ executor = new CpdExecutor(settings, index, publisher, componentStore, branchConfiguration, executorService);
reader = new ScannerReportReader(outputDir);
batchComponent1 = createComponent("src/Foo.php", 5);
@@ -197,7 +198,11 @@ public class CpdExecutorTest {
@Test
public void should_ignore_unmodified_files_in_SLB() {
- Block block = Block.builder().setBlockHash(new ByteArray("AAAABBBBCCCC")).build();
+ when(branchConfiguration.isShortOrPullRequest()).thenReturn(true);
+ Block block = Block.builder()
+ .setBlockHash(new ByteArray("AAAABBBBCCCC"))
+ .setResourceId(batchComponent4.key())
+ .build();
index.insert(batchComponent4, Collections.singletonList(block));
executor.execute();