gitweb.cgion commit v136 (8ab1da2)
   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 program 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);
  14use Fcntl ':mode';
  15
  16my $cgi = new CGI;
  17my $version =           "136";
  18my $my_url =            $cgi->url();
  19my $my_uri =            $cgi->url(-absolute => 1);
  20my $rss_link = "";
  21
  22# absolute fs-path which will be prepended to the project path
  23my $projectroot =       "/pub/scm";
  24
  25# location of the git-core binaries
  26my $gitbin =            "/usr/bin";
  27
  28# location for temporary files needed for diffs
  29my $gittmp =            "/tmp/gitweb";
  30
  31# target of the home link on top of all pages
  32#my $home_link =                $my_uri;
  33my $home_link =         "/git";
  34
  35# html text to include at home page
  36my $home_text =         "indextext.html";
  37
  38# source of projects list
  39#my $projects_list = $projectroot;
  40my $projects_list = "index/index.txt";
  41
  42# input validation and dispatch
  43my $action = $cgi->param('a');
  44if (defined $action) {
  45        if ($action =~ m/[^0-9a-zA-Z\.\-]+/) {
  46                undef $action;
  47                die_error(undef, "Invalid action parameter.");
  48        }
  49        if ($action eq "git-logo.png") {
  50                git_logo();
  51                exit;
  52        }
  53} else {
  54        $action = "log";
  55}
  56
  57my $project = $cgi->param('p');
  58if (defined $project) {
  59        if ($project =~ m/(^|\/)(|\.|\.\.)($|\/)/) {
  60                undef $project;
  61                die_error(undef, "Non-canonical project parameter.");
  62        }
  63        if ($project =~ m/[^a-zA-Z0-9_\.\/\-\+\#\~]/) {
  64                undef $project;
  65                die_error(undef, "Invalid character in project parameter.");
  66        }
  67        if (!(-d "$projectroot/$project")) {
  68                undef $project;
  69                die_error(undef, "No such directory.");
  70        }
  71        if (!(-e "$projectroot/$project/HEAD")) {
  72                undef $project;
  73                die_error(undef, "No such project.");
  74        }
  75        $rss_link = "<link rel=\"alternate\" title=\"$project log\" href=\"$my_uri?p=$project;a=rss\" type=\"application/rss+xml\"/>";
  76        $ENV{'SHA1_FILE_DIRECTORY'} = "$projectroot/$project/objects";
  77} else {
  78        git_project_list($projects_list);
  79        exit;
  80}
  81
  82my $file_name = $cgi->param('f');
  83if (defined $file_name) {
  84        if ($file_name =~ m/(^|\/)(|\.|\.\.)($|\/)/) {
  85                undef $file_name;
  86                die_error(undef, "Non-canonical file parameter.");
  87        }
  88        if ($file_name =~ m/[^a-zA-Z0-9_\.\/\-\+\#\~\:\!]/) {
  89                undef $file_name;
  90                die_error(undef, "Invalid character in file parameter.");
  91        }
  92}
  93
  94my $hash = $cgi->param('h');
  95if (defined $hash && !($hash =~ m/^[0-9a-fA-F]{40}$/)) {
  96        undef $hash;
  97        die_error(undef, "Invalid hash parameter.");
  98}
  99
 100my $hash_parent = $cgi->param('hp');
 101if (defined $hash_parent && !($hash_parent =~ m/^[0-9a-fA-F]{40}$/)) {
 102        undef $hash_parent;
 103        die_error(undef, "Invalid hash_parent parameter.");
 104}
 105
 106my $hash_base = $cgi->param('hb');
 107if (defined $hash_base && !($hash_base =~ m/^[0-9a-fA-F]{40}$/)) {
 108        undef $hash_base;
 109        die_error(undef, "Invalid parent hash parameter.");
 110}
 111
 112my $time_back = $cgi->param('t');
 113if (defined $time_back) {
 114        if ($time_back =~ m/^[^0-9]+$/) {
 115                undef $time_back;
 116                die_error(undef, "Invalid time parameter.");
 117        }
 118}
 119
 120if ($action eq "blob") {
 121        git_blob();
 122        exit;
 123} elsif ($action eq "tree") {
 124        git_tree();
 125        exit;
 126} elsif ($action eq "rss") {
 127        git_rss();
 128        exit;
 129} elsif ($action eq "commit") {
 130        git_commit();
 131        exit;
 132} elsif ($action eq "log") {
 133        git_log();
 134        exit;
 135} elsif ($action eq "blobdiff") {
 136        git_blobdiff();
 137        exit;
 138} elsif ($action eq "commitdiff") {
 139        git_commitdiff();
 140        exit;
 141} elsif ($action eq "history") {
 142        git_history();
 143        exit;
 144} else {
 145        undef $action;
 146        die_error(undef, "Unknown action.");
 147        exit;
 148}
 149
 150sub git_header_html {
 151        my $status = shift || "200 OK";
 152
 153        my $title = "git";
 154        if (defined $project) {
 155                $title .= " - $project";
 156                if (defined $action) {
 157                        $title .= "/$action";
 158                }
 159        }
 160        print $cgi->header(-type=>'text/html',  -charset => 'utf-8', -status=> $status);
 161        print <<EOF;
 162<?xml version="1.0" encoding="utf-8"?>
 163<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 164<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
 165<!-- git web interface v$version, (C) 2005, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke <ch\@gierke.de> -->
 166<head>
 167<title>$title</title>
 168$rss_link
 169<style type="text/css">
 170body { font-family: sans-serif; font-size: 12px; margin:0px; border:solid #d9d8d1; border-width:1px; margin:10px; }
 171a { color:#0000cc; }
 172a:hover, a:visited, a:active { color:#880000; }
 173div.page_header { height:25px; padding:8px; font-size:18px; font-weight:bold; background-color:#d9d8d1; }
 174div.page_header a:visited { color:#0000cc; }
 175div.page_header a:hover { color:#880000; }
 176div.page_nav { padding:8px; }
 177div.page_nav a:visited { color:#0000cc; }
 178div.page_path { font-weight:bold; padding:8px; border:solid #d9d8d1; border-width:0px 0px 1px}
 179div.page_footer { height:17px; padding:4px 8px; background-color: #d9d8d1; }
 180div.page_footer_text { float:left; color:#555555; font-style:italic; }
 181div.page_body { padding:8px; }
 182div.title, a.title {
 183        display:block; padding:6px 8px;
 184        font-weight:bold; background-color:#edece6; text-decoration:none; color:#000000;
 185}
 186a.title:hover { background-color: #d9d8d1; }
 187div.title_text { padding:6px 8px; border: solid #d9d8d1; border-width:0px 0px 1px; }
 188div.log_body { padding:8px 8px 8px 150px; }
 189span.log_age { position:relative; float:left; width:142px; font-style:italic; }
 190div.log_link {
 191        font-size:10px; font-family:sans-serif; font-style:normal;
 192        position:relative; float:left; width:142px;
 193}
 194div.list { display:block; padding:4px 8px 2px; font-weight:bold; }
 195div.list_head {
 196        display:block; padding:6px 6px 4px; border:solid #d9d8d1;
 197        border-width:0px 0px 1px; font-style:italic;
 198}
 199div.list a { text-decoration:none; color:#000000; }
 200div.list a:hover { color:#880000; }
 201div.link {
 202        padding:4px 8px 6px; border:solid #d9d8d1; border-width:0px 0px 1px;
 203        font-family:sans-serif; font-size:10px;
 204}
 205td { padding:5px 15px 0px 0px; font-size:12px; }
 206th { padding-right:10px; font-size:12px; text-align:left; }
 207td.link { font-family:sans-serif; font-size:10px; }
 208td.pre { font-family:monospace; font-size:12px; white-space:pre; padding:2px 15px 0px 0px; }
 209div.pre { font-family:monospace; font-size:12px; white-space:pre; }
 210div.diff_info { font-family:monospace; color:#000099; background-color:#edece6; font-style:italic; }
 211div.index_include { border:solid #d9d8d1; border-width:0px 0px 1px; padding:12px 8px; }
 212a.rss_logo { float:right; padding:3px 0px; width:35px; line-height:10px;
 213        border:1px solid; border-color:#fcc7a5 #7d3302 #3e1a01 #ff954e;
 214        color:#ffffff; background-color:#ff6600;
 215        font-weight:bold; font-family:sans-serif; font-size:10px;
 216        text-align:center; text-decoration:none;
 217}
 218a.rss_logo:hover { background-color:#ee5500; }
 219</style>
 220</head>
 221<body>
 222EOF
 223        print "<div class=\"page_header\">\n" .
 224              "<a href=\"http://kernel.org/pub/software/scm/git/docs/\">" .
 225              "<img src=\"$my_uri?a=git-logo.png\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/></a>";
 226        print $cgi->a({-href => $home_link}, "projects") . " / ";
 227        if (defined $project) {
 228                print $cgi->a({-href => "$my_uri?p=$project;a=log"}, escapeHTML($project));
 229                if (defined $action) {
 230                        print " / $action";
 231                }
 232        }
 233        print "</div>\n";
 234}
 235
 236sub git_footer_html {
 237        print "<div class=\"page_footer\">\n";
 238        if (defined $project) {
 239                my $descr = git_read_description($project);
 240                if (defined $descr) {
 241                        print "<div class=\"page_footer_text\">" . escapeHTML($descr) . "</div>\n";
 242                }
 243                print $cgi->a({-href => "$my_uri?p=$project;a=rss", -class => "rss_logo"}, "RSS") . "\n";
 244        }
 245        print "</div>\n" .
 246              "</body>\n" .
 247              "</html>";
 248}
 249
 250sub die_error {
 251        my $status = shift || "403 Forbidden";
 252        my $error = shift || "Malformed query, file missing or permission denied"; 
 253
 254        git_header_html($status);
 255        print "<div class=\"page_body\">\n" .
 256              "<br/><br/>\n";
 257        print "$status - $error\n";
 258        print "<br/></div>\n";
 259        git_footer_html();
 260        exit;
 261}
 262
 263sub git_get_type {
 264        my $hash = shift;
 265
 266        open my $fd, "-|", "$gitbin/git-cat-file -t $hash" || return;
 267        my $type = <$fd>;
 268        close $fd;
 269        chomp $type;
 270        return $type;
 271}
 272
 273sub git_read_head {
 274        my $path = shift;
 275
 276        open my $fd, "$projectroot/$path/HEAD" || return undef;
 277        my $head = <$fd>;
 278        close $fd;
 279        chomp $head;
 280        if ($head =~ m/^[0-9a-fA-F]{40}$/) {
 281                return $head;
 282        } else {
 283                return undef;
 284        }
 285}
 286
 287sub git_read_description {
 288        my $path = shift;
 289
 290        open my $fd, "$projectroot/$path/description" || return undef;
 291        my $descr = <$fd>;
 292        close $fd;
 293        chomp $descr;
 294        return $descr;
 295}
 296
 297sub git_read_commit {
 298        my $commit = shift;
 299        my %co;
 300        my @parents;
 301
 302        open my $fd, "-|", "$gitbin/git-cat-file commit $commit" || return;
 303        while (my $line = <$fd>) {
 304                last if $line eq "\n";
 305                chomp $line;
 306                if ($line =~ m/^tree (.*)$/) {
 307                        $co{'tree'} = $1;
 308                } elsif ($line =~ m/^parent (.*)$/) {
 309                        push @parents, $1;
 310                } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
 311                        $co{'author'} = $1;
 312                        $co{'author_epoch'} = $2;
 313                        $co{'author_tz'} = $3;
 314                        $co{'author_name'} = $co{'author'};
 315                        $co{'author_name'} =~ s/ <.*//;
 316                } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
 317                        $co{'committer'} = $1;
 318                        $co{'committer_epoch'} = $2;
 319                        $co{'committer_tz'} = $3;
 320                        $co{'committer_name'} = $co{'committer'};
 321                        $co{'committer_name'} =~ s/ <.*//;
 322                }
 323        }
 324        $co{'parents'} = \@parents;
 325        $co{'parent'} = $parents[0];
 326        my (@comment) = map { chomp; $_ } <$fd>;
 327        $co{'comment'} = \@comment;
 328        $comment[0] =~ m/^(.{0,60}[^ ]*)/;
 329        $co{'title'} = $1;
 330        if ($comment[0] ne $co{'title'}) {
 331                $co{'title'} .= " [...]";
 332        }
 333        close $fd || return;
 334        if (!defined $co{'tree'}) {
 335                return undef
 336        };
 337
 338        my $age = time - $co{'committer_epoch'};
 339        $co{'age'} = $age;
 340        if ($age > 60*60*24*365*2) {
 341                $co{'age_string'} = (int $age/60/60/24/365);
 342                $co{'age_string'} .= " years ago";
 343        } elsif ($age > 60*60*24*365/12*2) {
 344                $co{'age_string'} = int $age/60/60/24/365/12;
 345                $co{'age_string'} .= " months ago";
 346        } elsif ($age > 60*60*24*7*2) {
 347                $co{'age_string'} = int $age/60/60/24/7;
 348                $co{'age_string'} .= " weeks ago";
 349        } elsif ($age > 60*60*24*2) {
 350                $co{'age_string'} = int $age/60/60/24;
 351                $co{'age_string'} .= " days ago";
 352        } elsif ($age > 60*60*2) {
 353                $co{'age_string'} = int $age/60/60;
 354                $co{'age_string'} .= " hours ago";
 355        } elsif ($age > 60*2) {
 356                $co{'age_string'} = int $age/60;
 357                $co{'age_string'} .= " minutes ago";
 358        } elsif ($age > 2) {
 359                $co{'age_string'} = int $age;
 360                $co{'age_string'} .= " seconds ago";
 361        } else {
 362                $co{'age_string'} .= " right now";
 363        }
 364        return %co;
 365}
 366
 367sub git_diff_html {
 368        my $from = shift;
 369        my $from_name = shift;
 370        my $to = shift;
 371        my $to_name = shift;
 372
 373        my $from_tmp = "/dev/null";
 374        my $to_tmp = "/dev/null";
 375        my $pid = $$;
 376
 377        # create tmp from-file
 378        if (defined $from) {
 379                $from_tmp = "$gittmp/gitweb_" . $$ . "_from";
 380                open my $fd2, "> $from_tmp";
 381                open my $fd, "-|", "$gitbin/git-cat-file blob $from";
 382                my @file = <$fd>;
 383                print $fd2 @file;
 384                close $fd2;
 385                close $fd;
 386        }
 387
 388        # create tmp to-file
 389        if (defined $to) {
 390                $to_tmp = "$gittmp/gitweb_" . $$ . "_to";
 391                open my $fd2, "> $to_tmp";
 392                open my $fd, "-|", "$gitbin/git-cat-file blob $to";
 393                my @file = <$fd>;
 394                print $fd2 @file;
 395                close $fd2;
 396                close $fd;
 397        }
 398
 399        open my $fd, "-|", "/usr/bin/diff -u -p -L $from_name -L $to_name $from_tmp $to_tmp";
 400        while (my $line = <$fd>) {
 401                chomp($line);
 402                my $char = substr($line, 0, 1);
 403                my $color = "";
 404                if ($char eq '+') {
 405                        $color = " style=\"color:#008800;\"";
 406                } elsif ($char eq '-') {
 407                        $color = " style=\"color:#cc0000;\"";
 408                } elsif ($char eq '@') {
 409                        $color = " style=\"color:#990099;\"";
 410                } elsif ($char eq '\\') {
 411                        # skip errors
 412                        next;
 413                }
 414                print "<div class=\"pre\"$color>" . escapeHTML($line) . "</div>\n";
 415        }
 416        close $fd;
 417
 418        if (defined $from) {
 419                unlink($from_tmp);
 420        }
 421        if (defined $to) {
 422                unlink($to_tmp);
 423        }
 424}
 425
 426sub mode_str {
 427        my $mode = oct shift;
 428
 429        if (S_ISDIR($mode & S_IFMT)) {
 430                return 'drwxr-xr-x';
 431        } elsif (S_ISLNK($mode)) {
 432                return 'lrwxrwxrwx';
 433        } elsif (S_ISREG($mode)) {
 434                # git cares only about the executable bit
 435                if ($mode & S_IXUSR) {
 436                        return '-rwxr-xr-x';
 437                } else {
 438                        return '-rw-r--r--';
 439                };
 440        } else {
 441                return '----------';
 442        }
 443}
 444
 445sub file_type {
 446        my $mode = oct shift;
 447
 448        if (S_ISDIR($mode & S_IFMT)) {
 449                return "directory";
 450        } elsif (S_ISLNK($mode)) {
 451                return "symlink";
 452        } elsif (S_ISREG($mode)) {
 453                return "file";
 454        } else {
 455                return "unknown";
 456        }
 457}
 458
 459sub date_str {
 460        my $epoch = shift;
 461        my $tz = shift || "-0000";
 462
 463        my %date;
 464        my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
 465        my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
 466        my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
 467        $date{'hour'} = $hour;
 468        $date{'minute'} = $min;
 469        $date{'mday'} = $mday;
 470        $date{'day'} = $days[$wday];
 471        $date{'month'} = $months[$mon];
 472        $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000", $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
 473        $date{'mday-time'} = sprintf "%d %s %02d:%02d", $mday, $months[$mon], $hour ,$min;
 474
 475        $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
 476        my $local = $epoch + ((int $1 + ($2/60)) * 3600);
 477        ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
 478        $date{'hour_local'} = $hour;
 479        $date{'minute_local'} = $min;
 480        $date{'tz_local'} = $tz;
 481        return %date;
 482}
 483
 484# git-logo (cached in browser for one day)
 485sub git_logo {
 486        print $cgi->header(-type => 'image/png', -expires => '+1d');
 487        # cat git-logo.png | hexdump -e '16/1 " %02x"  "\n"' | sed 's/ /\\x/g'
 488        print   "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52" .
 489                "\x00\x00\x00\x48\x00\x00\x00\x1b\x04\x03\x00\x00\x00\x2d\xd9\xd4" .
 490                "\x2d\x00\x00\x00\x18\x50\x4c\x54\x45\xff\xff\xff\x60\x60\x5d\xb0" .
 491                "\xaf\xaa\x00\x80\x00\xce\xcd\xc7\xc0\x00\x00\xe8\xe8\xe6\xf7\xf7" .
 492                "\xf6\x95\x0c\xa7\x47\x00\x00\x00\x73\x49\x44\x41\x54\x28\xcf\x63" .
 493                "\x48\x67\x20\x04\x4a\x5c\x18\x0a\x08\x2a\x62\x53\x61\x20\x02\x08" .
 494                "\x0d\x69\x45\xac\xa1\xa1\x01\x30\x0c\x93\x60\x36\x26\x52\x91\xb1" .
 495                "\x01\x11\xd6\xe1\x55\x64\x6c\x6c\xcc\x6c\x6c\x0c\xa2\x0c\x70\x2a" .
 496                "\x62\x06\x2a\xc1\x62\x1d\xb3\x01\x02\x53\xa4\x08\xe8\x00\x03\x18" .
 497                "\x26\x56\x11\xd4\xe1\x20\x97\x1b\xe0\xb4\x0e\x35\x24\x71\x29\x82" .
 498                "\x99\x30\xb8\x93\x0a\x11\xb9\x45\x88\xc1\x8d\xa0\xa2\x44\x21\x06" .
 499                "\x27\x41\x82\x40\x85\xc1\x45\x89\x20\x70\x01\x00\xa4\x3d\x21\xc5" .
 500                "\x12\x1c\x9a\xfe\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82";
 501}
 502
 503sub get_file_owner {
 504        my $path = shift;
 505
 506        my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
 507        my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
 508        if (!defined $gcos) {
 509                return undef;
 510        }
 511        my $owner = $gcos;
 512        $owner =~ s/[,;].*$//;
 513        return $owner;
 514}
 515
 516sub git_project_list {
 517        my $project_list = shift;
 518        my @list;
 519
 520        if (-d $project_list) {
 521                # search in directory
 522                my $dir = $project_list;
 523                opendir my $dh, $dir || return undef;
 524                while (my $dir = readdir($dh)) {
 525                        if (-e "$projectroot/$dir/HEAD") {
 526                                my $pr = {
 527                                        path => $dir,
 528                                };
 529                                push @list, $pr
 530                        }
 531                }
 532                closedir($dh);
 533        } elsif (-e $project_list) {
 534                # read from file
 535                # 'git/git.git:Linus Torvalds'
 536                # 'linux/hotplug/udev.git'
 537                open my $fd , $project_list || return undef;
 538                while (my $line = <$fd>) {
 539                        chomp $line;
 540                        my ($path, $owner) = split ':', $line;
 541                        if (!defined $path) {
 542                                next;
 543                        }
 544                        if (-e "$projectroot/$path/HEAD") {
 545                                my $pr = {
 546                                        path => $path,
 547                                        owner => $owner,
 548                                };
 549                                push @list, $pr
 550                        }
 551                }
 552                close $fd;
 553        }
 554
 555        if (!@list) {
 556                die_error(undef, "No project found.");
 557        }
 558        @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
 559
 560        git_header_html();
 561        if (-f $home_text) {
 562                print "<div class=\"index_include\">\n";
 563                open my $fd, $home_text;
 564                print <$fd>;
 565                close $fd;
 566                print "</div>\n";
 567        }
 568        print "<div class=\"page_body\"><br/>\n" .
 569              "<table cellspacing=\"0\">\n" .
 570              "<tr>\n" .
 571              "<th>Project</th>\n" .
 572              "<th>Description</th>\n" .
 573              "<th>Owner</th>\n" .
 574              "<th>last change</th>\n" .
 575              "</tr>\n";
 576        foreach my $pr (@list) {
 577                my %proj = %$pr;
 578                my $head = git_read_head($proj{'path'});
 579                if (!defined $head) {
 580                        next;
 581                }
 582                $ENV{'SHA1_FILE_DIRECTORY'} = "$projectroot/$proj{'path'}/objects";
 583                my %co = git_read_commit($head);
 584                if (!%co) {
 585                        next;
 586                }
 587                my $descr = git_read_description($proj{'path'}) || "";
 588                # get directory owner if not already specified
 589                if (!defined $proj{'owner'}) {
 590                        $proj{'owner'} = get_file_owner("$projectroot/$proj{'path'}") || "";
 591                }
 592                print "<tr>\n" .
 593                      "<td>" . $cgi->a({-href => "$my_uri?p=" . $proj{'path'} . ";a=log"}, escapeHTML($proj{'path'})) . "</td>\n" .
 594                      "<td>$descr</td>\n" .
 595                      "<td><i>$proj{'owner'}</i></td>\n";
 596                if ($co{'age'} < 60*60*2) {
 597                        print "<td><span style =\"color: #009900;\"><b><i>$co{'age_string'}</i></b></span></td>\n";
 598                } elsif ($co{'age'} < 60*60*24*2) {
 599                        print "<td><span style =\"color: #009900;\"><i>$co{'age_string'}</i></span></td>\n";
 600                } else {
 601                        print "<td><i>$co{'age_string'}</i></td>\n";
 602                }
 603                print "</tr>\n";
 604        }
 605        print "</table>\n" .
 606              "<br/>\n" .
 607              "</div>\n";
 608        git_footer_html();
 609}
 610
 611sub git_get_hash_by_path {
 612        my $base = shift;
 613        my $path = shift;
 614
 615        my $tree = $base;
 616        my @parts = split '/', $path;
 617        while (my $part = shift @parts) {
 618                open my $fd, "-|", "$gitbin/git-ls-tree $tree" || die_error(undef, "Open git-ls-tree failed.");
 619                my (@entries) = map { chomp; $_ } <$fd>;
 620                close $fd || return undef;
 621                foreach my $line (@entries) {
 622                        #'100644        blob    0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa        panic.c'
 623                        $line =~ m/^([0-9]+)\t(.*)\t(.*)\t(.*)$/;
 624                        my $t_mode = $1;
 625                        my $t_type = $2;
 626                        my $t_hash = $3;
 627                        my $t_name = $4;
 628                        if ($t_name eq $part) {
 629                                if (!(@parts)) {
 630                                        return $t_hash;
 631                                }
 632                                if ($t_type eq "tree") {
 633                                        $tree = $t_hash;
 634                                }
 635                                last;
 636                        }
 637                }
 638        }
 639}
 640
 641sub git_blob {
 642        if (!defined $hash && defined $file_name) {
 643                my $base = $hash_base || git_read_head($project);
 644                $hash = git_get_hash_by_path($base, $file_name, "blob");
 645        }
 646        open my $fd, "-|", "$gitbin/git-cat-file blob $hash" || die_error(undef, "Open failed.");
 647        my $base = $file_name || "";
 648        git_header_html();
 649        if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
 650                print "<div class=\"page_nav\">\n" .
 651                      $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash_base"}, "commit") .
 652                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash_base"}, "commitdiff") .
 653                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=" . $co{'tree'} . ";hb=$hash_base"}, "tree");
 654                if (defined $file_name) {
 655                        print " | " . $cgi->a({-href => "$my_uri?p=$project;a=history;h=$hash_base;f=$file_name"}, "history");
 656                }
 657                print "<br/><br/>\n" .
 658                      "</div>\n";
 659                print "<div>" .
 660                      $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash_base", -class => "title"}, escapeHTML($co{'title'})) .
 661                      "</div>\n";
 662        } else {
 663                print "<div class=\"page_nav\">\n" .
 664                      "<br/><br/></div>\n" .
 665                      "<div class=\"title\">$hash</div>\n";
 666        }
 667        if (defined $file_name) {
 668                print "<div class=\"page_path\">/$file_name</div>\n";
 669        }
 670        print "<div class=\"page_body\">\n";
 671        my $nr;
 672        while (my $line = <$fd>) {
 673                chomp $line;
 674                $nr++;
 675                print "<div class=\"pre\">";
 676                printf "<span style=\"color:#999999;\">%4i</span>", $nr;
 677                print " " .escapeHTML($line) . "</div>\n";
 678        }
 679        close $fd || print "Reading blob failed.\n";
 680        print "</div>";
 681        git_footer_html();
 682}
 683
 684sub git_tree {
 685        if (!defined $hash) {
 686                $hash = git_read_head($project);
 687                if (defined $file_name) {
 688                        my $base = $hash_base || git_read_head($project);
 689                        $hash = git_get_hash_by_path($base, $file_name, "tree");
 690                }
 691        }
 692        open my $fd, "-|", "$gitbin/git-ls-tree $hash" || die_error(undef, "Open git-ls-tree failed.");
 693        my (@entries) = map { chomp; $_ } <$fd>;
 694        close $fd || die_error(undef, "Reading tree failed.");
 695
 696        git_header_html();
 697        my $base_key = "";
 698        my $file_key = "";
 699        my $base = "";
 700        if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
 701                $base_key = ";hb=$hash_base";
 702                print "<div class=\"page_nav\">\n" .
 703                      $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash_base"}, "commit") . " | " .
 704                      $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash_base"}, "commitdiff") . " | " .
 705                      $cgi->a({-href => "$my_uri?p=$project;a=tree;h=" . $co{'tree'} . ";hb=$hash_base"}, "tree") .
 706                      "<br/><br/>\n" .
 707                      "</div>\n";
 708                print "<div>\n" .
 709                      $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash_base", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
 710                      "</div>\n";
 711        } else {
 712                print "<div class=\"page_nav\">\n";
 713                print "<br/><br/></div>\n";
 714                print "<div class=\"title\">$hash</div>\n";
 715        }
 716        if (defined $file_name) {
 717                $base = "$file_name/";
 718                print "<div class=\"page_path\">/$file_name</div>\n";
 719        } else {
 720                print "<div class=\"page_path\">/</div>\n";
 721        }
 722        print "<div class=\"page_body\">\n";
 723        print "<table cellspacing=\"0\">\n";
 724        foreach my $line (@entries) {
 725                #'100644        blob    0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa        panic.c'
 726                $line =~ m/^([0-9]+)\t(.*)\t(.*)\t(.*)$/;
 727                my $t_mode = $1;
 728                my $t_type = $2;
 729                my $t_hash = $3;
 730                my $t_name = $4;
 731                $file_key = ";f=$base$t_name";
 732                print "<tr>\n" .
 733                      "<td class=\"pre\">" . mode_str($t_mode) . "</td>\n";
 734                if ($t_type eq "blob") {
 735                        print "<td class=\"pre\">$t_name</td>\n";
 736                        print "<td class=\"link\">" .
 737                              $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$t_hash" . $base_key . $file_key}, "blob") .
 738                              " | " . $cgi->a({-href => "$my_uri?p=$project;a=history;h=$hash_base" . $file_key}, "history") .
 739                              "</td>\n";
 740                } elsif ($t_type eq "tree") {
 741                        print "<td class=\"pre\">" .
 742                              $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$t_hash" . $base_key . $file_key}, $t_name) .
 743                              "</td>\n";
 744                }
 745                print "</tr>\n";
 746        }
 747        print "</table>\n" .
 748              "</div>";
 749        git_footer_html();
 750}
 751
 752sub git_rss {
 753        open my $fd, "-|", "$gitbin/git-rev-list --max-count=20 " . git_read_head($project) || die_error(undef, "Open failed.");
 754        my (@revlist) = map { chomp; $_ } <$fd>;
 755        close $fd || die_error(undef, "Reading rev-list failed.");
 756
 757        print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
 758        print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".
 759              "<rss version=\"0.91\">\n";
 760        print "<channel>\n";
 761        print "<title>$project</title>\n".
 762              "<link> " . $my_url . "/$project/log</link>\n".
 763              "<description>$project log</description>\n".
 764              "<language>en</language>\n";
 765
 766        foreach my $commit (@revlist) {
 767                my %co = git_read_commit($commit);
 768                my %ad = date_str($co{'author_epoch'});
 769                print "<item>\n" .
 770                      "\t<title>" . sprintf("%d %s %02d:%02d", $ad{'mday'}, $ad{'month'}, $ad{'hour'}, $ad{'minute'}) . " - " . escapeHTML($co{'title'}) . "</title>\n" .
 771                      "\t<link> " . $my_url . "?p=$project;a=commit;h=$commit</link>\n" .
 772                      "\t<description>";
 773                my $comment = $co{'comment'};
 774                foreach my $line (@$comment) {
 775                        print escapeHTML($line) . "<br/>\n";
 776                }
 777                print "\t</description>\n" .
 778                      "</item>\n";
 779        }
 780        print "</channel></rss>";
 781}
 782
 783sub git_log {
 784        my $head = git_read_head($project);
 785        my $limit_option = "";
 786        if (!defined $time_back) {
 787                $limit_option = "--max-count=10";
 788        } elsif ($time_back > 0) {
 789                my $date = time - $time_back*24*60*60;
 790                $limit_option = "--max-age=$date";
 791        }
 792        open my $fd, "-|", "$gitbin/git-rev-list $limit_option $head" || die_error(undef, "Open failed.");
 793        my (@revlist) = map { chomp; $_ } <$fd>;
 794        close $fd || die_error(undef, "Reading rev-list failed.");
 795
 796        git_header_html();
 797        print "<div class=\"page_nav\">\n";
 798        print $cgi->a({-href => "$my_uri?p=$project;a=log"}, "last 10") . " | " .
 799              $cgi->a({-href => "$my_uri?p=$project;a=log;t=1"}, "day") . " | " .
 800              $cgi->a({-href => "$my_uri?p=$project;a=log;t=7"}, "week") . " | " .
 801              $cgi->a({-href => "$my_uri?p=$project;a=log;t=31"}, "month") . " | " .
 802              $cgi->a({-href => "$my_uri?p=$project;a=log;t=365"}, "year") . " | " .
 803              $cgi->a({-href => "$my_uri?p=$project;a=log;t=0"}, "all") . "<br/>\n";
 804        print "<br/><br/>\n" .
 805              "</div>\n";
 806
 807        if (!@revlist) {
 808                my %co = git_read_commit($head);
 809                print "<div class=\"page_body\"> Last change " . $co{'age_string'} . ".<br/><br/></div>\n";
 810        }
 811
 812        foreach my $commit (@revlist) {
 813                my %co = git_read_commit($commit);
 814                next if !%co;
 815                my %ad = date_str($co{'author_epoch'});
 816                print "<div>\n" .
 817                      $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit", -class => "title"},
 818                      "<span class=\"log_age\">" . $co{'age_string'} . "</span>" . escapeHTML($co{'title'})) . "\n" .
 819                      "</div>\n";
 820                print "<div class=\"title_text\">\n" .
 821                      "<div class=\"log_link\">\n" .
 822                      $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit"}, "commit") .
 823                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$commit"}, "commitdiff") .
 824                      "<br/>\n" .
 825                      "</div>\n" .
 826                      "<i>" . escapeHTML($co{'author_name'}) .  " [" . $ad{'rfc2822'} . "]</i><br/>\n" .
 827                      "</div>\n" .
 828                      "<div class=\"log_body\">\n";
 829                my $comment = $co{'comment'};
 830                my $empty = 0;
 831                foreach my $line (@$comment) {
 832                        if ($line =~ m/^(signed[ \-]off[\-]by[ :]|acked[\-]by[ \:]|cc[ :])/i) {
 833                                next;
 834                        }
 835                        if ($line eq "") {
 836                                if ($empty) {
 837                                        next;
 838                                }
 839                                $empty = 1;
 840                        } else {
 841                                $empty = 0;
 842                        }
 843                        print escapeHTML($line) . "<br/>\n";
 844                }
 845                if (!$empty) {
 846                        print "<br/>\n";
 847                }
 848                print "</div>\n";
 849        }
 850        git_footer_html();
 851}
 852
 853sub git_commit {
 854        my %co = git_read_commit($hash);
 855        if (!%co) {
 856                die_error(undef, "Unknown commit object.");
 857        }
 858        my %ad = date_str($co{'author_epoch'}, $co{'author_tz'});
 859        my %cd = date_str($co{'committer_epoch'}, $co{'committer_tz'});
 860
 861        my @difftree;
 862        if (defined $co{'parent'}) {
 863                open my $fd, "-|", "$gitbin/git-diff-tree -r " . $co{'parent'} . " $hash" || die_error(undef, "Open failed.");
 864                @difftree = map { chomp; $_ } <$fd>;
 865                close $fd || die_error(undef, "Reading diff-tree failed.");
 866        } else {
 867                # fake git-diff-tree output for initial revision
 868                open my $fd, "-|", "$gitbin/git-ls-tree -r $hash" || die_error(undef, "Open failed.");
 869                @difftree = map { chomp;  "+" . $_ } <$fd>;
 870                close $fd || die_error(undef, "Reading ls-tree failed.");
 871        }
 872        git_header_html();
 873        print "<div class=\"page_nav\">\n" .
 874              $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash"}, "commit");
 875        if (defined $co{'parent'}) {
 876                print " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash"}, "commitdiff");
 877        }
 878        print " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=" . $co{'tree'} . ";hb=$hash"}, "tree") . "\n" .
 879              "<br/><br/></div>\n";
 880        if (defined $co{'parent'}) {
 881                print "<div>\n" .
 882                      $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
 883                      "</div>\n";
 884        } else {
 885                print "<div>\n" .
 886                      $cgi->a({-href => "$my_uri?p=$project;a=tree;h=" . $co{'tree'} . ";hb=$hash", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
 887                      "</div>\n";
 888        }
 889        print "<div class=\"title_text\">\n" .
 890              "<table cellspacing=\"0\">\n";
 891        print "<tr><td>author</td><td>" . escapeHTML($co{'author'}) . "</td></tr>\n".
 892              "<tr><td></td><td> " . $ad{'rfc2822'};
 893        if ($ad{'hour_local'} < 6) {
 894                printf(" (<span style=\"color: #cc0000;\">%02d:%02d</span> %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
 895        } else {
 896                printf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
 897        }
 898        print "</td></tr>\n";
 899        print "<tr><td>committer</td><td>" . escapeHTML($co{'committer'}) . "</td></tr>\n";
 900        print "<tr><td></td><td> " . $cd{'rfc2822'} . sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) . "</td></tr>\n";
 901        print "<tr><td>commit</td><td style=\"font-family: monospace;\">$hash</td></tr>\n";
 902        print "<tr><td>tree</td><td style=\"font-family: monospace;\">" .
 903              $cgi->a({-href => "$my_uri?p=$project;a=tree;h=" . $co{'tree'} . ";hb=" . $hash}, $co{'tree'}) . "</td></tr>\n";
 904        my $parents  = $co{'parents'};
 905        foreach my $par (@$parents) {
 906                print "<tr><td>parent</td><td style=\"font-family: monospace;\">" .
 907                      $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$par"}, $par) . "</td></tr>\n";
 908        }
 909        print "</table>". 
 910              "</div>\n";
 911        print "<div class=\"page_body\">\n";
 912        my $comment = $co{'comment'};
 913        my $empty = 0;
 914        my $signed = 0;
 915        foreach my $line (@$comment) {
 916                # print only one empty line
 917                if ($line eq "") {
 918                        if ($empty || $signed) {
 919                                next;
 920                        }
 921                        $empty = 1;
 922                } else {
 923                        $empty = 0;
 924                }
 925                if ($line =~ m/^(signed[ \-]off[\-]by[ :]|acked[\-]by[ \:]|cc[ :])/i) {
 926                        $signed = 1;
 927                        print "<span style=\"color: #888888\">" . escapeHTML($line) . "</span><br/>\n";
 928                } else {
 929                        $signed = 0;
 930                        print escapeHTML($line) . "<br/>\n";
 931                }
 932        }
 933        print "</div>\n";
 934        print "<div class=\"list_head\">\n";
 935        if ($#difftree > 10) {
 936                print(($#difftree + 1) . " files changed:\n");
 937        }
 938        print "</div>\n";
 939        foreach my $line (@difftree) {
 940                # '*100644->100644      blob    9f91a116d91926df3ba936a80f020a6ab1084d2b->bb90a0c3a91eb52020d0db0e8b4f94d30e02d596      net/ipv4/route.c'
 941                # '+100644      blob    4a83ab6cd565d21ab0385bac6643826b83c2fcd4        arch/arm/lib/bitops.h'
 942                # '*100664->100644      blob    b1a8e3dd5556b61dd771d32307c6ee5d7150fa43->b1a8e3dd5556b61dd771d32307c6ee5d7150fa43      show-files.c'
 943                # '*100664->100644      blob    d08e895238bac36d8220586fdc28c27e1a7a76d3->d08e895238bac36d8220586fdc28c27e1a7a76d3      update-cache.c'
 944                $line =~ m/^(.)(.*)\t(.*)\t(.*)\t(.*)$/;
 945                my $op = $1;
 946                my $mode = $2;
 947                my $type = $3;
 948                my $id = $4;
 949                my $file = $5;
 950                if ($type eq "blob") {
 951                        if ($op eq "+") {
 952                                my $mode_chng = "";
 953                                if (S_ISREG(oct $mode)) {
 954                                        $mode_chng = sprintf(" with mode: %04o", (oct $mode) & 0777);
 955                                }
 956                                print "<div class=\"list\">\n" .
 957                                      $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$id;hb=$hash;f=$file"},
 958                                      escapeHTML($file) . " <span style=\"color: #008000;\">[new " . file_type($mode) . $mode_chng . "]</span>") . "\n" .
 959                                      "</div>\n";
 960                                print "<div class=\"link\">\n" .
 961                                      $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$id;hb=$hash;f=$file"}, "blob") . "\n" .
 962                                      "</div>\n";
 963                        } elsif ($op eq "-") {
 964                                print "<div class=\"list\">\n" .
 965                                      $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$id;hb=$hash;f=$file"},
 966                                      escapeHTML($file) .  " <span style=\"color: #c00000;\">[deleted " . file_type($mode) . "]</span>") . "\n" .
 967                                      "</div>";
 968                                print "<div class=\"link\">\n" .
 969                                      $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$id;hb=$hash;f=$file"}, "blob") . " | " .
 970                                      $cgi->a({-href => "$my_uri?p=$project;a=history;h=$hash;f=$file"}, "history") . "\n" .
 971                                      "</div>\n";
 972                        } elsif ($op eq "*") {
 973                                $id =~ m/([0-9a-fA-F]+)->([0-9a-fA-F]+)/;
 974                                my $from_id = $1;
 975                                my $to_id = $2;
 976                                $mode =~ m/^([0-7]{6})->([0-7]{6})$/;
 977                                my $from_mode = $1;
 978                                my $to_mode = $2;
 979                                my $mode_chnge = "";
 980                                if ($from_mode != $to_mode) {
 981                                        $mode_chnge = " <span style=\"color: #888888;\">[changed";
 982                                        if (((oct $from_mode) & S_IFMT) != ((oct $to_mode) & S_IFMT)) {
 983                                                $mode_chnge .= " from " . file_type($from_mode) . " to " . file_type($to_mode);
 984                                        }
 985                                        if (((oct $from_mode) & 0777) != ((oct $to_mode) & 0777)) {
 986                                                if (S_ISREG($from_mode) && S_ISREG($to_mode)) {
 987                                                        $mode_chnge .= sprintf(" mode: %04o->%04o", (oct $from_mode) & 0777, (oct $to_mode) & 0777);
 988                                                } elsif (S_ISREG($to_mode)) {
 989                                                        $mode_chnge .= sprintf(" mode: %04o", (oct $to_mode) & 0777);
 990                                                }
 991                                        }
 992                                        $mode_chnge .= "]</span>\n";
 993                                }
 994                                print "<div class=\"list\">\n";
 995                                if ($to_id ne $from_id) {
 996                                        print $cgi->a({-href => "$my_uri?p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$file"},
 997                                              escapeHTML($file) . $mode_chnge) . "\n" .
 998                                              "</div>\n";
 999                                } else {
1000                                        print $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$to_id;hb=$hash;f=$file"},
1001                                              escapeHTML($file) . $mode_chnge) . "\n" .
1002                                              "</div>\n";
1003                                }
1004                                print "<div class=\"link\">\n";
1005                                if ($to_id ne $from_id) {
1006                                        print $cgi->a({-href => "$my_uri?p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$file"}, "diff") . " | ";
1007                                }
1008                                print $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$to_id;hb=$hash;f=$file"}, "blob") . " | " .
1009                                      $cgi->a({-href => "$my_uri?p=$project;a=history;h=$hash;f=$file"}, "history") . "\n" .
1010                                      "</div>\n";
1011                        }
1012                }
1013        }
1014        git_footer_html();
1015}
1016
1017sub git_blobdiff {
1018        mkdir($gittmp, 0700);
1019        git_header_html();
1020        if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
1021                print "<div class=\"page_nav\">\n" .
1022                      $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash_base"}, "commit") .
1023                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash_base"}, "commitdiff") .
1024                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=" . $co{'tree'} . ";hb=$hash_base"}, "tree");
1025                        if (defined $file_name) {
1026                                print " | " . $cgi->a({-href => "$my_uri?p=$project;a=history;h=$hash_base;f=$file_name"}, "history");
1027                        }
1028                print "<br/><br/>\n" .
1029                      "</div>\n";
1030                print "<div>\n" .
1031                      $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash_base", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
1032                      "</div>\n";
1033        } else {
1034                print "<div class=\"page_nav\">\n" .
1035                      "<br/><br/></div>\n" .
1036                      "<div class=\"title\">$hash vs $hash_parent</div>\n";
1037        }
1038        if (defined $file_name) {
1039                print "<div class=\"page_path\">\n" .
1040                      "/$file_name\n" .
1041                      "</div>\n";
1042        }
1043        print "<div class=\"page_body\">\n" .
1044              "<div class=\"diff_info\">blob:" .
1045              $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$hash_parent;hb=$hash_base;f=$file_name"}, $hash_parent) .
1046              " -> blob:" .
1047              $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$hash;hb=$hash_base;f=$file_name"}, $hash) .
1048              "</div>\n";
1049        git_diff_html($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash);
1050        print "</div>";
1051        git_footer_html();
1052}
1053
1054sub git_commitdiff {
1055        mkdir($gittmp, 0700);
1056        my %co = git_read_commit($hash);
1057        if (!%co) {
1058                die_error(undef, "Unknown commit object.");
1059        }
1060        open my $fd, "-|", "$gitbin/git-diff-tree -r " . $co{'parent'} . " $hash" || die_error(undef, "Open failed.");
1061        my (@difftree) = map { chomp; $_ } <$fd>;
1062        close $fd || die_error(undef, "Reading diff-tree failed.");
1063
1064        git_header_html();
1065        print "<div class=\"page_nav\">\n" .
1066              $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash"}, "commit") . " | \n" .
1067              $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash"}, "commitdiff") . " | \n" .
1068              $cgi->a({-href => "$my_uri?p=$project;a=tree;h=" .  $co{'tree'} . ";hb=$hash"}, "tree") . "\n" .
1069              "<br/><br/></div>\n";
1070        print "<div>\n" .
1071              $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
1072              "</div>\n";
1073        print "<div class=\"page_body\">\n";
1074        foreach my $line (@difftree) {
1075                # '*100644->100644      blob    8e5f9bbdf4de94a1bc4b4da8cb06677ce0a57716->8da3a306d0c0c070d87048d14a033df02f40a154      Makefile'
1076                $line =~ m/^(.)(.*)\t(.*)\t(.*)\t(.*)$/;
1077                my $op = $1;
1078                my $mode = $2;
1079                my $type = $3;
1080                my $id = $4;
1081                my $file = $5;
1082                if ($type eq "blob") {
1083                        if ($op eq "+") {
1084                                print "<div class=\"diff_info\">" .  file_type($mode) . ":" .
1085                                      $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$id;hb=$hash;f=$file"}, $id) . "(new)" .
1086                                      "</div>\n";
1087                                git_diff_html(undef, "/dev/null", $id, "b/$file");
1088                        } elsif ($op eq "-") {
1089                                print "<div class=\"diff_info\">" . file_type($mode) . ":" .
1090                                      $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$id;hb=$hash;f=$file"}, $id) . "(deleted)" .
1091                                      "</div>\n";
1092                                git_diff_html($id, "a/$file", undef, "/dev/null");
1093                        } elsif ($op eq "*") {
1094                                $id =~ m/([0-9a-fA-F]+)->([0-9a-fA-F]+)/;
1095                                my $from_id = $1;
1096                                my $to_id = $2;
1097                                $mode =~ m/([0-7]+)->([0-7]+)/;
1098                                my $from_mode = $1;
1099                                my $to_mode = $2;
1100                                if ($from_id ne $to_id) {
1101                                        print "<div class=\"diff_info\">" .
1102                                              file_type($from_mode) . ":" . $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$from_id;hb=$hash;f=$file"}, $from_id) .
1103                                              " -> " .
1104                                              file_type($to_mode) . ":" . $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$to_id;hb=$hash;f=$file"}, $to_id);
1105                                        print "</div>\n";
1106                                        git_diff_html($from_id, "a/$file",  $to_id, "b/$file");
1107                                }
1108                        }
1109                }
1110        }
1111        print "<br/>\n" .
1112              "</div>";
1113        git_footer_html();
1114}
1115
1116sub git_history {
1117        if (!defined $hash) {
1118                $hash = git_read_head($project);
1119        }
1120        my %co = git_read_commit($hash);
1121        if (!%co) {
1122                die_error(undef, "Unknown commit object.");
1123        }
1124        git_header_html();
1125        print "<div class=\"page_nav\">\n" .
1126              $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash"}, "commit") . " | " .
1127              $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash"}, "commitdiff") . " | " .
1128              $cgi->a({-href => "$my_uri?p=$project;a=tree;h=" . $co{'tree'} . ";hb=$hash"}, "tree") .
1129              "<br/><br/>\n" .
1130              "</div>\n";
1131        print "<div>\n" .
1132              $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
1133              "</div>\n";
1134        print "<div class=\"page_path\">\n" .
1135              "/$file_name<br/>\n";
1136        print "</div>\n";
1137        open my $fd, "-|", "$gitbin/git-rev-list $hash | $gitbin/git-diff-tree -r --stdin $file_name";
1138        my $commit;
1139        while (my $line = <$fd>) {
1140                if ($line =~ m/^([0-9a-fA-F]{40}) /){
1141                        $commit = $1;
1142                        next;
1143                }
1144                if ($line =~ m/^(.)(.*)\t(.*)\t(.*)\t(.*)$/ && (defined $commit)) {
1145                        my $type = $3;
1146                        my $file = $5;
1147                        if ($file ne $file_name || $type ne "blob") {
1148                                next;
1149                        }
1150                        my %co = git_read_commit($commit);
1151                        if (!%co) {
1152                                next;
1153                        }
1154                        print "<div class=\"list\">\n" .
1155                              $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit"},
1156                              "<span class=\"log_age\">" . $co{'age_string'} . "</span>" . escapeHTML($co{'title'})) . "\n" .
1157                              "</div>\n";
1158                        print "<div class=\"link\">\n" .
1159                              $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit"}, "commit") .
1160                              " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=" .  $co{'tree'} . ";hb=$commit"}, "tree") .
1161                              " | " . $cgi->a({-href => "$my_uri?p=$project;a=blob;hb=$commit;f=" . $file}, "blob");
1162                        my $blob = git_get_hash_by_path($hash, $file_name);
1163                        my $blob_parent = git_get_hash_by_path($commit, $file_name);
1164                        if (defined $blob && defined $blob_parent && $blob ne $blob_parent) {
1165                                print " | " . $cgi->a({-href => "$my_uri?p=$project;a=blobdiff;h=$blob;hp=$blob_parent;hb=$commit;f=" . $file}, "diff");
1166                        }
1167                        print "<br/>\n" .
1168                              "</div>\n";
1169                        undef $commit;
1170                }
1171        }
1172        close $fd;
1173        git_footer_html();
1174}