summaryrefslogtreecommitdiff
path: root/bin/lo-commit-stat
blob: 342d47ae50145b553387b27d60b1ec18c81802ea (plain)
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#!/usr/bin/perl
    eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
        if $running_under_some_shell;
#!/usr/bin/perl

use strict;

my $main_repo="bootstrap";
my @pieces=("artwork", "base", "calc", "components",
            "extensions", "extras", "filters", "help", "impress",
            "libs-core", "libs-extern", "libs-extern-sys", "libs-gui",
            "translations", "postprocess", "sdk", "testing", "ure", "writer");

sub search_bugs($$$$)
{
    my ($pdata, $piece, $commit_id, $line) = @_;

    my $bug = "";
    my $bug_orig;
    while (defined $bug) {

        # match fdo#123, rhz#123, i#123
        if ( $line =~ m/(\w*\#+\d+)/ ) {
            $bug_orig = $1;
            $bug = $1;
        # match #i123#
        } elsif ( $line =~ m/(\#i)(\d+)(\#)/ ) {
            $bug_orig = $1 . $2 . $3;
            $bug = "i#$2";
        } else {
            $bug = undef;
            next;
        }

#        print "   found $bug\n";
        # remove bug number from the comment; it will be added later a standardized way
        $bug_orig =~ s/\#/\\#/;
        $line =~ s/[Rr]esolves:\s*$bug_orig\s*//;
        $line =~ s/\s*-\s*$bug_orig\s*//;
        $line =~ s/\(?$bug_orig\)?[:,]?\s*//;

        # bnc# is prefered over n# for novell bugs
        $bug =~ s/^n\#/bnc#/;
        # save the bug number
        %{$pdata->{$piece}{$commit_id}{'bugs'}} = () if (! defined %{$pdata->{$piece}{$commit_id}{'bugs'}});
        $pdata->{$piece}{$commit_id}{'bugs'}{$bug} = 1;
        $pdata->{$piece}{$commit_id}{'flags'}{'bug'} = 1;
    }

    return $line;
}

sub standardize_summary($)
{
    my $line = shift;

    $line =~ s/^\s*//;
    $line =~ s/\s*$//;

    # lower first letter
    $line =~ m/(^.)/;
    my $first_char = lc($1);
    $line =~ s/^./$first_char/;

    # FIXME: remove do at the end of line
    #        remove bug numbers
    return $line;
}

sub load_git_log($$$$) 
{
    my ($pdata, $repo_dir, $piece, $pgit_args) = @_;

    my $cmd = "cd $repo_dir && git log " . join ' ', @{$pgit_args};
    my $commit_id;
    my $summary;

    print STDERR "Analyzing log from the git repo: $piece...\n";

    open (GIT, "$cmd 2>&1|") || die "Can't run $cmd: $!";
    %{$pdata->{$piece}} = ();

    while (my $line = <GIT>) {
        chomp $line;

        if ( $line =~ m/^commit ([0-9a-z]{20})/ ) {
            $commit_id = "$1";
            $summary=undef;
            %{$pdata->{$piece}{"$commit_id"}} = ();
            %{$pdata->{$piece}{"$commit_id"}{'flags'}} = ();
            next;
        }

        if ( $line =~ /^Author:\s*([^\<]*)\<([^\>]*)>/ ) {
            # get rid of extra empty spaces;
            my $name = "$1";
            $name =~ s/\s+$//;
            die "Error: Author already defined for the commit {$commit_id}\n" if defined ($pdata->{$piece}{$commit_id}{'author'});
            %{$pdata->{$piece}{$commit_id}{'author'}} = ();
            $pdata->{$piece}{$commit_id}{'author'}{'name'} = "$name";
            $pdata->{$piece}{$commit_id}{'author'}{'email'} = "$2";
            next;
        }

        if ( $line =~ /^Date:\s+/ ) {
            # ignore date line
            next;
        }

        if ( $line =~ /^\s*$/ ) {
            # ignore empty line
            next;
        }

        $line = search_bugs($pdata, $piece, $commit_id, $line);
        # FIXME: need to be implemeted
#        search_keywords($pdata, $line);

        unless (defined $pdata->{$piece}{$commit_id}{'summary'}) {
            $summary = standardize_summary($line);
            $pdata->{$piece}{$commit_id}{'summary'} = $summary;
        }
    }

    close GIT;
}

sub get_repo_name($)
{
    my $repo_dir = shift;
    
    open (GIT_CONFIG, "$repo_dir/.git/config") ||
        die "can't open \"$$repo_dir/.git/config\" for reading: $!\n";

    while (my $line = <GIT_CONFIG>) {
        chomp $line;

        if ( $line =~ /^\s*url\s*=\s*(\S+)$/ ) {
            my $repo_name = "$1";
            $repo_name = s/.*\///g;
            return "$repo_name";
        }
    }
    die "Error: can't find repo name in \"$$repo_dir/.git/config\"\n";
}

sub load_data($$$$)
{
    my ($pdata, $top_dir, $piece, $pgit_args) = @_;

    if (defined $piece) {
        my $piece_dir;
        if ("$piece" eq "$main_repo") {
            $piece_dir = "$top_dir";
        } else {
            $piece_dir = "$top_dir/clone/$piece";
        }
        load_git_log($pdata, $piece_dir, $piece, $pgit_args);
    } else {
        load_git_log($pdata, $top_dir, $main_repo, $pgit_args);
        foreach my $piece (@pieces) {
            load_git_log($pdata, "$top_dir/clone/$piece", $piece, $pgit_args);
        }
    }
}

sub get_branch_name($)
{
    my ($top_dir) = @_;

    my $branch;
    my $cmd = "cd $top_dir && git branch";

    print STDERR "Analyzing git branch: ";

    open (GIT, "$cmd 2>&1|") || die "Can't run $cmd: $!";

    while (my $line = <GIT>) {
        chomp $line;

        if ( $line =~ m/^\*\s*(\S+)/ ) {
            $branch = "$1";
        }
    }

    close GIT;

    die "Error: did not detect git branch name\n" unless defined ($branch);

    print STDERR "$branch\n";

    return $branch;
}

sub open_log_file($$$)
{
    my ($log_prefix, $log_suffix, $top_dir) = @_;

    my $branch_name = get_branch_name($top_dir);
    my $logfilename = "$log_prefix-$branch_name-$log_suffix.log";

    if (-f $logfilename) {
        print "WARNING: The log file already exists: $logfilename\n";
        print "Do you want to ovewrite it? (Y/n)?\n";
        my $answer = <STDIN>;
        chomp $answer;
        $answer = "y" unless ($answer);
        die "Please, rename the file or choose another log suffix\n" if ( lc($answer) ne "y" );
    }
    
    my $log;
    open($log, '>', $logfilename) || die "Can't open \"$logfilename\" for writing: $!\n";
    
    return $log;
}

sub print_summary_in_stat($$$$$$$$)
{
    my ($summary, $pprint_filters, $ppiece_title, $pflags, $pbugs, $pauthors, $prefix, $log) = @_;

    return if ( $summary eq "" );
    
    # do we want to print this summary at all?
    my $print;
    if (%{$pprint_filters}) {
        foreach my $flag (keys %{$pprint_filters}) {
            $print = 1 if (defined $pflags->{$flag});
        }
    } else {
        $print = 1;
    }
    return unless (defined $print);

    # print piece title if not done yet
    if (defined ${$ppiece_title}) {
        printf $log "${$ppiece_title}\n";
        ${$ppiece_title} = undef;
    }

    # finally print the summary line
    my $bugs = "";
    if ( %{$pbugs} ) {
        $bugs = " (" . join (", ", keys %{$pbugs}) . ")";
    }

    my $authors = "";
    if ( %{$pauthors} ) {
        $authors = " [" . join (", ", keys %{$pauthors}) . "]";
    }

    printf $log $prefix . $summary . $bugs . $authors . "\n";
}

sub print_stat($$$)
{
    my ($pdata, $pprint_filters, $log) = @_;

    foreach my $piece ( sort { $a cmp $b } keys %{$pdata}) {
        # check if this peice has any entries at all
        my $piece_title = "+ $piece";
        if ( %{$pdata->{$piece}} ) {
            my $old_summary="";
            my %authors = ();
            my %bugs = ();
            my %flags = ();
            foreach my $id ( sort { $pdata->{$piece}{$a}{'summary'} cmp $pdata->{$piece}{$b}{'summary'} } keys %{$pdata->{$piece}}) {
                my $summary = $pdata->{$piece}{$id}{'summary'};
                if ($summary ne $old_summary) {
                    print_summary_in_stat($old_summary, $pprint_filters, \$piece_title, \%flags, \%bugs, \%authors, "    + ", $log);
                    $old_summary = $summary;
                    %authors = ();
                    %bugs = ();
                    %flags = ();
                }
                # collect bug numbers
                if (defined $pdata->{$piece}{$id}{'bugs'}) {
                    foreach my $bug (keys %{$pdata->{$piece}{$id}{'bugs'}}) {
                        $bugs{$bug} = 1;
                    }
                }
                # collect author names
                my $author = $pdata->{$piece}{$id}{'author'}{'name'};
                $authors{$author} = 1;
                # collect flags
                foreach my $flag ( keys %{$pdata->{$piece}{$id}{'flags'}} ) {
                    $flags{$flag} = 1;
                }
            }
            print_summary_in_stat($old_summary, $pprint_filters, \$piece_title, \%flags, \%bugs, \%authors, "    + ", $log);
        }
    }
}

########################################################################
# help

sub usage()
{
    print "This script generates LO git commit summary\n\n" .
          
          "Usage: lo-commit-stat [--help] [--no-pieces] [--piece=<piece>] --log-suffix=<string> topdir [git_log_param...]\n\n" .

          "Options:\n" .
          "     --help          print this help\n" .
          "     --no-pieces     read changes just from the main repository, ignore other cloned repos\n" .
          "     --piece=<piece> summarize just chnages from the given piece\n" .
          "     --log-suffix=<string> suffix of the log file name; the result will be\n" .
          "                     commit-log-<branch>-<log-name-suffix>.log; the branch name\n" .
          "                     is detected autoamtically\n" .
          "     --bugs          print just bug fixes\n" .
          "      topdir         directory with the libreoffice/bootstrap clone; the piece repos\n" .
          "                     must be cloned in the main-repo-root/clone/<piece> subdirectories\n" .
          "      git_log_param  extra parameters passed to the git log command to define\n" .
          "                     the area of interest , e.g. --after=\"2010-09-27\" or\n" .
          "                     TAG..HEAD";
}


#######################################################################
#######################################################################
# MAIN
#######################################################################
#######################################################################


my $piece;
my $top_dir;
my $log_prefix = "commit-log";
my $log_suffix;
my $log;
my @git_args;
my %data;
my %print_filters = ();

foreach my $arg (@ARGV) {
    if ($arg eq '--help') {
        usage();
        exit;
    } elsif ($arg eq '--no-pieces') {
        $piece = "bootstrap";
    } elsif ($arg =~ m/--piece=(.*)/) {
	$piece = $1;
    } elsif ($arg =~ m/--log-suffix=(.*)/) {
	$log_suffix = "$1";
    } elsif ($arg eq '--bugs') {
        $print_filters{'bug'} = 1;
        $log_prefix = "bugfixes"
    } else {
        if (! defined $top_dir) {
            $top_dir=$arg;
        } else {
            push @git_args, $arg;
        }
    }
}

(defined $top_dir) || die "Error: top direcotry is not defined\n";
(-d "$top_dir") || die "Error: not a directory: $top_dir\n";
(-f "$top_dir/.git/config") || die "Error: can't find $top_dir/.git/config\n";

(defined $log_suffix) || die "Error: define log suffix using --log-suffix=<string>\n";

load_data(\%data, $top_dir,$piece, \@git_args);

$log = open_log_file($log_prefix, $log_suffix, $top_dir);
print_stat(\%data, \%print_filters, $log);
close $log;