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