Skip to content

Instantly share code, notes, and snippets.

@harrytran998
Last active January 24, 2021 10:02
Show Gist options
  • Save harrytran998/5fb1419525827601f2e7515505164d10 to your computer and use it in GitHub Desktop.
Save harrytran998/5fb1419525827601f2e7515505164d10 to your computer and use it in GitHub Desktop.
Githooks
Tạo 1 file là `prepare-commit-msg` ở githooks với nội dung sau
```bash
#!/bin/bash -e
textBase='\033[0m'
textInformation='\033[1;34m'
textSuccess='\033[1;32m'
branchName=$(git rev-parse --abbrev-ref HEAD)
entradeTicketRegex='^ENTRADE-[0-9]+|merge|hotfix'
gitCommitMsg=$(head -n 1 "$1") # $1 is path = .git/COMMIT_EDITMSG
if [[ $gitCommitMsg =~ $entradeTicketRegex ]];then
echo -e "$textInformation Already exist prefix branch-name, ignore appending prefix."
echo -e "$textBase"
else
echo -e "$textInformation Starting append prefix branch-name..."
if [[ -n "$branchName" ]] && [[ $branchName =~ $entradeTicketRegex ]]; then
# Append prefix branch name into commit message
sed -i.bak -e "1s~^~$branchName ~" "$1"
echo -e "$textSuccess Append prefix branch-name successfully."
echo -e "$textBase"
fi
fi
```
--> Run `make git-hook`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment