Merge branch 'pb/config' into next
[git] / gitweb / gitweb.cgi
1 #!/usr/bin/perl
2
3 # gitweb - simple web interface to track changes in git repositories
4 #
5 # (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org>
6 # (C) 2005, Christian Gierke
7 #
8 # This program is licensed under the GPLv2
9
10 use strict;
11 use warnings;
12 use CGI qw(:standard :escapeHTML -nosticky);
13 use CGI::Util qw(unescape);
14 use CGI::Carp qw(fatalsToBrowser);
15 use Encode;
16 use Fcntl ':mode';
17 binmode STDOUT, ':utf8';
18
19 my $cgi = new CGI;
20 my $version =           "267";
21 my $my_url =            $cgi->url();
22 my $my_uri =            $cgi->url(-absolute => 1);
23 my $rss_link =          "";
24
25 # absolute fs-path which will be prepended to the project path
26 #my $projectroot =      "/pub/scm";
27 my $projectroot =       "/home/kay/public_html/pub/scm";
28
29 # location of the git-core binaries
30 my $gitbin =            "/usr/bin";
31
32 # location for temporary files needed for diffs
33 my $git_temp =          "/tmp/gitweb";
34
35 # target of the home link on top of all pages
36 my $home_link =         $my_uri;
37
38 # html text to include at home page
39 my $home_text =         "indextext.html";
40
41 # URI of default stylesheet
42 my $stylesheet =        "gitweb.css";
43
44 # source of projects list
45 #my $projects_list =    $projectroot;
46 my $projects_list =     "index/index.aux";
47
48 # default blob_plain mimetype and default charset for text/plain blob
49 my $default_blob_plain_mimetype = 'text/plain';
50 my $default_text_plain_charset  = undef;
51
52 # file to use for guessing MIME types before trying /etc/mime.types
53 # (relative to the current git repository)
54 my $mimetypes_file              = undef;
55
56
57 # input validation and dispatch
58 my $action = $cgi->param('a');
59 if (defined $action) {
60         if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
61                 undef $action;
62                 die_error(undef, "Invalid action parameter.");
63         }
64         if ($action eq "git-logo.png") {
65                 git_logo();
66                 exit;
67         } elsif ($action eq "opml") {
68                 git_opml();
69                 exit;
70         }
71 }
72
73 my $order = $cgi->param('o');
74 if (defined $order) {
75         if ($order =~ m/[^0-9a-zA-Z_]/) {
76                 undef $order;
77                 die_error(undef, "Invalid order parameter.");
78         }
79 }
80
81 my $project = $cgi->param('p');
82 if (defined $project) {
83         $project = validate_input($project);
84         if (!defined($project)) {
85                 die_error(undef, "Invalid project parameter.");
86         }
87         if (!(-d "$projectroot/$project")) {
88                 undef $project;
89                 die_error(undef, "No such directory.");
90         }
91         if (!(-e "$projectroot/$project/HEAD")) {
92                 undef $project;
93                 die_error(undef, "No such project.");
94         }
95         $rss_link = "<link rel=\"alternate\" title=\"" . esc_param($project) . " log\" href=\"" .
96                     "$my_uri?" . esc_param("p=$project;a=rss") . "\" type=\"application/rss+xml\"/>";
97         $ENV{'GIT_DIR'} = "$projectroot/$project";
98 } else {
99         git_project_list();
100         exit;
101 }
102
103 my $file_name = $cgi->param('f');
104 if (defined $file_name) {
105         $file_name = validate_input($file_name);
106         if (!defined($file_name)) {
107                 die_error(undef, "Invalid file parameter.");
108         }
109 }
110
111 my $hash = $cgi->param('h');
112 if (defined $hash) {
113         $hash = validate_input($hash);
114         if (!defined($hash)) {
115                 die_error(undef, "Invalid hash parameter.");
116         }
117 }
118
119 my $hash_parent = $cgi->param('hp');
120 if (defined $hash_parent) {
121         $hash_parent = validate_input($hash_parent);
122         if (!defined($hash_parent)) {
123                 die_error(undef, "Invalid hash parent parameter.");
124         }
125 }
126
127 my $hash_base = $cgi->param('hb');
128 if (defined $hash_base) {
129         $hash_base = validate_input($hash_base);
130         if (!defined($hash_base)) {
131                 die_error(undef, "Invalid hash base parameter.");
132         }
133 }
134
135 my $page = $cgi->param('pg');
136 if (defined $page) {
137         if ($page =~ m/[^0-9]$/) {
138                 undef $page;
139                 die_error(undef, "Invalid page parameter.");
140         }
141 }
142
143 my $searchtext = $cgi->param('s');
144 if (defined $searchtext) {
145         if ($searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
146                 undef $searchtext;
147                 die_error(undef, "Invalid search parameter.");
148         }
149         $searchtext = quotemeta $searchtext;
150 }
151
152 sub validate_input {
153         my $input = shift;
154
155         if ($input =~ m/^[0-9a-fA-F]{40}$/) {
156                 return $input;
157         }
158         if ($input =~ m/(^|\/)(|\.|\.\.)($|\/)/) {
159                 return undef;
160         }
161         if ($input =~ m/[^a-zA-Z0-9_\x80-\xff\ \t\.\/\-\+\#\~\%]/) {
162                 return undef;
163         }
164         return $input;
165 }
166
167 if (!defined $action || $action eq "summary") {
168         git_summary();
169         exit;
170 } elsif ($action eq "heads") {
171         git_heads();
172         exit;
173 } elsif ($action eq "tags") {
174         git_tags();
175         exit;
176 } elsif ($action eq "blob") {
177         git_blob();
178         exit;
179 } elsif ($action eq "blob_plain") {
180         git_blob_plain();
181         exit;
182 } elsif ($action eq "tree") {
183         git_tree();
184         exit;
185 } elsif ($action eq "rss") {
186         git_rss();
187         exit;
188 } elsif ($action eq "commit") {
189         git_commit();
190         exit;
191 } elsif ($action eq "log") {
192         git_log();
193         exit;
194 } elsif ($action eq "blobdiff") {
195         git_blobdiff();
196         exit;
197 } elsif ($action eq "blobdiff_plain") {
198         git_blobdiff_plain();
199         exit;
200 } elsif ($action eq "commitdiff") {
201         git_commitdiff();
202         exit;
203 } elsif ($action eq "commitdiff_plain") {
204         git_commitdiff_plain();
205         exit;
206 } elsif ($action eq "history") {
207         git_history();
208         exit;
209 } elsif ($action eq "search") {
210         git_search();
211         exit;
212 } elsif ($action eq "shortlog") {
213         git_shortlog();
214         exit;
215 } elsif ($action eq "tag") {
216         git_tag();
217         exit;
218 } elsif ($action eq "blame") {
219         git_blame();
220         exit;
221 } else {
222         undef $action;
223         die_error(undef, "Unknown action.");
224         exit;
225 }
226
227 # quote unsafe chars, but keep the slash, even when it's not
228 # correct, but quoted slashes look too horrible in bookmarks
229 sub esc_param {
230         my $str = shift;
231         $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
232         $str =~ s/\+/%2B/g;
233         $str =~ s/ /\+/g;
234         return $str;
235 }
236
237 # replace invalid utf8 character with SUBSTITUTION sequence
238 sub esc_html {
239         my $str = shift;
240         $str = decode("utf8", $str, Encode::FB_DEFAULT);
241         $str = escapeHTML($str);
242         return $str;
243 }
244
245 # git may return quoted and escaped filenames
246 sub unquote {
247         my $str = shift;
248         if ($str =~ m/^"(.*)"$/) {
249                 $str = $1;
250                 $str =~ s/\\([0-7]{1,3})/chr(oct($1))/eg;
251         }
252         return $str;
253 }
254
255 sub git_header_html {
256         my $status = shift || "200 OK";
257         my $expires = shift;
258
259         my $title = "git";
260         if (defined $project) {
261                 $title .= " - $project";
262                 if (defined $action) {
263                         $title .= "/$action";
264                 }
265         }
266         print $cgi->header(-type=>'text/html',  -charset => 'utf-8', -status=> $status, -expires => $expires);
267         print <<EOF;
268 <?xml version="1.0" encoding="utf-8"?>
269 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
270 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
271 <!-- git web interface v$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
272 <head>
273 <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
274 <meta name="robots" content="index, nofollow"/>
275 <link rel="stylesheet" href="$stylesheet"/>
276 <title>$title</title>
277 $rss_link
278 </head>
279 <body>
280 EOF
281         print "<div class=\"page_header\">\n" .
282               "<a href=\"http://www.kernel.org/pub/software/scm/git/docs/\" title=\"git documentation\">" .
283               "<img src=\"$my_uri?" . esc_param("a=git-logo.png") . "\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/>" .
284               "</a>\n";
285         print $cgi->a({-href => esc_param($home_link)}, "projects") . " / ";
286         if (defined $project) {
287                 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, esc_html($project));
288                 if (defined $action) {
289                         print " / $action";
290                 }
291                 print "\n";
292                 if (!defined $searchtext) {
293                         $searchtext = "";
294                 }
295                 my $search_hash;
296                 if (defined $hash) {
297                         $search_hash = $hash;
298                 } else {
299                         $search_hash  = "HEAD";
300                 }
301                 $cgi->param("a", "search");
302                 $cgi->param("h", $search_hash);
303                 print $cgi->startform(-method => "get", -action => $my_uri) .
304                       "<div class=\"search\">\n" .
305                       $cgi->hidden(-name => "p") . "\n" .
306                       $cgi->hidden(-name => "a") . "\n" .
307                       $cgi->hidden(-name => "h") . "\n" .
308                       $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
309                       "</div>" .
310                       $cgi->end_form() . "\n";
311         }
312         print "</div>\n";
313 }
314
315 sub git_footer_html {
316         print "<div class=\"page_footer\">\n";
317         if (defined $project) {
318                 my $descr = git_read_description($project);
319                 if (defined $descr) {
320                         print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
321                 }
322                 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=rss"), -class => "rss_logo"}, "RSS") . "\n";
323         } else {
324                 print $cgi->a({-href => "$my_uri?" . esc_param("a=opml"), -class => "rss_logo"}, "OPML") . "\n";
325         }
326         print "</div>\n" .
327               "</body>\n" .
328               "</html>";
329 }
330
331 sub die_error {
332         my $status = shift || "403 Forbidden";
333         my $error = shift || "Malformed query, file missing or permission denied"; 
334
335         git_header_html($status);
336         print "<div class=\"page_body\">\n" .
337               "<br/><br/>\n" .
338               "$status - $error\n" .
339               "<br/>\n" .
340               "</div>\n";
341         git_footer_html();
342         exit;
343 }
344
345 sub git_get_type {
346         my $hash = shift;
347
348         open my $fd, "-|", "$gitbin/git-cat-file -t $hash" or return;
349         my $type = <$fd>;
350         close $fd or return;
351         chomp $type;
352         return $type;
353 }
354
355 sub git_read_head {
356         my $project = shift;
357         my $oENV = $ENV{'GIT_DIR'};
358         my $retval = undef;
359         $ENV{'GIT_DIR'} = "$projectroot/$project";
360         if (open my $fd, "-|", "$gitbin/git-rev-parse", "--verify", "HEAD") {
361                 my $head = <$fd>;
362                 close $fd;
363                 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
364                         $retval = $1;
365                 }
366         }
367         if (defined $oENV) {
368                 $ENV{'GIT_DIR'} = $oENV;
369         }
370         return $retval;
371 }
372
373 sub git_read_hash {
374         my $path = shift;
375
376         open my $fd, "$projectroot/$path" or return undef;
377         my $head = <$fd>;
378         close $fd;
379         chomp $head;
380         if ($head =~ m/^[0-9a-fA-F]{40}$/) {
381                 return $head;
382         }
383 }
384
385 sub git_read_description {
386         my $path = shift;
387
388         open my $fd, "$projectroot/$path/description" or return undef;
389         my $descr = <$fd>;
390         close $fd;
391         chomp $descr;
392         return $descr;
393 }
394
395 sub git_read_tag {
396         my $tag_id = shift;
397         my %tag;
398         my @comment;
399
400         open my $fd, "-|", "$gitbin/git-cat-file tag $tag_id" or return;
401         $tag{'id'} = $tag_id;
402         while (my $line = <$fd>) {
403                 chomp $line;
404                 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
405                         $tag{'object'} = $1;
406                 } elsif ($line =~ m/^type (.+)$/) {
407                         $tag{'type'} = $1;
408                 } elsif ($line =~ m/^tag (.+)$/) {
409                         $tag{'name'} = $1;
410                 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
411                         $tag{'author'} = $1;
412                         $tag{'epoch'} = $2;
413                         $tag{'tz'} = $3;
414                 } elsif ($line =~ m/--BEGIN/) {
415                         push @comment, $line;
416                         last;
417                 } elsif ($line eq "") {
418                         last;
419                 }
420         }
421         push @comment, <$fd>;
422         $tag{'comment'} = \@comment;
423         close $fd or return;
424         if (!defined $tag{'name'}) {
425                 return
426         };
427         return %tag
428 }
429
430 sub age_string {
431         my $age = shift;
432         my $age_str;
433
434         if ($age > 60*60*24*365*2) {
435                 $age_str = (int $age/60/60/24/365);
436                 $age_str .= " years ago";
437         } elsif ($age > 60*60*24*(365/12)*2) {
438                 $age_str = int $age/60/60/24/(365/12);
439                 $age_str .= " months ago";
440         } elsif ($age > 60*60*24*7*2) {
441                 $age_str = int $age/60/60/24/7;
442                 $age_str .= " weeks ago";
443         } elsif ($age > 60*60*24*2) {
444                 $age_str = int $age/60/60/24;
445                 $age_str .= " days ago";
446         } elsif ($age > 60*60*2) {
447                 $age_str = int $age/60/60;
448                 $age_str .= " hours ago";
449         } elsif ($age > 60*2) {
450                 $age_str = int $age/60;
451                 $age_str .= " min ago";
452         } elsif ($age > 2) {
453                 $age_str = int $age;
454                 $age_str .= " sec ago";
455         } else {
456                 $age_str .= " right now";
457         }
458         return $age_str;
459 }
460
461 sub git_read_commit {
462         my $commit_id = shift;
463         my $commit_text = shift;
464
465         my @commit_lines;
466         my %co;
467
468         if (defined $commit_text) {
469                 @commit_lines = @$commit_text;
470         } else {
471                 $/ = "\0";
472                 open my $fd, "-|", "$gitbin/git-rev-list --header --parents --max-count=1 $commit_id" or return;
473                 @commit_lines = split '\n', <$fd>;
474                 close $fd or return;
475                 $/ = "\n";
476                 pop @commit_lines;
477         }
478         my $header = shift @commit_lines;
479         if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
480                 return;
481         }
482         ($co{'id'}, my @parents) = split ' ', $header;
483         $co{'parents'} = \@parents;
484         $co{'parent'} = $parents[0];
485         while (my $line = shift @commit_lines) {
486                 last if $line eq "\n";
487                 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
488                         $co{'tree'} = $1;
489                 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
490                         $co{'author'} = $1;
491                         $co{'author_epoch'} = $2;
492                         $co{'author_tz'} = $3;
493                         if ($co{'author'} =~ m/^([^<]+) </) {
494                                 $co{'author_name'} = $1;
495                         } else {
496                                 $co{'author_name'} = $co{'author'};
497                         }
498                 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
499                         $co{'committer'} = $1;
500                         $co{'committer_epoch'} = $2;
501                         $co{'committer_tz'} = $3;
502                         $co{'committer_name'} = $co{'committer'};
503                         $co{'committer_name'} =~ s/ <.*//;
504                 }
505         }
506         if (!defined $co{'tree'}) {
507                 return;
508         };
509
510         foreach my $title (@commit_lines) {
511                 $title =~ s/^    //;
512                 if ($title ne "") {
513                         $co{'title'} = chop_str($title, 80, 5);
514                         # remove leading stuff of merges to make the interesting part visible
515                         if (length($title) > 50) {
516                                 $title =~ s/^Automatic //;
517                                 $title =~ s/^merge (of|with) /Merge ... /i;
518                                 if (length($title) > 50) {
519                                         $title =~ s/(http|rsync):\/\///;
520                                 }
521                                 if (length($title) > 50) {
522                                         $title =~ s/(master|www|rsync)\.//;
523                                 }
524                                 if (length($title) > 50) {
525                                         $title =~ s/kernel.org:?//;
526                                 }
527                                 if (length($title) > 50) {
528                                         $title =~ s/\/pub\/scm//;
529                                 }
530                         }
531                         $co{'title_short'} = chop_str($title, 50, 5);
532                         last;
533                 }
534         }
535         # remove added spaces
536         foreach my $line (@commit_lines) {
537                 $line =~ s/^    //;
538         }
539         $co{'comment'} = \@commit_lines;
540
541         my $age = time - $co{'committer_epoch'};
542         $co{'age'} = $age;
543         $co{'age_string'} = age_string($age);
544         my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
545         if ($age > 60*60*24*7*2) {
546                 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
547                 $co{'age_string_age'} = $co{'age_string'};
548         } else {
549                 $co{'age_string_date'} = $co{'age_string'};
550                 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
551         }
552         return %co;
553 }
554
555 sub git_diff_print {
556         my $from = shift;
557         my $from_name = shift;
558         my $to = shift;
559         my $to_name = shift;
560         my $format = shift || "html";
561
562         my $from_tmp = "/dev/null";
563         my $to_tmp = "/dev/null";
564         my $pid = $$;
565
566         # create tmp from-file
567         if (defined $from) {
568                 $from_tmp = "$git_temp/gitweb_" . $$ . "_from";
569                 open my $fd2, "> $from_tmp";
570                 open my $fd, "-|", "$gitbin/git-cat-file blob $from";
571                 my @file = <$fd>;
572                 print $fd2 @file;
573                 close $fd2;
574                 close $fd;
575         }
576
577         # create tmp to-file
578         if (defined $to) {
579                 $to_tmp = "$git_temp/gitweb_" . $$ . "_to";
580                 open my $fd2, "> $to_tmp";
581                 open my $fd, "-|", "$gitbin/git-cat-file blob $to";
582                 my @file = <$fd>;
583                 print $fd2 @file;
584                 close $fd2;
585                 close $fd;
586         }
587
588         open my $fd, "-|", "/usr/bin/diff -u -p -L \'$from_name\' -L \'$to_name\' $from_tmp $to_tmp";
589         if ($format eq "plain") {
590                 undef $/;
591                 print <$fd>;
592                 $/ = "\n";
593         } else {
594                 while (my $line = <$fd>) {
595                         chomp($line);
596                         my $char = substr($line, 0, 1);
597                         my $color = "";
598                         if ($char eq '+') {
599                                 $color = " style=\"color:#008800;\"";
600                         } elsif ($char eq "-") {
601                                 $color = " style=\"color:#cc0000;\"";
602                         } elsif ($char eq "@") {
603                                 $color = " style=\"color:#990099;\"";
604                         } elsif ($char eq "\\") {
605                                 # skip errors
606                                 next;
607                         }
608                         while ((my $pos = index($line, "\t")) != -1) {
609                                 if (my $count = (8 - (($pos-1) % 8))) {
610                                         my $spaces = ' ' x $count;
611                                         $line =~ s/\t/$spaces/;
612                                 }
613                         }
614                         print "<div class=\"pre\"$color>" . esc_html($line) . "</div>\n";
615                 }
616         }
617         close $fd;
618
619         if (defined $from) {
620                 unlink($from_tmp);
621         }
622         if (defined $to) {
623                 unlink($to_tmp);
624         }
625 }
626
627 sub mode_str {
628         my $mode = oct shift;
629
630         if (S_ISDIR($mode & S_IFMT)) {
631                 return 'drwxr-xr-x';
632         } elsif (S_ISLNK($mode)) {
633                 return 'lrwxrwxrwx';
634         } elsif (S_ISREG($mode)) {
635                 # git cares only about the executable bit
636                 if ($mode & S_IXUSR) {
637                         return '-rwxr-xr-x';
638                 } else {
639                         return '-rw-r--r--';
640                 };
641         } else {
642                 return '----------';
643         }
644 }
645
646 sub chop_str {
647         my $str = shift;
648         my $len = shift;
649         my $add_len = shift || 10;
650
651         # allow only $len chars, but don't cut a word if it would fit in $add_len
652         # if it doesn't fit, cut it if it's still longer than the dots we would add
653         $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
654         my $body = $1;
655         my $tail = $2;
656         if (length($tail) > 4) {
657                 $tail = " ...";
658         }
659         return "$body$tail";
660 }
661
662 sub file_type {
663         my $mode = oct shift;
664
665         if (S_ISDIR($mode & S_IFMT)) {
666                 return "directory";
667         } elsif (S_ISLNK($mode)) {
668                 return "symlink";
669         } elsif (S_ISREG($mode)) {
670                 return "file";
671         } else {
672                 return "unknown";
673         }
674 }
675
676 sub format_log_line_html {
677         my $line = shift;
678
679         $line = esc_html($line);
680         $line =~ s/ /&nbsp;/g;
681         if ($line =~ m/([0-9a-fA-F]{40})/) {
682                 my $hash_text = $1;
683                 if (git_get_type($hash_text) eq "commit") {
684                         my $link = $cgi->a({-class => "text", -href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_text")}, $hash_text);
685                         $line =~ s/$hash_text/$link/;
686                 }
687         }
688         return $line;
689 }
690
691 sub date_str {
692         my $epoch = shift;
693         my $tz = shift || "-0000";
694
695         my %date;
696         my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
697         my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
698         my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
699         $date{'hour'} = $hour;
700         $date{'minute'} = $min;
701         $date{'mday'} = $mday;
702         $date{'day'} = $days[$wday];
703         $date{'month'} = $months[$mon];
704         $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000", $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
705         $date{'mday-time'} = sprintf "%d %s %02d:%02d", $mday, $months[$mon], $hour ,$min;
706
707         $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
708         my $local = $epoch + ((int $1 + ($2/60)) * 3600);
709         ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
710         $date{'hour_local'} = $hour;
711         $date{'minute_local'} = $min;
712         $date{'tz_local'} = $tz;
713         return %date;
714 }
715
716 # git-logo (cached in browser for one day)
717 sub git_logo {
718         binmode STDOUT, ':raw';
719         print $cgi->header(-type => 'image/png', -expires => '+1d');
720         # cat git-logo.png | hexdump -e '16/1 " %02x"  "\n"' | sed 's/ /\\x/g'
721         print   "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52" .
722                 "\x00\x00\x00\x48\x00\x00\x00\x1b\x04\x03\x00\x00\x00\x2d\xd9\xd4" .
723                 "\x2d\x00\x00\x00\x18\x50\x4c\x54\x45\xff\xff\xff\x60\x60\x5d\xb0" .
724                 "\xaf\xaa\x00\x80\x00\xce\xcd\xc7\xc0\x00\x00\xe8\xe8\xe6\xf7\xf7" .
725                 "\xf6\x95\x0c\xa7\x47\x00\x00\x00\x73\x49\x44\x41\x54\x28\xcf\x63" .
726                 "\x48\x67\x20\x04\x4a\x5c\x18\x0a\x08\x2a\x62\x53\x61\x20\x02\x08" .
727                 "\x0d\x69\x45\xac\xa1\xa1\x01\x30\x0c\x93\x60\x36\x26\x52\x91\xb1" .
728                 "\x01\x11\xd6\xe1\x55\x64\x6c\x6c\xcc\x6c\x6c\x0c\xa2\x0c\x70\x2a" .
729                 "\x62\x06\x2a\xc1\x62\x1d\xb3\x01\x02\x53\xa4\x08\xe8\x00\x03\x18" .
730                 "\x26\x56\x11\xd4\xe1\x20\x97\x1b\xe0\xb4\x0e\x35\x24\x71\x29\x82" .
731                 "\x99\x30\xb8\x93\x0a\x11\xb9\x45\x88\xc1\x8d\xa0\xa2\x44\x21\x06" .
732                 "\x27\x41\x82\x40\x85\xc1\x45\x89\x20\x70\x01\x00\xa4\x3d\x21\xc5" .
733                 "\x12\x1c\x9a\xfe\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82";
734 }
735
736 sub get_file_owner {
737         my $path = shift;
738
739         my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
740         my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
741         if (!defined $gcos) {
742                 return undef;
743         }
744         my $owner = $gcos;
745         $owner =~ s/[,;].*$//;
746         return decode("utf8", $owner, Encode::FB_DEFAULT);
747 }
748
749 sub git_read_projects {
750         my @list;
751
752         if (-d $projects_list) {
753                 # search in directory
754                 my $dir = $projects_list;
755                 opendir my $dh, $dir or return undef;
756                 while (my $dir = readdir($dh)) {
757                         if (-e "$projectroot/$dir/HEAD") {
758                                 my $pr = {
759                                         path => $dir,
760                                 };
761                                 push @list, $pr
762                         }
763                 }
764                 closedir($dh);
765         } elsif (-f $projects_list) {
766                 # read from file(url-encoded):
767                 # 'git%2Fgit.git Linus+Torvalds'
768                 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
769                 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
770                 open my $fd , $projects_list or return undef;
771                 while (my $line = <$fd>) {
772                         chomp $line;
773                         my ($path, $owner) = split ' ', $line;
774                         $path = unescape($path);
775                         $owner = unescape($owner);
776                         if (!defined $path) {
777                                 next;
778                         }
779                         if (-e "$projectroot/$path/HEAD") {
780                                 my $pr = {
781                                         path => $path,
782                                         owner => decode("utf8", $owner, Encode::FB_DEFAULT),
783                                 };
784                                 push @list, $pr
785                         }
786                 }
787                 close $fd;
788         }
789         @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
790         return @list;
791 }
792
793 sub git_get_project_config {
794         my $key = shift;
795
796         return unless ($key);
797         $key =~ s/^gitweb\.//;
798         return if ($key =~ m/\W/);
799
800         my $val = qx(git-repo-config --get gitweb.$key);
801         return ($val);
802 }
803
804 sub git_get_project_config_bool {
805         my $val = git_get_project_config (@_);
806         if ($val and $val =~ m/true|yes|on/) {
807                 return (1);
808         }
809         return; # implicit false
810 }
811
812 sub git_project_list {
813         my @list = git_read_projects();
814         my @projects;
815         if (!@list) {
816                 die_error(undef, "No project found.");
817         }
818         foreach my $pr (@list) {
819                 my $head = git_read_head($pr->{'path'});
820                 if (!defined $head) {
821                         next;
822                 }
823                 $ENV{'GIT_DIR'} = "$projectroot/$pr->{'path'}";
824                 my %co = git_read_commit($head);
825                 if (!%co) {
826                         next;
827                 }
828                 $pr->{'commit'} = \%co;
829                 if (!defined $pr->{'descr'}) {
830                         my $descr = git_read_description($pr->{'path'}) || "";
831                         $pr->{'descr'} = chop_str($descr, 25, 5);
832                 }
833                 if (!defined $pr->{'owner'}) {
834                         $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
835                 }
836                 push @projects, $pr;
837         }
838         git_header_html();
839         if (-f $home_text) {
840                 print "<div class=\"index_include\">\n";
841                 open (my $fd, $home_text);
842                 print <$fd>;
843                 close $fd;
844                 print "</div>\n";
845         }
846         print "<table cellspacing=\"0\">\n" .
847               "<tr>\n";
848         if (!defined($order) || (defined($order) && ($order eq "project"))) {
849                 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
850                 print "<th>Project</th>\n";
851         } else {
852                 print "<th>" . $cgi->a({-class => "header", -href => "$my_uri?" . esc_param("o=project")}, "Project") . "</th>\n";
853         }
854         if (defined($order) && ($order eq "descr")) {
855                 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
856                 print "<th>Description</th>\n";
857         } else {
858                 print "<th>" . $cgi->a({-class => "header", -href => "$my_uri?" . esc_param("o=descr")}, "Description") . "</th>\n";
859         }
860         if (defined($order) && ($order eq "owner")) {
861                 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
862                 print "<th>Owner</th>\n";
863         } else {
864                 print "<th>" . $cgi->a({-class => "header", -href => "$my_uri?" . esc_param("o=owner")}, "Owner") . "</th>\n";
865         }
866         if (defined($order) && ($order eq "age")) {
867                 @projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;
868                 print "<th>Last Change</th>\n";
869         } else {
870                 print "<th>" . $cgi->a({-class => "header", -href => "$my_uri?" . esc_param("o=age")}, "Last Change") . "</th>\n";
871         }
872         print "<th></th>\n" .
873               "</tr>\n";
874         my $alternate = 0;
875         foreach my $pr (@projects) {
876                 if ($alternate) {
877                         print "<tr class=\"dark\">\n";
878                 } else {
879                         print "<tr class=\"light\">\n";
880                 }
881                 $alternate ^= 1;
882                 print "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=summary"), -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
883                       "<td>$pr->{'descr'}</td>\n" .
884                       "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
885                 my $colored_age;
886                 if ($pr->{'commit'}{'age'} < 60*60*2) {
887                         $colored_age = "<span style =\"color: #009900;\"><b><i>$pr->{'commit'}{'age_string'}</i></b></span>";
888                 } elsif ($pr->{'commit'}{'age'} < 60*60*24*2) {
889                         $colored_age = "<span style =\"color: #009900;\"><i>$pr->{'commit'}{'age_string'}</i></span>";
890                 } else {
891                         $colored_age = "<i>$pr->{'commit'}{'age_string'}</i>";
892                 }
893                 print "<td>$colored_age</td>\n" .
894                       "<td class=\"link\">" .
895                       $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=summary")}, "summary") .
896                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=shortlog")}, "shortlog") .
897                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=log")}, "log") .
898                       "</td>\n" .
899                       "</tr>\n";
900         }
901         print "</table>\n";
902         git_footer_html();
903 }
904
905 sub read_info_ref {
906         my $type = shift || "";
907         my %refs;
908         # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c      refs/tags/v2.6.11
909         # c39ae07f393806ccf406ef966e9a15afc43cc36a      refs/tags/v2.6.11^{}
910         open my $fd, "$projectroot/$project/info/refs" or return;
911         while (my $line = <$fd>) {
912                 chomp($line);
913                 if ($line =~ m/^([0-9a-fA-F]{40})\t.*$type\/([^\^]+)/) {
914                         if (defined $refs{$1}) {
915                                 $refs{$1} .= " / $2";
916                         } else {
917                                 $refs{$1} = $2;
918                         }
919                 }
920         }
921         close $fd or return;
922         return \%refs;
923 }
924
925 sub git_read_refs {
926         my $ref_dir = shift;
927         my @reflist;
928
929         my @refs;
930         opendir my $dh, "$projectroot/$project/$ref_dir";
931         while (my $dir = readdir($dh)) {
932                 if ($dir =~ m/^\./) {
933                         next;
934                 }
935                 if (-d "$projectroot/$project/$ref_dir/$dir") {
936                         opendir my $dh2, "$projectroot/$project/$ref_dir/$dir";
937                         my @subdirs = grep !m/^\./, readdir $dh2;
938                         closedir($dh2);
939                         foreach my $subdir (@subdirs) {
940                                 push @refs, "$dir/$subdir"
941                         }
942                         next;
943                 }
944                 push @refs, $dir;
945         }
946         closedir($dh);
947         foreach my $ref_file (@refs) {
948                 my $ref_id = git_read_hash("$project/$ref_dir/$ref_file");
949                 my $type = git_get_type($ref_id) || next;
950                 my %ref_item;
951                 my %co;
952                 $ref_item{'type'} = $type;
953                 $ref_item{'id'} = $ref_id;
954                 $ref_item{'epoch'} = 0;
955                 $ref_item{'age'} = "unknown";
956                 if ($type eq "tag") {
957                         my %tag = git_read_tag($ref_id);
958                         $ref_item{'comment'} = $tag{'comment'};
959                         if ($tag{'type'} eq "commit") {
960                                 %co = git_read_commit($tag{'object'});
961                                 $ref_item{'epoch'} = $co{'committer_epoch'};
962                                 $ref_item{'age'} = $co{'age_string'};
963                         } elsif (defined($tag{'epoch'})) {
964                                 my $age = time - $tag{'epoch'};
965                                 $ref_item{'epoch'} = $tag{'epoch'};
966                                 $ref_item{'age'} = age_string($age);
967                         }
968                         $ref_item{'reftype'} = $tag{'type'};
969                         $ref_item{'name'} = $tag{'name'};
970                         $ref_item{'refid'} = $tag{'object'};
971                 } elsif ($type eq "commit"){
972                         %co = git_read_commit($ref_id);
973                         $ref_item{'reftype'} = "commit";
974                         $ref_item{'name'} = $ref_file;
975                         $ref_item{'title'} = $co{'title'};
976                         $ref_item{'refid'} = $ref_id;
977                         $ref_item{'epoch'} = $co{'committer_epoch'};
978                         $ref_item{'age'} = $co{'age_string'};
979                 }
980
981                 push @reflist, \%ref_item;
982         }
983         # sort tags by age
984         @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
985         return \@reflist;
986 }
987
988 sub git_summary {
989         my $descr = git_read_description($project) || "none";
990         my $head = git_read_head($project);
991         my %co = git_read_commit($head);
992         my %cd = date_str($co{'committer_epoch'}, $co{'committer_tz'});
993
994         my $owner;
995         if (-f $projects_list) {
996                 open (my $fd , $projects_list);
997                 while (my $line = <$fd>) {
998                         chomp $line;
999                         my ($pr, $ow) = split ' ', $line;
1000                         $pr = unescape($pr);
1001                         $ow = unescape($ow);
1002                         if ($pr eq $project) {
1003                                 $owner = decode("utf8", $ow, Encode::FB_DEFAULT);
1004                                 last;
1005                         }
1006                 }
1007                 close $fd;
1008         }
1009         if (!defined $owner) {
1010                 $owner = get_file_owner("$projectroot/$project");
1011         }
1012
1013         my $refs = read_info_ref();
1014         git_header_html();
1015         print "<div class=\"page_nav\">\n" .
1016               "summary".
1017               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
1018               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") .
1019               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$head")}, "commit") .
1020               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$head")}, "commitdiff") .
1021               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree")}, "tree") .
1022               "<br/><br/>\n" .
1023               "</div>\n";
1024         print "<div class=\"title\">&nbsp;</div>\n";
1025         print "<table cellspacing=\"0\">\n" .
1026               "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
1027               "<tr><td>owner</td><td>$owner</td></tr>\n" .
1028               "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n" .
1029               "</table>\n";
1030         open my $fd, "-|", "$gitbin/git-rev-list --max-count=17 " . git_read_head($project) or die_error(undef, "Open failed.");
1031         my (@revlist) = map { chomp; $_ } <$fd>;
1032         close $fd;
1033         print "<div>\n" .
1034               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog"), -class => "title"}, "shortlog") .
1035               "</div>\n";
1036         my $i = 16;
1037         print "<table cellspacing=\"0\">\n";
1038         my $alternate = 0;
1039         foreach my $commit (@revlist) {
1040                 my %co = git_read_commit($commit);
1041                 my %ad = date_str($co{'author_epoch'});
1042                 if ($alternate) {
1043                         print "<tr class=\"dark\">\n";
1044                 } else {
1045                         print "<tr class=\"light\">\n";
1046                 }
1047                 $alternate ^= 1;
1048                 if ($i-- > 0) {
1049                         my $ref = "";
1050                         if (defined $refs->{$commit}) {
1051                                 $ref = " <span class=\"tag\">" . esc_html($refs->{$commit}) . "</span>";
1052                         }
1053                         print "<td><i>$co{'age_string'}</i></td>\n" .
1054                               "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
1055                               "<td>";
1056                         if (length($co{'title_short'}) < length($co{'title'})) {
1057                                 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "list", -title => "$co{'title'}"},
1058                                       "<b>" . esc_html($co{'title_short'}) . "$ref</b>");
1059                         } else {
1060                                 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "list"},
1061                                       "<b>" . esc_html($co{'title'}) . "$ref</b>");
1062                         }
1063                         print "</td>\n" .
1064                               "<td class=\"link\">" .
1065                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") .
1066                               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$commit")}, "commitdiff") .
1067                               "</td>\n" .
1068                               "</tr>";
1069                 } else {
1070                         print "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "...") . "</td>\n" .
1071                         "</tr>";
1072                         last;
1073                 }
1074         }
1075         print "</table\n>";
1076
1077         my $taglist = git_read_refs("refs/tags");
1078         if (defined @$taglist) {
1079                 print "<div>\n" .
1080                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tags"), -class => "title"}, "tags") .
1081                       "</div>\n";
1082                 my $i = 16;
1083                 print "<table cellspacing=\"0\">\n";
1084                 my $alternate = 0;
1085                 foreach my $entry (@$taglist) {
1086                         my %tag = %$entry;
1087                         my $comment_lines = $tag{'comment'};
1088                         my $comment = shift @$comment_lines;
1089                         if (defined($comment)) {
1090                                 $comment = chop_str($comment, 30, 5);
1091                         }
1092                         if ($alternate) {
1093                                 print "<tr class=\"dark\">\n";
1094                         } else {
1095                                 print "<tr class=\"light\">\n";
1096                         }
1097                         $alternate ^= 1;
1098                         if ($i-- > 0) {
1099                                 print "<td><i>$tag{'age'}</i></td>\n" .
1100                                       "<td>" .
1101                                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'reftype'};h=$tag{'refid'}"), -class => "list"},
1102                                       "<b>" . esc_html($tag{'name'}) . "</b>") .
1103                                       "</td>\n" .
1104                                       "<td>";
1105                                 if (defined($comment)) {
1106                                       print $cgi->a({-class => "list", -href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}")}, $comment);
1107                                 }
1108                                 print "</td>\n" .
1109                                       "<td class=\"link\">";
1110                                 if ($tag{'type'} eq "tag") {
1111                                       print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}")}, "tag") . " | ";
1112                                 }
1113                                 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'reftype'};h=$tag{'refid'}")}, $tag{'reftype'});
1114                                 if ($tag{'reftype'} eq "commit") {
1115                                       print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}")}, "shortlog") .
1116                                             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$tag{'refid'}")}, "log");
1117                                 }
1118                                 print "</td>\n" .
1119                                       "</tr>";
1120                         } else {
1121                                 print "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tags")}, "...") . "</td>\n" .
1122                                 "</tr>";
1123                                 last;
1124                         }
1125                 }
1126                 print "</table\n>";
1127         }
1128
1129         my $headlist = git_read_refs("refs/heads");
1130         if (defined @$headlist) {
1131                 print "<div>\n" .
1132                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=heads"), -class => "title"}, "heads") .
1133                       "</div>\n";
1134                 my $i = 16;
1135                 print "<table cellspacing=\"0\">\n";
1136                 my $alternate = 0;
1137                 foreach my $entry (@$headlist) {
1138                         my %tag = %$entry;
1139                         if ($alternate) {
1140                                 print "<tr class=\"dark\">\n";
1141                         } else {
1142                                 print "<tr class=\"light\">\n";
1143                         }
1144                         $alternate ^= 1;
1145                         if ($i-- > 0) {
1146                                 print "<td><i>$tag{'age'}</i></td>\n" .
1147                                       "<td>" .
1148                                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}"), -class => "list"},
1149                                       "<b>" . esc_html($tag{'name'}) . "</b>") .
1150                                       "</td>\n" .
1151                                       "<td class=\"link\">" .
1152                                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}")}, "shortlog") .
1153                                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$tag{'name'}")}, "log") .
1154                                       "</td>\n" .
1155                                       "</tr>";
1156                         } else {
1157                                 print "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=heads")}, "...") . "</td>\n" .
1158                                 "</tr>";
1159                                 last;
1160                         }
1161                 }
1162                 print "</table\n>";
1163         }
1164         git_footer_html();
1165 }
1166
1167 sub git_tag {
1168         my $head = git_read_head($project);
1169         git_header_html();
1170         print "<div class=\"page_nav\">\n" .
1171               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
1172               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
1173               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") .
1174               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$head")}, "commit") .
1175               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$head")}, "commitdiff") .
1176               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;hb=$head")}, "tree") . "<br/>\n" .
1177               "<br/>\n" .
1178               "</div>\n";
1179         my %tag = git_read_tag($hash);
1180         print "<div>\n" .
1181               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash"), -class => "title"}, esc_html($tag{'name'})) . "\n" .
1182               "</div>\n";
1183         print "<div class=\"title_text\">\n" .
1184               "<table cellspacing=\"0\">\n" .
1185               "<tr>\n" .
1186               "<td>object</td>\n" .
1187               "<td>" . $cgi->a({-class => "list", -href => "$my_uri?" . esc_param("p=$project;a=$tag{'type'};h=$tag{'object'}")}, $tag{'object'}) . "</td>\n" .
1188               "<td class=\"link\">" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'type'};h=$tag{'object'}")}, $tag{'type'}) . "</td>\n" .
1189               "</tr>\n";
1190         if (defined($tag{'author'})) {
1191                 my %ad = date_str($tag{'epoch'}, $tag{'tz'});
1192                 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
1193                 print "<tr><td></td><td>" . $ad{'rfc2822'} . sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) . "</td></tr>\n";
1194         }
1195         print "</table>\n\n" .
1196               "</div>\n";
1197         print "<div class=\"page_body\">";
1198         my $comment = $tag{'comment'};
1199         foreach my $line (@$comment) {
1200                 print esc_html($line) . "<br/>\n";
1201         }
1202         print "</div>\n";
1203         git_footer_html();
1204 }
1205
1206 sub git_blame {
1207         my $fd;
1208         die_error('403 Permission denied', "Permission denied.") if (!git_get_project_config_bool ('blame'));
1209         die_error('404 Not Found', "What file will it be, master?") if (!$file_name);
1210         $hash_base ||= git_read_head($project);
1211         die_error(undef, "Reading commit failed.") unless ($hash_base);
1212         my %co = git_read_commit($hash_base)
1213                 or die_error(undef, "Reading commit failed.");
1214         if (!defined $hash) {
1215                 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
1216                         or die_error(undef, "Error lookup file.");
1217         }
1218         open ($fd, "-|", "$gitbin/git-annotate", '-l', '-t', '-r', $file_name, $hash_base)
1219                 or die_error(undef, "Open failed.");
1220         git_header_html();
1221         print "<div class=\"page_nav\">\n" .
1222                 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
1223                 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
1224                 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") .
1225                 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base")}, "commit") .
1226                 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash_base")}, "commitdiff") .
1227                 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash_base")}, "tree") . "<br/>\n";
1228         print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$hash;hb=$hash_base;f=$file_name")}, "blob") .
1229                 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;f=$file_name")}, "head") . "<br/>\n";
1230         print "</div>\n".
1231                 "<div>" .
1232                 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base"), -class => "title"}, esc_html($co{'title'})) .
1233                 "</div>\n";
1234         print "<div class=\"page_path\"><b>" . esc_html($file_name) . "</b></div>\n";
1235         print "<div class=\"page_body\">\n";
1236         print <<HTML;
1237 <table style="border-collapse: collapse;">
1238   <tr>
1239     <th>Commit</th>
1240     <th>Age</th>
1241     <th>Author</th>
1242     <th>Line</th>
1243     <th>Data</th>
1244   </tr>
1245 HTML
1246         my @line_class = (qw(light dark));
1247         my $line_class_len = scalar (@line_class);
1248         my $line_class_num = $#line_class;
1249         while (my $line = <$fd>) {
1250                 my $long_rev;
1251                 my $short_rev;
1252                 my $author;
1253                 my $time;
1254                 my $lineno;
1255                 my $data;
1256                 my $age;
1257                 my $age_str;
1258                 my $age_style;
1259
1260                 chomp $line;
1261                 $line_class_num = ($line_class_num + 1) % $line_class_len;
1262
1263                 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) \+\d\d\d\d\t(\d+)\)(.*)$/) {
1264                         $long_rev = $1;
1265                         $author   = $2;
1266                         $time     = $3;
1267                         $lineno   = $4;
1268                         $data     = $5;
1269                 } else {
1270                         print qq(  <tr><td colspan="5" style="color: red; background-color: yellow;">Unable to parse: $line</td></tr>\n);
1271                         next;
1272                 }
1273                 $short_rev  = substr ($long_rev, 0, 8);
1274                 $age        = time () - $time;
1275                 $age_str    = age_string ($age);
1276                 $age_str    =~ s/ /&nbsp;/g;
1277                 $age_style  = 'font-style: italic;';
1278                 $age_style .= ' color: #009900; background: transparent;' if ($age < 60*60*24*2);
1279                 $age_style .= ' font-weight: bold;' if ($age < 60*60*2);
1280                 $author     = esc_html ($author);
1281                 $author     =~ s/ /&nbsp;/g;
1282                 # escape tabs
1283                 while ((my $pos = index($data, "\t")) != -1) {
1284                         if (my $count = (8 - ($pos % 8))) {
1285                                 my $spaces = ' ' x $count;
1286                                 $data =~ s/\t/$spaces/;
1287                         }
1288                 }
1289                 $data = esc_html ($data);
1290                 $data =~ s/ /&nbsp;/g;
1291
1292                 print <<HTML;
1293   <tr class="$line_class[$line_class_num]">
1294     <td style="font-family: monospace;"><a href="$my_uri?${\esc_param ("p=$project;a=commit;h=$long_rev")}" class="text">$short_rev..</a></td>
1295     <td style="$age_style">$age_str</td>
1296     <td>$author</td>
1297     <td style="text-align: right;"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
1298     <td style="font-family: monospace;">$data</td>
1299   </tr>
1300 HTML
1301         } # while (my $line = <$fd>)
1302         print "</table>\n\n";
1303         close $fd or print "Reading blob failed.\n";
1304         print "</div>";
1305         git_footer_html();
1306 }
1307
1308 sub git_tags {
1309         my $head = git_read_head($project);
1310         git_header_html();
1311         print "<div class=\"page_nav\">\n" .
1312               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
1313               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
1314               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") .
1315               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$head")}, "commit") .
1316               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$head")}, "commitdiff") .
1317               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;hb=$head")}, "tree") . "<br/>\n" .
1318               "<br/>\n" .
1319               "</div>\n";
1320         my $taglist = git_read_refs("refs/tags");
1321         print "<div>\n" .
1322               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary"), -class => "title"}, "&nbsp;") .
1323               "</div>\n";
1324         print "<table cellspacing=\"0\">\n";
1325         my $alternate = 0;
1326         if (defined @$taglist) {
1327                 foreach my $entry (@$taglist) {
1328                         my %tag = %$entry;
1329                         my $comment_lines = $tag{'comment'};
1330                         my $comment = shift @$comment_lines;
1331                         if (defined($comment)) {
1332                                 $comment = chop_str($comment, 30, 5);
1333                         }
1334                         if ($alternate) {
1335                                 print "<tr class=\"dark\">\n";
1336                         } else {
1337                                 print "<tr class=\"light\">\n";
1338                         }
1339                         $alternate ^= 1;
1340                         print "<td><i>$tag{'age'}</i></td>\n" .
1341                               "<td>" .
1342                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'reftype'};h=$tag{'refid'}"), -class => "list"},
1343                               "<b>" . esc_html($tag{'name'}) . "</b>") .
1344                               "</td>\n" .
1345                               "<td>";
1346                         if (defined($comment)) {
1347                               print $cgi->a({-class => "list", -href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}")}, $comment);
1348                         }
1349                         print "</td>\n" .
1350                               "<td class=\"link\">";
1351                         if ($tag{'type'} eq "tag") {
1352                               print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}")}, "tag") . " | ";
1353                         }
1354                         print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'reftype'};h=$tag{'refid'}")}, $tag{'reftype'});
1355                         if ($tag{'reftype'} eq "commit") {
1356                               print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}")}, "shortlog") .
1357                                     " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$tag{'refid'}")}, "log");
1358                         }
1359                         print "</td>\n" .
1360                               "</tr>";
1361                 }
1362         }
1363         print "</table\n>";
1364         git_footer_html();
1365 }
1366
1367 sub git_heads {
1368         my $head = git_read_head($project);
1369         git_header_html();
1370         print "<div class=\"page_nav\">\n" .
1371               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
1372               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
1373               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") .
1374               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$head")}, "commit") .
1375               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$head")}, "commitdiff") .
1376               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;hb=$head")}, "tree") . "<br/>\n" .
1377               "<br/>\n" .
1378               "</div>\n";
1379         my $taglist = git_read_refs("refs/heads");
1380         print "<div>\n" .
1381               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary"), -class => "title"}, "&nbsp;") .
1382               "</div>\n";
1383         print "<table cellspacing=\"0\">\n";
1384         my $alternate = 0;
1385         if (defined @$taglist) {
1386                 foreach my $entry (@$taglist) {
1387                         my %tag = %$entry;
1388                         if ($alternate) {
1389                                 print "<tr class=\"dark\">\n";
1390                         } else {
1391                                 print "<tr class=\"light\">\n";
1392                         }
1393                         $alternate ^= 1;
1394                         print "<td><i>$tag{'age'}</i></td>\n" .
1395                               "<td>" .
1396                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}"), -class => "list"}, "<b>" . esc_html($tag{'name'}) . "</b>") .
1397                               "</td>\n" .
1398                               "<td class=\"link\">" .
1399                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}")}, "shortlog") .
1400                               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$tag{'name'}")}, "log") .
1401                               "</td>\n" .
1402                               "</tr>";
1403                 }
1404         }
1405         print "</table\n>";
1406         git_footer_html();
1407 }
1408
1409 sub git_get_hash_by_path {
1410         my $base = shift;
1411         my $path = shift || return undef;
1412
1413         my $tree = $base;
1414         my @parts = split '/', $path;
1415         while (my $part = shift @parts) {
1416                 open my $fd, "-|", "$gitbin/git-ls-tree $tree" or die_error(undef, "Open git-ls-tree failed.");
1417                 my (@entries) = map { chomp; $_ } <$fd>;
1418                 close $fd or return undef;
1419                 foreach my $line (@entries) {
1420                         #'100644        blob    0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa        panic.c'
1421                         $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
1422                         my $t_mode = $1;
1423                         my $t_type = $2;
1424                         my $t_hash = $3;
1425                         my $t_name = validate_input(unquote($4));
1426                         if ($t_name eq $part) {
1427                                 if (!(@parts)) {
1428                                         return $t_hash;
1429                                 }
1430                                 if ($t_type eq "tree") {
1431                                         $tree = $t_hash;
1432                                 }
1433                                 last;
1434                         }
1435                 }
1436         }
1437 }
1438
1439 sub git_blob {
1440         if (!defined $hash && defined $file_name) {
1441                 my $base = $hash_base || git_read_head($project);
1442                 $hash = git_get_hash_by_path($base, $file_name, "blob") || die_error(undef, "Error lookup file.");
1443         }
1444         my $have_blame = git_get_project_config_bool ('blame');
1445         open my $fd, "-|", "$gitbin/git-cat-file blob $hash" or die_error(undef, "Open failed.");
1446         git_header_html();
1447         if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
1448                 print "<div class=\"page_nav\">\n" .
1449                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
1450                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
1451                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") .
1452                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base")}, "commit") .
1453                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash_base")}, "commitdiff") .
1454                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash_base")}, "tree") . "<br/>\n";
1455                 if (defined $file_name) {
1456                         if ($have_blame) {
1457                                 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;h=$hash;hb=$hash_base;f=$file_name")}, "blame") .  " | ";
1458                         }
1459                         print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$hash;f=$file_name")}, "plain") .
1460                         " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;hb=HEAD;f=$file_name")}, "head") . "<br/>\n";
1461                 } else {
1462                         print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$hash")}, "plain") . "<br/>\n";
1463                 }
1464                 print "</div>\n".
1465                        "<div>" .
1466                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base"), -class => "title"}, esc_html($co{'title'})) .
1467                       "</div>\n";
1468         } else {
1469                 print "<div class=\"page_nav\">\n" .
1470                       "<br/><br/></div>\n" .
1471                       "<div class=\"title\">$hash</div>\n";
1472         }
1473         if (defined $file_name) {
1474                 print "<div class=\"page_path\"><b>" . esc_html($file_name) . "</b></div>\n";
1475         }
1476         print "<div class=\"page_body\">\n";
1477         my $nr;
1478         while (my $line = <$fd>) {
1479                 chomp $line;
1480                 $nr++;
1481                 while ((my $pos = index($line, "\t")) != -1) {
1482                         if (my $count = (8 - ($pos % 8))) {
1483                                 my $spaces = ' ' x $count;
1484                                 $line =~ s/\t/$spaces/;
1485                         }
1486                 }
1487                 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n", $nr, $nr, $nr, esc_html($line);
1488         }
1489         close $fd or print "Reading blob failed.\n";
1490         print "</div>";
1491         git_footer_html();
1492 }
1493
1494 sub mimetype_guess_file {
1495         my $filename = shift;
1496         my $mimemap = shift;
1497         -r $mimemap or return undef;
1498
1499         my %mimemap;
1500         open(MIME, $mimemap) or return undef;
1501         while (<MIME>) {
1502                 my ($mime, $exts) = split(/\t+/);
1503                 my @exts = split(/\s+/, $exts);
1504                 foreach my $ext (@exts) {
1505                         $mimemap{$ext} = $mime;
1506                 }
1507         }
1508         close(MIME);
1509
1510         $filename =~ /\.(.*?)$/;
1511         return $mimemap{$1};
1512 }
1513
1514 sub mimetype_guess {
1515         my $filename = shift;
1516         my $mime;
1517         $filename =~ /\./ or return undef;
1518
1519         if ($mimetypes_file) {
1520                 my $file = $mimetypes_file;
1521                 $file =~ m#^/# or $file = "$projectroot/$path/$file";
1522                 $mime = mimetype_guess_file($filename, $file);
1523         }
1524         $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1525         return $mime;
1526 }
1527
1528 sub git_blob_plain_mimetype {
1529         my $fd = shift;
1530         my $filename = shift;
1531
1532         # just in case
1533         return $default_blob_plain_mimetype unless $fd;
1534
1535         if ($filename) {
1536                 my $mime = mimetype_guess($filename);
1537                 $mime and return $mime;
1538         }
1539
1540         if (-T $fd) {
1541                 return 'text/plain' .
1542                        ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1543         } elsif (! $filename) {
1544                 return 'application/octet-stream';
1545         } elsif ($filename =~ m/\.png$/i) {
1546                 return 'image/png';
1547         } elsif ($filename =~ m/\.gif$/i) {
1548                 return 'image/gif';
1549         } elsif ($filename =~ m/\.jpe?g$/i) {
1550                 return 'image/jpeg';
1551         } else {
1552                 return 'application/octet-stream';
1553         }
1554 }
1555
1556 sub git_blob_plain {
1557         open my $fd, "-|", "$gitbin/git-cat-file blob $hash" or return;
1558         my $type = git_blob_plain_mimetype($fd, $file_name);
1559
1560         # save as filename, even when no $file_name is given
1561         my $save_as = "$hash";
1562         if (defined $file_name) {
1563                 $save_as = $file_name;
1564         } elsif ($type =~ m/^text\//) {
1565                 $save_as .= '.txt';
1566         }
1567
1568         print $cgi->header(-type => "$type", '-content-disposition' => "inline; filename=\"$save_as\"");
1569         undef $/;
1570         binmode STDOUT, ':raw';
1571         print <$fd>;
1572         binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
1573         $/ = "\n";
1574         close $fd;
1575 }
1576
1577 sub git_tree {
1578         if (!defined $hash) {
1579                 $hash = git_read_head($project);
1580                 if (defined $file_name) {
1581                         my $base = $hash_base || $hash;
1582                         $hash = git_get_hash_by_path($base, $file_name, "tree");
1583                 }
1584                 if (!defined $hash_base) {
1585                         $hash_base = $hash;
1586                 }
1587         }
1588         $/ = "\0";
1589         open my $fd, "-|", "$gitbin/git-ls-tree -z $hash" or die_error(undef, "Open git-ls-tree failed.");
1590         chomp (my (@entries) = <$fd>);
1591         close $fd or die_error(undef, "Reading tree failed.");
1592         $/ = "\n";
1593
1594         my $refs = read_info_ref();
1595         my $ref = "";
1596         if (defined $refs->{$hash_base}) {
1597                 $ref = " <span class=\"tag\">" . esc_html($refs->{$hash_base}) . "</span>";
1598         }
1599         git_header_html();
1600         my $base_key = "";
1601         my $base = "";
1602         if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
1603                 $base_key = ";hb=$hash_base";
1604                 print "<div class=\"page_nav\">\n" .
1605                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
1606                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash_base")}, "shortlog") .
1607                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$hash_base")}, "log") .
1608                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base")}, "commit") .
1609                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash_base")}, "commitdiff") .
1610                       " | tree" .
1611                       "<br/><br/>\n" .
1612                       "</div>\n";
1613                 print "<div>\n" .
1614                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base"), -class => "title"}, esc_html($co{'title'}) . $ref) . "\n" .
1615                       "</div>\n";
1616         } else {
1617                 print "<div class=\"page_nav\">\n";
1618                 print "<br/><br/></div>\n";
1619                 print "<div class=\"title\">$hash</div>\n";
1620         }
1621         if (defined $file_name) {
1622                 $base = esc_html("$file_name/");
1623                 print "<div class=\"page_path\"><b>/" . esc_html($file_name) . "</b></div>\n";
1624         } else {
1625                 print "<div class=\"page_path\"><b>/</b></div>\n";
1626         }
1627         print "<div class=\"page_body\">\n";
1628         print "<table cellspacing=\"0\">\n";
1629         my $alternate = 0;
1630         foreach my $line (@entries) {
1631                 #'100644        blob    0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa        panic.c'
1632                 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
1633                 my $t_mode = $1;
1634                 my $t_type = $2;
1635                 my $t_hash = $3;
1636                 my $t_name = validate_input($4);
1637                 if ($alternate) {
1638                         print "<tr class=\"dark\">\n";
1639                 } else {
1640                         print "<tr class=\"light\">\n";
1641                 }
1642                 $alternate ^= 1;
1643                 print "<td style=\"font-family:monospace\">" . mode_str($t_mode) . "</td>\n";
1644                 if ($t_type eq "blob") {
1645                         print "<td class=\"list\">" .
1646                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$t_hash$base_key;f=$base$t_name"), -class => "list"}, esc_html($t_name)) .
1647                               "</td>\n" .
1648                               "<td class=\"link\">" .
1649                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$t_hash$base_key;f=$base$t_name")}, "blob") .
1650 #                             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;h=$t_hash$base_key;f=$base$t_name")}, "blame") .
1651                               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;h=$hash_base;f=$base$t_name")}, "history") .
1652                               "</td>\n";
1653                 } elsif ($t_type eq "tree") {
1654                         print "<td class=\"list\">" .
1655                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$t_hash$base_key;f=$base$t_name")}, esc_html($t_name)) .
1656                               "</td>\n" .
1657                               "<td class=\"link\">" .
1658                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$t_hash$base_key;f=$base$t_name")}, "tree") .
1659                               "</td>\n";
1660                 }
1661                 print "</tr>\n";
1662         }
1663         print "</table>\n" .
1664               "</div>";
1665         git_footer_html();
1666 }
1667
1668 sub git_rss {
1669         # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
1670         open my $fd, "-|", "$gitbin/git-rev-list --max-count=150 " . git_read_head($project) or die_error(undef, "Open failed.");
1671         my (@revlist) = map { chomp; $_ } <$fd>;
1672         close $fd or die_error(undef, "Reading rev-list failed.");
1673         print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
1674         print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".
1675               "<rss version=\"2.0\" xmlns:content=\"http://purl.org/rss/1.0/modules/content/\">\n";
1676         print "<channel>\n";
1677         print "<title>$project</title>\n".
1678               "<link>" . esc_html("$my_url?p=$project;a=summary") . "</link>\n".
1679               "<description>$project log</description>\n".
1680               "<language>en</language>\n";
1681
1682         for (my $i = 0; $i <= $#revlist; $i++) {
1683                 my $commit = $revlist[$i];
1684                 my %co = git_read_commit($commit);
1685                 # we read 150, we always show 30 and the ones more recent than 48 hours
1686                 if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
1687                         last;
1688                 }
1689                 my %cd = date_str($co{'committer_epoch'});
1690                 open $fd, "-|", "$gitbin/git-diff-tree -r $co{'parent'} $co{'id'}" or next;
1691                 my @difftree = map { chomp; $_ } <$fd>;
1692                 close $fd or next;
1693                 print "<item>\n" .
1694                       "<title>" .
1695                       sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
1696                       "</title>\n" .
1697                       "<author>" . esc_html($co{'author'}) . "</author>\n" .
1698                       "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
1699                       "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
1700                       "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
1701                       "<description>" . esc_html($co{'title'}) . "</description>\n" .
1702                       "<content:encoded>" .
1703                       "<![CDATA[\n";
1704                 my $comment = $co{'comment'};
1705                 foreach my $line (@$comment) {
1706                         $line = decode("utf8", $line, Encode::FB_DEFAULT);
1707                         print "$line<br/>\n";
1708                 }
1709                 print "<br/>\n";
1710                 foreach my $line (@difftree) {
1711                         if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
1712                                 next;
1713                         }
1714                         my $file = validate_input(unquote($7));
1715                         $file = decode("utf8", $file, Encode::FB_DEFAULT);
1716                         print "$file<br/>\n";
1717                 }
1718                 print "]]>\n" .
1719                       "</content:encoded>\n" .
1720                       "</item>\n";
1721         }
1722         print "</channel></rss>";
1723 }
1724
1725 sub git_opml {
1726         my @list = git_read_projects();
1727
1728         print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
1729         print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".
1730               "<opml version=\"1.0\">\n".
1731               "<head>".
1732               "  <title>Git OPML Export</title>\n".
1733               "</head>\n".
1734               "<body>\n".
1735               "<outline text=\"git RSS feeds\">\n";
1736
1737         foreach my $pr (@list) {
1738                 my %proj = %$pr;
1739                 my $head = git_read_head($proj{'path'});
1740                 if (!defined $head) {
1741                         next;
1742                 }
1743                 $ENV{'GIT_DIR'} = "$projectroot/$proj{'path'}";
1744                 my %co = git_read_commit($head);
1745                 if (!%co) {
1746                         next;
1747                 }
1748
1749                 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
1750                 my $rss =  "$my_url?p=$proj{'path'};a=rss";
1751                 my $html =  "$my_url?p=$proj{'path'};a=summary";
1752                 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
1753         }
1754         print "</outline>\n".
1755               "</body>\n".
1756               "</opml>\n";
1757 }
1758
1759 sub git_log {
1760         my $head = git_read_head($project);
1761         if (!defined $hash) {
1762                 $hash = $head;
1763         }
1764         if (!defined $page) {
1765                 $page = 0;
1766         }
1767         my $refs = read_info_ref();
1768         git_header_html();
1769         print "<div class=\"page_nav\">\n";
1770         print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
1771               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash")}, "shortlog") .
1772               " | log" .
1773               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash")}, "commit") .
1774               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash")}, "commitdiff") .
1775               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$hash;hb=$hash")}, "tree") . "<br/>\n";
1776
1777         my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
1778         open my $fd, "-|", "$gitbin/git-rev-list $limit $hash" or die_error(undef, "Open failed.");
1779         my (@revlist) = map { chomp; $_ } <$fd>;
1780         close $fd;
1781
1782         if ($hash ne $head || $page) {
1783                 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "HEAD");
1784         } else {
1785                 print "HEAD";
1786         }
1787         if ($page > 0) {
1788                 print " &sdot; " .
1789                 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$hash;pg=" . ($page-1)), -accesskey => "p", -title => "Alt-p"}, "prev");
1790         } else {
1791                 print " &sdot; prev";
1792         }
1793         if ($#revlist >= (100 * ($page+1)-1)) {
1794                 print " &sdot; " .
1795                 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$hash;pg=" . ($page+1)), -accesskey => "n", -title => "Alt-n"}, "next");
1796         } else {
1797                 print " &sdot; next";
1798         }
1799         print "<br/>\n" .
1800               "</div>\n";
1801         if (!@revlist) {
1802                 print "<div>\n" .
1803                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary"), -class => "title"}, "&nbsp;") .
1804                       "</div>\n";
1805                 my %co = git_read_commit($hash);
1806                 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
1807         }
1808         for (my $i = ($page * 100); $i <= $#revlist; $i++) {
1809                 my $commit = $revlist[$i];
1810                 my $ref = "";
1811                 if (defined $refs->{$commit}) {
1812                         $ref = " <span class=\"tag\">" . esc_html($refs->{$commit}) . "</span>";
1813                 }
1814                 my %co = git_read_commit($commit);
1815                 next if !%co;
1816                 my %ad = date_str($co{'author_epoch'});
1817                 print "<div>\n" .
1818                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "title"},
1819                       "<span class=\"age\">$co{'age_string'}</span>" . esc_html($co{'title'}) . $ref) . "\n";
1820                 print "</div>\n";
1821                 print "<div class=\"title_text\">\n" .
1822                       "<div class=\"log_link\">\n" .
1823                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") .
1824                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$commit")}, "commitdiff") .
1825                       "<br/>\n" .
1826                       "</div>\n" .
1827                       "<i>" . esc_html($co{'author_name'}) .  " [$ad{'rfc2822'}]</i><br/>\n" .
1828                       "</div>\n" .
1829                       "<div class=\"log_body\">\n";
1830                 my $comment = $co{'comment'};
1831                 my $empty = 0;
1832                 foreach my $line (@$comment) {
1833                         if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1834                                 next;
1835                         }
1836                         if ($line eq "") {
1837                                 if ($empty) {
1838                                         next;
1839                                 }
1840                                 $empty = 1;
1841                         } else {
1842                                 $empty = 0;
1843                         }
1844                         print format_log_line_html($line) . "<br/>\n";
1845                 }
1846                 if (!$empty) {
1847                         print "<br/>\n";
1848                 }
1849                 print "</div>\n";
1850         }
1851         git_footer_html();
1852 }
1853
1854 sub git_commit {
1855         my %co = git_read_commit($hash);
1856         if (!%co) {
1857                 die_error(undef, "Unknown commit object.");
1858         }
1859         my %ad = date_str($co{'author_epoch'}, $co{'author_tz'});
1860         my %cd = date_str($co{'committer_epoch'}, $co{'committer_tz'});
1861
1862         my @difftree;
1863         my $root = "";
1864         my $parent = $co{'parent'};
1865         if (!defined $parent) {
1866                 $root = " --root";
1867                 $parent = "";
1868         }
1869         open my $fd, "-|", "$gitbin/git-diff-tree -r -M $root $parent $hash" or die_error(undef, "Open failed.");
1870         @difftree = map { chomp; $_ } <$fd>;
1871         close $fd or die_error(undef, "Reading diff-tree failed.");
1872
1873         # non-textual hash id's can be cached
1874         my $expires;
1875         if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
1876                 $expires = "+1d";
1877         }
1878         my $refs = read_info_ref();
1879         my $ref = "";
1880         if (defined $refs->{$co{'id'}}) {
1881                 $ref = " <span class=\"tag\">" . esc_html($refs->{$co{'id'}}) . "</span>";
1882         }
1883         git_header_html(undef, $expires);
1884         print "<div class=\"page_nav\">\n" .
1885               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
1886               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash")}, "shortlog") .
1887               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$hash")}, "log") .
1888               " | commit";
1889         if (defined $co{'parent'}) {
1890                 print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash")}, "commitdiff");
1891         }
1892         print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash")}, "tree") . "\n" .
1893               "<br/><br/></div>\n";
1894         if (defined $co{'parent'}) {
1895                 print "<div>\n" .
1896                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash"), -class => "title"}, esc_html($co{'title'}) . $ref) . "\n" .
1897                       "</div>\n";
1898         } else {
1899                 print "<div>\n" .
1900                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash"), -class => "title"}, esc_html($co{'title'})) . "\n" .
1901                       "</div>\n";
1902         }
1903         print "<div class=\"title_text\">\n" .
1904               "<table cellspacing=\"0\">\n";
1905         print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
1906               "<tr>" .
1907               "<td></td><td> $ad{'rfc2822'}";
1908         if ($ad{'hour_local'} < 6) {
1909                 printf(" (<span style=\"color: #cc0000;\">%02d:%02d</span> %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1910         } else {
1911                 printf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1912         }
1913         print "</td>" .
1914               "</tr>\n";
1915         print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
1916         print "<tr><td></td><td> $cd{'rfc2822'}" . sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) . "</td></tr>\n";
1917         print "<tr><td>commit</td><td style=\"font-family:monospace\">$co{'id'}</td></tr>\n";
1918         print "<tr>" .
1919               "<td>tree</td>" .
1920               "<td style=\"font-family:monospace\">" .
1921               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash"), class => "list"}, $co{'tree'}) .
1922               "</td>" .
1923               "<td class=\"link\">" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash")}, "tree") .
1924               "</td>" .
1925               "</tr>\n";
1926         my $parents  = $co{'parents'};
1927         foreach my $par (@$parents) {
1928                 print "<tr>" .
1929                       "<td>parent</td>" .
1930                       "<td style=\"font-family:monospace\">" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$par"), class => "list"}, $par) . "</td>" .
1931                       "<td class=\"link\">" .
1932                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$par")}, "commit") .
1933                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash;hp=$par")}, "commitdiff") .
1934                       "</td>" .
1935                       "</tr>\n";
1936         }
1937         print "</table>". 
1938               "</div>\n";
1939         print "<div class=\"page_body\">\n";
1940         my $comment = $co{'comment'};
1941         my $empty = 0;
1942         my $signed = 0;
1943         foreach my $line (@$comment) {
1944                 # print only one empty line
1945                 if ($line eq "") {
1946                         if ($empty || $signed) {
1947                                 next;
1948                         }
1949                         $empty = 1;
1950                 } else {
1951                         $empty = 0;
1952                 }
1953                 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1954                         $signed = 1;
1955                         print "<span style=\"color: #888888\">" . esc_html($line) . "</span><br/>\n";
1956                 } else {
1957                         $signed = 0;
1958                         print format_log_line_html($line) . "<br/>\n";
1959                 }
1960         }
1961         print "</div>\n";
1962         print "<div class=\"list_head\">\n";
1963         if ($#difftree > 10) {
1964                 print(($#difftree + 1) . " files changed:\n");
1965         }
1966         print "</div>\n";
1967         print "<table cellspacing=\"0\">\n";
1968         my $alternate = 0;
1969         foreach my $line (@difftree) {
1970                 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M      ls-files.c'
1971                 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M      rev-tree.c'
1972                 if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
1973                         next;
1974                 }
1975                 my $from_mode = $1;
1976                 my $to_mode = $2;
1977                 my $from_id = $3;
1978                 my $to_id = $4;
1979                 my $status = $5;
1980                 my $similarity = $6;
1981                 my $file = validate_input(unquote($7));
1982                 if ($alternate) {
1983                         print "<tr class=\"dark\">\n";
1984                 } else {
1985                         print "<tr class=\"light\">\n";
1986                 }
1987                 $alternate ^= 1;
1988                 if ($status eq "A") {
1989                         my $mode_chng = "";
1990                         if (S_ISREG(oct $to_mode)) {
1991                                 $mode_chng = sprintf(" with mode: %04o", (oct $to_mode) & 0777);
1992                         }
1993                         print "<td>" .
1994                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file"), -class => "list"}, esc_html($file)) . "</td>\n" .
1995                               "<td><span style=\"color: #008000;\">[new " . file_type($to_mode) . "$mode_chng]</span></td>\n" .
1996                               "<td class=\"link\">" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file")}, "blob") . "</td>\n";
1997                 } elsif ($status eq "D") {
1998                         print "<td>" .
1999                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$hash;f=$file"), -class => "list"}, esc_html($file)) . "</td>\n" .
2000                               "<td><span style=\"color: #c00000;\">[deleted " . file_type($from_mode). "]</span></td>\n" .
2001                               "<td class=\"link\">" .
2002                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$hash;f=$file")}, "blob") .
2003                               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;h=$hash;f=$file")}, "history") .
2004                               "</td>\n"
2005                 } elsif ($status eq "M" || $status eq "T") {
2006                         my $mode_chnge = "";
2007                         if ($from_mode != $to_mode) {
2008                                 $mode_chnge = " <span style=\"color: #777777;\">[changed";
2009                                 if (((oct $from_mode) & S_IFMT) != ((oct $to_mode) & S_IFMT)) {
2010                                         $mode_chnge .= " from " . file_type($from_mode) . " to " . file_type($to_mode);
2011                                 }
2012                                 if (((oct $from_mode) & 0777) != ((oct $to_mode) & 0777)) {
2013                                         if (S_ISREG($from_mode) && S_ISREG($to_mode)) {
2014                                                 $mode_chnge .= sprintf(" mode: %04o->%04o", (oct $from_mode) & 0777, (oct $to_mode) & 0777);
2015                                         } elsif (S_ISREG($to_mode)) {
2016                                                 $mode_chnge .= sprintf(" mode: %04o", (oct $to_mode) & 0777);
2017                                         }
2018                                 }
2019                                 $mode_chnge .= "]</span>\n";
2020                         }
2021                         print "<td>";
2022                         if ($to_id ne $from_id) {
2023                                 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$file"), -class => "list"}, esc_html($file));
2024                         } else {
2025                                 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file"), -class => "list"}, esc_html($file));
2026                         }
2027                         print "</td>\n" .
2028                               "<td>$mode_chnge</td>\n" .
2029                               "<td class=\"link\">";
2030                         print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file")}, "blob");
2031                         if ($to_id ne $from_id) {
2032                                 print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$file")}, "diff");
2033                         }
2034                         print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;h=$hash;f=$file")}, "history") . "\n";
2035                         print "</td>\n";
2036                 } elsif ($status eq "R") {
2037                         my ($from_file, $to_file) = split "\t", $file;
2038                         my $mode_chng = "";
2039                         if ($from_mode != $to_mode) {
2040                                 $mode_chng = sprintf(", mode: %04o", (oct $to_mode) & 0777);
2041                         }
2042                         print "<td>" .
2043                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$to_file"), -class => "list"}, esc_html($to_file)) . "</td>\n" .
2044                               "<td><span style=\"color: #777777;\">[moved from " .
2045                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$hash;f=$from_file"), -class => "list"}, esc_html($from_file)) .
2046                               " with " . (int $similarity) . "% similarity$mode_chng]</span></td>\n" .
2047                               "<td class=\"link\">" .
2048                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$to_file")}, "blob");
2049                         if ($to_id ne $from_id) {
2050                                 print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$to_file")}, "diff");
2051                         }
2052                         print "</td>\n";
2053                 }
2054                 print "</tr>\n";
2055         }
2056         print "</table>\n";
2057         git_footer_html();
2058 }
2059
2060 sub git_blobdiff {
2061         mkdir($git_temp, 0700);
2062         git_header_html();
2063         if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
2064                 print "<div class=\"page_nav\">\n" .
2065                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
2066                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
2067                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") .
2068                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base")}, "commit") .
2069                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash_base")}, "commitdiff") .
2070                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash_base")}, "tree") .
2071                       "<br/>\n";
2072                 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff_plain;h=$hash;hp=$hash_parent")}, "plain") .
2073                       "</div>\n";
2074                 print "<div>\n" .
2075                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base"), -class => "title"}, esc_html($co{'title'})) . "\n" .
2076                       "</div>\n";
2077         } else {
2078                 print "<div class=\"page_nav\">\n" .
2079                       "<br/><br/></div>\n" .
2080                       "<div class=\"title\">$hash vs $hash_parent</div>\n";
2081         }
2082         if (defined $file_name) {
2083                 print "<div class=\"page_path\"><b>/" . esc_html($file_name) . "</b></div>\n";
2084         }
2085         print "<div class=\"page_body\">\n" .
2086               "<div class=\"diff_info\">blob:" .
2087               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$hash_parent;hb=$hash_base;f=$file_name")}, $hash_parent) .
2088               " -> blob:" .
2089               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$hash;hb=$hash_base;f=$file_name")}, $hash) .
2090               "</div>\n";
2091         git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash);
2092         print "</div>";
2093         git_footer_html();
2094 }
2095
2096 sub git_blobdiff_plain {
2097         mkdir($git_temp, 0700);
2098         print $cgi->header(-type => "text/plain", -charset => 'utf-8');
2099         git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash, "plain");
2100 }
2101
2102 sub git_commitdiff {
2103         mkdir($git_temp, 0700);
2104         my %co = git_read_commit($hash);
2105         if (!%co) {
2106                 die_error(undef, "Unknown commit object.");
2107         }
2108         if (!defined $hash_parent) {
2109                 $hash_parent = $co{'parent'};
2110         }
2111         open my $fd, "-|", "$gitbin/git-diff-tree -r $hash_parent $hash" or die_error(undef, "Open failed.");
2112         my (@difftree) = map { chomp; $_ } <$fd>;
2113         close $fd or die_error(undef, "Reading diff-tree failed.");
2114
2115         # non-textual hash id's can be cached
2116         my $expires;
2117         if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2118                 $expires = "+1d";
2119         }
2120         my $refs = read_info_ref();
2121         my $ref = "";
2122         if (defined $refs->{$co{'id'}}) {
2123                 $ref = " <span class=\"tag\">" . esc_html($refs->{$co{'id'}}) . "</span>";
2124         }
2125         git_header_html(undef, $expires);
2126         print "<div class=\"page_nav\">\n" .
2127               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
2128               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash")}, "shortlog") .
2129               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$hash")}, "log") .
2130               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash")}, "commit") .
2131               " | commitdiff" .
2132               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash")}, "tree") . "<br/>\n";
2133         print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff_plain;h=$hash;hp=$hash_parent")}, "plain") . "\n" .
2134               "</div>\n";
2135         print "<div>\n" .
2136               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash"), -class => "title"}, esc_html($co{'title'}) . $ref) . "\n" .
2137               "</div>\n";
2138         print "<div class=\"page_body\">\n";
2139         my $comment = $co{'comment'};
2140         my $empty = 0;
2141         my $signed = 0;
2142         my @log = @$comment;
2143         # remove first and empty lines after that
2144         shift @log;
2145         while (defined $log[0] && $log[0] eq "") {
2146                 shift @log;
2147         }
2148         foreach my $line (@log) {
2149                 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
2150                         next;
2151                 }
2152                 if ($line eq "") {
2153                         if ($empty) {
2154                                 next;
2155                         }
2156                         $empty = 1;
2157                 } else {
2158                         $empty = 0;
2159                 }
2160                 print format_log_line_html($line) . "<br/>\n";
2161         }
2162         print "<br/>\n";
2163         foreach my $line (@difftree) {
2164                 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M      ls-files.c'
2165                 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M      rev-tree.c'
2166                 $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/;
2167                 my $from_mode = $1;
2168                 my $to_mode = $2;
2169                 my $from_id = $3;
2170                 my $to_id = $4;
2171                 my $status = $5;
2172                 my $file = validate_input(unquote($6));
2173                 if ($status eq "A") {
2174                         print "<div class=\"diff_info\">" .  file_type($to_mode) . ":" .
2175                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file")}, $to_id) . "(new)" .
2176                               "</div>\n";
2177                         git_diff_print(undef, "/dev/null", $to_id, "b/$file");
2178                 } elsif ($status eq "D") {
2179                         print "<div class=\"diff_info\">" . file_type($from_mode) . ":" .
2180                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$hash;f=$file")}, $from_id) . "(deleted)" .
2181                               "</div>\n";
2182                         git_diff_print($from_id, "a/$file", undef, "/dev/null");
2183                 } elsif ($status eq "M") {
2184                         if ($from_id ne $to_id) {
2185                                 print "<div class=\"diff_info\">" .
2186                                       file_type($from_mode) . ":" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$hash;f=$file")}, $from_id) .
2187                                       " -> " .
2188                                       file_type($to_mode) . ":" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file")}, $to_id);
2189                                 print "</div>\n";
2190                                 git_diff_print($from_id, "a/$file",  $to_id, "b/$file");
2191                         }
2192                 }
2193         }
2194         print "<br/>\n" .
2195               "</div>";
2196         git_footer_html();
2197 }
2198
2199 sub git_commitdiff_plain {
2200         mkdir($git_temp, 0700);
2201         open my $fd, "-|", "$gitbin/git-diff-tree -r $hash_parent $hash" or die_error(undef, "Open failed.");
2202         my (@difftree) = map { chomp; $_ } <$fd>;
2203         close $fd or die_error(undef, "Reading diff-tree failed.");
2204
2205         # try to figure out the next tag after this commit
2206         my $tagname;
2207         my $refs = read_info_ref("tags");
2208         open $fd, "-|", "$gitbin/git-rev-list HEAD";
2209         chomp (my (@commits) = <$fd>);
2210         close $fd;
2211         foreach my $commit (@commits) {
2212                 if (defined $refs->{$commit}) {
2213                         $tagname = $refs->{$commit}
2214                 }
2215                 if ($commit eq $hash) {
2216                         last;
2217                 }
2218         }
2219
2220         print $cgi->header(-type => "text/plain", -charset => 'utf-8', '-content-disposition' => "inline; filename=\"git-$hash.patch\"");
2221         my %co = git_read_commit($hash);
2222         my %ad = date_str($co{'author_epoch'}, $co{'author_tz'});
2223         my $comment = $co{'comment'};
2224         print "From: $co{'author'}\n" .
2225               "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n".
2226               "Subject: $co{'title'}\n";
2227         if (defined $tagname) {
2228               print "X-Git-Tag: $tagname\n";
2229         }
2230         print "X-Git-Url: $my_url?p=$project;a=commitdiff;h=$hash\n" .
2231               "\n";
2232
2233         foreach my $line (@$comment) {;
2234                 print "$line\n";
2235         }
2236         print "---\n\n";
2237
2238         foreach my $line (@difftree) {
2239                 $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/;
2240                 my $from_id = $3;
2241                 my $to_id = $4;
2242                 my $status = $5;
2243                 my $file = $6;
2244                 if ($status eq "A") {
2245                         git_diff_print(undef, "/dev/null", $to_id, "b/$file", "plain");
2246                 } elsif ($status eq "D") {
2247                         git_diff_print($from_id, "a/$file", undef, "/dev/null", "plain");
2248                 } elsif ($status eq "M") {
2249                         git_diff_print($from_id, "a/$file",  $to_id, "b/$file", "plain");
2250                 }
2251         }
2252 }
2253
2254 sub git_history {
2255         if (!defined $hash) {
2256                 $hash = git_read_head($project);
2257         }
2258         my %co = git_read_commit($hash);
2259         if (!%co) {
2260                 die_error(undef, "Unknown commit object.");
2261         }
2262         my $refs = read_info_ref();
2263         git_header_html();
2264         print "<div class=\"page_nav\">\n" .
2265               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
2266               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
2267               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") .
2268               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash")}, "commit") .
2269               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash")}, "commitdiff") .
2270               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash")}, "tree") .
2271               "<br/><br/>\n" .
2272               "</div>\n";
2273         print "<div>\n" .
2274               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash"), -class => "title"}, esc_html($co{'title'})) . "\n" .
2275               "</div>\n";
2276         print "<div class=\"page_path\"><b>/" . esc_html($file_name) . "</b><br/></div>\n";
2277
2278         open my $fd, "-|", "$gitbin/git-rev-list $hash | $gitbin/git-diff-tree -r --stdin -- \'$file_name\'";
2279         my $commit;
2280         print "<table cellspacing=\"0\">\n";
2281         my $alternate = 0;
2282         while (my $line = <$fd>) {
2283                 if ($line =~ m/^([0-9a-fA-F]{40})/){
2284                         $commit = $1;
2285                         next;
2286                 }
2287                 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/ && (defined $commit)) {
2288                         my %co = git_read_commit($commit);
2289                         if (!%co) {
2290                                 next;
2291                         }
2292                         my $ref = "";
2293                         if (defined $refs->{$commit}) {
2294                                 $ref = " <span class=\"tag\">" . esc_html($refs->{$commit}) . "</span>";
2295                         }
2296                         if ($alternate) {
2297                                 print "<tr class=\"dark\">\n";
2298                         } else {
2299                                 print "<tr class=\"light\">\n";
2300                         }
2301                         $alternate ^= 1;
2302                         print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2303                               "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
2304                               "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "list"}, "<b>" .
2305                               esc_html(chop_str($co{'title'}, 50)) . "$ref</b>") . "</td>\n" .
2306                               "<td class=\"link\">" .
2307                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") .
2308                               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$commit")}, "commitdiff") .
2309                               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;hb=$commit;f=$file_name")}, "blob");
2310                         my $blob = git_get_hash_by_path($hash, $file_name);
2311                         my $blob_parent = git_get_hash_by_path($commit, $file_name);
2312                         if (defined $blob && defined $blob_parent && $blob ne $blob_parent) {
2313                                 print " | " .
2314                                 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff;h=$blob;hp=$blob_parent;hb=$commit;f=$file_name")},
2315                                 "diff to current");
2316                         }
2317                         print "</td>\n" .
2318                               "</tr>\n";
2319                         undef $commit;
2320                 }
2321         }
2322         print "</table>\n";
2323         close $fd;
2324         git_footer_html();
2325 }
2326
2327 sub git_search {
2328         if (!defined $searchtext) {
2329                 die_error("", "Text field empty.");
2330         }
2331         if (!defined $hash) {
2332                 $hash = git_read_head($project);
2333         }
2334         my %co = git_read_commit($hash);
2335         if (!%co) {
2336                 die_error(undef, "Unknown commit object.");
2337         }
2338         # pickaxe may take all resources of your box and run for several minutes
2339         # with every query - so decide by yourself how public you make this feature :)
2340         my $commit_search = 1;
2341         my $author_search = 0;
2342         my $committer_search = 0;
2343         my $pickaxe_search = 0;
2344         if ($searchtext =~ s/^author\\://i) {
2345                 $author_search = 1;
2346         } elsif ($searchtext =~ s/^committer\\://i) {
2347                 $committer_search = 1;
2348         } elsif ($searchtext =~ s/^pickaxe\\://i) {
2349                 $commit_search = 0;
2350                 $pickaxe_search = 1;
2351         }
2352         git_header_html();
2353         print "<div class=\"page_nav\">\n" .
2354               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary;h=$hash")}, "summary") .
2355               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
2356               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$hash")}, "log") .
2357               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash")}, "commit") .
2358               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash")}, "commitdiff") .
2359               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash")}, "tree") .
2360               "<br/><br/>\n" .
2361               "</div>\n";
2362
2363         print "<div>\n" .
2364               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash"), -class => "title"}, esc_html($co{'title'})) . "\n" .
2365               "</div>\n";
2366         print "<table cellspacing=\"0\">\n";
2367         my $alternate = 0;
2368         if ($commit_search) {
2369                 $/ = "\0";
2370                 open my $fd, "-|", "$gitbin/git-rev-list --header --parents $hash" or next;
2371                 while (my $commit_text = <$fd>) {
2372                         if (!grep m/$searchtext/i, $commit_text) {
2373                                 next;
2374                         }
2375                         if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
2376                                 next;
2377                         }
2378                         if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
2379                                 next;
2380                         }
2381                         my @commit_lines = split "\n", $commit_text;
2382                         my %co = git_read_commit(undef, \@commit_lines);
2383                         if (!%co) {
2384                                 next;
2385                         }
2386                         if ($alternate) {
2387                                 print "<tr class=\"dark\">\n";
2388                         } else {
2389                                 print "<tr class=\"light\">\n";
2390                         }
2391                         $alternate ^= 1;
2392                         print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2393                               "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
2394                               "<td>" .
2395                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$co{'id'}"), -class => "list"}, "<b>" . esc_html(chop_str($co{'title'}, 50)) . "</b><br/>");
2396                         my $comment = $co{'comment'};
2397                         foreach my $line (@$comment) {
2398                                 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
2399                                         my $lead = esc_html($1) || "";
2400                                         $lead = chop_str($lead, 30, 10);
2401                                         my $match = esc_html($2) || "";
2402                                         my $trail = esc_html($3) || "";
2403                                         $trail = chop_str($trail, 30, 10);
2404                                         my $text = "$lead<span style=\"color:#e00000\">$match</span>$trail";
2405                                         print chop_str($text, 80, 5) . "<br/>\n";
2406                                 }
2407                         }
2408                         print "</td>\n" .
2409                               "<td class=\"link\">" .
2410                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$co{'id'}")}, "commit") .
2411                               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$co{'id'}")}, "tree");
2412                         print "</td>\n" .
2413                               "</tr>\n";
2414                 }
2415                 close $fd;
2416         }
2417
2418         if ($pickaxe_search) {
2419                 $/ = "\n";
2420                 open my $fd, "-|", "$gitbin/git-rev-list $hash | $gitbin/git-diff-tree -r --stdin -S\'$searchtext\'";
2421                 undef %co;
2422                 my @files;
2423                 while (my $line = <$fd>) {
2424                         if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
2425                                 my %set;
2426                                 $set{'file'} = $6;
2427                                 $set{'from_id'} = $3;
2428                                 $set{'to_id'} = $4;
2429                                 $set{'id'} = $set{'to_id'};
2430                                 if ($set{'id'} =~ m/0{40}/) {
2431                                         $set{'id'} = $set{'from_id'};
2432                                 }
2433                                 if ($set{'id'} =~ m/0{40}/) {
2434                                         next;
2435                                 }
2436                                 push @files, \%set;
2437                         } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
2438                                 if (%co) {
2439                                         if ($alternate) {
2440                                                 print "<tr class=\"dark\">\n";
2441                                         } else {
2442                                                 print "<tr class=\"light\">\n";
2443                                         }
2444                                         $alternate ^= 1;
2445                                         print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2446                                               "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
2447                                               "<td>" .
2448                                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$co{'id'}"), -class => "list"}, "<b>" .
2449                                               esc_html(chop_str($co{'title'}, 50)) . "</b><br/>");
2450                                         while (my $setref = shift @files) {
2451                                                 my %set = %$setref;
2452                                                 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$set{'id'};hb=$co{'id'};f=$set{'file'}"), class => "list"},
2453                                                       "<span style=\"color:#e00000\">" . esc_html($set{'file'}) . "</span>") .
2454                                                       "<br/>\n";
2455                                         }
2456                                         print "</td>\n" .
2457                                               "<td class=\"link\">" .
2458                                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$co{'id'}")}, "commit") .
2459                                               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$co{'id'}")}, "tree");
2460                                         print "</td>\n" .
2461                                               "</tr>\n";
2462                                 }
2463                                 %co = git_read_commit($1);
2464                         }
2465                 }
2466                 close $fd;
2467         }
2468         print "</table>\n";
2469         git_footer_html();
2470 }
2471
2472 sub git_shortlog {
2473         my $head = git_read_head($project);
2474         if (!defined $hash) {
2475                 $hash = $head;
2476         }
2477         if (!defined $page) {
2478                 $page = 0;
2479         }
2480         my $refs = read_info_ref();
2481         git_header_html();
2482         print "<div class=\"page_nav\">\n" .
2483               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
2484               " | shortlog" .
2485               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$hash")}, "log") .
2486               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash")}, "commit") .
2487               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash")}, "commitdiff") .
2488               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$hash;hb=$hash")}, "tree") . "<br/>\n";
2489
2490         my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
2491         open my $fd, "-|", "$gitbin/git-rev-list $limit $hash" or die_error(undef, "Open failed.");
2492         my (@revlist) = map { chomp; $_ } <$fd>;
2493         close $fd;
2494
2495         if ($hash ne $head || $page) {
2496                 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "HEAD");
2497         } else {
2498                 print "HEAD";
2499         }
2500         if ($page > 0) {
2501                 print " &sdot; " .
2502                 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash;pg=" . ($page-1)), -accesskey => "p", -title => "Alt-p"}, "prev");
2503         } else {
2504                 print " &sdot; prev";
2505         }
2506         if ($#revlist >= (100 * ($page+1)-1)) {
2507                 print " &sdot; " .
2508                 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash;pg=" . ($page+1)), -accesskey => "n", -title => "Alt-n"}, "next");
2509         } else {
2510                 print " &sdot; next";
2511         }
2512         print "<br/>\n" .
2513               "</div>\n";
2514         print "<div>\n" .
2515               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary"), -class => "title"}, "&nbsp;") .
2516               "</div>\n";
2517         print "<table cellspacing=\"0\">\n";
2518         my $alternate = 0;
2519         for (my $i = ($page * 100); $i <= $#revlist; $i++) {
2520                 my $commit = $revlist[$i];
2521                 my $ref = "";
2522                 if (defined $refs->{$commit}) {
2523                         $ref = " <span class=\"tag\">" . esc_html($refs->{$commit}) . "</span>";
2524                 }
2525                 my %co = git_read_commit($commit);
2526                 my %ad = date_str($co{'author_epoch'});
2527                 if ($alternate) {
2528                         print "<tr class=\"dark\">\n";
2529                 } else {
2530                         print "<tr class=\"light\">\n";
2531                 }
2532                 $alternate ^= 1;
2533                 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2534                       "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
2535                       "<td>";
2536                 if (length($co{'title_short'}) < length($co{'title'})) {
2537                         print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "list", -title => "$co{'title'}"},
2538                               "<b>" . esc_html($co{'title_short'}) . "$ref</b>");
2539                 } else {
2540                         print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "list"},
2541                               "<b>" . esc_html($co{'title_short'}) . "$ref</b>");
2542                 }
2543                 print "</td>\n" .
2544                       "<td class=\"link\">" .
2545                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") .
2546                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$commit")}, "commitdiff") .
2547                       "</td>\n" .
2548                       "</tr>";
2549         }
2550         if ($#revlist >= (100 * ($page+1)-1)) {
2551                 print "<tr>\n" .
2552                       "<td>" .
2553                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash;pg=" . ($page+1)), -title => "Alt-n"}, "next") .
2554                       "</td>\n" .
2555                       "</tr>\n";
2556         }
2557         print "</table\n>";
2558         git_footer_html();
2559 }