@matheusbsbs / matheus-bagliano / commits / 7c5e2ebb5d
Fork 0

gitweb: Use git-diff-tree or git-diff patch output for blobdiff

This is second part of removing gitweb dependency on external diff (used in git_diff_print). Get rid of git_diff_print invocation in git_blobdiff, and use either git-diff-tree (when both hash_base and hash_parent_base are provided) patch format or git-diff patch format (when only hash and hash_parent are provided) for output. Supported URI schemes, and output formats: * New URI scheme: both hash_base and hash_parent_base (trees-ish containing blobs versions we want to compare) are provided. Also either filename is provided, or hash (of blob) is provided (we try to find filename then). For this scheme we have copying and renames detection, mode changes, file types etc., and information extended diff header is correct. * Old URI scheme: hash_parent_base is not provided, we use hash and hash_parent to directly compare blobs using git-diff. If no filename is given, blobs hashes are used in place of filenames. This scheme has always "blob" as file type, it cannot detect mode changes, and we rely on CGI parameters to provide name of the file. Added git_to_hash subroutine, which transforms symbolic name or list of symbolic name to hash or list of hashes using git-rev-parse. To have "blob" instead of "unknown" (or "file" regardless of the type) in "gitweb diff header" for legacy scheme, file_type function now returns its argument if it is not octal string. Added support for fake "2" status code in git_patchset_body. Such code is generated by git_blobdiff in legacy scheme case. ATTENTION: The order of arguments (operands) to git-diff is reversed (sic!) to have correct diff in the legacy (no hash_parent_base) case. $hash_parent, $hash ordering is commented out, as it gives reversed patch (at least for git version 1.4.1.1) as compared to output in new scheme and output of older gitweb version. Signed-off-by: Jakub Narebski <jnareb@gmail.com> Signed-off-by: Junio C Hamano <junkio@cox.net>

