gitweb: make leftmost column of blame less cluttered.
[gitweb.git] / gitweb / gitweb.perl
old mode 100755 (executable)
new mode 100644 (file)
index 9550bd7..55d1b2c
@@ -155,6 +155,13 @@ sub feature_snapshot {
        return ($ctype, $suffix, $command);
 }
 
+sub gitweb_have_snapshot {
+       my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
+       my $have_snapshot = (defined $ctype && defined $suffix);
+
+       return $have_snapshot;
+}
+
 # To enable system wide have in $GITWEB_CONFIG
 # $feature{'pickaxe'}{'default'} = [1];
 # To have project specific config enable override in $GITWEB_CONFIG
@@ -458,6 +465,7 @@ sub esc_html {
        $str = decode("utf8", $str, Encode::FB_DEFAULT);
        $str = escapeHTML($str);
        $str =~ s/\014/^L/g; # escape FORM FEED (FF) character (e.g. in COPYING file)
+       $str =~ s/\033/^[/g; # "escape" ESCAPE (\e) character (e.g. commit 20a3847d8a5032ce41f90dcc68abfb36e6fee9b1)
        return $str;
 }
 
@@ -657,7 +665,7 @@ sub format_subject_html {
 
        if (length($short) < length($long)) {
                return $cgi->a({-href => $href, -class => "list subject",
-                               -title => $long},
+                               -title => decode("utf8", $long, Encode::FB_DEFAULT)},
                       esc_html($short) . $extra);
        } else {
                return $cgi->a({-href => $href, -class => "list subject"},
@@ -1665,9 +1673,9 @@ sub git_print_tree_entry {
                                      "history");
                }
                print " | " .
-                     $cgi->a({-href => href(action=>"blob_plain",
-                                            hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
-                             "raw");
+                       $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
+                                              file_name=>"$basedir$t->{'name'}")},
+                               "raw");
                print "</td>\n";
 
        } elsif ($t->{'type'} eq "tree") {
@@ -2013,9 +2021,9 @@ sub git_shortlog_body {
                                          href(action=>"commit", hash=>$commit), $ref);
                print "</td>\n" .
                      "<td class=\"link\">" .
-                     $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
                      $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
-                     $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
+                     $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") . " | " .
+                     $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
                print "</td>\n" .
                      "</tr>\n";
        }
@@ -2064,9 +2072,8 @@ sub git_history_body {
                                          href(action=>"commit", hash=>$commit), $ref);
                print "</td>\n" .
                      "<td class=\"link\">" .
