Last Updated: July 27, 2018
·
35.43K
· warby

Automatic git version tagging for NPM modules

When creating git tags for NPM modules, I almost always forget to update the version in the module's package.json. For this reason, I created a git post-commit hook which automatically creates a tag whenever the version is updated in package.json:

File: .git/hooks/post-commit
#! /bin/bash
version=`git diff HEAD^..HEAD -- "$(git rev-parse --show-toplevel)"/package.json | grep '^\+.*version' | sed -s 's/[^0-9\.]//g'`

if [ "$version" != "" ]; then
    git tag -a "v$version" -m "`git log -1 --format=%s`"
    echo "Created a new tag, v$version"
fi

So how does it work?

Firstly, we get the diff of the commit we just made, for the package.json file. Then, we grep the diff to see if the version tag has changed. If it has, we pipe through to sed, and strip everything that isn't a number or a dot, leaving us with something along the lines of 0.0.1.

Next, if we have a new version, we create the tag, using the commit message as the tag message, and then print out a little informative statement.

So now, if we change the version in package.json:

{
    ...
    "version": "0.1.0"
    ...
}

and commit:

[person@acomputer ~/project] $ git commit package.json -m 'Updated version'

we get:

Created a new tag, v0.1.0
[master 13138dd] Updated version
 1 file changed, 1 insertion(+), 1 deletion(-)

We can verify that our tag has been created by running:

[person@acomputer ~/project] $ git show v0.1.0
tag v0.1.0
Tagger: A Person <aperson@example.com>
Date:   Fri Jan 24 12:17:22 2014 +0800

Updated version

commit b0520a474bba259dd5e6b5542f3622b1eabebfad
Author: A Person <aperson@example.com>
Date:   Fri Jan 24 12:17:22 2014 +0800

    Updated version

diff --git a/package.json b/package.json
index 5fd5e5b..17b7223 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
     ...
-    "version": "0.0.9",
+    "version": "0.1.0",
     ...

All that is left to do now is:

git push && git push --tags

This could easily be adapted for other package managers as well - for instance Bower could be supported simply by changing package.json in the hook to bower.json.

4 Responses
Add your response

Check out my pro tip on using a Makefile to combine these steps into one command:

https://coderwall.com/p/dr4uvw

We use it for all our projects here at our company :)

over 1 year ago ·

Wait isn't this what npm version does?
E.g.

npm version patch -m "Fixed a bug in X"
  • Bumps the version in package.json (the patch part)
  • Creates a commit with specified message
  • Tags that commit with the new version
over 1 year ago ·

Sounds like it does, I wasn't aware of this! But I think I'd still prefer to specify the version, rather than rely on an auto-increment - personal preference :)

over 1 year ago ·

@james-duncan you can explicitly specify a version as long as it is in semver format (which is encouraged to use for NPM packages).

npm version 0.1.5 -m "Fixed a bug in X"

Doc: npm-version

over 1 year ago ·