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