6 from optparse
import OptionParser
7 from git_python
import Git
9 from git_stats
import commit
12 """Simple storage class containing stats on the activity in one file."""
21 return str(self
.count
) + ": +" + str(self
.added
) + " -" + str(self
.deleted
)
23 def activityInArea(log
, filterid
):
24 """Parses the specified file containing commit logs.
25 The output is filtered to contain only the results of the specified author.
26 The output is expected to be in the format described below:
29 [<lines added>\t<lines deleted>\t<path>]+
34 log: The log formatted as described above.
35 filterid: The id address to filter on.
38 A dictionary containing activity per path is returned.
39 Each path contains one Activity object with the aggregated results.
42 # Create a dict to store the activity stats per path
45 # Create a place to store the result in
50 # Parse all the lines in the file
54 # Split the line at the tab and store the data
55 splitline
= line
.split('\t')
57 length
= len(line
.lstrip())
59 # There is something on this line, but it contains no separator
60 if size
== 1 and length
> 0:
61 # Get the id address minus the newline
62 activity
.id.append(line
[:-1])
65 addpart
= splitline
[0]
66 deletepart
= splitline
[1]
74 activity
.added
= int(addpart
)
75 activity
.deleted
= int(deletepart
)
77 print("On line '" + str(i
) + "', could not convert number: " + str(e
))
79 path
= splitline
[2][:-1]
81 if not filterid
in activity
.id:
82 # Ignore other authors
87 # If we already encountered this path, update the count
88 if activityByPath
.has_key(path
):
89 known
= activityByPath
[path
]
90 result
.added
= known
.added
+ activity
.added
91 result
.deleted
= known
.deleted
+ activity
.deleted
92 result
.count
= known
.count
+ activity
.count
96 # Store it under it's path
97 activityByPath
[path
] = result
99 # Create a fresh activity to store the next round in
100 activity
= Activity()
102 print("Cannot parse line " + str(i
) + ".")
105 return activityByPath
107 def activity(id, field
, startFrom
):
108 """Shows the activity for the specified developer in the current repo.
111 id: The id of the developer, currently only e-mail address is accepted.
112 field: The field to filter on.
113 startfrom: The commit to start logging from.
117 result
= git
.log("--numstat", "--pretty=format:%" + field
, startFrom
)
119 log
= result
.splitlines(True)
120 activity
= activityInArea(log
, id)
124 for key
, value
in activity
.iteritems():
125 result
.append(str(key
) + " = " + str(value
))
129 def aggregateActivity(idFilter
, field
, startFrom
):
130 """Aggregates the activity for all developers
133 idFilter: The id to filter on, if None all developers will be shown.
134 field: The field to filter on.
144 """Dispatches author related commands
147 progname
= os
.path
.basename(sys
.argv
[0]) + " author"
149 parser
= OptionParser(option_class
=commit
.CommitOption
, prog
=progname
)
154 help="aggregate the results")
158 help="the id to filter on")
162 help="the one/two letter identifier specifying which field to filter on")
165 "-s", "--start-from",
168 help="the commit to start logging from")
170 parser
.set_default("id", "ae")
171 parser
.set_default("start_from", "HEAD")
173 (options
, args
) = parser
.parse_args(list(args
))
175 if not options
.aggregate
and not options
.developer
:
176 parser
.error("When not specifying a developer the -a switch is required")
178 if options
.aggregate
:
179 result
= aggregateActivity(options
.developer
, options
.id, options
.start_from
)
181 result
= activity(options
.developer
, options
.id, options
.start_from
)