Close multiple branches in TortoiseHg
Asked Answered
C

4

5

I use TortoiseHg 1.1 and in our team we tend to create named branches for each case we work on then merge with default when we're done. Unfortunately most of the named branches don't get closed. This isn't a huge problem but it's annoying when you try and filter by the branch you are currently working on and there is a massive list of old branches to search through.

So is there an easy way in TortoiseHg or from the command line to close multiple branches quickly without having to manually commit and close on each branch?

Clypeus answered 8/12, 2010 at 10:5 Comment(1)
If you have enough named branches to be confusing or difficult to browse, you're probably using named branches poorly. One of the ideas behind a DVCS is that you can make anonymous branches whenever you'd like simply by cloning, which you can work on independently and then push back up the chain once the feature is done, deleting the now useless anonymous branch without concern.Trichromat
E
8

Unfortunately no.

You need to close each branch separately with its own commit on that branch.

The best way to do that is to use the command line, you could even create a simple batch file to close a bunch of them:

for %%f in (branch1 branch2 branch4) do (
    hg up "%%f"
    if exitcode 1 goto quit
    hg commit --close-branch -m "closing branch %%f"
    if exitcode 1 goto quit
)
:quit

The workflow I use is to use the bookmarks extension to keep only local bookmarks for my lines of development, this way I use normal unnamed branches in Mercurial, but can still easily tell them apart locally. The commit messages are used to separate them later.

This way I don't have a bunch of named branches cluttering up my history, I don't have to remember to close them, and I only have to keep track of the branches I use locally.

Elimination answered 8/12, 2010 at 10:7 Comment(1)
+1 Missed the last portion on automatically closing the branch. I have always professed about using branch only for lines of development that will be long lived.Titular
T
3

See the following for all the possible options:

of which the closing branch option can be used.

hg up -C badbranch
hg commit --close-branch -m 'close badbranch, this approach never worked'
hg up -C default # switch back to "good" branch

Also, my understanding has been that it is preferable to clone for most work and use named branches only for few possible long lived trains of development.

Titular answered 8/12, 2010 at 10:7 Comment(1)
Yeah, I would use local bookmarks for my own work and only named branches for bigger features, not for normal day-to-day bugfixing.Elimination
A
2

Here is a powershell script that will close all active branches except the default. Add your own filtering logic to exclude branches you don't want to close.

$branches = hg branches -a | sort
$exclude = "default" 

foreach ($item in $branches)
{
 $branch = $item.Split([string[]]  " ", [StringSplitOptions]::RemoveEmptyEntries)[0]

 if(!$exclude.Contains($branch))
 {
    hg update $branch
    hg commit --close-branch -m "closing old branch"
    hg status
    Write-Host $branch 
 }
}

hg push
Anamorphoscope answered 13/8, 2013 at 12:44 Comment(1)
Wouldn't this fail to close branches that had a space in the name? I've been trying to do this in bash and I'm getting stuck on the output of hg branches. Each item is actually in the form <branch name> <tip change> <optional 'inactive' tag>. The hg documentation says something about an experimental --template command line argument, but my version of mercurial doesn't seem to support it...Animadvert
A
0

Okay so this is way late, but I set out to do this via bash and after a lot of pain I got a working solution. Take note that this does not do an hg push at the end, because I wanted to look over the results in tortoise before pushing.

#Protected branches to not close
develop='develop'
default='default'

IFS=$'\n'

#Get all branches
allBranches=$( hg branches | sort )

#Loop over and close old branches
for item in $allBranches
do

#Trim off everything but the branch name
branch=$( echo $item | sed -r 's/\s*[0-9]+:[0-9a-f]+(\s\(inactive\))?$//' )

if [ $branch != $develop ] && [ $branch != $default ]
then

hg update $branch
hg commit --close-branch -m "Closing old branch"

fi

done

After running the script I asked the team which branches they were actively working on and stripped those closure commits.

Animadvert answered 5/6, 2017 at 20:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.