gitweb: Change appereance of marker of refs pointing to given object
[gitweb.git] / gitweb / gitweb.perl
index 28df59e6be63c5c68983ca966db363f17ebb08c3..4fe3fc7b4494c37e05947736f0ae82d6feba5764 100755 (executable)
@@ -364,14 +364,47 @@ sub format_log_line_html {
 # format marker of refs pointing to given object
 sub format_ref_marker {
        my ($refs, $id) = @_;
+       my $markers = '';
 
        if (defined $refs->{$id}) {
-               return ' <span class="tag">' . esc_html($refs->{$id}) . '</span>';
+               foreach my $ref (@{$refs->{$id}}) {
+                       my ($type, $name) = qw();
+                       # e.g. tags/v2.6.11 or heads/next
+                       if ($ref =~ m!^(.*?)s?/(.*)$!) {
+                               $type = $1;
+                               $name = $2;
+                       } else {
+                               $type = "ref";
+                               $name = $ref;
+                       }
+
+                       $markers .= " <span class=\"$type\">" . esc_html($name) . "</span>";
+               }
+       }
+
+       if ($markers) {
+               return ' <span class="refs">'. $markers . '</span>';
        } else {
                return "";
        }
 }
 
+# format, perhaps shortened and with markers, title line
+sub format_subject_html {
+       my ($long, $short, $query, $extra) = @_;
+       $extra = '' unless defined($extra);
+
+       if (length($short) < length($long)) {
+               return $cgi->a({-href => "$my_uri?" . esc_param($query),
+                              -class => "list", -title => $long},
+                      esc_html($short) . $extra);
+       } else {
+               return $cgi->a({-href => "$my_uri?" . esc_param($query),
+                              -class => "list"},
+                      esc_html($long)  . $extra);
+       }
+}
+
 ## ----------------------------------------------------------------------
 ## git utility subroutines, invoking git commands
 
@@ -511,21 +544,58 @@ sub git_get_projects_list {
        return @list;
 }
 
+sub git_get_project_owner {
+       my $project = shift;
+       my $owner;
+
+       return undef unless $project;
+
+       # read from file (url-encoded):
+       # 'git%2Fgit.git Linus+Torvalds'
+       # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
+       # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
+       if (-f $projects_list) {
+               open (my $fd , $projects_list);
+               while (my $line = <$fd>) {
+                       chomp $line;
+                       my ($pr, $ow) = split ' ', $line;
+                       $pr = unescape($pr);
+                       $ow = unescape($ow);
+                       if ($pr eq $project) {
+                               $owner = decode("utf8", $ow, Encode::FB_DEFAULT);
+                               last;
+                       }
+               }
+               close $fd;
+       }
+       if (!defined $owner) {
+               $owner = get_file_owner("$projectroot/$project");
+       }
+
+       return $owner;
+}
+
 sub git_get_references {
        my $type = shift || "";
        my %refs;
+       my $fd;
        # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c      refs/tags/v2.6.11
        # c39ae07f393806ccf406ef966e9a15afc43cc36a      refs/tags/v2.6.11^{}
-       open my $fd, "$projectroot/$project/info/refs" or return;
+       if (-f "$projectroot/$project/info/refs") {
+               open $fd, "$projectroot/$project/info/refs"
+                       or return;
+       } else {
+               open $fd, "-|", $GIT, "ls-remote", "."
+                       or return;
+       }
+
        while (my $line = <$fd>) {
                chomp $line;
-               # attention: for $type == "" it saves only last path part of ref name
-               # e.g. from 'refs/heads/jn/gitweb' it would leave only 'gitweb'
-               if ($line =~ m/^([0-9a-fA-F]{40})\t.*$type\/([^\^]+)/) {
+               if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?[^\^]+)/) {
                        if (defined $refs{$1}) {
-                               $refs{$1} .= " / $2";
+                               push @{$refs{$1}}, $2;
                        } else {
-                               $refs{$1} = $2;
+                               $refs{$1} = [ $2 ];
                        }
                }
        }
@@ -690,6 +760,49 @@ sub parse_commit {
        return %co;
 }
 
+# parse ref from ref_file, given by ref_id, with given type
+sub parse_ref {
+       my $ref_file = shift;
+       my $ref_id = shift;
+       my $type = shift || git_get_type($ref_id);
+       my %ref_item;
+
+       $ref_item{'type'} = $type;
+       $ref_item{'id'} = $ref_id;
+       $ref_item{'epoch'} = 0;
+       $ref_item{'age'} = "unknown";
+       if ($type eq "tag") {
+               my %tag = parse_tag($ref_id);
+               $ref_item{'comment'} = $tag{'comment'};
+               if ($tag{'type'} eq "commit") {
+                       my %co = parse_commit($tag{'object'});
+                       $ref_item{'epoch'} = $co{'committer_epoch'};
+                       $ref_item{'age'} = $co{'age_string'};
+               } elsif (defined($tag{'epoch'})) {
+                       my $age = time - $tag{'epoch'};
+                       $ref_item{'epoch'} = $tag{'epoch'};
+                       $ref_item{'age'} = age_string($age);
+               }
+               $ref_item{'reftype'} = $tag{'type'};
+               $ref_item{'name'} = $tag{'name'};
+               $ref_item{'refid'} = $tag{'object'};
+       } elsif ($type eq "commit"){
+               my %co = parse_commit($ref_id);
+               $ref_item{'reftype'} = "commit";
+               $ref_item{'name'} = $ref_file;
+               $ref_item{'title'} = $co{'title'};
+               $ref_item{'refid'} = $ref_id;
+               $ref_item{'epoch'} = $co{'committer_epoch'};
+               $ref_item{'age'} = $co{'age_string'};
+       } else {
+               $ref_item{'reftype'} = $type;
+               $ref_item{'name'} = $ref_file;
+               $ref_item{'refid'} = $ref_id;
+       }
+
+       return %ref_item;
+}
+
 ## ......................................................................
 ## parse to array of hashes functions
 
@@ -709,44 +822,11 @@ sub git_get_refs_list {
        foreach my $ref_file (@refs) {
                my $ref_id = git_get_hash_by_ref("$project/$ref_dir/$ref_file");
                my $type = git_get_type($ref_id) || next;
-               my %ref_item;
-               my %co;
-               $ref_item{'type'} = $type;
-               $ref_item{'id'} = $ref_id;
-               $ref_item{'epoch'} = 0;
-               $ref_item{'age'} = "unknown";
-               if ($type eq "tag") {
-                       my %tag = parse_tag($ref_id);
-                       $ref_item{'comment'} = $tag{'comment'};
-                       if ($tag{'type'} eq "commit") {
-                               %co = parse_commit($tag{'object'});
-                               $ref_item{'epoch'} = $co{'committer_epoch'};
-                               $ref_item{'age'} = $co{'age_string'};
-                       } elsif (defined($tag{'epoch'})) {
-                               my $age = time - $tag{'epoch'};
-                               $ref_item{'epoch'} = $tag{'epoch'};
-                               $ref_item{'age'} = age_string($age);
-                       }
-                       $ref_item{'reftype'} = $tag{'type'};
-                       $ref_item{'name'} = $tag{'name'};
-                       $ref_item{'refid'} = $tag{'object'};
-               } elsif ($type eq "commit"){
-                       %co = parse_commit($ref_id);
-                       $ref_item{'reftype'} = "commit";
-                       $ref_item{'name'} = $ref_file;
-                       $ref_item{'title'} = $co{'title'};
-                       $ref_item{'refid'} = $ref_id;
-                       $ref_item{'epoch'} = $co{'committer_epoch'};
-                       $ref_item{'age'} = $co{'age_string'};
-               } else {
-                       $ref_item{'reftype'} = $type;
-                       $ref_item{'name'} = $ref_file;
-                       $ref_item{'refid'} = $ref_id;
-               }
+               my %ref_item = parse_ref($ref_file, $ref_id, $type);
 
                push @reflist, \%ref_item;
        }
-       # sort tags by age
+       # sort refs by age
        @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
        return \@reflist;
 }
@@ -1075,15 +1155,7 @@ sub git_shortlog_body {
                print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
                      "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
                      "<td>";
-               if (length($co{'title_short'}) < length($co{'title'})) {
-                       print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"),
-                                      -class => "list", -title => "$co{'title'}"},
-                             "<b>" . esc_html($co{'title_short'}) . "$ref</b>");
-               } else {
-                       print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"),
-                                      -class => "list"},
-                             "<b>" . esc_html($co{'title'}) . "$ref</b>");
-               }
+               print format_subject_html($co{'title'}, $co{'title_short'}, "p=$project;a=commit;h=$commit", $ref);
                print "</td>\n" .
                      "<td class=\"link\">" .
                      $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") . " | " .
