# HG changeset patch # User Eric Hopper # Date 1182447609 25200 # Node ID 6858a7477a5eeb187170b648448cabdaeb9c2f11 # Parent c7a81e3ae80f3e862e75636bf96808c29dce7ef3 Change branches to sort 'active' branches first, and add an option to show only active branches. diff --git a/mercurial/commands.py b/mercurial/commands.py --- a/mercurial/commands.py +++ b/mercurial/commands.py @@ -237,21 +237,34 @@ def branch(ui, repo, label=None, **opts) else: ui.write("%s\n" % util.tolocal(repo.dirstate.branch())) -def branches(ui, repo): +def branches(ui, repo, active=False): """list repository named branches - List the repository's named branches. + List the repository's named branches, indicating which ones are + inactive. If active is specified, only show active branches. + + A branch is considered active if it contains unmerged heads. """ b = repo.branchtags() - l = [(-repo.changelog.rev(n), n, t) for t, n in b.items()] + heads = dict.fromkeys(repo.heads(), 1) + l = [((n in heads), repo.changelog.rev(n), n, t) for t, n in b.items()] l.sort() - for r, n, t in l: - hexfunc = ui.debugflag and hex or short - if ui.quiet: - ui.write("%s\n" % t) + l.reverse() + for ishead, r, n, t in l: + if active and not ishead: + # If we're only displaying active branches, abort the loop on + # encountering the first inactive head + break else: - spaces = " " * (30 - util.locallen(t)) - ui.write("%s%s %s:%s\n" % (t, spaces, -r, hexfunc(n))) + hexfunc = ui.debugflag and hex or short + if ui.quiet: + ui.write("%s\n" % t) + else: + spaces = " " * (30 - util.locallen(t)) + # The code only gets here if inactive branches are being + # displayed or the branch is active. + isinactive = ((not ishead) and " (inactive)") or '' + ui.write("%s%s %s:%s%s\n" % (t, spaces, r, hexfunc(n), isinactive)) def bundle(ui, repo, fname, dest=None, **opts): """create a changegroup file @@ -2738,7 +2751,10 @@ table = { [('f', 'force', None, _('set branch name even if it shadows an existing branch'))], _('hg branch [NAME]')), - "branches": (branches, [], _('hg branches')), + "branches": (branches, + [('a', 'active', False, + _("show only branches that have unmerged heads"))], + _('hg branches [-a]')), "bundle": (bundle, [('f', 'force', None, diff --git a/tests/test-branches b/tests/test-branches new file mode 100755 --- /dev/null +++ b/tests/test-branches @@ -0,0 +1,36 @@ +#!/bin/sh + +hg init a +cd a +echo 'root' >root +hg add root +hg commit -d '0 0' -u test -m "Adding root node" + +echo 'a' >a +hg add a +hg branch a +hg commit -d '1 0' -u test -m "Adding a branch" + +hg update -C 0 +echo 'b' >b +hg add b +hg branch b +hg commit -d '2 0' -u test -m "Adding b branch" + +echo 'bh1' >bh1 +hg add bh1 +hg commit -d '3 0' -u test -m "Adding b branch head 1" + +hg update -C 2 +echo 'bh2' >bh2 +hg add bh2 +hg commit -d '4 0' -u test -m "Adding b branch head 2" + +echo 'c' >c +hg add c +hg branch c +hg commit -d '5 0' -u test -m "Adding c branch" + +hg branches +echo '-------' +hg branches -a diff --git a/tests/test-branches.out b/tests/test-branches.out new file mode 100644 --- /dev/null +++ b/tests/test-branches.out @@ -0,0 +1,12 @@ +marked working directory as branch a +0 files updated, 0 files merged, 1 files removed, 0 files unresolved +marked working directory as branch b +0 files updated, 0 files merged, 1 files removed, 0 files unresolved +marked working directory as branch c +c 5:5ca481e59b8c +a 1:dd6b440dd85a +b 4:22df7444f7c1 (inactive) +default 0:19709c5a4e75 (inactive) +------- +c 5:5ca481e59b8c +a 1:dd6b440dd85a diff --git a/tests/test-encoding.out b/tests/test-encoding.out --- a/tests/test-encoding.out +++ b/tests/test-encoding.out @@ -127,13 +127,13 @@ tip 5:db5 é 3:770b9b11621d % ascii ? 5:db5520b4645f -default 4:9cff3c980b58 +default 4:9cff3c980b58 (inactive) % latin-1 é 5:db5520b4645f -default 4:9cff3c980b58 +default 4:9cff3c980b58 (inactive) % utf-8 é 5:db5520b4645f -default 4:9cff3c980b58 +default 4:9cff3c980b58 (inactive) % utf-8 changeset: 5:db5520b4645f branch: é diff --git a/tests/test-newbranch.out b/tests/test-newbranch.out --- a/tests/test-newbranch.out +++ b/tests/test-newbranch.out @@ -48,8 +48,8 @@ date: Mon Jan 12 13:46:40 1970 +0 summary: initial foo 5:5f8fb06e083e -default 3:bf1bc2f45e83 -bar 2:67ec16bde7f1 +default 3:bf1bc2f45e83 (inactive) +bar 2:67ec16bde7f1 (inactive) foo default bar