v157
[git.git] / gitweb.cgi
1 #!/usr/bin/perl
2
3 # gitweb.pl - 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);
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 =           "157";
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 $gittmp =            "/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         }
53 } else {
54         $action = "summary";
55 }
56
57 my $project = $cgi->param('p');
58 if (defined $project) {
59         if ($project =~ m/(^|\/)(|\.|\.\.)($|\/)/) {
60                 undef $project;
61                 die_error(undef, "Non-canonical project parameter.");
62         }
63         if ($project =~ m/[^a-zA-Z0-9_\.\/\-\+\#\~]/) {
64                 undef $project;
65                 die_error(undef, "Invalid character in project parameter.");
66         }
67         if (!(-d "$projectroot/$project")) {
68                 undef $project;
69                 die_error(undef, "No such directory.");
70         }
71         if (!(-e "$projectroot/$project/HEAD")) {
72                 undef $project;
73                 die_error(undef, "No such project.");
74         }
75         $rss_link = "<link rel=\"alternate\" title=\"$project log\" href=\"$my_uri?p=$project;a=rss\" type=\"application/rss+xml\"/>";
76         $ENV{'GIT_OBJECT_DIRECTORY'} = "$projectroot/$project/objects";
77         $ENV{'SHA1_FILE_DIRECTORY'} = "$projectroot/$project/objects";
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 && !($hash =~ m/^[0-9a-fA-F]{40}$/)) {
97         undef $hash;
98         die_error(undef, "Invalid hash parameter.");
99 }
100
101 my $hash_parent = $cgi->param('hp');
102 if (defined $hash_parent && !($hash_parent =~ m/^[0-9a-fA-F]{40}$/)) {
103         undef $hash_parent;
104         die_error(undef, "Invalid hash_parent parameter.");
105 }
106
107 my $hash_base = $cgi->param('hb');
108 if (defined $hash_base && !($hash_base =~ m/^[0-9a-fA-F]{40}$/)) {
109         undef $hash_base;
110         die_error(undef, "Invalid parent hash parameter.");
111 }
112
113 my $time_back = $cgi->param('t');
114 if (defined $time_back) {
115         if ($time_back =~ m/^[^0-9]+$/) {
116                 undef $time_back;
117                 die_error(undef, "Invalid time parameter.");
118         }
119 }
120
121 if ($action eq "summary") {
122         git_summary();
123         exit;
124 } elsif ($action eq "branches") {
125         git_branches();
126         exit;
127 } elsif ($action eq "tags") {
128         git_tags();
129         exit;
130 } elsif ($action eq "blob") {
131         git_blob();
132         exit;
133 } elsif ($action eq "tree") {
134         git_tree();
135         exit;
136 } elsif ($action eq "rss") {
137         git_rss();
138         exit;
139 } elsif ($action eq "commit") {
140         git_commit();
141         exit;
142 } elsif ($action eq "log") {
143         git_log();
144         exit;
145 } elsif ($action eq "blobdiff") {
146         git_blobdiff();
147         exit;
148 } elsif ($action eq "commitdiff") {
149         git_commitdiff();
150         exit;
151 } elsif ($action eq "history") {
152         git_history();
153         exit;
154 } else {
155         undef $action;
156         die_error(undef, "Unknown action.");
157         exit;
158 }
159
160 sub git_header_html {
161         my $status = shift || "200 OK";
162
163         my $title = "git";
164         if (defined $project) {
165                 $title .= " - $project";
166                 if (defined $action) {
167                         $title .= "/$action";
168                 }
169         }
170         print $cgi->header(-type=>'text/html',  -charset => 'utf-8', -status=> $status);
171         print <<EOF;
172 <?xml version="1.0" encoding="utf-8"?>
173 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
174 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
175 <!-- git web interface v$version, (C) 2005, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke <ch\@gierke.de> -->
176 <head>
177 <title>$title</title>
178 $rss_link
179 <style type="text/css">
180 body { font-family: sans-serif; font-size: 12px; margin:0px; border:solid #d9d8d1; border-width:1px; margin:10px; }
181 a { color:#0000cc; }
182 a:hover, a:visited, a:active { color:#880000; }
183 div.page_header { height:25px; padding:8px; font-size:18px; font-weight:bold; background-color:#d9d8d1; }
184 div.page_header a:visited { color:#0000cc; }
185 div.page_header a:hover { color:#880000; }
186 div.page_nav { padding:8px; }
187 div.page_nav a:visited { color:#0000cc; }
188 div.page_path { font-weight:bold; padding:8px; border:solid #d9d8d1; border-width:0px 0px 1px}
189 div.page_footer { height:17px; padding:4px 8px; background-color: #d9d8d1; }
190 div.page_footer_text { float:left; color:#555555; font-style:italic; }
191 div.page_body { padding:8px; }
192 div.title, a.title {
193         display:block; padding:6px 8px;
194         font-weight:bold; background-color:#edece6; text-decoration:none; color:#000000;
195 }
196 a.title:hover { background-color: #d9d8d1; }
197 div.title_text { padding:6px 8px; border: solid #d9d8d1; border-width:0px 0px 1px; }
198 div.log_body { padding:8px 8px 8px 150px; }
199 span.age { position:relative; float:left; width:142px; font-style:italic; }
200 div.log_link {
201         font-size:10px; font-family:sans-serif; font-style:normal;
202         position:relative; float:left; width:142px;
203 }
204 div.list_head {
205         display:block; padding:6px 6px 4px; border:solid #d9d8d1;
206         border-width:1px 0px 0px; font-style:italic;
207 }
208 a.list { text-decoration:none; color:#000000; }
209 a.list:hover { color:#880000; }
210 td { padding:5px 15px 0px 0px; font-size:12px; }
211 th { padding-right:10px; font-size:12px; text-align:left; }
212 td.link { font-family:sans-serif; font-size:10px; }
213 td.pre { font-family:monospace; font-size:12px; white-space:pre; padding:2px 15px 0px 0px; }
214 div.pre { font-family:monospace; font-size:12px; white-space:pre; }
215 div.diff_info { font-family:monospace; color:#000099; background-color:#edece6; font-style:italic; }
216 div.index_include { border:solid #d9d8d1; border-width:0px 0px 1px; padding:12px 8px; }
217 a.rss_logo { float:right; padding:3px 0px; width:35px; line-height:10px;
218         border:1px solid; border-color:#fcc7a5 #7d3302 #3e1a01 #ff954e;
219         color:#ffffff; background-color:#ff6600;
220         font-weight:bold; font-family:sans-serif; font-size:10px;
221         text-align:center; text-decoration:none;
222 }
223 a.rss_logo:hover { background-color:#ee5500; }
224 </style>
225 </head>
226 <body>
227 EOF
228         print "<div class=\"page_header\">\n" .
229               "<a href=\"http://kernel.org/pub/software/scm/git/docs/\">" .
230               "<img src=\"$my_uri?a=git-logo.png\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/></a>";
231         print $cgi->a({-href => $home_link}, "projects") . " / ";
232         if (defined $project) {
233                 print $cgi->a({-href => "$my_uri?p=$project;a=summary"}, escapeHTML($project));
234                 if (defined $action) {
235                         print " / $action";
236                 }
237         }
238         print "</div>\n";
239 }
240
241 sub git_footer_html {
242         print "<div class=\"page_footer\">\n";
243         if (defined $project) {
244                 my $descr = git_read_description($project);
245                 if (defined $descr) {
246                         print "<div class=\"page_footer_text\">" . escapeHTML($descr) . "</div>\n";
247                 }
248                 print $cgi->a({-href => "$my_uri?p=$project;a=rss", -class => "rss_logo"}, "RSS") . "\n";
249         }
250         print "</div>\n" .
251               "</body>\n" .
252               "</html>";
253 }
254
255 sub die_error {
256         my $status = shift || "403 Forbidden";
257         my $error = shift || "Malformed query, file missing or permission denied"; 
258
259         git_header_html($status);
260         print "<div class=\"page_body\">\n" .
261               "<br/><br/>\n";
262         print "$status - $error\n";
263         print "<br/></div>\n";
264         git_footer_html();
265         exit;
266 }
267
268 sub git_get_type {
269         my $hash = shift;
270
271         open my $fd, "-|", "$gitbin/git-cat-file -t $hash" || return;
272         my $type = <$fd>;
273         close $fd;
274         chomp $type;
275         return $type;
276 }
277
278 sub git_read_hash {
279         my $path = shift;
280
281         open my $fd, "$projectroot/$path" || return undef;
282         my $head = <$fd>;
283         close $fd;
284         chomp $head;
285         if ($head =~ m/^[0-9a-fA-F]{40}$/) {
286                 return $head;
287         }
288 }
289
290 sub git_read_description {
291         my $path = shift;
292
293         open my $fd, "$projectroot/$path/description" || return undef;
294         my $descr = <$fd>;
295         close $fd;
296         chomp $descr;
297         return $descr;
298 }
299
300 sub git_read_tag {
301         my $tag_id = shift;
302         my %tag;
303
304         open my $fd, "-|", "$gitbin/git-cat-file tag $tag_id" || return;
305         while (my $line = <$fd>) {
306                 chomp $line;
307                 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
308                         $tag{'object'} = $1;
309                 } elsif ($line =~ m/^type (.*)$/) {
310                         $tag{'type'} = $1;
311                 } elsif ($line =~ m/^tag (.*)$/) {
312                         $tag{'name'} = $1;
313                 }
314         }
315         close $fd || return;
316         if (!defined $tag{'name'}) {
317                 return
318         };
319         return %tag
320 }
321
322 sub git_read_commit {
323         my $commit = shift;
324         my %co;
325         my @parents;
326
327         open my $fd, "-|", "$gitbin/git-cat-file commit $commit" || return;
328         while (my $line = <$fd>) {
329                 last if $line eq "\n";
330                 chomp $line;
331                 if ($line =~ m/^tree (.*)$/) {
332                         $co{'tree'} = $1;
333                 } elsif ($line =~ m/^parent (.*)$/) {
334                         push @parents, $1;
335                 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
336                         $co{'author'} = $1;
337                         $co{'author_epoch'} = $2;
338                         $co{'author_tz'} = $3;
339                         $co{'author_name'} = $co{'author'};
340                         $co{'author_name'} =~ s/ <.*//;
341                 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
342                         $co{'committer'} = $1;
343                         $co{'committer_epoch'} = $2;
344                         $co{'committer_tz'} = $3;
345                         $co{'committer_name'} = $co{'committer'};
346                         $co{'committer_name'} =~ s/ <.*//;
347                 }
348         }
349         if (!defined $co{'tree'}) {
350                 close $fd;
351                 return undef
352         };
353         $co{'parents'} = \@parents;
354         $co{'parent'} = $parents[0];
355         my (@comment) = map { chomp; $_ } <$fd>;
356         $co{'comment'} = \@comment;
357         $comment[0] =~ m/^(.{0,50}[^ \/\-_:\.]{0,10})/;
358         $co{'title'} = $1;
359         if ($comment[0] ne $co{'title'}) {
360                 $co{'title'} .= " ...";
361         }
362         close $fd || return;
363
364         my $age = time - $co{'committer_epoch'};
365         $co{'age'} = $age;
366         if ($age > 60*60*24*365*2) {
367                 $co{'age_string'} = (int $age/60/60/24/365);
368                 $co{'age_string'} .= " years ago";
369         } elsif ($age > 60*60*24*(365/12)*2) {
370                 $co{'age_string'} = int $age/60/60/24/(365/12);
371                 $co{'age_string'} .= " months ago";
372         } elsif ($age > 60*60*24*7*2) {
373                 $co{'age_string'} = int $age/60/60/24/7;
374                 $co{'age_string'} .= " weeks ago";
375         } elsif ($age > 60*60*24*2) {
376                 $co{'age_string'} = int $age/60/60/24;
377                 $co{'age_string'} .= " days ago";
378         } elsif ($age > 60*60*2) {
379                 $co{'age_string'} = int $age/60/60;
380                 $co{'age_string'} .= " hours ago";
381         } elsif ($age > 60*2) {
382                 $co{'age_string'} = int $age/60;
383                 $co{'age_string'} .= " min ago";
384         } elsif ($age > 2) {
385                 $co{'age_string'} = int $age;
386                 $co{'age_string'} .= " sec ago";
387         } else {
388                 $co{'age_string'} .= " right now";
389         }
390         return %co;
391 }
392
393 sub git_diff_html {
394         my $from = shift;
395         my $from_name = shift;
396         my $to = shift;
397         my $to_name = shift;
398
399         my $from_tmp = "/dev/null";
400         my $to_tmp = "/dev/null";
401         my $pid = $$;
402
403         # create tmp from-file
404         if (defined $from) {
405                 $from_tmp = "$gittmp/gitweb_" . $$ . "_from";
406                 open my $fd2, "> $from_tmp";
407                 open my $fd, "-|", "$gitbin/git-cat-file blob $from";
408                 my @file = <$fd>;
409                 print $fd2 @file;
410                 close $fd2;
411                 close $fd;
412         }
413
414         # create tmp to-file
415         if (defined $to) {
416                 $to_tmp = "$gittmp/gitweb_" . $$ . "_to";
417                 open my $fd2, "> $to_tmp";
418                 open my $fd, "-|", "$gitbin/git-cat-file blob $to";
419                 my @file = <$fd>;
420                 print $fd2 @file;
421                 close $fd2;
422                 close $fd;
423         }
424
425         open my $fd, "-|", "/usr/bin/diff -u -p -L $from_name -L $to_name $from_tmp $to_tmp";
426         while (my $line = <$fd>) {
427                 chomp($line);
428                 my $char = substr($line, 0, 1);
429                 my $color = "";
430                 if ($char eq '+') {
431                         $color = " style=\"color:#008800;\"";
432                 } elsif ($char eq '-') {
433                         $color = " style=\"color:#cc0000;\"";
434                 } elsif ($char eq '@') {
435                         $color = " style=\"color:#990099;\"";
436                 } elsif ($char eq '\\') {
437                         # skip errors
438                         next;
439                 }
440                 print "<div class=\"pre\"$color>" . escapeHTML($line) . "</div>\n";
441         }
442         close $fd;
443
444         if (defined $from) {
445                 unlink($from_tmp);
446         }
447         if (defined $to) {
448                 unlink($to_tmp);
449         }
450 }
451
452 sub mode_str {
453         my $mode = oct shift;
454
455         if (S_ISDIR($mode & S_IFMT)) {
456                 return 'drwxr-xr-x';
457         } elsif (S_ISLNK($mode)) {
458                 return 'lrwxrwxrwx';
459         } elsif (S_ISREG($mode)) {
460                 # git cares only about the executable bit
461                 if ($mode & S_IXUSR) {
462                         return '-rwxr-xr-x';
463                 } else {
464                         return '-rw-r--r--';
465                 };
466         } else {
467                 return '----------';
468         }
469 }
470
471 sub file_type {
472         my $mode = oct shift;
473
474         if (S_ISDIR($mode & S_IFMT)) {
475                 return "directory";
476         } elsif (S_ISLNK($mode)) {
477                 return "symlink";
478         } elsif (S_ISREG($mode)) {
479                 return "file";
480         } else {
481                 return "unknown";
482         }
483 }
484
485 sub date_str {
486         my $epoch = shift;
487         my $tz = shift || "-0000";
488
489         my %date;
490         my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
491         my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
492         my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
493         $date{'hour'} = $hour;
494         $date{'minute'} = $min;
495         $date{'mday'} = $mday;
496         $date{'day'} = $days[$wday];
497         $date{'month'} = $months[$mon];
498         $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000", $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
499         $date{'mday-time'} = sprintf "%d %s %02d:%02d", $mday, $months[$mon], $hour ,$min;
500
501         $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
502         my $local = $epoch + ((int $1 + ($2/60)) * 3600);
503         ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
504         $date{'hour_local'} = $hour;
505         $date{'minute_local'} = $min;
506         $date{'tz_local'} = $tz;
507         return %date;
508 }
509
510 # git-logo (cached in browser for one day)
511 sub git_logo {
512         print $cgi->header(-type => 'image/png', -expires => '+1d');
513         # cat git-logo.png | hexdump -e '16/1 " %02x"  "\n"' | sed 's/ /\\x/g'
514         print   "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52" .
515                 "\x00\x00\x00\x48\x00\x00\x00\x1b\x04\x03\x00\x00\x00\x2d\xd9\xd4" .
516                 "\x2d\x00\x00\x00\x18\x50\x4c\x54\x45\xff\xff\xff\x60\x60\x5d\xb0" .
517                 "\xaf\xaa\x00\x80\x00\xce\xcd\xc7\xc0\x00\x00\xe8\xe8\xe6\xf7\xf7" .
518                 "\xf6\x95\x0c\xa7\x47\x00\x00\x00\x73\x49\x44\x41\x54\x28\xcf\x63" .
519                 "\x48\x67\x20\x04\x4a\x5c\x18\x0a\x08\x2a\x62\x53\x61\x20\x02\x08" .
520                 "\x0d\x69\x45\xac\xa1\xa1\x01\x30\x0c\x93\x60\x36\x26\x52\x91\xb1" .
521                 "\x01\x11\xd6\xe1\x55\x64\x6c\x6c\xcc\x6c\x6c\x0c\xa2\x0c\x70\x2a" .
522                 "\x62\x06\x2a\xc1\x62\x1d\xb3\x01\x02\x53\xa4\x08\xe8\x00\x03\x18" .
523                 "\x26\x56\x11\xd4\xe1\x20\x97\x1b\xe0\xb4\x0e\x35\x24\x71\x29\x82" .
524                 "\x99\x30\xb8\x93\x0a\x11\xb9\x45\x88\xc1\x8d\xa0\xa2\x44\x21\x06" .
525                 "\x27\x41\x82\x40\x85\xc1\x45\x89\x20\x70\x01\x00\xa4\x3d\x21\xc5" .
526                 "\x12\x1c\x9a\xfe\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82";
527 }
528
529 sub get_file_owner {
530         my $path = shift;
531
532         my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
533         my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
534         if (!defined $gcos) {
535                 return undef;
536         }
537         my $owner = $gcos;
538         $owner =~ s/[,;].*$//;
539         return $owner;
540 }
541
542 sub git_project_list {
543         my @list;
544
545         if (-d $projects_list) {
546                 # search in directory
547                 my $dir = $projects_list;
548                 opendir my $dh, $dir || return undef;
549                 while (my $dir = readdir($dh)) {
550                         if (-e "$projectroot/$dir/HEAD") {
551                                 my $pr = {
552                                         path => $dir,
553                                 };
554                                 push @list, $pr
555                         }
556                 }
557                 closedir($dh);
558         } elsif (-f $projects_list) {
559                 # read from file:
560                 # 'git/git.git:Linus Torvalds'
561                 # 'linux/hotplug/udev.git'
562                 open my $fd , $projects_list || return undef;
563                 while (my $line = <$fd>) {
564                         chomp $line;
565                         my ($path, $owner) = split ' ', $line;
566                         $path = unescape($path);
567                         $owner = unescape($owner);
568                         if (!defined $path) {
569                                 next;
570                         }
571                         if (-e "$projectroot/$path/HEAD") {
572                                 my $pr = {
573                                         path => $path,
574                                         owner => $owner,
575                                 };
576                                 push @list, $pr
577                         }
578                 }
579                 close $fd;
580         }
581
582         if (!@list) {
583                 die_error(undef, "No project found.");
584         }
585         @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
586
587         git_header_html();
588         if (-f $home_text) {
589                 print "<div class=\"index_include\">\n";
590                 open (my $fd, $home_text);
591                 print <$fd>;
592                 close $fd;
593                 print "</div>\n";
594         }
595         print "<div class=\"page_body\"><br/>\n" .
596               "<table cellspacing=\"0\">\n" .
597               "<tr>\n" .
598               "<th>Project</th>\n" .
599               "<th>Description</th>\n" .
600               "<th>Owner</th>\n" .
601               "<th>last change</th>\n" .
602               "<th></th>\n" .
603               "</tr>\n";
604         foreach my $pr (@list) {
605                 my %proj = %$pr;
606                 my $head = git_read_hash("$proj{'path'}/HEAD");
607                 if (!defined $head) {
608                         next;
609                 }
610                 $ENV{'GIT_OBJECT_DIRECTORY'} = "$projectroot/$proj{'path'}/objects";
611                 $ENV{'SHA1_FILE_DIRECTORY'} = "$projectroot/$proj{'path'}/objects";
612                 my %co = git_read_commit($head);
613                 if (!%co) {
614                         next;
615                 }
616                 my $descr = git_read_description($proj{'path'}) || "";
617                 # get directory owner if not already specified
618                 if (!defined $proj{'owner'}) {
619                         $proj{'owner'} = get_file_owner("$projectroot/$proj{'path'}") || "";
620                 }
621                 print "<tr>\n" .
622                       "<td>" . $cgi->a({-href => "$my_uri?p=$proj{'path'};a=summary", -class => "list"}, escapeHTML($proj{'path'})) . "</td>\n" .
623                       "<td>$descr</td>\n" .
624                       "<td><i>$proj{'owner'}</i></td>\n";
625                 my $colored_age;
626                 if ($co{'age'} < 60*60*2) {
627                         $colored_age = "<span style =\"color: #009900;\"><b><i>$co{'age_string'}</i></b></span>";
628                 } elsif ($co{'age'} < 60*60*24*2) {
629                         $colored_age = "<span style =\"color: #009900;\"><i>$co{'age_string'}</i></span>";
630                 } else {
631                         $colored_age = "<i>$co{'age_string'}</i>";
632                 }
633                 print "<td>$colored_age</td>\n" .
634                       "<td class=\"link\">" .
635                       $cgi->a({-href => "$my_uri?p=$proj{'path'};a=summary"}, "summary") .
636                       " | " . $cgi->a({-href => "$my_uri?p=$proj{'path'};a=log"}, "log") .
637                       "</td>\n" .
638                       "</tr>\n";
639         }
640         print "</table>\n" .
641               "<br/>\n" .
642               "</div>\n";
643         git_footer_html();
644 }
645
646 sub git_read_refs {
647         my $ref_dir = shift;
648         my @reflist;
649
650         opendir my $dh, "$projectroot/$project/$ref_dir";
651         my @refs = grep !m/^\./, readdir $dh;
652         closedir($dh);
653         foreach my $ref_file (@refs) {
654                 my $ref_id = git_read_hash("$project/$ref_dir/$ref_file");
655                 my $type = git_get_type($ref_id) || next;
656                 my %ref_item;
657                 my %co;
658                 if ($type eq "tag") {
659                         my %tag = git_read_tag($ref_id);
660                         if ($tag{'type'} eq "commit") {
661                                 %co = git_read_commit($tag{'object'});
662                         }
663                         $ref_item{'type'} = $tag{'type'};
664                         $ref_item{'name'} = $tag{'name'};
665                         $ref_item{'id'} = $tag{'object'};
666                 } elsif ($type eq "commit"){
667                         %co = git_read_commit($ref_id);
668                         $ref_item{'type'} = "commit";
669                         $ref_item{'name'} = $ref_file;
670                         $ref_item{'title'} = $co{'title'};
671                         $ref_item{'id'} = $ref_id;
672                 }
673                 $ref_item{'epoch'} = $co{'committer_epoch'} || 0;
674                 $ref_item{'age'} = $co{'age_string'} || "unknown";
675
676                 push @reflist, \%ref_item;
677         }
678         # sort tags by age
679         @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
680         return \@reflist;
681 }
682
683 sub git_summary {
684         my $descr = git_read_description($project) || "none";
685         my $head = git_read_hash("$project/HEAD");
686         $ENV{'GIT_OBJECT_DIRECTORY'} = "$projectroot/$project/objects";
687         $ENV{'SHA1_FILE_DIRECTORY'} = "$projectroot/$project/objects";
688         my %co = git_read_commit($head);
689         my %cd = date_str($co{'committer_epoch'}, $co{'committer_tz'});
690
691         my $owner;
692         if (-f $projects_list) {
693                 open (my $fd , $projects_list);
694                 while (my $line = <$fd>) {
695                         chomp $line;
696                         my ($pr, $ow) = split ' ', $line;
697                         $pr = unescape($pr);
698                         $ow = unescape($ow);
699                         if ($pr eq $project) {
700                                 $owner = $ow;
701                                 last;
702                         }
703                 }
704                 close $fd;
705         }
706         if (!defined $owner) {
707                 $owner = get_file_owner("$projectroot/$project");
708         }
709
710         git_header_html();
711         print "<div class=\"page_nav\">\n" .
712               $cgi->a({-href => "$my_uri?p=$project;a=log"}, "log") .
713               " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree"}, "tree") .
714               "<br/><br/>\n" .
715               "</div>\n";
716         print "<div class=\"title\">project</div>\n";
717         print "<div class=\"page_body\">\n" .
718               "<table cellspacing=\"0\">\n" .
719               "<tr><td>description</td><td>" . escapeHTML($descr) . "</td></tr>\n" .
720               "<tr><td>owner</td><td>$owner</td></tr>\n" .
721               "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n" .
722               "</table>\n" .
723               "<br/></div>\n";
724         open my $fd, "-|", "$gitbin/git-rev-list --max-count=11 " . git_read_hash("$project/HEAD") || die_error(undef, "Open failed.");
725         my (@revlist) = map { chomp; $_ } <$fd>;
726         close $fd;
727         print "<div>\n" .
728               $cgi->a({-href => "$my_uri?p=$project;a=log", -class => "title"}, "commits") .
729               "</div>\n";
730         my $i = 10;
731         print  "<div class=\"page_body\">\n" .
732                "<table cellspacing=\"0\">\n";
733         foreach my $commit (@revlist) {
734                 my %co = git_read_commit($commit);
735                 my %ad = date_str($co{'author_epoch'});
736                 print "<tr>\n";
737                 if (--$i > 0) {
738                         print "<td><i>$co{'age_string'}</i></td>\n" .
739                               "<td><i>$co{'author_name'}</i></td>\n" .
740                               "<td>" . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit", -class => "list"}, "<b>" . escapeHTML($co{'title'}) . "</b>") . "</td>\n" .
741                               "<td class=\"link\">" .
742                               $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit"}, "commit") .
743                               " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$commit"}, "commitdiff") .
744                               "</td>\n" .
745                               "</tr>";
746                 } else {
747                         print "<td>" . $cgi->a({-href => "$my_uri?p=$project;a=log"}, "...") . "</td>\n" .
748                         "</tr>";
749                         last;
750                 }
751         }
752         print "</table\n>" .
753               "</div>\n";
754
755         my $taglist = git_read_refs("refs/tags");
756         if (defined @$taglist) {
757                 print "<div>\n" .
758                       $cgi->a({-href => "$my_uri?p=$project;a=tags", -class => "title"}, "tags") .
759                       "</div>\n";
760                 my $i = 10;
761                 print  "<div class=\"page_body\">\n" .
762                        "<table cellspacing=\"0\">\n";
763                 foreach my $entry (@$taglist) {
764                         my %tag = %$entry;
765                         print "<tr>\n";
766                         if (--$i > 0) {
767                                 print "<td><i>$tag{'age'}</i></td>\n" .
768                                       "<td>" . $cgi->a({-href => "$my_uri?p=$project;a=$tag{'type'};h=$tag{'id'}", -class => "list"}, "<b>" . escapeHTML($tag{'name'}) . "</b>") . "</td>\n" .
769                                       "<td class=\"link\">" . $cgi->a({-href => "$my_uri?p=$project;a=$tag{'type'};h=$tag{'id'}"}, $tag{'type'}) . "</td>\n" .
770                                       "</tr>";
771                         } else {
772                                 print "<td>" . $cgi->a({-href => "$my_uri?p=$project;a=tags"}, "...") . "</td>\n" .
773                                 "</tr>";
774                                 last;
775                         }
776                 }
777                 print "</table\n>" .
778                       "</div>\n";
779         }
780
781         my $branchlist = git_read_refs("refs/heads");
782         if (defined @$branchlist) {
783                 print "<div>\n" .
784                       $cgi->a({-href => "$my_uri?p=$project;a=branches", -class => "title"}, "branches") .
785                       "</div>\n";
786                 my $i = 10;
787                 print  "<div class=\"page_body\">\n" .
788                        "<table cellspacing=\"0\">\n";
789                 foreach my $entry (@$branchlist) {
790                         my %tag = %$entry;
791                         print "<tr>\n";
792                         if (--$i > 0) {
793                                 print "<td><i>$tag{'age'}</i></td>\n" .
794                                       "<td>" . $cgi->a({-href => "$my_uri?p=$project;a=log;h=$tag{'id'}", -class => "list"}, "<b>" . escapeHTML($tag{'name'}) . "</b>") . "</td>\n" .
795                                       "<td class=\"link\">" . $cgi->a({-href => "$my_uri?p=$project;a=log;h=$tag{'id'}"}, "log") . "</td>\n" .
796                                       "</tr>";
797                         } else {
798                                 print "<td>" . $cgi->a({-href => "$my_uri?p=$project;a=branches"}, "...") . "</td>\n" .
799                                 "</tr>";
800                                 last;
801                         }
802                 }
803                 print "</table\n>" .
804                       "</div>\n";
805         }
806         git_footer_html();
807 }
808
809 sub git_tags {
810         my $head = git_read_hash("$project/HEAD");
811         git_header_html();
812         print "<div class=\"page_nav\">\n" .
813               $cgi->a({-href => "$my_uri?p=$project;a=log"}, "log") .
814               " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$head"}, "commit") .
815               " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree"}, "tree") .
816               "<br/><br/>\n" .
817               "</div>\n";
818         my $taglist = git_read_refs("refs/tags");
819         print "<div>\n" .
820               $cgi->a({-href => "$my_uri?p=$project;a=summary", -class => "title"}, "tags") .
821               "</div>\n";
822         print  "<div class=\"page_body\">\n" .
823                "<table cellspacing=\"0\">\n";
824         if (defined @$taglist) {
825                 foreach my $entry (@$taglist) {
826                         my %tag = %$entry;
827                         print "<tr>\n" .
828                               "<td><i>$tag{'age'}</i></td>\n" .
829                               "<td>" . $cgi->a({-href => "$my_uri?p=$project;a=log;h=$tag{'id'}", -class => "list"}, "<b>" . escapeHTML($tag{'name'}) . "</b>") . "</td>\n" .
830                               "<td class=\"link\">" . $cgi->a({-href => "$my_uri?p=$project;a=$tag{'type'};h=$tag{'id'}"}, $tag{'type'}) . "</td>\n" .
831                               "</tr>";
832                 }
833         }
834         print "</table\n>" .
835               "</div>\n";
836         git_footer_html();
837 }
838
839 sub git_branches {
840         my $head = git_read_hash("$project/HEAD");
841         git_header_html();
842         print "<div class=\"page_nav\">\n" .
843               $cgi->a({-href => "$my_uri?p=$project;a=log"}, "log") .
844               " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$head"}, "commit") .
845               " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree"}, "tree") .
846               "<br/><br/>\n" .
847               "</div>\n";
848         my $taglist = git_read_refs("refs/heads");
849         print "<div>\n" .
850               $cgi->a({-href => "$my_uri?p=$project;a=summary", -class => "title"}, "branches") .
851               "</div>\n";
852         print  "<div class=\"page_body\">\n" .
853                "<table cellspacing=\"0\">\n";
854         if (defined @$taglist) {
855                 foreach my $entry (@$taglist) {
856                         my %tag = %$entry;
857                         print "<tr>\n" .
858                               "<td><i>$tag{'age'}</i></td>\n" .
859                               "<td>" . $cgi->a({-href => "$my_uri?p=$project;a=log;h=$tag{'id'}", -class => "list"}, "<b>" . escapeHTML($tag{'name'}) . "</b>") . "</td>\n" .
860                               "<td class=\"link\">" . $cgi->a({-href => "$my_uri?p=$project;a=log;h=$tag{'id'}"}, "log") . "</td>\n" .
861                               "</tr>";
862                 }
863         }
864         print "</table\n>" .
865               "</div>\n";
866         git_footer_html();
867 }
868
869 sub git_get_hash_by_path {
870         my $base = shift;
871         my $path = shift;
872
873         my $tree = $base;
874         my @parts = split '/', $path;
875         while (my $part = shift @parts) {
876                 open my $fd, "-|", "$gitbin/git-ls-tree $tree" || die_error(undef, "Open git-ls-tree failed.");
877                 my (@entries) = map { chomp; $_ } <$fd>;
878                 close $fd || return undef;
879                 foreach my $line (@entries) {
880                         #'100644        blob    0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa        panic.c'
881                         $line =~ m/^([0-9]+)\t(.*)\t(.*)\t(.*)$/;
882                         my $t_mode = $1;
883                         my $t_type = $2;
884                         my $t_hash = $3;
885                         my $t_name = $4;
886                         if ($t_name eq $part) {
887                                 if (!(@parts)) {
888                                         return $t_hash;
889                                 }
890                                 if ($t_type eq "tree") {
891                                         $tree = $t_hash;
892                                 }
893                                 last;
894                         }
895                 }
896         }
897 }
898
899 sub git_blob {
900         if (!defined $hash && defined $file_name) {
901                 my $base = $hash_base || git_read_hash("$project/HEAD");
902                 $hash = git_get_hash_by_path($base, $file_name, "blob");
903         }
904         open my $fd, "-|", "$gitbin/git-cat-file blob $hash" || die_error(undef, "Open failed.");
905         my $base = $file_name || "";
906         git_header_html();
907         if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
908                 print "<div class=\"page_nav\">\n" .
909                       $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash_base"}, "commit") .
910                       " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash_base"}, "commitdiff") .
911                       " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash_base"}, "tree");
912                 if (defined $file_name) {
913                         print " | " . $cgi->a({-href => "$my_uri?p=$project;a=history;h=$hash_base;f=$file_name"}, "history");
914                 }
915                 print "<br/><br/>\n" .
916                       "</div>\n";
917                 print "<div>" .
918                       $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash_base", -class => "title"}, escapeHTML($co{'title'})) .
919                       "</div>\n";
920         } else {
921                 print "<div class=\"page_nav\">\n" .
922                       "<br/><br/></div>\n" .
923                       "<div class=\"title\">$hash</div>\n";
924         }
925         if (defined $file_name) {
926                 print "<div class=\"page_path\">/$file_name</div>\n";
927         }
928         print "<div class=\"page_body\">\n";
929         my $nr;
930         while (my $line = <$fd>) {
931                 chomp $line;
932                 $nr++;
933                 print "<div class=\"pre\">";
934                 printf "<span style=\"color:#999999;\">%4i</span>", $nr;
935                 print " " .escapeHTML($line) . "</div>\n";
936         }
937         close $fd || print "Reading blob failed.\n";
938         print "</div>";
939         git_footer_html();
940 }
941
942 sub git_tree {
943         if (!defined $hash) {
944                 $hash = git_read_hash("$project/HEAD");
945                 if (defined $file_name) {
946                         my $base = $hash_base || git_read_hash("$project/HEAD");
947                         $hash = git_get_hash_by_path($base, $file_name, "tree");
948                 }
949                 if (!defined $hash_base) {
950                         $hash_base = git_read_hash("$project/HEAD");
951                 }
952         }
953         open my $fd, "-|", "$gitbin/git-ls-tree $hash" || die_error(undef, "Open git-ls-tree failed.");
954         my (@entries) = map { chomp; $_ } <$fd>;
955         close $fd || die_error(undef, "Reading tree failed.");
956
957         git_header_html();
958         my $base_key = "";
959         my $file_key = "";
960         my $base = "";
961         if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
962                 $base_key = ";hb=$hash_base";
963                 print "<div class=\"page_nav\">\n" .
964                       $cgi->a({-href => "$my_uri?p=$project;a=log"}, "log") .
965                       " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash_base"}, "commit") .
966                       " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash_base"}, "commitdiff") .
967                       " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash_base"}, "tree") .
968                       "<br/><br/>\n" .
969                       "</div>\n";
970                 print "<div>\n" .
971                       $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash_base", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
972                       "</div>\n";
973         } else {
974                 print "<div class=\"page_nav\">\n";
975                 print "<br/><br/></div>\n";
976                 print "<div class=\"title\">$hash</div>\n";
977         }
978         if (defined $file_name) {
979                 $base = "$file_name/";
980                 print "<div class=\"page_path\">/$file_name</div>\n";
981         } else {
982                 print "<div class=\"page_path\">/</div>\n";
983         }
984         print "<div class=\"page_body\">\n";
985         print "<table cellspacing=\"0\">\n";
986         foreach my $line (@entries) {
987                 #'100644        blob    0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa        panic.c'
988                 $line =~ m/^([0-9]+)\t(.*)\t(.*)\t(.*)$/;
989                 my $t_mode = $1;
990                 my $t_type = $2;
991                 my $t_hash = $3;
992                 my $t_name = $4;
993                 $file_key = ";f=$base$t_name";
994                 print "<tr>\n" .
995                       "<td style=\"font-family:monospace\">" . mode_str($t_mode) . "</td>\n";
996                 if ($t_type eq "blob") {
997                         print "<td class=\"list\">" .
998                         $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$t_hash" . $base_key . $file_key, -class => "list"}, $t_name) .
999                         "</td>\n";
1000                         print "<td class=\"link\">" .
1001                               $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$t_hash" . $base_key . $file_key}, "blob") .
1002                               " | " . $cgi->a({-href => "$my_uri?p=$project;a=history;h=$hash_base" . $file_key}, "history") .
1003                               "</td>\n";
1004                 } elsif ($t_type eq "tree") {
1005                         print "<td class=\"list\">" .
1006                               $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$t_hash" . $base_key . $file_key}, $t_name) .
1007                               "</td>\n";
1008                 }
1009                 print "</tr>\n";
1010         }
1011         print "</table>\n" .
1012               "</div>";
1013         git_footer_html();
1014 }
1015
1016 sub git_rss {
1017         open my $fd, "-|", "$gitbin/git-rev-list --max-count=20 " . git_read_hash("$project/HEAD") || die_error(undef, "Open failed.");
1018         my (@revlist) = map { chomp; $_ } <$fd>;
1019         close $fd || die_error(undef, "Reading rev-list failed.");
1020
1021         print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
1022         print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".
1023               "<rss version=\"0.91\">\n";
1024         print "<channel>\n";
1025         print "<title>$project</title>\n".
1026               "<link> $my_url/$project/log</link>\n".
1027               "<description>$project log</description>\n".
1028               "<language>en</language>\n";
1029
1030         foreach my $commit (@revlist) {
1031                 my %co = git_read_commit($commit);
1032                 my %ad = date_str($co{'author_epoch'});
1033                 print "<item>\n" .
1034                       "\t<title>" . sprintf("%d %s %02d:%02d", $ad{'mday'}, $ad{'month'}, $ad{'hour'}, $ad{'minute'}) . " - " . escapeHTML($co{'title'}) . "</title>\n" .
1035                       "\t<link> $my_url?p=$project;a=commit;h=$commit</link>\n" .
1036                       "\t<description>";
1037                 my $comment = $co{'comment'};
1038                 foreach my $line (@$comment) {
1039                         print escapeHTML($line) . "<br/>\n";
1040                 }
1041                 print "\t</description>\n" .
1042                       "</item>\n";
1043         }
1044         print "</channel></rss>";
1045 }
1046
1047 sub git_log {
1048         if (!defined $hash) {
1049                 $hash = git_read_hash("$project/HEAD");
1050         }
1051         my $limit_option = "";
1052         if (!defined $time_back) {
1053                 $limit_option = "--max-count=10";
1054         } elsif ($time_back > 0) {
1055                 my $date = time - $time_back*24*60*60;
1056                 $limit_option = "--max-age=$date";
1057         }
1058         open my $fd, "-|", "$gitbin/git-rev-list $limit_option $hash" || die_error(undef, "Open failed.");
1059         my (@revlist) = map { chomp; $_ } <$fd>;
1060         close $fd || die_error(undef, "Reading rev-list failed.");
1061
1062         git_header_html();
1063         print "<div class=\"page_nav\">\n";
1064         print $cgi->a({-href => "$my_uri?p=$project;a=log"}, "last 10") . " | " .
1065               $cgi->a({-href => "$my_uri?p=$project;a=log;t=1"}, "day") . " | " .
1066               $cgi->a({-href => "$my_uri?p=$project;a=log;t=7"}, "week") . " | " .
1067               $cgi->a({-href => "$my_uri?p=$project;a=log;t=31"}, "month") . " | " .
1068               $cgi->a({-href => "$my_uri?p=$project;a=log;t=365"}, "year") . " | " .
1069               $cgi->a({-href => "$my_uri?p=$project;a=log;t=0"}, "all") . "<br/>\n";
1070         print "<br/>\n" .
1071               "</div>\n";
1072
1073         if (!@revlist) {
1074                 my %co = git_read_commit($hash);
1075                 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
1076         }
1077
1078         foreach my $commit (@revlist) {
1079                 my %co = git_read_commit($commit);
1080                 next if !%co;
1081                 my %ad = date_str($co{'author_epoch'});
1082                 print "<div>\n" .
1083                       $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit", -class => "title"},
1084                       "<span class=\"age\">$co{'age_string'}</span>" . escapeHTML($co{'title'})) . "\n" .
1085                       "</div>\n";
1086                 print "<div class=\"title_text\">\n" .
1087                       "<div class=\"log_link\">\n" .
1088                       $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit"}, "commit") .
1089                       " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$commit"}, "commitdiff") .
1090                       "<br/>\n" .
1091                       "</div>\n" .
1092                       "<i>" . escapeHTML($co{'author_name'}) .  " [$ad{'rfc2822'}]</i><br/>\n" .
1093                       "</div>\n" .
1094                       "<div class=\"log_body\">\n";
1095                 my $comment = $co{'comment'};
1096                 my $empty = 0;
1097                 foreach my $line (@$comment) {
1098                         if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1099                                 next;
1100                         }
1101                         if ($line eq "") {
1102                                 if ($empty) {
1103                                         next;
1104                                 }
1105                                 $empty = 1;
1106                         } else {
1107                                 $empty = 0;
1108                         }
1109                         print escapeHTML($line) . "<br/>\n";
1110                 }
1111                 if (!$empty) {
1112                         print "<br/>\n";
1113                 }
1114                 print "</div>\n";
1115         }
1116         git_footer_html();
1117 }
1118
1119 sub git_commit {
1120         my %co = git_read_commit($hash);
1121         if (!%co) {
1122                 die_error(undef, "Unknown commit object.");
1123         }
1124         my %ad = date_str($co{'author_epoch'}, $co{'author_tz'});
1125         my %cd = date_str($co{'committer_epoch'}, $co{'committer_tz'});
1126
1127         my @difftree;
1128         if (defined $co{'parent'}) {
1129                 open my $fd, "-|", "$gitbin/git-diff-tree -r $co{'parent'} $hash" || die_error(undef, "Open failed.");
1130                 @difftree = map { chomp; $_ } <$fd>;
1131                 close $fd || die_error(undef, "Reading diff-tree failed.");
1132         } else {
1133                 # fake git-diff-tree output for initial revision
1134                 open my $fd, "-|", "$gitbin/git-ls-tree -r $hash" || die_error(undef, "Open failed.");
1135                 @difftree = map { chomp;  "+" . $_ } <$fd>;
1136                 close $fd || die_error(undef, "Reading ls-tree failed.");
1137         }
1138         git_header_html();
1139         print "<div class=\"page_nav\">\n" .
1140               $cgi->a({-href => "$my_uri?p=$project;a=log"}, "log") .
1141               " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash"}, "commit");
1142         if (defined $co{'parent'}) {
1143                 print " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash"}, "commitdiff");
1144         }
1145         print " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash"}, "tree") . "\n" .
1146               "<br/><br/></div>\n";
1147         if (defined $co{'parent'}) {
1148                 print "<div>\n" .
1149                       $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
1150                       "</div>\n";
1151         } else {
1152                 print "<div>\n" .
1153                       $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
1154                       "</div>\n";
1155         }
1156         print "<div class=\"title_text\">\n" .
1157               "<table cellspacing=\"0\">\n";
1158         print "<tr><td>author</td><td>" . escapeHTML($co{'author'}) . "</td></tr>\n".
1159               "<tr><td></td><td> $ad{'rfc2822'}";
1160         if ($ad{'hour_local'} < 6) {
1161                 printf(" (<span style=\"color: #cc0000;\">%02d:%02d</span> %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1162         } else {
1163                 printf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1164         }
1165         print "</td></tr>\n";
1166         print "<tr><td>committer</td><td>" . escapeHTML($co{'committer'}) . "</td></tr>\n";
1167         print "<tr><td></td><td> $cd{'rfc2822'}" . sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) . "</td></tr>\n";
1168         print "<tr><td>commit</td><td style=\"font-family:monospace\">$hash</td></tr>\n";
1169         print "<tr><td>tree</td><td style=\"font-family:monospace\">" .
1170               $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash"}, $co{'tree'}) . "</td></tr>\n";
1171         my $parents  = $co{'parents'};
1172         foreach my $par (@$parents) {
1173                 print "<tr><td>parent</td><td style=\"font-family:monospace\">" .
1174                       $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$par"}, $par) . "</td></tr>\n";
1175         }
1176         print "</table>". 
1177               "</div>\n";
1178         print "<div class=\"page_body\">\n";
1179         my $comment = $co{'comment'};
1180         my $empty = 0;
1181         my $signed = 0;
1182         foreach my $line (@$comment) {
1183                 # print only one empty line
1184                 if ($line eq "") {
1185                         if ($empty || $signed) {
1186                                 next;
1187                         }
1188                         $empty = 1;
1189                 } else {
1190                         $empty = 0;
1191                 }
1192                 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1193                         $signed = 1;
1194                         print "<span style=\"color: #888888\">" . escapeHTML($line) . "</span><br/>\n";
1195                 } else {
1196                         $signed = 0;
1197                         print escapeHTML($line) . "<br/>\n";
1198                 }
1199         }
1200         print "</div>\n";
1201         print "<div class=\"list_head\">\n";
1202         if ($#difftree > 10) {
1203                 print(($#difftree + 1) . " files changed:\n");
1204         }
1205         print "</div>\n";
1206         print "<div class=\"page_body\">\n" .
1207               "<table cellspacing=\"0\">\n";
1208         foreach my $line (@difftree) {
1209                 # '*100644->100644      blob    9f91a116d91926df3ba936a80f020a6ab1084d2b->bb90a0c3a91eb52020d0db0e8b4f94d30e02d596      net/ipv4/route.c'
1210                 # '+100644      blob    4a83ab6cd565d21ab0385bac6643826b83c2fcd4        arch/arm/lib/bitops.h'
1211                 # '*100664->100644      blob    b1a8e3dd5556b61dd771d32307c6ee5d7150fa43->b1a8e3dd5556b61dd771d32307c6ee5d7150fa43      show-files.c'
1212                 # '*100664->100644      blob    d08e895238bac36d8220586fdc28c27e1a7a76d3->d08e895238bac36d8220586fdc28c27e1a7a76d3      update-cache.c'
1213                 $line =~ m/^(.)(.*)\t(.*)\t(.*)\t(.*)$/;
1214                 my $op = $1;
1215                 my $mode = $2;
1216                 my $type = $3;
1217                 my $id = $4;
1218                 my $file = $5;
1219                 if ($type ne "blob") {
1220                         next;
1221                 }
1222                 print "<tr>\n";
1223                 if ($op eq "+") {
1224                         my $mode_chng = "";
1225                         if (S_ISREG(oct $mode)) {
1226                                 $mode_chng = sprintf(" with mode: %04o", (oct $mode) & 0777);
1227                         }
1228                         print "<td>" .
1229                               $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$id;hp=$hash;f=$file", -class => "list"}, escapeHTML($file)) . "</td>\n" .
1230                               "<td><span style=\"color: #008000;\">[new " . file_type($mode) . "$mode_chng]</span></td>\n" .
1231                               "<td class=\"link\">" . $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$id;hb=$hash;f=$file"}, "blob") . "</td>\n";
1232                 } elsif ($op eq "-") {
1233                         print "<td>" .
1234                               $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$id;hb=$hash;f=$file", -class => "list"}, escapeHTML($file)) . "</td>\n" .
1235                               "<td><span style=\"color: #c00000;\">[deleted " . file_type($mode). "]</span></td>\n" .
1236                               "<td class=\"link\">" .
1237                               $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$id;hb=$hash;f=$file"}, "blob") .
1238                               " | " . $cgi->a({-href => "$my_uri?p=$project;a=history;h=$hash;f=$file"}, "history") .
1239                               "</td>\n"
1240                 } elsif ($op eq "*") {
1241                         $id =~ m/([0-9a-fA-F]+)->([0-9a-fA-F]+)/;
1242                         my $from_id = $1;
1243                         my $to_id = $2;
1244                         $mode =~ m/^([0-7]{6})->([0-7]{6})$/;
1245                         my $from_mode = $1;
1246                         my $to_mode = $2;
1247                         my $mode_chnge = "";
1248                         if ($from_mode != $to_mode) {
1249                                 $mode_chnge = " <span style=\"color: #777777;\">[changed";
1250                                 if (((oct $from_mode) & S_IFMT) != ((oct $to_mode) & S_IFMT)) {
1251                                         $mode_chnge .= " from " . file_type($from_mode) . " to " . file_type($to_mode);
1252                                 }
1253                                 if (((oct $from_mode) & 0777) != ((oct $to_mode) & 0777)) {
1254                                         if (S_ISREG($from_mode) && S_ISREG($to_mode)) {
1255                                                 $mode_chnge .= sprintf(" mode: %04o->%04o", (oct $from_mode) & 0777, (oct $to_mode) & 0777);
1256                                         } elsif (S_ISREG($to_mode)) {
1257                                                 $mode_chnge .= sprintf(" mode: %04o", (oct $to_mode) & 0777);
1258                                         }
1259                                 }
1260                                 $mode_chnge .= "]</span>\n";
1261                         }
1262                         print "<td>";
1263                         if ($to_id ne $from_id) {
1264                                 print $cgi->a({-href => "$my_uri?p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$file", -class => "list"}, escapeHTML($file));
1265                         } else {
1266                                 print $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$to_id;hb=$hash;f=$file", -class => "list"}, escapeHTML($file));
1267                         }
1268                         print "</td>\n" .
1269                               "<td>$mode_chnge</td>\n" .
1270                               "<td class=\"link\">";
1271                         print $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$to_id;hb=$hash;f=$file"}, "blob");
1272                         if ($to_id ne $from_id) {
1273                                 print " | " . $cgi->a({-href => "$my_uri?p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$file"}, "diff");
1274                         }
1275                         print " | " . $cgi->a({-href => "$my_uri?p=$project;a=history;h=$hash;f=$file"}, "history") . "\n";
1276                         print "</td>\n";
1277                 }
1278                 print "</tr>\n";
1279         }
1280         print "</table><br/>\n" .
1281               "</div>\n";
1282         git_footer_html();
1283 }
1284
1285 sub git_blobdiff {
1286         mkdir($gittmp, 0700);
1287         git_header_html();
1288         if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
1289                 print "<div class=\"page_nav\">\n" .
1290                       $cgi->a({-href => "$my_uri?p=$project;a=log"}, "log") .
1291                       " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash_base"}, "commit") .
1292                       " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash_base"}, "commitdiff") .
1293                       " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash_base"}, "tree");
1294                         if (defined $file_name) {
1295                                 print " | " . $cgi->a({-href => "$my_uri?p=$project;a=history;h=$hash_base;f=$file_name"}, "history");
1296                         }
1297                 print "<br/><br/>\n" .
1298                       "</div>\n";
1299                 print "<div>\n" .
1300                       $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash_base", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
1301                       "</div>\n";
1302         } else {
1303                 print "<div class=\"page_nav\">\n" .
1304                       "<br/><br/></div>\n" .
1305                       "<div class=\"title\">$hash vs $hash_parent</div>\n";
1306         }
1307         if (defined $file_name) {
1308                 print "<div class=\"page_path\">\n" .
1309                       "/$file_name\n" .
1310                       "</div>\n";
1311         }
1312         print "<div class=\"page_body\">\n" .
1313               "<div class=\"diff_info\">blob:" .
1314               $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$hash_parent;hb=$hash_base;f=$file_name"}, $hash_parent) .
1315               " -> blob:" .
1316               $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$hash;hb=$hash_base;f=$file_name"}, $hash) .
1317               "</div>\n";
1318         git_diff_html($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash);
1319         print "</div>";
1320         git_footer_html();
1321 }
1322
1323 sub git_commitdiff {
1324         mkdir($gittmp, 0700);
1325         my %co = git_read_commit($hash);
1326         if (!%co) {
1327                 die_error(undef, "Unknown commit object.");
1328         }
1329         open my $fd, "-|", "$gitbin/git-diff-tree -r $co{'parent'} $hash" || die_error(undef, "Open failed.");
1330         my (@difftree) = map { chomp; $_ } <$fd>;
1331         close $fd || die_error(undef, "Reading diff-tree failed.");
1332
1333         git_header_html();
1334         print "<div class=\"page_nav\">\n" .
1335               $cgi->a({-href => "$my_uri?p=$project;a=log"}, "log") .
1336               " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash"}, "commit") .
1337               " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash"}, "commitdiff") .
1338               " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=" .  $co{'tree'} . ";hb=$hash"}, "tree") .
1339               "<br/><br/></div>\n";
1340         print "<div>\n" .
1341               $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
1342               "</div>\n";
1343         print "<div class=\"page_body\">\n";
1344         my $comment = $co{'comment'};
1345         my $empty = 0;
1346         my $signed = 0;
1347         my @log = @$comment;
1348         # remove first and empty lines after that
1349         shift @log;
1350         while (defined $log[0] && $log[0] eq "") {
1351                 shift @log;
1352         }
1353         foreach my $line (@log) {
1354                 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1355                         next;
1356                 }
1357                 if ($line eq "") {
1358                         if ($empty) {
1359                                 next;
1360                         }
1361                         $empty = 1;
1362                 } else {
1363                         $empty = 0;
1364                 }
1365                 print escapeHTML($line) . "<br/>\n";
1366         }
1367         print "<br/>\n";
1368         foreach my $line (@difftree) {
1369                 # '*100644->100644      blob    8e5f9bbdf4de94a1bc4b4da8cb06677ce0a57716->8da3a306d0c0c070d87048d14a033df02f40a154      Makefile'
1370                 $line =~ m/^(.)(.*)\t(.*)\t(.*)\t(.*)$/;
1371                 my $op = $1;
1372                 my $mode = $2;
1373                 my $type = $3;
1374                 my $id = $4;
1375                 my $file = $5;
1376                 if ($type eq "blob") {
1377                         if ($op eq "+") {
1378                                 print "<div class=\"diff_info\">" .  file_type($mode) . ":" .
1379                                       $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$id;hb=$hash;f=$file"}, $id) . "(new)" .
1380                                       "</div>\n";
1381                                 git_diff_html(undef, "/dev/null", $id, "b/$file");
1382                         } elsif ($op eq "-") {
1383                                 print "<div class=\"diff_info\">" . file_type($mode) . ":" .
1384                                       $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$id;hb=$hash;f=$file"}, $id) . "(deleted)" .
1385                                       "</div>\n";
1386                                 git_diff_html($id, "a/$file", undef, "/dev/null");
1387                         } elsif ($op eq "*") {
1388                                 $id =~ m/([0-9a-fA-F]+)->([0-9a-fA-F]+)/;
1389                                 my $from_id = $1;
1390                                 my $to_id = $2;
1391                                 $mode =~ m/([0-7]+)->([0-7]+)/;
1392                                 my $from_mode = $1;
1393                                 my $to_mode = $2;
1394                                 if ($from_id ne $to_id) {
1395                                         print "<div class=\"diff_info\">" .
1396                                               file_type($from_mode) . ":" . $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$from_id;hb=$hash;f=$file"}, $from_id) .
1397                                               " -> " .
1398                                               file_type($to_mode) . ":" . $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$to_id;hb=$hash;f=$file"}, $to_id);
1399                                         print "</div>\n";
1400                                         git_diff_html($from_id, "a/$file",  $to_id, "b/$file");
1401                                 }
1402                         }
1403                 }
1404         }
1405         print "<br/>\n" .
1406               "</div>";
1407         git_footer_html();
1408 }
1409
1410 sub git_history {
1411         if (!defined $hash) {
1412                 $hash = git_read_hash("$project/HEAD");
1413         }
1414         my %co = git_read_commit($hash);
1415         if (!%co) {
1416                 die_error(undef, "Unknown commit object.");
1417         }
1418         git_header_html();
1419         print "<div class=\"page_nav\">\n" .
1420               $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash"}, "commit") . " | " .
1421               $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash"}, "commitdiff") . " | " .
1422               $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash"}, "tree") .
1423               "<br/><br/>\n" .
1424               "</div>\n";
1425         print "<div>\n" .
1426               $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
1427               "</div>\n";
1428         print "<div class=\"page_path\">\n" .
1429               "/$file_name<br/>\n";
1430         print "</div>\n";
1431
1432         open my $fd, "-|", "$gitbin/git-rev-list $hash | $gitbin/git-diff-tree -r --stdin $file_name";
1433         my $commit;
1434         print "<div class=\"page_body\">\n" .
1435               "<table cellspacing=\"0\">\n";
1436         while (my $line = <$fd>) {
1437                 if ($line =~ m/^([0-9a-fA-F]{40}) /){
1438                         $commit = $1;
1439                         next;
1440                 }
1441                 if ($line =~ m/^(.)(.*)\t(.*)\t(.*)\t(.*)$/ && (defined $commit)) {
1442                         my $type = $3;
1443                         my $file = $5;
1444                         if ($file ne $file_name || $type ne "blob") {
1445                                 next;
1446                         }
1447                         my %co = git_read_commit($commit);
1448                         if (!%co) {
1449                                 next;
1450                         }
1451                         print "<tr>" .
1452                               "<td><i>$co{'age_string'}</i></td>\n" .
1453                               "<td><i>$co{'author_name'}</i></td>\n" .
1454                               "<td>" . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit", -class => "list"}, "<b>" . escapeHTML($co{'title'}) . "</b>") . "</td>\n" .
1455                               "<td class=\"link\">" .
1456                               $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit"}, "commit") .
1457                               " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=" .  $co{'tree'} . ";hb=$commit"}, "tree") .
1458                               " | " . $cgi->a({-href => "$my_uri?p=$project;a=blob;hb=$commit;f=$file"}, "blob");
1459                         my $blob = git_get_hash_by_path($hash, $file_name);
1460                         my $blob_parent = git_get_hash_by_path($commit, $file_name);
1461                         if (defined $blob && defined $blob_parent && $blob ne $blob_parent) {
1462                                 print " | " . $cgi->a({-href => "$my_uri?p=$project;a=blobdiff;h=$blob;hp=$blob_parent;hb=$commit;f=$file"}, "diff");
1463                         }
1464                         print "</td>\n" .
1465                               "</tr>\n";
1466                         undef $commit;
1467                 }
1468         }
1469         print "</table><br/>\n" .
1470               "</div>\n";
1471         close $fd;
1472         git_footer_html();
1473 }