@@ -1099,6 +1171,64 @@ sub git_shortlog_body {
        print "</table>\n";
 }
 
+sub git_history_body {
+       # Warning: assumes constant type (blob or tree) during history
+       my ($fd, $refs, $hash_base, $ftype, $extra) = @_;
+
+       print "<table class=\"history\" cellspacing=\"0\">\n";
+       my $alternate = 0;
+       while (my $line = <$fd>) {
+               if ($line !~ m/^([0-9a-fA-F]{40})/) {
+                       next;
+               }
+
+               my $commit = $1;
+               my %co = parse_commit($commit);
+               if (!%co) {
+                       next;
+               }
+
+               my $ref = format_ref_marker($refs, $commit);
+
+               if ($alternate) {
+                       print "<tr class=\"dark\">\n";
+               } else {
+                       print "<tr class=\"light\">\n";
+               }
+               $alternate ^= 1;
+               print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
+                     # shortlog uses      chop_str($co{'author_name'}, 10)
+                     "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
+                     "<td>";
+               # originally git_history used chop_str($co{'title'}, 50)
+               print format_subject_html($co{'title'}, $co{'title_short'}, "p=$project;a=commit;h=$commit", $ref);
+               print "</td>\n" .
+                     "<td class=\"link\">" .
+                     $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") . " | " .
+                     $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$commit")}, "commitdiff") . " | " .
+                     $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$ftype;hb=$commit;f=$file_name")}, $ftype);
+
+               if ($ftype eq 'blob') {
+                       my $blob_current = git_get_hash_by_path($hash_base, $file_name);
+                       my $blob_parent  = git_get_hash_by_path($commit, $file_name);
+                       if (defined $blob_current && defined $blob_parent &&
+                                       $blob_current ne $blob_parent) {
+                               print " | " .
+                                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff;h=$blob_current;hp=$blob_parent;hb=$commit;f=$file_name")},
+                                               "diff to current");
+                       }
+               }
+               print "</td>\n" .
+                     "</tr>\n";
+       }
+       if (defined $extra) {
+               print "<tr>\n" .
+                     "<td colspan=\"4\">$extra</td>\n" .
+                     "</tr>\n";
+       }
+       print "</table>\n";
+}
+
 sub git_tags_body {
        # uses global variable $project
        my ($taglist, $from, $to, $extra) = @_;
@@ -1129,13 +1259,7 @@ sub git_tags_body {
                      "</td>\n" .
                      "<td>";
                if (defined $comment) {
-                       if (length($comment_short) < length($comment)) {
-                               print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}"),
-                                              -class => "list", -title => $comment}, $comment_short);
-                       } else {
-                               print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}"),
-                                              -class => "list"}, $comment);
-                       }
+                       print format_subject_html($comment, $comment_short, "p=$project;a=tag;h=$tag{'id'}");
                }
                print "</td>\n" .
                      "<td class=\"selflink\">";
