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