Skip to content

Instantly share code, notes, and snippets.

@hverr
Last active September 8, 2015 15:29
Show Gist options
  • Save hverr/4010fa7dfd200a4c04d6 to your computer and use it in GitHub Desktop.
Save hverr/4010fa7dfd200a4c04d6 to your computer and use it in GitHub Desktop.
Git hook to prepend the issue number to the commit message
#!/bin/sh
exec < /dev/tty
detect_issue() {
local branch="$(git branch | grep '^\*' | sed 's/^\* //')"
local issue=$(echo "$branch" | grep -o '[0-9][0-9]*' | head -n 1)
if [ -z "$issue" ]; then
return 1
fi
echo "$issue"
}
check_commit_msg() {
local issue=$(detect_issue)
if [ -z "$issue" ]; then
return 0
fi
local detected_issue=$(head -n 1 "$1" | \
grep -o '^[0-9][0-9]*:' | \
grep -o '^[0-9][0-9]*' )
if [ -n "$detected_issue" ]; then
if [ "$detected_issue" != "$issue" ]; then
echo "$detect_issue"
return 1
else
return 0
fi
fi
return 1
}
case "$2,$3" in
message,)
check_commit_msg "$1" > /dev/null
if [ $? -ne 0 ]; then
printf "No issue number at head of commit. Want to prepend? [Y/n] "
read choice
if [ -z "$choice" -o \
"$choice" = "Y" -o \
"$choice" = "y" -o \
"$choice" = "yes" ]; then
printf "$(detect_issue ): " | cat - "$1" > "$1.$$"
mv "$1.$$" "$1"
elif [ "$choice" = "n" -o \
"$choice" = "N" -o \
"$choice" = "no" ]; then
:
else
echo "Invalid choice. Aborting..." >&2
exit 1
fi
fi
;;
,)
issue=$(detect_issue)
if [ $? -eq 0 ]; then
printf "$issue: " | cat - "$1" >> "$1.$$"
mv "$1.$$" "$1"
fi
;;
esac
# SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment