kurttheviking / git-rev-sync-js

Synchronously get the current git commit hash, tag, or branch
MIT License
171 stars 58 forks source link

How to get tag output like: 3.5.3-18-dirty ? #72

Open phatpaul opened 1 year ago

phatpaul commented 1 year ago

I need my version number to be compiled into my app. I want it to reflect the latest tag, the number of commits since that tag, and whether the working copy is dirty.

This is how it is typically done:

git describe --tags --always --dirty
3.5.3-8-geb51d63-dirty

(I don't really care if the hash is there or not, so it could also be 3.5.3-8-dirty. But I need to know if the app was built off the actual commit that the tag is pointing to, or a later commit, which the -8 tells me.).

But when I run:

node
Welcome to Node.js v14.17.3.
Type ".help" for more information.
> var git = require('git-rev-sync');
> console.log(git.tag(true));
3.5.3
undefined

This plugin is only returning the tag name without the expected suffix. I think the issue may be with the --abbrev=0. The git documentation says: "An of 0 will suppress long format, only showing the closest tag."

git version 2.38.1.windows.1

phatpaul commented 1 year ago

FYI, I've solved my own problem by removing this plugin and just using a simple 2-liner.

const execSync = require('child_process').execSync;
...
console.log( execSync('git describe --tags --always --dirty').toString('utf8').replace(/^\s+|\s+$/g, '') );