@matheusbsbs / matheus-bagliano / commits / e72c0eaf9b
Fork 0

gitweb: Add combined diff support to git_patchset_body

Calling convention for combined diff similar to the one for git_difftree_body subroutine: difftree info (first parameter) must be result of calling git-diff-tree with -c/--cc option, and all parents of a commit must be passed as last parameters. See also description in "gitweb: Add combined diff support to git_difftree_body" This ability is not used yet. Generating "src" file name for renames in combined diff was separated into fill_from_file_info subroutine; git_difftree_body was modified to use it. Currently git_difftree_body and git_patchset_body fills this info separately. The from-file line in two-line from-file/to-file header is not hyperlinked: there can be more than one "from"/"src" file. This differs from HTML output of ordinary (not combined) diff. format_diff_line subroutine needs extra $from/$to parameters to format combined diff patch line correctly. Signed-off-by: Jakub Narebski <jnareb@gmail.com> Signed-off-by: Junio C Hamano <junkio@cox.net>

Jakub Narebski committed May 7, 2007 at 01:10 UTC e72c0eaf9bbdb2a9467f3f026b39b1c1755c1105
1 file changed +181 -40
gitweb/gitweb.perl
+181 -40
@@ -897,19 +897,34 @@ sub format_subject_html {
897 sub format_diff_line {
898 my $line = shift;
899 my ($from, $to) = @_;
900 - my $char = substr($line, 0, 1);
900 my $diff_class = "";
901
902 chomp $line;
903
905 - if ($char eq '+') {
906 - $diff_class = " add";
907 - } elsif ($char eq "-") {
908 - $diff_class = " rem";
909 - } elsif ($char eq "@") {
910 - $diff_class = " chunk_header";
911 - } elsif ($char eq "\\") {
912 - $diff_class = " incomplete";
904 + if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
905 + # combined diff
906 + my $prefix = substr($line, 0, scalar @{$from->{'href'}});
907 + if ($line =~ m/^\@{3}/) {
908 + $diff_class = " chunk_header";
909 + } elsif ($line =~ m/^\\/) {
910 + $diff_class = " incomplete";
911 + } elsif ($prefix =~ tr/+/+/) {
912 + $diff_class = " add";
913 + } elsif ($prefix =~ tr/-/-/) {
914 + $diff_class = " rem";
915 + }
916 + } else {
917 + # assume ordinary diff
918 + my $char = substr($line, 0, 1);
919 + if ($char eq '+') {
920 + $diff_class = " add";
921 + } elsif ($char eq '-') {
922 + $diff_class = " rem";
923 + } elsif ($char eq '@') {
924 + $diff_class = " chunk_header";
925 + } elsif ($char eq "\\") {
926 + $diff_class = " incomplete";
927 + }
928 }
929 $line = untabify($line);
930 if ($from && $to && $line =~ m/^\@{2} /) {
@@ -930,6 +945,39 @@ sub format_diff_line {
945 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
946 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
947 return "<div class=\"diff$diff_class\">$line</div>\n";
948 + } elsif ($from && $to && $line =~ m/^\@{3}/) {
949 + my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
950 + my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
951 +
952 + @from_text = split(' ', $ranges);
953 + for (my $i = 0; $i < @from_text; ++$i) {
954 + ($from_start[$i], $from_nlines[$i]) =
955 + (split(',', substr($from_text[$i], 1)), 0);
956 + }
957 +
958 + $to_text = pop @from_text;
959 + $to_start = pop @from_start;
960 + $to_nlines = pop @from_nlines;
961 +
962 + $line = "<span class=\"chunk_info\">$prefix ";
963 + for (my $i = 0; $i < @from_text; ++$i) {
964 + if ($from->{'href'}[$i]) {
965 + $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
966 + -class=>"list"}, $from_text[$i]);
967 + } else {
968 + $line .= $from_text[$i];
969 + }
970 + $line .= " ";
971 + }
972 + if ($to->{'href'}) {
973 + $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
974 + -class=>"list"}, $to_text);
975 + } else {
976 + $line .= $to_text;
977 + }
978 + $line .= " $prefix</span>" .
979 + "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
980 + return "<div class=\"diff$diff_class\">$line</div>\n";
981 }
982 return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
983 }
@@ -2233,6 +2281,39 @@ sub git_print_tree_entry {
2281 ## ......................................................................
2282 ## functions printing large fragments of HTML
2283
2284 +sub fill_from_file_info {
2285 + my ($diff, @parents) = @_;
2286 +
2287 + $diff->{'from_file'} = [ ];
2288 + $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
2289 + for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
2290 + if ($diff->{'status'}[$i] eq 'R' ||
2291 + $diff->{'status'}[$i] eq 'C') {
2292 + $diff->{'from_file'}[$i] =
2293 + git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
2294 + }
2295 + }
2296 +
2297 + return $diff;
2298 +}
2299 +
2300 +# parameters can be strings, or references to arrays of strings
2301 +sub from_ids_eq {
2302 + my ($a, $b) = @_;
2303 +
2304 + if (ref($a) eq "ARRAY" && ref($b) eq "ARRAY" && @$a == @$b) {
2305 + for (my $i = 0; $i < @$a; ++$i) {
2306 + return 0 unless ($a->[$i] eq $b->[$i]);
2307 + }
2308 + return 1;
2309 + } elsif (!ref($a) && !ref($b)) {
2310 + return $a eq $b;
2311 + } else {
2312 + return 0;
2313 + }
2314 +}
2315 +
2316 +
2317 sub git_difftree_body {
2318 my ($difftree, $hash, @parents) = @_;
2319 my ($parent) = $parents[0];
@@ -2260,6 +2341,8 @@ sub git_difftree_body {
2341
2342 if (exists $diff{'nparents'}) { # combined diff
2343
2344 + fill_from_file_info(\%diff, @parents);
2345 +
2346 if ($diff{'to_id'} ne ('0' x 40)) {
2347 # file exists in the result (child) commit
2348 print "<td>" .
@@ -2288,16 +2371,12 @@ sub git_difftree_body {
2371 for (my $i = 0; $i < $diff{'nparents'}; $i++) {
2372 my $hash_parent = $parents[$i];
2373 my $from_hash = $diff{'from_id'}[$i];
2291 - my $from_path = undef;
2374 + my $from_path = $diff{'from_file'}[$i];
2375 my $status = $diff{'status'}[$i];
2376
2377 $has_history ||= ($status ne 'A');
2378 $not_deleted ||= ($status ne 'D');
2379
2297 - if ($status eq 'R' || $status eq 'C') {
2298 - $from_path = git_get_path_by_hash($hash_parent, $from_hash);
2299 - }
2300 -
2380 if ($status eq 'A') {
2381 print "<td class=\"link\" align=\"right\"> | </td>\n";
2382 } elsif ($status eq 'D') {
@@ -2517,7 +2596,8 @@ sub git_difftree_body {
2596 }
2597
2598 sub git_patchset_body {
2520 - my ($fd, $difftree, $hash, $hash_parent) = @_;
2599 + my ($fd, $difftree, $hash, @hash_parents) = @_;
2600 + my ($hash_parent) = $hash_parents[0];
2601
2602 my $patch_idx = 0;
2603 my $patch_number = 0;
@@ -2555,6 +2635,9 @@ sub git_patchset_body {
2635 if ($patch_line =~ m/^index ([0-9a-fA-F]{40})..([0-9a-fA-F]{40})/) {
2636 $from_id = $1;
2637 $to_id = $2;
2638 + } elsif ($patch_line =~ m/^index ((?:[0-9a-fA-F]{40},)+[0-9a-fA-F]{40})..([0-9a-fA-F]{40})/) {
2639 + $from_id = [ split(',', $1) ];
2640 + $to_id = $2;
2641 }
2642
2643 push @diff_header, $patch_line;
@@ -2564,8 +2647,8 @@ sub git_patchset_body {
2647 # check if current patch belong to current raw line
2648 # and parse raw git-diff line if needed
2649 if (defined $diffinfo &&
2567 - $diffinfo->{'from_id'} eq $from_id &&
2568 - $diffinfo->{'to_id'} eq $to_id) {
2650 + from_ids_eq($diffinfo->{'from_id'}, $from_id) &&
2651 + $diffinfo->{'to_id'} eq $to_id) {
2652 # this is split patch
2653 print "<div class=\"patch cont\">\n";
2654 } else {
@@ -2579,15 +2662,34 @@ sub git_patchset_body {
2662 } else {
2663 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
2664 }
2582 - $from{'file'} = $diffinfo->{'from_file'} || $diffinfo->{'file'};
2583 - $to{'file'} = $diffinfo->{'to_file'} || $diffinfo->{'file'};
2584 - if ($diffinfo->{'status'} ne "A") { # not new (added) file
2585 - $from{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2586 - hash=>$diffinfo->{'from_id'},
2587 - file_name=>$from{'file'});
2665 + if ($diffinfo->{'nparents'}) {
2666 + # combined diff
2667 + $from{'file'} = [];
2668 + $from{'href'} = [];
2669 + fill_from_file_info($diffinfo, @hash_parents)
2670 + unless exists $diffinfo->{'from_file'};
2671 + for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2672 + $from{'file'}[$i] = $diffinfo->{'from_file'}[$i] || $diffinfo->{'to_file'};
2673 + if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
2674 + $from{'href'}[$i] = href(action=>"blob",
2675 + hash_base=>$hash_parents[$i],
2676 + hash=>$diffinfo->{'from_id'}[$i],
2677 + file_name=>$from{'file'}[$i]);
2678 + } else {
2679 + $from{'href'}[$i] = undef;
2680 + }
2681 + }
2682 } else {
2589 - delete $from{'href'};
2683 + $from{'file'} = $diffinfo->{'from_file'} || $diffinfo->{'file'};
2684 + if ($diffinfo->{'status'} ne "A") { # not new (added) file
2685 + $from{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2686 + hash=>$diffinfo->{'from_id'},
2687 + file_name=>$from{'file'});
2688 + } else {
2689 + delete $from{'href'};
2690 + }
2691 }
2692 + $to{'file'} = $diffinfo->{'to_file'} || $diffinfo->{'file'};
2693 if ($diffinfo->{'status'} ne "D") { # not deleted file
2694 $to{'href'} = href(action=>"blob", hash_base=>$hash,
2695 hash=>$diffinfo->{'to_id'},
@@ -2602,19 +2704,34 @@ sub git_patchset_body {
2704
2705 # print "git diff" header
2706 $patch_line = shift @diff_header;
2605 - $patch_line =~ s!^(diff (.*?) )"?a/.*$!$1!;
2606 - if ($from{'href'}) {
2607 - $patch_line .= $cgi->a({-href => $from{'href'}, -class => "path"},
2608 - 'a/' . esc_path($from{'file'}));
2609 - } else { # file was added
2610 - $patch_line .= 'a/' . esc_path($from{'file'});
2611 - }
2612 - $patch_line .= ' ';
2613 - if ($to{'href'}) {
2614 - $patch_line .= $cgi->a({-href => $to{'href'}, -class => "path"},
2615 - 'b/' . esc_path($to{'file'}));
2616 - } else { # file was deleted
2617 - $patch_line .= 'b/' . esc_path($to{'file'});
2707 + if ($diffinfo->{'nparents'}) {
2708 +
2709 + # combined diff
2710 + $patch_line =~ s!^(diff (.*?) )"?.*$!$1!;
2711 + if ($to{'href'}) {
2712 + $patch_line .= $cgi->a({-href => $to{'href'}, -class => "path"},
2713 + esc_path($to{'file'}));
2714 + } else { # file was deleted
2715 + $patch_line .= esc_path($to{'file'});
2716 + }
2717 +
2718 + } else {
2719 +
2720 + $patch_line =~ s!^(diff (.*?) )"?a/.*$!$1!;
2721 + if ($from{'href'}) {
2722 + $patch_line .= $cgi->a({-href => $from{'href'}, -class => "path"},
2723 + 'a/' . esc_path($from{'file'}));
2724 + } else { # file was added
2725 + $patch_line .= 'a/' . esc_path($from{'file'});
2726 + }
2727 + $patch_line .= ' ';
2728 + if ($to{'href'}) {
2729 + $patch_line .= $cgi->a({-href => $to{'href'}, -class => "path"},
2730 + 'b/' . esc_path($to{'file'}));
2731 + } else { # file was deleted
2732 + $patch_line .= 'b/' . esc_path($to{'file'});
2733 + }
2734 +
2735 }
2736 print "<div class=\"diff header\">$patch_line</div>\n";
2737
@@ -2631,14 +2748,37 @@ sub git_patchset_body {
2748 $patch_line .= $cgi->a({-href=>$to{'href'}, -class=>"path"},
2749 esc_path($to{'file'}));
2750 }
2634 - # match <mode>
2751 + # match single <mode>
2752 if ($patch_line =~ m/\s(\d{6})$/) {
2753 $patch_line .= '<span class="info"> (' .
2754 file_type_long($1) .
2755 ')</span>';
2756 }
2757 # match <hash>
2641 - if ($patch_line =~ m/^index/) {
2758 + if ($patch_line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
2759 + # can match only for combined diff
2760 + $patch_line = 'index ';
2761 + for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2762 + if ($from{'href'}[$i]) {
2763 + $patch_line .= $cgi->a({-href=>$from{'href'}[$i],
2764 + -class=>"hash"},
2765 + substr($diffinfo->{'from_id'}[$i],0,7));
2766 + } else {
2767 + $patch_line .= '0' x 7;
2768 + }
2769 + # separator
2770 + $patch_line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
2771 + }
2772 + $patch_line .= '..';
2773 + if ($to{'href'}) {
2774 + $patch_line .= $cgi->a({-href=>$to{'href'}, -class=>"hash"},
2775 + substr($diffinfo->{'to_id'},0,7));
2776 + } else {
2777 + $patch_line .= '0' x 7;
2778 + }
2779 +
2780 + } elsif ($patch_line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
2781 + # can match only for ordinary diff
2782 my ($from_link, $to_link);
2783 if ($from{'href'}) {
2784 $from_link = $cgi->a({-href=>$from{'href'}, -class=>"hash"},
@@ -2674,7 +2814,8 @@ sub git_patchset_body {
2814 }
2815 next PATCH if ($patch_line =~ m/^diff /);
2816 #assert($patch_line =~ m/^---/) if DEBUG;
2677 - if ($from{'href'} && $patch_line =~ m!^--- "?a/!) {
2817 + if (!$diffinfo->{'nparents'} && # not from-file line for combined diff
2818 + $from{'href'} && $patch_line =~ m!^--- "?a/!) {
2819 $patch_line = '--- a/' .
2820 $cgi->a({-href=>$from{'href'}, -class=>"path"},
2821 esc_path($from{'file'}));