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