diff options
author | Eric Wong <e@80x24.org> | 2016-03-14 18:20:16 +0000 |
---|---|---|
committer | Eric Wong <e@80x24.org> | 2016-04-05 18:58:27 +0000 |
commit | 2a89856c74a27eafa24ef3bd718455ea60581eb6 (patch) | |
tree | a89bbabad1d164384be46349056bd4bb69884c1d /lib/PublicInbox/RepobrowseGitDiff.pm | |
parent | 89abb41ac9a2ca0a4fff0ccf75edf0bc1d050d61 (diff) | |
download | public-inbox-2a89856c74a27eafa24ef3bd718455ea60581eb6.tar.gz |
This reduces the amount of duplicated code between the "diff" and "commit" views of repobrowse.
Diffstat (limited to 'lib/PublicInbox/RepobrowseGitDiff.pm')
-rw-r--r-- | lib/PublicInbox/RepobrowseGitDiff.pm | 129 |
1 files changed, 4 insertions, 125 deletions
diff --git a/lib/PublicInbox/RepobrowseGitDiff.pm b/lib/PublicInbox/RepobrowseGitDiff.pm index 7e137adb..3459ec5f 100644 --- a/lib/PublicInbox/RepobrowseGitDiff.pm +++ b/lib/PublicInbox/RepobrowseGitDiff.pm @@ -14,6 +14,8 @@ use warnings; use base qw(PublicInbox::RepobrowseBase); use PublicInbox::Hval qw(utf8_html to_attr); use PublicInbox::RepobrowseGit qw(git_unquote git_commit_title); +use PublicInbox::RepobrowseGitDiffCommon qw/git_diffstat_emit + git_diff_ab_index git_diff_ab_hdr git_diff_ab_hunk/; sub call_git_diff { my ($self, $req) = @_; @@ -92,44 +94,6 @@ sub call_git_diff { } } -sub git_diffstat_to_html ($$$) { - my ($req, $fh, undef) = @_; - my @stat = split("\0", $_[2]); # avoiding copy for $_[2] - my $nr = 0; - my ($nadd, $ndel) = (0, 0); - my $s = ''; - while (defined(my $l = shift @stat)) { - $l =~ s/\n?(\S+)\t+(\S+)\t+// or next; - my ($add, $del) = ($1, $2); - if ($add =~ /\A\d+\z/) { - $nadd += $add; - $ndel += $del; - $add = "+$add"; - $del = "-$del"; - } - my $num = sprintf('% 6s/%-6s', $del, $add); - if (length $l) { - my $anchor = to_attr(git_unquote($l)); - $req->{anchors}->{$anchor} = $l; - $l = utf8_html($l); - $l = qq(<a\nhref="#$anchor">$l</a>); - } else { - my $from = shift @stat; - my $to = shift @stat; - $l = git_diffstat_rename($req, $from, $to); - } - ++$nr; - $s .= ' '.$num."\t".$l."\n"; - } - $s .= "\n $nr "; - $s .= $nr == 1 ? 'file changed, ' : 'files changed, '; - $s .= $nadd; - $s .= $nadd == 1 ? ' insertion(+), ' : ' insertions(+), '; - $s .= $ndel; - $s .= $ndel == 1 ? " deletion(-)\n\n" : " deletions(-)\n\n"; - $fh->write($s); -} - sub git_diff_line_i { my ($req, $l) = @_; my $cmt = '[a-f0-9]+'; @@ -137,11 +101,9 @@ sub git_diff_line_i { if ($l =~ m{^diff --git ("?a/.+) ("?b/.+)$}) { # regular $l = git_diff_ab_hdr($req, $1, $2); } elsif ($l =~ /^index ($cmt)\.\.($cmt)(.*)$/o) { # regular - $l = git_diff_ab_index($req, $1, $2, $3); + $l = git_diff_ab_index($1, $2, $3); } elsif ($l =~ /^@@ (\S+) (\S+) @@(.*)$/) { # regular $l = git_diff_ab_hunk($req, $1, $2, $3); - } elsif ($l =~ /^index ($cmt,[^\.]+)\.\.($cmt)(.*)$/o) { # --cc - $l = git_diff_cc_index($req, $1, $2, $3); } else { $l = utf8_html($l); } @@ -154,7 +116,7 @@ sub git_diff_to_html { my ($stat, $buf) = split(/\0\0/, $req->{dbuf}, 2); return unless defined $buf; $req->{dbuf} = $buf; - git_diffstat_to_html($req, $fh, $stat); + git_diffstat_emit($req, $fh, $stat); $req->{diff_state} = 1; } my @buf = split(/\n/, $req->{dbuf}, -1); @@ -166,87 +128,4 @@ sub git_diff_to_html { } } -sub git_diffstat_rename { - my ($req, $from, $to) = @_; - my $anchor = to_attr(git_unquote($to)); - $req->{anchors}->{$anchor} = $to; - my @from = split('/', $from); - my @to = split('/', $to); - my $orig_to = $to; - my ($base, @base); - while (@to && @from && $to[0] eq $from[0]) { - push @base, shift(@to); - shift @from; - } - - $base = utf8_html(join('/', @base)) if @base; - $from = utf8_html(join('/', @from)); - $to = PublicInbox::Hval->utf8(join('/', @to), $orig_to); - my $tp = $to->as_path; - my $th = $to->as_html; - $to = qq(<a\nhref="#$anchor">$th</a>); - @base ? "$base/{$from => $to}" : "$from => $to"; -} - -# index abcdef89..01234567 -sub git_diff_ab_index { - my ($req, $xa, $xb, $end) = @_; - # not wasting bandwidth on links here, yet - # links in hunk headers are far more useful with line offsets - $end = utf8_html($end); - "index $xa..$xb$end"; -} - -# diff --git a/foo.c b/bar.c -sub git_diff_ab_hdr { - my ($req, $fa, $fb) = @_; - my $html_a = utf8_html($fa); - my $html_b = utf8_html($fb); - $fa = git_unquote($fa); - $fb = git_unquote($fb); - $fa =~ s!\Aa/!!; - $fb =~ s!\Ab/!!; - my $anchor = to_attr($fb); - delete $req->{anchors}->{$anchor}; - $fa = $req->{fa} = PublicInbox::Hval->utf8($fa); - $fb = $req->{fb} = PublicInbox::Hval->utf8($fb); - $req->{path_a} = $fa->as_path; - $req->{path_b} = $fb->as_path; - - # not wasting bandwidth on links here - # links in hunk headers are far more useful with line offsets - qq(<a\nhref=#D\nid="$anchor">diff</a> --git $html_a $html_b); -} - -# @@ -1,2 +3,4 @@ (regular diff) -sub git_diff_ab_hunk { - my ($req, $ca, $cb, $ctx) = @_; - my ($na) = ($ca =~ /\A-(\d+)/); - my ($nb) = ($cb =~ /\A\+(\d+)/); - - # we add "rel=nofollow" here to reduce load on search engines, here - my $rel = $req->{relcmd}; - my $rv = '@@ '; - if (defined($na) && $na == 0) { # new file - $rv .= $ca; - } else { - $na = defined $na ? "#n$na" : ''; - my $p = $req->{p}->[0]; - $rv .= qq(<a\nrel=nofollow); - $rv .= qq(\nhref="${rel}tree/$req->{path_a}?id=$p$na">); - $rv .= "$ca</a>"; - } - $rv .= ' '; - if (defined($nb) && $nb == 0) { # deleted file - $rv .= $cb; - } else { - my $h = $req->{h}; - $nb = defined $nb ? "#n$nb" : ''; - $rv .= qq(<a\nrel=nofollow); - $rv .= qq(\nhref="${rel}tree/$req->{path_b}?id=$h$nb">); - $rv .= "$cb</a>"; - } - $rv . ' @@' . utf8_html($ctx); -} - 1; |