I've found a way to generate a semver-compliant version string from an appropriately-tagged Mercurial repository using hg log
with a template. It looks like this:
hg log -r . --template "{latesttag('re:^[vV]\d+\.\d+\.\d+') % '{node}|{tag}.{distance}|{tag}{ifeq(distance, '1', '', '-build{distance}')}{ifeq(branch, 'default', '', '+{branch}')}'}"
This returns three things, the revision ID of the commit matching the tag pattern, the .NET System.Version
-compliant version string, and a semver 2.0-compliant version string using the tag distance as prerelease information if greater than 1 (i.e. more actual commits after the tag revision), and using the branch name as build metadata if it is any value other than "default" (e.g. "hotfix-JIRA-123").
I don't mind not being able to get this all formatted in one hg log
command, as long as I can get all the values to format it afterwards. This is just gravy on Mercurial's part.
However the only way I can see to do the same in git would be to use multiple commands, like so:
REM Returns the tag, tag distance, and node ID "abbreviated" to 40 chars (i.e. not abbreviated, LOLZ)
git describe --tags --long --match "v*.*.*" --abbrev=40
REM Returns the branch name
git rev-parse --abbrev-ref HEAD
If git describe
could additionally return the branch name in any way, that would solve my problem, but I can't actually see any one command in git that will return all these values, having already filtered to a tagged revision like these commands do.
I can carry on as I am, I'm just wondering if there's something I'm missing.
%
formatting directives ingit log
, but there is no formatting directive that does whatgit describe
does, andgit describe
doesn't do formatting directives. – Vanegas