+sub to_state ($$$) {
+ my ($dst, $state, $new_state) = @_;
+ $$dst .= '</span>' if $state2class[$state];
+ $_[1] = $new_state;
+ my $class = $state2class[$new_state] or return;
+ $$dst .= qq(<span\nclass="$class">);
+}
+
+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\.\.\./?!!) {
+ $ctx->{-long_path}->{$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 missing_diff_git_line ($$) {
+ my ($dctx, $pb) = @_;
+ # missing "diff --git ..."
+ $dctx->{path_b} = $pb;
+ $dctx->{Q} = '?b='.uri_escape_utf8($pb, UNSAFE);
+ my $pa = $dctx->{path_a};
+ if (defined($pa) && $pa ne $pb) {
+ $dctx->{Q} .= '&a='. uri_escape_utf8($pa, UNSAFE);
+ }
+}
+
+sub flush_diff ($$$) {
+ my ($dst, $ctx, $linkify) = @_;
+ my $diff = $ctx->{-diff};
+ my $spfx = $ctx->{-spfx};