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