Skip to content

Instantly share code, notes, and snippets.

@luizbraga
Last active March 9, 2020 18:52
Show Gist options
  • Save luizbraga/b542e11e2b9138aad5618a0cf3eca5fc to your computer and use it in GitHub Desktop.
Save luizbraga/b542e11e2b9138aad5618a0cf3eca5fc to your computer and use it in GitHub Desktop.
Commit with branch name
# {{ BRANCH_NAME }}
# Extended commit message. (Optional)
# docs: (changes to documentation?)
# feat: (new feature?)
# fix: (bug fix?)
# refactor: (refactoring production code?)
# test: (adding missing tests, refactoring tests; no production code change):
# wip: (wip on feature branch?)
# -----------------------------------
# [author: {{ AUTHOR_NAME }} ({{ AUTHOR_EMAIL }})]
#!/bin/sh
#
# Automatically adds branch name and branch description to every commit message.
#
COMMIT_EDITMSG="$1"
BRANCH_NAME=$(git branch | grep '*' | sed 's/* //' | sed 's/\(feature\|release\)\///')
AUTHOR_NAME=$GIT_AUTHOR_NAME
AUTHOR_EMAIL=$GIT_AUTHOR_EMAIL
#
# Merge skip the branch placement behavior in commit message.
#
MERGE=$(cat $COMMIT_EDITMSG|grep -i 'merge'|wc -l)
if [ $2 = "message" ] ; then
sed -i.bak -e "1s/^/[$BRANCH_NAME] /" $1
elif [ $MERGE -eq 0 ] ; then
if [ -n "$BRANCH_NAME" ] && [ "$BRANCH_NAME" != "master" ]; then
COMMIT_EDITMSG=$(sed "s/{{ BRANCH_NAME }}/[${BRANCH_NAME}] /; s/{{ AUTHOR_NAME }}/${AUTHOR_NAME}/; s/{{ AUTHOR_EMAIL }}/${AUTHOR_EMAIL}/" "$COMMIT_EDITMSG")
else
COMMIT_EDITMSG=$(sed "s/{{ BRANCH_NAME }}//; s/{{ COMMIT_MSG }}/${COMMIT_MSG}/; s/{{ AUTHOR_NAME }}/${AUTHOR_NAME}/; s/{{ AUTHOR_EMAIL }}/${AUTHOR_EMAIL}/" "$COMMIT_EDITMSG")
fi
echo "${COMMIT_EDITMSG}" > "$1"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment