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