Log ref changes made by resolve.
[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 mimetype_guess_file {
1462         my $filename = shift;
1463         my $mimemap = shift;
1464         -r $mimemap or return undef;
1465
1466         my %mimemap;
1467         open(MIME, $mimemap) or return undef;
1468         while (<MIME>) {
1469                 my ($mime, $exts) = split(/\t+/);
1470                 my @exts = split(/\s+/, $exts);
1471                 foreach my $ext (@exts) {
1472                         $mimemap{$ext} = $mime;
1473                 }
1474         }
1475         close(MIME);
1476
1477         $filename =~ /\.(.*?)$/;
1478         return $mimemap{$1};
1479 }
1480
1481 sub mimetype_guess {
1482         my $filename = shift;
1483         my $mime;
1484         $filename =~ /\./ or return undef;
1485
1486         if ($mimetypes_file) {
1487                 my $file = $mimetypes_file;
1488                 #$file =~ m#^/# or $file = "$projectroot/$path/$file";
1489                 $mime = mimetype_guess_file($filename, $file);
1490         }
1491         $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1492         return $mime;
1493 }
1494
1495 sub git_blob_plain_mimetype {
1496         my $fd = shift;
1497         my $filename = shift;
1498
1499         if ($filename) {
1500                 my $mime = mimetype_guess($filename);
1501                 $mime and return $mime;
1502         }
1503
1504         # just in case
1505         return $default_blob_plain_mimetype unless $fd;
1506
1507         if (-T $fd) {
1508                 return 'text/plain' .
1509                        ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1510         } elsif (! $filename) {
1511                 return 'application/octet-stream';
1512         } elsif ($filename =~ m/\.png$/i) {
1513                 return 'image/png';
1514         } elsif ($filename =~ m/\.gif$/i) {
1515                 return 'image/gif';
1516         } elsif ($filename =~ m/\.jpe?g$/i) {
1517                 return 'image/jpeg';
1518         } else {
1519                 return 'application/octet-stream';
1520         }
1521 }
1522
1523 sub git_blob_plain {
1524         my $type = shift;
1525         open my $fd, "-|", "$gitbin/git-cat-file blob $hash" or die_error("Couldn't cat $file_name, $hash");
1526
1527         $type ||= git_blob_plain_mimetype($fd, $file_name);
1528
1529         # save as filename, even when no $file_name is given
1530         my $save_as = "$hash";
1531         if (defined $file_name) {
1532                 $save_as = $file_name;
1533         } elsif ($type =~ m/^text\//) {
1534                 $save_as .= '.txt';
1535         }
1536
1537         print $cgi->header(-type => "$type", '-content-disposition' => "inline; filename=\"$save_as\"");
1538         undef $/;
1539         binmode STDOUT, ':raw';
1540         print <$fd>;
1541         binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
1542         $/ = "\n";
1543         close $fd;
1544 }
1545
1546 sub git_blob {
1547         if (!defined $hash && defined $file_name) {
1548                 my $base = $hash_base || git_read_head($project);
1549                 $hash = git_get_hash_by_path($base, $file_name, "blob") || die_error(undef, "Error lookup file.");
1550         }
1551         my $have_blame = git_get_project_config_bool ('blame');
1552         open my $fd, "-|", "$gitbin/git-cat-file blob $hash" or die_error(undef, "Open failed.");
1553         my $mimetype = git_blob_plain_mimetype($fd, $file_name);
1554         if ($mimetype !~ m/^text\//) {
1555                 close $fd;
1556                 return git_blob_plain($mimetype);
1557         }
1558         git_header_html();
1559         if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
1560                 print "<div class=\"page_nav\">\n" .
1561                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
1562                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
1563                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") .
1564                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base")}, "commit") .
1565                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash_base")}, "commitdiff") .
1566                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash_base")}, "tree") . "<br/>\n";
1567                 if (defined $file_name) {
1568                         if ($have_blame) {
1569                                 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;h=$hash;hb=$hash_base;f=$file_name")}, "blame") .  " | ";
1570                         }
1571                         print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$hash;f=$file_name")}, "plain") .
1572                         " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;hb=HEAD;f=$file_name")}, "head") . "<br/>\n";
1573                 } else {
1574                         print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$hash")}, "plain") . "<br/>\n";
1575                 }
1576                 print "</div>\n".
1577                        "<div>" .
1578                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base"), -class => "title"}, esc_html($co{'title'})) .
1579                       "</div>\n";
1580         } else {
1581                 print "<div class=\"page_nav\">\n" .
1582                       "<br/><br/></div>\n" .
1583                       "<div class=\"title\">$hash</div>\n";
1584         }
1585         if (defined $file_name) {
1586                 print "<div class=\"page_path\"><b>" . esc_html($file_name) . "</b></div>\n";
1587         }
1588         print "<div class=\"page_body\">\n";
1589         my $nr;
1590         while (my $line = <$fd>) {
1591                 chomp $line;
1592                 $nr++;
1593                 while ((my $pos = index($line, "\t")) != -1) {
1594                         if (my $count = (8 - ($pos % 8))) {
1595                                 my $spaces = ' ' x $count;
1596                                 $line =~ s/\t/$spaces/;
1597                         }
1598                 }
1599                 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n", $nr, $nr, $nr, esc_html($line);
1600         }
1601         close $fd or print "Reading blob failed.\n";
1602         print "</div>";
1603         git_footer_html();
1604 }
1605
1606 sub git_tree {
1607         if (!defined $hash) {
1608                 $hash = git_read_head($project);
1609                 if (defined $file_name) {
1610                         my $base = $hash_base || $hash;
1611                         $hash = git_get_hash_by_path($base, $file_name, "tree");
1612                 }
1613                 if (!defined $hash_base) {
1614                         $hash_base = $hash;
1615                 }
1616         }
1617         $/ = "\0";
1618         open my $fd, "-|", "$gitbin/git-ls-tree -z $hash" or die_error(undef, "Open git-ls-tree failed.");
1619         chomp (my (@entries) = <$fd>);
1620         close $fd or die_error(undef, "Reading tree failed.");
1621         $/ = "\n";
1622
1623         my $refs = read_info_ref();
1624         my $ref = "";
1625         if (defined $refs->{$hash_base}) {
1626                 $ref = " <span class=\"tag\">" . esc_html($refs->{$hash_base}) . "</span>";
1627         }
1628         git_header_html();
1629         my $base_key = "";
1630         my $base = "";
1631         if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
1632                 $base_key = ";hb=$hash_base";
1633                 print "<div class=\"page_nav\">\n" .
1634                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
1635                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash_base")}, "shortlog") .
1636                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$hash_base")}, "log") .
1637                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base")}, "commit") .
1638                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash_base")}, "commitdiff") .
1639                       " | tree" .
1640                       "<br/><br/>\n" .
1641                       "</div>\n";
1642                 print "<div>\n" .
1643                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base"), -class => "title"}, esc_html($co{'title'}) . $ref) . "\n" .
1644                       "</div>\n";
1645         } else {
1646                 print "<div class=\"page_nav\">\n";
1647                 print "<br/><br/></div>\n";
1648                 print "<div class=\"title\">$hash</div>\n";
1649         }
1650         if (defined $file_name) {
1651                 $base = esc_html("$file_name/");
1652                 print "<div class=\"page_path\"><b>/" . esc_html($file_name) . "</b></div>\n";
1653         } else {
1654                 print "<div class=\"page_path\"><b>/</b></div>\n";
1655         }
1656         print "<div class=\"page_body\">\n";
1657         print "<table cellspacing=\"0\">\n";
1658         my $alternate = 0;
1659         foreach my $line (@entries) {
1660                 #'100644        blob    0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa        panic.c'
1661                 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
1662                 my $t_mode = $1;
1663                 my $t_type = $2;
1664                 my $t_hash = $3;
1665                 my $t_name = validate_input($4);
1666                 if ($alternate) {
1667                         print "<tr class=\"dark\">\n";
1668                 } else {
1669                         print "<tr class=\"light\">\n";
1670                 }
1671                 $alternate ^= 1;
1672                 print "<td class=\"mode\">" . mode_str($t_mode) . "</td>\n";
1673                 if ($t_type eq "blob") {
1674                         print "<td class=\"list\">" .
1675                               $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)) .
1676                               "</td>\n" .
1677                               "<td class=\"link\">" .
1678                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$t_hash$base_key;f=$base$t_name")}, "blob") .
1679 #                             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;h=$t_hash$base_key;f=$base$t_name")}, "blame") .
1680                               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;h=$hash_base;f=$base$t_name")}, "history") .
1681                               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$t_hash;f=$base$t_name")}, "raw") .
1682                               "</td>\n";
1683                 } elsif ($t_type eq "tree") {
1684                         print "<td class=\"list\">" .
1685                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$t_hash$base_key;f=$base$t_name")}, esc_html($t_name)) .
1686                               "</td>\n" .
1687                               "<td class=\"link\">" .
1688                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$t_hash$base_key;f=$base$t_name")}, "tree") .
1689                               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;h=$hash_base;f=$base$t_name")}, "history") .
1690                               "</td>\n";
1691                 }
1692                 print "</tr>\n";
1693         }
1694         print "</table>\n" .
1695               "</div>";
1696         git_footer_html();
1697 }
1698
1699 sub git_rss {
1700         # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
1701         open my $fd, "-|", "$gitbin/git-rev-list --max-count=150 " . git_read_head($project) or die_error(undef, "Open failed.");
1702         my (@revlist) = map { chomp; $_ } <$fd>;
1703         close $fd or die_error(undef, "Reading rev-list failed.");
1704         print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
1705         print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".
1706               "<rss version=\"2.0\" xmlns:content=\"http://purl.org/rss/1.0/modules/content/\">\n";
1707         print "<channel>\n";
1708         print "<title>$project</title>\n".
1709               "<link>" . esc_html("$my_url?p=$project;a=summary") . "</link>\n".
1710               "<description>$project log</description>\n".
1711               "<language>en</language>\n";
1712
1713         for (my $i = 0; $i <= $#revlist; $i++) {
1714                 my $commit = $revlist[$i];
1715                 my %co = git_read_commit($commit);
1716                 # we read 150, we always show 30 and the ones more recent than 48 hours
1717                 if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
1718                         last;
1719                 }
1720                 my %cd = date_str($co{'committer_epoch'});
1721                 open $fd, "-|", "$gitbin/git-diff-tree -r $co{'parent'} $co{'id'}" or next;
1722                 my @difftree = map { chomp; $_ } <$fd>;
1723                 close $fd or next;
1724                 print "<item>\n" .
1725                       "<title>" .
1726                       sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
1727                       "</title>\n" .
1728                       "<author>" . esc_html($co{'author'}) . "</author>\n" .
1729                       "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
1730                       "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
1731                       "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
1732                       "<description>" . esc_html($co{'title'}) . "</description>\n" .
1733                       "<content:encoded>" .
1734                       "<![CDATA[\n";
1735                 my $comment = $co{'comment'};
1736                 foreach my $line (@$comment) {
1737                         $line = decode("utf8", $line, Encode::FB_DEFAULT);
1738                         print "$line<br/>\n";
1739                 }
1740                 print "<br/>\n";
1741                 foreach my $line (@difftree) {
1742                         if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
1743                                 next;
1744                         }
1745                         my $file = validate_input(unquote($7));
1746                         $file = decode("utf8", $file, Encode::FB_DEFAULT);
1747                         print "$file<br/>\n";
1748                 }
1749                 print "]]>\n" .
1750                       "</content:encoded>\n" .
1751                       "</item>\n";
1752         }
1753         print "</channel></rss>";
1754 }
1755
1756 sub git_opml {
1757         my @list = git_read_projects();
1758
1759         print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
1760         print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".
1761               "<opml version=\"1.0\">\n".
1762               "<head>".
1763               "  <title>Git OPML Export</title>\n".
1764               "</head>\n".
1765               "<body>\n".
1766               "<outline text=\"git RSS feeds\">\n";
1767
1768         foreach my $pr (@list) {
1769                 my %proj = %$pr;
1770                 my $head = git_read_head($proj{'path'});
1771                 if (!defined $head) {
1772                         next;
1773                 }
1774                 $ENV{'GIT_DIR'} = "$projectroot/$proj{'path'}";
1775                 my %co = git_read_commit($head);
1776                 if (!%co) {
1777                         next;
1778                 }
1779
1780                 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
1781                 my $rss  = "$my_url?p=$proj{'path'};a=rss";
1782                 my $html = "$my_url?p=$proj{'path'};a=summary";
1783                 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
1784         }
1785         print "</outline>\n".
1786               "</body>\n".
1787               "</opml>\n";
1788 }
1789
1790 sub git_log {
1791         my $head = git_read_head($project);
1792         if (!defined $hash) {
1793                 $hash = $head;
1794         }
1795         if (!defined $page) {
1796                 $page = 0;
1797         }
1798         my $refs = read_info_ref();
1799         git_header_html();
1800         print "<div class=\"page_nav\">\n";
1801         print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
1802               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash")}, "shortlog") .
1803               " | log" .
1804               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash")}, "commit") .
1805               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash")}, "commitdiff") .
1806               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$hash;hb=$hash")}, "tree") . "<br/>\n";
1807
1808         my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
1809         open my $fd, "-|", "$gitbin/git-rev-list $limit $hash" or die_error(undef, "Open failed.");
1810         my (@revlist) = map { chomp; $_ } <$fd>;
1811         close $fd;
1812
1813         if ($hash ne $head || $page) {
1814                 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "HEAD");
1815         } else {
1816                 print "HEAD";
1817         }
1818         if ($page > 0) {
1819                 print " &sdot; " .
1820                 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$hash;pg=" . ($page-1)), -accesskey => "p", -title => "Alt-p"}, "prev");
1821         } else {
1822                 print " &sdot; prev";
1823         }
1824         if ($#revlist >= (100 * ($page+1)-1)) {
1825                 print " &sdot; " .
1826                 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$hash;pg=" . ($page+1)), -accesskey => "n", -title => "Alt-n"}, "next");
1827         } else {
1828                 print " &sdot; next";
1829         }
1830         print "<br/>\n" .
1831               "</div>\n";
1832         if (!@revlist) {
1833                 print "<div>\n" .
1834                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary"), -class => "title"}, "&nbsp;") .
1835                       "</div>\n";
1836                 my %co = git_read_commit($hash);
1837                 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
1838         }
1839         for (my $i = ($page * 100); $i <= $#revlist; $i++) {
1840                 my $commit = $revlist[$i];
1841                 my $ref = "";
1842                 if (defined $refs->{$commit}) {
1843                         $ref = " <span class=\"tag\">" . esc_html($refs->{$commit}) . "</span>";
1844                 }
1845                 my %co = git_read_commit($commit);
1846                 next if !%co;
1847                 my %ad = date_str($co{'author_epoch'});
1848                 print "<div>\n" .
1849                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "title"},
1850                       "<span class=\"age\">$co{'age_string'}</span>" . esc_html($co{'title'}) . $ref) . "\n";
1851                 print "</div>\n";
1852                 print "<div class=\"title_text\">\n" .
1853                       "<div class=\"log_link\">\n" .
1854                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") .
1855                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$commit")}, "commitdiff") .
1856                       "<br/>\n" .
1857                       "</div>\n" .
1858                       "<i>" . esc_html($co{'author_name'}) .  " [$ad{'rfc2822'}]</i><br/>\n" .
1859                       "</div>\n" .
1860                       "<div class=\"log_body\">\n";
1861                 my $comment = $co{'comment'};
1862                 my $empty = 0;
1863                 foreach my $line (@$comment) {
1864                         if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1865                                 next;
1866                         }
1867                         if ($line eq "") {
1868                                 if ($empty) {
1869                                         next;
1870                                 }
1871                                 $empty = 1;
1872                         } else {
1873                                 $empty = 0;
1874                         }
1875                         print format_log_line_html($line) . "<br/>\n";
1876                 }
1877                 if (!$empty) {
1878                         print "<br/>\n";
1879                 }
1880                 print "</div>\n";
1881         }
1882         git_footer_html();
1883 }
1884
1885 sub git_commit {
1886         my %co = git_read_commit($hash);
1887         if (!%co) {
1888                 die_error(undef, "Unknown commit object.");
1889         }
1890         my %ad = date_str($co{'author_epoch'}, $co{'author_tz'});
1891         my %cd = date_str($co{'committer_epoch'}, $co{'committer_tz'});
1892
1893         my @difftree;
1894         my $root = "";
1895         my $parent = $co{'parent'};
1896         if (!defined $parent) {
1897                 $root = " --root";
1898                 $parent = "";
1899         }
1900         open my $fd, "-|", "$gitbin/git-diff-tree -r -M $root $parent $hash" or die_error(undef, "Open failed.");
1901         @difftree = map { chomp; $_ } <$fd>;
1902         close $fd or die_error(undef, "Reading diff-tree failed.");
1903
1904         # non-textual hash id's can be cached
1905         my $expires;
1906         if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
1907                 $expires = "+1d";
1908         }
1909         my $refs = read_info_ref();
1910         my $ref = "";
1911         if (defined $refs->{$co{'id'}}) {
1912                 $ref = " <span class=\"tag\">" . esc_html($refs->{$co{'id'}}) . "</span>";
1913         }
1914         git_header_html(undef, $expires);
1915         print "<div class=\"page_nav\">\n" .
1916               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
1917               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash")}, "shortlog") .
1918               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$hash")}, "log") .
1919               " | commit";
1920         if (defined $co{'parent'}) {
1921                 print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash")}, "commitdiff");
1922         }
1923         print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash")}, "tree") . "\n" .
1924               "<br/><br/></div>\n";
1925         if (defined $co{'parent'}) {
1926                 print "<div>\n" .
1927                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash"), -class => "title"}, esc_html($co{'title'}) . $ref) . "\n" .
1928                       "</div>\n";
1929         } else {
1930                 print "<div>\n" .
1931                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash"), -class => "title"}, esc_html($co{'title'})) . "\n" .
1932                       "</div>\n";
1933         }
1934         print "<div class=\"title_text\">\n" .
1935               "<table cellspacing=\"0\">\n";
1936         print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
1937               "<tr>" .
1938               "<td></td><td> $ad{'rfc2822'}";
1939         if ($ad{'hour_local'} < 6) {
1940                 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1941         } else {
1942                 printf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1943         }
1944         print "</td>" .
1945               "</tr>\n";
1946         print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
1947         print "<tr><td></td><td> $cd{'rfc2822'}" . sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) . "</td></tr>\n";
1948         print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
1949         print "<tr>" .
1950               "<td>tree</td>" .
1951               "<td class=\"sha1\">" .
1952               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash"), class => "list"}, $co{'tree'}) .
1953               "</td>" .
1954               "<td class=\"link\">" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash")}, "tree") .
1955               "</td>" .
1956               "</tr>\n";
1957         my $parents = $co{'parents'};
1958         foreach my $par (@$parents) {
1959                 print "<tr>" .
1960                       "<td>parent</td>" .
1961                       "<td class=\"sha1\">" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$par"), class => "list"}, $par) . "</td>" .
1962                       "<td class=\"link\">" .
1963                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$par")}, "commit") .
1964                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash;hp=$par")}, "commitdiff") .
1965                       "</td>" .
1966                       "</tr>\n";
1967         }
1968         print "</table>".
1969               "</div>\n";
1970         print "<div class=\"page_body\">\n";
1971         my $comment = $co{'comment'};
1972         my $empty = 0;
1973         my $signed = 0;
1974         foreach my $line (@$comment) {
1975                 # print only one empty line
1976                 if ($line eq "") {
1977                         if ($empty || $signed) {
1978                                 next;
1979                         }
1980                         $empty = 1;
1981                 } else {
1982                         $empty = 0;
1983                 }
1984                 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1985                         $signed = 1;
1986                         print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
1987                 } else {
1988                         $signed = 0;
1989                         print format_log_line_html($line) . "<br/>\n";
1990                 }
1991         }
1992         print "</div>\n";
1993         print "<div class=\"list_head\">\n";
1994         if ($#difftree > 10) {
1995                 print(($#difftree + 1) . " files changed:\n");
1996         }
1997         print "</div>\n";
1998         print "<table class=\"diff_tree\">\n";
1999         my $alternate = 0;
2000         foreach my $line (@difftree) {
2001                 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M      ls-files.c'
2002                 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M      rev-tree.c'
2003                 if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
2004                         next;
2005                 }
2006                 my $from_mode = $1;
2007                 my $to_mode = $2;
2008                 my $from_id = $3;
2009                 my $to_id = $4;
2010                 my $status = $5;
2011                 my $similarity = $6;
2012                 my $file = validate_input(unquote($7));
2013                 if ($alternate) {
2014                         print "<tr class=\"dark\">\n";
2015                 } else {
2016                         print "<tr class=\"light\">\n";
2017                 }
2018                 $alternate ^= 1;
2019                 if ($status eq "A") {
2020                         my $mode_chng = "";
2021                         if (S_ISREG(oct $to_mode)) {
2022                                 $mode_chng = sprintf(" with mode: %04o", (oct $to_mode) & 0777);
2023                         }
2024                         print "<td>" .
2025                               $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" .
2026                               "<td><span class=\"file_status new\">[new " . file_type($to_mode) . "$mode_chng]</span></td>\n" .
2027                               "<td class=\"link\">" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file")}, "blob") . "</td>\n";
2028                 } elsif ($status eq "D") {
2029                         print "<td>" .
2030                               $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" .
2031                               "<td><span class=\"file_status deleted\">[deleted " . file_type($from_mode). "]</span></td>\n" .
2032                               "<td class=\"link\">" .
2033                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$hash;f=$file")}, "blob") .
2034                               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;h=$hash;f=$file")}, "history") .
2035                               "</td>\n"
2036                 } elsif ($status eq "M" || $status eq "T") {
2037                         my $mode_chnge = "";
2038                         if ($from_mode != $to_mode) {
2039                                 $mode_chnge = " <span class=\"file_status mode_chnge\">[changed";
2040                                 if (((oct $from_mode) & S_IFMT) != ((oct $to_mode) & S_IFMT)) {
2041                                         $mode_chnge .= " from " . file_type($from_mode) . " to " . file_type($to_mode);
2042                                 }
2043                                 if (((oct $from_mode) & 0777) != ((oct $to_mode) & 0777)) {
2044                                         if (S_ISREG($from_mode) && S_ISREG($to_mode)) {
2045                                                 $mode_chnge .= sprintf(" mode: %04o->%04o", (oct $from_mode) & 0777, (oct $to_mode) & 0777);
2046                                         } elsif (S_ISREG($to_mode)) {
2047                                                 $mode_chnge .= sprintf(" mode: %04o", (oct $to_mode) & 0777);
2048                                         }
2049                                 }
2050                                 $mode_chnge .= "]</span>\n";
2051                         }
2052                         print "<td>";
2053                         if ($to_id ne $from_id) {
2054                                 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));
2055                         } else {
2056                                 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file"), -class => "list"}, esc_html($file));
2057                         }
2058                         print "</td>\n" .
2059                               "<td>$mode_chnge</td>\n" .
2060                               "<td class=\"link\">";
2061                         print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file")}, "blob");
2062                         if ($to_id ne $from_id) {
2063                                 print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$file")}, "diff");
2064                         }
2065                         print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;h=$hash;f=$file")}, "history") . "\n";
2066                         print "</td>\n";
2067                 } elsif ($status eq "R") {
2068                         my ($from_file, $to_file) = split "\t", $file;
2069                         my $mode_chng = "";
2070                         if ($from_mode != $to_mode) {
2071                                 $mode_chng = sprintf(", mode: %04o", (oct $to_mode) & 0777);
2072                         }
2073                         print "<td>" .
2074                               $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" .
2075                               "<td><span class=\"file_status moved\">[moved from " .
2076                               $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)) .
2077                               " with " . (int $similarity) . "% similarity$mode_chng]</span></td>\n" .
2078                               "<td class=\"link\">" .
2079                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$to_file")}, "blob");
2080                         if ($to_id ne $from_id) {
2081                                 print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$to_file")}, "diff");
2082                         }
2083                         print "</td>\n";
2084                 }
2085                 print "</tr>\n";
2086         }
2087         print "</table>\n";
2088         git_footer_html();
2089 }
2090
2091 sub git_blobdiff {
2092         mkdir($git_temp, 0700);
2093         git_header_html();
2094         if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
2095                 print "<div class=\"page_nav\">\n" .
2096                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
2097                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
2098                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") .
2099                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base")}, "commit") .
2100                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash_base")}, "commitdiff") .
2101                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash_base")}, "tree") .
2102                       "<br/>\n";
2103                 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff_plain;h=$hash;hp=$hash_parent")}, "plain") .
2104                       "</div>\n";
2105                 print "<div>\n" .
2106                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base"), -class => "title"}, esc_html($co{'title'})) . "\n" .
2107                       "</div>\n";
2108         } else {
2109                 print "<div class=\"page_nav\">\n" .
2110                       "<br/><br/></div>\n" .
2111                       "<div class=\"title\">$hash vs $hash_parent</div>\n";
2112         }
2113         if (defined $file_name) {
2114                 print "<div class=\"page_path\"><b>/" . esc_html($file_name) . "</b></div>\n";
2115         }
2116         print "<div class=\"page_body\">\n" .
2117               "<div class=\"diff_info\">blob:" .
2118               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$hash_parent;hb=$hash_base;f=$file_name")}, $hash_parent) .
2119               " -> blob:" .
2120               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$hash;hb=$hash_base;f=$file_name")}, $hash) .
2121               "</div>\n";
2122         git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash);
2123         print "</div>";
2124         git_footer_html();
2125 }
2126
2127 sub git_blobdiff_plain {
2128         mkdir($git_temp, 0700);
2129         print $cgi->header(-type => "text/plain", -charset => 'utf-8');
2130         git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash, "plain");
2131 }
2132
2133 sub git_commitdiff {
2134         mkdir($git_temp, 0700);
2135         my %co = git_read_commit($hash);
2136         if (!%co) {
2137                 die_error(undef, "Unknown commit object.");
2138         }
2139         if (!defined $hash_parent) {
2140                 $hash_parent = $co{'parent'};
2141         }
2142         open my $fd, "-|", "$gitbin/git-diff-tree -r $hash_parent $hash" or die_error(undef, "Open failed.");
2143         my (@difftree) = map { chomp; $_ } <$fd>;
2144         close $fd or die_error(undef, "Reading diff-tree failed.");
2145
2146         # non-textual hash id's can be cached
2147         my $expires;
2148         if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2149                 $expires = "+1d";
2150         }
2151         my $refs = read_info_ref();
2152         my $ref = "";
2153         if (defined $refs->{$co{'id'}}) {
2154                 $ref = " <span class=\"tag\">" . esc_html($refs->{$co{'id'}}) . "</span>";
2155         }
2156         git_header_html(undef, $expires);
2157         print "<div class=\"page_nav\">\n" .
2158               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
2159               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash")}, "shortlog") .
2160               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$hash")}, "log") .
2161               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash")}, "commit") .
2162               " | commitdiff" .
2163               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash")}, "tree") . "<br/>\n";
2164         print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff_plain;h=$hash;hp=$hash_parent")}, "plain") . "\n" .
2165               "</div>\n";
2166         print "<div>\n" .
2167               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash"), -class => "title"}, esc_html($co{'title'}) . $ref) . "\n" .
2168               "</div>\n";
2169         print "<div class=\"page_body\">\n";
2170         my $comment = $co{'comment'};
2171         my $empty = 0;
2172         my $signed = 0;
2173         my @log = @$comment;
2174         # remove first and empty lines after that
2175         shift @log;
2176         while (defined $log[0] && $log[0] eq "") {
2177                 shift @log;
2178         }
2179         foreach my $line (@log) {
2180                 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
2181                         next;
2182                 }
2183                 if ($line eq "") {
2184                         if ($empty) {
2185                                 next;
2186                         }
2187                         $empty = 1;
2188                 } else {
2189                         $empty = 0;
2190                 }
2191                 print format_log_line_html($line) . "<br/>\n";
2192         }
2193         print "<br/>\n";
2194         foreach my $line (@difftree) {
2195                 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M      ls-files.c'
2196                 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M      rev-tree.c'
2197                 $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/;
2198                 my $from_mode = $1;
2199                 my $to_mode = $2;
2200                 my $from_id = $3;
2201                 my $to_id = $4;
2202                 my $status = $5;
2203                 my $file = validate_input(unquote($6));
2204                 if ($status eq "A") {
2205                         print "<div class=\"diff_info\">" .  file_type($to_mode) . ":" .
2206                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file")}, $to_id) . "(new)" .
2207                               "</div>\n";
2208                         git_diff_print(undef, "/dev/null", $to_id, "b/$file");
2209                 } elsif ($status eq "D") {
2210                         print "<div class=\"diff_info\">" . file_type($from_mode) . ":" .
2211                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$hash;f=$file")}, $from_id) . "(deleted)" .
2212                               "</div>\n";
2213                         git_diff_print($from_id, "a/$file", undef, "/dev/null");
2214                 } elsif ($status eq "M") {
2215                         if ($from_id ne $to_id) {
2216                                 print "<div class=\"diff_info\">" .
2217                                       file_type($from_mode) . ":" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$hash;f=$file")}, $from_id) .
2218                                       " -> " .
2219                                       file_type($to_mode) . ":" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file")}, $to_id);
2220                                 print "</div>\n";
2221                                 git_diff_print($from_id, "a/$file",  $to_id, "b/$file");
2222                         }
2223                 }
2224         }
2225         print "<br/>\n" .
2226               "</div>";
2227         git_footer_html();
2228 }
2229
2230 sub git_commitdiff_plain {
2231         mkdir($git_temp, 0700);
2232         open my $fd, "-|", "$gitbin/git-diff-tree -r $hash_parent $hash" or die_error(undef, "Open failed.");
2233         my (@difftree) = map { chomp; $_ } <$fd>;
2234         close $fd or die_error(undef, "Reading diff-tree failed.");
2235
2236         # try to figure out the next tag after this commit
2237         my $tagname;
2238         my $refs = read_info_ref("tags");
2239         open $fd, "-|", "$gitbin/git-rev-list HEAD";
2240         chomp (my (@commits) = <$fd>);
2241         close $fd;
2242         foreach my $commit (@commits) {
2243                 if (defined $refs->{$commit}) {
2244                         $tagname = $refs->{$commit}
2245                 }
2246                 if ($commit eq $hash) {
2247                         last;
2248                 }
2249         }
2250
2251         print $cgi->header(-type => "text/plain", -charset => 'utf-8', '-content-disposition' => "inline; filename=\"git-$hash.patch\"");
2252         my %co = git_read_commit($hash);
2253         my %ad = date_str($co{'author_epoch'}, $co{'author_tz'});
2254         my $comment = $co{'comment'};
2255         print "From: $co{'author'}\n" .
2256               "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n".
2257               "Subject: $co{'title'}\n";
2258         if (defined $tagname) {
2259               print "X-Git-Tag: $tagname\n";
2260         }
2261         print "X-Git-Url: $my_url?p=$project;a=commitdiff;h=$hash\n" .
2262               "\n";
2263
2264         foreach my $line (@$comment) {;
2265                 print "$line\n";
2266         }
2267         print "---\n\n";
2268
2269         foreach my $line (@difftree) {
2270                 $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/;
2271                 my $from_id = $3;
2272                 my $to_id = $4;
2273                 my $status = $5;
2274                 my $file = $6;
2275                 if ($status eq "A") {
2276                         git_diff_print(undef, "/dev/null", $to_id, "b/$file", "plain");
2277                 } elsif ($status eq "D") {
2278                         git_diff_print($from_id, "a/$file", undef, "/dev/null", "plain");
2279                 } elsif ($status eq "M") {
2280                         git_diff_print($from_id, "a/$file",  $to_id, "b/$file", "plain");
2281                 }
2282         }
2283 }
2284
2285 sub git_history {
2286         if (!defined $hash) {
2287                 $hash = git_read_head($project);
2288         }
2289         my %co = git_read_commit($hash);
2290         if (!%co) {
2291                 die_error(undef, "Unknown commit object.");
2292         }
2293         my $refs = read_info_ref();
2294         git_header_html();
2295         print "<div class=\"page_nav\">\n" .
2296               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
2297               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
2298               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") .
2299               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash")}, "commit") .
2300               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash")}, "commitdiff") .
2301               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash")}, "tree") .
2302               "<br/><br/>\n" .
2303               "</div>\n";
2304         print "<div>\n" .
2305               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash"), -class => "title"}, esc_html($co{'title'})) . "\n" .
2306               "</div>\n";
2307         print "<div class=\"page_path\"><b>/" . esc_html($file_name) . "</b><br/></div>\n";
2308
2309         open my $fd, "-|",
2310                 "$gitbin/git-rev-list --full-history $hash -- \'$file_name\'";
2311         print "<table cellspacing=\"0\">\n";
2312         my $alternate = 0;
2313         while (my $line = <$fd>) {
2314                 if ($line =~ m/^([0-9a-fA-F]{40})/){
2315                         my $commit = $1;
2316                         my %co = git_read_commit($commit);
2317                         if (!%co) {
2318                                 next;
2319                         }
2320                         my $ref = "";
2321                         if (defined $refs->{$commit}) {
2322                                 $ref = " <span class=\"tag\">" . esc_html($refs->{$commit}) . "</span>";
2323                         }
2324                         if ($alternate) {
2325                                 print "<tr class=\"dark\">\n";
2326                         } else {
2327                                 print "<tr class=\"light\">\n";
2328                         }
2329                         $alternate ^= 1;
2330                         print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2331                               "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
2332                               "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "list"}, "<b>" .
2333                               esc_html(chop_str($co{'title'}, 50)) . "$ref</b>") . "</td>\n" .
2334                               "<td class=\"link\">" .
2335                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") .
2336                               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$commit")}, "commitdiff") .
2337                               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;hb=$commit;f=$file_name")}, "blob");
2338                         my $blob = git_get_hash_by_path($hash, $file_name);
2339                         my $blob_parent = git_get_hash_by_path($commit, $file_name);
2340                         if (defined $blob && defined $blob_parent && $blob ne $blob_parent) {
2341                                 print " | " .
2342                                 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff;h=$blob;hp=$blob_parent;hb=$commit;f=$file_name")},
2343                                 "diff to current");
2344                         }
2345                         print "</td>\n" .
2346                               "</tr>\n";
2347                 }
2348         }
2349         print "</table>\n";
2350         close $fd;
2351         git_footer_html();
2352 }
2353
2354 sub git_search {
2355         if (!defined $searchtext) {
2356                 die_error("", "Text field empty.");
2357         }
2358         if (!defined $hash) {
2359                 $hash = git_read_head($project);
2360         }
2361         my %co = git_read_commit($hash);
2362         if (!%co) {
2363                 die_error(undef, "Unknown commit object.");
2364         }
2365         # pickaxe may take all resources of your box and run for several minutes
2366         # with every query - so decide by yourself how public you make this feature :)
2367         my $commit_search = 1;
2368         my $author_search = 0;
2369         my $committer_search = 0;
2370         my $pickaxe_search = 0;
2371         if ($searchtext =~ s/^author\\://i) {
2372                 $author_search = 1;
2373         } elsif ($searchtext =~ s/^committer\\://i) {
2374                 $committer_search = 1;
2375         } elsif ($searchtext =~ s/^pickaxe\\://i) {
2376                 $commit_search = 0;
2377                 $pickaxe_search = 1;
2378         }
2379         git_header_html();
2380         print "<div class=\"page_nav\">\n" .
2381               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary;h=$hash")}, "summary") .
2382               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
2383               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$hash")}, "log") .
2384               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash")}, "commit") .
2385               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash")}, "commitdiff") .
2386               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash")}, "tree") .
2387               "<br/><br/>\n" .
2388               "</div>\n";
2389
2390         print "<div>\n" .
2391               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash"), -class => "title"}, esc_html($co{'title'})) . "\n" .
2392               "</div>\n";
2393         print "<table cellspacing=\"0\">\n";
2394         my $alternate = 0;
2395         if ($commit_search) {
2396                 $/ = "\0";
2397                 open my $fd, "-|", "$gitbin/git-rev-list --header --parents $hash" or next;
2398                 while (my $commit_text = <$fd>) {
2399                         if (!grep m/$searchtext/i, $commit_text) {
2400                                 next;
2401                         }
2402                         if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
2403                                 next;
2404                         }
2405                         if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
2406                                 next;
2407                         }
2408                         my @commit_lines = split "\n", $commit_text;
2409                         my %co = git_read_commit(undef, \@commit_lines);
2410                         if (!%co) {
2411                                 next;
2412                         }
2413                         if ($alternate) {
2414                                 print "<tr class=\"dark\">\n";
2415                         } else {
2416                                 print "<tr class=\"light\">\n";
2417                         }
2418                         $alternate ^= 1;
2419                         print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2420                               "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
2421                               "<td>" .
2422                               $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/>");
2423                         my $comment = $co{'comment'};
2424                         foreach my $line (@$comment) {
2425                                 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
2426                                         my $lead = esc_html($1) || "";
2427                                         $lead = chop_str($lead, 30, 10);
2428                                         my $match = esc_html($2) || "";
2429                                         my $trail = esc_html($3) || "";
2430                                         $trail = chop_str($trail, 30, 10);
2431                                         my $text = "$lead<span class=\"match\">$match</span>$trail";
2432                                         print chop_str($text, 80, 5) . "<br/>\n";
2433                                 }
2434                         }
2435                         print "</td>\n" .
2436                               "<td class=\"link\">" .
2437                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$co{'id'}")}, "commit") .
2438                               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$co{'id'}")}, "tree");
2439                         print "</td>\n" .
2440                               "</tr>\n";
2441                 }
2442                 close $fd;
2443         }
2444
2445         if ($pickaxe_search) {
2446                 $/ = "\n";
2447                 open my $fd, "-|", "$gitbin/git-rev-list $hash | $gitbin/git-diff-tree -r --stdin -S\'$searchtext\'";
2448                 undef %co;
2449                 my @files;
2450                 while (my $line = <$fd>) {
2451                         if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
2452                                 my %set;
2453                                 $set{'file'} = $6;
2454                                 $set{'from_id'} = $3;
2455                                 $set{'to_id'} = $4;
2456                                 $set{'id'} = $set{'to_id'};
2457                                 if ($set{'id'} =~ m/0{40}/) {
2458                                         $set{'id'} = $set{'from_id'};
2459                                 }
2460                                 if ($set{'id'} =~ m/0{40}/) {
2461                                         next;
2462                                 }
2463                                 push @files, \%set;
2464                         } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
2465                                 if (%co) {
2466                                         if ($alternate) {
2467                                                 print "<tr class=\"dark\">\n";
2468                                         } else {
2469                                                 print "<tr class=\"light\">\n";
2470                                         }
2471                                         $alternate ^= 1;
2472                                         print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2473                                               "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
2474                                               "<td>" .
2475                                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$co{'id'}"), -class => "list"}, "<b>" .
2476                                               esc_html(chop_str($co{'title'}, 50)) . "</b><br/>");
2477                                         while (my $setref = shift @files) {
2478                                                 my %set = %$setref;
2479                                                 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$set{'id'};hb=$co{'id'};f=$set{'file'}"), class => "list"},
2480                                                       "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
2481                                                       "<br/>\n";
2482                                         }
2483                                         print "</td>\n" .
2484                                               "<td class=\"link\">" .
2485                                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$co{'id'}")}, "commit") .
2486                                               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$co{'id'}")}, "tree");
2487                                         print "</td>\n" .
2488                                               "</tr>\n";
2489                                 }
2490                                 %co = git_read_commit($1);
2491                         }
2492                 }
2493                 close $fd;
2494         }
2495         print "</table>\n";
2496         git_footer_html();
2497 }
2498
2499 sub git_shortlog {
2500         my $head = git_read_head($project);
2501         if (!defined $hash) {
2502                 $hash = $head;
2503         }
2504         if (!defined $page) {
2505                 $page = 0;
2506         }
2507         my $refs = read_info_ref();
2508         git_header_html();
2509         print "<div class=\"page_nav\">\n" .
2510               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
2511               " | shortlog" .
2512               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$hash")}, "log") .
2513               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash")}, "commit") .
2514               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash")}, "commitdiff") .
2515               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$hash;hb=$hash")}, "tree") . "<br/>\n";
2516
2517         my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
2518         open my $fd, "-|", "$gitbin/git-rev-list $limit $hash" or die_error(undef, "Open failed.");
2519         my (@revlist) = map { chomp; $_ } <$fd>;
2520         close $fd;
2521
2522         if ($hash ne $head || $page) {
2523                 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "HEAD");
2524         } else {
2525                 print "HEAD";
2526         }
2527         if ($page > 0) {
2528                 print " &sdot; " .
2529                 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash;pg=" . ($page-1)), -accesskey => "p", -title => "Alt-p"}, "prev");
2530         } else {
2531                 print " &sdot; prev";
2532         }
2533         if ($#revlist >= (100 * ($page+1)-1)) {
2534                 print " &sdot; " .
2535                 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash;pg=" . ($page+1)), -accesskey => "n", -title => "Alt-n"}, "next");
2536         } else {
2537                 print " &sdot; next";
2538         }
2539         print "<br/>\n" .
2540               "</div>\n";
2541         print "<div>\n" .
2542               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary"), -class => "title"}, "&nbsp;") .
2543               "</div>\n";
2544         print "<table cellspacing=\"0\">\n";
2545         my $alternate = 0;
2546         for (my $i = ($page * 100); $i <= $#revlist; $i++) {
2547                 my $commit = $revlist[$i];
2548                 my $ref = "";
2549                 if (defined $refs->{$commit}) {
2550                         $ref = " <span class=\"tag\">" . esc_html($refs->{$commit}) . "</span>";
2551                 }
2552                 my %co = git_read_commit($commit);
2553                 my %ad = date_str($co{'author_epoch'});
2554                 if ($alternate) {
2555                         print "<tr class=\"dark\">\n";
2556                 } else {
2557                         print "<tr class=\"light\">\n";
2558                 }
2559                 $alternate ^= 1;
2560                 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2561                       "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
2562                       "<td>";
2563                 if (length($co{'title_short'}) < length($co{'title'})) {
2564                         print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "list", -title => "$co{'title'}"},
2565                               "<b>" . esc_html($co{'title_short'}) . "$ref</b>");
2566                 } else {
2567                         print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "list"},
2568                               "<b>" . esc_html($co{'title_short'}) . "$ref</b>");
2569                 }
2570                 print "</td>\n" .
2571                       "<td class=\"link\">" .
2572                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") .
2573                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$commit")}, "commitdiff") .
2574                       "</td>\n" .
2575                       "</tr>";
2576         }
2577         if ($#revlist >= (100 * ($page+1)-1)) {
2578                 print "<tr>\n" .
2579                       "<td>" .
2580                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash;pg=" . ($page+1)), -title => "Alt-n"}, "next") .
2581                       "</td>\n" .
2582                       "</tr>\n";
2583         }
2584         print "</table\n>";
2585         git_footer_html();
2586 }