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