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