What's cooking (2012/02 #07)
[alt-git.git] / cook
blob2c6ff93034f6b0b7d2a8cb5f1f28fe866afe2e38
1 #!/usr/bin/perl -w
2 # Maintain "what's cooking" messages
4 use strict;
6 my %reverts = ('next' => {
7 map { $_ => 1 } qw(
8 ) });
10 %reverts = ();
12 sub phrase_these {
13 my %uniq = ();
14 my (@u) = grep { $uniq{$_}++ == 0 } sort @_;
15 my @d = ();
16 for (my $i = 0; $i < @u; $i++) {
17 push @d, $u[$i];
18 if ($i == @u - 2) {
19 push @d, " and ";
20 } elsif ($i < @u - 2) {
21 push @d, ", ";
24 return join('', @d);
27 sub describe_relation {
28 my ($topic_info) = @_;
29 my @desc;
31 if (exists $topic_info->{'used'}) {
32 push @desc, ("is used by " .
33 phrase_these(@{$topic_info->{'used'}}));
36 if (exists $topic_info->{'uses'}) {
37 push @desc, ("uses " .
38 phrase_these(@{$topic_info->{'uses'}}));
41 if (exists $topic_info->{'shares'}) {
42 push @desc, ("is tangled with " .
43 phrase_these(@{$topic_info->{'shares'}}));
46 if (!@desc) {
47 return "";
50 return "(this branch " . join("; ", @desc) . ".)";
53 sub forks_from {
54 my ($topic, $fork, $forkee, @overlap) = @_;
55 my %ovl = map { $_ => 1 } (@overlap, @{$topic->{$forkee}{'log'}});
57 push @{$topic->{$fork}{'uses'}}, $forkee;
58 push @{$topic->{$forkee}{'used'}}, $fork;
59 @{$topic->{$fork}{'log'}} = (grep { !exists $ovl{$_} }
60 @{$topic->{$fork}{'log'}});
63 sub topic_relation {
64 my ($topic, $one, $two) = @_;
66 my $fh;
67 open($fh, '-|',
68 qw(git log --abbrev=7), "--format=%m %h",
69 "$one...$two", "^master")
70 or die "$!: open log --left-right";
71 my (@left, @right);
72 while (<$fh>) {
73 my ($sign, $sha1) = /^(.) (.*)/;
74 if ($sign eq '<') {
75 push @left, $sha1;
76 } elsif ($sign eq '>') {
77 push @right, $sha1;
80 close($fh) or die "$!: close log --left-right";
82 if (!@left) {
83 if (@right) {
84 forks_from($topic, $two, $one);
86 } elsif (!@right) {
87 forks_from($topic, $one, $two);
88 } else {
89 push @{$topic->{$one}{'shares'}}, $two;
90 push @{$topic->{$two}{'shares'}}, $one;
94 =head1
95 Inspect the current set of topics
97 Returns a hash:
99 $topic = {
100 $branchname => {
101 'tipdate' => date of the tip commit,
102 'desc' => description string,
103 'log' => [ $commit,... ],
107 =cut
109 sub get_commit {
110 my (@base) = qw(master next pu);
111 my $fh;
112 open($fh, '-|',
113 qw(git for-each-ref),
114 "--format=%(refname:short) %(committerdate:iso8601)",
115 "refs/heads/??/*")
116 or die "$!: open for-each-ref";
117 my @topic;
118 my %topic;
120 while (<$fh>) {
121 chomp;
122 my ($branch, $date) = /^(\S+) (.*)$/;
123 push @topic, $branch;
124 $date =~ s/ .*//;
125 $topic{$branch} = +{
126 log => [],
127 tipdate => $date,
130 close($fh) or die "$!: close for-each-ref";
132 my %base = map { $_ => undef } @base;
133 my %commit;
134 my $show_branch_batch = 20;
136 while (@topic) {
137 my @t = (@base, splice(@topic, 0, $show_branch_batch));
138 my $header_delim = '-' x scalar(@t);
139 my $contain_pat = '.' x scalar(@t);
140 open($fh, '-|', qw(git show-branch --sparse --sha1-name),
141 map { "refs/heads/$_" } @t)
142 or die "$!: open show-branch";
143 while (<$fh>) {
144 chomp;
145 if ($header_delim) {
146 if (/^$header_delim$/) {
147 $header_delim = undef;
149 next;
151 my ($contain, $sha1, $log) =
152 ($_ =~ /^($contain_pat) \[([0-9a-f]+)\] (.*)$/);
154 for (my $i = 0; $i < @t; $i++) {
155 my $branch = $t[$i];
156 my $sign = substr($contain, $i, 1);
157 next if ($sign eq ' ');
158 next if (substr($contain, 0, 1) ne ' ');
160 if (!exists $commit{$sha1}) {
161 $commit{$sha1} = +{
162 branch => {},
163 log => $log,
166 my $co = $commit{$sha1};
167 if (!exists $reverts{$branch}{$sha1}) {
168 $co->{'branch'}{$branch} = 1;
170 next if (exists $base{$branch});
171 push @{$topic{$branch}{'log'}}, $sha1;
174 close($fh) or die "$!: close show-branch";
177 my %shared;
178 for my $sha1 (keys %commit) {
179 my $sign;
180 my $co = $commit{$sha1};
181 if (exists $co->{'branch'}{'next'}) {
182 $sign = '+';
183 } elsif (exists $co->{'branch'}{'pu'}) {
184 $sign = '-';
185 } else {
186 $sign = '.';
188 $co->{'log'} = $sign . ' ' . $co->{'log'};
189 my @t = (sort grep { !exists $base{$_} }
190 keys %{$co->{'branch'}});
191 next if (@t < 2);
192 my $t = "@t";
193 $shared{$t} = 1;
196 for my $combo (keys %shared) {
197 my @combo = split(' ', $combo);
198 for (my $i = 0; $i < @combo - 1; $i++) {
199 for (my $j = $i + 1; $j < @combo; $j++) {
200 topic_relation(\%topic, $combo[$i], $combo[$j]);
205 open($fh, '-|',
206 qw(git log --first-parent --abbrev=7),
207 "--format=%ci %h %p :%s", "master..next")
208 or die "$!: open log master..next";
209 while (<$fh>) {
210 my ($date, $commit, $parent, $tips);
211 unless (($date, $commit, $parent, $tips) =
212 /^([-0-9]+) ..:..:.. .\d{4} (\S+) (\S+) ([^:]*):/) {
213 die "Oops: $_";
215 for my $tip (split(' ', $tips)) {
216 my $co = $commit{$tip};
217 next unless ($co->{'branch'}{'next'});
218 $co->{'merged'} = " (merged to 'next' on $date at $commit)";
221 close($fh) or die "$!: close log master..next";
223 for my $branch (keys %topic) {
224 my @log = ();
225 my $n = scalar(@{$topic{$branch}{'log'}});
226 if (!$n) {
227 delete $topic{$branch};
228 next;
229 } elsif ($n == 1) {
230 $n = "1 commit";
231 } else {
232 $n = "$n commits";
234 my $d = $topic{$branch}{'tipdate'};
235 my $head = "* $branch ($d) $n\n";
236 my @desc;
237 for (@{$topic{$branch}{'log'}}) {
238 my $co = $commit{$_};
239 if (exists $co->{'merged'}) {
240 push @desc, $co->{'merged'};
242 push @desc, $commit{$_}->{'log'};
244 my $list = join("\n", map { " " . $_ } @desc);
245 my $relation = describe_relation($topic{$branch});
246 $topic{$branch}{'desc'} = $head . $list;
247 if ($relation) {
248 $topic{$branch}{'desc'} .= "\n $relation";
252 return \%topic;
255 sub blurb_text {
256 my ($mon, $year, $issue, $dow, $date,
257 $master_at, $next_at, $text) = @_;
259 my $now_string = localtime;
260 my ($current_dow, $current_mon, $current_date, $current_year) =
261 ($now_string =~ /^(\w+) (\w+) (\d+) [\d:]+ (\d+)$/);
263 $mon ||= $current_mon;
264 $year ||= $current_year;
265 $issue ||= "01";
266 $dow ||= $current_dow;
267 $date ||= $current_date;
268 $master_at ||= '0' x 40;
269 $next_at ||= '0' x 40;
270 $text ||= <<'EOF';
271 Here are the topics that have been cooking. Commits prefixed with '-' are
272 only in 'pu' (proposed updates) while commits prefixed with '+' are in 'next'.
273 The ones marked with '.' do not appear in any of the integration branches,
274 but I am still holding onto them.
276 You can find the changes described here in the integration branches of the
277 repositories listed at
279 http://git-blame.blogspot.com/p/git-public-repositories.html
282 $text = <<EOF;
283 To: git\@vger.kernel.org
284 Subject: What's cooking in git.git ($mon $year, #$issue; $dow, $date)
285 X-master-at: $master_at
286 X-next-at: $next_at
288 What's cooking in git.git ($mon $year, #$issue; $dow, $date)
289 --------------------------------------------------
291 $text
293 $text =~ s/\n+\Z/\n/;
294 return $text;
297 my $blurb_match = <<'EOF';
298 To: .*
299 Subject: What's cooking in \S+ \((\w+) (\d+), #(\d+); (\w+), (\d+)\)
300 X-master-at: ([0-9a-f]{40})
301 X-next-at: ([0-9a-f]{40})
303 What's cooking in \S+ \(\1 \2, #\3; \4, \5\)
304 -{30,}
308 my $blurb = "b..l..u..r..b";
309 sub read_previous {
310 my ($fn) = @_;
311 my $fh;
312 my $section = undef;
313 my $serial = 1;
314 my $branch = $blurb;
315 my $last_empty = undef;
316 my (@section, %section, @branch, %branch, %description, @leader);
317 my $in_unedited_olde = 0;
319 if (!-r $fn) {
320 return +{
321 'section_list' => [],
322 'section_data' => {},
323 'topic_description' => {
324 $blurb => {
325 desc => undef,
326 text => blurb_text(),
332 open ($fh, '<', $fn) or die "$!: open $fn";
333 while (<$fh>) {
334 chomp;
335 if ($in_unedited_olde) {
336 if (/^>>$/) {
337 $in_unedited_olde = 0;
338 $_ = " | $_";
340 } elsif (/^<<$/) {
341 $in_unedited_olde = 1;
344 if ($in_unedited_olde) {
345 $_ = " | $_";
348 if (defined $section && /^-{20,}$/) {
349 $_ = "";
351 if (/^$/) {
352 $last_empty = 1;
353 next;
355 if (/^\[(.*)\]\s*$/) {
356 $section = $1;
357 $branch = undef;
358 if (!exists $section{$section}) {
359 push @section, $section;
360 $section{$section} = [];
362 next;
364 if (defined $section && /^\* (\S+) /) {
365 $branch = $1;
366 $last_empty = 0;
367 if (!exists $branch{$branch}) {
368 push @branch, [$branch, $section];
369 $branch{$branch} = 1;
371 push @{$section{$section}}, $branch;
373 if (defined $branch) {
374 my $was_last_empty = $last_empty;
375 $last_empty = 0;
376 if (!exists $description{$branch}) {
377 $description{$branch} = [];
379 if ($was_last_empty) {
380 push @{$description{$branch}}, "";
382 push @{$description{$branch}}, $_;
385 close($fh);
387 for my $branch (keys %description) {
388 my $ary = $description{$branch};
389 if ($branch eq $blurb) {
390 while (@{$ary} && $ary->[-1] =~ /^-{30,}$/) {
391 pop @{$ary};
393 $description{$branch} = +{
394 desc => undef,
395 text => join("\n", @{$ary}),
397 } else {
398 my @desc = ();
399 while (@{$ary}) {
400 my $elem = shift @{$ary};
401 last if ($elem eq '');
402 push @desc, $elem;
404 $description{$branch} = +{
405 desc => join("\n", @desc),
406 text => join("\n", @{$ary}),
411 return +{
412 section_list => \@section,
413 section_data => \%section,
414 topic_description => \%description,
418 sub write_cooking {
419 my ($fn, $cooking) = @_;
420 my $fh;
422 open($fh, '>', $fn) or die "$!: open $fn";
423 print $fh $cooking->{'topic_description'}{$blurb}{'text'};
425 for my $section_name (@{$cooking->{'section_list'}}) {
426 my $topic_list = $cooking->{'section_data'}{$section_name};
427 next if (!@{$topic_list});
429 print $fh "\n";
430 print $fh '-' x 50, "\n";
431 print $fh "[$section_name]\n";
432 for my $topic (@{$topic_list}) {
433 my $d = $cooking->{'topic_description'}{$topic};
435 print $fh "\n", $d->{'desc'}, "\n";
436 if ($d->{'text'}) {
437 print $fh "\n", $d->{'text'}, "\n";
441 close($fh);
444 my $graduated = 'Graduated to "master"';
445 my $new_topics = 'New Topics';
446 my $discarded = 'Discarded';
447 my $old_new_topics = 'Old New Topics';
449 sub update_issue {
450 my ($cooking) = @_;
451 my ($fh, $master_at, $next_at, $incremental);
453 open($fh, '-|',
454 qw(git for-each-ref),
455 "--format=%(refname:short) %(objectname)",
456 "refs/heads/master",
457 "refs/heads/next") or die "$!: open for-each-ref";
458 while (<$fh>) {
459 my ($branch, $at) = /^(\S+) (\S+)$/;
460 if ($branch eq 'master') { $master_at = $at; }
461 if ($branch eq 'next') { $next_at = $at; }
463 close($fh) or die "$!: close for-each-ref";
465 $incremental = ((-r "Meta/whats-cooking.txt") &&
466 system("cd Meta && " .
467 "git diff --quiet --no-ext-diff HEAD -- " .
468 "whats-cooking.txt"));
470 my $now_string = localtime;
471 my ($current_dow, $current_mon, $current_date, $current_year) =
472 ($now_string =~ /^(\w+) (\w+) +(\d+) [\d:]+ (\d+)$/);
474 my $btext = $cooking->{'topic_description'}{$blurb}{'text'};
475 if ($btext !~ s/\A$blurb_match//) {
476 die "match pattern broken?";
478 my ($mon, $year, $issue, $dow, $date) = ($1, $2, $3, $4, $5);
480 if ($current_mon ne $mon || $current_year ne $year) {
481 $issue = "01";
482 } elsif (!$incremental) {
483 $issue =~ s/^0*//;
484 $issue = sprintf "%02d", ($issue + 1);
486 $mon = $current_mon;
487 $year = $current_year;
488 $dow = $current_dow;
489 $date = $current_date;
491 $cooking->{'topic_description'}{$blurb}{'text'} =
492 blurb_text($mon, $year, $issue, $dow, $date,
493 $master_at, $next_at, $btext);
495 if (!$incremental) {
496 my $sd = $cooking->{'section_data'};
497 my $sl = $cooking->{'section_list'};
498 # Rename "New" to "Old New" and insert "New".
499 # Move "New" to "Old New"
500 my $i;
501 my $doneso;
502 for ($i = 0; $i < @{$sl}; $i++) {
503 if ($sl->[$i] eq $new_topics) {
504 $sl->[$i] = $old_new_topics;
505 unshift @{$sl}, $new_topics;
506 $doneso = 1;
507 last;
510 if ($doneso) {
511 $sd->{$old_new_topics} = $sd->{$new_topics};
513 $sd->{$new_topics} = [];
516 return $incremental;
519 sub topic_in_pu {
520 my ($topic_desc) = @_;
521 for my $line (split(/\n/, $topic_desc)) {
522 if ($line =~ /^ [+-] /) {
523 return 1;
526 return 0;
529 sub merge_cooking {
530 my ($cooking, $current) = @_;
531 my $td = $cooking->{'topic_description'};
532 my $sd = $cooking->{'section_data'};
533 my $sl = $cooking->{'section_list'};
534 my (@new_topic, @gone_topic);
536 # Make sure "New Topics" and "Graduated" exists
537 if (!exists $sd->{$new_topics}) {
538 $sd->{$new_topics} = [];
539 unshift @{$sl}, $new_topics;
542 if (!exists $sd->{$graduated}) {
543 $sd->{$graduated} = [];
544 unshift @{$sl}, $graduated;
547 my $incremental = update_issue($cooking);
549 for my $topic (sort keys %{$current}) {
550 if (!exists $td->{$topic}) {
551 # Ignore new topics without anything merged
552 if (topic_in_pu($current->{$topic}{'desc'})) {
553 push @new_topic, $topic;
555 next;
557 # Annotate if the contents of the topic changed
558 my $n = $current->{$topic}{'desc'};
559 my $o = $td->{$topic}{'desc'};
560 if ($n ne $o) {
561 $td->{$topic}{'desc'} = $n . "\n<<\n" . $o ."\n>>";
565 for my $topic (sort keys %{$td}) {
566 next if ($topic eq $blurb);
567 next if (!$incremental &&
568 grep { $topic eq $_ } @{$sd->{$graduated}});
569 next if (grep { $topic eq $_ } @{$sd->{$discarded}});
570 if (!exists $current->{$topic}) {
571 push @gone_topic, $topic;
575 for (@new_topic) {
576 push @{$sd->{$new_topics}}, $_;
577 $td->{$_}{'desc'} = $current->{$_}{'desc'};
580 if (!$incremental) {
581 $sd->{$graduated} = [];
584 if (@gone_topic) {
585 for my $topic (@gone_topic) {
586 for my $section (@{$sl}) {
587 my $pre = scalar(@{$sd->{$section}});
588 @{$sd->{$section}} = (grep { $_ ne $topic }
589 @{$sd->{$section}});
590 my $post = scalar(@{$sd->{$section}});
591 next if ($pre == $post);
594 for (@gone_topic) {
595 push @{$sd->{$graduated}}, $_;
600 ################################################################
601 # WilDo
602 sub wildo_queue {
603 my ($what, $action, $topic) = @_;
604 if (!exists $what->{$action}) {
605 $what->{$action} = [];
607 push @{$what->{$action}}, $topic;
610 sub section_action {
611 my ($section) = @_;
612 if ($section) {
613 for ($section) {
614 return if (/^Graduated to/ || /^Discarded$/);
615 return $_ if (/^Stalled$/);
618 return "Undecided";
621 sub wildo_flush_topic {
622 my ($in_section, $what, $topic) = @_;
623 if (defined $topic) {
624 my $action = section_action($in_section);
625 if ($action) {
626 wildo_queue($what, $action, $topic);
631 sub wildo {
632 my (%what, $topic, $last_merge_to_next, $in_section);
633 my $too_recent = '9999-99-99';
634 while (<>) {
635 chomp;
637 if (/^\[(.*)\]$/) {
638 my $old_section = $in_section;
639 $in_section = $1;
640 wildo_flush_topic($old_section, \%what, $topic);
641 $topic = undef;
642 next;
645 if (/^\* (\S+) \(([-0-9]+)\) (\d+) commits?$/) {
646 wildo_flush_topic($in_section, \%what, $topic);
648 # tip-date, next-date, topic, count, pu-count
649 $topic = [$2, $too_recent, $1, $3, 0];
650 next;
653 if (defined $topic &&
654 ($topic->[1] eq $too_recent) &&
655 ($topic->[4] == 0) &&
656 (/^ \(merged to 'next' on ([-0-9]+)/)) {
657 $topic->[1] = $1;
659 if (defined $topic && /^ - /) {
660 $topic->[4]++;
662 next if (/^ /);
663 next unless defined $topic;
665 if (/^Will (?:\S+ ){0,2}(keep|merge|drop|discard|cook|kick|defer)[,. ]/ ||
666 /^Not urgent/ || /^Not ready/ || /^Waiting for / ||
667 /^Needs? / || /Expecting /) {
668 wildo_queue(\%what, $_, $topic);
669 $topic = undef;
671 if (/^Not urgent;/) {
672 wildo_queue(\%what, $_, $topic);
673 $topic = undef;
675 if (/Originally merged to 'next' on ([-0-9]+)/) {
676 $topic->[1] = $1;
679 wildo_flush_topic($in_section, \%what, $topic);
681 my $ipbl = "";
682 for my $what (sort keys %what) {
683 print "$ipbl$what\n";
684 for $topic (sort { (($a->[1] cmp $b->[1]) ||
685 ($a->[0] cmp $b->[0])) }
686 @{$what{$what}}) {
687 my ($tip, $next, $name, $count, $pu) = @$topic;
688 my ($sign);
689 $tip =~ s/^\d{4}-//;
690 if (($next eq $too_recent) || (0 < $pu)) {
691 $sign = "-";
692 $next = " " x 6;
693 } else {
694 $sign = "+";
695 $next =~ s|^\d{4}-|/|;
697 $count = "#$count";
698 printf " %s %-60s %s%s %5s\n", $sign, $name, $tip, $next, $count;
700 $ipbl = "\n";
704 ################################################################
705 # WhatsCooking
707 sub doit {
708 my $topic = get_commit();
709 my $cooking = read_previous('Meta/whats-cooking.txt');
710 merge_cooking($cooking, $topic);
711 write_cooking('Meta/whats-cooking.txt', $cooking);
714 ################################################################
715 # Main
717 use Getopt::Long;
719 my $wildo;
720 if (!GetOptions("wildo" => \$wildo)) {
721 print STDERR "$0 [--wildo]";
722 exit 1;
725 if ($wildo) {
726 if (!@ARGV) {
727 push @ARGV, "Meta/whats-cooking.txt";
729 wildo();
730 } else {
731 doit();