From 549c6935bfca36fb59e692c3fe94cc61e7ebf19e Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Mon, 10 Nov 2014 09:16:56 -0500 Subject: [PATCH] Factor out a duplicative check. --- src | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/src b/src index 7b42de0..2f3851f 100755 --- 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) -- 2.43.0