gitstats: Fix some spelling mistakes.
[git-stats.git] / src / git_stats / author.py
blobd8404949080c9f162084db3b57c6f021cfc73538
1 #!/usr/bin/env python
3 from git_python import Git
5 class Activity:
6 """Simple storage class containing stats on the activity in one file."""
8 count = 1
9 added = 0
10 deleted = 0
11 id = []
13 def __str__(self):
14 return str(self.count) + ": +" + str(self.added) + " -" + str(self.deleted)
16 def activityInArea(log, filterid):
17 """Parses the specified file containing commit logs.
18 The output is filtered to contain only the results of the specified author.
19 The output is expected to be in the format described below:
21 [<id>\n]+
22 [<lines added>\t<lines deleted>\t<path>]+
26 Params:
27 log: The log formatted as described above.
28 filterid: The id address to filter on.
30 Returns:
31 A dictionary containing activity per path is returned.
32 Each path contains one Activity object with the aggregated results.
33 """
35 # Create a dict to store the activity stats per path
36 activityByPath = {}
38 # Create a place to store the result in
39 activity = Activity()
41 i = 0
43 # Parse all the lines in the file
44 for line in log:
45 i += 1
47 # Split the line at the tab and store the data
48 splitline = line.split('\t')
49 size = len(splitline)
50 length = len(line.lstrip())
52 # There is something on this line, but it contains no separator
53 if size == 1 and length > 0:
54 # Get the id address minus the newline
55 activity.id.append(line[:-1])
56 elif size == 3:
57 try:
58 addpart = splitline[0]
59 deletepart = splitline[1]
61 if addpart == '-':
62 addpart = 0
64 if deletepart == '-':
65 deletepart = 0
67 activity.added = int(addpart)
68 activity.deleted = int(deletepart)
69 except ValueError, e:
70 print("On line '" + str(i) + "', could not convert number: " + str(e))
72 path = splitline[2][:-1]
73 elif length == 0:
74 if not filterid in activity.id:
75 # Ignore other authors
76 pass
77 else:
78 result = Activity()
80 # If we already encountered this path, update the count
81 if activityByPath.has_key(path):
82 known = activityByPath[path]
83 result.added = known.added + activity.added
84 result.deleted = known.deleted + activity.deleted
85 result.count = known.count + activity.count
86 else:
87 result = activity
89 # Store it under it's path
90 activityByPath[path] = result
92 # Create a fresh activity to store the next round in
93 activity = Activity()
94 else:
95 print("Cannot parse line " + str(i) + ".")
97 # Return the result
98 return activityByPath
100 def activity(id):
101 """Shows the activity for the specified developer in the current repo.
103 Params:
104 id: The id of the developer, currently only e-mail address is accepted.
107 git = Git(".")
108 result = git.log("--numstat", "--pretty=format:%ae")
110 log = result.splitlines(True)
111 activity = activityInArea(log, id)
113 for key, value in activity.iteritems():
114 print(str(key) + " = " + str(value))
116 def dispatch(*args):
117 """Dispatches author related commands
120 pass
122 if __name__ == '__main__':
123 path = "testrepo.log"
125 # Open the log file
126 file = open(path)
128 log = file.readlines()
130 activity = activityInArea(log, "sverre@rabbelier.nl")
132 for key, value in activity.iteritems():
133 print(str(key) + " = " + str(value))