Sebastian Sebald
Sebastian Sebald

Reputation: 16856

How to write Git hash to (node's) package.json?

is there a way to automatically update version number of a package.json (https://npmjs.org/doc/json.html) with the HEAD hash of git? I would like to have something like version: 1.0.0+rev82e4b91cfe42cd86e9453b4987b9cc446566de6 inside the project's package.json file. Eveything before the plus is set manually and the hash is updated everytime I commit something.

Is this possible? I couldn't find anything on this topic :-/

Upvotes: 14

Views: 8719

Answers (4)

eflat
eflat

Reputation: 1019

It appears that this can be done directly without a plugin. npm version $(git describe) will take the value from git describe and update the value of the version in package.json.

(I'm currently using npm version 3.10.)

Upvotes: 5

Matt Styles
Matt Styles

Reputation: 2452

git rev-parse HEAD will write out the last commit to the current head branch which you could then append to a version number in your package whenever you run your build.

There are a number of git rev-xxx commands that might be useful for any additional stuff you might want to record.

Upvotes: 2

Mikael Karon
Mikael Karon

Reputation: 623

Part of that can be solved with git-describe and there's actually a grunt plug-in that wraps that for you (https://github.com/mikaelkaron/grunt-git-describe/).

The second part you'd have to do manually (right now), but I actually have a similar problem at the moment, so I can try to hack up a grunt plug-in for you (and myself).

Full disclosure, I'm the author of grunt-git-describe above.

Upvotes: 1

Brandon
Brandon

Reputation: 10038

Automatically at what point? Possibilities are:

  1. Update the version field every time you run a build from a machine environment
  2. Update the version field every time you run any build.
  3. Update the version field in a git hook to keep in sync. Personally, I'd be nervous about a hook that performs a change when something changes. This strategy is highest risk, highest reward.

If you go with the first or second approach, it depends of course on what build tool you use. If you use grunt, see about a grunt plugin. I can't find any grunt plugins that do what you're asking for, but you can create one fairly easily.

http://gruntjs.com/plugins

If you do end up creating one, let me know as I am also in need of a similar process :)

In my case, I am using SVN, but want the same pattern. I want to put the SVN revision number as my build number.

My recommendation is to leave the build number blank in the file that is checked in and have your build environment do a git clone for a new build and update the build number. Then built packages always have something about them indicating the git commit they came from.

I think that for development, you don't really need it because you can always ask git which version you have checked out. There is a grunt-git plugin which you could maybe use to figure out the version in dev. (maybe git show?)

Upvotes: -2

Related Questions