aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBelen Pruvost <belen.pruvost@sonarsource.com>2022-01-17 20:52:02 +0100
committersonartech <sonartech@sonarsource.com>2022-01-21 20:03:22 +0000
commit1f6d6df116aa34289d08ecc227d29bb069d456e8 (patch)
treef74e2176e67c12ef395a7617b62fbad1cb3b410a
parente2ee8eeba4e3c7b08cd929c56d0fb136d1cdf372 (diff)
downloadsonarqube-1f6d6df116aa34289d08ecc227d29bb069d456e8.tar.gz
sonarqube-1f6d6df116aa34289d08ecc227d29bb069d456e8.zip
SONAR-14929 - Detect trailing lines as new when it's a new change
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scm/git/GitScmProvider.java2
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scm/git/GitScmProviderTest.java28
2 files changed, 28 insertions, 2 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scm/git/GitScmProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scm/git/GitScmProvider.java
index d9157b5a3ce..784b9182af0 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scm/git/GitScmProvider.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scm/git/GitScmProvider.java
@@ -191,7 +191,7 @@ public class GitScmProvider extends ScmProvider {
// copied from DiffCommand so that we can use a custom DiffFormatter which ignores white spaces.
diffFmt.setRepository(repo);
diffFmt.setProgressMonitor(NullProgressMonitor.INSTANCE);
- diffFmt.setDiffComparator(RawTextComparator.WS_IGNORE_ALL);
+ diffFmt.setDiffComparator(RawTextComparator.DEFAULT);
Path workTree = repo.getWorkTree().toPath();
String relativePath = workTree.relativize(changedFile).toString();
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scm/git/GitScmProviderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scm/git/GitScmProviderTest.java
index daac3162570..a2c27b9c5c4 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scm/git/GitScmProviderTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scm/git/GitScmProviderTest.java
@@ -27,7 +27,6 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.time.Instant;
-import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
@@ -433,6 +432,33 @@ public class GitScmProviderTest {
}
@Test
+ public void branchChangedLines_should_always_detect_trailing_lines_that_are_not_endings() throws IOException, GitAPIException {
+ String fileName = "file-m1.xoo";
+ Path filePath = worktree.resolve(fileName);
+
+ StringBuilder newFileContent = new StringBuilder();
+ newFileContent
+ .append(randomizedContent(fileName, 3))
+ .append("\r\n")
+ .append("\n");
+
+ createAndCommitFile(fileName, newFileContent.toString());
+ ObjectId forkPoint = git.getRepository().exactRef("HEAD").getObjectId();
+
+ git.branchCreate().setName("b1").setStartPoint(forkPoint.getName()).call();
+ git.checkout().setName("b1").call();
+
+ String newFileContent2 = new String(Files.readAllBytes(filePath), StandardCharsets.UTF_8)
+ .replaceAll("\r\n", " \n");
+ Files.write(filePath, newFileContent2.getBytes(StandardCharsets.UTF_8), StandardOpenOption.TRUNCATE_EXISTING);
+ commit(fileName);
+
+ assertThat(newScmProvider().branchChangedLines("master", worktree, Collections.singleton(filePath)))
+ .containsOnly(
+ entry(worktree.resolve(fileName), new HashSet<>(Arrays.asList(4))));
+ }
+
+ @Test
public void branchChangedFiles_falls_back_to_origin_when_local_branch_does_not_exist() throws IOException, GitAPIException {
git.branchCreate().setName("b1").call();
git.checkout().setName("b1").call();