Skip to content

Commit 125b31b

Browse files
committed
Add a pre-push hook
For security reasons, this hook needs to be installed manually (after inspecting it!!!) to `.git/hooks/pre-push`. The hook assists developers by verifying that `dist/` is up to date and that the version number in `package.json` is set correctly when pushing a tag. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent a6c2c1e commit 125b31b

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

pre-push.hook

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/bin/sh
2+
3+
# This pre-push hook ensures that we only push branches with up to date files in `dist/`.
4+
#
5+
# While at it, it also ensures that it is itself up to date with `pre-push.hook`
6+
7+
die () {
8+
echo "$*" >&2
9+
exit 1
10+
}
11+
12+
LF='
13+
'
14+
15+
git diff --no-index --quiet pre-push.hook "$(git rev-parse --git-path hooks/pre-push)" ||
16+
die 'The `pre-push` hook is not up to date with `pre-push.hook`'
17+
18+
# Verify that any tagged version is reflected in its `package.json`
19+
for tag in $(git for-each-ref --format='%(refname:short)' --points-at=HEAD 'refs/tags/v[0-9]*')
20+
do
21+
out="$(git tag --verify $tag 2>&1)" ||
22+
die "$out$LF${LF}Tag $tag is not signed/signature cannot be verified"
23+
24+
git grep -q '"version": "'"${tag#v}"'"' refs/tags/$tag -- package.json || {
25+
sed 's/\("version": "\)[^"]*/\1'"${tag#v}"/ <package.json >package.json.new &&
26+
mv -f package.json.new package.json
27+
die "package.json did not reflect $tag; It was adjusted."
28+
exit 1
29+
}
30+
done
31+
32+
git diff --quiet dist/ ||
33+
die '`dist/` is dirty'
34+
35+
base="$(git rev-list HEAD -1 -- dist/)"
36+
if test 0 -lt $(git rev-list --count ${base+$base..}HEAD -- \*.ts)
37+
then
38+
echo "Verifying that dist/ is up to date" >&2
39+
npm run prepare &&
40+
if ! git diff --quiet dist/
41+
then
42+
echo "Committing dist/ because it was not up to date" >&2
43+
git commit -sm "npm run prepare" dist/
44+
fi
45+
fi

0 commit comments

Comments
 (0)