[PATCH 8 of 9] bookmarks: add explicit option to list bookmarks of the given names

Yuya Nishihara yuya at tcha.org
Fri Sep 21 09:24:10 EDT 2018


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1536983063 -32400
#      Sat Sep 15 12:44:23 2018 +0900
# Node ID 323412d960084affba93ec7a23d8436392ba3fa6
# Parent  27e99ca7224b0fe214091df83e8e24dee3ffb725
bookmarks: add explicit option to list bookmarks of the given names

This is a generalized form of the --active option.

A redundant sorted() call is removed. There was no point to update dict items
in lexicographical order.

diff --git a/mercurial/bookmarks.py b/mercurial/bookmarks.py
--- a/mercurial/bookmarks.py
+++ b/mercurial/bookmarks.py
@@ -936,21 +936,23 @@ def _printbookmarks(ui, repo, fm, bmarks
         fm.data(active=(activebookmarklabel in label))
         fm.plain('\n')
 
-def printbookmarks(ui, repo, fm):
+def printbookmarks(ui, repo, fm, names=None):
     """print bookmarks by the given formatter
 
     Provides a way for extensions to control how bookmarks are printed.
     """
     marks = repo._bookmarks
     bmarks = {}
-    for bmark, n in sorted(marks.iteritems()):
+    for bmark in (names or marks):
+        if bmark not in marks:
+            raise error.Abort(_("bookmark '%s' does not exist") % bmark)
         active = repo._activebookmark
         if bmark == active:
             prefix, label = '*', activebookmarklabel
         else:
             prefix, label = ' ', ''
 
-        bmarks[bmark] = (n, prefix, label)
+        bmarks[bmark] = (marks[bmark], prefix, label)
     _printbookmarks(ui, repo, fm, bmarks)
 
 def preparehookargs(name, old, new):
diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -903,6 +903,7 @@ def bisect(ui, repo, rev=None, extra=Non
     ('d', 'delete', False, _('delete a given bookmark')),
     ('m', 'rename', '', _('rename a given bookmark'), _('OLD')),
     ('i', 'inactive', False, _('mark a bookmark inactive')),
+    ('l', 'list', False, _('list existing bookmarks')),
     ('', 'active', False, _('display the active bookmark')),
     ] + formatteropts,
     _('hg bookmarks [OPTIONS]... [NAME]...'))
@@ -924,7 +925,7 @@ def bookmark(ui, repo, *names, **opts):
     diverged, a new 'divergent bookmark' of the form 'name at path' will
     be created. Using :hg:`merge` will resolve the divergence.
 
-    Specifying bookmark as '.' to -m or -d options is equivalent to specifying
+    Specifying bookmark as '.' to -m/-d/-l options is equivalent to specifying
     the active bookmark's name.
 
     A bookmark named '@' has the special property that :hg:`clone` will
@@ -963,7 +964,8 @@ def bookmark(ui, repo, *names, **opts):
     rev = opts.get('rev')
     inactive = opts.get('inactive')  # meaning add/rename to inactive bookmark
 
-    selactions = [k for k in ['delete', 'rename', 'active'] if opts.get(k)]
+    selactions = [k for k in ['delete', 'rename', 'active', 'list']
+                  if opts.get(k)]
     if len(selactions) > 1:
         raise error.Abort(_('--%s and --%s are incompatible')
                           % tuple(selactions[:2]))
@@ -974,13 +976,13 @@ def bookmark(ui, repo, *names, **opts):
     elif inactive:
         action = 'inactive'  # meaning deactivate
     else:
-        action = None
-
-    if rev and action in {'delete', 'rename', 'active'}:
+        action = 'list'
+
+    if rev and action in {'delete', 'rename', 'active', 'list'}:
         raise error.Abort(_("--rev is incompatible with --%s") % action)
     if names and action == 'active':
         raise error.Abort(_("NAMES is incompatible with --active"))
-    if inactive and action in {'delete', 'active'}:
+    if inactive and action in {'delete', 'active', 'list'}:
         raise error.Abort(_("--inactive is incompatible with --%s") % action)
     if not names and action in {'add', 'delete'}:
         raise error.Abort(_("bookmark name required"))
@@ -1011,9 +1013,12 @@ def bookmark(ui, repo, *names, **opts):
         if book is None:
             return 1
         ui.write("%s\n" % book, label=bookmarks.activebookmarklabel)
-    else: # show bookmarks
+    elif action == 'list':
+        names = pycompat.maplist(repo._bookmarks.expandname, names)
         with ui.formatter('bookmarks', opts) as fm:
-            bookmarks.printbookmarks(ui, repo, fm)
+            bookmarks.printbookmarks(ui, repo, fm, names)
+    else:
+        raise error.ProgrammingError('invalid action: %s' % action)
 
 @command('branch',
     [('f', 'force', None,
diff --git a/tests/test-bookmarks.t b/tests/test-bookmarks.t
--- a/tests/test-bookmarks.t
+++ b/tests/test-bookmarks.t
@@ -68,6 +68,25 @@ list bookmarks
      X                         0:f7b1eb17ad24
    * X2                        0:f7b1eb17ad24
      Y                         -1:000000000000
+  $ hg bookmarks -l
+     X                         0:f7b1eb17ad24
+   * X2                        0:f7b1eb17ad24
+     Y                         -1:000000000000
+  $ hg bookmarks -l X Y
+     X                         0:f7b1eb17ad24
+     Y                         -1:000000000000
+  $ hg bookmarks -l .
+   * X2                        0:f7b1eb17ad24
+  $ hg bookmarks -l X A Y
+  abort: bookmark 'A' does not exist
+  [255]
+  $ hg bookmarks -l -r0
+  abort: --rev is incompatible with --list
+  [255]
+  $ hg bookmarks -l --inactive
+  abort: --inactive is incompatible with --list
+  [255]
+
   $ hg log -T '{bookmarks % "{rev} {bookmark}\n"}'
   0 X
   0 X2
diff --git a/tests/test-completion.t b/tests/test-completion.t
--- a/tests/test-completion.t
+++ b/tests/test-completion.t
@@ -250,7 +250,7 @@ Show all commands + options
   archive: no-decode, prefix, rev, type, subrepos, include, exclude
   backout: merge, commit, no-commit, parent, rev, edit, tool, include, exclude, message, logfile, date, user
   bisect: reset, good, bad, skip, extend, command, noupdate
-  bookmarks: force, rev, delete, rename, inactive, active, template
+  bookmarks: force, rev, delete, rename, inactive, list, active, template
   branch: force, clean, rev
   branches: active, closed, template
   bundle: force, rev, branch, base, all, type, ssh, remotecmd, insecure


More information about the Mercurial-devel mailing list