+sub anchor0 ($$$$$) {
+ my ($dst, $ctx, $linkify, $fn, $rest) = @_;
+
+ my $orig = $fn;
+
+ # normal git diffstat output is impossible to parse reliably
+ # without --numstat, and that isn't the default for format-patch.
+ # So only do best-effort handling of renames for common cases;
+ # which works well in practice. If projects put "=>", or trailing
+ # spaces in filenames, oh well :P
+ $fn =~ s/(?: *$DIFFSTAT_COMMENT)? *\z//so;
+ $fn =~ s/{(?:.+) => (.+)}/$1/ or $fn =~ s/.* => (.+)/$1/;
+ $fn = git_unquote($fn);
+
+ # long filenames will require us to walk backwards in anchor1
+ if ($fn =~ s!\A\.\.\./?!!) {
+ my $lp = $ctx->{-long_path} ||= {};
+ $lp->{$fn} = qr/\Q$fn\E\z/s;
+ }
+
+ if (my $attr = to_attr($ctx->{-apfx}.$fn)) {
+ $ctx->{-anchors}->{$attr} = 1;
+ my $spaces = ($orig =~ s/( +)\z//) ? $1 : '';
+ $$dst .= " <a\nid=i$attr\nhref=#$attr>" .
+ ascii_html($orig) . '</a>' . $spaces .
+ $linkify->to_html($rest);
+ return 1;
+ }
+ undef;
+}
+
+sub anchor1 ($$$$$) {
+ my ($dst, $ctx, $linkify, $pb, $s) = @_;
+ my $attr = to_attr($ctx->{-apfx}.$pb) or return;
+ my $line = $linkify->to_html($s);
+
+ my $ok = delete $ctx->{-anchors}->{$attr};
+
+ # unlikely, check the end of all long path names we captured:
+ unless ($ok) {
+ my $lp = $ctx->{-long_path} or return;
+ foreach my $fn (keys %$lp) {
+ $pb =~ $lp->{$fn} or next;
+
+ delete $lp->{$fn};
+ $attr = to_attr($ctx->{-apfx}.$fn) or return;
+ $ok = delete $ctx->{-anchors}->{$attr} or return;
+ last;
+ }
+ }
+ if ($ok && $line =~ s/^diff //) {
+ $$dst .= "<a\nhref=#i$attr\nid=$attr>diff</a> ".$line;
+ return 1;
+ }
+ undef
+}
+
+sub flush_diff ($$$) {
+ my ($dst, $ctx, $linkify) = @_;
+ my $diff = $ctx->{-diff};
+ my $spfx = $ctx->{-spfx};