Skip to content

Commit 930cf7d

Browse files
ltuikovJunio C Hamano
authored andcommitted
gitweb.cgi: Teach "a=blob" action to know the blob/file mime type
Now action "blob" knows the file type: if the file type is not "text/*" then action "blob" defaults to "blob_plain", i.e. the file is downloaded raw for the browser to interpret. If the file type is "text/*", then "blob" defaults to the current "cat -n"-like output, from which you can click "plain", to get the "blob_plain" output. Signed-off-by: Luben Tuikov <ltuikov@yahoo.com> Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent 9af2511 commit 930cf7d

File tree

1 file changed

+67
-60
lines changed

1 file changed

+67
-60
lines changed

gitweb/gitweb.cgi

Lines changed: 67 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,61 +1458,6 @@ sub git_get_hash_by_path {
14581458
}
14591459
}
14601460

1461-
sub git_blob {
1462-
if (!defined $hash && defined $file_name) {
1463-
my $base = $hash_base || git_read_head($project);
1464-
$hash = git_get_hash_by_path($base, $file_name, "blob") || die_error(undef, "Error lookup file.");
1465-
}
1466-
my $have_blame = git_get_project_config_bool ('blame');
1467-
open my $fd, "-|", "$gitbin/git-cat-file blob $hash" or die_error(undef, "Open failed.");
1468-
git_header_html();
1469-
if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
1470-
print "<div class=\"page_nav\">\n" .
1471-
$cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
1472-
" | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
1473-
" | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") .
1474-
" | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base")}, "commit") .
1475-
" | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash_base")}, "commitdiff") .
1476-
" | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash_base")}, "tree") . "<br/>\n";
1477-
if (defined $file_name) {
1478-
if ($have_blame) {
1479-
print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;h=$hash;hb=$hash_base;f=$file_name")}, "blame") . " | ";
1480-
}
1481-
print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$hash;f=$file_name")}, "plain") .
1482-
" | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;hb=HEAD;f=$file_name")}, "head") . "<br/>\n";
1483-
} else {
1484-
print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$hash")}, "plain") . "<br/>\n";
1485-
}
1486-
print "</div>\n".
1487-
"<div>" .
1488-
$cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base"), -class => "title"}, esc_html($co{'title'})) .
1489-
"</div>\n";
1490-
} else {
1491-
print "<div class=\"page_nav\">\n" .
1492-
"<br/><br/></div>\n" .
1493-
"<div class=\"title\">$hash</div>\n";
1494-
}
1495-
if (defined $file_name) {
1496-
print "<div class=\"page_path\"><b>" . esc_html($file_name) . "</b></div>\n";
1497-
}
1498-
print "<div class=\"page_body\">\n";
1499-
my $nr;
1500-
while (my $line = <$fd>) {
1501-
chomp $line;
1502-
$nr++;
1503-
while ((my $pos = index($line, "\t")) != -1) {
1504-
if (my $count = (8 - ($pos % 8))) {
1505-
my $spaces = ' ' x $count;
1506-
$line =~ s/\t/$spaces/;
1507-
}
1508-
}
1509-
printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n", $nr, $nr, $nr, esc_html($line);
1510-
}
1511-
close $fd or print "Reading blob failed.\n";
1512-
print "</div>";
1513-
git_footer_html();
1514-
}
1515-
15161461
sub mimetype_guess_file {
15171462
my $filename = shift;
15181463
my $mimemap = shift;
@@ -1551,14 +1496,14 @@ sub git_blob_plain_mimetype {
15511496
my $fd = shift;
15521497
my $filename = shift;
15531498

1554-
# just in case
1555-
return $default_blob_plain_mimetype unless $fd;
1556-
15571499
if ($filename) {
15581500
my $mime = mimetype_guess($filename);
15591501
$mime and return $mime;
15601502
}
15611503

1504+
# just in case
1505+
return $default_blob_plain_mimetype unless $fd;
1506+
15621507
if (-T $fd) {
15631508
return 'text/plain' .
15641509
($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
@@ -1576,8 +1521,10 @@ sub git_blob_plain_mimetype {
15761521
}
15771522

15781523
sub git_blob_plain {
1579-
open my $fd, "-|", "$gitbin/git-cat-file blob $hash" or return;
1580-
my $type = git_blob_plain_mimetype($fd, $file_name);
1524+
my $type = shift;
1525+
open my $fd, "-|", "$gitbin/git-cat-file blob $hash" or die_error("Couldn't cat $file_name, $hash");
1526+
1527+
$type ||= git_blob_plain_mimetype($fd, $file_name);
15811528

15821529
# save as filename, even when no $file_name is given
15831530
my $save_as = "$hash";
@@ -1596,6 +1543,66 @@ sub git_blob_plain {
15961543
close $fd;
15971544
}
15981545

1546+
sub git_blob {
1547+
if (!defined $hash && defined $file_name) {
1548+
my $base = $hash_base || git_read_head($project);
1549+
$hash = git_get_hash_by_path($base, $file_name, "blob") || die_error(undef, "Error lookup file.");
1550+
}
1551+
my $have_blame = git_get_project_config_bool ('blame');
1552+
open my $fd, "-|", "$gitbin/git-cat-file blob $hash" or die_error(undef, "Open failed.");
1553+
my $mimetype = git_blob_plain_mimetype($fd, $file_name);
1554+
if ($mimetype !~ m/^text\//) {
1555+
close $fd;
1556+
return git_blob_plain($mimetype);
1557+
}
1558+
git_header_html();
1559+
if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
1560+
print "<div class=\"page_nav\">\n" .
1561+
$cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
1562+
" | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
1563+
" | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") .
1564+
" | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base")}, "commit") .
1565+
" | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash_base")}, "commitdiff") .
1566+
" | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash_base")}, "tree") . "<br/>\n";
1567+
if (defined $file_name) {
1568+
if ($have_blame) {
1569+
print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;h=$hash;hb=$hash_base;f=$file_name")}, "blame") . " | ";
1570+
}
1571+
print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$hash;f=$file_name")}, "plain") .
1572+
" | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;hb=HEAD;f=$file_name")}, "head") . "<br/>\n";
1573+
} else {
1574+
print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$hash")}, "plain") . "<br/>\n";
1575+
}
1576+
print "</div>\n".
1577+
"<div>" .
1578+
$cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base"), -class => "title"}, esc_html($co{'title'})) .
1579+
"</div>\n";
1580+
} else {
1581+
print "<div class=\"page_nav\">\n" .
1582+
"<br/><br/></div>\n" .
1583+
"<div class=\"title\">$hash</div>\n";
1584+
}
1585+
if (defined $file_name) {
1586+
print "<div class=\"page_path\"><b>" . esc_html($file_name) . "</b></div>\n";
1587+
}
1588+
print "<div class=\"page_body\">\n";
1589+
my $nr;
1590+
while (my $line = <$fd>) {
1591+
chomp $line;
1592+
$nr++;
1593+
while ((my $pos = index($line, "\t")) != -1) {
1594+
if (my $count = (8 - ($pos % 8))) {
1595+
my $spaces = ' ' x $count;
1596+
$line =~ s/\t/$spaces/;
1597+
}
1598+
}
1599+
printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n", $nr, $nr, $nr, esc_html($line);
1600+
}
1601+
close $fd or print "Reading blob failed.\n";
1602+
print "</div>";
1603+
git_footer_html();
1604+
}
1605+
15991606
sub git_tree {
16001607
if (!defined $hash) {
16011608
$hash = git_read_head($project);

0 commit comments

Comments
 (0)