Skip to content

Commit

Permalink
Merge pull request Kaaveh#245 from VahidGarousi/240-verify-that-a-com…
Browse files Browse the repository at this point in the history
…mit-message-contains-issue-number-with-pre-commit-hook

Verify that a commit message contains issue number with pre commit hook
  • Loading branch information
Kaaveh authored Jan 5, 2025
2 parents 3457e5a + efda6c5 commit 6884151
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 29 deletions.
131 changes: 131 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import org.apache.tools.ant.taskdefs.condition.Os

plugins {
libs.plugins.apply {
alias(android.application) apply false
Expand Down Expand Up @@ -33,3 +35,132 @@ subprojects {
}
}
}

tasks.register("clean", Delete::class) {
delete(rootProject.layout.buildDirectory)
}

configureGitHooks()
fun Project.configureGitHooks() {
registerCopyCommitMsgHookTask()
registerCopyPreCommitHookTask()
registerCopyPrePushHookTask()
registerCopyGitHooksTask()
registerInstallGitHooksTask()
}

fun Project.registerCopyPreCommitHookTask() {
tasks.register("copyPreCommitHook", Copy::class.java) {
group = "git hooks"
val suffix = osSuffix()
val preCommitFile = file("$rootDir/.git/hooks/pre-commit")
doFirst {
if (preCommitFile.exists()) {
logger.warn("\u001b[31mExisting pre-commit hook found. Deleting...\u001b[0m")
if (!preCommitFile.delete()) {
throw GradleException("Failed to delete existing pre-commit hook. Please check file permissions.")
}
}
}
from(file("$rootDir/git-hooks/pre-commit-$suffix.sh"))
into("$rootDir/.git/hooks")
rename("pre-commit-$suffix.sh", "pre-commit")
filePermissions {
user {
read = true
write = true
execute = true
}
}
}
}

fun Project.registerCopyPrePushHookTask() {
tasks.register("copyPrePushHook", Copy::class.java) {
group = "git hooks"
val suffix = osSuffix()
val prePushFile = file("$rootDir/.git/hooks/pre-push")
doFirst {
if (prePushFile.exists()) {
logger.lifecycle("\u001b[31mExisting pre-push hook found. Deleting...\u001b[0m")
if (!prePushFile.delete()) {
throw GradleException("Failed to delete existing pre-push hook. Please check file permissions.")
}
}
}
from(file("$rootDir/git-hooks/pre-push-$suffix.sh"))
into("$rootDir/.git/hooks")
rename("pre-push-$suffix.sh", "pre-push")
filePermissions {
user {
read = true
write = true
execute = true
}
}
}
}

fun Project.registerCopyCommitMsgHookTask() {
tasks.register("copyCommitMsgHook", Copy::class.java) {
group = "git hooks"
val suffix = osSuffix()
val commitMsgFile = file("$rootDir/.git/hooks/commit-msg")
doFirst {
if (commitMsgFile.exists()) {
logger.warn("\u001b[31mExisting commit-msg hook found. Deleting...\u001b[0m")
if (!commitMsgFile.delete()) {
throw GradleException("Failed to delete existing commit-msg hook. Please check file permissions.")
}
}
}
from(file("$rootDir/git-hooks/commit-msg-$suffix.sh"))
into("$rootDir/.git/hooks")
rename("commit-msg-$suffix.sh", "commit-msg")
filePermissions {
user {
read = true
write = true
execute = true
}
}
}
}

fun Project.registerCopyGitHooksTask() {
tasks.register("copyGitHooks", Copy::class.java) {
group = "git hooks"
description = "Copies the git hooks from /git-hooks to the .git folder."
dependsOn("copyPrePushHook", "copyPreCommitHook","copyCommitMsgHook")
}
}
fun Project.registerInstallGitHooksTask() {
tasks.register("installGitHooks", Exec::class.java) {
group = "git hooks"
description = "Installs the pre-commit git hooks from /git-hooks."
workingDir = rootDir
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
commandLine("attrib", "+r", "/s", ".git\\hooks\\*.*")
} else {
commandLine("chmod", "-R", "+x", ".git/hooks/")
}
dependsOn("copyGitHooks")
doLast {
logger.info("Git hook installed successfully.")
}
}
}

