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