Skip to content

Commit

Permalink
pre-push: automagically update the version
Browse files Browse the repository at this point in the history
Apart from keeping the `.js` and `npm prepare` parts and adjusting the
expected package name in `package*.json` to `rss-to-issues`, this is
pretty much a verbatim copy-pasta of the `pre-push.hook` from
https://github.com/git-for-windows/get-azure-pipelines-artifact/.

Signed-off-by: Johannes Schindelin <[email protected]>
  • Loading branch information
dscho committed Apr 9, 2024
1 parent f9a23e8 commit c47b32f
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions pre-push.hook
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,37 @@ 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`'
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"

git grep -q '"version": "'"${tag#v}"'"' refs/tags/$tag -- package.json || {
sed 's/\("version": "\)[^"]*/\1'"${tag#v}"/ <package.json >package.json.new &&
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/ ||
Expand All @@ -42,4 +59,4 @@ then
echo "Committing dist/ because it was not up to date" >&2
git commit -sm "npm run prepare" dist/
fi
fi
fi

0 comments on commit c47b32f

Please sign in to comment.