[PATCH 3 of 3] Change branches to sort 'active' branches first, and add an option to show only active branches

Eric Hopper hopper at omnifarious.org
Tue Jun 19 10:40:58 CDT 2007


# HG changeset patch
# User Eric Hopper <hopper at omnifarious.org>
# Date 1182267465 25200
# Node ID 0b02a2290ca74423817dbcfa9db302a3585b5967
# Parent  1b5e885c3fab11b87590e6e6a7606b57b69585a6
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
@@ -2713,7 +2726,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,9 @@
+0 files updated, 0 files merged, 1 files removed, 0 files unresolved
+0 files updated, 0 files merged, 1 files removed, 0 files unresolved
+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 @@ summary:     initial
 summary:     initial
 
 foo                            5:5f8fb06e083e
-default                        3:bf1bc2f45e83
-bar                            2:67ec16bde7f1
+default                        3:bf1bc2f45e83 (inactive)
+bar                            2:67ec16bde7f1 (inactive)
 foo
 default
 bar

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://selenic.com/pipermail/mercurial-devel/attachments/20070619/094719b3/attachment-0001.pgp 


More information about the Mercurial-devel mailing list