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