gitweb / gitweb.perlon commit gitweb: Remove git_diff_print subroutine (8cce8e3)
   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();
  18use File::Basename qw(basename);
  19binmode STDOUT, ':utf8';
  20
  21our $cgi = new CGI;
  22our $version = "++GIT_VERSION++";
  23our $my_url = $cgi->url();
  24our $my_uri = $cgi->url(-absolute => 1);
  25
  26# core git executable to use
  27# this can just be "git" if your webserver has a sensible PATH
  28our $GIT = "++GIT_BINDIR++/git";
  29
  30# absolute fs-path which will be prepended to the project path
  31#our $projectroot = "/pub/scm";
  32our $projectroot = "++GITWEB_PROJECTROOT++";
  33
  34# location for temporary files needed for diffs
  35our $git_temp = "/tmp/gitweb";
  36
  37# target of the home link on top of all pages
  38our $home_link = $my_uri || "/";
  39
  40# string of the home link on top of all pages
  41our $home_link_str = "++GITWEB_HOME_LINK_STR++";
  42
  43# name of your site or organization to appear in page titles
  44# replace this with something more descriptive for clearer bookmarks
  45our $site_name = "++GITWEB_SITENAME++" || $ENV{'SERVER_NAME'} || "Untitled";
  46
  47# html text to include at home page
  48our $home_text = "++GITWEB_HOMETEXT++";
  49
  50# URI of default stylesheet
  51our $stylesheet = "++GITWEB_CSS++";
  52# URI of GIT logo
  53our $logo = "++GITWEB_LOGO++";
  54
  55# source of projects list
  56our $projects_list = "++GITWEB_LIST++";
  57
  58# list of git base URLs used for URL to where fetch project from,
  59# i.e. full URL is "$git_base_url/$project"
  60our @git_base_url_list = ("++GITWEB_BASE_URL++");
  61
  62# default blob_plain mimetype and default charset for text/plain blob
  63our $default_blob_plain_mimetype = 'text/plain';
  64our $default_text_plain_charset  = undef;
  65
  66# file to use for guessing MIME types before trying /etc/mime.types
  67# (relative to the current git repository)
  68our $mimetypes_file = undef;
  69
  70# You define site-wide feature defaults here; override them with
  71# $GITWEB_CONFIG as necessary.
  72our %feature = (
  73        # feature => {'sub' => feature-sub, 'override' => allow-override, 'default' => [ default options...]
  74        # if feature is overridable, feature-sub will be called with default options;
  75        # return value indicates if to enable specified feature
  76
  77        'blame' => {
  78                'sub' => \&feature_blame,
  79                'override' => 0,
  80                'default' => [0]},
  81
  82        'snapshot' => {
  83                'sub' => \&feature_snapshot,
  84                'override' => 0,
  85                #         => [content-encoding, suffix, program]
  86                'default' => ['x-gzip', 'gz', 'gzip']},
  87);
  88
  89sub gitweb_check_feature {
  90        my ($name) = @_;
  91        return undef unless exists $feature{$name};
  92        my ($sub, $override, @defaults) = (
  93                $feature{$name}{'sub'},
  94                $feature{$name}{'override'},
  95                @{$feature{$name}{'default'}});
  96        if (!$override) { return @defaults; }
  97        return $sub->(@defaults);
  98}
  99
 100# To enable system wide have in $GITWEB_CONFIG
 101# $feature{'blame'}{'default'} =  [1];
 102# To have project specific config enable override in  $GITWEB_CONFIG
 103# $feature{'blame'}{'override'} =  1;
 104# and in project config gitweb.blame = 0|1;
 105
 106sub feature_blame {
 107        my ($val) = git_get_project_config('blame', '--bool');
 108
 109        if ($val eq 'true') {
 110                return 1;
 111        } elsif ($val eq 'false') {
 112                return 0;
 113        }
 114
 115        return $_[0];
 116}
 117
 118# To disable system wide have in $GITWEB_CONFIG
 119# $feature{'snapshot'}{'default'} =  [undef];
 120# To have project specific config enable override in  $GITWEB_CONFIG
 121# $feature{'blame'}{'override'} =  1;
 122# and in project config  gitweb.snapshot = none|gzip|bzip2
 123
 124sub feature_snapshot {
 125        my ($ctype, $suffix, $command) = @_;
 126
 127        my ($val) = git_get_project_config('snapshot');
 128
 129        if ($val eq 'gzip') {
 130                return ('x-gzip', 'gz', 'gzip');
 131        } elsif ($val eq 'bzip2') {
 132                return ('x-bzip2', 'bz2', 'bzip2');
 133        } elsif ($val eq 'none') {
 134                return ();
 135        }
 136
 137        return ($ctype, $suffix, $command);
 138}
 139
 140our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
 141require $GITWEB_CONFIG if -e $GITWEB_CONFIG;
 142
 143# version of the core git binary
 144our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
 145
 146$projects_list ||= $projectroot;
 147if (! -d $git_temp) {
 148        mkdir($git_temp, 0700) || die_error(undef, "Couldn't mkdir $git_temp");
 149}
 150
 151# ======================================================================
 152# input validation and dispatch
 153our $action = $cgi->param('a');
 154if (defined $action) {
 155        if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
 156                die_error(undef, "Invalid action parameter");
 157        }
 158}
 159
 160our $project = ($cgi->param('p') || $ENV{'PATH_INFO'});
 161if (defined $project) {
 162        $project =~ s|^/||;
 163        $project =~ s|/$||;
 164        $project = undef unless $project;
 165}
 166if (defined $project) {
 167        if (!validate_input($project)) {
 168                die_error(undef, "Invalid project parameter");
 169        }
 170        if (!(-d "$projectroot/$project")) {
 171                die_error(undef, "No such directory");
 172        }
 173        if (!(-e "$projectroot/$project/HEAD")) {
 174                die_error(undef, "No such project");
 175        }
 176        $ENV{'GIT_DIR'} = "$projectroot/$project";
 177}
 178
 179our $file_name = $cgi->param('f');
 180if (defined $file_name) {
 181        if (!validate_input($file_name)) {
 182                die_error(undef, "Invalid file parameter");
 183        }
 184}
 185
 186our $file_parent = $cgi->param('fp');
 187if (defined $file_parent) {
 188        if (!validate_input($file_parent)) {
 189                die_error(undef, "Invalid file parent parameter");
 190        }
 191}
 192
 193our $hash = $cgi->param('h');
 194if (defined $hash) {
 195        if (!validate_input($hash)) {
 196                die_error(undef, "Invalid hash parameter");
 197        }
 198}
 199
 200our $hash_parent = $cgi->param('hp');
 201if (defined $hash_parent) {
 202        if (!validate_input($hash_parent)) {
 203                die_error(undef, "Invalid hash parent parameter");
 204        }
 205}
 206
 207our $hash_base = $cgi->param('hb');
 208if (defined $hash_base) {
 209        if (!validate_input($hash_base)) {
 210                die_error(undef, "Invalid hash base parameter");
 211        }
 212}
 213
 214our $hash_parent_base = $cgi->param('hpb');
 215if (defined $hash_parent_base) {
 216        if (!validate_input($hash_parent_base)) {
 217                die_error(undef, "Invalid hash parent base parameter");
 218        }
 219}
 220
 221our $page = $cgi->param('pg');
 222if (defined $page) {
 223        if ($page =~ m/[^0-9]$/) {
 224                die_error(undef, "Invalid page parameter");
 225        }
 226}
 227
 228our $searchtext = $cgi->param('s');
 229if (defined $searchtext) {
 230        if ($searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
 231                die_error(undef, "Invalid search parameter");
 232        }
 233        $searchtext = quotemeta $searchtext;
 234}
 235
 236# dispatch
 237my %actions = (
 238        "blame" => \&git_blame2,
 239        "blobdiff" => \&git_blobdiff,
 240        "blobdiff_plain" => \&git_blobdiff_plain,
 241        "blob" => \&git_blob,
 242        "blob_plain" => \&git_blob_plain,
 243        "commitdiff" => \&git_commitdiff,
 244        "commitdiff_plain" => \&git_commitdiff_plain,
 245        "commit" => \&git_commit,
 246        "heads" => \&git_heads,
 247        "history" => \&git_history,
 248        "log" => \&git_log,
 249        "rss" => \&git_rss,
 250        "search" => \&git_search,
 251        "shortlog" => \&git_shortlog,
 252        "summary" => \&git_summary,
 253        "tag" => \&git_tag,
 254        "tags" => \&git_tags,
 255        "tree" => \&git_tree,
 256        "snapshot" => \&git_snapshot,
 257        # those below don't need $project
 258        "opml" => \&git_opml,
 259        "project_list" => \&git_project_list,
 260);
 261
 262if (defined $project) {
 263        $action ||= 'summary';
 264} else {
 265        $action ||= 'project_list';
 266}
 267if (!defined($actions{$action})) {
 268        die_error(undef, "Unknown action");
 269}
 270$actions{$action}->();
 271exit;
 272
 273## ======================================================================
 274## action links
 275
 276sub href(%) {
 277        my %params = @_;
 278
 279        my @mapping = (
 280                project => "p",
 281                action => "a",
 282                file_name => "f",
 283                file_parent => "fp",
 284                hash => "h",
 285                hash_parent => "hp",
 286                hash_base => "hb",
 287                hash_parent_base => "hpb",
 288                page => "pg",
 289                searchtext => "s",
 290        );
 291        my %mapping = @mapping;
 292
 293        $params{"project"} ||= $project;
 294
 295        my @result = ();
 296        for (my $i = 0; $i < @mapping; $i += 2) {
 297                my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
 298                if (defined $params{$name}) {
 299                        push @result, $symbol . "=" . esc_param($params{$name});
 300                }
 301        }
 302        return "$my_uri?" . join(';', @result);
 303}
 304
 305
 306## ======================================================================
 307## validation, quoting/unquoting and escaping
 308
 309sub validate_input {
 310        my $input = shift;
 311
 312        if ($input =~ m/^[0-9a-fA-F]{40}$/) {
 313                return $input;
 314        }
 315        if ($input =~ m/(^|\/)(|\.|\.\.)($|\/)/) {
 316                return undef;
 317        }
 318        if ($input =~ m/[^a-zA-Z0-9_\x80-\xff\ \t\.\/\-\+\#\~\%]/) {
 319                return undef;
 320        }
 321        return $input;
 322}
 323
 324# quote unsafe chars, but keep the slash, even when it's not
 325# correct, but quoted slashes look too horrible in bookmarks
 326sub esc_param {
 327        my $str = shift;
 328        $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
 329        $str =~ s/\+/%2B/g;
 330        $str =~ s/ /\+/g;
 331        return $str;
 332}
 333
 334# replace invalid utf8 character with SUBSTITUTION sequence
 335sub esc_html {
 336        my $str = shift;
 337        $str = decode("utf8", $str, Encode::FB_DEFAULT);
 338        $str = escapeHTML($str);
 339        $str =~ s/\014/^L/g; # escape FORM FEED (FF) character (e.g. in COPYING file)
 340        return $str;
 341}
 342
 343# git may return quoted and escaped filenames
 344sub unquote {
 345        my $str = shift;
 346        if ($str =~ m/^"(.*)"$/) {
 347                $str = $1;
 348                $str =~ s/\\([0-7]{1,3})/chr(oct($1))/eg;
 349        }
 350        return $str;
 351}
 352
 353# escape tabs (convert tabs to spaces)
 354sub untabify {
 355        my $line = shift;
 356
 357        while ((my $pos = index($line, "\t")) != -1) {
 358                if (my $count = (8 - ($pos % 8))) {
 359                        my $spaces = ' ' x $count;
 360                        $line =~ s/\t/$spaces/;
 361                }
 362        }
 363
 364        return $line;
 365}
 366
 367## ----------------------------------------------------------------------
 368## HTML aware string manipulation
 369
 370sub chop_str {
 371        my $str = shift;
 372        my $len = shift;
 373        my $add_len = shift || 10;
 374
 375        # allow only $len chars, but don't cut a word if it would fit in $add_len
 376        # if it doesn't fit, cut it if it's still longer than the dots we would add
 377        $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
 378        my $body = $1;
 379        my $tail = $2;
 380        if (length($tail) > 4) {
 381                $tail = " ...";
 382                $body =~ s/&[^;]*$//; # remove chopped character entities
 383        }
 384        return "$body$tail";
 385}
 386
 387## ----------------------------------------------------------------------
 388## functions returning short strings
 389
 390# CSS class for given age value (in seconds)
 391sub age_class {
 392        my $age = shift;
 393
 394        if ($age < 60*60*2) {
 395                return "age0";
 396        } elsif ($age < 60*60*24*2) {
 397                return "age1";
 398        } else {
 399                return "age2";
 400        }
 401}
 402
 403# convert age in seconds to "nn units ago" string
 404sub age_string {
 405        my $age = shift;
 406        my $age_str;
 407
 408        if ($age > 60*60*24*365*2) {
 409                $age_str = (int $age/60/60/24/365);
 410                $age_str .= " years ago";
 411        } elsif ($age > 60*60*24*(365/12)*2) {
 412                $age_str = int $age/60/60/24/(365/12);
 413                $age_str .= " months ago";
 414        } elsif ($age > 60*60*24*7*2) {
 415                $age_str = int $age/60/60/24/7;
 416                $age_str .= " weeks ago";
 417        } elsif ($age > 60*60*24*2) {
 418                $age_str = int $age/60/60/24;
 419                $age_str .= " days ago";
 420        } elsif ($age > 60*60*2) {
 421                $age_str = int $age/60/60;
 422                $age_str .= " hours ago";
 423        } elsif ($age > 60*2) {
 424                $age_str = int $age/60;
 425                $age_str .= " min ago";
 426        } elsif ($age > 2) {
 427                $age_str = int $age;
 428                $age_str .= " sec ago";
 429        } else {
 430                $age_str .= " right now";
 431        }
 432        return $age_str;
 433}
 434
 435# convert file mode in octal to symbolic file mode string
 436sub mode_str {
 437        my $mode = oct shift;
 438
 439        if (S_ISDIR($mode & S_IFMT)) {
 440                return 'drwxr-xr-x';
 441        } elsif (S_ISLNK($mode)) {
 442                return 'lrwxrwxrwx';
 443        } elsif (S_ISREG($mode)) {
 444                # git cares only about the executable bit
 445                if ($mode & S_IXUSR) {
 446                        return '-rwxr-xr-x';
 447                } else {
 448                        return '-rw-r--r--';
 449                };
 450        } else {
 451                return '----------';
 452        }
 453}
 454
 455# convert file mode in octal to file type string
 456sub file_type {
 457        my $mode = shift;
 458
 459        if ($mode !~ m/^[0-7]+$/) {
 460                return $mode;
 461        } else {
 462                $mode = oct $mode;
 463        }
 464
 465        if (S_ISDIR($mode & S_IFMT)) {
 466                return "directory";
 467        } elsif (S_ISLNK($mode)) {
 468                return "symlink";
 469        } elsif (S_ISREG($mode)) {
 470                return "file";
 471        } else {
 472                return "unknown";
 473        }
 474}
 475
 476## ----------------------------------------------------------------------
 477## functions returning short HTML fragments, or transforming HTML fragments
 478## which don't beling to other sections
 479
 480# format line of commit message or tag comment
 481sub format_log_line_html {
 482        my $line = shift;
 483
 484        $line = esc_html($line);
 485        $line =~ s/ /&nbsp;/g;
 486        if ($line =~ m/([0-9a-fA-F]{40})/) {
 487                my $hash_text = $1;
 488                if (git_get_type($hash_text) eq "commit") {
 489                        my $link =
 490                                $cgi->a({-href => href(action=>"commit", hash=>$hash_text),
 491                                        -class => "text"}, $hash_text);
 492                        $line =~ s/$hash_text/$link/;
 493                }
 494        }
 495        return $line;
 496}
 497
 498# format marker of refs pointing to given object
 499sub format_ref_marker {
 500        my ($refs, $id) = @_;
 501        my $markers = '';
 502
 503        if (defined $refs->{$id}) {
 504                foreach my $ref (@{$refs->{$id}}) {
 505                        my ($type, $name) = qw();
 506                        # e.g. tags/v2.6.11 or heads/next
 507                        if ($ref =~ m!^(.*?)s?/(.*)$!) {
 508                                $type = $1;
 509                                $name = $2;
 510                        } else {
 511                                $type = "ref";
 512                                $name = $ref;
 513                        }
 514
 515                        $markers .= " <span class=\"$type\">" . esc_html($name) . "</span>";
 516                }
 517        }
 518
 519        if ($markers) {
 520                return ' <span class="refs">'. $markers . '</span>';
 521        } else {
 522                return "";
 523        }
 524}
 525
 526# format, perhaps shortened and with markers, title line
 527sub format_subject_html {
 528        my ($long, $short, $href, $extra) = @_;
 529        $extra = '' unless defined($extra);
 530
 531        if (length($short) < length($long)) {
 532                return $cgi->a({-href => $href, -class => "list subject",
 533                                -title => $long},
 534                       esc_html($short) . $extra);
 535        } else {
 536                return $cgi->a({-href => $href, -class => "list subject"},
 537                       esc_html($long)  . $extra);
 538        }
 539}
 540
 541sub format_diff_line {
 542        my $line = shift;
 543        my $char = substr($line, 0, 1);
 544        my $diff_class = "";
 545
 546        chomp $line;
 547
 548        if ($char eq '+') {
 549                $diff_class = " add";
 550        } elsif ($char eq "-") {
 551                $diff_class = " rem";
 552        } elsif ($char eq "@") {
 553                $diff_class = " chunk_header";
 554        } elsif ($char eq "\\") {
 555                $diff_class = " incomplete";
 556        }
 557        $line = untabify($line);
 558        return "<div class=\"diff$diff_class\">" . esc_html($line) . "</div>\n";
 559}
 560
 561## ----------------------------------------------------------------------
 562## git utility subroutines, invoking git commands
 563
 564# get HEAD ref of given project as hash
 565sub git_get_head_hash {
 566        my $project = shift;
 567        my $oENV = $ENV{'GIT_DIR'};
 568        my $retval = undef;
 569        $ENV{'GIT_DIR'} = "$projectroot/$project";
 570        if (open my $fd, "-|", $GIT, "rev-parse", "--verify", "HEAD") {
 571                my $head = <$fd>;
 572                close $fd;
 573                if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
 574                        $retval = $1;
 575                }
 576        }
 577        if (defined $oENV) {
 578                $ENV{'GIT_DIR'} = $oENV;
 579        }
 580        return $retval;
 581}
 582
 583# get type of given object
 584sub git_get_type {
 585        my $hash = shift;
 586
 587        open my $fd, "-|", $GIT, "cat-file", '-t', $hash or return;
 588        my $type = <$fd>;
 589        close $fd or return;
 590        chomp $type;
 591        return $type;
 592}
 593
 594sub git_get_project_config {
 595        my ($key, $type) = @_;
 596
 597        return unless ($key);
 598        $key =~ s/^gitweb\.//;
 599        return if ($key =~ m/\W/);
 600
 601        my @x = ($GIT, 'repo-config');
 602        if (defined $type) { push @x, $type; }
 603        push @x, "--get";
 604        push @x, "gitweb.$key";
 605        my $val = qx(@x);
 606        chomp $val;
 607        return ($val);
 608}
 609
 610# get hash of given path at given ref
 611sub git_get_hash_by_path {
 612        my $base = shift;
 613        my $path = shift || return undef;
 614
 615        my $tree = $base;
 616
 617        open my $fd, "-|", $GIT, "ls-tree", $base, "--", $path
 618                or die_error(undef, "Open git-ls-tree failed");
 619        my $line = <$fd>;
 620        close $fd or return undef;
 621
 622        #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa  panic.c'
 623        $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
 624        return $3;
 625}
 626
 627# converts symbolic name to hash
 628sub git_to_hash {
 629        my @params = @_;
 630        return undef unless @params;
 631
 632        open my $fd, "-|", $GIT, "rev-parse", @params
 633                or return undef;
 634        my @hashes = map { chomp; $_ } <$fd>;
 635        close $fd;
 636
 637        if (wantarray) {
 638                return @hashes;
 639        } elsif (scalar(@hashes) == 1) {
 640                # single hash
 641                return $hashes[0];
 642        } else {
 643                return \@hashes;
 644        }
 645}
 646
 647## ......................................................................
 648## git utility functions, directly accessing git repository
 649
 650# assumes that PATH is not symref
 651sub git_get_hash_by_ref {
 652        my $path = shift;
 653
 654        open my $fd, "$projectroot/$path" or return undef;
 655        my $head = <$fd>;
 656        close $fd;
 657        chomp $head;
 658        if ($head =~ m/^[0-9a-fA-F]{40}$/) {
 659                return $head;
 660        }
 661}
 662
 663sub git_get_project_description {
 664        my $path = shift;
 665
 666        open my $fd, "$projectroot/$path/description" or return undef;
 667        my $descr = <$fd>;
 668        close $fd;
 669        chomp $descr;
 670        return $descr;
 671}
 672
 673sub git_get_project_url_list {
 674        my $path = shift;
 675
 676        open my $fd, "$projectroot/$path/cloneurl" or return undef;
 677        my @git_project_url_list = map { chomp; $_ } <$fd>;
 678        close $fd;
 679
 680        return wantarray ? @git_project_url_list : \@git_project_url_list;
 681}
 682
 683sub git_get_projects_list {
 684        my @list;
 685
 686        if (-d $projects_list) {
 687                # search in directory
 688                my $dir = $projects_list;
 689                opendir my ($dh), $dir or return undef;
 690                while (my $dir = readdir($dh)) {
 691                        if (-e "$projectroot/$dir/HEAD") {
 692                                my $pr = {
 693                                        path => $dir,
 694                                };
 695                                push @list, $pr
 696                        }
 697                }
 698                closedir($dh);
 699        } elsif (-f $projects_list) {
 700                # read from file(url-encoded):
 701                # 'git%2Fgit.git Linus+Torvalds'
 702                # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
 703                # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
 704                open my ($fd), $projects_list or return undef;
 705                while (my $line = <$fd>) {
 706                        chomp $line;
 707                        my ($path, $owner) = split ' ', $line;
 708                        $path = unescape($path);
 709                        $owner = unescape($owner);
 710                        if (!defined $path) {
 711                                next;
 712                        }
 713                        if (-e "$projectroot/$path/HEAD") {
 714                                my $pr = {
 715                                        path => $path,
 716                                        owner => decode("utf8", $owner, Encode::FB_DEFAULT),
 717                                };
 718                                push @list, $pr
 719                        }
 720                }
 721                close $fd;
 722        }
 723        @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
 724        return @list;
 725}
 726
 727sub git_get_project_owner {
 728        my $project = shift;
 729        my $owner;
 730
 731        return undef unless $project;
 732
 733        # read from file (url-encoded):
 734        # 'git%2Fgit.git Linus+Torvalds'
 735        # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
 736        # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
 737        if (-f $projects_list) {
 738                open (my $fd , $projects_list);
 739                while (my $line = <$fd>) {
 740                        chomp $line;
 741                        my ($pr, $ow) = split ' ', $line;
 742                        $pr = unescape($pr);
 743                        $ow = unescape($ow);
 744                        if ($pr eq $project) {
 745                                $owner = decode("utf8", $ow, Encode::FB_DEFAULT);
 746                                last;
 747                        }
 748                }
 749                close $fd;
 750        }
 751        if (!defined $owner) {
 752                $owner = get_file_owner("$projectroot/$project");
 753        }
 754
 755        return $owner;
 756}
 757
 758sub git_get_references {
 759        my $type = shift || "";
 760        my %refs;
 761        my $fd;
 762        # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c      refs/tags/v2.6.11
 763        # c39ae07f393806ccf406ef966e9a15afc43cc36a      refs/tags/v2.6.11^{}
 764        if (-f "$projectroot/$project/info/refs") {
 765                open $fd, "$projectroot/$project/info/refs"
 766                        or return;
 767        } else {
 768                open $fd, "-|", $GIT, "ls-remote", "."
 769                        or return;
 770        }
 771
 772        while (my $line = <$fd>) {
 773                chomp $line;
 774                if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?[^\^]+)/) {
 775                        if (defined $refs{$1}) {
 776                                push @{$refs{$1}}, $2;
 777                        } else {
 778                                $refs{$1} = [ $2 ];
 779                        }
 780                }
 781        }
 782        close $fd or return;
 783        return \%refs;
 784}
 785
 786sub git_get_following_references {
 787        my $hash = shift || return undef;
 788        my $type = shift;
 789        my $base = shift || $hash_base || "HEAD";
 790
 791        my $refs = git_get_references($type);
 792        open my $fd, "-|", $GIT, "rev-list", $base
 793                or return undef;
 794        my @commits = map { chomp; $_ } <$fd>;
 795        close $fd
 796                or return undef;
 797
 798        my @reflist;
 799        my $lastref;
 800
 801        foreach my $commit (@commits) {
 802                foreach my $ref (@{$refs->{$commit}}) {
 803                        $lastref = $ref;
 804                        push @reflist, $lastref;
 805                }
 806                if ($commit eq $hash) {
 807                        last;
 808                }
 809        }
 810
 811        return wantarray ? @reflist : $lastref;
 812}
 813
 814sub git_get_preceding_references {
 815        my $hash = shift || return undef;
 816        my $type = shift;
 817
 818        my $refs = git_get_references($type);
 819        open my $fd, "-|", $GIT, "rev-list", $hash
 820                or return undef;
 821        my @commits = map { chomp; $_ } <$fd>;
 822        close $fd
 823                or return undef;
 824
 825        my @reflist;
 826
 827        foreach my $commit (@commits) {
 828                foreach my $ref (@{$refs->{$commit}}) {
 829                        return $ref unless wantarray;
 830                        push @reflist, $ref;
 831                }
 832        }
 833
 834        return @reflist;
 835}
 836
 837sub git_get_rev_name_tags {
 838        my $hash = shift || return undef;
 839
 840        open my $fd, "-|", $GIT, "name-rev", "--tags", $hash
 841                or return;
 842        my $name_rev = <$fd>;
 843        close $fd;
 844
 845        if ($name_rev =~ m|^$hash tags/(.*)$|) {
 846                return $1;
 847        } else {
 848                # catches also '$hash undefined' output
 849                return undef;
 850        }
 851}
 852
 853## ----------------------------------------------------------------------
 854## parse to hash functions
 855
 856sub parse_date {
 857        my $epoch = shift;
 858        my $tz = shift || "-0000";
 859
 860        my %date;
 861        my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
 862        my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
 863        my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
 864        $date{'hour'} = $hour;
 865        $date{'minute'} = $min;
 866        $date{'mday'} = $mday;
 867        $date{'day'} = $days[$wday];
 868        $date{'month'} = $months[$mon];
 869        $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
 870                           $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
 871        $date{'mday-time'} = sprintf "%d %s %02d:%02d",
 872                             $mday, $months[$mon], $hour ,$min;
 873
 874        $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
 875        my $local = $epoch + ((int $1 + ($2/60)) * 3600);
 876        ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
 877        $date{'hour_local'} = $hour;
 878        $date{'minute_local'} = $min;
 879        $date{'tz_local'} = $tz;
 880        return %date;
 881}
 882
 883sub parse_tag {
 884        my $tag_id = shift;
 885        my %tag;
 886        my @comment;
 887
 888        open my $fd, "-|", $GIT, "cat-file", "tag", $tag_id or return;
 889        $tag{'id'} = $tag_id;
 890        while (my $line = <$fd>) {
 891                chomp $line;
 892                if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
 893                        $tag{'object'} = $1;
 894                } elsif ($line =~ m/^type (.+)$/) {
 895                        $tag{'type'} = $1;
 896                } elsif ($line =~ m/^tag (.+)$/) {
 897                        $tag{'name'} = $1;
 898                } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
 899                        $tag{'author'} = $1;
 900                        $tag{'epoch'} = $2;
 901                        $tag{'tz'} = $3;
 902                } elsif ($line =~ m/--BEGIN/) {
 903                        push @comment, $line;
 904                        last;
 905                } elsif ($line eq "") {
 906                        last;
 907                }
 908        }
 909        push @comment, <$fd>;
 910        $tag{'comment'} = \@comment;
 911        close $fd or return;
 912        if (!defined $tag{'name'}) {
 913                return
 914        };
 915        return %tag
 916}
 917
 918sub parse_commit {
 919        my $commit_id = shift;
 920        my $commit_text = shift;
 921
 922        my @commit_lines;
 923        my %co;
 924
 925        if (defined $commit_text) {
 926                @commit_lines = @$commit_text;
 927        } else {
 928                $/ = "\0";
 929                open my $fd, "-|", $GIT, "rev-list", "--header", "--parents", "--max-count=1", $commit_id
 930                        or return;
 931                @commit_lines = split '\n', <$fd>;
 932                close $fd or return;
 933                $/ = "\n";
 934                pop @commit_lines;
 935        }
 936        my $header = shift @commit_lines;
 937        if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
 938                return;
 939        }
 940        ($co{'id'}, my @parents) = split ' ', $header;
 941        $co{'parents'} = \@parents;
 942        $co{'parent'} = $parents[0];
 943        while (my $line = shift @commit_lines) {
 944                last if $line eq "\n";
 945                if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
 946                        $co{'tree'} = $1;
 947                } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
 948                        $co{'author'} = $1;
 949                        $co{'author_epoch'} = $2;
 950                        $co{'author_tz'} = $3;
 951                        if ($co{'author'} =~ m/^([^<]+) </) {
 952                                $co{'author_name'} = $1;
 953                        } else {
 954                                $co{'author_name'} = $co{'author'};
 955                        }
 956                } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
 957                        $co{'committer'} = $1;
 958                        $co{'committer_epoch'} = $2;
 959                        $co{'committer_tz'} = $3;
 960                        $co{'committer_name'} = $co{'committer'};
 961                        $co{'committer_name'} =~ s/ <.*//;
 962                }
 963        }
 964        if (!defined $co{'tree'}) {
 965                return;
 966        };
 967
 968        foreach my $title (@commit_lines) {
 969                $title =~ s/^    //;
 970                if ($title ne "") {
 971                        $co{'title'} = chop_str($title, 80, 5);
 972                        # remove leading stuff of merges to make the interesting part visible
 973                        if (length($title) > 50) {
 974                                $title =~ s/^Automatic //;
 975                                $title =~ s/^merge (of|with) /Merge ... /i;
 976                                if (length($title) > 50) {
 977                                        $title =~ s/(http|rsync):\/\///;
 978                                }
 979                                if (length($title) > 50) {
 980                                        $title =~ s/(master|www|rsync)\.//;
 981                                }
 982                                if (length($title) > 50) {
 983                                        $title =~ s/kernel.org:?//;
 984                                }
 985                                if (length($title) > 50) {
 986                                        $title =~ s/\/pub\/scm//;
 987                                }
 988                        }
 989                        $co{'title_short'} = chop_str($title, 50, 5);
 990                        last;
 991                }
 992        }
 993        # remove added spaces
 994        foreach my $line (@commit_lines) {
 995                $line =~ s/^    //;
 996        }
 997        $co{'comment'} = \@commit_lines;
 998
 999        my $age = time - $co{'committer_epoch'};
