{"thread":{"id":"4822","subject":"[PATCH] gitweb.cgi: Teach \"a=blob\" action to know the blob/file mime type","startedAt":"2006-07-10T03:18:57Z","lastAt":"2006-07-10T03:18:57Z","messageCount":1,"participants":["Luben Tuikov"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"23512","messageId":"20060710031857.59352.qmail@web31812.mail.mud.yahoo.com","threadId":"4822","inReplyTo":null,"subject":"[PATCH] gitweb.cgi: Teach \"a=blob\" action to know the blob/file mime type","fromName":"Luben Tuikov","fromEmail":"ltuikov@yahoo.com","sentAt":"2006-07-10T03:18:57Z","receivedAt":"2006-07-10T03:18:57Z","isPatch":true,"sender":{"key":"ltuikov@yahoo.com","avatar":null},"body":"Now action \"blob\" knows the file type: if the file type is\nnot \"text/*\" then action \"blob\" defaults to \"blob_plain\",\ni.e. the file is downloaded raw for the browser to interpret.\nIf the file type is \"text/*\", then \"blob\" defaults to the\ncurrent \"cat -n\"-like output, from which you can click\n\"plain\", to get the \"blob_plain\" output.\n\nSigned-off-by: Luben Tuikov <ltuikov@yahoo.com>\n---\n gitweb/gitweb.cgi |  127 ++++++++++++++++++++++++++++-------------------------\n 1 files changed, 67 insertions(+), 60 deletions(-)\n\n\ndiff --git a/gitweb/gitweb.cgi b/gitweb/gitweb.cgi\nindex 3e2790c..1e61ae6 100755\n--- a/gitweb/gitweb.cgi\n+++ b/gitweb/gitweb.cgi\n@@ -1455,61 +1455,6 @@ sub git_get_hash_by_path {\n \t}\n }\n \n-sub git_blob {\n-\tif (!defined $hash && defined $file_name) {\n-\t\tmy $base = $hash_base || git_read_head($project);\n-\t\t$hash = git_get_hash_by_path($base, $file_name, \"blob\") || die_error(undef, \"Error lookup file.\");\n-\t}\n-\tmy $have_blame = git_get_project_config_bool ('blame');\n-\topen my $fd, \"-|\", \"$gitbin/git-cat-file blob $hash\" or die_error(undef, \"Open failed.\");\n-\tgit_header_html();\n-\tif (defined $hash_base && (my %co = git_read_commit($hash_base))) {\n-\t\tprint \"<div class=\\\"page_nav\\\">\\n\" .\n-\t\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=summary\")}, \"summary\") .\n-\t\t      \" | \" . $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=shortlog\")}, \"shortlog\") .\n-\t\t      \" | \" . $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=log\")}, \"log\") .\n-\t\t      \" | \" . $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=commit;h=$hash_base\")}, \"commit\") .\n-\t\t      \" | \" . $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=commitdiff;h=$hash_base\")}, \"commitdiff\") .\n-\t\t      \" | \" . $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=tree;h=$co{'tree'};hb=$hash_base\")}, \"tree\") . \"<br/>\\n\";\n-\t\tif (defined $file_name) {\n-\t\t\tif ($have_blame) {\n-\t\t\t\tprint $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blame;h=$hash;hb=$hash_base;f=$file_name\")}, \"blame\") .  \" | \";\n-\t\t\t}\n-\t\t\tprint $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blob_plain;h=$hash;f=$file_name\")}, \"plain\") .\n-\t\t\t\" | \" . $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blob;hb=HEAD;f=$file_name\")}, \"head\") . \"<br/>\\n\";\n-\t\t} else {\n-\t\t\tprint $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blob_plain;h=$hash\")}, \"plain\") . \"<br/>\\n\";\n-\t\t}\n-\t\tprint \"</div>\\n\".\n-\t\t       \"<div>\" .\n-\t\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=commit;h=$hash_base\"), -class => \"title\"}, esc_html($co{'title'})) .\n-\t\t      \"</div>\\n\";\n-\t} else {\n-\t\tprint \"<div class=\\\"page_nav\\\">\\n\" .\n-\t\t      \"<br/><br/></div>\\n\" .\n-\t\t      \"<div class=\\\"title\\\">$hash</div>\\n\";\n-\t}\n-\tif (defined $file_name) {\n-\t\tprint \"<div class=\\\"page_path\\\"><b>\" . esc_html($file_name) . \"</b></div>\\n\";\n-\t}\n-\tprint \"<div class=\\\"page_body\\\">\\n\";\n-\tmy $nr;\n-\twhile (my $line = <$fd>) {\n-\t\tchomp $line;\n-\t\t$nr++;\n-\t\twhile ((my $pos = index($line, \"\\t\")) != -1) {\n-\t\t\tif (my $count = (8 - ($pos % 8))) {\n-\t\t\t\tmy $spaces = ' ' x $count;\n-\t\t\t\t$line =~ s/\\t/$spaces/;\n-\t\t\t}\n-\t\t}\n-\t\tprintf \"<div class=\\\"pre\\\"><a id=\\\"l%i\\\" href=\\\"#l%i\\\" class=\\\"linenr\\\">%4i</a> %s</div>\\n\", $nr, $nr, $nr, esc_html($line);\n-\t}\n-\tclose $fd or print \"Reading blob failed.\\n\";\n-\tprint \"</div>\";\n-\tgit_footer_html();\n-}\n-\n sub mimetype_guess_file {\n \tmy $filename = shift;\n \tmy $mimemap = shift;\n@@ -1548,14 +1493,14 @@ sub git_blob_plain_mimetype {\n \tmy $fd = shift;\n \tmy $filename = shift;\n \n-\t# just in case\n-\treturn $default_blob_plain_mimetype unless $fd;\n-\n \tif ($filename) {\n \t\tmy $mime = mimetype_guess($filename);\n \t\t$mime and return $mime;\n \t}\n \n+\t# just in case\n+\treturn $default_blob_plain_mimetype unless $fd;\n+\n \tif (-T $fd) {\n \t\treturn 'text/plain' .\n \t\t       ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');\n@@ -1573,8 +1518,10 @@ sub git_blob_plain_mimetype {\n }\n \n sub git_blob_plain {\n-\topen my $fd, \"-|\", \"$gitbin/git-cat-file blob $hash\" or return;\n-\tmy $type = git_blob_plain_mimetype($fd, $file_name);\n+\tmy $type = shift;\n+\topen my $fd, \"-|\", \"$gitbin/git-cat-file blob $hash\" or die_error(\"Couldn't cat $file_name, $hash\");\n+\n+\t$type ||= git_blob_plain_mimetype($fd, $file_name);\n \n \t# save as filename, even when no $file_name is given\n \tmy $save_as = \"$hash\";\n@@ -1593,7 +1540,66 @@ sub git_blob_plain {\n \tclose $fd;\n }\n \n+sub git_blob {\n+\tif (!defined $hash && defined $file_name) {\n+\t\tmy $base = $hash_base || git_read_head($project);\n+\t\t$hash = git_get_hash_by_path($base, $file_name, \"blob\") || die_error(undef, \"Error lookup file.\");\n+\t}\n+\tmy $have_blame = git_get_project_config_bool ('blame');\n+\topen my $fd, \"-|\", \"$gitbin/git-cat-file blob $hash\" or die_error(undef, \"Open failed.\");\n+\tmy $mimetype = git_blob_plain_mimetype($fd, $file_name);\n+\tif ($mimetype !~ m/^text\\//) {\n+\t\tclose $fd;\n+\t\treturn git_blob_plain($mimetype);\n+\t}\n+\tgit_header_html();\n+\tif (defined $hash_base && (my %co = git_read_commit($hash_base))) {\n+\t\tprint \"<div class=\\\"page_nav\\\">\\n\" .\n+\t\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=summary\")}, \"summary\") .\n+\t\t      \" | \" . $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=shortlog\")}, \"shortlog\") .\n+\t\t      \" | \" . $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=log\")}, \"log\") .\n+\t\t      \" | \" . $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=commit;h=$hash_base\")}, \"commit\") .\n+\t\t      \" | \" . $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=commitdiff;h=$hash_base\")}, \"commitdiff\") .\n+\t\t      \" | \" . $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=tree;h=$co{'tree'};hb=$hash_base\")}, \"tree\") . \"<br/>\\n\";\n+\t\tif (defined $file_name) {\n+\t\t\tif ($have_blame) {\n+\t\t\t\tprint $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blame;h=$hash;hb=$hash_base;f=$file_name\")}, \"blame\") .  \" | \";\n+\t\t\t}\n+\t\t\tprint $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blob_plain;h=$hash;f=$file_name\")}, \"plain\") .\n+\t\t\t\" | \" . $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blob;hb=HEAD;f=$file_name\")}, \"head\") . \"<br/>\\n\";\n+\t\t} else {\n+\t\t\tprint $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blob_plain;h=$hash\")}, \"plain\") . \"<br/>\\n\";\n+\t\t}\n+\t\tprint \"</div>\\n\".\n+\t\t       \"<div>\" .\n+\t\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=commit;h=$hash_base\"), -class => \"title\"}, esc_html($co{'title'})) .\n+\t\t      \"</div>\\n\";\n+\t} else {\n+\t\tprint \"<div class=\\\"page_nav\\\">\\n\" .\n+\t\t      \"<br/><br/></div>\\n\" .\n+\t\t      \"<div class=\\\"title\\\">$hash</div>\\n\";\n+\t}\n+\tif (defined $file_name) {\n+\t\tprint \"<div class=\\\"page_path\\\"><b>\" . esc_html($file_name) . \"</b></div>\\n\";\n+\t}\n+\tprint \"<div class=\\\"page_body\\\">\\n\";\n+\tmy $nr;\n+\twhile (my $line = <$fd>) {\n+\t\tchomp $line;\n+\t\t$nr++;\n+\t\twhile ((my $pos = index($line, \"\\t\")) != -1) {\n+\t\t\tif (my $count = (8 - ($pos % 8))) {\n+\t\t\t\tmy $spaces = ' ' x $count;\n+\t\t\t\t$line =~ s/\\t/$spaces/;\n+\t\t\t}\n+\t\t}\n+\t\tprintf \"<div class=\\\"pre\\\"><a id=\\\"l%i\\\" href=\\\"#l%i\\\" class=\\\"linenr\\\">%4i</a> %s</div>\\n\", $nr, $nr, $nr, esc_html($line);\n+\t}\n+\tclose $fd or print \"Reading blob failed.\\n\";\n+\tprint \"</div>\";\n+\tgit_footer_html();\n+}\n+\n sub git_tree {\n \tif (!defined $hash) {\n \t\t$hash = git_read_head($project);\n-- \n1.4.1.g35dbd\n\n"}]}