Added code to recognize signatures (untested)
[gitweb.git] / gitweb.cgi
1 #!/usr/bin/perl
2
3 # gitweb - simple web interface to track changes in git repositories
4 #
5 # (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org>
6 # (C) 2005, Christian Gierke
7 #
8 # This program is licensed under the GPLv2
9
10 use 5.008;
11 use strict;
12 use warnings;
13 use CGI qw(:standard :escapeHTML -nosticky);
14 use CGI::Util qw(unescape);
15 use CGI::Carp qw(fatalsToBrowser set_message);
16 use Encode;
17 use Fcntl ':mode';
18 use File::Find qw();
19 use File::Basename qw(basename);
20 use Time::HiRes qw(gettimeofday tv_interval);
21 binmode STDOUT, ':utf8';
22
23 if (!defined($CGI::VERSION) || $CGI::VERSION < 4.08) {
24 eval 'sub CGI::multi_param { CGI::param(@_) }'
25 }
26
27 our $t0 = [ gettimeofday() ];
28 our $number_of_git_cmds = 0;
29
30 BEGIN {
31 CGI->compile() if $ENV{'MOD_PERL'};
32 }
33
34 our $version = "2.1.4";
35
36 our ($my_url, $my_uri, $base_url, $path_info, $home_link);
37 sub evaluate_uri {
38 our $cgi;
39
40 our $my_url = $cgi->url();
41 our $my_uri = $cgi->url(-absolute => 1);
42
43 # Base URL for relative URLs in gitweb ($logo, $favicon, ...),
44 # needed and used only for URLs with nonempty PATH_INFO
45 our $base_url = $my_url;
46
47 # When the script is used as DirectoryIndex, the URL does not contain the name
48 # of the script file itself, and $cgi->url() fails to strip PATH_INFO, so we
49 # have to do it ourselves. We make $path_info global because it's also used
50 # later on.
51 #
52 # Another issue with the script being the DirectoryIndex is that the resulting
53 # $my_url data is not the full script URL: this is good, because we want
54 # generated links to keep implying the script name if it wasn't explicitly
55 # indicated in the URL we're handling, but it means that $my_url cannot be used
56 # as base URL.
57 # Therefore, if we needed to strip PATH_INFO, then we know that we have
58 # to build the base URL ourselves:
59 our $path_info = decode_utf8($ENV{"PATH_INFO"});
60 if ($path_info) {
61 # $path_info has already been URL-decoded by the web server, but
62 # $my_url and $my_uri have not. URL-decode them so we can properly
63 # strip $path_info.
64 $my_url = unescape($my_url);
65 $my_uri = unescape($my_uri);
66 if ($my_url =~ s,\Q$path_info\E$,, &&
67 $my_uri =~ s,\Q$path_info\E$,, &&
68 defined $ENV{'SCRIPT_NAME'}) {
69 $base_url = $cgi->url(-base => 1) . $ENV{'SCRIPT_NAME'};
70 }
71 }
72
73 # target of the home link on top of all pages
74 our $home_link = $my_uri || "/";
75 }
76
77 # core git executable to use
78 # this can just be "git" if your webserver has a sensible PATH
79 our $GIT = "/usr/bin/git";
80
81 # absolute fs-path which will be prepended to the project path
82 #our $projectroot = "/pub/scm";
83 our $projectroot = "/pub/git";
84
85 # fs traversing limit for getting project list
86 # the number is relative to the projectroot
87 our $project_maxdepth = 2007;
88
89 # string of the home link on top of all pages
90 our $home_link_str = "projects";
91
92 # extra breadcrumbs preceding the home link
93 our @extra_breadcrumbs = ();
94
95 # name of your site or organization to appear in page titles
96 # replace this with something more descriptive for clearer bookmarks
97 our $site_name = ""
98 || ($ENV{'SERVER_NAME'} || "Untitled") . " Git";
99
100 # html snippet to include in the <head> section of each page
101 our $site_html_head_string = "";
102 # filename of html text to include at top of each page
103 our $site_header = "";
104 # html text to include at home page
105 our $home_text = "indextext.html";
106 # filename of html text to include at bottom of each page
107 our $site_footer = "";
108
109 # URI of stylesheets
110 our @stylesheets = ("static/gitweb.css");
111 # URI of a single stylesheet, which can be overridden in GITWEB_CONFIG.
112 our $stylesheet = undef;
113 # URI of GIT logo (72x27 size)
114 our $logo = "static/git-logo.png";
115 # URI of GIT favicon, assumed to be image/png type
116 our $favicon = "static/git-favicon.png";
117 # URI of gitweb.js (JavaScript code for gitweb)
118 our $javascript = "static/gitweb.js";
119
120 # URI and label (title) of GIT logo link
121 #our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
122 #our $logo_label = "git documentation";
123 our $logo_url = "http://git-scm.com/";
124 our $logo_label = "git homepage";
125
126 # source of projects list
127 our $projects_list = "";
128
129 # the width (in characters) of the projects list "Description" column
130 our $projects_list_description_width = 25;
131
132 # group projects by category on the projects list
133 # (enabled if this variable evaluates to true)
134 our $projects_list_group_categories = 0;
135
136 # default category if none specified
137 # (leave the empty string for no category)
138 our $project_list_default_category = "";
139
140 # default order of projects list
141 # valid values are none, project, descr, owner, and age
142 our $default_projects_order = "project";
143
144 # show repository only if this file exists
145 # (only effective if this variable evaluates to true)
146 our $export_ok = "";
147
148 # don't generate age column on the projects list page
149 our $omit_age_column = 0;
150
151 # don't generate information about owners of repositories
152 our $omit_owner=0;
153
154 # show repository only if this subroutine returns true
155 # when given the path to the project, for example:
156 # sub { return -e "$_[0]/git-daemon-export-ok"; }
157 our $export_auth_hook = undef;
158
159 # only allow viewing of repositories also shown on the overview page
160 our $strict_export = "";
161
162 # list of git base URLs used for URL to where fetch project from,
163 # i.e. full URL is "$git_base_url/$project"
164 our @git_base_url_list = grep { $_ ne '' } ("");
165
166 # default blob_plain mimetype and default charset for text/plain blob
167 our $default_blob_plain_mimetype = 'text/plain';
168 our $default_text_plain_charset = undef;
169
170 # file to use for guessing MIME types before trying /etc/mime.types
171 # (relative to the current git repository)
172 our $mimetypes_file = undef;
173
174 # assume this charset if line contains non-UTF-8 characters;
175 # it should be valid encoding (see Encoding::Supported(3pm) for list),
176 # for which encoding all byte sequences are valid, for example
177 # 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
178 # could be even 'utf-8' for the old behavior)
179 our $fallback_encoding = 'latin1';
180
181 # rename detection options for git-diff and git-diff-tree
182 # - default is '-M', with the cost proportional to
183 # (number of removed files) * (number of new files).
184 # - more costly is '-C' (which implies '-M'), with the cost proportional to
185 # (number of changed files + number of removed files) * (number of new files)
186 # - even more costly is '-C', '--find-copies-harder' with cost
187 # (number of files in the original tree) * (number of new files)
188 # - one might want to include '-B' option, e.g. '-B', '-M'
189 our @diff_opts = ('-M'); # taken from git_commit
190
191 # Disables features that would allow repository owners to inject script into
192 # the gitweb domain.
193 our $prevent_xss = 0;
194
195 # Path to the highlight executable to use (must be the one from
196 # http://www.andre-simon.de due to assumptions about parameters and output).
197 # Useful if highlight is not installed on your webserver's PATH.
198 # [Default: highlight]
199 our $highlight_bin = "highlight";
200
201 # information about snapshot formats that gitweb is capable of serving
202 our %known_snapshot_formats = (
203 # name => {
204 # 'display' => display name,
205 # 'type' => mime type,
206 # 'suffix' => filename suffix,
207 # 'format' => --format for git-archive,
208 # 'compressor' => [compressor command and arguments]
209 # (array reference, optional)
210 # 'disabled' => boolean (optional)}
211 #
212 'tgz' => {
213 'display' => 'tar.gz',
214 'type' => 'application/x-gzip',
215 'suffix' => '.tar.gz',
216 'format' => 'tar',
217 'compressor' => ['gzip', '-n']},
218
219 'tbz2' => {
220 'display' => 'tar.bz2',
221 'type' => 'application/x-bzip2',
222 'suffix' => '.tar.bz2',
223 'format' => 'tar',
224 'compressor' => ['bzip2']},
225
226 'txz' => {
227 'display' => 'tar.xz',
228 'type' => 'application/x-xz',
229 'suffix' => '.tar.xz',
230 'format' => 'tar',
231 'compressor' => ['xz'],
232 'disabled' => 1},
233
234 'zip' => {
235 'display' => 'zip',
236 'type' => 'application/x-zip',
237 'suffix' => '.zip',
238 'format' => 'zip'},
239 );
240
241 # Aliases so we understand old gitweb.snapshot values in repository
242 # configuration.
243 our %known_snapshot_format_aliases = (
244 'gzip' => 'tgz',
245 'bzip2' => 'tbz2',
246 'xz' => 'txz',
247
248 # backward compatibility: legacy gitweb config support
249 'x-gzip' => undef, 'gz' => undef,
250 'x-bzip2' => undef, 'bz2' => undef,
251 'x-zip' => undef, '' => undef,
252 );
253
254 # Pixel sizes for icons and avatars. If the default font sizes or lineheights
255 # are changed, it may be appropriate to change these values too via
256 # $GITWEB_CONFIG.
257 our %avatar_size = (
258 'default' => 16,
259 'double' => 32
260 );
261
262 # Used to set the maximum load that we will still respond to gitweb queries.
263 # If server load exceed this value then return "503 server busy" error.
264 # If gitweb cannot determined server load, it is taken to be 0.
265 # Leave it undefined (or set to 'undef') to turn off load checking.
266 our $maxload = 300;
267
268 # configuration for 'highlight' (http://www.andre-simon.de/)
269 # match by basename
270 our %highlight_basename = (
271 #'Program' => 'py',
272 #'Library' => 'py',
273 'SConstruct' => 'py', # SCons equivalent of Makefile
274 'Makefile' => 'make',
275 );
276 # match by extension
277 our %highlight_ext = (
278 # main extensions, defining name of syntax;
279 # see files in /usr/share/highlight/langDefs/ directory
280 (map { $_ => $_ } qw(py rb java css js tex bib xml awk bat ini spec tcl sql)),
281 # alternate extensions, see /etc/highlight/filetypes.conf
282 (map { $_ => 'c' } qw(c h)),
283 (map { $_ => 'sh' } qw(sh bash zsh ksh)),
284 (map { $_ => 'cpp' } qw(cpp cxx c++ cc)),
285 (map { $_ => 'php' } qw(php php3 php4 php5 phps)),
286 (map { $_ => 'pl' } qw(pl perl pm)), # perhaps also 'cgi'
287 (map { $_ => 'make'} qw(make mak mk)),
288 (map { $_ => 'xml' } qw(xml xhtml html htm)),
289 );
290
291 # You define site-wide feature defaults here; override them with
292 # $GITWEB_CONFIG as necessary.
293 our %feature = (
294 # feature => {
295 # 'sub' => feature-sub (subroutine),
296 # 'override' => allow-override (boolean),
297 # 'default' => [ default options...] (array reference)}
298 #
299 # if feature is overridable (it means that allow-override has true value),
300 # then feature-sub will be called with default options as parameters;
301 # return value of feature-sub indicates if to enable specified feature
302 #
303 # if there is no 'sub' key (no feature-sub), then feature cannot be
304 # overridden
305 #
306 # use gitweb_get_feature(<feature>) to retrieve the <feature> value
307 # (an array) or gitweb_check_feature(<feature>) to check if <feature>
308 # is enabled
309
310 # Enable the 'blame' blob view, showing the last commit that modified
311 # each line in the file. This can be very CPU-intensive.
312
313 # To enable system wide have in $GITWEB_CONFIG
314 # $feature{'blame'}{'default'} = [1];
315 # To have project specific config enable override in $GITWEB_CONFIG
316 # $feature{'blame'}{'override'} = 1;
317 # and in project config gitweb.blame = 0|1;
318 'blame' => {
319 'sub' => sub { feature_bool('blame', @_) },
320 'override' => 0,
321 'default' => [0]},
322
323 # Enable the 'snapshot' link, providing a compressed archive of any
324 # tree. This can potentially generate high traffic if you have large
325 # project.
326
327 # Value is a list of formats defined in %known_snapshot_formats that
328 # you wish to offer.
329 # To disable system wide have in $GITWEB_CONFIG
330 # $feature{'snapshot'}{'default'} = [];
331 # To have project specific config enable override in $GITWEB_CONFIG
332 # $feature{'snapshot'}{'override'} = 1;
333 # and in project config, a comma-separated list of formats or "none"
334 # to disable. Example: gitweb.snapshot = tbz2,zip;
335 'snapshot' => {
336 'sub' => \&feature_snapshot,
337 'override' => 0,
338 'default' => ['tgz']},
339
340 # Enable text search, which will list the commits which match author,
341 # committer or commit text to a given string. Enabled by default.
342 # Project specific override is not supported.
343 #
344 # Note that this controls all search features, which means that if
345 # it is disabled, then 'grep' and 'pickaxe' search would also be
346 # disabled.
347 'search' => {
348 'override' => 0,
349 'default' => [1]},
350
351 # Enable grep search, which will list the files in currently selected
352 # tree containing the given string. Enabled by default. This can be
353 # potentially CPU-intensive, of course.
354 # Note that you need to have 'search' feature enabled too.
355
356 # To enable system wide have in $GITWEB_CONFIG
357 # $feature{'grep'}{'default'} = [1];
358 # To have project specific config enable override in $GITWEB_CONFIG
359 # $feature{'grep'}{'override'} = 1;
360 # and in project config gitweb.grep = 0|1;
361 'grep' => {
362 'sub' => sub { feature_bool('grep', @_) },
363 'override' => 0,
364 'default' => [1]},
365
366 # Enable the pickaxe search, which will list the commits that modified
367 # a given string in a file. This can be practical and quite faster
368 # alternative to 'blame', but still potentially CPU-intensive.
369 # Note that you need to have 'search' feature enabled too.
370
371 # To enable system wide have in $GITWEB_CONFIG
372 # $feature{'pickaxe'}{'default'} = [1];
373 # To have project specific config enable override in $GITWEB_CONFIG
374 # $feature{'pickaxe'}{'override'} = 1;
375 # and in project config gitweb.pickaxe = 0|1;
376 'pickaxe' => {
377 'sub' => sub { feature_bool('pickaxe', @_) },
378 'override' => 0,
379 'default' => [1]},
380
381 # Enable showing size of blobs in a 'tree' view, in a separate
382 # column, similar to what 'ls -l' does. This cost a bit of IO.
383
384 # To disable system wide have in $GITWEB_CONFIG
385 # $feature{'show-sizes'}{'default'} = [0];
386 # To have project specific config enable override in $GITWEB_CONFIG
387 # $feature{'show-sizes'}{'override'} = 1;
388 # and in project config gitweb.showsizes = 0|1;
389 'show-sizes' => {
390 'sub' => sub { feature_bool('showsizes', @_) },
391 'override' => 0,
392 'default' => [1]},
393
394 # Make gitweb use an alternative format of the URLs which can be
395 # more readable and natural-looking: project name is embedded
396 # directly in the path and the query string contains other
397 # auxiliary information. All gitweb installations recognize
398 # URL in either format; this configures in which formats gitweb
399 # generates links.
400
401 # To enable system wide have in $GITWEB_CONFIG
402 # $feature{'pathinfo'}{'default'} = [1];
403 # Project specific override is not supported.
404
405 # Note that you will need to change the default location of CSS,
406 # favicon, logo and possibly other files to an absolute URL. Also,
407 # if gitweb.cgi serves as your indexfile, you will need to force
408 # $my_uri to contain the script name in your $GITWEB_CONFIG.
409 'pathinfo' => {
410 'override' => 0,
411 'default' => [0]},
412
413 # Make gitweb consider projects in project root subdirectories
414 # to be forks of existing projects. Given project $projname.git,
415 # projects matching $projname/*.git will not be shown in the main
416 # projects list, instead a '+' mark will be added to $projname
417 # there and a 'forks' view will be enabled for the project, listing
418 # all the forks. If project list is taken from a file, forks have
419 # to be listed after the main project.
420
421 # To enable system wide have in $GITWEB_CONFIG
422 # $feature{'forks'}{'default'} = [1];
423 # Project specific override is not supported.
424 'forks' => {
425 'override' => 0,
426 'default' => [0]},
427
428 # Insert custom links to the action bar of all project pages.
429 # This enables you mainly to link to third-party scripts integrating
430 # into gitweb; e.g. git-browser for graphical history representation
431 # or custom web-based repository administration interface.
432
433 # The 'default' value consists of a list of triplets in the form
434 # (label, link, position) where position is the label after which
435 # to insert the link and link is a format string where %n expands
436 # to the project name, %f to the project path within the filesystem,
437 # %h to the current hash (h gitweb parameter) and %b to the current
438 # hash base (hb gitweb parameter); %% expands to %.
439
440 # To enable system wide have in $GITWEB_CONFIG e.g.
441 # $feature{'actions'}{'default'} = [('graphiclog',
442 # '/git-browser/by-commit.html?r=%n', 'summary')];
443 # Project specific override is not supported.
444 'actions' => {
445 'override' => 0,
446 'default' => []},
447
448 # Allow gitweb scan project content tags of project repository,
449 # and display the popular Web 2.0-ish "tag cloud" near the projects
450 # list. Note that this is something COMPLETELY different from the
451 # normal Git tags.
452
453 # gitweb by itself can show existing tags, but it does not handle
454 # tagging itself; you need to do it externally, outside gitweb.
455 # The format is described in git_get_project_ctags() subroutine.
456 # You may want to install the HTML::TagCloud Perl module to get
457 # a pretty tag cloud instead of just a list of tags.
458
459 # To enable system wide have in $GITWEB_CONFIG
460 # $feature{'ctags'}{'default'} = [1];
461 # Project specific override is not supported.
462
463 # In the future whether ctags editing is enabled might depend
464 # on the value, but using 1 should always mean no editing of ctags.
465 'ctags' => {
466 'override' => 0,
467 'default' => [0]},
468
469 # The maximum number of patches in a patchset generated in patch
470 # view. Set this to 0 or undef to disable patch view, or to a
471 # negative number to remove any limit.
472
473 # To disable system wide have in $GITWEB_CONFIG
474 # $feature{'patches'}{'default'} = [0];
475 # To have project specific config enable override in $GITWEB_CONFIG
476 # $feature{'patches'}{'override'} = 1;
477 # and in project config gitweb.patches = 0|n;
478 # where n is the maximum number of patches allowed in a patchset.
479 'patches' => {
480 'sub' => \&feature_patches,
481 'override' => 0,
482 'default' => [16]},
483
484 # Avatar support. When this feature is enabled, views such as
485 # shortlog or commit will display an avatar associated with
486 # the email of the committer(s) and/or author(s).
487
488 # Currently available providers are gravatar and picon.
489 # If an unknown provider is specified, the feature is disabled.
490
491 # Gravatar depends on Digest::MD5.
492 # Picon currently relies on the indiana.edu database.
493
494 # To enable system wide have in $GITWEB_CONFIG
495 # $feature{'avatar'}{'default'} = ['<provider>'];
496 # where <provider> is either gravatar or picon.
497 # To have project specific config enable override in $GITWEB_CONFIG
498 # $feature{'avatar'}{'override'} = 1;
499 # and in project config gitweb.avatar = <provider>;
500 'avatar' => {
501 'sub' => \&feature_avatar,
502 'override' => 0,
503 'default' => ['']},
504
505 # Enable displaying how much time and how many git commands
506 # it took to generate and display page. Disabled by default.
507 # Project specific override is not supported.
508 'timed' => {
509 'override' => 0,
510 'default' => [0]},
511
512 # Enable turning some links into links to actions which require
513 # JavaScript to run (like 'blame_incremental'). Not enabled by
514 # default. Project specific override is currently not supported.
515 'javascript-actions' => {
516 'override' => 0,
517 'default' => [0]},
518
519 # Enable and configure ability to change common timezone for dates
520 # in gitweb output via JavaScript. Enabled by default.
521 # Project specific override is not supported.
522 'javascript-timezone' => {
523 'override' => 0,
524 'default' => [
525 'local', # default timezone: 'utc', 'local', or '(-|+)HHMM' format,
526 # or undef to turn off this feature
527 'gitweb_tz', # name of cookie where to store selected timezone
528 'datetime', # CSS class used to mark up dates for manipulation
529 ]},
530
531 # Syntax highlighting support. This is based on Daniel Svensson's
532 # and Sham Chukoury's work in gitweb-xmms2.git.
533 # It requires the 'highlight' program present in $PATH,
534 # and therefore is disabled by default.
535
536 # To enable system wide have in $GITWEB_CONFIG
537 # $feature{'highlight'}{'default'} = [1];
538
539 'highlight' => {
540 'sub' => sub { feature_bool('highlight', @_) },
541 'override' => 0,
542 'default' => [0]},
543
544 # Enable displaying of remote heads in the heads list
545
546 # To enable system wide have in $GITWEB_CONFIG
547 # $feature{'remote_heads'}{'default'} = [1];
548 # To have project specific config enable override in $GITWEB_CONFIG
549 # $feature{'remote_heads'}{'override'} = 1;
550 # and in project config gitweb.remoteheads = 0|1;
551 'remote_heads' => {
552 'sub' => sub { feature_bool('remote_heads', @_) },
553 'override' => 0,
554 'default' => [0]},
555
556 # Enable showing branches under other refs in addition to heads
557
558 # To set system wide extra branch refs have in $GITWEB_CONFIG
559 # $feature{'extra-branch-refs'}{'default'} = ['dirs', 'of', 'choice'];
560 # To have project specific config enable override in $GITWEB_CONFIG
561 # $feature{'extra-branch-refs'}{'override'} = 1;
562 # and in project config gitweb.extrabranchrefs = dirs of choice
563 # Every directory is separated with whitespace.
564
565 'extra-branch-refs' => {
566 'sub' => \&feature_extra_branch_refs,
567 'override' => 0,
568 'default' => []},
569 );
570
571 sub gitweb_get_feature {
572 my ($name) = @_;
573 return unless exists $feature{$name};
574 my ($sub, $override, @defaults) = (
575 $feature{$name}{'sub'},
576 $feature{$name}{'override'},
577 @{$feature{$name}{'default'}});
578 # project specific override is possible only if we have project
579 our $git_dir; # global variable, declared later
580 if (!$override || !defined $git_dir) {
581 return @defaults;
582 }
583 if (!defined $sub) {
584 warn "feature $name is not overridable";
585 return @defaults;
586 }
587 return $sub->(@defaults);
588 }
589
590 # A wrapper to check if a given feature is enabled.
591 # With this, you can say
592 #
593 # my $bool_feat = gitweb_check_feature('bool_feat');
594 # gitweb_check_feature('bool_feat') or somecode;
595 #
596 # instead of
597 #
598 # my ($bool_feat) = gitweb_get_feature('bool_feat');
599 # (gitweb_get_feature('bool_feat'))[0] or somecode;
600 #
601 sub gitweb_check_feature {
602 return (gitweb_get_feature(@_))[0];
603 }
604
605
606 sub feature_bool {
607 my $key = shift;
608 my ($val) = git_get_project_config($key, '--bool');
609
610 if (!defined $val) {
611 return ($_[0]);
612 } elsif ($val eq 'true') {
613 return (1);
614 } elsif ($val eq 'false') {
615 return (0);
616 }
617 }
618
619 sub feature_snapshot {
620 my (@fmts) = @_;
621
622 my ($val) = git_get_project_config('snapshot');
623
624 if ($val) {
625 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
626 }
627
628 return @fmts;
629 }
630
631 sub feature_patches {
632 my @val = (git_get_project_config('patches', '--int'));
633
634 if (@val) {
635 return @val;
636 }
637
638 return ($_[0]);
639 }
640
641 sub feature_avatar {
642 my @val = (git_get_project_config('avatar'));
643
644 return @val ? @val : @_;
645 }
646
647 sub feature_extra_branch_refs {
648 my (@branch_refs) = @_;
649 my $values = git_get_project_config('extrabranchrefs');
650
651 if ($values) {
652 $values = config_to_multi ($values);
653 @branch_refs = ();
654 foreach my $value (@{$values}) {
655 push @branch_refs, split /\s+/, $value;
656 }
657 }
658
659 return @branch_refs;
660 }
661
662 # checking HEAD file with -e is fragile if the repository was
663 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
664 # and then pruned.
665 sub check_head_link {
666 my ($dir) = @_;
667 my $headfile = "$dir/HEAD";
668 return ((-e $headfile) ||
669 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
670 }
671
672 sub check_export_ok {
673 my ($dir) = @_;
674 return (check_head_link($dir) &&
675 (!$export_ok || -e "$dir/$export_ok") &&
676 (!$export_auth_hook || $export_auth_hook->($dir)));
677 }
678
679 # process alternate names for backward compatibility
680 # filter out unsupported (unknown) snapshot formats
681 sub filter_snapshot_fmts {
682 my @fmts = @_;
683
684 @fmts = map {
685 exists $known_snapshot_format_aliases{$_} ?
686 $known_snapshot_format_aliases{$_} : $_} @fmts;
687 @fmts = grep {
688 exists $known_snapshot_formats{$_} &&
689 !$known_snapshot_formats{$_}{'disabled'}} @fmts;
690 }
691
692 sub filter_and_validate_refs {
693 my @refs = @_;
694 my %unique_refs = ();
695
696 foreach my $ref (@refs) {
697 die_error(500, "Invalid ref '$ref' in 'extra-branch-refs' feature") unless (is_valid_ref_format($ref));
698 # 'heads' are added implicitly in get_branch_refs().
699 $unique_refs{$ref} = 1 if ($ref ne 'heads');
700 }
701 return sort keys %unique_refs;
702 }
703
704 # If it is set to code reference, it is code that it is to be run once per
705 # request, allowing updating configurations that change with each request,
706 # while running other code in config file only once.
707 #
708 # Otherwise, if it is false then gitweb would process config file only once;
709 # if it is true then gitweb config would be run for each request.
710 our $per_request_config = 1;
711
712 # read and parse gitweb config file given by its parameter.
713 # returns true on success, false on recoverable error, allowing
714 # to chain this subroutine, using first file that exists.
715 # dies on errors during parsing config file, as it is unrecoverable.
716 sub read_config_file {
717 my $filename = shift;
718 return unless defined $filename;
719 # die if there are errors parsing config file
720 if (-e $filename) {
721 do $filename;
722 die $@ if $@;
723 return 1;
724 }
725 return;
726 }
727
728 our ($GITWEB_CONFIG, $GITWEB_CONFIG_SYSTEM, $GITWEB_CONFIG_COMMON);
729 sub evaluate_gitweb_config {
730 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "gitweb_config.perl";
731 our $GITWEB_CONFIG_SYSTEM = $ENV{'GITWEB_CONFIG_SYSTEM'} || "/etc/gitweb.conf";
732 our $GITWEB_CONFIG_COMMON = $ENV{'GITWEB_CONFIG_COMMON'} || "/etc/gitweb-common.conf";
733
734 # Protect against duplications of file names, to not read config twice.
735 # Only one of $GITWEB_CONFIG and $GITWEB_CONFIG_SYSTEM is used, so
736 # there possibility of duplication of filename there doesn't matter.
737 $GITWEB_CONFIG = "" if ($GITWEB_CONFIG eq $GITWEB_CONFIG_COMMON);
738 $GITWEB_CONFIG_SYSTEM = "" if ($GITWEB_CONFIG_SYSTEM eq $GITWEB_CONFIG_COMMON);
739
740 # Common system-wide settings for convenience.
741 # Those settings can be ovverriden by GITWEB_CONFIG or GITWEB_CONFIG_SYSTEM.
742 read_config_file($GITWEB_CONFIG_COMMON);
743
744 # Use first config file that exists. This means use the per-instance
745 # GITWEB_CONFIG if exists, otherwise use GITWEB_SYSTEM_CONFIG.
746 read_config_file($GITWEB_CONFIG) and return;
747 read_config_file($GITWEB_CONFIG_SYSTEM);
748 }
749
750 # Get loadavg of system, to compare against $maxload.
751 # Currently it requires '/proc/loadavg' present to get loadavg;
752 # if it is not present it returns 0, which means no load checking.
753 sub get_loadavg {
754 if( -e '/proc/loadavg' ){
755 open my $fd, '<', '/proc/loadavg'
756 or return 0;
757 my @load = split(/\s+/, scalar <$fd>);
758 close $fd;
759
760 # The first three columns measure CPU and IO utilization of the last one,
761 # five, and 10 minute periods. The fourth column shows the number of
762 # currently running processes and the total number of processes in the m/n
763 # format. The last column displays the last process ID used.
764 return $load[0] || 0;
765 }
766 # additional checks for load average should go here for things that don't export
767 # /proc/loadavg
768
769 return 0;
770 }
771
772 # version of the core git binary
773 our $git_version;
774 sub evaluate_git_version {
775 our $git_version = qx("$GIT" --version) =~ m/git version (.*)$/ ? $1 : "unknown";
776 $number_of_git_cmds++;
777 }
778
779 sub check_loadavg {
780 if (defined $maxload && get_loadavg() > $maxload) {
781 die_error(503, "The load average on the server is too high");
782 }
783 }
784
785 # ======================================================================
786 # input validation and dispatch
787
788 # input parameters can be collected from a variety of sources (presently, CGI
789 # and PATH_INFO), so we define an %input_params hash that collects them all
790 # together during validation: this allows subsequent uses (e.g. href()) to be
791 # agnostic of the parameter origin
792
793 our %input_params = ();
794
795 # input parameters are stored with the long parameter name as key. This will
796 # also be used in the href subroutine to convert parameters to their CGI
797 # equivalent, and since the href() usage is the most frequent one, we store
798 # the name -> CGI key mapping here, instead of the reverse.
799 #
800 # XXX: Warning: If you touch this, check the search form for updating,
801 # too.
802
803 our @cgi_param_mapping = (
804 project => "p",
805 action => "a",
806 file_name => "f",
807 file_parent => "fp",
808 hash => "h",
809 hash_parent => "hp",
810 hash_base => "hb",
811 hash_parent_base => "hpb",
812 page => "pg",
813 order => "o",
814 searchtext => "s",
815 searchtype => "st",
816 snapshot_format => "sf",
817 extra_options => "opt",
818 search_use_regexp => "sr",
819 ctag => "by_tag",
820 diff_style => "ds",
821 project_filter => "pf",
822 # this must be last entry (for manipulation from JavaScript)
823 javascript => "js"
824 );
825 our %cgi_param_mapping = @cgi_param_mapping;
826
827 # we will also need to know the possible actions, for validation
828 our %actions = (
829 "blame" => \&git_blame,
830 "blame_incremental" => \&git_blame_incremental,
831 "blame_data" => \&git_blame_data,
832 "blobdiff" => \&git_blobdiff,
833 "blobdiff_plain" => \&git_blobdiff_plain,
834 "blob" => \&git_blob,
835 "blob_plain" => \&git_blob_plain,
836 "commitdiff" => \&git_commitdiff,
837 "commitdiff_plain" => \&git_commitdiff_plain,
838 "commit" => \&git_commit,
839 "forks" => \&git_forks,
840 "heads" => \&git_heads,
841 "history" => \&git_history,
842 "log" => \&git_log,
843 "patch" => \&git_patch,
844 "patches" => \&git_patches,
845 "remotes" => \&git_remotes,
846 "rss" => \&git_rss,
847 "atom" => \&git_atom,
848 "search" => \&git_search,
849 "search_help" => \&git_search_help,
850 "shortlog" => \&git_shortlog,
851 "summary" => \&git_summary,
852 "tag" => \&git_tag,
853 "tags" => \&git_tags,
854 "tree" => \&git_tree,
855 "snapshot" => \&git_snapshot,
856 "object" => \&git_object,
857 # those below don't need $project
858 "opml" => \&git_opml,
859 "project_list" => \&git_project_list,
860 "project_index" => \&git_project_index,
861 );
862
863 # finally, we have the hash of allowed extra_options for the commands that
864 # allow them
865 our %allowed_options = (
866 "--no-merges" => [ qw(rss atom log shortlog history) ],
867 );
868
869 # fill %input_params with the CGI parameters. All values except for 'opt'
870 # should be single values, but opt can be an array. We should probably
871 # build an array of parameters that can be multi-valued, but since for the time
872 # being it's only this one, we just single it out
873 sub evaluate_query_params {
874 our $cgi;
875
876 while (my ($name, $symbol) = each %cgi_param_mapping) {
877 if ($symbol eq 'opt') {
878 $input_params{$name} = [ map { decode_utf8($_) } $cgi->multi_param($symbol) ];
879 } else {
880 $input_params{$name} = decode_utf8($cgi->param($symbol));
881 }
882 }
883 }
884
885 # now read PATH_INFO and update the parameter list for missing parameters
886 sub evaluate_path_info {
887 return if defined $input_params{'project'};
888 return if !$path_info;
889 $path_info =~ s,^/+,,;
890 return if !$path_info;
891
892 # find which part of PATH_INFO is project
893 my $project = $path_info;
894 $project =~ s,/+$,,;
895 while ($project && !check_head_link("$projectroot/$project")) {
896 $project =~ s,/*[^/]*$,,;
897 }
898 return unless $project;
899 $input_params{'project'} = $project;
900
901 # do not change any parameters if an action is given using the query string
902 return if $input_params{'action'};
903 $path_info =~ s,^\Q$project\E/*,,;
904
905 # next, check if we have an action
906 my $action = $path_info;
907 $action =~ s,/.*$,,;
908 if (exists $actions{$action}) {
909 $path_info =~ s,^$action/*,,;
910 $input_params{'action'} = $action;
911 }
912
913 # list of actions that want hash_base instead of hash, but can have no
914 # pathname (f) parameter
915 my @wants_base = (
916 'tree',
917 'history',
918 );
919
920 # we want to catch, among others
921 # [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name]
922 my ($parentrefname, $parentpathname, $refname, $pathname) =
923 ($path_info =~ /^(?:(.+?)(?::(.+))?\.\.)?([^:]+?)?(?::(.+))?$/);
924
925 # first, analyze the 'current' part
926 if (defined $pathname) {
927 # we got "branch:filename" or "branch:dir/"
928 # we could use git_get_type(branch:pathname), but:
929 # - it needs $git_dir
930 # - it does a git() call
931 # - the convention of terminating directories with a slash
932 # makes it superfluous
933 # - embedding the action in the PATH_INFO would make it even
934 # more superfluous
935 $pathname =~ s,^/+,,;
936 if (!$pathname || substr($pathname, -1) eq "/") {
937 $input_params{'action'} ||= "tree";
938 $pathname =~ s,/$,,;
939 } else {
940 # the default action depends on whether we had parent info
941 # or not
942 if ($parentrefname) {
943 $input_params{'action'} ||= "blobdiff_plain";
944 } else {
945 $input_params{'action'} ||= "blob_plain";
946 }
947 }
948 $input_params{'hash_base'} ||= $refname;
949 $input_params{'file_name'} ||= $pathname;
950 } elsif (defined $refname) {
951 # we got "branch". In this case we have to choose if we have to
952 # set hash or hash_base.
953 #
954 # Most of the actions without a pathname only want hash to be
955 # set, except for the ones specified in @wants_base that want
956 # hash_base instead. It should also be noted that hand-crafted
957 # links having 'history' as an action and no pathname or hash
958 # set will fail, but that happens regardless of PATH_INFO.
959 if (defined $parentrefname) {
960 # if there is parent let the default be 'shortlog' action
961 # (for http://git.example.com/repo.git/A..B links); if there
962 # is no parent, dispatch will detect type of object and set
963 # action appropriately if required (if action is not set)
964 $input_params{'action'} ||= "shortlog";
965 }
966 if ($input_params{'action'} &&
967 grep { $_ eq $input_params{'action'} } @wants_base) {
968 $input_params{'hash_base'} ||= $refname;
969 } else {
970 $input_params{'hash'} ||= $refname;
971 }
972 }
973
974 # next, handle the 'parent' part, if present
975 if (defined $parentrefname) {
976 # a missing pathspec defaults to the 'current' filename, allowing e.g.
977 # someproject/blobdiff/oldrev..newrev:/filename
978 if ($parentpathname) {
979 $parentpathname =~ s,^/+,,;
980 $parentpathname =~ s,/$,,;
981 $input_params{'file_parent'} ||= $parentpathname;
982 } else {
983 $input_params{'file_parent'} ||= $input_params{'file_name'};
984 }
985 # we assume that hash_parent_base is wanted if a path was specified,
986 # or if the action wants hash_base instead of hash
987 if (defined $input_params{'file_parent'} ||
988 grep { $_ eq $input_params{'action'} } @wants_base) {
989 $input_params{'hash_parent_base'} ||= $parentrefname;
990 } else {
991 $input_params{'hash_parent'} ||= $parentrefname;
992 }
993 }
994
995 # for the snapshot action, we allow URLs in the form
996 # $project/snapshot/$hash.ext
997 # where .ext determines the snapshot and gets removed from the
998 # passed $refname to provide the $hash.
999 #
1000 # To be able to tell that $refname includes the format extension, we
1001 # require the following two conditions to be satisfied:
1002 # - the hash input parameter MUST have been set from the $refname part
1003 # of the URL (i.e. they must be equal)
1004 # - the snapshot format MUST NOT have been defined already (e.g. from
1005 # CGI parameter sf)
1006 # It's also useless to try any matching unless $refname has a dot,
1007 # so we check for that too
1008 if (defined $input_params{'action'} &&
1009 $input_params{'action'} eq 'snapshot' &&
1010 defined $refname && index($refname, '.') != -1 &&
1011 $refname eq $input_params{'hash'} &&
1012 !defined $input_params{'snapshot_format'}) {
1013 # We loop over the known snapshot formats, checking for
1014 # extensions. Allowed extensions are both the defined suffix
1015 # (which includes the initial dot already) and the snapshot
1016 # format key itself, with a prepended dot
1017 while (my ($fmt, $opt) = each %known_snapshot_formats) {
1018 my $hash = $refname;
1019 unless ($hash =~ s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) {
1020 next;
1021 }
1022 my $sfx = $1;
1023 # a valid suffix was found, so set the snapshot format
1024 # and reset the hash parameter
1025 $input_params{'snapshot_format'} = $fmt;
1026 $input_params{'hash'} = $hash;
1027 # we also set the format suffix to the one requested
1028 # in the URL: this way a request for e.g. .tgz returns
1029 # a .tgz instead of a .tar.gz
1030 $known_snapshot_formats{$fmt}{'suffix'} = $sfx;
1031 last;
1032 }
1033 }
1034 }
1035
1036 our ($action, $project, $file_name, $file_parent, $hash, $hash_parent, $hash_base,
1037 $hash_parent_base, @extra_options, $page, $searchtype, $search_use_regexp,
1038 $searchtext, $search_regexp, $project_filter);
1039 sub evaluate_and_validate_params {
1040 our $action = $input_params{'action'};
1041 if (defined $action) {
1042 if (!is_valid_action($action)) {
1043 die_error(400, "Invalid action parameter");
1044 }
1045 }
1046
1047 # parameters which are pathnames
1048 our $project = $input_params{'project'};
1049 if (defined $project) {
1050 if (!is_valid_project($project)) {
1051 undef $project;
1052 die_error(404, "No such project");
1053 }
1054 }
1055
1056 our $project_filter = $input_params{'project_filter'};
1057 if (defined $project_filter) {
1058 if (!is_valid_pathname($project_filter)) {
1059 die_error(404, "Invalid project_filter parameter");
1060 }
1061 }
1062
1063 our $file_name = $input_params{'file_name'};
1064 if (defined $file_name) {
1065 if (!is_valid_pathname($file_name)) {
1066 die_error(400, "Invalid file parameter");
1067 }
1068 }
1069
1070 our $file_parent = $input_params{'file_parent'};
1071 if (defined $file_parent) {
1072 if (!is_valid_pathname($file_parent)) {
1073 die_error(400, "Invalid file parent parameter");
1074 }
1075 }
1076
1077 # parameters which are refnames
1078 our $hash = $input_params{'hash'};
1079 if (defined $hash) {
1080 if (!is_valid_refname($hash)) {
1081 die_error(400, "Invalid hash parameter");
1082 }
1083 }
1084
1085 our $hash_parent = $input_params{'hash_parent'};
1086 if (defined $hash_parent) {
1087 if (!is_valid_refname($hash_parent)) {
1088 die_error(400, "Invalid hash parent parameter");
1089 }
1090 }
1091
1092 our $hash_base = $input_params{'hash_base'};
1093 if (defined $hash_base) {
1094 if (!is_valid_refname($hash_base)) {
1095 die_error(400, "Invalid hash base parameter");
1096 }
1097 }
1098
1099 our @extra_options = @{$input_params{'extra_options'}};
1100 # @extra_options is always defined, since it can only be (currently) set from
1101 # CGI, and $cgi->param() returns the empty array in array context if the param
1102 # is not set
1103 foreach my $opt (@extra_options) {
1104 if (not exists $allowed_options{$opt}) {
1105 die_error(400, "Invalid option parameter");
1106 }
1107 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
1108 die_error(400, "Invalid option parameter for this action");
1109 }
1110 }
1111
1112 our $hash_parent_base = $input_params{'hash_parent_base'};
1113 if (defined $hash_parent_base) {
1114 if (!is_valid_refname($hash_parent_base)) {
1115 die_error(400, "Invalid hash parent base parameter");
1116 }
1117 }
1118
1119 # other parameters
1120 our $page = $input_params{'page'};
1121 if (defined $page) {
1122 if ($page =~ m/[^0-9]/) {
1123 die_error(400, "Invalid page parameter");
1124 }
1125 }
1126
1127 our $searchtype = $input_params{'searchtype'};
1128 if (defined $searchtype) {
1129 if ($searchtype =~ m/[^a-z]/) {
1130 die_error(400, "Invalid searchtype parameter");
1131 }
1132 }
1133
1134 our $search_use_regexp = $input_params{'search_use_regexp'};
1135
1136 our $searchtext = $input_params{'searchtext'};
1137 our $search_regexp = undef;
1138 if (defined $searchtext) {
1139 if (length($searchtext) < 2) {
1140 die_error(403, "At least two characters are required for search parameter");
1141 }
1142 if ($search_use_regexp) {
1143 $search_regexp = $searchtext;
1144 if (!eval { qr/$search_regexp/; 1; }) {
1145 (my $error = $@) =~ s/ at \S+ line \d+.*\n?//;
1146 die_error(400, "Invalid search regexp '$search_regexp'",
1147 esc_html($error));
1148 }
1149 } else {
1150 $search_regexp = quotemeta $searchtext;
1151 }
1152 }
1153 }
1154
1155 # path to the current git repository
1156 our $git_dir;
1157 sub evaluate_git_dir {
1158 our $git_dir = "$projectroot/$project" if $project;
1159 }
1160
1161 our (@snapshot_fmts, $git_avatar, @extra_branch_refs);
1162 sub configure_gitweb_features {
1163 # list of supported snapshot formats
1164 our @snapshot_fmts = gitweb_get_feature('snapshot');
1165 @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
1166
1167 # check that the avatar feature is set to a known provider name,
1168 # and for each provider check if the dependencies are satisfied.
1169 # if the provider name is invalid or the dependencies are not met,
1170 # reset $git_avatar to the empty string.
1171 our ($git_avatar) = gitweb_get_feature('avatar');
1172 if ($git_avatar eq 'gravatar') {
1173 $git_avatar = '' unless (eval { require Digest::MD5; 1; });
1174 } elsif ($git_avatar eq 'picon') {
1175 # no dependencies
1176 } else {
1177 $git_avatar = '';
1178 }
1179
1180 our @extra_branch_refs = gitweb_get_feature('extra-branch-refs');
1181 @extra_branch_refs = filter_and_validate_refs (@extra_branch_refs);
1182 }
1183
1184 sub get_branch_refs {
1185 return ('heads', @extra_branch_refs);
1186 }
1187
1188 # custom error handler: 'die <message>' is Internal Server Error
1189 sub handle_errors_html {
1190 my $msg = shift; # it is already HTML escaped
1191
1192 # to avoid infinite loop where error occurs in die_error,
1193 # change handler to default handler, disabling handle_errors_html
1194 set_message("Error occurred when inside die_error:\n$msg");
1195
1196 # you cannot jump out of die_error when called as error handler;
1197 # the subroutine set via CGI::Carp::set_message is called _after_
1198 # HTTP headers are already written, so it cannot write them itself
1199 die_error(undef, undef, $msg, -error_handler => 1, -no_http_header => 1);
1200 }
1201 set_message(\&handle_errors_html);
1202
1203 # dispatch
1204 sub dispatch {
1205 if (!defined $action) {
1206 if (defined $hash) {
1207 $action = git_get_type($hash);
1208 $action or die_error(404, "Object does not exist");
1209 } elsif (defined $hash_base && defined $file_name) {
1210 $action = git_get_type("$hash_base:$file_name");
1211 $action or die_error(404, "File or directory does not exist");
1212 } elsif (defined $project) {
1213 $action = 'summary';
1214 } else {
1215 $action = 'project_list';
1216 }
1217 }
1218 if (!defined($actions{$action})) {
1219 die_error(400, "Unknown action");
1220 }
1221 if ($action !~ m/^(?:opml|project_list|project_index)$/ &&
1222 !$project) {
1223 die_error(400, "Project needed");
1224 }
1225 $actions{$action}->();
1226 }
1227
1228 sub reset_timer {
1229 our $t0 = [ gettimeofday() ]
1230 if defined $t0;
1231 our $number_of_git_cmds = 0;
1232 }
1233
1234 our $first_request = 1;
1235 sub run_request {
1236 reset_timer();
1237
1238 evaluate_uri();
1239 if ($first_request) {
1240 evaluate_gitweb_config();
1241 evaluate_git_version();
1242 }
1243 if ($per_request_config) {
1244 if (ref($per_request_config) eq 'CODE') {
1245 $per_request_config->();
1246 } elsif (!$first_request) {
1247 evaluate_gitweb_config();
1248 }
1249 }
1250 check_loadavg();
1251
1252 # $projectroot and $projects_list might be set in gitweb config file
1253 $projects_list ||= $projectroot;
1254
1255 evaluate_query_params();
1256 evaluate_path_info();
1257 evaluate_and_validate_params();
1258 evaluate_git_dir();
1259
1260 configure_gitweb_features();
1261
1262 dispatch();
1263 }
1264
1265 our $is_last_request = sub { 1 };
1266 our ($pre_dispatch_hook, $post_dispatch_hook, $pre_listen_hook);
1267 our $CGI = 'CGI';
1268 our $cgi;
1269 sub configure_as_fcgi {
1270 require CGI::Fast;
1271 our $CGI = 'CGI::Fast';
1272
1273 my $request_number = 0;
1274 # let each child service 100 requests
1275 our $is_last_request = sub { ++$request_number > 100 };
1276 }
1277 sub evaluate_argv {
1278 my $script_name = $ENV{'SCRIPT_NAME'} || $ENV{'SCRIPT_FILENAME'} || __FILE__;
1279 configure_as_fcgi()
1280 if $script_name =~ /\.fcgi$/;
1281
1282 return unless (@ARGV);
1283
1284 require Getopt::Long;
1285 Getopt::Long::GetOptions(
1286 'fastcgi|fcgi|f' => \&configure_as_fcgi,
1287 'nproc|n=i' => sub {
1288 my ($arg, $val) = @_;
1289 return unless eval { require FCGI::ProcManager; 1; };
1290 my $proc_manager = FCGI::ProcManager->new({
1291 n_processes => $val,
1292 });
1293 our $pre_listen_hook = sub { $proc_manager->pm_manage() };
1294 our $pre_dispatch_hook = sub { $proc_manager->pm_pre_dispatch() };
1295 our $post_dispatch_hook = sub { $proc_manager->pm_post_dispatch() };
1296 },
1297 );
1298 }
1299
1300 sub run {
1301 evaluate_argv();
1302
1303 $first_request = 1;
1304 $pre_listen_hook->()
1305 if $pre_listen_hook;
1306
1307 REQUEST:
1308 while ($cgi = $CGI->new()) {
1309 $pre_dispatch_hook->()
1310 if $pre_dispatch_hook;
1311
1312 run_request();
1313
1314 $post_dispatch_hook->()
1315 if $post_dispatch_hook;
1316 $first_request = 0;
1317
1318 last REQUEST if ($is_last_request->());
1319 }
1320
1321 DONE_GITWEB:
1322 1;
1323 }
1324
1325 run();
1326
1327 if (defined caller) {
1328 # wrapped in a subroutine processing requests,
1329 # e.g. mod_perl with ModPerl::Registry, or PSGI with Plack::App::WrapCGI
1330 return;
1331 } else {
1332 # pure CGI script, serving single request
1333 exit;
1334 }
1335
1336 ## ======================================================================
1337 ## action links
1338
1339 # possible values of extra options
1340 # -full => 0|1 - use absolute/full URL ($my_uri/$my_url as base)
1341 # -replay => 1 - start from a current view (replay with modifications)
1342 # -path_info => 0|1 - don't use/use path_info URL (if possible)
1343 # -anchor => ANCHOR - add #ANCHOR to end of URL, implies -replay if used alone
1344 sub href {
1345 my %params = @_;
1346 # default is to use -absolute url() i.e. $my_uri
1347 my $href = $params{-full} ? $my_url : $my_uri;
1348
1349 # implicit -replay, must be first of implicit params
1350 $params{-replay} = 1 if (keys %params == 1 && $params{-anchor});
1351
1352 $params{'project'} = $project unless exists $params{'project'};
1353
1354 if ($params{-replay}) {
1355 while (my ($name, $symbol) = each %cgi_param_mapping) {
1356 if (!exists $params{$name}) {
1357 $params{$name} = $input_params{$name};
1358 }
1359 }
1360 }
1361
1362 my $use_pathinfo = gitweb_check_feature('pathinfo');
1363 if (defined $params{'project'} &&
1364 (exists $params{-path_info} ? $params{-path_info} : $use_pathinfo)) {
1365 # try to put as many parameters as possible in PATH_INFO:
1366 # - project name
1367 # - action
1368 # - hash_parent or hash_parent_base:/file_parent
1369 # - hash or hash_base:/filename
1370 # - the snapshot_format as an appropriate suffix
1371
1372 # When the script is the root DirectoryIndex for the domain,
1373 # $href here would be something like http://gitweb.example.com/
1374 # Thus, we strip any trailing / from $href, to spare us double
1375 # slashes in the final URL
1376 $href =~ s,/$,,;
1377
1378 # Then add the project name, if present
1379 $href .= "/".esc_path_info($params{'project'});
1380 delete $params{'project'};
1381
1382 # since we destructively absorb parameters, we keep this
1383 # boolean that remembers if we're handling a snapshot
1384 my $is_snapshot = $params{'action'} eq 'snapshot';
1385
1386 # Summary just uses the project path URL, any other action is
1387 # added to the URL
1388 if (defined $params{'action'}) {
1389 $href .= "/".esc_path_info($params{'action'})
1390 unless $params{'action'} eq 'summary';
1391 delete $params{'action'};
1392 }
1393
1394 # Next, we put hash_parent_base:/file_parent..hash_base:/file_name,
1395 # stripping nonexistent or useless pieces
1396 $href .= "/" if ($params{'hash_base'} || $params{'hash_parent_base'}
1397 || $params{'hash_parent'} || $params{'hash'});
1398 if (defined $params{'hash_base'}) {
1399 if (defined $params{'hash_parent_base'}) {
1400 $href .= esc_path_info($params{'hash_parent_base'});
1401 # skip the file_parent if it's the same as the file_name
1402 if (defined $params{'file_parent'}) {
1403 if (defined $params{'file_name'} && $params{'file_parent'} eq $params{'file_name'}) {
1404 delete $params{'file_parent'};
1405 } elsif ($params{'file_parent'} !~ /\.\./) {
1406 $href .= ":/".esc_path_info($params{'file_parent'});
1407 delete $params{'file_parent'};
1408 }
1409 }
1410 $href .= "..";
1411 delete $params{'hash_parent'};
1412 delete $params{'hash_parent_base'};
1413 } elsif (defined $params{'hash_parent'}) {
1414 $href .= esc_path_info($params{'hash_parent'}). "..";
1415 delete $params{'hash_parent'};
1416 }
1417
1418 $href .= esc_path_info($params{'hash_base'});
1419 if (defined $params{'file_name'} && $params{'file_name'} !~ /\.\./) {
1420 $href .= ":/".esc_path_info($params{'file_name'});
1421 delete $params{'file_name'};
1422 }
1423 delete $params{'hash'};
1424 delete $params{'hash_base'};
1425 } elsif (defined $params{'hash'}) {
1426 $href .= esc_path_info($params{'hash'});
1427 delete $params{'hash'};
1428 }
1429
1430 # If the action was a snapshot, we can absorb the
1431 # snapshot_format parameter too
1432 if ($is_snapshot) {
1433 my $fmt = $params{'snapshot_format'};
1434 # snapshot_format should always be defined when href()
1435 # is called, but just in case some code forgets, we
1436 # fall back to the default
1437 $fmt ||= $snapshot_fmts[0];
1438 $href .= $known_snapshot_formats{$fmt}{'suffix'};
1439 delete $params{'snapshot_format'};
1440 }
1441 }
1442
1443 # now encode the parameters explicitly
1444 my @result = ();
1445 for (my $i = 0; $i < @cgi_param_mapping; $i += 2) {
1446 my ($name, $symbol) = ($cgi_param_mapping[$i], $cgi_param_mapping[$i+1]);
1447 if (defined $params{$name}) {
1448 if (ref($params{$name}) eq "ARRAY") {
1449 foreach my $par (@{$params{$name}}) {
1450 push @result, $symbol . "=" . esc_param($par);
1451 }
1452 } else {
1453 push @result, $symbol . "=" . esc_param($params{$name});
1454 }
1455 }
1456 }
1457 $href .= "?" . join(';', @result) if scalar @result;
1458
1459 # final transformation: trailing spaces must be escaped (URI-encoded)
1460 $href =~ s/(\s+)$/CGI::escape($1)/e;
1461
1462 if ($params{-anchor}) {
1463 $href .= "#".esc_param($params{-anchor});
1464 }
1465 # print $href;
1466 return $href;
1467 }
1468
1469
1470 ## ======================================================================
1471 ## validation, quoting/unquoting and escaping
1472
1473 sub is_valid_action {
1474 my $input = shift;
1475 return undef unless exists $actions{$input};
1476 return 1;
1477 }
1478
1479 sub is_valid_project {
1480 my $input = shift;
1481
1482 return unless defined $input;
1483 if (!is_valid_pathname($input) ||
1484 !(-d "$projectroot/$input") ||
1485 !check_export_ok("$projectroot/$input") ||
1486 ($strict_export && !project_in_list($input))) {
1487 return undef;
1488 } else {
1489 return 1;
1490 }
1491 }
1492
1493 sub is_valid_pathname {
1494 my $input = shift;
1495
1496 return undef unless defined $input;
1497 # no '.' or '..' as elements of path, i.e. no '.' or '..'
1498 # at the beginning, at the end, and between slashes.
1499 # also this catches doubled slashes
1500 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
1501 return undef;
1502 }
1503 # no null characters
1504 if ($input =~ m!\0!) {
1505 return undef;
1506 }
1507 return 1;
1508 }
1509
1510 sub is_valid_ref_format {
1511 my $input = shift;
1512
1513 return undef unless defined $input;
1514 # restrictions on ref name according to git-check-ref-format
1515 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
1516 return undef;
1517 }
1518 return 1;
1519 }
1520
1521 sub is_valid_refname {
1522 my $input = shift;
1523
1524 return undef unless defined $input;
1525 # textual hashes are O.K.
1526 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
1527 return 1;
1528 }
1529 # it must be correct pathname
1530 is_valid_pathname($input) or return undef;
1531 # check git-check-ref-format restrictions
1532 is_valid_ref_format($input) or return undef;
1533 return 1;
1534 }
1535
1536 # decode sequences of octets in utf8 into Perl's internal form,
1537 # which is utf-8 with utf8 flag set if needed. gitweb writes out
1538 # in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
1539 sub to_utf8 {
1540 my $str = shift;
1541 return undef unless defined $str;
1542
1543 if (utf8::is_utf8($str) || utf8::decode($str)) {
1544 return $str;
1545 } else {
1546 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
1547 }
1548 }
1549
1550 # quote unsafe chars, but keep the slash, even when it's not
1551 # correct, but quoted slashes look too horrible in bookmarks
1552 sub esc_param {
1553 my $str = shift;
1554 return undef unless defined $str;
1555 $str =~ s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;
1556 $str =~ s/ /\+/g;
1557 return $str;
1558 }
1559
1560 # the quoting rules for path_info fragment are slightly different
1561 sub esc_path_info {
1562 my $str = shift;
1563 return undef unless defined $str;
1564
1565 # path_info doesn't treat '+' as space (specially), but '?' must be escaped
1566 $str =~ s/([^A-Za-z0-9\-_.~();\/;:@&= +]+)/CGI::escape($1)/eg;
1567
1568 return $str;
1569 }
1570
1571 # quote unsafe chars in whole URL, so some characters cannot be quoted
1572 sub esc_url {
1573 my $str = shift;
1574 return undef unless defined $str;
1575 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&= ]+)/CGI::escape($1)/eg;
1576 $str =~ s/ /\+/g;
1577 return $str;
1578 }
1579
1580 # quote unsafe characters in HTML attributes
1581 sub esc_attr {
1582
1583 # for XHTML conformance escaping '"' to '&quot;' is not enough
1584 return esc_html(@_);
1585 }
1586
1587 # replace invalid utf8 character with SUBSTITUTION sequence
1588 sub esc_html {
1589 my $str = shift;
1590 my %opts = @_;
1591
1592 return undef unless defined $str;
1593
1594 $str = to_utf8($str);
1595 $str = $cgi->escapeHTML($str);
1596 if ($opts{'-nbsp'}) {
1597 $str =~ s/ /&nbsp;/g;
1598 }
1599 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
1600 return $str;
1601 }
1602
1603 # quote control characters and escape filename to HTML
1604 sub esc_path {
1605 my $str = shift;
1606 my %opts = @_;
1607
1608 return undef unless defined $str;
1609
1610 $str = to_utf8($str);
1611 $str = $cgi->escapeHTML($str);
1612 if ($opts{'-nbsp'}) {
1613 $str =~ s/ /&nbsp;/g;
1614 }
1615 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
1616 return $str;
1617 }
1618
1619 # Sanitize for use in XHTML + application/xml+xhtm (valid XML 1.0)
1620 sub sanitize {
1621 my $str = shift;
1622
1623 return undef unless defined $str;
1624
1625 $str = to_utf8($str);
1626 $str =~ s|([[:cntrl:]])|(index("\t\n\r", $1) != -1 ? $1 : quot_cec($1))|eg;
1627 return $str;
1628 }
1629
1630 # Make control characters "printable", using character escape codes (CEC)
1631 sub quot_cec {
1632 my $cntrl = shift;
1633 my %opts = @_;
1634 my %es = ( # character escape codes, aka escape sequences
1635 "\t" => '\t', # tab (HT)
1636 "\n" => '\n', # line feed (LF)
1637 "\r" => '\r', # carrige return (CR)
1638 "\f" => '\f', # form feed (FF)
1639 "\b" => '\b', # backspace (BS)
1640 "\a" => '\a', # alarm (bell) (BEL)
1641 "\e" => '\e', # escape (ESC)
1642 "\013" => '\v', # vertical tab (VT)
1643 "\000" => '\0', # nul character (NUL)
1644 );
1645 my $chr = ( (exists $es{$cntrl})
1646 ? $es{$cntrl}
1647 : sprintf('\%2x', ord($cntrl)) );
1648 if ($opts{-nohtml}) {
1649 return $chr;
1650 } else {
1651 return "<span class=\"cntrl\">$chr</span>";
1652 }
1653 }
1654
1655 # Alternatively use unicode control pictures codepoints,
1656 # Unicode "printable representation" (PR)
1657 sub quot_upr {
1658 my $cntrl = shift;
1659 my %opts = @_;
1660
1661 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
1662 if ($opts{-nohtml}) {
1663 return $chr;
1664 } else {
1665 return "<span class=\"cntrl\">$chr</span>";
1666 }
1667 }
1668
1669 # git may return quoted and escaped filenames
1670 sub unquote {
1671 my $str = shift;
1672
1673 sub unq {
1674 my $seq = shift;
1675 my %es = ( # character escape codes, aka escape sequences
1676 't' => "\t", # tab (HT, TAB)
1677 'n' => "\n", # newline (NL)
1678 'r' => "\r", # return (CR)
1679 'f' => "\f", # form feed (FF)
1680 'b' => "\b", # backspace (BS)
1681 'a' => "\a", # alarm (bell) (BEL)
1682 'e' => "\e", # escape (ESC)
1683 'v' => "\013", # vertical tab (VT)
1684 );
1685
1686 if ($seq =~ m/^[0-7]{1,3}$/) {
1687 # octal char sequence
1688 return chr(oct($seq));
1689 } elsif (exists $es{$seq}) {
1690 # C escape sequence, aka character escape code
1691 return $es{$seq};
1692 }
1693 # quoted ordinary character
1694 return $seq;
1695 }
1696
1697 if ($str =~ m/^"(.*)"$/) {
1698 # needs unquoting
1699 $str = $1;
1700 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
1701 }
1702 return $str;
1703 }
1704
1705 # escape tabs (convert tabs to spaces)
1706 sub untabify {
1707 my $line = shift;
1708
1709 while ((my $pos = index($line, "\t")) != -1) {
1710 if (my $count = (8 - ($pos % 8))) {
1711 my $spaces = ' ' x $count;
1712 $line =~ s/\t/$spaces/;
1713 }
1714 }
1715
1716 return $line;
1717 }
1718
1719 sub project_in_list {
1720 my $project = shift;
1721 my @list = git_get_projects_list();
1722 return @list && scalar(grep { $_->{'path'} eq $project } @list);
1723 }
1724
1725 ## ----------------------------------------------------------------------
1726 ## HTML aware string manipulation
1727
1728 # Try to chop given string on a word boundary between position
1729 # $len and $len+$add_len. If there is no word boundary there,
1730 # chop at $len+$add_len. Do not chop if chopped part plus ellipsis
1731 # (marking chopped part) would be longer than given string.
1732 sub chop_str {
1733 my $str = shift;
1734 my $len = shift;
1735 my $add_len = shift || 10;
1736 my $where = shift || 'right'; # 'left' | 'center' | 'right'
1737
1738 # Make sure perl knows it is utf8 encoded so we don't
1739 # cut in the middle of a utf8 multibyte char.
1740 $str = to_utf8($str);
1741
1742 # allow only $len chars, but don't cut a word if it would fit in $add_len
1743 # if it doesn't fit, cut it if it's still longer than the dots we would add
1744 # remove chopped character entities entirely
1745
1746 # when chopping in the middle, distribute $len into left and right part
1747 # return early if chopping wouldn't make string shorter
1748 if ($where eq 'center') {
1749 return $str if ($len + 5 >= length($str)); # filler is length 5
1750 $len = int($len/2);
1751 } else {
1752 return $str if ($len + 4 >= length($str)); # filler is length 4
1753 }
1754
1755 # regexps: ending and beginning with word part up to $add_len
1756 my $endre = qr/.{$len}\w{0,$add_len}/;
1757 my $begre = qr/\w{0,$add_len}.{$len}/;
1758
1759 if ($where eq 'left') {
1760 $str =~ m/^(.*?)($begre)$/;
1761 my ($lead, $body) = ($1, $2);
1762 if (length($lead) > 4) {
1763 $lead = " ...";
1764 }
1765 return "$lead$body";
1766
1767 } elsif ($where eq 'center') {
1768 $str =~ m/^($endre)(.*)$/;
1769 my ($left, $str) = ($1, $2);
1770 $str =~ m/^(.*?)($begre)$/;
1771 my ($mid, $right) = ($1, $2);
1772 if (length($mid) > 5) {
1773 $mid = " ... ";
1774 }
1775 return "$left$mid$right";
1776
1777 } else {
1778 $str =~ m/^($endre)(.*)$/;
1779 my $body = $1;
1780 my $tail = $2;
1781 if (length($tail) > 4) {
1782 $tail = "... ";
1783 }
1784 return "$body$tail";
1785 }
1786 }
1787
1788 # takes the same arguments as chop_str, but also wraps a <span> around the
1789 # result with a title attribute if it does get chopped. Additionally, the
1790 # string is HTML-escaped.
1791 sub chop_and_escape_str {
1792 my ($str) = @_;
1793
1794 my $chopped = chop_str(@_);
1795 $str = to_utf8($str);
1796 if ($chopped eq $str) {
1797 return esc_html($chopped);
1798 } else {
1799 $str =~ s/[[:cntrl:]]/?/g;
1800 return $cgi->span({-title=>$str}, esc_html($chopped));
1801 }
1802 }
1803
1804 # Highlight selected fragments of string, using given CSS class,
1805 # and escape HTML. It is assumed that fragments do not overlap.
1806 # Regions are passed as list of pairs (array references).
1807 #
1808 # Example: esc_html_hl_regions("foobar", "mark", [ 0, 3 ]) returns
1809 # '<span class="mark">foo</span>bar'
1810 sub esc_html_hl_regions {
1811 my ($str, $css_class, @sel) = @_;
1812 my %opts = grep { ref($_) ne 'ARRAY' } @sel;
1813 @sel = grep { ref($_) eq 'ARRAY' } @sel;
1814 return esc_html($str, %opts) unless @sel;
1815
1816 my $out = '';
1817 my $pos = 0;
1818
1819 for my $s (@sel) {
1820 my ($begin, $end) = @$s;
1821
1822 # Don't create empty <span> elements.
1823 next if $end <= $begin;
1824
1825 my $escaped = esc_html(substr($str, $begin, $end - $begin),
1826 %opts);
1827
1828 $out .= esc_html(substr($str, $pos, $begin - $pos), %opts)
1829 if ($begin - $pos > 0);
1830 $out .= $cgi->span({-class => $css_class}, $escaped);
1831
1832 $pos = $end;
1833 }
1834 $out .= esc_html(substr($str, $pos), %opts)
1835 if ($pos < length($str));
1836
1837 return $out;
1838 }
1839
1840 # return positions of beginning and end of each match
1841 sub matchpos_list {
1842 my ($str, $regexp) = @_;
1843 return unless (defined $str && defined $regexp);
1844
1845 my @matches;
1846 while ($str =~ /$regexp/g) {
1847 push @matches, [$-[0], $+[0]];
1848 }
1849 return @matches;
1850 }
1851
1852 # highlight match (if any), and escape HTML
1853 sub esc_html_match_hl {
1854 my ($str, $regexp) = @_;
1855 return esc_html($str) unless defined $regexp;
1856
1857 my @matches = matchpos_list($str, $regexp);
1858 return esc_html($str) unless @matches;
1859
1860 return esc_html_hl_regions($str, 'match', @matches);
1861 }
1862
1863
1864 # highlight match (if any) of shortened string, and escape HTML
1865 sub esc_html_match_hl_chopped {
1866 my ($str, $chopped, $regexp) = @_;
1867 return esc_html_match_hl($str, $regexp) unless defined $chopped;
1868
1869 my @matches = matchpos_list($str, $regexp);
1870 return esc_html($chopped) unless @matches;
1871
1872 # filter matches so that we mark chopped string
1873 my $tail = "... "; # see chop_str
1874 unless ($chopped =~ s/\Q$tail\E$//) {
1875 $tail = '';
1876 }
1877 my $chop_len = length($chopped);
1878 my $tail_len = length($tail);
1879 my @filtered;
1880
1881 for my $m (@matches) {
1882 if ($m->[0] > $chop_len) {
1883 push @filtered, [ $chop_len, $chop_len + $tail_len ] if ($tail_len > 0);
1884 last;
1885 } elsif ($m->[1] > $chop_len) {
1886 push @filtered, [ $m->[0], $chop_len + $tail_len ];
1887 last;
1888 }
1889 push @filtered, $m;
1890 }
1891
1892 return esc_html_hl_regions($chopped . $tail, 'match', @filtered);
1893 }
1894
1895 ## ----------------------------------------------------------------------
1896 ## functions returning short strings
1897
1898 # CSS class for given age value (in seconds)
1899 sub age_class {
1900 my $age = shift;
1901
1902 if (!defined $age) {
1903 return "noage";
1904 } elsif ($age < 60*60*2) {
1905 return "age0";
1906 } elsif ($age < 60*60*24*2) {
1907 return "age1";
1908 } else {
1909 return "age2";
1910 }
1911 }
1912
1913 # convert age in seconds to "nn units ago" string
1914 sub age_string {
1915 my $age = shift;
1916 my $age_str;
1917
1918 if ($age > 60*60*24*365*2) {
1919 $age_str = (int $age/60/60/24/365);
1920 $age_str .= " years ago";
1921 } elsif ($age > 60*60*24*(365/12)*2) {
1922 $age_str = int $age/60/60/24/(365/12);
1923 $age_str .= " months ago";
1924 } elsif ($age > 60*60*24*7*2) {
1925 $age_str = int $age/60/60/24/7;
1926 $age_str .= " weeks ago";
1927 } elsif ($age > 60*60*24*2) {
1928 $age_str = int $age/60/60/24;
1929 $age_str .= " days ago";
1930 } elsif ($age > 60*60*2) {
1931 $age_str = int $age/60/60;
1932 $age_str .= " hours ago";
1933 } elsif ($age > 60*2) {
1934 $age_str = int $age/60;
1935 $age_str .= " min ago";
1936 } elsif ($age > 2) {
1937 $age_str = int $age;
1938 $age_str .= " sec ago";
1939 } else {
1940 $age_str .= " right now";
1941 }
1942 return $age_str;
1943 }
1944
1945 use constant {
1946 S_IFINVALID => 0030000,
1947 S_IFGITLINK => 0160000,
1948 };
1949
1950 # submodule/subproject, a commit object reference
1951 sub S_ISGITLINK {
1952 my $mode = shift;
1953
1954 return (($mode & S_IFMT) == S_IFGITLINK)
1955 }
1956
1957 # convert file mode in octal to symbolic file mode string
1958 sub mode_str {
1959 my $mode = oct shift;
1960
1961 if (S_ISGITLINK($mode)) {
1962 return 'm---------';
1963 } elsif (S_ISDIR($mode & S_IFMT)) {
1964 return 'drwxr-xr-x';
1965 } elsif (S_ISLNK($mode)) {
1966 return 'lrwxrwxrwx';
1967 } elsif (S_ISREG($mode)) {
1968 # git cares only about the executable bit
1969 if ($mode & S_IXUSR) {
1970 return '-rwxr-xr-x';
1971 } else {
1972 return '-rw-r--r--';
1973 };
1974 } else {
1975 return '----------';
1976 }
1977 }
1978
1979 # convert file mode in octal to file type string
1980 sub file_type {
1981 my $mode = shift;
1982
1983 if ($mode !~ m/^[0-7]+$/) {
1984 return $mode;
1985 } else {
1986 $mode = oct $mode;
1987 }
1988
1989 if (S_ISGITLINK($mode)) {
1990 return "submodule";
1991 } elsif (S_ISDIR($mode & S_IFMT)) {
1992 return "directory";
1993 } elsif (S_ISLNK($mode)) {
1994 return "symlink";
1995 } elsif (S_ISREG($mode)) {
1996 return "file";
1997 } else {
1998 return "unknown";
1999 }
2000 }
2001
2002 # convert file mode in octal to file type description string
2003 sub file_type_long {
2004 my $mode = shift;
2005
2006 if ($mode !~ m/^[0-7]+$/) {
2007 return $mode;
2008 } else {
2009 $mode = oct $mode;
2010 }
2011
2012 if (S_ISGITLINK($mode)) {
2013 return "submodule";
2014 } elsif (S_ISDIR($mode & S_IFMT)) {
2015 return "directory";
2016 } elsif (S_ISLNK($mode)) {
2017 return "symlink";
2018 } elsif (S_ISREG($mode)) {
2019 if ($mode & S_IXUSR) {
2020 return "executable";
2021 } else {
2022 return "file";
2023 };
2024 } else {
2025 return "unknown";
2026 }
2027 }
2028
2029
2030 ## ----------------------------------------------------------------------
2031 ## functions returning short HTML fragments, or transforming HTML fragments
2032 ## which don't belong to other sections
2033
2034 # format line of commit message.
2035 sub format_log_line_html {
2036 my $line = shift;
2037
2038 $line = esc_html($line, -nbsp=>1);
2039 $line =~ s{\b([0-9a-fA-F]{8,40})\b}{
2040 $cgi->a({-href => href(action=>"object", hash=>$1),
2041 -class => "text"}, $1);
2042 }eg;
2043
2044 return $line;
2045 }
2046
2047 # format marker of refs pointing to given object
2048
2049 # the destination action is chosen based on object type and current context:
2050 # - for annotated tags, we choose the tag view unless it's the current view
2051 # already, in which case we go to shortlog view
2052 # - for other refs, we keep the current view if we're in history, shortlog or
2053 # log view, and select shortlog otherwise
2054 sub format_ref_marker {
2055 my ($refs, $id) = @_;
2056 my $markers = '';
2057
2058 if (defined $refs->{$id}) {
2059 foreach my $ref (@{$refs->{$id}}) {
2060 # this code exploits the fact that non-lightweight tags are the
2061 # only indirect objects, and that they are the only objects for which
2062 # we want to use tag instead of shortlog as action
2063 my ($type, $name) = qw();
2064 my $indirect = ($ref =~ s/\^\{\}$//);
2065 # e.g. tags/v2.6.11 or heads/next
2066 if ($ref =~ m!^(.*?)s?/(.*)$!) {
2067 $type = $1;
2068 $name = $2;
2069 } else {
2070 $type = "ref";
2071 $name = $ref;
2072 }
2073
2074 my $class = $type;
2075 $class .= " indirect" if $indirect;
2076
2077 my $dest_action = "shortlog";
2078
2079 if ($indirect) {
2080 $dest_action = "tag" unless $action eq "tag";
2081 } elsif ($action =~ /^(history|(short)?log)$/) {
2082 $dest_action = $action;
2083 }
2084
2085 my $dest = "";
2086 $dest .= "refs/" unless $ref =~ m!^refs/!;
2087 $dest .= $ref;
2088
2089 my $link = $cgi->a({
2090 -href => href(
2091 action=>$dest_action,
2092 hash=>$dest
2093 )}, $name);
2094
2095 $markers .= " <span
2096 class=\"".esc_attr($class)."\"
2097 title=\"".esc_attr($ref)."\">" . '<span class="octicon
2098 octicon-git-branch"></span>'.$link . "</span>";
2099 }
2100 }
2101
2102 if ($markers) {
2103 return ' <span class="refs">'. $markers . '</span>';
2104 } else {
2105 return "";
2106 }
2107 }
2108
2109 # format, perhaps shortened and with markers, title line
2110 sub format_subject_html {
2111 my ($long, $short, $href, $extra) = @_;
2112 $extra = '' unless defined($extra);
2113
2114 if (length($short) < length($long)) {
2115 $long =~ s/[[:cntrl:]]/?/g;
2116 return $cgi->a({-href => $href, -class => "list subject",
2117 -title => to_utf8($long)},
2118 esc_html($short)) . $extra;
2119 } else {
2120 return $cgi->a({-href => $href, -class => "list subject"},
2121 esc_html($long)) . $extra;
2122 }
2123 }
2124
2125 # Rather than recomputing the url for an email multiple times, we cache it
2126 # after the first hit. This gives a visible benefit in views where the avatar
2127 # for the same email is used repeatedly (e.g. shortlog).
2128 # The cache is shared by all avatar engines (currently gravatar only), which
2129 # are free to use it as preferred. Since only one avatar engine is used for any
2130 # given page, there's no risk for cache conflicts.
2131 our %avatar_cache = ();
2132
2133 # Compute the picon url for a given email, by using the picon search service over at
2134 # http://www.cs.indiana.edu/picons/search.html
2135 sub picon_url {
2136 my $email = lc shift;
2137 if (!$avatar_cache{$email}) {
2138 my ($user, $domain) = split('@', $email);
2139 $avatar_cache{$email} =
2140 "//www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/" .
2141 "$domain/$user/" .
2142 "users+domains+unknown/up/single";
2143 }
2144 return $avatar_cache{$email};
2145 }
2146
2147 # Compute the gravatar url for a given email, if it's not in the cache already.
2148 # Gravatar stores only the part of the URL before the size, since that's the
2149 # one computationally more expensive. This also allows reuse of the cache for
2150 # different sizes (for this particular engine).
2151 sub gravatar_url {
2152 my $email = lc shift;
2153 my $size = shift;
2154 $avatar_cache{$email} ||=
2155 "//www.gravatar.com/avatar/" .
2156 Digest::MD5::md5_hex($email) . "?s=";
2157 return $avatar_cache{$email} . $size;
2158 }
2159
2160 # Insert an avatar for the given $email at the given $size if the feature
2161 # is enabled.
2162 sub git_get_avatar {
2163 my ($email, %opts) = @_;
2164 my $pre_white = ($opts{-pad_before} ? "&nbsp;" : "");
2165 my $post_white = ($opts{-pad_after} ? "&nbsp;" : "");
2166 $opts{-size} ||= 'default';
2167 my $size = $avatar_size{$opts{-size}} || $avatar_size{'default'};
2168 my $url = "";
2169 if ($git_avatar eq 'gravatar') {
2170 $url = gravatar_url($email, $size);
2171 } elsif ($git_avatar eq 'picon') {
2172 $url = picon_url($email);
2173 }
2174 # Other providers can be added by extending the if chain, defining $url
2175 # as needed. If no variant puts something in $url, we assume avatars
2176 # are completely disabled/unavailable.
2177 if ($url) {
2178 return $pre_white .
2179 "<img width=\"$size\" " .
2180 "class=\"avatar\" " .
2181 "src=\"".esc_url($url)."\" " .
2182 "alt=\"\" " .
2183 "/>" . $post_white;
2184 } else {
2185 return "";
2186 }
2187 }
2188
2189 sub format_search_author {
2190 my ($author, $searchtype, $displaytext) = @_;
2191 my $have_search = gitweb_check_feature('search');
2192
2193 if ($have_search) {
2194 my $performed = "";
2195 if ($searchtype eq 'author') {
2196 $performed = "authored";
2197 } elsif ($searchtype eq 'committer') {
2198 $performed = "committed";
2199 }
2200
2201 return $cgi->a({-href => href(action=>"search", hash=>$hash,
2202 searchtext=>$author,
2203 searchtype=>$searchtype), class=>"list",
2204 title=>"Search for commits $performed by $author"},
2205 $displaytext);
2206
2207 } else {
2208 return $displaytext;
2209 }
2210 }
2211
2212 # format the author name of the given commit with the given tag
2213 # the author name is chopped and escaped according to the other
2214 # optional parameters (see chop_str).
2215 sub format_author_html {
2216 my $tag = shift;
2217 my $co = shift;
2218 my $author = chop_and_escape_str($co->{'author_name'}, @_);
2219 return "<$tag class=\"author\">" .
2220 format_search_author($co->{'author_name'}, "author",
2221 git_get_avatar($co->{'author_email'}, -pad_after => 1) .
2222 $author) .
2223 "</$tag>";
2224 }
2225
2226 # format git diff header line, i.e. "diff --(git|combined|cc) ..."
2227 sub format_git_diff_header_line {
2228 my $line = shift;
2229 my $diffinfo = shift;
2230 my ($from, $to) = @_;
2231
2232 if ($diffinfo->{'nparents'}) {
2233 # combined diff
2234 $line =~ s!^(diff (.*?) )"?.*$!$1!;
2235 if ($to->{'href'}) {
2236 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
2237 esc_path($to->{'file'}));
2238 } else { # file was deleted (no href)
2239 $line .= esc_path($to->{'file'});
2240 }
2241 } else {
2242 # "ordinary" diff
2243 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
2244 if ($from->{'href'}) {
2245 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
2246 'a/' . esc_path($from->{'file'}));
2247 } else { # file was added (no href)
2248 $line .= 'a/' . esc_path($from->{'file'});
2249 }
2250 $line .= ' ';
2251 if ($to->{'href'}) {
2252 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
2253 'b/' . esc_path($to->{'file'}));
2254 } else { # file was deleted
2255 $line .= 'b/' . esc_path($to->{'file'});
2256 }
2257 }
2258
2259 return "<div class=\"diff header\">$line</div>\n";
2260 }
2261
2262 # format extended diff header line, before patch itself
2263 sub format_extended_diff_header_line {
2264 my $line = shift;
2265 my $diffinfo = shift;
2266 my ($from, $to) = @_;
2267
2268 # match <path>
2269 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
2270 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
2271 esc_path($from->{'file'}));
2272 }
2273 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
2274 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
2275 esc_path($to->{'file'}));
2276 }
2277 # match single <mode>
2278 if ($line =~ m/\s(\d{6})$/) {
2279 $line .= '<span class="info"> (' .
2280 file_type_long($1) .
2281 ')</span>';
2282 }
2283 # match <hash>
2284 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
2285 # can match only for combined diff
2286 $line = 'index ';
2287 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2288 if ($from->{'href'}[$i]) {
2289 $line .= $cgi->a({-href=>$from->{'href'}[$i],
2290 -class=>"hash"},
2291 substr($diffinfo->{'from_id'}[$i],0,7));
2292 } else {
2293 $line .= '0' x 7;
2294 }
2295 # separator
2296 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
2297 }
2298 $line .= '..';
2299 if ($to->{'href'}) {
2300 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
2301 substr($diffinfo->{'to_id'},0,7));
2302 } else {
2303 $line .= '0' x 7;
2304 }
2305
2306 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
2307 # can match only for ordinary diff
2308 my ($from_link, $to_link);
2309 if ($from->{'href'}) {
2310 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
2311 substr($diffinfo->{'from_id'},0,7));
2312 } else {
2313 $from_link = '0' x 7;
2314 }
2315 if ($to->{'href'}) {
2316 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
2317 substr($diffinfo->{'to_id'},0,7));
2318 } else {
2319 $to_link = '0' x 7;
2320 }
2321 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
2322 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
2323 }
2324
2325 return $line . "<br/>\n";
2326 }
2327
2328 # format from-file/to-file diff header
2329 sub format_diff_from_to_header {
2330 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
2331 my $line;
2332 my $result = '';
2333
2334 $line = $from_line;
2335 #assert($line =~ m/^---/) if DEBUG;
2336 # no extra formatting for "^--- /dev/null"
2337 if (! $diffinfo->{'nparents'}) {
2338 # ordinary (single parent) diff
2339 if ($line =~ m!^--- "?a/!) {
2340 if ($from->{'href'}) {
2341 $line = '--- a/' .
2342 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
2343 esc_path($from->{'file'}));
2344 } else {
2345 $line = '--- a/' .
2346 esc_path($from->{'file'});
2347 }
2348 }
2349 $result .= qq!<div class="diff from_file">$line</div>\n!;
2350
2351 } else {
2352 # combined diff (merge commit)
2353 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2354 if ($from->{'href'}[$i]) {
2355 $line = '--- ' .
2356 $cgi->a({-href=>href(action=>"blobdiff",
2357 hash_parent=>$diffinfo->{'from_id'}[$i],
2358 hash_parent_base=>$parents[$i],
2359 file_parent=>$from->{'file'}[$i],
2360 hash=>$diffinfo->{'to_id'},
2361 hash_base=>$hash,
2362 file_name=>$to->{'file'}),
2363 -class=>"path",
2364 -title=>"diff" . ($i+1)},
2365 $i+1) .
2366 '/' .
2367 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
2368 esc_path($from->{'file'}[$i]));
2369 } else {
2370 $line = '--- /dev/null';
2371 }
2372 $result .= qq!<div class="diff from_file">$line</div>\n!;
2373 }
2374 }
2375
2376 $line = $to_line;
2377 #assert($line =~ m/^\+\+\+/) if DEBUG;
2378 # no extra formatting for "^+++ /dev/null"
2379 if ($line =~ m!^\+\+\+ "?b/!) {
2380 if ($to->{'href'}) {
2381 $line = '+++ b/' .
2382 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
2383 esc_path($to->{'file'}));
2384 } else {
2385 $line = '+++ b/' .
2386 esc_path($to->{'file'});
2387 }
2388 }
2389 $result .= qq!<div class="diff to_file">$line</div>\n!;
2390
2391 return $result;
2392 }
2393
2394 # create note for patch simplified by combined diff
2395 sub format_diff_cc_simplified {
2396 my ($diffinfo, @parents) = @_;
2397 my $result = '';
2398
2399 $result .= "<div class=\"diff header\">" .
2400 "diff --cc ";
2401 if (!is_deleted($diffinfo)) {
2402 $result .= $cgi->a({-href => href(action=>"blob",
2403 hash_base=>$hash,
2404 hash=>$diffinfo->{'to_id'},
2405 file_name=>$diffinfo->{'to_file'}),
2406 -class => "path"},
2407 esc_path($diffinfo->{'to_file'}));
2408 } else {
2409 $result .= esc_path($diffinfo->{'to_file'});
2410 }
2411 $result .= "</div>\n" . # class="diff header"
2412 "<div class=\"diff nodifferences\">" .
2413 "Simple merge" .
2414 "</div>\n"; # class="diff nodifferences"
2415
2416 return $result;
2417 }
2418
2419 sub diff_line_class {
2420 my ($line, $from, $to) = @_;
2421
2422 # ordinary diff
2423 my $num_sign = 1;
2424 # combined diff
2425 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
2426 $num_sign = scalar @{$from->{'href'}};
2427 }
2428
2429 my @diff_line_classifier = (
2430 { regexp => qr/^\@\@{$num_sign} /, class => "chunk_header"},
2431 { regexp => qr/^\\/, class => "incomplete" },
2432 { regexp => qr/^ {$num_sign}/, class => "ctx" },
2433 # classifier for context must come before classifier add/rem,
2434 # or we would have to use more complicated regexp, for example
2435 # qr/(?= {0,$m}\+)[+ ]{$num_sign}/, where $m = $num_sign - 1;
2436 { regexp => qr/^[+ ]{$num_sign}/, class => "add" },
2437 { regexp => qr/^[- ]{$num_sign}/, class => "rem" },
2438 );
2439 for my $clsfy (@diff_line_classifier) {
2440 return $clsfy->{'class'}
2441 if ($line =~ $clsfy->{'regexp'});
2442 }
2443
2444 # fallback
2445 return "";
2446 }
2447
2448 # assumes that $from and $to are defined and correctly filled,
2449 # and that $line holds a line of chunk header for unified diff
2450 sub format_unidiff_chunk_header {
2451 my ($line, $from, $to) = @_;
2452
2453 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
2454 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
2455
2456 $from_lines = 0 unless defined $from_lines;
2457 $to_lines = 0 unless defined $to_lines;
2458
2459 if ($from->{'href'}) {
2460 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
2461 -class=>"list"}, $from_text);
2462 }
2463 if ($to->{'href'}) {
2464 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
2465 -class=>"list"}, $to_text);
2466 }
2467 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
2468 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
2469 return $line;
2470 }
2471
2472 # assumes that $from and $to are defined and correctly filled,
2473 # and that $line holds a line of chunk header for combined diff
2474 sub format_cc_diff_chunk_header {
2475 my ($line, $from, $to) = @_;
2476
2477 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
2478 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
2479
2480 @from_text = split(' ', $ranges);
2481 for (my $i = 0; $i < @from_text; ++$i) {
2482 ($from_start[$i], $from_nlines[$i]) =
2483 (split(',', substr($from_text[$i], 1)), 0);
2484 }
2485
2486 $to_text = pop @from_text;
2487 $to_start = pop @from_start;
2488 $to_nlines = pop @from_nlines;
2489
2490 $line = "<span class=\"chunk_info\">$prefix ";
2491 for (my $i = 0; $i < @from_text; ++$i) {
2492 if ($from->{'href'}[$i]) {
2493 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
2494 -class=>"list"}, $from_text[$i]);
2495 } else {
2496 $line .= $from_text[$i];
2497 }
2498 $line .= " ";
2499 }
2500 if ($to->{'href'}) {
2501 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
2502 -class=>"list"}, $to_text);
2503 } else {
2504 $line .= $to_text;
2505 }
2506 $line .= " $prefix</span>" .
2507 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
2508 return $line;
2509 }
2510
2511 # process patch (diff) line (not to be used for diff headers),
2512 # returning HTML-formatted (but not wrapped) line.
2513 # If the line is passed as a reference, it is treated as HTML and not
2514 # esc_html()'ed.
2515 sub format_diff_line {
2516 my ($line, $diff_class, $from, $to) = @_;
2517
2518 if (ref($line)) {
2519 $line = $$line;
2520 } else {
2521 chomp $line;
2522 $line = untabify($line);
2523
2524 if ($from && $to && $line =~ m/^\@{2} /) {
2525 $line = format_unidiff_chunk_header($line, $from, $to);
2526 } elsif ($from && $to && $line =~ m/^\@{3}/) {
2527 $line = format_cc_diff_chunk_header($line, $from, $to);
2528 } else {
2529 $line = esc_html($line, -nbsp=>1);
2530 }
2531 }
2532
2533 my $diff_classes = "diff";
2534 $diff_classes .= " $diff_class" if ($diff_class);
2535 $line = "<div class=\"$diff_classes\">$line</div>\n";
2536
2537 return $line;
2538 }
2539
2540 # Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
2541 # linked. Pass the hash of the tree/commit to snapshot.
2542 sub format_snapshot_links {
2543 my ($hash) = @_;
2544 my $num_fmts = @snapshot_fmts;
2545 if ($num_fmts > 1) {
2546 # A parenthesized list of links bearing format names.
2547 # e.g. "snapshot (_tar.gz_ _zip_)"
2548 return "snapshot (" . join('&bull;', map
2549 $cgi->a({
2550 -href => href(
2551 action=>"snapshot",
2552 hash=>$hash,
2553 snapshot_format=>$_
2554 )
2555 },'<span class="octicon octicon-package"></span> '. $known_snapshot_formats{$_}{'display'})
2556 , @snapshot_fmts) . ")";
2557 } elsif ($num_fmts == 1) {
2558 # A single "snapshot" link whose tooltip bears the format name.
2559 # i.e. "_snapshot_"
2560 my ($fmt) = @snapshot_fmts;
2561 return
2562 $cgi->a({
2563 -href => href(
2564 action=>"snapshot",
2565 hash=>$hash,
2566 snapshot_format=>$fmt
2567 ),
2568 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
2569 }, '<span class="octicon octicon-package"></span> '. "snapshot");
2570 } else { # $num_fmts == 0
2571 return undef;
2572 }
2573 }
2574
2575 ## ......................................................................
2576 ## functions returning values to be passed, perhaps after some
2577 ## transformation, to other functions; e.g. returning arguments to href()
2578
2579 # returns hash to be passed to href to generate gitweb URL
2580 # in -title key it returns description of link
2581 sub get_feed_info {
2582 my $format = shift || 'Atom';
2583 my %res = (action => lc($format));
2584 my $matched_ref = 0;
2585
2586 # feed links are possible only for project views
2587 return unless (defined $project);
2588 # some views should link to OPML, or to generic project feed,
2589 # or don't have specific feed yet (so they should use generic)
2590 return if (!$action || $action =~ /^(?:tags|heads|forks|tag|search)$/x);
2591
2592 my $branch = undef;
2593 # branches refs uses 'refs/' + $get_branch_refs()[x] + '/' prefix
2594 # (fullname) to differentiate from tag links; this also makes
2595 # possible to detect branch links
2596 for my $ref (get_branch_refs()) {
2597 if ((defined $hash_base && $hash_base =~ m!^refs/\Q$ref\E/(.*)$!) ||
2598 (defined $hash && $hash =~ m!^refs/\Q$ref\E/(.*)$!)) {
2599 $branch = $1;
2600 $matched_ref = $ref;
2601 last;
2602 }
2603 }
2604 # find log type for feed description (title)
2605 my $type = 'log';
2606 if (defined $file_name) {
2607 $type = "history of $file_name";
2608 $type .= "/" if ($action eq 'tree');
2609 $type .= " on '$branch'" if (defined $branch);
2610 } else {
2611 $type = "log of $branch" if (defined $branch);
2612 }
2613
2614 $res{-title} = $type;
2615 $res{'hash'} = (defined $branch ? "refs/$matched_ref/$branch" : undef);
2616 $res{'file_name'} = $file_name;
2617
2618 return %res;
2619 }
2620
2621 ## ----------------------------------------------------------------------
2622 ## git utility subroutines, invoking git commands
2623
2624 # returns path to the core git executable and the --git-dir parameter as list
2625 sub git_cmd {
2626 $number_of_git_cmds++;
2627 return $GIT, '--git-dir='.$git_dir;
2628 }
2629
2630 # quote the given arguments for passing them to the shell
2631 # quote_command("command", "arg 1", "arg with ' and ! characters")
2632 # => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
2633 # Try to avoid using this function wherever possible.
2634 sub quote_command {
2635 return join(' ',
2636 map { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ );
2637 }
2638
2639 # get HEAD ref of given project as hash
2640 sub git_get_head_hash {
2641 return git_get_full_hash(shift, 'HEAD');
2642 }
2643
2644 sub git_get_full_hash {
2645 return git_get_hash(@_);
2646 }
2647
2648 sub git_get_short_hash {
2649 return git_get_hash(@_, '--short=7');
2650 }
2651
2652 sub git_get_hash {
2653 my ($project, $hash, @options) = @_;
2654 my $o_git_dir = $git_dir;
2655 my $retval = undef;
2656 $git_dir = "$projectroot/$project";
2657 if (open my $fd, '-|', git_cmd(), 'rev-parse',
2658 '--verify', '-q', @options, $hash) {
2659 $retval = <$fd>;
2660 chomp $retval if defined $retval;
2661 close $fd;
2662 }
2663 if (defined $o_git_dir) {
2664 $git_dir = $o_git_dir;
2665 }
2666 return $retval;
2667 }
2668
2669 # get type of given object
2670 sub git_get_type {
2671 my $hash = shift;
2672
2673 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
2674 my $type = <$fd>;
2675 close $fd or return;
2676 chomp $type;
2677 return $type;
2678 }
2679
2680 # repository configuration
2681 our $config_file = '';
2682 our %config;
2683
2684 # store multiple values for single key as anonymous array reference
2685 # single values stored directly in the hash, not as [ <value> ]
2686 sub hash_set_multi {
2687 my ($hash, $key, $value) = @_;
2688
2689 if (!exists $hash->{$key}) {
2690 $hash->{$key} = $value;
2691 } elsif (!ref $hash->{$key}) {
2692 $hash->{$key} = [ $hash->{$key}, $value ];
2693 } else {
2694 push @{$hash->{$key}}, $value;
2695 }
2696 }
2697
2698 # return hash of git project configuration
2699 # optionally limited to some section, e.g. 'gitweb'
2700 sub git_parse_project_config {
2701 my $section_regexp = shift;
2702 my %config;
2703
2704 local $/ = "\0";
2705
2706 open my $fh, "-|", git_cmd(), "config", '-z', '-l',
2707 or return;
2708
2709 while (my $keyval = <$fh>) {
2710 chomp $keyval;
2711 my ($key, $value) = split(/\n/, $keyval, 2);
2712
2713 hash_set_multi(\%config, $key, $value)
2714 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
2715 }
2716 close $fh;
2717
2718 return %config;
2719 }
2720
2721 # convert config value to boolean: 'true' or 'false'
2722 # no value, number > 0, 'true' and 'yes' values are true
2723 # rest of values are treated as false (never as error)
2724 sub config_to_bool {
2725 my $val = shift;
2726
2727 return 1 if !defined $val; # section.key
2728
2729 # strip leading and trailing whitespace
2730 $val =~ s/^\s+//;
2731 $val =~ s/\s+$//;
2732
2733 return (($val =~ /^\d+$/ && $val) || # section.key = 1
2734 ($val =~ /^(?:true|yes)$/i)); # section.key = true
2735 }
2736
2737 # convert config value to simple decimal number
2738 # an optional value suffix of 'k', 'm', or 'g' will cause the value
2739 # to be multiplied by 1024, 1048576, or 1073741824
2740 sub config_to_int {
2741 my $val = shift;
2742
2743 # strip leading and trailing whitespace
2744 $val =~ s/^\s+//;
2745 $val =~ s/\s+$//;
2746
2747 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
2748 $unit = lc($unit);
2749 # unknown unit is treated as 1
2750 return $num * ($unit eq 'g' ? 1073741824 :
2751 $unit eq 'm' ? 1048576 :
2752 $unit eq 'k' ? 1024 : 1);
2753 }
2754 return $val;
2755 }
2756
2757 # convert config value to array reference, if needed
2758 sub config_to_multi {
2759 my $val = shift;
2760
2761 return ref($val) ? $val : (defined($val) ? [ $val ] : []);
2762 }
2763
2764 sub git_get_project_config {
2765 my ($key, $type) = @_;
2766
2767 return unless defined $git_dir;
2768
2769 # key sanity check
2770 return unless ($key);
2771 # only subsection, if exists, is case sensitive,
2772 # and not lowercased by 'git config -z -l'
2773 if (my ($hi, $mi, $lo) = ($key =~ /^([^.]*)\.(.*)\.([^.]*)$/)) {
2774 $lo =~ s/_//g;
2775 $key = join(".", lc($hi), $mi, lc($lo));
2776 return if ($lo =~ /\W/ || $hi =~ /\W/);
2777 } else {
2778 $key = lc($key);
2779 $key =~ s/_//g;
2780 return if ($key =~ /\W/);
2781 }
2782 $key =~ s/^gitweb\.//;
2783
2784 # type sanity check
2785 if (defined $type) {
2786 $type =~ s/^--//;
2787 $type = undef
2788 unless ($type eq 'bool' || $type eq 'int');
2789 }
2790
2791 # get config
2792 if (!defined $config_file ||
2793 $config_file ne "$git_dir/config") {
2794 %config = git_parse_project_config('gitweb');
2795 $config_file = "$git_dir/config";
2796 }
2797
2798 # check if config variable (key) exists
2799 return unless exists $config{"gitweb.$key"};
2800
2801 # ensure given type
2802 if (!defined $type) {
2803 return $config{"gitweb.$key"};
2804 } elsif ($type eq 'bool') {
2805 # backward compatibility: 'git config --bool' returns true/false
2806 return config_to_bool($config{"gitweb.$key"}) ? 'true' : 'false';
2807 } elsif ($type eq 'int') {
2808 return config_to_int($config{"gitweb.$key"});
2809 }
2810 return $config{"gitweb.$key"};
2811 }
2812
2813 # get hash of given path at given ref
2814 sub git_get_hash_by_path {
2815 my $base = shift;
2816 my $path = shift || return undef;
2817 my $type = shift;
2818
2819 $path =~ s,/+$,,;
2820
2821 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
2822 or die_error(500, "Open git-ls-tree failed");
2823 my $line = <$fd>;
2824 close $fd or return undef;
2825
2826 if (!defined $line) {
2827 # there is no tree or hash given by $path at $base
2828 return undef;
2829 }
2830
2831 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2832 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
2833 if (defined $type && $type ne $2) {
2834 # type doesn't match
2835 return undef;
2836 }
2837 return $3;
2838 }
2839
2840 # get path of entry with given hash at given tree-ish (ref)
2841 # used to get 'from' filename for combined diff (merge commit) for renames
2842 sub git_get_path_by_hash {
2843 my $base = shift || return;
2844 my $hash = shift || return;
2845
2846 local $/ = "\0";
2847
2848 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
2849 or return undef;
2850 while (my $line = <$fd>) {
2851 chomp $line;
2852
2853 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
2854 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
2855 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
2856 close $fd;
2857 return $1;
2858 }
2859 }
2860 close $fd;
2861 return undef;
2862 }
2863
2864 ## ......................................................................
2865 ## git utility functions, directly accessing git repository
2866
2867 # get the value of config variable either from file named as the variable
2868 # itself in the repository ($GIT_DIR/$name file), or from gitweb.$name
2869 # configuration variable in the repository config file.
2870 sub git_get_file_or_project_config {
2871 my ($path, $name) = @_;
2872
2873 $git_dir = "$projectroot/$path";
2874 open my $fd, '<', "$git_dir/$name"
2875 or return git_get_project_config($name);
2876 my $conf = <$fd>;
2877 close $fd;
2878 if (defined $conf) {
2879 chomp $conf;
2880 }
2881 return $conf;
2882 }
2883
2884 sub git_get_project_description {
2885 my $path = shift;
2886 return git_get_file_or_project_config($path, 'description');
2887 }
2888
2889 sub git_get_project_category {
2890 my $path = shift;
2891 return git_get_file_or_project_config($path, 'category');
2892 }
2893
2894
2895 # supported formats:
2896 # * $GIT_DIR/ctags/<tagname> file (in 'ctags' subdirectory)
2897 # - if its contents is a number, use it as tag weight,
2898 # - otherwise add a tag with weight 1
2899 # * $GIT_DIR/ctags file, each line is a tag (with weight 1)
2900 # the same value multiple times increases tag weight
2901 # * `gitweb.ctag' multi-valued repo config variable
2902 sub git_get_project_ctags {
2903 my $project = shift;
2904 my $ctags = {};
2905
2906 $git_dir = "$projectroot/$project";
2907 if (opendir my $dh, "$git_dir/ctags") {
2908 my @files = grep { -f $_ } map { "$git_dir/ctags/$_" } readdir($dh);
2909 foreach my $tagfile (@files) {
2910 open my $ct, '<', $tagfile
2911 or next;
2912 my $val = <$ct>;
2913 chomp $val if $val;
2914 close $ct;
2915
2916 (my $ctag = $tagfile) =~ s#.*/##;
2917 if ($val =~ /^\d+$/) {
2918 $ctags->{$ctag} = $val;
2919 } else {
2920 $ctags->{$ctag} = 1;
2921 }
2922 }
2923 closedir $dh;
2924
2925 } elsif (open my $fh, '<', "$git_dir/ctags") {
2926 while (my $line = <$fh>) {
2927 chomp $line;
2928 $ctags->{$line}++ if $line;
2929 }
2930 close $fh;
2931
2932 } else {
2933 my $taglist = config_to_multi(git_get_project_config('ctag'));
2934 foreach my $tag (@$taglist) {
2935 $ctags->{$tag}++;
2936 }
2937 }
2938
2939 return $ctags;
2940 }
2941
2942 # return hash, where keys are content tags ('ctags'),
2943 # and values are sum of weights of given tag in every project
2944 sub git_gather_all_ctags {
2945 my $projects = shift;
2946 my $ctags = {};
2947
2948 foreach my $p (@$projects) {
2949 foreach my $ct (keys %{$p->{'ctags'}}) {
2950 $ctags->{$ct} += $p->{'ctags'}->{$ct};
2951 }
2952 }
2953
2954 return $ctags;
2955 }
2956
2957 sub git_populate_project_tagcloud {
2958 my $ctags = shift;
2959
2960 # First, merge different-cased tags; tags vote on casing
2961 my %ctags_lc;
2962 foreach (keys %$ctags) {
2963 $ctags_lc{lc $_}->{count} += $ctags->{$_};
2964 if (not $ctags_lc{lc $_}->{topcount}
2965 or $ctags_lc{lc $_}->{topcount} < $ctags->{$_}) {
2966 $ctags_lc{lc $_}->{topcount} = $ctags->{$_};
2967 $ctags_lc{lc $_}->{topname} = $_;
2968 }
2969 }
2970
2971 my $cloud;
2972 my $matched = $input_params{'ctag'};
2973 if (eval { require HTML::TagCloud; 1; }) {
2974 $cloud = HTML::TagCloud->new;
2975 foreach my $ctag (sort keys %ctags_lc) {
2976 # Pad the title with spaces so that the cloud looks
2977 # less crammed.
2978 my $title = esc_html($ctags_lc{$ctag}->{topname});
2979 $title =~ s/ /&nbsp;/g;
2980 $title =~ s/^/&nbsp;/g;
2981 $title =~ s/$/&nbsp;/g;
2982 if (defined $matched && $matched eq $ctag) {
2983 $title = qq(<span class="match">$title</span>);
2984 }
2985 $cloud->add($title, href(project=>undef, ctag=>$ctag),
2986 $ctags_lc{$ctag}->{count});
2987 }
2988 } else {
2989 $cloud = {};
2990 foreach my $ctag (keys %ctags_lc) {
2991 my $title = esc_html($ctags_lc{$ctag}->{topname}, -nbsp=>1);
2992 if (defined $matched && $matched eq $ctag) {
2993 $title = qq(<span class="match">$title</span>);
2994 }
2995 $cloud->{$ctag}{count} = $ctags_lc{$ctag}->{count};
2996 $cloud->{$ctag}{ctag} =
2997 $cgi->a({-href=>href(project=>undef, ctag=>$ctag)}, $title);
2998 }
2999 }
3000 return $cloud;
3001 }
3002
3003 sub git_show_project_tagcloud {
3004 my ($cloud, $count) = @_;
3005 if (ref $cloud eq 'HTML::TagCloud') {
3006 return $cloud->html_and_css($count);
3007 } else {
3008 my @tags = sort { $cloud->{$a}->{'count'} <=> $cloud->{$b}->{'count'} } keys %$cloud;
3009 return
3010 '<div id="htmltagcloud"'.($project ? '' : ' align="center"').'>' .
3011 join (', ', map {
3012 $cloud->{$_}->{'ctag'}
3013 } splice(@tags, 0, $count)) .
3014 '</div>';
3015 }
3016 }
3017
3018 sub git_get_project_url_list {
3019 my $path = shift;
3020
3021 $git_dir = "$projectroot/$path";
3022 open my $fd, '<', "$git_dir/cloneurl"
3023 or return wantarray ?
3024 @{ config_to_multi(git_get_project_config('url')) } :
3025 config_to_multi(git_get_project_config('url'));
3026 my @git_project_url_list = map { chomp; $_ } <$fd>;
3027 close $fd;
3028
3029 return wantarray ? @git_project_url_list : \@git_project_url_list;
3030 }
3031
3032 sub git_get_projects_list {
3033 my $filter = shift || '';
3034 my $paranoid = shift;
3035 my @list;
3036
3037 if (-d $projects_list) {
3038 # search in directory
3039 my $dir = $projects_list;
3040 # remove the trailing "/"
3041 $dir =~ s!/+$!!;
3042 my $pfxlen = length("$dir");
3043 my $pfxdepth = ($dir =~ tr!/!!);
3044 # when filtering, search only given subdirectory
3045 if ($filter && !$paranoid) {
3046 $dir .= "/$filter";
3047 $dir =~ s!/+$!!;
3048 }
3049
3050 File::Find::find({
3051 follow_fast => 1, # follow symbolic links
3052 follow_skip => 2, # ignore duplicates
3053 dangling_symlinks => 0, # ignore dangling symlinks, silently
3054 wanted => sub {
3055 # global variables
3056 our $project_maxdepth;
3057 our $projectroot;
3058 # skip project-list toplevel, if we get it.
3059 return if (m!^[/.]$!);
3060 # only directories can be git repositories
3061 return unless (-d $_);
3062 # don't traverse too deep (Find is super slow on os x)
3063 # $project_maxdepth excludes depth of $projectroot
3064 if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
3065 $File::Find::prune = 1;
3066 return;
3067 }
3068
3069 my $path = substr($File::Find::name, $pfxlen + 1);
3070 # paranoidly only filter here
3071 if ($paranoid && $filter && $path !~ m!^\Q$filter\E/!) {
3072 next;
3073 }
3074 # we check related file in $projectroot
3075 if (check_export_ok("$projectroot/$path")) {
3076 push @list, { path => $path };
3077 $File::Find::prune = 1;
3078 }
3079 },
3080 }, "$dir");
3081
3082 } elsif (-f $projects_list) {
3083 # read from file(url-encoded):
3084 # 'git%2Fgit.git Linus+Torvalds'
3085 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
3086 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
3087 open my $fd, '<', $projects_list or return;
3088 PROJECT:
3089 while (my $line = <$fd>) {
3090 chomp $line;
3091 my ($path, $owner) = split ' ', $line;
3092 $path = unescape($path);
3093 $owner = unescape($owner);
3094 if (!defined $path) {
3095 next;
3096 }
3097 # if $filter is rpovided, check if $path begins with $filter
3098 if ($filter && $path !~ m!^\Q$filter\E/!) {
3099 next;
3100 }
3101 if (check_export_ok("$projectroot/$path")) {
3102 my $pr = {
3103 path => $path
3104 };
3105 if ($owner) {
3106 $pr->{'owner'} = to_utf8($owner);
3107 }
3108 push @list, $pr;
3109 }
3110 }
3111 close $fd;
3112 }
3113 return @list;
3114 }
3115
3116 # written with help of Tree::Trie module (Perl Artistic License, GPL compatibile)
3117 # as side effects it sets 'forks' field to list of forks for forked projects
3118 sub filter_forks_from_projects_list {
3119 my $projects = shift;
3120
3121 my %trie; # prefix tree of directories (path components)
3122 # generate trie out of those directories that might contain forks
3123 foreach my $pr (@$projects) {
3124 my $path = $pr->{'path'};
3125 $path =~ s/\.git$//; # forks of 'repo.git' are in 'repo/' directory
3126 next if ($path =~ m!/$!); # skip non-bare repositories, e.g. 'repo/.git'
3127 next unless ($path); # skip '.git' repository: tests, git-instaweb
3128 next unless (-d "$projectroot/$path"); # containing directory exists
3129 $pr->{'forks'} = []; # there can be 0 or more forks of project
3130
3131 # add to trie
3132 my @dirs = split('/', $path);
3133 # walk the trie, until either runs out of components or out of trie
3134 my $ref = \%trie;
3135 while (scalar @dirs &&
3136 exists($ref->{$dirs[0]})) {
3137 $ref = $ref->{shift @dirs};
3138 }
3139 # create rest of trie structure from rest of components
3140 foreach my $dir (@dirs) {
3141 $ref = $ref->{$dir} = {};
3142 }
3143 # create end marker, store $pr as a data
3144 $ref->{''} = $pr if (!exists $ref->{''});
3145 }
3146
3147 # filter out forks, by finding shortest prefix match for paths
3148 my @filtered;
3149 PROJECT:
3150 foreach my $pr (@$projects) {
3151 # trie lookup
3152 my $ref = \%trie;
3153 DIR:
3154 foreach my $dir (split('/', $pr->{'path'})) {
3155 if (exists $ref->{''}) {
3156 # found [shortest] prefix, is a fork - skip it
3157 push @{$ref->{''}{'forks'}}, $pr;
3158 next PROJECT;
3159 }
3160 if (!exists $ref->{$dir}) {
3161 # not in trie, cannot have prefix, not a fork
3162 push @filtered, $pr;
3163 next PROJECT;
3164 }
3165 # If the dir is there, we just walk one step down the trie.
3166 $ref = $ref->{$dir};
3167 }
3168 # we ran out of trie
3169 # (shouldn't happen: it's either no match, or end marker)
3170 push @filtered, $pr;
3171 }
3172
3173 return @filtered;
3174 }
3175
3176 # note: fill_project_list_info must be run first,
3177 # for 'descr_long' and 'ctags' to be filled
3178 sub search_projects_list {