[PATCH 1 of 2] add a --closed option to 'hg branches' to display closed branches

John Mulligan phlogistonjohn at asynchrono.us
Mon Jun 8 19:21:43 CDT 2009


# HG changeset patch
# User John Mulligan <phlogistonjohn at asynchrono.us>
# Date 1244506665 14400
# Node ID f190d25e75f804fb025f23466f61e5221b20608c
# Parent  353b1c160c2d735607b5b3f5e537b32b5f8afd14
add a --closed option to 'hg branches' to display closed branches

change 'hg branches' so that closed branches are not displayed by default
closed branches can be revealed by using the --closed option
reorganise the commands.branches function a bit

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -432,38 +432,48 @@
     else:
         ui.write("%s\n" % encoding.tolocal(repo.dirstate.branch()))
 
-def branches(ui, repo, active=False):
+def branches(ui, repo, active=False, closed=False):
     """list repository named branches
 
-    List the repository's named branches, indicating which ones are
-    inactive. If active is specified, only show active branches.
+    List the repository's named branches. Active and inactive branches are
+    shown by default, and closed branches are hidden.
+    If active is specified only active branches will be displayed.
+    If closed is specified explicitly closed branches will be displayed.
+    The output of the command will indicate if a branch is closed or
+    inactive.
 
     A branch is considered active if it contains repository heads.
 
     Use the command 'hg update' to switch to an existing branch.
     """
     hexfunc = ui.debugflag and hex or short
-    activebranches = [encoding.tolocal(repo[n].branch())
-                            for n in repo.heads(closed=False)]
-    branches = sorted([(tag in activebranches, repo.changelog.rev(node), tag)
-                          for tag, node in repo.branchtags().items()],
-                      reverse=True)
+    _closed = set(repo.heads(closed=True))
+    _open = set(repo.heads(closed=False))
+    _states = {0:' (inactive)', 1:' (closed)', 2:''}
+    def _branches():
+        for tag, node in repo.branchtags().items():
+            if node in _open:
+                state = 2
+            elif node in _closed:
+                state = 1
+            else:
+                state = 0
+            yield state, repo.changelog.rev(node), tag, hexfunc(node)
 
-    for isactive, node, tag in branches:
-        if (not active) or isactive:
-            if ui.quiet:
-                ui.write("%s\n" % tag)
-            else:
-                hn = repo.lookup(node)
-                if isactive:
-                    notice = ''
-                elif hn not in repo.branchheads(tag, closed=False):
-                    notice = ' (closed)'
-                else:
-                    notice = ' (inactive)'
-                rev = str(node).rjust(31 - encoding.colwidth(tag))
-                data = tag, rev, hexfunc(hn), notice
-                ui.write("%s %s:%s%s\n" % data)
+    include = [2]
+    if closed:
+        include.append(1)
+    if not active:
+        include.append(0)
+    branches = (b for b in _branches() if b[0] in include)
+    for state, node, tag, hex in sorted(branches, reverse=True):
+        if ui.quiet:
+            ui.write("%s\n" % tag)
+        else:
+            state = _states[state]
+            rev = str(node).rjust(31 - encoding.colwidth(tag))
+            data = tag, rev, hex, state
+            ui.write("%s %s:%s%s\n" % data)
 
 def bundle(ui, repo, fname, dest=None, **opts):
     """create a changegroup file
@@ -3133,7 +3143,10 @@
     "branches":
         (branches,
          [('a', 'active', False,
-           _('show only branches that have unmerged heads'))],
+           _('show only branches that have unmerged heads')),
+          ('c', 'closed', False,
+           _('show closed branches')),
+         ],
          _('[-a]')),
     "bundle":
         (bundle,
diff --git a/tests/test-branches b/tests/test-branches
--- a/tests/test-branches
+++ b/tests/test-branches
@@ -69,7 +69,7 @@
 hg up -C b
 hg commit -d '9 0' --close-branch -m 'close this part branch too'
 echo '--- b branch should be inactive'
-hg branches
+hg branches -c
 hg branches -a
 echo 'xxx4' >> b
 hg commit -d '9 0' -m 'reopen branch with a change'
diff --git a/tests/test-debugcomplete.out b/tests/test-debugcomplete.out
--- a/tests/test-debugcomplete.out
+++ b/tests/test-debugcomplete.out
@@ -181,7 +181,7 @@
 backout: merge, parent, rev, include, exclude, message, logfile, date, user
 bisect: reset, good, bad, skip, command, noupdate
 branch: force, clean
-branches: active
+branches: active, closed
 bundle: force, rev, base, all, type, ssh, remotecmd
 cat: output, rev, decode, include, exclude
 copy: after, force, include, exclude, dry-run


More information about the Mercurial-devel mailing list