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?
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.
Thanks to Vince for the approach. I've implemented this as a Python script - it was a little more work than I initially expected, so hopefully this will save others some time. Tested on Windows 8.1 with TortoiseHg 3.3 and Python 2.7.9.
Feedback welcome, it could probably do with some finesse, particularly with regards to error-handling.
#!/usr/bin/python
from subprocess import check_output
def close_branch( branch, message ):
if not message:
message = 'Closing branch "{}"'.format( branch )
print( 'Closing branch "{}"'.format( branch ) )
try:
check_output( 'hg debugsetparent ' + branch )
check_output( 'hg branch ' + branch )
check_output( 'hg commit --close-branch -X * -m "' + message + '"' )
except:
print( 'Failed to close branch.' )
def main():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('branch', help='branch name(s)', nargs = '+' )
parser.add_argument('-m', '--message', help='message (defaults to "Closing branch <branch name>")' )
args = parser.parse_args()
status = check_output( 'hg status' )
if len(status) > 0:
print( 'Do not use this script with local changes. Commit/revert changes and try again' )
exit(1)
# Cache initial revision and branch.
initial_revision = check_output( 'hg id -i -b' ).split()
# print( 'Using: ' + initial_revision[0].decode("utf-8") )
for branch in args.branch:
close_branch( branch, args.message )
# Return to original changeset
check_output( 'hg debugsetparent ' + initial_revision[0].decode("utf-8") )
check_output( 'hg branch ' + initial_revision[1].decode("utf-8") )
if __name__ == '__main__':
main()
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 =