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