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

gitweb: Use git-diff-tree patch output for commitdiff

Get rid of git_diff_print invocation in git_commitdiff and therefore external diff (/usr/bin/diff) invocation, and use only git-diff-tree to generate patch. git_commitdiff and git_commitdiff_plain are collapsed into one subroutine git_commitdiff, with format (currently 'html' which is default format corresponding to git_commitdiff, and 'plain' corresponding to git_commitdiff_plain) specified in argument. Separate patch (diff) pretty-printing into git_patchset_body. It is used in git_commitdiff. Separate patch (diff) line formatting from git_diff_print into format_diff_line function. It is used in git_patchset_body. While at it, add $hash parameter to git_difftree_body, according to rule that inner functions should use parameter passing, and not global variables. CHANGES TO OUTPUT: * "commitdiff" now products patches with renaming and copying detection (git-diff-tree is invoked with -M and -C options). Empty patches (mode changes and pure renames and copying) are not written currently. Former version broke renaming and copying, and didn't notice mode changes, like this version. * "commitdiff" output is now divided into several div elements of class "log", "patchset" and "patch". * "commitdiff_plain" now only generates X-Git-Tag: line only if there is tag pointing to the current commit. Former version which wrote first tag following current commit was broken[*1*]; besides we are interested rather in tags _preceding_ the commit, and _heads_ following the commit. X-Git-Url: now is current URL; former version tried[*2*] to output URL to HTML version of commitdiff. * "commitdiff_plain" is generated by git-diff-tree, and has therefore has git specific extensions to diff format: "git diff" header and optional extended header lines. FOOTNOTES [*1*] First it generated rev-list starting from HEAD even if hash_base parameter was set, second it wasn't corrected according to changes made in git_get_references (formerly read_info_ref) output, third even for older version of read_info_ref output it didn't work for multiple tags pointing to the current commit (rare). [*2*] It wrote URL for commitdiff without hash_parent, which produces diff to first parent and is not the same as current diff if it is diff of merge commit to non-first parent. Signed-off-by: Jakub Narebski <jnareb@gmail.com> Signed-off-by: Junio C Hamano <junkio@cox.net>

