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