Git equivalent for "hg log" with a template?
Asked Answered
C

0

6

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.

Cocke answered 22/2, 2019 at 9:24 Comment(2)
You're not missing anything here. The closest Git comes to the flexibility of Mercurial log templates is Git's % formatting directives in git log, but there is no formatting directive that does what git describe does, and git describe doesn't do formatting directives.Vanegas
Maybe asking a separate question about methods to generate semver #s from git would be useful... a quick Google does seem to turn up some approaches. But I'm not a git guy...Officiate

© 2022 - 2024 — McMap. All rights reserved.