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