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