Merge branch 'jn/gitweb-blame'

* jn/gitweb-blame:
  gitweb: cache $parent_commit info in git_blame()
  gitweb: A bit of code cleanup in git_blame()
  gitweb: Move 'lineno' id from link to row element in git_blame
This commit is contained in:
Junio C Hamano 2009-01-07 00:09:06 -08:00
commit d9befc8b0b

View File

@ -4576,28 +4576,33 @@ sub git_tag {
} }
sub git_blame { sub git_blame {
my $fd; # permissions
my $ftype;
gitweb_check_feature('blame') gitweb_check_feature('blame')
or die_error(403, "Blame view not allowed"); or die_error(403, "Blame view not allowed");
# error checking
die_error(400, "No file name given") unless $file_name; die_error(400, "No file name given") unless $file_name;
$hash_base ||= git_get_head_hash($project); $hash_base ||= git_get_head_hash($project);
die_error(404, "Couldn't find base commit") unless ($hash_base); die_error(404, "Couldn't find base commit") unless $hash_base;
my %co = parse_commit($hash_base) my %co = parse_commit($hash_base)
or die_error(404, "Commit not found"); or die_error(404, "Commit not found");
my $ftype = "blob";
if (!defined $hash) { if (!defined $hash) {
$hash = git_get_hash_by_path($hash_base, $file_name, "blob") $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
or die_error(404, "Error looking up file"); or die_error(404, "Error looking up file");
} else {
$ftype = git_get_type($hash);
if ($ftype !~ "blob") {
die_error(400, "Object is not a blob");
}
} }
$ftype = git_get_type($hash);
if ($ftype !~ "blob") { # run git-blame --porcelain
die_error(400, "Object is not a blob"); open my $fd, "-|", git_cmd(), "blame", '-p',
} $hash_base, '--', $file_name
open ($fd, "-|", git_cmd(), "blame", '-p', '--',
$file_name, $hash_base)
or die_error(500, "Open git-blame failed"); or die_error(500, "Open git-blame failed");
# page header
git_header_html(); git_header_html();
my $formats_nav = my $formats_nav =
$cgi->a({-href => href(action=>"blob", -replay=>1)}, $cgi->a({-href => href(action=>"blob", -replay=>1)},
@ -4611,42 +4616,46 @@ sub git_blame {
git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav); git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
git_print_header_div('commit', esc_html($co{'title'}), $hash_base); git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
git_print_page_path($file_name, $ftype, $hash_base); git_print_page_path($file_name, $ftype, $hash_base);
my @rev_color = (qw(light2 dark2));
# page body
my @rev_color = qw(light2 dark2);
my $num_colors = scalar(@rev_color); my $num_colors = scalar(@rev_color);
my $current_color = 0; my $current_color = 0;
my $last_rev; my %metainfo = ();
print <<HTML; print <<HTML;
<div class="page_body"> <div class="page_body">
<table class="blame"> <table class="blame">
<tr><th>Commit</th><th>Line</th><th>Data</th></tr> <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
HTML HTML
my %metainfo = (); LINE:
while (1) { while (my $line = <$fd>) {
$_ = <$fd>; chomp $line;
last unless defined $_; # the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]
# no <lines in group> for subsequent lines in group of lines
my ($full_rev, $orig_lineno, $lineno, $group_size) = my ($full_rev, $orig_lineno, $lineno, $group_size) =
/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/; ($line =~ /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);
if (!exists $metainfo{$full_rev}) { if (!exists $metainfo{$full_rev}) {
$metainfo{$full_rev} = {}; $metainfo{$full_rev} = {};
} }
my $meta = $metainfo{$full_rev}; my $meta = $metainfo{$full_rev};
while (<$fd>) { my $data;
last if (s/^\t//); while ($data = <$fd>) {
if (/^(\S+) (.*)$/) { chomp $data;
last if ($data =~ s/^\t//); # contents of line
if ($data =~ /^(\S+) (.*)$/) {
$meta->{$1} = $2; $meta->{$1} = $2;
} }
} }
my $data = $_; my $short_rev = substr($full_rev, 0, 8);
chomp $data;
my $rev = substr($full_rev, 0, 8);
my $author = $meta->{'author'}; my $author = $meta->{'author'};
my %date = parse_date($meta->{'author-time'}, my %date =
$meta->{'author-tz'}); parse_date($meta->{'author-time'}, $meta->{'author-tz'});
my $date = $date{'iso-tz'}; my $date = $date{'iso-tz'};
if ($group_size) { if ($group_size) {
$current_color = ++$current_color % $num_colors; $current_color = ($current_color + 1) % $num_colors;
} }
print "<tr class=\"$rev_color[$current_color]\">\n"; print "<tr id=\"l$lineno\" class=\"$rev_color[$current_color]\">\n";
if ($group_size) { if ($group_size) {
print "<td class=\"sha1\""; print "<td class=\"sha1\"";
print " title=\"". esc_html($author) . ", $date\""; print " title=\"". esc_html($author) . ", $date\"";
@ -4655,20 +4664,25 @@ HTML
print $cgi->a({-href => href(action=>"commit", print $cgi->a({-href => href(action=>"commit",
hash=>$full_rev, hash=>$full_rev,
file_name=>$file_name)}, file_name=>$file_name)},
esc_html($rev)); esc_html($short_rev));
print "</td>\n"; print "</td>\n";
} }
open (my $dd, "-|", git_cmd(), "rev-parse", "$full_rev^") my $parent_commit;
or die_error(500, "Open git-rev-parse failed"); if (!exists $meta->{'parent'}) {
my $parent_commit = <$dd>; open (my $dd, "-|", git_cmd(), "rev-parse", "$full_rev^")
close $dd; or die_error(500, "Open git-rev-parse failed");
chomp($parent_commit); $parent_commit = <$dd>;
close $dd;
chomp($parent_commit);
$meta->{'parent'} = $parent_commit;
} else {
$parent_commit = $meta->{'parent'};
}
my $blamed = href(action => 'blame', my $blamed = href(action => 'blame',
file_name => $meta->{'filename'}, file_name => $meta->{'filename'},
hash_base => $parent_commit); hash_base => $parent_commit);
print "<td class=\"linenr\">"; print "<td class=\"linenr\">";
print $cgi->a({ -href => "$blamed#l$orig_lineno", print $cgi->a({ -href => "$blamed#l$orig_lineno",
-id => "l$lineno",
-class => "linenr" }, -class => "linenr" },
esc_html($lineno)); esc_html($lineno));
print "</td>"; print "</td>";
@ -4679,6 +4693,8 @@ HTML
print "</div>"; print "</div>";
close $fd close $fd
or print "Reading blob failed\n"; or print "Reading blob failed\n";
# page footer
git_footer_html(); git_footer_html();
} }