Skip to content

Instantly share code, notes, and snippets.

@dsheiko
Last active June 27, 2024 08:17
Show Gist options
  • Save dsheiko/620c7b0dcfbda39ce959eab70c5b2077 to your computer and use it in GitHub Desktop.
Save dsheiko/620c7b0dcfbda39ce959eab70c5b2077 to your computer and use it in GitHub Desktop.
Git-hook to create a tag automatically based on lately committed package.json version
#! /bin/bash
version=`git diff HEAD^..HEAD -- "$(git rev-parse --show-toplevel)"/package.json | grep -m 1 '^\+.*version' | sed -s 's/[^A-Z0-9\.\-]//g'`
if [[ ! $version =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)(\-[A-Z]+\.[0-9]+)?$ ]]; then
echo -e "Skip tag: invalid version '$version'"
exit 1
fi
git tag -a "v$version" -m "`git log -1 --format=%s`"
echo "Created a new tag, v$version"
@dsheiko
Copy link
Author

dsheiko commented Jul 1, 2020

Just copy it into .git/hooks/

@acurrieclark
Copy link

This is incredibly useful. Thank you!

One note for anyone on OS X, you will need to remove the -s flag from the sed call on line 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment