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