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