Skip to content

Instantly share code, notes, and snippets.

@FranAguiar
Last active August 27, 2018 07:40
Show Gist options
  • Save FranAguiar/e231a457287ad01ea20248fcc4f6f4b6 to your computer and use it in GitHub Desktop.
Save FranAguiar/e231a457287ad01ea20248fcc4f6f4b6 to your computer and use it in GitHub Desktop.
Format commit message
#!/bin/bash
#
# Add name branch as first line in commit body and flow and context in commit tittle
#
# If the branch has the format 'dev/flow/task' commit with the format 'git commit -m "context # commit title"'
# The commit will be:
#
# _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
#
# flow(context) commit title
#
# dev/flow/task
# -------------------
# Description if exists
# _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
#
# USAGE:
# Download to your project root
# mv commit-msg .git/hooks/
# chmod +x .git/hooks/commit-msg
BRANCH_NAME=$(git branch | sed -n -e 's/^* //gp')
FLOW=""
TITLE=""
BODY=""
COMMIT_MSG=""
LINE_NUMBER=0
NEWLINE=$'\n'
if [[ $BRANCH_NAME == *"/"*"/"* ]]; then
FLOW=$(echo "$BRANCH_NAME" | cut -f 2 -d "/")
fi
while IFS='' read -r LINE || [[ -n "$LINE" ]]; do
if [ "$LINE_NUMBER" -eq "0" ]; then
if [[ $LINE == *"#"* ]]; then
CONTEXT=$(echo "$LINE" | cut -f 1 -d "#" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
TITLE=$(echo "$LINE" | cut -f 2 -d "#" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
else
TITLE="$LINE"
fi
elif [[ "$LINE_NUMBER" -gt "1" ]]; then
BODY="$BODY$LINE$NEWLINE"
fi
let LINE_NUMBER++
done < "$1"
if [ ! -z "$FLOW" -a "$FLOW" != " " ]; then
COMMIT_MSG=""$FLOW"("$CONTEXT") "
echo
fi
COMMIT_MSG=""$COMMIT_MSG""$TITLE""
echo "$COMMIT_MSG" > $1
echo "" >> $1
echo "$BRANCH_NAME" >> $1
echo "-------------------" >> $1
echo "$BODY" >> $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment