gitstats: Added with_exceptions=False to 'git rev-parse --verify'
[git-stats.git] / src / git_stats / parse.py
blobbaa802b8e42b6aca386e4e646dacb0d0761be091
1 #!/usr/bin/env python
3 import copy
5 from git import Git
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
10 """
12 return check_file(value)
15 def check_file(value, relative=False):
16 """Check whether value is a valid file and returns it
17 """
19 git = Git(".")
21 file = git.ls_files("--full-name", "--", value, with_keep_cwd=relative)
23 if not file:
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")
31 return splitfile[0]
33 def _check_commit(option, opt, value):
34 """Checks whether value is a valid rev and returns its hash
35 """
37 git = Git(".")
38 rev = git.rev_parse("--verify", value, with_exceptions=False)
40 if not rev:
41 raise OptionValueError("Unknown commit '" + value + "'")
43 return rev
45 class GitOption(Option):
46 """This parser understands a new type, "commit"
47 """
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