Skip to content

Instantly share code, notes, and snippets.

@yeliu84
Last active August 29, 2015 14:09
Show Gist options
  • Save yeliu84/0380afaaada554488054 to your computer and use it in GitHub Desktop.
Save yeliu84/0380afaaada554488054 to your computer and use it in GitHub Desktop.
Prepend issue number to commit message.
#!/bin/bash
#
# Author: Ye Liu
# URL: https://gist.github.com/jaux/0380afaaada554488054
# License: MIT
TICKET_FILE="$(pwd)/.last-ticket"
line=$(head -n 1 "$1")
# test if message has [TICKET#] already
echo $line | grep -q '^\s*\[.\+\].*'
if [ $? -eq 0 ]; then
exit 0
fi
# get last ticket number
last_ticket=
if [ -f "$TICKET_FILE" ]; then
last_ticket=$(head -n 1 "$TICKET_FILE")
echo "$last_ticket" | grep -q '\d\+'
if [ $? -ne 0 ]; then
last_ticket=
fi
fi
exec < /dev/tty
# prompt to enter ticket number
echo -n 'Ticket number (q to skip)'
if [ -z "$last_ticket" ]; then
echo -n ': '
else
echo -n " [$last_ticket]: "
fi
read ticket
if [ -z "$ticket" ]; then
ticket="$last_ticket"
elif [ ${#ticket} -eq 1 -a $(echo "$ticket" | tr '[:upper:]' '[:lower:]') == 'q' ]; then
exit 0
fi
echo "$ticket" > "$TICKET_FILE"
# confirm commit message
msg="[$ticket] $line"
echo "$msg"
while true; do
read -p 'Look good? [Y/n/e] ' confirm
if [ -z "$confirm" ]; then
confirm='Y'
else
confirm=$(echo "${confirm:0:1}" | tr '[:lower:]' '[:upper:]')
fi
case "$confirm" in
Y)
# write commit message file
echo "$msg" > "$1"
exit 0
;;
E)
editor="$EDITOR"
if [ -z "$editor" ]; then
editor=$(which vim)
if [ -z "$editor" ]; then
editor=$(which vi)
if [ -z "$editor" ]; then
echo 'No editor, abort commit'
exit 1
fi
fi
fi
# write commit message file
echo "$msg" > "$1"
# edit commit message file
$editor $1
if [ $? -ne 0 ]; then
echo 'abort commit'
exit 1
fi
exit 0
;;
N)
# abort commit
echo 'abort commit'
exit 1
;;
*)
;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment