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