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