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