gitweb / gitweb.perlon commit Merge branch 'jc/color' (cd39076)
   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
  21BEGIN {
  22        CGI->compile() if $ENV{'MOD_PERL'};
  23}
  24
  25our $cgi = new CGI;
  26our $version = "++GIT_VERSION++";
  27our $my_url = $cgi->url();
  28our $my_uri = $cgi->url(-absolute => 1);
  29
  30# core git executable to use
  31# this can just be "git" if your webserver has a sensible PATH
  32our $GIT = "++GIT_BINDIR++/git";
  33
  34# absolute fs-path which will be prepended to the project path
  35#our $projectroot = "/pub/scm";
  36our $projectroot = "++GITWEB_PROJECTROOT++";
  37
  38# fs traversing limit for getting project list
  39# the number is relative to the projectroot
  40our $project_maxdepth = "++GITWEB_PROJECT_MAXDEPTH++";
  41
  42# target of the home link on top of all pages
  43our $home_link = $my_uri || "/";
  44
  45# string of the home link on top of all pages
  46our $home_link_str = "++GITWEB_HOME_LINK_STR++";
  47
  48# name of your site or organization to appear in page titles
  49# replace this with something more descriptive for clearer bookmarks
  50our $site_name = "++GITWEB_SITENAME++"
  51                 || ($ENV{'SERVER_NAME'} || "Untitled") . " Git";
  52
  53# filename of html text to include at top of each page
  54our $site_header = "++GITWEB_SITE_HEADER++";
  55# html text to include at home page
  56our $home_text = "++GITWEB_HOMETEXT++";
  57# filename of html text to include at bottom of each page
  58our $site_footer = "++GITWEB_SITE_FOOTER++";
  59
  60# URI of stylesheets
  61our @stylesheets = ("++GITWEB_CSS++");
  62# URI of a single stylesheet, which can be overridden in GITWEB_CONFIG.
  63our $stylesheet = undef;
  64# URI of GIT logo (72x27 size)
  65our $logo = "++GITWEB_LOGO++";
  66# URI of GIT favicon, assumed to be image/png type
  67our $favicon = "++GITWEB_FAVICON++";
  68
  69# URI and label (title) of GIT logo link
  70#our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
  71#our $logo_label = "git documentation";
  72our $logo_url = "http://git.or.cz/";
  73our $logo_label = "git homepage";
  74
  75# source of projects list
  76our $projects_list = "++GITWEB_LIST++";
  77
  78# the width (in characters) of the projects list "Description" column
  79our $projects_list_description_width = 25;
  80
  81# default order of projects list
  82# valid values are none, project, descr, owner, and age
  83our $default_projects_order = "project";
  84
  85# show repository only if this file exists
  86# (only effective if this variable evaluates to true)
  87our $export_ok = "++GITWEB_EXPORT_OK++";
  88
  89# only allow viewing of repositories also shown on the overview page
  90our $strict_export = "++GITWEB_STRICT_EXPORT++";
  91
  92# list of git base URLs used for URL to where fetch project from,
  93# i.e. full URL is "$git_base_url/$project"
  94our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
  95
  96# default blob_plain mimetype and default charset for text/plain blob
  97our $default_blob_plain_mimetype = 'text/plain';
  98our $default_text_plain_charset  = undef;
  99
 100# file to use for guessing MIME types before trying /etc/mime.types
 101# (relative to the current git repository)
 102our $mimetypes_file = undef;
 103
 104# assume this charset if line contains non-UTF-8 characters;
 105# it should be valid encoding (see Encoding::Supported(3pm) for list),
 106# for which encoding all byte sequences are valid, for example
 107# 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
 108# could be even 'utf-8' for the old behavior)
 109our $fallback_encoding = 'latin1';
 110
 111# rename detection options for git-diff and git-diff-tree
 112# - default is '-M', with the cost proportional to
 113#   (number of removed files) * (number of new files).
 114# - more costly is '-C' (which implies '-M'), with the cost proportional to
 115#   (number of changed files + number of removed files) * (number of new files)
 116# - even more costly is '-C', '--find-copies-harder' with cost
 117#   (number of files in the original tree) * (number of new files)
 118# - one might want to include '-B' option, e.g. '-B', '-M'
 119our @diff_opts = ('-M'); # taken from git_commit
 120
 121# information about snapshot formats that gitweb is capable of serving
 122our %known_snapshot_formats = (
 123        # name => {
 124        #       'display' => display name,
 125        #       'type' => mime type,
 126        #       'suffix' => filename suffix,
 127        #       'format' => --format for git-archive,
 128        #       'compressor' => [compressor command and arguments]
 129        #                       (array reference, optional)}
 130        #
 131        'tgz' => {
 132                'display' => 'tar.gz',
 133                'type' => 'application/x-gzip',
 134                'suffix' => '.tar.gz',
 135                'format' => 'tar',
 136                'compressor' => ['gzip']},
 137
 138        'tbz2' => {
 139                'display' => 'tar.bz2',
 140                'type' => 'application/x-bzip2',
 141                'suffix' => '.tar.bz2',
 142                'format' => 'tar',
 143                'compressor' => ['bzip2']},
 144
 145        'zip' => {
 146                'display' => 'zip',
 147                'type' => 'application/x-zip',
 148                'suffix' => '.zip',
 149                'format' => 'zip'},
 150);
 151
 152# Aliases so we understand old gitweb.snapshot values in repository
 153# configuration.
 154our %known_snapshot_format_aliases = (
 155        'gzip'  => 'tgz',
 156        'bzip2' => 'tbz2',
 157
 158        # backward compatibility: legacy gitweb config support
 159        'x-gzip' => undef, 'gz' => undef,
 160        'x-bzip2' => undef, 'bz2' => undef,
 161        'x-zip' => undef, '' => undef,
 162);
 163
 164# You define site-wide feature defaults here; override them with
 165# $GITWEB_CONFIG as necessary.
 166our %feature = (
 167        # feature => {
 168        #       'sub' => feature-sub (subroutine),
 169        #       'override' => allow-override (boolean),
 170        #       'default' => [ default options...] (array reference)}
 171        #
 172        # if feature is overridable (it means that allow-override has true value),
 173        # then feature-sub will be called with default options as parameters;
 174        # return value of feature-sub indicates if to enable specified feature
 175        #
 176        # if there is no 'sub' key (no feature-sub), then feature cannot be
 177        # overriden
 178        #
 179        # use gitweb_check_feature(<feature>) to check if <feature> is enabled
 180
 181        # Enable the 'blame' blob view, showing the last commit that modified
 182        # each line in the file. This can be very CPU-intensive.
 183
 184        # To enable system wide have in $GITWEB_CONFIG
 185        # $feature{'blame'}{'default'} = [1];
 186        # To have project specific config enable override in $GITWEB_CONFIG
 187        # $feature{'blame'}{'override'} = 1;
 188        # and in project config gitweb.blame = 0|1;
 189        'blame' => {
 190                'sub' => \&feature_blame,
 191                'override' => 0,
 192                'default' => [0]},
 193
 194        # Enable the 'snapshot' link, providing a compressed archive of any
 195        # tree. This can potentially generate high traffic if you have large
 196        # project.
 197
 198        # Value is a list of formats defined in %known_snapshot_formats that
 199        # you wish to offer.
 200        # To disable system wide have in $GITWEB_CONFIG
 201        # $feature{'snapshot'}{'default'} = [];
 202        # To have project specific config enable override in $GITWEB_CONFIG
 203        # $feature{'snapshot'}{'override'} = 1;
 204        # and in project config, a comma-separated list of formats or "none"
 205        # to disable.  Example: gitweb.snapshot = tbz2,zip;
 206        'snapshot' => {
 207                'sub' => \&feature_snapshot,
 208                'override' => 0,
 209                'default' => ['tgz']},
 210
 211        # Enable text search, which will list the commits which match author,
 212        # committer or commit text to a given string.  Enabled by default.
 213        # Project specific override is not supported.
 214        'search' => {
 215                'override' => 0,
 216                'default' => [1]},
 217
 218        # Enable grep search, which will list the files in currently selected
 219        # tree containing the given string. Enabled by default. This can be
 220        # potentially CPU-intensive, of course.
 221
 222        # To enable system wide have in $GITWEB_CONFIG
 223        # $feature{'grep'}{'default'} = [1];
 224        # To have project specific config enable override in $GITWEB_CONFIG
 225        # $feature{'grep'}{'override'} = 1;
 226        # and in project config gitweb.grep = 0|1;
 227        'grep' => {
 228                'override' => 0,
 229                'default' => [1]},
 230
 231        # Enable the pickaxe search, which will list the commits that modified
 232        # a given string in a file. This can be practical and quite faster
 233        # alternative to 'blame', but still potentially CPU-intensive.
 234
 235        # To enable system wide have in $GITWEB_CONFIG
 236        # $feature{'pickaxe'}{'default'} = [1];
 237        # To have project specific config enable override in $GITWEB_CONFIG
 238        # $feature{'pickaxe'}{'override'} = 1;
 239        # and in project config gitweb.pickaxe = 0|1;
 240        'pickaxe' => {
 241                'sub' => \&feature_pickaxe,
 242                'override' => 0,
 243                'default' => [1]},
 244
 245        # Make gitweb use an alternative format of the URLs which can be
 246        # more readable and natural-looking: project name is embedded
 247        # directly in the path and the query string contains other
 248        # auxiliary information. All gitweb installations recognize
 249        # URL in either format; this configures in which formats gitweb
 250        # generates links.
 251
 252        # To enable system wide have in $GITWEB_CONFIG
 253        # $feature{'pathinfo'}{'default'} = [1];
 254        # Project specific override is not supported.
 255
 256        # Note that you will need to change the default location of CSS,
 257        # favicon, logo and possibly other files to an absolute URL. Also,
 258        # if gitweb.cgi serves as your indexfile, you will need to force
 259        # $my_uri to contain the script name in your $GITWEB_CONFIG.
 260        'pathinfo' => {
 261                'override' => 0,
 262                'default' => [0]},
 263
 264        # Make gitweb consider projects in project root subdirectories
 265        # to be forks of existing projects. Given project $projname.git,
 266        # projects matching $projname/*.git will not be shown in the main
 267        # projects list, instead a '+' mark will be added to $projname
 268        # there and a 'forks' view will be enabled for the project, listing
 269        # all the forks. If project list is taken from a file, forks have
 270        # to be listed after the main project.
 271
 272        # To enable system wide have in $GITWEB_CONFIG
 273        # $feature{'forks'}{'default'} = [1];
 274        # Project specific override is not supported.
 275        'forks' => {
 276                'override' => 0,
 277                'default' => [0]},
 278);
 279
 280sub gitweb_check_feature {
 281        my ($name) = @_;
 282        return unless exists $feature{$name};
 283        my ($sub, $override, @defaults) = (
 284                $feature{$name}{'sub'},
 285                $feature{$name}{'override'},
 286                @{$feature{$name}{'default'}});
 287        if (!$override) { return @defaults; }
 288        if (!defined $sub) {
 289                warn "feature $name is not overrideable";
 290                return @defaults;
 291        }
 292        return $sub->(@defaults);
 293}
 294
 295sub feature_blame {
 296        my ($val) = git_get_project_config('blame', '--bool');
 297
 298        if ($val eq 'true') {
 299                return 1;
 300        } elsif ($val eq 'false') {
 301                return 0;
 302        }
 303
 304        return $_[0];
 305}
 306
 307sub feature_snapshot {
 308        my (@fmts) = @_;
 309
 310        my ($val) = git_get_project_config('snapshot');
 311
 312        if ($val) {
 313                @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
 314        }
 315
 316        return @fmts;
 317}
 318
 319sub feature_grep {
 320        my ($val) = git_get_project_config('grep', '--bool');
 321
 322        if ($val eq 'true') {
 323                return (1);
 324        } elsif ($val eq 'false') {
 325                return (0);
 326        }
 327
 328        return ($_[0]);
 329}
 330
 331sub feature_pickaxe {
 332        my ($val) = git_get_project_config('pickaxe', '--bool');
 333
 334        if ($val eq 'true') {
 335                return (1);
 336        } elsif ($val eq 'false') {
 337                return (0);
 338        }
 339
 340        return ($_[0]);
 341}
 342
 343# checking HEAD file with -e is fragile if the repository was
 344# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
 345# and then pruned.
 346sub check_head_link {
 347        my ($dir) = @_;
 348        my $headfile = "$dir/HEAD";
 349        return ((-e $headfile) ||
 350                (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
 351}
 352
 353sub check_export_ok {
 354        my ($dir) = @_;
 355        return (check_head_link($dir) &&
 356                (!$export_ok || -e "$dir/$export_ok"));
 357}
 358
 359# process alternate names for backward compatibility
 360# filter out unsupported (unknown) snapshot formats
 361sub filter_snapshot_fmts {
 362        my @fmts = @_;
 363
 364        @fmts = map {
 365                exists $known_snapshot_format_aliases{$_} ?
 366                       $known_snapshot_format_aliases{$_} : $_} @fmts;
 367        @fmts = grep(exists $known_snapshot_formats{$_}, @fmts);
 368
 369}
 370
 371our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
 372do $GITWEB_CONFIG if -e $GITWEB_CONFIG;
 373
 374# version of the core git binary
 375our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
 376
 377$projects_list ||= $projectroot;
 378
 379# ======================================================================
 380# input validation and dispatch
 381our $action = $cgi->param('a');
 382if (defined $action) {
 383        if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
 384                die_error(undef, "Invalid action parameter");
 385        }
 386}
 387
 388# parameters which are pathnames
 389our $project = $cgi->param('p');
 390if (defined $project) {
 391        if (!validate_pathname($project) ||
 392            !(-d "$projectroot/$project") ||
 393            !check_head_link("$projectroot/$project") ||
 394            ($export_ok && !(-e "$projectroot/$project/$export_ok")) ||
 395            ($strict_export && !project_in_list($project))) {
 396                undef $project;
 397                die_error(undef, "No such project");
 398        }
 399}
 400
 401our $file_name = $cgi->param('f');
 402if (defined $file_name) {
 403        if (!validate_pathname($file_name)) {
 404                die_error(undef, "Invalid file parameter");
 405        }
 406}
 407
 408our $file_parent = $cgi->param('fp');
 409if (defined $file_parent) {
 410        if (!validate_pathname($file_parent)) {
 411                die_error(undef, "Invalid file parent parameter");
 412        }
 413}
 414
 415# parameters which are refnames
 416our $hash = $cgi->param('h');
 417if (defined $hash) {
 418        if (!validate_refname($hash)) {
 419                die_error(undef, "Invalid hash parameter");
 420        }
 421}
 422
 423our $hash_parent = $cgi->param('hp');
 424if (defined $hash_parent) {
 425        if (!validate_refname($hash_parent)) {
 426                die_error(undef, "Invalid hash parent parameter");
 427        }
 428}
 429
 430our $hash_base = $cgi->param('hb');
 431if (defined $hash_base) {
 432        if (!validate_refname($hash_base)) {
 433                die_error(undef, "Invalid hash base parameter");
 434        }
 435}
 436
 437my %allowed_options = (
 438        "--no-merges" => [ qw(rss atom log shortlog history) ],
 439);
 440
 441our @extra_options = $cgi->param('opt');
 442if (defined @extra_options) {
 443        foreach my $opt (@extra_options) {
 444                if (not exists $allowed_options{$opt}) {
 445                        die_error(undef, "Invalid option parameter");
 446                }
 447                if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
 448                        die_error(undef, "Invalid option parameter for this action");
 449                }
 450        }
 451}
 452
 453our $hash_parent_base = $cgi->param('hpb');
 454if (defined $hash_parent_base) {
 455        if (!validate_refname($hash_parent_base)) {
 456                die_error(undef, "Invalid hash parent base parameter");
 457        }
 458}
 459
 460# other parameters
 461our $page = $cgi->param('pg');
 462if (defined $page) {
 463        if ($page =~ m/[^0-9]/) {
 464                die_error(undef, "Invalid page parameter");
 465        }
 466}
 467
 468our $searchtype = $cgi->param('st');
 469if (defined $searchtype) {
 470        if ($searchtype =~ m/[^a-z]/) {
 471                die_error(undef, "Invalid searchtype parameter");
 472        }
 473}
 474
 475our $searchtext = $cgi->param('s');
 476our $search_regexp;
 477if (defined $searchtext) {
 478        if (length($searchtext) < 2) {
 479                die_error(undef, "At least two characters are required for search parameter");
 480        }
 481        $search_regexp = quotemeta $searchtext;
 482}
 483
 484# now read PATH_INFO and use it as alternative to parameters
 485sub evaluate_path_info {
 486        return if defined $project;
 487        my $path_info = $ENV{"PATH_INFO"};
 488        return if !$path_info;
 489        $path_info =~ s,^/+,,;
 490        return if !$path_info;
 491        # find which part of PATH_INFO is project
 492        $project = $path_info;
 493        $project =~ s,/+$,,;
 494        while ($project && !check_head_link("$projectroot/$project")) {
 495                $project =~ s,/*[^/]*$,,;
 496        }
 497        # validate project
 498        $project = validate_pathname($project);
 499        if (!$project ||
 500            ($export_ok && !-e "$projectroot/$project/$export_ok") ||
 501            ($strict_export && !project_in_list($project))) {
 502                undef $project;
 503                return;
 504        }
 505        # do not change any parameters if an action is given using the query string
 506        return if $action;
 507        $path_info =~ s,^$project/*,,;
 508        my ($refname, $pathname) = split(/:/, $path_info, 2);
 509        if (defined $pathname) {
 510                # we got "project.git/branch:filename" or "project.git/branch:dir/"
 511                # we could use git_get_type(branch:pathname), but it needs $git_dir
 512                $pathname =~ s,^/+,,;
 513                if (!$pathname || substr($pathname, -1) eq "/") {
 514                        $action  ||= "tree";
 515                        $pathname =~ s,/$,,;
 516                } else {
 517                        $action  ||= "blob_plain";
 518                }
 519                $hash_base ||= validate_refname($refname);
 520                $file_name ||= validate_pathname($pathname);
 521        } elsif (defined $refname) {
 522                # we got "project.git/branch"
 523                $action ||= "shortlog";
 524                $hash   ||= validate_refname($refname);
 525        }
 526}
 527evaluate_path_info();
 528
 529# path to the current git repository
 530our $git_dir;
 531$git_dir = "$projectroot/$project" if $project;
 532
 533# dispatch
 534my %actions = (
 535        "blame" => \&git_blame2,
 536        "blobdiff" => \&git_blobdiff,
 537        "blobdiff_plain" => \&git_blobdiff_plain,
 538        "blob" => \&git_blob,
 539        "blob_plain" => \&git_blob_plain,
 540        "commitdiff" => \&git_commitdiff,
 541        "commitdiff_plain" => \&git_commitdiff_plain,
 542        "commit" => \&git_commit,
 543        "forks" => \&git_forks,
 544        "heads" => \&git_heads,
 545        "history" => \&git_history,
 546        "log" => \&git_log,
 547        "rss" => \&git_rss,
 548        "atom" => \&git_atom,
 549        "search" => \&git_search,
 550        "search_help" => \&git_search_help,
 551        "shortlog" => \&git_shortlog,
 552        "summary" => \&git_summary,
 553        "tag" => \&git_tag,
 554        "tags" => \&git_tags,
 555        "tree" => \&git_tree,
 556        "snapshot" => \&git_snapshot,
 557        "object" => \&git_object,
 558        # those below don't need $project
 559        "opml" => \&git_opml,
 560        "project_list" => \&git_project_list,
 561        "project_index" => \&git_project_index,
 562);
 563
 564if (!defined $action) {
 565        if (defined $hash) {
 566                $action = git_get_type($hash);
 567        } elsif (defined $hash_base && defined $file_name) {
 568                $action = git_get_type("$hash_base:$file_name");
 569        } elsif (defined $project) {
 570                $action = 'summary';
 571        } else {
 572                $action = 'project_list';
 573        }
 574}
 575if (!defined($actions{$action})) {
 576        die_error(undef, "Unknown action");
 577}
 578if ($action !~ m/^(opml|project_list|project_index)$/ &&
 579    !$project) {
 580        die_error(undef, "Project needed");
 581}
 582$actions{$action}->();
 583exit;
 584
 585## ======================================================================
 586## action links
 587
 588sub href(%) {
 589        my %params = @_;
 590        # default is to use -absolute url() i.e. $my_uri
 591        my $href = $params{-full} ? $my_url : $my_uri;
 592
 593        # XXX: Warning: If you touch this, check the search form for updating,
 594        # too.
 595
 596        my @mapping = (
 597                project => "p",
 598                action => "a",
 599                file_name => "f",
 600                file_parent => "fp",
 601                hash => "h",
 602                hash_parent => "hp",
 603                hash_base => "hb",
 604                hash_parent_base => "hpb",
 605                page => "pg",
 606                order => "o",
 607                searchtext => "s",
 608                searchtype => "st",
 609                snapshot_format => "sf",
 610                extra_options => "opt",
 611        );
 612        my %mapping = @mapping;
 613
 614        if ($params{-replay}) {
 615                while (my ($name, $symbol) = each %mapping) {
 616                        if (!exists $params{$name}) {
 617                                # to allow for multivalued params we use arrayref form
 618                                $params{$name} = [ $cgi->param($symbol) ];
 619                        }
 620                }
 621        }
 622
 623        $params{'project'} = $project unless exists $params{'project'};
 624
 625        my ($use_pathinfo) = gitweb_check_feature('pathinfo');
 626        if ($use_pathinfo) {
 627                # use PATH_INFO for project name
 628                $href .= "/$params{'project'}" if defined $params{'project'};
 629                delete $params{'project'};
 630
 631                # Summary just uses the project path URL
 632                if (defined $params{'action'} && $params{'action'} eq 'summary') {
 633                        delete $params{'action'};
 634                }
 635        }
 636
 637        # now encode the parameters explicitly
 638        my @result = ();
 639        for (my $i = 0; $i < @mapping; $i += 2) {
 640                my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
 641                if (defined $params{$name}) {
 642                        if (ref($params{$name}) eq "ARRAY") {
 643                                foreach my $par (@{$params{$name}}) {
 644                                        push @result, $symbol . "=" . esc_param($par);
 645                                }
 646                        } else {
 647                                push @result, $symbol . "=" . esc_param($params{$name});
 648                        }
 649                }
 650        }
 651        $href .= "?" . join(';', @result) if scalar @result;
 652
 653        return $href;
 654}
 655
 656
 657## ======================================================================
 658## validation, quoting/unquoting and escaping
 659
 660sub validate_pathname {
 661        my $input = shift || return undef;
 662
 663        # no '.' or '..' as elements of path, i.e. no '.' nor '..'
 664        # at the beginning, at the end, and between slashes.
 665        # also this catches doubled slashes
 666        if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
 667                return undef;
 668        }
 669        # no null characters
 670        if ($input =~ m!\0!) {
 671                return undef;
 672        }
 673        return $input;
 674}
 675
 676sub validate_refname {
 677        my $input = shift || return undef;
 678
 679        # textual hashes are O.K.
 680        if ($input =~ m/^[0-9a-fA-F]{40}$/) {
 681                return $input;
 682        }
 683        # it must be correct pathname
 684        $input = validate_pathname($input)
 685                or return undef;
 686        # restrictions on ref name according to git-check-ref-format
 687        if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
 688                return undef;
 689        }
 690        return $input;
 691}
 692
 693# decode sequences of octets in utf8 into Perl's internal form,
 694# which is utf-8 with utf8 flag set if needed.  gitweb writes out
 695# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
 696sub to_utf8 {
 697        my $str = shift;
 698        my $res;
 699        eval { $res = decode_utf8($str, Encode::FB_CROAK); };
 700        if (defined $res) {
 701                return $res;
 702        } else {
 703                return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
 704        }
 705}
 706
 707# quote unsafe chars, but keep the slash, even when it's not
 708# correct, but quoted slashes look too horrible in bookmarks
 709sub esc_param {
 710        my $str = shift;
 711        $str =~ s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X", ord($1))/eg;
 712        $str =~ s/\+/%2B/g;
 713        $str =~ s/ /\+/g;
 714        return $str;
 715}
 716
 717# quote unsafe chars in whole URL, so some charactrs cannot be quoted
 718sub esc_url {
 719        my $str = shift;
 720        $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
 721        $str =~ s/\+/%2B/g;
 722        $str =~ s/ /\+/g;
 723        return $str;
 724}
 725
 726# replace invalid utf8 character with SUBSTITUTION sequence
 727sub esc_html ($;%) {
 728        my $str = shift;
 729        my %opts = @_;
 730
 731        $str = to_utf8($str);
 732        $str = $cgi->escapeHTML($str);
 733        if ($opts{'-nbsp'}) {
 734                $str =~ s/ /&nbsp;/g;
 735        }
 736        $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
 737        return $str;
 738}
 739
 740# quote control characters and escape filename to HTML
 741sub esc_path {
 742        my $str = shift;
 743        my %opts = @_;
 744
 745        $str = to_utf8($str);
 746        $str = $cgi->escapeHTML($str);
 747        if ($opts{'-nbsp'}) {
 748                $str =~ s/ /&nbsp;/g;
 749        }
 750        $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
 751        return $str;
 752}
 753
 754# Make control characters "printable", using character escape codes (CEC)
 755sub quot_cec {
 756        my $cntrl = shift;
 757        my %es = ( # character escape codes, aka escape sequences
 758                   "\t" => '\t',   # tab            (HT)
 759                   "\n" => '\n',   # line feed      (LF)
 760                   "\r" => '\r',   # carrige return (CR)
 761                   "\f" => '\f',   # form feed      (FF)
 762                   "\b" => '\b',   # backspace      (BS)
 763                   "\a" => '\a',   # alarm (bell)   (BEL)
 764                   "\e" => '\e',   # escape         (ESC)
 765                   "\013" => '\v', # vertical tab   (VT)
 766                   "\000" => '\0', # nul character  (NUL)
 767                   );
 768        my $chr = ( (exists $es{$cntrl})
 769                    ? $es{$cntrl}
 770                    : sprintf('\%03o', ord($cntrl)) );
 771        return "<span class=\"cntrl\">$chr</span>";
 772}
 773
 774# Alternatively use unicode control pictures codepoints,
 775# Unicode "printable representation" (PR)
 776sub quot_upr {
 777        my $cntrl = shift;
 778        my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
 779        return "<span class=\"cntrl\">$chr</span>";
 780}
 781
 782# git may return quoted and escaped filenames
 783sub unquote {
 784        my $str = shift;
 785
 786        sub unq {
 787                my $seq = shift;
 788                my %es = ( # character escape codes, aka escape sequences
 789                        't' => "\t",   # tab            (HT, TAB)
 790                        'n' => "\n",   # newline        (NL)
 791                        'r' => "\r",   # return         (CR)
 792                        'f' => "\f",   # form feed      (FF)
 793                        'b' => "\b",   # backspace      (BS)
 794                        'a' => "\a",   # alarm (bell)   (BEL)
 795                        'e' => "\e",   # escape         (ESC)
 796                        'v' => "\013", # vertical tab   (VT)
 797                );
 798
 799                if ($seq =~ m/^[0-7]{1,3}$/) {
 800                        # octal char sequence
 801                        return chr(oct($seq));
 802                } elsif (exists $es{$seq}) {
 803                        # C escape sequence, aka character escape code
 804                        return $es{$seq}
 805                }
 806                # quoted ordinary character
 807                return $seq;
 808        }
 809
 810        if ($str =~ m/^"(.*)"$/) {
 811                # needs unquoting
 812                $str = $1;
 813                $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
 814        }
 815        return $str;
 816}
 817
 818# escape tabs (convert tabs to spaces)
 819sub untabify {
 820        my $line = shift;
 821
 822        while ((my $pos = index($line, "\t")) != -1) {
 823                if (my $count = (8 - ($pos % 8))) {
 824                        my $spaces = ' ' x $count;
 825                        $line =~ s/\t/$spaces/;
 826                }
 827        }
 828
 829        return $line;
 830}
 831
 832sub project_in_list {
 833        my $project = shift;
 834        my @list = git_get_projects_list();
 835        return @list && scalar(grep { $_->{'path'} eq $project } @list);
 836}
 837
 838## ----------------------------------------------------------------------
 839## HTML aware string manipulation
 840
 841sub chop_str {
 842        my $str = shift;
 843        my $len = shift;
 844        my $add_len = shift || 10;
 845
 846        # allow only $len chars, but don't cut a word if it would fit in $add_len
 847        # if it doesn't fit, cut it if it's still longer than the dots we would add
 848        $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
 849        my $body = $1;
 850        my $tail = $2;
 851        if (length($tail) > 4) {
 852                $tail = " ...";
 853                $body =~ s/&[^;]*$//; # remove chopped character entities
 854        }
 855        return "$body$tail";
 856}
 857
 858# takes the same arguments as chop_str, but also wraps a <span> around the
 859# result with a title attribute if it does get chopped. Additionally, the
 860# string is HTML-escaped.
 861sub chop_and_escape_str {
 862        my $str = shift;
 863        my $len = shift;
 864        my $add_len = shift || 10;
 865
 866        my $chopped = chop_str($str, $len, $add_len);
 867        if ($chopped eq $str) {
 868                return esc_html($chopped);
 869        } else {
 870                return qq{<span title="} . esc_html($str) . qq{">} .
 871                        esc_html($chopped) . qq{</span>};
 872        }
 873}
 874
 875## ----------------------------------------------------------------------
 876## functions returning short strings
 877
 878# CSS class for given age value (in seconds)
 879sub age_class {
 880        my $age = shift;
 881
 882        if (!defined $age) {
 883                return "noage";
 884        } elsif ($age < 60*60*2) {
 885                return "age0";
 886        } elsif ($age < 60*60*24*2) {
 887                return "age1";
 888        } else {
 889                return "age2";
 890        }
 891}
 892
 893# convert age in seconds to "nn units ago" string
 894sub age_string {
 895        my $age = shift;
 896        my $age_str;
 897
 898        if ($age > 60*60*24*365*2) {
 899                $age_str = (int $age/60/60/24/365);
 900                $age_str .= " years ago";
 901        } elsif ($age > 60*60*24*(365/12)*2) {
 902                $age_str = int $age/60/60/24/(365/12);
 903                $age_str .= " months ago";
 904        } elsif ($age > 60*60*24*7*2) {
 905                $age_str = int $age/60/60/24/7;
 906                $age_str .= " weeks ago";
 907        } elsif ($age > 60*60*24*2) {
 908                $age_str = int $age/60/60/24;
 909                $age_str .= " days ago";
 910        } elsif ($age > 60*60*2) {
 911                $age_str = int $age/60/60;
 912                $age_str .= " hours ago";
 913        } elsif ($age > 60*2) {
 914                $age_str = int $age/60;
 915                $age_str .= " min ago";
 916        } elsif ($age > 2) {
 917                $age_str = int $age;
 918                $age_str .= " sec ago";
 919        } else {
 920                $age_str .= " right now";
 921        }
 922        return $age_str;
 923}
 924
 925use constant {
 926        S_IFINVALID => 0030000,
 927        S_IFGITLINK => 0160000,
 928};
 929
 930# submodule/subproject, a commit object reference
 931sub S_ISGITLINK($) {
 932        my $mode = shift;
 933
 934        return (($mode & S_IFMT) == S_IFGITLINK)
 935}
 936
 937# convert file mode in octal to symbolic file mode string
 938sub mode_str {
 939        my $mode = oct shift;
 940
 941        if (S_ISGITLINK($mode)) {
 942                return 'm---------';
 943        } elsif (S_ISDIR($mode & S_IFMT)) {
 944                return 'drwxr-xr-x';
 945        } elsif (S_ISLNK($mode)) {
 946                return 'lrwxrwxrwx';
 947        } elsif (S_ISREG($mode)) {
 948                # git cares only about the executable bit
 949                if ($mode & S_IXUSR) {
 950                        return '-rwxr-xr-x';
 951                } else {
 952                        return '-rw-r--r--';
 953                };
 954        } else {
 955                return '----------';
 956        }
 957}
 958
 959# convert file mode in octal to file type string
 960sub file_type {
 961        my $mode = shift;
 962
 963        if ($mode !~ m/^[0-7]+$/) {
 964                return $mode;
 965        } else {
 966                $mode = oct $mode;
 967        }
 968
 969        if (S_ISGITLINK($mode)) {
 970                return "submodule";
 971        } elsif (S_ISDIR($mode & S_IFMT)) {
 972                return "directory";
 973        } elsif (S_ISLNK($mode)) {
 974                return "symlink";
 975        } elsif (S_ISREG($mode)) {
 976                return "file";
 977        } else {
 978                return "unknown";
 979        }
 980}
 981
 982# convert file mode in octal to file type description string
 983sub file_type_long {
 984        my $mode = shift;
 985
 986        if ($mode !~ m/^[0-7]+$/) {
 987                return $mode;
 988        } else {
 989                $mode = oct $mode;
 990        }
 991
 992        if (S_ISGITLINK($mode)) {
 993                return "submodule";
 994        } elsif (S_ISDIR($mode & S_IFMT)) {
 995                return "directory";
 996        } elsif (S_ISLNK($mode)) {
 997                return "symlink";
 998        } elsif (S_ISREG($mode)) {
 999                if ($mode & S_IXUSR) {
1000                        return "executable";
1001                } else {
1002                        return "file";
1003                };
1004        } else {
1005                return "unknown";
1006        }
1007}
1008
1009
1010## ----------------------------------------------------------------------
1011## functions returning short HTML fragments, or transforming HTML fragments
1012## which don't belong to other sections
1013
1014# format line of commit message.
1015sub format_log_line_html {
1016        my $line = shift;
1017
1018        $line = esc_html($line, -nbsp=>1);
1019        if ($line =~ m/([0-9a-fA-F]{8,40})/) {
1020                my $hash_text = $1;
1021                my $link =
1022                        $cgi->a({-href => href(action=>"object", hash=>$hash_text),
1023                                -class => "text"}, $hash_text);
1024                $line =~ s/$hash_text/$link/;
1025        }
1026        return $line;
1027}
1028
1029# format marker of refs pointing to given object
1030sub format_ref_marker {
1031        my ($refs, $id) = @_;
1032        my $markers = '';
1033
1034        if (defined $refs->{$id}) {
1035                foreach my $ref (@{$refs->{$id}}) {
1036                        my ($type, $name) = qw();
1037                        # e.g. tags/v2.6.11 or heads/next
1038                        if ($ref =~ m!^(.*?)s?/(.*)$!) {
1039                                $type = $1;
1040                                $name = $2;
1041                        } else {
1042                                $type = "ref";
1043                                $name = $ref;
1044                        }
1045
1046                        $markers .= " <span class=\"$type\" title=\"$ref\">" .
1047                                    esc_html($name) . "</span>";
1048                }
1049        }
1050
1051        if ($markers) {
1052                return ' <span class="refs">'. $markers . '</span>';
1053        } else {
1054                return "";
1055        }
1056}
1057
1058# format, perhaps shortened and with markers, title line
1059sub format_subject_html {
1060        my ($long, $short, $href, $extra) = @_;
1061        $extra = '' unless defined($extra);
1062
1063        if (length($short) < length($long)) {
1064                return $cgi->a({-href => $href, -class => "list subject",
1065                                -title => to_utf8($long)},
1066                       esc_html($short) . $extra);
1067        } else {
1068                return $cgi->a({-href => $href, -class => "list subject"},
1069                       esc_html($long)  . $extra);
1070        }
1071}
1072
1073# format git diff header line, i.e. "diff --(git|combined|cc) ..."
1074sub format_git_diff_header_line {
1075        my $line = shift;
1076        my $diffinfo = shift;
1077        my ($from, $to) = @_;
1078
1079        if ($diffinfo->{'nparents'}) {
1080                # combined diff
1081                $line =~ s!^(diff (.*?) )"?.*$!$1!;
1082                if ($to->{'href'}) {
1083                        $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1084                                         esc_path($to->{'file'}));
1085                } else { # file was deleted (no href)
1086                        $line .= esc_path($to->{'file'});
1087                }
1088        } else {
1089                # "ordinary" diff
1090                $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
1091                if ($from->{'href'}) {
1092                        $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
1093                                         'a/' . esc_path($from->{'file'}));
1094                } else { # file was added (no href)
1095                        $line .= 'a/' . esc_path($from->{'file'});
1096                }
1097                $line .= ' ';
1098                if ($to->{'href'}) {
1099                        $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1100                                         'b/' . esc_path($to->{'file'}));
1101                } else { # file was deleted
1102                        $line .= 'b/' . esc_path($to->{'file'});
1103                }
1104        }
1105
1106        return "<div class=\"diff header\">$line</div>\n";
1107}
1108
1109# format extended diff header line, before patch itself
1110sub format_extended_diff_header_line {
1111        my $line = shift;
1112        my $diffinfo = shift;
1113        my ($from, $to) = @_;
1114
1115        # match <path>
1116        if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
1117                $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1118                                       esc_path($from->{'file'}));
1119        }
1120        if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
1121                $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1122                                 esc_path($to->{'file'}));
1123        }
1124        # match single <mode>
1125        if ($line =~ m/\s(\d{6})$/) {
1126                $line .= '<span class="info"> (' .
1127                         file_type_long($1) .
1128                         ')</span>';
1129        }
1130        # match <hash>
1131        if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
1132                # can match only for combined diff
1133                $line = 'index ';
1134                for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1135                        if ($from->{'href'}[$i]) {
1136                                $line .= $cgi->a({-href=>$from->{'href'}[$i],
1137                                                  -class=>"hash"},
1138                                                 substr($diffinfo->{'from_id'}[$i],0,7));
1139                        } else {
1140                                $line .= '0' x 7;
1141                        }
1142                        # separator
1143                        $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
1144                }
1145                $line .= '..';
1146                if ($to->{'href'}) {
1147                        $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1148                                         substr($diffinfo->{'to_id'},0,7));
1149                } else {
1150                        $line .= '0' x 7;
1151                }
1152
1153        } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
1154                # can match only for ordinary diff
1155                my ($from_link, $to_link);
1156                if ($from->{'href'}) {
1157                        $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
1158                                             substr($diffinfo->{'from_id'},0,7));
1159                } else {
1160                        $from_link = '0' x 7;
1161                }
1162                if ($to->{'href'}) {
1163                        $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1164                                           substr($diffinfo->{'to_id'},0,7));
1165                } else {
1166                        $to_link = '0' x 7;
1167                }
1168                my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
1169                $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
1170        }
1171
1172        return $line . "<br/>\n";
1173}
1174
1175# format from-file/to-file diff header
1176sub format_diff_from_to_header {
1177        my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
1178        my $line;
1179        my $result = '';
1180
1181        $line = $from_line;
1182        #assert($line =~ m/^---/) if DEBUG;
1183        # no extra formatting for "^--- /dev/null"
1184        if (! $diffinfo->{'nparents'}) {
1185                # ordinary (single parent) diff
1186                if ($line =~ m!^--- "?a/!) {
1187                        if ($from->{'href'}) {
1188                                $line = '--- a/' .
1189                                        $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1190                                                esc_path($from->{'file'}));
1191                        } else {
1192                                $line = '--- a/' .
1193                                        esc_path($from->{'file'});
1194                        }
1195                }
1196                $result .= qq!<div class="diff from_file">$line</div>\n!;
1197
1198        } else {
1199                # combined diff (merge commit)
1200                for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1201                        if ($from->{'href'}[$i]) {
1202                                $line = '--- ' .
1203                                        $cgi->a({-href=>href(action=>"blobdiff",
1204                                                             hash_parent=>$diffinfo->{'from_id'}[$i],
1205                                                             hash_parent_base=>$parents[$i],
1206                                                             file_parent=>$from->{'file'}[$i],
1207                                                             hash=>$diffinfo->{'to_id'},
1208                                                             hash_base=>$hash,
1209                                                             file_name=>$to->{'file'}),
1210                                                 -class=>"path",
1211                                                 -title=>"diff" . ($i+1)},
1212                                                $i+1) .
1213                                        '/' .
1214                                        $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
1215                                                esc_path($from->{'file'}[$i]));
1216                        } else {
1217                                $line = '--- /dev/null';
1218                        }
1219                        $result .= qq!<div class="diff from_file">$line</div>\n!;
1220                }
1221        }
1222
1223        $line = $to_line;
1224        #assert($line =~ m/^\+\+\+/) if DEBUG;
1225        # no extra formatting for "^+++ /dev/null"
1226        if ($line =~ m!^\+\+\+ "?b/!) {
1227                if ($to->{'href'}) {
1228                        $line = '+++ b/' .
1229                                $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1230                                        esc_path($to->{'file'}));
1231                } else {
1232                        $line = '+++ b/' .
1233                                esc_path($to->{'file'});
1234                }
1235        }
1236        $result .= qq!<div class="diff to_file">$line</div>\n!;
1237
1238        return $result;
1239}
1240
1241# create note for patch simplified by combined diff
1242sub format_diff_cc_simplified {
1243        my ($diffinfo, @parents) = @_;
1244        my $result = '';
1245
1246        $result .= "<div class=\"diff header\">" .
1247                   "diff --cc ";
1248        if (!is_deleted($diffinfo)) {
1249                $result .= $cgi->a({-href => href(action=>"blob",
1250                                                  hash_base=>$hash,
1251                                                  hash=>$diffinfo->{'to_id'},
1252                                                  file_name=>$diffinfo->{'to_file'}),
1253                                    -class => "path"},
1254                                   esc_path($diffinfo->{'to_file'}));
1255        } else {
1256                $result .= esc_path($diffinfo->{'to_file'});
1257        }
1258        $result .= "</div>\n" . # class="diff header"
1259                   "<div class=\"diff nodifferences\">" .
1260                   "Simple merge" .
1261                   "</div>\n"; # class="diff nodifferences"
1262
1263        return $result;
1264}
1265
1266# format patch (diff) line (not to be used for diff headers)
1267sub format_diff_line {
1268        my $line = shift;
1269        my ($from, $to) = @_;
1270        my $diff_class = "";
1271
1272        chomp $line;
1273
1274        if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
1275                # combined diff
1276                my $prefix = substr($line, 0, scalar @{$from->{'href'}});
1277                if ($line =~ m/^\@{3}/) {
1278                        $diff_class = " chunk_header";
1279                } elsif ($line =~ m/^\\/) {
1280                        $diff_class = " incomplete";
1281                } elsif ($prefix =~ tr/+/+/) {
1282                        $diff_class = " add";
1283                } elsif ($prefix =~ tr/-/-/) {
1284                        $diff_class = " rem";
1285                }
1286        } else {
1287                # assume ordinary diff
1288                my $char = substr($line, 0, 1);
1289                if ($char eq '+') {
1290                        $diff_class = " add";
1291                } elsif ($char eq '-') {
1292                        $diff_class = " rem";
1293                } elsif ($char eq '@') {
1294                        $diff_class = " chunk_header";
1295                } elsif ($char eq "\\") {
1296                        $diff_class = " incomplete";
1297                }
1298        }
1299        $line = untabify($line);
1300        if ($from && $to && $line =~ m/^\@{2} /) {
1301                my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
1302                        $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
1303
1304                $from_lines = 0 unless defined $from_lines;
1305                $to_lines   = 0 unless defined $to_lines;
1306
1307                if ($from->{'href'}) {
1308                        $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
1309                                             -class=>"list"}, $from_text);
1310                }
1311                if ($to->{'href'}) {
1312                        $to_text   = $cgi->a({-href=>"$to->{'href'}#l$to_start",
1313                                             -class=>"list"}, $to_text);
1314                }
1315                $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
1316                        "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1317                return "<div class=\"diff$diff_class\">$line</div>\n";
1318        } elsif ($from && $to && $line =~ m/^\@{3}/) {
1319                my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
1320                my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
1321
1322                @from_text = split(' ', $ranges);
1323                for (my $i = 0; $i < @from_text; ++$i) {
1324                        ($from_start[$i], $from_nlines[$i]) =
1325                                (split(',', substr($from_text[$i], 1)), 0);
1326                }
1327
1328                $to_text   = pop @from_text;
1329                $to_start  = pop @from_start;
1330                $to_nlines = pop @from_nlines;
1331
1332                $line = "<span class=\"chunk_info\">$prefix ";
1333                for (my $i = 0; $i < @from_text; ++$i) {
1334                        if ($from->{'href'}[$i]) {
1335                                $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
1336                                                  -class=>"list"}, $from_text[$i]);
1337                        } else {
1338                                $line .= $from_text[$i];
1339                        }
1340                        $line .= " ";
1341                }
1342                if ($to->{'href'}) {
1343                        $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
1344                                          -class=>"list"}, $to_text);
1345                } else {
1346                        $line .= $to_text;
1347                }
1348                $line .= " $prefix</span>" .
1349                         "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1350                return "<div class=\"diff$diff_class\">$line</div>\n";
1351        }
1352        return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
1353}
1354
1355# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
1356# linked.  Pass the hash of the tree/commit to snapshot.
1357sub format_snapshot_links {
1358        my ($hash) = @_;
1359        my @snapshot_fmts = gitweb_check_feature('snapshot');
1360        @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
1361        my $num_fmts = @snapshot_fmts;
1362        if ($num_fmts > 1) {
1363                # A parenthesized list of links bearing format names.
1364                # e.g. "snapshot (_tar.gz_ _zip_)"
1365                return "snapshot (" . join(' ', map
1366                        $cgi->a({
1367                                -href => href(
1368                                        action=>"snapshot",
1369                                        hash=>$hash,
1370                                        snapshot_format=>$_
1371                                )
1372                        }, $known_snapshot_formats{$_}{'display'})
1373                , @snapshot_fmts) . ")";
1374        } elsif ($num_fmts == 1) {
1375                # A single "snapshot" link whose tooltip bears the format name.
1376                # i.e. "_snapshot_"
1377                my ($fmt) = @snapshot_fmts;
1378                return
1379                        $cgi->a({
1380                                -href => href(
1381                                        action=>"snapshot",
1382                                        hash=>$hash,
1383                                        snapshot_format=>$fmt
1384                                ),
1385                                -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
1386                        }, "snapshot");
1387        } else { # $num_fmts == 0
1388                return undef;
1389        }
1390}
1391
1392## ----------------------------------------------------------------------
1393## git utility subroutines, invoking git commands
1394
1395# returns path to the core git executable and the --git-dir parameter as list
1396sub git_cmd {
1397        return $GIT, '--git-dir='.$git_dir;
1398}
1399
1400# returns path to the core git executable and the --git-dir parameter as string
1401sub git_cmd_str {
1402        return join(' ', git_cmd());
1403}
1404
1405# get HEAD ref of given project as hash
1406sub git_get_head_hash {
1407        my $project = shift;
1408        my $o_git_dir = $git_dir;
1409        my $retval = undef;
1410        $git_dir = "$projectroot/$project";
1411        if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
1412                my $head = <$fd>;
1413                close $fd;
1414                if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
1415                        $retval = $1;
1416                }
1417        }
1418        if (defined $o_git_dir) {
1419                $git_dir = $o_git_dir;
1420        }
1421        return $retval;
1422}
1423
1424# get type of given object
1425sub git_get_type {
1426        my $hash = shift;
1427
1428        open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
1429        my $type = <$fd>;
1430        close $fd or return;
1431        chomp $type;
1432        return $type;
1433}
1434
1435# repository configuration
1436our $config_file = '';
1437our %config;
1438
1439# store multiple values for single key as anonymous array reference
1440# single values stored directly in the hash, not as [ <value> ]
1441sub hash_set_multi {
1442        my ($hash, $key, $value) = @_;
1443
1444        if (!exists $hash->{$key}) {
1445                $hash->{$key} = $value;
1446        } elsif (!ref $hash->{$key}) {
1447                $hash->{$key} = [ $hash->{$key}, $value ];
1448        } else {
1449                push @{$hash->{$key}}, $value;
1450        }
1451}
1452
1453# return hash of git project configuration
1454# optionally limited to some section, e.g. 'gitweb'
1455sub git_parse_project_config {
1456        my $section_regexp = shift;
1457        my %config;
1458
1459        local $/ = "\0";
1460
1461        open my $fh, "-|", git_cmd(), "config", '-z', '-l',
1462                or return;
1463
1464        while (my $keyval = <$fh>) {
1465                chomp $keyval;
1466                my ($key, $value) = split(/\n/, $keyval, 2);
1467
1468                hash_set_multi(\%config, $key, $value)
1469                        if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
1470        }
1471        close $fh;
1472
1473        return %config;
1474}
1475
1476# convert config value to boolean, 'true' or 'false'
1477# no value, number > 0, 'true' and 'yes' values are true
1478# rest of values are treated as false (never as error)
1479sub config_to_bool {
1480        my $val = shift;
1481
1482        # strip leading and trailing whitespace
1483        $val =~ s/^\s+//;
1484        $val =~ s/\s+$//;
1485
1486        return (!defined $val ||               # section.key
1487                ($val =~ /^\d+$/ && $val) ||   # section.key = 1
1488                ($val =~ /^(?:true|yes)$/i));  # section.key = true
1489}
1490
1491# convert config value to simple decimal number
1492# an optional value suffix of 'k', 'm', or 'g' will cause the value
1493# to be multiplied by 1024, 1048576, or 1073741824
1494sub config_to_int {
1495        my $val = shift;
1496
1497        # strip leading and trailing whitespace
1498        $val =~ s/^\s+//;
1499        $val =~ s/\s+$//;
1500
1501        if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
1502                $unit = lc($unit);
1503                # unknown unit is treated as 1
1504                return $num * ($unit eq 'g' ? 1073741824 :
1505                               $unit eq 'm' ?    1048576 :
1506                               $unit eq 'k' ?       1024 : 1);
1507        }
1508        return $val;
1509}
1510
1511# convert config value to array reference, if needed
1512sub config_to_multi {
1513        my $val = shift;
1514
1515        return ref($val) ? $val : [ $val ];
1516}
1517
1518sub git_get_project_config {
1519        my ($key, $type) = @_;
1520
1521        # key sanity check
1522        return unless ($key);
1523        $key =~ s/^gitweb\.//;
1524        return if ($key =~ m/\W/);
1525
1526        # type sanity check
1527        if (defined $type) {
1528                $type =~ s/^--//;
1529                $type = undef
1530                        unless ($type eq 'bool' || $type eq 'int');
1531        }
1532
1533        # get config
1534        if (!defined $config_file ||
1535            $config_file ne "$git_dir/config") {
1536                %config = git_parse_project_config('gitweb');
1537                $config_file = "$git_dir/config";
1538        }
1539
1540        # ensure given type
1541        if (!defined $type) {
1542                return $config{"gitweb.$key"};
1543        } elsif ($type eq 'bool') {
1544                # backward compatibility: 'git config --bool' returns true/false
1545                return config_to_bool($config{"gitweb.$key"}) ? 'true' : 'false';
1546        } elsif ($type eq 'int') {
1547                return config_to_int($config{"gitweb.$key"});
1548        }
1549        return $config{"gitweb.$key"};
1550}
1551
1552# get hash of given path at given ref
1553sub git_get_hash_by_path {
1554        my $base = shift;
1555        my $path = shift || return undef;
1556        my $type = shift;
1557
1558        $path =~ s,/+$,,;
1559
1560        open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
1561                or die_error(undef, "Open git-ls-tree failed");
1562        my $line = <$fd>;
1563        close $fd or return undef;
1564
1565        if (!defined $line) {
1566                # there is no tree or hash given by $path at $base
1567                return undef;
1568        }
1569
1570        #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa  panic.c'
1571        $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
1572        if (defined $type && $type ne $2) {
1573                # type doesn't match
1574                return undef;
1575        }
1576        return $3;
1577}
1578
1579# get path of entry with given hash at given tree-ish (ref)
1580# used to get 'from' filename for combined diff (merge commit) for renames
1581sub git_get_path_by_hash {
1582        my $base = shift || return;
1583        my $hash = shift || return;
1584
1585        local $/ = "\0";
1586
1587        open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
1588                or return undef;
1589        while (my $line = <$fd>) {
1590                chomp $line;
1591
1592                #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423  gitweb'
1593                #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f  gitweb/README'
1594                if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
1595                        close $fd;
1596                        return $1;
1597                }
1598        }
1599        close $fd;
1600        return undef;
1601}
1602
1603## ......................................................................
1604## git utility functions, directly accessing git repository
1605
1606sub git_get_project_description {
1607        my $path = shift;
1608
1609        $git_dir = "$projectroot/$path";
1610        open my $fd, "$projectroot/$path/description"
1611                or return git_get_project_config('description');
1612        my $descr = <$fd>;
1613        close $fd;
1614        if (defined $descr) {
1615                chomp $descr;
1616        }
1617        return $descr;
1618}
1619
1620sub git_get_project_url_list {
1621        my $path = shift;
1622
1623        $git_dir = "$projectroot/$path";
1624        open my $fd, "$projectroot/$path/cloneurl"
1625                or return wantarray ?
1626                @{ config_to_multi(git_get_project_config('url')) } :
1627                   config_to_multi(git_get_project_config('url'));
1628        my @git_project_url_list = map { chomp; $_ } <$fd>;
1629        close $fd;
1630
1631        return wantarray ? @git_project_url_list : \@git_project_url_list;
1632}
1633
1634sub git_get_projects_list {
1635        my ($filter) = @_;
1636        my @list;
1637
1638        $filter ||= '';
1639        $filter =~ s/\.git$//;
1640
1641        my ($check_forks) = gitweb_check_feature('forks');
1642
1643        if (-d $projects_list) {
1644                # search in directory
1645                my $dir = $projects_list . ($filter ? "/$filter" : '');
1646                # remove the trailing "/"
1647                $dir =~ s!/+$!!;
1648                my $pfxlen = length("$dir");
1649                my $pfxdepth = ($dir =~ tr!/!!);
1650
1651                File::Find::find({
1652                        follow_fast => 1, # follow symbolic links
1653                        follow_skip => 2, # ignore duplicates
1654                        dangling_symlinks => 0, # ignore dangling symlinks, silently
1655                        wanted => sub {
1656                                # skip project-list toplevel, if we get it.
1657                                return if (m!^[/.]$!);
1658                                # only directories can be git repositories
1659                                return unless (-d $_);
1660                                # don't traverse too deep (Find is super slow on os x)
1661                                if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
1662                                        $File::Find::prune = 1;
1663                                        return;
1664                                }
1665
1666                                my $subdir = substr($File::Find::name, $pfxlen + 1);
1667                                # we check related file in $projectroot
1668                                if ($check_forks and $subdir =~ m#/.#) {
1669                                        $File::Find::prune = 1;
1670                                } elsif (check_export_ok("$projectroot/$filter/$subdir")) {
1671                                        push @list, { path => ($filter ? "$filter/" : '') . $subdir };
1672                                        $File::Find::prune = 1;
1673                                }
1674                        },
1675                }, "$dir");
1676
1677        } elsif (-f $projects_list) {
1678                # read from file(url-encoded):
1679                # 'git%2Fgit.git Linus+Torvalds'
1680                # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1681                # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1682                my %paths;
1683                open my ($fd), $projects_list or return;
1684        PROJECT:
1685                while (my $line = <$fd>) {
1686                        chomp $line;
1687                        my ($path, $owner) = split ' ', $line;
1688                        $path = unescape($path);
1689                        $owner = unescape($owner);
1690                        if (!defined $path) {
1691                                next;
1692                        }
1693                        if ($filter ne '') {
1694                                # looking for forks;
1695                                my $pfx = substr($path, 0, length($filter));
1696                                if ($pfx ne $filter) {
1697                                        next PROJECT;
1698                                }
1699                                my $sfx = substr($path, length($filter));
1700                                if ($sfx !~ /^\/.*\.git$/) {
1701                                        next PROJECT;
1702                                }
1703                        } elsif ($check_forks) {
1704                        PATH:
1705                                foreach my $filter (keys %paths) {
1706                                        # looking for forks;
1707                                        my $pfx = substr($path, 0, length($filter));
1708                                        if ($pfx ne $filter) {
1709                                                next PATH;
1710                                        }
1711                                        my $sfx = substr($path, length($filter));
1712                                        if ($sfx !~ /^\/.*\.git$/) {
1713                                                next PATH;
1714                                        }
1715                                        # is a fork, don't include it in
1716                                        # the list
1717                                        next PROJECT;
1718                                }
1719                        }
1720                        if (check_export_ok("$projectroot/$path")) {
1721                                my $pr = {
1722                                        path => $path,
1723                                        owner => to_utf8($owner),
1724                                };
1725                                push @list, $pr;
1726                                (my $forks_path = $path) =~ s/\.git$//;
1727                                $paths{$forks_path}++;
1728                        }
1729                }
1730                close $fd;
1731        }
1732        return @list;
1733}
1734
1735our $gitweb_project_owner = undef;
1736sub git_get_project_list_from_file {
1737
1738        return if (defined $gitweb_project_owner);
1739
1740        $gitweb_project_owner = {};
1741        # read from file (url-encoded):
1742        # 'git%2Fgit.git Linus+Torvalds'
1743        # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1744        # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1745        if (-f $projects_list) {
1746                open (my $fd , $projects_list);
1747                while (my $line = <$fd>) {
1748                        chomp $line;
1749                        my ($pr, $ow) = split ' ', $line;
1750                        $pr = unescape($pr);
1751                        $ow = unescape($ow);
1752                        $gitweb_project_owner->{$pr} = to_utf8($ow);
1753                }
1754                close $fd;
1755        }
1756}
1757
1758sub git_get_project_owner {
1759        my $project = shift;
1760        my $owner;
1761
1762        return undef unless $project;
1763
1764        if (!defined $gitweb_project_owner) {
1765                git_get_project_list_from_file();
1766        }
1767
1768        if (exists $gitweb_project_owner->{$project}) {
1769                $owner = $gitweb_project_owner->{$project};
1770        }
1771        if (!defined $owner) {
1772                $owner = get_file_owner("$projectroot/$project");
1773        }
1774
1775        return $owner;
1776}
1777
1778sub git_get_last_activity {
1779        my ($path) = @_;
1780        my $fd;
1781
1782        $git_dir = "$projectroot/$path";
1783        open($fd, "-|", git_cmd(), 'for-each-ref',
1784             '--format=%(committer)',
1785             '--sort=-committerdate',
1786             '--count=1',
1787             'refs/heads') or return;
1788        my $most_recent = <$fd>;
1789        close $fd or return;
1790        if (defined $most_recent &&
1791            $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
1792                my $timestamp = $1;
1793                my $age = time - $timestamp;
1794                return ($age, age_string($age));
1795        }
1796        return (undef, undef);
1797}
1798
1799sub git_get_references {
1800        my $type = shift || "";
1801        my %refs;
1802        # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
1803        # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
1804        open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
1805                ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
1806                or return;
1807
1808        while (my $line = <$fd>) {
1809                chomp $line;
1810                if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type/?[^^]+)!) {
1811                        if (defined $refs{$1}) {
1812                                push @{$refs{$1}}, $2;
1813                        } else {
1814                                $refs{$1} = [ $2 ];
1815                        }
1816                }
1817        }
1818        close $fd or return;
1819        return \%refs;
1820}
1821
1822sub git_get_rev_name_tags {
1823        my $hash = shift || return undef;
1824
1825        open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
1826                or return;
1827        my $name_rev = <$fd>;
1828        close $fd;
1829
1830        if ($name_rev =~ m|^$hash tags/(.*)$|) {
1831                return $1;
1832        } else {
1833                # catches also '$hash undefined' output
1834                return undef;
1835        }
1836}
1837
1838## ----------------------------------------------------------------------
1839## parse to hash functions
1840
1841sub parse_date {
1842        my $epoch = shift;
1843        my $tz = shift || "-0000";
1844
1845        my %date;
1846        my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
1847        my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
1848        my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
1849        $date{'hour'} = $hour;
1850        $date{'minute'} = $min;
1851        $date{'mday'} = $mday;
1852        $date{'day'} = $days[$wday];
1853        $date{'month'} = $months[$mon];
1854        $date{'rfc2822'}   = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
1855                             $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
1856        $date{'mday-time'} = sprintf "%d %s %02d:%02d",
1857                             $mday, $months[$mon], $hour ,$min;
1858        $date{'iso-8601'}  = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
1859                             1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
1860
1861        $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
1862        my $local = $epoch + ((int $1 + ($2/60)) * 3600);
1863        ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
1864        $date{'hour_local'} = $hour;
1865        $date{'minute_local'} = $min;
1866        $date{'tz_local'} = $tz;
1867        $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
1868                                  1900+$year, $mon+1, $mday,
1869                                  $hour, $min, $sec, $tz);
1870        return %date;
1871}
1872
1873sub parse_tag {
1874        my $tag_id = shift;
1875        my %tag;
1876        my @comment;
1877
1878        open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
1879        $tag{'id'} = $tag_id;
1880        while (my $line = <$fd>) {
1881                chomp $line;
1882                if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
1883                        $tag{'object'} = $1;
1884                } elsif ($line =~ m/^type (.+)$/) {
1885                        $tag{'type'} = $1;
1886                } elsif ($line =~ m/^tag (.+)$/) {
1887                        $tag{'name'} = $1;
1888                } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
1889                        $tag{'author'} = $1;
1890                        $tag{'epoch'} = $2;
1891                        $tag{'tz'} = $3;
1892                } elsif ($line =~ m/--BEGIN/) {
1893                        push @comment, $line;
1894                        last;
1895                } elsif ($line eq "") {
1896                        last;
1897                }
1898        }
1899        push @comment, <$fd>;
1900        $tag{'comment'} = \@comment;
1901        close $fd or return;
1902        if (!defined $tag{'name'}) {
1903                return
1904        };
1905        return %tag
1906}
1907
1908sub parse_commit_text {
1909        my ($commit_text, $withparents) = @_;
1910        my @commit_lines = split '\n', $commit_text;
1911        my %co;
1912
1913        pop @commit_lines; # Remove '\0'
1914
1915        if (! @commit_lines) {
1916                return;
1917        }
1918
1919        my $header = shift @commit_lines;
1920        if ($header !~ m/^[0-9a-fA-F]{40}/) {
1921                return;
1922        }
1923        ($co{'id'}, my @parents) = split ' ', $header;
1924        while (my $line = shift @commit_lines) {
1925                last if $line eq "\n";
1926                if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
1927                        $co{'tree'} = $1;
1928                } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
1929                        push @parents, $1;
1930                } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
1931                        $co{'author'} = $1;
1932                        $co{'author_epoch'} = $2;
1933                        $co{'author_tz'} = $3;
1934                        if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
1935                                $co{'author_name'}  = $1;
1936                                $co{'author_email'} = $2;
1937                        } else {
1938                                $co{'author_name'} = $co{'author'};
1939                        }
1940                } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
1941                        $co{'committer'} = $1;
1942                        $co{'committer_epoch'} = $2;
1943                        $co{'committer_tz'} = $3;
1944                        $co{'committer_name'} = $co{'committer'};
1945                        if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
1946                                $co{'committer_name'}  = $1;
1947                                $co{'committer_email'} = $2;
1948                        } else {
1949                                $co{'committer_name'} = $co{'committer'};
1950                        }
1951                }
1952        }
1953        if (!defined $co{'tree'}) {
1954                return;
1955        };
1956        $co{'parents'} = \@parents;
1957        $co{'parent'} = $parents[0];
1958
1959        foreach my $title (@commit_lines) {
1960                $title =~ s/^    //;
1961                if ($title ne "") {
1962                        $co{'title'} = chop_str($title, 80, 5);
1963                        # remove leading stuff of merges to make the interesting part visible
1964                        if (length($title) > 50) {
1965                                $title =~ s/^Automatic //;
1966                                $title =~ s/^merge (of|with) /Merge ... /i;
1967                                if (length($title) > 50) {
1968                                        $title =~ s/(http|rsync):\/\///;
1969                                }
1970                                if (length($title) > 50) {
1971                                        $title =~ s/(master|www|rsync)\.//;
1972                                }
1973                                if (length($title) > 50) {
1974                                        $title =~ s/kernel.org:?//;
1975                                }
1976                                if (length($title) > 50) {
1977                                        $title =~ s/\/pub\/scm//;
1978                                }
1979                        }
1980                        $co{'title_short'} = chop_str($title, 50, 5);
1981                        last;
1982                }
1983        }
1984        if ($co{'title'} eq "") {
1985                $co{'title'} = $co{'title_short'} = '(no commit message)';
1986        }
1987        # remove added spaces
1988        foreach my $line (@commit_lines) {
1989                $line =~ s/^    //;
1990        }
1991        $co{'comment'} = \@commit_lines;
1992
1993        my $age = time - $co{'committer_epoch'};
1994        $co{'age'} = $age;
1995        $co{'age_string'} = age_string($age);
1996        my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
1997        if ($age > 60*60*24*7*2) {
1998                $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1999                $co{'age_string_age'} = $co{'age_string'};
2000        } else {
2001                $co{'age_string_date'} = $co{'age_string'};
2002                $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2003        }
2004        return %co;
2005}
2006
2007sub parse_commit {
2008        my ($commit_id) = @_;
2009        my %co;
2010
2011        local $/ = "\0";
2012
2013        open my $fd, "-|", git_cmd(), "rev-list",
2014                "--parents",
2015                "--header",
2016                "--max-count=1",
2017                $commit_id,
2018                "--",
2019                or die_error(undef, "Open git-rev-list failed");
2020        %co = parse_commit_text(<$fd>, 1);
2021        close $fd;
2022
2023        return %co;
2024}
2025
2026sub parse_commits {
2027        my ($commit_id, $maxcount, $skip, $arg, $filename) = @_;
2028        my @cos;
2029
2030        $maxcount ||= 1;
2031        $skip ||= 0;
2032
2033        local $/ = "\0";
2034
2035        open my $fd, "-|", git_cmd(), "rev-list",
2036                "--header",
2037                ($arg ? ($arg) : ()),
2038                ("--max-count=" . $maxcount),
2039                ("--skip=" . $skip),
2040                @extra_options,
2041                $commit_id,
2042                "--",
2043                ($filename ? ($filename) : ())
2044                or die_error(undef, "Open git-rev-list failed");
2045        while (my $line = <$fd>) {
2046                my %co = parse_commit_text($line);
2047                push @cos, \%co;
2048        }
2049        close $fd;
2050
2051        return wantarray ? @cos : \@cos;
2052}
2053
2054# parse ref from ref_file, given by ref_id, with given type
2055sub parse_ref {
2056        my $ref_file = shift;
2057        my $ref_id = shift;
2058        my $type = shift || git_get_type($ref_id);
2059        my %ref_item;
2060
2061        $ref_item{'type'} = $type;
2062        $ref_item{'id'} = $ref_id;
2063        $ref_item{'epoch'} = 0;
2064        $ref_item{'age'} = "unknown";
2065        if ($type eq "tag") {
2066                my %tag = parse_tag($ref_id);
2067                $ref_item{'comment'} = $tag{'comment'};
2068                if ($tag{'type'} eq "commit") {
2069                        my %co = parse_commit($tag{'object'});
2070                        $ref_item{'epoch'} = $co{'committer_epoch'};
2071                        $ref_item{'age'} = $co{'age_string'};
2072                } elsif (defined($tag{'epoch'})) {
2073                        my $age = time - $tag{'epoch'};
2074                        $ref_item{'epoch'} = $tag{'epoch'};
2075                        $ref_item{'age'} = age_string($age);
2076                }
2077                $ref_item{'reftype'} = $tag{'type'};
2078                $ref_item{'name'} = $tag{'name'};
2079                $ref_item{'refid'} = $tag{'object'};
2080        } elsif ($type eq "commit"){
2081                my %co = parse_commit($ref_id);
2082                $ref_item{'reftype'} = "commit";
2083                $ref_item{'name'} = $ref_file;
2084                $ref_item{'title'} = $co{'title'};
2085                $ref_item{'refid'} = $ref_id;
2086                $ref_item{'epoch'} = $co{'committer_epoch'};
2087                $ref_item{'age'} = $co{'age_string'};
2088        } else {
2089                $ref_item{'reftype'} = $type;
2090                $ref_item{'name'} = $ref_file;
2091                $ref_item{'refid'} = $ref_id;
2092        }
2093
2094        return %ref_item;
2095}
2096
2097# parse line of git-diff-tree "raw" output
2098sub parse_difftree_raw_line {
2099        my $line = shift;
2100        my %res;
2101
2102        # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M   ls-files.c'
2103        # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M   rev-tree.c'
2104        if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
2105                $res{'from_mode'} = $1;
2106                $res{'to_mode'} = $2;
2107                $res{'from_id'} = $3;
2108                $res{'to_id'} = $4;
2109                $res{'status'} = $res{'status_str'} = $5;
2110                $res{'similarity'} = $6;
2111                if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
2112                        ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
2113                } else {
2114                        $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote($7);
2115                }
2116        }
2117        # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
2118        # combined diff (for merge commit)
2119        elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
2120                $res{'nparents'}  = length($1);
2121                $res{'from_mode'} = [ split(' ', $2) ];
2122                $res{'to_mode'} = pop @{$res{'from_mode'}};
2123                $res{'from_id'} = [ split(' ', $3) ];
2124                $res{'to_id'} = pop @{$res{'from_id'}};
2125                $res{'status_str'} = $4;
2126                $res{'status'} = [ split('', $4) ];
2127                $res{'to_file'} = unquote($5);
2128        }
2129        # 'c512b523472485aef4fff9e57b229d9d243c967f'
2130        elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
2131                $res{'commit'} = $1;
2132        }
2133
2134        return wantarray ? %res : \%res;
2135}
2136
2137# wrapper: return parsed line of git-diff-tree "raw" output
2138# (the argument might be raw line, or parsed info)
2139sub parsed_difftree_line {
2140        my $line_or_ref = shift;
2141
2142        if (ref($line_or_ref) eq "HASH") {
2143                # pre-parsed (or generated by hand)
2144                return $line_or_ref;
2145        } else {
2146                return parse_difftree_raw_line($line_or_ref);
2147        }
2148}
2149
2150# parse line of git-ls-tree output
2151sub parse_ls_tree_line ($;%) {
2152        my $line = shift;
2153        my %opts = @_;
2154        my %res;
2155
2156        #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa  panic.c'
2157        $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
2158
2159        $res{'mode'} = $1;
2160        $res{'type'} = $2;
2161        $res{'hash'} = $3;
2162        if ($opts{'-z'}) {
2163                $res{'name'} = $4;
2164        } else {
2165                $res{'name'} = unquote($4);
2166        }
2167
2168        return wantarray ? %res : \%res;
2169}
2170
2171# generates _two_ hashes, references to which are passed as 2 and 3 argument
2172sub parse_from_to_diffinfo {
2173        my ($diffinfo, $from, $to, @parents) = @_;
2174
2175        if ($diffinfo->{'nparents'}) {
2176                # combined diff
2177                $from->{'file'} = [];
2178                $from->{'href'} = [];
2179                fill_from_file_info($diffinfo, @parents)
2180                        unless exists $diffinfo->{'from_file'};
2181                for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2182                        $from->{'file'}[$i] =
2183                                defined $diffinfo->{'from_file'}[$i] ?
2184                                        $diffinfo->{'from_file'}[$i] :
2185                                        $diffinfo->{'to_file'};
2186                        if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
2187                                $from->{'href'}[$i] = href(action=>"blob",
2188                                                           hash_base=>$parents[$i],
2189                                                           hash=>$diffinfo->{'from_id'}[$i],
2190                                                           file_name=>$from->{'file'}[$i]);
2191                        } else {
2192                                $from->{'href'}[$i] = undef;
2193                        }
2194                }
2195        } else {
2196                # ordinary (not combined) diff
2197                $from->{'file'} = $diffinfo->{'from_file'};
2198                if ($diffinfo->{'status'} ne "A") { # not new (added) file
2199                        $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2200                                               hash=>$diffinfo->{'from_id'},
2201                                               file_name=>$from->{'file'});
2202                } else {
2203                        delete $from->{'href'};
2204                }
2205        }
2206
2207        $to->{'file'} = $diffinfo->{'to_file'};
2208        if (!is_deleted($diffinfo)) { # file exists in result
2209                $to->{'href'} = href(action=>"blob", hash_base=>$hash,
2210                                     hash=>$diffinfo->{'to_id'},
2211                                     file_name=>$to->{'file'});
2212        } else {
2213                delete $to->{'href'};
2214        }
2215}
2216
2217## ......................................................................
2218## parse to array of hashes functions
2219
2220sub git_get_heads_list {
2221        my $limit = shift;
2222        my @headslist;
2223
2224        open my $fd, '-|', git_cmd(), 'for-each-ref',
2225                ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
2226                '--format=%(objectname) %(refname) %(subject)%00%(committer)',
2227                'refs/heads'
2228                or return;
2229        while (my $line = <$fd>) {
2230                my %ref_item;
2231
2232                chomp $line;
2233                my ($refinfo, $committerinfo) = split(/\0/, $line);
2234                my ($hash, $name, $title) = split(' ', $refinfo, 3);
2235                my ($committer, $epoch, $tz) =
2236                        ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
2237                $name =~ s!^refs/heads/!!;
2238
2239                $ref_item{'name'}  = $name;
2240                $ref_item{'id'}    = $hash;
2241                $ref_item{'title'} = $title || '(no commit message)';
2242                $ref_item{'epoch'} = $epoch;
2243                if ($epoch) {
2244                        $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2245                } else {
2246                        $ref_item{'age'} = "unknown";
2247                }
2248
2249                push @headslist, \%ref_item;
2250        }
2251        close $fd;
2252
2253        return wantarray ? @headslist : \@headslist;
2254}
2255
2256sub git_get_tags_list {
2257        my $limit = shift;
2258        my @tagslist;
2259
2260        open my $fd, '-|', git_cmd(), 'for-each-ref',
2261                ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
2262                '--format=%(objectname) %(objecttype) %(refname) '.
2263                '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
2264                'refs/tags'
2265                or return;
2266        while (my $line = <$fd>) {
2267                my %ref_item;
2268
2269                chomp $line;
2270                my ($refinfo, $creatorinfo) = split(/\0/, $line);
2271                my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
2272                my ($creator, $epoch, $tz) =
2273                        ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
2274                $name =~ s!^refs/tags/!!;
2275
2276                $ref_item{'type'} = $type;
2277                $ref_item{'id'} = $id;
2278                $ref_item{'name'} = $name;
2279                if ($type eq "tag") {
2280                        $ref_item{'subject'} = $title;
2281                        $ref_item{'reftype'} = $reftype;
2282                        $ref_item{'refid'}   = $refid;
2283                } else {
2284                        $ref_item{'reftype'} = $type;
2285                        $ref_item{'refid'}   = $id;
2286                }
2287
2288                if ($type eq "tag" || $type eq "commit") {
2289                        $ref_item{'epoch'} = $epoch;
2290                        if ($epoch) {
2291                                $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2292                        } else {
2293                                $ref_item{'age'} = "unknown";
2294                        }
2295                }
2296
2297                push @tagslist, \%ref_item;
2298        }
2299        close $fd;
2300
2301        return wantarray ? @tagslist : \@tagslist;
2302}
2303
2304## ----------------------------------------------------------------------
2305## filesystem-related functions
2306
2307sub get_file_owner {
2308        my $path = shift;
2309
2310        my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
2311        my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
2312        if (!defined $gcos) {
2313                return undef;
2314        }
2315        my $owner = $gcos;
2316        $owner =~ s/[,;].*$//;
2317        return to_utf8($owner);
2318}
2319
2320## ......................................................................
2321## mimetype related functions
2322
2323sub mimetype_guess_file {
2324        my $filename = shift;
2325        my $mimemap = shift;
2326        -r $mimemap or return undef;
2327
2328        my %mimemap;
2329        open(MIME, $mimemap) or return undef;
2330        while (<MIME>) {
2331                next if m/^#/; # skip comments
2332                my ($mime, $exts) = split(/\t+/);
2333                if (defined $exts) {
2334                        my @exts = split(/\s+/, $exts);
2335                        foreach my $ext (@exts) {
2336                                $mimemap{$ext} = $mime;
2337                        }
2338                }
2339        }
2340        close(MIME);
2341
2342        $filename =~ /\.([^.]*)$/;
2343        return $mimemap{$1};
2344}
2345
2346sub mimetype_guess {
2347        my $filename = shift;
2348        my $mime;
2349        $filename =~ /\./ or return undef;
2350
2351        if ($mimetypes_file) {
2352                my $file = $mimetypes_file;
2353                if ($file !~ m!^/!) { # if it is relative path
2354                        # it is relative to project
2355                        $file = "$projectroot/$project/$file";
2356                }
2357                $mime = mimetype_guess_file($filename, $file);
2358        }
2359        $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
2360        return $mime;
2361}
2362
2363sub blob_mimetype {
2364        my $fd = shift;
2365        my $filename = shift;
2366
2367        if ($filename) {
2368                my $mime = mimetype_guess($filename);
2369                $mime and return $mime;
2370        }
2371
2372        # just in case
2373        return $default_blob_plain_mimetype unless $fd;
2374
2375        if (-T $fd) {
2376                return 'text/plain' .
2377                       ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
2378        } elsif (! $filename) {
2379                return 'application/octet-stream';
2380        } elsif ($filename =~ m/\.png$/i) {
2381                return 'image/png';
2382        } elsif ($filename =~ m/\.gif$/i) {
2383                return 'image/gif';
2384        } elsif ($filename =~ m/\.jpe?g$/i) {
2385                return 'image/jpeg';
2386        } else {
2387                return 'application/octet-stream';
2388        }
2389}
2390
2391## ======================================================================
2392## functions printing HTML: header, footer, error page
2393
2394sub git_header_html {
2395        my $status = shift || "200 OK";
2396        my $expires = shift;
2397
2398        my $title = "$site_name";
2399        if (defined $project) {
2400                $title .= " - " . to_utf8($project);
2401                if (defined $action) {
2402                        $title .= "/$action";
2403                        if (defined $file_name) {
2404                                $title .= " - " . esc_path($file_name);
2405                                if ($action eq "tree" && $file_name !~ m|/$|) {
2406                                        $title .= "/";
2407                                }
2408                        }
2409                }
2410        }
2411        my $content_type;
2412        # require explicit support from the UA if we are to send the page as
2413        # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
2414        # we have to do this because MSIE sometimes globs '*/*', pretending to
2415        # support xhtml+xml but choking when it gets what it asked for.
2416        if (defined $cgi->http('HTTP_ACCEPT') &&
2417            $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
2418            $cgi->Accept('application/xhtml+xml') != 0) {
2419                $content_type = 'application/xhtml+xml';
2420        } else {
2421                $content_type = 'text/html';
2422        }
2423        print $cgi->header(-type=>$content_type, -charset => 'utf-8',
2424                           -status=> $status, -expires => $expires);
2425        my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
2426        print <<EOF;
2427<?xml version="1.0" encoding="utf-8"?>
2428<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2429<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
2430<!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
2431<!-- git core binaries version $git_version -->
2432<head>
2433<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
2434<meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
2435<meta name="robots" content="index, nofollow"/>
2436<title>$title</title>
2437EOF
2438# print out each stylesheet that exist
2439        if (defined $stylesheet) {
2440#provides backwards capability for those people who define style sheet in a config file
2441                print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2442        } else {
2443                foreach my $stylesheet (@stylesheets) {
2444                        next unless $stylesheet;
2445                        print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2446                }
2447        }
2448        if (defined $project) {
2449                printf('<link rel="alternate" title="%s log RSS feed" '.
2450                       'href="%s" type="application/rss+xml" />'."\n",
2451                       esc_param($project), href(action=>"rss"));
2452                printf('<link rel="alternate" title="%s log RSS feed (no merges)" '.
2453                       'href="%s" type="application/rss+xml" />'."\n",
2454                       esc_param($project), href(action=>"rss",
2455                                                 extra_options=>"--no-merges"));
2456                printf('<link rel="alternate" title="%s log Atom feed" '.
2457                       'href="%s" type="application/atom+xml" />'."\n",
2458                       esc_param($project), href(action=>"atom"));
2459                printf('<link rel="alternate" title="%s log Atom feed (no merges)" '.
2460                       'href="%s" type="application/atom+xml" />'."\n",
2461                       esc_param($project), href(action=>"atom",
2462                                                 extra_options=>"--no-merges"));
2463        } else {
2464                printf('<link rel="alternate" title="%s projects list" '.
2465                       'href="%s" type="text/plain; charset=utf-8"/>'."\n",
2466                       $site_name, href(project=>undef, action=>"project_index"));
2467                printf('<link rel="alternate" title="%s projects feeds" '.
2468                       'href="%s" type="text/x-opml"/>'."\n",
2469                       $site_name, href(project=>undef, action=>"opml"));
2470        }
2471        if (defined $favicon) {
2472                print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n);
2473        }
2474
2475        print "</head>\n" .
2476              "<body>\n";
2477
2478        if (-f $site_header) {
2479                open (my $fd, $site_header);
2480                print <$fd>;
2481                close $fd;
2482        }
2483
2484        print "<div class=\"page_header\">\n" .
2485              $cgi->a({-href => esc_url($logo_url),
2486                       -title => $logo_label},
2487                      qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
2488        print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
2489        if (defined $project) {
2490                print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
2491                if (defined $action) {
2492                        print " / $action";
2493                }
2494                print "\n";
2495        }
2496        print "</div>\n";
2497
2498        my ($have_search) = gitweb_check_feature('search');
2499        if ((defined $project) && ($have_search)) {
2500                if (!defined $searchtext) {
2501                        $searchtext = "";
2502                }
2503                my $search_hash;
2504                if (defined $hash_base) {
2505                        $search_hash = $hash_base;
2506                } elsif (defined $hash) {
2507                        $search_hash = $hash;
2508                } else {
2509                        $search_hash = "HEAD";
2510                }
2511                my $action = $my_uri;
2512                my ($use_pathinfo) = gitweb_check_feature('pathinfo');
2513                if ($use_pathinfo) {
2514                        $action .= "/$project";
2515                } else {
2516                        $cgi->param("p", $project);
2517                }
2518                $cgi->param("a", "search");
2519                $cgi->param("h", $search_hash);
2520                print $cgi->startform(-method => "get", -action => $action) .
2521                      "<div class=\"search\">\n" .
2522                      (!$use_pathinfo && $cgi->hidden(-name => "p") . "\n") .
2523                      $cgi->hidden(-name => "a") . "\n" .
2524                      $cgi->hidden(-name => "h") . "\n" .
2525                      $cgi->popup_menu(-name => 'st', -default => 'commit',
2526                                       -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
2527                      $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
2528                      " search:\n",
2529                      $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
2530                      "</div>" .
2531                      $cgi->end_form() . "\n";
2532        }
2533}
2534
2535sub git_footer_html {
2536        print "<div class=\"page_footer\">\n";
2537        if (defined $project) {
2538                my $descr = git_get_project_description($project);
2539                if (defined $descr) {
2540                        print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
2541                }
2542                print $cgi->a({-href => href(action=>"rss"),
2543                              -class => "rss_logo"}, "RSS") . " ";
2544                print $cgi->a({-href => href(action=>"atom"),
2545                              -class => "rss_logo"}, "Atom") . "\n";
2546        } else {
2547                print $cgi->a({-href => href(project=>undef, action=>"opml"),
2548                              -class => "rss_logo"}, "OPML") . " ";
2549                print $cgi->a({-href => href(project=>undef, action=>"project_index"),
2550                              -class => "rss_logo"}, "TXT") . "\n";
2551        }
2552        print "</div>\n" ;
2553
2554        if (-f $site_footer) {
2555                open (my $fd, $site_footer);
2556                print <$fd>;
2557                close $fd;
2558        }
2559
2560        print "</body>\n" .
2561              "</html>";
2562}
2563
2564sub die_error {
2565        my $status = shift || "403 Forbidden";
2566        my $error = shift || "Malformed query, file missing or permission denied";
2567
2568        git_header_html($status);
2569        print <<EOF;
2570<div class="page_body">
2571<br /><br />
2572$status - $error
2573<br />
2574</div>
2575EOF
2576        git_footer_html();
2577        exit;
2578}
2579
2580## ----------------------------------------------------------------------
2581## functions printing or outputting HTML: navigation
2582
2583sub git_print_page_nav {
2584        my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
2585        $extra = '' if !defined $extra; # pager or formats
2586
2587        my @navs = qw(summary shortlog log commit commitdiff tree);
2588        if ($suppress) {
2589                @navs = grep { $_ ne $suppress } @navs;
2590        }
2591
2592        my %arg = map { $_ => {action=>$_} } @navs;
2593        if (defined $head) {
2594                for (qw(commit commitdiff)) {
2595                        $arg{$_}{'hash'} = $head;
2596                }
2597                if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
2598                        for (qw(shortlog log)) {
2599                                $arg{$_}{'hash'} = $head;
2600                        }
2601                }
2602        }
2603        $arg{'tree'}{'hash'} = $treehead if defined $treehead;
2604        $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
2605
2606        print "<div class=\"page_nav\">\n" .
2607                (join " | ",
2608                 map { $_ eq $current ?
2609                       $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
2610                 } @navs);
2611        print "<br/>\n$extra<br/>\n" .
2612              "</div>\n";
2613}
2614
2615sub format_paging_nav {
2616        my ($action, $hash, $head, $page, $nrevs) = @_;
2617        my $paging_nav;
2618
2619
2620        if ($hash ne $head || $page) {
2621                $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
2622        } else {
2623                $paging_nav .= "HEAD";
2624        }
2625
2626        if ($page > 0) {
2627                $paging_nav .= " &sdot; " .
2628                        $cgi->a({-href => href(-replay=>1, page=>$page-1),
2629                                 -accesskey => "p", -title => "Alt-p"}, "prev");
2630        } else {
2631                $paging_nav .= " &sdot; prev";
2632        }
2633
2634        if ($nrevs >= (100 * ($page+1)-1)) {
2635                $paging_nav .= " &sdot; " .
2636                        $cgi->a({-href => href(-replay=>1, page=>$page+1),
2637                                 -accesskey => "n", -title => "Alt-n"}, "next");
2638        } else {
2639                $paging_nav .= " &sdot; next";
2640        }
2641
2642        return $paging_nav;
2643}
2644
2645## ......................................................................
2646## functions printing or outputting HTML: div
2647
2648sub git_print_header_div {
2649        my ($action, $title, $hash, $hash_base) = @_;
2650        my %args = ();
2651
2652        $args{'action'} = $action;
2653        $args{'hash'} = $hash if $hash;
2654        $args{'hash_base'} = $hash_base if $hash_base;
2655
2656        print "<div class=\"header\">\n" .
2657              $cgi->a({-href => href(%args), -class => "title"},
2658              $title ? $title : $action) .
2659              "\n</div>\n";
2660}
2661
2662#sub git_print_authorship (\%) {
2663sub git_print_authorship {
2664        my $co = shift;
2665
2666        my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
2667        print "<div class=\"author_date\">" .
2668              esc_html($co->{'author_name'}) .
2669              " [$ad{'rfc2822'}";
2670        if ($ad{'hour_local'} < 6) {
2671                printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2672                       $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2673        } else {
2674                printf(" (%02d:%02d %s)",
2675                       $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2676        }
2677        print "]</div>\n";
2678}
2679
2680sub git_print_page_path {
2681        my $name = shift;
2682        my $type = shift;
2683        my $hb = shift;
2684
2685
2686        print "<div class=\"page_path\">";
2687        print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
2688                      -title => 'tree root'}, to_utf8("[$project]"));
2689        print " / ";
2690        if (defined $name) {
2691                my @dirname = split '/', $name;
2692                my $basename = pop @dirname;
2693                my $fullname = '';
2694
2695                foreach my $dir (@dirname) {
2696                        $fullname .= ($fullname ? '/' : '') . $dir;
2697                        print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
2698                                                     hash_base=>$hb),
2699                                      -title => $fullname}, esc_path($dir));
2700                        print " / ";
2701                }
2702                if (defined $type && $type eq 'blob') {
2703                        print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
2704                                                     hash_base=>$hb),
2705                                      -title => $name}, esc_path($basename));
2706                } elsif (defined $type && $type eq 'tree') {
2707                        print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
2708                                                     hash_base=>$hb),
2709                                      -title => $name}, esc_path($basename));
2710                        print " / ";
2711                } else {
2712                        print esc_path($basename);
2713                }
2714        }
2715        print "<br/></div>\n";
2716}
2717
2718# sub git_print_log (\@;%) {
2719sub git_print_log ($;%) {
2720        my $log = shift;
2721        my %opts = @_;
2722
2723        if ($opts{'-remove_title'}) {
2724                # remove title, i.e. first line of log
2725                shift @$log;
2726        }
2727        # remove leading empty lines
2728        while (defined $log->[0] && $log->[0] eq "") {
2729                shift @$log;
2730        }
2731
2732        # print log
2733        my $signoff = 0;
2734        my $empty = 0;
2735        foreach my $line (@$log) {
2736                if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
2737                        $signoff = 1;
2738                        $empty = 0;
2739                        if (! $opts{'-remove_signoff'}) {
2740                                print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
2741                                next;
2742                        } else {
2743                                # remove signoff lines
2744                                next;
2745                        }
2746                } else {
2747                        $signoff = 0;
2748                }
2749
2750                # print only one empty line
2751                # do not print empty line after signoff
2752                if ($line eq "") {
2753                        next if ($empty || $signoff);
2754                        $empty = 1;
2755                } else {
2756                        $empty = 0;
2757                }
2758
2759                print format_log_line_html($line) . "<br/>\n";
2760        }
2761
2762        if ($opts{'-final_empty_line'}) {
2763                # end with single empty line
2764                print "<br/>\n" unless $empty;
2765        }
2766}
2767
2768# return link target (what link points to)
2769sub git_get_link_target {
2770        my $hash = shift;
2771        my $link_target;
2772
2773        # read link
2774        open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2775                or return;
2776        {
2777                local $/;
2778                $link_target = <$fd>;
2779        }
2780        close $fd
2781                or return;
2782
2783        return $link_target;
2784}
2785
2786# given link target, and the directory (basedir) the link is in,
2787# return target of link relative to top directory (top tree);
2788# return undef if it is not possible (including absolute links).
2789sub normalize_link_target {
2790        my ($link_target, $basedir, $hash_base) = @_;
2791
2792        # we can normalize symlink target only if $hash_base is provided
2793        return unless $hash_base;
2794
2795        # absolute symlinks (beginning with '/') cannot be normalized
2796        return if (substr($link_target, 0, 1) eq '/');
2797
2798        # normalize link target to path from top (root) tree (dir)
2799        my $path;
2800        if ($basedir) {
2801                $path = $basedir . '/' . $link_target;
2802        } else {
2803                # we are in top (root) tree (dir)
2804                $path = $link_target;
2805        }
2806
2807        # remove //, /./, and /../
2808        my @path_parts;
2809        foreach my $part (split('/', $path)) {
2810                # discard '.' and ''
2811                next if (!$part || $part eq '.');
2812                # handle '..'
2813                if ($part eq '..') {
2814                        if (@path_parts) {
2815                                pop @path_parts;
2816                        } else {
2817                                # link leads outside repository (outside top dir)
2818                                return;
2819                        }
2820                } else {
2821                        push @path_parts, $part;
2822                }
2823        }
2824        $path = join('/', @path_parts);
2825
2826        return $path;
2827}
2828
2829# print tree entry (row of git_tree), but without encompassing <tr> element
2830sub git_print_tree_entry {
2831        my ($t, $basedir, $hash_base, $have_blame) = @_;
2832
2833        my %base_key = ();
2834        $base_key{'hash_base'} = $hash_base if defined $hash_base;
2835
2836        # The format of a table row is: mode list link.  Where mode is
2837        # the mode of the entry, list is the name of the entry, an href,
2838        # and link is the action links of the entry.
2839
2840        print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
2841        if ($t->{'type'} eq "blob") {
2842                print "<td class=\"list\">" .
2843                        $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
2844                                               file_name=>"$basedir$t->{'name'}", %base_key),
2845                                -class => "list"}, esc_path($t->{'name'}));
2846                if (S_ISLNK(oct $t->{'mode'})) {
2847                        my $link_target = git_get_link_target($t->{'hash'});
2848                        if ($link_target) {
2849                                my $norm_target = normalize_link_target($link_target, $basedir, $hash_base);
2850                                if (defined $norm_target) {
2851                                        print " -> " .
2852                                              $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
2853                                                                     file_name=>$norm_target),
2854                                                       -title => $norm_target}, esc_path($link_target));
2855                                } else {
2856                                        print " -> " . esc_path($link_target);
2857                                }
2858                        }
2859                }
2860                print "</td>\n";
2861                print "<td class=\"link\">";
2862                print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
2863                                             file_name=>"$basedir$t->{'name'}", %base_key)},
2864                              "blob");
2865                if ($have_blame) {
2866                        print " | " .
2867                              $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
2868                                                     file_name=>"$basedir$t->{'name'}", %base_key)},
2869                                      "blame");
2870                }
2871                if (defined $hash_base) {
2872                        print " | " .
2873                              $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2874                                                     hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
2875                                      "history");
2876                }
2877                print " | " .
2878                        $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
2879                                               file_name=>"$basedir$t->{'name'}")},
2880                                "raw");
2881                print "</td>\n";
2882
2883        } elsif ($t->{'type'} eq "tree") {
2884                print "<td class=\"list\">";
2885                print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
2886                                             file_name=>"$basedir$t->{'name'}", %base_key)},
2887                              esc_path($t->{'name'}));
2888                print "</td>\n";
2889                print "<td class=\"link\">";
2890                print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
2891                                             file_name=>"$basedir$t->{'name'}", %base_key)},
2892                              "tree");
2893                if (defined $hash_base) {
2894                        print " | " .
2895                              $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2896                                                     file_name=>"$basedir$t->{'name'}")},
2897                                      "history");
2898                }
2899                print "</td>\n";
2900        } else {
2901                # unknown object: we can only present history for it
2902                # (this includes 'commit' object, i.e. submodule support)
2903                print "<td class=\"list\">" .
2904                      esc_path($t->{'name'}) .
2905                      "</td>\n";
2906                print "<td class=\"link\">";
2907                if (defined $hash_base) {
2908                        print $cgi->a({-href => href(action=>"history",
2909                                                     hash_base=>$hash_base,
2910                                                     file_name=>"$basedir$t->{'name'}")},
2911                                      "history");
2912                }
2913                print "</td>\n";
2914        }
2915}
2916
2917## ......................................................................
2918## functions printing large fragments of HTML
2919
2920# get pre-image filenames for merge (combined) diff
2921sub fill_from_file_info {
2922        my ($diff, @parents) = @_;
2923
2924        $diff->{'from_file'} = [ ];
2925        $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
2926        for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
2927                if ($diff->{'status'}[$i] eq 'R' ||
2928                    $diff->{'status'}[$i] eq 'C') {
2929                        $diff->{'from_file'}[$i] =
2930                                git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
2931                }
2932        }
2933
2934        return $diff;
2935}
2936
2937# is current raw difftree line of file deletion
2938sub is_deleted {
2939        my $diffinfo = shift;
2940
2941        return $diffinfo->{'status_str'} =~ /D/;
2942}
2943
2944# does patch correspond to [previous] difftree raw line
2945# $diffinfo  - hashref of parsed raw diff format
2946# $patchinfo - hashref of parsed patch diff format
2947#              (the same keys as in $diffinfo)
2948sub is_patch_split {
2949        my ($diffinfo, $patchinfo) = @_;
2950
2951        return defined $diffinfo && defined $patchinfo
2952                && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
2953}
2954
2955
2956sub git_difftree_body {
2957        my ($difftree, $hash, @parents) = @_;
2958        my ($parent) = $parents[0];
2959        my ($have_blame) = gitweb_check_feature('blame');
2960        print "<div class=\"list_head\">\n";
2961        if ($#{$difftree} > 10) {
2962                print(($#{$difftree} + 1) . " files changed:\n");
2963        }
2964        print "</div>\n";
2965
2966        print "<table class=\"" .
2967              (@parents > 1 ? "combined " : "") .
2968              "diff_tree\">\n";
2969
2970        # header only for combined diff in 'commitdiff' view
2971        my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
2972        if ($has_header) {
2973                # table header
2974                print "<thead><tr>\n" .
2975                       "<th></th><th></th>\n"; # filename, patchN link
2976                for (my $i = 0; $i < @parents; $i++) {
2977                        my $par = $parents[$i];
2978                        print "<th>" .
2979                              $cgi->a({-href => href(action=>"commitdiff",
2980                                                     hash=>$hash, hash_parent=>$par),
2981                                       -title => 'commitdiff to parent number ' .
2982                                                  ($i+1) . ': ' . substr($par,0,7)},
2983                                      $i+1) .
2984                              "&nbsp;</th>\n";
2985                }
2986                print "</tr></thead>\n<tbody>\n";
2987        }
2988
2989        my $alternate = 1;
2990        my $patchno = 0;
2991        foreach my $line (@{$difftree}) {
2992                my $diff = parsed_difftree_line($line);
2993
2994                if ($alternate) {
2995                        print "<tr class=\"dark\">\n";
2996                } else {
2997                        print "<tr class=\"light\">\n";
2998                }
2999                $alternate ^= 1;
3000
3001                if (exists $diff->{'nparents'}) { # combined diff
3002
3003                        fill_from_file_info($diff, @parents)
3004                                unless exists $diff->{'from_file'};
3005
3006                        if (!is_deleted($diff)) {
3007                                # file exists in the result (child) commit
3008                                print "<td>" .
3009                                      $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3010                                                             file_name=>$diff->{'to_file'},
3011                                                             hash_base=>$hash),
3012                                              -class => "list"}, esc_path($diff->{'to_file'})) .
3013                                      "</td>\n";
3014                        } else {
3015                                print "<td>" .
3016                                      esc_path($diff->{'to_file'}) .
3017                                      "</td>\n";
3018                        }
3019
3020                        if ($action eq 'commitdiff') {
3021                                # link to patch
3022                                $patchno++;
3023                                print "<td class=\"link\">" .
3024                                      $cgi->a({-href => "#patch$patchno"}, "patch") .
3025                                      " | " .
3026                                      "</td>\n";
3027                        }
3028
3029                        my $has_history = 0;
3030                        my $not_deleted = 0;
3031                        for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
3032                                my $hash_parent = $parents[$i];
3033                                my $from_hash = $diff->{'from_id'}[$i];
3034                                my $from_path = $diff->{'from_file'}[$i];
3035                                my $status = $diff->{'status'}[$i];
3036
3037                                $has_history ||= ($status ne 'A');
3038                                $not_deleted ||= ($status ne 'D');
3039
3040                                if ($status eq 'A') {
3041                                        print "<td  class=\"link\" align=\"right\"> | </td>\n";
3042                                } elsif ($status eq 'D') {
3043                                        print "<td class=\"link\">" .
3044                                              $cgi->a({-href => href(action=>"blob",
3045                                                                     hash_base=>$hash,
3046                                                                     hash=>$from_hash,
3047                                                                     file_name=>$from_path)},
3048                                                      "blob" . ($i+1)) .
3049                                              " | </td>\n";
3050                                } else {
3051                                        if ($diff->{'to_id'} eq $from_hash) {
3052                                                print "<td class=\"link nochange\">";
3053                                        } else {
3054                                                print "<td class=\"link\">";
3055                                        }
3056                                        print $cgi->a({-href => href(action=>"blobdiff",
3057                                                                     hash=>$diff->{'to_id'},
3058                                                                     hash_parent=>$from_hash,
3059                                                                     hash_base=>$hash,
3060                                                                     hash_parent_base=>$hash_parent,
3061                                                                     file_name=>$diff->{'to_file'},
3062                                                                     file_parent=>$from_path)},
3063                                                      "diff" . ($i+1)) .
3064                                              " | </td>\n";
3065                                }
3066                        }
3067
3068                        print "<td class=\"link\">";
3069                        if ($not_deleted) {
3070                                print $cgi->a({-href => href(action=>"blob",
3071                                                             hash=>$diff->{'to_id'},
3072                                                             file_name=>$diff->{'to_file'},
3073                                                             hash_base=>$hash)},
3074                                              "blob");
3075                                print " | " if ($has_history);
3076                        }
3077                        if ($has_history) {
3078                                print $cgi->a({-href => href(action=>"history",
3079                                                             file_name=>$diff->{'to_file'},
3080                                                             hash_base=>$hash)},
3081                                              "history");
3082                        }
3083                        print "</td>\n";
3084
3085                        print "</tr>\n";
3086                        next; # instead of 'else' clause, to avoid extra indent
3087                }
3088                # else ordinary diff
3089
3090                my ($to_mode_oct, $to_mode_str, $to_file_type);
3091                my ($from_mode_oct, $from_mode_str, $from_file_type);
3092                if ($diff->{'to_mode'} ne ('0' x 6)) {
3093                        $to_mode_oct = oct $diff->{'to_mode'};
3094                        if (S_ISREG($to_mode_oct)) { # only for regular file
3095                                $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
3096                        }
3097                        $to_file_type = file_type($diff->{'to_mode'});
3098                }
3099                if ($diff->{'from_mode'} ne ('0' x 6)) {
3100                        $from_mode_oct = oct $diff->{'from_mode'};
3101                        if (S_ISREG($to_mode_oct)) { # only for regular file
3102                                $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
3103                        }
3104                        $from_file_type = file_type($diff->{'from_mode'});
3105                }
3106
3107                if ($diff->{'status'} eq "A") { # created
3108                        my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
3109                        $mode_chng   .= " with mode: $to_mode_str" if $to_mode_str;
3110                        $mode_chng   .= "]</span>";
3111                        print "<td>";
3112                        print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3113                                                     hash_base=>$hash, file_name=>$diff->{'file'}),
3114                                      -class => "list"}, esc_path($diff->{'file'}));
3115                        print "</td>\n";
3116                        print "<td>$mode_chng</td>\n";
3117                        print "<td class=\"link\">";
3118                        if ($action eq 'commitdiff') {
3119                                # link to patch
3120                                $patchno++;
3121                                print $cgi->a({-href => "#patch$patchno"}, "patch");
3122                                print " | ";
3123                        }
3124                        print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3125                                                     hash_base=>$hash, file_name=>$diff->{'file'})},
3126                                      "blob");
3127                        print "</td>\n";
3128
3129                } elsif ($diff->{'status'} eq "D") { # deleted
3130                        my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
3131                        print "<td>";
3132                        print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3133                                                     hash_base=>$parent, file_name=>$diff->{'file'}),
3134                                       -class => "list"}, esc_path($diff->{'file'}));
3135                        print "</td>\n";
3136                        print "<td>$mode_chng</td>\n";
3137                        print "<td class=\"link\">";
3138                        if ($action eq 'commitdiff') {
3139                                # link to patch
3140                                $patchno++;
3141                                print $cgi->a({-href => "#patch$patchno"}, "patch");
3142                                print " | ";
3143                        }
3144                        print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3145                                                     hash_base=>$parent, file_name=>$diff->{'file'})},
3146                                      "blob") . " | ";
3147                        if ($have_blame) {
3148                                print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
3149                                                             file_name=>$diff->{'file'})},
3150                                              "blame") . " | ";
3151                        }
3152                        print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
3153                                                     file_name=>$diff->{'file'})},
3154                                      "history");
3155                        print "</td>\n";
3156
3157                } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
3158                        my $mode_chnge = "";
3159                        if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3160                                $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
3161                                if ($from_file_type ne $to_file_type) {
3162                                        $mode_chnge .= " from $from_file_type to $to_file_type";
3163                                }
3164                                if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
3165                                        if ($from_mode_str && $to_mode_str) {
3166                                                $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
3167                                        } elsif ($to_mode_str) {
3168                                                $mode_chnge .= " mode: $to_mode_str";
3169                                        }
3170                                }
3171                                $mode_chnge .= "]</span>\n";
3172                        }
3173                        print "<td>";
3174                        print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3175                                                     hash_base=>$hash, file_name=>$diff->{'file'}),
3176                                      -class => "list"}, esc_path($diff->{'file'}));
3177                        print "</td>\n";
3178                        print "<td>$mode_chnge</td>\n";
3179                        print "<td class=\"link\">";
3180                        if ($action eq 'commitdiff') {
3181                                # link to patch
3182                                $patchno++;
3183                                print $cgi->a({-href => "#patch$patchno"}, "patch") .
3184                                      " | ";
3185                        } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
3186                                # "commit" view and modified file (not onlu mode changed)
3187                                print $cgi->a({-href => href(action=>"blobdiff",
3188                                                             hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
3189                                                             hash_base=>$hash, hash_parent_base=>$parent,
3190                                                             file_name=>$diff->{'file'})},
3191                                              "diff") .
3192                                      " | ";
3193                        }
3194                        print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3195                                                     hash_base=>$hash, file_name=>$diff->{'file'})},
3196                                       "blob") . " | ";
3197                        if ($have_blame) {
3198                                print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
3199                                                             file_name=>$diff->{'file'})},
3200                                              "blame") . " | ";
3201                        }
3202                        print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
3203                                                     file_name=>$diff->{'file'})},
3204                                      "history");
3205                        print "</td>\n";
3206
3207                } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
3208                        my %status_name = ('R' => 'moved', 'C' => 'copied');
3209                        my $nstatus = $status_name{$diff->{'status'}};
3210                        my $mode_chng = "";
3211                        if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3212                                # mode also for directories, so we cannot use $to_mode_str
3213                                $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
3214                        }
3215                        print "<td>" .
3216                              $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
3217                                                     hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
3218                                      -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
3219                              "<td><span class=\"file_status $nstatus\">[$nstatus from " .
3220                              $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
3221                                                     hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
3222                                      -class => "list"}, esc_path($diff->{'from_file'})) .
3223                              " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
3224                              "<td class=\"link\">";
3225                        if ($action eq 'commitdiff') {
3226                                # link to patch
3227                                $patchno++;
3228                                print $cgi->a({-href => "#patch$patchno"}, "patch") .
3229                                      " | ";
3230                        } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
3231                                # "commit" view and modified file (not only pure rename or copy)
3232                                print $cgi->a({-href => href(action=>"blobdiff",
3233                                                             hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
3234                                                             hash_base=>$hash, hash_parent_base=>$parent,
3235                                                             file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
3236                                              "diff") .
3237                                      " | ";
3238                        }
3239                        print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3240                                                     hash_base=>$parent, file_name=>$diff->{'to_file'})},
3241                                      "blob") . " | ";
3242                        if ($have_blame) {
3243                                print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
3244                                                             file_name=>$diff->{'to_file'})},
3245                                              "blame") . " | ";
3246                        }
3247                        print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
3248                                                    file_name=>$diff->{'to_file'})},
3249                                      "history");
3250                        print "</td>\n";
3251
3252                } # we should not encounter Unmerged (U) or Unknown (X) status
3253                print "</tr>\n";
3254        }
3255        print "</tbody>" if $has_header;
3256        print "</table>\n";
3257}
3258
3259sub git_patchset_body {
3260        my ($fd, $difftree, $hash, @hash_parents) = @_;
3261        my ($hash_parent) = $hash_parents[0];
3262
3263        my $is_combined = (@hash_parents > 1);
3264        my $patch_idx = 0;
3265        my $patch_number = 0;
3266        my $patch_line;
3267        my $diffinfo;
3268        my $to_name;
3269        my (%from, %to);
3270
3271        print "<div class=\"patchset\">\n";
3272
3273        # skip to first patch
3274        while ($patch_line = <$fd>) {
3275                chomp $patch_line;
3276
3277                last if ($patch_line =~ m/^diff /);
3278        }
3279
3280 PATCH:
3281        while ($patch_line) {
3282
3283                # parse "git diff" header line
3284                if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
3285                        # $1 is from_name, which we do not use
3286                        $to_name = unquote($2);
3287                        $to_name =~ s!^b/!!;
3288                } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
3289                        # $1 is 'cc' or 'combined', which we do not use
3290                        $to_name = unquote($2);
3291                } else {
3292                        $to_name = undef;
3293                }
3294
3295                # check if current patch belong to current raw line
3296                # and parse raw git-diff line if needed
3297                if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
3298                        # this is continuation of a split patch
3299                        print "<div class=\"patch cont\">\n";
3300                } else {
3301                        # advance raw git-diff output if needed
3302                        $patch_idx++ if defined $diffinfo;
3303
3304                        # read and prepare patch information
3305                        $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3306
3307                        # compact combined diff output can have some patches skipped
3308                        # find which patch (using pathname of result) we are at now;
3309                        if ($is_combined) {
3310                                while ($to_name ne $diffinfo->{'to_file'}) {
3311                                        print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3312                                              format_diff_cc_simplified($diffinfo, @hash_parents) .
3313                                              "</div>\n";  # class="patch"
3314
3315                                        $patch_idx++;
3316                                        $patch_number++;
3317
3318                                        last if $patch_idx > $#$difftree;
3319                                        $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3320                                }
3321                        }
3322
3323                        # modifies %from, %to hashes
3324                        parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
3325
3326                        # this is first patch for raw difftree line with $patch_idx index
3327                        # we index @$difftree array from 0, but number patches from 1
3328                        print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
3329                }
3330
3331                # git diff header
3332                #assert($patch_line =~ m/^diff /) if DEBUG;
3333                #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
3334                $patch_number++;
3335                # print "git diff" header
3336                print format_git_diff_header_line($patch_line, $diffinfo,
3337                                                  \%from, \%to);
3338
3339                # print extended diff header
3340                print "<div class=\"diff extended_header\">\n";
3341        EXTENDED_HEADER:
3342                while ($patch_line = <$fd>) {
3343                        chomp $patch_line;
3344
3345                        last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
3346
3347                        print format_extended_diff_header_line($patch_line, $diffinfo,
3348                                                               \%from, \%to);
3349                }
3350                print "</div>\n"; # class="diff extended_header"
3351
3352                # from-file/to-file diff header
3353                if (! $patch_line) {
3354                        print "</div>\n"; # class="patch"
3355                        last PATCH;
3356                }
3357                next PATCH if ($patch_line =~ m/^diff /);
3358                #assert($patch_line =~ m/^---/) if DEBUG;
3359
3360                my $last_patch_line = $patch_line;
3361                $patch_line = <$fd>;
3362                chomp $patch_line;
3363                #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
3364
3365                print format_diff_from_to_header($last_patch_line, $patch_line,
3366                                                 $diffinfo, \%from, \%to,
3367                                                 @hash_parents);
3368
3369                # the patch itself
3370        LINE:
3371                while ($patch_line = <$fd>) {
3372                        chomp $patch_line;
3373
3374                        next PATCH if ($patch_line =~ m/^diff /);
3375
3376                        print format_diff_line($patch_line, \%from, \%to);
3377                }
3378
3379        } continue {
3380                print "</div>\n"; # class="patch"
3381        }
3382
3383        # for compact combined (--cc) format, with chunk and patch simpliciaction
3384        # patchset might be empty, but there might be unprocessed raw lines
3385        for (++$patch_idx if $patch_number > 0;
3386             $patch_idx < @$difftree;
3387             ++$patch_idx) {
3388                # read and prepare patch information
3389                $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3390
3391                # generate anchor for "patch" links in difftree / whatchanged part
3392                print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3393                      format_diff_cc_simplified($diffinfo, @hash_parents) .
3394                      "</div>\n";  # class="patch"
3395
3396                $patch_number++;
3397        }
3398
3399        if ($patch_number == 0) {
3400                if (@hash_parents > 1) {
3401                        print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
3402                } else {
3403                        print "<div class=\"diff nodifferences\">No differences found</div>\n";
3404                }
3405        }
3406
3407        print "</div>\n"; # class="patchset"
3408}
3409
3410# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3411
3412sub git_project_list_body {
3413        my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
3414
3415        my ($check_forks) = gitweb_check_feature('forks');
3416
3417        my @projects;
3418        foreach my $pr (@$projlist) {
3419                my (@aa) = git_get_last_activity($pr->{'path'});
3420                unless (@aa) {
3421                        next;
3422                }
3423                ($pr->{'age'}, $pr->{'age_string'}) = @aa;
3424                if (!defined $pr->{'descr'}) {
3425                        my $descr = git_get_project_description($pr->{'path'}) || "";
3426                        $pr->{'descr_long'} = to_utf8($descr);
3427                        $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
3428                }
3429                if (!defined $pr->{'owner'}) {
3430                        $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
3431                }
3432                if ($check_forks) {
3433                        my $pname = $pr->{'path'};
3434                        if (($pname =~ s/\.git$//) &&
3435                            ($pname !~ /\/$/) &&
3436                            (-d "$projectroot/$pname")) {
3437                                $pr->{'forks'} = "-d $projectroot/$pname";
3438                        }
3439                        else {
3440                                $pr->{'forks'} = 0;
3441                        }
3442                }
3443                push @projects, $pr;
3444        }
3445
3446        $order ||= $default_projects_order;
3447        $from = 0 unless defined $from;
3448        $to = $#projects if (!defined $to || $#projects < $to);
3449
3450        print "<table class=\"project_list\">\n";
3451        unless ($no_header) {
3452                print "<tr>\n";
3453                if ($check_forks) {
3454                        print "<th></th>\n";
3455                }
3456                if ($order eq "project") {
3457                        @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
3458                        print "<th>Project</th>\n";
3459                } else {
3460                        print "<th>" .
3461                              $cgi->a({-href => href(project=>undef, order=>'project'),
3462                                       -class => "header"}, "Project") .
3463                              "</th>\n";
3464                }
3465                if ($order eq "descr") {
3466                        @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
3467                        print "<th>Description</th>\n";
3468                } else {
3469                        print "<th>" .
3470                              $cgi->a({-href => href(project=>undef, order=>'descr'),
3471                                       -class => "header"}, "Description") .
3472                              "</th>\n";
3473                }
3474                if ($order eq "owner") {
3475                        @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
3476                        print "<th>Owner</th>\n";
3477                } else {
3478                        print "<th>" .
3479                              $cgi->a({-href => href(project=>undef, order=>'owner'),
3480                                       -class => "header"}, "Owner") .
3481                              "</th>\n";
3482                }
3483                if ($order eq "age") {
3484                        @projects = sort {$a->{'age'} <=> $b->{'age'}} @projects;
3485                        print "<th>Last Change</th>\n";
3486                } else {
3487                        print "<th>" .
3488                              $cgi->a({-href => href(project=>undef, order=>'age'),
3489                                       -class => "header"}, "Last Change") .
3490                              "</th>\n";
3491                }
3492                print "<th></th>\n" .
3493                      "</tr>\n";
3494        }
3495        my $alternate = 1;
3496        for (my $i = $from; $i <= $to; $i++) {
3497                my $pr = $projects[$i];
3498                if ($alternate) {
3499                        print "<tr class=\"dark\">\n";
3500                } else {
3501                        print "<tr class=\"light\">\n";
3502                }
3503                $alternate ^= 1;
3504                if ($check_forks) {
3505                        print "<td>";
3506                        if ($pr->{'forks'}) {
3507                                print "<!-- $pr->{'forks'} -->\n";
3508                                print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
3509                        }
3510                        print "</td>\n";
3511                }
3512                print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3513                                        -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
3514                      "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3515                                        -class => "list", -title => $pr->{'descr_long'}},
3516                                        esc_html($pr->{'descr'})) . "</td>\n" .
3517                      "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n";
3518                print "<td class=\"". age_class($pr->{'age'}) . "\">" .
3519                      (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
3520                      "<td class=\"link\">" .
3521                      $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary")   . " | " .
3522                      $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
3523                      $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
3524                      $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
3525                      ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
3526                      "</td>\n" .
3527                      "</tr>\n";
3528        }
3529        if (defined $extra) {
3530                print "<tr>\n";
3531                if ($check_forks) {
3532                        print "<td></td>\n";
3533                }
3534                print "<td colspan=\"5\">$extra</td>\n" .
3535                      "</tr>\n";
3536        }
3537        print "</table>\n";
3538}
3539
3540sub git_shortlog_body {
3541        # uses global variable $project
3542        my ($commitlist, $from, $to, $refs, $extra) = @_;
3543
3544        $from = 0 unless defined $from;
3545        $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3546
3547        print "<table class=\"shortlog\">\n";
3548        my $alternate = 1;
3549        for (my $i = $from; $i <= $to; $i++) {
3550                my %co = %{$commitlist->[$i]};
3551                my $commit = $co{'id'};
3552                my $ref = format_ref_marker($refs, $commit);
3553                if ($alternate) {
3554                        print "<tr class=\"dark\">\n";
3555                } else {
3556                        print "<tr class=\"light\">\n";
3557                }
3558                $alternate ^= 1;
3559                my $author = chop_and_escape_str($co{'author_name'}, 10);
3560                # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
3561                print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3562                      "<td><i>" . $author . "</i></td>\n" .
3563                      "<td>";
3564                print format_subject_html($co{'title'}, $co{'title_short'},
3565                                          href(action=>"commit", hash=>$commit), $ref);
3566                print "</td>\n" .
3567                      "<td class=\"link\">" .
3568                      $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
3569                      $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
3570                      $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
3571                my $snapshot_links = format_snapshot_links($commit);
3572                if (defined $snapshot_links) {
3573                        print " | " . $snapshot_links;
3574                }
3575                print "</td>\n" .
3576                      "</tr>\n";
3577        }
3578        if (defined $extra) {
3579                print "<tr>\n" .
3580                      "<td colspan=\"4\">$extra</td>\n" .
3581                      "</tr>\n";
3582        }
3583        print "</table>\n";
3584}
3585
3586sub git_history_body {
3587        # Warning: assumes constant type (blob or tree) during history
3588        my ($commitlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
3589
3590        $from = 0 unless defined $from;
3591        $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
3592
3593        print "<table class=\"history\">\n";
3594        my $alternate = 1;
3595        for (my $i = $from; $i <= $to; $i++) {
3596                my %co = %{$commitlist->[$i]};
3597                if (!%co) {
3598                        next;
3599                }
3600                my $commit = $co{'id'};
3601
3602                my $ref = format_ref_marker($refs, $commit);
3603
3604                if ($alternate) {
3605                        print "<tr class=\"dark\">\n";
3606                } else {
3607                        print "<tr class=\"light\">\n";
3608                }
3609                $alternate ^= 1;
3610        # shortlog uses      chop_str($co{'author_name'}, 10)
3611                my $author = chop_and_escape_str($co{'author_name'}, 15, 3);
3612                print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3613                      "<td><i>" . $author . "</i></td>\n" .
3614                      "<td>";
3615                # originally git_history used chop_str($co{'title'}, 50)
3616                print format_subject_html($co{'title'}, $co{'title_short'},
3617                                          href(action=>"commit", hash=>$commit), $ref);
3618                print "</td>\n" .
3619                      "<td class=\"link\">" .
3620                      $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
3621                      $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
3622
3623                if ($ftype eq 'blob') {
3624                        my $blob_current = git_get_hash_by_path($hash_base, $file_name);
3625                        my $blob_parent  = git_get_hash_by_path($commit, $file_name);
3626                        if (defined $blob_current && defined $blob_parent &&
3627                                        $blob_current ne $blob_parent) {
3628                                print " | " .
3629                                        $cgi->a({-href => href(action=>"blobdiff",
3630                                                               hash=>$blob_current, hash_parent=>$blob_parent,
3631                                                               hash_base=>$hash_base, hash_parent_base=>$commit,
3632                                                               file_name=>$file_name)},
3633                                                "diff to current");
3634                        }
3635                }
3636                print "</td>\n" .
3637                      "</tr>\n";
3638        }
3639        if (defined $extra) {
3640                print "<tr>\n" .
3641                      "<td colspan=\"4\">$extra</td>\n" .
3642                      "</tr>\n";
3643        }
3644        print "</table>\n";
3645}
3646
3647sub git_tags_body {
3648        # uses global variable $project
3649        my ($taglist, $from, $to, $extra) = @_;
3650        $from = 0 unless defined $from;
3651        $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
3652
3653        print "<table class=\"tags\">\n";
3654        my $alternate = 1;
3655        for (my $i = $from; $i <= $to; $i++) {
3656                my $entry = $taglist->[$i];
3657                my %tag = %$entry;
3658                my $comment = $tag{'subject'};
3659                my $comment_short;
3660                if (defined $comment) {
3661                        $comment_short = chop_str($comment, 30, 5);
3662                }
3663                if ($alternate) {
3664                        print "<tr class=\"dark\">\n";
3665                } else {
3666                        print "<tr class=\"light\">\n";
3667                }
3668                $alternate ^= 1;
3669                if (defined $tag{'age'}) {
3670                        print "<td><i>$tag{'age'}</i></td>\n";
3671                } else {
3672                        print "<td></td>\n";
3673                }
3674                print "<td>" .
3675                      $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
3676                               -class => "list name"}, esc_html($tag{'name'})) .
3677                      "</td>\n" .
3678                      "<td>";
3679                if (defined $comment) {
3680                        print format_subject_html($comment, $comment_short,
3681                                                  href(action=>"tag", hash=>$tag{'id'}));
3682                }
3683                print "</td>\n" .
3684                      "<td class=\"selflink\">";
3685                if ($tag{'type'} eq "tag") {
3686                        print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
3687                } else {
3688                        print "&nbsp;";
3689                }
3690                print "</td>\n" .
3691                      "<td class=\"link\">" . " | " .
3692                      $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
3693                if ($tag{'reftype'} eq "commit") {
3694                        print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
3695                              " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log");
3696                } elsif ($tag{'reftype'} eq "blob") {
3697                        print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
3698                }
3699                print "</td>\n" .
3700                      "</tr>";
3701        }
3702        if (defined $extra) {
3703                print "<tr>\n" .
3704                      "<td colspan=\"5\">$extra</td>\n" .
3705                      "</tr>\n";
3706        }
3707        print "</table>\n";
3708}
3709
3710sub git_heads_body {
3711        # uses global variable $project
3712        my ($headlist, $head, $from, $to, $extra) = @_;
3713        $from = 0 unless defined $from;
3714        $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
3715
3716        print "<table class=\"heads\">\n";
3717        my $alternate = 1;
3718        for (my $i = $from; $i <= $to; $i++) {
3719                my $entry = $headlist->[$i];
3720                my %ref = %$entry;
3721                my $curr = $ref{'id'} eq $head;
3722                if ($alternate) {
3723                        print "<tr class=\"dark\">\n";
3724                } else {
3725                        print "<tr class=\"light\">\n";
3726                }
3727                $alternate ^= 1;
3728                print "<td><i>$ref{'age'}</i></td>\n" .
3729                      ($curr ? "<td class=\"current_head\">" : "<td>") .
3730                      $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'}),
3731                               -class => "list name"},esc_html($ref{'name'})) .
3732                      "</td>\n" .
3733                      "<td class=\"link\">" .
3734                      $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'})}, "shortlog") . " | " .
3735                      $cgi->a({-href => href(action=>"log", hash=>$ref{'name'})}, "log") . " | " .
3736                      $cgi->a({-href => href(action=>"tree", hash=>$ref{'name'}, hash_base=>$ref{'name'})}, "tree") .
3737                      "</td>\n" .
3738                      "</tr>";
3739        }
3740        if (defined $extra) {
3741                print "<tr>\n" .
3742                      "<td colspan=\"3\">$extra</td>\n" .
3743                      "</tr>\n";
3744        }
3745        print "</table>\n";
3746}
3747
3748sub git_search_grep_body {
3749        my ($commitlist, $from, $to, $extra) = @_;
3750        $from = 0 unless defined $from;
3751        $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3752
3753        print "<table class=\"commit_search\">\n";
3754        my $alternate = 1;
3755        for (my $i = $from; $i <= $to; $i++) {
3756                my %co = %{$commitlist->[$i]};
3757                if (!%co) {
3758                        next;
3759                }
3760                my $commit = $co{'id'};
3761                if ($alternate) {
3762                        print "<tr class=\"dark\">\n";
3763                } else {
3764                        print "<tr class=\"light\">\n";
3765                }
3766                $alternate ^= 1;
3767                my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
3768                print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3769                      "<td><i>" . $author . "</i></td>\n" .
3770                      "<td>" .
3771                      $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
3772                               chop_and_escape_str($co{'title'}, 50) . "<br/>");
3773                my $comment = $co{'comment'};
3774                foreach my $line (@$comment) {
3775                        if ($line =~ m/^(.*)($search_regexp)(.*)$/i) {
3776                                my $lead = esc_html($1) || "";
3777                                $lead = chop_str($lead, 30, 10);
3778                                my $match = esc_html($2) || "";
3779                                my $trail = esc_html($3) || "";
3780                                $trail = chop_str($trail, 30, 10);
3781                                my $text = "$lead<span class=\"match\">$match</span>$trail";
3782                                print chop_str($text, 80, 5) . "<br/>\n";
3783                        }
3784                }
3785                print "</td>\n" .
3786                      "<td class=\"link\">" .
3787                      $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3788                      " | " .
3789                      $cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})}, "commitdiff") .
3790                      " | " .
3791                      $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3792                print "</td>\n" .
3793                      "</tr>\n";
3794        }
3795        if (defined $extra) {
3796                print "<tr>\n" .
3797                      "<td colspan=\"3\">$extra</td>\n" .
3798                      "</tr>\n";
3799        }
3800        print "</table>\n";
3801}
3802
3803## ======================================================================
3804## ======================================================================
3805## actions
3806
3807sub git_project_list {
3808        my $order = $cgi->param('o');
3809        if (defined $order && $order !~ m/none|project|descr|owner|age/) {
3810                die_error(undef, "Unknown order parameter");
3811        }
3812
3813        my @list = git_get_projects_list();
3814        if (!@list) {
3815                die_error(undef, "No projects found");
3816        }
3817
3818        git_header_html();
3819        if (-f $home_text) {
3820                print "<div class=\"index_include\">\n";
3821                open (my $fd, $home_text);
3822                print <$fd>;
3823                close $fd;
3824                print "</div>\n";
3825        }
3826        git_project_list_body(\@list, $order);
3827        git_footer_html();
3828}
3829
3830sub git_forks {
3831        my $order = $cgi->param('o');
3832        if (defined $order && $order !~ m/none|project|descr|owner|age/) {
3833                die_error(undef, "Unknown order parameter");
3834        }
3835
3836        my @list = git_get_projects_list($project);
3837        if (!@list) {
3838                die_error(undef, "No forks found");
3839        }
3840
3841        git_header_html();
3842        git_print_page_nav('','');
3843        git_print_header_div('summary', "$project forks");
3844        git_project_list_body(\@list, $order);
3845        git_footer_html();
3846}
3847
3848sub git_project_index {
3849        my @projects = git_get_projects_list($project);
3850
3851        print $cgi->header(
3852                -type => 'text/plain',
3853                -charset => 'utf-8',
3854                -content_disposition => 'inline; filename="index.aux"');
3855
3856        foreach my $pr (@projects) {
3857                if (!exists $pr->{'owner'}) {
3858                        $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
3859                }
3860
3861                my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
3862                # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
3863                $path  =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
3864                $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
3865                $path  =~ s/ /\+/g;
3866                $owner =~ s/ /\+/g;
3867
3868                print "$path $owner\n";
3869        }
3870}
3871
3872sub git_summary {
3873        my $descr = git_get_project_description($project) || "none";
3874        my %co = parse_commit("HEAD");
3875        my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
3876        my $head = $co{'id'};
3877
3878        my $owner = git_get_project_owner($project);
3879
3880        my $refs = git_get_references();
3881        # These get_*_list functions return one more to allow us to see if
3882        # there are more ...
3883        my @taglist  = git_get_tags_list(16);
3884        my @headlist = git_get_heads_list(16);
3885        my @forklist;
3886        my ($check_forks) = gitweb_check_feature('forks');
3887
3888        if ($check_forks) {
3889                @forklist = git_get_projects_list($project);
3890        }
3891
3892        git_header_html();
3893        git_print_page_nav('summary','', $head);
3894
3895        print "<div class=\"title\">&nbsp;</div>\n";
3896        print "<table class=\"projects_list\">\n" .
3897              "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
3898              "<tr><td>owner</td><td>" . esc_html($owner) . "</td></tr>\n";
3899        if (defined $cd{'rfc2822'}) {
3900                print "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
3901        }
3902
3903        # use per project git URL list in $projectroot/$project/cloneurl
3904        # or make project git URL from git base URL and project name
3905        my $url_tag = "URL";
3906        my @url_list = git_get_project_url_list($project);
3907        @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
3908        foreach my $git_url (@url_list) {
3909                next unless $git_url;
3910                print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
3911                $url_tag = "";
3912        }
3913        print "</table>\n";
3914
3915        if (-s "$projectroot/$project/README.html") {
3916                if (open my $fd, "$projectroot/$project/README.html") {
3917                        print "<div class=\"title\">readme</div>\n" .
3918                              "<div class=\"readme\">\n";
3919                        print $_ while (<$fd>);
3920                        print "\n</div>\n"; # class="readme"
3921                        close $fd;
3922                }
3923        }
3924
3925        # we need to request one more than 16 (0..15) to check if
3926        # those 16 are all
3927        my @commitlist = $head ? parse_commits($head, 17) : ();
3928        if (@commitlist) {
3929                git_print_header_div('shortlog');
3930                git_shortlog_body(\@commitlist, 0, 15, $refs,
3931                                  $#commitlist <=  15 ? undef :
3932                                  $cgi->a({-href => href(action=>"shortlog")}, "..."));
3933        }
3934
3935        if (@taglist) {
3936                git_print_header_div('tags');
3937                git_tags_body(\@taglist, 0, 15,
3938                              $#taglist <=  15 ? undef :
3939                              $cgi->a({-href => href(action=>"tags")}, "..."));
3940        }
3941
3942        if (@headlist) {
3943                git_print_header_div('heads');
3944                git_heads_body(\@headlist, $head, 0, 15,
3945                               $#headlist <= 15 ? undef :
3946                               $cgi->a({-href => href(action=>"heads")}, "..."));
3947        }
3948
3949        if (@forklist) {
3950                git_print_header_div('forks');
3951                git_project_list_body(\@forklist, undef, 0, 15,
3952                                      $#forklist <= 15 ? undef :
3953                                      $cgi->a({-href => href(action=>"forks")}, "..."),
3954                                      'noheader');
3955        }
3956
3957        git_footer_html();
3958}
3959
3960sub git_tag {
3961        my $head = git_get_head_hash($project);
3962        git_header_html();
3963        git_print_page_nav('','', $head,undef,$head);
3964        my %tag = parse_tag($hash);
3965
3966        if (! %tag) {
3967                die_error(undef, "Unknown tag object");
3968        }
3969
3970        git_print_header_div('commit', esc_html($tag{'name'}), $hash);
3971        print "<div class=\"title_text\">\n" .
3972              "<table class=\"object_header\">\n" .
3973              "<tr>\n" .
3974              "<td>object</td>\n" .
3975              "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
3976                               $tag{'object'}) . "</td>\n" .
3977              "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
3978                                              $tag{'type'}) . "</td>\n" .
3979              "</tr>\n";
3980        if (defined($tag{'author'})) {
3981                my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
3982                print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
3983                print "<tr><td></td><td>" . $ad{'rfc2822'} .
3984                        sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
3985                        "</td></tr>\n";
3986        }
3987        print "</table>\n\n" .
3988              "</div>\n";
3989        print "<div class=\"page_body\">";
3990        my $comment = $tag{'comment'};
3991        foreach my $line (@$comment) {
3992                chomp $line;
3993                print esc_html($line, -nbsp=>1) . "<br/>\n";
3994        }
3995        print "</div>\n";
3996        git_footer_html();
3997}
3998
3999sub git_blame2 {
4000        my $fd;
4001        my $ftype;
4002
4003        my ($have_blame) = gitweb_check_feature('blame');
4004        if (!$have_blame) {
4005                die_error('403 Permission denied', "Permission denied");
4006        }
4007        die_error('404 Not Found', "File name not defined") if (!$file_name);
4008        $hash_base ||= git_get_head_hash($project);
4009        die_error(undef, "Couldn't find base commit") unless ($hash_base);
4010        my %co = parse_commit($hash_base)
4011                or die_error(undef, "Reading commit failed");
4012        if (!defined $hash) {
4013                $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
4014                        or die_error(undef, "Error looking up file");
4015        }
4016        $ftype = git_get_type($hash);
4017        if ($ftype !~ "blob") {
4018                die_error('400 Bad Request', "Object is not a blob");
4019        }
4020        open ($fd, "-|", git_cmd(), "blame", '-p', '--',
4021              $file_name, $hash_base)
4022                or die_error(undef, "Open git-blame failed");
4023        git_header_html();
4024        my $formats_nav =
4025                $cgi->a({-href => href(action=>"blob", -replay=>1)},
4026                        "blob") .
4027                " | " .
4028                $cgi->a({-href => href(action=>"history", -replay=>1)},
4029                        "history") .
4030                " | " .
4031                $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
4032                        "HEAD");
4033        git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4034        git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4035        git_print_page_path($file_name, $ftype, $hash_base);
4036        my @rev_color = (qw(light2 dark2));
4037        my $num_colors = scalar(@rev_color);
4038        my $current_color = 0;
4039        my $last_rev;
4040        print <<HTML;
4041<div class="page_body">
4042<table class="blame">
4043<tr><th>Commit</th><th>Line</th><th>Data</th></tr>
4044HTML
4045        my %metainfo = ();
4046        while (1) {
4047                $_ = <$fd>;
4048                last unless defined $_;
4049                my ($full_rev, $orig_lineno, $lineno, $group_size) =
4050                    /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;
4051                if (!exists $metainfo{$full_rev}) {
4052                        $metainfo{$full_rev} = {};
4053                }
4054                my $meta = $metainfo{$full_rev};
4055                while (<$fd>) {
4056                        last if (s/^\t//);
4057                        if (/^(\S+) (.*)$/) {
4058                                $meta->{$1} = $2;
4059                        }
4060                }
4061                my $data = $_;
4062                chomp $data;
4063                my $rev = substr($full_rev, 0, 8);
4064                my $author = $meta->{'author'};
4065                my %date = parse_date($meta->{'author-time'},
4066                                      $meta->{'author-tz'});
4067                my $date = $date{'iso-tz'};
4068                if ($group_size) {
4069                        $current_color = ++$current_color % $num_colors;
4070                }
4071                print "<tr class=\"$rev_color[$current_color]\">\n";
4072                if ($group_size) {
4073                        print "<td class=\"sha1\"";
4074                        print " title=\"". esc_html($author) . ", $date\"";
4075                        print " rowspan=\"$group_size\"" if ($group_size > 1);
4076                        print ">";
4077                        print $cgi->a({-href => href(action=>"commit",
4078                                                     hash=>$full_rev,
4079                                                     file_name=>$file_name)},
4080                                      esc_html($rev));
4081                        print "</td>\n";
4082                }
4083                open (my $dd, "-|", git_cmd(), "rev-parse", "$full_rev^")
4084                        or die_error(undef, "Open git-rev-parse failed");
4085                my $parent_commit = <$dd>;
4086                close $dd;
4087                chomp($parent_commit);
4088                my $blamed = href(action => 'blame',
4089                                  file_name => $meta->{'filename'},
4090                                  hash_base => $parent_commit);
4091                print "<td class=\"linenr\">";
4092                print $cgi->a({ -href => "$blamed#l$orig_lineno",
4093                                -id => "l$lineno",
4094                                -class => "linenr" },
4095                              esc_html($lineno));
4096                print "</td>";
4097                print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
4098                print "</tr>\n";
4099        }
4100        print "</table>\n";
4101        print "</div>";
4102        close $fd
4103                or print "Reading blob failed\n";
4104        git_footer_html();
4105}
4106
4107sub git_blame {
4108        my $fd;
4109
4110        my ($have_blame) = gitweb_check_feature('blame');
4111        if (!$have_blame) {
4112                die_error('403 Permission denied', "Permission denied");
4113        }
4114        die_error('404 Not Found', "File name not defined") if (!$file_name);
4115        $hash_base ||= git_get_head_hash($project);
4116        die_error(undef, "Couldn't find base commit") unless ($hash_base);
4117        my %co = parse_commit($hash_base)
4118                or die_error(undef, "Reading commit failed");
4119        if (!defined $hash) {
4120                $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
4121                        or die_error(undef, "Error lookup file");
4122        }
4123        open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
4124                or die_error(undef, "Open git-annotate failed");
4125        git_header_html();
4126        my $formats_nav =
4127                $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
4128                        "blob") .
4129                " | " .
4130                $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
4131                        "history") .
4132                " | " .
4133                $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
4134                        "HEAD");
4135        git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4136        git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4137        git_print_page_path($file_name, 'blob', $hash_base);
4138        print "<div class=\"page_body\">\n";
4139        print <<HTML;
4140<table class="blame">
4141  <tr>
4142    <th>Commit</th>
4143    <th>Age</th>
4144    <th>Author</th>
4145    <th>Line</th>
4146    <th>Data</th>
4147  </tr>
4148HTML
4149        my @line_class = (qw(light dark));
4150        my $line_class_len = scalar (@line_class);
4151        my $line_class_num = $#line_class;
4152        while (my $line = <$fd>) {
4153                my $long_rev;
4154                my $short_rev;
4155                my $author;
4156                my $time;
4157                my $lineno;
4158                my $data;
4159                my $age;
4160                my $age_str;
4161                my $age_class;
4162
4163                chomp $line;
4164                $line_class_num = ($line_class_num + 1) % $line_class_len;
4165
4166                if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
4167                        $long_rev = $1;
4168                        $author   = $2;
4169                        $time     = $3;
4170                        $lineno   = $4;
4171                        $data     = $5;
4172                } else {
4173                        print qq(  <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
4174                        next;
4175                }
4176                $short_rev  = substr ($long_rev, 0, 8);
4177                $age        = time () - $time;
4178                $age_str    = age_string ($age);
4179                $age_str    =~ s/ /&nbsp;/g;
4180                $age_class  = age_class($age);
4181                $author     = esc_html ($author);
4182                $author     =~ s/ /&nbsp;/g;
4183
4184                $data = untabify($data);
4185                $data = esc_html ($data);
4186
4187                print <<HTML;
4188  <tr class="$line_class[$line_class_num]">
4189    <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
4190    <td class="$age_class">$age_str</td>
4191    <td>$author</td>
4192    <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
4193    <td class="pre">$data</td>
4194  </tr>
4195HTML
4196        } # while (my $line = <$fd>)
4197        print "</table>\n\n";
4198        close $fd
4199                or print "Reading blob failed.\n";
4200        print "</div>";
4201        git_footer_html();
4202}
4203
4204sub git_tags {
4205        my $head = git_get_head_hash($project);
4206        git_header_html();
4207        git_print_page_nav('','', $head,undef,$head);
4208        git_print_header_div('summary', $project);
4209
4210        my @tagslist = git_get_tags_list();
4211        if (@tagslist) {
4212                git_tags_body(\@tagslist);
4213        }
4214        git_footer_html();
4215}
4216
4217sub git_heads {
4218        my $head = git_get_head_hash($project);
4219        git_header_html();
4220        git_print_page_nav('','', $head,undef,$head);
4221        git_print_header_div('summary', $project);
4222
4223        my @headslist = git_get_heads_list();
4224        if (@headslist) {
4225                git_heads_body(\@headslist, $head);
4226        }
4227        git_footer_html();
4228}
4229
4230sub git_blob_plain {
4231        my $expires;
4232
4233        if (!defined $hash) {
4234                if (defined $file_name) {
4235                        my $base = $hash_base || git_get_head_hash($project);
4236                        $hash = git_get_hash_by_path($base, $file_name, "blob")
4237                                or die_error(undef, "Error lookup file");
4238                } else {
4239                        die_error(undef, "No file name defined");
4240                }
4241        } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4242                # blobs defined by non-textual hash id's can be cached
4243                $expires = "+1d";
4244        }
4245
4246        my $type = shift;
4247        open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4248                or die_error(undef, "Couldn't cat $file_name, $hash");
4249
4250        $type ||= blob_mimetype($fd, $file_name);
4251
4252        # save as filename, even when no $file_name is given
4253        my $save_as = "$hash";
4254        if (defined $file_name) {
4255                $save_as = $file_name;
4256        } elsif ($type =~ m/^text\//) {
4257                $save_as .= '.txt';
4258        }
4259
4260        print $cgi->header(
4261                -type => "$type",
4262                -expires=>$expires,
4263                -content_disposition => 'inline; filename="' . "$save_as" . '"');
4264        undef $/;
4265        binmode STDOUT, ':raw';
4266        print <$fd>;
4267        binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4268        $/ = "\n";
4269        close $fd;
4270}
4271
4272sub git_blob {
4273        my $expires;
4274
4275        if (!defined $hash) {
4276                if (defined $file_name) {
4277                        my $base = $hash_base || git_get_head_hash($project);
4278                        $hash = git_get_hash_by_path($base, $file_name, "blob")
4279                                or die_error(undef, "Error lookup file");
4280                } else {
4281                        die_error(undef, "No file name defined");
4282                }
4283        } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4284                # blobs defined by non-textual hash id's can be cached
4285                $expires = "+1d";
4286        }
4287
4288        my ($have_blame) = gitweb_check_feature('blame');
4289        open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4290                or die_error(undef, "Couldn't cat $file_name, $hash");
4291        my $mimetype = blob_mimetype($fd, $file_name);
4292        if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)!) {
4293                close $fd;
4294                return git_blob_plain($mimetype);
4295        }
4296        # we can have blame only for text/* mimetype
4297        $have_blame &&= ($mimetype =~ m!^text/!);
4298
4299        git_header_html(undef, $expires);
4300        my $formats_nav = '';
4301        if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4302                if (defined $file_name) {
4303                        if ($have_blame) {
4304                                $formats_nav .=
4305                                        $cgi->a({-href => href(action=>"blame", -replay=>1)},
4306                                                "blame") .
4307                                        " | ";
4308                        }
4309                        $formats_nav .=
4310                                $cgi->a({-href => href(action=>"history", -replay=>1)},
4311                                        "history") .
4312                                " | " .
4313                                $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
4314                                        "raw") .
4315                                " | " .
4316                                $cgi->a({-href => href(action=>"blob",
4317                                                       hash_base=>"HEAD", file_name=>$file_name)},
4318                                        "HEAD");
4319                } else {
4320                        $formats_nav .=
4321                                $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
4322                                        "raw");
4323                }
4324                git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4325                git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4326        } else {
4327                print "<div class=\"page_nav\">\n" .
4328                      "<br/><br/></div>\n" .
4329                      "<div class=\"title\">$hash</div>\n";
4330        }
4331        git_print_page_path($file_name, "blob", $hash_base);
4332        print "<div class=\"page_body\">\n";
4333        if ($mimetype =~ m!^text/!) {
4334                my $nr;
4335                while (my $line = <$fd>) {
4336                        chomp $line;
4337                        $nr++;
4338                        $line = untabify($line);
4339                        printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
4340                               $nr, $nr, $nr, esc_html($line, -nbsp=>1);
4341                }
4342        } elsif ($mimetype =~ m!^image/!) {
4343                print qq!<img type="$mimetype"!;
4344                if ($file_name) {
4345                        print qq! alt="$file_name" title="$file_name"!;
4346                }
4347                print qq! src="! .
4348                      href(action=>"blob_plain", hash=>$hash,
4349                           hash_base=>$hash_base, file_name=>$file_name) .
4350                      qq!" />\n!;
4351        }
4352        close $fd
4353                or print "Reading blob failed.\n";
4354        print "</div>";
4355        git_footer_html();
4356}
4357
4358sub git_tree {
4359        if (!defined $hash_base) {
4360                $hash_base = "HEAD";
4361        }
4362        if (!defined $hash) {
4363                if (defined $file_name) {
4364                        $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
4365                } else {
4366                        $hash = $hash_base;
4367                }
4368        }
4369        $/ = "\0";
4370        open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
4371                or die_error(undef, "Open git-ls-tree failed");
4372        my @entries = map { chomp; $_ } <$fd>;
4373        close $fd or die_error(undef, "Reading tree failed");
4374        $/ = "\n";
4375
4376        my $refs = git_get_references();
4377        my $ref = format_ref_marker($refs, $hash_base);
4378        git_header_html();
4379        my $basedir = '';
4380        my ($have_blame) = gitweb_check_feature('blame');
4381        if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4382                my @views_nav = ();
4383                if (defined $file_name) {
4384                        push @views_nav,
4385                                $cgi->a({-href => href(action=>"history", -replay=>1)},
4386                                        "history"),
4387                                $cgi->a({-href => href(action=>"tree",
4388                                                       hash_base=>"HEAD", file_name=>$file_name)},
4389                                        "HEAD"),
4390                }
4391                my $snapshot_links = format_snapshot_links($hash);
4392                if (defined $snapshot_links) {
4393                        # FIXME: Should be available when we have no hash base as well.
4394                        push @views_nav, $snapshot_links;
4395                }
4396                git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
4397                git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
4398        } else {
4399                undef $hash_base;
4400                print "<div class=\"page_nav\">\n";
4401                print "<br/><br/></div>\n";
4402                print "<div class=\"title\">$hash</div>\n";
4403        }
4404        if (defined $file_name) {
4405                $basedir = $file_name;
4406                if ($basedir ne '' && substr($basedir, -1) ne '/') {
4407                        $basedir .= '/';
4408                }
4409        }
4410        git_print_page_path($file_name, 'tree', $hash_base);
4411        print "<div class=\"page_body\">\n";
4412        print "<table class=\"tree\">\n";
4413        my $alternate = 1;
4414        # '..' (top directory) link if possible
4415        if (defined $hash_base &&
4416            defined $file_name && $file_name =~ m![^/]+$!) {
4417                if ($alternate) {
4418                        print "<tr class=\"dark\">\n";
4419                } else {
4420                        print "<tr class=\"light\">\n";
4421                }
4422                $alternate ^= 1;
4423
4424                my $up = $file_name;
4425                $up =~ s!/?[^/]+$!!;
4426                undef $up unless $up;
4427                # based on git_print_tree_entry
4428                print '<td class="mode">' . mode_str('040000') . "</td>\n";
4429                print '<td class="list">';
4430                print $cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,
4431                                             file_name=>$up)},
4432                              "..");
4433                print "</td>\n";
4434                print "<td class=\"link\"></td>\n";
4435
4436                print "</tr>\n";
4437        }
4438        foreach my $line (@entries) {
4439                my %t = parse_ls_tree_line($line, -z => 1);
4440
4441                if ($alternate) {
4442                        print "<tr class=\"dark\">\n";
4443                } else {
4444                        print "<tr class=\"light\">\n";
4445                }
4446                $alternate ^= 1;
4447
4448                git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
4449
4450                print "</tr>\n";
4451        }
4452        print "</table>\n" .
4453              "</div>";
4454        git_footer_html();
4455}
4456
4457sub git_snapshot {
4458        my @supported_fmts = gitweb_check_feature('snapshot');
4459        @supported_fmts = filter_snapshot_fmts(@supported_fmts);
4460
4461        my $format = $cgi->param('sf');
4462        if (!@supported_fmts) {
4463                die_error('403 Permission denied', "Permission denied");
4464        }
4465        # default to first supported snapshot format
4466        $format ||= $supported_fmts[0];
4467        if ($format !~ m/^[a-z0-9]+$/) {
4468                die_error(undef, "Invalid snapshot format parameter");
4469        } elsif (!exists($known_snapshot_formats{$format})) {
4470                die_error(undef, "Unknown snapshot format");
4471        } elsif (!grep($_ eq $format, @supported_fmts)) {
4472                die_error(undef, "Unsupported snapshot format");
4473        }
4474
4475        if (!defined $hash) {
4476                $hash = git_get_head_hash($project);
4477        }
4478
4479        my $git_command = git_cmd_str();
4480        my $name = $project;
4481        $name =~ s,([^/])/*\.git$,$1,;
4482        $name = basename($name);
4483        my $filename = to_utf8($name);
4484        $name =~ s/\047/\047\\\047\047/g;
4485        my $cmd;
4486        $filename .= "-$hash$known_snapshot_formats{$format}{'suffix'}";
4487        $cmd = "$git_command archive " .
4488                "--format=$known_snapshot_formats{$format}{'format'} " .
4489                "--prefix=\'$name\'/ $hash";
4490        if (exists $known_snapshot_formats{$format}{'compressor'}) {
4491                $cmd .= ' | ' . join ' ', @{$known_snapshot_formats{$format}{'compressor'}};
4492        }
4493
4494        print $cgi->header(
4495                -type => $known_snapshot_formats{$format}{'type'},
4496                -content_disposition => 'inline; filename="' . "$filename" . '"',
4497                -status => '200 OK');
4498
4499        open my $fd, "-|", $cmd
4500                or die_error(undef, "Execute git-archive failed");
4501        binmode STDOUT, ':raw';
4502        print <$fd>;
4503        binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4504        close $fd;
4505}
4506
4507sub git_log {
4508        my $head = git_get_head_hash($project);
4509        if (!defined $hash) {
4510                $hash = $head;
4511        }
4512        if (!defined $page) {
4513                $page = 0;
4514        }
4515        my $refs = git_get_references();
4516
4517        my @commitlist = parse_commits($hash, 101, (100 * $page));
4518
4519        my $paging_nav = format_paging_nav('log', $hash, $head, $page, (100 * ($page+1)));
4520
4521        git_header_html();
4522        git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
4523
4524        if (!@commitlist) {
4525                my %co = parse_commit($hash);
4526
4527                git_print_header_div('summary', $project);
4528                print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
4529        }
4530        my $to = ($#commitlist >= 99) ? (99) : ($#commitlist);
4531        for (my $i = 0; $i <= $to; $i++) {
4532                my %co = %{$commitlist[$i]};
4533                next if !%co;
4534                my $commit = $co{'id'};
4535                my $ref = format_ref_marker($refs, $commit);
4536                my %ad = parse_date($co{'author_epoch'});
4537                git_print_header_div('commit',
4538                               "<span class=\"age\">$co{'age_string'}</span>" .
4539                               esc_html($co{'title'}) . $ref,
4540                               $commit);
4541                print "<div class=\"title_text\">\n" .
4542                      "<div class=\"log_link\">\n" .
4543                      $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
4544                      " | " .
4545                      $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
4546                      " | " .
4547                      $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
4548                      "<br/>\n" .
4549                      "</div>\n" .
4550                      "<i>" . esc_html($co{'author_name'}) .  " [$ad{'rfc2822'}]</i><br/>\n" .
4551                      "</div>\n";
4552
4553                print "<div class=\"log_body\">\n";
4554                git_print_log($co{'comment'}, -final_empty_line=> 1);
4555                print "</div>\n";
4556        }
4557        if ($#commitlist >= 100) {
4558                print "<div class=\"page_nav\">\n";
4559                print $cgi->a({-href => href(-replay=>1, page=>$page+1),
4560                               -accesskey => "n", -title => "Alt-n"}, "next");
4561                print "</div>\n";
4562        }
4563        git_footer_html();
4564}
4565
4566sub git_commit {
4567        $hash ||= $hash_base || "HEAD";
4568        my %co = parse_commit($hash);
4569        if (!%co) {
4570                die_error(undef, "Unknown commit object");
4571        }
4572        my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4573        my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
4574
4575        my $parent  = $co{'parent'};
4576        my $parents = $co{'parents'}; # listref
4577
4578        # we need to prepare $formats_nav before any parameter munging
4579        my $formats_nav;
4580        if (!defined $parent) {
4581                # --root commitdiff
4582                $formats_nav .= '(initial)';
4583        } elsif (@$parents == 1) {
4584                # single parent commit
4585                $formats_nav .=
4586                        '(parent: ' .
4587                        $cgi->a({-href => href(action=>"commit",
4588                                               hash=>$parent)},
4589                                esc_html(substr($parent, 0, 7))) .
4590                        ')';
4591        } else {
4592                # merge commit
4593                $formats_nav .=
4594                        '(merge: ' .
4595                        join(' ', map {
4596                                $cgi->a({-href => href(action=>"commit",
4597                                                       hash=>$_)},
4598                                        esc_html(substr($_, 0, 7)));
4599                        } @$parents ) .
4600                        ')';
4601        }
4602
4603        if (!defined $parent) {
4604                $parent = "--root";
4605        }
4606        my @difftree;
4607        open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
4608                @diff_opts,
4609                (@$parents <= 1 ? $parent : '-c'),
4610                $hash, "--"
4611                or die_error(undef, "Open git-diff-tree failed");
4612        @difftree = map { chomp; $_ } <$fd>;
4613        close $fd or die_error(undef, "Reading git-diff-tree failed");
4614
4615        # non-textual hash id's can be cached
4616        my $expires;
4617        if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4618                $expires = "+1d";
4619        }
4620        my $refs = git_get_references();
4621        my $ref = format_ref_marker($refs, $co{'id'});
4622
4623        git_header_html(undef, $expires);
4624        git_print_page_nav('commit', '',
4625                           $hash, $co{'tree'}, $hash,
4626                           $formats_nav);
4627
4628        if (defined $co{'parent'}) {
4629                git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
4630        } else {
4631                git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
4632        }
4633        print "<div class=\"title_text\">\n" .
4634              "<table class=\"object_header\">\n";
4635        print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
4636              "<tr>" .
4637              "<td></td><td> $ad{'rfc2822'}";
4638        if ($ad{'hour_local'} < 6) {
4639                printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
4640                       $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4641        } else {
4642                printf(" (%02d:%02d %s)",
4643                       $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4644        }
4645        print "</td>" .
4646              "</tr>\n";
4647        print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
4648        print "<tr><td></td><td> $cd{'rfc2822'}" .
4649              sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
4650              "</td></tr>\n";
4651        print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
4652        print "<tr>" .
4653              "<td>tree</td>" .
4654              "<td class=\"sha1\">" .
4655              $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
4656                       class => "list"}, $co{'tree'}) .
4657              "</td>" .
4658              "<td class=\"link\">" .
4659              $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
4660                      "tree");
4661        my $snapshot_links = format_snapshot_links($hash);
4662        if (defined $snapshot_links) {
4663                print " | " . $snapshot_links;
4664        }
4665        print "</td>" .
4666              "</tr>\n";
4667
4668        foreach my $par (@$parents) {
4669                print "<tr>" .
4670                      "<td>parent</td>" .
4671                      "<td class=\"sha1\">" .
4672                      $cgi->a({-href => href(action=>"commit", hash=>$par),
4673                               class => "list"}, $par) .
4674                      "</td>" .
4675                      "<td class=\"link\">" .
4676                      $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
4677                      " | " .
4678                      $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
4679                      "</td>" .
4680                      "</tr>\n";
4681        }
4682        print "</table>".
4683              "</div>\n";
4684
4685        print "<div class=\"page_body\">\n";
4686        git_print_log($co{'comment'});
4687        print "</div>\n";
4688
4689        git_difftree_body(\@difftree, $hash, @$parents);
4690
4691        git_footer_html();
4692}
4693
4694sub git_object {
4695        # object is defined by:
4696        # - hash or hash_base alone
4697        # - hash_base and file_name
4698        my $type;
4699
4700        # - hash or hash_base alone
4701        if ($hash || ($hash_base && !defined $file_name)) {
4702                my $object_id = $hash || $hash_base;
4703
4704                my $git_command = git_cmd_str();
4705                open my $fd, "-|", "$git_command cat-file -t $object_id 2>/dev/null"
4706                        or die_error('404 Not Found', "Object does not exist");
4707                $type = <$fd>;
4708                chomp $type;
4709                close $fd
4710                        or die_error('404 Not Found', "Object does not exist");
4711
4712        # - hash_base and file_name
4713        } elsif ($hash_base && defined $file_name) {
4714                $file_name =~ s,/+$,,;
4715
4716                system(git_cmd(), "cat-file", '-e', $hash_base) == 0
4717                        or die_error('404 Not Found', "Base object does not exist");
4718
4719                # here errors should not hapen
4720                open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
4721                        or die_error(undef, "Open git-ls-tree failed");
4722                my $line = <$fd>;
4723                close $fd;
4724
4725                #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa  panic.c'
4726                unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
4727                        die_error('404 Not Found', "File or directory for given base does not exist");
4728                }
4729                $type = $2;
4730                $hash = $3;
4731        } else {
4732                die_error('404 Not Found', "Not enough information to find object");
4733        }
4734
4735        print $cgi->redirect(-uri => href(action=>$type, -full=>1,
4736                                          hash=>$hash, hash_base=>$hash_base,
4737                                          file_name=>$file_name),
4738                             -status => '302 Found');
4739}
4740
4741sub git_blobdiff {
4742        my $format = shift || 'html';
4743
4744        my $fd;
4745        my @difftree;
4746        my %diffinfo;
4747        my $expires;
4748
4749        # preparing $fd and %diffinfo for git_patchset_body
4750        # new style URI
4751        if (defined $hash_base && defined $hash_parent_base) {
4752                if (defined $file_name) {
4753                        # read raw output
4754                        open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4755                                $hash_parent_base, $hash_base,
4756                                "--", (defined $file_parent ? $file_parent : ()), $file_name
4757                                or die_error(undef, "Open git-diff-tree failed");
4758                        @difftree = map { chomp; $_ } <$fd>;
4759                        close $fd
4760                                or die_error(undef, "Reading git-diff-tree failed");
4761                        @difftree
4762                                or die_error('404 Not Found', "Blob diff not found");
4763
4764                } elsif (defined $hash &&
4765                         $hash =~ /[0-9a-fA-F]{40}/) {
4766                        # try to find filename from $hash
4767
4768                        # read filtered raw output
4769                        open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4770                                $hash_parent_base, $hash_base, "--"
4771                                or die_error(undef, "Open git-diff-tree failed");
4772                        @difftree =
4773                                # ':100644 100644 03b21826... 3b93d5e7... M     ls-files.c'
4774                                # $hash == to_id
4775                                grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
4776                                map { chomp; $_ } <$fd>;
4777                        close $fd
4778                                or die_error(undef, "Reading git-diff-tree failed");
4779                        @difftree
4780                                or die_error('404 Not Found', "Blob diff not found");
4781
4782                } else {
4783                        die_error('404 Not Found', "Missing one of the blob diff parameters");
4784                }
4785
4786                if (@difftree > 1) {
4787                        die_error('404 Not Found', "Ambiguous blob diff specification");
4788                }
4789
4790                %diffinfo = parse_difftree_raw_line($difftree[0]);
4791                $file_parent ||= $diffinfo{'from_file'} || $file_name;
4792                $file_name   ||= $diffinfo{'to_file'};
4793
4794                $hash_parent ||= $diffinfo{'from_id'};
4795                $hash        ||= $diffinfo{'to_id'};
4796
4797                # non-textual hash id's can be cached
4798                if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
4799                    $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
4800                        $expires = '+1d';
4801                }
4802
4803                # open patch output
4804                open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4805                        '-p', ($format eq 'html' ? "--full-index" : ()),
4806                        $hash_parent_base, $hash_base,
4807                        "--", (defined $file_parent ? $file_parent : ()), $file_name
4808                        or die_error(undef, "Open git-diff-tree failed");
4809        }
4810
4811        # old/legacy style URI
4812        if (!%diffinfo && # if new style URI failed
4813            defined $hash && defined $hash_parent) {
4814                # fake git-diff-tree raw output
4815                $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
4816                $diffinfo{'from_id'} = $hash_parent;
4817                $diffinfo{'to_id'}   = $hash;
4818                if (defined $file_name) {
4819                        if (defined $file_parent) {
4820                                $diffinfo{'status'} = '2';
4821                                $diffinfo{'from_file'} = $file_parent;
4822                                $diffinfo{'to_file'}   = $file_name;
4823                        } else { # assume not renamed
4824                                $diffinfo{'status'} = '1';
4825                                $diffinfo{'from_file'} = $file_name;
4826                                $diffinfo{'to_file'}   = $file_name;
4827                        }
4828                } else { # no filename given
4829                        $diffinfo{'status'} = '2';
4830                        $diffinfo{'from_file'} = $hash_parent;
4831                        $diffinfo{'to_file'}   = $hash;
4832                }
4833
4834                # non-textual hash id's can be cached
4835                if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
4836                    $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4837                        $expires = '+1d';
4838                }
4839
4840                # open patch output
4841                open $fd, "-|", git_cmd(), "diff", @diff_opts,
4842                        '-p', ($format eq 'html' ? "--full-index" : ()),
4843                        $hash_parent, $hash, "--"
4844                        or die_error(undef, "Open git-diff failed");
4845        } else  {
4846                die_error('404 Not Found', "Missing one of the blob diff parameters")
4847                        unless %diffinfo;
4848        }
4849
4850        # header
4851        if ($format eq 'html') {
4852                my $formats_nav =
4853                        $cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},
4854                                "raw");
4855                git_header_html(undef, $expires);
4856                if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4857                        git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4858                        git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4859                } else {
4860                        print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
4861                        print "<div class=\"title\">$hash vs $hash_parent</div>\n";
4862                }
4863                if (defined $file_name) {
4864                        git_print_page_path($file_name, "blob", $hash_base);
4865                } else {
4866                        print "<div class=\"page_path\"></div>\n";
4867                }
4868
4869        } elsif ($format eq 'plain') {
4870                print $cgi->header(
4871                        -type => 'text/plain',
4872                        -charset => 'utf-8',
4873                        -expires => $expires,
4874                        -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
4875
4876                print "X-Git-Url: " . $cgi->self_url() . "\n\n";
4877
4878        } else {
4879                die_error(undef, "Unknown blobdiff format");
4880        }
4881
4882        # patch
4883        if ($format eq 'html') {
4884                print "<div class=\"page_body\">\n";
4885
4886                git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
4887                close $fd;
4888
4889                print "</div>\n"; # class="page_body"
4890                git_footer_html();
4891
4892        } else {
4893                while (my $line = <$fd>) {
4894                        $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
4895                        $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
4896
4897                        print $line;
4898
4899                        last if $line =~ m!^\+\+\+!;
4900                }
4901                local $/ = undef;
4902                print <$fd>;
4903                close $fd;
4904        }
4905}
4906
4907sub git_blobdiff_plain {
4908        git_blobdiff('plain');
4909}
4910
4911sub git_commitdiff {
4912        my $format = shift || 'html';
4913        $hash ||= $hash_base || "HEAD";
4914        my %co = parse_commit($hash);
4915        if (!%co) {
4916                die_error(undef, "Unknown commit object");
4917        }
4918
4919        # choose format for commitdiff for merge
4920        if (! defined $hash_parent && @{$co{'parents'}} > 1) {
4921                $hash_parent = '--cc';
4922        }
4923        # we need to prepare $formats_nav before almost any parameter munging
4924        my $formats_nav;
4925        if ($format eq 'html') {
4926                $formats_nav =
4927                        $cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
4928                                "raw");
4929
4930                if (defined $hash_parent &&
4931                    $hash_parent ne '-c' && $hash_parent ne '--cc') {
4932                        # commitdiff with two commits given
4933                        my $hash_parent_short = $hash_parent;
4934                        if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4935                                $hash_parent_short = substr($hash_parent, 0, 7);
4936                        }
4937                        $formats_nav .=
4938                                ' (from';
4939                        for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
4940                                if ($co{'parents'}[$i] eq $hash_parent) {
4941                                        $formats_nav .= ' parent ' . ($i+1);
4942                                        last;
4943                                }
4944                        }
4945                        $formats_nav .= ': ' .
4946                                $cgi->a({-href => href(action=>"commitdiff",
4947                                                       hash=>$hash_parent)},
4948                                        esc_html($hash_parent_short)) .
4949                                ')';
4950                } elsif (!$co{'parent'}) {
4951                        # --root commitdiff
4952                        $formats_nav .= ' (initial)';
4953                } elsif (scalar @{$co{'parents'}} == 1) {
4954                        # single parent commit
4955                        $formats_nav .=
4956                                ' (parent: ' .
4957                                $cgi->a({-href => href(action=>"commitdiff",
4958                                                       hash=>$co{'parent'})},
4959                                        esc_html(substr($co{'parent'}, 0, 7))) .
4960                                ')';
4961                } else {
4962                        # merge commit
4963                        if ($hash_parent eq '--cc') {
4964                                $formats_nav .= ' | ' .
4965                                        $cgi->a({-href => href(action=>"commitdiff",
4966                                                               hash=>$hash, hash_parent=>'-c')},
4967                                                'combined');
4968                        } else { # $hash_parent eq '-c'
4969                                $formats_nav .= ' | ' .
4970                                        $cgi->a({-href => href(action=>"commitdiff",
4971                                                               hash=>$hash, hash_parent=>'--cc')},
4972                                                'compact');
4973                        }
4974                        $formats_nav .=
4975                                ' (merge: ' .
4976                                join(' ', map {
4977                                        $cgi->a({-href => href(action=>"commitdiff",
4978                                                               hash=>$_)},
4979                                                esc_html(substr($_, 0, 7)));
4980                                } @{$co{'parents'}} ) .
4981                                ')';
4982                }
4983        }
4984
4985        my $hash_parent_param = $hash_parent;
4986        if (!defined $hash_parent_param) {
4987                # --cc for multiple parents, --root for parentless
4988                $hash_parent_param =
4989                        @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
4990        }
4991
4992        # read commitdiff
4993        my $fd;
4994        my @difftree;
4995        if ($format eq 'html') {
4996                open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4997                        "--no-commit-id", "--patch-with-raw", "--full-index",
4998                        $hash_parent_param, $hash, "--"
4999                        or die_error(undef, "Open git-diff-tree failed");
5000
5001                while (my $line = <$fd>) {
5002                        chomp $line;
5003                        # empty line ends raw part of diff-tree output
5004                        last unless $line;
5005                        push @difftree, scalar parse_difftree_raw_line($line);
5006                }
5007
5008        } elsif ($format eq 'plain') {
5009                open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5010                        '-p', $hash_parent_param, $hash, "--"
5011                        or die_error(undef, "Open git-diff-tree failed");
5012
5013        } else {
5014                die_error(undef, "Unknown commitdiff format");
5015        }
5016
5017        # non-textual hash id's can be cached
5018        my $expires;
5019        if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5020                $expires = "+1d";
5021        }
5022
5023        # write commit message
5024        if ($format eq 'html') {
5025                my $refs = git_get_references();
5026                my $ref = format_ref_marker($refs, $co{'id'});
5027
5028                git_header_html(undef, $expires);
5029                git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
5030                git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
5031                git_print_authorship(\%co);
5032                print "<div class=\"page_body\">\n";
5033                if (@{$co{'comment'}} > 1) {
5034                        print "<div class=\"log\">\n";
5035                        git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
5036                        print "</div>\n"; # class="log"
5037                }
5038
5039        } elsif ($format eq 'plain') {
5040                my $refs = git_get_references("tags");
5041                my $tagname = git_get_rev_name_tags($hash);
5042                my $filename = basename($project) . "-$hash.patch";
5043
5044                print $cgi->header(
5045                        -type => 'text/plain',
5046                        -charset => 'utf-8',
5047                        -expires => $expires,
5048                        -content_disposition => 'inline; filename="' . "$filename" . '"');
5049                my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
5050                print <<TEXT;
5051From: $co{'author'}
5052Date: $ad{'rfc2822'} ($ad{'tz_local'})
5053Subject: $co{'title'}
5054TEXT
5055                print "X-Git-Tag: $tagname\n" if $tagname;
5056                print "X-Git-Url: " . $cgi->self_url() . "\n\n";
5057
5058                foreach my $line (@{$co{'comment'}}) {
5059                        print "$line\n";
5060                }
5061                print "---\n\n";
5062        }
5063
5064        # write patch
5065        if ($format eq 'html') {
5066                my $use_parents = !defined $hash_parent ||
5067                        $hash_parent eq '-c' || $hash_parent eq '--cc';
5068                git_difftree_body(\@difftree, $hash,
5069                                  $use_parents ? @{$co{'parents'}} : $hash_parent);
5070                print "<br/>\n";
5071
5072                git_patchset_body($fd, \@difftree, $hash,
5073                                  $use_parents ? @{$co{'parents'}} : $hash_parent);
5074                close $fd;
5075                print "</div>\n"; # class="page_body"
5076                git_footer_html();
5077
5078        } elsif ($format eq 'plain') {
5079                local $/ = undef;
5080                print <$fd>;
5081                close $fd
5082                        or print "Reading git-diff-tree failed\n";
5083        }
5084}
5085
5086sub git_commitdiff_plain {
5087        git_commitdiff('plain');
5088}
5089
5090sub git_history {
5091        if (!defined $hash_base) {
5092                $hash_base = git_get_head_hash($project);
5093        }
5094        if (!defined $page) {
5095                $page = 0;
5096        }
5097        my $ftype;
5098        my %co = parse_commit($hash_base);
5099        if (!%co) {
5100                die_error(undef, "Unknown commit object");
5101        }
5102
5103        my $refs = git_get_references();
5104        my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
5105
5106        if (!defined $hash && defined $file_name) {
5107                $hash = git_get_hash_by_path($hash_base, $file_name);
5108        }
5109        if (defined $hash) {
5110                $ftype = git_get_type($hash);
5111        }
5112
5113        my @commitlist = parse_commits($hash_base, 101, (100 * $page), "--full-history", $file_name);
5114
5115        my $paging_nav = '';
5116        if ($page > 0) {
5117                $paging_nav .=
5118                        $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
5119                                               file_name=>$file_name)},
5120                                "first");
5121                $paging_nav .= " &sdot; " .
5122                        $cgi->a({-href => href(-replay=>1, page=>$page-1),
5123                                 -accesskey => "p", -title => "Alt-p"}, "prev");
5124        } else {
5125                $paging_nav .= "first";
5126                $paging_nav .= " &sdot; prev";
5127        }
5128        my $next_link = '';
5129        if ($#commitlist >= 100) {
5130                $next_link =
5131                        $cgi->a({-href => href(-replay=>1, page=>$page+1),
5132                                 -accesskey => "n", -title => "Alt-n"}, "next");
5133                $paging_nav .= " &sdot; $next_link";
5134        } else {
5135                $paging_nav .= " &sdot; next";
5136        }
5137
5138        git_header_html();
5139        git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
5140        git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5141        git_print_page_path($file_name, $ftype, $hash_base);
5142
5143        git_history_body(\@commitlist, 0, 99,
5144                         $refs, $hash_base, $ftype, $next_link);
5145
5146        git_footer_html();
5147}
5148
5149sub git_search {
5150        my ($have_search) = gitweb_check_feature('search');
5151        if (!$have_search) {
5152                die_error('403 Permission denied', "Permission denied");
5153        }
5154        if (!defined $searchtext) {
5155                die_error(undef, "Text field empty");
5156        }
5157        if (!defined $hash) {
5158                $hash = git_get_head_hash($project);
5159        }
5160        my %co = parse_commit($hash);
5161        if (!%co) {
5162                die_error(undef, "Unknown commit object");
5163        }
5164        if (!defined $page) {
5165                $page = 0;
5166        }
5167
5168        $searchtype ||= 'commit';
5169        if ($searchtype eq 'pickaxe') {
5170                # pickaxe may take all resources of your box and run for several minutes
5171                # with every query - so decide by yourself how public you make this feature
5172                my ($have_pickaxe) = gitweb_check_feature('pickaxe');
5173                if (!$have_pickaxe) {
5174                        die_error('403 Permission denied', "Permission denied");
5175                }
5176        }
5177        if ($searchtype eq 'grep') {
5178                my ($have_grep) = gitweb_check_feature('grep');
5179                if (!$have_grep) {
5180                        die_error('403 Permission denied', "Permission denied");
5181                }
5182        }
5183
5184        git_header_html();
5185
5186        if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
5187                my $greptype;
5188                if ($searchtype eq 'commit') {
5189                        $greptype = "--grep=";
5190                } elsif ($searchtype eq 'author') {
5191                        $greptype = "--author=";
5192                } elsif ($searchtype eq 'committer') {
5193                        $greptype = "--committer=";
5194                }
5195                $greptype .= $search_regexp;
5196                my @commitlist = parse_commits($hash, 101, (100 * $page), $greptype);
5197
5198                my $paging_nav = '';
5199                if ($page > 0) {
5200                        $paging_nav .=
5201                                $cgi->a({-href => href(action=>"search", hash=>$hash,
5202                                                       searchtext=>$searchtext, searchtype=>$searchtype)},
5203                                        "first");
5204                        $paging_nav .= " &sdot; " .
5205                                $cgi->a({-href => href(-replay=>1, page=>$page-1),
5206                                         -accesskey => "p", -title => "Alt-p"}, "prev");
5207                } else {
5208                        $paging_nav .= "first";
5209                        $paging_nav .= " &sdot; prev";
5210                }
5211                my $next_link = '';
5212                if ($#commitlist >= 100) {
5213                        $next_link =
5214                                $cgi->a({-href => href(-replay=>1, page=>$page+1),
5215                                         -accesskey => "n", -title => "Alt-n"}, "next");
5216                        $paging_nav .= " &sdot; $next_link";
5217                } else {
5218                        $paging_nav .= " &sdot; next";
5219                }
5220
5221                if ($#commitlist >= 100) {
5222                }
5223
5224                git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
5225                git_print_header_div('commit', esc_html($co{'title'}), $hash);
5226                git_search_grep_body(\@commitlist, 0, 99, $next_link);
5227        }
5228
5229        if ($searchtype eq 'pickaxe') {
5230                git_print_page_nav('','', $hash,$co{'tree'},$hash);
5231                git_print_header_div('commit', esc_html($co{'title'}), $hash);
5232
5233                print "<table class=\"pickaxe search\">\n";
5234                my $alternate = 1;
5235                $/ = "\n";
5236                my $git_command = git_cmd_str();
5237                my $searchqtext = $searchtext;
5238                $searchqtext =~ s/'/'\\''/;
5239                open my $fd, "-|", "$git_command rev-list $hash | " .
5240                        "$git_command diff-tree -r --stdin -S\'$searchqtext\'";
5241                undef %co;
5242                my @files;
5243                while (my $line = <$fd>) {
5244                        if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
5245                                my %set;
5246                                $set{'file'} = $6;
5247                                $set{'from_id'} = $3;
5248                                $set{'to_id'} = $4;
5249                                $set{'id'} = $set{'to_id'};
5250                                if ($set{'id'} =~ m/0{40}/) {
5251                                        $set{'id'} = $set{'from_id'};
5252                                }
5253                                if ($set{'id'} =~ m/0{40}/) {
5254                                        next;
5255                                }
5256                                push @files, \%set;
5257                        } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
5258                                if (%co) {
5259                                        if ($alternate) {
5260                                                print "<tr class=\"dark\">\n";
5261                                        } else {
5262                                                print "<tr class=\"light\">\n";
5263                                        }
5264                                        $alternate ^= 1;
5265                                        my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
5266                                        print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5267                                              "<td><i>" . $author . "</i></td>\n" .
5268                                              "<td>" .
5269                                              $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
5270                                                      -class => "list subject"},
5271                                                      chop_and_escape_str($co{'title'}, 50) . "<br/>");
5272                                        while (my $setref = shift @files) {
5273                                                my %set = %$setref;
5274                                                print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
5275                                                                             hash=>$set{'id'}, file_name=>$set{'file'}),
5276                                                              -class => "list"},
5277                                                              "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
5278                                                      "<br/>\n";
5279                                        }
5280                                        print "</td>\n" .
5281                                              "<td class=\"link\">" .
5282                                              $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5283                                              " | " .
5284                                              $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5285                                        print "</td>\n" .
5286                                              "</tr>\n";
5287                                }
5288                                %co = parse_commit($1);
5289                        }
5290                }
5291                close $fd;
5292
5293                print "</table>\n";
5294        }
5295
5296        if ($searchtype eq 'grep') {
5297                git_print_page_nav('','', $hash,$co{'tree'},$hash);
5298                git_print_header_div('commit', esc_html($co{'title'}), $hash);
5299
5300                print "<table class=\"grep_search\">\n";
5301                my $alternate = 1;
5302                my $matches = 0;
5303                $/ = "\n";
5304                open my $fd, "-|", git_cmd(), 'grep', '-n', '-i', '-E', $searchtext, $co{'tree'};
5305                my $lastfile = '';
5306                while (my $line = <$fd>) {
5307                        chomp $line;
5308                        my ($file, $lno, $ltext, $binary);
5309                        last if ($matches++ > 1000);
5310                        if ($line =~ /^Binary file (.+) matches$/) {
5311                                $file = $1;
5312                                $binary = 1;
5313                        } else {
5314                                (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
5315                        }
5316                        if ($file ne $lastfile) {
5317                                $lastfile and print "</td></tr>\n";
5318                                if ($alternate++) {
5319                                        print "<tr class=\"dark\">\n";
5320                                } else {
5321                                        print "<tr class=\"light\">\n";
5322                                }
5323                                print "<td class=\"list\">".
5324                                        $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5325                                                               file_name=>"$file"),
5326                                                -class => "list"}, esc_path($file));
5327                                print "</td><td>\n";
5328                                $lastfile = $file;
5329                        }
5330                        if ($binary) {
5331                                print "<div class=\"binary\">Binary file</div>\n";
5332                        } else {
5333                                $ltext = untabify($ltext);
5334                                if ($ltext =~ m/^(.*)($searchtext)(.*)$/i) {
5335                                        $ltext = esc_html($1, -nbsp=>1);
5336                                        $ltext .= '<span class="match">';
5337                                        $ltext .= esc_html($2, -nbsp=>1);
5338                                        $ltext .= '</span>';
5339                                        $ltext .= esc_html($3, -nbsp=>1);
5340                                } else {
5341                                        $ltext = esc_html($ltext, -nbsp=>1);
5342                                }
5343                                print "<div class=\"pre\">" .
5344                                        $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5345                                                               file_name=>"$file").'#l'.$lno,
5346                                                -class => "linenr"}, sprintf('%4i', $lno))
5347                                        . ' ' .  $ltext . "</div>\n";
5348                        }
5349                }
5350                if ($lastfile) {
5351                        print "</td></tr>\n";
5352                        if ($matches > 1000) {
5353                                print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
5354                        }
5355                } else {
5356                        print "<div class=\"diff nodifferences\">No matches found</div>\n";
5357                }
5358                close $fd;
5359
5360                print "</table>\n";
5361        }
5362        git_footer_html();
5363}
5364
5365sub git_search_help {
5366        git_header_html();
5367        git_print_page_nav('','', $hash,$hash,$hash);
5368        print <<EOT;
5369<dl>
5370<dt><b>commit</b></dt>
5371<dd>The commit messages and authorship information will be scanned for the given string.</dd>
5372EOT
5373        my ($have_grep) = gitweb_check_feature('grep');
5374        if ($have_grep) {
5375                print <<EOT;
5376<dt><b>grep</b></dt>
5377<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
5378    a different one) are searched for the given
5379<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a>
5380(POSIX extended) and the matches are listed. On large
5381trees, this search can take a while and put some strain on the server, so please use it with
5382some consideration.</dd>
5383EOT
5384        }
5385        print <<EOT;
5386<dt><b>author</b></dt>
5387<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given string.</dd>
5388<dt><b>committer</b></dt>
5389<dd>Name and e-mail of the committer and date of commit will be scanned for the given string.</dd>
5390EOT
5391        my ($have_pickaxe) = gitweb_check_feature('pickaxe');
5392        if ($have_pickaxe) {
5393                print <<EOT;
5394<dt><b>pickaxe</b></dt>
5395<dd>All commits that caused the string to appear or disappear from any file (changes that
5396added, removed or "modified" the string) will be listed. This search can take a while and
5397takes a lot of strain on the server, so please use it wisely.</dd>
5398EOT
5399        }
5400        print "</dl>\n";
5401        git_footer_html();
5402}
5403
5404sub git_shortlog {
5405        my $head = git_get_head_hash($project);
5406        if (!defined $hash) {
5407                $hash = $head;
5408        }
5409        if (!defined $page) {
5410                $page = 0;
5411        }
5412        my $refs = git_get_references();
5413
5414        my @commitlist = parse_commits($hash, 101, (100 * $page));
5415
5416        my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, (100 * ($page+1)));
5417        my $next_link = '';
5418        if ($#commitlist >= 100) {
5419                $next_link =
5420                        $cgi->a({-href => href(-replay=>1, page=>$page+1),
5421                                 -accesskey => "n", -title => "Alt-n"}, "next");
5422        }
5423
5424        git_header_html();
5425        git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
5426        git_print_header_div('summary', $project);
5427
5428        git_shortlog_body(\@commitlist, 0, 99, $refs, $next_link);
5429
5430        git_footer_html();
5431}
5432
5433## ......................................................................
5434## feeds (RSS, Atom; OPML)
5435
5436sub git_feed {
5437        my $format = shift || 'atom';
5438        my ($have_blame) = gitweb_check_feature('blame');
5439
5440        # Atom: http://www.atomenabled.org/developers/syndication/
5441        # RSS:  http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
5442        if ($format ne 'rss' && $format ne 'atom') {
5443                die_error(undef, "Unknown web feed format");
5444        }
5445
5446        # log/feed of current (HEAD) branch, log of given branch, history of file/directory
5447        my $head = $hash || 'HEAD';
5448        my @commitlist = parse_commits($head, 150, 0, undef, $file_name);
5449
5450        my %latest_commit;
5451        my %latest_date;
5452        my $content_type = "application/$format+xml";
5453        if (defined $cgi->http('HTTP_ACCEPT') &&
5454                 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
5455                # browser (feed reader) prefers text/xml
5456                $content_type = 'text/xml';
5457        }
5458        if (defined($commitlist[0])) {
5459                %latest_commit = %{$commitlist[0]};
5460                %latest_date   = parse_date($latest_commit{'author_epoch'});
5461                print $cgi->header(
5462                        -type => $content_type,
5463                        -charset => 'utf-8',
5464                        -last_modified => $latest_date{'rfc2822'});
5465        } else {
5466                print $cgi->header(
5467                        -type => $content_type,
5468                        -charset => 'utf-8');
5469        }
5470
5471        # Optimization: skip generating the body if client asks only
5472        # for Last-Modified date.
5473        return if ($cgi->request_method() eq 'HEAD');
5474
5475        # header variables
5476        my $title = "$site_name - $project/$action";
5477        my $feed_type = 'log';
5478        if (defined $hash) {
5479                $title .= " - '$hash'";
5480                $feed_type = 'branch log';
5481                if (defined $file_name) {
5482                        $title .= " :: $file_name";
5483                        $feed_type = 'history';
5484                }
5485        } elsif (defined $file_name) {
5486                $title .= " - $file_name";
5487                $feed_type = 'history';
5488        }
5489        $title .= " $feed_type";
5490        my $descr = git_get_project_description($project);
5491        if (defined $descr) {
5492                $descr = esc_html($descr);
5493        } else {
5494                $descr = "$project " .
5495                         ($format eq 'rss' ? 'RSS' : 'Atom') .
5496                         " feed";
5497        }
5498        my $owner = git_get_project_owner($project);
5499        $owner = esc_html($owner);
5500
5501        #header
5502        my $alt_url;
5503        if (defined $file_name) {
5504                $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
5505        } elsif (defined $hash) {
5506                $alt_url = href(-full=>1, action=>"log", hash=>$hash);
5507        } else {
5508                $alt_url = href(-full=>1, action=>"summary");
5509        }
5510        print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
5511        if ($format eq 'rss') {
5512                print <<XML;
5513<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
5514<channel>
5515XML
5516                print "<title>$title</title>\n" .
5517                      "<link>$alt_url</link>\n" .
5518                      "<description>$descr</description>\n" .
5519                      "<language>en</language>\n";
5520        } elsif ($format eq 'atom') {
5521                print <<XML;
5522<feed xmlns="http://www.w3.org/2005/Atom">
5523XML
5524                print "<title>$title</title>\n" .
5525                      "<subtitle>$descr</subtitle>\n" .
5526                      '<link rel="alternate" type="text/html" href="' .
5527                      $alt_url . '" />' . "\n" .
5528                      '<link rel="self" type="' . $content_type . '" href="' .
5529                      $cgi->self_url() . '" />' . "\n" .
5530                      "<id>" . href(-full=>1) . "</id>\n" .
5531                      # use project owner for feed author
5532                      "<author><name>$owner</name></author>\n";
5533                if (defined $favicon) {
5534                        print "<icon>" . esc_url($favicon) . "</icon>\n";
5535                }
5536                if (defined $logo_url) {
5537                        # not twice as wide as tall: 72 x 27 pixels
5538                        print "<logo>" . esc_url($logo) . "</logo>\n";
5539                }
5540                if (! %latest_date) {
5541                        # dummy date to keep the feed valid until commits trickle in:
5542                        print "<updated>1970-01-01T00:00:00Z</updated>\n";
5543                } else {
5544                        print "<updated>$latest_date{'iso-8601'}</updated>\n";
5545                }
5546        }
5547
5548        # contents
5549        for (my $i = 0; $i <= $#commitlist; $i++) {
5550                my %co = %{$commitlist[$i]};
5551                my $commit = $co{'id'};
5552                # we read 150, we always show 30 and the ones more recent than 48 hours
5553                if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
5554                        last;
5555                }
5556                my %cd = parse_date($co{'author_epoch'});
5557
5558                # get list of changed files
5559                open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5560                        $co{'parent'} || "--root",
5561                        $co{'id'}, "--", (defined $file_name ? $file_name : ())
5562                        or next;
5563                my @difftree = map { chomp; $_ } <$fd>;
5564                close $fd
5565                        or next;
5566
5567                # print element (entry, item)
5568                my $co_url = href(-full=>1, action=>"commit", hash=>$commit);
5569                if ($format eq 'rss') {
5570                        print "<item>\n" .
5571                              "<title>" . esc_html($co{'title'}) . "</title>\n" .
5572                              "<author>" . esc_html($co{'author'}) . "</author>\n" .
5573                              "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
5574                              "<guid isPermaLink=\"true\">$co_url</guid>\n" .
5575                              "<link>$co_url</link>\n" .
5576                              "<description>" . esc_html($co{'title'}) . "</description>\n" .
5577                              "<content:encoded>" .
5578                              "<![CDATA[\n";
5579                } elsif ($format eq 'atom') {
5580                        print "<entry>\n" .
5581                              "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
5582                              "<updated>$cd{'iso-8601'}</updated>\n" .
5583                              "<author>\n" .
5584                              "  <name>" . esc_html($co{'author_name'}) . "</name>\n";
5585                        if ($co{'author_email'}) {
5586                                print "  <email>" . esc_html($co{'author_email'}) . "</email>\n";
5587                        }
5588                        print "</author>\n" .
5589                              # use committer for contributor
5590                              "<contributor>\n" .
5591                              "  <name>" . esc_html($co{'committer_name'}) . "</name>\n";
5592                        if ($co{'committer_email'}) {
5593                                print "  <email>" . esc_html($co{'committer_email'}) . "</email>\n";
5594                        }
5595                        print "</contributor>\n" .
5596                              "<published>$cd{'iso-8601'}</published>\n" .
5597                              "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
5598                              "<id>$co_url</id>\n" .
5599                              "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
5600                              "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
5601                }
5602                my $comment = $co{'comment'};
5603                print "<pre>\n";
5604                foreach my $line (@$comment) {
5605                        $line = esc_html($line);
5606                        print "$line\n";
5607                }
5608                print "</pre><ul>\n";
5609                foreach my $difftree_line (@difftree) {
5610                        my %difftree = parse_difftree_raw_line($difftree_line);
5611                        next if !$difftree{'from_id'};
5612
5613                        my $file = $difftree{'file'} || $difftree{'to_file'};
5614
5615                        print "<li>" .
5616                              "[" .
5617                              $cgi->a({-href => href(-full=>1, action=>"blobdiff",
5618                                                     hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
5619                                                     hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
5620                                                     file_name=>$file, file_parent=>$difftree{'from_file'}),
5621                                      -title => "diff"}, 'D');
5622                        if ($have_blame) {
5623                                print $cgi->a({-href => href(-full=>1, action=>"blame",
5624                                                             file_name=>$file, hash_base=>$commit),
5625                                              -title => "blame"}, 'B');
5626                        }
5627                        # if this is not a feed of a file history
5628                        if (!defined $file_name || $file_name ne $file) {
5629                                print $cgi->a({-href => href(-full=>1, action=>"history",
5630                                                             file_name=>$file, hash=>$commit),
5631                                              -title => "history"}, 'H');
5632                        }
5633                        $file = esc_path($file);
5634                        print "] ".
5635                              "$file</li>\n";
5636                }
5637                if ($format eq 'rss') {
5638                        print "</ul>]]>\n" .
5639                              "</content:encoded>\n" .
5640                              "</item>\n";
5641                } elsif ($format eq 'atom') {
5642                        print "</ul>\n</div>\n" .
5643                              "</content>\n" .
5644                              "</entry>\n";
5645                }
5646        }
5647
5648        # end of feed
5649        if ($format eq 'rss') {
5650                print "</channel>\n</rss>\n";
5651        }       elsif ($format eq 'atom') {
5652                print "</feed>\n";
5653        }
5654}
5655
5656sub git_rss {
5657        git_feed('rss');
5658}
5659
5660sub git_atom {
5661        git_feed('atom');
5662}
5663
5664sub git_opml {
5665        my @list = git_get_projects_list();
5666
5667        print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
5668        print <<XML;
5669<?xml version="1.0" encoding="utf-8"?>
5670<opml version="1.0">
5671<head>
5672  <title>$site_name OPML Export</title>
5673</head>
5674<body>
5675<outline text="git RSS feeds">
5676XML
5677
5678        foreach my $pr (@list) {
5679                my %proj = %$pr;
5680                my $head = git_get_head_hash($proj{'path'});
5681                if (!defined $head) {
5682                        next;
5683                }
5684                $git_dir = "$projectroot/$proj{'path'}";
5685                my %co = parse_commit($head);
5686                if (!%co) {
5687                        next;
5688                }
5689
5690                my $path = esc_html(chop_str($proj{'path'}, 25, 5));
5691                my $rss  = "$my_url?p=$proj{'path'};a=rss";
5692                my $html = "$my_url?p=$proj{'path'};a=summary";
5693                print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
5694        }
5695        print <<XML;
5696</outline>
5697</body>
5698</opml>
5699XML
5700}