forked from guilhem/rss-issues-action
-
Notifications
You must be signed in to change notification settings - Fork 6
/
pre-push.hook
executable file
·62 lines (51 loc) · 2.13 KB
/
pre-push.hook
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/bin/sh
# This pre-push hook ensures that we only push branches with up to date files in `dist/`.
#
# While at it, it also ensures that it is itself up to date with `pre-push.hook`
die () {
echo "$*" >&2
exit 1
}
LF='
'
git diff --no-index --quiet pre-push.hook "$(git rev-parse --git-path hooks/pre-push)" ||
die 'The `pre-push` hook is not up to date with `pre-push.hook`. Please update:
cp pre-push.hook .git/hooks/pre-push'
# Verify that any tagged version is reflected in its `package.json`
for tag in $(git for-each-ref --format='%(refname:short)' --points-at=HEAD 'refs/tags/v[0-9]*')
do
out="$(git tag --verify $tag 2>&1)" ||
die "$out$LF${LF}Tag $tag is not signed/signature cannot be verified"
test 0 = $(git rev-list --count ...main) || die 'HEAD is not up to date with `main`'
test 0 = $(git rev-list --count ...origin/main) ||
echo 'warning: HEAD is not up to date with `origin/main`' >&2
regex="^ \"version\": \"$(echo "${tag#v}" | sed 's/\./\\./g')\",\$"
substitute="/\"name\": \"rss-to-issues\"/{N;s/\\(\"version\": \"\\).*\",\$/\\1${tag#v}\",/}"
git grep -q "$regex" refs/tags/$tag -- package.json || {
sed "$substitute" <package.json >package.json.new &&
mv -f package.json.new package.json
sed "$substitute" <package-lock.json >package-lock.json.new &&
mv -f package-lock.json.new package-lock.json
die "package.json did not reflect $tag; It was adjusted."
exit 1
}
git grep -q "$regex" refs/tags/$tag -- package-lock.json || {
sed "$substitute" <package-lock.json >package-lock.json.new &&
mv -f package-lock.json.new package-lock.json
die "package-lock.json did not reflect $tag; It was adjusted."
exit 1
}
done
git diff --quiet dist/ ||
die '`dist/` is dirty'
base="$(git rev-list HEAD -1 -- dist/)"
if test 0 -lt $(git rev-list --count ${base+$base..}HEAD -- \*.js)
then
echo "Verifying that dist/ is up to date" >&2
npm run prepare &&
if ! git diff --quiet dist/
then
echo "Committing dist/ because it was not up to date" >&2
git commit -sm "npm run prepare" dist/
fi
fi