]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-16080 Skipping git ignore collection for git submodules which were not cloned
authorJacek <jacek.poreda@sonarsource.com>
Mon, 11 Apr 2022 08:21:00 +0000 (10:21 +0200)
committersonartech <sonartech@sonarsource.com>
Mon, 11 Apr 2022 20:02:58 +0000 (20:02 +0000)
sonar-scanner-engine/src/main/java/org/sonar/scm/git/IncludedFilesRepository.java
sonar-scanner-engine/src/test/java/org/sonar/scm/git/GitIgnoreCommandTest.java

index 73bc85aeb26a7383bd77ca3038e429398cacf324..af1b7ea29cd2f993472ccb493b52bd9fa769144e 100644 (file)
@@ -81,6 +81,10 @@ public class IncludedFilesRepository {
     try (SubmoduleWalk submoduleWalk = SubmoduleWalk.forIndex(repo)) {
       while (submoduleWalk.next()) {
         try (Repository submoduleRepo = submoduleWalk.getRepository()) {
+          if (submoduleRepo == null) {
+            LOG.debug("Git submodule [{}] found, but has not been cloned, skipping.", submoduleWalk.getPath());
+            continue;
+          }
           collectFilesIterative(submoduleRepo, baseDir);
         }
       }
index ed43b81be6ffda7e377e75de4a36bf3f01f17a53..85c4c7f0e85b39925aaf4f655b8ef0e06e2a7561 100644 (file)
  */
 package org.sonar.scm.git;
 
+import java.io.File;
 import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.util.Arrays;
+import org.apache.commons.io.FileUtils;
 import org.eclipse.jgit.api.Git;
 import org.eclipse.jgit.api.SubmoduleAddCommand;
 import org.eclipse.jgit.api.errors.GitAPIException;
@@ -115,6 +117,34 @@ public class GitIgnoreCommandTest {
     assertThat(logTester.logs(LoggerLevel.DEBUG)).contains(expectedIncludedFiles + " non excluded files in this Git repository");
   }
 
+  @Test
+  public void skip_submodules_if_not_cloned() throws IOException, GitAPIException {
+    Path projectDir = temp.newFolder().toPath();
+    Git git = Git.init().setDirectory(projectDir.toFile()).call();
+
+    createSubmoduleWithFiles(git, "module1");
+
+    Files.write(projectDir.resolve(".gitignore"), Arrays.asList("**/*.java"), UTF_8, TRUNCATE_EXISTING, CREATE);
+    createFolderStructure(projectDir, 1, 0, 1);
+
+    //clean submodule
+    FileUtils.cleanDirectory(new File(projectDir.toString(), "module1"));
+
+    logTester.setLevel(LoggerLevel.DEBUG);
+
+    GitIgnoreCommand underTest = new GitIgnoreCommand();
+    underTest.init(projectDir);
+
+    assertThat(underTest.isIgnored(projectDir.resolve("folder_0_0/Foo.java"))).isTrue();
+    assertThat(underTest.isIgnored(projectDir.resolve("folder_0_0/Foo.php"))).isFalse();
+
+    // ignoring not cloned submodules
+    assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("Git submodule [module1] found, but has not been cloned, skipping.");
+
+    int expectedIncludedFiles = 3;
+    assertThat(logTester.logs(LoggerLevel.DEBUG)).contains(expectedIncludedFiles + " non excluded files in this Git repository");
+  }
+
   @Test
   public void dont_index_files_outside_basedir() throws Exception {
     Path repoRoot = createGitRepoWithIgnore();