gitweb: Adding a `blame' interface.
This patch adds an interface for `git-blame' to `gitweb.cgi'. Links to it are placed in `git_blob'. Internally the code uses `git-annotate' because `git-blame's output differs for files that have been renamed in the past. However, I like the term `blame' better. [jc: blame can be told to produce the compatible format btw...] Signed-off-by: Florian Forster <octo@verplant.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Florian Forster committed
Jun 11, 2006 at 17:45 UTC
e34ef621482a14143187bb99a8e341fad885c5d5
1 file changed
+107
-1
gitweb/gitweb.cgi
+107
-1
@@ -203,6 +203,9 @@ if (!defined $action || $action eq "summary") {
203
} elsif ($action eq "tag") {
204
git_tag();
205
exit;
206
+} elsif ($action eq "blame") {
207
+ git_blame();
208
+ exit;
209
} else {
210
undef $action;
211
die_error(undef, "Unknown action.");
@@ -1228,6 +1231,107 @@ sub git_tag {
1231
git_footer_html();
1232
}
1233
1234
+sub git_blame {
1235
+ my $fd;
1236
+ die_error('404 Not Found', "What file will it be, master?") if (!$file_name);
1237
+ $hash_base ||= git_read_head($project);
1238
+ die_error(undef, "Reading commit failed.") unless ($hash_base);
1239
+ my %co = git_read_commit($hash_base)
1240
+ or die_error(undef, "Reading commit failed.");
1241
+ if (!defined $hash) {
1242
+ $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
1243
+ or die_error(undef, "Error lookup file.");
1244
+ }
1245
+ open ($fd, "-|", "$gitbin/git-annotate", '-l', '-t', '-r', $file_name, $hash_base)
1246
+ or die_error(undef, "Open failed.");
1247
+ git_header_html();
1248
+ print "<div class=\"page_nav\">\n" .
1249
+ $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
1250
+ " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
1251
+ " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") .
1252
+ " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base")}, "commit") .
1253
+ " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash_base")}, "commitdiff") .
1254
+ " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash_base")}, "tree") . "<br/>\n";
1255
+ print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$hash;hb=$hash_base;f=$file_name")}, "blob") .
1256
+ " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;f=$file_name")}, "head") . "<br/>\n";
1257
+ print "</div>\n".
1258
+ "<div>" .
1259
+ $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base"), -class => "title"}, esc_html($co{'title'})) .
1260
+ "</div>\n";
1261
+ print "<div class=\"page_path\"><b>" . esc_html($file_name) . "</b></div>\n";
1262
+ print "<div class=\"page_body\">\n";
1263
+ print <<HTML;
1264
+<table style="border-collapse: collapse;">
1265
+ <tr>
1266
+ <th>Commit</th>
1267
+ <th>Age</th>
1268
+ <th>Author</th>
1269
+ <th>Line</th>
1270
+ <th>Data</th>
1271
+ </tr>
1272
+HTML
1273
+ my @line_class = (qw(light dark));
1274
+ my $line_class_len = scalar (@line_class);
1275
+ my $line_class_num = $#line_class;
1276
+ while (my $line = <$fd>) {
1277
+ my $long_rev;
1278
+ my $short_rev;
1279
+ my $author;
1280
+ my $time;
1281
+ my $lineno;
1282
+ my $data;
1283
+ my $age;
1284
+ my $age_str;
1285
+ my $age_style;
1286
+
1287
+ chomp $line;
1288
+ $line_class_num = ($line_class_num + 1) % $line_class_len;
1289
+
1290
+ if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) \+\d\d\d\d\t(\d+)\)(.*)$/) {
1291
+ $long_rev = $1;
1292
+ $author = $2;
1293
+ $time = $3;
1294
+ $lineno = $4;
1295
+ $data = $5;
1296
+ } else {
1297
+ print qq( <tr><td colspan="5" style="color: red; background-color: yellow;">Unable to parse: $line</td></tr>\n);
1298
+ next;
1299
+ }
1300
+ $short_rev = substr ($long_rev, 0, 8);
1301
+ $age = time () - $time;
1302
+ $age_str = age_string ($age);
1303
+ $age_str =~ s/ / /g;
1304
+ $age_style = 'font-style: italic;';
1305
+ $age_style .= ' color: #009900; background: transparent;' if ($age < 60*60*24*2);
1306
+ $age_style .= ' font-weight: bold;' if ($age < 60*60*2);
1307
+ $author = esc_html ($author);
1308
+ $author =~ s/ / /g;
1309
+ # escape tabs
1310
+ while ((my $pos = index($data, "\t")) != -1) {
1311
+ if (my $count = (8 - ($pos % 8))) {
1312
+ my $spaces = ' ' x $count;
1313
+ $data =~ s/\t/$spaces/;
1314
+ }
1315
+ }
1316
+ $data = esc_html ($data);
1317
+ $data =~ s/ / /g;
1318
+
1319
+ print <<HTML;
1320
+ <tr class="$line_class[$line_class_num]">
1321
+ <td style="font-family: monospace;"><a href="$my_uri?${\esc_param ("p=$project;a=commit;h=$long_rev")}" class="text">$short_rev..</a></td>
1322
+ <td style="$age_style">$age_str</td>
1323
+ <td>$author</td>
1324
+ <td style="text-align: right;"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
1325
+ <td style="font-family: monospace;">$data</td>
1326
+ </tr>
1327
+HTML
1328
+ } # while (my $line = <$fd>)
1329
+ print "</table>\n\n";
1330
+ close $fd or print "Reading blob failed.\n";
1331
+ print "</div>";
1332
+ git_footer_html();
1333
+}
1334
+
1335
sub git_tags {
1336
my $head = git_read_head($project);
1337
git_header_html();
@@ -1375,7 +1479,8 @@ sub git_blob {
1479
" | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash_base")}, "commitdiff") .
1480
" | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash_base")}, "tree") . "<br/>\n";
1481
if (defined $file_name) {
1378
- print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$hash;f=$file_name")}, "plain") .
1482
+ print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;h=$hash;hb=$hash_base;f=$file_name")}, "blame") .
1483
+ " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$hash;f=$file_name")}, "plain") .
1484
" | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;hb=HEAD;f=$file_name")}, "head") . "<br/>\n";
1485
} else {
1486
print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$hash")}, "plain") . "<br/>\n";
@@ -1496,6 +1601,7 @@ sub git_tree {
1601
"</td>\n" .
1602
"<td class=\"link\">" .
1603
$cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$t_hash$base_key;f=$base$t_name")}, "blob") .
1604
+# " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;h=$t_hash$base_key;f=$base$t_name")}, "blame") .
1605
" | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;h=$hash_base;f=$base$t_name")}, "history") .
1606
"</td>\n";
1607
} elsif ($t_type eq "tree") {