Meta/cook: use b4 mbox to attempt to grab all messages in the thread

Junio C Hamano committed May 21, 2026 at 16:19 UTC 9784b6cfb65cc263b2a680693e950197d3f36ea9
1 file changed +32 -18
cook
+32 -18
@@ -124,33 +124,47 @@ sub topic_relation {
124 }
125 }
126
127 +my %msgid_to_in_reply_to = ();
128 +
129 sub get_message_parent {
130 my ($mid) = @_;
129 - my @line = ();
130 - my %irt = ();
131 + my $state = 0;
132 + my ($message_id, $in_reply_to);
133 +
134 + if (exists $msgid_to_in_reply_to{$mid}) {
135 + return $msgid_to_in_reply_to{$mid};
136 + }
137
132 - open(my $fh, "-|", qw(b4 -q mbox --single-message -o-), "$mid");
138 + open(my $fh, "-|", qw(b4 -q mbox -o-), "$mid");
139 while (<$fh>) {
134 - last if (/^$/);
140 chomp;
136 - if (/^\s/) {
137 - $line[-1] .= $_;
138 - } else {
139 - push @line, $_;
141 + if ($state == 0) {
142 + if (/^$/) {
143 + if (defined $message_id && defined $in_reply_to) {
144 + $msgid_to_in_reply_to{$message_id} = $in_reply_to;
145 + }
146 + $state = 1;
147 + $message_id = $in_reply_to = undef;
148 + } elsif (/^message-id:\s*<(.*)>\s*$/i) {
149 + $message_id = $1;
150 + } elsif (/^in-reply-to:\s*<(.*)>\s*/i) {
151 + $in_reply_to = $1;
152 + }
153 + next;
154 + } elsif ($state == 1) {
155 + if (/^From /) {
156 + $state = 0;
157 + }
158 + next;
159 }
160 }
142 - while (<$fh>) { # slurp
143 - }
161 close($fh);
145 - for (@line) {
146 - if (s/^in-reply-to:\s*//i) {
147 - while (/\s*<([^<]*)>\s*(.*)/) {
148 - $irt{$1} = $1;
149 - $_ = $2;
150 - }
151 - }
162 +
163 + if (exists $msgid_to_in_reply_to{$mid}) {
164 + return $msgid_to_in_reply_to{$mid};
165 + } else {
166 + return ();
167 }
153 - keys %irt;
168 }
169
170 sub get_source {