Skip to content

Commit

Permalink
Add the ability to configure ignoredFiles (#236)
Browse files Browse the repository at this point in the history
* Add the ability to configure ignoredFiles

* doc tweak
  • Loading branch information
kpagratis authored Feb 13, 2024
1 parent c5977c7 commit ae16557
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 6 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ affectedModuleDetector {
excludedModules = [
"sample-util", ":(app|library):.+"
]
ignoredFiles = [
".*\\.md", ".*\\.txt", ".*README"
]
includeUncommitted = true
top = "HEAD"
customTasks = [
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ class AffectedModuleConfiguration {
*/
var excludedModules = emptySet<String>()

/**
* A set of files that will be filtered out of the list of changed files retrieved by git.
*/
var ignoredFiles = emptySet<String>()

/**
* If uncommitted files should be considered affected
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ internal class GitClientImpl(
workingDir = workingDir,
logger = logger
),
private val commitShaProvider: CommitShaProvider
private val commitShaProvider: CommitShaProvider,
private val ignoredFiles: Set<String>?
) : GitClient {

/**
Expand All @@ -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<String> ->
fileList.filterNot { it.matches(ignoredFileRegex) }
}
}

private fun findGitDirInParentFilepath(filepath: File): File? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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(
Expand Down

0 comments on commit ae16557

Please sign in to comment.