Skip to content

Commit ba705ad

Browse files
committed
package: add git hook instructions
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
1 parent 0e79acf commit ba705ad

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ $ core-validate-commit --list
3131
title-length enforce max length of commit title
3232
```
3333

34+
## Git hook installation
35+
36+
- install `node` and `core-validate-commits` as above
37+
- install this hook in your clone of node (here `~/src/node`):
38+
```
39+
ln -s commit-msg.sh ~/src/node/.git/hooks/commit-msg
40+
```
41+
- Alternatively, you may be able to pull from the npm-installed package:
42+
```
43+
ln -s /usr/local/lib/node_modules/core-validate-commit/commit-msg.sh \
44+
~/src/node/.git/hooks/commit-msg
45+
```
46+
- make sure `~/src/node/.git/hooks/commit-msg` is executable.
47+
3448
## Test
3549

3650
```bash

commit-msg.sh

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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

Comments
 (0)