-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add conventional-pre-commit hook
- Loading branch information
Atiqur Rahman
committed
Apr 11, 2022
1 parent
e3a2150
commit 8f0c719
Showing
3 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#!/usr/bin/env bash | ||
|
||
# list of Conventional Commits types | ||
cc_types=("feat" "fix") | ||
default_types=("build" "chore" "ci" "docs" "${cc_types[@]}" "perf" "refactor" "revert" "style" "test") | ||
types=( "${cc_types[@]}" ) | ||
|
||
if [ $# -eq 1 ]; then | ||
types=( "${default_types[@]}" ) | ||
else | ||
# assume all args but the last are types | ||
while [ $# -gt 1 ]; do | ||
types+=( "$1" ) | ||
shift | ||
done | ||
fi | ||
|
||
# the commit message file is the last remaining arg | ||
msg_file="$1" | ||
|
||
commit_msg_type_regex='feat|fix|refactor|style|test|docs|build' | ||
commit_msg_scope_regex='.{1,20}' | ||
commit_msg_subject_regex='.{1,100}' | ||
commit_msg_regex="^(${commit_msg_type_regex})(\(${commit_msg_scope_regex}\))?: (${commit_msg_subject_regex})\$" | ||
merge_msg_regex="^Merge branch '.+'\$" | ||
|
||
commit_msg_header=$(git show -s --format=%s $msg_file) | ||
|
||
# Check if commit is conventional commit | ||
if ! [[ "$commit_msg_header" =~ (${commit_msg_regex})|(${merge_msg_regex}) ]]; then | ||
exit 0 | ||
fi | ||
|
||
echo "[Commit message] $( cat "$msg_file" )" | ||
echo " | ||
Your commit message does not follow Conventional Commits formatting | ||
https://www.conventionalcommits.org/ | ||
Conventional Commits start with one of the below types, followed by a colon, | ||
followed by the commit message: | ||
$(IFS=' '; echo "${types[*]}") | ||
Example commit message adding a feature: | ||
feat: implement new API | ||
Example commit message fixing an issue: | ||
fix: remove infinite loop | ||
Optionally, include a scope in parentheses after the type for more context: | ||
fix(account): remove infinite loop | ||
" | ||
exit 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
#!/bin/bash | ||
echo "Running detekt check" | ||
./gradlew detekt | ||
./gradlew detekt |