Jakub Narebski committed Aug 25, 2006 at 21:13 UTC 7c5e2ebb5d8ca3d8428e19593c69a4f329d64855
1 file changed +132 -17
gitweb/gitweb.perl
+132 -17
@@ -454,7 +454,13 @@ sub mode_str {
454
455 # convert file mode in octal to file type string
456 sub file_type {
457 - my $mode = oct shift;
457 + my $mode = shift;
458 +
459 + if ($mode !~ m/^[0-7]+$/) {
460 + return $mode;
461 + } else {
462 + $mode = oct $mode;
463 + }
464
465 if (S_ISDIR($mode & S_IFMT)) {
466 return "directory";
@@ -618,6 +624,26 @@ sub git_get_hash_by_path {
624 return $3;
625 }
626
627 +# converts symbolic name to hash
628 +sub git_to_hash {
629 + my @params = @_;
630 + return undef unless @params;
631 +
632 + open my $fd, "-|", $GIT, "rev-parse", @params
633 + or return undef;
634 + my @hashes = map { chomp; $_ } <$fd>;
635 + close $fd;
636 +
637 + if (wantarray) {
638 + return @hashes;
639 + } elsif (scalar(@hashes) == 1) {
640 + # single hash
641 + return $hashes[0];
642 + } else {
643 + return \@hashes;
644 + }
645 +}
646 +
647 ## ......................................................................
648 ## git utility functions, directly accessing git repository
649
@@ -1672,7 +1698,8 @@ sub git_patchset_body {
1698 "</div>\n"; # class="diff_info"
1699
1700 } elsif ($diffinfo->{'status'} eq "R" || # renamed
1675 - $diffinfo->{'status'} eq "C") { # copied
1701 + $diffinfo->{'status'} eq "C" || # copied
1702 + $diffinfo->{'status'} eq "2") { # with two filenames (from git_blobdiff)
1703 print "<div class=\"diff_info\">" .
1704 file_type($diffinfo->{'from_mode'}) . ":" .
1705 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
@@ -2788,14 +2815,102 @@ sub git_commit {
2815 }
2816
2817 sub git_blobdiff {
2791 - mkdir($git_temp, 0700);
2792 - git_header_html();
2818 + my $fd;
2819 + my @difftree;
2820 + my %diffinfo;
2821 +
2822 + # preparing $fd and %diffinfo for git_patchset_body
2823 + # new style URI
2824 + if (defined $hash_base && defined $hash_parent_base) {
2825 + if (defined $file_name) {
2826 + # read raw output
2827 + open $fd, "-|", $GIT, "diff-tree", '-r', '-M', '-C', $hash_parent_base, $hash_base,
2828 + "--", $file_name
2829 + or die_error(undef, "Open git-diff-tree failed");
2830 + @difftree = map { chomp; $_ } <$fd>;
2831 + close $fd
2832 + or die_error(undef, "Reading git-diff-tree failed");
2833 + @difftree
2834 + or die_error('404 Not Found', "Blob diff not found");
2835 +
2836 + } elsif (defined $hash) { # try to find filename from $hash
2837 + if ($hash !~ /[0-9a-fA-F]{40}/) {
2838 + $hash = git_to_hash($hash);
2839 + }
2840 +
2841 + # read filtered raw output
2842 + open $fd, "-|", $GIT, "diff-tree", '-r', '-M', '-C', $hash_parent_base, $hash_base
2843 + or die_error(undef, "Open git-diff-tree failed");
2844 + @difftree =
2845 + # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
2846 + # $hash == to_id
2847 + grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
2848 + map { chomp; $_ } <$fd>;
2849 + close $fd
2850 + or die_error(undef, "Reading git-diff-tree failed");
2851 + @difftree
2852 + or die_error('404 Not Found', "Blob diff not found");
2853 +
2854 + } else {
2855 + die_error('404 Not Found', "Missing one of the blob diff parameters");
2856 + }
2857 +
2858 + if (@difftree > 1) {
2859 + die_error('404 Not Found', "Ambiguous blob diff specification");
2860 + }
2861 +
2862 + %diffinfo = parse_difftree_raw_line($difftree[0]);
2863 + $file_parent ||= $diffinfo{'from_file'} || $file_name || $diffinfo{'file'};
2864 + $file_name ||= $diffinfo{'to_file'} || $diffinfo{'file'};
2865 +
2866 + $hash_parent ||= $diffinfo{'from_id'};
2867 + $hash ||= $diffinfo{'to_id'};
2868 +
2869 + # open patch output
2870 + open $fd, "-|", $GIT, "diff-tree", '-r', '-p', '-M', '-C', $hash_parent_base, $hash_base,
2871 + "--", $file_name
2872 + or die_error(undef, "Open git-diff-tree failed");
2873 + }
2874 +
2875 + # old/legacy style URI
2876 + if (!%diffinfo && # if new style URI failed
2877 + defined $hash && defined $hash_parent) {
2878 + # fake git-diff-tree raw output
2879 + $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
2880 + $diffinfo{'from_id'} = $hash_parent;
2881 + $diffinfo{'to_id'} = $hash;
2882 + if (defined $file_name) {
2883 + if (defined $file_parent) {
2884 + $diffinfo{'status'} = '2';
2885 + $diffinfo{'from_file'} = $file_parent;
2886 + $diffinfo{'to_file'} = $file_name;
2887 + } else { # assume not renamed
2888 + $diffinfo{'status'} = '1';
2889 + $diffinfo{'from_file'} = $file_name;
2890 + $diffinfo{'to_file'} = $file_name;
2891 + }
2892 + } else { # no filename given
2893 + $diffinfo{'status'} = '2';
2894 + $diffinfo{'from_file'} = $hash_parent;
2895 + $diffinfo{'to_file'} = $hash;
2896 + }
2897 +
2898 + #open $fd, "-|", $GIT, "diff", '-p', $hash_parent, $hash
2899 + open $fd, "-|", $GIT, "diff", '-p', $hash, $hash_parent
2900 + or die_error(undef, "Open git-diff failed");
2901 + } else {
2902 + die_error('404 Not Found', "Missing one of the blob diff parameters")
2903 + unless %diffinfo;
2904 + }
2905 +
2906 + # header
2907 my $formats_nav =
2908 $cgi->a({-href => href(action=>"blobdiff_plain",
2909 hash=>$hash, hash_parent=>$hash_parent,
2910 hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
2911 file_name=>$file_name, file_parent=>$file_parent)},
2912 "plain");
2913 + git_header_html();
2914 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2915 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2916 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
@@ -2803,19 +2918,19 @@ sub git_blobdiff {
2918 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
2919 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
2920 }
2806 - git_print_page_path($file_name, "blob", $hash_base);
2807 - print "<div class=\"page_body\">\n" .
2808 - "<div class=\"diff_info\">blob:" .
2809 - $cgi->a({-href => href(action=>"blob", hash=>$hash_parent,
2810 - hash_base=>$hash_parent_base, file_name=>($file_parent || $file_name))},
2811 - $hash_parent) .
2812 - " -> blob:" .
2813 - $cgi->a({-href => href(action=>"blob", hash=>$hash,
2814 - hash_base=>$hash_base, file_name=>$file_name)},
2815 - $hash) .
2816 - "</div>\n";
2817 - git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash);
2818 - print "</div>"; # page_body
2921 + if (defined $file_name) {
2922 + git_print_page_path($file_name, "blob", $hash_base);
2923 + } else {
2924 + print "<div class=\"page_path\"></div>\n";
2925 + }
2926 +
2927 + # patch
2928 + print "<div class=\"page_body\">\n";
2929 +
2930 + git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
2931 + close $fd;
2932 +
2933 + print "</div>\n"; # class="page_body"
2934 git_footer_html();
2935 }
2936