{"thread":{"id":"4963","subject":"[PATCH 1/3] gitweb.cgi: git_blame2: an alternative simple working git blame","startedAt":"2006-07-23T20:34:55Z","lastAt":"2006-07-23T20:34:55Z","messageCount":1,"participants":["Luben Tuikov"],"isPatch":true,"patchVersion":1,"patchTotal":3},"messages":[{"id":"24025","messageId":"20060723203455.95803.qmail@web31805.mail.mud.yahoo.com","threadId":"4963","inReplyTo":null,"subject":"[PATCH 1/3] gitweb.cgi: git_blame2: an alternative simple working git blame","fromName":"Luben Tuikov","fromEmail":"ltuikov@yahoo.com","sentAt":"2006-07-23T20:34:55Z","receivedAt":"2006-07-23T20:34:55Z","isPatch":true,"sender":{"key":"ltuikov@yahoo.com","avatar":null},"body":"This patch adds an alternative simple working git-blame\ncalled git_blame2().  Simple, because it displays just\nthree columns: the commit, the line number and the line\nof code.  Alternative, because the original git_blame()\nis left untouched.  Lines of code are printed html\nescaped, but as-is.\n\ngit_blame2() uses git-blame as opposed to git-annotate\nused by git_blame().\n\nSigned-off-by: Luben Tuikov <ltuikov@yahoo.com>\n---\n gitweb/gitweb.cgi |   71 ++++++++++++++++++++++++++++++++++++++++++++++++++++-\n 1 files changed, 70 insertions(+), 1 deletions(-)\n\ndiff --git a/gitweb/gitweb.cgi b/gitweb/gitweb.cgi\nindex 4106cb8..57f4a2e 100755\n--- a/gitweb/gitweb.cgi\n+++ b/gitweb/gitweb.cgi\n@@ -227,7 +227,7 @@ if (!defined $action || $action eq \"summ\n \tgit_tag();\n \texit;\n } elsif ($action eq \"blame\") {\n-\tgit_blame();\n+\tgit_blame2();\n \texit;\n } else {\n \tundef $action;\n@@ -1252,7 +1252,75 @@ sub git_tag {\n \tgit_footer_html();\n }\n \n+sub git_read_blame_line {\n+\tmy %bl;\n+\t$_ = shift;\n+\n+\t($bl{'hash'}, $bl{'lineno'}, $bl{'data'}) = /^([0-9a-fA-F]{40}).*?(\\d+)\\)\\s{1}(\\s*.*)/;\n+\n+\treturn %bl;\n+}\n+\n+sub git_blame2 {\n+\tmy $fd;\n+\tmy $ftype;\n+\tdie_error(undef, \"Permission denied.\") if (!git_get_project_config_bool ('blame'));\n+\tdie_error('404 Not Found', \"File name not defined\") if (!$file_name);\n+\t$hash_base ||= git_read_head($project);\n+\tdie_error(undef, \"Reading commit failed\") unless ($hash_base);\n+\tmy %co = git_read_commit($hash_base)\n+\t\tor die_error(undef, \"Reading commit failed\");\n+\tif (!defined $hash) {\n+\t\t$hash = git_get_hash_by_path($hash_base, $file_name, \"blob\")\n+\t\t\tor die_error(undef, \"Error looking up file\");\n+\t}\n+\t$ftype = git_get_type($hash);\n+\tif ($ftype !~ \"blob\") {\n+\t\tdie_error(\"400 Bad Request\", \"object is not a blob\");\n+\t}\n+\topen ($fd, \"-|\", $GIT, \"blame\", '-l', $file_name, $hash_base)\n+\t\tor die_error(undef, \"Open failed\");\n+\tgit_header_html();\n+\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+\tprint $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blob;h=$hash;hb=$hash_base;f=$file_name\")}, \"blob\") .\n+\t\t\" | \" . $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blame;f=$file_name\")}, \"head\") . \"<br/>\\n\";\n+\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+\tgit_print_page_path($file_name, $ftype);\n+\n+\tprint \"<div class=\\\"page_body\\\">\\n\";\n+\n+\tprint \"<table class=\\\"blame\\\">\\n\";\n+\tprint \"<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\\n\";\n+\twhile (my $line = <$fd>) {\n+\t\tmy %blame_line = git_read_blame_line($line);\n+\t\tmy $full_rev = $blame_line{'hash'};\n+\t\tmy $rev = substr($full_rev, 0, 8);\n+\t\tmy $lineno = $blame_line{'lineno'};\n+\t\tmy $data = $blame_line{'data'};\n+\n+\t\tprint \"<tr>\\n\";\n+\t\tprint \"<td class=\\\"sha1\\\">\" .\n+\t\t\t$cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=commit;h=$full_rev;f=$file_name\")}, esc_html($rev)) . \"</td>\\n\";\n+\t\tprint \"<td class=\\\"linenr\\\"><a id=\\\"l$lineno\\\" href=\\\"#l$lineno\\\" class=\\\"linenr\\\">\" . esc_html($lineno) . \"</a></td>\\n\";\n+\t\tprint \"<td class=\\\"pre\\\">\" . esc_html($data) . \"</td>\\n\";\n+\t\tprint \"</tr>\\n\";\n+\t}\n+\tprint \"</table>\\n\";\n+\tprint \"</div>\";\n+\n+\tclose $fd or print \"Reading blob failed\\n\";\n+\tgit_footer_html();\n+}\n+\n sub git_blame {\n \tmy $fd;\n \tdie_error('403 Permission denied', \"Permission denied.\") if (!git_get_project_config_bool ('blame'));\n-- \n1.4.2.rc1.g9b54\n\n"}]}