@matheusbsbs / matheus-bagliano / commits / 1f2857ea32
Fork 0

gitweb.cgi: git_blame2: an alternative simple working git blame

This patch adds an alternative simple working git-blame called git_blame2(). Simple, because it displays just three columns: the commit, the line number and the line of code. Alternative, because the original git_blame() is left untouched. Lines of code are printed html escaped, but as-is. git_blame2() uses git-blame as opposed to git-annotate used by git_blame(). Signed-off-by: Luben Tuikov <ltuikov@yahoo.com> Signed-off-by: Junio C Hamano <junkio@cox.net>

Luben Tuikov committed Jul 23, 2006 at 13:34 UTC 1f2857ea32fa82ba6039b7fd298dc763b1e6c112
1 file changed +70 -1
gitweb/gitweb.cgi
+70 -1
@@ -227,7 +227,7 @@ if (!defined $action || $action eq "summary") {
227 git_tag();
228 exit;
229 } elsif ($action eq "blame") {
230 - git_blame();
230 + git_blame2();
231 exit;
232 } else {
233 undef $action;
@@ -1252,6 +1252,75 @@ sub git_tag {
1252 git_footer_html();
1253 }
1254
1255 +sub git_read_blame_line {
1256 + my %bl;
1257 + $_ = shift;
1258 +
1259 + ($bl{'hash'}, $bl{'lineno'}, $bl{'data'}) = /^([0-9a-fA-F]{40}).*?(\d+)\)\s{1}(\s*.*)/;
1260 +
1261 + return %bl;
1262 +}
1263 +
1264 +sub git_blame2 {
1265 + my $fd;
1266 + my $ftype;
1267 + die_error(undef, "Permission denied.") if (!git_get_project_config_bool ('blame'));
1268 + die_error('404 Not Found', "File name not defined") if (!$file_name);
1269 + $hash_base ||= git_read_head($project);
1270 + die_error(undef, "Reading commit failed") unless ($hash_base);
1271 + my %co = git_read_commit($hash_base)
1272 + or die_error(undef, "Reading commit failed");
1273 + if (!defined $hash) {
1274 + $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
1275 + or die_error(undef, "Error looking up file");
1276 + }
1277 + $ftype = git_get_type($hash);
1278 + if ($ftype !~ "blob") {
1279 + die_error("400 Bad Request", "object is not a blob");
1280 + }
1281 + open ($fd, "-|", $GIT, "blame", '-l', $file_name, $hash_base)
1282 + or die_error(undef, "Open failed");
1283 + git_header_html();
1284 + print "<div class=\"page_nav\">\n" .
1285 + $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
1286 + " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
1287 + " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") .
1288 + " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base")}, "commit") .
1289 + " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash_base")}, "commitdiff") .
1290 + " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash_base")}, "tree") . "<br/>\n";
1291 + print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$hash;hb=$hash_base;f=$file_name")}, "blob") .
1292 + " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;f=$file_name")}, "head") . "<br/>\n";
1293 + print "</div>\n".
1294 + "<div>" .
1295 + $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base"), -class => "title"}, esc_html($co{'title'})) .
1296 + "</div>\n";
1297 + git_print_page_path($file_name, $ftype);
1298 +
1299 + print "<div class=\"page_body\">\n";
1300 +
1301 + print "<table class=\"blame\">\n";
1302 + print "<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n";
1303 + while (my $line = <$fd>) {
1304 + my %blame_line = git_read_blame_line($line);
1305 + my $full_rev = $blame_line{'hash'};
1306 + my $rev = substr($full_rev, 0, 8);
1307 + my $lineno = $blame_line{'lineno'};
1308 + my $data = $blame_line{'data'};
1309 +
1310 + print "<tr>\n";
1311 + print "<td class=\"sha1\">" .
1312 + $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$full_rev;f=$file_name")}, esc_html($rev)) . "</td>\n";
1313 + print "<td class=\"linenr\"><a id=\"l$lineno\" href=\"#l$lineno\" class=\"linenr\">" . esc_html($lineno) . "</a></td>\n";
1314 + print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
1315 + print "</tr>\n";
1316 + }
1317 + print "</table>\n";
1318 + print "</div>";
1319 +
1320 + close $fd or print "Reading blob failed\n";
1321 + git_footer_html();
1322 +}
1323 +
1324 sub git_blame {
1325 my $fd;
1326 die_error('403 Permission denied', "Permission denied.") if (!git_get_project_config_bool ('blame'));