Skip to content

Instantly share code, notes, and snippets.

@florianpasteur
Last active September 27, 2022 09:10
Show Gist options
  • Save florianpasteur/f1cc272815f08d133ead0ddd2c21e421 to your computer and use it in GitHub Desktop.
Save florianpasteur/f1cc272815f08d133ead0ddd2c21e421 to your computer and use it in GitHub Desktop.
Post commit hook to increment version of JSON file
#!/bin/bash
# Install command:
# curl https://gist.githubusercontent.com/florianpasteur/f1cc272815f08d133ead0ddd2c21e421/raw -o .git/hooks/post-commit && chmod +x .git/hooks/post-commit
IS_AMEND=$(ps -ocommand= -p $PPID | grep -e '--amend');
if [ -n "$IS_AMEND" ]; then
exit 0;
fi
FULL_VERSION=$(jq -r '.version' manifest.json)
IFS='.' read -ra VER <<< $FULL_VERSION
edit_json() {
file=$1
shift
jq "$*" "${file}" >/tmp/jq-tmp.json && cat /tmp/jq-tmp.json >"${file}"
}
get_new_version() {
VER_INCREMENTED=$(($3+1))
echo $1"."$2"."$VER_INCREMENTED
}
NEW_VER=$(get_new_version $VER)
if git diff manifest.json; then
git stash
edit_json manifest.json '.version="'$NEW_VER'"'
git add manifest.json
git commit --amend --no-edit --no-verify
git stash pop
else
edit_json manifest.json '.version="'$NEW_VER'"'
git add manifest.json
git commit --amend --no-edit --no-verify
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment