mercurial/commands.py
changeset 4676 0f6e2b37512d
parent 4673 d8442fc0da8d
parent 4675 6858a7477a5e
child 4682 51ec5e069505
--- 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
@@ -2746,7 +2759,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,