Fix typo
[LibreOffice.git] / bin / lo-commit-stat
blob08e8a1785f86977eec68086ccceab9a7f1c50521
1 #!/usr/bin/perl
2 eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
3 if $running_under_some_shell;
4 #!/usr/bin/perl
6 use strict;
7 use warnings;
8 use LWP::UserAgent;
9 use utf8;
10 use File::Temp;
11 use Encode;
12 use open ':encoding(utf8)';
13 use open ':std' => ':encoding(utf8)';
15 my %module_dirname = (
16 "core" => "",
17 "dictionaries" => "dictionaries",
18 "help" => "helpcontent2",
19 "translations" => "translations"
23 my %bugzillas = (
24 fdo => "https://bugs.documentfoundation.org/show_bug.cgi?id=",
25 tdf => "https://bugs.documentfoundation.org/show_bug.cgi?id=",
26 bnc => "https://bugzilla.novell.com/show_bug.cgi?id=",
27 rhbz => "https://bugzilla.redhat.com/show_bug.cgi?id=",
28 i => "https://bz.apache.org/ooo/show_bug.cgi?id=",
29 fate => "https://features.opensuse.org/",
32 sub search_bugs($$$$)
34 my ($pdata, $module, $commit_id, $line) = @_;
36 my $bug = "";
37 my $bug_orig;
38 while (defined $bug) {
40 # match fdo#123, rhz#123, i#123, #123
41 # but match only bug number with >= 4 digits
42 if ( $line =~ m/(\w+\#+\d{4,})/ ) {
43 $bug_orig = $1;
44 $bug = $1;
45 # default to issuezilla for the #123 variant
46 # but match only bug number with >= 4 digits
47 } elsif ( $line =~ m/(\#)(\d{4,})/ ) {
48 $bug_orig = $1 . $2;
49 $bug = "i#$2";
50 # match #i123#
51 } elsif ( $line =~ m/(\#i)(\d+)(\#)/ ) {
52 $bug_orig = $1 . $2 . $3;
53 $bug = "i#$2";
54 } else {
55 $bug = undef;
56 next;
59 # print " found $bug\n";
60 # remove bug number from the comment; it will be added later a standardized way
61 $bug_orig =~ s/\#/\\#/;
62 $line =~ s/(,\s)*[Rr](elated|esolve[ds]):?\s*$bug_orig\s?:?\s*//;
63 $line =~ s/\s*-\s*$bug_orig\s*//;
64 $line =~ s/\(?$bug_orig\)?\s*[:,-]?\s*//;
66 # bnc# is preferred over n# for novell bugs
67 $bug =~ s/^n\#/bnc#/;
68 # deb# is preferred over debian# for debian bugs
69 $bug =~ s/^debian\#/deb#/;
70 # easyhack# is sometimes used for fdo# - based easy hacks
71 $bug =~ s/^easyhack\#/fdo#/;
72 # someone mistyped fdo as fd0
73 $bug =~ s/^fd0\#/fdo#/;
74 # save the bug number
75 $pdata->{$module}{$commit_id}{'bugs'}{$bug} = 1;
78 return $line;
81 sub standardize_summary($)
83 my $line = shift;
85 $line =~ s/^\s*//;
86 $line =~ s/\s*$//;
88 # lower first letter if the word contains only lowercase letter
89 if ( $line =~ m/(^.[a-z]+\b)/ ) {
90 $line =~ m/(^.)/;
91 my $first_char = lc($1);
92 $line =~ s/^./$first_char/;
95 # FIXME: remove do at the end of line
96 # remove bug numbers
97 return $line;
100 sub generate_git_cherry_ids_log($$$$$)
102 my ($pdata, $repo_dir, $module, $branch_name, $git_args) = @_;
104 my $commit_ids_log;
105 my $commit_ids_log_fh;
106 $commit_ids_log_fh = File::Temp->new(TEMPLATE => 'lo-commit-stat-ids-XXXXXX',
107 DIR => '/tmp',
108 UNLINK => 0);
109 $commit_ids_log = $commit_ids_log_fh->filename;
111 print STDERR "Filtering cherry-picked commits in the git repo: $module...\n";
113 my $cmd = "cd $repo_dir; git cherry $git_args";
114 open (GIT, "$cmd 2>&1|") || die "Can't run $cmd: $!";
116 while (my $line = <GIT>) {
118 # skip cherry-picked commits
119 next if ( $line =~ m/^\-/ );
121 if ( $line =~ m/^\+ / ) {
122 $line =~ s/^\+ //;
123 print $commit_ids_log_fh $line;
127 close GIT;
128 close $commit_ids_log_fh;
130 return $commit_ids_log;
133 sub load_git_log($$$$$$$)
135 my ($pdata, $repo_dir, $module, $branch_name, $git_command, $git_cherry, $git_args) = @_;
137 my $cmd = "cd $repo_dir;";
138 my $commit_ids_log;
140 if ($git_cherry) {
141 $commit_ids_log = generate_git_cherry_ids_log($pdata, $repo_dir, $module, $branch_name, $git_args);
142 $cmd .= " cat $commit_ids_log | xargs -n 1 $git_command -1";
143 } else {
144 $cmd .= " $git_command $git_args";
147 my $commit_id;
148 my $summary;
150 print STDERR "Analyzing log from the git repo: $module...\n";
152 # FIXME: ./g pull move submodules in unnamed branches
153 # my $repo_branch_name = get_branch_name($repo_dir);
154 # if ( $branch_name ne $repo_branch_name ) {
155 # die "Error: mismatch of branches:\n" .
156 # " main repo is on the branch: $branch_name\n" .
157 # " $module repo is on the branch: $repo_branch_name\n";
160 open (GIT, "$cmd 2>&1|") || die "Can't run $cmd: $!";
162 while (my $line = <GIT>) {
163 chomp $line;
165 if ( $line =~ m/^commit ([0-9a-z]{20})/ ) {
166 $commit_id = $1;
167 $summary=undef;
168 next;
171 if ( $line =~ /^Author:\s*([^\<]*)\<([^\>]*)>/ ) {
172 # get rid of extra empty spaces;
173 my $name = $1;
174 my $email = $2;
175 $name =~ s/\s+$//;
176 die "Error: Author already defined for the commit {$commit_id}\n" if defined ($pdata->{$module}{$commit_id}{'author'});
177 $pdata->{$module}{$commit_id}{'author'}{'name'} = $name;
178 $pdata->{$module}{$commit_id}{'author'}{'email'} = $email;
179 next;
182 if ( $line =~ /^Date:\s+/ ) {
183 # ignore date line
184 next;
187 if ( $line =~ /^\s*$/ ) {
188 # ignore empty line
189 next;
192 unless (defined $pdata->{$module}{$commit_id}{'summary'}) {
193 $line = search_bugs($pdata, $module, $commit_id, $line);
194 # FIXME: need to be implemented
195 # search_keywords($pdata, $line);
197 $summary = standardize_summary($line);
198 $pdata->{$module}{$commit_id}{'summary'} = $summary;
202 close GIT;
203 unlink $commit_ids_log if ($git_cherry);
206 sub get_repo_name($)
208 my $repo_dir = shift;
210 open (GIT_CONFIG, "$repo_dir/.git/config") ||
211 die "can't open \"$$repo_dir/.git/config\" for reading: $!\n";
213 while (my $line = <GIT_CONFIG>) {
214 chomp $line;
216 if ( $line =~ /^\s*url\s*=\s*(\S+)$/ ) {
217 my $repo_name = "$1";
218 $repo_name = s/.*\///g;
219 return "$repo_name";
222 die "Error: can't find repo name in \"$$repo_dir/.git/config\"\n";
225 sub load_data($$$$$$$)
227 my ($pdata, $top_dir, $p_module_dirname, $branch_name, $git_command, $git_cherry, $git_args) = @_;
229 foreach my $module (sort { $a cmp $b } keys %{$p_module_dirname}) {
230 load_git_log($pdata, "$top_dir/$p_module_dirname->{$module}", $module, $branch_name, $git_command, $git_cherry, $git_args);
234 sub get_branch_name($)
236 my ($top_dir) = @_;
238 my $branch;
239 my $cmd = "cd $top_dir && git branch";
241 open (GIT, "$cmd 2>&1|") || die "Can't run $cmd: $!";
243 while (my $line = <GIT>) {
244 chomp $line;
246 if ( $line =~ m/^\*\s*(\S+)/ ) {
247 $branch = "$1";
251 close GIT;
253 die "Error: did not detect git branch name in $top_dir\n" unless defined ($branch);
255 return $branch;
258 sub get_bug_list($$$)
260 my ($pdata, $pbugs, $check_bugzilla) = @_;
262 # associate bugs with their summaries and fixers
263 foreach my $module ( keys %{$pdata}) {
264 foreach my $id ( keys %{$pdata->{$module}}) {
265 foreach my $bug (keys %{$pdata->{$module}{$id}{'bugs'}}) {
266 my $author = $pdata->{$module}{$id}{'author'}{'name'};
267 my $summary = $pdata->{$module}{$id}{'summary'};
268 $pbugs->{$bug}{'summary'} = $summary;
269 $pbugs->{$bug}{'author'}{$author} = 1;
274 # try to replace summaries with bug names from bugzilla
275 if ($check_bugzilla) {
276 print "Getting bug titles:\n";
277 foreach my $bug ( sort { $a cmp $b } keys %{$pbugs}) {
278 $pbugs->{$bug}{'summary'} = get_bug_name($bug, $pbugs->{$bug}{'summary'});
283 sub open_log_file($$$$$$)
285 my ($log_dir, $log_prefix, $log_suffix, $top_dir, $branch_name, $wiki) = @_;
287 my $logfilename = "$log_prefix-$branch_name-$log_suffix";
288 $logfilename = "$log_dir/$logfilename" if (defined $log_dir);
289 if ($wiki) {
290 $logfilename .= ".wiki";
291 } else {
292 $logfilename .= ".log";
295 if (-f $logfilename) {
296 print "WARNING: The log file already exists: $logfilename\n";
297 print "Do you want to overwrite it? (Y/n)?\n";
298 my $answer = <STDIN>;
299 chomp $answer;
300 $answer = "y" unless ($answer);
301 die "Please, rename the file or choose another log suffix\n" if ( lc($answer) ne "y" );
304 my $log;
305 open($log, '>', $logfilename) || die "Can't open \"$logfilename\" for writing: $!\n";
307 return $log;
310 sub print_commit_summary($$$$$$)
312 my ($summary, $pmodule_title, $pbugs, $pauthors, $prefix, $log) = @_;
314 return if ( $summary eq "" );
316 # print module title if not done yet
317 if ( defined ${$pmodule_title} ) {
318 print $log "${$pmodule_title}\n";
319 ${$pmodule_title} = undef;
322 # finally print the summary line
323 my $bugs = "";
324 if ( %{$pbugs} ) {
325 $bugs = " (" . join (", ", keys %{$pbugs}) . ")";
328 my $authors = "";
329 if ( %{$pauthors} ) {
330 $authors = " [" . join (", ", keys %{$pauthors}) . "]";
333 print $log $prefix, $summary, $bugs, $authors, "\n";
336 sub print_commits($$$)
338 my ($pdata, $log, $wiki) = @_;
340 foreach my $module ( sort { $a cmp $b } keys %{$pdata}) {
341 # check if this module has any entries at all
342 my $module_title = "+ $module";
343 if ( %{$pdata->{$module}} ) {
344 my $old_summary="";
345 my %authors = ();
346 my %bugs = ();
347 foreach my $id ( sort { lc $pdata->{$module}{$a}{'summary'} cmp lc $pdata->{$module}{$b}{'summary'} } keys %{$pdata->{$module}}) {
348 my $summary = $pdata->{$module}{$id}{'summary'};
349 if ($summary ne $old_summary) {
350 print_commit_summary($old_summary, \$module_title, \%bugs, \%authors, " + ", $log);
351 $old_summary = $summary;
352 %authors = ();
353 %bugs = ();
355 # collect bug numbers
356 if (defined $pdata->{$module}{$id}{'bugs'}) {
357 foreach my $bug (keys %{$pdata->{$module}{$id}{'bugs'}}) {
358 $bugs{$bug} = 1;
361 # collect author names
362 my $author = $pdata->{$module}{$id}{'author'}{'name'};
363 $authors{$author} = 1;
365 print_commit_summary($old_summary, \$module_title, \%bugs, \%authors, " + ", $log);
370 sub get_bug_name($$)
372 my ($bug, $summary) = @_;
373 print "$bug: ";
375 $bug =~ m/(?:(\w*)\#+(\d+))/; # fdo#12345
376 my $bugzilla = $1; # fdo
377 my $bug_number = $2; # 12345
379 if ( $bugzillas{$bugzilla} ) {
380 my $url = $bugzillas{$bugzilla} . $bug_number;
381 my $ua = LWP::UserAgent->new;
382 $ua->timeout(10);
383 $ua->env_proxy;
384 my $response = $ua->get($url);
385 if ($response->is_success) {
386 my $title = decode('utf8', $response->title);
387 if ( $title =~ s/^(?:Bug $bug_number \S+|$bug_number –) // ) {
388 print "$title\n";
389 return $title;
390 } else {
391 print "warning: not found; using commit message (only got $title)";
395 print "\n";
397 return $summary;
400 sub print_bugs($$$$)
402 my ($pbugs, $log, $wiki) = @_;
404 # sort alphabetically by bugzilla-type, but within that numerically
405 foreach my $bug ( sort { ($a =~ /(\D+)/)[0] cmp ($b =~ /(\D+)/)[0] ||
406 ($a =~ /(\d+)/)[0] <=> ($b =~ /(\d+)/)[0] } keys %{$pbugs}) {
407 my $summary = $pbugs->{$bug}{'summary'};
409 my $authors = "";
410 if ( %{$pbugs->{$bug}{'author'}} ) {
411 $authors = " [" . join (", ", keys %{$pbugs->{$bug}{'author'}}) . "]";
414 $bug =~ s/(.*)\#(.*)/# {{$1|$2}}/ if ($wiki);
415 print $log $bug, " ", $summary, $authors, "\n";
419 sub print_bugs_changelog($$$$)
421 my ($pbugs, $log, $wiki) = @_;
423 foreach my $bug ( sort { $a cmp $b } keys %{$pbugs}) {
424 my $summary = $pbugs->{$bug}{'summary'};
426 my $authors = "";
427 if ( %{$pbugs->{$bug}{'author'}} ) {
428 $authors = " [" . join (", ", keys %{$pbugs->{$bug}{'author'}}) . "]";
431 print $log " + $summary ($bug)$authors\n";
435 sub print_bugnumbers($$$$)
437 my ($pbugs, $log, $wiki) = @_;
439 print $log join ("\n", sort { $a cmp $b } keys %{$pbugs}), "\n";
442 sub generate_log($$$$$$$$)
444 my ($pused_data, $print_func, $log_dir, $log_prefix, $log_suffix, $top_dir, $branch_name, $wiki) = @_;
446 my $log = open_log_file($log_dir, $log_prefix, $log_suffix, $top_dir, $branch_name, $wiki);
447 & {$print_func} ($pused_data, $log, $wiki);
448 close $log;
451 ########################################################################
452 # help
454 sub usage()
456 print "This script generates LO git commit summary\n\n" .
458 "Usage: lo-commit-stat [--help] [--no-submodules] [--module=<module>] --log-dir=<dir> --log-suffix=<string> topdir [git_arg...]\n\n" .
460 "Options:\n" .
461 " --help print this help\n" .
462 " --no-submodule read changes just from the main repository, ignore submodules\n" .
463 " --module=<module> summarize just changes from the given module, use \"core\"\n" .
464 " for the main module\n" .
465 " --log-dir=<dir> directory where to put the generated log\n" .
466 " --log-suffix=<string> suffix of the log file name; the result will be\n" .
467 " commit-log-<branch>-<log-name-suffix>.log; the branch name\n" .
468 " is detected automatically\n" .
469 " --commits generate log with all commits (default)\n" .
470 " --bugs generate log with bugzilla entries\n" .
471 " --bugs-changelog generate log with bugzilla entries, use changelog style\n" .
472 " --bugs-wiki generate log with bugzilla entries, use wiki markup\n" .
473 " --bugs-numbers generate log with bugzilla numbers\n" .
474 " --rev-list use \"git rev-list\" instead of \"git log\"; useful to check\n" .
475 " differences between branches\n" .
476 " --cherry use \"git cherry\" instead of \"git log\"; detects cherry-picked\n" .
477 " commits between branches\n" .
478 " topdir directory with the libreoffice/core clone\n" .
479 " git_arg extra parameters passed to the git command to define\n" .
480 " the area of interest; The default command is \"git log\" and\n" .
481 " parameters might be, for example, --after=\"2010-09-27\" or\n" .
482 " TAG..HEAD; with the option --rev-list, useful might be, for\n" .
483 " example origin/master ^origin/libreoffice-3-3; with the option\n" .
484 " --rev-list, useful might be, for example libreoffice-3.6.3.2\n" .
485 " libreoffice-3.6.4.1\n";
489 #######################################################################
490 #######################################################################
491 # MAIN
492 #######################################################################
493 #######################################################################
496 my $module;
497 my %generate_log = ();
498 my $top_dir;
499 my $log_dir;
500 my $log_suffix;
501 my $log;
502 my $list_bugs = 0;
503 my $check_bugzilla = 0;
504 my $branch_name;
505 my $git_command = "git log";
506 my $git_cherry;
507 my $git_args = "";
508 my %data;
509 my %bugs = ();
512 foreach my $arg (@ARGV) {
513 if ($arg eq '--help') {
514 usage();
515 exit;
516 } elsif ($arg eq '--no-submodule') {
517 $module = "core";
518 } elsif ($arg =~ m/--module=(.*)/) {
519 $module = $1;
520 } elsif ($arg =~ m/--log-suffix=(.*)/) {
521 $log_suffix = "$1";
522 } elsif ($arg =~ m/--log-dir=(.*)/) {
523 $log_dir = "$1";
524 } elsif ($arg eq '--commits') {
525 $generate_log{"commits"} = 1;
526 } elsif ($arg eq '--bugs') {
527 $generate_log{"bugs"} = 1;
528 $check_bugzilla = 1;
529 $list_bugs = 1;
530 } elsif ($arg eq '--bugs-changelog') {
531 $generate_log{"bugs-changelog"} = 1;
532 $check_bugzilla = 1;
533 $list_bugs = 1;
534 } elsif ($arg eq '--bugs-wiki' || $arg eq '--wikibugs') {
535 $generate_log{"bugs-wiki"} = 1;
536 $check_bugzilla = 1;
537 $list_bugs = 1;
538 } elsif ($arg eq '--bugs-numbers' || $arg eq '--bug-numbers') {
539 $generate_log{"bugs-numbers"} = 1;
540 $list_bugs = 1;
541 } elsif ($arg eq '--rev-list') {
542 $git_command = "git rev-list --pretty=medium"
543 } elsif ($arg eq '--cherry') {
544 $git_command = "git log";
545 $git_cherry = 1;
546 } else {
547 if (! defined $top_dir) {
548 $top_dir=$arg;
549 } else {
550 $git_args .= " $arg";
555 # default log
556 unless (%generate_log) {
557 $generate_log{"commits"} = 1;
560 # we want only one module
561 if ($module) {
562 my $name = $module_dirname{$module};
563 %module_dirname = ();
564 $module_dirname{$module} = $name;
567 (defined $top_dir) || die "Error: top directory is not defined\n";
568 (-d "$top_dir") || die "Error: not a directory: $top_dir\n";
569 (-f "$top_dir/.git/config") || die "Error: can't find $top_dir/.git/config\n";
571 (!defined $log_dir) || (-d $log_dir) || die "Error: directory does no exist: $log_dir\n";
573 (defined $log_suffix) || die "Error: define log suffix using --log-suffix=<string>\n";
575 $branch_name = get_branch_name($top_dir);
577 load_data(\%data, $top_dir, \%module_dirname, $branch_name, $git_command, $git_cherry, $git_args);
578 get_bug_list(\%data, \%bugs, $check_bugzilla) if ($list_bugs);
580 generate_log(\%data, \&print_commits, $log_dir, "commits", $log_suffix, $top_dir, $branch_name, 0) if (defined $generate_log{"commits"});
581 generate_log(\%bugs, \&print_bugs, $log_dir, "bugs", $log_suffix, $top_dir, $branch_name, 0) if (defined $generate_log{"bugs"});
582 generate_log(\%bugs, \&print_bugs, $log_dir, "bugs", $log_suffix, $top_dir, $branch_name, 1) if (defined $generate_log{"bugs-wiki"});
583 generate_log(\%bugs, \&print_bugs_changelog, $log_dir, "bugs-changelog", $log_suffix, $top_dir, $branch_name, 0) if (defined $generate_log{"bugs-changelog"});
584 generate_log(\%bugs, \&print_bugnumbers, $log_dir, "bug-numbers", $log_suffix, $top_dir, $branch_name, 0) if (defined $generate_log{"bugs-numbers"});