@matheusbsbs / matheus-bagliano / commits / d2ce10d7b7
Fork 0

gitweb: A bit of code cleanup in git_blame()

Among others, here are the highlights: * move variable declaration closer to the place it is set and used, if possible, * uniquify and simplify coding style a bit, which includes removing unnecessary '()'. * check type only if $hash was defined, as otherwise from the way git_get_hash_by_path() is called (and works), we know that it is a blob, * use modern calling convention for git-blame, * remove unused variable, * don't use implicit variables ($_), * add some comments Signed-off-by: Jakub Narebski <jnareb@gmail.com> Acked-by: Luben Tuikov <ltuikov@yahoo.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jakub Narebski committed Dec 9, 2008 at 23:48 UTC d2ce10d7b7d67ff8b50ae749ce4c5b1a2b8d133c
1 file changed +39 -28
gitweb/gitweb.perl
+39 -28
@@ -4575,28 +4575,33 @@ sub git_tag {
4575 }
4576
4577 sub git_blame {
4578 - my $fd;
4579 - my $ftype;
4580 -
4578 + # permissions
4579 gitweb_check_feature('blame')
4582 - or die_error(403, "Blame view not allowed");
4580 + or die_error(403, "Blame view not allowed");
4581
4582 + # error checking
4583 die_error(400, "No file name given") unless $file_name;
4584 $hash_base ||= git_get_head_hash($project);
4586 - die_error(404, "Couldn't find base commit") unless ($hash_base);
4585 + die_error(404, "Couldn't find base commit") unless $hash_base;
4586 my %co = parse_commit($hash_base)
4587 or die_error(404, "Commit not found");
4588 + my $ftype = "blob";
4589 if (!defined $hash) {
4590 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
4591 or die_error(404, "Error looking up file");
4592 + } else {
4593 + $ftype = git_get_type($hash);
4594 + if ($ftype !~ "blob") {
4595 + die_error(400, "Object is not a blob");
4596 + }
4597 }
4593 - $ftype = git_get_type($hash);
4594 - if ($ftype !~ "blob") {
4595 - die_error(400, "Object is not a blob");
4596 - }
4597 - open ($fd, "-|", git_cmd(), "blame", '-p', '--',
4598 - $file_name, $hash_base)
4598 +
4599 + # run git-blame --porcelain
4600 + open my $fd, "-|", git_cmd(), "blame", '-p',
4601 + $hash_base, '--', $file_name
4602 or die_error(500, "Open git-blame failed");
4603 +
4604 + # page header
4605 git_header_html();
4606 my $formats_nav =
4607 $cgi->a({-href => href(action=>"blob", -replay=>1)},
@@ -4610,40 +4615,44 @@ sub git_blame {
4615 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4616 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4617 git_print_page_path($file_name, $ftype, $hash_base);
4613 - my @rev_color = (qw(light2 dark2));
4618 +
4619 + # page body
4620 + my @rev_color = qw(light2 dark2);
4621 my $num_colors = scalar(@rev_color);
4622 my $current_color = 0;
4616 - my $last_rev;
4623 + my %metainfo = ();
4624 +
4625 print <<HTML;
4626 <div class="page_body">
4627 <table class="blame">
4628 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
4629 HTML
4622 - my %metainfo = ();
4623 - while (1) {
4624 - $_ = <$fd>;
4625 - last unless defined $_;
4630 + LINE:
4631 + while (my $line = <$fd>) {
4632 + chomp $line;
4633 + # the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]
4634 + # no <lines in group> for subsequent lines in group of lines
4635 my ($full_rev, $orig_lineno, $lineno, $group_size) =
4627 - /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;
4636 + ($line =~ /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);
4637 if (!exists $metainfo{$full_rev}) {
4638 $metainfo{$full_rev} = {};
4639 }
4640 my $meta = $metainfo{$full_rev};
4632 - while (<$fd>) {
4633 - last if (s/^\t//);
4634 - if (/^(\S+) (.*)$/) {
4641 + my $data;
4642 + while ($data = <$fd>) {
4643 + chomp $data;
4644 + last if ($data =~ s/^\t//); # contents of line
4645 + if ($data =~ /^(\S+) (.*)$/) {
4646 $meta->{$1} = $2;
4647 }
4648 }
4638 - my $data = $_;
4639 - chomp $data;
4640 - my $rev = substr($full_rev, 0, 8);
4649 + my $short_rev = substr($full_rev, 0, 8);
4650 my $author = $meta->{'author'};
4642 - my %date = parse_date($meta->{'author-time'},
4643 - $meta->{'author-tz'});
4651 + my %date =
4652 + parse_date($meta->{'author-time'}, $meta->{'author-tz'});
4653 my $date = $date{'iso-tz'};
4654 if ($group_size) {
4646 - $current_color = ++$current_color % $num_colors;
4655 + $current_color = ($current_color + 1) % $num_colors;
4656 }
4657 print "<tr id=\"l$lineno\" class=\"$rev_color[$current_color]\">\n";
4658 if ($group_size) {
@@ -4654,7 +4663,7 @@ HTML
4663 print $cgi->a({-href => href(action=>"commit",
4664 hash=>$full_rev,
4665 file_name=>$file_name)},
4657 - esc_html($rev));
4666 + esc_html($short_rev));
4667 print "</td>\n";
4668 }
4669 open (my $dd, "-|", git_cmd(), "rev-parse", "$full_rev^")
@@ -4677,6 +4686,8 @@ HTML
4686 print "</div>";
4687 close $fd
4688 or print "Reading blob failed\n";
4689 +
4690 + # page footer
4691 git_footer_html();
4692 }
4693