diff --git a/README.md b/README.md index 28083873..95e5f154 100644 --- a/README.md +++ b/README.md @@ -112,7 +112,10 @@ affectedModuleDetector { - `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 - - SpecifiedBranchCommit: specify the branch to compare changes against using the `specifiedBranch` configuration before the `compareFrom` configuration + - SpecifiedBranchCommit: compare against the last commit of `$specifiedBranch` using `git rev-parse` approach. + - SpecifiedBranchCommit2: compare against the nearest ancestors with `$specifiedBranch` using `git merge base` approach. + + **Note:** specify the branch to compare changes against using the `specifiedBranch` configuration before the `compareFrom` configuration - `excludedModules`: A list of modules that will be excluded from the build process - `includeUncommitted`: If uncommitted files should be considered affected - `top`: The top of the git log to use. Must be used in combination with configuration `includeUncommitted = false` diff --git a/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/commitshaproviders/CommitShaProvider.kt b/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/commitshaproviders/CommitShaProvider.kt index 0d2c158d..2f00bae9 100644 --- a/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/commitshaproviders/CommitShaProvider.kt +++ b/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/commitshaproviders/CommitShaProvider.kt @@ -4,6 +4,7 @@ import com.dropbox.affectedmoduledetector.GitClient import com.dropbox.affectedmoduledetector.Sha interface CommitShaProvider { + fun get(commandRunner: GitClient.CommandRunner): Sha companion object { @@ -17,9 +18,14 @@ interface CommitShaProvider { } SpecifiedBranchCommit(specifiedBranch) } + "SpecifiedBranchCommit2" -> { + requireNotNull(specifiedBranch) { + "Specified branch must be defined" + } + SpecifiedBranchCommit2(specifiedBranch) + } else -> throw IllegalArgumentException("Unsupported compareFrom type") } } } } - diff --git a/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/commitshaproviders/SpecifiedBranchCommit2.kt b/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/commitshaproviders/SpecifiedBranchCommit2.kt new file mode 100644 index 00000000..e344d5cf --- /dev/null +++ b/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/commitshaproviders/SpecifiedBranchCommit2.kt @@ -0,0 +1,17 @@ +package com.dropbox.affectedmoduledetector.commitshaproviders + +import com.dropbox.affectedmoduledetector.GitClient +import com.dropbox.affectedmoduledetector.Sha + +class SpecifiedBranchCommit2(private val branch: String) : CommitShaProvider { + + override fun get(commandRunner: GitClient.CommandRunner): Sha { + val currentBranch = commandRunner.executeAndParseFirst(CURRENT_BRANCH_CMD) + return commandRunner.executeAndParseFirst("git merge-base $currentBranch $branch") + } + + companion object { + + const val CURRENT_BRANCH_CMD = "git rev-parse --abbrev-ref HEAD" + } +}