Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an equivalent to git's "describe" function for Mercurial?

Tags:

mercurial

I'm currently adding packaging to a something that is maintained in Mercurial. Currently the version is defined in the Makefile. I would like to change this so I can build daily packages or properly versioned packages.

Git provides a use "describe" function that can give you a description of the closest tagged build and current revision. For example if I run this in the kernel:

git describe HEAD

GIT returns:

v3.0-rc7-68-g51414d4

telling me that the revision is later than v3.0-rc7, with a git commitish of 51414d4

Is there something similar I can do in Mercurial?

like image 294
stsquad Avatar asked Jul 14 '11 12:07

stsquad


2 Answers

Maybe something like this?

hg log -r . --template '{latesttag}-{latesttagdistance}-{node|short}\n'

Of course you should make an alias for that with AliasExtension.

Note however, unlike "git describe", this command will always show the "latesttagdistance" and "node|short" parts, instead of omitting them when latesttagdistance is 0.

like image 54
ckruse Avatar answered Sep 21 '22 12:09

ckruse


This is a close emulation of git describe:

hg log -r . -T "{latesttag}{sub('^-0-.*', '', '-{latesttagdistance}-m{node|short}')}"

The {sub(...)} function ensures that a working copy that's exactly at tag v0.1.0 will show up as v0.1.0 and not v0.1.0-0-m123456789abc.

Note that the m before the hash is for mercurial, similar to the way git describe uses a g for git.

For convenience, create an alias by adding the following to your ~/.hgrc:

[alias]
describe = log -r . -T "{latesttag}{sub('^-0-.*', '', '-{latesttagdistance}-m{node|short}')}"

Then use the alias by simply typing hg describe.

If you'd like to emulate git describe --dirty, things get even messier – but you can still hide it all in an hg alias:

[alias]
describe = !
    dirtymark=;
    case " $1 " in " --dirty ") dirtymark=-dirty; ;; esac;
    echo $($HG log -r . --template "{latesttag}-{latesttagdistance}-m")$($HG id -i) |
        sed -r -e "s/\+\$/${dirtymark}/" -e 's/-0-m[[:xdigit:]]+//'

Now running hg describe --dirty will produce strings like:

  1. v0.1.0
  2. v0.1.0-dirty
  3. v0.1.0-1-mf6caaa650816
  4. v0.1.0-1-mf6caaa650816-dirty

Omitting the --dirty option means that you'll never get a -dirty suffix like (2) and (4), even when the working copy contains uncommitted changes.

like image 21
nocnokneo Avatar answered Sep 18 '22 12:09

nocnokneo