[PATCH 9 of 9 bm-refactor] bookmarks: factor method _printer out of for loop in printbookmarks

Sean Farley sean at farley.io
Tue Jun 20 20:29:32 EDT 2017


# HG changeset patch
# User Sean Farley <sean at farley.io>
# Date 1498004300 25200
#      Tue Jun 20 17:18:20 2017 -0700
# Branch bm-refactor
# Node ID 504dd6e8e458eb3b07c958cbf3bc205c1e85fc8d
# Parent  e9cbc8f652302c0f69c8b05dc7fa6ec2b7eadd8e
bookmarks: factor method _printer out of for loop in printbookmarks

This allows even further customization via extensions for printing
bookmarks. For example, in hg-git this would allow printing remote refs
by just modifying the 'bmarks' parameter instead of reimplementing the
old commands.bookmarks method.

Furthermore, there is another benefit: now multiple extensions can
chain their custom data to bookmark printing. Previously, an extension
could have conflicting bookmark output due to which loaded first and
overrode commands.bookmarks. Now they can all play nicely together.

diff --git a/mercurial/bookmarks.py b/mercurial/bookmarks.py
index b8d31fb..0b53de0 100644
--- a/mercurial/bookmarks.py
+++ b/mercurial/bookmarks.py
@@ -768,32 +768,43 @@ def addbookmarks(repo, names, rev=None, 
             activate(repo, newact)
         elif cur != tgt and newact == repo._activebookmark:
             deactivate(repo)
         marks.recordchange(tr)
 
+def _printer(ui, repo, bmarks, **opts):
+    """private method to print bookmarks
+
+    Provides a way for extensions to control how bookmarks are printed (e.g.
+    prepend or postpend names)
+    """
+    fm = ui.formatter('bookmarks', opts)
+    hexfn = fm.hexfunc
+    if len(bmarks) == 0 and fm.isplain():
+        ui.status(_("no bookmarks set\n"))
+    for bmark, (n, prefix, label) in sorted(bmarks.iteritems()):
+        fm.startitem()
+        if not ui.quiet:
+            fm.plain(' %s ' % prefix, label=label)
+        fm.write('bookmark', '%s', bmark, label=label)
+        pad = " " * (25 - encoding.colwidth(bmark))
+        fm.condwrite(not ui.quiet, 'rev node', pad + ' %d:%s',
+                     repo.changelog.rev(n), hexfn(n), label=label)
+        fm.data(active=(activebookmarklabel in label))
+        fm.plain('\n')
+    fm.end()
+
 def printbookmarks(ui, repo, **opts):
     """print bookmarks to a formatter
 
     Provides a way for extensions to control how bookmarks are printed.
     """
-    fm = ui.formatter('bookmarks', opts)
-    hexfn = fm.hexfunc
     marks = repo._bookmarks
-    if len(marks) == 0 and fm.isplain():
-        ui.status(_("no bookmarks set\n"))
+    bmarks = {}
     for bmark, n in sorted(marks.iteritems()):
         active = repo._activebookmark
         if bmark == active:
             prefix, label = '*', activebookmarklabel
         else:
             prefix, label = ' ', ''
 
-        fm.startitem()
-        if not ui.quiet:
-            fm.plain(' %s ' % prefix, label=label)
-        fm.write('bookmark', '%s', bmark, label=label)
-        pad = " " * (25 - encoding.colwidth(bmark))
-        fm.condwrite(not ui.quiet, 'rev node', pad + ' %d:%s',
-                     repo.changelog.rev(n), hexfn(n), label=label)
-        fm.data(active=(bmark == active))
-        fm.plain('\n')
-    fm.end()
+        bmarks[bmark] = (n, prefix, label)
+    _printer(ui, repo, bmarks, **opts)


More information about the Mercurial-devel mailing list