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