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