@@ -1388,24 +1512,7 @@ sub git_summary {
        my %co = parse_commit($head);
        my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
 
-       my $owner;
-       if (-f $projects_list) {
-               open (my $fd , $projects_list);
-               while (my $line = <$fd>) {
-                       chomp $line;
-                       my ($pr, $ow) = split ' ', $line;
-                       $pr = unescape($pr);
-                       $ow = unescape($ow);
-                       if ($pr eq $project) {
-                               $owner = decode("utf8", $ow, Encode::FB_DEFAULT);
-                               last;
-                       }
-               }
-               close $fd;
-       }
-       if (!defined $owner) {
-               $owner = get_file_owner("$projectroot/$project");
-       }
+       my $owner = git_get_project_owner($project);
 
        my $refs = git_get_references();
        git_header_html();
@@ -2285,42 +2392,8 @@ sub git_history {
 
        open my $fd, "-|",
                $GIT, "rev-list", "--full-history", $hash_base, "--", $file_name;
-       print "<table cellspacing=\"0\">\n";
-       my $alternate = 0;
-       while (my $line = <$fd>) {
-               if ($line =~ m/^([0-9a-fA-F]{40})/){
-                       my $commit = $1;
-                       my %co = parse_commit($commit);
-                       if (!%co) {
-                               next;
-                       }
-                       my $ref = format_ref_marker($refs, $commit);
-                       if ($alternate) {
-                               print "<tr class=\"dark\">\n";
-                       } else {
-                               print "<tr class=\"light\">\n";
-                       }
-                       $alternate ^= 1;
-                       print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
-                             "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
-                             "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "list"}, "<b>" .
-                             esc_html(chop_str($co{'title'}, 50)) . "$ref</b>") . "</td>\n" .
-                             "<td class=\"link\">" .
-                             $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") .
-                             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$commit")}, "commitdiff") .
-                             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$ftype;hb=$commit;f=$file_name")}, $ftype);
-                       my $blob = git_get_hash_by_path($hash_base, $file_name);
-                       my $blob_parent = git_get_hash_by_path($commit, $file_name);
-                       if (defined $blob && defined $blob_parent && $blob ne $blob_parent) {
-                               print " | " .
-                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff;h=$blob;hp=$blob_parent;hb=$commit;f=$file_name")},
-                               "diff to current");
-                       }
-                       print "</td>\n" .
-                             "</tr>\n";
-               }
-       }
-       print "</table>\n";
+       git_history_body($fd, $refs, $hash_base, $ftype);
+
        close $fd;
        git_footer_html();
 }