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