Add <author> and <guid> to RSS feed
[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 =           "240";
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, a.header { 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) || (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 => "header", -href => "$my_uri?o=project"}, "Project") . "</th>\n";
803         }
804         if (defined($order) && ($order eq "descr")) {
805                 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
806                 print "<th>Description</th>\n";
807         } else {
808                 print "<th>" . $cgi->a({-class => "header", -href => "$my_uri?o=descr"}, "Description") . "</th>\n";
809         }
810         if (defined($order) && ($order eq "owner")) {
811                 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
812                 print "<th>Owner</th>\n";
813         } else {
814                 print "<th>" . $cgi->a({-class => "header", -href => "$my_uri?o=owner"}, "Owner") . "</th>\n";
815         }
816         if (defined($order) && ($order eq "age")) {
817                 @projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;
818                 print "<th>Last Change</th>\n";
819         } else {
820                 print "<th>" . $cgi->a({-class => "header", -href => "$my_uri?o=age"}, "Last Change") . "</th>\n";
821         }
822         print "<th></th>\n" .
823               "</tr>\n";
824         my $alternate = 0;
825         foreach my $pr (@projects) {
826                 if ($alternate) {
827                         print "<tr class=\"dark\">\n";
828                 } else {
829                         print "<tr class=\"light\">\n";
830                 }
831                 $alternate ^= 1;
832                 print "<td>" . $cgi->a({-href => "$my_uri?p=$pr->{'path'};a=summary", -class => "list"}, escapeHTML($pr->{'path'})) . "</td>\n" .
833                       "<td>$pr->{'descr'}</td>\n" .
834                       "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
835                 my $colored_age;
836                 if ($pr->{'commit'}{'age'} < 60*60*2) {
837                         $colored_age = "<span style =\"color: #009900;\"><b><i>$pr->{'commit'}{'age_string'}</i></b></span>";
838                 } elsif ($pr->{'commit'}{'age'} < 60*60*24*2) {
839                         $colored_age = "<span style =\"color: #009900;\"><i>$pr->{'commit'}{'age_string'}</i></span>";
840                 } else {
841                         $colored_age = "<i>$pr->{'commit'}{'age_string'}</i>";
842                 }
843                 print "<td>$colored_age</td>\n" .
844                       "<td class=\"link\">" .
845                       $cgi->a({-href => "$my_uri?p=$pr->{'path'};a=summary"}, "summary") .
846                       " | " . $cgi->a({-href => "$my_uri?p=$pr->{'path'};a=shortlog"}, "shortlog") .
847                       " | " . $cgi->a({-href => "$my_uri?p=$pr->{'path'};a=log"}, "log") .
848                       "</td>\n" .
849                       "</tr>\n";
850         }
851         print "</table>\n";
852         git_footer_html();
853 }
854
855 sub git_read_refs {
856         my $ref_dir = shift;
857         my @reflist;
858
859         my @refs;
860         opendir my $dh, "$projectroot/$project/$ref_dir";
861         while (my $dir = readdir($dh)) {
862                 if ($dir =~ m/^\./) {
863                         next;
864                 }
865                 if (-d "$projectroot/$project/$ref_dir/$dir") {
866                         opendir my $dh2, "$projectroot/$project/$ref_dir/$dir";
867                         my @subdirs = grep !m/^\./, readdir $dh2;
868                         closedir($dh2);
869                         foreach my $subdir (@subdirs) {
870                                 push @refs, "$dir/$subdir"
871                         }
872                         next;
873                 }
874                 push @refs, $dir;
875         }
876         closedir($dh);
877         foreach my $ref_file (@refs) {
878                 my $ref_id = git_read_hash("$project/$ref_dir/$ref_file");
879                 my $type = git_get_type($ref_id) || next;
880                 my %ref_item;
881                 my %co;
882                 $ref_item{'type'} = $type;
883                 $ref_item{'id'} = $ref_id;
884                 $ref_item{'epoch'} = 0;
885                 $ref_item{'age'} = "unknown";
886                 if ($type eq "tag") {
887                         my %tag = git_read_tag($ref_id);
888                         $ref_item{'comment'} = $tag{'comment'};
889                         if ($tag{'type'} eq "commit") {
890                                 %co = git_read_commit($tag{'object'});
891                                 $ref_item{'epoch'} = $co{'committer_epoch'};
892                                 $ref_item{'age'} = $co{'age_string'};
893                         } elsif (defined($tag{'epoch'})) {
894                                 my $age = time - $tag{'epoch'};
895                                 $ref_item{'epoch'} = $tag{'epoch'};
896                                 $ref_item{'age'} = age_string($age);
897                         }
898                         $ref_item{'reftype'} = $tag{'type'};
899                         $ref_item{'name'} = $tag{'name'};
900                         $ref_item{'refid'} = $tag{'object'};
901                 } elsif ($type eq "commit"){
902                         %co = git_read_commit($ref_id);
903                         $ref_item{'reftype'} = "commit";
904                         $ref_item{'name'} = $ref_file;
905                         $ref_item{'title'} = $co{'title'};
906                         $ref_item{'refid'} = $ref_id;
907                         $ref_item{'epoch'} = $co{'committer_epoch'};
908                         $ref_item{'age'} = $co{'age_string'};
909                 }
910
911                 push @reflist, \%ref_item;
912         }
913         # sort tags by age
914         @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
915         return \@reflist;
916 }
917
918 sub git_summary {
919         my $descr = git_read_description($project) || "none";
920         my $head = git_read_hash("$project/HEAD");
921         my %co = git_read_commit($head);
922         my %cd = date_str($co{'committer_epoch'}, $co{'committer_tz'});
923
924         my $owner;
925         if (-f $projects_list) {
926                 open (my $fd , $projects_list);
927                 while (my $line = <$fd>) {
928                         chomp $line;
929                         my ($pr, $ow) = split ' ', $line;
930                         $pr = unescape($pr);
931                         $ow = unescape($ow);
932                         if ($pr eq $project) {
933                                 $owner = $ow;
934                                 last;
935                         }
936                 }
937                 close $fd;
938         }
939         if (!defined $owner) {
940                 $owner = get_file_owner("$projectroot/$project");
941         }
942
943         git_header_html();
944         print "<div class=\"page_nav\">\n" .
945               "summary".
946               " | " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog"}, "shortlog") .
947               " | " . $cgi->a({-href => "$my_uri?p=$project;a=log"}, "log") .
948               " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$head"}, "commit") .
949               " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$head"}, "commitdiff") .
950               " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree"}, "tree") .
951               "<br/><br/>\n" .
952               "</div>\n";
953         print "<div class=\"title\">&nbsp;</div>\n";
954         print "<table cellspacing=\"0\">\n" .
955               "<tr><td>description</td><td>" . escapeHTML($descr) . "</td></tr>\n" .
956               "<tr><td>owner</td><td>$owner</td></tr>\n" .
957               "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n" .
958               "</table>\n";
959         open my $fd, "-|", "$gitbin/git-rev-list --max-count=17 " . git_read_hash("$project/HEAD") or die_error(undef, "Open failed.");
960         my (@revlist) = map { chomp; $_ } <$fd>;
961         close $fd;
962         print "<div>\n" .
963               $cgi->a({-href => "$my_uri?p=$project;a=shortlog", -class => "title"}, "shortlog") .
964               "</div>\n";
965         my $i = 16;
966         print "<table cellspacing=\"0\">\n";
967         my $alternate = 0;
968         foreach my $commit (@revlist) {
969                 my %co = git_read_commit($commit);
970                 my %ad = date_str($co{'author_epoch'});
971                 if ($alternate) {
972                         print "<tr class=\"dark\">\n";
973                 } else {
974                         print "<tr class=\"light\">\n";
975                 }
976                 $alternate ^= 1;
977                 if ($i-- > 0) {
978                         print "<td><i>$co{'age_string'}</i></td>\n" .
979                               "<td><i>" . escapeHTML(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
980                               "<td>" .
981                               $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit", -class => "list"},
982                               "<b>" . escapeHTML($co{'title_short'}) . "</b>") .
983                               "</td>\n" .
984                               "<td class=\"link\">" .
985                               $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit"}, "commit") .
986                               " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$commit"}, "commitdiff") .
987                               "</td>\n" .
988                               "</tr>";
989                 } else {
990                         print "<td>" . $cgi->a({-href => "$my_uri?p=$project;a=shortlog"}, "...") . "</td>\n" .
991                         "</tr>";
992                         last;
993                 }
994         }
995         print "</table\n>";
996
997         my $taglist = git_read_refs("refs/tags");
998         if (defined @$taglist) {
999                 print "<div>\n" .
1000                       $cgi->a({-href => "$my_uri?p=$project;a=tags", -class => "title"}, "tags") .
1001                       "</div>\n";
1002                 my $i = 16;
1003                 print "<table cellspacing=\"0\">\n";
1004                 my $alternate = 0;
1005                 foreach my $entry (@$taglist) {
1006                         my %tag = %$entry;
1007                         my $comment_lines = $tag{'comment'};
1008                         my $comment = shift @$comment_lines;
1009                         if (defined($comment)) {
1010                                 $comment = chop_str($comment, 30, 5);
1011                         }
1012                         if ($alternate) {
1013                                 print "<tr class=\"dark\">\n";
1014                         } else {
1015                                 print "<tr class=\"light\">\n";
1016                         }
1017                         $alternate ^= 1;
1018                         if ($i-- > 0) {
1019                                 print "<td><i>$tag{'age'}</i></td>\n" .
1020                                       "<td>" .
1021                                       $cgi->a({-href => "$my_uri?p=$project;a=$tag{'reftype'};h=$tag{'refid'}", -class => "list"},
1022                                       "<b>" . escapeHTML($tag{'name'}) . "</b>") .
1023                                       "</td>\n" .
1024                                       "<td>";
1025                                 if (defined($comment)) {
1026                                       print $cgi->a({-class => "list", -href => "$my_uri?p=$project;a=tag;h=$tag{'id'}"}, $comment);
1027                                 }
1028                                 print "</td>\n" .
1029                                       "<td class=\"link\">";
1030                                 if ($tag{'type'} eq "tag") {
1031                                       print $cgi->a({-href => "$my_uri?p=$project;a=tag;h=$tag{'id'}"}, "tag") . " | ";
1032                                 }
1033                                 print $cgi->a({-href => "$my_uri?p=$project;a=$tag{'reftype'};h=$tag{'refid'}"}, $tag{'reftype'});
1034                                 if ($tag{'reftype'} eq "commit") {
1035                                       print " | " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog;h=$tag{'name'}"}, "shortlog") .
1036                                             " | " . $cgi->a({-href => "$my_uri?p=$project;a=log;h=$tag{'refid'}"}, "log");
1037                                 }
1038                                 print "</td>\n" .
1039                                       "</tr>";
1040                         } else {
1041                                 print "<td>" . $cgi->a({-href => "$my_uri?p=$project;a=tags"}, "...") . "</td>\n" .
1042                                 "</tr>";
1043                                 last;
1044                         }
1045                 }
1046                 print "</table\n>";
1047         }
1048
1049         my $branchlist = git_read_refs("refs/heads");
1050         if (defined @$branchlist) {
1051                 print "<div>\n" .
1052                       $cgi->a({-href => "$my_uri?p=$project;a=branches", -class => "title"}, "branches") .
1053                       "</div>\n";
1054                 my $i = 16;
1055                 print "<table cellspacing=\"0\">\n";
1056                 my $alternate = 0;
1057                 foreach my $entry (@$branchlist) {
1058                         my %tag = %$entry;
1059                         if ($alternate) {
1060                                 print "<tr class=\"dark\">\n";
1061                         } else {
1062                                 print "<tr class=\"light\">\n";
1063                         }
1064                         $alternate ^= 1;
1065                         if ($i-- > 0) {
1066                                 print "<td><i>$tag{'age'}</i></td>\n" .
1067                                       "<td>" .
1068                                       $cgi->a({-href => "$my_uri?p=$project;a=shortlog;h=$tag{'name'}", -class => "list"},
1069                                       "<b>" . escapeHTML($tag{'name'}) . "</b>") .
1070                                       "</td>\n" .
1071                                       "<td class=\"link\">" .
1072                                       $cgi->a({-href => "$my_uri?p=$project;a=shortlog;h=$tag{'name'}"}, "shortlog") .
1073                                       " | " . $cgi->a({-href => "$my_uri?p=$project;a=log;h=$tag{'name'}"}, "log") .
1074                                       "</td>\n" .
1075                                       "</tr>";
1076                         } else {
1077                                 print "<td>" . $cgi->a({-href => "$my_uri?p=$project;a=branches"}, "...") . "</td>\n" .
1078                                 "</tr>";
1079                                 last;
1080                         }
1081                 }
1082                 print "</table\n>";
1083         }
1084         git_footer_html();
1085 }
1086
1087 sub git_tag {
1088         my $head = git_read_hash("$project/HEAD");
1089         git_header_html();
1090         print "<div class=\"page_nav\">\n" .
1091               $cgi->a({-href => "$my_uri?p=$project;a=summary"}, "summary") .
1092               " | " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog"}, "shortlog") .
1093               " | " . $cgi->a({-href => "$my_uri?p=$project;a=log"}, "log") .
1094               " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$head"}, "commit") .
1095               " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$head"}, "commitdiff") .
1096               " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;hb=$head"}, "tree") . "<br/>\n" .
1097               "<br/>\n" .
1098               "</div>\n";
1099         my %tag = git_read_tag($hash);
1100         print "<div>\n" .
1101               $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash", -class => "title"}, escapeHTML($tag{'name'})) . "\n" .
1102               "</div>\n";
1103         print "<div class=\"title_text\">\n" .
1104               "<table cellspacing=\"0\">\n" .
1105               "<tr>\n" .
1106               "<td>object</td>\n" .
1107               "<td>" . $cgi->a({-class => "list", -href => "$my_uri?p=$project;a=$tag{'type'};h=$tag{'object'}"}, $tag{'object'}) . "</td>\n" .
1108               "<td class=\"link\">" . $cgi->a({-href => "$my_uri?p=$project;a=$tag{'type'};h=$tag{'object'}"}, $tag{'type'}) . "</td>\n" .
1109               "</tr>\n";
1110         if (defined($tag{'author'})) {
1111                 my %ad = date_str($tag{'epoch'}, $tag{'tz'});
1112                 print "<tr><td>author</td><td>" . escapeHTML($tag{'author'}) . "</td></tr>\n";
1113                 print "<tr><td></td><td>" . $ad{'rfc2822'} . sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) . "</td></tr>\n";
1114         }
1115         print "</table>\n\n" .
1116               "</div>\n";
1117         print "<div class=\"page_body\">";
1118         my $comment = $tag{'comment'};
1119         foreach my $line (@$comment) {
1120                 print escapeHTML($line) . "<br/>\n";
1121         }
1122         print "</div>\n";
1123         git_footer_html();
1124 }
1125
1126 sub git_tags {
1127         my $head = git_read_hash("$project/HEAD");
1128         git_header_html();
1129         print "<div class=\"page_nav\">\n" .
1130               $cgi->a({-href => "$my_uri?p=$project;a=summary"}, "summary") .
1131               " | " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog"}, "shortlog") .
1132               " | " . $cgi->a({-href => "$my_uri?p=$project;a=log"}, "log") .
1133               " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$head"}, "commit") .
1134               " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$head"}, "commitdiff") .
1135               " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;hb=$head"}, "tree") . "<br/>\n" .
1136               "<br/>\n" .
1137               "</div>\n";
1138         my $taglist = git_read_refs("refs/tags");
1139         print "<div>\n" .
1140               $cgi->a({-href => "$my_uri?p=$project;a=summary", -class => "title"}, "&nbsp;") .
1141               "</div>\n";
1142         print "<table cellspacing=\"0\">\n";
1143         my $alternate = 0;
1144         if (defined @$taglist) {
1145                 foreach my $entry (@$taglist) {
1146                         my %tag = %$entry;
1147                         my $comment_lines = $tag{'comment'};
1148                         my $comment = shift @$comment_lines;
1149                         if (defined($comment)) {
1150                                 $comment = chop_str($comment, 30, 5);
1151                         }
1152                         if ($alternate) {
1153                                 print "<tr class=\"dark\">\n";
1154                         } else {
1155                                 print "<tr class=\"light\">\n";
1156                         }
1157                         $alternate ^= 1;
1158                         print "<td><i>$tag{'age'}</i></td>\n" .
1159                               "<td>" .
1160                               $cgi->a({-href => "$my_uri?p=$project;a=$tag{'reftype'};h=$tag{'refid'}", -class => "list"},
1161                               "<b>" . escapeHTML($tag{'name'}) . "</b>") .
1162                               "</td>\n" .
1163                               "<td>";
1164                         if (defined($comment)) {
1165                               print $cgi->a({-class => "list", -href => "$my_uri?p=$project;a=tag;h=$tag{'id'}"}, $comment);
1166                         }
1167                         print "</td>\n" .
1168                               "<td class=\"link\">";
1169                         if ($tag{'type'} eq "tag") {
1170                               print $cgi->a({-href => "$my_uri?p=$project;a=tag;h=$tag{'id'}"}, "tag") . " | ";
1171                         }
1172                         print $cgi->a({-href => "$my_uri?p=$project;a=$tag{'reftype'};h=$tag{'refid'}"}, $tag{'reftype'});
1173                         if ($tag{'reftype'} eq "commit") {
1174                               print " | " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog;h=$tag{'name'}"}, "shortlog") .
1175                                     " | " . $cgi->a({-href => "$my_uri?p=$project;a=log;h=$tag{'refid'}"}, "log");
1176                         }
1177                         print "</td>\n" .
1178                               "</tr>";
1179                 }
1180         }
1181         print "</table\n>";
1182         git_footer_html();
1183 }
1184
1185 sub git_branches {
1186         my $head = git_read_hash("$project/HEAD");
1187         git_header_html();
1188         print "<div class=\"page_nav\">\n" .
1189               $cgi->a({-href => "$my_uri?p=$project;a=summary"}, "summary") .
1190               " | " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog"}, "shortlog") .
1191               " | " . $cgi->a({-href => "$my_uri?p=$project;a=log"}, "log") .
1192               " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$head"}, "commit") .
1193               " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$head"}, "commitdiff") .
1194               " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;hb=$head"}, "tree") . "<br/>\n" .
1195               "<br/>\n" .
1196               "</div>\n";
1197         my $taglist = git_read_refs("refs/heads");
1198         print "<div>\n" .
1199               $cgi->a({-href => "$my_uri?p=$project;a=summary", -class => "title"}, "&nbsp;") .
1200               "</div>\n";
1201         print "<table cellspacing=\"0\">\n";
1202         my $alternate = 0;
1203         if (defined @$taglist) {
1204                 foreach my $entry (@$taglist) {
1205                         my %tag = %$entry;
1206                         if ($alternate) {
1207                                 print "<tr class=\"dark\">\n";
1208                         } else {
1209                                 print "<tr class=\"light\">\n";
1210                         }
1211                         $alternate ^= 1;
1212                         print "<td><i>$tag{'age'}</i></td>\n" .
1213                               "<td>" .
1214                               $cgi->a({-href => "$my_uri?p=$project;a=shortlog;h=$tag{'name'}", -class => "list"}, "<b>" . escapeHTML($tag{'name'}) . "</b>") .
1215                               "</td>\n" .
1216                               "<td class=\"link\">" .
1217                               $cgi->a({-href => "$my_uri?p=$project;a=shortlog;h=$tag{'name'}"}, "shortlog") .
1218                               " | " . $cgi->a({-href => "$my_uri?p=$project;a=log;h=$tag{'name'}"}, "log") .
1219                               "</td>\n" .
1220                               "</tr>";
1221                 }
1222         }
1223         print "</table\n>";
1224         git_footer_html();
1225 }
1226
1227 sub git_get_hash_by_path {
1228         my $base = shift;
1229         my $path = shift || return undef;
1230
1231         my $tree = $base;
1232         my @parts = split '/', $path;
1233         while (my $part = shift @parts) {
1234                 open my $fd, "-|", "$gitbin/git-ls-tree $tree" or die_error(undef, "Open git-ls-tree failed.");
1235                 my (@entries) = map { chomp; $_ } <$fd>;
1236                 close $fd or return undef;
1237                 foreach my $line (@entries) {
1238                         #'100644        blob    0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa        panic.c'
1239                         $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
1240                         my $t_mode = $1;
1241                         my $t_type = $2;
1242                         my $t_hash = $3;
1243                         my $t_name = $4;
1244                         if ($t_name eq $part) {
1245                                 if (!(@parts)) {
1246                                         return $t_hash;
1247                                 }
1248                                 if ($t_type eq "tree") {
1249                                         $tree = $t_hash;
1250                                 }
1251                                 last;
1252                         }
1253                 }
1254         }
1255 }
1256
1257 sub git_blob {
1258         if (!defined $hash && defined $file_name) {
1259                 my $base = $hash_base || git_read_hash("$project/HEAD");
1260                 $hash = git_get_hash_by_path($base, $file_name, "blob");
1261         }
1262         open my $fd, "-|", "$gitbin/git-cat-file blob $hash" or die_error(undef, "Open failed.");
1263         my $base = $file_name || "";
1264         git_header_html();
1265         if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
1266                 print "<div class=\"page_nav\">\n" .
1267                       $cgi->a({-href => "$my_uri?p=$project;a=summary"}, "summary") .
1268                       " | " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog"}, "shortlog") .
1269                       " | " . $cgi->a({-href => "$my_uri?p=$project;a=log"}, "log") .
1270                       " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash_base"}, "commit") .
1271                       " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash_base"}, "commitdiff") .
1272                       " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash_base"}, "tree") . "<br/>\n";
1273                 print $cgi->a({-href => "$my_uri?p=$project;a=blob_plain;h=$hash"}, "plain") . "<br/>\n" .
1274                       "</div>\n";
1275                 print "<div>" .
1276                       $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash_base", -class => "title"}, escapeHTML($co{'title'})) .
1277                       "</div>\n";
1278         } else {
1279                 print "<div class=\"page_nav\">\n" .
1280                       "<br/><br/></div>\n" .
1281                       "<div class=\"title\">$hash</div>\n";
1282         }
1283         if (defined $file_name) {
1284                 print "<div class=\"page_path\"><b>$file_name</b></div>\n";
1285         }
1286         print "<div class=\"page_body\">\n";
1287         my $nr;
1288         while (my $line = <$fd>) {
1289                 chomp $line;
1290                 $nr++;
1291                 while ((my $pos = index($line, "\t")) != -1) {
1292                         if (my $count = (8 - ($pos % 8))) {
1293                                 my $spaces = ' ' x $count;
1294                                 $line =~ s/\t/$spaces/;
1295                         }
1296                 }
1297                 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n", $nr, $nr, $nr, escapeHTML($line);
1298         }
1299         close $fd or print "Reading blob failed.\n";
1300         print "</div>";
1301         git_footer_html();
1302 }
1303
1304 sub git_blob_plain {
1305         print $cgi->header(-type => "text/plain", -charset => 'utf-8');
1306         open my $fd, "-|", "$gitbin/git-cat-file blob $hash" or return;
1307         undef $/;
1308         print <$fd>;
1309         $/ = "\n";
1310         close $fd;
1311 }
1312
1313 sub git_tree {
1314         if (!defined $hash) {
1315                 $hash = git_read_hash("$project/HEAD");
1316                 if (defined $file_name) {
1317                         my $base = $hash_base || git_read_hash("$project/HEAD");
1318                         $hash = git_get_hash_by_path($base, $file_name, "tree");
1319                 }
1320                 if (!defined $hash_base) {
1321                         $hash_base = git_read_hash("$project/HEAD");
1322                 }
1323         }
1324         open my $fd, "-|", "$gitbin/git-ls-tree $hash" or die_error(undef, "Open git-ls-tree failed.");
1325         my (@entries) = map { chomp; $_ } <$fd>;
1326         close $fd or die_error(undef, "Reading tree failed.");
1327
1328         git_header_html();
1329         my $base_key = "";
1330         my $file_key = "";
1331         my $base = "";
1332         if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
1333                 $base_key = ";hb=$hash_base";
1334                 print "<div class=\"page_nav\">\n" .
1335                       $cgi->a({-href => "$my_uri?p=$project;a=summary"}, "summary") .
1336                       " | " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog;h=$hash_base"}, "shortlog") .
1337                       " | " . $cgi->a({-href => "$my_uri?p=$project;a=log;h=$hash_base"}, "log") .
1338                       " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash_base"}, "commit") .
1339                       " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash_base"}, "commitdiff") .
1340                       " | tree" .
1341                       "<br/><br/>\n" .
1342                       "</div>\n";
1343                 print "<div>\n" .
1344                       $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash_base", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
1345                       "</div>\n";
1346         } else {
1347                 print "<div class=\"page_nav\">\n";
1348                 print "<br/><br/></div>\n";
1349                 print "<div class=\"title\">$hash</div>\n";
1350         }
1351         if (defined $file_name) {
1352                 $base = "$file_name/";
1353                 print "<div class=\"page_path\"><b>/$file_name</b></div>\n";
1354         } else {
1355                 print "<div class=\"page_path\"><b>/</b></div>\n";
1356         }
1357         print "<div class=\"page_body\">\n";
1358         print "<table cellspacing=\"0\">\n";
1359         my $alternate = 0;
1360         foreach my $line (@entries) {
1361                 #'100644        blob    0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa        panic.c'
1362                 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
1363                 my $t_mode = $1;
1364                 my $t_type = $2;
1365                 my $t_hash = $3;
1366                 my $t_name = $4;
1367                 $file_key = ";f=$base$t_name";
1368                 if ($alternate) {
1369                         print "<tr class=\"dark\">\n";
1370                 } else {
1371                         print "<tr class=\"light\">\n";
1372                 }
1373                 $alternate ^= 1;
1374                 print "<td style=\"font-family:monospace\">" . mode_str($t_mode) . "</td>\n";
1375                 if ($t_type eq "blob") {
1376                         print "<td class=\"list\">" .
1377                               $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$t_hash" . $base_key . $file_key, -class => "list"}, $t_name) .
1378                               "</td>\n" .
1379                               "<td class=\"link\">" .
1380                               $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$t_hash" . $base_key . $file_key}, "blob") .
1381                               " | " . $cgi->a({-href => "$my_uri?p=$project;a=history;h=$hash_base" . $file_key}, "history") .
1382                               "</td>\n";
1383                 } elsif ($t_type eq "tree") {
1384                         print "<td class=\"list\">" .
1385                               $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$t_hash" . $base_key . $file_key}, $t_name) .
1386                               "</td>\n" .
1387                               "<td class=\"link\">" .
1388                               $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$t_hash" . $base_key . $file_key}, "tree") .
1389                               "</td>\n";
1390                 }
1391                 print "</tr>\n";
1392         }
1393         print "</table>\n" .
1394               "</div>";
1395         git_footer_html();
1396 }
1397
1398 sub git_rss {
1399         # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
1400         open my $fd, "-|", "$gitbin/git-rev-list --max-count=150 " . git_read_hash("$project/HEAD") or die_error(undef, "Open failed.");
1401         my (@revlist) = map { chomp; $_ } <$fd>;
1402         close $fd or die_error(undef, "Reading rev-list failed.");
1403         print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
1404         print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".
1405               "<rss version=\"2.0\" xmlns:content=\"http://purl.org/rss/1.0/modules/content/\">\n";
1406         print "<channel>\n";
1407         print "<title>$project</title>\n".
1408               "<link>" . escapeHTML("$my_url?p=$project;a=summary") . "</link>\n".
1409               "<description>$project log</description>\n".
1410               "<language>en</language>\n";
1411
1412         for (my $i = 0; $i <= $#revlist; $i++) {
1413                 my $commit = $revlist[$i];
1414                 my %co = git_read_commit($commit);
1415                 # we read 150, we always show 30 and the ones more recent than 48 hours
1416                 if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
1417                         last;
1418                 }
1419                 my %cd = date_str($co{'committer_epoch'});
1420                 open $fd, "-|", "$gitbin/git-diff-tree -r $co{'parent'} $co{'id'}" or next;
1421                 my @difftree = map { chomp; $_ } <$fd>;
1422                 close $fd or next;
1423                 print "<item>\n" .
1424                       "<title>" .
1425                       sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . escapeHTML($co{'title'}) .
1426                       "</title>\n" .
1427                       "<author>" . escapeHTML($co{'author'}) . "</author>\n" .
1428                       "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
1429                       "<guid isPermaLink=\"true\">" . escapeHTML("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
1430                       "<link>" . escapeHTML("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
1431                       "<description>" . escapeHTML($co{'title'}) . "</description>\n" .
1432                       "<content:encoded>" .
1433                       "<![CDATA[\n";
1434                 my $comment = $co{'comment'};
1435                 foreach my $line (@$comment) {
1436                         print "$line<br/>\n";
1437                 }
1438                 print "<br/>\n";
1439                 foreach my $line (@difftree) {
1440                         if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
1441                                 next;
1442                         }
1443                         my $file = $7;
1444                         print "$file<br/>\n";
1445                 }
1446                 print "]]>\n" .
1447                       "</content:encoded>\n" .
1448                       "</item>\n";
1449         }
1450         print "</channel></rss>";
1451 }
1452
1453 sub git_opml {
1454         my @list = git_read_projects();
1455
1456         print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
1457         print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".
1458               "<opml version=\"1.0\">\n".
1459               "<head>".
1460               "  <title>Git OPML Export</title>\n".
1461               "</head>\n".
1462               "<body>\n".
1463               "<outline text=\"git RSS feeds\">\n";
1464
1465         foreach my $pr (@list) {
1466                 my %proj = %$pr;
1467                 my $head = git_read_hash("$proj{'path'}/HEAD");
1468                 if (!defined $head) {
1469                         next;
1470                 }
1471                 $ENV{'GIT_DIR'} = "$projectroot/$proj{'path'}";
1472                 my %co = git_read_commit($head);
1473                 if (!%co) {
1474                         next;
1475                 }
1476
1477                 my $path = escapeHTML(chop_str($proj{'path'}, 25, 5));
1478                 my $rss =  "$my_url?p=$proj{'path'};a=rss";
1479                 my $html =  "$my_url?p=$proj{'path'};a=summary";
1480                 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
1481         }
1482         print "</outline>\n".
1483               "</body>\n".
1484               "</opml>\n";
1485 }
1486
1487 sub git_log {
1488         my $head = git_read_hash("$project/HEAD");
1489         if (!defined $hash) {
1490                 $hash = $head;
1491         }
1492         if (!defined $page) {
1493                 $page = 0;
1494         }
1495         git_header_html();
1496         print "<div class=\"page_nav\">\n";
1497         print $cgi->a({-href => "$my_uri?p=$project;a=summary"}, "summary") .
1498               " | " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog;h=$hash"}, "shortlog") .
1499               " | log" .
1500               " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash"}, "commit") .
1501               " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash"}, "commitdiff") .
1502               " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$hash;hb=$hash"}, "tree") . "<br/>\n";
1503
1504         my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
1505         open my $fd, "-|", "$gitbin/git-rev-list $limit $hash" or die_error(undef, "Open failed.");
1506         my (@revlist) = map { chomp; $_ } <$fd>;
1507         close $fd;
1508
1509         if ($hash ne $head || $page) {
1510                 print $cgi->a({-href => "$my_uri?p=$project;a=log"}, "HEAD");
1511         } else {
1512                 print "HEAD";
1513         }
1514         if ($page > 0) {
1515                 print " &sdot; " .
1516                 $cgi->a({-href => "$my_uri?p=$project;a=log;h=$hash;pg=" . ($page-1), -accesskey => "p", -title => "Alt-p"}, "prev");
1517         } else {
1518                 print " &sdot; prev";
1519         }
1520         if ($#revlist >= (100 * ($page+1)-1)) {
1521                 print " &sdot; " .
1522                 $cgi->a({-href => "$my_uri?p=$project;a=log;h=$hash;pg=" . ($page+1), -accesskey => "n", -title => "Alt-n"}, "next");
1523         } else {
1524                 print " &sdot; next";
1525         }
1526         print "<br/>\n" .
1527               "</div>\n";
1528         if (!@revlist) {
1529                 print "<div>\n" .
1530                       $cgi->a({-href => "$my_uri?p=$project;a=summary", -class => "title"}, "&nbsp;") .
1531                       "</div>\n";
1532                 my %co = git_read_commit($hash);
1533                 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
1534         }
1535         for (my $i = ($page * 100); $i <= $#revlist; $i++) {
1536                 my $commit = $revlist[$i];
1537                 my %co = git_read_commit($commit);
1538                 next if !%co;
1539                 my %ad = date_str($co{'author_epoch'});
1540                 print "<div>\n" .
1541                       $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit", -class => "title"},
1542                       "<span class=\"age\">$co{'age_string'}</span>" . escapeHTML($co{'title'})) . "\n" .
1543                       "</div>\n";
1544                 print "<div class=\"title_text\">\n" .
1545                       "<div class=\"log_link\">\n" .
1546                       $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit"}, "commit") .
1547                       " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$commit"}, "commitdiff") .
1548                       "<br/>\n" .
1549                       "</div>\n" .
1550                       "<i>" . escapeHTML($co{'author_name'}) .  " [$ad{'rfc2822'}]</i><br/>\n" .
1551                       "</div>\n" .
1552                       "<div class=\"log_body\">\n";
1553                 my $comment = $co{'comment'};
1554                 my $empty = 0;
1555                 foreach my $line (@$comment) {
1556                         if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1557                                 next;
1558                         }
1559                         if ($line eq "") {
1560                                 if ($empty) {
1561                                         next;
1562                                 }
1563                                 $empty = 1;
1564                         } else {
1565                                 $empty = 0;
1566                         }
1567                         print format_log_line_html($line) . "<br/>\n";
1568                 }
1569                 if (!$empty) {
1570                         print "<br/>\n";
1571                 }
1572                 print "</div>\n";
1573         }
1574         git_footer_html();
1575 }
1576
1577 sub git_commit {
1578         my %co = git_read_commit($hash);
1579         if (!%co) {
1580                 die_error(undef, "Unknown commit object.");
1581         }
1582         my %ad = date_str($co{'author_epoch'}, $co{'author_tz'});
1583         my %cd = date_str($co{'committer_epoch'}, $co{'committer_tz'});
1584
1585         my @difftree;
1586         my $root = "";
1587         my $parent = $co{'parent'};
1588         if (!defined $parent) {
1589                 $root = " --root";
1590                 $parent = "";
1591         }
1592         open my $fd, "-|", "$gitbin/git-diff-tree -r -M $root $parent $hash" or die_error(undef, "Open failed.");
1593         @difftree = map { chomp; $_ } <$fd>;
1594         close $fd or die_error(undef, "Reading diff-tree failed.");
1595         git_header_html();
1596         print "<div class=\"page_nav\">\n" .
1597               $cgi->a({-href => "$my_uri?p=$project;a=summary"}, "summary") .
1598               " | " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog;h=$hash"}, "shortlog") .
1599               " | " . $cgi->a({-href => "$my_uri?p=$project;a=log;h=$hash"}, "log") .
1600               " | commit";
1601         if (defined $co{'parent'}) {
1602                 print " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash"}, "commitdiff");
1603         }
1604         print " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash"}, "tree") . "\n" .
1605               "<br/><br/></div>\n";
1606         if (defined $co{'parent'}) {
1607                 print "<div>\n" .
1608                       $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
1609                       "</div>\n";
1610         } else {
1611                 print "<div>\n" .
1612                       $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
1613                       "</div>\n";
1614         }
1615         print "<div class=\"title_text\">\n" .
1616               "<table cellspacing=\"0\">\n";
1617         print "<tr><td>author</td><td>" . escapeHTML($co{'author'}) . "</td></tr>\n".
1618               "<tr>" .
1619               "<td></td><td> $ad{'rfc2822'}";
1620         if ($ad{'hour_local'} < 6) {
1621                 printf(" (<span style=\"color: #cc0000;\">%02d:%02d</span> %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1622         } else {
1623                 printf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1624         }
1625         print "</td>" .
1626               "</tr>\n";
1627         print "<tr><td>committer</td><td>" . escapeHTML($co{'committer'}) . "</td></tr>\n";
1628         print "<tr><td></td><td> $cd{'rfc2822'}" . sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) . "</td></tr>\n";
1629         print "<tr><td>commit</td><td style=\"font-family:monospace\">$hash</td></tr>\n";
1630         print "<tr>" .
1631               "<td>tree</td>" .
1632               "<td style=\"font-family:monospace\">" .
1633               $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash", class => "list"}, $co{'tree'}) .
1634               "</td>" .
1635               "<td class=\"link\">" . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash"}, "tree") .
1636               "</td>" .
1637               "</tr>\n";
1638         my $parents  = $co{'parents'};
1639         foreach my $par (@$parents) {
1640                 print "<tr>" .
1641                       "<td>parent</td>" .
1642                       "<td style=\"font-family:monospace\">" . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$par", class => "list"}, $par) . "</td>" .
1643                       "<td class=\"link\">" .
1644                       $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$par"}, "commit") .
1645                       " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash;hp=$par"}, "commitdiff") .
1646                       "</td>" .
1647                       "</tr>\n";
1648         }
1649         print "</table>". 
1650               "</div>\n";
1651         print "<div class=\"page_body\">\n";
1652         my $comment = $co{'comment'};
1653         my $empty = 0;
1654         my $signed = 0;
1655         foreach my $line (@$comment) {
1656                 # print only one empty line
1657                 if ($line eq "") {
1658                         if ($empty || $signed) {
1659                                 next;
1660                         }
1661                         $empty = 1;
1662                 } else {
1663                         $empty = 0;
1664                 }
1665                 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1666                         $signed = 1;
1667                         print "<span style=\"color: #888888\">" . escapeHTML($line) . "</span><br/>\n";
1668                 } else {
1669                         $signed = 0;
1670                         print format_log_line_html($line) . "<br/>\n";
1671                 }
1672         }
1673         print "</div>\n";
1674         print "<div class=\"list_head\">\n";
1675         if ($#difftree > 10) {
1676                 print(($#difftree + 1) . " files changed:\n");
1677         }
1678         print "</div>\n";
1679         print "<table cellspacing=\"0\">\n";
1680         my $alternate = 0;
1681         foreach my $line (@difftree) {
1682                 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M      ls-files.c'
1683                 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M      rev-tree.c'
1684                 if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
1685                         next;
1686                 }
1687                 my $from_mode = $1;
1688                 my $to_mode = $2;
1689                 my $from_id = $3;
1690                 my $to_id = $4;
1691                 my $status = $5;
1692                 my $similarity = $6;
1693                 my $file = $7;
1694                 if ($alternate) {
1695                         print "<tr class=\"dark\">\n";
1696                 } else {
1697                         print "<tr class=\"light\">\n";
1698                 }
1699                 $alternate ^= 1;
1700                 if ($status eq "A") {
1701                         my $mode_chng = "";
1702                         if (S_ISREG(oct $to_mode)) {
1703                                 $mode_chng = sprintf(" with mode: %04o", (oct $to_mode) & 0777);
1704                         }
1705                         print "<td>" .
1706                               $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$to_id;hp=$hash;f=$file", -class => "list"}, escapeHTML($file)) . "</td>\n" .
1707                               "<td><span style=\"color: #008000;\">[new " . file_type($to_mode) . "$mode_chng]</span></td>\n" .
1708                               "<td class=\"link\">" . $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$to_id;hb=$hash;f=$file"}, "blob") . "</td>\n";
1709                 } elsif ($status eq "D") {
1710                         print "<td>" .
1711                               $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$from_id;hb=$hash;f=$file", -class => "list"}, escapeHTML($file)) . "</td>\n" .
1712                               "<td><span style=\"color: #c00000;\">[deleted " . file_type($from_mode). "]</span></td>\n" .
1713                               "<td class=\"link\">" .
1714                               $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$from_id;hb=$hash;f=$file"}, "blob") .
1715                               " | " . $cgi->a({-href => "$my_uri?p=$project;a=history;h=$hash;f=$file"}, "history") .
1716                               "</td>\n"
1717                 } elsif ($status eq "M" || $status eq "T") {
1718                         my $mode_chnge = "";
1719                         if ($from_mode != $to_mode) {
1720                                 $mode_chnge = " <span style=\"color: #777777;\">[changed";
1721                                 if (((oct $from_mode) & S_IFMT) != ((oct $to_mode) & S_IFMT)) {
1722                                         $mode_chnge .= " from " . file_type($from_mode) . " to " . file_type($to_mode);
1723                                 }
1724                                 if (((oct $from_mode) & 0777) != ((oct $to_mode) & 0777)) {
1725                                         if (S_ISREG($from_mode) && S_ISREG($to_mode)) {
1726                                                 $mode_chnge .= sprintf(" mode: %04o->%04o", (oct $from_mode) & 0777, (oct $to_mode) & 0777);
1727                                         } elsif (S_ISREG($to_mode)) {
1728                                                 $mode_chnge .= sprintf(" mode: %04o", (oct $to_mode) & 0777);
1729                                         }
1730                                 }
1731                                 $mode_chnge .= "]</span>\n";
1732                         }
1733                         print "<td>";
1734                         if ($to_id ne $from_id) {
1735                                 print $cgi->a({-href => "$my_uri?p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$file", -class => "list"}, escapeHTML($file));
1736                         } else {
1737                                 print $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$to_id;hb=$hash;f=$file", -class => "list"}, escapeHTML($file));
1738                         }
1739                         print "</td>\n" .
1740                               "<td>$mode_chnge</td>\n" .
1741                               "<td class=\"link\">";
1742                         print $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$to_id;hb=$hash;f=$file"}, "blob");
1743                         if ($to_id ne $from_id) {
1744                                 print " | " . $cgi->a({-href => "$my_uri?p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$file"}, "diff");
1745                         }
1746                         print " | " . $cgi->a({-href => "$my_uri?p=$project;a=history;h=$hash;f=$file"}, "history") . "\n";
1747                         print "</td>\n";
1748                 } elsif ($status eq "R") {
1749                         my ($from_file, $to_file) = split "\t", $file;
1750                         my $mode_chng = "";
1751                         if ($from_mode != $to_mode) {
1752                                 $mode_chng = sprintf(", mode: %04o", (oct $to_mode) & 0777);
1753                         }
1754                         print "<td>" .
1755                               $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$to_id;hb=$hash;f=$to_file", -class => "list"}, escapeHTML($to_file)) . "</td>\n" .
1756                               "<td><span style=\"color: #777777;\">[moved from " .
1757                               $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$from_id;hb=$hash;f=$from_file", -class => "list"}, escapeHTML($from_file)) .
1758                               " with " . (int $similarity) . "% similarity$mode_chng]</span></td>\n" .
1759                               "<td class=\"link\">" .
1760                               $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$to_id;hb=$hash;f=$to_file"}, "blob");
1761                         if ($to_id ne $from_id) {
1762                                 print " | " . $cgi->a({-href => "$my_uri?p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$to_file"}, "diff");
1763                         }
1764                         print "</td>\n";
1765                 }
1766                 print "</tr>\n";
1767         }
1768         print "</table>\n";
1769         git_footer_html();
1770 }
1771
1772 sub git_blobdiff {
1773         mkdir($git_temp, 0700);
1774         git_header_html();
1775         if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
1776                 print "<div class=\"page_nav\">\n" .
1777                       $cgi->a({-href => "$my_uri?p=$project;a=summary"}, "summary") .
1778                       " | " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog"}, "shortlog") .
1779                       " | " . $cgi->a({-href => "$my_uri?p=$project;a=log"}, "log") .
1780                       " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash_base"}, "commit") .
1781                       " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash_base"}, "commitdiff") .
1782                       " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash_base"}, "tree") .
1783                       "<br/>\n";
1784                 print $cgi->a({-href => "$my_uri?p=$project;a=blobdiff_plain;h=$hash;hp=$hash_parent"}, "plain") .
1785                       "</div>\n";
1786                 print "<div>\n" .
1787                       $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash_base", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
1788                       "</div>\n";
1789         } else {
1790                 print "<div class=\"page_nav\">\n" .
1791                       "<br/><br/></div>\n" .
1792                       "<div class=\"title\">$hash vs $hash_parent</div>\n";
1793         }
1794         if (defined $file_name) {
1795                 print "<div class=\"page_path\"><b>/$file_name</b></div>\n";
1796         }
1797         print "<div class=\"page_body\">\n" .
1798               "<div class=\"diff_info\">blob:" .
1799               $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$hash_parent;hb=$hash_base;f=$file_name"}, $hash_parent) .
1800               " -> blob:" .
1801               $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$hash;hb=$hash_base;f=$file_name"}, $hash) .
1802               "</div>\n";
1803         git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash);
1804         print "</div>";
1805         git_footer_html();
1806 }
1807
1808 sub git_blobdiff_plain {
1809         mkdir($git_temp, 0700);
1810         print $cgi->header(-type => "text/plain", -charset => 'utf-8');
1811         git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash, "plain");
1812 }
1813
1814 sub git_commitdiff {
1815         mkdir($git_temp, 0700);
1816         my %co = git_read_commit($hash);
1817         if (!%co) {
1818                 die_error(undef, "Unknown commit object.");
1819         }
1820         if (!defined $hash_parent) {
1821                 $hash_parent = $co{'parent'};
1822         }
1823         open my $fd, "-|", "$gitbin/git-diff-tree -r $hash_parent $hash" or die_error(undef, "Open failed.");
1824         my (@difftree) = map { chomp; $_ } <$fd>;
1825         close $fd or die_error(undef, "Reading diff-tree failed.");
1826
1827         git_header_html();
1828         print "<div class=\"page_nav\">\n" .
1829               $cgi->a({-href => "$my_uri?p=$project;a=summary"}, "summary") .
1830               " | " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog;h=$hash"}, "shortlog") .
1831               " | " . $cgi->a({-href => "$my_uri?p=$project;a=log;h=$hash"}, "log") .
1832               " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash"}, "commit") .
1833               " | commitdiff" .
1834               " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash"}, "tree") . "<br/>\n";
1835         print $cgi->a({-href => "$my_uri?p=$project;a=commitdiff_plain;h=$hash;hp=$hash_parent"}, "plain") . "\n" .
1836               "</div>\n";
1837         print "<div>\n" .
1838               $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
1839               "</div>\n";
1840         print "<div class=\"page_body\">\n";
1841         my $comment = $co{'comment'};
1842         my $empty = 0;
1843         my $signed = 0;
1844         my @log = @$comment;
1845         # remove first and empty lines after that
1846         shift @log;
1847         while (defined $log[0] && $log[0] eq "") {
1848                 shift @log;
1849         }
1850         foreach my $line (@log) {
1851                 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1852                         next;
1853                 }
1854                 if ($line eq "") {
1855                         if ($empty) {
1856                                 next;
1857                         }
1858                         $empty = 1;
1859                 } else {
1860                         $empty = 0;
1861                 }
1862                 print format_log_line_html($line) . "<br/>\n";
1863         }
1864         print "<br/>\n";
1865         foreach my $line (@difftree) {
1866                 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M      ls-files.c'
1867                 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M      rev-tree.c'
1868                 $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/;
1869                 my $from_mode = $1;
1870                 my $to_mode = $2;
1871                 my $from_id = $3;
1872                 my $to_id = $4;
1873                 my $status = $5;
1874                 my $file = $6;
1875                 if ($status eq "A") {
1876                         print "<div class=\"diff_info\">" .  file_type($to_mode) . ":" .
1877                               $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$to_id;hb=$hash;f=$file"}, $to_id) . "(new)" .
1878                               "</div>\n";
1879                         git_diff_print(undef, "/dev/null", $to_id, "b/$file");
1880                 } elsif ($status eq "D") {
1881                         print "<div class=\"diff_info\">" . file_type($from_mode) . ":" .
1882                               $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$from_id;hb=$hash;f=$file"}, $from_id) . "(deleted)" .
1883                               "</div>\n";
1884                         git_diff_print($from_id, "a/$file", undef, "/dev/null");
1885                 } elsif ($status eq "M") {
1886                         if ($from_id ne $to_id) {
1887                                 print "<div class=\"diff_info\">" .
1888                                       file_type($from_mode) . ":" . $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$from_id;hb=$hash;f=$file"}, $from_id) .
1889                                       " -> " .
1890                                       file_type($to_mode) . ":" . $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$to_id;hb=$hash;f=$file"}, $to_id);
1891                                 print "</div>\n";
1892                                 git_diff_print($from_id, "a/$file",  $to_id, "b/$file");
1893                         }
1894                 }
1895         }
1896         print "<br/>\n" .
1897               "</div>";
1898         git_footer_html();
1899 }
1900
1901 sub git_commitdiff_plain {
1902         mkdir($git_temp, 0700);
1903         open my $fd, "-|", "$gitbin/git-diff-tree -r $hash_parent $hash" or die_error(undef, "Open failed.");
1904         my (@difftree) = map { chomp; $_ } <$fd>;
1905         close $fd or die_error(undef, "Reading diff-tree failed.");
1906
1907         # try to figure out the next tag after this commit
1908         my $tagname;
1909         my %taghash;
1910         my $tags = git_read_refs("refs/tags");
1911         foreach my $entry (@$tags) {
1912                 my %tag = %$entry;
1913                 $taghash{$tag{'refid'}} = $tag{'name'};
1914         }
1915         open $fd, "-|", "$gitbin/git-rev-list HEAD";
1916         while (my $commit = <$fd>) {
1917                 chomp $commit;
1918                 if ($taghash{$commit}) {
1919                         $tagname = $taghash{$commit};
1920                 }
1921                 if ($commit eq $hash) {
1922                         last;
1923                 }
1924         }
1925         close $fd;
1926
1927         print $cgi->header(-type => "text/plain", -charset => 'utf-8');
1928         my %co = git_read_commit($hash);
1929         my %ad = date_str($co{'author_epoch'}, $co{'author_tz'});
1930         my $comment = $co{'comment'};
1931         print "From: $co{'author'}\n" .
1932               "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n".
1933               "Subject: $co{'title'}\n";
1934         if (defined $tagname) {
1935               print "X-Git-Tag: $tagname\n";
1936         }
1937         print "X-Git-Url: $my_url?p=$project;a=commitdiff;h=$hash\n" .
1938               "\n";
1939
1940         foreach my $line (@$comment) {;
1941                 print "  $line\n";
1942         }
1943         print "---\n\n";
1944
1945         foreach my $line (@difftree) {
1946                 $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/;
1947                 my $from_id = $3;
1948                 my $to_id = $4;
1949                 my $status = $5;
1950                 my $file = $6;
1951                 if ($status eq "A") {
1952                         git_diff_print(undef, "/dev/null", $to_id, "b/$file", "plain");
1953                 } elsif ($status eq "D") {
1954                         git_diff_print($from_id, "a/$file", undef, "/dev/null", "plain");
1955                 } elsif ($status eq "M") {
1956                         git_diff_print($from_id, "a/$file",  $to_id, "b/$file", "plain");
1957                 }
1958         }
1959 }
1960
1961 sub git_history {
1962         if (!defined $hash) {
1963                 $hash = git_read_hash("$project/HEAD");
1964         }
1965         my %co = git_read_commit($hash);
1966         if (!%co) {
1967                 die_error(undef, "Unknown commit object.");
1968         }
1969         git_header_html();
1970         print "<div class=\"page_nav\">\n" .
1971               $cgi->a({-href => "$my_uri?p=$project;a=summary"}, "summary") .
1972               " | " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog"}, "shortlog") .
1973               " | " . $cgi->a({-href => "$my_uri?p=$project;a=log"}, "log") .
1974               " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash"}, "commit") .
1975               " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash"}, "commitdiff") .
1976               " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash"}, "tree") .
1977               "<br/><br/>\n" .
1978               "</div>\n";
1979         print "<div>\n" .
1980               $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
1981               "</div>\n";
1982         print "<div class=\"page_path\"><b>/$file_name</b><br/></div>\n";
1983
1984         open my $fd, "-|", "$gitbin/git-rev-list $hash | $gitbin/git-diff-tree -r --stdin $file_name";
1985         my $commit;
1986         print "<table cellspacing=\"0\">\n";
1987         my $alternate = 0;
1988         while (my $line = <$fd>) {
1989                 if ($line =~ m/^([0-9a-fA-F]{40})/){
1990                         $commit = $1;
1991                         next;
1992                 }
1993                 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/ && (defined $commit)) {
1994                         my %co = git_read_commit($commit);
1995                         if (!%co) {
1996                                 next;
1997                         }
1998                         if ($alternate) {
1999                                 print "<tr class=\"dark\">\n";
2000                         } else {
2001                                 print "<tr class=\"light\">\n";
2002                         }
2003                         $alternate ^= 1;
2004                         print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2005                               "<td><i>" . escapeHTML(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
2006                               "<td>" . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit", -class => "list"}, "<b>" .
2007                               escapeHTML(chop_str($co{'title'}, 50)) . "</b>") . "</td>\n" .
2008                               "<td class=\"link\">" .
2009                               $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit"}, "commit") .
2010                               " | " . $cgi->a({-href => "$my_uri?p=$project;a=blob;hb=$commit;f=$file_name"}, "blob");
2011                         my $blob = git_get_hash_by_path($hash, $file_name);
2012                         my $blob_parent = git_get_hash_by_path($commit, $file_name);
2013                         if (defined $blob && defined $blob_parent && $blob ne $blob_parent) {
2014                                 print " | " .
2015                                 $cgi->a({-href => "$my_uri?p=$project;a=blobdiff;h=$blob;hp=$blob_parent;hb=$commit;f=$file_name"},
2016                                 "diff to current");
2017                         }
2018                         print "</td>\n" .
2019                               "</tr>\n";
2020                         undef $commit;
2021                 }
2022         }
2023         print "</table>\n";
2024         close $fd;
2025         git_footer_html();
2026 }
2027
2028 sub git_search {
2029         if (!defined $searchtext) {
2030                 die_error("", "Text field empty.");
2031         }
2032         if (!defined $hash) {
2033                 $hash = git_read_hash("$project/HEAD");
2034         }
2035         my %co = git_read_commit($hash);
2036         if (!%co) {
2037                 die_error(undef, "Unknown commit object.");
2038         }
2039         # pickaxe may take all resources of your box and run for several minutes
2040         # with every query - so decide by yourself how public you make this feature :)
2041         my $commit_search = 1;
2042         my $author_search = 0;
2043         my $committer_search = 0;
2044         my $pickaxe_search = 0;
2045         if ($searchtext =~ s/^author\\://i) {
2046                 $author_search = 1;
2047         } elsif ($searchtext =~ s/^committer\\://i) {
2048                 $committer_search = 1;
2049         } elsif ($searchtext =~ s/^pickaxe\\://i) {
2050                 $commit_search = 0;
2051                 $pickaxe_search = 1;
2052         }
2053         git_header_html();
2054         print "<div class=\"page_nav\">\n" .
2055               $cgi->a({-href => "$my_uri?p=$project;a=summary;h=$hash"}, "summary") .
2056               " | " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog"}, "shortlog") .
2057               " | " . $cgi->a({-href => "$my_uri?p=$project;a=log;h=$hash"}, "log") .
2058               " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash"}, "commit") .
2059               " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash"}, "commitdiff") .
2060               " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash"}, "tree") .
2061               "<br/><br/>\n" .
2062               "</div>\n";
2063
2064         print "<div>\n" .
2065               $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
2066               "</div>\n";
2067         print "<table cellspacing=\"0\">\n";
2068         my $alternate = 0;
2069         if ($commit_search) {
2070                 $/ = "\0";
2071                 open my $fd, "-|", "$gitbin/git-rev-list --header $hash";
2072                 while (my $commit_text = <$fd>) {
2073                         if (!grep m/$searchtext/i, $commit_text) {
2074                                 next;
2075                         }
2076                         if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
2077                                 next;
2078                         }
2079                         if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
2080                                 next;
2081                         }
2082                         my @commit_lines = split "\n", $commit_text;
2083                         my $commit = shift @commit_lines;
2084                         my %co = git_read_commit($commit, \@commit_lines);
2085                         if (!%co) {
2086                                 next;
2087                         }
2088                         if ($alternate) {
2089                                 print "<tr class=\"dark\">\n";
2090                         } else {
2091                                 print "<tr class=\"light\">\n";
2092                         }
2093                         $alternate ^= 1;
2094                         print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2095                               "<td><i>" . escapeHTML(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
2096                               "<td>" .
2097                               $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit", -class => "list"}, "<b>" . escapeHTML(chop_str($co{'title'}, 50)) . "</b><br/>");
2098                         my $comment = $co{'comment'};
2099                         foreach my $line (@$comment) {
2100                                 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
2101                                         my $lead = escapeHTML($1) || "";
2102                                         $lead = chop_str($lead, 30, 10);
2103                                         my $match = escapeHTML($2) || "";
2104                                         my $trail = escapeHTML($3) || "";
2105                                         $trail = chop_str($trail, 30, 10);
2106                                         my $text = "$lead<span style=\"color:#e00000\">$match</span>$trail";
2107                                         print chop_str($text, 80, 5) . "<br/>\n";
2108                                 }
2109                         }
2110                         print "</td>\n" .
2111                               "<td class=\"link\">" .
2112                               $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit"}, "commit") .
2113                               " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$commit"}, "tree");
2114                         print "</td>\n" .
2115                               "</tr>\n";
2116                 }
2117                 close $fd;
2118         }
2119
2120         if ($pickaxe_search) {
2121                 $/ = "\n";
2122                 open my $fd, "-|", "$gitbin/git-rev-list $hash | $gitbin/git-diff-tree -r --stdin -S$searchtext";
2123                 undef %co;
2124                 my @files;
2125                 while (my $line = <$fd>) {
2126                         if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
2127                                 my %set;
2128                                 $set{'file'} = $6;
2129                                 $set{'from_id'} = $3;
2130                                 $set{'to_id'} = $4;
2131                                 $set{'id'} = $set{'to_id'};
2132                                 if ($set{'id'} =~ m/0{40}/) {
2133                                         $set{'id'} = $set{'from_id'};
2134                                 }
2135                                 if ($set{'id'} =~ m/0{40}/) {
2136                                         next;
2137                                 }
2138                                 push @files, \%set;
2139                         } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
2140                                 if (%co) {
2141                                         if ($alternate) {
2142                                                 print "<tr class=\"dark\">\n";
2143                                         } else {
2144                                                 print "<tr class=\"light\">\n";
2145                                         }
2146                                         $alternate ^= 1;
2147                                         print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2148                                               "<td><i>" . escapeHTML(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
2149                                               "<td>" .
2150                                               $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$co{'id'}", -class => "list"}, "<b>" .
2151                                               escapeHTML(chop_str($co{'title'}, 50)) . "</b><br/>");
2152                                         while (my $setref = shift @files) {
2153                                                 my %set = %$setref;
2154                                                 print $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$set{'id'};hb=$co{'id'};f=$set{'file'}", class => "list"},
2155                                                       "<span style=\"color:#e00000\">" . escapeHTML($set{'file'}) . "</span>") .
2156                                                       "<br/>\n";
2157                                         }
2158                                         print "</td>\n" .
2159                                               "<td class=\"link\">" .
2160                                               $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$co{'id'}"}, "commit") .
2161                                               " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$co{'id'}"}, "tree");
2162                                         print "</td>\n" .
2163                                               "</tr>\n";
2164                                 }
2165                                 %co = git_read_commit($1);
2166                         }
2167                 }
2168                 close $fd;
2169         }
2170         print "</table>\n";
2171         git_footer_html();
2172 }
2173
2174 sub git_shortlog {
2175         my $head = git_read_hash("$project/HEAD");
2176         if (!defined $hash) {
2177                 $hash = $head;
2178         }
2179         if (!defined $page) {
2180                 $page = 0;
2181         }
2182         git_header_html();
2183         print "<div class=\"page_nav\">\n" .
2184               $cgi->a({-href => "$my_uri?p=$project;a=summary"}, "summary") .
2185               " | shortlog" .
2186               " | " . $cgi->a({-href => "$my_uri?p=$project;a=log;h=$hash"}, "log") .
2187               " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash"}, "commit") .
2188               " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash"}, "commitdiff") .
2189               " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$hash;hb=$hash"}, "tree") . "<br/>\n";
2190
2191         my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
2192         open my $fd, "-|", "$gitbin/git-rev-list $limit $hash" or die_error(undef, "Open failed.");
2193         my (@revlist) = map { chomp; $_ } <$fd>;
2194         close $fd;
2195
2196         if ($hash ne $head || $page) {
2197                 print $cgi->a({-href => "$my_uri?p=$project;a=shortlog"}, "HEAD");
2198         } else {
2199                 print "HEAD";
2200         }
2201         if ($page > 0) {
2202                 print " &sdot; " .
2203                 $cgi->a({-href => "$my_uri?p=$project;a=shortlog;h=$hash;pg=" . ($page-1), -accesskey => "p", -title => "Alt-p"}, "prev");
2204         } else {
2205                 print " &sdot; prev";
2206         }
2207         if ($#revlist >= (100 * ($page+1)-1)) {
2208                 print " &sdot; " .
2209                 $cgi->a({-href => "$my_uri?p=$project;a=shortlog;h=$hash;pg=" . ($page+1), -accesskey => "n", -title => "Alt-n"}, "next");
2210         } else {
2211                 print " &sdot; next";
2212         }
2213         print "<br/>\n" .
2214               "</div>\n";
2215         print "<div>\n" .
2216               $cgi->a({-href => "$my_uri?p=$project;a=summary", -class => "title"}, "&nbsp;") .
2217               "</div>\n";
2218         print "<table cellspacing=\"0\">\n";
2219         my $alternate = 0;
2220         for (my $i = ($page * 100); $i <= $#revlist; $i++) {
2221                 my $commit = $revlist[$i];
2222                 my %co = git_read_commit($commit);
2223                 my %ad = date_str($co{'author_epoch'});
2224                 if ($alternate) {
2225                         print "<tr class=\"dark\">\n";
2226                 } else {
2227                         print "<tr class=\"light\">\n";
2228                 }
2229                 $alternate ^= 1;
2230                 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2231                       "<td><i>" . escapeHTML(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
2232                       "<td>" . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit", -class => "list"}, "<b>" .
2233                       escapeHTML($co{'title_short'}) . "</b>") . "</td>\n" .
2234                       "<td class=\"link\">" .
2235                       $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit"}, "commit") .
2236                       " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$commit"}, "commitdiff") .
2237                       "</td>\n" .
2238                       "</tr>";
2239         }
2240         if ($#revlist >= (100 * ($page+1)-1)) {
2241                 print "<tr>\n" .
2242                       "<td>" .
2243                       $cgi->a({-href => "$my_uri?p=$project;a=shortlog;h=$hash;pg=" . ($page+1), -title => "Alt-n"}, "next") .
2244                       "</td>\n" .
2245                       "</tr>\n";
2246         }
2247         print "</table\n>";
2248         git_footer_html();
2249 }