6 from optparse
import OptionParser
, Option
, OptionValueError
8 def _check_file(option
, opt
, value
):
9 """Checks whether value is a valid file and returns it
12 return check_file(value
)
15 def check_file(value
, relative
=False):
16 """Check whether value is a valid file and returns it
21 file = git
.ls_files("--full-name", "--", value
, with_keep_cwd
=relative
)
24 raise OptionValueError("Unknown file '" + value
+ "'")
26 splitfile
= file.split('\n')
28 if len(splitfile
) > 1:
29 raise OptionValueError("Please specify only one file, '" + value
+ "' matches more than one file")
33 def _check_commit(option
, opt
, value
):
34 """Checks whether value is a valid rev and returns its hash
38 rev
= git
.rev_parse("--verify", value
, with_exceptions
=False)
41 raise OptionValueError("Unknown commit '" + value
+ "'")
45 class GitOption(Option
):
46 """This parser understands a new type, "commit"
49 TYPES
= Option
.TYPES
+ ("commit",) + ("file",)
50 TYPE_CHECKER
= copy
.copy(Option
.TYPE_CHECKER
)
51 TYPE_CHECKER
["commit"] = _check_commit
52 TYPE_CHECKER
["file"] = _check_file