Patchwork [1,of,2,V5] shelve: refactor option combination check to easily add new ones

login
register
mail settings
Submitter Katsunori FUJIWARA
Date June 20, 2014, 7:19 a.m.
Message ID <7ed8ff8f8b904e22f8e8.1403248794@feefifofum>
Download mbox | patch
Permalink /patch/5023/
State Accepted
Commit aad28ff87788c14e7b86abf444c811f237aea9b6
Headers show

Comments

Katsunori FUJIWARA - June 20, 2014, 7:19 a.m.
# HG changeset patch
# User FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
# Date 1403248538 -32400
#      Fri Jun 20 16:15:38 2014 +0900
# Node ID 7ed8ff8f8b904e22f8e834810716861755ef5696
# Parent  cd3c79392056a0d965236e4986a7a4b5d580f3e5
shelve: refactor option combination check to easily add new ones

Before this patch, the name of a newly added option had to be added
into each string that was passed to the "checkopt()" internal
function: these are white-space-separated list of un-acceptable option
names (= "black list" for the specified "opt").

This new option had to be added into multiple strings because each
option could belong to only one action of "create", "cleanup",
"delete" or "list".

In addition to this redundancy, each string passed to "checkopt()" was
already too long to include a new one.

This patch refactors option combination check to make it easier to add
a new option in a subsequent patch.

New "checkopt()" only takes one action ("cleanup", "delete" or
"list"), and checks whether all explicitly activated options are
allowed for it or not (if specified action is activated in "opts").

The "date" entry is listed in "allowables", but commented out,
because:

  - "date" shouldn't be checked for test

    checking "date" causes unexpected failure of "test-shelve.t",
    because "run-test.py" puts "[default] shelve = --date '0 0'" into
    hgrc.

  - explicitly listing it can advertise that ignoring it is intentional

This patch doesn't choose "white list" for the specified "opt", to
avoid treating global options.

Patch

diff --git a/hgext/shelve.py b/hgext/shelve.py
--- a/hgext/shelve.py
+++ b/hgext/shelve.py
@@ -675,20 +675,31 @@ 
     '''
     cmdutil.checkunfinished(repo)
 
-    def checkopt(opt, incompatible):
+    allowables = [
+        ('addremove', 'create'), # 'create' is pseudo action
+        ('cleanup', 'cleanup'),
+#       ('date', 'create'), # ignored for passing '--date "0 0"' in tests
+        ('delete', 'delete'),
+        ('list', 'list'),
+        ('message', 'create'),
+        ('name', 'create'),
+        ('patch', 'list'),
+        ('stat', 'list'),
+    ]
+    def checkopt(opt):
         if opts[opt]:
-            for i in incompatible.split():
-                if opts[i]:
+            for i, allowable in allowables:
+                if opts[i] and opt != allowable:
                     raise util.Abort(_("options '--%s' and '--%s' may not be "
                                        "used together") % (opt, i))
             return True
-    if checkopt('cleanup', 'addremove delete list message name patch stat'):
+    if checkopt('cleanup'):
         if pats:
             raise util.Abort(_("cannot specify names when using '--cleanup'"))
         return cleanupcmd(ui, repo)
-    elif checkopt('delete', 'addremove cleanup list message name patch stat'):
+    elif checkopt('delete'):
         return deletecmd(ui, repo, pats)
-    elif checkopt('list', 'addremove cleanup delete message name'):
+    elif checkopt('list'):
         return listcmd(ui, repo, pats, opts)
     else:
         for i in ('patch', 'stat'):