gitstats: Made use of the new config parser in bug.py
[git-stats.git] / src / git_stats / bug.py
blob89f669decb07cb517421e7ddde968578dfd65435
1 #!/usr/bin/env python
3 import os
4 import sys
6 from optparse import OptionParser, OptionValueError
7 from git import Repo
9 from git_stats import branch
10 from git_stats import commit
11 from git_stats import config
12 from git_stats import parse
14 def aggregateType(debug):
15 """
16 """
18 result = []
20 # TODO, implement
22 return result
24 def determineType(target,
25 debug=False,
26 branchFilter=None,
27 msgFilter=None,
28 diffFilter=None):
29 """Determines the type of the specified commit
31 Args:
32 commit: The commit to determine the type of.
33 debug: Whether to include debug information.
34 branchFilter: Type=fix if a commit belongs to any of these branches.
35 msgFilter: Type=fix if the commit log msg matches this filter.
36 diffFilter: Type=fix if the commit diff matches this filter.
37 """
39 branches = branch.belongsTo(target)
40 reverts = commit.findReverts(target)
41 msg_matches = msgFilter and commit.commitmsgMatches(target, msgFilter)
42 diff_matches = diffFilter and commit.commitdiffMatches(target, diffFilter)
44 result = []
46 if branchFilter in branches:
47 result.append("In maintenance branch!")
49 if reverts:
50 result.append("Reverts a previous commit!")
52 if msg_matches:
53 result.append("Message matches: '" + msgFilter + "'.")
55 if diff_matches:
56 result.append("Diff matches: '" + diffFilter + "'.")
58 return result
60 def _checkOptions(parser, options):
61 """Checks the specified options and uses the parser to indicate errors
63 Args:
64 parser: The parser to use to signal when the options are bad.
65 options: The options to check.
66 """
68 opts = [options.aggregate, options.type]
70 if not parse.isUnique(opts):
71 parser.error("Please choose exactly one mode")
73 def dispatch(*args):
74 """Dispatches index related commands
75 """
77 progname = os.path.basename(sys.argv[0]) + " bug"
79 parser = OptionParser(option_class=parse.GitOption, prog=progname)
81 parser.add_option(
82 "-d", "--debug",
83 action="store_true",
84 help="show debug information")
86 parser.add_option(
87 "-a", "--aggregate",
88 action="store_true",
89 help="aggregate bug information of all commits")
91 parser.add_option(
92 "-t", "--type",
93 type="commit",
94 help="shows which type the specified commit is")
96 parser.add_option(
97 "-m", "--message-filter",
98 help="mark the commit as a fix if it's log matches this filter")
100 parser.add_option(
101 "-f", "--diff-filter",
102 help="mark the commit as a fix if it's diff matches this filter")
104 parser.add_option(
105 "-b", "--branch-filter",
106 help="mark the commit as a fix if it belongs to this branch")
108 # Default to True for now, until there is another option
109 parser.set_default("debug", False)
110 parser.set_default("aggregate", False)
112 (options, args) = parser.parse_args(list(args))
114 _checkOptions(parser, options)
116 if options.aggregate:
117 result = aggregateType(options.debug)
118 elif options.type:
119 opts = config.read()
120 result = determineType(
121 options.type,
122 debug=options.debug or opts.get("debug", False),
123 msgFilter=options.message_filter or opts["msgFilter"],
124 diffFilter=options.diff_filter or opts["diffFilter"],
125 branchFilter=options.branch_filter or opts["branchFilter"])
127 for line in result:
128 print(line)