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

gitweb: Teach "a=blob" action to be more lenient about blob/file mime type

Since 930cf7dd7cc6b87d173f182230763e1f1913d319 'blob' action knows the file type; if the file type is not "text/*" or one of common network image formats/mimetypes (gif, png, jpeg) then the action "blob" defaulted to "blob_plain". This caused the problem if mimetypes file was not well suited for web, for example returning "application/x-sh" for "*.sh" shell scripts, instead of "text/plain" (or other "text/*"). Now "blob" action defaults to "blob_plain" ('raw' view) only if file is of type which is neither "text/*" nor "image/{gif,png,jpeg}" AND it is binary file. Otherwise it assumes that it can be displayed either in <img> tag ("image/*" mimetype), or can be displayed line by line (otherwise). Signed-off-by: Jakub Narebski <jnareb@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jakub Narebski committed Dec 15, 2007 at 15:41 UTC dfa7c7d221d05499063f9bf95f507af8a882bfeb
1 file changed +11 -11
gitweb/gitweb.perl
+11 -11
@@ -4290,7 +4290,7 @@ sub git_blob {
4290 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4291 or die_error(undef, "Couldn't cat $file_name, $hash");
4292 my $mimetype = blob_mimetype($fd, $file_name);
4293 - if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)!) {
4293 + if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {
4294 close $fd;
4295 return git_blob_plain($mimetype);
4296 }
@@ -4331,16 +4331,7 @@ sub git_blob {
4331 }
4332 git_print_page_path($file_name, "blob", $hash_base);
4333 print "<div class=\"page_body\">\n";
4334 - if ($mimetype =~ m!^text/!) {
4335 - my $nr;
4336 - while (my $line = <$fd>) {
4337 - chomp $line;
4338 - $nr++;
4339 - $line = untabify($line);
4340 - printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
4341 - $nr, $nr, $nr, esc_html($line, -nbsp=>1);
4342 - }
4343 - } elsif ($mimetype =~ m!^image/!) {
4334 + if ($mimetype =~ m!^image/!) {
4335 print qq!<img type="$mimetype"!;
4336 if ($file_name) {
4337 print qq! alt="$file_name" title="$file_name"!;
@@ -4349,6 +4340,15 @@ sub git_blob {
4340 href(action=>"blob_plain", hash=>$hash,
4341 hash_base=>$hash_base, file_name=>$file_name) .
4342 qq!" />\n!;
4343 + } else {
4344 + my $nr;
4345 + while (my $line = <$fd>) {
4346 + chomp $line;
4347 + $nr++;
4348 + $line = untabify($line);
4349 + printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
4350 + $nr, $nr, $nr, esc_html($line, -nbsp=>1);
4351 + }
4352 }
4353 close $fd
4354 or print "Reading blob failed.\n";