-                     $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
-                     $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
-                     $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype);
+                     $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
+                     $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
 
                if ($ftype eq 'blob') {
                        my $blob_current = git_get_hash_by_path($hash_base, $file_name);
@@ -2423,9 +2430,64 @@ sub git_tag {
        git_footer_html();
 }
 
+sub git_blame_flush_chunk {
+       my ($name, $revdata, $color, $rev, @line) = @_;
+       my $label = substr($rev, 0, 8);
+       my $line = scalar(@line);
+       my $cnt = 0;
+       my $pop = '';
+
+       if ($revdata->{$rev} ne '') {
+               $pop = ' title="' . esc_html($revdata->{$rev}) . '"';
+       }
+
+       for (@line) {
+               my ($lineno, $data) = @$_;
+               $cnt++;
+               print "<tr class=\"$color\">\n";
+               if ($cnt == 1) {
+                       print "<td class=\"sha1\"$pop";
+                       if ($line > 1) {
+                               print " rowspan=\"$line\"";
+                       }
+                       print ">";
+                       print $cgi->a({-href => href(action=>"commit",
+                                                    hash=>$rev,
+                                                    file_name=>$name)},
+                                     $label);
+                       print "</td>\n";
+               }
+               print "<td class=\"linenr\">".
+                   "<a id=\"l$lineno\" href=\"#l$lineno\" class=\"linenr\">" .
+                   esc_html($lineno) . "</a></td>\n";
+               print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
+               print "</tr>\n";
+       }
+}
+
+# We can have up to N*2 lines.  If it is more than N lines, split it
+# into two to avoid orphans.
+sub git_blame_flush_chunk_1 {
+       my ($chunk_cap, $name, $revdata, $color, $rev, @chunk) = @_;
+       if ($chunk_cap < @chunk) {
+               my @first = splice(@chunk, 0, @chunk/2);
+               git_blame_flush_chunk($name,
+                                     $revdata,
+                                     $color,
+                                     $rev,
+                                     @first);
+       }
+       git_blame_flush_chunk($name,
+                             $revdata,
+                             $color,
+                             $rev,
+                             @chunk);
+}
+
 sub git_blame2 {
        my $fd;
        my $ftype;
+       my $chunk_cap = 20;
 
        my ($have_blame) = gitweb_check_feature('blame');
        if (!$have_blame) {
@@ -2468,27 +2530,45 @@ sub git_blame2 {
 <table class="blame">
 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
 HTML
+       my @chunk = ();
+       my %revdata = ();
        while (<$fd>) {
                /^([0-9a-fA-F]{40}).*?(\d+)\)\s{1}(\s*.*)/;
-               my $full_rev = $1;
-               my $rev = substr($full_rev, 0, 8);
-               my $lineno = $2;
-               my $data = $3;
-
+               my ($full_rev, $author, $date, $lineno, $data) =
+                   /^([0-9a-f]{40}).*?\s\((.*?)\s+([-\d]+ [:\d]+ [-+\d]+)\s+(\d+)\)\s(.*)/;
+               if (!exists $revdata{$full_rev}) {
+                       $revdata{$full_rev} = "$author, $date";
+               }
                if (!defined $last_rev) {
                        $last_rev = $full_rev;
                } elsif ($last_rev ne $full_rev) {
+                       git_blame_flush_chunk_1($chunk_cap,
+                                               $file_name,
+                                               \%revdata,
+                                               $rev_color[$current_color],
+                                               $last_rev, @chunk);
+                       @chunk = ();
                        $last_rev = $full_rev;
                        $current_color = ++$current_color % $num_colors;
                }
-               print "<tr class=\"$rev_color[$current_color]\">\n";
-               print "<td class=\"sha1\">" .
-                       $cgi->a({-href => href(action=>"commit", hash=>$full_rev, file_name=>$file_name)},
-                               esc_html($rev)) . "</td>\n";
-               print "<td class=\"linenr\"><a id=\"l$lineno\" href=\"#l$lineno\" class=\"linenr\">" .
-                     esc_html($lineno) . "</a></td>\n";
-               print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
-               print "</tr>\n";
+               elsif ($chunk_cap * 2 < @chunk) {
+                       # We have more than N*2 lines from the same
+                       # revision.  Flush N lines and leave N lines
+                       # in @chunk to avoid orphaned lines.
+                       my @first = splice(@chunk, 0, $chunk_cap);
+                       git_blame_flush_chunk($file_name,
+                                             \%revdata,
+                                             $rev_color[$current_color],
+                                             $last_rev, @first);
+               }
+               push @chunk, [$lineno, $data];
+       }
+       if (@chunk) {
+               git_blame_flush_chunk_1($chunk_cap,
+                                       $file_name,
+                                       \%revdata,
+                                       $rev_color[$current_color],
+                                       $last_rev, @chunk);
        }
        print "</table>\n";
        print "</div>";
@@ -2653,7 +2733,7 @@ sub git_blob_plain {
        print $cgi->header(
                -type => "$type",
                -expires=>$expires,
-               -content_disposition => 'inline; filename="' . quotemeta($save_as) . '"');
+               -content_disposition => 'inline; filename="' . "$save_as" . '"');
        undef $/;
        binmode STDOUT, ':raw';
        print <$fd>;
@@ -2737,17 +2817,16 @@ sub git_blob {
 }
 
 sub git_tree {
-       my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
-       my $have_snapshot = (defined $ctype && defined $suffix);
+       my $have_snapshot = gitweb_have_snapshot();
 
+       if (!defined $hash_base) {
+               $hash_base = "HEAD";
+       }
        if (!defined $hash) {
-               $hash = git_get_head_hash($project);
                if (defined $file_name) {
-                       my $base = $hash_base || $hash;
-                       $hash = git_get_hash_by_path($base, $file_name, "tree");
-               }
-               if (!defined $hash_base) {
-                       $hash_base = $hash;
+                       $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
+               } else {
+                       $hash = $hash_base;
                }
        }
        $/ = "\0";
@@ -2814,7 +2893,6 @@ sub git_tree {
 }
 
 sub git_snapshot {
-
        my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
        my $have_snapshot = (defined $ctype && defined $suffix);
        if (!$have_snapshot) {
@@ -2830,7 +2908,7 @@ sub git_snapshot {
        print $cgi->header(
                -type => 'application/x-tar',
                -content_encoding => $ctype,
-               -content_disposition => 'inline; filename="' . quotemeta($filename) . '"',
+               -content_disposition => 'inline; filename="' . "$filename" . '"',
                -status => '200 OK');
 
        my $git_command = git_cmd_str();
@@ -2924,12 +3002,10 @@ sub git_commit {
        my $refs = git_get_references();
        my $ref = format_ref_marker($refs, $co{'id'});
 
-       my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
-       my $have_snapshot = (defined $ctype && defined $suffix);
+       my $have_snapshot = gitweb_have_snapshot();
 
        my @views_nav = ();
        if (defined $file_name && defined $co{'parent'}) {
-               my $parent = $co{'parent'};
                push @views_nav,
                        $cgi->a({-href => href(action=>"blame", hash_parent=>$parent, file_name=>$file_name)},
                                "blame");
@@ -3141,7 +3217,7 @@ sub git_blobdiff {
                        -type => 'text/plain',
                        -charset => 'utf-8',
                        -expires => $expires,
-                       -content_disposition => 'inline; filename="' . quotemeta($file_name) . '.patch"');
+                       -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
 
                print "X-Git-Url: " . $cgi->self_url() . "\n\n";
 
@@ -3244,7 +3320,7 @@ sub git_commitdiff {
                        -type => 'text/plain',
                        -charset => 'utf-8',
                        -expires => $expires,
-                       -content_disposition => 'inline; filename="' . quotemeta($filename) . '"');
+                       -content_disposition => 'inline; filename="' . "$filename" . '"');
                my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
                print <<TEXT;
 From: $co{'author'}