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