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