gitweb.cgion commit add test files (cc3245b)
   1#!/usr/bin/perl
   2
   3# gitweb - 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 GPLv2
   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 =           "248";
  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
  24#my $projectroot =      "/pub/scm";
  25my $projectroot = "/home/kay/public_html/pub/scm";
  26
  27# location of the git-core binaries
  28my $gitbin =            "/usr/bin";
  29
  30# location for temporary files needed for diffs
  31my $git_temp =          "/tmp/gitweb";
  32
  33# target of the home link on top of all pages
  34my $home_link =         $my_uri;
  35
  36# html text to include at home page
  37my $home_text =         "indextext.html";
  38
  39# source of projects list
  40#my $projects_list = $projectroot;
  41my $projects_list = "index/index.aux";
  42
  43# input validation and dispatch
  44my $action = $cgi->param('a');
  45if (defined $action) {
  46        if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
  47                undef $action;
  48                die_error(undef, "Invalid action parameter.");
  49        }
  50        if ($action eq "git-logo.png") {
  51                git_logo();
  52                exit;
  53        } elsif ($action eq "opml") {
  54                git_opml();
  55                exit;
  56        }
  57}
  58
  59my $order = $cgi->param('o');
  60if (defined $order) {
  61        if ($order =~ m/[^0-9a-zA-Z_]/) {
  62                undef $order;
  63                die_error(undef, "Invalid order parameter.");
  64        }
  65}
  66
  67my $project = $cgi->param('p');
  68if (defined $project) {
  69        $project = validate_input($project);
  70        if (!defined($project)) {
  71                die_error(undef, "Invalid project parameter.");
  72        }
  73        if (!(-d "$projectroot/$project")) {
  74                undef $project;
  75                die_error(undef, "No such directory.");
  76        }
  77        if (!(-e "$projectroot/$project/HEAD")) {
  78                undef $project;
  79                die_error(undef, "No such project.");
  80        }
  81        $rss_link = "<link rel=\"alternate\" title=\"$project log\" href=\"$my_uri?p=$project;a=rss\" type=\"application/rss+xml\"/>";
  82        $ENV{'GIT_DIR'} = "$projectroot/$project";
  83} else {
  84        git_project_list();
  85        exit;
  86}
  87
  88my $file_name = $cgi->param('f');
  89if (defined $file_name) {
  90        $file_name = validate_input($file_name);
  91        if (!defined($file_name)) {
  92                die_error(undef, "Invalid file parameter.");
  93        }
  94}
  95
  96my $hash = $cgi->param('h');
  97if (defined $hash) {
  98        $hash = validate_input($hash);
  99        if (!defined($hash)) {
 100                die_error(undef, "Invalid hash parameter.");
 101        }
 102}
 103
 104my $hash_parent = $cgi->param('hp');
 105if (defined $hash_parent) {
 106        $hash_parent = validate_input($hash_parent);
 107        if (!defined($hash_parent)) {
 108                die_error(undef, "Invalid hash parent parameter.");
 109        }
 110}
 111
 112my $hash_base = $cgi->param('hb');
 113if (defined $hash_base) {
 114        $hash_base = validate_input($hash_base);
 115        if (!defined($hash_base)) {
 116                die_error(undef, "Invalid hash base parameter.");
 117        }
 118}
 119
 120my $page = $cgi->param('pg');
 121if (defined $page) {
 122        if ($page =~ m/[^0-9]$/) {
 123                undef $page;
 124                die_error(undef, "Invalid page parameter.");
 125        }
 126}
 127
 128my $searchtext = $cgi->param('s');
 129if (defined $searchtext) {
 130        if ($searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
 131                undef $searchtext;
 132                die_error(undef, "Invalid search parameter.");
 133        }
 134        $searchtext = quotemeta $searchtext;
 135}
 136
 137sub validate_input {
 138        my $input = shift;
 139
 140        if ($input =~ m/^[0-9a-fA-F]{40}$/) {
 141                return $input;
 142        }
 143        if ($input =~ m/(^|\/)(|\.|\.\.)($|\/)/) {
 144                return undef;
 145        }
 146        if ($input =~ m/[^a-zA-Z0-9_\.\/\-\+\#\~]/) {
 147                return undef;
 148        }
 149        return $input;
 150}
 151
 152if (!defined $action || $action eq "summary") {
 153        git_summary();
 154        exit;
 155} elsif ($action eq "heads") {
 156        git_heads();
 157        exit;
 158} elsif ($action eq "tags") {
 159        git_tags();
 160        exit;
 161} elsif ($action eq "blob") {
 162        git_blob();
 163        exit;
 164} elsif ($action eq "blob_plain") {
 165        git_blob_plain();
 166        exit;
 167} elsif ($action eq "tree") {
 168        git_tree();
 169        exit;
 170} elsif ($action eq "rss") {
 171        git_rss();
 172        exit;
 173} elsif ($action eq "commit") {
 174        git_commit();
 175        exit;
 176} elsif ($action eq "log") {
 177        git_log();
 178        exit;
 179} elsif ($action eq "blobdiff") {
 180        git_blobdiff();
 181        exit;
 182} elsif ($action eq "blobdiff_plain") {
 183        git_blobdiff_plain();
 184        exit;
 185} elsif ($action eq "commitdiff") {
 186        git_commitdiff();
 187        exit;
 188} elsif ($action eq "commitdiff_plain") {
 189        git_commitdiff_plain();
 190        exit;
 191} elsif ($action eq "history") {
 192        git_history();
 193        exit;
 194} elsif ($action eq "search") {
 195        git_search();
 196        exit;
 197} elsif ($action eq "shortlog") {
 198        git_shortlog();
 199        exit;
 200} elsif ($action eq "tag") {
 201        git_tag();
 202        exit;
 203} else {
 204        undef $action;
 205        die_error(undef, "Unknown action.");
 206        exit;
 207}
 208
 209sub git_header_html {
 210        my $status = shift || "200 OK";
 211        my $expires = shift;
 212
 213        my $title = "git";
 214        if (defined $project) {
 215                $title .= " - $project";
 216                if (defined $action) {
 217                        $title .= "/$action";
 218                }
 219        }
 220        print $cgi->header(-type=>'text/html',  -charset => 'utf-8', -status=> $status, -expires => $expires);
 221        print <<EOF;
 222<?xml version="1.0" encoding="utf-8"?>
 223<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 224<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
 225<!-- git web interface v$version, (C) 2005, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke <ch\@gierke.de> -->
 226<head>
 227<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
 228<meta name="robots" content="index, nofollow"/>
 229<title>$title</title>
 230$rss_link
 231<style type="text/css">
 232body { font-family: sans-serif; font-size: 12px; margin:0px; border:solid #d9d8d1; border-width:1px; margin:10px; }
 233a { color:#0000cc; }
 234a:hover, a:visited, a:active { color:#880000; }
 235div.page_header { height:25px; padding:8px; font-size:18px; font-weight:bold; background-color:#d9d8d1; }
 236div.page_header a:visited, a.header { color:#0000cc; }
 237div.page_header a:hover { color:#880000; }
 238div.page_nav { padding:8px; }
 239div.page_nav a:visited { color:#0000cc; }
 240div.page_path { padding:8px; border:solid #d9d8d1; border-width:0px 0px 1px}
 241div.page_footer { height:17px; padding:4px 8px; background-color: #d9d8d1; }
 242div.page_footer_text { float:left; color:#555555; font-style:italic; }
 243div.page_body { padding:8px; }
 244div.title, a.title {
 245        display:block; padding:6px 8px;
 246        font-weight:bold; background-color:#edece6; text-decoration:none; color:#000000;
 247}
 248a.title:hover { background-color: #d9d8d1; }
 249div.title_text { padding:6px 0px; border: solid #d9d8d1; border-width:0px 0px 1px; }
 250div.log_body { padding:8px 8px 8px 150px; }
 251span.age { position:relative; float:left; width:142px; font-style:italic; }
 252div.log_link {
 253        padding:0px 8px;
 254        font-size:10px; font-family:sans-serif; font-style:normal;
 255        position:relative; float:left; width:136px;
 256}
 257div.list_head { padding:6px 8px 4px; border:solid #d9d8d1; border-width:1px 0px 0px; font-style:italic; }
 258a.list { text-decoration:none; color:#000000; }
 259a.list:hover { text-decoration:underline; color:#880000; }
 260a.text { text-decoration:none; color:#0000cc; }
 261a.text:visited { text-decoration:none; color:#880000; }
 262a.text:hover { text-decoration:underline; color:#880000; }
 263table { padding:8px 4px; }
 264th { padding:2px 5px; font-size:12px; text-align:left; }
 265tr.light:hover { background-color:#edece6; }
 266tr.dark { background-color:#f6f6f0; }
 267tr.dark:hover { background-color:#edece6; }
 268td { padding:2px 5px; font-size:12px; vertical-align:top; }
 269td.link { padding:2px 5px; font-family:sans-serif; font-size:10px; }
 270div.pre { font-family:monospace; font-size:12px; white-space:pre; }
 271div.diff_info { font-family:monospace; color:#000099; background-color:#edece6; font-style:italic; }
 272div.index_include { border:solid #d9d8d1; border-width:0px 0px 1px; padding:12px 8px; }
 273div.search { margin:4px 8px; position:absolute; top:56px; right:12px }
 274a.linenr { color:#999999; text-decoration:none }
 275a.rss_logo {
 276        float:right; padding:3px 0px; width:35px; line-height:10px;
 277        border:1px solid; border-color:#fcc7a5 #7d3302 #3e1a01 #ff954e;
 278        color:#ffffff; background-color:#ff6600;
 279        font-weight:bold; font-family:sans-serif; font-size:10px;
 280        text-align:center; text-decoration:none;
 281}
 282a.rss_logo:hover { background-color:#ee5500; }
 283</style>
 284</head>
 285<body>
 286EOF
 287        print "<div class=\"page_header\">\n" .
 288              "<a href=\"http://www.kernel.org/pub/software/scm/git/docs/\" title=\"git documentation\">" .
 289              "<img src=\"$my_uri?a=git-logo.png\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/>" .
 290              "</a>\n";
 291        print $cgi->a({-href => $home_link}, "projects") . " / ";
 292        if (defined $project) {
 293                print $cgi->a({-href => "$my_uri?p=$project;a=summary"}, escapeHTML($project));
 294                if (defined $action) {
 295                        print " / $action";
 296                }
 297                print "\n";
 298                if (!defined $searchtext) {
 299                        $searchtext = "";
 300                }
 301                my $search_hash;
 302                if (defined $hash) {
 303                        $search_hash = $hash;
 304                } else {
 305                        $search_hash  = "HEAD";
 306                }
 307                $cgi->param("a", "search");
 308                $cgi->param("h", $search_hash);
 309                print $cgi->startform(-method => "get", -action => "$my_uri") .
 310                      "<div class=\"search\">\n" .
 311                      $cgi->hidden(-name => "p") . "\n" .
 312                      $cgi->hidden(-name => "a") . "\n" .
 313                      $cgi->hidden(-name => "h") . "\n" .
 314                      $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
 315                      "</div>" .
 316                      $cgi->end_form() . "\n";
 317        }
 318        print "</div>\n";
 319}
 320
 321sub git_footer_html {
 322        print "<div class=\"page_footer\">\n";
 323        if (defined $project) {
 324                my $descr = git_read_description($project);
 325                if (defined $descr) {
 326                        print "<div class=\"page_footer_text\">" . escapeHTML($descr) . "</div>\n";
 327                }
 328                print $cgi->a({-href => "$my_uri?p=$project;a=rss", -class => "rss_logo"}, "RSS") . "\n";
 329        } else {
 330                print $cgi->a({-href => "$my_uri?a=opml", -class => "rss_logo"}, "OPML") . "\n";
 331        }
 332        print "</div>\n" .
 333              "</body>\n" .
 334              "</html>";
 335}
 336
 337sub die_error {
 338        my $status = shift || "403 Forbidden";
 339        my $error = shift || "Malformed query, file missing or permission denied"; 
 340
 341        git_header_html($status);
 342        print "<div class=\"page_body\">\n" .
 343              "<br/><br/>\n" .
 344              "$status - $error\n" .
 345              "<br/>\n" .
 346              "</div>\n";
 347        git_footer_html();
 348        exit;
 349}
 350
 351sub git_get_type {
 352        my $hash = shift;
 353
 354        open my $fd, "-|", "$gitbin/git-cat-file -t $hash" or return;
 355        my $type = <$fd>;
 356        close $fd or return;
 357        chomp $type;
 358        return $type;
 359}
 360
 361sub git_read_hash {
 362        my $path = shift;
 363
 364        open my $fd, "$projectroot/$path" or return undef;
 365        my $head = <$fd>;
 366        close $fd;
 367        chomp $head;
 368        if ($head =~ m/^[0-9a-fA-F]{40}$/) {
 369                return $head;
 370        }
 371}
 372
 373sub git_read_description {
 374        my $path = shift;
 375
 376        open my $fd, "$projectroot/$path/description" or return undef;
 377        my $descr = <$fd>;
 378        close $fd;
 379        chomp $descr;
 380        return $descr;
 381}
 382
 383sub git_read_tag {
 384        my $tag_id = shift;
 385        my %tag;
 386        my @comment;
 387
 388        open my $fd, "-|", "$gitbin/git-cat-file tag $tag_id" or return;
 389        $tag{'id'} = $tag_id;
 390        while (my $line = <$fd>) {
 391                chomp $line;
 392                if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
 393                        $tag{'object'} = $1;
 394                } elsif ($line =~ m/^type (.+)$/) {
 395                        $tag{'type'} = $1;
 396                } elsif ($line =~ m/^tag (.+)$/) {
 397                        $tag{'name'} = $1;
 398                } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
 399                        $tag{'author'} = $1;
 400                        $tag{'epoch'} = $2;
 401                        $tag{'tz'} = $3;
 402                } elsif ($line =~ m/--BEGIN/) {
 403                        push @comment, $line;
 404                        last;
 405                } elsif ($line eq "") {
 406                        last;
 407                }
 408        }
 409        push @comment, <$fd>;
 410        $tag{'comment'} = \@comment;
 411        close $fd or return;
 412        if (!defined $tag{'name'}) {
 413                return
 414        };
 415        return %tag
 416}
 417
 418sub age_string {
 419        my $age = shift;
 420        my $age_str;
 421
 422        if ($age > 60*60*24*365*2) {
 423                $age_str = (int $age/60/60/24/365);
 424                $age_str .= " years ago";
 425        } elsif ($age > 60*60*24*(365/12)*2) {
 426                $age_str = int $age/60/60/24/(365/12);
 427                $age_str .= " months ago";
 428        } elsif ($age > 60*60*24*7*2) {
 429                $age_str = int $age/60/60/24/7;
 430                $age_str .= " weeks ago";
 431        } elsif ($age > 60*60*24*2) {
 432                $age_str = int $age/60/60/24;
 433                $age_str .= " days ago";
 434        } elsif ($age > 60*60*2) {
 435                $age_str = int $age/60/60;
 436                $age_str .= " hours ago";
 437        } elsif ($age > 60*2) {
 438                $age_str = int $age/60;
 439                $age_str .= " min ago";
 440        } elsif ($age > 2) {
 441                $age_str = int $age;
 442                $age_str .= " sec ago";
 443        } else {
 444                $age_str .= " right now";
 445        }
 446        return $age_str;
 447}
 448
 449sub git_read_commit {
 450        my $commit_id = shift;
 451        my $commit_text = shift;
 452
 453        my @commit_lines;
 454        my %co;
 455
 456        if (defined $commit_text) {
 457                @commit_lines = @$commit_text;
 458        } else {
 459                $/ = "\0";
 460                open my $fd, "-|", "$gitbin/git-rev-list --header --parents --max-count=1 $commit_id" or return;
 461                @commit_lines = split '\n', <$fd>;
 462                close $fd or return;
 463                $/ = "\n";
 464                pop @commit_lines;
 465        }
 466        my $header = shift @commit_lines;
 467        if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
 468                return;
 469        }
 470        ($co{'id'}, my @parents) = split ' ', $header;
 471        $co{'parents'} = \@parents;
 472        $co{'parent'} = $parents[0];
 473        while (my $line = shift @commit_lines) {
 474                last if $line eq "\n";
 475                if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
 476                        $co{'tree'} = $1;
 477                } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
 478                        $co{'author'} = $1;
 479                        $co{'author_epoch'} = $2;
 480                        $co{'author_tz'} = $3;
 481                        if ($co{'author'} =~ m/^([^<]+) </) {
 482                                $co{'author_name'} = $1;
 483                        } else {
 484                                $co{'author_name'} = $co{'author'};
 485                        }
 486                } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
 487                        $co{'committer'} = $1;
 488                        $co{'committer_epoch'} = $2;
 489                        $co{'committer_tz'} = $3;
 490                        $co{'committer_name'} = $co{'committer'};
 491                        $co{'committer_name'} =~ s/ <.*//;
 492                }
 493        }
 494        if (!defined $co{'tree'}) {
 495                return;
 496        };
 497
 498        foreach my $title (@commit_lines) {
 499                if ($title ne "") {
 500                        $co{'title'} = chop_str($title, 80, 5);
 501                        # remove leading stuff of merges to make the interesting part visible
 502                        if (length($title) > 50) {
 503                                $title =~ s/^Automatic //;
 504                                $title =~ s/^merge (of|with) /Merge ... /i;
 505                                if (length($title) > 50) {
 506                                        $title =~ s/(http|rsync):\/\///;
 507                                }
 508                                if (length($title) > 50) {
 509                                        $title =~ s/(master|www|rsync)\.//;
 510                                }
 511                                if (length($title) > 50) {
 512                                        $title =~ s/kernel.org:?//;
 513                                }
 514                                if (length($title) > 50) {
 515                                        $title =~ s/\/pub\/scm//;
 516                                }
 517                        }
 518                        $co{'title_short'} = chop_str($title, 50, 5);
 519                        last;
 520                }
 521        }
 522        # remove added spaces
 523        foreach my $line (@commit_lines) {
 524                $line =~ s/^    //;
 525        }
 526        $co{'comment'} = \@commit_lines;
 527
 528        my $age = time - $co{'committer_epoch'};
 529        $co{'age'} = $age;
 530        $co{'age_string'} = age_string($age);
 531        my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
 532        if ($age > 60*60*24*7*2) {
 533                $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
 534                $co{'age_string_age'} = $co{'age_string'};
 535        } else {
 536                $co{'age_string_date'} = $co{'age_string'};
 537                $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
 538        }
 539        return %co;
 540}
 541
 542sub git_diff_print {
 543        my $from = shift;
 544        my $from_name = shift;
 545        my $to = shift;
 546        my $to_name = shift;
 547        my $format = shift || "html";
 548
 549        my $from_tmp = "/dev/null";
 550        my $to_tmp = "/dev/null";
 551        my $pid = $$;
 552
 553        # create tmp from-file
 554        if (defined $from) {
 555                $from_tmp = "$git_temp/gitweb_" . $$ . "_from";
 556                open my $fd2, "> $from_tmp";
 557                open my $fd, "-|", "$gitbin/git-cat-file blob $from";
 558                my @file = <$fd>;
 559                print $fd2 @file;
 560                close $fd2;
 561                close $fd;
 562        }
 563
 564        # create tmp to-file
 565        if (defined $to) {
 566                $to_tmp = "$git_temp/gitweb_" . $$ . "_to";
 567                open my $fd2, "> $to_tmp";
 568                open my $fd, "-|", "$gitbin/git-cat-file blob $to";
 569                my @file = <$fd>;
 570                print $fd2 @file;
 571                close $fd2;
 572                close $fd;
 573        }
 574
 575        open my $fd, "-|", "/usr/bin/diff -u -p -L $from_name -L $to_name $from_tmp $to_tmp";
 576        if ($format eq "plain") {
 577                undef $/;
 578                print <$fd>;
 579                $/ = "\n";
 580        } else {
 581                while (my $line = <$fd>) {
 582                        chomp($line);
 583                        my $char = substr($line, 0, 1);
 584                        my $color = "";
 585                        if ($char eq '+') {
 586                                $color = " style=\"color:#008800;\"";
 587                        } elsif ($char eq "-") {
 588                                $color = " style=\"color:#cc0000;\"";
 589                        } elsif ($char eq "@") {
 590                                $color = " style=\"color:#990099;\"";
 591                        } elsif ($char eq "\\") {
 592                                # skip errors
 593                                next;
 594                        }
 595                        while ((my $pos = index($line, "\t")) != -1) {
 596                                if (my $count = (8 - (($pos-1) % 8))) {
 597                                        my $spaces = ' ' x $count;
 598                                        $line =~ s/\t/$spaces/;
 599                                }
 600                        }
 601                        print "<div class=\"pre\"$color>" . escapeHTML($line) . "</div>\n";
 602                }
 603        }
 604        close $fd;
 605
 606        if (defined $from) {
 607                unlink($from_tmp);
 608        }
 609        if (defined $to) {
 610                unlink($to_tmp);
 611        }
 612}
 613
 614sub mode_str {
 615        my $mode = oct shift;
 616
 617        if (S_ISDIR($mode & S_IFMT)) {
 618                return 'drwxr-xr-x';
 619        } elsif (S_ISLNK($mode)) {
 620                return 'lrwxrwxrwx';
 621        } elsif (S_ISREG($mode)) {
 622                # git cares only about the executable bit
 623                if ($mode & S_IXUSR) {
 624                        return '-rwxr-xr-x';
 625                } else {
 626                        return '-rw-r--r--';
 627                };
 628        } else {
 629                return '----------';
 630        }
 631}
 632
 633sub chop_str {
 634        my $str = shift;
 635        my $len = shift;
 636        my $add_len = shift || 10;
 637
 638        # allow only $len chars, but don't cut a word if it would fit in $add_len
 639        # if it doesn't fit, cut it if it's still longer than the dots we would add
 640        $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
 641        my $body = $1;
 642        my $tail = $2;
 643        if (length($tail) > 4) {
 644                $tail = " ...";
 645        }
 646        return "$body$tail";
 647}
 648
 649sub file_type {
 650        my $mode = oct shift;
 651
 652        if (S_ISDIR($mode & S_IFMT)) {
 653                return "directory";
 654        } elsif (S_ISLNK($mode)) {
 655                return "symlink";
 656        } elsif (S_ISREG($mode)) {
 657                return "file";
 658        } else {
 659                return "unknown";
 660        }
 661}
 662
 663sub format_log_line_html {
 664        my $line = shift;
 665
 666        $line = escapeHTML($line);
 667        $line =~ s/ /&nbsp;/g;
 668        if ($line =~ m/([0-9a-fA-F]{40})/) {
 669                my $hash_text = $1;
 670                if (git_get_type($hash_text) eq "commit") {
 671                        my $link = $cgi->a({-class => "text", -href => "$my_uri?p=$project;a=commit;h=$hash_text"}, $hash_text);
 672                        $line =~ s/$hash_text/$link/;
 673                }
 674        }
 675        return $line;
 676}
 677
 678sub date_str {
 679        my $epoch = shift;
 680        my $tz = shift || "-0000";
 681
 682        my %date;
 683        my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
 684        my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
 685        my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
 686        $date{'hour'} = $hour;
 687        $date{'minute'} = $min;
 688        $date{'mday'} = $mday;
 689        $date{'day'} = $days[$wday];
 690        $date{'month'} = $months[$mon];
 691        $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000", $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
 692        $date{'mday-time'} = sprintf "%d %s %02d:%02d", $mday, $months[$mon], $hour ,$min;
 693
 694        $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
 695        my $local = $epoch + ((int $1 + ($2/60)) * 3600);
 696        ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
 697        $date{'hour_local'} = $hour;
 698        $date{'minute_local'} = $min;
 699        $date{'tz_local'} = $tz;
 700        return %date;
 701}
 702
 703# git-logo (cached in browser for one day)
 704sub git_logo {
 705        print $cgi->header(-type => 'image/png', -expires => '+1d');
 706        # cat git-logo.png | hexdump -e '16/1 " %02x"  "\n"' | sed 's/ /\\x/g'
 707        print   "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52" .
 708                "\x00\x00\x00\x48\x00\x00\x00\x1b\x04\x03\x00\x00\x00\x2d\xd9\xd4" .
 709                "\x2d\x00\x00\x00\x18\x50\x4c\x54\x45\xff\xff\xff\x60\x60\x5d\xb0" .
 710                "\xaf\xaa\x00\x80\x00\xce\xcd\xc7\xc0\x00\x00\xe8\xe8\xe6\xf7\xf7" .
 711                "\xf6\x95\x0c\xa7\x47\x00\x00\x00\x73\x49\x44\x41\x54\x28\xcf\x63" .
 712                "\x48\x67\x20\x04\x4a\x5c\x18\x0a\x08\x2a\x62\x53\x61\x20\x02\x08" .
 713                "\x0d\x69\x45\xac\xa1\xa1\x01\x30\x0c\x93\x60\x36\x26\x52\x91\xb1" .
 714                "\x01\x11\xd6\xe1\x55\x64\x6c\x6c\xcc\x6c\x6c\x0c\xa2\x0c\x70\x2a" .
 715                "\x62\x06\x2a\xc1\x62\x1d\xb3\x01\x02\x53\xa4\x08\xe8\x00\x03\x18" .
 716                "\x26\x56\x11\xd4\xe1\x20\x97\x1b\xe0\xb4\x0e\x35\x24\x71\x29\x82" .
 717                "\x99\x30\xb8\x93\x0a\x11\xb9\x45\x88\xc1\x8d\xa0\xa2\x44\x21\x06" .
 718                "\x27\x41\x82\x40\x85\xc1\x45\x89\x20\x70\x01\x00\xa4\x3d\x21\xc5" .
 719                "\x12\x1c\x9a\xfe\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82";
 720}
 721
 722sub get_file_owner {
 723        my $path = shift;
 724
 725        my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
 726        my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
 727        if (!defined $gcos) {
 728                return undef;
 729        }
 730        my $owner = $gcos;
 731        $owner =~ s/[,;].*$//;
 732        return $owner;
 733}
 734
 735sub git_read_projects {
 736        my @list;
 737
 738        if (-d $projects_list) {
 739                # search in directory
 740                my $dir = $projects_list;
 741                opendir my $dh, $dir or return undef;
 742                while (my $dir = readdir($dh)) {
 743                        if (-e "$projectroot/$dir/HEAD") {
 744                                my $pr = {
 745                                        path => $dir,
 746                                };
 747                                push @list, $pr
 748                        }
 749                }
 750                closedir($dh);
 751        } elsif (-f $projects_list) {
 752                # read from file(url-encoded):
 753                # 'git%2Fgit.git Linus+Torvalds'
 754                # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
 755                # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
 756                open my $fd , $projects_list or return undef;
 757                while (my $line = <$fd>) {
 758                        chomp $line;
 759                        my ($path, $owner) = split ' ', $line;
 760                        $path = unescape($path);
 761                        $owner = unescape($owner);
 762                        if (!defined $path) {
 763                                next;
 764                        }
 765                        if (-e "$projectroot/$path/HEAD") {
 766                                my $pr = {
 767                                        path => $path,
 768                                        owner => $owner,
 769                                };
 770                                push @list, $pr
 771                        }
 772                }
 773                close $fd;
 774        }
 775        @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
 776        return @list;
 777}
 778
 779sub git_project_list {
 780        my @list = git_read_projects();
 781        my @projects;
 782        if (!@list) {
 783                die_error(undef, "No project found.");
 784        }
 785        foreach my $pr (@list) {
 786                my $head = git_read_hash("$pr->{'path'}/HEAD");
 787                if (!defined $head) {
 788                        next;
 789                }
 790                $ENV{'GIT_DIR'} = "$projectroot/$pr->{'path'}";
 791                my %co = git_read_commit($head);
 792                if (!%co) {
 793                        next;
 794                }
 795                $pr->{'commit'} = \%co;
 796                if (!defined $pr->{'descr'}) {
 797                        my $descr = git_read_description($pr->{'path'}) || "";
 798                        $pr->{'descr'} = chop_str($descr, 25, 5);
 799                }
 800                if (!defined $pr->{'owner'}) {
 801                        $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
 802                }
 803                push @projects, $pr;
 804        }
 805        git_header_html();
 806        if (-f $home_text) {
 807                print "<div class=\"index_include\">\n";
 808                open (my $fd, $home_text);
 809                print <$fd>;
 810                close $fd;
 811                print "</div>\n";
 812        }
 813        print "<table cellspacing=\"0\">\n" .
 814              "<tr>\n";
 815        if (!defined($order) || (defined($order) && ($order eq "project"))) {
 816                @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
 817                print "<th>Project</th>\n";
 818        } else {
 819                print "<th>" . $cgi->a({-class => "header", -href => "$my_uri?o=project"}, "Project") . "</th>\n";
 820        }
 821        if (defined($order) && ($order eq "descr")) {
 822                @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
 823                print "<th>Description</th>\n";
 824        } else {
 825                print "<th>" . $cgi->a({-class => "header", -href => "$my_uri?o=descr"}, "Description") . "</th>\n";
 826        }
 827        if (defined($order) && ($order eq "owner")) {
 828                @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
 829                print "<th>Owner</th>\n";
 830        } else {
 831                print "<th>" . $cgi->a({-class => "header", -href => "$my_uri?o=owner"}, "Owner") . "</th>\n";
 832        }
 833        if (defined($order) && ($order eq "age")) {
 834                @projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;
 835                print "<th>Last Change</th>\n";
 836        } else {
 837                print "<th>" . $cgi->a({-class => "header", -href => "$my_uri?o=age"}, "Last Change") . "</th>\n";
 838        }
 839        print "<th></th>\n" .
 840              "</tr>\n";
 841        my $alternate = 0;
 842        foreach my $pr (@projects) {
 843                if ($alternate) {
 844                        print "<tr class=\"dark\">\n";
 845                } else {
 846                        print "<tr class=\"light\">\n";
 847                }
 848                $alternate ^= 1;
 849                print "<td>" . $cgi->a({-href => "$my_uri?p=$pr->{'path'};a=summary", -class => "list"}, escapeHTML($pr->{'path'})) . "</td>\n" .
 850                      "<td>$pr->{'descr'}</td>\n" .
 851                      "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
 852                my $colored_age;
 853                if ($pr->{'commit'}{'age'} < 60*60*2) {
 854                        $colored_age = "<span style =\"color: #009900;\"><b><i>$pr->{'commit'}{'age_string'}</i></b></span>";
 855                } elsif ($pr->{'commit'}{'age'} < 60*60*24*2) {
 856                        $colored_age = "<span style =\"color: #009900;\"><i>$pr->{'commit'}{'age_string'}</i></span>";
 857                } else {
 858                        $colored_age = "<i>$pr->{'commit'}{'age_string'}</i>";
 859                }
 860                print "<td>$colored_age</td>\n" .
 861                      "<td class=\"link\">" .
 862                      $cgi->a({-href => "$my_uri?p=$pr->{'path'};a=summary"}, "summary") .
 863                      " | " . $cgi->a({-href => "$my_uri?p=$pr->{'path'};a=shortlog"}, "shortlog") .
 864                      " | " . $cgi->a({-href => "$my_uri?p=$pr->{'path'};a=log"}, "log") .
 865                      "</td>\n" .
 866                      "</tr>\n";
 867        }
 868        print "</table>\n";
 869        git_footer_html();
 870}
 871
 872sub git_read_refs {
 873        my $ref_dir = shift;
 874        my @reflist;
 875
 876        my @refs;
 877        opendir my $dh, "$projectroot/$project/$ref_dir";
 878        while (my $dir = readdir($dh)) {
 879                if ($dir =~ m/^\./) {
 880                        next;
 881                }
 882                if (-d "$projectroot/$project/$ref_dir/$dir") {
 883                        opendir my $dh2, "$projectroot/$project/$ref_dir/$dir";
 884                        my @subdirs = grep !m/^\./, readdir $dh2;
 885                        closedir($dh2);
 886                        foreach my $subdir (@subdirs) {
 887                                push @refs, "$dir/$subdir"
 888                        }
 889                        next;
 890                }
 891                push @refs, $dir;
 892        }
 893        closedir($dh);
 894        foreach my $ref_file (@refs) {
 895                my $ref_id = git_read_hash("$project/$ref_dir/$ref_file");
 896                my $type = git_get_type($ref_id) || next;
 897                my %ref_item;
 898                my %co;
 899                $ref_item{'type'} = $type;
 900                $ref_item{'id'} = $ref_id;
 901                $ref_item{'epoch'} = 0;
 902                $ref_item{'age'} = "unknown";
 903                if ($type eq "tag") {
 904                        my %tag = git_read_tag($ref_id);
 905                        $ref_item{'comment'} = $tag{'comment'};
 906                        if ($tag{'type'} eq "commit") {
 907                                %co = git_read_commit($tag{'object'});
 908                                $ref_item{'epoch'} = $co{'committer_epoch'};
 909                                $ref_item{'age'} = $co{'age_string'};
 910                        } elsif (defined($tag{'epoch'})) {
 911                                my $age = time - $tag{'epoch'};
 912                                $ref_item{'epoch'} = $tag{'epoch'};
 913                                $ref_item{'age'} = age_string($age);
 914                        }
 915                        $ref_item{'reftype'} = $tag{'type'};
 916                        $ref_item{'name'} = $tag{'name'};
 917                        $ref_item{'refid'} = $tag{'object'};
 918                } elsif ($type eq "commit"){
 919                        %co = git_read_commit($ref_id);
 920                        $ref_item{'reftype'} = "commit";
 921                        $ref_item{'name'} = $ref_file;
 922                        $ref_item{'title'} = $co{'title'};
 923                        $ref_item{'refid'} = $ref_id;
 924                        $ref_item{'epoch'} = $co{'committer_epoch'};
 925                        $ref_item{'age'} = $co{'age_string'};
 926                }
 927
 928                push @reflist, \%ref_item;
 929        }
 930        # sort tags by age
 931        @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
 932        return \@reflist;
 933}
 934
 935sub git_summary {
 936        my $descr = git_read_description($project) || "none";
 937        my $head = git_read_hash("$project/HEAD");
 938        my %co = git_read_commit($head);
 939        my %cd = date_str($co{'committer_epoch'}, $co{'committer_tz'});
 940
 941        my $owner;
 942        if (-f $projects_list) {
 943                open (my $fd , $projects_list);
 944                while (my $line = <$fd>) {
 945                        chomp $line;
 946                        my ($pr, $ow) = split ' ', $line;
 947                        $pr = unescape($pr);
 948                        $ow = unescape($ow);
 949                        if ($pr eq $project) {
 950                                $owner = $ow;
 951                                last;
 952                        }
 953                }
 954                close $fd;
 955        }
 956        if (!defined $owner) {
 957                $owner = get_file_owner("$projectroot/$project");
 958        }
 959
 960        git_header_html();
 961        print "<div class=\"page_nav\">\n" .
 962              "summary".
 963              " | " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog"}, "shortlog") .
 964              " | " . $cgi->a({-href => "$my_uri?p=$project;a=log"}, "log") .
 965              " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$head"}, "commit") .
 966              " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$head"}, "commitdiff") .
 967              " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree"}, "tree") .
 968              "<br/><br/>\n" .
 969              "</div>\n";
 970        print "<div class=\"title\">&nbsp;</div>\n";
 971        print "<table cellspacing=\"0\">\n" .
 972              "<tr><td>description</td><td>" . escapeHTML($descr) . "</td></tr>\n" .
 973              "<tr><td>owner</td><td>$owner</td></tr>\n" .
 974              "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n" .
 975              "</table>\n";
 976        open my $fd, "-|", "$gitbin/git-rev-list --max-count=17 " . git_read_hash("$project/HEAD") or die_error(undef, "Open failed.");
 977        my (@revlist) = map { chomp; $_ } <$fd>;
 978        close $fd;
 979        print "<div>\n" .
 980              $cgi->a({-href => "$my_uri?p=$project;a=shortlog", -class => "title"}, "shortlog") .
 981              "</div>\n";
 982        my $i = 16;
 983        print "<table cellspacing=\"0\">\n";
 984        my $alternate = 0;
 985        foreach my $commit (@revlist) {
 986                my %co = git_read_commit($commit);
 987                my %ad = date_str($co{'author_epoch'});
 988                if ($alternate) {
 989                        print "<tr class=\"dark\">\n";
 990                } else {
 991                        print "<tr class=\"light\">\n";
 992                }
 993                $alternate ^= 1;
 994                if ($i-- > 0) {
 995                        print "<td><i>$co{'age_string'}</i></td>\n" .
 996                              "<td><i>" . escapeHTML(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
 997                              "<td>";
 998                        if (length($co{'title_short'}) < length($co{'title'})) {
 999                                print $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit", -class => "list", -title => "$co{'title'}"},
1000                                      "<b>" . escapeHTML($co{'title_short'}) . "</b>");
1001                        } else {
1002                                print $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit", -class => "list"},
1003                                      "<b>" . escapeHTML($co{'title'}) . "</b>");
1004                        }
1005                        print "</td>\n" .
1006                              "<td class=\"link\">" .
1007                              $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit"}, "commit") .
1008                              " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$commit"}, "commitdiff") .
1009                              "</td>\n" .
1010                              "</tr>";
1011                } else {
1012                        print "<td>" . $cgi->a({-href => "$my_uri?p=$project;a=shortlog"}, "...") . "</td>\n" .
1013                        "</tr>";
1014                        last;
1015                }
1016        }
1017        print "</table\n>";
1018
1019        my $taglist = git_read_refs("refs/tags");
1020        if (defined @$taglist) {
1021                print "<div>\n" .
1022                      $cgi->a({-href => "$my_uri?p=$project;a=tags", -class => "title"}, "tags") .
1023                      "</div>\n";
1024                my $i = 16;
1025                print "<table cellspacing=\"0\">\n";
1026                my $alternate = 0;
1027                foreach my $entry (@$taglist) {
1028                        my %tag = %$entry;
1029                        my $comment_lines = $tag{'comment'};
1030                        my $comment = shift @$comment_lines;
1031                        if (defined($comment)) {
1032                                $comment = chop_str($comment, 30, 5);
1033                        }
1034                        if ($alternate) {
1035                                print "<tr class=\"dark\">\n";
1036                        } else {
1037                                print "<tr class=\"light\">\n";
1038                        }
1039                        $alternate ^= 1;
1040                        if ($i-- > 0) {
1041                                print "<td><i>$tag{'age'}</i></td>\n" .
1042                                      "<td>" .
1043                                      $cgi->a({-href => "$my_uri?p=$project;a=$tag{'reftype'};h=$tag{'refid'}", -class => "list"},
1044                                      "<b>" . escapeHTML($tag{'name'}) . "</b>") .
1045                                      "</td>\n" .
1046                                      "<td>";
1047                                if (defined($comment)) {
1048                                      print $cgi->a({-class => "list", -href => "$my_uri?p=$project;a=tag;h=$tag{'id'}"}, $comment);
1049                                }
1050                                print "</td>\n" .
1051                                      "<td class=\"link\">";
1052                                if ($tag{'type'} eq "tag") {
1053                                      print $cgi->a({-href => "$my_uri?p=$project;a=tag;h=$tag{'id'}"}, "tag") . " | ";
1054                                }
1055                                print $cgi->a({-href => "$my_uri?p=$project;a=$tag{'reftype'};h=$tag{'refid'}"}, $tag{'reftype'});
1056                                if ($tag{'reftype'} eq "commit") {
1057                                      print " | " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog;h=$tag{'name'}"}, "shortlog") .
1058                                            " | " . $cgi->a({-href => "$my_uri?p=$project;a=log;h=$tag{'refid'}"}, "log");
1059                                }
1060                                print "</td>\n" .
1061                                      "</tr>";
1062                        } else {
1063                                print "<td>" . $cgi->a({-href => "$my_uri?p=$project;a=tags"}, "...") . "</td>\n" .
1064                                "</tr>";
1065                                last;
1066                        }
1067                }
1068                print "</table\n>";
1069        }
1070
1071        my $headlist = git_read_refs("refs/heads");
1072        if (defined @$headlist) {
1073                print "<div>\n" .
1074                      $cgi->a({-href => "$my_uri?p=$project;a=heads", -class => "title"}, "heads") .
1075                      "</div>\n";
1076                my $i = 16;
1077                print "<table cellspacing=\"0\">\n";
1078                my $alternate = 0;
1079                foreach my $entry (@$headlist) {
1080                        my %tag = %$entry;
1081                        if ($alternate) {
1082                                print "<tr class=\"dark\">\n";
1083                        } else {
1084                                print "<tr class=\"light\">\n";
1085                        }
1086                        $alternate ^= 1;
1087                        if ($i-- > 0) {
1088                                print "<td><i>$tag{'age'}</i></td>\n" .
1089                                      "<td>" .
1090                                      $cgi->a({-href => "$my_uri?p=$project;a=shortlog;h=$tag{'name'}", -class => "list"},
1091                                      "<b>" . escapeHTML($tag{'name'}) . "</b>") .
1092                                      "</td>\n" .
1093                                      "<td class=\"link\">" .
1094                                      $cgi->a({-href => "$my_uri?p=$project;a=shortlog;h=$tag{'name'}"}, "shortlog") .
1095                                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=log;h=$tag{'name'}"}, "log") .
1096                                      "</td>\n" .
1097                                      "</tr>";
1098                        } else {
1099                                print "<td>" . $cgi->a({-href => "$my_uri?p=$project;a=heads"}, "...") . "</td>\n" .
1100                                "</tr>";
1101                                last;
1102                        }
1103                }
1104                print "</table\n>";
1105        }
1106        git_footer_html();
1107}
1108
1109sub git_tag {
1110        my $head = git_read_hash("$project/HEAD");
1111        git_header_html();
1112        print "<div class=\"page_nav\">\n" .
1113              $cgi->a({-href => "$my_uri?p=$project;a=summary"}, "summary") .
1114              " | " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog"}, "shortlog") .
1115              " | " . $cgi->a({-href => "$my_uri?p=$project;a=log"}, "log") .
1116              " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$head"}, "commit") .
1117              " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$head"}, "commitdiff") .
1118              " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;hb=$head"}, "tree") . "<br/>\n" .
1119              "<br/>\n" .
1120              "</div>\n";
1121        my %tag = git_read_tag($hash);
1122        print "<div>\n" .
1123              $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash", -class => "title"}, escapeHTML($tag{'name'})) . "\n" .
1124              "</div>\n";
1125        print "<div class=\"title_text\">\n" .
1126              "<table cellspacing=\"0\">\n" .
1127              "<tr>\n" .
1128              "<td>object</td>\n" .
1129              "<td>" . $cgi->a({-class => "list", -href => "$my_uri?p=$project;a=$tag{'type'};h=$tag{'object'}"}, $tag{'object'}) . "</td>\n" .
1130              "<td class=\"link\">" . $cgi->a({-href => "$my_uri?p=$project;a=$tag{'type'};h=$tag{'object'}"}, $tag{'type'}) . "</td>\n" .
1131              "</tr>\n";
1132        if (defined($tag{'author'})) {
1133                my %ad = date_str($tag{'epoch'}, $tag{'tz'});
1134                print "<tr><td>author</td><td>" . escapeHTML($tag{'author'}) . "</td></tr>\n";
1135                print "<tr><td></td><td>" . $ad{'rfc2822'} . sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) . "</td></tr>\n";
1136        }
1137        print "</table>\n\n" .
1138              "</div>\n";
1139        print "<div class=\"page_body\">";
1140        my $comment = $tag{'comment'};
1141        foreach my $line (@$comment) {
1142                print escapeHTML($line) . "<br/>\n";
1143        }
1144        print "</div>\n";
1145        git_footer_html();
1146}
1147
1148sub git_tags {
1149        my $head = git_read_hash("$project/HEAD");
1150        git_header_html();
1151        print "<div class=\"page_nav\">\n" .
1152              $cgi->a({-href => "$my_uri?p=$project;a=summary"}, "summary") .
1153              " | " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog"}, "shortlog") .
1154              " | " . $cgi->a({-href => "$my_uri?p=$project;a=log"}, "log") .
1155              " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$head"}, "commit") .
1156              " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$head"}, "commitdiff") .
1157              " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;hb=$head"}, "tree") . "<br/>\n" .
1158              "<br/>\n" .
1159              "</div>\n";
1160        my $taglist = git_read_refs("refs/tags");
1161        print "<div>\n" .
1162              $cgi->a({-href => "$my_uri?p=$project;a=summary", -class => "title"}, "&nbsp;") .
1163              "</div>\n";
1164        print "<table cellspacing=\"0\">\n";
1165        my $alternate = 0;
1166        if (defined @$taglist) {
1167                foreach my $entry (@$taglist) {
1168                        my %tag = %$entry;
1169                        my $comment_lines = $tag{'comment'};
1170                        my $comment = shift @$comment_lines;
1171                        if (defined($comment)) {
1172                                $comment = chop_str($comment, 30, 5);
1173                        }
1174                        if ($alternate) {
1175                                print "<tr class=\"dark\">\n";
1176                        } else {
1177                                print "<tr class=\"light\">\n";
1178                        }
1179                        $alternate ^= 1;
1180                        print "<td><i>$tag{'age'}</i></td>\n" .
1181                              "<td>" .
1182                              $cgi->a({-href => "$my_uri?p=$project;a=$tag{'reftype'};h=$tag{'refid'}", -class => "list"},
1183                              "<b>" . escapeHTML($tag{'name'}) . "</b>") .
1184                              "</td>\n" .
1185                              "<td>";
1186                        if (defined($comment)) {
1187                              print $cgi->a({-class => "list", -href => "$my_uri?p=$project;a=tag;h=$tag{'id'}"}, $comment);
1188                        }
1189                        print "</td>\n" .
1190                              "<td class=\"link\">";
1191                        if ($tag{'type'} eq "tag") {
1192                              print $cgi->a({-href => "$my_uri?p=$project;a=tag;h=$tag{'id'}"}, "tag") . " | ";
1193                        }
1194                        print $cgi->a({-href => "$my_uri?p=$project;a=$tag{'reftype'};h=$tag{'refid'}"}, $tag{'reftype'});
1195                        if ($tag{'reftype'} eq "commit") {
1196                              print " | " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog;h=$tag{'name'}"}, "shortlog") .
1197                                    " | " . $cgi->a({-href => "$my_uri?p=$project;a=log;h=$tag{'refid'}"}, "log");
1198                        }
1199                        print "</td>\n" .
1200                              "</tr>";
1201                }
1202        }
1203        print "</table\n>";
1204        git_footer_html();
1205}
1206
1207sub git_heads {
1208        my $head = git_read_hash("$project/HEAD");
1209        git_header_html();
1210        print "<div class=\"page_nav\">\n" .
1211              $cgi->a({-href => "$my_uri?p=$project;a=summary"}, "summary") .
1212              " | " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog"}, "shortlog") .
1213              " | " . $cgi->a({-href => "$my_uri?p=$project;a=log"}, "log") .
1214              " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$head"}, "commit") .
1215              " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$head"}, "commitdiff") .
1216              " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;hb=$head"}, "tree") . "<br/>\n" .
1217              "<br/>\n" .
1218              "</div>\n";
1219        my $taglist = git_read_refs("refs/heads");
1220        print "<div>\n" .
1221              $cgi->a({-href => "$my_uri?p=$project;a=summary", -class => "title"}, "&nbsp;") .
1222              "</div>\n";
1223        print "<table cellspacing=\"0\">\n";
1224        my $alternate = 0;
1225        if (defined @$taglist) {
1226                foreach my $entry (@$taglist) {
1227                        my %tag = %$entry;
1228                        if ($alternate) {
1229                                print "<tr class=\"dark\">\n";
1230                        } else {
1231                                print "<tr class=\"light\">\n";
1232                        }
1233                        $alternate ^= 1;
1234                        print "<td><i>$tag{'age'}</i></td>\n" .
1235                              "<td>" .
1236                              $cgi->a({-href => "$my_uri?p=$project;a=shortlog;h=$tag{'name'}", -class => "list"}, "<b>" . escapeHTML($tag{'name'}) . "</b>") .
1237                              "</td>\n" .
1238                              "<td class=\"link\">" .
1239                              $cgi->a({-href => "$my_uri?p=$project;a=shortlog;h=$tag{'name'}"}, "shortlog") .
1240                              " | " . $cgi->a({-href => "$my_uri?p=$project;a=log;h=$tag{'name'}"}, "log") .
1241                              "</td>\n" .
1242                              "</tr>";
1243                }
1244        }
1245        print "</table\n>";
1246        git_footer_html();
1247}
1248
1249sub git_get_hash_by_path {
1250        my $base = shift;
1251        my $path = shift || return undef;
1252
1253        my $tree = $base;
1254        my @parts = split '/', $path;
1255        while (my $part = shift @parts) {
1256                open my $fd, "-|", "$gitbin/git-ls-tree $tree" or die_error(undef, "Open git-ls-tree failed.");
1257                my (@entries) = map { chomp; $_ } <$fd>;
1258                close $fd or return undef;
1259                foreach my $line (@entries) {
1260                        #'100644        blob    0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa        panic.c'
1261                        $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
1262                        my $t_mode = $1;
1263                        my $t_type = $2;
1264                        my $t_hash = $3;
1265                        my $t_name = $4;
1266                        if ($t_name eq $part) {
1267                                if (!(@parts)) {
1268                                        return $t_hash;
1269                                }
1270                                if ($t_type eq "tree") {
1271                                        $tree = $t_hash;
1272                                }
1273                                last;
1274                        }
1275                }
1276        }
1277}
1278
1279sub git_blob {
1280        if (!defined $hash && defined $file_name) {
1281                my $base = $hash_base || git_read_hash("$project/HEAD");
1282                $hash = git_get_hash_by_path($base, $file_name, "blob");
1283        }
1284        open my $fd, "-|", "$gitbin/git-cat-file blob $hash" or die_error(undef, "Open failed.");
1285        git_header_html();
1286        if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
1287                print "<div class=\"page_nav\">\n" .
1288                      $cgi->a({-href => "$my_uri?p=$project;a=summary"}, "summary") .
1289                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog"}, "shortlog") .
1290                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=log"}, "log") .
1291                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash_base"}, "commit") .
1292                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash_base"}, "commitdiff") .
1293                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash_base"}, "tree") . "<br/>\n";
1294                if (defined $file_name) {
1295                        print $cgi->a({-href => "$my_uri?p=$project;a=blob_plain;h=$hash;f=$file_name"}, "plain") . "<br/>\n";
1296                } else {
1297                        print $cgi->a({-href => "$my_uri?p=$project;a=blob_plain;h=$hash"}, "plain") . "<br/>\n";
1298                }
1299                print "</div>\n".
1300                       "<div>" .
1301                      $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash_base", -class => "title"}, escapeHTML($co{'title'})) .
1302                      "</div>\n";
1303        } else {
1304                print "<div class=\"page_nav\">\n" .
1305                      "<br/><br/></div>\n" .
1306                      "<div class=\"title\">$hash</div>\n";
1307        }
1308        if (defined $file_name) {
1309                print "<div class=\"page_path\"><b>$file_name</b></div>\n";
1310        }
1311        print "<div class=\"page_body\">\n";
1312        my $nr;
1313        while (my $line = <$fd>) {
1314                chomp $line;
1315                $nr++;
1316                while ((my $pos = index($line, "\t")) != -1) {
1317                        if (my $count = (8 - ($pos % 8))) {
1318                                my $spaces = ' ' x $count;
1319                                $line =~ s/\t/$spaces/;
1320                        }
1321                }
1322                printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n", $nr, $nr, $nr, escapeHTML($line);
1323        }
1324        close $fd or print "Reading blob failed.\n";
1325        print "</div>";
1326        git_footer_html();
1327}
1328
1329sub git_blob_plain {
1330        my $save_as = "$hash.txt";
1331        if (defined $file_name) {
1332                $save_as = $file_name;
1333        }
1334        print $cgi->header(-type => "text/plain", -charset => 'utf-8', '-content-disposition' => "inline; filename=\"$save_as\"");
1335        open my $fd, "-|", "$gitbin/git-cat-file blob $hash" or return;
1336        undef $/;
1337        print <$fd>;
1338        $/ = "\n";
1339        close $fd;
1340}
1341
1342sub git_tree {
1343        if (!defined $hash) {
1344                $hash = git_read_hash("$project/HEAD");
1345                if (defined $file_name) {
1346                        my $base = $hash_base || git_read_hash("$project/HEAD");
1347                        $hash = git_get_hash_by_path($base, $file_name, "tree");
1348                }
1349                if (!defined $hash_base) {
1350                        $hash_base = git_read_hash("$project/HEAD");
1351                }
1352        }
1353        open my $fd, "-|", "$gitbin/git-ls-tree $hash" or die_error(undef, "Open git-ls-tree failed.");
1354        my (@entries) = map { chomp; $_ } <$fd>;
1355        close $fd or die_error(undef, "Reading tree failed.");
1356
1357        git_header_html();
1358        my $base_key = "";
1359        my $file_key = "";
1360        my $base = "";
1361        if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
1362                $base_key = ";hb=$hash_base";
1363                print "<div class=\"page_nav\">\n" .
1364                      $cgi->a({-href => "$my_uri?p=$project;a=summary"}, "summary") .
1365                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog;h=$hash_base"}, "shortlog") .
1366                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=log;h=$hash_base"}, "log") .
1367                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash_base"}, "commit") .
1368                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash_base"}, "commitdiff") .
1369                      " | tree" .
1370                      "<br/><br/>\n" .
1371                      "</div>\n";
1372                print "<div>\n" .
1373                      $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash_base", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
1374                      "</div>\n";
1375        } else {
1376                print "<div class=\"page_nav\">\n";
1377                print "<br/><br/></div>\n";
1378                print "<div class=\"title\">$hash</div>\n";
1379        }
1380        if (defined $file_name) {
1381                $base = "$file_name/";
1382                print "<div class=\"page_path\"><b>/$file_name</b></div>\n";
1383        } else {
1384                print "<div class=\"page_path\"><b>/</b></div>\n";
1385        }
1386        print "<div class=\"page_body\">\n";
1387        print "<table cellspacing=\"0\">\n";
1388        my $alternate = 0;
1389        foreach my $line (@entries) {
1390                #'100644        blob    0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa        panic.c'
1391                $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
1392                my $t_mode = $1;
1393                my $t_type = $2;
1394                my $t_hash = $3;
1395                my $t_name = $4;
1396                $file_key = ";f=$base$t_name";
1397                if ($alternate) {
1398                        print "<tr class=\"dark\">\n";
1399                } else {
1400                        print "<tr class=\"light\">\n";
1401                }
1402                $alternate ^= 1;
1403                print "<td style=\"font-family:monospace\">" . mode_str($t_mode) . "</td>\n";
1404                if ($t_type eq "blob") {
1405                        print "<td class=\"list\">" .
1406                              $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$t_hash" . $base_key . $file_key, -class => "list"}, $t_name) .
1407                              "</td>\n" .
1408                              "<td class=\"link\">" .
1409                              $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$t_hash" . $base_key . $file_key}, "blob") .
1410                              " | " . $cgi->a({-href => "$my_uri?p=$project;a=history;h=$hash_base" . $file_key}, "history") .
1411                              "</td>\n";
1412                } elsif ($t_type eq "tree") {
1413                        print "<td class=\"list\">" .
1414                              $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$t_hash" . $base_key . $file_key}, $t_name) .
1415                              "</td>\n" .
1416                              "<td class=\"link\">" .
1417                              $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$t_hash" . $base_key . $file_key}, "tree") .
1418                              "</td>\n";
1419                }
1420                print "</tr>\n";
1421        }
1422        print "</table>\n" .
1423              "</div>";
1424        git_footer_html();
1425}
1426
1427sub git_rss {
1428        # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
1429        open my $fd, "-|", "$gitbin/git-rev-list --max-count=150 " . git_read_hash("$project/HEAD") or die_error(undef, "Open failed.");
1430        my (@revlist) = map { chomp; $_ } <$fd>;
1431        close $fd or die_error(undef, "Reading rev-list failed.");
1432        print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
1433        print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".
1434              "<rss version=\"2.0\" xmlns:content=\"http://purl.org/rss/1.0/modules/content/\">\n";
1435        print "<channel>\n";
1436        print "<title>$project</title>\n".
1437              "<link>" . escapeHTML("$my_url?p=$project;a=summary") . "</link>\n".
1438              "<description>$project log</description>\n".
1439              "<language>en</language>\n";
1440
1441        for (my $i = 0; $i <= $#revlist; $i++) {
1442                my $commit = $revlist[$i];
1443                my %co = git_read_commit($commit);
1444                # we read 150, we always show 30 and the ones more recent than 48 hours
1445                if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
1446                        last;
1447                }
1448                my %cd = date_str($co{'committer_epoch'});
1449                open $fd, "-|", "$gitbin/git-diff-tree -r $co{'parent'} $co{'id'}" or next;
1450                my @difftree = map { chomp; $_ } <$fd>;
1451                close $fd or next;
1452                print "<item>\n" .
1453                      "<title>" .
1454                      sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . escapeHTML($co{'title'}) .
1455                      "</title>\n" .
1456                      "<author>" . escapeHTML($co{'author'}) . "</author>\n" .
1457                      "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
1458                      "<guid isPermaLink=\"true\">" . escapeHTML("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
1459                      "<link>" . escapeHTML("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
1460                      "<description>" . escapeHTML($co{'title'}) . "</description>\n" .
1461                      "<content:encoded>" .
1462                      "<![CDATA[\n";
1463                my $comment = $co{'comment'};
1464                foreach my $line (@$comment) {
1465                        print "$line<br/>\n";
1466                }
1467                print "<br/>\n";
1468                foreach my $line (@difftree) {
1469                        if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
1470                                next;
1471                        }
1472                        my $file = $7;
1473                        print "$file<br/>\n";
1474                }
1475                print "]]>\n" .
1476                      "</content:encoded>\n" .
1477                      "</item>\n";
1478        }
1479        print "</channel></rss>";
1480}
1481
1482sub git_opml {
1483        my @list = git_read_projects();
1484
1485        print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
1486        print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".
1487              "<opml version=\"1.0\">\n".
1488              "<head>".
1489              "  <title>Git OPML Export</title>\n".
1490              "</head>\n".
1491              "<body>\n".
1492              "<outline text=\"git RSS feeds\">\n";
1493
1494        foreach my $pr (@list) {
1495                my %proj = %$pr;
1496                my $head = git_read_hash("$proj{'path'}/HEAD");
1497                if (!defined $head) {
1498                        next;
1499                }
1500                $ENV{'GIT_DIR'} = "$projectroot/$proj{'path'}";
1501                my %co = git_read_commit($head);
1502                if (!%co) {
1503                        next;
1504                }
1505
1506                my $path = escapeHTML(chop_str($proj{'path'}, 25, 5));
1507                my $rss =  "$my_url?p=$proj{'path'};a=rss";
1508                my $html =  "$my_url?p=$proj{'path'};a=summary";
1509                print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
1510        }
1511        print "</outline>\n".
1512              "</body>\n".
1513              "</opml>\n";
1514}
1515
1516sub git_log {
1517        my $head = git_read_hash("$project/HEAD");
1518        if (!defined $hash) {
1519                $hash = $head;
1520        }
1521        if (!defined $page) {
1522                $page = 0;
1523        }
1524        git_header_html();
1525        print "<div class=\"page_nav\">\n";
1526        print $cgi->a({-href => "$my_uri?p=$project;a=summary"}, "summary") .
1527              " | " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog;h=$hash"}, "shortlog") .
1528              " | log" .
1529              " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash"}, "commit") .
1530              " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash"}, "commitdiff") .
1531              " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$hash;hb=$hash"}, "tree") . "<br/>\n";
1532
1533        my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
1534        open my $fd, "-|", "$gitbin/git-rev-list $limit $hash" or die_error(undef, "Open failed.");
1535        my (@revlist) = map { chomp; $_ } <$fd>;
1536        close $fd;
1537
1538        if ($hash ne $head || $page) {
1539                print $cgi->a({-href => "$my_uri?p=$project;a=log"}, "HEAD");
1540        } else {
1541                print "HEAD";
1542        }
1543        if ($page > 0) {
1544                print " &sdot; " .
1545                $cgi->a({-href => "$my_uri?p=$project;a=log;h=$hash;pg=" . ($page-1), -accesskey => "p", -title => "Alt-p"}, "prev");
1546        } else {
1547                print " &sdot; prev";
1548        }
1549        if ($#revlist >= (100 * ($page+1)-1)) {
1550                print " &sdot; " .
1551                $cgi->a({-href => "$my_uri?p=$project;a=log;h=$hash;pg=" . ($page+1), -accesskey => "n", -title => "Alt-n"}, "next");
1552        } else {
1553                print " &sdot; next";
1554        }
1555        print "<br/>\n" .
1556              "</div>\n";
1557        if (!@revlist) {
1558                print "<div>\n" .
1559                      $cgi->a({-href => "$my_uri?p=$project;a=summary", -class => "title"}, "&nbsp;") .
1560                      "</div>\n";
1561                my %co = git_read_commit($hash);
1562                print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
1563        }
1564        for (my $i = ($page * 100); $i <= $#revlist; $i++) {
1565                my $commit = $revlist[$i];
1566                my %co = git_read_commit($commit);
1567                next if !%co;
1568                my %ad = date_str($co{'author_epoch'});
1569                print "<div>\n" .
1570                      $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit", -class => "title"},
1571                      "<span class=\"age\">$co{'age_string'}</span>" . escapeHTML($co{'title'})) . "\n" .
1572                      "</div>\n";
1573                print "<div class=\"title_text\">\n" .
1574                      "<div class=\"log_link\">\n" .
1575                      $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit"}, "commit") .
1576                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$commit"}, "commitdiff") .
1577                      "<br/>\n" .
1578                      "</div>\n" .
1579                      "<i>" . escapeHTML($co{'author_name'}) .  " [$ad{'rfc2822'}]</i><br/>\n" .
1580                      "</div>\n" .
1581                      "<div class=\"log_body\">\n";
1582                my $comment = $co{'comment'};
1583                my $empty = 0;
1584                foreach my $line (@$comment) {
1585                        if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1586                                next;
1587                        }
1588                        if ($line eq "") {
1589                                if ($empty) {
1590                                        next;
1591                                }
1592                                $empty = 1;
1593                        } else {
1594                                $empty = 0;
1595                        }
1596                        print format_log_line_html($line) . "<br/>\n";
1597                }
1598                if (!$empty) {
1599                        print "<br/>\n";
1600                }
1601                print "</div>\n";
1602        }
1603        git_footer_html();
1604}
1605
1606sub git_commit {
1607        my %co = git_read_commit($hash);
1608        if (!%co) {
1609                die_error(undef, "Unknown commit object.");
1610        }
1611        my %ad = date_str($co{'author_epoch'}, $co{'author_tz'});
1612        my %cd = date_str($co{'committer_epoch'}, $co{'committer_tz'});
1613
1614        my @difftree;
1615        my $root = "";
1616        my $parent = $co{'parent'};
1617        if (!defined $parent) {
1618                $root = " --root";
1619                $parent = "";
1620        }
1621        open my $fd, "-|", "$gitbin/git-diff-tree -r -M $root $parent $hash" or die_error(undef, "Open failed.");
1622        @difftree = map { chomp; $_ } <$fd>;
1623        close $fd or die_error(undef, "Reading diff-tree failed.");
1624
1625        # non-textual hash id's can be cached
1626        my $expires;
1627        if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
1628                $expires = "+1d";
1629        }
1630        git_header_html(undef, $expires);
1631        print "<div class=\"page_nav\">\n" .
1632              $cgi->a({-href => "$my_uri?p=$project;a=summary"}, "summary") .
1633              " | " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog;h=$hash"}, "shortlog") .
1634              " | " . $cgi->a({-href => "$my_uri?p=$project;a=log;h=$hash"}, "log") .
1635              " | commit";
1636        if (defined $co{'parent'}) {
1637                print " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash"}, "commitdiff");
1638        }
1639        print " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash"}, "tree") . "\n" .
1640              "<br/><br/></div>\n";
1641        if (defined $co{'parent'}) {
1642                print "<div>\n" .
1643                      $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
1644                      "</div>\n";
1645        } else {
1646                print "<div>\n" .
1647                      $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
1648                      "</div>\n";
1649        }
1650        print "<div class=\"title_text\">\n" .
1651              "<table cellspacing=\"0\">\n";
1652        print "<tr><td>author</td><td>" . escapeHTML($co{'author'}) . "</td></tr>\n".
1653              "<tr>" .
1654              "<td></td><td> $ad{'rfc2822'}";
1655        if ($ad{'hour_local'} < 6) {
1656                printf(" (<span style=\"color: #cc0000;\">%02d:%02d</span> %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1657        } else {
1658                printf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1659        }
1660        print "</td>" .
1661              "</tr>\n";
1662        print "<tr><td>committer</td><td>" . escapeHTML($co{'committer'}) . "</td></tr>\n";
1663        print "<tr><td></td><td> $cd{'rfc2822'}" . sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) . "</td></tr>\n";
1664        print "<tr><td>commit</td><td style=\"font-family:monospace\">$co{'id'}</td></tr>\n";
1665        print "<tr>" .
1666              "<td>tree</td>" .
1667              "<td style=\"font-family:monospace\">" .
1668              $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash", class => "list"}, $co{'tree'}) .
1669              "</td>" .
1670              "<td class=\"link\">" . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash"}, "tree") .
1671              "</td>" .
1672              "</tr>\n";
1673        my $parents  = $co{'parents'};
1674        foreach my $par (@$parents) {
1675                print "<tr>" .
1676                      "<td>parent</td>" .
1677                      "<td style=\"font-family:monospace\">" . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$par", class => "list"}, $par) . "</td>" .
1678                      "<td class=\"link\">" .
1679                      $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$par"}, "commit") .
1680                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash;hp=$par"}, "commitdiff") .
1681                      "</td>" .
1682                      "</tr>\n";
1683        }
1684        print "</table>". 
1685              "</div>\n";
1686        print "<div class=\"page_body\">\n";
1687        my $comment = $co{'comment'};
1688        my $empty = 0;
1689        my $signed = 0;
1690        foreach my $line (@$comment) {
1691                # print only one empty line
1692                if ($line eq "") {
1693                        if ($empty || $signed) {
1694                                next;
1695                        }
1696                        $empty = 1;
1697                } else {
1698                        $empty = 0;
1699                }
1700                if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1701                        $signed = 1;
1702                        print "<span style=\"color: #888888\">" . escapeHTML($line) . "</span><br/>\n";
1703                } else {
1704                        $signed = 0;
1705                        print format_log_line_html($line) . "<br/>\n";
1706                }
1707        }
1708        print "</div>\n";
1709        print "<div class=\"list_head\">\n";
1710        if ($#difftree > 10) {
1711                print(($#difftree + 1) . " files changed:\n");
1712        }
1713        print "</div>\n";
1714        print "<table cellspacing=\"0\">\n";
1715        my $alternate = 0;
1716        foreach my $line (@difftree) {
1717                # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M      ls-files.c'
1718                # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M      rev-tree.c'
1719                if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
1720                        next;
1721                }
1722                my $from_mode = $1;
1723                my $to_mode = $2;
1724                my $from_id = $3;
1725                my $to_id = $4;
1726                my $status = $5;
1727                my $similarity = $6;
1728                my $file = $7;
1729                if ($alternate) {
1730                        print "<tr class=\"dark\">\n";
1731                } else {
1732                        print "<tr class=\"light\">\n";
1733                }
1734                $alternate ^= 1;
1735                if ($status eq "A") {
1736                        my $mode_chng = "";
1737                        if (S_ISREG(oct $to_mode)) {
1738                                $mode_chng = sprintf(" with mode: %04o", (oct $to_mode) & 0777);
1739                        }
1740                        print "<td>" .
1741                              $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$to_id;hb=$hash;f=$file", -class => "list"}, escapeHTML($file)) . "</td>\n" .
1742                              "<td><span style=\"color: #008000;\">[new " . file_type($to_mode) . "$mode_chng]</span></td>\n" .
1743                              "<td class=\"link\">" . $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$to_id;hb=$hash;f=$file"}, "blob") . "</td>\n";
1744                } elsif ($status eq "D") {
1745                        print "<td>" .
1746                              $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$from_id;hb=$hash;f=$file", -class => "list"}, escapeHTML($file)) . "</td>\n" .
1747                              "<td><span style=\"color: #c00000;\">[deleted " . file_type($from_mode). "]</span></td>\n" .
1748                              "<td class=\"link\">" .
1749                              $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$from_id;hb=$hash;f=$file"}, "blob") .
1750                              " | " . $cgi->a({-href => "$my_uri?p=$project;a=history;h=$hash;f=$file"}, "history") .
1751                              "</td>\n"
1752                } elsif ($status eq "M" || $status eq "T") {
1753                        my $mode_chnge = "";
1754                        if ($from_mode != $to_mode) {
1755                                $mode_chnge = " <span style=\"color: #777777;\">[changed";
1756                                if (((oct $from_mode) & S_IFMT) != ((oct $to_mode) & S_IFMT)) {
1757                                        $mode_chnge .= " from " . file_type($from_mode) . " to " . file_type($to_mode);
1758                                }
1759                                if (((oct $from_mode) & 0777) != ((oct $to_mode) & 0777)) {
1760                                        if (S_ISREG($from_mode) && S_ISREG($to_mode)) {
1761                                                $mode_chnge .= sprintf(" mode: %04o->%04o", (oct $from_mode) & 0777, (oct $to_mode) & 0777);
1762                                        } elsif (S_ISREG($to_mode)) {
1763                                                $mode_chnge .= sprintf(" mode: %04o", (oct $to_mode) & 0777);
1764                                        }
1765                                }
1766                                $mode_chnge .= "]</span>\n";
1767                        }
1768                        print "<td>";
1769                        if ($to_id ne $from_id) {
1770                                print $cgi->a({-href => "$my_uri?p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$file", -class => "list"}, escapeHTML($file));
1771                        } else {
1772                                print $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$to_id;hb=$hash;f=$file", -class => "list"}, escapeHTML($file));
1773                        }
1774                        print "</td>\n" .
1775                              "<td>$mode_chnge</td>\n" .
1776                              "<td class=\"link\">";
1777                        print $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$to_id;hb=$hash;f=$file"}, "blob");
1778                        if ($to_id ne $from_id) {
1779                                print " | " . $cgi->a({-href => "$my_uri?p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$file"}, "diff");
1780                        }
1781                        print " | " . $cgi->a({-href => "$my_uri?p=$project;a=history;h=$hash;f=$file"}, "history") . "\n";
1782                        print "</td>\n";
1783                } elsif ($status eq "R") {
1784                        my ($from_file, $to_file) = split "\t", $file;
1785                        my $mode_chng = "";
1786                        if ($from_mode != $to_mode) {
1787                                $mode_chng = sprintf(", mode: %04o", (oct $to_mode) & 0777);
1788                        }
1789                        print "<td>" .
1790                              $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$to_id;hb=$hash;f=$to_file", -class => "list"}, escapeHTML($to_file)) . "</td>\n" .
1791                              "<td><span style=\"color: #777777;\">[moved from " .
1792                              $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$from_id;hb=$hash;f=$from_file", -class => "list"}, escapeHTML($from_file)) .
1793                              " with " . (int $similarity) . "% similarity$mode_chng]</span></td>\n" .
1794                              "<td class=\"link\">" .
1795                              $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$to_id;hb=$hash;f=$to_file"}, "blob");
1796                        if ($to_id ne $from_id) {
1797                                print " | " . $cgi->a({-href => "$my_uri?p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$to_file"}, "diff");
1798                        }
1799                        print "</td>\n";
1800                }
1801                print "</tr>\n";
1802        }
1803        print "</table>\n";
1804        git_footer_html();
1805}
1806
1807sub git_blobdiff {
1808        mkdir($git_temp, 0700);
1809        git_header_html();
1810        if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
1811                print "<div class=\"page_nav\">\n" .
1812                      $cgi->a({-href => "$my_uri?p=$project;a=summary"}, "summary") .
1813                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog"}, "shortlog") .
1814                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=log"}, "log") .
1815                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash_base"}, "commit") .
1816                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash_base"}, "commitdiff") .
1817                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash_base"}, "tree") .
1818                      "<br/>\n";
1819                print $cgi->a({-href => "$my_uri?p=$project;a=blobdiff_plain;h=$hash;hp=$hash_parent"}, "plain") .
1820                      "</div>\n";
1821                print "<div>\n" .
1822                      $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash_base", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
1823                      "</div>\n";
1824        } else {
1825                print "<div class=\"page_nav\">\n" .
1826                      "<br/><br/></div>\n" .
1827                      "<div class=\"title\">$hash vs $hash_parent</div>\n";
1828        }
1829        if (defined $file_name) {
1830                print "<div class=\"page_path\"><b>/$file_name</b></div>\n";
1831        }
1832        print "<div class=\"page_body\">\n" .
1833              "<div class=\"diff_info\">blob:" .
1834              $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$hash_parent;hb=$hash_base;f=$file_name"}, $hash_parent) .
1835              " -> blob:" .
1836              $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$hash;hb=$hash_base;f=$file_name"}, $hash) .
1837              "</div>\n";
1838        git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash);
1839        print "</div>";
1840        git_footer_html();
1841}
1842
1843sub git_blobdiff_plain {
1844        mkdir($git_temp, 0700);
1845        print $cgi->header(-type => "text/plain", -charset => 'utf-8');
1846        git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash, "plain");
1847}
1848
1849sub git_commitdiff {
1850        mkdir($git_temp, 0700);
1851        my %co = git_read_commit($hash);
1852        if (!%co) {
1853                die_error(undef, "Unknown commit object.");
1854        }
1855        if (!defined $hash_parent) {
1856                $hash_parent = $co{'parent'};
1857        }
1858        open my $fd, "-|", "$gitbin/git-diff-tree -r $hash_parent $hash" or die_error(undef, "Open failed.");
1859        my (@difftree) = map { chomp; $_ } <$fd>;
1860        close $fd or die_error(undef, "Reading diff-tree failed.");
1861
1862        # non-textual hash id's can be cached
1863        my $expires;
1864        if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
1865                $expires = "+1d";
1866        }
1867        git_header_html(undef, $expires);
1868        print "<div class=\"page_nav\">\n" .
1869              $cgi->a({-href => "$my_uri?p=$project;a=summary"}, "summary") .
1870              " | " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog;h=$hash"}, "shortlog") .
1871              " | " . $cgi->a({-href => "$my_uri?p=$project;a=log;h=$hash"}, "log") .
1872              " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash"}, "commit") .
1873              " | commitdiff" .
1874              " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash"}, "tree") . "<br/>\n";
1875        print $cgi->a({-href => "$my_uri?p=$project;a=commitdiff_plain;h=$hash;hp=$hash_parent"}, "plain") . "\n" .
1876              "</div>\n";
1877        print "<div>\n" .
1878              $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
1879              "</div>\n";
1880        print "<div class=\"page_body\">\n";
1881        my $comment = $co{'comment'};
1882        my $empty = 0;
1883        my $signed = 0;
1884        my @log = @$comment;
1885        # remove first and empty lines after that
1886        shift @log;
1887        while (defined $log[0] && $log[0] eq "") {
1888                shift @log;
1889        }
1890        foreach my $line (@log) {
1891                if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1892                        next;
1893                }
1894                if ($line eq "") {
1895                        if ($empty) {
1896                                next;
1897                        }
1898                        $empty = 1;
1899                } else {
1900                        $empty = 0;
1901                }
1902                print format_log_line_html($line) . "<br/>\n";
1903        }
1904        print "<br/>\n";
1905        foreach my $line (@difftree) {
1906                # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M      ls-files.c'
1907                # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M      rev-tree.c'
1908                $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/;
1909                my $from_mode = $1;
1910                my $to_mode = $2;
1911                my $from_id = $3;
1912                my $to_id = $4;
1913                my $status = $5;
1914                my $file = $6;
1915                if ($status eq "A") {
1916                        print "<div class=\"diff_info\">" .  file_type($to_mode) . ":" .
1917                              $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$to_id;hb=$hash;f=$file"}, $to_id) . "(new)" .
1918                              "</div>\n";
1919                        git_diff_print(undef, "/dev/null", $to_id, "b/$file");
1920                } elsif ($status eq "D") {
1921                        print "<div class=\"diff_info\">" . file_type($from_mode) . ":" .
1922                              $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$from_id;hb=$hash;f=$file"}, $from_id) . "(deleted)" .
1923                              "</div>\n";
1924                        git_diff_print($from_id, "a/$file", undef, "/dev/null");
1925                } elsif ($status eq "M") {
1926                        if ($from_id ne $to_id) {
1927                                print "<div class=\"diff_info\">" .
1928                                      file_type($from_mode) . ":" . $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$from_id;hb=$hash;f=$file"}, $from_id) .
1929                                      " -> " .
1930                                      file_type($to_mode) . ":" . $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$to_id;hb=$hash;f=$file"}, $to_id);
1931                                print "</div>\n";
1932                                git_diff_print($from_id, "a/$file",  $to_id, "b/$file");
1933                        }
1934                }
1935        }
1936        print "<br/>\n" .
1937              "</div>";
1938        git_footer_html();
1939}
1940
1941sub git_commitdiff_plain {
1942        mkdir($git_temp, 0700);
1943        open my $fd, "-|", "$gitbin/git-diff-tree -r $hash_parent $hash" or die_error(undef, "Open failed.");
1944        my (@difftree) = map { chomp; $_ } <$fd>;
1945        close $fd or die_error(undef, "Reading diff-tree failed.");
1946
1947        # try to figure out the next tag after this commit
1948        my $tagname;
1949        my %taghash;
1950        my $tags = git_read_refs("refs/tags");
1951        foreach my $entry (@$tags) {
1952                my %tag = %$entry;
1953                $taghash{$tag{'refid'}} = $tag{'name'};
1954        }
1955        open $fd, "-|", "$gitbin/git-rev-list HEAD";
1956        while (my $commit = <$fd>) {
1957                chomp $commit;
1958                if ($taghash{$commit}) {
1959                        $tagname = $taghash{$commit};
1960                }
1961                if ($commit eq $hash) {
1962                        last;
1963                }
1964        }
1965        close $fd;
1966
1967        print $cgi->header(-type => "text/plain", -charset => 'utf-8', '-content-disposition' => "inline; filename=\"git-$hash.patch\"");
1968        my %co = git_read_commit($hash);
1969        my %ad = date_str($co{'author_epoch'}, $co{'author_tz'});
1970        my $comment = $co{'comment'};
1971        print "From: $co{'author'}\n" .
1972              "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n".
1973              "Subject: $co{'title'}\n";
1974        if (defined $tagname) {
1975              print "X-Git-Tag: $tagname\n";
1976        }
1977        print "X-Git-Url: $my_url?p=$project;a=commitdiff;h=$hash\n" .
1978              "\n";
1979
1980        foreach my $line (@$comment) {;
1981                print "  $line\n";
1982        }
1983        print "---\n\n";
1984
1985        foreach my $line (@difftree) {
1986                $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/;
1987                my $from_id = $3;
1988                my $to_id = $4;
1989                my $status = $5;
1990                my $file = $6;
1991                if ($status eq "A") {
1992                        git_diff_print(undef, "/dev/null", $to_id, "b/$file", "plain");
1993                } elsif ($status eq "D") {
1994                        git_diff_print($from_id, "a/$file", undef, "/dev/null", "plain");
1995                } elsif ($status eq "M") {
1996                        git_diff_print($from_id, "a/$file",  $to_id, "b/$file", "plain");
1997                }
1998        }
1999}
2000
2001sub git_history {
2002        if (!defined $hash) {
2003                $hash = git_read_hash("$project/HEAD");
2004        }
2005        my %co = git_read_commit($hash);
2006        if (!%co) {
2007                die_error(undef, "Unknown commit object.");
2008        }
2009        git_header_html();
2010        print "<div class=\"page_nav\">\n" .
2011              $cgi->a({-href => "$my_uri?p=$project;a=summary"}, "summary") .
2012              " | " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog"}, "shortlog") .
2013              " | " . $cgi->a({-href => "$my_uri?p=$project;a=log"}, "log") .
2014              " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash"}, "commit") .
2015              " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash"}, "commitdiff") .
2016              " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash"}, "tree") .
2017              "<br/><br/>\n" .
2018              "</div>\n";
2019        print "<div>\n" .
2020              $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
2021              "</div>\n";
2022        print "<div class=\"page_path\"><b>/$file_name</b><br/></div>\n";
2023
2024        open my $fd, "-|", "$gitbin/git-rev-list $hash | $gitbin/git-diff-tree -r --stdin $file_name";
2025        my $commit;
2026        print "<table cellspacing=\"0\">\n";
2027        my $alternate = 0;
2028        while (my $line = <$fd>) {
2029                if ($line =~ m/^([0-9a-fA-F]{40})/){
2030                        $commit = $1;
2031                        next;
2032                }
2033                if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/ && (defined $commit)) {
2034                        my %co = git_read_commit($commit);
2035                        if (!%co) {
2036                                next;
2037                        }
2038                        if ($alternate) {
2039                                print "<tr class=\"dark\">\n";
2040                        } else {
2041                                print "<tr class=\"light\">\n";
2042                        }
2043                        $alternate ^= 1;
2044                        print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2045                              "<td><i>" . escapeHTML(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
2046                              "<td>" . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit", -class => "list"}, "<b>" .
2047                              escapeHTML(chop_str($co{'title'}, 50)) . "</b>") . "</td>\n" .
2048                              "<td class=\"link\">" .
2049                              $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit"}, "commit") .
2050                              " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$commit"}, "commitdiff") .
2051                              " | " . $cgi->a({-href => "$my_uri?p=$project;a=blob;hb=$commit;f=$file_name"}, "blob");
2052                        my $blob = git_get_hash_by_path($hash, $file_name);
2053                        my $blob_parent = git_get_hash_by_path($commit, $file_name);
2054                        if (defined $blob && defined $blob_parent && $blob ne $blob_parent) {
2055                                print " | " .
2056                                $cgi->a({-href => "$my_uri?p=$project;a=blobdiff;h=$blob;hp=$blob_parent;hb=$commit;f=$file_name"},
2057                                "diff to current");
2058                        }
2059                        print "</td>\n" .
2060                              "</tr>\n";
2061                        undef $commit;
2062                }
2063        }
2064        print "</table>\n";
2065        close $fd;
2066        git_footer_html();
2067}
2068
2069sub git_search {
2070        if (!defined $searchtext) {
2071                die_error("", "Text field empty.");
2072        }
2073        if (!defined $hash) {
2074                $hash = git_read_hash("$project/HEAD");
2075        }
2076        my %co = git_read_commit($hash);
2077        if (!%co) {
2078                die_error(undef, "Unknown commit object.");
2079        }
2080        # pickaxe may take all resources of your box and run for several minutes
2081        # with every query - so decide by yourself how public you make this feature :)
2082        my $commit_search = 1;
2083        my $author_search = 0;
2084        my $committer_search = 0;
2085        my $pickaxe_search = 0;
2086        if ($searchtext =~ s/^author\\://i) {
2087                $author_search = 1;
2088        } elsif ($searchtext =~ s/^committer\\://i) {
2089                $committer_search = 1;
2090        } elsif ($searchtext =~ s/^pickaxe\\://i) {
2091                $commit_search = 0;
2092                $pickaxe_search = 1;
2093        }
2094        git_header_html();
2095        print "<div class=\"page_nav\">\n" .
2096              $cgi->a({-href => "$my_uri?p=$project;a=summary;h=$hash"}, "summary") .
2097              " | " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog"}, "shortlog") .
2098              " | " . $cgi->a({-href => "$my_uri?p=$project;a=log;h=$hash"}, "log") .
2099              " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash"}, "commit") .
2100              " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash"}, "commitdiff") .
2101              " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash"}, "tree") .
2102              "<br/><br/>\n" .
2103              "</div>\n";
2104
2105        print "<div>\n" .
2106              $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
2107              "</div>\n";
2108        print "<table cellspacing=\"0\">\n";
2109        my $alternate = 0;
2110        if ($commit_search) {
2111                $/ = "\0";
2112                open my $fd, "-|", "$gitbin/git-rev-list --header --parents $hash" or next;
2113                while (my $commit_text = <$fd>) {
2114                        if (!grep m/$searchtext/i, $commit_text) {
2115                                next;
2116                        }
2117                        if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
2118                                next;
2119                        }
2120                        if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
2121                                next;
2122                        }
2123                        my @commit_lines = split "\n", $commit_text;
2124                        my %co = git_read_commit(undef, \@commit_lines);
2125                        if (!%co) {
2126                                next;
2127                        }
2128                        if ($alternate) {
2129                                print "<tr class=\"dark\">\n";
2130                        } else {
2131                                print "<tr class=\"light\">\n";
2132                        }
2133                        $alternate ^= 1;
2134                        print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2135                              "<td><i>" . escapeHTML(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
2136                              "<td>" .
2137                              $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$co{'id'}", -class => "list"}, "<b>" . escapeHTML(chop_str($co{'title'}, 50)) . "</b><br/>");
2138                        my $comment = $co{'comment'};
2139                        foreach my $line (@$comment) {
2140                                if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
2141                                        my $lead = escapeHTML($1) || "";
2142                                        $lead = chop_str($lead, 30, 10);
2143                                        my $match = escapeHTML($2) || "";
2144                                        my $trail = escapeHTML($3) || "";
2145                                        $trail = chop_str($trail, 30, 10);
2146                                        my $text = "$lead<span style=\"color:#e00000\">$match</span>$trail";
2147                                        print chop_str($text, 80, 5) . "<br/>\n";
2148                                }
2149                        }
2150                        print "</td>\n" .
2151                              "<td class=\"link\">" .
2152                              $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$co{'id'}"}, "commit") .
2153                              " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$co{'id'}"}, "tree");
2154                        print "</td>\n" .
2155                              "</tr>\n";
2156                }
2157                close $fd;
2158        }
2159
2160        if ($pickaxe_search) {
2161                $/ = "\n";
2162                open my $fd, "-|", "$gitbin/git-rev-list $hash | $gitbin/git-diff-tree -r --stdin -S$searchtext";
2163                undef %co;
2164                my @files;
2165                while (my $line = <$fd>) {
2166                        if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
2167                                my %set;
2168                                $set{'file'} = $6;
2169                                $set{'from_id'} = $3;
2170                                $set{'to_id'} = $4;
2171                                $set{'id'} = $set{'to_id'};
2172                                if ($set{'id'} =~ m/0{40}/) {
2173                                        $set{'id'} = $set{'from_id'};
2174                                }
2175                                if ($set{'id'} =~ m/0{40}/) {
2176                                        next;
2177                                }
2178                                push @files, \%set;
2179                        } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
2180                                if (%co) {
2181                                        if ($alternate) {
2182                                                print "<tr class=\"dark\">\n";
2183                                        } else {
2184                                                print "<tr class=\"light\">\n";
2185                                        }
2186                                        $alternate ^= 1;
2187                                        print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2188                                              "<td><i>" . escapeHTML(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
2189                                              "<td>" .
2190                                              $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$co{'id'}", -class => "list"}, "<b>" .
2191                                              escapeHTML(chop_str($co{'title'}, 50)) . "</b><br/>");
2192                                        while (my $setref = shift @files) {
2193                                                my %set = %$setref;
2194                                                print $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$set{'id'};hb=$co{'id'};f=$set{'file'}", class => "list"},
2195                                                      "<span style=\"color:#e00000\">" . escapeHTML($set{'file'}) . "</span>") .
2196                                                      "<br/>\n";
2197                                        }
2198                                        print "</td>\n" .
2199                                              "<td class=\"link\">" .
2200                                              $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$co{'id'}"}, "commit") .
2201                                              " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$co{'id'}"}, "tree");
2202                                        print "</td>\n" .
2203                                              "</tr>\n";
2204                                }
2205                                %co = git_read_commit($1);
2206                        }
2207                }
2208                close $fd;
2209        }
2210        print "</table>\n";
2211        git_footer_html();
2212}
2213
2214sub git_shortlog {
2215        my $head = git_read_hash("$project/HEAD");
2216        if (!defined $hash) {
2217                $hash = $head;
2218        }
2219        if (!defined $page) {
2220                $page = 0;
2221        }
2222        git_header_html();
2223        print "<div class=\"page_nav\">\n" .
2224              $cgi->a({-href => "$my_uri?p=$project;a=summary"}, "summary") .
2225              " | shortlog" .
2226              " | " . $cgi->a({-href => "$my_uri?p=$project;a=log;h=$hash"}, "log") .
2227              " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash"}, "commit") .
2228              " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash"}, "commitdiff") .
2229              " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$hash;hb=$hash"}, "tree") . "<br/>\n";
2230
2231        my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
2232        open my $fd, "-|", "$gitbin/git-rev-list $limit $hash" or die_error(undef, "Open failed.");
2233        my (@revlist) = map { chomp; $_ } <$fd>;
2234        close $fd;
2235
2236        if ($hash ne $head || $page) {
2237                print $cgi->a({-href => "$my_uri?p=$project;a=shortlog"}, "HEAD");
2238        } else {
2239                print "HEAD";
2240        }
2241        if ($page > 0) {
2242                print " &sdot; " .
2243                $cgi->a({-href => "$my_uri?p=$project;a=shortlog;h=$hash;pg=" . ($page-1), -accesskey => "p", -title => "Alt-p"}, "prev");
2244        } else {
2245                print " &sdot; prev";
2246        }
2247        if ($#revlist >= (100 * ($page+1)-1)) {
2248                print " &sdot; " .
2249                $cgi->a({-href => "$my_uri?p=$project;a=shortlog;h=$hash;pg=" . ($page+1), -accesskey => "n", -title => "Alt-n"}, "next");
2250        } else {
2251                print " &sdot; next";
2252        }
2253        print "<br/>\n" .
2254              "</div>\n";
2255        print "<div>\n" .
2256              $cgi->a({-href => "$my_uri?p=$project;a=summary", -class => "title"}, "&nbsp;") .
2257              "</div>\n";
2258        print "<table cellspacing=\"0\">\n";
2259        my $alternate = 0;
2260        for (my $i = ($page * 100); $i <= $#revlist; $i++) {
2261                my $commit = $revlist[$i];
2262                my %co = git_read_commit($commit);
2263                my %ad = date_str($co{'author_epoch'});
2264                if ($alternate) {
2265                        print "<tr class=\"dark\">\n";
2266                } else {
2267                        print "<tr class=\"light\">\n";
2268                }
2269                $alternate ^= 1;
2270                print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2271                      "<td><i>" . escapeHTML(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
2272                      "<td>";
2273                if (length($co{'title_short'}) < length($co{'title'})) {
2274                        print $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit", -class => "list", -title => "$co{'title'}"},
2275                              "<b>" . escapeHTML($co{'title_short'}) . "</b>");
2276                } else {
2277                        print $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit", -class => "list"},
2278                              "<b>" . escapeHTML($co{'title_short'}) . "</b>");
2279                }
2280                print "</td>\n" .
2281                      "<td class=\"link\">" .
2282                      $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit"}, "commit") .
2283                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$commit"}, "commitdiff") .
2284                      "</td>\n" .
2285                      "</tr>";
2286        }
2287        if ($#revlist >= (100 * ($page+1)-1)) {
2288                print "<tr>\n" .
2289                      "<td>" .
2290                      $cgi->a({-href => "$my_uri?p=$project;a=shortlog;h=$hash;pg=" . ($page+1), -title => "Alt-n"}, "next") .
2291                      "</td>\n" .
2292                      "</tr>\n";
2293        }
2294        print "</table\n>";
2295        git_footer_html();
2296}