gitweb: git_blobdiff_plain is git_blobdiff('plain')
git_blobdiff and git_blobdiff_plain are now collapsed into one subroutine git_blobdiff, with format (currently 'html' which is default format corresponding to git_blobdiff, and 'plain' corresponding to git_blobdiff_plain) specified in argument. blobdiff_plain format is now generated either by git-diff-tree or by git-diff. Added X-Git-Url: header. From-file and to-file name in header are corrected. Note that for now commitdiff_plain does not detect renames and copying, while blobdiff_plain does. While at it, set expires to "+1d" for non-textual hash ids. 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:14 UTC
9b71b1f6b3b57b3771151d252a5e22886524a154
1 file changed
+68
-25
gitweb/gitweb.perl
+68
-25
@@ -2815,9 +2815,12 @@ sub git_commit {
2815
}
2816
2817
sub git_blobdiff {
2818
+ my $format = shift || 'html';
2819
+
2820
my $fd;
2821
my @difftree;
2822
my %diffinfo;
2823
+ my $expires;
2824
2825
# preparing $fd and %diffinfo for git_patchset_body
2826
# new style URI
@@ -2866,6 +2869,12 @@ sub git_blobdiff {
2869
$hash_parent ||= $diffinfo{'from_id'};
2870
$hash ||= $diffinfo{'to_id'};
2871
2872
+ # non-textual hash id's can be cached
2873
+ if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
2874
+ $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
2875
+ $expires = '+1d';
2876
+ }
2877
+
2878
# open patch output
2879
open $fd, "-|", $GIT, "diff-tree", '-r', '-p', '-M', '-C', $hash_parent_base, $hash_base,
2880
"--", $file_name
@@ -2895,6 +2904,13 @@ sub git_blobdiff {
2904
$diffinfo{'to_file'} = $hash;
2905
}
2906
2907
+ # non-textual hash id's can be cached
2908
+ if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
2909
+ $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
2910
+ $expires = '+1d';
2911
+ }
2912
+
2913
+ # open patch output
2914
#open $fd, "-|", $GIT, "diff", '-p', $hash_parent, $hash
2915
open $fd, "-|", $GIT, "diff", '-p', $hash, $hash_parent
2916
or die_error(undef, "Open git-diff failed");
@@ -2904,40 +2920,67 @@ sub git_blobdiff {
2920
}
2921
2922
# 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);
2917
- } else {
2918
- print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
2919
- print "<div class=\"title\">$hash vs $hash_parent</div>\n";
2920
- }
2921
- if (defined $file_name) {
2922
- git_print_page_path($file_name, "blob", $hash_base);
2923
+ if ($format eq 'html') {
2924
+ my $formats_nav =
2925
+ $cgi->a({-href => href(action=>"blobdiff_plain",
2926
+ hash=>$hash, hash_parent=>$hash_parent,
2927
+ hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
2928
+ file_name=>$file_name, file_parent=>$file_parent)},
2929
+ "plain");
2930
+ git_header_html(undef, $expires);
2931
+ if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2932
+ git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2933
+ git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2934
+ } else {
2935
+ print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
2936
+ print "<div class=\"title\">$hash vs $hash_parent</div>\n";
2937
+ }
2938
+ if (defined $file_name) {
2939
+ git_print_page_path($file_name, "blob", $hash_base);
2940
+ } else {
2941
+ print "<div class=\"page_path\"></div>\n";
2942
+ }
2943
+
2944
+ } elsif ($format eq 'plain') {
2945
+ print $cgi->header(
2946
+ -type => 'text/plain',
2947
+ -charset => 'utf-8',
2948
+ -expires => $expires,
2949
+ -content_disposition => qq(inline; filename="${file_name}.patch"));
2950
+
2951
+ print "X-Git-Url: " . $cgi->self_url() . "\n\n";
2952
+
2953
} else {
2924
- print "<div class=\"page_path\"></div>\n";
2954
+ die_error(undef, "Unknown blobdiff format");
2955
}
2956
2957
# patch
2928
- print "<div class=\"page_body\">\n";
2958
+ if ($format eq 'html') {
2959
+ print "<div class=\"page_body\">\n";
2960
2930
- git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
2931
- close $fd;
2961
+ git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
2962
+ close $fd;
2963
2933
- print "</div>\n"; # class="page_body"
2934
- git_footer_html();
2964
+ print "</div>\n"; # class="page_body"
2965
+ git_footer_html();
2966
+
2967
+ } else {
2968
+ while (my $line = <$fd>) {
2969
+ $line =~ s!a/($hash|$hash_parent)!a/$diffinfo{'from_file'}!g;
2970
+ $line =~ s!b/($hash|$hash_parent)!b/$diffinfo{'to_file'}!g;
2971
+
2972
+ print $line;
2973
+
2974
+ last if $line =~ m!^\+\+\+!;
2975
+ }
2976
+ local $/ = undef;
2977
+ print <$fd>;
2978
+ close $fd;
2979
+ }
2980
}
2981
2982
sub git_blobdiff_plain {
2938
- mkdir($git_temp, 0700);
2939
- print $cgi->header(-type => "text/plain", -charset => 'utf-8');
2940
- git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash, "plain");
2983
+ git_blobdiff('plain');
2984
}
2985
2986
sub git_commitdiff {