{"thread":{"id":"11193","subject":"[PATCH] gitweb: Teach \"a=blob\" action to be more lenient about blob/file mime type","startedAt":"2007-12-08T11:55:13Z","lastAt":"2007-12-09T10:06:13Z","messageCount":3,"participants":["Jakub Narebski","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"62408","messageId":"1197114913-15626-1-git-send-email-jnareb@gmail.com","threadId":"11193","inReplyTo":null,"subject":"[PATCH] gitweb: Teach \"a=blob\" action to be more lenient about blob/file mime type","fromName":"Jakub Narebski","fromEmail":"jnareb@gmail.com","sentAt":"2007-12-08T11:55:13Z","receivedAt":"2007-12-08T11:55:13Z","isPatch":true,"sender":{"key":"jnareb@gmail.com","avatar":"https://avatars.githubusercontent.com/u/2706?v=4"},"body":"Since 930cf7dd7cc6b87d173f182230763e1f1913d319 'blob' action knows the\nfile type; if the file type is not \"text/*\" or one of common network\nimage formats/mimetypes (gif, png, jpeg) then the action \"blob\"\ndefaulted to \"blob_plain\".  This caused the problem if mimetypes file\nwas not well suited for web, for example returning \"application/x-sh\"\nfor \"*.sh\" shell scripts, instead of \"text/plain\" (or other \"text/*\").\n\nNow \"blob\" action defaults to \"blob_plain\" ('raw' view) only if file\nis of type which is neither \"text/*\" nor \"image/{gif,png,jpeg}\"\nAND it is binary file.  Otherwise it assumes that it can be displayed\neither in <img> tag (\"image/*\" mimetype), or can be displayed line by\nline (otherwise).\n\nSigned-off-by: Jakub Narebski <jnareb@gmail.com>\n---\nThe patch I've send earlier\n  \"[RFC/PATCH] gitweb: Try to sanitize mimetype for 'blob_plain' view\"\n  Message-Id: <1195484054-18164-1-git-send-email-jnareb@gmail.com>\nshould also do what this patch is about. This one however IMHO\nis much less invasive, and much less controversial.\n\nP.S. BTW is there some plumbing for scripts to help with\ngitattributes, for example showing all gitattributes (or status of\nselected attributes) for given path?\n\n gitweb/gitweb.perl |   24 ++++++++++++------------\n 1 files changed, 12 insertions(+), 12 deletions(-)\n\ndiff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl\nindex eac7e16..b833327 100755\n--- a/gitweb/gitweb.perl\n+++ b/gitweb/gitweb.perl\n@@ -4302,7 +4302,7 @@ sub git_blob {\n \topen my $fd, \"-|\", git_cmd(), \"cat-file\", \"blob\", $hash\n \t\tor die_error(undef, \"Couldn't cat $file_name, $hash\");\n \tmy $mimetype = blob_mimetype($fd, $file_name);\n-\tif ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)!) {\n+\tif ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {\n \t\tclose $fd;\n \t\treturn git_blob_plain($mimetype);\n \t}\n@@ -4343,16 +4343,7 @@ sub git_blob {\n \t}\n \tgit_print_page_path($file_name, \"blob\", $hash_base);\n \tprint \"<div class=\\\"page_body\\\">\\n\";\n-\tif ($mimetype =~ m!^text/!) {\n-\t\tmy $nr;\n-\t\twhile (my $line = <$fd>) {\n-\t\t\tchomp $line;\n-\t\t\t$nr++;\n-\t\t\t$line = untabify($line);\n-\t\t\tprintf \"<div class=\\\"pre\\\"><a id=\\\"l%i\\\" href=\\\"#l%i\\\" class=\\\"linenr\\\">%4i</a> %s</div>\\n\",\n-\t\t\t       $nr, $nr, $nr, esc_html($line, -nbsp=>1);\n-\t\t}\n-\t} elsif ($mimetype =~ m!^image/!) {\n+\tif ($mimetype =~ m!^image/!) {\n \t\tprint qq!<img type=\"$mimetype\"!;\n \t\tif ($file_name) {\n \t\t\tprint qq! alt=\"$file_name\" title=\"$file_name\"!;\n@@ -4361,7 +4352,16 @@ sub git_blob {\n \t\t      href(action=>\"blob_plain\", hash=>$hash,\n \t\t           hash_base=>$hash_base, file_name=>$file_name) .\n \t\t      qq!\" />\\n!;\n-\t}\n+\t} else {\n+\t\tmy $nr;\n+\t\twhile (my $line = <$fd>) {\n+\t\t\tchomp $line;\n+\t\t\t$nr++;\n+\t\t\t$line = untabify($line);\n+\t\t\tprintf \"<div class=\\\"pre\\\"><a id=\\\"l%i\\\" href=\\\"#l%i\\\" class=\\\"linenr\\\">%4i</a> %s</div>\\n\",\n+\t\t\t       $nr, $nr, $nr, esc_html($line, -nbsp=>1);\n+\t\t}\n+\t} els\n \tclose $fd\n \t\tor print \"Reading blob failed.\\n\";\n \tprint \"</div>\";\n-- \n1.5.3.7\n"},{"id":"62454","messageId":"7vk5notub9.fsf@gitster.siamese.dyndns.org","threadId":"11193","inReplyTo":"1197114913-15626-1-git-send-email-jnareb@gmail.com","subject":"Re: [PATCH] gitweb: Teach \"a=blob\" action to be more lenient about blob/file mime type","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2007-12-09T03:34:34Z","receivedAt":"2007-12-09T03:34:34Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Jakub Narebski <jnareb@gmail.com> writes:\n\n> Since 930cf7dd7cc6b87d173f182230763e1f1913d319 'blob' action knows the\n> file type; if the file type is not \"text/*\" or one of common network\n> image formats/mimetypes (gif, png, jpeg) then the action \"blob\"\n> defaulted to \"blob_plain\".  This caused the problem if mimetypes file\n> was not well suited for web, for example returning \"application/x-sh\"\n> for \"*.sh\" shell scripts, instead of \"text/plain\" (or other \"text/*\").\n>\n> Now \"blob\" action defaults to \"blob_plain\" ('raw' view) only if file\n> is of type which is neither \"text/*\" nor \"image/{gif,png,jpeg}\"\n> AND it is binary file.  Otherwise it assumes that it can be displayed\n> either in <img> tag (\"image/*\" mimetype), or can be displayed line by\n> line (otherwise).\n\nOk, the intent sounds sane.  Let's see if the implementation is also\nsane.\n\n> diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl\n> index eac7e16..b833327 100755\n> --- a/gitweb/gitweb.perl\n> +++ b/gitweb/gitweb.perl\n> @@ -4302,7 +4302,7 @@ sub git_blob {\n>  \topen my $fd, \"-|\", git_cmd(), \"cat-file\", \"blob\", $hash\n>  \t\tor die_error(undef, \"Couldn't cat $file_name, $hash\");\n>  \tmy $mimetype = blob_mimetype($fd, $file_name);\n> -\tif ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)!) {\n> +\tif ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {\n>  \t\tclose $fd;\n>  \t\treturn git_blob_plain($mimetype);\n>  \t}\n\n\"If not text or image and binary go blob_plain\" -- Ok.\n\n> @@ -4343,16 +4343,7 @@ sub git_blob {\n>  \t}\n>  \tgit_print_page_path($file_name, \"blob\", $hash_base);\n>  \tprint \"<div class=\\\"page_body\\\">\\n\";\n> +\tif ($mimetype =~ m!^image/!) {\n>  \t\tprint qq!<img type=\"$mimetype\"!;\n>  \t\tif ($file_name) {\n>  \t\t\tprint qq! alt=\"$file_name\" title=\"$file_name\"!;\n> @@ -4361,7 +4352,16 @@ sub git_blob {\n>  \t\t      href(action=>\"blob_plain\", hash=>$hash,\n>  \t\t           hash_base=>$hash_base, file_name=>$file_name) .\n>  \t\t      qq!\" />\\n!;\n> +\t} else {\n> +\t\tmy $nr;\n> +\t\twhile (my $line = <$fd>) {\n> +\t\t\tchomp $line;\n> +\t\t\t$nr++;\n> +\t\t\t$line = untabify($line);\n> +\t\t\tprintf \"<div class=\\\"pre\\\"><a id=\\\"l%i\\\" href=\\\"#l%i\\\" class=\\\"linenr\\\">%4i</a> %s</div>\\n\",\n> +\t\t\t       $nr, $nr, $nr, esc_html($line, -nbsp=>1);\n> +\t\t}\n> +\t} els\n\n\"If image, do image, but otherwise show line-by-line\" -- Ok.\n\nThere is a \"Huh?\" on the last line, though.\n\n> P.S. BTW is there some plumbing for scripts to help with\n> gitattributes, for example showing all gitattributes (or status of\n> selected attributes) for given path?\n\n$ git grep gitattributes Documentation | grep -i display\n"},{"id":"62473","messageId":"200712091106.14124.jnareb@gmail.com","threadId":"11193","inReplyTo":"7vk5notub9.fsf@gitster.siamese.dyndns.org","subject":"Re: [PATCH] gitweb: Teach \"a=blob\" action to be more lenient about blob/file mime type","fromName":"Jakub Narebski","fromEmail":"jnareb@gmail.com","sentAt":"2007-12-09T10:06:13Z","receivedAt":"2007-12-09T10:06:13Z","isPatch":true,"sender":{"key":"jnareb@gmail.com","avatar":"https://avatars.githubusercontent.com/u/2706?v=4"},"body":"Junio C Hamano wrote:\n> Jakub Narebski <jnareb@gmail.com> writes:\n\n>> +\t\t}\n>> +\t} els\n \n> There is a \"Huh?\" on the last line, though.\n\nI'm sorry for this wart. Can you correct this, or should I send\namended patch?\n \n>> P.S. BTW is there some plumbing for scripts to help with\n>> gitattributes, for example showing all gitattributes (or status of\n>> selected attributes) for given path?\n> \n> $ git grep gitattributes Documentation | grep -i display\n\nThanks. \n\nBut as far as I can see you cannot specify _revision_,\ni.e. git-check-attr operates _only_ on working directory, and\ncan be run only in a work tree. For gitweb I'd like it to run\nin bare repository, and check $GIT_DIR/info/attributes and\n<revision>:[*/].gitattributes\n\n-- \nJakub Narebski\nPoland\n"}]}