Patchwork [5,of,5] show: fix corrupt json output with no bookmarks

login
register
mail settings
Submitter Ryan McElroy
Date April 10, 2017, 9:41 a.m.
Message ID <89020c7fc2a1734dcca1.1491817267@devbig314.prn1.facebook.com>
Download mbox | patch
Permalink /patch/20050/
State Accepted
Headers show

Comments

Ryan McElroy - April 10, 2017, 9:41 a.m.
# HG changeset patch
# User Ryan McElroy <rmcelroy@fb.com>
# Date 1491587192 25200
#      Fri Apr 07 10:46:32 2017 -0700
# Node ID 89020c7fc2a1734dcca1d8ea42fde96f7e7426f5
# Parent  eefe70d59a3ea5a09b5711ea8d7c568002a8d9e3
show: fix corrupt json output with no bookmarks
Yuya Nishihara - April 11, 2017, 1 p.m.
On Mon, 10 Apr 2017 02:41:07 -0700, Ryan McElroy wrote:
> # HG changeset patch
> # User Ryan McElroy <rmcelroy@fb.com>
> # Date 1491587192 25200
> #      Fri Apr 07 10:46:32 2017 -0700
> # Node ID 89020c7fc2a1734dcca1d8ea42fde96f7e7426f5
> # Parent  eefe70d59a3ea5a09b5711ea8d7c568002a8d9e3
> show: fix corrupt json output with no bookmarks

Queued this, thanks for adding the test.

Patch

diff --git a/hgext/show.py b/hgext/show.py
--- a/hgext/show.py
+++ b/hgext/show.py
@@ -18,6 +18,7 @@  from mercurial.i18n import _
 from mercurial import (
     cmdutil,
     error,
+    formatter,
     registrar,
 )
 
@@ -109,8 +110,11 @@  def showbookmarks(ui, repo, fm):
     """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
diff --git a/tests/test-show.t b/tests/test-show.t
--- a/tests/test-show.t
+++ b/tests/test-show.t
@@ -116,4 +116,11 @@  bookmarks JSON works
    }
   ]
 
+JSON works with no bookmarks
+
+  $ hg book -d a-longer-bookmark
+  $ hg book -d book1
+  $ hg show bookmarks -T json
+  [
+  ]
   $ cd ..