show: fix corrupt json output with no bookmarks
authorRyan McElroy <rmcelroy@fb.com>
Fri, 07 Apr 2017 10:46:32 -0700
changeset 31859 8e282aa3c3ff
parent 31858 ad366d57d574
child 31860 56f58027a513
show: fix corrupt json output with no bookmarks
hgext/show.py
tests/test-show.t
--- a/hgext/show.py	Fri Apr 07 10:26:13 2017 -0700
+++ b/hgext/show.py	Fri Apr 07 10:46:32 2017 -0700
@@ -19,6 +19,7 @@
     cmdutil,
     commands,
     error,
+    formatter,
     pycompat,
     registrar,
 )
@@ -109,8 +110,11 @@
     """bookmarks and their associated changeset"""
     marks = repo._bookmarks
     if not len(marks):
-        # TODO json output is corrupted; consider using formatter
-        ui.write(_('(no bookmarks set)\n'))
+        # This is a bit hacky. Ideally, templates would have a way to
+        # specify an empty output, but we shouldn't corrupt JSON while
+        # waiting for this functionality.
+        if not isinstance(fm, formatter.jsonformatter):
+            ui.write(_('(no bookmarks set)\n'))
         return
 
     active = repo._activebookmark
--- a/tests/test-show.t	Fri Apr 07 10:26:13 2017 -0700
+++ b/tests/test-show.t	Fri Apr 07 10:46:32 2017 -0700
@@ -114,4 +114,12 @@
    }
   ]
 
+JSON works with no bookmarks
+
+  $ hg book -d a-longer-bookmark
+  $ hg book -d book1
+  $ hg show bookmarks -T json
+  [
+  ]
+
   $ cd ..