Forked from bartoszmajsak/prepare-commit-msg.sh
Last active
September 7, 2018 16:28
-
-
Save amessinger/95368e736e0119ca69329363e7f41dbf to your computer and use it in GitHub Desktop.
Automatically generate git commit message from the branch name (angular convention)
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/bash | |
# Assesses your branch naming follow Angular's commit message format (https://github.com/angular/angular.js/blob/master/DEVELOPERS.md#commits) | |
# Global Setup | |
# - create a `~/.git/hooks` folder | |
# - create the `prepare-commit-msg` hook file in this folder and paste this script | |
# - make git use this folder `git config --global core.hooksPath ~/.git/hooks` | |
# - allow for emtpy commit message `git config --global alias.commit "commit --allow-empty"` | |
# Usage | |
# run `git commit` when you want to create commit, edit the commit message at your liking and save | |
# This way you can customize which branches should be skipped when | |
# prepending commit message. | |
if [ -z "$BRANCHES_TO_SKIP" ]; then | |
BRANCHES_TO_SKIP=(master) | |
fi | |
BRANCH_NAME=$(git symbolic-ref --short HEAD) | |
COMMIT_MSG=$BRANCH_NAME | |
# replace first "/" with ": " | |
COMMIT_MSG=`echo ${COMMIT_MSG} | sed -e "s/\//: /"` | |
# replace "-" with " " | |
COMMIT_MSG=`echo ${COMMIT_MSG} | sed -e "s/-/ /g"` | |
BRANCH_EXCLUDED=$(printf "%s\n" "${BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH_NAME$") | |
if [ -n "$BRANCH_NAME" ] && ! [[ $BRANCH_EXCLUDED -eq 1 ]]; then | |
sed -i.bak -e "1s/^/$COMMIT_MSG/" $1 | |
fi |
Add to the doc : chmod +x ~/.git/hooks/prepare-commit-msg
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TODO: check that the branch name matches the format.