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