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