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