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