]> thyrsus.com Git - src.git/commitdiff
Factor out a duplicative check.
authorEric S. Raymond <esr@thyrsus.com>
Mon, 10 Nov 2014 14:16:56 +0000 (09:16 -0500)
committerEric S. Raymond <esr@thyrsus.com>
Mon, 10 Nov 2014 14:16:56 +0000 (09:16 -0500)
src

diff --git a/src b/src
index 7b42de0880bad815cd66ae34e171075ef29b7ee3..2f3851f3c30ef11d6d48abc8a0da9183b647fad4 100755 (executable)
--- a/src
+++ b/src
@@ -289,7 +289,7 @@ The omission of 'src remove' is a deliberate speed bump.
 
 class CommandContext:
     "Consime a revision specification or range from an argument list"
-    def __init__(self, cmd, args):
+    def __init__(self, cmd, args, require_empty=False):
         if not os.path.exists(repodir):
             croak("repository subdirectory %s does not exist" % repodir)
         self.start = self.end = None
@@ -325,6 +325,8 @@ class CommandContext:
                         self.end = self.start
                 except ValueError:
                     croak("malformed revision spec: %s" % revspec)
+        if require_empty and not self.is_empty():
+            croak("%s doesn't take a revision spec" % legend)
         if not self.args:
             try:
                 masters = os.listdir(repodir)
@@ -358,9 +360,7 @@ def add_method(*args):
             os.mkdir(repodir)
         except OSError:
             croak(" %s creation failed, check directory permissions." % repodir)
-    ctx = CommandContext("add", args)
-    if not ctx.is_empty():
-        croak("add doesn't take a revision spec")
+    ctx = CommandContext("add", args, require_empty=True)
     for arg in ctx.args:
        if not os.path.exists(arg):
            croak("I see no '%s' here." % arg)
@@ -421,9 +421,7 @@ def commit_method(*args):
         croak("repository subdirectory %s does not exist" % repodir)
     args = list(args)
     comment = get_comment("commit", args)
-    ctx = CommandContext("commit", args)
-    if not ctx.is_empty():
-        croak("commit doesn't take a revision spec")
+    ctx = CommandContext("commit", args, require_empty=True)
     for arg in ctx.args:
        if not os.path.exists(arg):
            croak("I see no '%s' here." % arg)
@@ -696,10 +694,8 @@ def branch_method(*args):
     tag_helper(args, "branch", backend.delete_branch, backend.set_branch)
 
 def filecmd(legend, hook, args):
-    ctx = CommandContext(legend, args)
-    if not ctx.is_empty():
-        croak("%s doesn't take a revision spec" % legend)
-    elif len(args) != 2:
+    ctx = CommandContext(legend, args, require_empty=True)
+    if len(args) != 2:
         croak("%s requires exactly two arguments" % legend)
     (source, target) = args
     if not os.path.exists(source):
@@ -723,9 +719,7 @@ def copy_method(*args):
 
 def release_method(*args):
     "Release locks."
-    ctx = CommandContext("release", args)
-    if not ctx.is_empty():
-        croak("release doesn't take a revision spec")
+    ctx = CommandContext("release", args, require_empty=True)
     for arg in ctx.args:
         if not os.path.exists(arg):
             croak("I see no '%s' here." % arg)