1000        $co{'age'} = $age;
1001        $co{'age_string'} = age_string($age);
1002        my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
1003        if ($age > 60*60*24*7*2) {
1004                $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1005                $co{'age_string_age'} = $co{'age_string'};
1006        } else {
1007                $co{'age_string_date'} = $co{'age_string'};
1008                $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1009        }
1010        return %co;
1011}
1012
1013# parse ref from ref_file, given by ref_id, with given type
1014sub parse_ref {
1015        my $ref_file = shift;
1016        my $ref_id = shift;
1017        my $type = shift || git_get_type($ref_id);
1018        my %ref_item;
1019
1020        $ref_item{'type'} = $type;
1021        $ref_item{'id'} = $ref_id;
1022        $ref_item{'epoch'} = 0;
1023        $ref_item{'age'} = "unknown";
1024        if ($type eq "tag") {
1025                my %tag = parse_tag($ref_id);
1026                $ref_item{'comment'} = $tag{'comment'};
1027                if ($tag{'type'} eq "commit") {
1028                        my %co = parse_commit($tag{'object'});
1029                        $ref_item{'epoch'} = $co{'committer_epoch'};
1030                        $ref_item{'age'} = $co{'age_string'};
1031                } elsif (defined($tag{'epoch'})) {
1032                        my $age = time - $tag{'epoch'};
1033                        $ref_item{'epoch'} = $tag{'epoch'};
1034                        $ref_item{'age'} = age_string($age);
1035                }
1036                $ref_item{'reftype'} = $tag{'type'};
1037                $ref_item{'name'} = $tag{'name'};
1038                $ref_item{'refid'} = $tag{'object'};
1039        } elsif ($type eq "commit"){
1040                my %co = parse_commit($ref_id);
1041                $ref_item{'reftype'} = "commit";
1042                $ref_item{'name'} = $ref_file;
1043                $ref_item{'title'} = $co{'title'};
1044                $ref_item{'refid'} = $ref_id;
1045                $ref_item{'epoch'} = $co{'committer_epoch'};
1046                $ref_item{'age'} = $co{'age_string'};
1047        } else {
1048                $ref_item{'reftype'} = $type;
1049                $ref_item{'name'} = $ref_file;
1050                $ref_item{'refid'} = $ref_id;
1051        }
1052
1053        return %ref_item;
1054}
1055
1056# parse line of git-diff-tree "raw" output
1057sub parse_difftree_raw_line {
1058        my $line = shift;
1059        my %res;
1060
1061        # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M   ls-files.c'
1062        # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M   rev-tree.c'
1063        if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1064                $res{'from_mode'} = $1;
1065                $res{'to_mode'} = $2;
1066                $res{'from_id'} = $3;
1067                $res{'to_id'} = $4;
1068                $res{'status'} = $5;
1069                $res{'similarity'} = $6;
1070                if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
1071                        ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
1072                } else {
1073                        $res{'file'} = unquote($7);
1074                }
1075        }
1076        # 'c512b523472485aef4fff9e57b229d9d243c967f'
1077        #elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
1078        #       $res{'commit'} = $1;
1079        #}
1080
1081        return wantarray ? %res : \%res;
1082}
1083
1084## ......................................................................
1085## parse to array of hashes functions
1086
1087sub git_get_refs_list {
1088        my $ref_dir = shift;
1089        my @reflist;
1090
1091        my @refs;
1092        my $pfxlen = length("$projectroot/$project/$ref_dir");
1093        File::Find::find(sub {
1094                return if (/^\./);
1095                if (-f $_) {
1096                        push @refs, substr($File::Find::name, $pfxlen + 1);
1097                }
1098        }, "$projectroot/$project/$ref_dir");
1099
1100        foreach my $ref_file (@refs) {
1101                my $ref_id = git_get_hash_by_ref("$project/$ref_dir/$ref_file");
1102                my $type = git_get_type($ref_id) || next;
1103                my %ref_item = parse_ref($ref_file, $ref_id, $type);
1104
1105                push @reflist, \%ref_item;
1106        }
1107        # sort refs by age
1108        @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
1109        return \@reflist;
1110}
1111
1112## ----------------------------------------------------------------------
1113## filesystem-related functions
1114
1115sub get_file_owner {
1116        my $path = shift;
1117
1118        my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
1119        my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
1120        if (!defined $gcos) {
1121                return undef;
1122        }
1123        my $owner = $gcos;
1124        $owner =~ s/[,;].*$//;
1125        return decode("utf8", $owner, Encode::FB_DEFAULT);
1126}
1127
1128## ......................................................................
1129## mimetype related functions
1130
1131sub mimetype_guess_file {
1132        my $filename = shift;
1133        my $mimemap = shift;
1134        -r $mimemap or return undef;
1135
1136        my %mimemap;
1137        open(MIME, $mimemap) or return undef;
1138        while (<MIME>) {
1139                next if m/^#/; # skip comments
1140                my ($mime, $exts) = split(/\t+/);
1141                if (defined $exts) {
1142                        my @exts = split(/\s+/, $exts);
1143                        foreach my $ext (@exts) {
1144                                $mimemap{$ext} = $mime;
1145                        }
1146                }
1147        }
1148        close(MIME);
1149
1150        $filename =~ /\.(.*?)$/;
1151        return $mimemap{$1};
1152}
1153
1154sub mimetype_guess {
1155        my $filename = shift;
1156        my $mime;
1157        $filename =~ /\./ or return undef;
1158
1159        if ($mimetypes_file) {
1160                my $file = $mimetypes_file;
1161                if ($file !~ m!^/!) { # if it is relative path
1162                        # it is relative to project
1163                        $file = "$projectroot/$project/$file";
1164                }
1165                $mime = mimetype_guess_file($filename, $file);
1166        }
1167        $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1168        return $mime;
1169}
1170
1171sub blob_mimetype {
1172        my $fd = shift;
1173        my $filename = shift;
1174
1175        if ($filename) {
1176                my $mime = mimetype_guess($filename);
1177                $mime and return $mime;
1178        }
1179
1180        # just in case
1181        return $default_blob_plain_mimetype unless $fd;
1182
1183        if (-T $fd) {
1184                return 'text/plain' .
1185                       ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1186        } elsif (! $filename) {
1187                return 'application/octet-stream';
1188        } elsif ($filename =~ m/\.png$/i) {
1189                return 'image/png';
1190        } elsif ($filename =~ m/\.gif$/i) {
1191                return 'image/gif';
1192        } elsif ($filename =~ m/\.jpe?g$/i) {
1193                return 'image/jpeg';
1194        } else {
1195                return 'application/octet-stream';
1196        }
1197}
1198
1199## ======================================================================
1200## functions printing HTML: header, footer, error page
1201
1202sub git_header_html {
1203        my $status = shift || "200 OK";
1204        my $expires = shift;
1205
1206        my $title = "$site_name git";
1207        if (defined $project) {
1208                $title .= " - $project";
1209                if (defined $action) {
1210                        $title .= "/$action";
1211                        if (defined $file_name) {
1212                                $title .= " - $file_name";
1213                                if ($action eq "tree" && $file_name !~ m|/$|) {
1214                                        $title .= "/";
1215                                }
1216                        }
1217                }
1218        }
1219        my $content_type;
1220        # require explicit support from the UA if we are to send the page as
1221        # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
1222        # we have to do this because MSIE sometimes globs '*/*', pretending to
1223        # support xhtml+xml but choking when it gets what it asked for.
1224        if (defined $cgi->http('HTTP_ACCEPT') &&
1225            $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
1226            $cgi->Accept('application/xhtml+xml') != 0) {
1227                $content_type = 'application/xhtml+xml';
1228        } else {
1229                $content_type = 'text/html';
1230        }
1231        print $cgi->header(-type=>$content_type, -charset => 'utf-8',
1232                           -status=> $status, -expires => $expires);
1233        print <<EOF;
1234<?xml version="1.0" encoding="utf-8"?>
1235<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1236<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
1237<!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
1238<!-- git core binaries version $git_version -->
1239<head>
1240<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
1241<meta name="generator" content="gitweb/$version git/$git_version"/>
1242<meta name="robots" content="index, nofollow"/>
1243<title>$title</title>
1244<link rel="stylesheet" type="text/css" href="$stylesheet"/>
1245EOF
1246        if (defined $project) {
1247                printf('<link rel="alternate" title="%s log" '.
1248                       'href="%s" type="application/rss+xml"/>'."\n",
1249                       esc_param($project), href(action=>"rss"));
1250        }
1251
1252        print "</head>\n" .
1253              "<body>\n" .
1254              "<div class=\"page_header\">\n" .
1255              "<a href=\"http://www.kernel.org/pub/software/scm/git/docs/\" title=\"git documentation\">" .
1256              "<img src=\"$logo\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/>" .
1257              "</a>\n";
1258        print $cgi->a({-href => esc_param($home_link)}, $home_link_str) . " / ";
1259        if (defined $project) {
1260                print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
1261                if (defined $action) {
1262                        print " / $action";
1263                }
1264                print "\n";
1265                if (!defined $searchtext) {
1266                        $searchtext = "";
1267                }
1268                my $search_hash;
1269                if (defined $hash_base) {
1270                        $search_hash = $hash_base;
1271                } elsif (defined $hash) {
1272                        $search_hash = $hash;
1273                } else {
1274                        $search_hash = "HEAD";
1275                }
1276                $cgi->param("a", "search");
1277                $cgi->param("h", $search_hash);
1278                print $cgi->startform(-method => "get", -action => $my_uri) .
1279                      "<div class=\"search\">\n" .
1280                      $cgi->hidden(-name => "p") . "\n" .
1281                      $cgi->hidden(-name => "a") . "\n" .
1282                      $cgi->hidden(-name => "h") . "\n" .
1283                      $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
1284                      "</div>" .
1285                      $cgi->end_form() . "\n";
1286        }
1287        print "</div>\n";
1288}
1289
1290sub git_footer_html {
1291        print "<div class=\"page_footer\">\n";
1292        if (defined $project) {
1293                my $descr = git_get_project_description($project);
1294                if (defined $descr) {
1295                        print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
1296                }
1297                print $cgi->a({-href => href(action=>"rss"), -class => "rss_logo"}, "RSS") . "\n";
1298        } else {
1299                print $cgi->a({-href => href(action=>"opml"), -class => "rss_logo"}, "OPML") . "\n";
1300        }
1301        print "</div>\n" .
1302              "</body>\n" .
1303              "</html>";
1304}
1305
1306sub die_error {
1307        my $status = shift || "403 Forbidden";
1308        my $error = shift || "Malformed query, file missing or permission denied";
1309
1310        git_header_html($status);
1311        print <<EOF;
1312<div class="page_body">
1313<br /><br />
1314$status - $error
1315<br />
1316</div>
1317EOF
1318        git_footer_html();
1319        exit;
1320}
1321
1322## ----------------------------------------------------------------------
1323## functions printing or outputting HTML: navigation
1324
1325sub git_print_page_nav {
1326        my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
1327        $extra = '' if !defined $extra; # pager or formats
1328
1329        my @navs = qw(summary shortlog log commit commitdiff tree);
1330        if ($suppress) {
1331                @navs = grep { $_ ne $suppress } @navs;
1332        }
1333
1334        my %arg = map { $_ => {action=>$_} } @navs;
1335        if (defined $head) {
1336                for (qw(commit commitdiff)) {
1337                        $arg{$_}{hash} = $head;
1338                }
1339                if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
1340                        for (qw(shortlog log)) {
1341                                $arg{$_}{hash} = $head;
1342                        }
1343                }
1344        }
1345        $arg{tree}{hash} = $treehead if defined $treehead;
1346        $arg{tree}{hash_base} = $treebase if defined $treebase;
1347
1348        print "<div class=\"page_nav\">\n" .
1349                (join " | ",
1350                 map { $_ eq $current ?
1351                       $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
1352                 } @navs);
1353        print "<br/>\n$extra<br/>\n" .
1354              "</div>\n";
1355}
1356
1357sub format_paging_nav {
1358        my ($action, $hash, $head, $page, $nrevs) = @_;
1359        my $paging_nav;
1360
1361
1362        if ($hash ne $head || $page) {
1363                $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
1364        } else {
1365                $paging_nav .= "HEAD";
1366        }
1367
1368        if ($page > 0) {
1369                $paging_nav .= " &sdot; " .
1370                        $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
1371                                 -accesskey => "p", -title => "Alt-p"}, "prev");
1372        } else {
1373                $paging_nav .= " &sdot; prev";
1374        }
1375
1376        if ($nrevs >= (100 * ($page+1)-1)) {
1377                $paging_nav .= " &sdot; " .
1378                        $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
1379                                 -accesskey => "n", -title => "Alt-n"}, "next");
1380        } else {
1381                $paging_nav .= " &sdot; next";
1382        }
1383
1384        return $paging_nav;
1385}
1386
1387## ......................................................................
1388## functions printing or outputting HTML: div
1389
1390sub git_print_header_div {
1391        my ($action, $title, $hash, $hash_base) = @_;
1392        my %args = ();
1393
1394        $args{action} = $action;
1395        $args{hash} = $hash if $hash;
1396        $args{hash_base} = $hash_base if $hash_base;
1397
1398        print "<div class=\"header\">\n" .
1399              $cgi->a({-href => href(%args), -class => "title"},
1400              $title ? $title : $action) .
1401              "\n</div>\n";
1402}
1403
1404sub git_print_page_path {
1405        my $name = shift;
1406        my $type = shift;
1407        my $hb = shift;
1408
1409        if (!defined $name) {
1410                print "<div class=\"page_path\">/</div>\n";
1411        } elsif (defined $type && $type eq 'blob') {
1412                print "<div class=\"page_path\">";
1413                if (defined $hb) {
1414                        print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
1415                                                     hash_base=>$hb)},
1416                                      esc_html($name));
1417                } else {
1418                        print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name)},
1419                                      esc_html($name));
1420                }
1421                print "<br/></div>\n";
1422        } else {
1423                print "<div class=\"page_path\">" . esc_html($name) . "<br/></div>\n";
1424        }
1425}
1426
1427sub git_print_log {
1428        my $log = shift;
1429
1430        # remove leading empty lines
1431        while (defined $log->[0] && $log->[0] eq "") {
1432                shift @$log;
1433        }
1434
1435        # print log
1436        my $signoff = 0;
1437        my $empty = 0;
1438        foreach my $line (@$log) {
1439                # print only one empty line
1440                # do not print empty line after signoff
1441                if ($line eq "") {
1442                        next if ($empty || $signoff);
1443                        $empty = 1;
1444                } else {
1445                        $empty = 0;
1446                }
1447                if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1448                        $signoff = 1;
1449                        print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
1450                } else {
1451                        $signoff = 0;
1452                        print format_log_line_html($line) . "<br/>\n";
1453                }
1454        }
1455}
1456
1457sub git_print_simplified_log {
1458        my $log = shift;
1459        my $remove_title = shift;
1460
1461        shift @$log if $remove_title;
1462        # remove leading empty lines
1463        while (defined $log->[0] && $log->[0] eq "") {
1464                shift @$log;
1465        }
1466
1467        # simplify and print log
1468        my $empty = 0;
1469        foreach my $line (@$log) {
1470                # remove signoff lines
1471                if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1472                        next;
1473                }
1474                # print only one empty line
1475                if ($line eq "") {
1476                        next if $empty;
1477                        $empty = 1;
1478                } else {
1479                        $empty = 0;
1480                }
1481                print format_log_line_html($line) . "<br/>\n";
1482        }
1483        # end with single empty line
1484        print "<br/>\n" unless $empty;
1485}
1486
1487## ......................................................................
1488## functions printing large fragments of HTML
1489
1490sub git_difftree_body {
1491        my ($difftree, $hash, $parent) = @_;
1492
1493        print "<div class=\"list_head\">\n";
1494        if ($#{$difftree} > 10) {
1495                print(($#{$difftree} + 1) . " files changed:\n");
1496        }
1497        print "</div>\n";
1498
1499        print "<table class=\"diff_tree\">\n";
1500        my $alternate = 0;
1501        foreach my $line (@{$difftree}) {
1502                my %diff = parse_difftree_raw_line($line);
1503
1504                if ($alternate) {
1505                        print "<tr class=\"dark\">\n";
1506                } else {
1507                        print "<tr class=\"light\">\n";
1508                }
1509                $alternate ^= 1;
1510
1511                my ($to_mode_oct, $to_mode_str, $to_file_type);
1512                my ($from_mode_oct, $from_mode_str, $from_file_type);
1513                if ($diff{'to_mode'} ne ('0' x 6)) {
1514                        $to_mode_oct = oct $diff{'to_mode'};
1515                        if (S_ISREG($to_mode_oct)) { # only for regular file
1516                                $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
1517                        }
1518                        $to_file_type = file_type($diff{'to_mode'});
1519                }
1520                if ($diff{'from_mode'} ne ('0' x 6)) {
1521                        $from_mode_oct = oct $diff{'from_mode'};
1522                        if (S_ISREG($to_mode_oct)) { # only for regular file
1523                                $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
1524                        }
1525                        $from_file_type = file_type($diff{'from_mode'});
1526                }
1527
1528                if ($diff{'status'} eq "A") { # created
1529                        my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
1530                        $mode_chng   .= " with mode: $to_mode_str" if $to_mode_str;
1531                        $mode_chng   .= "]</span>";
1532                        print "<td>" .
1533                              $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1534                                                     hash_base=>$hash, file_name=>$diff{'file'}),
1535                                      -class => "list"}, esc_html($diff{'file'})) .
1536                              "</td>\n" .
1537                              "<td>$mode_chng</td>\n" .
1538                              "<td class=\"link\">" .
1539                              $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1540                                                     hash_base=>$hash, file_name=>$diff{'file'})},
1541                                      "blob") .
1542                              "</td>\n";
1543
1544                } elsif ($diff{'status'} eq "D") { # deleted
1545                        my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
1546                        print "<td>" .
1547                              $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1548                                                     hash_base=>$parent, file_name=>$diff{'file'}),
1549                                       -class => "list"}, esc_html($diff{'file'})) .
1550                              "</td>\n" .
1551                              "<td>$mode_chng</td>\n" .
1552                              "<td class=\"link\">" .
1553                              $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1554                                                     hash_base=>$parent, file_name=>$diff{'file'})},
1555                                      "blob") .
1556                              " | " .
1557                              $cgi->a({-href => href(action=>"history", hash_base=>$parent,
1558                                                     file_name=>$diff{'file'})},\
1559                                      "history") .
1560                              "</td>\n";
1561
1562                } elsif ($diff{'status'} eq "M" || $diff{'status'} eq "T") { # modified, or type changed
1563                        my $mode_chnge = "";
1564                        if ($diff{'from_mode'} != $diff{'to_mode'}) {
1565                                $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
1566                                if ($from_file_type != $to_file_type) {
1567                                        $mode_chnge .= " from $from_file_type to $to_file_type";
1568                                }
1569                                if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
1570                                        if ($from_mode_str && $to_mode_str) {
1571                                                $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
1572                                        } elsif ($to_mode_str) {
1573                                                $mode_chnge .= " mode: $to_mode_str";
1574                                        }
1575                                }
1576                                $mode_chnge .= "]</span>\n";
1577                        }
1578                        print "<td>";
1579                        if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
1580                                print $cgi->a({-href => href(action=>"blobdiff",
1581                                                             hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1582                                                             hash_base=>$hash, hash_parent_base=>$parent,
1583                                                             file_name=>$diff{'file'}),
1584                                              -class => "list"}, esc_html($diff{'file'}));
1585                        } else { # only mode changed
1586                                print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1587                                                             hash_base=>$hash, file_name=>$diff{'file'}),
1588                                              -class => "list"}, esc_html($diff{'file'}));
1589                        }
1590                        print "</td>\n" .
1591                              "<td>$mode_chnge</td>\n" .
1592                              "<td class=\"link\">" .
1593                                $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1594                                                       hash_base=>$hash, file_name=>$diff{'file'})},
1595                                        "blob");
1596                        if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
1597                                print " | " .
1598                                        $cgi->a({-href => href(action=>"blobdiff",
1599                                                               hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1600                                                               hash_base=>$hash, hash_parent_base=>$parent,
1601                                                               file_name=>$diff{'file'})},
1602                                                "diff");
1603                        }
1604                        print " | " .
1605                                $cgi->a({-href => href(action=>"history",
1606                                                       hash_base=>$hash, file_name=>$diff{'file'})},
1607                                        "history");
1608                        print "</td>\n";
1609
1610                } elsif ($diff{'status'} eq "R" || $diff{'status'} eq "C") { # renamed or copied
1611                        my %status_name = ('R' => 'moved', 'C' => 'copied');
1612                        my $nstatus = $status_name{$diff{'status'}};
1613                        my $mode_chng = "";
1614                        if ($diff{'from_mode'} != $diff{'to_mode'}) {
1615                                # mode also for directories, so we cannot use $to_mode_str
1616                                $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
1617                        }
1618                        print "<td>" .
1619                              $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1620                                                     hash=>$diff{'to_id'}, file_name=>$diff{'to_file'}),
1621                                      -class => "list"}, esc_html($diff{'to_file'})) . "</td>\n" .
1622                              "<td><span class=\"file_status $nstatus\">[$nstatus from " .
1623                              $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
1624                                                     hash=>$diff{'from_id'}, file_name=>$diff{'from_file'}),
1625                                      -class => "list"}, esc_html($diff{'from_file'})) .
1626                              " with " . (int $diff{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
1627                              "<td class=\"link\">" .
1628                              $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1629                                                     hash=>$diff{'to_id'}, file_name=>$diff{'to_file'})},
1630                                      "blob");
1631                        if ($diff{'to_id'} ne $diff{'from_id'}) {
1632                                print " | " .
1633                                        $cgi->a({-href => href(action=>"blobdiff",
1634                                                               hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1635                                                               hash_base=>$hash, hash_parent_base=>$parent,
1636                                                               file_name=>$diff{'to_file'}, file_parent=>$diff{'from_file'})},
1637                                                "diff");
1638                        }
1639                        print "</td>\n";
1640
1641                } # we should not encounter Unmerged (U) or Unknown (X) status
1642                print "</tr>\n";
1643        }
1644        print "</table>\n";
1645}
1646
1647sub git_patchset_body {
1648        my ($fd, $difftree, $hash, $hash_parent) = @_;
1649
1650        my $patch_idx = 0;
1651        my $in_header = 0;
1652        my $patch_found = 0;
1653        my $diffinfo;
1654
1655        print "<div class=\"patchset\">\n";
1656
1657        LINE:
1658        while (my $patch_line @$fd>) {
1659                chomp $patch_line;
1660
1661                if ($patch_line =~ m/^diff /) { # "git diff" header
1662                        # beginning of patch (in patchset)
1663                        if ($patch_found) {
1664                                # close previous patch
1665                                print "</div>\n"; # class="patch"
1666                        } else {
1667                                # first patch in patchset
1668                                $patch_found = 1;
1669                        }
1670                        print "<div class=\"patch\">\n";
1671
1672                        if (ref($difftree->[$patch_idx]) eq "HASH") {
1673                                $diffinfo = $difftree->[$patch_idx];
1674                        } else {
1675                                $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
1676                        }
1677                        $patch_idx++;
1678
1679                        # for now, no extended header, hence we skip empty patches
1680                        # companion to  next LINE if $in_header;
1681                        if ($diffinfo->{'from_id'} eq $diffinfo->{'to_id'}) { # no change
1682                                $in_header = 1;
1683                                next LINE;
1684                        }
1685
1686                        if ($diffinfo->{'status'} eq "A") { # added
1687                                print "<div class=\"diff_info\">" . file_type($diffinfo->{'to_mode'}) . ":" .
1688                                      $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1689                                                             hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
1690                                              $diffinfo->{'to_id'}) . "(new)" .
1691                                      "</div>\n"; # class="diff_info"
1692
1693                        } elsif ($diffinfo->{'status'} eq "D") { # deleted
1694                                print "<div class=\"diff_info\">" . file_type($diffinfo->{'from_mode'}) . ":" .
1695                                      $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1696                                                             hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
1697                                              $diffinfo->{'from_id'}) . "(deleted)" .
1698                                      "</div>\n"; # class="diff_info"
1699
1700                        } elsif ($diffinfo->{'status'} eq "R" || # renamed
1701                                 $diffinfo->{'status'} eq "C" || # copied
1702                                 $diffinfo->{'status'} eq "2") { # with two filenames (from git_blobdiff)
1703                                print "<div class=\"diff_info\">" .
1704                                      file_type($diffinfo->{'from_mode'}) . ":" .
1705                                      $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1706                                                             hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'from_file'})},
1707                                              $diffinfo->{'from_id'}) .
1708                                      " -> " .
1709                                      file_type($diffinfo->{'to_mode'}) . ":" .
1710                                      $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1711                                                             hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'to_file'})},
1712                                              $diffinfo->{'to_id'});
1713                                print "</div>\n"; # class="diff_info"
1714
1715                        } else { # modified, mode changed, ...
1716                                print "<div class=\"diff_info\">" .
1717                                      file_type($diffinfo->{'from_mode'}) . ":" .
1718                                      $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1719                                                             hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
1720                                              $diffinfo->{'from_id'}) .
1721                                      " -> " .
1722                                      file_type($diffinfo->{'to_mode'}) . ":" .
1723                                      $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1724                                                             hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
1725                                              $diffinfo->{'to_id'});
1726                                print "</div>\n"; # class="diff_info"
1727                        }
1728
1729                        #print "<div class=\"diff extended_header\">\n";
1730                        $in_header = 1;
1731                        next LINE;
1732                } # start of patch in patchset
1733
1734
1735                if ($in_header && $patch_line =~ m/^---/) {
1736                        #print "</div>\n"; # class="diff extended_header"
1737                        $in_header = 0;
1738
1739                        my $file = $diffinfo->{'from_file'};
1740                        $file  ||= $diffinfo->{'file'};
1741                        $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1742                                                       hash=>$diffinfo->{'from_id'}, file_name=>$file),
1743                                        -class => "list"}, esc_html($file));
1744                        $patch_line =~ s|a/.*$|a/$file|g;
1745                        print "<div class=\"diff from_file\">$patch_line</div>\n";
1746
1747                        $patch_line = <$fd>;
1748                        chomp $patch_line;
1749
1750                        #$patch_line =~ m/^+++/;
1751                        $file    = $diffinfo->{'to_file'};
1752                        $file  ||= $diffinfo->{'file'};
1753                        $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1754                                                       hash=>$diffinfo->{'to_id'}, file_name=>$file),
1755                                        -class => "list"}, esc_html($file));
1756                        $patch_line =~ s|b/.*|b/$file|g;
1757                        print "<div class=\"diff to_file\">$patch_line</div>\n";
1758
1759                        next LINE;
1760                }
1761                next LINE if $in_header;
1762
1763                print format_diff_line($patch_line);
1764        }
1765        print "</div>\n" if $patch_found; # class="patch"
1766
1767        print "</div>\n"; # class="patchset"
1768}
1769
1770# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1771
1772sub git_shortlog_body {
1773        # uses global variable $project
1774        my ($revlist, $from, $to, $refs, $extra) = @_;
1775
1776        my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
1777        my $have_snapshot = (defined $ctype && defined $suffix);
1778
1779        $from = 0 unless defined $from;
1780        $to = $#{$revlist} if (!defined $to || $#{$revlist} < $to);
1781
1782        print "<table class=\"shortlog\" cellspacing=\"0\">\n";
1783        my $alternate = 0;
1784        for (my $i = $from; $i <= $to; $i++) {
1785                my $commit = $revlist->[$i];
1786                #my $ref = defined $refs ? format_ref_marker($refs, $commit) : '';
1787                my $ref = format_ref_marker($refs, $commit);
1788                my %co = parse_commit($commit);
1789                if ($alternate) {
1790                        print "<tr class=\"dark\">\n";
1791                } else {
1792                        print "<tr class=\"light\">\n";
1793                }
1794                $alternate ^= 1;
1795                # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
1796                print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
1797                      "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
1798                      "<td>";
1799                print format_subject_html($co{'title'}, $co{'title_short'},
1800                                          href(action=>"commit", hash=>$commit), $ref);
1801                print "</td>\n" .
1802                      "<td class=\"link\">" .
1803                      $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
1804                      $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
1805                if ($have_snapshot) {
1806                        print " | " .  $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
1807                }
1808                print "</td>\n" .
1809                      "</tr>\n";
1810        }
1811        if (defined $extra) {
1812                print "<tr>\n" .
1813                      "<td colspan=\"4\">$extra</td>\n" .
1814                      "</tr>\n";
1815        }
1816        print "</table>\n";
1817}
1818
1819sub git_history_body {
1820        # Warning: assumes constant type (blob or tree) during history
1821        my ($fd, $refs, $hash_base, $ftype, $extra) = @_;
1822
1823        print "<table class=\"history\" cellspacing=\"0\">\n";
1824        my $alternate = 0;
1825        while (my $line = <$fd>) {
1826                if ($line !~ m/^([0-9a-fA-F]{40})/) {
1827                        next;
1828                }
1829
1830                my $commit = $1;
1831                my %co = parse_commit($commit);
1832                if (!%co) {
1833                        next;
1834                }
1835
1836                my $ref = format_ref_marker($refs, $commit);
1837
1838                if ($alternate) {
1839                        print "<tr class=\"dark\">\n";
1840                } else {
1841                        print "<tr class=\"light\">\n";
1842                }
1843                $alternate ^= 1;
1844                print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
1845                      # shortlog uses      chop_str($co{'author_name'}, 10)
1846                      "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
1847                      "<td>";
1848                # originally git_history used chop_str($co{'title'}, 50)
1849                print format_subject_html($co{'title'}, $co{'title_short'},
1850                                          href(action=>"commit", hash=>$commit), $ref);
1851                print "</td>\n" .
1852                      "<td class=\"link\">" .
1853                      $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
1854                      $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
1855                      $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype);
1856
1857                if ($ftype eq 'blob') {
1858                        my $blob_current = git_get_hash_by_path($hash_base, $file_name);
1859                        my $blob_parent  = git_get_hash_by_path($commit, $file_name);
1860                        if (defined $blob_current && defined $blob_parent &&
1861                                        $blob_current ne $blob_parent) {
1862                                print " | " .
1863                                        $cgi->a({-href => href(action=>"blobdiff",
1864                                                               hash=>$blob_current, hash_parent=>$blob_parent,
1865                                                               hash_base=>$hash_base, hash_parent_base=>$commit,
1866                                                               file_name=>$file_name)},
1867                                                "diff to current");
1868                        }
1869                }
1870                print "</td>\n" .
1871                      "</tr>\n";
1872        }
1873        if (defined $extra) {
1874                print "<tr>\n" .
1875                      "<td colspan=\"4\">$extra</td>\n" .
1876                      "</tr>\n";
1877        }
1878        print "</table>\n";
1879}
1880
1881sub git_tags_body {
1882        # uses global variable $project
1883        my ($taglist, $from, $to, $extra) = @_;
1884        $from = 0 unless defined $from;
1885        $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
1886
1887        print "<table class=\"tags\" cellspacing=\"0\">\n";
1888        my $alternate = 0;
1889        for (my $i = $from; $i <= $to; $i++) {
1890                my $entry = $taglist->[$i];
1891                my %tag = %$entry;
1892                my $comment_lines = $tag{'comment'};
1893                my $comment = shift @$comment_lines;
1894                my $comment_short;
1895                if (defined $comment) {
1896                        $comment_short = chop_str($comment, 30, 5);
1897                }
1898                if ($alternate) {
1899                        print "<tr class=\"dark\">\n";
1900                } else {
1901                        print "<tr class=\"light\">\n";
1902                }
1903                $alternate ^= 1;
1904                print "<td><i>$tag{'age'}</i></td>\n" .
1905                      "<td>" .
1906                      $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
1907                               -class => "list name"}, esc_html($tag{'name'})) .
1908                      "</td>\n" .
1909                      "<td>";
1910                if (defined $comment) {
1911                        print format_subject_html($comment, $comment_short,
1912                                                  href(action=>"tag", hash=>$tag{'id'}));
1913                }
1914                print "</td>\n" .
1915                      "<td class=\"selflink\">";
1916                if ($tag{'type'} eq "tag") {
1917                        print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
1918                } else {
1919                        print "&nbsp;";
1920                }
1921                print "</td>\n" .
1922                      "<td class=\"link\">" . " | " .
1923                      $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
1924                if ($tag{'reftype'} eq "commit") {
1925                        print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
1926                              " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'refid'})}, "log");
1927                } elsif ($tag{'reftype'} eq "blob") {
1928                        print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
1929                }
1930                print "</td>\n" .
1931                      "</tr>";
1932        }
1933        if (defined $extra) {
1934                print "<tr>\n" .
1935                      "<td colspan=\"5\">$extra</td>\n" .
1936                      "</tr>\n";
1937        }
1938        print "</table>\n";
1939}
1940
1941sub git_heads_body {
1942        # uses global variable $project
1943        my ($taglist, $head, $from, $to, $extra) = @_;
1944        $from = 0 unless defined $from;
1945        $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
1946
1947        print "<table class=\"heads\" cellspacing=\"0\">\n";
1948        my $alternate = 0;
1949        for (my $i = $from; $i <= $to; $i++) {
1950                my $entry = $taglist->[$i];
1951                my %tag = %$entry;
1952                my $curr = $tag{'id'} eq $head;
1953                if ($alternate) {
1954                        print "<tr class=\"dark\">\n";
1955                } else {
1956                        print "<tr class=\"light\">\n";
1957                }
1958                $alternate ^= 1;
1959                print "<td><i>$tag{'age'}</i></td>\n" .
1960                      ($tag{'id'} eq $head ? "<td class=\"current_head\">" : "<td>") .
1961                      $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'}),
1962                               -class => "list name"},esc_html($tag{'name'})) .
1963                      "</td>\n" .
1964                      "<td class=\"link\">" .
1965                      $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") . " | " .
1966                      $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log") .
1967                      "</td>\n" .
1968                      "</tr>";
1969        }
1970        if (defined $extra) {
1971                print "<tr>\n" .
1972                      "<td colspan=\"3\">$extra</td>\n" .
1973                      "</tr>\n";
1974        }
1975        print "</table>\n";
1976}
1977
1978## ======================================================================
1979## ======================================================================
1980## actions
1981
1982sub git_project_list {
1983        my $order = $cgi->param('o');
1984        if (defined $order && $order !~ m/project|descr|owner|age/) {
1985                die_error(undef, "Unknown order parameter");
1986        }
1987
1988        my @list = git_get_projects_list();
1989        my @projects;
1990        if (!@list) {
1991                die_error(undef, "No projects found");
1992        }
1993        foreach my $pr (@list) {
1994                my $head = git_get_head_hash($pr->{'path'});
1995                if (!defined $head) {
1996                        next;
1997                }
1998                $ENV{'GIT_DIR'} = "$projectroot/$pr->{'path'}";
1999                my %co = parse_commit($head);
2000                if (!%co) {
2001                        next;
2002                }
2003                $pr->{'commit'} = \%co;
2004                if (!defined $pr->{'descr'}) {
2005                        my $descr = git_get_project_description($pr->{'path'}) || "";
2006                        $pr->{'descr'} = chop_str($descr, 25, 5);
2007                }
2008                if (!defined $pr->{'owner'}) {
2009                        $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
2010                }
2011                push @projects, $pr;
2012        }
2013
2014        git_header_html();
2015        if (-f $home_text) {
2016                print "<div class=\"index_include\">\n";
2017                open (my $fd, $home_text);
2018                print <$fd>;
2019                close $fd;
2020                print "</div>\n";
2021        }
2022        print "<table class=\"project_list\">\n" .
2023              "<tr>\n";
2024        $order ||= "project";
2025        if ($order eq "project") {
2026                @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
2027                print "<th>Project</th>\n";
2028        } else {
2029                print "<th>" .
2030                      $cgi->a({-href => "$my_uri?" . esc_param("o=project"),
2031                               -class => "header"}, "Project") .
2032                      "</th>\n";
2033        }
2034        if ($order eq "descr") {
2035                @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
2036                print "<th>Description</th>\n";
2037        } else {
2038                print "<th>" .
2039                      $cgi->a({-href => "$my_uri?" . esc_param("o=descr"),
2040                               -class => "header"}, "Description") .
2041                      "</th>\n";
2042        }
2043        if ($order eq "owner") {
2044                @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
2045                print "<th>Owner</th>\n";
2046        } else {
2047                print "<th>" .
2048                      $cgi->a({-href => "$my_uri?" . esc_param("o=owner"),
2049                               -class => "header"}, "Owner") .
2050                      "</th>\n";
2051        }
2052        if ($order eq "age") {
2053                @projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;
2054                print "<th>Last Change</th>\n";
2055        } else {
2056                print "<th>" .
2057                      $cgi->a({-href => "$my_uri?" . esc_param("o=age"),
2058                               -class => "header"}, "Last Change") .
2059                      "</th>\n";
2060        }
2061        print "<th></th>\n" .
2062              "</tr>\n";
2063        my $alternate = 0;
2064        foreach my $pr (@projects) {
2065                if ($alternate) {
2066                        print "<tr class=\"dark\">\n";
2067                } else {
2068                        print "<tr class=\"light\">\n";
2069                }
2070                $alternate ^= 1;
2071                print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2072                                        -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
2073                      "<td>" . esc_html($pr->{'descr'}) . "</td>\n" .
2074                      "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
2075                print "<td class=\"". age_class($pr->{'commit'}{'age'}) . "\">" .
2076                      $pr->{'commit'}{'age_string'} . "</td>\n" .
2077                      "<td class=\"link\">" .
2078                      $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary")   . " | " .
2079                      $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
2080                      $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") .
2081                      "</td>\n" .
2082                      "</tr>\n";
2083        }
2084        print "</table>\n";
2085        git_footer_html();
2086}
2087
2088sub git_summary {
2089        my $descr = git_get_project_description($project) || "none";
2090        my $head = git_get_head_hash($project);
2091        my %co = parse_commit($head);
2092        my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2093
2094        my $owner = git_get_project_owner($project);
2095
2096        my $refs = git_get_references();
2097        git_header_html();
2098        git_print_page_nav('summary','', $head);
2099
2100        print "<div class=\"title\">&nbsp;</div>\n";
2101        print "<table cellspacing=\"0\">\n" .
2102              "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
2103              "<tr><td>owner</td><td>$owner</td></tr>\n" .
2104              "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
2105        # use per project git URL list in $projectroot/$project/cloneurl
2106        # or make project git URL from git base URL and project name
2107        my $url_tag = "URL";
2108        my @url_list = git_get_project_url_list($project);
2109        @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
2110        foreach my $git_url (@url_list) {
2111                next unless $git_url;
2112                print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
2113                $url_tag = "";
2114        }
2115        print "</table>\n";
2116
2117        open my $fd, "-|", $GIT, "rev-list", "--max-count=17", git_get_head_hash($project)
2118                or die_error(undef, "Open git-rev-list failed");
2119        my @revlist = map { chomp; $_ } <$fd>;
2120        close $fd;
2121        git_print_header_div('shortlog');
2122        git_shortlog_body(\@revlist, 0, 15, $refs,
2123                          $cgi->a({-href => href(action=>"shortlog")}, "..."));
2124
2125        my $taglist = git_get_refs_list("refs/tags");
2126        if (defined @$taglist) {
2127                git_print_header_div('tags');
2128                git_tags_body($taglist, 0, 15,
2129                              $cgi->a({-href => href(action=>"tags")}, "..."));
2130        }
2131
2132        my $headlist = git_get_refs_list("refs/heads");
2133        if (defined @$headlist) {
2134                git_print_header_div('heads');
2135                git_heads_body($headlist, $head, 0, 15,
2136                               $cgi->a({-href => href(action=>"heads")}, "..."));
2137        }
2138
2139        git_footer_html();
2140}
2141
2142sub git_tag {
2143        my $head = git_get_head_hash($project);
2144        git_header_html();
2145        git_print_page_nav('','', $head,undef,$head);
2146        my %tag = parse_tag($hash);
2147        git_print_header_div('commit', esc_html($tag{'name'}), $hash);
2148        print "<div class=\"title_text\">\n" .
2149              "<table cellspacing=\"0\">\n" .
2150              "<tr>\n" .
2151              "<td>object</td>\n" .
2152              "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2153                               $tag{'object'}) . "</td>\n" .
2154              "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2155                                              $tag{'type'}) . "</td>\n" .
2156              "</tr>\n";
2157        if (defined($tag{'author'})) {
2158                my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
2159                print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
2160                print "<tr><td></td><td>" . $ad{'rfc2822'} .
2161                        sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
2162                        "</td></tr>\n";
2163        }
2164        print "</table>\n\n" .
2165              "</div>\n";
2166        print "<div class=\"page_body\">";
2167        my $comment = $tag{'comment'};
2168        foreach my $line (@$comment) {
2169                print esc_html($line) . "<br/>\n";
2170        }
2171        print "</div>\n";
2172        git_footer_html();
2173}
2174
2175sub git_blame2 {
2176        my $fd;
2177        my $ftype;
2178
2179        if (!gitweb_check_feature('blame')) {
2180                die_error('403 Permission denied', "Permission denied");
2181        }
2182        die_error('404 Not Found', "File name not defined") if (!$file_name);
2183        $hash_base ||= git_get_head_hash($project);
2184        die_error(undef, "Couldn't find base commit") unless ($hash_base);
2185        my %co = parse_commit($hash_base)
2186                or die_error(undef, "Reading commit failed");
2187        if (!defined $hash) {
2188                $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2189                        or die_error(undef, "Error looking up file");
2190        }
2191        $ftype = git_get_type($hash);
2192        if ($ftype !~ "blob") {
2193                die_error("400 Bad Request", "Object is not a blob");
2194        }
2195        open ($fd, "-|", $GIT, "blame", '-l', $file_name, $hash_base)
2196                or die_error(undef, "Open git-blame failed");
2197        git_header_html();
2198        my $formats_nav =
2199                $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2200                        "blob") .
2201                " | " .
2202                $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2203                        "head");
2204        git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2205        git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2206        git_print_page_path($file_name, $ftype, $hash_base);
2207        my @rev_color = (qw(light2 dark2));
2208        my $num_colors = scalar(@rev_color);
2209        my $current_color = 0;
2210        my $last_rev;
2211        print <<HTML;
2212<div class="page_body">
2213<table class="blame">
2214<tr><th>Commit</th><th>Line</th><th>Data</th></tr>
2215HTML
2216        while (<$fd>) {
2217                /^([0-9a-fA-F]{40}).*?(\d+)\)\s{1}(\s*.*)/;
2218                my $full_rev = $1;
2219                my $rev = substr($full_rev, 0, 8);
2220                my $lineno = $2;
2221                my $data = $3;
2222
2223                if (!defined $last_rev) {
2224                        $last_rev = $full_rev;
2225                } elsif ($last_rev ne $full_rev) {
2226                        $last_rev = $full_rev;
2227                        $current_color = ++$current_color % $num_colors;
2228                }
2229                print "<tr class=\"$rev_color[$current_color]\">\n";
2230                print "<td class=\"sha1\">" .
2231                        $cgi->a({-href => href(action=>"commit", hash=>$full_rev, file_name=>$file_name)},
2232                                esc_html($rev)) . "</td>\n";
2233                print "<td class=\"linenr\"><a id=\"l$lineno\" href=\"#l$lineno\" class=\"linenr\">" .
2234                      esc_html($lineno) . "</a></td>\n";
2235                print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
2236                print "</tr>\n";
2237        }
2238        print "</table>\n";
2239        print "</div>";
2240        close $fd
2241                or print "Reading blob failed\n";
2242        git_footer_html();
2243}
2244
2245sub git_blame {
2246        my $fd;
2247
2248        if (!gitweb_check_feature('blame')) {
2249                die_error('403 Permission denied', "Permission denied");
2250        }
2251        die_error('404 Not Found', "File name not defined") if (!$file_name);
2252        $hash_base ||= git_get_head_hash($project);
2253        die_error(undef, "Couldn't find base commit") unless ($hash_base);
2254        my %co = parse_commit($hash_base)
2255                or die_error(undef, "Reading commit failed");
2256        if (!defined $hash) {
2257                $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2258                        or die_error(undef, "Error lookup file");
2259        }
2260        open ($fd, "-|", $GIT, "annotate", '-l', '-t', '-r', $file_name, $hash_base)
2261                or die_error(undef, "Open git-annotate failed");
2262        git_header_html();
2263        my $formats_nav =
2264                $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2265                        "blob") .
2266                " | " .
2267                $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2268                        "head");
2269        git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2270        git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2271        git_print_page_path($file_name, 'blob', $hash_base);
2272        print "<div class=\"page_body\">\n";
2273        print <<HTML;
2274<table class="blame">
2275  <tr>
2276    <th>Commit</th>
2277    <th>Age</th>
2278    <th>Author</th>
2279    <th>Line</th>
2280    <th>Data</th>
2281  </tr>
2282HTML
2283        my @line_class = (qw(light dark));
2284        my $line_class_len = scalar (@line_class);
2285        my $line_class_num = $#line_class;
2286        while (my $line = <$fd>) {
2287                my $long_rev;
2288                my $short_rev;
2289                my $author;
2290                my $time;
2291                my $lineno;
2292                my $data;
2293                my $age;
2294                my $age_str;
2295                my $age_class;
2296
2297                chomp $line;
2298                $line_class_num = ($line_class_num + 1) % $line_class_len;
2299
2300                if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) \+\d\d\d\d\t(\d+)\)(.*)$/) {
2301                        $long_rev = $1;
2302                        $author   = $2;
2303                        $time     = $3;
2304                        $lineno   = $4;
2305                        $data     = $5;
2306                } else {
2307                        print qq(  <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
2308                        next;
2309                }
2310                $short_rev  = substr ($long_rev, 0, 8);
2311                $age        = time () - $time;
2312                $age_str    = age_string ($age);
2313                $age_str    =~ s/ /&nbsp;/g;
2314                $age_class  = age_class($age);
2315                $author     = esc_html ($author);
2316                $author     =~ s/ /&nbsp;/g;
2317
2318                $data = untabify($data);
2319                $data = esc_html ($data);
2320
2321                print <<HTML;
2322  <tr class="$line_class[$line_class_num]">
2323    <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
2324    <td class="$age_class">$age_str</td>
2325    <td>$author</td>
2326    <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
2327    <td class="pre">$data</td>
2328  </tr>
2329HTML
2330        } # while (my $line = <$fd>)
2331        print "</table>\n\n";
2332        close $fd
2333                or print "Reading blob failed.\n";
2334        print "</div>";
2335        git_footer_html();
2336}
2337
2338sub git_tags {
2339        my $head = git_get_head_hash($project);
2340        git_header_html();
2341        git_print_page_nav('','', $head,undef,$head);
2342        git_print_header_div('summary', $project);
2343
2344        my $taglist = git_get_refs_list("refs/tags");
2345        if (defined @$taglist) {
2346                git_tags_body($taglist);
2347        }
2348        git_footer_html();
2349}
2350
2351sub git_heads {
2352        my $head = git_get_head_hash($project);
2353        git_header_html();
2354        git_print_page_nav('','', $head,undef,$head);
2355        git_print_header_div('summary', $project);
2356
2357        my $taglist = git_get_refs_list("refs/heads");
2358        if (defined @$taglist) {
2359                git_heads_body($taglist, $head);
2360        }
2361        git_footer_html();
2362}
2363
2364sub git_blob_plain {
2365        if (!defined $hash) {
2366                if (defined $file_name) {
2367                        my $base = $hash_base || git_get_head_hash($project);
2368                        $hash = git_get_hash_by_path($base, $file_name, "blob")
2369                                or die_error(undef, "Error lookup file");
2370                } else {
2371                        die_error(undef, "No file name defined");
2372                }
2373        }
2374        my $type = shift;
2375        open my $fd, "-|", $GIT, "cat-file", "blob", $hash
2376                or die_error(undef, "Couldn't cat $file_name, $hash");
2377
2378        $type ||= blob_mimetype($fd, $file_name);
2379
2380        # save as filename, even when no $file_name is given
2381        my $save_as = "$hash";
2382        if (defined $file_name) {
2383                $save_as = $file_name;
2384        } elsif ($type =~ m/^text\//) {
2385                $save_as .= '.txt';
2386        }
2387
2388        print $cgi->header(-type => "$type",
2389                           -content_disposition => "inline; filename=\"$save_as\"");
2390        undef $/;
2391        binmode STDOUT, ':raw';
2392        print <$fd>;
2393        binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2394        $/ = "\n";
2395        close $fd;
2396}
2397
2398sub git_blob {
2399        if (!defined $hash) {
2400                if (defined $file_name) {
2401                        my $base = $hash_base || git_get_head_hash($project);
2402                        $hash = git_get_hash_by_path($base, $file_name, "blob")
2403                                or die_error(undef, "Error lookup file");
2404                } else {
2405                        die_error(undef, "No file name defined");
2406                }
2407        }
2408        my $have_blame = gitweb_check_feature('blame');
2409        open my $fd, "-|", $GIT, "cat-file", "blob", $hash
2410                or die_error(undef, "Couldn't cat $file_name, $hash");
2411        my $mimetype = blob_mimetype($fd, $file_name);
2412        if ($mimetype !~ m/^text\//) {
2413                close $fd;
2414                return git_blob_plain($mimetype);
2415        }
2416        git_header_html();
2417        my $formats_nav = '';
2418        if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2419                if (defined $file_name) {
2420                        if ($have_blame) {
2421                                $formats_nav .=
2422                                        $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
2423                                                               hash=>$hash, file_name=>$file_name)},
2424                                                "blame") .
2425                                        " | ";
2426                        }
2427                        $formats_nav .=
2428                                $cgi->a({-href => href(action=>"blob_plain",
2429                                                       hash=>$hash, file_name=>$file_name)},
2430                                        "plain") .
2431                                " | " .
2432                                $cgi->a({-href => href(action=>"blob",
2433                                                       hash_base=>"HEAD", file_name=>$file_name)},
2434                                        "head");
2435                } else {
2436                        $formats_nav .=
2437                                $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "plain");
2438                }
2439                git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2440                git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2441        } else {
2442                print "<div class=\"page_nav\">\n" .
2443                      "<br/><br/></div>\n" .
2444                      "<div class=\"title\">$hash</div>\n";
2445        }
2446        git_print_page_path($file_name, "blob", $hash_base);
2447        print "<div class=\"page_body\">\n";
2448        my $nr;
2449        while (my $line = <$fd>) {
2450                chomp $line;
2451                $nr++;
2452                $line = untabify($line);
2453                printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
2454                       $nr, $nr, $nr, esc_html($line);
2455        }
2456        close $fd
2457                or print "Reading blob failed.\n";
2458        print "</div>";
2459        git_footer_html();
2460}
2461
2462sub git_tree {
2463        if (!defined $hash) {
2464                $hash = git_get_head_hash($project);
2465                if (defined $file_name) {
2466                        my $base = $hash_base || $hash;
2467                        $hash = git_get_hash_by_path($base, $file_name, "tree");
2468                }
2469                if (!defined $hash_base) {
2470                        $hash_base = $hash;
2471                }
2472        }
2473        $/ = "\0";
2474        open my $fd, "-|", $GIT, "ls-tree", '-z', $hash
2475                or die_error(undef, "Open git-ls-tree failed");
2476        my @entries = map { chomp; $_ } <$fd>;
2477        close $fd or die_error(undef, "Reading tree failed");
2478        $/ = "\n";
2479
2480        my $refs = git_get_references();
2481        my $ref = format_ref_marker($refs, $hash_base);
2482        git_header_html();
2483        my %base_key = ();
2484        my $base = "";
2485        my $have_blame = gitweb_check_feature('blame');
2486        if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2487                $base_key{hash_base} = $hash_base;
2488                git_print_page_nav('tree','', $hash_base);
2489                git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
2490        } else {
2491                print "<div class=\"page_nav\">\n";
2492                print "<br/><br/></div>\n";
2493                print "<div class=\"title\">$hash</div>\n";
2494        }
2495        if (defined $file_name) {
2496                $base = esc_html("$file_name/");
2497        }
2498        git_print_page_path($file_name, 'tree', $hash_base);
2499        print "<div class=\"page_body\">\n";
2500        print "<table cellspacing=\"0\">\n";
2501        my $alternate = 0;
2502        foreach my $line (@entries) {
2503                #'100644        blob    0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa        panic.c'
2504                $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
2505                my $t_mode = $1;
2506                my $t_type = $2;
2507                my $t_hash = $3;
2508                my $t_name = validate_input($4);
2509                if ($alternate) {
2510                        print "<tr class=\"dark\">\n";
2511                } else {
2512                        print "<tr class=\"light\">\n";
2513                }
2514                $alternate ^= 1;
2515                print "<td class=\"mode\">" . mode_str($t_mode) . "</td>\n";
2516                if ($t_type eq "blob") {
2517                        print "<td class=\"list\">" .
2518                              $cgi->a({-href => href(action=>"blob", hash=>$t_hash, file_name=>"$base$t_name", %base_key),
2519                                      -class => "list"}, esc_html($t_name)) .
2520                              "</td>\n" .
2521                              "<td class=\"link\">" .
2522                              $cgi->a({-href => href(action=>"blob", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2523                                      "blob");
2524                        if ($have_blame) {
2525                                print " | " .
2526                                        $cgi->a({-href => href(action=>"blame", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2527                                                "blame");
2528                        }
2529                        print " | " .
2530                              $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2531                                                     hash=>$t_hash, file_name=>"$base$t_name")},
2532                                      "history") .
2533                              " | " .
2534                              $cgi->a({-href => href(action=>"blob_plain",
2535                                                     hash=>$t_hash, file_name=>"$base$t_name")},
2536                                      "raw") .
2537                              "</td>\n";
2538                } elsif ($t_type eq "tree") {
2539                        print "<td class=\"list\">" .
2540                              $cgi->a({-href => href(action=>"tree", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2541                                      esc_html($t_name)) .
2542                              "</td>\n" .
2543                              "<td class=\"link\">" .
2544                              $cgi->a({-href => href(action=>"tree", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2545                                      "tree") .
2546                              " | " .
2547                              $cgi->a({-href => href(action=>"history", hash_base=>$hash_base, file_name=>"$base$t_name")},
2548                                      "history") .
2549                              "</td>\n";
2550                }
2551                print "</tr>\n";
2552        }
2553        print "</table>\n" .
2554              "</div>";
2555        git_footer_html();
2556}
2557
2558sub git_snapshot {
2559
2560        my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2561        my $have_snapshot = (defined $ctype && defined $suffix);
2562        if (!$have_snapshot) {
2563                die_error('403 Permission denied', "Permission denied");
2564        }
2565
2566        if (!defined $hash) {
2567                $hash = git_get_head_hash($project);
2568        }
2569
2570        my $filename = basename($project) . "-$hash.tar.$suffix";
2571
2572        print $cgi->header(-type => 'application/x-tar',
2573                           -content_encoding => $ctype,
2574                           -content_disposition => "inline; filename=\"$filename\"",
2575                           -status => '200 OK');
2576
2577        open my $fd, "-|", "$GIT tar-tree $hash \'$project\' | $command" or
2578                die_error(undef, "Execute git-tar-tree failed.");
2579        binmode STDOUT, ':raw';
2580        print <$fd>;
2581        binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2582        close $fd;
2583
2584}
2585
2586sub git_log {
2587        my $head = git_get_head_hash($project);
2588        if (!defined $hash) {
2589                $hash = $head;
2590        }
2591        if (!defined $page) {
2592                $page = 0;
2593        }
2594        my $refs = git_get_references();
2595
2596        my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
2597        open my $fd, "-|", $GIT, "rev-list", $limit, $hash
2598                or die_error(undef, "Open git-rev-list failed");
2599        my @revlist = map { chomp; $_ } <$fd>;
2600        close $fd;
2601
2602        my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#revlist);
2603
2604        git_header_html();
2605        git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
2606
2607        if (!@revlist) {
2608                my %co = parse_commit($hash);
2609
2610                git_print_header_div('summary', $project);
2611                print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
2612        }
2613        for (my $i = ($page * 100); $i <= $#revlist; $i++) {
2614                my $commit = $revlist[$i];
2615                my $ref = format_ref_marker($refs, $commit);
2616                my %co = parse_commit($commit);
2617                next if !%co;
2618                my %ad = parse_date($co{'author_epoch'});
2619                git_print_header_div('commit',
2620                               "<span class=\"age\">$co{'age_string'}</span>" .
2621                               esc_html($co{'title'}) . $ref,
2622                               $commit);
2623                print "<div class=\"title_text\">\n" .
2624                      "<div class=\"log_link\">\n" .
2625                      $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
2626                      " | " .
2627                      $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
2628                      "<br/>\n" .
2629                      "</div>\n" .
2630                      "<i>" . esc_html($co{'author_name'}) .  " [$ad{'rfc2822'}]</i><br/>\n" .
2631                      "</div>\n";
2632
2633                print "<div class=\"log_body\">\n";
2634                git_print_simplified_log($co{'comment'});
2635                print "</div>\n";
2636        }
2637        git_footer_html();
2638}
2639
2640sub git_commit {
2641        my %co = parse_commit($hash);
2642        if (!%co) {
2643                die_error(undef, "Unknown commit object");
2644        }
2645        my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
2646        my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2647
2648        my $parent = $co{'parent'};
2649        if (!defined $parent) {
2650                $parent = "--root";
2651        }
2652        open my $fd, "-|", $GIT, "diff-tree", '-r', '-M', $parent, $hash
2653                or die_error(undef, "Open git-diff-tree failed");
2654        my @difftree = map { chomp; $_ } <$fd>;
2655        close $fd or die_error(undef, "Reading git-diff-tree failed");
2656
2657        # non-textual hash id's can be cached
2658        my $expires;
2659        if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2660                $expires = "+1d";
2661        }
2662        my $refs = git_get_references();
2663        my $ref = format_ref_marker($refs, $co{'id'});
2664
2665        my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2666        my $have_snapshot = (defined $ctype && defined $suffix);
2667
2668        my $formats_nav = '';
2669        if (defined $file_name && defined $co{'parent'}) {
2670                my $parent = $co{'parent'};
2671                $formats_nav .=
2672                        $cgi->a({-href => href(action=>"blame", hash_parent=>$parent, file_name=>$file_name)},
2673                                "blame");
2674        }
2675        git_header_html(undef, $expires);
2676        git_print_page_nav('commit', defined $co{'parent'} ? '' : 'commitdiff',
2677                           $hash, $co{'tree'}, $hash,
2678                           $formats_nav);
2679
2680        if (defined $co{'parent'}) {
2681                git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
2682        } else {
2683                git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
2684        }
2685        print "<div class=\"title_text\">\n" .
2686              "<table cellspacing=\"0\">\n";
2687        print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
2688              "<tr>" .
2689              "<td></td><td> $ad{'rfc2822'}";
2690        if ($ad{'hour_local'} < 6) {
2691                printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2692                       $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2693        } else {
2694                printf(" (%02d:%02d %s)",
2695                       $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2696        }
2697        print "</td>" .
2698              "</tr>\n";
2699        print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
2700        print "<tr><td></td><td> $cd{'rfc2822'}" .
2701              sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
2702              "</td></tr>\n";
2703        print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
2704        print "<tr>" .
2705              "<td>tree</td>" .
2706              "<td class=\"sha1\">" .
2707              $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
2708                       class => "list"}, $co{'tree'}) .
2709              "</td>" .
2710              "<td class=\"link\">" .
2711              $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
2712                      "tree");
2713        if ($have_snapshot) {
2714                print " | " .
2715                      $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
2716        }
2717        print "</td>" .
2718              "</tr>\n";
2719        my $parents = $co{'parents'};
2720        foreach my $par (@$parents) {
2721                print "<tr>" .
2722                      "<td>parent</td>" .
2723                      "<td class=\"sha1\">" .
2724                      $cgi->a({-href => href(action=>"commit", hash=>$par),
2725                               class => "list"}, $par) .
2726                      "</td>" .
2727                      "<td class=\"link\">" .
2728                      $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
2729                      " | " .
2730                      $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "commitdiff") .
2731                      "</td>" .
2732                      "</tr>\n";
2733        }
2734        print "</table>".
2735              "</div>\n";
2736
2737        print "<div class=\"page_body\">\n";
2738        git_print_log($co{'comment'});
2739        print "</div>\n";
2740
2741        git_difftree_body(\@difftree, $hash, $parent);
2742
2743        git_footer_html();
2744}
2745
2746sub git_blobdiff {
2747        my $format = shift || 'html';
2748
2749        my $fd;
2750        my @difftree;
2751        my %diffinfo;
2752        my $expires;
2753
2754        # preparing $fd and %diffinfo for git_patchset_body
2755        # new style URI
2756        if (defined $hash_base && defined $hash_parent_base) {
2757                if (defined $file_name) {
2758                        # read raw output
2759                        open $fd, "-|", $GIT, "diff-tree", '-r', '-M', '-C', $hash_parent_base, $hash_base,
2760                                "--", $file_name
2761                                or die_error(undef, "Open git-diff-tree failed");
2762                        @difftree = map { chomp; $_ } <$fd>;
2763                        close $fd
2764                                or die_error(undef, "Reading git-diff-tree failed");
2765                        @difftree
2766                                or die_error('404 Not Found', "Blob diff not found");
2767
2768                } elsif (defined $hash) { # try to find filename from $hash
2769                        if ($hash !~ /[0-9a-fA-F]{40}/) {
2770                                $hash = git_to_hash($hash);
2771                        }
2772
2773                        # read filtered raw output
2774                        open $fd, "-|", $GIT, "diff-tree", '-r', '-M', '-C', $hash_parent_base, $hash_base
2775                                or die_error(undef, "Open git-diff-tree failed");
2776                        @difftree =
2777                                # ':100644 100644 03b21826... 3b93d5e7... M     ls-files.c'
2778                                # $hash == to_id
2779                                grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
2780                                map { chomp; $_ } <$fd>;
2781                        close $fd
2782                                or die_error(undef, "Reading git-diff-tree failed");
2783                        @difftree
2784                                or die_error('404 Not Found', "Blob diff not found");
2785
2786                } else {
2787                        die_error('404 Not Found', "Missing one of the blob diff parameters");
2788                }
2789
2790                if (@difftree > 1) {
2791                        die_error('404 Not Found', "Ambiguous blob diff specification");
2792                }
2793
2794                %diffinfo = parse_difftree_raw_line($difftree[0]);
2795                $file_parent ||= $diffinfo{'from_file'} || $file_name || $diffinfo{'file'};
2796                $file_name   ||= $diffinfo{'to_file'}   || $diffinfo{'file'};
2797
2798                $hash_parent ||= $diffinfo{'from_id'};
2799                $hash        ||= $diffinfo{'to_id'};
2800
2801                # non-textual hash id's can be cached
2802                if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
2803                    $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
2804                        $expires = '+1d';
2805                }
2806
2807                # open patch output
2808                open $fd, "-|", $GIT, "diff-tree", '-r', '-p', '-M', '-C', $hash_parent_base, $hash_base,
2809                        "--", $file_name
2810                        or die_error(undef, "Open git-diff-tree failed");
2811        }
2812
2813        # old/legacy style URI
2814        if (!%diffinfo && # if new style URI failed
2815            defined $hash && defined $hash_parent) {
2816                # fake git-diff-tree raw output
2817                $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
2818                $diffinfo{'from_id'} = $hash_parent;
2819                $diffinfo{'to_id'}   = $hash;
2820                if (defined $file_name) {
2821                        if (defined $file_parent) {
2822                                $diffinfo{'status'} = '2';
2823                                $diffinfo{'from_file'} = $file_parent;
2824                                $diffinfo{'to_file'}   = $file_name;
2825                        } else { # assume not renamed
2826                                $diffinfo{'status'} = '1';
2827                                $diffinfo{'from_file'} = $file_name;
2828                                $diffinfo{'to_file'}   = $file_name;
2829                        }
2830                } else { # no filename given
2831                        $diffinfo{'status'} = '2';
2832                        $diffinfo{'from_file'} = $hash_parent;
2833                        $diffinfo{'to_file'}   = $hash;
2834                }
2835
2836                # non-textual hash id's can be cached
2837                if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
2838                    $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
2839                        $expires = '+1d';
2840                }
2841
2842                # open patch output
2843                #open $fd, "-|", $GIT, "diff", '-p', $hash_parent, $hash
2844                open $fd, "-|", $GIT, "diff", '-p', $hash, $hash_parent
2845                        or die_error(undef, "Open git-diff failed");
2846        } else  {
2847                die_error('404 Not Found', "Missing one of the blob diff parameters")
2848                        unless %diffinfo;
2849        }
2850
2851        # header
2852        if ($format eq 'html') {
2853                my $formats_nav =
2854                        $cgi->a({-href => href(action=>"blobdiff_plain",
2855                                               hash=>$hash, hash_parent=>$hash_parent,
2856                                               hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
2857                                               file_name=>$file_name, file_parent=>$file_parent)},
2858                                "plain");
2859                git_header_html(undef, $expires);
2860                if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2861                        git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2862                        git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2863                } else {
2864                        print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
2865                        print "<div class=\"title\">$hash vs $hash_parent</div>\n";
2866                }
2867                if (defined $file_name) {
2868                        git_print_page_path($file_name, "blob", $hash_base);
2869                } else {
2870                        print "<div class=\"page_path\"></div>\n";
2871                }
2872
2873        } elsif ($format eq 'plain') {
2874                print $cgi->header(
2875                        -type => 'text/plain',
2876                        -charset => 'utf-8',
2877                        -expires => $expires,
2878                        -content_disposition => qq(inline; filename="${file_name}.patch"));
2879
2880                print "X-Git-Url: " . $cgi->self_url() . "\n\n";
2881
2882        } else {
2883                die_error(undef, "Unknown blobdiff format");
2884        }
2885
2886        # patch
2887        if ($format eq 'html') {
2888                print "<div class=\"page_body\">\n";
2889
2890                git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
2891                close $fd;
2892
2893                print "</div>\n"; # class="page_body"
2894                git_footer_html();
2895
2896        } else {
2897                while (my $line = <$fd>) {
2898                        $line =~ s!a/($hash|$hash_parent)!a/$diffinfo{'from_file'}!g;
2899                        $line =~ s!b/($hash|$hash_parent)!b/$diffinfo{'to_file'}!g;
2900
2901                        print $line;
2902
2903                        last if $line =~ m!^\+\+\+!;
2904                }
2905                local $/ = undef;
2906                print <$fd>;
2907                close $fd;
2908        }
2909}
2910
2911sub git_blobdiff_plain {
2912        git_blobdiff('plain');
2913}
2914
2915sub git_commitdiff {
2916        my $format = shift || 'html';
2917        my %co = parse_commit($hash);
2918        if (!%co) {
2919                die_error(undef, "Unknown commit object");
2920        }
2921        if (!defined $hash_parent) {
2922                $hash_parent = $co{'parent'} || '--root';
2923        }
2924
2925        # read commitdiff
2926        my $fd;
2927        my @difftree;
2928        if ($format eq 'html') {
2929                open $fd, "-|", $GIT, "diff-tree", '-r', '-M', '-C',
2930                        "--patch-with-raw", "--full-index", $hash_parent, $hash
2931                        or die_error(undef, "Open git-diff-tree failed");
2932
2933                while (chomp(my $line = <$fd>)) {
2934                        # empty line ends raw part of diff-tree output
2935                        last unless $line;
2936                        push @difftree, $line;
2937                }
2938
2939        } elsif ($format eq 'plain') {
2940                open $fd, "-|", $GIT, "diff-tree", '-r', '-p', '-B', $hash_parent, $hash
2941                        or die_error(undef, "Open git-diff-tree failed");
2942
2943        } else {
2944                die_error(undef, "Unknown commitdiff format");
2945        }
2946
2947        # non-textual hash id's can be cached
2948        my $expires;
2949        if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2950                $expires = "+1d";
2951        }
2952
2953        # write commit message
2954        if ($format eq 'html') {
2955                my $refs = git_get_references();
2956                my $ref = format_ref_marker($refs, $co{'id'});
2957                my $formats_nav =
2958                        $cgi->a({-href => href(action=>"commitdiff_plain",
2959                                               hash=>$hash, hash_parent=>$hash_parent)},
2960                                "plain");
2961
2962                git_header_html(undef, $expires);
2963                git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
2964                git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
2965                print "<div class=\"page_body\">\n";
2966                print "<div class=\"log\">\n";
2967                git_print_simplified_log($co{'comment'}, 1); # skip title
2968                print "</div>\n"; # class="log"
2969
2970        } elsif ($format eq 'plain') {
2971                my $refs = git_get_references("tags");
2972                my $tagname = git_get_rev_name_tags($hash);
2973                my $filename = basename($project) . "-$hash.patch";
2974
2975                print $cgi->header(
2976                        -type => 'text/plain',
2977                        -charset => 'utf-8',
2978                        -expires => $expires,
2979                        -content_disposition => qq(inline; filename="$filename"));
2980                my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
2981                print <<TEXT;
2982From: $co{'author'}
2983Date: $ad{'rfc2822'} ($ad{'tz_local'})
2984Subject: $co{'title'}
2985TEXT
2986                print "X-Git-Tag: $tagname\n" if $tagname;
2987                print "X-Git-Url: " . $cgi->self_url() . "\n\n";
2988
2989                foreach my $line (@{$co{'comment'}}) {
2990                        print "$line\n";
2991                }
2992                print "---\n\n";
2993        }
2994
2995        # write patch
2996        if ($format eq 'html') {
2997                #git_difftree_body(\@difftree, $hash, $hash_parent);
2998                #print "<br/>\n";
2999
3000                git_patchset_body($fd, \@difftree, $hash, $hash_parent);
3001                close $fd;
3002                print "</div>\n"; # class="page_body"
3003                git_footer_html();
3004
3005        } elsif ($format eq 'plain') {
3006                local $/ = undef;
3007                print <$fd>;
3008                close $fd
3009                        or print "Reading git-diff-tree failed\n";
3010        }
3011}
3012
3013sub git_commitdiff_plain {
3014        git_commitdiff('plain');
3015}
3016
3017sub git_history {
3018        if (!defined $hash_base) {
3019                $hash_base = git_get_head_hash($project);
3020        }
3021        my $ftype;
3022        my %co = parse_commit($hash_base);
3023        if (!%co) {
3024                die_error(undef, "Unknown commit object");
3025        }
3026        my $refs = git_get_references();
3027        git_header_html();
3028        git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base);
3029        git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3030        if (!defined $hash && defined $file_name) {
3031                $hash = git_get_hash_by_path($hash_base, $file_name);
3032        }
3033        if (defined $hash) {
3034                $ftype = git_get_type($hash);
3035        }
3036        git_print_page_path($file_name, $ftype, $hash_base);
3037
3038        open my $fd, "-|",
3039                $GIT, "rev-list", "--full-history", $hash_base, "--", $file_name;
3040        git_history_body($fd, $refs, $hash_base, $ftype);
3041
3042        close $fd;
3043        git_footer_html();
3044}
3045
3046sub git_search {
3047        if (!defined $searchtext) {
3048                die_error(undef, "Text field empty");
3049        }
3050        if (!defined $hash) {
3051                $hash = git_get_head_hash($project);
3052        }
3053        my %co = parse_commit($hash);
3054        if (!%co) {
3055                die_error(undef, "Unknown commit object");
3056        }
3057        # pickaxe may take all resources of your box and run for several minutes
3058        # with every query - so decide by yourself how public you make this feature :)
3059        my $commit_search = 1;
3060        my $author_search = 0;
3061        my $committer_search = 0;
3062        my $pickaxe_search = 0;
3063        if ($searchtext =~ s/^author\\://i) {
3064                $author_search = 1;
3065        } elsif ($searchtext =~ s/^committer\\://i) {
3066                $committer_search = 1;
3067        } elsif ($searchtext =~ s/^pickaxe\\://i) {
3068                $commit_search = 0;
3069                $pickaxe_search = 1;
3070        }
3071        git_header_html();
3072        git_print_page_nav('','', $hash,$co{'tree'},$hash);
3073        git_print_header_div('commit', esc_html($co{'title'}), $hash);
3074
3075        print "<table cellspacing=\"0\">\n";
3076        my $alternate = 0;
3077        if ($commit_search) {
3078                $/ = "\0";
3079                open my $fd, "-|", $GIT, "rev-list", "--header", "--parents", $hash or next;
3080                while (my $commit_text = <$fd>) {
3081                        if (!grep m/$searchtext/i, $commit_text) {
3082                                next;
3083                        }
3084                        if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
3085                                next;
3086                        }
3087                        if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
3088                                next;
3089                        }
3090                        my @commit_lines = split "\n", $commit_text;
3091                        my %co = parse_commit(undef, \@commit_lines);
3092                        if (!%co) {
3093                                next;
3094                        }
3095                        if ($alternate) {
3096                                print "<tr class=\"dark\">\n";
3097                        } else {
3098                                print "<tr class=\"light\">\n";
3099                        }
3100                        $alternate ^= 1;
3101                        print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3102                              "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3103                              "<td>" .
3104                              $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
3105                                       esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3106                        my $comment = $co{'comment'};
3107                        foreach my $line (@$comment) {
3108                                if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
3109                                        my $lead = esc_html($1) || "";
3110                                        $lead = chop_str($lead, 30, 10);
3111                                        my $match = esc_html($2) || "";
3112                                        my $trail = esc_html($3) || "";
3113                                        $trail = chop_str($trail, 30, 10);
3114                                        my $text = "$lead<span class=\"match\">$match</span>$trail";
3115                                        print chop_str($text, 80, 5) . "<br/>\n";
3116                                }
3117                        }
3118                        print "</td>\n" .
3119                              "<td class=\"link\">" .
3120                              $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3121                              " | " .
3122                              $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3123                        print "</td>\n" .
3124                              "</tr>\n";
3125                }
3126                close $fd;
3127        }
3128
3129        if ($pickaxe_search) {
3130                $/ = "\n";
3131                open my $fd, "-|", "$GIT rev-list $hash | $GIT diff-tree -r --stdin -S\'$searchtext\'";
3132                undef %co;
3133                my @files;
3134                while (my $line = <$fd>) {
3135                        if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
3136                                my %set;
3137                                $set{'file'} = $6;
3138                                $set{'from_id'} = $3;
3139                                $set{'to_id'} = $4;
3140                                $set{'id'} = $set{'to_id'};
3141                                if ($set{'id'} =~ m/0{40}/) {
3142                                        $set{'id'} = $set{'from_id'};
3143                                }
3144                                if ($set{'id'} =~ m/0{40}/) {
3145                                        next;
3146                                }
3147                                push @files, \%set;
3148                        } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
3149                                if (%co) {
3150                                        if ($alternate) {
3151                                                print "<tr class=\"dark\">\n";
3152                                        } else {
3153                                                print "<tr class=\"light\">\n";
3154                                        }
3155                                        $alternate ^= 1;
3156                                        print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3157                                              "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3158                                              "<td>" .
3159                                              $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
3160                                                      -class => "list subject"},
3161                                                      esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3162                                        while (my $setref = shift @files) {
3163                                                my %set = %$setref;
3164                                                print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
3165                                                                             hash=>$set{'id'}, file_name=>$set{'file'}),
3166                                                              -class => "list"},
3167                                                              "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
3168                                                      "<br/>\n";
3169                                        }
3170                                        print "</td>\n" .
3171                                              "<td class=\"link\">" .
3172                                              $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3173                                              " | " .
3174                                              $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3175                                        print "</td>\n" .
3176                                              "</tr>\n";
3177                                }
3178                                %co = parse_commit($1);
3179                        }
3180                }
3181                close $fd;
3182        }
3183        print "</table>\n";
3184        git_footer_html();
3185}
3186
3187sub git_shortlog {
3188        my $head = git_get_head_hash($project);
3189        if (!defined $hash) {
3190                $hash = $head;
3191        }
3192        if (!defined $page) {
3193                $page = 0;
3194        }
3195        my $refs = git_get_references();
3196
3197        my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3198        open my $fd, "-|", $GIT, "rev-list", $limit, $hash
3199                or die_error(undef, "Open git-rev-list failed");
3200        my @revlist = map { chomp; $_ } <$fd>;
3201        close $fd;
3202
3203        my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#revlist);
3204        my $next_link = '';
3205        if ($#revlist >= (100 * ($page+1)-1)) {
3206                $next_link =
3207                        $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
3208                                 -title => "Alt-n"}, "next");
3209        }
3210
3211
3212        git_header_html();
3213        git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
3214        git_print_header_div('summary', $project);
3215
3216        git_shortlog_body(\@revlist, ($page * 100), $#revlist, $refs, $next_link);
3217
3218        git_footer_html();
3219}
3220
3221## ......................................................................
3222## feeds (RSS, OPML)
3223
3224sub git_rss {
3225        # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
3226        open my $fd, "-|", $GIT, "rev-list", "--max-count=150", git_get_head_hash($project)
3227                or die_error(undef, "Open git-rev-list failed");
3228        my @revlist = map { chomp; $_ } <$fd>;
3229        close $fd or die_error(undef, "Reading git-rev-list failed");
3230        print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3231        print <<XML;
3232<?xml version="1.0" encoding="utf-8"?>
3233<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
3234<channel>
3235<title>$project $my_uri $my_url</title>
3236<link>${\esc_html("$my_url?p=$project;a=summary")}</link>
3237<description>$project log</description>
3238<language>en</language>
3239XML
3240
3241        for (my $i = 0; $i <= $#revlist; $i++) {
3242                my $commit = $revlist[$i];
3243                my %co = parse_commit($commit);
3244                # we read 150, we always show 30 and the ones more recent than 48 hours
3245                if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
3246                        last;
3247                }
3248                my %cd = parse_date($co{'committer_epoch'});
3249                open $fd, "-|", $GIT, "diff-tree", '-r', $co{'parent'}, $co{'id'} or next;
3250                my @difftree = map { chomp; $_ } <$fd>;
3251                close $fd or next;
3252                print "<item>\n" .
3253                      "<title>" .
3254                      sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
3255                      "</title>\n" .
3256                      "<author>" . esc_html($co{'author'}) . "</author>\n" .
3257                      "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
3258                      "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
3259                      "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
3260                      "<description>" . esc_html($co{'title'}) . "</description>\n" .
3261                      "<content:encoded>" .
3262                      "<![CDATA[\n";
3263                my $comment = $co{'comment'};
3264                foreach my $line (@$comment) {
3265                        $line = decode("utf8", $line, Encode::FB_DEFAULT);
3266                        print "$line<br/>\n";
3267                }
3268                print "<br/>\n";
3269                foreach my $line (@difftree) {
3270                        if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
3271                                next;
3272                        }
3273                        my $file = validate_input(unquote($7));
3274                        $file = decode("utf8", $file, Encode::FB_DEFAULT);
3275                        print "$file<br/>\n";
3276                }
3277                print "]]>\n" .
3278                      "</content:encoded>\n" .
3279                      "</item>\n";
3280        }
3281        print "</channel></rss>";
3282}
3283
3284sub git_opml {
3285        my @list = git_get_projects_list();
3286
3287        print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3288        print <<XML;
3289<?xml version="1.0" encoding="utf-8"?>
3290<opml version="1.0">
3291<head>
3292  <title>$site_name Git OPML Export</title>
3293</head>
3294<body>
3295<outline text="git RSS feeds">
3296XML
3297
3298        foreach my $pr (@list) {
3299                my %proj = %$pr;
3300                my $head = git_get_head_hash($proj{'path'});
3301                if (!defined $head) {
3302                        next;
3303                }
3304                $ENV{'GIT_DIR'} = "$projectroot/$proj{'path'}";
3305                my %co = parse_commit($head);
3306                if (!%co) {
3307                        next;
3308                }
3309
3310                my $path = esc_html(chop_str($proj{'path'}, 25, 5));
3311                my $rss  = "$my_url?p=$proj{'path'};a=rss";
3312                my $html = "$my_url?p=$proj{'path'};a=summary";
3313                print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
3314        }
3315        print <<XML;
3316</outline>
3317</body>
3318</opml>
3319XML
3320}