Skip to content

Commit

Permalink
package: add git hook instructions
Browse files Browse the repository at this point in the history
Add instructions and a hook to call core-validate-commit from
git commits.

Depends on nodejs#11

Known issues:
- leaves ${TMPF} around (temporary file)
- probably will reject `fixup:` type commit logs
- may be too strict if someone is just doing a simple
'work in progress' kind of local branch

Larger improvement:
- fold the entire hook script into a mode, and/or an alternate
script entrypoint into core-validate-commit. This would drop
requirements on sh, sed, grep…

Fixes: nodejs#12
  • Loading branch information
srl295 committed Oct 27, 2016
1 parent 0e79acf commit ba705ad
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ $ core-validate-commit --list
title-length enforce max length of commit title
```

## Git hook installation

- install `node` and `core-validate-commits` as above
- install this hook in your clone of node (here `~/src/node`):
```
ln -s commit-msg.sh ~/src/node/.git/hooks/commit-msg
```
- Alternatively, you may be able to pull from the npm-installed package:
```
ln -s /usr/local/lib/node_modules/core-validate-commit/commit-msg.sh \
~/src/node/.git/hooks/commit-msg
```
- make sure `~/src/node/.git/hooks/commit-msg` is executable.

## Test

```bash
Expand Down
70 changes: 70 additions & 0 deletions commit-msg.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/bin/sh

# git hook to call core-validate-commits when any commit (or amend) is done

# Author: Steven R. Loomis <srloomis@us.ibm.com>

# Usage: commit-msg .git/COMMIT_EDITMSG
# ( called by git )

# 0. install node
# 1. npm i -g core-validate-commits
# 2. ln -s `pwd`/commit-msg.sh ~/src/node/.git/hooks/commit-msg
# ( You may be able to do
# ln -s /usr/local/lib/node_modules/core-validate-commit/commit-msg.sh \
# ~/src/node/.git/hooks/commit-msg
# to pull from the npm installed version)
# 3. make sure "commit-msg" is executable.

#Debug:
#pwd
#echo COMMIT-MSG $*
#env

# We'll use this as a temporary file.
# TODO: clean up after ourselves.
TMPF=$(mktemp)

#Debug:
#echo Temporary file: ${TMPF}


# We need to 'fix up' the commit message to emulate 'git show'
# 1. header information
# 2. 4 space indent
# 3. strip comments

# 1. add header
cat >"${TMPF}" <<EOF
commit 0
Author: ${GIT_AUTHOR_NAME} <${GIT_AUTHOR_EMAIL}>
Date: Jan 1 00:00:00 1970 -0000
EOF

# 2/3. indent 4 spaces and remove comment lines.
grep -v '^#' < "${1}" | sed -e 's%^.*$% &%' >> "${TMPF}"

# by default, don't validate metadata
OPTS="--no-validate-metadata"

# Check which branch we're on
BRANCH=$(git symbolic-ref HEAD)

if [[ "${BRANCH}" == "refs/heads/master" ]];
then
# we're on master - so be more careful
# do validate metadata
OPTS=""
fi


# Now, run!

#Debug:
#set -x

core-validate-commit ${OPTS} "file://${TMPF}" ||
( echo "Please fix the above and try again. Your message is in ${1}" ; exit 1)
# TODO: rm "${TMPF}" - or perhaps trap?

0 comments on commit ba705ad

Please sign in to comment.