|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# git hook to call core-validate-commits when any commit (or amend) is done |
| 4 | + |
| 5 | +# Author: Steven R. Loomis <[email protected]> |
| 6 | + |
| 7 | +# Usage: commit-msg .git/COMMIT_EDITMSG |
| 8 | +# ( called by git ) |
| 9 | + |
| 10 | +# 0. install node |
| 11 | +# 1. npm i -g core-validate-commits |
| 12 | +# 2. ln -s `pwd`/commit-msg.sh ~/src/node/.git/hooks/commit-msg |
| 13 | +# ( You may be able to do |
| 14 | +# ln -s /usr/local/lib/node_modules/core-validate-commit/commit-msg.sh \ |
| 15 | +# ~/src/node/.git/hooks/commit-msg |
| 16 | +# to pull from the npm installed version) |
| 17 | +# 3. make sure "commit-msg" is executable. |
| 18 | + |
| 19 | +#Debug: |
| 20 | +#pwd |
| 21 | +#echo COMMIT-MSG $* |
| 22 | +#env |
| 23 | + |
| 24 | +# We'll use this as a temporary file. |
| 25 | +# TODO: clean up after ourselves. |
| 26 | +TMPF=$(mktemp) |
| 27 | + |
| 28 | +#Debug: |
| 29 | +#echo Temporary file: ${TMPF} |
| 30 | + |
| 31 | + |
| 32 | +# We need to 'fix up' the commit message to emulate 'git show' |
| 33 | +# 1. header information |
| 34 | +# 2. 4 space indent |
| 35 | +# 3. strip comments |
| 36 | + |
| 37 | +# 1. add header |
| 38 | +cat >"${TMPF}" <<EOF |
| 39 | +commit 0 |
| 40 | +Author: ${GIT_AUTHOR_NAME} <${GIT_AUTHOR_EMAIL}> |
| 41 | +Date: Jan 1 00:00:00 1970 -0000 |
| 42 | +
|
| 43 | +EOF |
| 44 | + |
| 45 | +# 2/3. indent 4 spaces and remove comment lines. |
| 46 | +grep -v '^#' < "${1}" | sed -e 's%^.*$% &%' >> "${TMPF}" |
| 47 | + |
| 48 | +# by default, don't validate metadata |
| 49 | +OPTS="--no-validate-metadata" |
| 50 | + |
| 51 | +# Check which branch we're on |
| 52 | +BRANCH=$(git symbolic-ref HEAD) |
| 53 | + |
| 54 | +if [[ "${BRANCH}" == "refs/heads/master" ]]; |
| 55 | +then |
| 56 | + # we're on master - so be more careful |
| 57 | + # do validate metadata |
| 58 | + OPTS="" |
| 59 | +fi |
| 60 | + |
| 61 | + |
| 62 | +# Now, run! |
| 63 | + |
| 64 | +#Debug: |
| 65 | +#set -x |
| 66 | + |
| 67 | +core-validate-commit ${OPTS} "file://${TMPF}" || |
| 68 | + ( echo "Please fix the above and try again. Your message is in ${1}" ; exit 1) |
| 69 | +# TODO: rm "${TMPF}" - or perhaps trap? |
| 70 | + |
0 commit comments