[PATCH 5 of 5] Change branches to only show 'active' branches by default. Add an option to show all branches

Eric Hopper hopper at omnifarious.org
Tue Jun 5 22:12:28 CDT 2007


# HG changeset patch
# User Eric Hopper <hopper at omnifarious.org>
# Date 1181098450 25200
# Node ID fb2086b7577a5016bd4ff9eb16dbf1d11ab61410
# Parent  fd53dbf6fb2bcc9c94b76f336b8b175aefb453d0
Change branches to only show 'active' branches by default.  Add an option to show all branches.

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -271,21 +271,42 @@ 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.  If active is specified,
+    only show branches that have tips that are unmerged heads in the
+    repository.
     """
     b = repo.branchtags()
-    l = [(-repo.changelog.rev(n), n, t) for t, n in b.items()]
-    l.sort()
+    heads = dict.fromkeys(repo.heads(), 1)
+    if active:
+        # Only branches who's tip is an unmerged head.
+        l = [(repo.changelog.rev(n), n, t) for t, n in b.items() if n in heads]
+    else:
+        l = [(repo.changelog.rev(n), n, t) for t, n in b.items()]
+    def branch_cmp(b1, b2):
+        # Make sure that branches who's tip is an unmerged head show
+        # up before all other branches.
+        b1_head = b1[1] in heads
+        b2_head = b2[1] in heads
+        if b1_head and not b2_head:
+            return -1
+        if b2_head and not b1_head:
+            return 1
+        return -cmp(b1, b2) # Descending by rev #
+    l.sort(branch_cmp)
     for r, n, t in l:
         hexfunc = ui.debugflag and hex or short
         if ui.quiet:
             ui.write("%s\n" % t)
         else:
             spaces = " " * (30 - util.locallen(t))
-            ui.write("%s%s %s:%s\n" % (t, spaces, -r, hexfunc(n)))
+            # Only display head indication when branch's tip is an
+            # unmerged head and we are display all branches, not just
+            # active ones.
+            ishead = ((not active) and (n in heads) and " *HEAD*") or ''
+            ui.write("%s%s %s:%s%s\n" % (t, spaces, r, hexfunc(n), ishead))
 
 def bundle(ui, repo, fname, dest=None, **opts):
     """create a changegroup file
@@ -2727,7 +2748,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-encoding.out b/tests/test-encoding.out
--- a/tests/test-encoding.out
+++ b/tests/test-encoding.out
@@ -125,13 +125,13 @@ tip                                5:db5
 tip                                5:db5520b4645f
 ??                                  3:770b9b11621d
 % ascii
-?                              5:db5520b4645f
+?                              5:db5520b4645f *HEAD*
 default                        4:9cff3c980b58
 % latin-1
-?                              5:db5520b4645f
+?                              5:db5520b4645f *HEAD*
 default                        4:9cff3c980b58
 % utf-8
-??                              5:db5520b4645f
+??                              5:db5520b4645f *HEAD*
 default                        4:9cff3c980b58
 % utf-8
 changeset:   5:db5520b4645f
diff --git a/tests/test-newbranch.out b/tests/test-newbranch.out
--- a/tests/test-newbranch.out
+++ b/tests/test-newbranch.out
@@ -44,7 +44,7 @@ date:        Mon Jan 12 13:46:40 1970 +0
 date:        Mon Jan 12 13:46:40 1970 +0000
 summary:     initial
 
-foo                            5:5f8fb06e083e
+foo                            5:5f8fb06e083e *HEAD*
 default                        3:bf1bc2f45e83
 bar                            2:67ec16bde7f1
 foo

-------------- 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/20070605/149f48e4/attachment-0001.pgp 


More information about the Mercurial-devel mailing list