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