Patchwork [3,of,9] bookmarks: refactor option checking to pick one from --delete/rename/active

login
register
mail settings
Submitter Yuya Nishihara
Date Sept. 21, 2018, 1:24 p.m.
Message ID <7dd56082951ff7977c1b.1537536245@mimosa>
Download mbox | patch
Permalink /patch/34896/
State Accepted
Headers show

Comments

Yuya Nishihara - Sept. 21, 2018, 1:24 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1536980669 -32400
#      Sat Sep 15 12:04:29 2018 +0900
# Node ID 7dd56082951ff7977c1b550a5b38d608d655618a
# Parent  c8d2fd3848a58477e0ce9eb15b62c0e7cc30303e
bookmarks: refactor option checking to pick one from --delete/rename/active

Patch

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -961,36 +961,33 @@  def bookmark(ui, repo, *names, **opts):
     opts = pycompat.byteskwargs(opts)
     force = opts.get('force')
     rev = opts.get('rev')
-    delete = opts.get('delete')
     rename = opts.get('rename')
     inactive = opts.get('inactive')
-    active = opts.get('active')
-
-    if delete and rename:
-        raise error.Abort(_("--delete and --rename are incompatible"))
-    if delete and rev:
-        raise error.Abort(_("--rev is incompatible with --delete"))
-    if rename and rev:
-        raise error.Abort(_("--rev is incompatible with --rename"))
-    if delete and active:
-        raise error.Abort(_("--delete is incompatible with --active"))
-    if rev and active:
-        raise error.Abort(_("--rev is incompatible with --active"))
-    if rename and active:
-        raise error.Abort(_("--rename is incompatible with --active"))
-    if names and active:
+
+    selactions = [k for k in ['delete', 'rename', 'active'] if opts.get(k)]
+    if len(selactions) > 1:
+        raise error.Abort(_('--%s and --%s are incompatible')
+                          % tuple(selactions[:2]))
+    if selactions:
+        action = selactions[0]
+    else:
+        action = None
+
+    if rev and action in {'delete', 'rename', 'active'}:
+        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 active:
+    if inactive and action == 'active':
         raise error.Abort(_("--inactive is incompatible with --active"))
-    if not names and (delete or rev):
+    if not names and (action == 'delete' or rev):
         raise error.Abort(_("bookmark name required"))
 
-    if delete or rename or names or inactive:
+    if action in {'delete', 'rename'} or names or inactive:
         with repo.wlock(), repo.lock(), repo.transaction('bookmark') as tr:
-            if delete:
+            if action == 'delete':
                 names = pycompat.maplist(repo._bookmarks.expandname, names)
                 bookmarks.delete(repo, tr, names)
-            elif rename:
+            elif action == 'rename':
                 if not names:
                     raise error.Abort(_("new bookmark name required"))
                 elif len(names) > 1:
@@ -1006,7 +1003,7 @@  def bookmark(ui, repo, *names, **opts):
                     ui.status(_("no active bookmark\n"))
                 else:
                     bookmarks.deactivate(repo)
-    elif active:
+    elif action == 'active':
         book = repo._activebookmark
         if book is None:
             return 1