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