6 from optparse
import OptionParser
, Option
, OptionValueError
8 def _checkFile(option
, opt
, value
):
9 """Checks whether value is a valid file and returns it if so
12 return checkFile(value
)
15 def checkFile(value
, relative
=False):
16 """Check whether value is a valid file and returns it if so
19 value: The value to check.
20 relative: Whether to treat value as a path relative to the working dir.
25 file = git
.ls_files("--full-name", "--", value
, with_keep_cwd
=relative
)
28 raise OptionValueError("Unknown file '%s'" % value
)
30 splitfile
= file.split('\n')
32 if len(splitfile
) > 1:
33 raise OptionValueError(
34 "Please specify only one file, '%s' matches more than one" % value
)
38 def _check_commit(option
, opt
, value
):
39 """Checks whether value is a valid refspec and returns its hash if so
43 rev
= git
.rev_parse("--verify", value
, with_exceptions
=False)
46 raise OptionValueError("Unknown commit '%s'" % value
)
50 def _check_bool(option
, opt
, value
):
51 """Checks whether a value is a boolean and returns it's value
53 Understands about 'True', 'False', and 'None'.
65 raise OptionValueError("Not a boolean: '%s'" % value
)
67 class GitOption(Option
):
68 """This parser understands three new types:
69 commit: A commit must specify exactly 1 commit in the current repository.
70 file: A file must specify exactly 1 tracked file in the current repository.
71 bool: A bool must be 'True', 'False', or 'None'.
74 # Define the new types
75 TYPES
= Option
.TYPES
+ ("commit",) + ("file",) + ("bool",)
77 # Copy the type checker list so that we don't keep the default ones
78 TYPE_CHECKER
= copy
.copy(Option
.TYPE_CHECKER
)
81 TYPE_CHECKER
["commit"] = _check_commit
82 TYPE_CHECKER
["file"] = _checkFile
83 TYPE_CHECKER
["bool"] = _check_bool
85 def isUnique(options
, at_least_one
=False):
86 """Checks if a list of options is unique
89 options: The list of options to check
90 at_least_one: If set, when no optiosn are set, return False
95 for option
in options
:
97 # If we already found one, it's not unique for sure
103 # If there is only one, it's unique
107 # None found, so unique only if we don't require at least one
108 return not at_least_one