diff options
author | Jacek <jacek.poreda@sonarsource.com> | 2022-04-11 10:21:00 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2022-04-11 20:02:58 +0000 |
commit | d3c6630b95fffa9c37a245672f25bdd412befc9b (patch) | |
tree | 49ed53277eb7cf139eb85b6e8bab073cd77de431 | |
parent | b574e0fd48c458e057e72b5f15b013cdc1d9e574 (diff) | |
download | sonarqube-d3c6630b95fffa9c37a245672f25bdd412befc9b.tar.gz sonarqube-d3c6630b95fffa9c37a245672f25bdd412befc9b.zip |
SONAR-16080 Skipping git ignore collection for git submodules which were not cloned
-rw-r--r-- | sonar-scanner-engine/src/main/java/org/sonar/scm/git/IncludedFilesRepository.java | 4 | ||||
-rw-r--r-- | sonar-scanner-engine/src/test/java/org/sonar/scm/git/GitIgnoreCommandTest.java | 30 |
2 files changed, 34 insertions, 0 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scm/git/IncludedFilesRepository.java b/sonar-scanner-engine/src/main/java/org/sonar/scm/git/IncludedFilesRepository.java index 73bc85aeb26..af1b7ea29cd 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scm/git/IncludedFilesRepository.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scm/git/IncludedFilesRepository.java @@ -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); } } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scm/git/GitIgnoreCommandTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scm/git/GitIgnoreCommandTest.java index ed43b81be6f..85c4c7f0e85 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scm/git/GitIgnoreCommandTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scm/git/GitIgnoreCommandTest.java @@ -19,10 +19,12 @@ */ 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; @@ -116,6 +118,34 @@ public class GitIgnoreCommandTest { } @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(); |