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