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