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