aboutsummaryrefslogtreecommitdiffstats
path: root/UWC
blob: 7a513af2ee3a21793b02bf402130c61318427274 (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
#!/usr/bin/perl -w
#
# Update an older edition of What's Cooking with the latest data.
#
# Usage: UWC [--keep-master] [ old [ new ] ]
#
# Giving no parameter is the same as giving a single "-" to the command.
#
# The command reads the old edition of (annotated) "What's Cooking"
# message from "old", and "new".  If "old" is "-", it is read from
# the standard input.  If "new" is not specified, WC script is run
# and its output is used.
#
# An annotated "What's Cooking" message can have group header (a line
# that has the group name enclosed in "[" and "]"), and annotatation
# paragraphs after each topic's commit list, in addition to the bare
# "WC" output.
#
# The group headers, topics in each group and their order in the group,
# and annotation to topics are preserved from the "old" message.  The
# list of commits in each topic is replaced with the one taken from the
# "new" message.  Any topic in "new" that did not exist in "old" appear
# in "New Topics" group.  Also, topics that do not appear in the "new"
# message are marked with <<deleted>>, topics whose commit list are
# different from "old" are marked with <<updated from...>>>.
#
# Typically the maintainer would place the What's Cooking message
# previously sent in a buffer in Emacs, and filter the buffer contents
# with this script, to prepare an up-to-date message.

my $keep_master = 1;

sub parse_whats_cooking {
	my ($fh) = @_;
	my $head = undef;
	my $group = undef;
	my %wc = ("group list" => [], "topic hash" => {});
	my $topic;
	my $skipping_comment = 0;

	while (<$fh>) {
		if (/^-{40,}$/) {
			# Group separator
			next;
		}

		if (!defined $head) {
			if (/^Here are the topics that have been/) {
				$head = $_;
			}
			next;
		}

		if (/^<<.*>>$/) {
			next;
		}

		if ($skipping_comment) {
			if (/^>>$/) {
				$skipping_comment = 0;
			}
			next;
		}

		if (!$skipping_comment && /^<</) {
			$skipping_comment = 1;
			next;
		}

		if (/^\[(.*)\]$/) {
			$group = $1;
			push @{$wc{"group list"}}, $group;
			$wc{" $group"} = [];
			$topic = undef;
			next;
		}

		if (!defined $group) {
			if (/^\* (\S+) (\(.*\) \d+ commits?)$/) {
				# raw output
				$group = "Misc";
				push @{$wc{"group list"}}, $group;
				$wc{" $group"} = [];
			} else {
				$head .= $_;
				next;
			}
		}

		if (/^\* (\S+) (\(.*\) \d+ commits?)$/) {
			$topic = +{
				topic => $1,
				head => $_,
				names => "",
				text => "",
			};
			$wc{"topic hash"}{$topic->{"topic"}} = $topic;
			push @{$wc{" $group"}}, $topic;
			next;
		}

		if (/^ [-+.?*] / || /^   \S/) {
			$topic->{"names"} .= $_;
			next;
		}
		$topic->{"text"} .= $_;
	}

	for ($head) {
		s/\A\s+//s;
		s/\s+\Z//s;
	}
	$wc{"head text"} = $head;
	for $topic (values %{$wc{"topic hash"}}) {
		for ($topic->{"text"}) {
			s/\A\s+//s;
			s/\s+\Z//s;
		}
	}
	return \%wc;
}

sub print_whats_cooking {
	my ($wc) = @_;

	print $wc->{"head text"}, "\n";

	for my $group (@{$wc->{"group list"}}) {
		print "\n", "-" x 64, "\n";
		print "[$group]\n";
		for my $topic (@{$wc->{" $group"}}) {
			next if ($topic->{"head"} eq '');
			print "\n", $topic->{"head"};
			print $topic->{"names"};
			if ($topic->{"text"} ne '') {
				print "\n", $topic->{"text"}, "\n";
			}
		}
	}
}

sub delete_topic {
	my ($wc, $topic) = @_;
	$topic->{"status"} = "deleted";
}

sub merge_whats_cooking {
	my ($old_wc, $new_wc) = @_;
	my $group;
	my @gone = ();

	for $group (@{$old_wc->{"group list"}}) {
		for my $topic (@{$old_wc->{" $group"}}) {
			my $name = $topic->{"topic"};
			my $newtopic = delete $new_wc->{"topic hash"}{$name};

			if (!defined $newtopic) {
				push @gone, +{ @{[ %$topic ]} };
				$topic->{"text"} = "";
				$topic->{"names"} = "";
				$topic->{"head"} = "";
				next;
			}
			if (($newtopic->{"names"} ne $topic->{"names"}) ||
			    ($newtopic->{"head"} ne $topic->{"head"})) {
				my $text = ("<<updated from\n" .
					    $topic->{"head"} .
					    $topic->{"names"} . ">>");

				if ($topic->{"text"} ne '') {
					$text .= "\n\n" . $topic->{"text"};
				}
				for ($text) {
					s/\A\s+//s;
					s/\s+\Z//s;
				}
				$topic->{"text"} = $text;
				$topic->{"names"} = $newtopic->{"names"};
				$topic->{"head"} = $newtopic->{"head"};
			}
		}
	}


	$group = 'Graduated to "master"';
	if (!$keep_master) {
		print STDERR "Not Keeping Master\n";
		my $o = delete $old_wc->{" $group"};
		for (@$o) {
			print STDERR " Dropping: ", $_->{'topic'}, "\n";
		}
		print STDERR "Gone are\n";
		for (@gone) {
			print STDERR " Gone: ", $_->{'topic'}, "\n";
		}
	}
	if (@gone) {
		if (!exists $old_wc->{" $group"}) {
			unshift @{$old_wc->{"group list"}}, $group;
			$old_wc->{" $group"} = [];
		}
		push @{$old_wc->{" $group"}}, @gone;
	}
	if (%{$new_wc->{"topic hash"}}) {
		$group = "New Topics";
		if (!exists $old_wc->{" $group"}) {
			unshift @{$old_wc->{"group list"}}, $group;
			$old_wc->{" $group"} = [];
		}
		for my $topic (values %{$new_wc->{"topic hash"}}) {
			my $name = $topic->{"topic"};
			$old_wc->{"topic hash"}{$name} = $topic;
			push @{$old_wc->{" $group"}}, $topic;
			$topic->{"text"} = $topic->{"text"};
		}
	}
}

if (@ARGV == 0) {
	@ARGV = ('-');
} elsif ($ARGV[0] eq '--keep-master') {
	$keep_master = 1;
	shift;
}
if (@ARGV != 2 && @ARGV != 1) {
	die "Usage: $0 old [new]\n";
}

my ($old_wc, $new_wc);

if ($ARGV[0] eq '-') {
	*FH = *STDIN;
} else {
	open FH, "$ARGV[0]";
}
$old_wc = parse_whats_cooking(\*FH);
close FH;

if (@ARGV > 1) {
	open FH, "$ARGV[1]";
} else {
	open FH, "Meta/WC generate |";
}
$new_wc = parse_whats_cooking(\*FH);
close FH;

merge_whats_cooking($old_wc, $new_wc);
print_whats_cooking($old_wc);