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