From ae165579d5e989bb19b4ade1088bae0cd65f312e Mon Sep 17 00:00:00 2001 From: Kostas Pagratis Date: Tue, 13 Feb 2024 14:24:06 -0700 Subject: [PATCH] Add the ability to configure ignoredFiles (#236) * Add the ability to configure ignoredFiles * doc tweak --- README.md | 4 ++++ .../AffectedModuleConfiguration.kt | 5 +++++ .../AffectedModuleDetector.kt | 3 ++- .../affectedmoduledetector/GitClient.kt | 12 ++++++++++-- .../GitClientImplTest.kt | 19 ++++++++++++++++--- 5 files changed, 37 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 5c1666e6..371df4fc 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,9 @@ affectedModuleDetector { excludedModules = [ "sample-util", ":(app|library):.+" ] + ignoredFiles = [ + ".*\\.md", ".*\\.txt", ".*README" + ] includeUncommitted = true top = "HEAD" customTasks = [ @@ -109,6 +112,7 @@ affectedModuleDetector { - `logFilename`: A filename for the output detector to use - `logFolder`: A folder to output the log file in - `specifiedBranch`: A branch to specify changes against. Must be used in combination with configuration `compareFrom = "SpecifiedBranchCommit"` + - `ignoredFiles`: A set of files that will be filtered out of the list of changed files retrieved by git. - `compareFrom`: A commit to compare the branch changes against. Can be either: - PreviousCommit: compare against the previous commit - ForkCommit: compare against the commit the branch was forked from diff --git a/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleConfiguration.kt b/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleConfiguration.kt index 83bddda2..82d4a1b1 100644 --- a/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleConfiguration.kt +++ b/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleConfiguration.kt @@ -105,6 +105,11 @@ class AffectedModuleConfiguration { */ var excludedModules = emptySet() + /** + * A set of files that will be filtered out of the list of changed files retrieved by git. + */ + var ignoredFiles = emptySet() + /** * If uncommitted files should be considered affected */ diff --git a/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetector.kt b/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetector.kt index 76012e28..2267465a 100644 --- a/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetector.kt +++ b/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetector.kt @@ -330,7 +330,8 @@ class AffectedModuleDetectorImpl constructor( injectedGitClient ?: GitClientImpl( rootProject.projectDir, logger, - commitShaProvider = CommitShaProvider.fromString(config.compareFrom, config.specifiedBranch) + commitShaProvider = CommitShaProvider.fromString(config.compareFrom, config.specifiedBranch), + ignoredFiles = config.ignoredFiles ) } diff --git a/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/GitClient.kt b/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/GitClient.kt index 1e5517cb..7063a80c 100644 --- a/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/GitClient.kt +++ b/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/GitClient.kt @@ -72,7 +72,8 @@ internal class GitClientImpl( workingDir = workingDir, logger = logger ), - private val commitShaProvider: CommitShaProvider + private val commitShaProvider: CommitShaProvider, + private val ignoredFiles: Set? ) : GitClient { /** @@ -85,13 +86,20 @@ internal class GitClientImpl( val sha = commitShaProvider.get(commandRunner) // use this if we don't want local changes - return commandRunner.executeAndParse( + val changedFiles = commandRunner.executeAndParse( if (includeUncommitted) { "$CHANGED_FILES_CMD_PREFIX $sha" } else { "$CHANGED_FILES_CMD_PREFIX $top..$sha" } ) + + return ignoredFiles + .orEmpty() + .map { it.toRegex() } + .foldRight(changedFiles){ignoredFileRegex: Regex, fileList: List -> + fileList.filterNot { it.matches(ignoredFileRegex) } + } } private fun findGitDirInParentFilepath(filepath: File): File? { diff --git a/affectedmoduledetector/src/test/kotlin/com/dropbox/affectedmoduledetector/GitClientImplTest.kt b/affectedmoduledetector/src/test/kotlin/com/dropbox/affectedmoduledetector/GitClientImplTest.kt index 90026404..82cf3346 100644 --- a/affectedmoduledetector/src/test/kotlin/com/dropbox/affectedmoduledetector/GitClientImplTest.kt +++ b/affectedmoduledetector/src/test/kotlin/com/dropbox/affectedmoduledetector/GitClientImplTest.kt @@ -27,17 +27,24 @@ class GitClientImplTest { workingDir = workingDir, logger = logger, commandRunner = commandRunner, - commitShaProvider = commitShaProvider + commitShaProvider = commitShaProvider, + ignoredFiles = setOf(".*\\.ignore", ".*IGNORED_FILE") ) @Test fun givenChangedFiles_whenFindChangedFilesIncludeUncommitted_thenReturnChanges() { val changes = listOf( + convertToFilePath("d", "e", ".ignoredNot"), convertToFilePath("a", "b", "c.java"), convertToFilePath("d", "e", "f.java")) + val changesWithIgnores = listOf( + convertToFilePath("a", "b", "anything.ignore"), + convertToFilePath("d", "e", ".ignore"), + convertToFilePath("d", "e", "IGNORED_FILE") + ) + changes commandRunner.addReply( "$CHANGED_FILES_CMD_PREFIX mySha", - changes.joinToString(System.lineSeparator()) + changesWithIgnores.joinToString(System.lineSeparator()) ) commitShaProvider.addReply("mySha") @@ -58,11 +65,17 @@ class GitClientImplTest { @Test fun findChangesSince_twoCls() { val changes = listOf( + convertToFilePath("d", "e", ".ignoredNot"), convertToFilePath("a", "b", "c.java"), convertToFilePath("d", "e", "f.java")) + val changesWithIgnores = listOf( + convertToFilePath("a", "b", "anything.ignore"), + convertToFilePath("d", "e", ".ignore"), + convertToFilePath("d", "e", "IGNORED_FILE") + ) + changes commandRunner.addReply( "$CHANGED_FILES_CMD_PREFIX otherSha..mySha", - changes.joinToString(System.lineSeparator()) + changesWithIgnores.joinToString(System.lineSeparator()) ) commitShaProvider.addReply("mySha") assertEquals(