Original info:
https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks
My goal:
Validate commit message and deny commit if a comment didn't contain required tag
Step 1
Create copy of file from ~project folder~/.git/hooks/
File name - commit-msg.sample
Step 2
Rename copy to "commit-msg"
Step 3
Perform command: chmod +x commit-msg
Step 4
Edit file:
Now try to perform commit ;)
Example of second validation message:

Additional info:
If you want to share your hooks with all members of project - perform next steps:
Step 1
Create folder 'git-hooks' in root project directory
Step 2
Store here created files with your hooks
Step 3
In android gradle file (in my case - build.gradle for app.module ) add next:
Step 4
Now rebuild your project. All your hooks will be copied to .git/hooks folder
WARNING:
If you want to remove hooks - don't forget to remove it manually from folder .git/hooks/
Android Studio in my post case use only hooks from .git/hooks/ folder
https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks
My goal:
Validate commit message and deny commit if a comment didn't contain required tag
Step 1
Create copy of file from ~project folder~/.git/hooks/
File name - commit-msg.sample
Step 2
Rename copy to "commit-msg"
Step 3
Perform command: chmod +x commit-msg
Step 4
Edit file:
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
#!/bin/sh | |
#Step 0: Check if merge/revert | |
CONTINUE_CHECK=true | |
COMMIT_MSG=$1 | |
START_LINE=`head -n1 $COMMIT_MSG` | |
PATTERN_MERGE_REVERT="^Merge|^Revert" | |
if [[ "$START_LINE" =~ $PATTERN_MERGE_REVERT ]]; then | |
CONTINUE_CHECK=false | |
fi | |
if $CONTINUE_CHECK; then | |
#Step 1: Check if commit text contain [TAG] | |
PATTERN_TAG="^\[([[:upper:]]+)]" | |
if ! [[ "$START_LINE" =~ $PATTERN_TAG ]]; then | |
echo "Bad commit message, see example: <br>[TAG] commit message" | |
exit 1 | |
fi | |
#Step 2: Check if [TAG] is in list of allowed tags | |
allowed_tags_array=(HF BF NF CR MC OPT REF) | |
if [[ ! "${allowed_tags_array[@]}" =~ "${BASH_REMATCH[1]}" ]]; then | |
echo "Unsupported tag. Please use one of proposed:<br>" | |
echo "HF - Hot Fix <br>" | |
echo "BF - Bug Fix <br>" | |
echo "NF - New Feature <br>" | |
echo "CR - Change Request <br>" | |
echo "MC - Merge Conflicts fix <br>" | |
echo "OPT - Optimization/Performance <br>" | |
echo "REF - Refactoring<br>" | |
exit 1 | |
fi | |
fi |
Now try to perform commit ;)
Example of second validation message:

Additional info:
If you want to share your hooks with all members of project - perform next steps:
Step 1
Create folder 'git-hooks' in root project directory
Step 2
Store here created files with your hooks
Step 3
In android gradle file (in my case - build.gradle for app.module ) add next:
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
apply plugin: 'com.android.application' | |
android { | |
... | |
preBuild.doLast{ | |
//Copy project githooks to .git/hooks | |
copy { | |
from "../git-hooks" | |
into "../.git/hooks" | |
} | |
FileTree tree = fileTree('../.git/hooks') | |
tree.each { | |
Runtime.getRuntime().exec("chmod +x ../.git/hooks/${it.name}") | |
} | |
} | |
} |
Step 4
Now rebuild your project. All your hooks will be copied to .git/hooks folder
WARNING:
If you want to remove hooks - don't forget to remove it manually from folder .git/hooks/
Android Studio in my post case use only hooks from .git/hooks/ folder