How to get the tag changeset after you clone or pull to a tag using mercurial?

橙三吉。 提交于 2019-12-05 05:31:04

You want a giant hack with bash and an embedded Perl script? Well, here it is...

#!/bin/bash
if [[ "$1" == "" || "$2" == "" || "$3" == "" ]]; then
  echo 'hgclonetag <src> <tgt> <tag>'
  exit 1;
fi

REV=`hg log -R $1 --rev $3: --limit=2 | perl -F: -lane 'if (/:([\dA-Fa-f]+)$/) {print $F[2] if defined($flag);$flag=1;}'`
hg clone --rev $REV $1 $2

This invokes the hg log command to extract the revision number after the first tag-related revision and then clones to this revision.

Currently this does not work on remote repos: -R switch only works on local repos unfortunately.

The more I think about it the more I'm convinced the right answer is to just clone everything and update to the tag, which can be done in a single step:

hg clone http://host/path#tagname

That gets you everything and then does hg update to tagname which sets your working directory to the correct revision. Given delta compression that's not necessarily much larger, and if it is you can automate cloning the bulk of it from a previous local clone.

There is a postclone hook. It's called post-clone (the hgrc manpage shows a post-ANYCOMMAND and pre-ANYCOMMAND exist) though as you pointed out you could also use *changegroup or update hooks too, since clone uses both of those functions (unless you suppress update with -U).

What about just adding a --localtag so you have the name but not the extra changeset if you need it for reference only. Something like

hg clone -r tagname URL
hg tag --local tagname

which you could easily build into a shell alias.

Other than that there's not necessarily guaranteed to be a way to have revision X and the revision where revision X is tagged without also having other revisions you don't want since the tag could have been applied after other work was done. You can, of course, always update to 'X' and to have subsequent changesets in you working dir, but they'll still be in your repo.

Honestly, once I figured out that the tag name doesn't come a long when you clone up to a tag, which I admit confused the heck out of me at first, I didn't find any need to bring along the changeset with the tag in it.

Yes it can be done by post-clone/pull hooks, but there are a couple of crooks.

First, it only works for local repo, since you can't get the list of tags in a remote repo.

Second, dealing with clone/pull arguments and options is not trivial. (For clone I need to get the target repo, -r, -u, -U. For pull I need -r and -u.) I tried to use fancyopts, but it can't deal with global options, which are processed away in dispatch. I managed to hack dispatch to give me only the args and opts of a command, but it feels and looks ugly.

Using command wrapper would eliminate the second problem.

I hope one day hg will add an option to clone and pull to do it cleanly.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!