fun osSuffix(): String {
return if (Os.isFamily(Os.FAMILY_WINDOWS)) {
"windows"
} else {
"unix"
}
}
afterEvaluate {
// We install the hook at the first occasion
tasks.named("clean") {
dependsOn(":installGitHooks")
}
}
29 changes: 29 additions & 0 deletions git-hooks/commit-msg-unix.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash

# Path to the commit message file
commit_msg_file="$1"

# Regular expression to match issue numbers starting with #
# Examples: #123, [#123]
issue_pattern="(\[#?[0-9]+\]|#?[0-9]+)"

# Read the commit message
commit_msg=$(cat "$commit_msg_file")

echo "Validating commit message..."
echo "Debug: Commit message is: '$commit_msg'"

# Validate commit message
if ! echo "$commit_msg" | grep -qE "$issue_pattern"; then
echo "❌ Commit message validation failed!"
echo "👉 Your commit message must include an issue number starting with # (e.g., #123 or [#123])."
echo ""
echo "Examples:"
echo " ✅ Fix bug in login flow #123"
echo " ✅ [#456] Refactor login module"
echo " ✅ Add tests for API #789"
echo ""
exit 1
fi

echo "✅ Commit message validation passed."
29 changes: 29 additions & 0 deletions git-hooks/commit-msg-windows.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!C:/Program\ Files/Git/usr/bin/sh.exe

# Path to the commit message file
commit_msg_file="$1"

# Regular expression to match issue numbers starting with #
# Examples: #123, [#123]
issue_pattern="(\[#?[0-9]+\]|#?[0-9]+)"

# Read the commit message
commit_msg=$(cat "$commit_msg_file")

echo "Validating commit message..."
echo "Debug: Commit message is: '$commit_msg'"

# Validate commit message
if ! echo "$commit_msg" | grep -qE "$issue_pattern"; then
echo "❌ Commit message validation failed!"
echo "👉 Your commit message must include an issue number starting with # (e.g., #123 or [#123])."
echo ""
echo "Examples:"
echo " ✅ Fix bug in login flow #123"
echo " ✅ [#456] Refactor login module"
echo " ✅ Add tests for API #789"
echo ""
exit 1
fi

echo "✅ Commit message validation passed."
3 changes: 2 additions & 1 deletion git-hooks/pre-commit → git-hooks/pre-commit-unix.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/sh
#!/bin/bash


######## KTLINT-GRADLE HOOK START ########

Expand Down
26 changes: 26 additions & 0 deletions git-hooks/pre-commit-windows.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!C:/Program\ Files/Git/usr/bin/sh.exe

######## KTLINT-GRADLE HOOK START ########

CHANGED_FILES="$(git --no-pager diff --name-status --no-color --cached | awk '$1 != "D" && $2 ~ /\.kts|\.kt/ { print $2}')"

if [ -z "$CHANGED_FILES" ]; then
echo "No Kotlin staged files."
exit 0
fi;

echo "Running ktlint over these files:"
echo "$CHANGED_FILES"

./gradlew --quiet formatKotlin -PinternalKtlintGitFilter="$CHANGED_FILES"

echo "Completed ktlint run."

echo "$CHANGED_FILES" | while read -r file; do
if [ -f $file ]; then
git add $file
fi
done

echo "Completed ktlint hook."
######## KTLINT-GRADLE HOOK END ########
3 changes: 2 additions & 1 deletion git-hooks/pre-push → git-hooks/pre-push-unix.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/sh
#!/bin/bash

echo "Running static analysis."

./gradlew lintKotlin
./gradlew detekt
echo "Running static analysis completed"
7 changes: 7 additions & 0 deletions git-hooks/pre-push-windows.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!C:/Program\ Files/Git/usr/bin/sh.exe

echo "Running static analysis."

./gradlew lintKotlin
./gradlew detekt
echo "Running static analysis completed"
27 changes: 0 additions & 27 deletions git-hooks/setup.sh

This file was deleted.

0 comments on commit 6884151

Please sign in to comment.