From d3c6630b95fffa9c37a245672f25bdd412befc9b Mon Sep 17 00:00:00 2001 From: Jacek Date: Mon, 11 Apr 2022 10:21:00 +0200 Subject: SONAR-16080 Skipping git ignore collection for git submodules which were not cloned --- .../org/sonar/scm/git/IncludedFilesRepository.java | 4 +++ .../org/sonar/scm/git/GitIgnoreCommandTest.java | 30 ++++++++++++++++++++++ 2 files changed, 34 insertions(+) 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; @@ -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(); -- cgit v1.2.3