summaryrefslogtreecommitdiff
path: root/worklog
blob: c2ee60ce034d495f479423e2bb0307c89d4e4633 (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
368
369
370
371
372
373
374
375
#!/usr/bin/perl
 
use strict;
use warnings;
use Getopt::Long;
use Time::Local;
 
################################################################
 
sub seconds_in_a_week () {
	return 7 * 24 * 3600;
}
 
# Convert seconds from epoch to datestring and dow
sub seconds_to_date {
	my $time = shift;
	my @time = localtime($time);
	return (sprintf("%04d-%02d-%02d",
			$time[5]+1900, $time[4]+1, $time[3]),
		$time[6]);
}
 
sub date_to_seconds {
	my $datestring = shift;
	my ($year, $mon, $mday) = ($datestring =~ /^(\d{4})-(\d{2})-(\d{2})$/);
	unless (defined $year && defined $mon && defined $mday &&
		1 <= $mon && $mon <= 12 &&
		1 <= $mday && $mday <= 31) {
		die "Bad datestring specification: $datestring";
	}
	return timelocal(0, 0, 0, $mday, $mon - 1, $year - 1900);
}
 
sub next_date {
	my $datestring = shift;
	my $time = date_to_seconds($datestring);
	return seconds_to_date($time + 3600 * 36);
}
 
sub prev_date {
	my $datestring = shift;
	my $time = date_to_seconds($datestring);
	return seconds_to_date($time - 3600 * 12);
}
 
sub beginning_of_reporting_week {
	my ($datestring, $bow) = @_;
	my ($date, $dow) = seconds_to_date(date_to_seconds($datestring));
	do {
		($date, $dow) = prev_date($date);
	} while ($dow != $bow);
	return $date;
}
 
sub bow {
	my ($week, $bow) = @_;
	my $time;
	if (!defined $week) {
		$time = time;
	} elsif ($week =~ /^\d+$/) {
		$time = time - seconds_in_a_week * $week;
	} else {
		$time = date_to_seconds($week);
	}
	my ($datestring, $dow) = seconds_to_date($time);
	return beginning_of_reporting_week($datestring, $bow);
}
 
sub date_within {
	my ($date, $bottom, $top) = @_;
	return ($bottom le $date && $date le $top);
}
 
################################################################
 
my $verbose = 0;
my $quiet = 0;
my $reporting_date;
my $weeks;
my $bow = 0;
my $bottom_date;
 
if (!GetOptions(
	     "verbose!" => \$verbose,
	     "quiet!" => \$quiet,
	     "date=s" => \$reporting_date,
	     "weeks=i" => \$weeks,
	     "bow=i" => \$bow,
    )) {
	print STDERR "$0 [-v|-q] [-d date] [-w weeks] [-b bow]\n";
	exit 1;
}
 
if ($verbose && $quiet) {
	print STDERR "Which? Verbose, or Quiet?\n";
	exit 1;
}
 
if (!defined $reporting_date) {
	($reporting_date, my $dow) = seconds_to_date(time);
	while ($dow != $bow) {
		($reporting_date, $dow) = next_date($reporting_date);
	}
}
 
$bottom_date = beginning_of_reporting_week($reporting_date, $bow);
 
if (!defined $weeks || $weeks < 0) {
	$weeks = 0;
}
 
for (my $i = 0; $i < $weeks; $i++) {
	for (my $j = 0; $j < 7; $j++) {
		($bottom_date, undef) = prev_date($bottom_date);
	}
}
($bottom_date, undef) = next_date($bottom_date);
 
my $cull_old = "--since=" . date_to_seconds($bottom_date);
 
sub plural {
	my ($number, $singular, $plural) = @_;
	return ($number == 1) ? "$number $singular": "$number $plural";
}
 
sub fmt_join {
	my ($leader, $limit, $joiner, @ary) = @_;
	my @result = ();
	my $width = 0;
	my $wj = length($joiner);
	my $wl = length($leader);
 
	for my $item (@ary) {
		my $need_joiner;
		if ($width == 0) {
			$width += $wl;
			push @result, $leader;
			$need_joiner = 0;
		} else {
			$need_joiner = 1;
		}
		my $len = length($item);
		if (($need_joiner ? $wj : 0) + $len + $width < $limit) {
			if ($need_joiner) {
				$width += $wj;
				push @result, $joiner;
			}
			$width += $len;
			push @result, $item;
		} else {
			if ($width) {
				push @result, "\n";
				$width = 0;
			}
			$width += $wl;
			push @result, $leader;
			$width += $len;
			push @result, $item;
		}
	}
	push @result, "\n" unless ($result[-1] eq "\n");
	return join("", @result);
}
 
################################################################
# Collection
 
# Map sha1 to patch information [$sha1, $author, $subject]
# or merge information [$sha1, undef, $branch].
my %patch;
 
# $dates{"YYYY-MM-DD"} exists iff something happened
my %dates;
 
# List of $sha1 of patches applied, grouped by date
my %patch_by_date;
 
# List of $sha1 of merges, grouped by date
my %merge_by_branch_date;
 
# List of tags, grouped by date
my %tag_by_date;
 
# List of integration branches.
my @integrate = (['master', ', to include in the next release'],
		 ['next', ' for public testing'],
		 ['maint', ', to include in the maintenance release'],
);
 
# Collect individial patch application
open I, "-|", ("git", "log",
	       "--pretty=%ci %H %an <%ae>\001%s",
	       $cull_old, "--glob=refs/heads",
	       "--no-merges") or die;
 
while (<I>) {
	my ($date, $sha1, $rest) = /^([-0-9]+) [:0-9]+ [-+][0-9]{4} ([0-9a-f]+) (.*)$/;
	next unless date_within($date, $bottom_date, $reporting_date);
 
	$patch_by_date{$date} ||= [];
	push @{$patch_by_date{$date}}, $sha1;
	my ($name, $subject) = split(/\001/, $rest, 2);
	$patch{$sha1} = [$sha1, $name, $subject];
	$dates{$date}++;
}
close (I) or die;
 
for my $branch (map { $_->[0] } @integrate) {
	open I, "-|", ("git", "log", "--pretty=%ci %H %s",
		       $cull_old,
		       "--first-parent",
		       "--merges",
		       $branch) or die;
	while (<I>) {
		my ($date, $sha1, $rest) = /^([-0-9]+) [:0-9]+ [-+][0-9]{4} ([0-9a-f]+) (.*)$/;
		next unless date_within($date, $bottom_date, $reporting_date);
		my $msg = $rest;
		$msg =~ s/^Merge branch //;
		$msg =~ s/ into \Q$branch\E$//;
		$msg =~ s/^'(.*)'$/$1/;
 
		next if (grep { $_ eq $msg } map { $_->[0] } @integrate);
 
		$merge_by_branch_date{$branch} ||= {};
		$merge_by_branch_date{$branch}{$date} ||= [];
		push @{$merge_by_branch_date{$branch}{$date}}, $sha1;
		$patch{$sha1} = [$sha1, undef, $msg];
		$dates{$date}++;
	}
	close (I) or die;
}
 
open I, "-|", ("git", "for-each-ref",
	       "--format=%(refname:short) %(taggerdate:iso)",
	       "refs/tags") or die;
while (<I>) {
	my ($tagname, $tagdate) = /^(\S+) ([-0-9]+) [:0-9]+ [-+][0-9]{4}$/;
 
	if (!defined $tagdate || 
	    !date_within($tagdate, $bottom_date, $reporting_date)) {
		next;
	}
	$dates{$tagdate}++;
	$tag_by_date{$tagdate} ||= [];
	push @{$tag_by_date{$tagdate}}, $tagname;
}
 
################################################################
# Summarize
 
my $sep = ""; 
my @dates = sort keys %dates;
 
sub day_summary {
	my ($date, $total_names, $total_merges, $total_patches, $total_tags) = @_;
	return if (!exists $dates{$date});
 
	print "$sep$date\n" if (!$quiet);
	if (exists $tag_by_date{$date}) {
		for my $tagname (@{$tag_by_date{$date}}) {
			$$total_tags++;
			print "Tagged $tagname.\n" if (!$quiet);
		}
	}
 
	if (exists $patch_by_date{$date}) {
		my $count = scalar @{$patch_by_date{$date}};
		my %names = ();
		for my $patch (map { $patch{$_} } (@{$patch_by_date{$date}})) {
			my $name = $patch->[1];
			$names{$name}++;
			$total_names->{$name}++;
		}
		my $people = scalar @{[keys %names]};
		$$total_patches += $count;
 
		$count = plural($count, "patch", "patches");
		$people = plural($people, "person", "people");
		print "Queued $count from $people.\n" if (!$quiet);
		if ($verbose) {
			for my $patch (map { $patch{$_} } @{$patch_by_date{$date}}) {
				print "  $patch->[2]\n";
			}
		}
	}
 
	for my $branch_data (@integrate) {
		my ($branch, $purpose) = @{$branch_data};
		next unless (exists $merge_by_branch_date{$branch}{$date});
		my $merges = $merge_by_branch_date{$branch}{$date};
		my $count = scalar @$merges;
		next unless $count;
 
		$total_merges->{$branch} ||= 0;
		$total_merges->{$branch} += $count;
		$count = plural($count, "topic", "topics");
		print "Merged $count to '$branch' branch$purpose.\n" if (!$quiet);
		if ($verbose) {
			my @pieces = map { $patch{$_}->[2] . "," } @$merges;
			$pieces[-1] =~ s/,$/./;
			print fmt_join("  ", 72, " ", @pieces);
		}
	}
	$sep = "\n" if (!$quiet);
}
 
sub range_summary {
	my ($range, $bottom, $date, $total_n, $total_m, $total_p, $total_t) = @_;
	(my $last_date, undef) = prev_date($date);
 
	print "$sep$range $bottom..$last_date\n";
 
	if ($total_t) {
		my $count = plural($total_t, "release", "releases");
		print "Tagged $count.\n";
	}
	if ($total_p) {
		my $people = plural(scalar @{[keys %{$total_n}]}, "person", "people");
		my$count = plural($total_p, "patch", "patches");
		print "Queued $count from $people.\n";
	}
	for my $branch_data (@integrate) {
		my ($branch, $purpose) = @{$branch_data};
		next unless $total_m->{$branch};
		my $count = plural($total_m->{$branch}, "merge", "merges");
		print "Made $count to '$branch' branch$purpose.\n";
	}
	$sep = "\n";
}
 
sub weekly_summary {
	my ($bottom, $total_names, $total_merges,
	    $total_patches, $total_tags) = @_;
	my $date = $bottom;
	my $shown = 0;
 
	my ($total_p, $total_t, %total_n, %total_m) = (0, 0);
	for (my $i = 0; $i < 7; $i++) {
		day_summary($date, \%total_n, \%total_m,
			    \$total_p, \$total_t);
		($date, undef) = next_date($date);
	}
	for my $name (keys %total_n) {
		$total_names->{$name}++;
		$shown++;
	}
	for my $merge (keys %total_m) {
		$total_merges->{$merge}++;
		$shown++;
	}
	$$total_patches += $total_p;
	$$total_tags += $total_t;
	if ($shown) {
		range_summary("Week of", $bottom, $date,
			      \%total_n, \%total_m, $total_p, $total_t);
	}
	return $date;
}
 
my %total_names;
my %total_merges;
my $total_patches = 0;
my $total_tags = 0;
 
my $date;
for ($date = $bottom_date; $date le $reporting_date; ) {
	$date = weekly_summary($date, \%total_names, \%total_merges,
			       \$total_patches, \$total_tags);
}
 
if ($weeks) {
	range_summary("Between", $bottom_date, $date,
		      \%total_names, \%total_merges,
		      $total_patches, $total_tags);
}