Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you close a Mercurial branch without updating to it first?

I am aware that you can close a Mercurial branch with:

hg update rev-number hg commit --close-branch -m "Closing branch." 

However, some of the repositories I work with a rather large, and after discovering a loose branch from years ago that I want to close, updating to it first can take many minutes (if not hours), only to do one commit and then update back to the original revision I was working from (more minutes, if not hours).

So my question, is there any way to close a Mercurial branch without updating the working directory to the branch revision first?

like image 669
gbmhunter Avatar asked Apr 16 '14 23:04

gbmhunter


2 Answers

Yes you can, but this is not documented anywhere. I've used this technique for a long time, don't worry, it is safe.

Instead of updating, you can issue the following commands

hg debugsetparent <revision> hg branch <branchOfRevision> 

Note that the order is important. This will make your repo think it is on the new revision, while all your files are from the initial one. After that, you can use the --close-branch commit, but use the -X * option to make an empty commit.

hg commit --close-branch -X * -m "Closing branch." 

Now, simply get back to your previous head, like nothing happened.

hg debugsetparent <InitialRevision> hg branch <branchOfInitialRevision> 

Finally, if you have sub-repos, you might want to temporarily rename the .hgsub file before committing the --close-branch, and rename back afterward.

like image 181
Vince Avatar answered Sep 20 '22 13:09

Vince


Mercurial 4.8 and upwards ships with the core extension closehead which provides this functionality:

hg close-head <revision> 

From the help:

This is equivalent to checking out each revision in a clean tree and running "hg commit --close-branch", except that it doesn't change the working directory.

At the moment you need to enable this extension explicitly in your hgrc:

[extensions] closehead = 
like image 33
Oben Sonne Avatar answered Sep 20 '22 13:09

Oben Sonne