Skip to content

Instantly share code, notes, and snippets.

@ae6rt
Created December 9, 2016 21:02
Show Gist options
  • Save ae6rt/41abd3f8fdab2f940a7d266b3f40198a to your computer and use it in GitHub Desktop.
Save ae6rt/41abd3f8fdab2f940a7d266b3f40198a to your computer and use it in GitHub Desktop.
Git prepare-commit hook to put branch name in commit message
#!/bin/sh
readonly TEMP_FILE=$(mktemp /tmp/commit-message-XXXX)
trap "rm -f ${TEMP_FILE}" EXIT
echo $(git rev-parse --abbrev-ref HEAD) > ${TEMP_FILE}
cat $1 >> ${TEMP_FILE}
mv ${TEMP_FILE} $1
@ae6rt
Copy link
Author

ae6rt commented Dec 3, 2020

#!/bin/sh

# Reference: https://git-scm.com/docs/githooks#_prepare_commit_msg

set -u

trap 'rm -f ${MESSAGE_FILE}' EXIT

readonly MESSAGE_SOURCE=${2:-none}
readonly MESSAGE_FILE=$(mktemp /tmp/commit-message-XXXX)
readonly ISSUE=$(git rev-parse --abbrev-ref HEAD 2>/dev/null | sed -e 's#.*/##g')

case ${MESSAGE_SOURCE} in
    commit|merge|squash|template)
        exit 0
        ;;
    message)
        echo "[${ISSUE}] $(head -1 $1)" > ${MESSAGE_FILE}
        tail -n +2 $1 >> ${MESSAGE_FILE}
        ;;
    none)
        #echo "[${ISSUE}] <you must have a one-line banner-type message here>" > ${MESSAGE_FILE}
        echo "[${ISSUE}] " > ${MESSAGE_FILE}
        cat $1 >> ${MESSAGE_FILE}
        ;;
esac

cat ${MESSAGE_FILE} > $1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment