forked from lolli42/Repo-Activity-Monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StatGenerator.php
executable file
·245 lines (223 loc) · 6.4 KB
/
StatGenerator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
class StatGenerator {
protected $dataDir;
protected $baseDir;
protected $maxMonths;
protected $persons;
protected $commitCounts;
public function __construct($dataDir, $baseDir, $after) {
$this->dataDir = $dataDir;
$this->baseDir = $baseDir;
$this->maxMonths = $this->getMonthDiff($after);
}
protected function getMonthDiff($after) {
$diff = date_diff(new DateTime($after), new DateTime());
return $diff->format('%y') * 12 + $diff->format('%m');
}
public function getMaxMonths() {
return $this->maxMonths;
}
/**
* Get the numbers together
*
* @param $modules
* @return array
*/
public function generateData($modules) {
$i = 0;
list($persons, $commitCounts) = $this->findData();
$now = strtotime("first day of this month");
do {
$index = strftime('%Y.%m', strtotime('-' . $i . ' months', $now));
if (isset($persons[$index])) { // assume that all "older" data was in the store
break;
}
$persons[$index] = array();
$count = 0;
foreach ($modules as $module) {
chdir($this->baseDir . $module);
$commits = $this->getCommitsForMonth(strtotime('-' . $i . ' months', $now));
$count += $this->buildStatForCommits($persons[$index], $commits);
unset($persons[$index]['']);
unset($persons[$index]['WWW']);
}
asort($persons[$index]);
$persons[$index] = array_reverse($persons[$index]);
$commitCounts[$index] = $count;
chdir($this->baseDir);
} while (++$i < $this->maxMonths);
krsort($persons);
// slice to make sure that the current month is not cached
$this->storeData(array(array_slice($persons, 1), array_slice($commitCounts, 1)));
return array($persons, $commitCounts);
}
/**
* Get all commits (one per line) for the given month
*
* @int $timestamp A timestamp within the month
* @return array
*/
protected function getCommitsForMonth($timestamp) {
if (strftime('%B %Y', $timestamp) == strftime('%B %Y', time())) {
$before = time();
} else {
$before = strtotime('last day of ' . strftime('%B %Y', $timestamp)) + 1;
}
$after = strtotime('first day of ' . strftime('%B %Y', $timestamp)) - 1;
return $this->getCommitsForTimeRange($after, $before, true);
}
protected function getCommitsForTimeRange($after, $before, $oneline = false) {
$commits = array();
$cmd = 'git log --after="' . strftime('%Y-%m-%d', $after) . '" --before="' . strftime('%Y-%m-%d', $before) . ($oneline ? '" --oneline' : '');
@exec($cmd, $commits);
return $commits;
}
/**
* Get the log for a single commit and get the related scores in place
*
* @param $persons
* @param $commits
* @return int
*/
protected function buildStatForCommits(&$persons, $commits) {
$count = 0;
$cmd = 'git log -n 1 --pretty ';
foreach ($commits as $commit) {
$hash = substr($commit, 0, strpos($commit, ' '));
$result = array();
@exec($cmd . $hash, $result);
$this->buildScoreForCommit($result, $persons);
$count++;
}
return $count;
}
/**
* Building the score for a single commit message
*
* @param $input
* @param $persons
*/
protected function buildScoreForCommit($input, &$persons) {
$persons[$this->getAuthor($input)] += 10;
$this->processPersons('/Tested-by: ([^<]*)/', $input, function($name,$patch) use(&$persons) {
$persons[$name] += 3;
});
$this->processPersons('/Reviewed-by: ([^<]*)/', $input, function($name,$patch) use(&$persons) {
$persons[$name] += 1;
});
}
/**
* Extract the name (with the given regex) from the input
*
* @param $regEx
* @param $input
* @return bool|mixed
*/
protected function getName($regEx, $input) {
$match = array();
preg_match($regEx, $input, $match);
return isset($match[1]) ? $this->mapName(trim($match[1])) : FALSE;
}
/**
* used to clean up names to have a unique appearance
*
* @param $name
* @return mixed
*/
protected function mapName($name) {
$names = array(
'cybercraft' => 'Jo Hasenau',
'etobi.de' => 'Tobias Liebig',
'xperseguers' => 'Xavier Perseguers',
'ohader' => 'Oliver Hader',
'francois' => 'Francois Suter',
'steffenk' => 'Steffen Kamper',
'psychomieze' => 'Susanne Moog',
'tolleiv' => 'Tolleiv Nietsch',
'jigal' => 'Jigal van Hemert',
'sgalinsk' => 'Stefan Galinski',
'lolli' => 'Christian Kuhn',
'sonne' => 'Sonja Scholz',
'baschny' => 'Ernesto Baschny',
'stan' => 'Stanislas Rolland',
'mbresch' => 'Marco Bresch',
'Tolleiv' => 'Tolleiv Nietsch',
'stephenking' => 'Steffen Gebert',
'Steffen Gebert and the T3UXW09 team' => 'Steffen Gebert',
'Benni Mack' => 'Benjamin Mack',
'nxpthx' => "Steffen Ritter",
"Björn Pedersen" => "Bjoern Pedersen"
);
$leName = isset($names[$name]) ? $names[$name] : $name;
return preg_replace('/,.*/', '', $leName);
}
/**
* Get any lines from the input block if they match the regex
* @param $regEx
* @param $input
* @return array
*/
protected function getMatchingLines($regEx, $input) {
$lines = array();
foreach ($input as $line) {
if (preg_match($regEx, $line)) {
$lines[] = $line;
}
}
return $lines;
}
/**
* Use the author or "thanks to" annotation
*
* @param $input
* @return bool|mixed
*/
protected function getAuthor($input) {
$lines = $this->getMatchingLines('/\(Thanks to.+\)/', $input);
if (!empty($lines)) {
$name = $this->getName('/Thanks to ([^<\)]*)/', $lines[0]);
$parts = explode(' and ', str_replace(array('/', '&'), array('and', 'and'), $name));
$name = $parts[0]; // Sorry have to take one
} else {
$lines = $this->getMatchingLines('/^Author:.*$/', $input);
$name = $this->getName('/Author: ([^<]*)/', $lines[0]);
}
return $name;
}
/**
* Find all people matching a specific contribution $regex
*
* @param $regex
* @param $input
* @param $callback
*/
protected function processPersons($regex, $input, $callback) {
$lines = $this->getMatchingLines($regex, $input);
foreach ($lines as $line) {
$callback($this->getName($regex, $line), $input[4]);
}
}
/**
* Check if we've cached data
*
* @param $dir
* @return array|mixed
*/
protected function findData() {
if (is_file($this->dataDir . 'stat.inc')) {
include($this->dataDir . 'stat.inc');
}
return isset($data) && !empty($data) ? $data : array();
}
/**
* Cache the data
*
* @param $dir
* @param $data
*/
protected function storeData($data) {
file_put_contents($this->dataDir . 'stat.inc',
sprintf('<?php $data = unserialize(\'%s\');', str_replace("'", "\\'", serialize($data)))
);
}
}