109

When using hg branch FeatureBranchName and publishing it to a central repo that is shared amongst developers, is there a way to eventually close the FeatureBranchName when its development has officially been merged with the default branch?

It would also be helpful if the FeatureBranchName was not visible when performing a hg branches command.

Nate
  • 18,892
  • 27
  • 70
  • 93

2 Answers2

165
hg commit --close-branch

should be enough to mark a branch close. (see hg commit)

--close-branch

mark a branch as closed, hiding it from the branch list.

See also this thread:

My expectation is that I close a branch because this line of development has come to a dead end, and I don't want to be bothered with it any more.
Therefore, when a branch has been closed I shouldn't see it (in branches, heads, log, for instance) unless I explicitly ask to see closed branches.

I should note that I expect a closed branch to remain in the repository; it may be useful in the future, and the commit --close-branch message should at least explain why the branch was closed.
Pruning branches is another thing altogether.


Note: that "closing branch" business is one aspect seen as missing in Git, when compared to Mercurial:

Branches in git are, we’re always told, ephemeral things to be used and thrown away, and so far as I know git doesn’t have a way to indicate to your colleagues that you’re done with a branch;
the only way to do this is to delete it, or to hope they see the final merge commit and understand that the branch is closed to further development.

[In Mercurial] When you’re done with a branch, however, you cannot delete it from the repository; instead, you issue a commit which closes the branch, and Mercurial notes that the branch is closed. It’ll remain a permanent part of your repository history.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 4
    Is there a way to close a non-current branch? – Thomas S. Jun 22 '15 at 16:06
  • @ThomasS. 5 years later, I don't know for sure. The doc has recently been updated: http://mercurial.808500.n3.nabble.com/PATCH-commit-improve-close-branch-documentation-td4023191.html – VonC Jun 22 '15 at 18:24
  • 2
    @ThomasS. yes, [this answer](https://stackoverflow.com/questions/23122183/can-you-close-a-mercurial-branch-without-updating-to-it-first) helped me with that problem. `hg debugsetparent ` `hg branch ` 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."` – Maske Nov 08 '17 at 18:46
8

I wrote a simple script that completes the branch close, commands found at PruningDeadBranches.

## Script ##

#!/bin/bash
#script to close the not required branch in mercurial

hg up -C $1
if [ $? -eq 0 ]; then
    echo "$1 is up"
else
    echo "branch not found, please recheck your argument"
    exit 1
fi 
# if we are here then the branch is up, so we do the following
hg commit --close-branch -m 'this branch no longer required' 
echo "$1 is closed"
hg up -C default
echo "default is up" 

How to

Move to the local copy of the repository, and run this script by giving an argument. For example:

$./the_script_above.sh bad_branch_name_to_close

What does it do

This does the following:

  1. If the branch exists, it updates to the given branch or else exists with an error message.
  2. It closes the branch.
  3. Updates to the default branch.
  4. Stops.
Roshan Poudyal
  • 652
  • 7
  • 13
  • The script doesn't handle branches with spaces in the name. – Jared Aug 22 '16 at 14:39
  • @Jared can you run an independent up command with your branch. For e.g: `$ hg update yourbranchnamecontainingspace`. I think we could create a branchname with space, however you get a parse error when you run mercurial commands like **update**. You might want to consider escaping the space character while passing branch name as a parameter to the script. Cheers. – Roshan Poudyal Aug 27 '16 at 15:01
  • isn't `echo "$1 is up"` duplicated here? In `then` and after `fi`? Also, such script could be helpful if supports an option to merge into default at once – YakovL Mar 19 '19 at 13:52
  • 1
    @YakovL yes =). Thank you for the notification. I updated the script. Of course it would be helpful for optional merge, I shall try to update in future but until then you can scale the script to your taste =) – Roshan Poudyal Mar 20 '19 at 18:51