From 930cf7dd7cc6b87d173f182230763e1f1913d319 Mon Sep 17 00:00:00 2001 From: Luben Tuikov Date: Sun, 9 Jul 2006 20:18:57 -0700 Subject: [PATCH] gitweb.cgi: Teach "a=blob" action to know the blob/file mime type Now action "blob" knows the file type: if the file type is not "text/*" then action "blob" defaults to "blob_plain", i.e. the file is downloaded raw for the browser to interpret. If the file type is "text/*", then "blob" defaults to the current "cat -n"-like output, from which you can click "plain", to get the "blob_plain" output. Signed-off-by: Luben Tuikov Signed-off-by: Junio C Hamano --- gitweb/gitweb.cgi | 127 ++++++++++++++++++++++++---------------------- 1 file changed, 67 insertions(+), 60 deletions(-) diff --git a/gitweb/gitweb.cgi b/gitweb/gitweb.cgi index 2fb2809fb..2e87de476 100755 --- a/gitweb/gitweb.cgi +++ b/gitweb/gitweb.cgi @@ -1458,61 +1458,6 @@ sub git_get_hash_by_path { } } -sub git_blob { - if (!defined $hash && defined $file_name) { - my $base = $hash_base || git_read_head($project); - $hash = git_get_hash_by_path($base, $file_name, "blob") || die_error(undef, "Error lookup file."); - } - my $have_blame = git_get_project_config_bool ('blame'); - open my $fd, "-|", "$gitbin/git-cat-file blob $hash" or die_error(undef, "Open failed."); - git_header_html(); - if (defined $hash_base && (my %co = git_read_commit($hash_base))) { - print "
\n" . - $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") . - " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") . - " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") . - " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base")}, "commit") . - " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash_base")}, "commitdiff") . - " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash_base")}, "tree") . "
\n"; - if (defined $file_name) { - if ($have_blame) { - print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;h=$hash;hb=$hash_base;f=$file_name")}, "blame") . " | "; - } - print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$hash;f=$file_name")}, "plain") . - " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;hb=HEAD;f=$file_name")}, "head") . "
\n"; - } else { - print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$hash")}, "plain") . "
\n"; - } - print "
\n". - "
" . - $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base"), -class => "title"}, esc_html($co{'title'})) . - "
\n"; - } else { - print "
\n" . - "

\n" . - "
$hash
\n"; - } - if (defined $file_name) { - print "
" . esc_html($file_name) . "
\n"; - } - print "
\n"; - my $nr; - while (my $line = <$fd>) { - chomp $line; - $nr++; - while ((my $pos = index($line, "\t")) != -1) { - if (my $count = (8 - ($pos % 8))) { - my $spaces = ' ' x $count; - $line =~ s/\t/$spaces/; - } - } - printf "
%4i %s
\n", $nr, $nr, $nr, esc_html($line); - } - close $fd or print "Reading blob failed.\n"; - print "
"; - git_footer_html(); -} - sub mimetype_guess_file { my $filename = shift; my $mimemap = shift; @@ -1551,14 +1496,14 @@ sub git_blob_plain_mimetype { my $fd = shift; my $filename = shift; - # just in case - return $default_blob_plain_mimetype unless $fd; - if ($filename) { my $mime = mimetype_guess($filename); $mime and return $mime; } + # just in case + return $default_blob_plain_mimetype unless $fd; + if (-T $fd) { return 'text/plain' . ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : ''); @@ -1576,8 +1521,10 @@ sub git_blob_plain_mimetype { } sub git_blob_plain { - open my $fd, "-|", "$gitbin/git-cat-file blob $hash" or return; - my $type = git_blob_plain_mimetype($fd, $file_name); + my $type = shift; + open my $fd, "-|", "$gitbin/git-cat-file blob $hash" or die_error("Couldn't cat $file_name, $hash"); + + $type ||= git_blob_plain_mimetype($fd, $file_name); # save as filename, even when no $file_name is given my $save_as = "$hash"; @@ -1596,6 +1543,66 @@ sub git_blob_plain { close $fd; } +sub git_blob { + if (!defined $hash && defined $file_name) { + my $base = $hash_base || git_read_head($project); + $hash = git_get_hash_by_path($base, $file_name, "blob") || die_error(undef, "Error lookup file."); + } + my $have_blame = git_get_project_config_bool ('blame'); + open my $fd, "-|", "$gitbin/git-cat-file blob $hash" or die_error(undef, "Open failed."); + my $mimetype = git_blob_plain_mimetype($fd, $file_name); + if ($mimetype !~ m/^text\//) { + close $fd; + return git_blob_plain($mimetype); + } + git_header_html(); + if (defined $hash_base && (my %co = git_read_commit($hash_base))) { + print "
\n" . + $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") . + " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") . + " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") . + " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base")}, "commit") . + " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash_base")}, "commitdiff") . + " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash_base")}, "tree") . "
\n"; + if (defined $file_name) { + if ($have_blame) { + print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;h=$hash;hb=$hash_base;f=$file_name")}, "blame") . " | "; + } + print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$hash;f=$file_name")}, "plain") . + " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;hb=HEAD;f=$file_name")}, "head") . "
\n"; + } else { + print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$hash")}, "plain") . "
\n"; + } + print "
\n". + "
" . + $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base"), -class => "title"}, esc_html($co{'title'})) . + "
\n"; + } else { + print "
\n" . + "

\n" . + "
$hash
\n"; + } + if (defined $file_name) { + print "
" . esc_html($file_name) . "
\n"; + } + print "
\n"; + my $nr; + while (my $line = <$fd>) { + chomp $line; + $nr++; + while ((my $pos = index($line, "\t")) != -1) { + if (my $count = (8 - ($pos % 8))) { + my $spaces = ' ' x $count; + $line =~ s/\t/$spaces/; + } + } + printf "
%4i %s
\n", $nr, $nr, $nr, esc_html($line); + } + close $fd or print "Reading blob failed.\n"; + print "
"; + git_footer_html(); +} + sub git_tree { if (!defined $hash) { $hash = git_read_head($project); -- 2.26.2