aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine
diff options
context:
space:
mode:
authorDuarte Meneses <duarte.meneses@sonarsource.com>2022-06-22 13:09:48 -0500
committersonartech <sonartech@sonarsource.com>2022-06-23 20:03:02 +0000
commit6b1a83cd3aa554094c019d634195729ece75ff3b (patch)
tree51c6c1878e13e826b2823dbca7b508445a6957a5 /sonar-scanner-engine
parent91042b91c6cff51d1d45884b42ba0235a94baffc (diff)
downloadsonarqube-6b1a83cd3aa554094c019d634195729ece75ff3b.tar.gz
sonarqube-6b1a83cd3aa554094c019d634195729ece75ff3b.zip
SONAR-16544 Scanner fails if there's no git HEAD commit
Diffstat (limited to 'sonar-scanner-engine')
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scm/git/CompositeBlameCommand.java12
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scm/git/CompositeBlameCommandTest.java22
-rw-r--r--sonar-scanner-engine/src/test/resources/org/sonar/scm/git/test-repos/no-head-git.zipbin0 -> 16789 bytes
3 files changed, 33 insertions, 1 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scm/git/CompositeBlameCommand.java b/sonar-scanner-engine/src/main/java/org/sonar/scm/git/CompositeBlameCommand.java
index a39b6401767..59cd3c40468 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scm/git/CompositeBlameCommand.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scm/git/CompositeBlameCommand.java
@@ -24,12 +24,14 @@ import java.io.IOException;
import java.nio.file.Files;
import java.util.HashSet;
import java.util.List;
+import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.Constants;
+import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
@@ -43,6 +45,8 @@ import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.api.utils.log.Profiler;
+import static java.util.Optional.ofNullable;
+
public class CompositeBlameCommand extends BlameCommand {
private static final Logger LOG = Loggers.get(CompositeBlameCommand.class);
@@ -96,9 +100,15 @@ public class CompositeBlameCommand extends BlameCommand {
private static Set<String> collectAllCommittedFiles(Repository repo) {
try {
Set<String> files = new HashSet<>();
+ Optional<ObjectId> headCommit = ofNullable(repo.resolve(Constants.HEAD));
+
+ if (headCommit.isEmpty()) {
+ LOG.warn("Could not find HEAD commit");
+ return files;
+ }
try (RevWalk revWalk = new RevWalk(repo)) {
- RevCommit head = revWalk.parseCommit(repo.resolve(Constants.HEAD));
+ RevCommit head = revWalk.parseCommit(headCommit.get());
try (TreeWalk treeWalk = new TreeWalk(repo)) {
treeWalk.addTree(head.getTree());
treeWalk.setRecursive(true);
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scm/git/CompositeBlameCommandTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scm/git/CompositeBlameCommandTest.java
index 5ed16c963de..792b501e093 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scm/git/CompositeBlameCommandTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scm/git/CompositeBlameCommandTest.java
@@ -132,6 +132,28 @@ public class CompositeBlameCommandTest {
}
@Test
+ public void skip_files_when_head_commit_is_missing() throws IOException {
+ // skip if git not installed
+ assumeTrue(gitBlameCommand.checkIfEnabled());
+
+ JGitBlameCommand jgit = mock(JGitBlameCommand.class);
+ BlameCommand blameCmd = new CompositeBlameCommand(analysisWarnings, pathResolver, jgit, gitBlameCommand);
+ File projectDir = createNewTempFolder();
+ javaUnzip("no-head-git.zip", projectDir);
+
+ File baseDir = new File(projectDir, "no-head-git");
+ setUpBlameInputWithFile(baseDir.toPath());
+ TestBlameOutput output = new TestBlameOutput();
+ blameCmd.blame(input, output);
+
+ assertThat(output.blame).isEmpty();
+ verifyNoInteractions(jgit);
+
+ assertThat(logTester.logs())
+ .contains("Could not find HEAD commit");
+ }
+
+ @Test
public void use_native_git_by_default() throws IOException {
// skip test if git is not installed
assumeTrue(gitBlameCommand.checkIfEnabled());
diff --git a/sonar-scanner-engine/src/test/resources/org/sonar/scm/git/test-repos/no-head-git.zip b/sonar-scanner-engine/src/test/resources/org/sonar/scm/git/test-repos/no-head-git.zip
new file mode 100644
index 00000000000..d63ef865c54
--- /dev/null
+++ b/sonar-scanner-engine/src/test/resources/org/sonar/scm/git/test-repos/no-head-git.zip
Binary files differ