gitweb.cgion commit v150 (0db3797)
   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 =           "150";
  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 @taglist;
 655
 656        opendir my $dh, "$projectroot/$project/$ref_dir";
 657        my @tags = grep !m/^\./, readdir $dh;
 658        closedir($dh);
 659        foreach my $tag_file (@tags) {
 660                my $tag_id = git_read_hash("$project/$ref_dir/$tag_file");
 661                my $type = git_get_type($tag_id) || next;
 662                my %tag_item;
 663                my %co;
 664                if ($type eq "tag") {
 665                        my %tag = git_read_tag($tag_id);
 666                        if ($tag{'type'} eq "commit") {
 667                                %co = git_read_commit($tag{'object'});
 668                        }
 669                        $tag_item{'type'} = $tag{'type'};
 670                        $tag_item{'name'} = $tag{'name'};
 671                        $tag_item{'id'} = $tag{'object'};
 672                } elsif ($type eq "commit"){
 673                        %co = git_read_commit($tag_id);
 674                        $tag_item{'type'} = "commit";
 675                        $tag_item{'name'} = $tag_file;
 676                        $tag_item{'title'} = $co{'title'};
 677                        $tag_item{'id'} = $tag_id;
 678                }
 679                $tag_item{'epoch'} = $co{'author_epoch'} || 0;
 680                $tag_item{'age'} = $co{'age_string'} || "unknown";
 681
 682                push @taglist, \%tag_item;
 683        }
 684        # sort tags by age
 685        @taglist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @taglist;
 686        return \@taglist;
 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                              "</tr>";
 748                } else {
 749                        print "<td>" . $cgi->a({-href => "$my_uri?p=$project;a=log"}, "...") . "</td>\n" .
 750                        "</tr>";
 751                        last;
 752                }
 753        }
 754        print "</table\n>" .
 755              "</div>\n";
 756
 757        my $taglist = git_read_refs("refs/tags");
 758        if (defined @$taglist) {
 759                print "<div>\n" .
 760                      $cgi->a({-href => "$my_uri?p=$project;a=tags", -class => "title"}, "tags") .
 761                      "</div>\n";
 762                my $i = 10;
 763                print  "<div class=\"page_body\">\n" .
 764                       "<table cellspacing=\"0\">\n";
 765                foreach my $entry (@$taglist) {
 766                        my %tag = %$entry;
 767                        print "<tr>\n";
 768                        if (--$i > 0) {
 769                                print "<td>$tag{'age'}</td>\n" .
 770                                      "<td>" . $cgi->a({-href => "$my_uri?p=$project;a=$tag{'type'};h=$tag{'id'}"}, escapeHTML($tag{'name'})) . "</td>\n" .
 771                                      "</tr>";
 772                        } else {
 773                                print "<td>" . $cgi->a({-href => "$my_uri?p=$project;a=tags"}, "...") . "</td>\n" .
 774                                "</tr>";
 775                                last;
 776                        }
 777                }
 778                print "</table\n>" .
 779                      "</div>\n";
 780        }
 781
 782        my $branchlist = git_read_refs("refs/heads");
 783        if (defined @$branchlist) {
 784                print "<div>\n" .
 785                      $cgi->a({-href => "$my_uri?p=$project;a=branches", -class => "title"}, "branches") .
 786                      "</div>\n";
 787                my $i = 10;
 788                print  "<div class=\"page_body\">\n" .
 789                       "<table cellspacing=\"0\">\n";
 790                foreach my $entry (@$branchlist) {
 791                        my %tag = %$entry;
 792                        print "<tr>\n";
 793                        if (--$i > 0) {
 794                                print "<td>$tag{'age'}</td>\n" .
 795                                      "<td>" . $cgi->a({-href => "$my_uri?p=$project;a=log;h=$tag{'id'}"}, escapeHTML($tag{'name'})) . "</td>\n" .
 796                                      "</tr>";
 797                        } else {
 798                                print "<td>" . $cgi->a({-href => "$my_uri?p=$project;a=branches"}, "...") . "</td>\n" .
 799                                "</tr>";
 800                                last;
 801                        }
 802                }
 803                print "</table\n>" .
 804                      "</div>\n";
 805        }
 806        git_footer_html();
 807}
 808
 809sub git_tags {
 810        my $head = git_read_hash("$project/HEAD");
 811        git_header_html();
 812        print "<div class=\"page_nav\">\n" .
 813              $cgi->a({-href => "$my_uri?p=$project;a=log"}, "log") .
 814              " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$head"}, "commit") .
 815              " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree"}, "tree") .
 816              "<br/><br/>\n" .
 817              "</div>\n";
 818        my $taglist = git_read_refs("refs/tags");
 819        print "<div>\n" .
 820              $cgi->a({-href => "$my_uri?p=$project;a=summary", -class => "title"}, "tags") .
 821              "</div>\n";
 822        if (defined @$taglist) {
 823                foreach my $entry (@$taglist) {
 824                        my %tag = %$entry;
 825                        print "<div class=\"list\">\n" .
 826                              $cgi->a({-href => "$my_uri?p=$project;a=$tag{'type'};h=$tag{'id'}"},
 827                              "<span class=\"age\">$tag{'age'}</span>" .  escapeHTML($tag{'name'})) . "\n" .
 828                              "</div>\n";
 829                }
 830        }
 831        print "<div class=\"list\"><br/></div>\n";
 832        git_footer_html();
 833}
 834
 835sub git_branches {
 836        my $head = git_read_hash("$project/HEAD");
 837        git_header_html();
 838        print "<div class=\"page_nav\">\n" .
 839              $cgi->a({-href => "$my_uri?p=$project;a=log"}, "log") .
 840              " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$head"}, "commit") .
 841              " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree"}, "tree") .
 842              "<br/><br/>\n" .
 843              "</div>\n";
 844        my $taglist = git_read_refs("refs/heads");
 845        print "<div>\n" .
 846              $cgi->a({-href => "$my_uri?p=$project;a=summary", -class => "title"}, "branches") .
 847              "</div>\n";
 848        if (defined @$taglist) {
 849                foreach my $entry (@$taglist) {
 850                        my %tag = %$entry;
 851                        print "<div class=\"list\">\n" .
 852                              $cgi->a({-href => "$my_uri?p=$project;a=log;h=$tag{'id'}"},
 853                              "<span class=\"age\">$tag{'age'}</span>" .  escapeHTML($tag{'name'})) . "\n" .
 854                              "</div>\n";
 855                }
 856        }
 857        print "<div class=\"list\"><br/></div>\n";
 858        git_footer_html();
 859}
 860
 861sub git_get_hash_by_path {
 862        my $base = shift;
 863        my $path = shift;
 864
 865        my $tree = $base;
 866        my @parts = split '/', $path;
 867        while (my $part = shift @parts) {
 868                open my $fd, "-|", "$gitbin/git-ls-tree $tree" || die_error(undef, "Open git-ls-tree failed.");
 869                my (@entries) = map { chomp; $_ } <$fd>;
 870                close $fd || return undef;
 871                foreach my $line (@entries) {
 872                        #'100644        blob    0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa        panic.c'
 873                        $line =~ m/^([0-9]+)\t(.*)\t(.*)\t(.*)$/;
 874                        my $t_mode = $1;
 875                        my $t_type = $2;
 876                        my $t_hash = $3;
 877                        my $t_name = $4;
 878                        if ($t_name eq $part) {
 879                                if (!(@parts)) {
 880                                        return $t_hash;
 881                                }
 882                                if ($t_type eq "tree") {
 883                                        $tree = $t_hash;
 884                                }
 885                                last;
 886                        }
 887                }
 888        }
 889}
 890
 891sub git_blob {
 892        if (!defined $hash && defined $file_name) {
 893                my $base = $hash_base || git_read_hash("$project/HEAD");
 894                $hash = git_get_hash_by_path($base, $file_name, "blob");
 895        }
 896        open my $fd, "-|", "$gitbin/git-cat-file blob $hash" || die_error(undef, "Open failed.");
 897        my $base = $file_name || "";
 898        git_header_html();
 899        if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
 900                print "<div class=\"page_nav\">\n" .
 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                if (defined $file_name) {
 905                        print " | " . $cgi->a({-href => "$my_uri?p=$project;a=history;h=$hash_base;f=$file_name"}, "history");
 906                }
 907                print "<br/><br/>\n" .
 908                      "</div>\n";
 909                print "<div>" .
 910                      $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash_base", -class => "title"}, escapeHTML($co{'title'})) .
 911                      "</div>\n";
 912        } else {
 913                print "<div class=\"page_nav\">\n" .
 914                      "<br/><br/></div>\n" .
 915                      "<div class=\"title\">$hash</div>\n";
 916        }
 917        if (defined $file_name) {
 918                print "<div class=\"page_path\">/$file_name</div>\n";
 919        }
 920        print "<div class=\"page_body\">\n";
 921        my $nr;
 922        while (my $line = <$fd>) {
 923                chomp $line;
 924                $nr++;
 925                print "<div class=\"pre\">";
 926                printf "<span style=\"color:#999999;\">%4i</span>", $nr;
 927                print " " .escapeHTML($line) . "</div>\n";
 928        }
 929        close $fd || print "Reading blob failed.\n";
 930        print "</div>";
 931        git_footer_html();
 932}
 933
 934sub git_tree {
 935        if (!defined $hash) {
 936                $hash = git_read_hash("$project/HEAD");
 937                if (defined $file_name) {
 938                        my $base = $hash_base || git_read_hash("$project/HEAD");
 939                        $hash = git_get_hash_by_path($base, $file_name, "tree");
 940                }
 941        }
 942        if (!defined $hash_base) {
 943                $hash_base = git_read_hash("$project/HEAD");
 944        }
 945        open my $fd, "-|", "$gitbin/git-ls-tree $hash" || die_error(undef, "Open git-ls-tree failed.");
 946        my (@entries) = map { chomp; $_ } <$fd>;
 947        close $fd || die_error(undef, "Reading tree failed.");
 948
 949        git_header_html();
 950        my $base_key = "";
 951        my $file_key = "";
 952        my $base = "";
 953        if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
 954                $base_key = ";hb=$hash_base";
 955                print "<div class=\"page_nav\">\n" .
 956                      $cgi->a({-href => "$my_uri?p=$project;a=log"}, "log") .
 957                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash_base"}, "commit") .
 958                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash_base"}, "commitdiff") .
 959                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash_base"}, "tree") .
 960                      "<br/><br/>\n" .
 961                      "</div>\n";
 962                print "<div>\n" .
 963                      $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash_base", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
 964                      "</div>\n";
 965        } else {
 966                print "<div class=\"page_nav\">\n";
 967                print "<br/><br/></div>\n";
 968                print "<div class=\"title\">$hash</div>\n";
 969        }
 970        if (defined $file_name) {
 971                $base = "$file_name/";
 972                print "<div class=\"page_path\">/$file_name</div>\n";
 973        } else {
 974                print "<div class=\"page_path\">/</div>\n";
 975        }
 976        print "<div class=\"page_body\">\n";
 977        print "<table cellspacing=\"0\">\n";
 978        foreach my $line (@entries) {
 979                #'100644        blob    0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa        panic.c'
 980                $line =~ m/^([0-9]+)\t(.*)\t(.*)\t(.*)$/;
 981                my $t_mode = $1;
 982                my $t_type = $2;
 983                my $t_hash = $3;
 984                my $t_name = $4;
 985                $file_key = ";f=$base$t_name";
 986                print "<tr>\n" .
 987                      "<td class=\"pre\">" . mode_str($t_mode) . "</td>\n";
 988                if ($t_type eq "blob") {
 989                        print "<td class=\"pre\">$t_name</td>\n";
 990                        print "<td class=\"link\">" .
 991                              $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$t_hash" . $base_key . $file_key}, "blob") .
 992                              " | " . $cgi->a({-href => "$my_uri?p=$project;a=history;h=$hash_base" . $file_key}, "history") .
 993                              "</td>\n";
 994                } elsif ($t_type eq "tree") {
 995                        print "<td class=\"pre\">" .
 996                              $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$t_hash" . $base_key . $file_key}, $t_name) .
 997                              "</td>\n";
 998                }
 999                print "</tr>\n";
1000        }
1001        print "</table>\n" .
1002              "</div>";
1003        git_footer_html();
1004}
1005
1006sub git_rss {
1007        open my $fd, "-|", "$gitbin/git-rev-list --max-count=20 " . git_read_hash("$project/HEAD") || die_error(undef, "Open failed.");
1008        my (@revlist) = map { chomp; $_ } <$fd>;
1009        close $fd || die_error(undef, "Reading rev-list failed.");
1010
1011        print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
1012        print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".
1013              "<rss version=\"0.91\">\n";
1014        print "<channel>\n";
1015        print "<title>$project</title>\n".
1016              "<link> $my_url/$project/log</link>\n".
1017              "<description>$project log</description>\n".
1018              "<language>en</language>\n";
1019
1020        foreach my $commit (@revlist) {
1021                my %co = git_read_commit($commit);
1022                my %ad = date_str($co{'author_epoch'});
1023                print "<item>\n" .
1024                      "\t<title>" . sprintf("%d %s %02d:%02d", $ad{'mday'}, $ad{'month'}, $ad{'hour'}, $ad{'minute'}) . " - " . escapeHTML($co{'title'}) . "</title>\n" .
1025                      "\t<link> $my_url?p=$project;a=commit;h=$commit</link>\n" .
1026                      "\t<description>";
1027                my $comment = $co{'comment'};
1028                foreach my $line (@$comment) {
1029                        print escapeHTML($line) . "<br/>\n";
1030                }
1031                print "\t</description>\n" .
1032                      "</item>\n";
1033        }
1034        print "</channel></rss>";
1035}
1036
1037sub git_log {
1038        if (!defined $hash) {
1039                $hash = git_read_hash("$project/HEAD");
1040        }
1041        my $limit_option = "";
1042        if (!defined $time_back) {
1043                $limit_option = "--max-count=10";
1044        } elsif ($time_back > 0) {
1045                my $date = time - $time_back*24*60*60;
1046                $limit_option = "--max-age=$date";
1047        }
1048        open my $fd, "-|", "$gitbin/git-rev-list $limit_option $hash" || die_error(undef, "Open failed.");
1049        my (@revlist) = map { chomp; $_ } <$fd>;
1050        close $fd || die_error(undef, "Reading rev-list failed.");
1051
1052        git_header_html();
1053        print "<div class=\"page_nav\">\n";
1054        print $cgi->a({-href => "$my_uri?p=$project;a=log"}, "last 10") . " | " .
1055              $cgi->a({-href => "$my_uri?p=$project;a=log;t=1"}, "day") . " | " .
1056              $cgi->a({-href => "$my_uri?p=$project;a=log;t=7"}, "week") . " | " .
1057              $cgi->a({-href => "$my_uri?p=$project;a=log;t=31"}, "month") . " | " .
1058              $cgi->a({-href => "$my_uri?p=$project;a=log;t=365"}, "year") . " | " .
1059              $cgi->a({-href => "$my_uri?p=$project;a=log;t=0"}, "all") . "<br/>\n";
1060        print "<br/>\n" .
1061              "</div>\n";
1062
1063        if (!@revlist) {
1064                my %co = git_read_commit($hash);
1065                print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
1066        }
1067
1068        foreach my $commit (@revlist) {
1069                my %co = git_read_commit($commit);
1070                next if !%co;
1071                my %ad = date_str($co{'author_epoch'});
1072                print "<div>\n" .
1073                      $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit", -class => "title"},
1074                      "<span class=\"age\">$co{'age_string'}</span>" . escapeHTML($co{'title'})) . "\n" .
1075                      "</div>\n";
1076                print "<div class=\"title_text\">\n" .
1077                      "<div class=\"log_link\">\n" .
1078                      $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit"}, "commit") .
1079                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$commit"}, "commitdiff") .
1080                      "<br/>\n" .
1081                      "</div>\n" .
1082                      "<i>" . escapeHTML($co{'author_name'}) .  " [$ad{'rfc2822'}]</i><br/>\n" .
1083                      "</div>\n" .
1084                      "<div class=\"log_body\">\n";
1085                my $comment = $co{'comment'};
1086                my $empty = 0;
1087                foreach my $line (@$comment) {
1088                        if ($line =~ m/^(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1089                                next;
1090                        }
1091                        if ($line eq "") {
1092                                if ($empty) {
1093                                        next;
1094                                }
1095                                $empty = 1;
1096                        } else {
1097                                $empty = 0;
1098                        }
1099                        print escapeHTML($line) . "<br/>\n";
1100                }
1101                if (!$empty) {
1102                        print "<br/>\n";
1103                }
1104                print "</div>\n";
1105        }
1106        git_footer_html();
1107}
1108
1109sub git_commit {
1110        my %co = git_read_commit($hash);
1111        if (!%co) {
1112                die_error(undef, "Unknown commit object.");
1113        }
1114        my %ad = date_str($co{'author_epoch'}, $co{'author_tz'});
1115        my %cd = date_str($co{'committer_epoch'}, $co{'committer_tz'});
1116
1117        my @difftree;
1118        if (defined $co{'parent'}) {
1119                open my $fd, "-|", "$gitbin/git-diff-tree -r $co{'parent'} $hash" || die_error(undef, "Open failed.");
1120                @difftree = map { chomp; $_ } <$fd>;
1121                close $fd || die_error(undef, "Reading diff-tree failed.");
1122        } else {
1123                # fake git-diff-tree output for initial revision
1124                open my $fd, "-|", "$gitbin/git-ls-tree -r $hash" || die_error(undef, "Open failed.");
1125                @difftree = map { chomp;  "+" . $_ } <$fd>;
1126                close $fd || die_error(undef, "Reading ls-tree failed.");
1127        }
1128        git_header_html();
1129        print "<div class=\"page_nav\">\n" .
1130              $cgi->a({-href => "$my_uri?p=$project;a=log"}, "log") .
1131              " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash"}, "commit");
1132        if (defined $co{'parent'}) {
1133                print " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash"}, "commitdiff");
1134        }
1135        print " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash"}, "tree") . "\n" .
1136              "<br/><br/></div>\n";
1137        if (defined $co{'parent'}) {
1138                print "<div>\n" .
1139                      $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
1140                      "</div>\n";
1141        } else {
1142                print "<div>\n" .
1143                      $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
1144                      "</div>\n";
1145        }
1146        print "<div class=\"title_text\">\n" .
1147              "<table cellspacing=\"0\">\n";
1148        print "<tr><td>author</td><td>" . escapeHTML($co{'author'}) . "</td></tr>\n".
1149              "<tr><td></td><td> $ad{'rfc2822'}";
1150        if ($ad{'hour_local'} < 6) {
1151                printf(" (<span style=\"color: #cc0000;\">%02d:%02d</span> %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1152        } else {
1153                printf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1154        }
1155        print "</td></tr>\n";
1156        print "<tr><td>committer</td><td>" . escapeHTML($co{'committer'}) . "</td></tr>\n";
1157        print "<tr><td></td><td> $cd{'rfc2822'}" . sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) . "</td></tr>\n";
1158        print "<tr><td>commit</td><td style=\"font-family: monospace;\">$hash</td></tr>\n";
1159        print "<tr><td>tree</td><td style=\"font-family: monospace;\">" .
1160              $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash"}, $co{'tree'}) . "</td></tr>\n";
1161        my $parents  = $co{'parents'};
1162        foreach my $par (@$parents) {
1163                print "<tr><td>parent</td><td style=\"font-family: monospace;\">" .
1164                      $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$par"}, $par) . "</td></tr>\n";
1165        }
1166        print "</table>". 
1167              "</div>\n";
1168        print "<div class=\"page_body\">\n";
1169        my $comment = $co{'comment'};
1170        my $empty = 0;
1171        my $signed = 0;
1172        foreach my $line (@$comment) {
1173                # print only one empty line
1174                if ($line eq "") {
1175                        if ($empty || $signed) {
1176                                next;
1177                        }
1178                        $empty = 1;
1179                } else {
1180                        $empty = 0;
1181                }
1182                if ($line =~ m/^(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1183                        $signed = 1;
1184                        print "<span style=\"color: #888888\">" . escapeHTML($line) . "</span><br/>\n";
1185                } else {
1186                        $signed = 0;
1187                        print escapeHTML($line) . "<br/>\n";
1188                }
1189        }
1190        print "</div>\n";
1191        print "<div class=\"list_head\">\n";
1192        if ($#difftree > 10) {
1193                print(($#difftree + 1) . " files changed:\n");
1194        }
1195        print "</div>\n";
1196        foreach my $line (@difftree) {
1197                # '*100644->100644      blob    9f91a116d91926df3ba936a80f020a6ab1084d2b->bb90a0c3a91eb52020d0db0e8b4f94d30e02d596      net/ipv4/route.c'
1198                # '+100644      blob    4a83ab6cd565d21ab0385bac6643826b83c2fcd4        arch/arm/lib/bitops.h'
1199                # '*100664->100644      blob    b1a8e3dd5556b61dd771d32307c6ee5d7150fa43->b1a8e3dd5556b61dd771d32307c6ee5d7150fa43      show-files.c'
1200                # '*100664->100644      blob    d08e895238bac36d8220586fdc28c27e1a7a76d3->d08e895238bac36d8220586fdc28c27e1a7a76d3      update-cache.c'
1201                $line =~ m/^(.)(.*)\t(.*)\t(.*)\t(.*)$/;
1202                my $op = $1;
1203                my $mode = $2;
1204                my $type = $3;
1205                my $id = $4;
1206                my $file = $5;
1207                if ($type eq "blob") {
1208                        if ($op eq "+") {
1209                                my $mode_chng = "";
1210                                if (S_ISREG(oct $mode)) {
1211                                        $mode_chng = sprintf(" with mode: %04o", (oct $mode) & 0777);
1212                                }
1213                                print "<div class=\"list\">\n" .
1214                                      $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$id;hb=$hash;f=$file"},
1215                                      escapeHTML($file) . " <span style=\"color: #008000;\">[new " . file_type($mode) . "$mode_chng]</span>") . "\n" .
1216                                      "</div>\n";
1217                                print "<div class=\"list_link\">\n" .
1218                                      $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$id;hb=$hash;f=$file"}, "blob") . "\n" .
1219                                      "</div>\n";
1220                        } elsif ($op eq "-") {
1221                                print "<div class=\"list\">\n" .
1222                                      $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$id;hb=$hash;f=$file"},
1223                                      escapeHTML($file) .  " <span style=\"color: #c00000;\">[deleted " . file_type($mode) . "]</span>") . "\n" .
1224                                      "</div>";
1225                                print "<div class=\"list_link\">\n" .
1226                                      $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$id;hb=$hash;f=$file"}, "blob") . " | " .
1227                                      $cgi->a({-href => "$my_uri?p=$project;a=history;h=$hash;f=$file"}, "history") . "\n" .
1228                                      "</div>\n";
1229                        } elsif ($op eq "*") {
1230                                $id =~ m/([0-9a-fA-F]+)->([0-9a-fA-F]+)/;
1231                                my $from_id = $1;
1232                                my $to_id = $2;
1233                                $mode =~ m/^([0-7]{6})->([0-7]{6})$/;
1234                                my $from_mode = $1;
1235                                my $to_mode = $2;
1236                                my $mode_chnge = "";
1237                                if ($from_mode != $to_mode) {
1238                                        $mode_chnge = " <span style=\"color: #777777;\">[changed";
1239                                        if (((oct $from_mode) & S_IFMT) != ((oct $to_mode) & S_IFMT)) {
1240                                                $mode_chnge .= " from " . file_type($from_mode) . " to " . file_type($to_mode);
1241                                        }
1242                                        if (((oct $from_mode) & 0777) != ((oct $to_mode) & 0777)) {
1243                                                if (S_ISREG($from_mode) && S_ISREG($to_mode)) {
1244                                                        $mode_chnge .= sprintf(" mode: %04o->%04o", (oct $from_mode) & 0777, (oct $to_mode) & 0777);
1245                                                } elsif (S_ISREG($to_mode)) {
1246                                                        $mode_chnge .= sprintf(" mode: %04o", (oct $to_mode) & 0777);
1247                                                }
1248                                        }
1249                                        $mode_chnge .= "]</span>\n";
1250                                }
1251                                print "<div class=\"list\">\n";
1252                                if ($to_id ne $from_id) {
1253                                        print $cgi->a({-href => "$my_uri?p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$file"},
1254                                              escapeHTML($file) . $mode_chnge) . "\n" .
1255                                              "</div>\n";
1256                                } else {
1257                                        print $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$to_id;hb=$hash;f=$file"},
1258                                              escapeHTML($file) . $mode_chnge) . "\n" .
1259                                              "</div>\n";
1260                                }
1261                                print "<div class=\"list_link\">\n";
1262                                if ($to_id ne $from_id) {
1263                                        print $cgi->a({-href => "$my_uri?p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$file"}, "diff") . " | ";
1264                                }
1265                                print $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$to_id;hb=$hash;f=$file"}, "blob") . " | " .
1266                                      $cgi->a({-href => "$my_uri?p=$project;a=history;h=$hash;f=$file"}, "history") . "\n" .
1267                                      "</div>\n";
1268                        }
1269                }
1270        }
1271        git_footer_html();
1272}
1273
1274sub git_blobdiff {
1275        mkdir($gittmp, 0700);
1276        git_header_html();
1277        if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
1278                print "<div class=\"page_nav\">\n" .
1279                      $cgi->a({-href => "$my_uri?p=$project;a=log"}, "log") .
1280                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash_base"}, "commit") .
1281                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash_base"}, "commitdiff") .
1282                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash_base"}, "tree");
1283                        if (defined $file_name) {
1284                                print " | " . $cgi->a({-href => "$my_uri?p=$project;a=history;h=$hash_base;f=$file_name"}, "history");
1285                        }
1286                print "<br/><br/>\n" .
1287                      "</div>\n";
1288                print "<div>\n" .
1289                      $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash_base", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
1290                      "</div>\n";
1291        } else {
1292                print "<div class=\"page_nav\">\n" .
1293                      "<br/><br/></div>\n" .
1294                      "<div class=\"title\">$hash vs $hash_parent</div>\n";
1295        }
1296        if (defined $file_name) {
1297                print "<div class=\"page_path\">\n" .
1298                      "/$file_name\n" .
1299                      "</div>\n";
1300        }
1301        print "<div class=\"page_body\">\n" .
1302              "<div class=\"diff_info\">blob:" .
1303              $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$hash_parent;hb=$hash_base;f=$file_name"}, $hash_parent) .
1304              " -> blob:" .
1305              $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$hash;hb=$hash_base;f=$file_name"}, $hash) .
1306              "</div>\n";
1307        git_diff_html($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash);
1308        print "</div>";
1309        git_footer_html();
1310}
1311
1312sub git_commitdiff {
1313        mkdir($gittmp, 0700);
1314        my %co = git_read_commit($hash);
1315        if (!%co) {
1316                die_error(undef, "Unknown commit object.");
1317        }
1318        open my $fd, "-|", "$gitbin/git-diff-tree -r $co{'parent'} $hash" || die_error(undef, "Open failed.");
1319        my (@difftree) = map { chomp; $_ } <$fd>;
1320        close $fd || die_error(undef, "Reading diff-tree failed.");
1321
1322        git_header_html();
1323        print "<div class=\"page_nav\">\n" .
1324              $cgi->a({-href => "$my_uri?p=$project;a=log"}, "log") .
1325              " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash"}, "commit") .
1326              " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash"}, "commitdiff") .
1327              " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=" .  $co{'tree'} . ";hb=$hash"}, "tree") .
1328              "<br/><br/></div>\n";
1329        print "<div>\n" .
1330              $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
1331              "</div>\n";
1332        print "<div class=\"page_body\">\n";
1333        my $comment = $co{'comment'};
1334        my $empty = 0;
1335        my $signed = 0;
1336        foreach my $line (@$comment) {
1337                if ($line =~ m/^(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1338                        next;
1339                }
1340                if ($line eq "") {
1341                        if ($empty) {
1342                                next;
1343                        }
1344                        $empty = 1;
1345                } else {
1346                        $empty = 0;
1347                }
1348                print escapeHTML($line) . "<br/>\n";
1349        }
1350        print "<br/>\n";
1351        foreach my $line (@difftree) {
1352                # '*100644->100644      blob    8e5f9bbdf4de94a1bc4b4da8cb06677ce0a57716->8da3a306d0c0c070d87048d14a033df02f40a154      Makefile'
1353                $line =~ m/^(.)(.*)\t(.*)\t(.*)\t(.*)$/;
1354                my $op = $1;
1355                my $mode = $2;
1356                my $type = $3;
1357                my $id = $4;
1358                my $file = $5;
1359                if ($type eq "blob") {
1360                        if ($op eq "+") {
1361                                print "<div class=\"diff_info\">" .  file_type($mode) . ":" .
1362                                      $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$id;hb=$hash;f=$file"}, $id) . "(new)" .
1363                                      "</div>\n";
1364                                git_diff_html(undef, "/dev/null", $id, "b/$file");
1365                        } elsif ($op eq "-") {
1366                                print "<div class=\"diff_info\">" . file_type($mode) . ":" .
1367                                      $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$id;hb=$hash;f=$file"}, $id) . "(deleted)" .
1368                                      "</div>\n";
1369                                git_diff_html($id, "a/$file", undef, "/dev/null");
1370                        } elsif ($op eq "*") {
1371                                $id =~ m/([0-9a-fA-F]+)->([0-9a-fA-F]+)/;
1372                                my $from_id = $1;
1373                                my $to_id = $2;
1374                                $mode =~ m/([0-7]+)->([0-7]+)/;
1375                                my $from_mode = $1;
1376                                my $to_mode = $2;
1377                                if ($from_id ne $to_id) {
1378                                        print "<div class=\"diff_info\">" .
1379                                              file_type($from_mode) . ":" . $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$from_id;hb=$hash;f=$file"}, $from_id) .
1380                                              " -> " .
1381                                              file_type($to_mode) . ":" . $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$to_id;hb=$hash;f=$file"}, $to_id);
1382                                        print "</div>\n";
1383                                        git_diff_html($from_id, "a/$file",  $to_id, "b/$file");
1384                                }
1385                        }
1386                }
1387        }
1388        print "<br/>\n" .
1389              "</div>";
1390        git_footer_html();
1391}
1392
1393sub git_history {
1394        if (!defined $hash) {
1395                $hash = git_read_hash("$project/HEAD");
1396        }
1397        my %co = git_read_commit($hash);
1398        if (!%co) {
1399                die_error(undef, "Unknown commit object.");
1400        }
1401        git_header_html();
1402        print "<div class=\"page_nav\">\n" .
1403              $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash"}, "commit") . " | " .
1404              $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash"}, "commitdiff") . " | " .
1405              $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash"}, "tree") .
1406              "<br/><br/>\n" .
1407              "</div>\n";
1408        print "<div>\n" .
1409              $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
1410              "</div>\n";
1411        print "<div class=\"page_path\">\n" .
1412              "/$file_name<br/>\n";
1413        print "</div>\n";
1414        open my $fd, "-|", "$gitbin/git-rev-list $hash | $gitbin/git-diff-tree -r --stdin $file_name";
1415        my $commit;
1416        while (my $line = <$fd>) {
1417                if ($line =~ m/^([0-9a-fA-F]{40}) /){
1418                        $commit = $1;
1419                        next;
1420                }
1421                if ($line =~ m/^(.)(.*)\t(.*)\t(.*)\t(.*)$/ && (defined $commit)) {
1422                        my $type = $3;
1423                        my $file = $5;
1424                        if ($file ne $file_name || $type ne "blob") {
1425                                next;
1426                        }
1427                        my %co = git_read_commit($commit);
1428                        if (!%co) {
1429                                next;
1430                        }
1431                        print "<div class=\"list\">\n" .
1432                              $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit"},
1433                              "<span class=\"age\">$co{'age_string'}</span>" . escapeHTML($co{'title'})) . "\n" .
1434                              "</div>\n";
1435                        print "<div class=\"list_link\">\n" .
1436                              $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit"}, "commit") .
1437                              " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=" .  $co{'tree'} . ";hb=$commit"}, "tree") .
1438                              " | " . $cgi->a({-href => "$my_uri?p=$project;a=blob;hb=$commit;f=$file"}, "blob");
1439                        my $blob = git_get_hash_by_path($hash, $file_name);
1440                        my $blob_parent = git_get_hash_by_path($commit, $file_name);
1441                        if (defined $blob && defined $blob_parent && $blob ne $blob_parent) {
1442                                print " | " . $cgi->a({-href => "$my_uri?p=$project;a=blobdiff;h=$blob;hp=$blob_parent;hb=$commit;f=$file"}, "diff");
1443                        }
1444                        print "<br/>\n" .
1445                              "</div>\n";
1446                        undef $commit;
1447                }
1448        }
1449        close $fd;
1450        git_footer_html();
1451}