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