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?
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}"'
This answer comes with a huge disclaimer:
- There might be better/different ways than this one, harnessing only PowerShell code, but I'm no PowerShell expert
- 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...
^--------^
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With