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