Skip to content

Commit

Permalink
feat: add the ability to exclude commit message that match a configur…
Browse files Browse the repository at this point in the history
…ed regex. Closes #130
  • Loading branch information
nicolasfara committed Dec 31, 2022
1 parent 620e59d commit a822194
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ internal fun writeScript(
scopes: List<String>,
warningIfNoGitRoot: Boolean,
successMessage: String?,
failureMessage: String?
failureMessage: String?,
ignoreMessageCommit: String
) {
project.logger.debug("Finding the '.git' folder")
generateSequence(baseDir) { it.parentFile }.find { it.isGitFolder() }?.let {
val scriptContent = createCommitMessage(types, scopes, successMessage, failureMessage)
val scriptContent = createCommitMessage(types, scopes, successMessage, failureMessage, ignoreMessageCommit)
it.resolve(".git/hooks/commit-msg").writeScript(scriptContent)
project.logger.debug("[ConventionalCommits] script file written in '.git/hooks/commit-msg'")
} ?: run {
Expand All @@ -29,7 +30,8 @@ private fun createCommitMessage(
types: List<String>,
scopes: List<String>,
successMessage: String?,
failureMessage: String?
failureMessage: String?,
ignoreMessageCommit: String
): String {
val typesRegex = types.joinToString("|")
val scopesRegex = if (scopes.isEmpty()) "[a-z \\-]+" else scopes.joinToString("|")
Expand All @@ -41,10 +43,19 @@ private fun createCommitMessage(
# Regex for conventional commits
conventional_commits_regex="^($typesRegex)(\(($scopesRegex)\))?\!?:\ .+$"
# Regex used to exclude message commit that match this regex
exclude="$ignoreMessageCommit"
# Get the commit message (the parameter we're given is just the path to the
# temporary file which holds the message).
commit_message=$(cat "$1")
# Check if the message math the exclude regex, if so, all good baby.
if [[ "${'$'}commit_message" =~ ${'$'}exclude ]]; then
$successMessageEcho
exit 0
fi
# Check the message, if we match, all good baby.
if [[ "${'$'}commit_message" =~ ${'$'}conventional_commits_regex ]]; then
$successMessageEcho
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,24 @@ open class ConventionalCommitsExtension(private val project: Project) {
More details at: https://www.conventionalcommits.org/en/v1.0.0/#summary
""".trimIndent()

/**
* If the commit message match the following regex, the message is excluded from the check.
*/
var ignoreCommitMessage: String = ".^"

/**
* This method do all the work needed to write the script file.
*/
internal fun setupScript() {
writeScript(project, project.projectDir, types, scopes, warningIfNoGitRoot, successMessage, failureMessage)
writeScript(
project,
project.projectDir,
types,
scopes,
warningIfNoGitRoot,
successMessage,
failureMessage,
ignoreCommitMessage
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,33 @@ class ConventionalCommitsTest : WordSpec({
tasks.task(":tasks")?.outcome shouldBe TaskOutcome.SUCCESS
tasks.output.contains("Not '.git' root found! No script will be generated") shouldBe false
}
"ignore the messages that match the given regex" {
val projectDirectory = tempdir()
projectDirectory.resolve(".git/hooks").mkdirs()
projectDirectory.configureSettingsGradle { "" }
projectDirectory.configureBuildGradle {
"""
plugins {
id("it.nicolasfarabegoli.conventional-commits")
}
conventionalCommits {
ignoreCommitMessage = "^Merge .+$"
}
""".trimIndent()
}

val tasks = GradleRunner.create()
.forwardOutput()
.withPluginClasspath()
.withProjectDir(projectDirectory)
.withArguments("tasks")
.build()
tasks.task(":tasks")?.outcome shouldBe TaskOutcome.SUCCESS
val scriptContent = projectDirectory.resolve(".git/hooks/commit-msg").readText()
println(scriptContent)
scriptContent shouldContain "^Merge .+\$"
}
}
}
})

0 comments on commit a822194

Please sign in to comment.