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