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