changeset 39753:b05b4b91de3d

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.
author Yuya Nishihara <yuya@tcha.org>
date Sat, 15 Sep 2018 12:44:23 +0900
parents 2d478b05fb14
children c48738923dba
files mercurial/bookmarks.py mercurial/commands.py tests/test-bookmarks.t tests/test-completion.t
diffstat 4 files changed, 38 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/bookmarks.py	Sat Sep 15 12:34:13 2018 +0900
+++ b/mercurial/bookmarks.py	Sat Sep 15 12:44:23 2018 +0900
@@ -936,21 +936,23 @@
         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):
--- a/mercurial/commands.py	Sat Sep 15 12:34:13 2018 +0900
+++ b/mercurial/commands.py	Sat Sep 15 12:44:23 2018 +0900
@@ -903,6 +903,7 @@
     ('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 @@
     diverged, a new 'divergent bookmark' of the form 'name@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 @@
     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 @@
     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 @@
         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,
--- a/tests/test-bookmarks.t	Sat Sep 15 12:34:13 2018 +0900
+++ b/tests/test-bookmarks.t	Sat Sep 15 12:44:23 2018 +0900
@@ -68,6 +68,25 @@
      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
--- a/tests/test-completion.t	Sat Sep 15 12:34:13 2018 +0900
+++ b/tests/test-completion.t	Sat Sep 15 12:44:23 2018 +0900
@@ -250,7 +250,7 @@
   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