821766cc295e27cb7bd59e99913766fb40ee6d68
   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 -nosticky);
  13use CGI::Util qw(unescape);
  14use CGI::Carp qw(fatalsToBrowser);
  15use Fcntl ':mode';
  16
  17my $cgi = new CGI;
  18my $version =           "203";
  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 $git_temp =          "/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} 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
 120
 121my $searchtext = $cgi->param('s');
 122if (defined $searchtext) {
 123        if ($searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
 124                undef $searchtext;
 125                die_error(undef, "Invalid search parameter.");
 126        }
 127        $searchtext = quotemeta $searchtext;
 128}
 129
 130if ($action eq "summary") {
 131        git_summary();
 132        exit;
 133} elsif ($action eq "branches") {
 134        git_branches();
 135        exit;
 136} elsif ($action eq "tags") {
 137        git_tags();
 138        exit;
 139} elsif ($action eq "blob") {
 140        git_blob();
 141        exit;
 142} elsif ($action eq "blob_plain") {
 143        git_blob_plain();
 144        exit;
 145} elsif ($action eq "tree") {
 146        git_tree();
 147        exit;
 148} elsif ($action eq "rss") {
 149        git_rss();
 150        exit;
 151} elsif ($action eq "commit") {
 152        git_commit();
 153        exit;
 154} elsif ($action eq "log") {
 155        git_log();
 156        exit;
 157} elsif ($action eq "blobdiff") {
 158        git_blobdiff();
 159        exit;
 160} elsif ($action eq "blobdiff_plain") {
 161        git_blobdiff_plain();
 162        exit;
 163} elsif ($action eq "commitdiff") {
 164        git_commitdiff();
 165        exit;
 166} elsif ($action eq "commitdiff_plain") {
 167        git_commitdiff_plain();
 168        exit;
 169} elsif ($action eq "history") {
 170        git_history();
 171        exit;
 172} elsif ($action eq "search") {
 173        git_search();
 174        exit;
 175} elsif ($action eq "shortlog") {
 176        git_shortlog();
 177        exit;
 178} else {
 179        undef $action;
 180        die_error(undef, "Unknown action.");
 181        exit;
 182}
 183
 184sub git_header_html {
 185        my $status = shift || "200 OK";
 186
 187        my $title = "git";
 188        if (defined $project) {
 189                $title .= " - $project";
 190                if (defined $action) {
 191                        $title .= "/$action";
 192                }
 193        }
 194        print $cgi->header(-type=>'text/html',  -charset => 'utf-8', -status=> $status);
 195        print <<EOF;
 196<?xml version="1.0" encoding="utf-8"?>
 197<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 198<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
 199<!-- git web interface v$version, (C) 2005, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke <ch\@gierke.de> -->
 200<head>
 201<title>$title</title>
 202$rss_link
 203<style type="text/css">
 204body { font-family: sans-serif; font-size: 12px; margin:0px; border:solid #d9d8d1; border-width:1px; margin:10px; }
 205a { color:#0000cc; }
 206a:hover, a:visited, a:active { color:#880000; }
 207div.page_header { height:25px; padding:8px; font-size:18px; font-weight:bold; background-color:#d9d8d1; }
 208div.page_header a:visited { color:#0000cc; }
 209div.page_header a:hover { color:#880000; }
 210div.page_nav { padding:8px; }
 211div.page_nav a:visited { color:#0000cc; }
 212div.page_path { padding:8px; border:solid #d9d8d1; border-width:0px 0px 1px}
 213div.page_footer { height:17px; padding:4px 8px; background-color: #d9d8d1; }
 214div.page_footer_text { float:left; color:#555555; font-style:italic; }
 215div.page_body { padding:8px; }
 216div.title, a.title {
 217        display:block; padding:6px 8px;
 218        font-weight:bold; background-color:#edece6; text-decoration:none; color:#000000;
 219}
 220a.title:hover { background-color: #d9d8d1; }
 221div.title_text { padding:6px 0px; border: solid #d9d8d1; border-width:0px 0px 1px; }
 222div.log_body { padding:8px 8px 8px 150px; }
 223span.age { position:relative; float:left; width:142px; font-style:italic; }
 224div.log_link {
 225        padding:0px 8px;
 226        font-size:10px; font-family:sans-serif; font-style:normal;
 227        position:relative; float:left; width:136px;
 228}
 229div.list_head { padding:6px 8px 4px; border:solid #d9d8d1; border-width:1px 0px 0px; font-style:italic; }
 230a.list { text-decoration:none; color:#000000; }
 231a.list:hover { color:#880000; }
 232table { padding:8px 4px; }
 233th { padding:2px 5px; font-size:12px; text-align:left; }
 234td { padding:2px 5px; font-size:12px; vertical-align:top; }
 235td.link { padding:2px 5px; font-family:sans-serif; font-size:10px; }
 236div.pre { font-family:monospace; font-size:12px; white-space:pre; }
 237div.diff_info { font-family:monospace; color:#000099; background-color:#edece6; font-style:italic; }
 238div.index_include { border:solid #d9d8d1; border-width:0px 0px 1px; padding:12px 8px; }
 239input.search { margin:4px 8px; position:absolute; top:56px; right:12px }
 240a.rss_logo { float:right; padding:3px 0px; width:35px; line-height:10px;
 241        border:1px solid; border-color:#fcc7a5 #7d3302 #3e1a01 #ff954e;
 242        color:#ffffff; background-color:#ff6600;
 243        font-weight:bold; font-family:sans-serif; font-size:10px;
 244        text-align:center; text-decoration:none;
 245}
 246a.rss_logo:hover { background-color:#ee5500; }
 247</style>
 248</head>
 249<body>
 250EOF
 251        print "<div class=\"page_header\">\n" .
 252              "<a href=\"http://www.kernel.org/pub/software/scm/git/docs/\">" .
 253              "<img src=\"$my_uri?a=git-logo.png\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/>" .
 254              "</a>\n";
 255        print $cgi->a({-href => $home_link}, "projects") . " / ";
 256        if (defined $project) {
 257                print $cgi->a({-href => "$my_uri?p=$project;a=summary"}, escapeHTML($project));
 258                if (defined $action) {
 259                        print " / $action";
 260                }
 261                print "\n";
 262                if (!defined $searchtext) {
 263                        $searchtext = "";
 264                }
 265                $cgi->param("a", "search");
 266                # post search form, but fake get parameter in browser
 267                #print $cgi->startform(-name => "search", -action => "$my_uri",
 268                #      -onsubmit => "document.search.action='?p=$project;a=search;s='+document.search.s.value") .
 269                #      $cgi->hidden(-name => "p") . "\n" .
 270                #      $cgi->hidden(-name => "a") . "\n" .
 271                #      $cgi->textfield(-name => "s", -value => $searchtext, -class => "search") .
 272                #      $cgi->end_form() . "\n";
 273        }
 274        print "</div>\n";
 275}
 276
 277sub git_footer_html {
 278        print "<div class=\"page_footer\">\n";
 279        if (defined $project) {
 280                my $descr = git_read_description($project);
 281                if (defined $descr) {
 282                        print "<div class=\"page_footer_text\">" . escapeHTML($descr) . "</div>\n";
 283                }
 284                print $cgi->a({-href => "$my_uri?p=$project;a=rss", -class => "rss_logo"}, "RSS") . "\n";
 285        }
 286        print "</div>\n" .
 287              "</body>\n" .
 288              "</html>";
 289}
 290
 291sub die_error {
 292        my $status = shift || "403 Forbidden";
 293        my $error = shift || "Malformed query, file missing or permission denied"; 
 294
 295        git_header_html($status);
 296        print "<div class=\"page_body\">\n" .
 297              "<br/><br/>\n" .
 298              "$status - $error\n" .
 299              "<br/>\n" .
 300              "</div>\n";
 301        git_footer_html();
 302        exit;
 303}
 304
 305sub git_get_type {
 306        my $hash = shift;
 307
 308        open my $fd, "-|", "$gitbin/git-cat-file -t $hash" or return;
 309        my $type = <$fd>;
 310        close $fd;
 311        chomp $type;
 312        return $type;
 313}
 314
 315sub git_read_hash {
 316        my $path = shift;
 317
 318        open my $fd, "$projectroot/$path" or return undef;
 319        my $head = <$fd>;
 320        close $fd;
 321        chomp $head;
 322        if ($head =~ m/^[0-9a-fA-F]{40}$/) {
 323                return $head;
 324        }
 325}
 326
 327sub git_read_description {
 328        my $path = shift;
 329
 330        open my $fd, "$projectroot/$path/description" or return undef;
 331        my $descr = <$fd>;
 332        close $fd;
 333        chomp $descr;
 334        return $descr;
 335}
 336
 337sub git_read_tag {
 338        my $tag_id = shift;
 339        my %tag;
 340
 341        open my $fd, "-|", "$gitbin/git-cat-file tag $tag_id" or return;
 342        while (my $line = <$fd>) {
 343                chomp $line;
 344                if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
 345                        $tag{'object'} = $1;
 346                } elsif ($line =~ m/^type (.+)$/) {
 347                        $tag{'type'} = $1;
 348                } elsif ($line =~ m/^tag (.+)$/) {
 349                        $tag{'name'} = $1;
 350                }
 351        }
 352        close $fd or return;
 353        if (!defined $tag{'name'}) {
 354                return
 355        };
 356        return %tag
 357}
 358
 359sub git_read_commit {
 360        my $commit_id = shift;
 361        my $commit_text = shift;
 362
 363        my @commit_lines;
 364        my %co;
 365        my @parents;
 366
 367        if (defined $commit_text) {
 368                @commit_lines = @$commit_text;
 369        } else {
 370                open my $fd, "-|", "$gitbin/git-cat-file commit $commit_id" or return;
 371                @commit_lines = map { chomp; $_ } <$fd>;
 372                close $fd or return;
 373        }
 374        while (my $line = shift @commit_lines) {
 375                last if $line eq "\n";
 376                if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
 377                        $co{'tree'} = $1;
 378                } elsif ($line =~ m/^parent ([0-9a-fA-F]{40})$/) {
 379                        push @parents, $1;
 380                } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
 381                        $co{'author'} = $1;
 382                        $co{'author_epoch'} = $2;
 383                        $co{'author_tz'} = $3;
 384                        if ($co{'author'} =~ m/^([^<]+) </) {
 385                                $co{'author_name'} = $1;
 386                        } else {
 387                                $co{'author_name'} = $co{'author'};
 388                        }
 389                } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
 390                        $co{'committer'} = $1;
 391                        $co{'committer_epoch'} = $2;
 392                        $co{'committer_tz'} = $3;
 393                        $co{'committer_name'} = $co{'committer'};
 394                        $co{'committer_name'} =~ s/ <.*//;
 395                }
 396        }
 397        if (!defined $co{'tree'}) {
 398                return undef
 399        };
 400        $co{'id'} = $commit_id;
 401        $co{'parents'} = \@parents;
 402        $co{'parent'} = $parents[0];
 403        $co{'comment'} = \@commit_lines;
 404        foreach my $title (@commit_lines) {
 405                if ($title ne "") {
 406                        $co{'title'} = chop_str($title, 80);
 407                        # remove leading stuff of merges to make the interesting part visible
 408                        if (length($title) > 50) {
 409                                $title =~ s/^Automatic //;
 410                                $title =~ s/^merge (of|with) /Merge ... /i;
 411                                if (length($title) > 50) {
 412                                        $title =~ s/(http|rsync):\/\///;
 413                                }
 414                                if (length($title) > 50) {
 415                                        $title =~ s/(master|www|rsync)\.//;
 416                                }
 417                                if (length($title) > 50) {
 418                                        $title =~ s/kernel.org:?//;
 419                                }
 420                                if (length($title) > 50) {
 421                                        $title =~ s/\/pub\/scm//;
 422                                }
 423                        }
 424                        $co{'title_short'} = chop_str($title, 50);
 425                        last;
 426                }
 427        }
 428
 429        my $age = time - $co{'committer_epoch'};
 430        $co{'age'} = $age;
 431        if ($age > 60*60*24*365*2) {
 432                $co{'age_string'} = (int $age/60/60/24/365);
 433                $co{'age_string'} .= " years ago";
 434        } elsif ($age > 60*60*24*(365/12)*2) {
 435                $co{'age_string'} = int $age/60/60/24/(365/12);
 436                $co{'age_string'} .= " months ago";
 437        } elsif ($age > 60*60*24*7*2) {
 438                $co{'age_string'} = int $age/60/60/24/7;
 439                $co{'age_string'} .= " weeks ago";
 440        } elsif ($age > 60*60*24*2) {
 441                $co{'age_string'} = int $age/60/60/24;
 442                $co{'age_string'} .= " days ago";
 443        } elsif ($age > 60*60*2) {
 444                $co{'age_string'} = int $age/60/60;
 445                $co{'age_string'} .= " hours ago";
 446        } elsif ($age > 60*2) {
 447                $co{'age_string'} = int $age/60;
 448                $co{'age_string'} .= " min ago";
 449        } elsif ($age > 2) {
 450                $co{'age_string'} = int $age;
 451                $co{'age_string'} .= " sec ago";
 452        } else {
 453                $co{'age_string'} .= " right now";
 454        }
 455        return %co;
 456}
 457
 458sub git_diff_print {
 459        my $from = shift;
 460        my $from_name = shift;
 461        my $to = shift;
 462        my $to_name = shift;
 463        my $format = shift || "html";
 464
 465        my $from_tmp = "/dev/null";
 466        my $to_tmp = "/dev/null";
 467        my $pid = $$;
 468
 469        # create tmp from-file
 470        if (defined $from) {
 471                $from_tmp = "$git_temp/gitweb_" . $$ . "_from";
 472                open my $fd2, "> $from_tmp";
 473                open my $fd, "-|", "$gitbin/git-cat-file blob $from";
 474                my @file = <$fd>;
 475                print $fd2 @file;
 476                close $fd2;
 477                close $fd;
 478        }
 479
 480        # create tmp to-file
 481        if (defined $to) {
 482                $to_tmp = "$git_temp/gitweb_" . $$ . "_to";
 483                open my $fd2, "> $to_tmp";
 484                open my $fd, "-|", "$gitbin/git-cat-file blob $to";
 485                my @file = <$fd>;
 486                print $fd2 @file;
 487                close $fd2;
 488                close $fd;
 489        }
 490
 491        open my $fd, "-|", "/usr/bin/diff -u -p -L $from_name -L $to_name $from_tmp $to_tmp";
 492        if ($format eq "plain") {
 493                undef $/;
 494                print <$fd>;
 495                $/ = "\n";
 496        } else {
 497                while (my $line = <$fd>) {
 498                        chomp($line);
 499                        my $char = substr($line, 0, 1);
 500                        my $color = "";
 501                        if ($char eq '+') {
 502                                $color = " style=\"color:#008800;\"";
 503                        } elsif ($char eq "-") {
 504                                $color = " style=\"color:#cc0000;\"";
 505                        } elsif ($char eq "@") {
 506                                $color = " style=\"color:#990099;\"";
 507                        } elsif ($char eq "\\") {
 508                                # skip errors
 509                                next;
 510                        }
 511                        while ((my $pos = index($line, "\t")) != -1) {
 512                                if (my $count = (8 - (($pos-1) % 8))) {
 513                                        my $spaces = ' ' x $count;
 514                                        $line =~ s/\t/$spaces/;
 515                                }
 516                        }
 517                        print "<div class=\"pre\"$color>" . escapeHTML($line) . "</div>\n";
 518                }
 519        }
 520        close $fd;
 521
 522        if (defined $from) {
 523                unlink($from_tmp);
 524        }
 525        if (defined $to) {
 526                unlink($to_tmp);
 527        }
 528}
 529
 530sub mode_str {
 531        my $mode = oct shift;
 532
 533        if (S_ISDIR($mode & S_IFMT)) {
 534                return 'drwxr-xr-x';
 535        } elsif (S_ISLNK($mode)) {
 536                return 'lrwxrwxrwx';
 537        } elsif (S_ISREG($mode)) {
 538                # git cares only about the executable bit
 539                if ($mode & S_IXUSR) {
 540                        return '-rwxr-xr-x';
 541                } else {
 542                        return '-rw-r--r--';
 543                };
 544        } else {
 545                return '----------';
 546        }
 547}
 548
 549sub chop_str {
 550        my $str = shift;
 551        my $len = shift;
 552        my $add_len = shift || 10;
 553
 554        $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})/;
 555        my $chopped = $1;
 556        if ($chopped ne $str) {
 557                $chopped .= " ...";
 558        }
 559        return $chopped;
 560}
 561
 562sub file_type {
 563        my $mode = oct shift;
 564
 565        if (S_ISDIR($mode & S_IFMT)) {
 566                return "directory";
 567        } elsif (S_ISLNK($mode)) {
 568                return "symlink";
 569        } elsif (S_ISREG($mode)) {
 570                return "file";
 571        } else {
 572                return "unknown";
 573        }
 574}
 575
 576sub date_str {
 577        my $epoch = shift;
 578        my $tz = shift || "-0000";
 579
 580        my %date;
 581        my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
 582        my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
 583        my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
 584        $date{'hour'} = $hour;
 585        $date{'minute'} = $min;
 586        $date{'mday'} = $mday;
 587        $date{'day'} = $days[$wday];
 588        $date{'month'} = $months[$mon];
 589        $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000", $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
 590        $date{'mday-time'} = sprintf "%d %s %02d:%02d", $mday, $months[$mon], $hour ,$min;
 591
 592        $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
 593        my $local = $epoch + ((int $1 + ($2/60)) * 3600);
 594        ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
 595        $date{'hour_local'} = $hour;
 596        $date{'minute_local'} = $min;
 597        $date{'tz_local'} = $tz;
 598        return %date;
 599}
 600
 601# git-logo (cached in browser for one day)
 602sub git_logo {
 603        print $cgi->header(-type => 'image/png', -expires => '+1d');
 604        # cat git-logo.png | hexdump -e '16/1 " %02x"  "\n"' | sed 's/ /\\x/g'
 605        print   "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52" .
 606                "\x00\x00\x00\x48\x00\x00\x00\x1b\x04\x03\x00\x00\x00\x2d\xd9\xd4" .
 607                "\x2d\x00\x00\x00\x18\x50\x4c\x54\x45\xff\xff\xff\x60\x60\x5d\xb0" .
 608                "\xaf\xaa\x00\x80\x00\xce\xcd\xc7\xc0\x00\x00\xe8\xe8\xe6\xf7\xf7" .
 609                "\xf6\x95\x0c\xa7\x47\x00\x00\x00\x73\x49\x44\x41\x54\x28\xcf\x63" .
 610                "\x48\x67\x20\x04\x4a\x5c\x18\x0a\x08\x2a\x62\x53\x61\x20\x02\x08" .
 611                "\x0d\x69\x45\xac\xa1\xa1\x01\x30\x0c\x93\x60\x36\x26\x52\x91\xb1" .
 612                "\x01\x11\xd6\xe1\x55\x64\x6c\x6c\xcc\x6c\x6c\x0c\xa2\x0c\x70\x2a" .
 613                "\x62\x06\x2a\xc1\x62\x1d\xb3\x01\x02\x53\xa4\x08\xe8\x00\x03\x18" .
 614                "\x26\x56\x11\xd4\xe1\x20\x97\x1b\xe0\xb4\x0e\x35\x24\x71\x29\x82" .
 615                "\x99\x30\xb8\x93\x0a\x11\xb9\x45\x88\xc1\x8d\xa0\xa2\x44\x21\x06" .
 616                "\x27\x41\x82\x40\x85\xc1\x45\x89\x20\x70\x01\x00\xa4\x3d\x21\xc5" .
 617                "\x12\x1c\x9a\xfe\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82";
 618}
 619
 620sub get_file_owner {
 621        my $path = shift;
 622
 623        my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
 624        my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
 625        if (!defined $gcos) {
 626                return undef;
 627        }
 628        my $owner = $gcos;
 629        $owner =~ s/[,;].*$//;
 630        return $owner;
 631}
 632
 633sub git_project_list {
 634        my @list;
 635
 636        if (-d $projects_list) {
 637                # search in directory
 638                my $dir = $projects_list;
 639                opendir my $dh, $dir or return undef;
 640                while (my $dir = readdir($dh)) {
 641                        if (-e "$projectroot/$dir/HEAD") {
 642                                my $pr = {
 643                                        path => $dir,
 644                                };
 645                                push @list, $pr
 646                        }
 647                }
 648                closedir($dh);
 649        } elsif (-f $projects_list) {
 650                # read from file(url-encoded):
 651                # 'git%2Fgit.git Linus+Torvalds'
 652                # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
 653                # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
 654                open my $fd , $projects_list or return undef;
 655                while (my $line = <$fd>) {
 656                        chomp $line;
 657                        my ($path, $owner) = split ' ', $line;
 658                        $path = unescape($path);
 659                        $owner = unescape($owner);
 660                        if (!defined $path) {
 661                                next;
 662                        }
 663                        if (-e "$projectroot/$path/HEAD") {
 664                                my $pr = {
 665                                        path => $path,
 666                                        owner => $owner,
 667                                };
 668                                push @list, $pr
 669                        }
 670                }
 671                close $fd;
 672        }
 673
 674        if (!@list) {
 675                die_error(undef, "No project found.");
 676        }
 677        @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
 678
 679        git_header_html();
 680        if (-f $home_text) {
 681                print "<div class=\"index_include\">\n";
 682                open (my $fd, $home_text);
 683                print <$fd>;
 684                close $fd;
 685                print "</div>\n";
 686        }
 687        print "<table cellspacing=\"0\">\n" .
 688              "<tr>\n" .
 689              "<th>Project</th>\n" .
 690              "<th>Description</th>\n" .
 691              "<th>Owner</th>\n" .
 692              "<th>last change</th>\n" .
 693              "<th></th>\n" .
 694              "</tr>\n";
 695        my $alternate = 0;
 696        foreach my $pr (@list) {
 697                my %proj = %$pr;
 698                my $head = git_read_hash("$proj{'path'}/HEAD");
 699                if (!defined $head) {
 700                        next;
 701                }
 702                $ENV{'GIT_OBJECT_DIRECTORY'} = "$projectroot/$proj{'path'}/objects";
 703                my %co = git_read_commit($head);
 704                if (!%co) {
 705                        next;
 706                }
 707                my $descr = git_read_description($proj{'path'}) || "";
 708                $descr = chop_str($descr, 25, 5);
 709                # get directory owner if not already specified
 710                if (!defined $proj{'owner'}) {
 711                        $proj{'owner'} = get_file_owner("$projectroot/$proj{'path'}") || "";
 712                }
 713                if ($alternate) {
 714                        print "<tr style=\"background-color:#f6f5ed\">\n";
 715                } else {
 716                        print "<tr>\n";
 717                }
 718                $alternate ^= 1;
 719                print "<td>" . $cgi->a({-href => "$my_uri?p=$proj{'path'};a=summary", -class => "list"}, escapeHTML($proj{'path'})) . "</td>\n" .
 720                      "<td>$descr</td>\n" .
 721                      "<td><i>" . chop_str($proj{'owner'}, 15) . "</i></td>\n";
 722                my $colored_age;
 723                if ($co{'age'} < 60*60*2) {
 724                        $colored_age = "<span style =\"color: #009900;\"><b><i>$co{'age_string'}</i></b></span>";
 725                } elsif ($co{'age'} < 60*60*24*2) {
 726                        $colored_age = "<span style =\"color: #009900;\"><i>$co{'age_string'}</i></span>";
 727                } else {
 728                        $colored_age = "<i>$co{'age_string'}</i>";
 729                }
 730                print "<td>$colored_age</td>\n" .
 731                      "<td class=\"link\">" .
 732                      $cgi->a({-href => "$my_uri?p=$proj{'path'};a=summary"}, "summary") .
 733                      " | " . $cgi->a({-href => "$my_uri?p=$proj{'path'};a=shortlog"}, "shortlog") .
 734                      " | " . $cgi->a({-href => "$my_uri?p=$proj{'path'};a=log"}, "log") .
 735                      "</td>\n" .
 736                      "</tr>\n";
 737        }
 738        print "</table>\n";
 739        git_footer_html();
 740}
 741
 742sub git_read_refs {
 743        my $ref_dir = shift;
 744        my @reflist;
 745
 746        opendir my $dh, "$projectroot/$project/$ref_dir";
 747        my @refs = grep !m/^\./, readdir $dh;
 748        closedir($dh);
 749        foreach my $ref_file (@refs) {
 750                my $ref_id = git_read_hash("$project/$ref_dir/$ref_file");
 751                my $type = git_get_type($ref_id) || next;
 752                my %ref_item;
 753                my %co;
 754                if ($type eq "tag") {
 755                        my %tag = git_read_tag($ref_id);
 756                        if ($tag{'type'} eq "commit") {
 757                                %co = git_read_commit($tag{'object'});
 758                        }
 759                        $ref_item{'type'} = $tag{'type'};
 760                        $ref_item{'name'} = $tag{'name'};
 761                        $ref_item{'id'} = $tag{'object'};
 762                } elsif ($type eq "commit"){
 763                        %co = git_read_commit($ref_id);
 764                        $ref_item{'type'} = "commit";
 765                        $ref_item{'name'} = $ref_file;
 766                        $ref_item{'title'} = $co{'title'};
 767                        $ref_item{'id'} = $ref_id;
 768                }
 769                $ref_item{'epoch'} = $co{'committer_epoch'} || 0;
 770                $ref_item{'age'} = $co{'age_string'} || "unknown";
 771
 772                push @reflist, \%ref_item;
 773        }
 774        # sort tags by age
 775        @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
 776        return \@reflist;
 777}
 778
 779sub git_summary {
 780        my $descr = git_read_description($project) || "none";
 781        my $head = git_read_hash("$project/HEAD");
 782        my %co = git_read_commit($head);
 783        my %cd = date_str($co{'committer_epoch'}, $co{'committer_tz'});
 784
 785        my $owner;
 786        if (-f $projects_list) {
 787                open (my $fd , $projects_list);
 788                while (my $line = <$fd>) {
 789                        chomp $line;
 790                        my ($pr, $ow) = split ' ', $line;
 791                        $pr = unescape($pr);
 792                        $ow = unescape($ow);
 793                        if ($pr eq $project) {
 794                                $owner = $ow;
 795                                last;
 796                        }
 797                }
 798                close $fd;
 799        }
 800        if (!defined $owner) {
 801                $owner = get_file_owner("$projectroot/$project");
 802        }
 803
 804        git_header_html();
 805        print "<div class=\"page_nav\">\n" .
 806              "summary".
 807              " | " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog"}, "shortlog") .
 808              " | " . $cgi->a({-href => "$my_uri?p=$project;a=log"}, "log") .
 809              " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$head"}, "commit") .
 810              " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$head"}, "commitdiff") .
 811              " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree"}, "tree") .
 812              "<br/><br/>\n" .
 813              "</div>\n";
 814        print "<div class=\"title\">&nbsp;</div>\n";
 815        print "<table cellspacing=\"0\">\n" .
 816              "<tr><td>description</td><td>" . escapeHTML($descr) . "</td></tr>\n" .
 817              "<tr><td>owner</td><td>$owner</td></tr>\n" .
 818              "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n" .
 819              "</table>\n";
 820        open my $fd, "-|", "$gitbin/git-rev-list --max-count=17 " . git_read_hash("$project/HEAD") or die_error(undef, "Open failed.");
 821        my (@revlist) = map { chomp; $_ } <$fd>;
 822        close $fd;
 823        print "<div>\n" .
 824              $cgi->a({-href => "$my_uri?p=$project;a=shortlog", -class => "title"}, "shortlog") .
 825              "</div>\n";
 826        my $i = 16;
 827        print "<table cellspacing=\"0\">\n";
 828        my $alternate = 0;
 829        foreach my $commit (@revlist) {
 830                my %co = git_read_commit($commit);
 831                my %ad = date_str($co{'author_epoch'});
 832                if ($alternate) {
 833                        print "<tr style=\"background-color:#f6f5ed\">\n";
 834                } else {
 835                        print "<tr>\n";
 836                }
 837                $alternate ^= 1;
 838                if ($i-- > 0) {
 839                        print "<td><i>$co{'age_string'}</i></td>\n" .
 840                              "<td><i>" . escapeHTML(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
 841                              "<td>" .
 842                              $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit", -class => "list"},
 843                              "<b>" . escapeHTML($co{'title_short'}) . "</b>") .
 844                              "</td>\n" .
 845                              "<td class=\"link\">" .
 846                              $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit"}, "commit") .
 847                              " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$commit"}, "commitdiff") .
 848                              "</td>\n" .
 849                              "</tr>";
 850                } else {
 851                        print "<td>" . $cgi->a({-href => "$my_uri?p=$project;a=shortlog"}, "...") . "</td>\n" .
 852                        "</tr>";
 853                        last;
 854                }
 855        }
 856        print "</table\n>";
 857
 858        my $taglist = git_read_refs("refs/tags");
 859        if (defined @$taglist) {
 860                print "<div>\n" .
 861                      $cgi->a({-href => "$my_uri?p=$project;a=tags", -class => "title"}, "tags") .
 862                      "</div>\n";
 863                my $i = 16;
 864                print "<table cellspacing=\"0\">\n";
 865                my $alternate = 0;
 866                foreach my $entry (@$taglist) {
 867                        my %tag = %$entry;
 868                        if ($alternate) {
 869                                print "<tr style=\"background-color:#f6f5ed\">\n";
 870                        } else {
 871                                print "<tr>\n";
 872                        }
 873                        $alternate ^= 1;
 874                        if ($i-- > 0) {
 875                                print "<td><i>$tag{'age'}</i></td>\n" .
 876                                      "<td>" .
 877                                      $cgi->a({-href => "$my_uri?p=$project;a=$tag{'type'};h=$tag{'id'}", -class => "list"}, "<b>" .
 878                                      escapeHTML($tag{'name'}) . "</b>") .
 879                                      "</td>\n" .
 880                                      "<td class=\"link\">" .
 881                                      $cgi->a({-href => "$my_uri?p=$project;a=$tag{'type'};h=$tag{'id'}"}, $tag{'type'});
 882                                if ($tag{'type'} eq "commit") {
 883                                      print " | " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog;h=$tag{'id'}"}, "shortlog") .
 884                                            " | " . $cgi->a({-href => "$my_uri?p=$project;a=log;h=$tag{'id'}"}, "log");
 885                                }
 886                                print "</td>\n" .
 887                                      "</tr>";
 888                        } else {
 889                                print "<td>" . $cgi->a({-href => "$my_uri?p=$project;a=tags"}, "...") . "</td>\n" .
 890                                "</tr>";
 891                                last;
 892                        }
 893                }
 894                print "</table\n>";
 895        }
 896
 897        my $branchlist = git_read_refs("refs/heads");
 898        if (defined @$branchlist) {
 899                print "<div>\n" .
 900                      $cgi->a({-href => "$my_uri?p=$project;a=branches", -class => "title"}, "branches") .
 901                      "</div>\n";
 902                my $i = 16;
 903                print "<table cellspacing=\"0\">\n";
 904                my $alternate = 0;
 905                foreach my $entry (@$branchlist) {
 906                        my %tag = %$entry;
 907                        if ($alternate) {
 908                                print "<tr style=\"background-color:#f6f5ed\">\n";
 909                        } else {
 910                                print "<tr>\n";
 911                        }
 912                        $alternate ^= 1;
 913                        if ($i-- > 0) {
 914                                print "<td><i>$tag{'age'}</i></td>\n" .
 915                                      "<td>" .
 916                                      $cgi->a({-href => "$my_uri?p=$project;a=shortlog;h=$tag{'id'}", -class => "list"},
 917                                      "<b>" . escapeHTML($tag{'name'}) . "</b>") .
 918                                      "</td>\n" .
 919                                      "<td class=\"link\">" .
 920                                      $cgi->a({-href => "$my_uri?p=$project;a=shortlog;h=$tag{'id'}"}, "shortlog") .
 921                                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=log;h=$tag{'id'}"}, "log") .
 922                                      "</td>\n" .
 923                                      "</tr>";
 924                        } else {
 925                                print "<td>" . $cgi->a({-href => "$my_uri?p=$project;a=branches"}, "...") . "</td>\n" .
 926                                "</tr>";
 927                                last;
 928                        }
 929                }
 930                print "</table\n>";
 931        }
 932        git_footer_html();
 933}
 934
 935sub git_tags {
 936        my $head = git_read_hash("$project/HEAD");
 937        git_header_html();
 938        print "<div class=\"page_nav\">\n" .
 939              $cgi->a({-href => "$my_uri?p=$project;a=summary"}, "summary") .
 940              " | " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog"}, "shortlog") .
 941              " | " . $cgi->a({-href => "$my_uri?p=$project;a=log"}, "log") .
 942              " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$head"}, "commit") .
 943              " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$head"}, "commitdiff") .
 944              " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;hb=$head"}, "tree") . "<br/>\n" .
 945              "<br/>\n" .
 946              "</div>\n";
 947        my $taglist = git_read_refs("refs/tags");
 948        print "<div>\n" .
 949              $cgi->a({-href => "$my_uri?p=$project;a=summary", -class => "title"}, "&nbsp;") .
 950              "</div>\n";
 951        print "<table cellspacing=\"0\">\n";
 952        my $alternate = 0;
 953        if (defined @$taglist) {
 954                foreach my $entry (@$taglist) {
 955                        my %tag = %$entry;
 956                        if ($alternate) {
 957                                print "<tr style=\"background-color:#f6f5ed\">\n";
 958                        } else {
 959                                print "<tr>\n";
 960                        }
 961                        $alternate ^= 1;
 962                        print "<td><i>$tag{'age'}</i></td>\n" .
 963                              "<td>" .
 964                              $cgi->a({-href => "$my_uri?p=$project;a=log;h=$tag{'id'}", -class => "list"},
 965                              "<b>" . escapeHTML($tag{'name'}) . "</b>") .
 966                              "</td>\n" .
 967                              "<td class=\"link\">" .
 968                              $cgi->a({-href => "$my_uri?p=$project;a=$tag{'type'};h=$tag{'id'}"}, $tag{'type'});
 969                        if ($tag{'type'} eq "commit") {
 970                              print " | " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog;h=$tag{'id'}"}, "shortlog") .
 971                                    " | " . $cgi->a({-href => "$my_uri?p=$project;a=log;h=$tag{'id'}"}, "log");
 972                        }
 973                        print "</td>\n" .
 974                              "</tr>";
 975                }
 976        }
 977        print "</table\n>";
 978        git_footer_html();
 979}
 980
 981sub git_branches {
 982        my $head = git_read_hash("$project/HEAD");
 983        git_header_html();
 984        print "<div class=\"page_nav\">\n" .
 985              $cgi->a({-href => "$my_uri?p=$project;a=summary"}, "summary") .
 986              " | " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog"}, "shortlog") .
 987              " | " . $cgi->a({-href => "$my_uri?p=$project;a=log"}, "log") .
 988              " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$head"}, "commit") .
 989              " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$head"}, "commitdiff") .
 990              " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;hb=$head"}, "tree") . "<br/>\n" .
 991              "<br/>\n" .
 992              "</div>\n";
 993        my $taglist = git_read_refs("refs/heads");
 994        print "<div>\n" .
 995              $cgi->a({-href => "$my_uri?p=$project;a=summary", -class => "title"}, "&nbsp;") .
 996              "</div>\n";
 997        print "<table cellspacing=\"0\">\n";
 998        my $alternate = 0;
 999        if (defined @$taglist) {
1000                foreach my $entry (@$taglist) {
1001                        my %tag = %$entry;
1002                        if ($alternate) {
1003                                print "<tr style=\"background-color:#f6f5ed\">\n";
1004                        } else {
1005                                print "<tr>\n";
1006                        }
1007                        $alternate ^= 1;
1008                        print "<td><i>$tag{'age'}</i></td>\n" .
1009                              "<td>" .
1010                              $cgi->a({-href => "$my_uri?p=$project;a=log;h=$tag{'id'}", -class => "list"}, "<b>" . escapeHTML($tag{'name'}) . "</b>") .
1011                              "</td>\n" .
1012                              "<td class=\"link\">" .
1013                              $cgi->a({-href => "$my_uri?p=$project;a=shortlog;h=$tag{'id'}"}, "shortog") .
1014                              " | " . $cgi->a({-href => "$my_uri?p=$project;a=log;h=$tag{'id'}"}, "log") .
1015                              "</td>\n" .
1016                              "</tr>";
1017                }
1018        }
1019        print "</table\n>";
1020        git_footer_html();
1021}
1022
1023sub git_get_hash_by_path {
1024        my $base = shift;
1025        my $path = shift || return undef;
1026
1027        my $tree = $base;
1028        my @parts = split '/', $path;
1029        while (my $part = shift @parts) {
1030                open my $fd, "-|", "$gitbin/git-ls-tree $tree" or die_error(undef, "Open git-ls-tree failed.");
1031                my (@entries) = map { chomp; $_ } <$fd>;
1032                close $fd or return undef;
1033                foreach my $line (@entries) {
1034                        #'100644        blob    0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa        panic.c'
1035                        $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
1036                        my $t_mode = $1;
1037                        my $t_type = $2;
1038                        my $t_hash = $3;
1039                        my $t_name = $4;
1040                        if ($t_name eq $part) {
1041                                if (!(@parts)) {
1042                                        return $t_hash;
1043                                }
1044                                if ($t_type eq "tree") {
1045                                        $tree = $t_hash;
1046                                }
1047                                last;
1048                        }
1049                }
1050        }
1051}
1052
1053sub git_blob {
1054        if (!defined $hash && defined $file_name) {
1055                my $base = $hash_base || git_read_hash("$project/HEAD");
1056                $hash = git_get_hash_by_path($base, $file_name, "blob");
1057        }
1058        open my $fd, "-|", "$gitbin/git-cat-file blob $hash" or die_error(undef, "Open failed.");
1059        my $base = $file_name || "";
1060        git_header_html();
1061        if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
1062                print "<div class=\"page_nav\">\n" .
1063                      $cgi->a({-href => "$my_uri?p=$project;a=summary"}, "summary") .
1064                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog"}, "shortlog") .
1065                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=log"}, "log") .
1066                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash_base"}, "commit") .
1067                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash_base"}, "commitdiff") .
1068                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash_base"}, "tree") . "<br/>\n";
1069                print $cgi->a({-href => "$my_uri?p=$project;a=blob_plain;h=$hash"}, "plain") . "<br/>\n" .
1070                      "</div>\n";
1071                print "<div>" .
1072                      $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash_base", -class => "title"}, escapeHTML($co{'title'})) .
1073                      "</div>\n";
1074        } else {
1075                print "<div class=\"page_nav\">\n" .
1076                      "<br/><br/></div>\n" .
1077                      "<div class=\"title\">$hash</div>\n";
1078        }
1079        if (defined $file_name) {
1080                print "<div class=\"page_path\"><b>$file_name</b></div>\n";
1081        }
1082        print "<div class=\"page_body\">\n";
1083        my $nr;
1084        while (my $line = <$fd>) {
1085                chomp $line;
1086                $nr++;
1087                while ((my $pos = index($line, "\t")) != -1) {
1088                        if (my $count = (8 - ($pos % 8))) {
1089                                my $spaces = ' ' x $count;
1090                                $line =~ s/\t/$spaces/;
1091                        }
1092                }
1093                printf "<div class=\"pre\"><span style=\"color:#999999;\">%4i</span> %s</div>\n", $nr, escapeHTML($line);
1094        }
1095        close $fd or print "Reading blob failed.\n";
1096        print "</div>";
1097        git_footer_html();
1098}
1099
1100sub git_blob_plain {
1101        print $cgi->header(-type => "text/plain", -charset => 'utf-8');
1102        open my $fd, "-|", "$gitbin/git-cat-file blob $hash" or return;
1103        undef $/;
1104        print <$fd>;
1105        $/ = "\n";
1106        close $fd;
1107}
1108
1109sub git_tree {
1110        if (!defined $hash) {
1111                $hash = git_read_hash("$project/HEAD");
1112                if (defined $file_name) {
1113                        my $base = $hash_base || git_read_hash("$project/HEAD");
1114                        $hash = git_get_hash_by_path($base, $file_name, "tree");
1115                }
1116                if (!defined $hash_base) {
1117                        $hash_base = git_read_hash("$project/HEAD");
1118                }
1119        }
1120        open my $fd, "-|", "$gitbin/git-ls-tree $hash" or die_error(undef, "Open git-ls-tree failed.");
1121        my (@entries) = map { chomp; $_ } <$fd>;
1122        close $fd or die_error(undef, "Reading tree failed.");
1123
1124        git_header_html();
1125        my $base_key = "";
1126        my $file_key = "";
1127        my $base = "";
1128        if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
1129                $base_key = ";hb=$hash_base";
1130                print "<div class=\"page_nav\">\n" .
1131                      $cgi->a({-href => "$my_uri?p=$project;a=summary"}, "summary") .
1132                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog;h=$hash_base"}, "shortlog") .
1133                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=log;h=$hash_base"}, "log") .
1134                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash_base"}, "commit") .
1135                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash_base"}, "commitdiff") .
1136                      " | tree" .
1137                      "<br/><br/>\n" .
1138                      "</div>\n";
1139                print "<div>\n" .
1140                      $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash_base", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
1141                      "</div>\n";
1142        } else {
1143                print "<div class=\"page_nav\">\n";
1144                print "<br/><br/></div>\n";
1145                print "<div class=\"title\">$hash</div>\n";
1146        }
1147        if (defined $file_name) {
1148                $base = "$file_name/";
1149                print "<div class=\"page_path\"><b>/$file_name</b></div>\n";
1150        } else {
1151                print "<div class=\"page_path\"><b>/</b></div>\n";
1152        }
1153        print "<div class=\"page_body\">\n";
1154        print "<table cellspacing=\"0\">\n";
1155        my $alternate = 0;
1156        foreach my $line (@entries) {
1157                #'100644        blob    0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa        panic.c'
1158                $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
1159                my $t_mode = $1;
1160                my $t_type = $2;
1161                my $t_hash = $3;
1162                my $t_name = $4;
1163                $file_key = ";f=$base$t_name";
1164                if ($alternate) {
1165                        print "<tr style=\"background-color:#f6f5ed\">\n";
1166                } else {
1167                        print "<tr>\n";
1168                }
1169                $alternate ^= 1;
1170                print "<td style=\"font-family:monospace\">" . mode_str($t_mode) . "</td>\n";
1171                if ($t_type eq "blob") {
1172                        print "<td class=\"list\">" .
1173                        $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$t_hash" . $base_key . $file_key, -class => "list"}, $t_name) .
1174                        "</td>\n";
1175                        print "<td class=\"link\">" .
1176                              $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$t_hash" . $base_key . $file_key}, "blob") .
1177                              " | " . $cgi->a({-href => "$my_uri?p=$project;a=history;h=$hash_base" . $file_key}, "history") .
1178                              "</td>\n";
1179                } elsif ($t_type eq "tree") {
1180                        print "<td class=\"list\">" .
1181                              $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$t_hash" . $base_key . $file_key}, $t_name) .
1182                              "</td>\n" .
1183                              "<td></td>\n";
1184                }
1185                print "</tr>\n";
1186        }
1187        print "</table>\n" .
1188              "</div>";
1189        git_footer_html();
1190}
1191
1192sub git_rss {
1193        # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
1194        open my $fd, "-|", "$gitbin/git-rev-list --max-count=20 " . git_read_hash("$project/HEAD") or die_error(undef, "Open failed.");
1195        my (@revlist) = map { chomp; $_ } <$fd>;
1196        close $fd or die_error(undef, "Reading rev-list failed.");
1197
1198        print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
1199        print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".
1200              "<rss version=\"2.0\" xmlns:content=\"http://purl.org/rss/1.0/modules/content/\">\n";
1201        print "<channel>\n";
1202        print "<title>$project</title>\n".
1203              "<link>" . escapeHTML("$my_url/$project/log") . "</link>\n".
1204              "<description>$project log</description>\n".
1205              "<language>en</language>\n";
1206
1207        foreach my $commit (@revlist) {
1208                my %co = git_read_commit($commit);
1209                my %cd = date_str($co{'committer_epoch'});
1210                print "<item>\n" .
1211                      "<title>" .
1212                      sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . escapeHTML($co{'title'}) .
1213                      "</title>\n" .
1214                      "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
1215                      "<link>" . escapeHTML("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
1216                      "<description>" . escapeHTML($co{'title'}) . "</description>\n" .
1217                      "<content:encoded>" .
1218                      "<![CDATA[\n";
1219                my $comment = $co{'comment'};
1220                foreach my $line (@$comment) {
1221                        print "$line<br/>\n";
1222                }
1223                print "]]>\n" .
1224                      "</content:encoded>\n" .
1225                      "</item>\n";
1226        }
1227        print "</channel></rss>";
1228}
1229
1230sub git_log {
1231        my $head = git_read_hash("$project/HEAD");
1232        if (!defined $hash) {
1233                $hash = $head;
1234        }
1235        my $limit_option = "";
1236        if (!defined $time_back) {
1237                $limit_option = "--max-count=100";
1238        } elsif ($time_back > 0) {
1239                my $date = time - $time_back*24*60*60;
1240                $limit_option = "--max-age=$date";
1241        }
1242        open my $fd, "-|", "$gitbin/git-rev-list $limit_option $hash" or die_error(undef, "Open failed.");
1243        my (@revlist) = map { chomp; $_ } <$fd>;
1244        close $fd or die_error(undef, "Reading rev-list failed.");
1245
1246        git_header_html();
1247        print "<div class=\"page_nav\">\n";
1248        print $cgi->a({-href => "$my_uri?p=$project;a=summary"}, "summary") .
1249              " | " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog;h=$hash"}, "shortlog") .
1250              " | log" .
1251              " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash"}, "commit") .
1252              " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash"}, "commitdiff") .
1253              " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$hash;hb=$hash"}, "tree") . "<br/>\n";
1254        if ($hash ne $head) {
1255                print $cgi->a({-href => "$my_uri?p=$project;a=log"}, "HEAD") . " &sdot; ";
1256        }
1257        print $cgi->a({-href => "$my_uri?p=$project;a=log;h=$hash"}, "100") .
1258              " &sdot; " . $cgi->a({-href => "$my_uri?p=$project;a=log;t=1;h=$hash"}, "day") .
1259              " &sdot; " .$cgi->a({-href => "$my_uri?p=$project;a=log;t=7;h=$hash"}, "week") .
1260              " &sdot; " . $cgi->a({-href => "$my_uri?p=$project;a=log;t=31;h=$hash"}, "month") .
1261              " &sdot; " . $cgi->a({-href => "$my_uri?p=$project;a=log;t=365;h=$hash"}, "year") .
1262              " &sdot; " . $cgi->a({-href => "$my_uri?p=$project;a=log;t=0;h=$hash"}, "all");
1263        print "<br/>\n" .
1264              "</div>\n";
1265        if (!@revlist) {
1266                print "<div>\n" .
1267                      $cgi->a({-href => "$my_uri?p=$project;a=summary", -class => "title"}, "&nbsp;") .
1268                      "</div>\n";
1269                my %co = git_read_commit($hash);
1270                print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
1271        }
1272        foreach my $commit (@revlist) {
1273                my %co = git_read_commit($commit);
1274                next if !%co;
1275                my %ad = date_str($co{'author_epoch'});
1276                print "<div>\n" .
1277                      $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit", -class => "title"},
1278                      "<span class=\"age\">$co{'age_string'}</span>" . escapeHTML($co{'title'})) . "\n" .
1279                      "</div>\n";
1280                print "<div class=\"title_text\">\n" .
1281                      "<div class=\"log_link\">\n" .
1282                      $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit"}, "commit") .
1283                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$commit"}, "commitdiff") .
1284                      "<br/>\n" .
1285                      "</div>\n" .
1286                      "<i>" . escapeHTML($co{'author_name'}) .  " [$ad{'rfc2822'}]</i><br/>\n" .
1287                      "</div>\n" .
1288                      "<div class=\"log_body\">\n";
1289                my $comment = $co{'comment'};
1290                my $empty = 0;
1291                foreach my $line (@$comment) {
1292                        if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1293                                next;
1294                        }
1295                        if ($line eq "") {
1296                                if ($empty) {
1297                                        next;
1298                                }
1299                                $empty = 1;
1300                        } else {
1301                                $empty = 0;
1302                        }
1303                        print escapeHTML($line) . "<br/>\n";
1304                }
1305                if (!$empty) {
1306                        print "<br/>\n";
1307                }
1308                print "</div>\n";
1309        }
1310        git_footer_html();
1311}
1312
1313sub git_commit {
1314        my %co = git_read_commit($hash);
1315        if (!%co) {
1316                die_error(undef, "Unknown commit object.");
1317        }
1318        my %ad = date_str($co{'author_epoch'}, $co{'author_tz'});
1319        my %cd = date_str($co{'committer_epoch'}, $co{'committer_tz'});
1320
1321        my @difftree;
1322        my $root = "";
1323        if (!defined $co{'parent'}) {
1324                $root = " --root";
1325        }
1326        open my $fd, "-|", "$gitbin/git-diff-tree -r $root $co{'parent'} $hash" or die_error(undef, "Open failed.");
1327        @difftree = map { chomp; $_ } <$fd>;
1328        close $fd or die_error(undef, "Reading diff-tree failed.");
1329        git_header_html();
1330        print "<div class=\"page_nav\">\n" .
1331              $cgi->a({-href => "$my_uri?p=$project;a=summary"}, "summary") .
1332              " | " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog;h=$hash"}, "shortlog") .
1333              " | " . $cgi->a({-href => "$my_uri?p=$project;a=log;h=$hash"}, "log") .
1334              " | commit";
1335        if (defined $co{'parent'}) {
1336                print " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash"}, "commitdiff");
1337        }
1338        print " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash"}, "tree") . "\n" .
1339              "<br/><br/></div>\n";
1340        if (defined $co{'parent'}) {
1341                print "<div>\n" .
1342                      $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
1343                      "</div>\n";
1344        } else {
1345                print "<div>\n" .
1346                      $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
1347                      "</div>\n";
1348        }
1349        print "<div class=\"title_text\">\n" .
1350              "<table cellspacing=\"0\">\n";
1351        print "<tr><td>author</td><td>" . escapeHTML($co{'author'}) . "</td></tr>\n".
1352              "<tr>" .
1353              "<td></td><td> $ad{'rfc2822'}";
1354        if ($ad{'hour_local'} < 6) {
1355                printf(" (<span style=\"color: #cc0000;\">%02d:%02d</span> %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1356        } else {
1357                printf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1358        }
1359        print "</td>" .
1360              "</tr>\n";
1361        print "<tr><td>committer</td><td>" . escapeHTML($co{'committer'}) . "</td></tr>\n";
1362        print "<tr><td></td><td> $cd{'rfc2822'}" . sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) . "</td></tr>\n";
1363        print "<tr><td>commit</td><td style=\"font-family:monospace\">$hash</td></tr>\n";
1364        print "<tr>" .
1365              "<td>tree</td>" .
1366              "<td style=\"font-family:monospace\">" .
1367              $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash", class => "list"}, $co{'tree'}) .
1368              "</td>" .
1369              "<td class=\"link\">" . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash"}, "tree") .
1370              "</td>" .
1371              "</tr>\n";
1372        my $parents  = $co{'parents'};
1373        foreach my $par (@$parents) {
1374                print "<tr>" .
1375                      "<td>parent</td>" .
1376                      "<td style=\"font-family:monospace\">" . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$par", class => "list"}, $par) . "</td>" .
1377                      "<td class=\"link\">" .
1378                      $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$par"}, "commit") .
1379                      " |" . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash;hp=$par"}, "commitdiff") .
1380                      "</td>" .
1381                      "</tr>\n";
1382        }
1383        print "</table>". 
1384              "</div>\n";
1385        print "<div class=\"page_body\">\n";
1386        my $comment = $co{'comment'};
1387        my $empty = 0;
1388        my $signed = 0;
1389        foreach my $line (@$comment) {
1390                # print only one empty line
1391                if ($line eq "") {
1392                        if ($empty || $signed) {
1393                                next;
1394                        }
1395                        $empty = 1;
1396                } else {
1397                        $empty = 0;
1398                }
1399                if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1400                        $signed = 1;
1401                        print "<span style=\"color: #888888\">" . escapeHTML($line) . "</span><br/>\n";
1402                } else {
1403                        $signed = 0;
1404                        print escapeHTML($line) . "<br/>\n";
1405                }
1406        }
1407        print "</div>\n";
1408        print "<div class=\"list_head\">\n";
1409        if ($#difftree > 10) {
1410                print(($#difftree + 1) . " files changed:\n");
1411        }
1412        print "</div>\n";
1413        print "<table cellspacing=\"0\">\n";
1414        my $alternate = 0;
1415        foreach my $line (@difftree) {
1416                # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M      ls-files.c'
1417                # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M      rev-tree.c'
1418                $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/;
1419                my $from_mode = $1;
1420                my $to_mode = $2;
1421                my $from_id = $3;
1422                my $to_id = $4;
1423                my $status = $5;
1424                my $file = $6;
1425                #print "$line ($status)<br/>\n";
1426                if ($alternate) {
1427                        print "<tr style=\"background-color:#f6f5ed\">\n";
1428                } else {
1429                        print "<tr>\n";
1430                }
1431                $alternate ^= 1;
1432                if ($status eq "N") {
1433                        my $mode_chng = "";
1434                        if (S_ISREG(oct $to_mode)) {
1435                                $mode_chng = sprintf(" with mode: %04o", (oct $to_mode) & 0777);
1436                        }
1437                        print "<td>" .
1438                              $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$to_id;hp=$hash;f=$file", -class => "list"}, escapeHTML($file)) . "</td>\n" .
1439                              "<td><span style=\"color: #008000;\">[new " . file_type($to_mode) . "$mode_chng]</span></td>\n" .
1440                              "<td class=\"link\">" . $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$to_id;hb=$hash;f=$file"}, "blob") . "</td>\n";
1441                } elsif ($status eq "D") {
1442                        print "<td>" .
1443                              $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$from_id;hb=$hash;f=$file", -class => "list"}, escapeHTML($file)) . "</td>\n" .
1444                              "<td><span style=\"color: #c00000;\">[deleted " . file_type($from_mode). "]</span></td>\n" .
1445                              "<td class=\"link\">" .
1446                              $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$from_id;hb=$hash;f=$file"}, "blob") .
1447                              " | " . $cgi->a({-href => "$my_uri?p=$project;a=history;h=$hash;f=$file"}, "history") .
1448                              "</td>\n"
1449                } elsif ($status eq "M" || $status eq "T") {
1450                        my $mode_chnge = "";
1451                        if ($from_mode != $to_mode) {
1452                                $mode_chnge = " <span style=\"color: #777777;\">[changed";
1453                                if (((oct $from_mode) & S_IFMT) != ((oct $to_mode) & S_IFMT)) {
1454                                        $mode_chnge .= " from " . file_type($from_mode) . " to " . file_type($to_mode);
1455                                }
1456                                if (((oct $from_mode) & 0777) != ((oct $to_mode) & 0777)) {
1457                                        if (S_ISREG($from_mode) && S_ISREG($to_mode)) {
1458                                                $mode_chnge .= sprintf(" mode: %04o->%04o", (oct $from_mode) & 0777, (oct $to_mode) & 0777);
1459                                        } elsif (S_ISREG($to_mode)) {
1460                                                $mode_chnge .= sprintf(" mode: %04o", (oct $to_mode) & 0777);
1461                                        }
1462                                }
1463                                $mode_chnge .= "]</span>\n";
1464                        }
1465                        print "<td>";
1466                        if ($to_id ne $from_id) {
1467                                print $cgi->a({-href => "$my_uri?p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$file", -class => "list"}, escapeHTML($file));
1468                        } else {
1469                                print $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$to_id;hb=$hash;f=$file", -class => "list"}, escapeHTML($file));
1470                        }
1471                        print "</td>\n" .
1472                              "<td>$mode_chnge</td>\n" .
1473                              "<td class=\"link\">";
1474                        print $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$to_id;hb=$hash;f=$file"}, "blob");
1475                        if ($to_id ne $from_id) {
1476                                print " | " . $cgi->a({-href => "$my_uri?p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$file"}, "diff");
1477                        }
1478                        print " | " . $cgi->a({-href => "$my_uri?p=$project;a=history;h=$hash;f=$file"}, "history") . "\n";
1479                        print "</td>\n";
1480                }
1481                print "</tr>\n";
1482        }
1483        print "</table>\n";
1484        git_footer_html();
1485}
1486
1487sub git_blobdiff {
1488        mkdir($git_temp, 0700);
1489        git_header_html();
1490        if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
1491                print "<div class=\"page_nav\">\n" .
1492                      $cgi->a({-href => "$my_uri?p=$project;a=summary"}, "summary") .
1493                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog"}, "shortlog") .
1494                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=log"}, "log") .
1495                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash_base"}, "commit") .
1496                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash_base"}, "commitdiff") .
1497                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash_base"}, "tree") .
1498                      "<br/>\n";
1499                print $cgi->a({-href => "$my_uri?p=$project;a=blobdiff_plain;h=$hash;hp=$hash_parent"}, "plain") .
1500                      "</div>\n";
1501                print "<div>\n" .
1502                      $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash_base", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
1503                      "</div>\n";
1504        } else {
1505                print "<div class=\"page_nav\">\n" .
1506                      "<br/><br/></div>\n" .
1507                      "<div class=\"title\">$hash vs $hash_parent</div>\n";
1508        }
1509        if (defined $file_name) {
1510                print "<div class=\"page_path\"><b>/$file_name</b></div>\n";
1511        }
1512        print "<div class=\"page_body\">\n" .
1513              "<div class=\"diff_info\">blob:" .
1514              $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$hash_parent;hb=$hash_base;f=$file_name"}, $hash_parent) .
1515              " -> blob:" .
1516              $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$hash;hb=$hash_base;f=$file_name"}, $hash) .
1517              "</div>\n";
1518        git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash);
1519        print "</div>";
1520        git_footer_html();
1521}
1522
1523sub git_blobdiff_plain {
1524        mkdir($git_temp, 0700);
1525        print $cgi->header(-type => "text/plain", -charset => 'utf-8');
1526        git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash, "plain");
1527}
1528
1529sub git_commitdiff {
1530        mkdir($git_temp, 0700);
1531        my %co = git_read_commit($hash);
1532        if (!%co) {
1533                die_error(undef, "Unknown commit object.");
1534        }
1535        if (!defined $hash_parent) {
1536                $hash_parent = $co{'parent'};
1537        }
1538        open my $fd, "-|", "$gitbin/git-diff-tree -r $hash_parent $hash" or die_error(undef, "Open failed.");
1539        my (@difftree) = map { chomp; $_ } <$fd>;
1540        close $fd or die_error(undef, "Reading diff-tree failed.");
1541
1542        git_header_html();
1543        print "<div class=\"page_nav\">\n" .
1544              $cgi->a({-href => "$my_uri?p=$project;a=summary"}, "summary") .
1545              " | " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog;h=$hash"}, "shortlog") .
1546              " | " . $cgi->a({-href => "$my_uri?p=$project;a=log;h=$hash"}, "log") .
1547              " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash"}, "commit") .
1548              " | commitdiff" .
1549              " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash"}, "tree") . "<br/>\n";
1550        print $cgi->a({-href => "$my_uri?p=$project;a=commitdiff_plain;h=$hash;hp=$hash_parent"}, "plain") . "\n" .
1551              "</div>\n";
1552        print "<div>\n" .
1553              $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
1554              "</div>\n";
1555        print "<div class=\"page_body\">\n";
1556        my $comment = $co{'comment'};
1557        my $empty = 0;
1558        my $signed = 0;
1559        my @log = @$comment;
1560        # remove first and empty lines after that
1561        shift @log;
1562        while (defined $log[0] && $log[0] eq "") {
1563                shift @log;
1564        }
1565        foreach my $line (@log) {
1566                if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1567                        next;
1568                }
1569                if ($line eq "") {
1570                        if ($empty) {
1571                                next;
1572                        }
1573                        $empty = 1;
1574                } else {
1575                        $empty = 0;
1576                }
1577                print escapeHTML($line) . "<br/>\n";
1578        }
1579        print "<br/>\n";
1580        foreach my $line (@difftree) {
1581                # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M      ls-files.c'
1582                # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M      rev-tree.c'
1583                $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/;
1584                my $from_mode = $1;
1585                my $to_mode = $2;
1586                my $from_id = $3;
1587                my $to_id = $4;
1588                my $status = $5;
1589                my $file = $6;
1590                if ($status eq "N") {
1591                        print "<div class=\"diff_info\">" .  file_type($to_mode) . ":" .
1592                              $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$to_id;hb=$hash;f=$file"}, $to_id) . "(new)" .
1593                              "</div>\n";
1594                        git_diff_print(undef, "/dev/null", $to_id, "b/$file");
1595                } elsif ($status eq "D") {
1596                        print "<div class=\"diff_info\">" . file_type($from_mode) . ":" .
1597                              $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$from_id;hb=$hash;f=$file"}, $from_id) . "(deleted)" .
1598                              "</div>\n";
1599                        git_diff_print($from_id, "a/$file", undef, "/dev/null");
1600                } elsif ($status eq "M") {
1601                        if ($from_id ne $to_id) {
1602                                print "<div class=\"diff_info\">" .
1603                                      file_type($from_mode) . ":" . $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$from_id;hb=$hash;f=$file"}, $from_id) .
1604                                      " -> " .
1605                                      file_type($to_mode) . ":" . $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$to_id;hb=$hash;f=$file"}, $to_id);
1606                                print "</div>\n";
1607                                git_diff_print($from_id, "a/$file",  $to_id, "b/$file");
1608                        }
1609                }
1610        }
1611        print "<br/>\n" .
1612              "</div>";
1613        git_footer_html();
1614}
1615
1616sub git_commitdiff_plain {
1617        mkdir($git_temp, 0700);
1618        open my $fd, "-|", "$gitbin/git-diff-tree -r $hash_parent $hash" or die_error(undef, "Open failed.");
1619        my (@difftree) = map { chomp; $_ } <$fd>;
1620        close $fd or die_error(undef, "Reading diff-tree failed.");
1621
1622        print $cgi->header(-type => "text/plain", -charset => 'utf-8');
1623        foreach my $line (@difftree) {
1624                $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/;
1625                my $from_id = $3;
1626                my $to_id = $4;
1627                my $status = $5;
1628                my $file = $6;
1629                if ($status eq "N") {
1630                        git_diff_print(undef, "/dev/null", $to_id, "b/$file", "plain");
1631                } elsif ($status eq "D") {
1632                        git_diff_print($from_id, "a/$file", undef, "/dev/null", "plain");
1633                } elsif ($status eq "M") {
1634                        git_diff_print($from_id, "a/$file",  $to_id, "b/$file", "plain");
1635                }
1636        }
1637}
1638
1639sub git_history {
1640        if (!defined $hash) {
1641                $hash = git_read_hash("$project/HEAD");
1642        }
1643        my %co = git_read_commit($hash);
1644        if (!%co) {
1645                die_error(undef, "Unknown commit object.");
1646        }
1647        git_header_html();
1648        print "<div class=\"page_nav\">\n" .
1649              $cgi->a({-href => "$my_uri?p=$project;a=summary"}, "summary") .
1650              " | " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog"}, "shortlog") .
1651              " | " . $cgi->a({-href => "$my_uri?p=$project;a=log"}, "log") .
1652              " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash"}, "commit") .
1653              " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash"}, "commitdiff") .
1654              " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash"}, "tree") .
1655              "<br/><br/>\n" .
1656              "</div>\n";
1657        print "<div>\n" .
1658              $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
1659              "</div>\n";
1660        print "<div class=\"page_path\"><b>/$file_name</b><br/></div>\n";
1661
1662        open my $fd, "-|", "$gitbin/git-rev-list $hash | $gitbin/git-diff-tree -r --stdin $file_name";
1663        my $commit;
1664        print "<table cellspacing=\"0\">\n";
1665        my $alternate = 0;
1666        while (my $line = <$fd>) {
1667                if ($line =~ m/^([0-9a-fA-F]{40}) /){
1668                        $commit = $1;
1669                        next;
1670                }
1671                if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/ && (defined $commit)) {
1672                        my %co = git_read_commit($commit);
1673                        if (!%co) {
1674                                next;
1675                        }
1676                        if ($alternate) {
1677                                print "<tr style=\"background-color:#f6f5ed\">\n";
1678                        } else {
1679                                print "<tr>\n";
1680                        }
1681                        $alternate ^= 1;
1682                        print "<td><i>$co{'age_string'}</i></td>\n" .
1683                              "<td><i>" . escapeHTML(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
1684                              "<td>" . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit", -class => "list"}, "<b>" .
1685                              escapeHTML(chop_str($co{'title'}, 50)) . "</b>") . "</td>\n" .
1686                              "<td class=\"link\">" .
1687                              $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit"}, "commit") .
1688                              " | " . $cgi->a({-href => "$my_uri?p=$project;a=blob;hb=$commit;f=$file_name"}, "blob");
1689                        my $blob = git_get_hash_by_path($hash, $file_name);
1690                        my $blob_parent = git_get_hash_by_path($commit, $file_name);
1691                        if (defined $blob && defined $blob_parent && $blob ne $blob_parent) {
1692                                print " | " . $cgi->a({-href => "$my_uri?p=$project;a=blobdiff;h=$blob;hp=$blob_parent;hb=$commit;f=$file_name"}, "diff to current");
1693                        }
1694                        print "</td>\n" .
1695                              "</tr>\n";
1696                        undef $commit;
1697                }
1698        }
1699        print "</table>\n";
1700        close $fd;
1701        git_footer_html();
1702}
1703
1704sub git_search {
1705        if (!defined $searchtext) {
1706                die_error("", "Text field empty.");
1707        }
1708        if (!defined $hash) {
1709                $hash = git_read_hash("$project/HEAD");
1710        }
1711        my %co = git_read_commit($hash);
1712        if (!%co) {
1713                die_error(undef, "Unknown commit object.");
1714        }
1715        git_header_html();
1716        print "<div class=\"page_nav\">\n" .
1717              $cgi->a({-href => "$my_uri?p=$project;a=summary;h=$hash"}, "summary") .
1718              " | " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog"}, "shortlog") .
1719              " | " . $cgi->a({-href => "$my_uri?p=$project;a=log;h=$hash"}, "log") .
1720              " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash"}, "commit") .
1721              " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash"}, "commitdiff") .
1722              " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash"}, "tree") .
1723              "<br/><br/>\n" .
1724              "</div>\n";
1725
1726        print "<div>\n" .
1727              $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
1728              "</div>\n";
1729        print "<table cellspacing=\"0\">\n";
1730        $/ = "\0";
1731        open my $fd, "-|", "$gitbin/git-rev-list --header $hash";
1732        my $alternate = 0;
1733        while (my $commit_text = <$fd>) {
1734                if (!grep m/$searchtext/, $commit_text) {
1735                        next;
1736                }
1737                my @commit_lines = split "\n", $commit_text;
1738                my $commit = shift @commit_lines;
1739                my %co = git_read_commit($commit, \@commit_lines);
1740                if (!%co) {
1741                        next;
1742                }
1743                if ($alternate) {
1744                        print "<tr style=\"background-color:#f6f5ed\">\n";
1745                } else {
1746                        print "<tr>\n";
1747                }
1748                $alternate ^= 1;
1749                print "<td><i>$co{'age_string'}</i></td>\n" .
1750                      "<td><i>" . escapeHTML(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
1751                      "<td>" .
1752                      $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit", -class => "list"}, "<b>" . escapeHTML(chop_str($co{'title'}, 50)) . "</b><br/>");
1753                my $comment = $co{'comment'};
1754                foreach my $line (@$comment) {
1755                        if ($line =~ m/^(.*)($searchtext)(.*)$/) {
1756                                my $lead = escapeHTML($1) || "";
1757                                $lead = chop_str($lead, 30, 10);
1758                                my $match = escapeHTML($2) || "";
1759                                my $trail = escapeHTML($3) || "";
1760                                $trail = chop_str($trail, 30, 10);
1761                                my $text = "$lead<span style=\"color:#e00000\">$match</span>$trail";
1762                                print chop_str($text, 80, 5) . "<br/>\n";
1763                        }
1764                }
1765                print "</td>\n" .
1766                      "<td class=\"link\">" .
1767                      $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit"}, "commit") .
1768                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$commit"}, "tree");
1769                print "</td>\n" .
1770                      "</tr>\n";
1771        }
1772        close $fd;
1773
1774        $/ = "\n";
1775        open $fd, "-|", "$gitbin/git-rev-list $hash | $gitbin/git-diff-tree -r --stdin -S$searchtext";
1776        undef %co;
1777        my @files;
1778        while (my $line = <$fd>) {
1779                if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
1780                        my %set;
1781                        $set{'file'} = $6;
1782                        $set{'from_id'} = $3;
1783                        $set{'to_id'} = $4;
1784                        $set{'id'} = $set{'to_id'};
1785                        if ($set{'id'} =~ m/0{40}/) {
1786                                $set{'id'} = $set{'from_id'};
1787                        }
1788                        if ($set{'id'} =~ m/0{40}/) {
1789                                next;
1790                        }
1791                        push @files, \%set;
1792                } elsif ($line =~ m/^([0-9a-fA-F]{40}) /){
1793                        if (%co) {
1794                                if ($alternate) {
1795                                        print "<tr style=\"background-color:#f6f5ed\">\n";
1796                                } else {
1797                                        print "<tr>\n";
1798                                }
1799                                $alternate ^= 1;
1800                                print "<td><i>$co{'age_string'}</i></td>\n" .
1801                                      "<td><i>" . escapeHTML(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
1802                                      "<td>" .
1803                                      $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$co{'id'}", -class => "list"}, "<b>" .
1804                                      escapeHTML(chop_str($co{'title'}, 50)) . "</b><br/>");
1805                                while (my $setref = shift @files) {
1806                                        my %set = %$setref;
1807                                        print $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$set{'id'};hb=$co{'id'};f=$set{'file'}", class => "list"},
1808                                        escapeHTML($set{'file'})) . "<br/>\n";
1809                                }
1810                                print "</td>\n" .
1811                                      "<td class=\"link\">" .
1812                                      $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$co{'id'}"}, "commit") .
1813                                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$co{'id'}"}, "tree");
1814                                print "</td>\n" .
1815                                      "</tr>\n";
1816                        }
1817                        %co = git_read_commit($1);
1818                }
1819        }
1820        print "</table>\n";
1821        close $fd;
1822        git_footer_html();
1823}
1824
1825sub git_shortlog {
1826        my $head = git_read_hash("$project/HEAD");
1827        if (!defined $hash) {
1828                $hash = $head;
1829        }
1830
1831        git_header_html();
1832        print "<div class=\"page_nav\">\n" .
1833              $cgi->a({-href => "$my_uri?p=$project;a=summary"}, "summary") .
1834              " | shortlog" .
1835              " | " . $cgi->a({-href => "$my_uri?p=$project;a=log;h=$hash"}, "log") .
1836              " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash"}, "commit") .
1837              " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash"}, "commitdiff") .
1838              " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$hash;hb=$hash"}, "tree") . "<br/>\n";
1839        if ($hash ne $head) {
1840                print $cgi->a({-href => "$my_uri?p=$project;a=shortlog"}, "HEAD") . " &sdot; ";
1841        }
1842        print $cgi->a({-href => "$my_uri?p=$project;a=shortlog;h=$hash"}, "100") .
1843              " &sdot; " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog;t=1;h=$hash"}, "day") .
1844              " &sdot; " .$cgi->a({-href => "$my_uri?p=$project;a=shortlog;t=7;h=$hash"}, "week") .
1845              " &sdot; " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog;t=31;h=$hash"}, "month") .
1846              " &sdot; " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog;t=365;h=$hash"}, "year") .
1847              " &sdot; " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog;t=0;h=$hash"}, "all") .
1848              "<br/>\n" .
1849              "</div>\n";
1850        my $limit = "";
1851        if (defined $time_back) {
1852                if ($time_back) {
1853                        $limit = sprintf(" --max-age=%i", time - 60*60*24*$time_back);
1854                }
1855        } else {
1856                $limit = " --max-count=100";
1857        }
1858        open my $fd, "-|", "$gitbin/git-rev-list $limit $hash" or die_error(undef, "Open failed.");
1859        my (@revlist) = map { chomp; $_ } <$fd>;
1860        close $fd;
1861        print "<div>\n" .
1862              $cgi->a({-href => "$my_uri?p=$project;a=summary", -class => "title"}, "&nbsp;") .
1863              "</div>\n";
1864        print "<table cellspacing=\"0\">\n";
1865        if (!@revlist) {
1866                my %co = git_read_commit($hash);
1867                print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
1868        }
1869        my $alternate = 0;
1870        foreach my $commit (@revlist) {
1871                my %co = git_read_commit($commit);
1872                my %ad = date_str($co{'author_epoch'});
1873                if ($alternate) {
1874                        print "<tr style=\"background-color:#f6f5ed\">\n";
1875                } else {
1876                        print "<tr>\n";
1877                }
1878                $alternate ^= 1;
1879                print "<td><i>$co{'age_string'}</i></td>\n" .
1880                      "<td><i>" . escapeHTML(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
1881                      "<td>" . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit", -class => "list"}, "<b>" .
1882                      escapeHTML($co{'title_short'}) . "</b>") . "</td>\n" .
1883                      "<td class=\"link\">" .
1884                      $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit"}, "commit") .
1885                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$commit"}, "commitdiff") .
1886                      "</td>\n" .
1887                      "</tr>";
1888        }
1889        print "</table\n>";
1890        git_footer_html();
1891}