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