gitweb / gitweb.cgion commit gitweb: Remove characters entities entirely when shortening string (7ca84b5)
   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                $body =~ s/&[^;]$//; # remove chopped character entities
 780        }
 781        return "$body$tail";
 782}
 783
 784sub file_type {
 785        my $mode = oct shift;
 786
 787        if (S_ISDIR($mode & S_IFMT)) {
 788                return "directory";
 789        } elsif (S_ISLNK($mode)) {
 790                return "symlink";
 791        } elsif (S_ISREG($mode)) {
 792                return "file";
 793        } else {
 794                return "unknown";
 795        }
 796}
 797
 798sub format_log_line_html {
 799        my $line = shift;
 800
 801        $line = esc_html($line);
 802        $line =~ s/ /&nbsp;/g;
 803        if ($line =~ m/([0-9a-fA-F]{40})/) {
 804                my $hash_text = $1;
 805                if (git_get_type($hash_text) eq "commit") {
 806                        my $link = $cgi->a({-class => "text", -href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_text")}, $hash_text);
 807                        $line =~ s/$hash_text/$link/;
 808                }
 809        }
 810        return $line;
 811}
 812
 813sub date_str {
 814        my $epoch = shift;
 815        my $tz = shift || "-0000";
 816
 817        my %date;
 818        my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
 819        my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
 820        my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
 821        $date{'hour'} = $hour;
 822        $date{'minute'} = $min;
 823        $date{'mday'} = $mday;
 824        $date{'day'} = $days[$wday];
 825        $date{'month'} = $months[$mon];
 826        $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000", $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
 827        $date{'mday-time'} = sprintf "%d %s %02d:%02d", $mday, $months[$mon], $hour ,$min;
 828
 829        $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
 830        my $local = $epoch + ((int $1 + ($2/60)) * 3600);
 831        ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
 832        $date{'hour_local'} = $hour;
 833        $date{'minute_local'} = $min;
 834        $date{'tz_local'} = $tz;
 835        return %date;
 836}
 837
 838# git-logo (cached in browser for one day)
 839sub git_logo {
 840        binmode STDOUT, ':raw';
 841        print $cgi->header(-type => 'image/png', -expires => '+1d');
 842        # cat git-logo.png | hexdump -e '16/1 " %02x"  "\n"' | sed 's/ /\\x/g'
 843        print   "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52" .
 844                "\x00\x00\x00\x48\x00\x00\x00\x1b\x04\x03\x00\x00\x00\x2d\xd9\xd4" .
 845                "\x2d\x00\x00\x00\x18\x50\x4c\x54\x45\xff\xff\xff\x60\x60\x5d\xb0" .
 846                "\xaf\xaa\x00\x80\x00\xce\xcd\xc7\xc0\x00\x00\xe8\xe8\xe6\xf7\xf7" .
 847                "\xf6\x95\x0c\xa7\x47\x00\x00\x00\x73\x49\x44\x41\x54\x28\xcf\x63" .
 848                "\x48\x67\x20\x04\x4a\x5c\x18\x0a\x08\x2a\x62\x53\x61\x20\x02\x08" .
 849                "\x0d\x69\x45\xac\xa1\xa1\x01\x30\x0c\x93\x60\x36\x26\x52\x91\xb1" .
 850                "\x01\x11\xd6\xe1\x55\x64\x6c\x6c\xcc\x6c\x6c\x0c\xa2\x0c\x70\x2a" .
 851                "\x62\x06\x2a\xc1\x62\x1d\xb3\x01\x02\x53\xa4\x08\xe8\x00\x03\x18" .
 852                "\x26\x56\x11\xd4\xe1\x20\x97\x1b\xe0\xb4\x0e\x35\x24\x71\x29\x82" .
 853                "\x99\x30\xb8\x93\x0a\x11\xb9\x45\x88\xc1\x8d\xa0\xa2\x44\x21\x06" .
 854                "\x27\x41\x82\x40\x85\xc1\x45\x89\x20\x70\x01\x00\xa4\x3d\x21\xc5" .
 855                "\x12\x1c\x9a\xfe\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82";
 856}
 857
 858sub get_file_owner {
 859        my $path = shift;
 860
 861        my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
 862        my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
 863        if (!defined $gcos) {
 864                return undef;
 865        }
 866        my $owner = $gcos;
 867        $owner =~ s/[,;].*$//;
 868        return decode("utf8", $owner, Encode::FB_DEFAULT);
 869}
 870
 871sub git_read_projects {
 872        my @list;
 873
 874        if (-d $projects_list) {
 875                # search in directory
 876                my $dir = $projects_list;
 877                opendir my ($dh), $dir or return undef;
 878                while (my $dir = readdir($dh)) {
 879                        if (-e "$projectroot/$dir/HEAD") {
 880                                my $pr = {
 881                                        path => $dir,
 882                                };
 883                                push @list, $pr
 884                        }
 885                }
 886                closedir($dh);
 887        } elsif (-f $projects_list) {
 888                # read from file(url-encoded):
 889                # 'git%2Fgit.git Linus+Torvalds'
 890                # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
 891                # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
 892                open my ($fd), $projects_list or return undef;
 893                while (my $line = <$fd>) {
 894                        chomp $line;
 895                        my ($path, $owner) = split ' ', $line;
 896                        $path = unescape($path);
 897                        $owner = unescape($owner);
 898                        if (!defined $path) {
 899                                next;
 900                        }
 901                        if (-e "$projectroot/$path/HEAD") {
 902                                my $pr = {
 903                                        path => $path,
 904                                        owner => decode("utf8", $owner, Encode::FB_DEFAULT),
 905                                };
 906                                push @list, $pr
 907                        }
 908                }
 909                close $fd;
 910        }
 911        @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
 912        return @list;
 913}
 914
 915sub git_get_project_config {
 916        my $key = shift;
 917
 918        return unless ($key);
 919        $key =~ s/^gitweb\.//;
 920        return if ($key =~ m/\W/);
 921
 922        my $val = qx($GIT repo-config --get gitweb.$key);
 923        return ($val);
 924}
 925
 926sub git_get_project_config_bool {
 927        my $val = git_get_project_config (@_);
 928        if ($val and $val =~ m/true|yes|on/) {
 929                return (1);
 930        }
 931        return; # implicit false
 932}
 933
 934sub git_project_list {
 935        my @list = git_read_projects();
 936        my @projects;
 937        if (!@list) {
 938                die_error(undef, "No project found.");
 939        }
 940        foreach my $pr (@list) {
 941                my $head = git_read_head($pr->{'path'});
 942                if (!defined $head) {
 943                        next;
 944                }
 945                $ENV{'GIT_DIR'} = "$projectroot/$pr->{'path'}";
 946                my %co = git_read_commit($head);
 947                if (!%co) {
 948                        next;
 949                }
 950                $pr->{'commit'} = \%co;
 951                if (!defined $pr->{'descr'}) {
 952                        my $descr = git_read_description($pr->{'path'}) || "";
 953                        $pr->{'descr'} = chop_str($descr, 25, 5);
 954                }
 955                if (!defined $pr->{'owner'}) {
 956                        $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
 957                }
 958                push @projects, $pr;
 959        }
 960        git_header_html();
 961        if (-f $home_text) {
 962                print "<div class=\"index_include\">\n";
 963                open (my $fd, $home_text);
 964                print <$fd>;
 965                close $fd;
 966                print "</div>\n";
 967        }
 968        print "<table class=\"project_list\">\n" .
 969              "<tr>\n";
 970        if (!defined($order) || (defined($order) && ($order eq "project"))) {
 971                @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
 972                print "<th>Project</th>\n";
 973        } else {
 974                print "<th>" . $cgi->a({-class => "header", -href => "$my_uri?" . esc_param("o=project")}, "Project") . "</th>\n";
 975        }
 976        if (defined($order) && ($order eq "descr")) {
 977                @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
 978                print "<th>Description</th>\n";
 979        } else {
 980                print "<th>" . $cgi->a({-class => "header", -href => "$my_uri?" . esc_param("o=descr")}, "Description") . "</th>\n";
 981        }
 982        if (defined($order) && ($order eq "owner")) {
 983                @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
 984                print "<th>Owner</th>\n";
 985        } else {
 986                print "<th>" . $cgi->a({-class => "header", -href => "$my_uri?" . esc_param("o=owner")}, "Owner") . "</th>\n";
 987        }
 988        if (defined($order) && ($order eq "age")) {
 989                @projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;
 990                print "<th>Last Change</th>\n";
 991        } else {
 992                print "<th>" . $cgi->a({-class => "header", -href => "$my_uri?" . esc_param("o=age")}, "Last Change") . "</th>\n";
 993        }
 994        print "<th></th>\n" .
 995              "</tr>\n";
 996        my $alternate = 0;
 997        foreach my $pr (@projects) {
 998                if ($alternate) {
 999                        print "<tr class=\"dark\">\n";
1000                } else {
1001                        print "<tr class=\"light\">\n";
1002                }
1003                $alternate ^= 1;
1004                print "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=summary"), -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
1005                      "<td>" . esc_html($pr->{'descr'}) . "</td>\n" .
1006                      "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
1007                print "<td class=\"". age_class($pr->{'commit'}{'age'}) . "\">" . $pr->{'commit'}{'age_string'} . "</td>\n" .
1008                      "<td class=\"link\">" .
1009                      $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=summary")}, "summary") .
1010                      " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=shortlog")}, "shortlog") .
1011                      " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=log")}, "log") .
1012                      "</td>\n" .
1013                      "</tr>\n";
1014        }
1015        print "</table>\n";
1016        git_footer_html();
1017}
1018
1019sub read_info_ref {
1020        my $type = shift || "";
1021        my %refs;
1022        # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c      refs/tags/v2.6.11
1023        # c39ae07f393806ccf406ef966e9a15afc43cc36a      refs/tags/v2.6.11^{}
1024        open my $fd, "$projectroot/$project/info/refs" or return;
1025        while (my $line = <$fd>) {
1026                chomp $line;
1027                if ($line =~ m/^([0-9a-fA-F]{40})\t.*$type\/([^\^]+)/) {
1028                        if (defined $refs{$1}) {
1029                                $refs{$1} .= " / $2";
1030                        } else {
1031                                $refs{$1} = $2;
1032                        }
1033                }
1034        }
1035        close $fd or return;
1036        return \%refs;
1037}
1038
1039sub git_read_refs {
1040        my $ref_dir = shift;
1041        my @reflist;
1042
1043        my @refs;
1044        opendir my $dh, "$projectroot/$project/$ref_dir";
1045        while (my $dir = readdir($dh)) {
1046                if ($dir =~ m/^\./) {
1047                        next;
1048                }
1049                if (-d "$projectroot/$project/$ref_dir/$dir") {
1050                        opendir my $dh2, "$projectroot/$project/$ref_dir/$dir";
1051                        my @subdirs = grep !m/^\./, readdir $dh2;
1052                        closedir($dh2);
1053                        foreach my $subdir (@subdirs) {
1054                                push @refs, "$dir/$subdir"
1055                        }
1056                        next;
1057                }
1058                push @refs, $dir;
1059        }
1060        closedir($dh);
1061        foreach my $ref_file (@refs) {
1062                my $ref_id = git_read_hash("$project/$ref_dir/$ref_file");
1063                my $type = git_get_type($ref_id) || next;
1064                my %ref_item;
1065                my %co;
1066                $ref_item{'type'} = $type;
1067                $ref_item{'id'} = $ref_id;
1068                $ref_item{'epoch'} = 0;
1069                $ref_item{'age'} = "unknown";
1070                if ($type eq "tag") {
1071                        my %tag = git_read_tag($ref_id);
1072                        $ref_item{'comment'} = $tag{'comment'};
1073                        if ($tag{'type'} eq "commit") {
1074                                %co = git_read_commit($tag{'object'});
1075                                $ref_item{'epoch'} = $co{'committer_epoch'};
1076                                $ref_item{'age'} = $co{'age_string'};
1077                        } elsif (defined($tag{'epoch'})) {
1078                                my $age = time - $tag{'epoch'};
1079                                $ref_item{'epoch'} = $tag{'epoch'};
1080                                $ref_item{'age'} = age_string($age);
1081                        }
1082                        $ref_item{'reftype'} = $tag{'type'};
1083                        $ref_item{'name'} = $tag{'name'};
1084                        $ref_item{'refid'} = $tag{'object'};
1085                } elsif ($type eq "commit"){
1086                        %co = git_read_commit($ref_id);
1087                        $ref_item{'reftype'} = "commit";
1088                        $ref_item{'name'} = $ref_file;
1089                        $ref_item{'title'} = $co{'title'};
1090                        $ref_item{'refid'} = $ref_id;
1091                        $ref_item{'epoch'} = $co{'committer_epoch'};
1092                        $ref_item{'age'} = $co{'age_string'};
1093                }
1094
1095                push @reflist, \%ref_item;
1096        }
1097        # sort tags by age
1098        @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
1099        return \@reflist;
1100}
1101
1102sub git_summary {
1103        my $descr = git_read_description($project) || "none";
1104        my $head = git_read_head($project);
1105        my %co = git_read_commit($head);
1106        my %cd = date_str($co{'committer_epoch'}, $co{'committer_tz'});
1107
1108        my $owner;
1109        if (-f $projects_list) {
1110                open (my $fd , $projects_list);
1111                while (my $line = <$fd>) {
1112                        chomp $line;
1113                        my ($pr, $ow) = split ' ', $line;
1114                        $pr = unescape($pr);
1115                        $ow = unescape($ow);
1116                        if ($pr eq $project) {
1117                                $owner = decode("utf8", $ow, Encode::FB_DEFAULT);
1118                                last;
1119                        }
1120                }
1121                close $fd;
1122        }
1123        if (!defined $owner) {
1124                $owner = get_file_owner("$projectroot/$project");
1125        }
1126
1127        my $refs = read_info_ref();
1128        git_header_html();
1129        git_page_nav('summary','', $head);
1130        print "<div class=\"title\">&nbsp;</div>\n";
1131        print "<table cellspacing=\"0\">\n" .
1132              "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
1133              "<tr><td>owner</td><td>$owner</td></tr>\n" .
1134              "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n" .
1135              "</table>\n";
1136        open my $fd, "-|", $GIT, "rev-list", "--max-count=17", git_read_head($project)
1137                or die_error(undef, "Open git-rev-list failed.");
1138        my @revlist = map { chomp; $_ } <$fd>;
1139        close $fd;
1140        git_header_div('shortlog');
1141        my $i = 16;
1142        print "<table cellspacing=\"0\">\n";
1143        my $alternate = 0;
1144        foreach my $commit (@revlist) {
1145                my %co = git_read_commit($commit);
1146                my %ad = date_str($co{'author_epoch'});
1147                if ($alternate) {
1148                        print "<tr class=\"dark\">\n";
1149                } else {
1150                        print "<tr class=\"light\">\n";
1151                }
1152                $alternate ^= 1;
1153                if ($i-- > 0) {
1154                        my $ref = "";
1155                        if (defined $refs->{$commit}) {
1156                                $ref = " <span class=\"tag\">" . esc_html($refs->{$commit}) . "</span>";
1157                        }
1158                        print "<td><i>$co{'age_string'}</i></td>\n" .
1159                              "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
1160                              "<td>";
1161                        if (length($co{'title_short'}) < length($co{'title'})) {
1162                                print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "list", -title => "$co{'title'}"},
1163                                      "<b>" . esc_html($co{'title_short'}) . "$ref</b>");
1164                        } else {
1165                                print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "list"},
1166                                      "<b>" . esc_html($co{'title'}) . "$ref</b>");
1167                        }
1168                        print "</td>\n" .
1169                              "<td class=\"link\">" .
1170                              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") .
1171                              " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$commit")}, "commitdiff") .
1172                              "</td>\n" .
1173                              "</tr>";
1174                } else {
1175                        print "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "...") . "</td>\n" .
1176                        "</tr>";
1177                        last;
1178                }
1179        }
1180        print "</table\n>";
1181
1182        my $taglist = git_read_refs("refs/tags");
1183        if (defined @$taglist) {
1184                git_header_div('tags');
1185                my $i = 16;
1186                print "<table cellspacing=\"0\">\n";
1187                my $alternate = 0;
1188                foreach my $entry (@$taglist) {
1189                        my %tag = %$entry;
1190                        my $comment_lines = $tag{'comment'};
1191                        my $comment = shift @$comment_lines;
1192                        if (defined($comment)) {
1193                                $comment = chop_str($comment, 30, 5);
1194                        }
1195                        if ($alternate) {
1196                                print "<tr class=\"dark\">\n";
1197                        } else {
1198                                print "<tr class=\"light\">\n";
1199                        }
1200                        $alternate ^= 1;
1201                        if ($i-- > 0) {
1202                                print "<td><i>$tag{'age'}</i></td>\n" .
1203                                      "<td>" .
1204                                      $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'reftype'};h=$tag{'refid'}"), -class => "list"},
1205                                      "<b>" . esc_html($tag{'name'}) . "</b>") .
1206                                      "</td>\n" .
1207                                      "<td>";
1208                                if (defined($comment)) {
1209                                        print $cgi->a({-class => "list", -href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}")}, esc_html($comment));
1210                                }
1211                                print "</td>\n" .
1212                                      "<td class=\"link\">";
1213                                if ($tag{'type'} eq "tag") {
1214                                        print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}")}, "tag") . " | ";
1215                                }
1216                                print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'reftype'};h=$tag{'refid'}")}, $tag{'reftype'});
1217                                if ($tag{'reftype'} eq "commit") {
1218                                        print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}")}, "shortlog") .
1219                                              " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$tag{'refid'}")}, "log");
1220                                }
1221                                print "</td>\n" .
1222                                      "</tr>";
1223                        } else {
1224                                print "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tags")}, "...") . "</td>\n" .
1225                                "</tr>";
1226                                last;
1227                        }
1228                }
1229                print "</table\n>";
1230        }
1231
1232        my $headlist = git_read_refs("refs/heads");
1233        if (defined @$headlist) {
1234                git_header_div('heads');
1235                my $i = 16;
1236                print "<table cellspacing=\"0\">\n";
1237                my $alternate = 0;
1238                foreach my $entry (@$headlist) {
1239                        my %tag = %$entry;
1240                        if ($alternate) {
1241                                print "<tr class=\"dark\">\n";
1242                        } else {
1243                                print "<tr class=\"light\">\n";
1244                        }
1245                        $alternate ^= 1;
1246                        if ($i-- > 0) {
1247                                print "<td><i>$tag{'age'}</i></td>\n" .
1248                                      "<td>" .
1249                                      $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}"), -class => "list"},
1250                                      "<b>" . esc_html($tag{'name'}) . "</b>") .
1251                                      "</td>\n" .
1252                                      "<td class=\"link\">" .
1253                                      $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}")}, "shortlog") .
1254                                      " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$tag{'name'}")}, "log") .
1255                                      "</td>\n" .
1256                                      "</tr>";
1257                        } else {
1258                                print "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=heads")}, "...") . "</td>\n" .
1259                                "</tr>";
1260                                last;
1261                        }
1262                }
1263                print "</table\n>";
1264        }
1265        git_footer_html();
1266}
1267
1268sub git_print_page_path {
1269        my $name = shift;
1270        my $type = shift;
1271
1272        if (!defined $name) {
1273                print "<div class=\"page_path\"><b>/</b></div>\n";
1274        } elsif ($type =~ "blob") {
1275                print "<div class=\"page_path\"><b>" .
1276                        $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;f=$file_name")}, esc_html($name)) . "</b><br/></div>\n";
1277        } else {
1278                print "<div class=\"page_path\"><b>" . esc_html($name) . "</b><br/></div>\n";
1279        }
1280}
1281
1282sub git_tag {
1283        my $head = git_read_head($project);
1284        git_header_html();
1285        git_page_nav('','', $head,undef,$head);
1286        my %tag = git_read_tag($hash);
1287        git_header_div('commit', esc_html($tag{'name'}), $hash);
1288        print "<div class=\"title_text\">\n" .
1289              "<table cellspacing=\"0\">\n" .
1290              "<tr>\n" .
1291              "<td>object</td>\n" .
1292              "<td>" . $cgi->a({-class => "list", -href => "$my_uri?" . esc_param("p=$project;a=$tag{'type'};h=$tag{'object'}")}, $tag{'object'}) . "</td>\n" .
1293              "<td class=\"link\">" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'type'};h=$tag{'object'}")}, $tag{'type'}) . "</td>\n" .
1294              "</tr>\n";
1295        if (defined($tag{'author'})) {
1296                my %ad = date_str($tag{'epoch'}, $tag{'tz'});
1297                print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
1298                print "<tr><td></td><td>" . $ad{'rfc2822'} . sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) . "</td></tr>\n";
1299        }
1300        print "</table>\n\n" .
1301              "</div>\n";
1302        print "<div class=\"page_body\">";
1303        my $comment = $tag{'comment'};
1304        foreach my $line (@$comment) {
1305                print esc_html($line) . "<br/>\n";
1306        }
1307        print "</div>\n";
1308        git_footer_html();
1309}
1310
1311sub git_blame2 {
1312        my $fd;
1313        my $ftype;
1314        die_error(undef, "Permission denied.") if (!git_get_project_config_bool ('blame'));
1315        die_error('404 Not Found', "File name not defined") if (!$file_name);
1316        $hash_base ||= git_read_head($project);
1317        die_error(undef, "Reading commit failed") unless ($hash_base);
1318        my %co = git_read_commit($hash_base)
1319                or die_error(undef, "Reading commit failed");
1320        if (!defined $hash) {
1321                $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
1322                        or die_error(undef, "Error looking up file");
1323        }
1324        $ftype = git_get_type($hash);
1325        if ($ftype !~ "blob") {
1326                die_error("400 Bad Request", "object is not a blob");
1327        }
1328        open ($fd, "-|", $GIT, "blame", '-l', $file_name, $hash_base)
1329                or die_error(undef, "Open git-blame failed.");
1330        git_header_html();
1331        my $formats_nav =
1332                $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$hash;hb=$hash_base;f=$file_name")}, "blob") .
1333                " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;f=$file_name")}, "head");
1334        git_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
1335        git_header_div('commit', esc_html($co{'title'}), $hash_base);
1336        git_print_page_path($file_name, $ftype);
1337        my @rev_color = (qw(light dark));
1338        my $num_colors = scalar(@rev_color);
1339        my $current_color = 0;
1340        my $last_rev;
1341        print "<div class=\"page_body\">\n";
1342        print "<table class=\"blame\">\n";
1343        print "<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n";
1344        while (<$fd>) {
1345                /^([0-9a-fA-F]{40}).*?(\d+)\)\s{1}(\s*.*)/;
1346                my $full_rev = $1;
1347                my $rev = substr($full_rev, 0, 8);
1348                my $lineno = $2;
1349                my $data = $3;
1350
1351                if (!defined $last_rev) {
1352                        $last_rev = $full_rev;
1353                } elsif ($last_rev ne $full_rev) {
1354                        $last_rev = $full_rev;
1355                        $current_color = ++$current_color % $num_colors;
1356                }
1357                print "<tr class=\"$rev_color[$current_color]\">\n";
1358                print "<td class=\"sha1\">" .
1359                        $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$full_rev;f=$file_name")}, esc_html($rev)) . "</td>\n";
1360                print "<td class=\"linenr\"><a id=\"l$lineno\" href=\"#l$lineno\" class=\"linenr\">" . esc_html($lineno) . "</a></td>\n";
1361                print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
1362                print "</tr>\n";
1363        }
1364        print "</table>\n";
1365        print "</div>";
1366        close $fd or print "Reading blob failed\n";
1367        git_footer_html();
1368}
1369
1370sub git_blame {
1371        my $fd;
1372        die_error('403 Permission denied', "Permission denied.") if (!git_get_project_config_bool ('blame'));
1373        die_error('404 Not Found', "What file will it be, master?") if (!$file_name);
1374        $hash_base ||= git_read_head($project);
1375        die_error(undef, "Reading commit failed.") unless ($hash_base);
1376        my %co = git_read_commit($hash_base)
1377                or die_error(undef, "Reading commit failed.");
1378        if (!defined $hash) {
1379                $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
1380                        or die_error(undef, "Error lookup file.");
1381        }
1382        open ($fd, "-|", $GIT, "annotate", '-l', '-t', '-r', $file_name, $hash_base)
1383                or die_error(undef, "Open git-annotate failed.");
1384        git_header_html();
1385        my $formats_nav =
1386                $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$hash;hb=$hash_base;f=$file_name")}, "blob") .
1387                " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;f=$file_name")}, "head");
1388        git_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
1389        git_header_div('commit', esc_html($co{'title'}), $hash_base);
1390        git_print_page_path($file_name);
1391        print "<div class=\"page_body\">\n";
1392        print <<HTML;
1393<table class="blame">
1394  <tr>
1395    <th>Commit</th>
1396    <th>Age</th>
1397    <th>Author</th>
1398    <th>Line</th>
1399    <th>Data</th>
1400  </tr>
1401HTML
1402        my @line_class = (qw(light dark));
1403        my $line_class_len = scalar (@line_class);
1404        my $line_class_num = $#line_class;
1405        while (my $line = <$fd>) {
1406                my $long_rev;
1407                my $short_rev;
1408                my $author;
1409                my $time;
1410                my $lineno;
1411                my $data;
1412                my $age;
1413                my $age_str;
1414                my $age_class;
1415
1416                chomp $line;
1417                $line_class_num = ($line_class_num + 1) % $line_class_len;
1418
1419                if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) \+\d\d\d\d\t(\d+)\)(.*)$/) {
1420                        $long_rev = $1;
1421                        $author   = $2;
1422                        $time     = $3;
1423                        $lineno   = $4;
1424                        $data     = $5;
1425                } else {
1426                        print qq(  <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
1427                        next;
1428                }
1429                $short_rev  = substr ($long_rev, 0, 8);
1430                $age        = time () - $time;
1431                $age_str    = age_string ($age);
1432                $age_str    =~ s/ /&nbsp;/g;
1433                $age_class  = age_class($age);
1434                $author     = esc_html ($author);
1435                $author     =~ s/ /&nbsp;/g;
1436                # escape tabs
1437                while ((my $pos = index($data, "\t")) != -1) {
1438                        if (my $count = (8 - ($pos % 8))) {
1439                                my $spaces = ' ' x $count;
1440                                $data =~ s/\t/$spaces/;
1441                        }
1442                }
1443                $data = esc_html ($data);
1444
1445                print <<HTML;
1446  <tr class="$line_class[$line_class_num]">
1447    <td class="sha1"><a href="$my_uri?${\esc_param ("p=$project;a=commit;h=$long_rev")}" class="text">$short_rev..</a></td>
1448    <td class="$age_class">$age_str</td>
1449    <td>$author</td>
1450    <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
1451    <td class="pre">$data</td>
1452  </tr>
1453HTML
1454        } # while (my $line = <$fd>)
1455        print "</table>\n\n";
1456        close $fd or print "Reading blob failed.\n";
1457        print "</div>";
1458        git_footer_html();
1459}
1460
1461sub git_tags {
1462        my $head = git_read_head($project);
1463        git_header_html();
1464        git_page_nav('','', $head,undef,$head);
1465        git_header_div('summary', $project);
1466        print "<table cellspacing=\"0\">\n";
1467
1468        my $taglist = git_read_refs("refs/tags");
1469        my $alternate = 0;
1470        if (defined @$taglist) {
1471                foreach my $entry (@$taglist) {
1472                        my %tag = %$entry;
1473                        my $comment_lines = $tag{'comment'};
1474                        my $comment = shift @$comment_lines;
1475                        if (defined($comment)) {
1476                                $comment = chop_str($comment, 30, 5);
1477                        }
1478                        if ($alternate) {
1479                                print "<tr class=\"dark\">\n";
1480                        } else {
1481                                print "<tr class=\"light\">\n";
1482                        }
1483                        $alternate ^= 1;
1484                        print "<td><i>$tag{'age'}</i></td>\n" .
1485                              "<td>" .
1486                              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'reftype'};h=$tag{'refid'}"), -class => "list"},
1487                              "<b>" . esc_html($tag{'name'}) . "</b>") .
1488                              "</td>\n" .
1489                              "<td>";
1490                        if (defined($comment)) {
1491                                print $cgi->a({-class => "list", -href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}")}, $comment);
1492                        }
1493                        print "</td>\n" .
1494                              "<td class=\"link\">";
1495                        if ($tag{'type'} eq "tag") {
1496                                print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}")}, "tag") . " | ";
1497                        }
1498                        print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'reftype'};h=$tag{'refid'}")}, $tag{'reftype'});
1499                        if ($tag{'reftype'} eq "commit") {
1500                                print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}")}, "shortlog") .
1501                                      " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$tag{'refid'}")}, "log");
1502                        }
1503                        print "</td>\n" .
1504                              "</tr>";
1505                }
1506        }
1507        print "</table\n>";
1508        git_footer_html();
1509}
1510
1511sub git_heads {
1512        my $head = git_read_head($project);
1513        git_header_html();
1514        git_page_nav('','', $head,undef,$head);
1515        hit_header_div('summary', $project);
1516        print "<table cellspacing=\"0\">\n";
1517
1518        my $taglist = git_read_refs("refs/heads");
1519        my $alternate = 0;
1520        if (defined @$taglist) {
1521                foreach my $entry (@$taglist) {
1522                        my %tag = %$entry;
1523                        if ($alternate) {
1524                                print "<tr class=\"dark\">\n";
1525                        } else {
1526                                print "<tr class=\"light\">\n";
1527                        }
1528                        $alternate ^= 1;
1529                        print "<td><i>$tag{'age'}</i></td>\n" .
1530                              "<td>" .
1531                              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}"), -class => "list"}, "<b>" . esc_html($tag{'name'}) . "</b>") .
1532                              "</td>\n" .
1533                              "<td class=\"link\">" .
1534                              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}")}, "shortlog") .
1535                              " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$tag{'name'}")}, "log") .
1536                              "</td>\n" .
1537                              "</tr>";
1538                }
1539        }
1540        print "</table\n>";
1541        git_footer_html();
1542}
1543
1544sub git_get_hash_by_path {
1545        my $base = shift;
1546        my $path = shift || return undef;
1547
1548        my $tree = $base;
1549
1550        open my $fd, "-|", $GIT, "ls-tree", $base, "--", $path
1551                or die_error(undef, "Open git-ls-tree failed.");
1552        my $line = <$fd>;
1553        close $fd or return undef;
1554
1555        #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa  panic.c'
1556        $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
1557        return $3;
1558}
1559
1560sub mimetype_guess_file {
1561        my $filename = shift;
1562        my $mimemap = shift;
1563        -r $mimemap or return undef;
1564
1565        my %mimemap;
1566        open(MIME, $mimemap) or return undef;
1567        while (<MIME>) {
1568                my ($mime, $exts) = split(/\t+/);
1569                my @exts = split(/\s+/, $exts);
1570                foreach my $ext (@exts) {
1571                        $mimemap{$ext} = $mime;
1572                }
1573        }
1574        close(MIME);
1575
1576        $filename =~ /\.(.*?)$/;
1577        return $mimemap{$1};
1578}
1579
1580sub mimetype_guess {
1581        my $filename = shift;
1582        my $mime;
1583        $filename =~ /\./ or return undef;
1584
1585        if ($mimetypes_file) {
1586                my $file = $mimetypes_file;
1587                #$file =~ m#^/# or $file = "$projectroot/$path/$file";
1588                $mime = mimetype_guess_file($filename, $file);
1589        }
1590        $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1591        return $mime;
1592}
1593
1594sub git_blob_plain_mimetype {
1595        my $fd = shift;
1596        my $filename = shift;
1597
1598        if ($filename) {
1599                my $mime = mimetype_guess($filename);
1600                $mime and return $mime;
1601        }
1602
1603        # just in case
1604        return $default_blob_plain_mimetype unless $fd;
1605
1606        if (-T $fd) {
1607                return 'text/plain' .
1608                       ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1609        } elsif (! $filename) {
1610                return 'application/octet-stream';
1611        } elsif ($filename =~ m/\.png$/i) {
1612                return 'image/png';
1613        } elsif ($filename =~ m/\.gif$/i) {
1614                return 'image/gif';
1615        } elsif ($filename =~ m/\.jpe?g$/i) {
1616                return 'image/jpeg';
1617        } else {
1618                return 'application/octet-stream';
1619        }
1620}
1621
1622sub git_blob_plain {
1623        if (!defined $hash) {
1624                if (defined $file_name) {
1625                        my $base = $hash_base || git_read_head($project);
1626                        $hash = git_get_hash_by_path($base, $file_name, "blob")
1627                                or die_error(undef, "Error lookup file.");
1628                } else {
1629                        die_error(undef, "No file name defined.");
1630                }
1631        }
1632        my $type = shift;
1633        open my $fd, "-|", $GIT, "cat-file", "blob", $hash
1634                or die_error("Couldn't cat $file_name, $hash");
1635
1636        $type ||= git_blob_plain_mimetype($fd, $file_name);
1637
1638        # save as filename, even when no $file_name is given
1639        my $save_as = "$hash";
1640        if (defined $file_name) {
1641                $save_as = $file_name;
1642        } elsif ($type =~ m/^text\//) {
1643                $save_as .= '.txt';
1644        }
1645
1646        print $cgi->header(-type => "$type", '-content-disposition' => "inline; filename=\"$save_as\"");
1647        undef $/;
1648        binmode STDOUT, ':raw';
1649        print <$fd>;
1650        binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
1651        $/ = "\n";
1652        close $fd;
1653}
1654
1655sub git_blob {
1656        if (!defined $hash) {
1657                if (defined $file_name) {
1658                        my $base = $hash_base || git_read_head($project);
1659                        $hash = git_get_hash_by_path($base, $file_name, "blob")
1660                                or die_error(undef, "Error lookup file.");
1661                } else {
1662                        die_error(undef, "No file name defined.");
1663                }
1664        }
1665        my $have_blame = git_get_project_config_bool ('blame');
1666        open my $fd, "-|", $GIT, "cat-file", "blob", $hash
1667                or die_error(undef, "Couldn't cat $file_name, $hash.");
1668        my $mimetype = git_blob_plain_mimetype($fd, $file_name);
1669        if ($mimetype !~ m/^text\//) {
1670                close $fd;
1671                return git_blob_plain($mimetype);
1672        }
1673        git_header_html();
1674        my $formats_nav = '';
1675        if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
1676                if (defined $file_name) {
1677                        if ($have_blame) {
1678                                $formats_nav .= $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;h=$hash;hb=$hash_base;f=$file_name")}, "blame") . " | ";
1679                        }
1680                        $formats_nav .=
1681                                $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$hash;f=$file_name")}, "plain") .
1682                                " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;hb=HEAD;f=$file_name")}, "head");
1683                } else {
1684                        $formats_nav .= $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$hash")}, "plain");
1685                }
1686                git_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
1687                git_header_div('commit', esc_html($co{'title'}), $hash_base);
1688        } else {
1689                print "<div class=\"page_nav\">\n" .
1690                      "<br/><br/></div>\n" .
1691                      "<div class=\"title\">$hash</div>\n";
1692        }
1693        git_print_page_path($file_name, "blob");
1694        print "<div class=\"page_body\">\n";
1695        my $nr;
1696        while (my $line = <$fd>) {
1697                chomp $line;
1698                $nr++;
1699                while ((my $pos = index($line, "\t")) != -1) {
1700                        if (my $count = (8 - ($pos % 8))) {
1701                                my $spaces = ' ' x $count;
1702                                $line =~ s/\t/$spaces/;
1703                        }
1704                }
1705                printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n", $nr, $nr, $nr, esc_html($line);
1706        }
1707        close $fd or print "Reading blob failed.\n";
1708        print "</div>";
1709        git_footer_html();
1710}
1711
1712sub git_tree {
1713        if (!defined $hash) {
1714                $hash = git_read_head($project);
1715                if (defined $file_name) {
1716                        my $base = $hash_base || $hash;
1717                        $hash = git_get_hash_by_path($base, $file_name, "tree");
1718                }
1719                if (!defined $hash_base) {
1720                        $hash_base = $hash;
1721                }
1722        }
1723        $/ = "\0";
1724        open my $fd, "-|", $GIT, "ls-tree", '-z', $hash
1725                or die_error(undef, "Open git-ls-tree failed.");
1726        my @entries = map { chomp; $_ } <$fd>;
1727        close $fd or die_error(undef, "Reading tree failed.");
1728        $/ = "\n";
1729
1730        my $refs = read_info_ref();
1731        my $ref = "";
1732        if (defined $refs->{$hash_base}) {
1733                $ref = " <span class=\"tag\">" . esc_html($refs->{$hash_base}) . "</span>";
1734        }
1735        git_header_html();
1736        my $base_key = "";
1737        my $base = "";
1738        if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
1739                $base_key = ";hb=$hash_base";
1740                git_page_nav('tree','', $hash_base);
1741                git_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
1742        } else {
1743                print "<div class=\"page_nav\">\n";
1744                print "<br/><br/></div>\n";
1745                print "<div class=\"title\">$hash</div>\n";
1746        }
1747        if (defined $file_name) {
1748                $base = esc_html("$file_name/");
1749        }
1750        git_print_page_path($file_name);
1751        print "<div class=\"page_body\">\n";
1752        print "<table cellspacing=\"0\">\n";
1753        my $alternate = 0;
1754        foreach my $line (@entries) {
1755                #'100644        blob    0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa        panic.c'
1756                $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
1757                my $t_mode = $1;
1758                my $t_type = $2;
1759                my $t_hash = $3;
1760                my $t_name = validate_input($4);
1761                if ($alternate) {
1762                        print "<tr class=\"dark\">\n";
1763                } else {
1764                        print "<tr class=\"light\">\n";
1765                }
1766                $alternate ^= 1;
1767                print "<td class=\"mode\">" . mode_str($t_mode) . "</td>\n";
1768                if ($t_type eq "blob") {
1769                        print "<td class=\"list\">" .
1770                              $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)) .
1771                              "</td>\n" .
1772                              "<td class=\"link\">" .
1773                              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$t_hash$base_key;f=$base$t_name")}, "blob") .
1774#                             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;h=$t_hash$base_key;f=$base$t_name")}, "blame") .
1775                              " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;h=$t_hash;hb=$hash_base;f=$base$t_name")}, "history") .
1776                              " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$t_hash;f=$base$t_name")}, "raw") .
1777                              "</td>\n";
1778                } elsif ($t_type eq "tree") {
1779                        print "<td class=\"list\">" .
1780                              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$t_hash$base_key;f=$base$t_name")}, esc_html($t_name)) .
1781                              "</td>\n" .
1782                              "<td class=\"link\">" .
1783                              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$t_hash$base_key;f=$base$t_name")}, "tree") .
1784                              " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;hb=$hash_base;f=$base$t_name")}, "history") .
1785                              "</td>\n";
1786                }
1787                print "</tr>\n";
1788        }
1789        print "</table>\n" .
1790              "</div>";
1791        git_footer_html();
1792}
1793
1794sub git_rss {
1795        # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
1796        open my $fd, "-|", $GIT, "rev-list", "--max-count=150", git_read_head($project)
1797                or die_error(undef, "Open git-rev-list failed.");
1798        my @revlist = map { chomp; $_ } <$fd>;
1799        close $fd or die_error(undef, "Reading rev-list failed.");
1800        print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
1801        print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".
1802              "<rss version=\"2.0\" xmlns:content=\"http://purl.org/rss/1.0/modules/content/\">\n";
1803        print "<channel>\n";
1804        print "<title>$project</title>\n".
1805              "<link>" . esc_html("$my_url?p=$project;a=summary") . "</link>\n".
1806              "<description>$project log</description>\n".
1807              "<language>en</language>\n";
1808
1809        for (my $i = 0; $i <= $#revlist; $i++) {
1810                my $commit = $revlist[$i];
1811                my %co = git_read_commit($commit);
1812                # we read 150, we always show 30 and the ones more recent than 48 hours
1813                if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
1814                        last;
1815                }
1816                my %cd = date_str($co{'committer_epoch'});
1817                open $fd, "-|", $GIT, "diff-tree", '-r', $co{'parent'}, $co{'id'} or next;
1818                my @difftree = map { chomp; $_ } <$fd>;
1819                close $fd or next;
1820                print "<item>\n" .
1821                      "<title>" .
1822                      sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
1823                      "</title>\n" .
1824                      "<author>" . esc_html($co{'author'}) . "</author>\n" .
1825                      "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
1826                      "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
1827                      "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
1828                      "<description>" . esc_html($co{'title'}) . "</description>\n" .
1829                      "<content:encoded>" .
1830                      "<![CDATA[\n";
1831                my $comment = $co{'comment'};
1832                foreach my $line (@$comment) {
1833                        $line = decode("utf8", $line, Encode::FB_DEFAULT);
1834                        print "$line<br/>\n";
1835                }
1836                print "<br/>\n";
1837                foreach my $line (@difftree) {
1838                        if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
1839                                next;
1840                        }
1841                        my $file = validate_input(unquote($7));
1842                        $file = decode("utf8", $file, Encode::FB_DEFAULT);
1843                        print "$file<br/>\n";
1844                }
1845                print "]]>\n" .
1846                      "</content:encoded>\n" .
1847                      "</item>\n";
1848        }
1849        print "</channel></rss>";
1850}
1851
1852sub git_opml {
1853        my @list = git_read_projects();
1854
1855        print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
1856        print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".
1857              "<opml version=\"1.0\">\n".
1858              "<head>".
1859              "  <title>$site_name Git OPML Export</title>\n".
1860              "</head>\n".
1861              "<body>\n".
1862              "<outline text=\"git RSS feeds\">\n";
1863
1864        foreach my $pr (@list) {
1865                my %proj = %$pr;
1866                my $head = git_read_head($proj{'path'});
1867                if (!defined $head) {
1868                        next;
1869                }
1870                $ENV{'GIT_DIR'} = "$projectroot/$proj{'path'}";
1871                my %co = git_read_commit($head);
1872                if (!%co) {
1873                        next;
1874                }
1875
1876                my $path = esc_html(chop_str($proj{'path'}, 25, 5));
1877                my $rss  = "$my_url?p=$proj{'path'};a=rss";
1878                my $html = "$my_url?p=$proj{'path'};a=summary";
1879                print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
1880        }
1881        print "</outline>\n".
1882              "</body>\n".
1883              "</opml>\n";
1884}
1885
1886sub git_log {
1887        my $head = git_read_head($project);
1888        if (!defined $hash) {
1889                $hash = $head;
1890        }
1891        if (!defined $page) {
1892                $page = 0;
1893        }
1894        my $refs = read_info_ref();
1895
1896        my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
1897        open my $fd, "-|", $GIT, "rev-list", $limit, $hash
1898                or die_error(undef, "Open git-rev-list failed.");
1899        my @revlist = map { chomp; $_ } <$fd>;
1900        close $fd;
1901
1902        my $paging_nav = git_get_paging_nav('log', $hash, $head, $page, $#revlist);
1903
1904        git_header_html();
1905        git_page_nav('log','', $hash,undef,undef, $paging_nav);
1906
1907        if (!@revlist) {
1908                my %co = git_read_commit($hash);
1909
1910                git_header_div('summary', $project);
1911                print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
1912        }
1913        for (my $i = ($page * 100); $i <= $#revlist; $i++) {
1914                my $commit = $revlist[$i];
1915                my $ref = "";
1916                if (defined $refs->{$commit}) {
1917                        $ref = " <span class=\"tag\">" . esc_html($refs->{$commit}) . "</span>";
1918                }
1919                my %co = git_read_commit($commit);
1920                next if !%co;
1921                my %ad = date_str($co{'author_epoch'});
1922                git_header_div('commit',
1923                                                                         "<span class=\"age\">$co{'age_string'}</span>" .
1924                                                                         esc_html($co{'title'}) . $ref,
1925                                                                         $commit);
1926                print "<div class=\"title_text\">\n" .
1927                      "<div class=\"log_link\">\n" .
1928                      $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") .
1929                      " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$commit")}, "commitdiff") .
1930                      "<br/>\n" .
1931                      "</div>\n" .
1932                      "<i>" . esc_html($co{'author_name'}) .  " [$ad{'rfc2822'}]</i><br/>\n" .
1933                      "</div>\n" .
1934                      "<div class=\"log_body\">\n";
1935                my $comment = $co{'comment'};
1936                my $empty = 0;
1937                foreach my $line (@$comment) {
1938                        if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1939                                next;
1940                        }
1941                        if ($line eq "") {
1942                                if ($empty) {
1943                                        next;
1944                                }
1945                                $empty = 1;
1946                        } else {
1947                                $empty = 0;
1948                        }
1949                        print format_log_line_html($line) . "<br/>\n";
1950                }
1951                if (!$empty) {
1952                        print "<br/>\n";
1953                }
1954                print "</div>\n";
1955        }
1956        git_footer_html();
1957}
1958
1959sub git_commit {
1960        my %co = git_read_commit($hash);
1961        if (!%co) {
1962                die_error(undef, "Unknown commit object.");
1963        }
1964        my %ad = date_str($co{'author_epoch'}, $co{'author_tz'});
1965        my %cd = date_str($co{'committer_epoch'}, $co{'committer_tz'});
1966
1967        my $parent = $co{'parent'};
1968        if (!defined $parent) {
1969                $parent = "--root";
1970        }
1971        open my $fd, "-|", $GIT, "diff-tree", '-r', '-M', $parent, $hash
1972                or die_error(undef, "Open git-diff-tree failed.");
1973        my @difftree = map { chomp; $_ } <$fd>;
1974        close $fd or die_error(undef, "Reading git-diff-tree failed.");
1975
1976        # non-textual hash id's can be cached
1977        my $expires;
1978        if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
1979                $expires = "+1d";
1980        }
1981        my $refs = read_info_ref();
1982        my $ref = "";
1983        if (defined $refs->{$co{'id'}}) {
1984                $ref = " <span class=\"tag\">" . esc_html($refs->{$co{'id'}}) . "</span>";
1985        }
1986        git_header_html(undef, $expires);
1987        my $formats_nav = '';
1988        if (defined $file_name && defined $co{'parent'}) {
1989                my $parent = $co{'parent'};
1990                $formats_nav .= $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;hb=$parent;f=$file_name")}, "blame");
1991        }
1992        git_page_nav('commit', defined $co{'parent'} ? '' : 'commitdiff',
1993                                                         $hash, $co{'tree'}, $hash,
1994                                                         $formats_nav);
1995
1996        if (defined $co{'parent'}) {
1997                git_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
1998        } else {
1999                git_header_div('tree', esc_html($co{'title'}), $co{'tree'}, $hash);
2000        }
2001        print "<div class=\"title_text\">\n" .
2002              "<table cellspacing=\"0\">\n";
2003        print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
2004              "<tr>" .
2005              "<td></td><td> $ad{'rfc2822'}";
2006        if ($ad{'hour_local'} < 6) {
2007                printf(" (<span class=\"atnight\">%02d:%02d</span> %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2008        } else {
2009                printf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2010        }
2011        print "</td>" .
2012              "</tr>\n";
2013        print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
2014        print "<tr><td></td><td> $cd{'rfc2822'}" . sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) . "</td></tr>\n";
2015        print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
2016        print "<tr>" .
2017              "<td>tree</td>" .
2018              "<td class=\"sha1\">" .
2019              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash"), class => "list"}, $co{'tree'}) .
2020              "</td>" .
2021              "<td class=\"link\">" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash")}, "tree") .
2022              "</td>" .
2023              "</tr>\n";
2024        my $parents = $co{'parents'};
2025        foreach my $par (@$parents) {
2026                print "<tr>" .
2027                      "<td>parent</td>" .
2028                      "<td class=\"sha1\">" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$par"), class => "list"}, $par) . "</td>" .
2029                      "<td class=\"link\">" .
2030                      $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$par")}, "commit") .
2031                      " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash;hp=$par")}, "commitdiff") .
2032                      "</td>" .
2033                      "</tr>\n";
2034        }
2035        print "</table>".
2036              "</div>\n";
2037        print "<div class=\"page_body\">\n";
2038        my $comment = $co{'comment'};
2039        my $empty = 0;
2040        my $signed = 0;
2041        foreach my $line (@$comment) {
2042                # print only one empty line
2043                if ($line eq "") {
2044                        if ($empty || $signed) {
2045                                next;
2046                        }
2047                        $empty = 1;
2048                } else {
2049                        $empty = 0;
2050                }
2051                if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
2052                        $signed = 1;
2053                        print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
2054                } else {
2055                        $signed = 0;
2056                        print format_log_line_html($line) . "<br/>\n";
2057                }
2058        }
2059        print "</div>\n";
2060        print "<div class=\"list_head\">\n";
2061        if ($#difftree > 10) {
2062                print(($#difftree + 1) . " files changed:\n");
2063        }
2064        print "</div>\n";
2065        print "<table class=\"diff_tree\">\n";
2066        my $alternate = 0;
2067        foreach my $line (@difftree) {
2068                # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M      ls-files.c'
2069                # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M      rev-tree.c'
2070                if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
2071                        next;
2072                }
2073                my $from_mode = $1;
2074                my $to_mode = $2;
2075                my $from_id = $3;
2076                my $to_id = $4;
2077                my $status = $5;
2078                my $similarity = $6;
2079                my $file = validate_input(unquote($7));
2080                if ($alternate) {
2081                        print "<tr class=\"dark\">\n";
2082                } else {
2083                        print "<tr class=\"light\">\n";
2084                }
2085                $alternate ^= 1;
2086                if ($status eq "A") {
2087                        my $mode_chng = "";
2088                        if (S_ISREG(oct $to_mode)) {
2089                                $mode_chng = sprintf(" with mode: %04o", (oct $to_mode) & 0777);
2090                        }
2091                        print "<td>" .
2092                              $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" .
2093                              "<td><span class=\"file_status new\">[new " . file_type($to_mode) . "$mode_chng]</span></td>\n" .
2094                              "<td class=\"link\">" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file")}, "blob") . "</td>\n";
2095                } elsif ($status eq "D") {
2096                        print "<td>" .
2097                              $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" .
2098                              "<td><span class=\"file_status deleted\">[deleted " . file_type($from_mode). "]</span></td>\n" .
2099                              "<td class=\"link\">" .
2100                              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$hash;f=$file")}, "blob") .
2101                              " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;hb=$hash;f=$file")}, "history") .
2102                              "</td>\n"
2103                } elsif ($status eq "M" || $status eq "T") {
2104                        my $mode_chnge = "";
2105                        if ($from_mode != $to_mode) {
2106                                $mode_chnge = " <span class=\"file_status mode_chnge\">[changed";
2107                                if (((oct $from_mode) & S_IFMT) != ((oct $to_mode) & S_IFMT)) {
2108                                        $mode_chnge .= " from " . file_type($from_mode) . " to " . file_type($to_mode);
2109                                }
2110                                if (((oct $from_mode) & 0777) != ((oct $to_mode) & 0777)) {
2111                                        if (S_ISREG($from_mode) && S_ISREG($to_mode)) {
2112                                                $mode_chnge .= sprintf(" mode: %04o->%04o", (oct $from_mode) & 0777, (oct $to_mode) & 0777);
2113                                        } elsif (S_ISREG($to_mode)) {
2114                                                $mode_chnge .= sprintf(" mode: %04o", (oct $to_mode) & 0777);
2115                                        }
2116                                }
2117                                $mode_chnge .= "]</span>\n";
2118                        }
2119                        print "<td>";
2120                        if ($to_id ne $from_id) {
2121                                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));
2122                        } else {
2123                                print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file"), -class => "list"}, esc_html($file));
2124                        }
2125                        print "</td>\n" .
2126                              "<td>$mode_chnge</td>\n" .
2127                              "<td class=\"link\">";
2128                        print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file")}, "blob");
2129                        if ($to_id ne $from_id) {
2130                                print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$file")}, "diff");
2131                        }
2132                        print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;hb=$hash;f=$file")}, "history") . "\n";
2133                        print "</td>\n";
2134                } elsif ($status eq "R") {
2135                        my ($from_file, $to_file) = split "\t", $file;
2136                        my $mode_chng = "";
2137                        if ($from_mode != $to_mode) {
2138                                $mode_chng = sprintf(", mode: %04o", (oct $to_mode) & 0777);
2139                        }
2140                        print "<td>" .
2141                              $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" .
2142                              "<td><span class=\"file_status moved\">[moved from " .
2143                              $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)) .
2144                              " with " . (int $similarity) . "% similarity$mode_chng]</span></td>\n" .
2145                              "<td class=\"link\">" .
2146                              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$to_file")}, "blob");
2147                        if ($to_id ne $from_id) {
2148                                print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$to_file")}, "diff");
2149                        }
2150                        print "</td>\n";
2151                }
2152                print "</tr>\n";
2153        }
2154        print "</table>\n";
2155        git_footer_html();
2156}
2157
2158sub git_blobdiff {
2159        mkdir($git_temp, 0700);
2160        git_header_html();
2161        if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
2162                my $formats_nav =
2163                        $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff_plain;h=$hash;hp=$hash_parent")}, "plain");
2164                git_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2165                git_header_div('commit', esc_html($co{'title'}), $hash_base);
2166        } else {
2167                print "<div class=\"page_nav\">\n" .
2168                      "<br/><br/></div>\n" .
2169                      "<div class=\"title\">$hash vs $hash_parent</div>\n";
2170        }
2171        git_print_page_path($file_name, "blob");
2172        print "<div class=\"page_body\">\n" .
2173              "<div class=\"diff_info\">blob:" .
2174              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$hash_parent;hb=$hash_base;f=$file_name")}, $hash_parent) .
2175              " -> blob:" .
2176              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$hash;hb=$hash_base;f=$file_name")}, $hash) .
2177              "</div>\n";
2178        git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash);
2179        print "</div>";
2180        git_footer_html();
2181}
2182
2183sub git_blobdiff_plain {
2184        mkdir($git_temp, 0700);
2185        print $cgi->header(-type => "text/plain", -charset => 'utf-8');
2186        git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash, "plain");
2187}
2188
2189sub git_commitdiff {
2190        mkdir($git_temp, 0700);
2191        my %co = git_read_commit($hash);
2192        if (!%co) {
2193                die_error(undef, "Unknown commit object.");
2194        }
2195        if (!defined $hash_parent) {
2196                $hash_parent = $co{'parent'};
2197        }
2198        open my $fd, "-|", $GIT, "diff-tree", '-r', $hash_parent, $hash
2199                or die_error(undef, "Open git-diff-tree failed.");
2200        my @difftree = map { chomp; $_ } <$fd>;
2201        close $fd or die_error(undef, "Reading diff-tree failed.");
2202
2203        # non-textual hash id's can be cached
2204        my $expires;
2205        if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2206                $expires = "+1d";
2207        }
2208        my $refs = read_info_ref();
2209        my $ref = "";
2210        if (defined $refs->{$co{'id'}}) {
2211                $ref = " <span class=\"tag\">" . esc_html($refs->{$co{'id'}}) . "</span>";
2212        }
2213        git_header_html(undef, $expires);
2214        my $formats_nav =
2215                $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff_plain;h=$hash;hp=$hash_parent")}, "plain");
2216        git_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
2217        git_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
2218        print "<div class=\"page_body\">\n";
2219        my $comment = $co{'comment'};
2220        my $empty = 0;
2221        my $signed = 0;
2222        my @log = @$comment;
2223        # remove first and empty lines after that
2224        shift @log;
2225        while (defined $log[0] && $log[0] eq "") {
2226                shift @log;
2227        }
2228        foreach my $line (@log) {
2229                if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
2230                        next;
2231                }
2232                if ($line eq "") {
2233                        if ($empty) {
2234                                next;
2235                        }
2236                        $empty = 1;
2237                } else {
2238                        $empty = 0;
2239                }
2240                print format_log_line_html($line) . "<br/>\n";
2241        }
2242        print "<br/>\n";
2243        foreach my $line (@difftree) {
2244                # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M      ls-files.c'
2245                # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M      rev-tree.c'
2246                $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/;
2247                my $from_mode = $1;
2248                my $to_mode = $2;
2249                my $from_id = $3;
2250                my $to_id = $4;
2251                my $status = $5;
2252                my $file = validate_input(unquote($6));
2253                if ($status eq "A") {
2254                        print "<div class=\"diff_info\">" . file_type($to_mode) . ":" .
2255                              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file")}, $to_id) . "(new)" .
2256                              "</div>\n";
2257                        git_diff_print(undef, "/dev/null", $to_id, "b/$file");
2258                } elsif ($status eq "D") {
2259                        print "<div class=\"diff_info\">" . file_type($from_mode) . ":" .
2260                              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$hash;f=$file")}, $from_id) . "(deleted)" .
2261                              "</div>\n";
2262                        git_diff_print($from_id, "a/$file", undef, "/dev/null");
2263                } elsif ($status eq "M") {
2264                        if ($from_id ne $to_id) {
2265                                print "<div class=\"diff_info\">" .
2266                                      file_type($from_mode) . ":" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$hash;f=$file")}, $from_id) .
2267                                      " -> " .
2268                                      file_type($to_mode) . ":" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file")}, $to_id);
2269                                print "</div>\n";
2270                                git_diff_print($from_id, "a/$file",  $to_id, "b/$file");
2271                        }
2272                }
2273        }
2274        print "<br/>\n" .
2275              "</div>";
2276        git_footer_html();
2277}
2278
2279sub git_commitdiff_plain {
2280        mkdir($git_temp, 0700);
2281        open my $fd, "-|", $GIT, "diff-tree", '-r', $hash_parent, $hash
2282                or die_error(undef, "Open git-diff-tree failed.");
2283        my @difftree = map { chomp; $_ } <$fd>;
2284        close $fd or die_error(undef, "Reading diff-tree failed.");
2285
2286        # try to figure out the next tag after this commit
2287        my $tagname;
2288        my $refs = read_info_ref("tags");
2289        open $fd, "-|", $GIT, "rev-list", "HEAD";
2290        my @commits = map { chomp; $_ } <$fd>;
2291        close $fd;
2292        foreach my $commit (@commits) {
2293                if (defined $refs->{$commit}) {
2294                        $tagname = $refs->{$commit}
2295                }
2296                if ($commit eq $hash) {
2297                        last;
2298                }
2299        }
2300
2301        print $cgi->header(-type => "text/plain", -charset => 'utf-8', '-content-disposition' => "inline; filename=\"git-$hash.patch\"");
2302        my %co = git_read_commit($hash);
2303        my %ad = date_str($co{'author_epoch'}, $co{'author_tz'});
2304        my $comment = $co{'comment'};
2305        print "From: $co{'author'}\n" .
2306              "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n".
2307              "Subject: $co{'title'}\n";
2308        if (defined $tagname) {
2309                print "X-Git-Tag: $tagname\n";
2310        }
2311        print "X-Git-Url: $my_url?p=$project;a=commitdiff;h=$hash\n" .
2312              "\n";
2313
2314        foreach my $line (@$comment) {;
2315                print "$line\n";
2316        }
2317        print "---\n\n";
2318
2319        foreach my $line (@difftree) {
2320                $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/;
2321                my $from_id = $3;
2322                my $to_id = $4;
2323                my $status = $5;
2324                my $file = $6;
2325                if ($status eq "A") {
2326                        git_diff_print(undef, "/dev/null", $to_id, "b/$file", "plain");
2327                } elsif ($status eq "D") {
2328                        git_diff_print($from_id, "a/$file", undef, "/dev/null", "plain");
2329                } elsif ($status eq "M") {
2330                        git_diff_print($from_id, "a/$file",  $to_id, "b/$file", "plain");
2331                }
2332        }
2333}
2334
2335sub git_history {
2336        if (!defined $hash_base) {
2337                $hash_base = git_read_head($project);
2338        }
2339        my $ftype;
2340        my %co = git_read_commit($hash_base);
2341        if (!%co) {
2342                die_error(undef, "Unknown commit object.");
2343        }
2344        my $refs = read_info_ref();
2345        git_header_html();
2346        git_page_nav('','', $hash_base,$co{'tree'},$hash_base);
2347        git_header_div('commit', esc_html($co{'title'}), $hash_base);
2348        if (!defined $hash && defined $file_name) {
2349                $hash = git_get_hash_by_path($hash_base, $file_name);
2350        }
2351        if (defined $hash) {
2352                $ftype = git_get_type($hash);
2353        }
2354        git_print_page_path($file_name, $ftype);
2355
2356        open my $fd, "-|",
2357                $GIT, "rev-list", "--full-history", $hash_base, "--", "\'$file_name\'";
2358        print "<table cellspacing=\"0\">\n";
2359        my $alternate = 0;
2360        while (my $line = <$fd>) {
2361                if ($line =~ m/^([0-9a-fA-F]{40})/){
2362                        my $commit = $1;
2363                        my %co = git_read_commit($commit);
2364                        if (!%co) {
2365                                next;
2366                        }
2367                        my $ref = "";
2368                        if (defined $refs->{$commit}) {
2369                                $ref = " <span class=\"tag\">" . esc_html($refs->{$commit}) . "</span>";
2370                        }
2371                        if ($alternate) {
2372                                print "<tr class=\"dark\">\n";
2373                        } else {
2374                                print "<tr class=\"light\">\n";
2375                        }
2376                        $alternate ^= 1;
2377                        print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2378                              "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
2379                              "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "list"}, "<b>" .
2380                              esc_html(chop_str($co{'title'}, 50)) . "$ref</b>") . "</td>\n" .
2381                              "<td class=\"link\">" .
2382                              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") .
2383                              " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$commit")}, "commitdiff") .
2384                              " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;hb=$commit;f=$file_name")}, "blob");
2385                        my $blob = git_get_hash_by_path($hash_base, $file_name);
2386                        my $blob_parent = git_get_hash_by_path($commit, $file_name);
2387                        if (defined $blob && defined $blob_parent && $blob ne $blob_parent) {
2388                                print " | " .
2389                                $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff;h=$blob;hp=$blob_parent;hb=$commit;f=$file_name")},
2390                                "diff to current");
2391                        }
2392                        print "</td>\n" .
2393                              "</tr>\n";
2394                }
2395        }
2396        print "</table>\n";
2397        close $fd;
2398        git_footer_html();
2399}
2400
2401sub git_search {
2402        if (!defined $searchtext) {
2403                die_error("", "Text field empty.");
2404        }
2405        if (!defined $hash) {
2406                $hash = git_read_head($project);
2407        }
2408        my %co = git_read_commit($hash);
2409        if (!%co) {
2410                die_error(undef, "Unknown commit object.");
2411        }
2412        # pickaxe may take all resources of your box and run for several minutes
2413        # with every query - so decide by yourself how public you make this feature :)
2414        my $commit_search = 1;
2415        my $author_search = 0;
2416        my $committer_search = 0;
2417        my $pickaxe_search = 0;
2418        if ($searchtext =~ s/^author\\://i) {
2419                $author_search = 1;
2420        } elsif ($searchtext =~ s/^committer\\://i) {
2421                $committer_search = 1;
2422        } elsif ($searchtext =~ s/^pickaxe\\://i) {
2423                $commit_search = 0;
2424                $pickaxe_search = 1;
2425        }
2426        git_header_html();
2427        git_page_nav('','', $hash,$co{'tree'},$hash);
2428        git_header_div('commit', esc_html($co{'title'}), $hash);
2429
2430        print "<table cellspacing=\"0\">\n";
2431        my $alternate = 0;
2432        if ($commit_search) {
2433                $/ = "\0";
2434                open my $fd, "-|", $GIT, "rev-list", "--header", "--parents", $hash or next;
2435                while (my $commit_text = <$fd>) {
2436                        if (!grep m/$searchtext/i, $commit_text) {
2437                                next;
2438                        }
2439                        if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
2440                                next;
2441                        }
2442                        if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
2443                                next;
2444                        }
2445                        my @commit_lines = split "\n", $commit_text;
2446                        my %co = git_read_commit(undef, \@commit_lines);
2447                        if (!%co) {
2448                                next;
2449                        }
2450                        if ($alternate) {
2451                                print "<tr class=\"dark\">\n";
2452                        } else {
2453                                print "<tr class=\"light\">\n";
2454                        }
2455                        $alternate ^= 1;
2456                        print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2457                              "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
2458                              "<td>" .
2459                              $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/>");
2460                        my $comment = $co{'comment'};
2461                        foreach my $line (@$comment) {
2462                                if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
2463                                        my $lead = esc_html($1) || "";
2464                                        $lead = chop_str($lead, 30, 10);
2465                                        my $match = esc_html($2) || "";
2466                                        my $trail = esc_html($3) || "";
2467                                        $trail = chop_str($trail, 30, 10);
2468                                        my $text = "$lead<span class=\"match\">$match</span>$trail";
2469                                        print chop_str($text, 80, 5) . "<br/>\n";
2470                                }
2471                        }
2472                        print "</td>\n" .
2473                              "<td class=\"link\">" .
2474                              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$co{'id'}")}, "commit") .
2475                              " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$co{'id'}")}, "tree");
2476                        print "</td>\n" .
2477                              "</tr>\n";
2478                }
2479                close $fd;
2480        }
2481
2482        if ($pickaxe_search) {
2483                $/ = "\n";
2484                open my $fd, "-|", "$GIT rev-list $hash | $GIT diff-tree -r --stdin -S\'$searchtext\'";
2485                undef %co;
2486                my @files;
2487                while (my $line = <$fd>) {
2488                        if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
2489                                my %set;
2490                                $set{'file'} = $6;
2491                                $set{'from_id'} = $3;
2492                                $set{'to_id'} = $4;
2493                                $set{'id'} = $set{'to_id'};
2494                                if ($set{'id'} =~ m/0{40}/) {
2495                                        $set{'id'} = $set{'from_id'};
2496                                }
2497                                if ($set{'id'} =~ m/0{40}/) {
2498                                        next;
2499                                }
2500                                push @files, \%set;
2501                        } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
2502                                if (%co) {
2503                                        if ($alternate) {
2504                                                print "<tr class=\"dark\">\n";
2505                                        } else {
2506                                                print "<tr class=\"light\">\n";
2507                                        }
2508                                        $alternate ^= 1;
2509                                        print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2510                                              "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
2511                                              "<td>" .
2512                                              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$co{'id'}"), -class => "list"}, "<b>" .
2513                                              esc_html(chop_str($co{'title'}, 50)) . "</b><br/>");
2514                                        while (my $setref = shift @files) {
2515                                                my %set = %$setref;
2516                                                print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$set{'id'};hb=$co{'id'};f=$set{'file'}"), class => "list"},
2517                                                      "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
2518                                                      "<br/>\n";
2519                                        }
2520                                        print "</td>\n" .
2521                                              "<td class=\"link\">" .
2522                                              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$co{'id'}")}, "commit") .
2523                                              " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$co{'id'}")}, "tree");
2524                                        print "</td>\n" .
2525                                              "</tr>\n";
2526                                }
2527                                %co = git_read_commit($1);
2528                        }
2529                }
2530                close $fd;
2531        }
2532        print "</table>\n";
2533        git_footer_html();
2534}
2535
2536sub git_shortlog {
2537        my $head = git_read_head($project);
2538        if (!defined $hash) {
2539                $hash = $head;
2540        }
2541        if (!defined $page) {
2542                $page = 0;
2543        }
2544        my $refs = read_info_ref();
2545
2546        my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
2547        open my $fd, "-|", $GIT, "rev-list", $limit, $hash
2548                or die_error(undef, "Open git-rev-list failed.");
2549        my @revlist = map { chomp; $_ } <$fd>;
2550        close $fd;
2551
2552        my $paging_nav = git_get_paging_nav('shortlog', $hash, $head, $page, $#revlist);
2553
2554        git_header_html();
2555        git_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
2556        git_header_div('summary', $project);
2557
2558        print "<table cellspacing=\"0\">\n";
2559        my $alternate = 0;
2560        for (my $i = ($page * 100); $i <= $#revlist; $i++) {
2561                my $commit = $revlist[$i];
2562                my $ref = "";
2563                if (defined $refs->{$commit}) {
2564                        $ref = " <span class=\"tag\">" . esc_html($refs->{$commit}) . "</span>";
2565                }
2566                my %co = git_read_commit($commit);
2567                my %ad = date_str($co{'author_epoch'});
2568                if ($alternate) {
2569                        print "<tr class=\"dark\">\n";
2570                } else {
2571                        print "<tr class=\"light\">\n";
2572                }
2573                $alternate ^= 1;
2574                print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2575                      "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
2576                      "<td>";
2577                if (length($co{'title_short'}) < length($co{'title'})) {
2578                        print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "list", -title => "$co{'title'}"},
2579                              "<b>" . esc_html($co{'title_short'}) . "$ref</b>");
2580                } else {
2581                        print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "list"},
2582                              "<b>" . esc_html($co{'title_short'}) . "$ref</b>");
2583                }
2584                print "</td>\n" .
2585                      "<td class=\"link\">" .
2586                      $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") .
2587                      " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$commit")}, "commitdiff") .
2588                      "</td>\n" .
2589                      "</tr>";
2590        }
2591        if ($#revlist >= (100 * ($page+1)-1)) {
2592                print "<tr>\n" .
2593                      "<td>" .
2594                      $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash;pg=" . ($page+1)), -title => "Alt-n"}, "next") .
2595                      "</td>\n" .
2596                      "</tr>\n";
2597        }
2598        print "</table\n>";
2599        git_footer_html();
2600}