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