v085
[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 file 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
15 my $cgi = new CGI;
16
17 my $version =           "085";
18 my $projectroot =       "/pub/scm";
19 my $home_link =         "/git";
20 my $gitbin =            "/usr/bin";
21 my $gittmp =            "/tmp/gitweb";
22 my $logo_link =         "/pub/software/scm/cogito";
23 my $my_url =            $cgi->url();
24 my $my_uri =            $cgi->url(-absolute => 1);
25 my $rss_link = "";
26
27 my $project = $cgi->param('p');
28 if (defined($project)) {
29         if ($project =~ /(^|\/)(|\.|\.\.)($|\/)/) {
30                 $project = "";
31                 die_error("", "Invalid project parameter.");
32         }
33         if (!(-d "$projectroot/$project")) {
34                 $project = "";
35                 die_error("", "No such project.");
36         }
37         $rss_link = "<link rel=\"alternate\" title=\"$project log\" href=\"$my_uri?p=$project;a=rss\" type=\"application/rss+xml\"/>";
38 }
39
40 my $file_name = $cgi->param('f');
41 if (defined($file_name) && $file_name =~ /(^|\/)(|\.|\.\.)($|\/)/) {
42         $file_name = "";
43         die_error("", "Invalid file parameter.");
44 }
45
46 my $action = $cgi->param('a');
47 if (defined($action) && $action =~ m/[^0-9a-zA-Z\.\-]+$/) {
48         $action = "";
49         die_error("", "Invalid action parameter.");
50 }
51
52 my $hash = $cgi->param('h');
53 if (defined($hash) && !($hash =~ m/^[0-9a-fA-F]{40}$/)) {
54         $hash = "";
55         die_error("", "Invalid hash parameter.");
56 }
57
58 my $hash_parent = $cgi->param('hp');
59 if (defined($hash_parent) && !($hash_parent =~ m/^[0-9a-fA-F]{40}$/)) {
60         $hash_parent = "";
61         die_error("", "Invalid parent hash parameter.");
62 }
63
64 my $time_back = $cgi->param('t');
65 if (defined($time_back) && !($time_back =~ m/^[0-9]+$/)) {
66         $time_back = "";
67         die_error("", "Invalid time parameter.");
68 }
69
70 $ENV{'SHA1_FILE_DIRECTORY'} = "$projectroot/$project/objects";
71 mkdir($gittmp, 0700);
72
73 sub git_header_html {
74         my $status = shift || "200 OK";
75
76         print $cgi->header(-type=>'text/html',  -charset => 'utf-8', -status=> $status);
77         print <<EOF;
78 <?xml version="1.0" encoding="utf-8"?>
79 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
80 <!-- git web interface v$version, (C) 2005, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke <ch\@gierke.de> -->
81 <html>
82 <head>
83 <title>git - $project</title>
84 $rss_link
85 <style type="text/css">
86 body { font-family: sans-serif; font-size: 12px; margin:0px; }
87 a { color:#0000cc; }
88 a:hover { color:#880000; }
89 a:visited { color:#880000; }
90 a:active { color:#880000; }
91 div.page_header {
92         margin:15px 25px 0px; height:25px; padding:8px;
93         font-size:18px; font-weight:bold; background-color:#d9d8d1;
94 }
95 div.page_header a:visited { color:#0000cc; }
96 div.page_header a:hover { color:#880000; }
97 div.page_nav { margin:0px 25px; padding:8px; border:solid #d9d8d1; border-width:0px 1px; }
98 div.page_nav a:visited { color:#0000cc; }
99 div.page_footer { margin:0px 25px 15px; height:17px; padding:4px; padding-left:8px; background-color: #d9d8d1; }
100 div.page_footer_text { float:left; color:#555555; font-style:italic; }
101 div.page_body { margin:0px 25px; padding:8px; border:solid #d9d8d1; border-width:0px 1px; }
102 div.title, a.title {
103         display:block; margin:0px 25px; padding:6px 8px;
104         font-weight:bold; background-color:#edece6; text-decoration:none; color:#000000;
105 }
106 a.title:hover { background-color: #d9d8d1; }
107 div.title_text { margin:0px 25px; padding:6px 8px; border: solid #d9d8d1; border-width:0px 1px 1px; }
108 div.log_body { margin:0px 25px; padding:8px; padding-left:150px; border:solid #d9d8d1; border-width:0px 1px; }
109 span.log_age { position:relative; float:left; width:142px; font-style:italic; }
110 div.log_link { font-size:10px; font-family:sans-serif; font-style:normal; position:relative; float:left; width:142px; }
111 div.list {
112         display:block; margin:0px 25px; padding:4px 6px 2px; border:solid #d9d8d1; border-width:1px 1px 0px;
113         font-weight:bold;
114 }
115 div.list_head {
116         display:block; margin:0px 25px; padding:4px 6px 4px; border:solid #d9d8d1; border-width:1px 1px 0px;
117         font-style:italic;
118 }
119 div.list a { text-decoration:none; color:#000000; }
120 div.list a:hover { color:#880000; }
121 div.link {
122         margin:0px 25px; padding:0px 6px 8px; border:solid #d9d8d1; border-width:0px 1px;
123         font-family:sans-serif; font-size:10px;
124 }
125 td.key { padding-right:10px;  }
126 a.xml_logo { float:right; border:1px solid;
127         line-height:15px;
128         border-color:#fcc7a5 #7d3302 #3e1a01 #ff954e; width:35px;
129         color:#ffffff; background-color:#ff6600;
130         font-weight:bold; font-family:sans-serif; text-align:center;
131         font-size:10px; display:block; text-decoration:none;
132 }
133 a.xml_logo:hover { background-color:#ee5500; }
134 </style>
135 </head>
136 <body>
137 EOF
138         print "<div class=\"page_header\">\n" .
139               "<a href=\"$logo_link\">" .
140               "<img src=\"$my_uri?a=git-logo.png\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/></a>";
141         print $cgi->a({-href => "$my_uri"}, "projects") . " / ";
142         if ($project ne "") {
143                 print $cgi->a({-href => "$my_uri?p=$project;a=log"}, escapeHTML($project));
144         }
145         if ($action ne "") {
146                 print " / $action";
147         }
148         print "</div>\n";
149 }
150
151 sub git_footer_html {
152         print "<div class=\"page_footer\">\n";
153         if ($project ne "") {
154                 if (-e "$projectroot/$project/description") {
155                         open(my $fd, "$projectroot/$project/description");
156                         my $descr = <$fd>;
157                         print "<div class=\"page_footer_text\">" . escapeHTML($descr) . "</div>\n";
158                         close $fd;
159                 }
160                 print $cgi->a({-href => "$my_uri?p=$project;a=rss", -class => "xml_logo"}, "XML") . "\n";
161         }
162         print "</div>\n" .
163               "</body>\n" .
164               "</html>";
165 }
166
167 sub die_error {
168         my $status = shift || "403 Forbidden";
169         my $error = shift || "Malformed query, file missing or permission denied"; 
170
171         git_header_html($status);
172         print "<div class=\"page_body\">\n" .
173               "<br/><br/>\n";
174         print "$status - $error\n";
175         print "<br/></div>\n";
176         git_footer_html();
177         exit 0;
178 }
179
180 sub git_head {
181         my $path = shift;
182         open(my $fd, "$projectroot/$path/HEAD") || die_error("", "Invalid project directory.");
183         my $head = <$fd>;
184         close $fd;
185         chomp $head;
186         return $head;
187 }
188
189 sub git_commit {
190         my $commit = shift;
191         my %co;
192         my @parents;
193
194         open my $fd, "-|", "$gitbin/cat-file commit $commit";
195         while (my $line = <$fd>) {
196                 chomp($line);
197                 last if $line eq "";
198                 if ($line =~ m/^tree (.*)$/) {
199                         $co{'tree'} = $1;
200                 } elsif ($line =~ m/^parent (.*)$/) {
201                         push @parents, $1;
202                 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
203                         $co{'author'} = $1;
204                         $co{'author_epoch'} = $2;
205                         $co{'author_tz'} = $3;
206                         $co{'author_name'} = $co{'author'};
207                         $co{'author_name'} =~ s/ <.*//;
208                 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
209                         $co{'committer'} = $1;
210                         $co{'committer_epoch'} = $2;
211                         $co{'committer_tz'} = $3;
212                         $co{'committer_name'} = $co{'committer'};
213                         $co{'committer_name'} =~ s/ <.*//;
214                 }
215         }
216         if (!defined($co{'tree'})) {
217                 return;
218         }
219         $co{'parents'} = \@parents;
220         $co{'parent'} = $parents[0];
221         my (@comment) = map { chomp; $_ } <$fd>;
222         $co{'comment'} = \@comment;
223         $co{'title'} = $comment[0];
224         close $fd;
225
226         my $age = time - $co{'committer_epoch'};
227         $co{'age'} = $age;
228         if ($age > 60*60*24*365*2) {
229                 $co{'age_string'} = (int $age/60/60/24/365);
230                 $co{'age_string'} .= " years ago";
231         } elsif ($age > 60*60*24*365/12*2) {
232                 $co{'age_string'} = int $age/60/60/24/365/12;
233                 $co{'age_string'} .= " months ago";
234         } elsif ($age > 60*60*24*7*2) {
235                 $co{'age_string'} = int $age/60/60/24/7;
236                 $co{'age_string'} .= " weeks ago";
237         } elsif ($age > 60*60*24*2) {
238                 $co{'age_string'} = int $age/60/60/24;
239                 $co{'age_string'} .= " days ago";
240         } elsif ($age > 60*60*2) {
241                 $co{'age_string'} = int $age/60/60;
242                 $co{'age_string'} .= " hours ago";
243         } elsif ($age > 60*2) {
244                 $co{'age_string'} = int $age/60;
245                 $co{'age_string'} .= " minutes ago";
246         }
247         return %co;
248 }
249
250 sub git_diff_html {
251         my $from_name = shift || "/dev/null";
252         my $to_name = shift || "/dev/null";
253         my $from = shift;
254         my $to = shift;
255
256         my $from_tmp = "/dev/null";
257         my $to_tmp = "/dev/null";
258         my $from_label = "/dev/null";
259         my $to_label = "/dev/null";
260         my $pid = $$;
261
262         # create tmp from-file
263         if ($from ne "") {
264                 $from_tmp = "$gittmp/gitweb_" . $$ . "_from";
265                 open(my $fd2, "> $from_tmp");
266                 open my $fd, "-|", "$gitbin/cat-file blob $from";
267                 my @file = <$fd>;
268                 print $fd2 @file;
269                 close $fd2;
270                 close $fd;
271                 $from_label = "a/$from_name";
272         }
273
274         # create tmp to-file
275         if ($to ne "") {
276                 $to_tmp = "$gittmp/gitweb_" . $$ . "_to";
277                 open my $fd2, "> $to_tmp";
278                 open my $fd, "-|", "$gitbin/cat-file blob $to";
279                 my @file = <$fd>;
280                 print $fd2 @file;
281                 close $fd2;
282                 close $fd;
283                 $to_label = "b/$to_name";
284         }
285
286         open my $fd, "-|", "/usr/bin/diff -u -p -L $from_label -L $to_label $from_tmp $to_tmp";
287         print "<span style=\"color: #000099;\">===== ";
288         if ($from ne "") {
289                 print $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$from"}, $from);
290         } else {
291                 print $from_name;
292         }
293         print " vs ";
294         if ($to ne "") {
295                 print $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$to"}, $to);
296         } else {
297                 print $to_name;
298         }
299         print " =====</span>\n";
300         while (my $line = <$fd>) {
301                 my $char = substr($line,0,1);
302                 print '<span style="color: #008800;">' if $char eq '+';
303                 print '<span style="color: #CC0000;">' if $char eq '-';
304                 print '<span style="color: #990099;">' if $char eq '@';
305                 print escapeHTML($line);
306                 print '</span>' if $char eq '+' or $char eq '-' or $char eq '@';
307         }
308         close $fd;
309
310         if ($from ne "") {
311                 unlink("$from_tmp");
312         }
313         if ($to ne "") {
314                 unlink("$to_tmp");
315         }
316 }
317
318 sub mode_str {
319         my $perms = oct shift;
320         my $modestr;
321         if ($perms & 040000) {
322                 $modestr .= 'drwxr-xr-x';
323         } else {
324                 # git cares only about the executable bit
325                 if ($perms & 0100) {
326                         $modestr .= '-rwxr-xr-x';
327                 } else {
328                         $modestr .= '-rw-r--r--';
329                 };
330         }
331         return $modestr;
332 }
333
334 sub date_str {
335         my $epoch = shift;
336         my $tz = shift || "-0000";
337
338         my %date;
339         my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
340         my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
341         my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
342         $date{'hour'} = $hour;
343         $date{'minute'} = $min;
344         $date{'mday'} = $mday;
345         $date{'day'} = $days[$wday];
346         $date{'month'} = $months[$mon];
347         $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000", $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
348         $date{'mday-time'} = sprintf "%d %s %02d:%02d", $mday, $months[$mon], $hour ,$min;
349
350         $tz =~ m/((-|\+)[0-9][0-9])([0-9][0-9])/;
351         my $local = $epoch + (($1 + ($2/60)) * 3600);
352         ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
353         $date{'hour_local'} = $hour;
354         $date{'minute_local'} = $min;
355         $date{'tz_local'} = $tz;
356         return %date;
357 }
358
359 if ($action eq "git-logo.png") {
360         print $cgi->header(-type => 'image/png', -expires => '+1d');
361         print   "\211\120\116\107\015\012\032\012\000\000\000\015\111\110\104\122".
362                 "\000\000\000\110\000\000\000\033\004\003\000\000\000\055\331\324".
363                 "\055\000\000\000\030\120\114\124\105\377\377\377\140\140\135\260".
364                 "\257\252\000\200\000\316\315\307\300\000\000\350\350\346\367\367".
365                 "\366\225\014\247\107\000\000\000\163\111\104\101\124\050\317\143".
366                 "\110\147\040\004\112\134\030\012\010\052\142\123\141\040\002\010".
367                 "\015\151\105\254\241\241\001\060\014\223\140\066\046\122\221\261".
368                 "\001\021\326\341\125\144\154\154\314\154\154\014\242\014\160\052".
369                 "\142\006\052\301\142\035\263\001\002\123\244\010\350\000\003\030".
370                 "\046\126\021\324\341\040\227\033\340\264\016\065\044\161\051\202".
371                 "\231\060\270\223\012\021\271\105\210\301\215\240\242\104\041\006".
372                 "\047\101\202\100\205\301\105\211\040\160\001\000\244\075\041\305".
373                 "\022\034\232\376\000\000\000\000\111\105\116\104\256\102\140\202";
374         exit;
375 }
376
377 if (!defined($project)) {
378         print $cgi->redirect($home_link);
379         exit;
380 }
381
382 if (!defined($action)) {
383         $action = "log";
384 }
385
386 if (!defined($time_back)) {
387         $time_back = 1;
388 }
389
390 if ($action eq "blob") {
391         git_header_html();
392         print "<div class=\"page_nav\">\n";
393         print "<br/><br/></div>\n";
394         print "<div class=\"title\">$hash</div>\n";
395         print "<div class=\"page_body\"><pre>\n";
396         open(my $fd, "-|", "$gitbin/cat-file blob $hash");
397         my $nr;
398         while (my $line = <$fd>) {
399                 $nr++;
400                 printf "<span style =\"color: #999999;\">%4i\t</span>%s", $nr, escapeHTML($line);;
401         }
402         close $fd;
403         print "<br/></pre>\n";
404         print "</div>";
405         git_footer_html();
406 } elsif ($action eq "tree") {
407         if ($hash eq "") {
408                 $hash = git_head($project);
409         }
410         open my $fd, "-|", "$gitbin/ls-tree $hash";
411         my (@entries) = map { chomp; $_ } <$fd>;
412         close $fd;
413
414         git_header_html();
415         my %co = git_commit($hash);
416         if (defined(%co)) {
417                 print "<div class=\"page_nav\"> view\n" .
418                       $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash"}, "commit") . " | " .
419                       $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash"}, "diffs") . " | " .
420                       $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$hash"}, "tree") .
421                       "<br/><br/>\n" .
422                       "</div>\n";
423                 print "<div>\n" .
424                       $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
425                       "</div>\n";
426         } else {
427                 print "<div class=\"page_nav\">\n";
428                 print "<br/><br/></div>\n";
429                 print "<div class=\"title\">$hash</div>\n";
430         }
431         print "<div class=\"page_body\">\n";
432         print "<br/><pre>\n";
433         foreach my $line (@entries) {
434                 #'100644        blob    0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa        panic.c'
435                 $line =~ m/^([0-9]+)\t(.*)\t(.*)\t(.*)$/;
436                 my $t_mode = $1;
437                 my $t_type = $2;
438                 my $t_hash = $3;
439                 my $t_name = $4;
440                 if ($t_type eq "blob") {
441                         print mode_str($t_mode). " " . $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$t_hash"}, $t_name) . "\n";
442                 } elsif ($t_type eq "tree") {
443                         print mode_str($t_mode). " " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$t_hash"}, $t_name) . "\n";
444                 }
445         }
446         print "</pre>\n";
447         print "<br/></div>";
448         git_footer_html();
449 } elsif ($action eq "log" || $action eq "rss") {
450         open my $fd, "-|", "$gitbin/rev-list " . git_head($project);
451         my (@revlist) = map { chomp; $_ } <$fd>;
452         close $fd;
453
454         if ($action eq "log") {
455                 git_header_html();
456                 print "<div class=\"page_nav\">\n";
457                 print "view  ";
458                 print $cgi->a({-href => "$my_uri?p=$project;a=log"}, "last day") . " | " .
459                       $cgi->a({-href => "$my_uri?p=$project;a=log;t=7"}, "week") . " | " .
460                       $cgi->a({-href => "$my_uri?p=$project;a=log;t=31"}, "month") . " | " .
461                       $cgi->a({-href => "$my_uri?p=$project;a=log;t=365"}, "year") . " | " .
462                       $cgi->a({-href => "$my_uri?p=$project;a=log;t=0"}, "all") . "<br/>\n";
463                 print "<br/><br/>\n" .
464                       "</div>\n";
465         } elsif ($action eq "rss") {
466                 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
467                 print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".
468                       "<rss version=\"0.91\">\n";
469                 print "<channel>\n";
470                 print "<title>$project</title>\n".
471                       "<link> " . $my_url . "/$project/log</link>\n".
472                       "<description>$project log</description>\n".
473                       "<language>en</language>\n";
474         }
475
476         for (my $i = 0; $i <= $#revlist; $i++) {
477                 my $commit = $revlist[$i];
478                 my %co = git_commit($commit);
479                 my %ad = date_str($co{'author_epoch'});
480                 if ($action eq "log") {
481                 if ($time_back > 0 && $co{'age'} > $time_back*60*60*24) {
482                                 if ($i == 0) {
483                                         print "<div class=\"page_body\"> Last change " . $co{'age_string'} . ".<br/><br/></div>\n";
484                                 }
485                                 last;
486                         }
487                         print "<div>\n" .
488                               $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit", -class => "title"}, 
489                               "<span class=\"log_age\">" . $co{'age_string'} . "</span>" . escapeHTML($co{'title'})) . "\n" .
490                               "</div>\n";
491                         print "<div class=\"title_text\">\n" .
492                               "<div class=\"log_link\">\n" .
493                               "view " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit"}, "commit") . " | " .
494                               $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$commit"}, "diff") . "<br/>\n" .
495                               "</div>\n" .
496                               "<i>" . escapeHTML($co{'author_name'}) .  " [" . $ad{'rfc2822'} . "]</i><br/>\n" .
497                               "</div>\n" .
498                               "<div class=\"log_body\">\n";
499                         my $comment = $co{'comment'};
500                         foreach my $line (@$comment) {
501                                 last if ($line =~ m/^(signed-off|acked)-by:/i);
502                                 print escapeHTML($line) . "<br/>\n";
503                         }
504                         print "<br/>\n" .
505                               "</div>\n";
506                 } elsif ($action eq "rss") {
507                         last if ($i >= 20);
508                         print "<item>\n" .
509                               "\t<title>" . sprintf("%d %s %02d:%02d", $ad{'mday'}, $ad{'month'}, $ad{'hour'}, $ad{'min'}) . " - " . escapeHTML($co{'title'}) . "</title>\n" .
510                               "\t<link> " . $my_url . "?p=$project;a=commit;h=$commit</link>\n" .
511                               "\t<description>";
512                         my $comment = $co{'comment'};
513                         foreach my $line (@$comment) {
514                                 print escapeHTML($line) . "\n";
515                         }
516                         print "\t</description>\n" .
517                               "</item>\n";
518                 }
519         }
520         if ($action eq "log") {
521                 git_footer_html();
522         } elsif ($action eq "rss") {
523                 print "</channel></rss>";
524         }
525 } elsif ($action eq "commit") {
526         my %co = git_commit($hash);
527         if (!defined(%co)) {
528                 die_error("", "Unknown commit object.");
529         }
530         my %ad = date_str($co{'author_epoch'}, $co{'author_tz'});
531         my %cd = date_str($co{'committer_epoch'}, $co{'committer_tz'});
532
533         my @difftree;
534         if (defined($co{'parent'})) {
535                 open my $fd, "-|", "$gitbin/diff-tree -r " . $co{'parent'} . " $hash";
536                 @difftree = map { chomp; $_ } <$fd>;
537                 close $fd;
538         } else {
539                 # fake git-diff-tree output for initial revision
540                 open my $fd, "-|", "$gitbin/ls-tree -r $hash";
541                 @difftree = map { chomp;  "+" . $_ } <$fd>;
542                 close $fd;
543         }
544         git_header_html();
545         print "<div class=\"page_nav\"> view\n" .
546               $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash"}, "commit") . " | \n" .
547               $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash"}, "diffs") . " | \n" .
548               $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$hash"}, "tree") . "\n" .
549               "<br/><br/></div>\n";
550         print "<div>\n" .
551               $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
552               "</div>\n";
553         print "<div class=\"title_text\">\n" .
554               "<table cellspacing=\"0\">";
555         print "<tr><td class=\"key\">author</td><td>" . escapeHTML($co{'author'}) . "</td><tr><td></td><td>" .
556               " " . $ad{'rfc2822'};
557         if ($ad{'hour_local'} < 6) {
558                 print "<span style=\"color: #cc0000;\">";
559         }
560         printf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
561         if ($ad{'hour_local'} < 6 ) {
562                 print "</span>";
563         }
564         print "</i>\n" .
565               "</td></tr>\n";
566         print "<tr><td class=\"key\">committer</td><td>" . escapeHTML($co{'committer'}) . "</td><tr><td></td><td>" .
567               " " . $cd{'rfc2822'} . sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) . "</i>\n" .
568               "</td></tr>\n";
569         print "<tr><td class=\"key\">commit</td><td style=\"font-family: monospace;\">$hash</td></tr>\n";
570         print "<tr><td class=\"key\">tree</td><td style=\"font-family: monospace;\">" . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$hash"}, $co{'tree'}) . "</td></tr>\n";
571         my $parents  = $co{'parents'};
572         foreach my $par (@$parents) {
573                 print "<tr><td class=\"key\">parent</td><td style=\"font-family: monospace;\">" . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$par"}, $par) . "</td></tr>\n";
574         }
575         print "</table></div>\n";
576         print "<div class=\"page_body\">\n";
577         my $comment = $co{'comment'};
578         foreach my $line (@$comment) {
579                 if ($line =~ m/(signed-off|acked)-by:/i) {
580                         print "<span style=\"color: #888888\">" . escapeHTML($line) . "</span><br/>\n";
581                 } else {
582                         print escapeHTML($line) . "<br/>\n";
583                 }
584         }
585         print "</div>\n";
586         if ($#difftree > 10) {
587                 print "<div class=\"list_head\">" . ($#difftree + 1) . " files changed:<br/></div>\n";
588         }
589         foreach my $line (@difftree) {
590                 # '*100644->100644      blob    9f91a116d91926df3ba936a80f020a6ab1084d2b->bb90a0c3a91eb52020d0db0e8b4f94d30e02d596      net/ipv4/route.c'
591                 # '+100644      blob    4a83ab6cd565d21ab0385bac6643826b83c2fcd4        arch/arm/lib/bitops.h'
592                 # '*100664->100644      blob    b1a8e3dd5556b61dd771d32307c6ee5d7150fa43->b1a8e3dd5556b61dd771d32307c6ee5d7150fa43      show-files.c'
593                 # '*100664->100644      blob    d08e895238bac36d8220586fdc28c27e1a7a76d3->d08e895238bac36d8220586fdc28c27e1a7a76d3      update-cache.c'
594                 $line =~ m/^(.)(.*)\t(.*)\t(.*)\t(.*)$/;
595                 my $op = $1;
596                 my $mode = $2;
597                 my $type = $3;
598                 my $id = $4;
599                 my $file = $5;
600                 my $mode_chng = "";
601                 if ($type eq "blob") {
602                         if ($op eq "+") {
603                                 print "<div class=\"list\">\n" .
604                                       $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$id"},
605                                       escapeHTML($file) . " <span style=\"color: #008000;\">[new]</span>") . "\n" .
606                                       "</div>";
607                                 print "<div class=\"link\">\n" .
608                                       "view " .
609                                       $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$id"}, "file") . "<br/>\n" .
610                                       "</div>\n";
611                         } elsif ($op eq "-") {
612                                 print "<div class=\"list\">\n" .
613                                       $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$id"},
614                                       escapeHTML($file) .  " <span style=\"color: #c00000;\">[removed]</span>") . "\n" .
615                                       "</div>";
616                                 print "<div class=\"link\">\n" .
617                                       "view " .
618                                       $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$id"}, "file") . " | " .
619                                       $cgi->a({-href => "$my_uri?p=$project;a=history;h=$hash;f=$file"}, "history") . "<br/>\n" .
620                                       "</div>\n";
621                         } elsif ($op eq "*") {
622                                 $id =~ m/([0-9a-fA-F]+)->([0-9a-fA-F]+)/;
623                                 my $from_id = $1;
624                                 my $to_id = $2;
625                                 $mode =~ m/^([0-7]{6})->([0-7]{6})$/;
626                                 my $from_mode = $1;
627                                 my $to_mode = $2;
628                                 my $mode_chnge = "";
629                                 if ($from_mode != $to_mode) {
630                                         $mode_chnge = " <span style=\"color: #888888;\"> [chmod $mode]</span>\n";
631                                 }
632                                 print "<div class=\"list\">\n";
633                                 if ($to_id ne $from_id) {
634                                         print $cgi->a({-href => "$my_uri?p=$project;a=blobdiff;h=$to_id;hp=$from_id"},
635                                               escapeHTML($file) . $mode_chnge) . "\n" .
636                                               "</div>\n";
637                                 } else {
638                                         print $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$to_id"},
639                                               escapeHTML($file) . $mode_chnge) . "\n" .
640                                               "</div>\n";
641                                 }
642                                 print "<div class=\"link\">\n" .
643                                       "view ";
644                                 if ($to_id ne $from_id) {
645                                         print $cgi->a({-href => "$my_uri?p=$project;a=blobdiff;h=$to_id;hp=$from_id"}, "diff") . " | ";
646                                 }
647                                 print $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$to_id"}, "file") . " | " .
648                                       $cgi->a({-href => "$my_uri?p=$project;a=history;h=$hash;f=$file"}, "history") . "<br/>\n" .
649                                       "</div>\n";
650                         }
651                 }
652         }
653         git_footer_html();
654 } elsif ($action eq "blobdiff") {
655         git_header_html();
656         print "<div class=\"page_nav\">\n";
657         print "<br/><br/></div>\n";
658         print "<div class=\"title\">$hash vs $hash_parent</div>\n";
659         print "<div class=\"page_body\">\n" .
660               "<pre>\n";
661         git_diff_html($hash_parent, $hash, $hash_parent, $hash);
662         print "</pre>\n" .
663               "<br/></div>";
664         git_footer_html();
665 } elsif ($action eq "commitdiff") {
666         my %co = git_commit($hash);
667         if (!defined(%co)) {
668                 die_error("", "Unknown commit object.");
669         }
670         open my $fd, "-|", "$gitbin/diff-tree -r " . $co{'parent'} . " $hash";
671         my (@difftree) = map { chomp; $_ } <$fd>;
672         close $fd;
673
674         git_header_html();
675         print "<div class=\"page_nav\"> view\n" .
676               $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash"}, "commit") . " | \n" .
677               $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash"}, "diffs") . " | \n" .
678               $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$hash"}, "tree") . "\n" .
679               "<br/><br/></div>\n";
680         print "<div>\n" .
681               $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
682               "</div>\n";
683         print "<div class=\"page_body\">\n" .
684               "<pre>\n";
685         foreach my $line (@difftree) {
686                 # '*100644->100644      blob    8e5f9bbdf4de94a1bc4b4da8cb06677ce0a57716->8da3a306d0c0c070d87048d14a033df02f40a154      Makefile'
687                 $line =~ m/^(.)(.*)\t(.*)\t(.*)\t(.*)$/;
688                 my $op = $1;
689                 my $mode = $2;
690                 my $type = $3;
691                 my $id = $4;
692                 my $file = $5;
693                 if ($type eq "blob") {
694                         if ($op eq "+") {
695                                 git_diff_html("", $file, "", $id);
696                         } elsif ($op eq "-") {
697                                 git_diff_html($file, "", $id, "");
698                         } elsif ($op eq "*") {
699                                 $id =~ m/([0-9a-fA-F]+)->([0-9a-fA-F]+)/;
700                                 if ($1 ne $2) {
701                                         git_diff_html($file, $file, $1, $2);
702                                 }
703                         }
704                 }
705         }
706         print "<br/></pre>\n";
707         print "</div>";
708         git_footer_html();
709 } elsif ($action eq "history") {
710         if (!(defined($hash))) {
711                 $hash = git_head($project);
712         }
713         open my $fd, "-|", "$gitbin/rev-list $hash";
714         my (@revlist) = map { chomp; $_ } <$fd>;
715         close $fd;
716
717         git_header_html();
718         print "<div class=\"page_nav\">\n";
719         print "<br/><br/></div>\n";
720         print "<div>\n" .
721               $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash", -class => "title"}, escapeHTML($file_name)) . "\n" .
722               "</div>\n";
723         foreach my $rev (@revlist) {
724                 my %co = git_commit($rev);
725                 my $parents  = $co{'parents'};
726                 my $found = 0;
727                 foreach my $parent (@$parents) {
728                         open $fd, "-|", "$gitbin/diff-tree -r $parent $rev $file_name";
729                         my (@difftree) = map { chomp; $_ } <$fd>;
730                         close $fd;
731
732                         foreach my $line (@difftree) {
733                                 $line =~ m/^(.)(.*)\t(.*)\t(.*)\t(.*)$/;
734                                 my $file = $5;
735                                 if ($file eq $file_name) {
736                                         $found = 1;
737                                         last;
738                                 }
739                         }
740                 }
741                 if ($found) {
742                         print "<div class=\"list\">\n" .
743                               $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$rev"},
744                               "<span class=\"log_age\">" . $co{'age_string'} . "</span>" . escapeHTML($co{'title'})) . "\n" .
745                               "</div>\n";
746                         print "<div class=\"link\">\n" .
747                               "view " .
748                               $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$rev"}, "commit") . " | " .
749                               $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$rev"}, "tree") . "<br/><br/>\n" .
750                               "</div>\n";
751                 }
752         }
753         git_footer_html();
754 } else {
755         $action = "";
756         die_error("", "Unknown action.");
757 }