2 eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
3 if $running_under_some_shell;
12 use open ':encoding(utf8)';
13 use open ':std' => ':encoding(utf8)';
15 my %module_dirname = (
17 "dictionaries" => "dictionaries",
18 "help" => "helpcontent2",
19 "translations" => "translations"
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/",
34 my ($pdata, $module, $commit_id, $line) = @_;
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,})/ ) {
45 # default to issuezilla for the #123 variant
46 # but match only bug number with >= 4 digits
47 } elsif ( $line =~ m/(\#)(\d{4,})/ ) {
51 } elsif ( $line =~ m/(\#i)(\d+)(\#)/ ) {
52 $bug_orig = $1 . $2 . $3;
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
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#/;
75 $pdata->{$module}{$commit_id}{'bugs'}{$bug} = 1;
81 sub standardize_summary
($)
88 # lower first letter if the word contains only lowercase letter
89 if ( $line =~ m/(^.[a-z]+\b)/ ) {
91 my $first_char = lc($1);
92 $line =~ s/^./$first_char/;
95 # FIXME: remove do at the end of line
100 sub generate_git_cherry_ids_log
($$$$$)
102 my ($pdata, $repo_dir, $module, $branch_name, $git_args) = @_;
105 my $commit_ids_log_fh;
106 $commit_ids_log_fh = File
::Temp
->new(TEMPLATE
=> 'lo-commit-stat-ids-XXXXXX',
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/^\+ / ) {
123 print $commit_ids_log_fh $line;
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;";
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";
144 $cmd .= " $git_command $git_args";
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
>) {
165 if ( $line =~ m/^commit ([0-9a-z]{20})/ ) {
171 if ( $line =~ /^Author:\s*([^\<]*)\<([^\>]*)>/ ) {
172 # get rid of extra empty spaces;
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;
182 if ( $line =~ /^Date:\s+/ ) {
187 if ( $line =~ /^\s*$/ ) {
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;
203 unlink $commit_ids_log if ($git_cherry);
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
>) {
216 if ( $line =~ /^\s*url\s*=\s*(\S+)$/ ) {
217 my $repo_name = "$1";
218 $repo_name = s/.*\///g
;
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
($)
239 my $cmd = "cd $top_dir && git branch";
241 open (GIT
, "$cmd 2>&1|") || die "Can't run $cmd: $!";
243 while (my $line = <GIT
>) {
246 if ( $line =~ m/^\*\s*(\S+)/ ) {
253 die "Error: did not detect git branch name in $top_dir\n" unless defined ($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);
290 $logfilename .= ".wiki";
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
>;
300 $answer = "y" unless ($answer);
301 die "Please, rename the file or choose another log suffix\n" if ( lc($answer) ne "y" );
305 open($log, '>', $logfilename) || die "Can't open \"$logfilename\" for writing: $!\n";
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
325 $bugs = " (" . join (", ", keys %{$pbugs}) . ")";
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}} ) {
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;
355 # collect bug numbers
356 if (defined $pdata->{$module}{$id}{'bugs'}) {
357 foreach my $bug (keys %{$pdata->{$module}{$id}{'bugs'}}) {
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);
372 my ($bug, $summary) = @_;
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;
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 –) // ) {
391 print "warning: not found; using commit message (only got $title)";
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'};
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'};
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);
451 ########################################################################
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" .
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 #######################################################################
492 #######################################################################
493 #######################################################################
497 my %generate_log = ();
503 my $check_bugzilla = 0;
505 my $git_command = "git log";
512 foreach my $arg (@ARGV) {
513 if ($arg eq '--help') {
516 } elsif ($arg eq '--no-submodule') {
518 } elsif ($arg =~ m/--module=(.*)/) {
520 } elsif ($arg =~ m/--log-suffix=(.*)/) {
522 } elsif ($arg =~ m/--log-dir=(.*)/) {
524 } elsif ($arg eq '--commits') {
525 $generate_log{"commits"} = 1;
526 } elsif ($arg eq '--bugs') {
527 $generate_log{"bugs"} = 1;
530 } elsif ($arg eq '--bugs-changelog') {
531 $generate_log{"bugs-changelog"} = 1;
534 } elsif ($arg eq '--bugs-wiki' || $arg eq '--wikibugs') {
535 $generate_log{"bugs-wiki"} = 1;
538 } elsif ($arg eq '--bugs-numbers' || $arg eq '--bug-numbers') {
539 $generate_log{"bugs-numbers"} = 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";
547 if (! defined $top_dir) {
550 $git_args .= " $arg";
556 unless (%generate_log) {
557 $generate_log{"commits"} = 1;
560 # we want only one 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"});