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