Jakub Narebski committed Aug 24, 2006 at 00:15 UTC eee08903b2cb9cfaf9ee2cd69a255ace829ae7c5
1 file changed +201 -122
gitweb/gitweb.perl
+201 -122
@@ -524,6 +524,27 @@ sub format_subject_html {
524 }
525 }
526
527 +sub format_diff_line {
528 + my $line = shift;
529 + my $char = substr($line, 0, 1);
530 + my $diff_class = "";
531 +
532 + chomp $line;
533 +
534 + if ($char eq '+') {
535 + $diff_class = " add";
536 + } elsif ($char eq "-") {
537 + $diff_class = " rem";
538 + } elsif ($char eq "@") {
539 + $diff_class = " chunk_header";
540 + } elsif ($char eq "\\") {
541 + # skip errors (incomplete lines)
542 + return "";
543 + }
544 + $line = untabify($line);
545 + return "<div class=\"diff$diff_class\">" . esc_html($line) . "</div>\n";
546 +}
547 +
548 ## ----------------------------------------------------------------------
549 ## git utility subroutines, invoking git commands
550
@@ -1367,7 +1388,7 @@ sub git_print_simplified_log {
1388 ## functions printing large fragments of HTML
1389
1390 sub git_difftree_body {
1370 - my ($difftree, $parent) = @_;
1391 + my ($difftree, $hash, $parent) = @_;
1392
1393 print "<div class=\"list_head\">\n";
1394 if ($#{$difftree} > 10) {
@@ -1518,6 +1539,101 @@ sub git_difftree_body {
1539 print "</table>\n";
1540 }
1541
1542 +sub git_patchset_body {
1543 + my ($patchset, $difftree, $hash, $hash_parent) = @_;
1544 +
1545 + my $patch_idx = 0;
1546 + my $in_header = 0;
1547 + my $patch_found = 0;
1548 + my %diffinfo;
1549 +
1550 + print "<div class=\"patchset\">\n";
1551 +
1552 + LINE: foreach my $patch_line (@$patchset) {
1553 +
1554 + if ($patch_line =~ m/^diff /) { # "git diff" header
1555 + # beginning of patch (in patchset)
1556 + if ($patch_found) {
1557 + # close previous patch
1558 + print "</div>\n"; # class="patch"
1559 + } else {
1560 + # first patch in patchset
1561 + $patch_found = 1;
1562 + }
1563 + print "<div class=\"patch\">\n";
1564 +
1565 + %diffinfo = parse_difftree_raw_line($difftree->[$patch_idx++]);
1566 +
1567 + # for now, no extended header, hence we skip empty patches
1568 + # companion to next LINE if $in_header;
1569 + if ($diffinfo{'from_id'} eq $diffinfo{'to_id'}) { # no change
1570 + $in_header = 1;
1571 + next LINE;
1572 + }
1573 +
1574 + if ($diffinfo{'status'} eq "A") { # added
1575 + print "<div class=\"diff_info\">" . file_type($diffinfo{'to_mode'}) . ":" .
1576 + $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1577 + hash=>$diffinfo{'to_id'}, file_name=>$diffinfo{'file'})},
1578 + $diffinfo{'to_id'}) . "(new)" .
1579 + "</div>\n"; # class="diff_info"
1580 +
1581 + } elsif ($diffinfo{'status'} eq "D") { # deleted
1582 + print "<div class=\"diff_info\">" . file_type($diffinfo{'from_mode'}) . ":" .
1583 + $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1584 + hash=>$diffinfo{'from_id'}, file_name=>$diffinfo{'file'})},
1585 + $diffinfo{'from_id'}) . "(deleted)" .
1586 + "</div>\n"; # class="diff_info"
1587 +
1588 + } elsif ($diffinfo{'status'} eq "R" || # renamed
1589 + $diffinfo{'status'} eq "C") { # copied
1590 + print "<div class=\"diff_info\">" .
1591 + file_type($diffinfo{'from_mode'}) . ":" .
1592 + $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1593 + hash=>$diffinfo{'from_id'}, file_name=>$diffinfo{'from_file'})},
1594 + $diffinfo{'from_id'}) .
1595 + " -> " .
1596 + file_type($diffinfo{'to_mode'}) . ":" .
1597 + $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1598 + hash=>$diffinfo{'to_id'}, file_name=>$diffinfo{'to_file'})},
1599 + $diffinfo{'to_id'});
1600 + print "</div>\n"; # class="diff_info"
1601 +
1602 + } else { # modified, mode changed, ...
1603 + print "<div class=\"diff_info\">" .
1604 + file_type($diffinfo{'from_mode'}) . ":" .
1605 + $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1606 + hash=>$diffinfo{'from_id'}, file_name=>$diffinfo{'file'})},
1607 + $diffinfo{'from_id'}) .
1608 + " -> " .
1609 + file_type($diffinfo{'to_mode'}) . ":" .
1610 + $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1611 + hash=>$diffinfo{'to_id'}, file_name=>$diffinfo{'file'})},
1612 + $diffinfo{'to_id'});
1613 + print "</div>\n"; # class="diff_info"
1614 + }
1615 +
1616 + #print "<div class=\"diff extended_header\">\n";
1617 + $in_header = 1;
1618 + next LINE;
1619 + } # start of patch in patchset
1620 +
1621 +
1622 + if ($in_header && $patch_line =~ m/^---/) {
1623 + #print "</div>\n"
1624 + $in_header = 0;
1625 + }
1626 + next LINE if $in_header;
1627 +
1628 + print format_diff_line($patch_line);
1629 + }
1630 + print "</div>\n" if $patch_found; # class="patch"
1631 +
1632 + print "</div>\n"; # class="patchset"
1633 +}
1634 +
1635 +# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1636 +
1637 sub git_shortlog_body {
1638 # uses global variable $project
1639 my ($revlist, $from, $to, $refs, $extra) = @_;
@@ -2556,7 +2672,7 @@ sub git_commit {
2672 git_print_log($co{'comment'});
2673 print "</div>\n";
2674
2559 - git_difftree_body(\@difftree, $parent);
2675 + git_difftree_body(\@difftree, $hash, $parent);
2676
2677 git_footer_html();
2678 }
@@ -2600,7 +2716,7 @@ sub git_blobdiff_plain {
2716 }
2717
2718 sub git_commitdiff {
2603 - mkdir($git_temp, 0700);
2719 + my $format = shift || 'html';
2720 my %co = parse_commit($hash);
2721 if (!%co) {
2722 die_error(undef, "Unknown commit object");
@@ -2608,143 +2724,106 @@ sub git_commitdiff {
2724 if (!defined $hash_parent) {
2725 $hash_parent = $co{'parent'} || '--root';
2726 }
2611 - open my $fd, "-|", $GIT, "diff-tree", '-r', $hash_parent, $hash
2612 - or die_error(undef, "Open git-diff-tree failed");
2613 - my @difftree = map { chomp; $_ } <$fd>;
2614 - close $fd or die_error(undef, "Reading git-diff-tree failed");
2727 +
2728 + # read commitdiff
2729 + my $fd;
2730 + my @difftree;
2731 + my @patchset;
2732 + if ($format eq 'html') {
2733 + open $fd, "-|", $GIT, "diff-tree", '-r', '-M', '-C',
2734 + "--patch-with-raw", "--full-index", $hash_parent, $hash
2735 + or die_error(undef, "Open git-diff-tree failed");
2736 +
2737 + while (chomp(my $line = <$fd>)) {
2738 + # empty line ends raw part of diff-tree output
2739 + last unless $line;
2740 + push @difftree, $line;
2741 + }
2742 + @patchset = map { chomp; $_ } <$fd>;
2743 +
2744 + close $fd
2745 + or die_error(undef, "Reading git-diff-tree failed");
2746 + } elsif ($format eq 'plain') {
2747 + open $fd, "-|", $GIT, "diff-tree", '-r', '-p', '-B', $hash_parent, $hash
2748 + or die_error(undef, "Open git-diff-tree failed");
2749 + } else {
2750 + die_error(undef, "Unknown commitdiff format");
2751 + }
2752
2753 # non-textual hash id's can be cached
2754 my $expires;
2755 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2756 $expires = "+1d";
2757 }
2621 - my $refs = git_get_references();
2622 - my $ref = format_ref_marker($refs, $co{'id'});
2623 - my $formats_nav =
2624 - $cgi->a({-href => href(action=>"commitdiff_plain", hash=>$hash, hash_parent=>$hash_parent)},
2625 - "plain");
2626 - git_header_html(undef, $expires);
2627 - git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
2628 - git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
2629 - print "<div class=\"page_body\">\n";
2630 - git_print_simplified_log($co{'comment'}, 1); # skip title
2631 - print "<br/>\n";
2632 - foreach my $line (@difftree) {
2633 - # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
2634 - # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
2635 - if ($line !~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
2636 - next;
2637 - }
2638 - my $from_mode = $1;
2639 - my $to_mode = $2;
2640 - my $from_id = $3;
2641 - my $to_id = $4;
2642 - my $status = $5;
2643 - my $file = validate_input(unquote($6));
2644 - if ($status eq "A") {
2645 - print "<div class=\"diff_info\">" . file_type($to_mode) . ":" .
2646 - $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2647 - hash=>$to_id, file_name=>$file)},
2648 - $to_id) . "(new)" .
2649 - "</div>\n";
2650 - git_diff_print(undef, "/dev/null", $to_id, "b/$file");
2651 - } elsif ($status eq "D") {
2652 - print "<div class=\"diff_info\">" . file_type($from_mode) . ":" .
2653 - $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2654 - hash=>$from_id, file_name=>$file)},
2655 - $from_id) . "(deleted)" .
2656 - "</div>\n";
2657 - git_diff_print($from_id, "a/$file", undef, "/dev/null");
2658 - } elsif ($status eq "M") {
2659 - if ($from_id ne $to_id) {
2660 - print "<div class=\"diff_info\">" .
2661 - file_type($from_mode) . ":" .
2662 - $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2663 - hash=>$from_id, file_name=>$file)},
2664 - $from_id) .
2665 - " -> " .
2666 - file_type($to_mode) . ":" .
2667 - $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2668 - hash=>$to_id, file_name=>$file)},
2669 - $to_id);
2670 - print "</div>\n";
2671 - git_diff_print($from_id, "a/$file", $to_id, "b/$file");
2672 - }
2673 - }
2674 - }
2675 - print "<br/>\n" .
2676 - "</div>";
2677 - git_footer_html();
2678 -}
2758
2680 -sub git_commitdiff_plain {
2681 - mkdir($git_temp, 0700);
2682 - my %co = parse_commit($hash);
2683 - if (!%co) {
2684 - die_error(undef, "Unknown commit object");
2685 - }
2686 - if (!defined $hash_parent) {
2687 - $hash_parent = $co{'parent'} || '--root';
2688 - }
2689 - open my $fd, "-|", $GIT, "diff-tree", '-r', $hash_parent, $hash
2690 - or die_error(undef, "Open git-diff-tree failed");
2691 - my @difftree = map { chomp; $_ } <$fd>;
2692 - close $fd or die_error(undef, "Reading diff-tree failed");
2693 -
2694 - # try to figure out the next tag after this commit
2695 - my $tagname;
2696 - my $refs = git_get_references("tags");
2697 - open $fd, "-|", $GIT, "rev-list", "HEAD";
2698 - my @commits = map { chomp; $_ } <$fd>;
2699 - close $fd;
2700 - foreach my $commit (@commits) {
2701 - if (defined $refs->{$commit}) {
2702 - $tagname = $refs->{$commit}
2703 - }
2704 - if ($commit eq $hash) {
2705 - last;
2706 - }
2707 - }
2759 + # write commit message
2760 + if ($format eq 'html') {
2761 + my $refs = git_get_references();
2762 + my $ref = format_ref_marker($refs, $co{'id'});
2763 + my $formats_nav =
2764 + $cgi->a({-href => href(action=>"commitdiff_plain",
2765 + hash=>$hash, hash_parent=>$hash_parent)},
2766 + "plain");
2767
2709 - print $cgi->header(-type => "text/plain",
2710 - -charset => 'utf-8',
2711 - -content_disposition => "inline; filename=\"git-$hash.patch\"");
2712 - my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
2713 - my $comment = $co{'comment'};
2714 - print <<TEXT;
2768 + git_header_html(undef, $expires);
2769 + git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
2770 + git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
2771 + print "<div class=\"page_body\">\n";
2772 + print "<div class=\"log\">\n";
2773 + git_print_simplified_log($co{'comment'}, 1); # skip title
2774 + print "</div>\n"; # class="log"
2775 +
2776 + } elsif ($format eq 'plain') {
2777 + my $refs = git_get_references("tags");
2778 + my @tagnames;
2779 + if (exists $refs->{$hash}) {
2780 + @tagnames = map { s|^tags/|| } $refs->{$hash};
2781 + }
2782 + my $filename = basename($project) . "-$hash.patch";
2783 +
2784 + print $cgi->header(
2785 + -type => 'text/plain',
2786 + -charset => 'utf-8',
2787 + -expires => $expires,
2788 + -content_disposition => qq(inline; filename="$filename"));
2789 + my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
2790 + print <<TEXT;
2791 From: $co{'author'}
2792 Date: $ad{'rfc2822'} ($ad{'tz_local'})
2793 Subject: $co{'title'}
2794 TEXT
2719 - if (defined $tagname) {
2720 - print "X-Git-Tag: $tagname\n";
2795 + foreach my $tag (@tagnames) {
2796 + print "X-Git-Tag: $tag\n";
2797 + }
2798 + print "X-Git-Url: " . $cgi->self_url() . "\n\n";
2799 + foreach my $line (@{$co{'comment'}}) {
2800 + print "$line\n";
2801 + }
2802 + print "---\n\n";
2803 }
2722 - print "X-Git-Url: $my_url?p=$project;a=commitdiff;h=$hash\n" .
2723 - "\n";
2804
2725 - foreach my $line (@$comment) {;
2726 - print "$line\n";
2727 - }
2728 - print "---\n\n";
2805 + # write patch
2806 + if ($format eq 'html') {
2807 + #git_difftree_body(\@difftree, $hash, $hash_parent);
2808 + #print "<br/>\n";
2809
2730 - foreach my $line (@difftree) {
2731 - if ($line !~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
2732 - next;
2733 - }
2734 - my $from_id = $3;
2735 - my $to_id = $4;
2736 - my $status = $5;
2737 - my $file = $6;
2738 - if ($status eq "A") {
2739 - git_diff_print(undef, "/dev/null", $to_id, "b/$file", "plain");
2740 - } elsif ($status eq "D") {
2741 - git_diff_print($from_id, "a/$file", undef, "/dev/null", "plain");
2742 - } elsif ($status eq "M") {
2743 - git_diff_print($from_id, "a/$file", $to_id, "b/$file", "plain");
2744 - }
2810 + git_patchset_body(\@patchset, \@difftree, $hash, $hash_parent);
2811 +
2812 + print "</div>\n"; # class="page_body"
2813 + git_footer_html();
2814 +
2815 + } elsif ($format eq 'plain') {
2816 + local $/ = undef;
2817 + print <$fd>;
2818 + close $fd
2819 + or print "Reading git-diff-tree failed\n";
2820 }
2821 }
2822
2823 +sub git_commitdiff_plain {
2824 + git_commitdiff('plain');
2825 +}
2826 +
2827 sub git_history {
2828 if (!defined $hash_base) {
2829 $hash_base = git_get_head_hash($project);