Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the latest Mercurial tag from within Powershell

If i run the following command from a DOS prompt:

hg parents --template {latesttag}

then I get the latest tag value returned as expected. However if I run the same command from within a powershell console I get the following error:

hg parents: option -i not recognized

I need the command to run in powershell so I can get it's results as a variable to use. Any idea what I need to do to get the command running?

like image 693
Danny Jones Avatar asked May 30 '11 21:05

Danny Jones


2 Answers

You should just need to surround the argument to --template in quotes so that Powershell knows it's a string:

hg parents --template '{latesttag}'

Sometimes, however, with the way Powershell parses things you have to make doubly sure that double-quotes survive (such as passing an argument that contains spaces but should be 1 argument instead of many, like paths or a longer template):

hg parents --template '"{latesttag}"'
like image 179
Joel B Fant Avatar answered Oct 15 '22 08:10

Joel B Fant


This answer comes with a huge disclaimer:

  1. There might be better/different ways than this one, harnessing only PowerShell code, but I'm no PowerShell expert
  2. I am the author of the following class library

I have made a .NET class library, Mercurial.Net that allows a .NET program to wrap around the hg command line client without having to deal with all the intricacies of doing all of that, waiting for the program to complete, etc. Note: You will need Mercurial installed, and available through the PATH for the class library to work.

It can be installed through Nuget package.

Since I'm no PowerShell expert, I'll show how to do this with C# code and my library.

Considering that the library does not implement all options (yet), sadly the parents command cannot be used by my library, but the log command can, although it will probably have more overhead. You will have to be the judge of whether this is acceptable.

The reason the parents command cannot be used is that I have, for now, decided not to implement support for the templating syntax in Mercurial, because then I know what kind of output to expect and can parse that back into proper .NET instances.

Here's the .NET code that would find the tipmost tag:

var repo = new Repository(".");
var tags = repo.Log(new LogCommand().WithRevision("tag()")).First().Tags.ToArray();

This will populate tags with an array of tag-names. The log command returns the log in reverse order, newest changesets first, so .First() will find the newest changeset.

If you need the closest tag that is also an ancestor of the current working folder, here's the changed code:

repo.Log(new LogCommand().WithRevision("tag() and ancestors(.)")).First().Tags...
                                             ^---------------^

This includes the revision of the working folder if that has a tag. If you need the ancestor, not including the revision of the working folder:

repo.Log(new LogCommand().WithRevision("tag() and ancestors(.) and not .")).First().Tags...
                                                              ^--------^
like image 44
Lasse V. Karlsen Avatar answered Oct 15 '22 09:10

Lasse V. Karlsen