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