blob: 789cb051d71dbc3751d74f13e99c080f5883b09b [file] [log] [blame]
Martin Roth0ad5fbd2020-12-24 12:06:38 -07001#!/usr/bin/env perl
Martin Rothce005da2016-04-01 14:30:33 -06002#
Patrick Georgi7333a112020-05-08 20:48:04 +02003# SPDX-License-Identifier: GPL-2.0-only
Martin Rothce005da2016-04-01 14:30:33 -06004
5package gerrit_stats;
6
7# To install any needed modules install the cpanm app, and use it to install the required modules:
8# sudo cpan App::cpanminus
9# sudo /usr/local/bin/cpanm JSON::Util Net::OpenSSH DateTime Devel::Size
10
Martin Rothae7d8372022-12-10 22:00:40 -070011# perltidy -l=200 -bt=2 -ce
12
Martin Rothce005da2016-04-01 14:30:33 -060013use strict;
14use warnings;
15use English qw( -no_match_vars );
16use File::Find;
17use File::Path;
18use Getopt::Long;
19use Getopt::Std;
20use JSON::Util;
21use Net::OpenSSH;
22use Data::Dumper qw(Dumper);
23use DateTime;
24use Devel::Size qw(size total_size);
25
26my $old_version;
27my $new_version;
Martin Rothae7d8372022-12-10 22:00:40 -070028my $infodir = "$ENV{'HOME'}/.local/commit_info/" . `git config -l | grep remote.origin.url | sed 's|.*@||' | sed 's|:.*||'`;
Martin Rothce005da2016-04-01 14:30:33 -060029chomp($infodir);
30my $URL_WITH_USER;
31my $SKIP_GERRIT_CHECK;
32my $print_commit_list = 1;
33
34#disable print buffering
35$OUTPUT_AUTOFLUSH = 1;
36binmode STDOUT, ":utf8";
37
38Main();
39
40#-------------------------------------------------------------------------------
41# Main
42#-------------------------------------------------------------------------------
43sub Main {
44 check_arguments();
45
Martin Rothae7d8372022-12-10 22:00:40 -070046 my %submitters = ();
47 my %authors = ();
48 my %owners = ();
49 my %reviewers = ();
50 my %commenters = ();
51 my %author_added = ();
52 my %author_removed = ();
53 my $total_added = 0;
54 my $total_removed = 0;
55 my $number_of_commits = 0;
56 my $number_of_submitters = 0;
57 my $submit_epoch = "";
58 my $first_submit_epoch = "";
59 my $patches_over_100_lines = 0;
60 my $total_lines_large_patches = 0;
61 my %email_addresses = (
62 'Kacper Stojek' => 'kacper.stojek@3mdeb.com',
63 'Damien Zammit' => 'damien@zamaudio.com',
64 'Pavel Sayekat' => 'pavelsayekat@gmail.com',
65 'Lance Zhao' => 'lance.zhao@gmail.com',
66 );
67
68 my %aliases = (
69 ' Felix Singer' => 'Felix Singer',
70 'Abhay kumar' => 'Abhay Kumar',
71 'AlexandruX Gagniuc' => 'Alexandru Gagniuc',
72 'Anish K. Patel' => 'Anish K Patel',
73 'Bao Zheng' => 'Zheng Bao',
74 'Bernhard M. Wiedemann' => 'Bernhard M. Wiedermann',
75 'Björn Busse' => 'Bjarn Busse',
76 'BryantOu' => 'Bryant Ou',
77 'Chen Wisley' => 'Wisley Chen',
78 'Cheng-Yi Chiang' => 'Jimmy Cheng-Yi Chiang',
79 'Chris Ching (using chromium account)' => 'Chris Ching,',
80 'ChromeOS Developer' => 'Dave Parker',
81 'Cristi M' => 'Cristian Magherusan-Stanciu',
82 'Cristian M?gheru?an-Stanciu' => 'Cristian Magherusan-Stanciu',
83 'Cristian MÄgheruÈan-Stanciu' => 'Cristian Magherusan-Stanciu',
84 'Cristian MÄgheruÈan-Stanciu' => 'Cristian Magherusan-Stanciu',
85 'DAWEI CHIEN' => 'Dawei Chien',
86 'efdesign98' => 'Frank Vibrans',
87 'Eugene D. Myers' => 'Eugene Myers',
88 'Frank Vibrans III' => 'Frank Vibrans',
89 'frank vibrans' => 'Frank Vibrans',
90 'Frank.Vibrans' => 'Frank Vibrans',
91 'FrankChu' => 'Frank Chu',
92 'garmin chang' => 'Garmin Chang',
93 'Garmin.Chang' => 'Garmin Chang',
94 'hannahwilliams2' => 'Hannah Williams',
95 'HAOUAS Elyes' => 'Elyes Haouas',
96 'Harshapriya N' => 'Harsha Priya',
97 'Hsuan-ting Chen' => 'Hsuan Ting Chen',
98 'Iru Cai (vimacs)' => 'Iru Cai',
99 'Jérémy Compostella' => 'Jeremy Compostella',
100 'Jérémy Compostella' => 'Jeremy Compostella',
101 'JG Poxu' => 'Po Xu',
102 'JonathonHall-Purism' => 'Jonathon Hall',
103 'Karthikeyan Ramasubramanian' => 'Karthik Ramasubramanian',
104 'Kerry She' => 'Kerry Sheh',
105 'kewei.xu' => 'kewei xu',
106 'Kumar, Gomathi' => 'Gomathi Kumar',
107 'Kyösti Mälkki' => 'Kyösti Mälkki',
108 'Kyösti Mälkki' => 'Kyösti Mälkki',
109 'Marcello Sylvester Bauer' => 'Marcello Sylvester Bauer',
110 'Martin L Roth' => 'Martin Roth',
111 'Martin Roth - Personal' => 'Martin Roth',
112 'Matt Ziegelbaum' => 'Matthew Ziegelbaum',
113 'mengqi.zhang' => 'Mengqi Zhang',
114 'mrnuke' => 'Alexandru Gagniuc',
115 'Nina-CM Wu' => 'Nina Wu',
116 'ot_zhenguo.li' => 'Zhenguo Li',
117 'Patrick Georgi patrick.georgi' => 'Patrick Georgi',
118 'Patrick Georgi patrick' => 'Patrick Georgi',
119 'Pavlushka' => 'Pavel Sayekat',
120 'Ravi Kumar Bokka' => 'Ravi Kumar',
121 'Ravi kumar' => 'Ravi Kumar',
122 'Ravi Sarawadi' => 'Ravishankar Sarawadi',
123 'ravindr1' => 'Ravindra',
124 'Ravindra N' => 'Ravindra',
125 'Ricardo Ribalda Delgado' => 'Ricardo Ribalda',
126 'ron minnich' => 'Ron Minnich',
127 'Ronald G. Minnich' => 'Ron Minnich',
128 'samrab' => 'Sudheer Amrabadi',
129 'SANTHOSH JANARDHANA HASSAN' => 'Santhosh Janardhana Hassan',
130 'semihalf-czapiga-jakub' => 'Jakub Czapiga',
131 'Seunghwan Kim' => 'SH Kim',
132 'Sooi, Li Cheng' => 'Li Cheng Sooi',
133 'Stefan Reinauerstepan' => 'Stefan Reinauer',
134 'stepan' => 'Stefan Reinauer',
135 'Swift Geek (Sebastian Grzywna)' => 'Sebastian "Swift Geek" Grzywna',
136 'Sylvain "ythier" Hitier' => 'Sylvain Hitier',
137 'Thomas Gstädtner' => 'Thomas Gstaedtner',
138 'UwePoeche' => 'Uwe Poeche',
139 'UwePoeche' => 'Uwe Poeche',
140 'Varshit Pandya' => 'Varshit B Pandya',
141 'Wayne3 Wang' => 'Wayne Wang',
142 'Wayne3_Wang' => 'Wayne Wang',
143 'Xi Chen' => 'Xixi Chen',
144 'Yu-Hsuan Hsu' => 'Yu-hsuan Hsu',
145 'zbao' => 'Zheng Bao',
146 'Zheng Bao zheng.bao' => 'Zheng Bao',
147 'zhiyong tao' => 'Zhiyong Tao',
148 'Дмитрий Понаморев' => 'Dmitry Ponamorev',
149 );
150
151 if ( !$URL_WITH_USER ) {
152 get_user();
Martin Rothce005da2016-04-01 14:30:33 -0600153 }
154
Martin Rothae7d8372022-12-10 22:00:40 -0700155 print "Saving data to $infodir\n";
156
157 # Make sure the versions exist
Martin Rothce005da2016-04-01 14:30:33 -0600158 check_versions();
159
Martin Rothae7d8372022-12-10 22:00:40 -0700160 # Fetch patches if needed. Get ids of first and last commits
161 my @commits = `git log --pretty=%H "$old_version..$new_version" 2>/dev/null`;
Martin Rothce005da2016-04-01 14:30:33 -0600162 get_commits(@commits);
Martin Rothae7d8372022-12-10 22:00:40 -0700163 my $last_commit_id = $commits[0];
164 my $first_commit_id = $commits[ @commits - 1 ];
Martin Rothce005da2016-04-01 14:30:33 -0600165 chomp $last_commit_id;
166 chomp $first_commit_id;
167
168 print "Statistics from commit $first_commit_id to commit $last_commit_id\n";
Martin Rothae7d8372022-12-10 22:00:40 -0700169 print "Patch, Date, Owner, Author, Submitter, Inserted lines, Deleted lines, Subject, Reviewers, Commenters\n";
Martin Rothce005da2016-04-01 14:30:33 -0600170
Martin Rothae7d8372022-12-10 22:00:40 -0700171 # Loop through all commits
Martin Rothce005da2016-04-01 14:30:33 -0600172 for my $commit_id (@commits) {
173 $commit_id =~ s/^\s+|\s+$//g;
174
Martin Rothae7d8372022-12-10 22:00:40 -0700175 my $submitter = "";
176 my %patch_reviewers = ();
177 my %patch_commenters = ();
Martin Rothce005da2016-04-01 14:30:33 -0600178 my $info;
179 my $owner;
180 my $author;
181 my $author_email;
182 my $inserted_lines = 0;
Martin Rothae7d8372022-12-10 22:00:40 -0700183 my $deleted_lines = 0;
Martin Rothce005da2016-04-01 14:30:33 -0600184 my $subject;
185
186 $number_of_commits++;
187 print "\"$commit_id\", ";
188
Martin Rothae7d8372022-12-10 22:00:40 -0700189 # Read the data file for the current commit
190 if ( -f "$infodir/$commit_id" && -s "$infodir/$commit_id" > 20 ) {
Martin Rothce005da2016-04-01 14:30:33 -0600191 open( my $HANDLE, "<", "$infodir/$commit_id" ) or die "Error: could not open file '$infodir/$commit_id'\n";
192 $info = <$HANDLE>;
193 close $HANDLE;
194
195 my $commit_info = JSON::Util->decode($info);
196
Martin Rothae7d8372022-12-10 22:00:40 -0700197 # Get the easy data
Martin Rothce005da2016-04-01 14:30:33 -0600198 $owner = $commit_info->{'owner'}{'name'};
Martin Rothae7d8372022-12-10 22:00:40 -0700199 if ( !$owner ) {
Martin Rothce005da2016-04-01 14:30:33 -0600200 $owner = $commit_info->{'owner'}{'username'};
201 }
Martin Rothae7d8372022-12-10 22:00:40 -0700202 if ( !$owner ) {
203 $owner = "-";
Martin Rothce005da2016-04-01 14:30:33 -0600204 }
Martin Rothae7d8372022-12-10 22:00:40 -0700205 if ( $owner && exists( $aliases{$owner} ) ) {
206 $owner = $aliases{$owner};
Martin Rothce005da2016-04-01 14:30:33 -0600207 }
208
Martin Rothae7d8372022-12-10 22:00:40 -0700209 $author = $commit_info->{'currentPatchSet'}{'author'}{'name'};
210 if ( $author && exists( $aliases{$author} ) ) {
211 $author = $aliases{$author};
212 }
213 if ( !$author ) {
214 $author = $commit_info->{'currentPatchSet'}{'author'}{'username'};
215 }
216 $author_email = $commit_info->{'currentPatchSet'}{'author'}{'email'};
Martin Rothce005da2016-04-01 14:30:33 -0600217 $inserted_lines = $commit_info->{'currentPatchSet'}{'sizeInsertions'};
Martin Rothae7d8372022-12-10 22:00:40 -0700218 $deleted_lines = $commit_info->{'currentPatchSet'}{'sizeDeletions'};
219 $subject = $commit_info->{'subject'};
Martin Rothce005da2016-04-01 14:30:33 -0600220
221 #get the patch's submitter
222 my $approvals = $commit_info->{'currentPatchSet'}{'approvals'};
223 for my $approval (@$approvals) {
Martin Rothae7d8372022-12-10 22:00:40 -0700224 if ( $approval->{'type'} eq "SUBM" ) {
Martin Rothce005da2016-04-01 14:30:33 -0600225 $submit_epoch = $approval->{'grantedOn'};
Martin Rothae7d8372022-12-10 22:00:40 -0700226 $submitter = $approval->{'by'}{'name'};
227 if ( exists( $aliases{$submitter} ) ) {
228 $submitter = $aliases{$submitter};
229 }
Martin Rothce005da2016-04-01 14:30:33 -0600230 }
231 }
232
Martin Rothae7d8372022-12-10 22:00:40 -0700233 # Get all the commenters for all patch revisions
234 my $comments = $commit_info->{'comments'};
235 for my $comment (@$comments) {
236 my $commenter;
237 if ( $comment->{'reviewer'}{'username'} ) {
238 if ( $comment->{'reviewer'}{'username'} eq "jenkins" ) {
239 next;
240 }
241 if ( $comment->{'reviewer'}{'username'} eq "hardwaretestrobot" ) {
242 next;
243 }
244 if ( $comment->{'reviewer'}{'username'} eq "raptor-automated-test" ) {
245 next;
246 }
247 }
248
249 if ( $comment->{'reviewer'}{'name'} ) {
250 if ( $comment->{'reviewer'}{'name'} eq "Gerrit Code Review" ) {
251 next;
252 }
253 }
254 if ( $comment->{'message'} ) {
255 if ( $comment->{'message'} =~ "successfully cherry-picked" ) {
256 next;
257 }
258 if ( $comment->{'message'} =~ ": Code-Review" ) {
259 next;
260 }
261 if ( $comment->{'message'} =~ "Uploaded patch set" ) {
262 next;
263 }
264 }
265
266 if ( !$commenter ) {
267 $commenter = $comment->{'reviewer'}{'name'};
268 if ( $commenter && exists( $aliases{$commenter} ) ) {
269 $commenter = $aliases{$commenter};
270 }
271 }
272 if ( !$commenter ) {
273 $commenter = $comment->{'reviewer'}{'username'};
274 if ( $commenter && exists( $aliases{$commenter} ) ) {
275 $commenter = $aliases{$commenter};
276 }
277 }
278 if ( $commenter && $author && $commenter eq $author ) {
279 next;
280 }
281 if ($commenter) {
282 if ( $commenter && exists $patch_commenters{$commenter} ) {
283 $patch_commenters{$commenter}++;
284 } else {
285 $patch_commenters{$commenter} = 1;
286 }
287 }
288 }
289
290 # Get all the reviewers for all patch revisions
Martin Rothce005da2016-04-01 14:30:33 -0600291 my $patchsets = $commit_info->{'patchSets'};
292 for my $patch (@$patchsets) {
Martin Rothae7d8372022-12-10 22:00:40 -0700293 if ( !$author ) {
Martin Rothce005da2016-04-01 14:30:33 -0600294 $author = $patch->{'author'}{'name'};
Martin Rothae7d8372022-12-10 22:00:40 -0700295 if ( $author && exists( $aliases{$author} ) ) {
296 $author = $aliases{$author};
297 }
Martin Rothce005da2016-04-01 14:30:33 -0600298 }
Martin Rothae7d8372022-12-10 22:00:40 -0700299
Martin Rothce005da2016-04-01 14:30:33 -0600300 my $approvals = $patch->{'approvals'};
301 for my $approval (@$approvals) {
302
Martin Rothae7d8372022-12-10 22:00:40 -0700303 if ( ( !$submitter ) && ( $approval->{'type'} eq "SUBM" ) ) {
Martin Rothce005da2016-04-01 14:30:33 -0600304 $submit_epoch = $approval->{'grantedOn'};
Martin Rothae7d8372022-12-10 22:00:40 -0700305 $submitter = $approval->{'by'}{'name'};
306 if ( $submitter && exists( $aliases{$submitter} ) ) {
307 $submitter = $aliases{$submitter};
308 }
Martin Rothce005da2016-04-01 14:30:33 -0600309 }
310
Martin Rothae7d8372022-12-10 22:00:40 -0700311 if ( $approval->{'type'} eq "Code-Review" ) {
Martin Rothce005da2016-04-01 14:30:33 -0600312 my $patch_reviewer = $approval->{'by'}{'name'};
313 if ($patch_reviewer) {
Martin Rothae7d8372022-12-10 22:00:40 -0700314 if ( exists $patch_reviewers{$patch_reviewer} ) {
Martin Rothce005da2016-04-01 14:30:33 -0600315 $patch_reviewers{$patch_reviewer}++;
316 } else {
317 $patch_reviewers{$patch_reviewer} = 1;
318 }
319 }
320 }
321 }
322 }
323
324 } else {
Martin Rothae7d8372022-12-10 22:00:40 -0700325
326 # Get the info from git
Martin Rothce005da2016-04-01 14:30:33 -0600327 my $logline = `git log --pretty="%ct@@@%s@@@%an@@@%aE@@@%cn" $commit_id^..$commit_id --`;
328 $logline =~ m/^(.*)@@@(.*)@@@(.*)@@@(.*)@@@(.*)\n/;
Martin Rothae7d8372022-12-10 22:00:40 -0700329 ( $submit_epoch, $subject, $author, $author_email, $submitter ) = ( $1, $2, $3, $4, $5 );
330 if ( exists( $aliases{$author} ) ) {
331 $author = $aliases{$author};
332 }
Martin Rothce005da2016-04-01 14:30:33 -0600333 $owner = $author;
Martin Rothae7d8372022-12-10 22:00:40 -0700334
335 if ( $submitter && exists( $aliases{$submitter} ) ) {
336 $submitter = $aliases{$submitter};
337 }
338
Martin Rothce005da2016-04-01 14:30:33 -0600339 $logline = `git log --pretty= --shortstat $commit_id^..$commit_id --`;
Martin Rothae7d8372022-12-10 22:00:40 -0700340 if ( $logline =~ m/\s+(\d+)\s+insertion/ ) {
Martin Rothce005da2016-04-01 14:30:33 -0600341 $inserted_lines = $1;
342 }
Martin Rothae7d8372022-12-10 22:00:40 -0700343 if ( $logline =~ m/\s+(\d+)\s+deletion/ ) {
Martin Rothce005da2016-04-01 14:30:33 -0600344 $deleted_lines = $1 * -1;
345 }
346 my @loglines = `git log $commit_id^..$commit_id -- | grep '\\sReviewed-by:'`;
Martin Rothae7d8372022-12-10 22:00:40 -0700347 for my $line (@loglines) {
348 if ( $line =~ m/.*:\s+(.*)\s</ ) {
Martin Rothce005da2016-04-01 14:30:33 -0600349 my $patch_reviewer = $1;
Martin Rothae7d8372022-12-10 22:00:40 -0700350 if ( exists( $aliases{$patch_reviewer} ) ) {
351 $patch_reviewer = $aliases{$patch_reviewer};
352 }
Martin Rothce005da2016-04-01 14:30:33 -0600353 if ($patch_reviewer) {
Martin Rothae7d8372022-12-10 22:00:40 -0700354 if ( exists $patch_reviewers{$patch_reviewer} ) {
Martin Rothce005da2016-04-01 14:30:33 -0600355 $patch_reviewers{$patch_reviewer}++;
356 } else {
357 $patch_reviewers{$patch_reviewer} = 1;
358 }
359 }
360 }
361 }
362
363 }
364
365 # Not entirely certain why this is needed, but for a number of patches have been submitted
366 # the submit time in gerrit is set to April 9, 2015.
Martin Rothae7d8372022-12-10 22:00:40 -0700367 if ( $submit_epoch == 1428586219 ) {
Martin Rothce005da2016-04-01 14:30:33 -0600368 my $logline = `git log --pretty="%ct" $commit_id^..$commit_id --`;
369 $logline =~ m/^(.*)\n/;
370 $submit_epoch = $1;
371 }
372
Martin Rothae7d8372022-12-10 22:00:40 -0700373 # Add the count and owner to the submitter hash
374 if ( $submitter && exists $submitters{$submitter} && exists $submitters{$submitter}{count} ) {
375 $submitters{$submitter}{count}++;
Martin Rothce005da2016-04-01 14:30:33 -0600376 } else {
Martin Rothae7d8372022-12-10 22:00:40 -0700377 $submitters{$submitter}{count} = 1;
Martin Rothce005da2016-04-01 14:30:33 -0600378 $number_of_submitters++;
Martin Rothae7d8372022-12-10 22:00:40 -0700379 $submitters{$submitter}{"self"} = 0;
380 $submitters{$submitter}{others} = 0;
381 $submitters{$submitter}{name} = $submitter;
Martin Rothce005da2016-04-01 14:30:33 -0600382 }
383
Martin Rothae7d8372022-12-10 22:00:40 -0700384 if ( $submitter eq $author ) {
385 $submitters{$submitter}{"self"}++;
386 } else {
387 $submitters{$submitter}{others}++;
388 }
389
390 # Create a readable date
391 my $dt = DateTime->from_epoch( epoch => $submit_epoch );
392 $dt->set_time_zone('Europe/Paris');
393 my $submit_time = $dt->strftime('%Y/%m/%d');
394 if ( !$first_submit_epoch ) {
Martin Rothce005da2016-04-01 14:30:33 -0600395 $first_submit_epoch = $submit_epoch;
396 }
397
Martin Rothae7d8372022-12-10 22:00:40 -0700398 # Create the list of commenters to print
399 my $commenterlist = "";
400 foreach my $commenter ( keys %patch_commenters ) {
401 if ( $commenter && exists( $aliases{$commenter} ) ) {
402 $commenter = $aliases{$commenter};
403 }
404
405 if ( $commenterlist eq "" ) {
406 $commenterlist = $commenter;
407 } else {
408 $commenterlist .= ", $commenter";
409 }
410
411 if ( $commenter && exists $commenters{$commenter} ) {
412 $commenters{$commenter}++;
413 } else {
414 $commenters{$commenter} = 1;
415 }
416 }
417 if ( !$commenterlist ) {
418 $commenterlist = "-";
419 }
420
421 # Create the list of reviewers to print
Martin Rothce005da2016-04-01 14:30:33 -0600422 my $reviewerlist = "";
Martin Rothae7d8372022-12-10 22:00:40 -0700423 foreach my $reviewer ( keys %patch_reviewers ) {
424 if ( exists( $aliases{$reviewer} ) ) {
425 $reviewer = $aliases{$reviewer};
426 }
427
428 if ( $reviewerlist eq "" ) {
Martin Rothce005da2016-04-01 14:30:33 -0600429 $reviewerlist = $reviewer;
430 } else {
431 $reviewerlist .= ", $reviewer";
432 }
433
Martin Rothae7d8372022-12-10 22:00:40 -0700434 if ( $reviewer && exists $reviewers{$reviewer} ) {
Martin Rothce005da2016-04-01 14:30:33 -0600435 $reviewers{$reviewer}++;
436 } else {
437 $reviewers{$reviewer} = 1;
438 }
439 }
Martin Rothae7d8372022-12-10 22:00:40 -0700440 if ( !$reviewerlist ) {
441 $reviewerlist = "-";
Martin Rothce005da2016-04-01 14:30:33 -0600442 }
443
444 if ($print_commit_list) {
Martin Rothae7d8372022-12-10 22:00:40 -0700445 print "$submit_time, $owner, $author, $submitter, $inserted_lines, $deleted_lines, \"$subject\", \"$reviewerlist\" , \"$commenterlist\"\n";
Martin Rothce005da2016-04-01 14:30:33 -0600446 } else {
447 print "$number_of_commits\n";
448 }
449 $total_added += $inserted_lines;
Martin Rothae7d8372022-12-10 22:00:40 -0700450 if ( $inserted_lines - $deleted_lines > 100 ) {
451 $patches_over_100_lines++;
452 $total_lines_large_patches += $inserted_lines;
453 }
Martin Rothce005da2016-04-01 14:30:33 -0600454 $total_removed += $deleted_lines;
Martin Rothae7d8372022-12-10 22:00:40 -0700455 if ( exists $owners{$owner} ) {
Martin Rothce005da2016-04-01 14:30:33 -0600456 $owners{$owner}++;
457 } else {
458 $owners{$owner} = 1;
459 }
460
Martin Rothae7d8372022-12-10 22:00:40 -0700461 if ( $author && exists $authors{$author}{"num"} ) {
Martin Rothce005da2016-04-01 14:30:33 -0600462 $authors{$author}{"num"}++;
Martin Rothae7d8372022-12-10 22:00:40 -0700463 $author_added{$author} += $inserted_lines;
Martin Rothce005da2016-04-01 14:30:33 -0600464 $author_removed{$author} += $deleted_lines;
Martin Rothae7d8372022-12-10 22:00:40 -0700465 $authors{$author}{"earliest_commit"} = $submit_time;
Martin Rothce005da2016-04-01 14:30:33 -0600466 } else {
Martin Rothae7d8372022-12-10 22:00:40 -0700467 $authors{$author}{"num"} = 1;
468 $authors{$author}{"latest_commit"} = $submit_time;
469 $authors{$author}{"earliest_commit"} = $submit_time;
470 $author_added{$author} = $inserted_lines;
471 $author_removed{$author} = $deleted_lines;
Martin Rothce005da2016-04-01 14:30:33 -0600472 }
Martin Rothae7d8372022-12-10 22:00:40 -0700473 if ( $author && ( !exists $authors{$author}{email} || $authors{$author}{email} eq "-" ) ) {
474 if ($author_email) {
475 $authors{$author}{email} = "$author_email";
476 } elsif ( exists $email_addresses{$author} ) {
477 $authors{$author}{email} = $email_addresses{$author};
478 }
Martin Rothce005da2016-04-01 14:30:33 -0600479 }
480 }
Martin Rothae7d8372022-12-10 22:00:40 -0700481 my $Days = ( $first_submit_epoch - $submit_epoch ) / 86400;
482 if ( ( $first_submit_epoch - $submit_epoch ) % 86400 ) {
Martin Rothce005da2016-04-01 14:30:33 -0600483 $Days += 1;
484 }
485
Martin Rothae7d8372022-12-10 22:00:40 -0700486 print "\n- Total Commits: $number_of_commits\n";
Martin Rothce005da2016-04-01 14:30:33 -0600487 printf "- Average Commits per day: %.2f\n", $number_of_commits / $Days;
488 print "- Total lines added: $total_added\n";
Martin Rothae7d8372022-12-10 22:00:40 -0700489 printf "- Average lines added per commit: %.2f\n", $total_added / $number_of_commits;
490 print "- Number of patches adding more than 100 lines: $patches_over_100_lines\n";
491 printf "- Average lines added per small commit: %.2f\n", ( $total_added - $total_lines_large_patches ) / ( $number_of_commits - $patches_over_100_lines );
492
Martin Rothce005da2016-04-01 14:30:33 -0600493 print "- Total lines removed: $total_removed\n";
Martin Rothae7d8372022-12-10 22:00:40 -0700494 printf "- Average lines removed per commit: %.2f\n", $total_removed / $number_of_commits;
495 print "- Total difference between added and removed: " . ( $total_added - $total_removed ) . "\n\n";
Martin Rothce005da2016-04-01 14:30:33 -0600496
497 print "=== Authors - Number of commits ===\n";
Martin Rothae7d8372022-12-10 22:00:40 -0700498 printf "%-30s ,%5s ,%5s ,%6s ,%6s , %-52s ,%6s, %-19s , %s\n", "Author", "Ptchs", "Revws", "Cmnts", "Sbmts", "Email", "Prcnt", "Last commit", "Earliest_commit";
499
Martin Rothce005da2016-04-01 14:30:33 -0600500 my $number_of_authors = 0;
Martin Rothae7d8372022-12-10 22:00:40 -0700501 foreach my $author ( sort { $authors{$b}{num} <=> $authors{$a}{num} } ( keys %authors ) ) {
502 my $submissions = 0;
503 if ( $author && exists $submitters{$author} ) {
504 $submissions = $submitters{$author}{count};
505 }
506 my $review_count = 0;
507 if ( $author && exists $reviewers{$author} ) {
508 $review_count = $reviewers{$author};
509 }
510
511 my $comment_count = 0;
512 if ( $author && exists $commenters{$author} ) {
513 $comment_count = $commenters{$author};
514 }
515
516 if ( $author && !exists $authors{$author}{"email"} ) {
517 $authors{$author}{"email"} = "-";
518 }
519
520 printf "%-30s ,%5d ,%5d ,%6d ,%6d , %-52s ,%5.2f%%, %s , %s\n", $author, $authors{$author}{"num"}, $review_count,
521 $comment_count, $submissions, $authors{$author}{"email"}, $authors{$author}{"num"} / $number_of_commits * 100,
522 $authors{$author}{"latest_commit"}, $authors{$author}{"earliest_commit"};
Martin Rothce005da2016-04-01 14:30:33 -0600523 $number_of_authors++;
524 }
525 print "Total Authors: $number_of_authors\n\n";
526
527 print "=== Authors - Lines added ===\n";
Martin Rothae7d8372022-12-10 22:00:40 -0700528 foreach my $author ( sort { $author_added{$b} <=> $author_added{$a} } ( keys %author_added ) ) {
529 if ( $author_added{$author} ) {
530 printf "%-30s, %10d, %2.3f%%\n", $author, $author_added{$author}, $author_added{$author} / $total_added * 100;
Martin Rothce005da2016-04-01 14:30:33 -0600531 }
532 }
533 print "\n";
534
535 print "=== Authors - Lines removed ===\n";
Martin Rothae7d8372022-12-10 22:00:40 -0700536 foreach my $author ( sort { $author_removed{$b} <=> $author_removed{$a} } ( keys %author_removed ) ) {
537 if ( $author_removed{$author} ) {
538 printf "%-30s, %10d, %6.3f%%\n", $author, $author_removed{$author} * -1, $author_removed{$author} / $total_removed * 100;
Martin Rothce005da2016-04-01 14:30:33 -0600539 }
540 }
541 print "\n";
542
543 print "=== Reviewers - Number of patches reviewed ===\n";
544 my $number_of_reviewers = 0;
Martin Rothae7d8372022-12-10 22:00:40 -0700545 foreach my $reviewer ( sort { $reviewers{$b} <=> $reviewers{$a} } ( keys %reviewers ) ) {
546 printf "%-30s, %6d, %6.3f%%\n", $reviewer, $reviewers{$reviewer}, $reviewers{$reviewer} / $number_of_commits * 100;
Martin Rothce005da2016-04-01 14:30:33 -0600547 $number_of_reviewers++;
548 }
549 print "Total Reviewers: $number_of_reviewers\n\n";
550
551 print "=== Submitters - Number of patches submitted ===\n";
Martin Rothae7d8372022-12-10 22:00:40 -0700552 printf "%-30s, %6s, %7s, %6s, %7s, %6s, %7s\n", "Name", "#", "total%", "Own", "own%", "Other", "other%";
553 foreach my $submitter ( sort { $submitters{$b}{count} <=> $submitters{$a}{count} } ( keys %submitters ) ) {
554 printf "%-30s, % 6d, %6.3f%%, %6d, %6.2f%%, %6d, %6.2f%%\n",
555 $submitter,
556 $submitters{$submitter}{count},
557 $submitters{$submitter}{count} / $number_of_commits * 100,
558 $submitters{$submitter}{"self"},
559 $submitters{$submitter}{"self"} / $submitters{$submitter}{count} * 100,
560 $submitters{$submitter}{others}, $submitters{$submitter}{others} / $submitters{$submitter}{count} * 100;
Martin Rothce005da2016-04-01 14:30:33 -0600561 }
562 print "Total Submitters: $number_of_submitters\n\n";
563
564 print "Commits, Ave, Added, Removed, Diff, Authors, Reviewers, Submitters\n";
Martin Rothae7d8372022-12-10 22:00:40 -0700565 printf "$number_of_commits, %.2f, $total_added, $total_removed, " . ( $total_added + $total_removed ) . ", $number_of_authors, $number_of_reviewers, $number_of_submitters\n",
566 $number_of_commits / $Days;
Martin Rothce005da2016-04-01 14:30:33 -0600567}
568
569#-------------------------------------------------------------------------------
570#-------------------------------------------------------------------------------
571sub check_versions {
572 `git cat-file -e $old_version^{commit} 2>/dev/null`;
Martin Rothae7d8372022-12-10 22:00:40 -0700573 if ( ${^CHILD_ERROR_NATIVE} ) {
Martin Rothce005da2016-04-01 14:30:33 -0600574 print "Error: Old version ($old_version) does not exist.\n";
575 exit 1;
576 }
577
578 `git cat-file -e $new_version^{commit} 2>/dev/null`;
Martin Rothae7d8372022-12-10 22:00:40 -0700579 if ( ${^CHILD_ERROR_NATIVE} ) {
Martin Rothce005da2016-04-01 14:30:33 -0600580 print "Error: New version ($new_version) does not exist.\n";
581 exit 1;
582 }
583}
584
585#-------------------------------------------------------------------------------
Martin Rothce005da2016-04-01 14:30:33 -0600586sub get_user {
Martin Rothce005da2016-04-01 14:30:33 -0600587
Martin Rothae7d8372022-12-10 22:00:40 -0700588 my $url = `git config -l | grep remote.origin.url`;
589
590 if ( $url =~ /.*url=ssh:\/\/(\w+@[a-zA-Z][a-zA-Z0-9\.]+:\d+)(\/\w+)*/ ) {
Martin Rothce005da2016-04-01 14:30:33 -0600591 $URL_WITH_USER = $1;
592 } else {
593 print "Error: Could not get a ssh url with a username from gitconfig.\n";
594 print " use the -u option to set a url.\n";
595 exit 1;
596 }
597}
598
599#-------------------------------------------------------------------------------
600#-------------------------------------------------------------------------------
601sub get_commits {
Martin Rothae7d8372022-12-10 22:00:40 -0700602 my @commits = @_;
Martin Rothce005da2016-04-01 14:30:33 -0600603 my $submit_time = "";
Martin Rothae7d8372022-12-10 22:00:40 -0700604 if ( defined $SKIP_GERRIT_CHECK && $SKIP_GERRIT_CHECK ) {
Martin Rothce005da2016-04-01 14:30:33 -0600605 return;
606 }
Martin Rothae7d8372022-12-10 22:00:40 -0700607 my $ssh = Net::OpenSSH->new( "$URL_WITH_USER", );
608 $ssh->error and die "Couldn't establish SSH connection to $URL_WITH_USER:" . $ssh->error;
Martin Rothce005da2016-04-01 14:30:33 -0600609
610 print "Using URL: ssh://$URL_WITH_USER\n";
611
Martin Rothae7d8372022-12-10 22:00:40 -0700612 if ( !-d $infodir ) {
613 mkpath($infodir);
Martin Rothce005da2016-04-01 14:30:33 -0600614 }
615
616 for my $commit_id (@commits) {
617 $commit_id =~ s/^\s+|\s+$//g;
618 $submit_time = "";
Martin Rothae7d8372022-12-10 22:00:40 -0700619 my $gerrit_review;
Martin Rothce005da2016-04-01 14:30:33 -0600620
Martin Rothae7d8372022-12-10 22:00:40 -0700621 # Look for last coreboot commit
622 if ( $commit_id eq "7309709742" ) {
Martin Rothce005da2016-04-01 14:30:33 -0600623 last;
624 }
625
Martin Rothae7d8372022-12-10 22:00:40 -0700626 if ( -f "$infodir/$commit_id" ) {
Martin Rothce005da2016-04-01 14:30:33 -0600627 $gerrit_review = 1;
628 } else {
629 $gerrit_review = `git log $commit_id^..$commit_id | grep '\\sReviewed-on:\\s'`;
630 }
631
Martin Rothae7d8372022-12-10 22:00:40 -0700632 if ( $gerrit_review && $commit_id && ( !-f "$infodir/$commit_id" ) ) {
Martin Rothce005da2016-04-01 14:30:33 -0600633 print "Downloading $commit_id";
634 my @info = $ssh->capture("gerrit query --format=JSON --comments --files --current-patch-set --all-approvals --submit-records --dependencies commit:$commit_id");
635 $ssh->error and die "remote ls command failed: " . $ssh->error;
636
Martin Rothae7d8372022-12-10 22:00:40 -0700637 my $commit_info = JSON::Util->decode( $info[0] );
638 my $rowcount = $commit_info->{'rowCount'};
639 if ( defined $rowcount && ( $rowcount eq "0" ) ) {
Martin Rothce005da2016-04-01 14:30:33 -0600640 print " - no gerrit commit for that id.\n";
641 open( my $HANDLE, ">", "$infodir/$commit_id" ) or die "Error: could not open file '$infodir/$commit_id'\n";
642 print $HANDLE "No gerrit commit";
643 close $HANDLE;
644 next;
645 }
646 my $approvals = $commit_info->{'currentPatchSet'}{'approvals'};
647
648 for my $approval (@$approvals) {
Martin Rothae7d8372022-12-10 22:00:40 -0700649 if ( $approval->{'type'} eq "SUBM" ) {
650 $submit_time = $approval->{'grantedOn'};
Martin Rothce005da2016-04-01 14:30:33 -0600651 }
652 }
Martin Rothae7d8372022-12-10 22:00:40 -0700653 my $dt = "";
Martin Rothce005da2016-04-01 14:30:33 -0600654 if ($submit_time) {
Martin Rothae7d8372022-12-10 22:00:40 -0700655 $dt = DateTime->from_epoch( epoch => $submit_time );
Martin Rothce005da2016-04-01 14:30:33 -0600656 } else {
657 print " - no submit time for that id.\n";
658 open( my $HANDLE, ">", "$infodir/$commit_id" ) or die "Error: could not open file '$infodir/$commit_id'\n";
659 print $HANDLE "No submit time";
660 close $HANDLE;
661
662 next;
663 }
664
665 open( my $HANDLE, ">", "$infodir/$commit_id" ) or die "Error: could not open file '$infodir/$commit_id'\n";
666 print $HANDLE $info[0];
667 close $HANDLE;
668
Martin Rothae7d8372022-12-10 22:00:40 -0700669 $dt->set_time_zone('Europe/Paris');
Martin Rothce005da2016-04-01 14:30:33 -0600670 print " - submit time: " . $dt->strftime('%Y/%m/%d %H:%M:%S') . "\n";
Martin Rothae7d8372022-12-10 22:00:40 -0700671 } elsif ( $commit_id && ( !-f "$infodir/$commit_id" ) ) {
Martin Rothce005da2016-04-01 14:30:33 -0600672 print "No gerrit commit for $commit_id\n";
673 open( my $HANDLE, ">", "$infodir/$commit_id" ) or die "Error: could not open file '$infodir/$commit_id'\n";
674 print $HANDLE "No gerrit commit";
675 close $HANDLE;
676 }
677 }
678 print "\n";
679}
680
681#-------------------------------------------------------------------------------
682# check_arguments parse the command line arguments
683#-------------------------------------------------------------------------------
684sub check_arguments {
685 my $show_usage = 0;
686 GetOptions(
Martin Rothae7d8372022-12-10 22:00:40 -0700687 'help|?' => sub { usage() },
688 'url|u=s' => \$URL_WITH_USER,
689 'skip|s' => \$SKIP_GERRIT_CHECK,
Martin Rothce005da2016-04-01 14:30:33 -0600690 );
Martin Rothae7d8372022-12-10 22:00:40 -0700691
Martin Rothce005da2016-04-01 14:30:33 -0600692 # strip ssh:// from url if passed in.
Martin Rothae7d8372022-12-10 22:00:40 -0700693 if ( defined $URL_WITH_USER ) {
Martin Rothce005da2016-04-01 14:30:33 -0600694 $URL_WITH_USER =~ s|ssh://||;
695 }
696 if (@ARGV) {
Martin Rothae7d8372022-12-10 22:00:40 -0700697 ( $old_version, $new_version ) = @ARGV;
Martin Rothce005da2016-04-01 14:30:33 -0600698 } else {
699 usage();
700 }
701}
702
703#-------------------------------------------------------------------------------
704# usage - Print the arguments for the user
705#-------------------------------------------------------------------------------
706sub usage {
707 print "gerrit_stats <options> [Old version] [New version]\n";
708 print "Old version should be a tag (4.1), a branch (origin/4.1), or a commit id\n";
709 print "New version can be 'HEAD' a branch (origin/master) a tag (4.2), or a commit id\n";
710 print " Options:\n";
711 print " u | url [url] url with username.\n";
712 print "Example: \"$0 -u Gaumless\@review.coreboot.org:29418 origin/4.1 4.2\"\n";
713 exit(0);
714}
715
7161;