import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReader;
import org.sonar.server.computation.task.projectanalysis.component.Component;
+import org.sonar.server.computation.task.projectanalysis.component.Component.Status;
import org.sonar.server.computation.task.projectanalysis.source.SourceHashRepository;
import static java.util.Objects.requireNonNull;
LOGGER.trace("Reading SCM info from db for file '{}'", file.getKey());
try (DbSession dbSession = dbClient.openSession(false)) {
FileSourceDto dto = dbClient.fileSourceDao().selectSourceByFileUuid(dbSession, file.getUuid());
- if (dto == null || !sourceHashRepository.getRawSourceHash(file).equals(dto.getSrcHash())) {
+ if (dto == null || !isDtoValid(file, dto)) {
return NoScmInfo.INSTANCE;
}
return DbScmInfo.create(file, dto.getSourceData().getLinesList()).or(NoScmInfo.INSTANCE);
}
}
+ private boolean isDtoValid(Component file, FileSourceDto dto) {
+ if (analysisMetadataHolder.isIncrementalAnalysis() && file.getStatus() == Status.SAME) {
+ return true;
+ }
+ return sourceHashRepository.getRawSourceHash(file).equals(dto.getSrcHash());
+ }
+
private static ScmInfo getScmInfoFromReport(Component file, ScannerReport.Changesets changesets) {
LOGGER.trace("Reading SCM info from report for file '{}'", file.getKey());
return new ReportScmInfo(changesets);
*/
package org.sonar.server.computation.task.projectanalysis.scm;
-import com.google.common.collect.ImmutableList;
-import com.tngtech.java.junit.dataprovider.DataProvider;
-import com.tngtech.java.junit.dataprovider.DataProviderRunner;
-import com.tngtech.java.junit.dataprovider.UseDataProvider;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.guava.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.sonar.api.utils.log.LoggerLevel.TRACE;
+import static org.sonar.server.computation.task.projectanalysis.component.ReportComponent.builder;
+
import java.util.EnumSet;
import java.util.Iterator;
import java.util.List;
+
import javax.annotation.Nullable;
+
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReader;
import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReaderRule;
import org.sonar.server.computation.task.projectanalysis.component.Component;
+import org.sonar.server.computation.task.projectanalysis.component.Component.Status;
import org.sonar.server.computation.task.projectanalysis.component.ReportComponent;
import org.sonar.server.computation.task.projectanalysis.component.ViewsComponent;
import org.sonar.server.computation.task.projectanalysis.source.SourceHashRepository;
import org.sonar.server.computation.task.projectanalysis.source.SourceHashRepositoryImpl;
import org.sonar.server.computation.task.projectanalysis.source.SourceLinesRepositoryImpl;
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.guava.api.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verifyNoMoreInteractions;
-import static org.sonar.api.utils.log.LoggerLevel.TRACE;
-import static org.sonar.server.computation.task.projectanalysis.component.ReportComponent.builder;
+import com.google.common.collect.ImmutableList;
+import com.tngtech.java.junit.dataprovider.DataProvider;
+import com.tngtech.java.junit.dataprovider.DataProviderRunner;
+import com.tngtech.java.junit.dataprovider.UseDataProvider;
@RunWith(DataProviderRunner.class)
public class ScmInfoRepositoryImplTest {
ScmInfoRepositoryImpl underTest = new ScmInfoRepositoryImpl(reportReader, analysisMetadataHolder, dbClient,
new SourceHashRepositoryImpl(new SourceLinesRepositoryImpl(reportReader)));
+ @Test
+ public void dont_check_hash_for_unmodified_files_incremental_analysis() {
+ analysisMetadataHolder.setIncrementalAnalysis(true);
+ analysisMetadataHolder.setBaseAnalysis(baseProjectAnalysis);
+
+ addFileSourceInDb("henry", DATE_1, "rev-1", computeSourceHash(1));
+ addCopyFromPreviousChangesetInReport();
+
+ Component file = builder(Component.Type.FILE, FILE_REF).setKey("FILE_KEY").setUuid("FILE_UUID").setStatus(Status.SAME).build();
+ ScmInfo scmInfo = underTest.getScmInfo(file).get();
+ assertThat(scmInfo.getAllChangesets()).hasSize(1);
+
+ assertThat(logTester.logs(TRACE)).containsOnly("Reading SCM info from db for file 'FILE_KEY'");
+ }
+
@Test
public void read_from_report() throws Exception {
analysisMetadataHolder.setBaseAnalysis(baseProjectAnalysis);
@Test
public void getScmInfo_returns_ScmInfo_from_DB_CopyFromPrevious_is_true_if_hashes_are_the_same() throws Exception {
analysisMetadataHolder.setBaseAnalysis(baseProjectAnalysis);
+ analysisMetadataHolder.setIncrementalAnalysis(false);
+
addFileSourceInDb("henry", DATE_1, "rev-1", computeSourceHash(1));
addFileSourceInReport(1);
addCopyFromPreviousChangesetInReport();
@Test
public void getScmInfo_returns_absent_when_CopyFromPrevious_is_true_but_hashes_are_not_the_same() throws Exception {
analysisMetadataHolder.setBaseAnalysis(baseProjectAnalysis);
+ analysisMetadataHolder.setIncrementalAnalysis(false);
+
addFileSourceInDb("henry", DATE_1, "rev-1", computeSourceHash(1) + "_different");
addFileSourceInReport(1);
addCopyFromPreviousChangesetInReport();
@Test
public void read_from_db_even_if_data_in_report_exists_when_CopyFromPrevious_is_true() throws Exception {
analysisMetadataHolder.setBaseAnalysis(baseProjectAnalysis);
+ analysisMetadataHolder.setIncrementalAnalysis(false);
+
addFileSourceInDb("henry", DATE_1, "rev-1", computeSourceHash(1));
addFileSourceInReport(1);
addChangesetInReport("john", DATE_2, "rev-2", true);