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