]> Lady’s Gitweb - Gitweb/blobdiff - gitweb.perl
Update the gitweb/README file to include setting the GITWEB_CONFIG environment
[Gitweb] / gitweb.perl
old mode 100755 (executable)
new mode 100644 (file)
index 590ebdd..fd9c4a4
@@ -102,6 +102,10 @@ our %feature = (
                'sub' => \&feature_pickaxe,
                'override' => 0,
                'default' => [1]},
+
+       'pathinfo' => {
+               'override' => 0,
+               'default' => [0]},
 );
 
 sub gitweb_check_feature {
@@ -180,6 +184,22 @@ sub feature_pickaxe {
        return ($_[0]);
 }
 
+# checking HEAD file with -e is fragile if the repository was
+# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
+# and then pruned.
+sub check_head_link {
+       my ($dir) = @_;
+       my $headfile = "$dir/HEAD";
+       return ((-e $headfile) ||
+               (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
+}
+
+sub check_export_ok {
+       my ($dir) = @_;
+       return (check_head_link($dir) &&
+               (!$export_ok || -e "$dir/$export_ok"));
+}
+
 # rename detection options for git-diff and git-diff-tree
 # - default is '-M', with the cost proportional to
 #   (number of removed files) * (number of new files).
@@ -212,7 +232,7 @@ our $project = $cgi->param('p');
 if (defined $project) {
        if (!validate_pathname($project) ||
            !(-d "$projectroot/$project") ||
-           !(-e "$projectroot/$project/HEAD") ||
+           !check_head_link("$projectroot/$project") ||
            ($export_ok && !(-e "$projectroot/$project/$export_ok")) ||
            ($strict_export && !project_in_list($project))) {
                undef $project;
@@ -289,7 +309,7 @@ sub evaluate_path_info {
        # find which part of PATH_INFO is project
        $project = $path_info;
        $project =~ s,/+$,,;
-       while ($project && !-e "$projectroot/$project/HEAD") {
+       while ($project && !check_head_link("$projectroot/$project")) {
                $project =~ s,/*[^/]*$,,;
        }
        # validate project
@@ -375,6 +395,7 @@ exit;
 
 sub href(%) {
        my %params = @_;
+       my $href = $my_uri;
 
        my @mapping = (
                project => "p",
@@ -393,6 +414,19 @@ sub href(%) {
 
        $params{'project'} = $project unless exists $params{'project'};
 
+       my ($use_pathinfo) = gitweb_check_feature('pathinfo');
+       if ($use_pathinfo) {
+               # use PATH_INFO for project name
+               $href .= "/$params{'project'}" if defined $params{'project'};
+               delete $params{'project'};
+
+               # Summary just uses the project path URL
+               if (defined $params{'action'} && $params{'action'} eq 'summary') {
+                       delete $params{'action'};
+               }
+       }
+
+       # now encode the parameters explicitly
        my @result = ();
        for (my $i = 0; $i < @mapping; $i += 2) {
                my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
@@ -400,7 +434,9 @@ sub href(%) {
                        push @result, $symbol . "=" . esc_param($params{$name});
                }
        }
-       return "$my_uri?" . join(';', @result);
+       $href .= "?" . join(';', @result) if scalar @result;
+
+       return $href;
 }
 
 
@@ -465,6 +501,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;
 }
 
@@ -815,8 +852,7 @@ sub git_get_projects_list {
 
                                my $subdir = substr($File::Find::name, $pfxlen + 1);
                                # we check related file in $projectroot
-                               if (-e "$projectroot/$subdir/HEAD" && (!$export_ok ||
-                                   -e "$projectroot/$subdir/$export_ok")) {
+                               if (check_export_ok("$projectroot/$subdir")) {
                                        push @list, { path => $subdir };
                                        $File::Find::prune = 1;
                                }
@@ -837,8 +873,7 @@ sub git_get_projects_list {
                        if (!defined $path) {
                                next;
                        }
-                       if (-e "$projectroot/$path/HEAD" && (!$export_ok ||
-                           -e "$projectroot/$path/$export_ok")) {
+                       if (check_export_ok("$projectroot/$path")) {
                                my $pr = {
                                        path => $path,
                                        owner => decode("utf8", $owner, Encode::FB_DEFAULT),
@@ -2429,9 +2464,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) {
@@ -2474,27 +2564,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>";
This page took 0.292865 seconds and 4 git commands to generate.