Styling previous commit (pretty-render markdown files in blob mode) with 'readme...
[gitweb-sgk] / gitweb.cgi
CommitLineData
f35f44b7
AT
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
10use 5.008;
11use strict;
12use warnings;
13use CGI qw(:standard :escapeHTML -nosticky);
14use CGI::Util qw(unescape);
15use CGI::Carp qw(fatalsToBrowser set_message);
16use Encode;
17use Fcntl ':mode';
18use File::Find qw();
19use File::Basename qw(basename);
20use Time::HiRes qw(gettimeofday tv_interval);
21binmode STDOUT, ':utf8';
22
23if (!defined($CGI::VERSION) || $CGI::VERSION < 4.08) {
24 eval 'sub CGI::multi_param { CGI::param(@_) }'
25}
26
27our $t0 = [ gettimeofday() ];
28our $number_of_git_cmds = 0;
29
30BEGIN {
31 CGI->compile() if $ENV{'MOD_PERL'};
32}
33
34our $version = "2.11.0";
35
36our ($my_url, $my_uri, $base_url, $path_info, $home_link);
37sub 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
79our $GIT = "/usr/bin/git";
80
81# absolute fs-path which will be prepended to the project path
82#our $projectroot = "/pub/scm";
83our $projectroot = "/pub/git";
84
85# fs traversing limit for getting project list
86# the number is relative to the projectroot
87our $project_maxdepth = 2007;
88
89# string of the home link on top of all pages
90our $home_link_str = "projects";
91
92# extra breadcrumbs preceding the home link
93our @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
97our $site_name = ""
98 || ($ENV{'SERVER_NAME'} || "Untitled") . " Git";
99
100# html snippet to include in the <head> section of each page
101our $site_html_head_string = "";
102# filename of html text to include at top of each page
103our $site_header = "";
104# html text to include at home page
105our $home_text = "indextext.html";
106# filename of html text to include at bottom of each page
107our $site_footer = "";
108
109# URI of stylesheets
110our @stylesheets = ("static/gitweb.css");
111# URI of a single stylesheet, which can be overridden in GITWEB_CONFIG.
112our $stylesheet = undef;
113# URI of GIT logo (72x27 size)
114our $logo = "static/git-logo.png";
115# URI of GIT favicon, assumed to be image/png type
116our $favicon = "static/git-favicon.png";
117# URI of gitweb.js (JavaScript code for gitweb)
118our $javascript = "static/gitweb.js";
119
120# URI and label (title) of GIT logo link
a062b364
AT
121our $logo_url = "http://subgeniuskitty.com/";
122our $logo_label = "SGK Homepage";
f35f44b7
AT
123
124# source of projects list
125our $projects_list = "";
126
127# the width (in characters) of the projects list "Description" column
128our $projects_list_description_width = 80;
129
130# group projects by category on the projects list
131# (enabled if this variable evaluates to true)
132our $projects_list_group_categories = 0;
133
134# default category if none specified
135# (leave the empty string for no category)
136our $project_list_default_category = "";
137
138# default order of projects list
139# valid values are none, project, descr, owner, and age
140our $default_projects_order = "project";
141
142# show repository only if this file exists
143# (only effective if this variable evaluates to true)
144our $export_ok = "";
145
146# don't generate age column on the projects list page
147our $omit_age_column = 0;
148
149# don't generate information about owners of repositories
150our $omit_owner=0;
151
152# show repository only if this subroutine returns true
153# when given the path to the project, for example:
154# sub { return -e "$_[0]/git-daemon-export-ok"; }
155our $export_auth_hook = undef;
156
157# only allow viewing of repositories also shown on the overview page
158our $strict_export = "";
159
160# list of git base URLs used for URL to where fetch project from,
161# i.e. full URL is "$git_base_url/$project"
162our @git_base_url_list = grep { $_ ne '' } ("");
163
164# default blob_plain mimetype and default charset for text/plain blob
165our $default_blob_plain_mimetype = 'text/plain';
166our $default_text_plain_charset = undef;
167
168# file to use for guessing MIME types before trying /etc/mime.types
169# (relative to the current git repository)
170our $mimetypes_file = undef;
171
172# assume this charset if line contains non-UTF-8 characters;
173# it should be valid encoding (see Encoding::Supported(3pm) for list),
174# for which encoding all byte sequences are valid, for example
175# 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
176# could be even 'utf-8' for the old behavior)
177our $fallback_encoding = 'latin1';
178
179# rename detection options for git-diff and git-diff-tree
180# - default is '-M', with the cost proportional to
181# (number of removed files) * (number of new files).
182# - more costly is '-C' (which implies '-M'), with the cost proportional to
183# (number of changed files + number of removed files) * (number of new files)
184# - even more costly is '-C', '--find-copies-harder' with cost
185# (number of files in the original tree) * (number of new files)
186# - one might want to include '-B' option, e.g. '-B', '-M'
187our @diff_opts = ('-M'); # taken from git_commit
188
189# Disables features that would allow repository owners to inject script into
190# the gitweb domain.
191our $prevent_xss = 0;
192
193# Path to the highlight executable to use (must be the one from
194# http://www.andre-simon.de due to assumptions about parameters and output).
195# Useful if highlight is not installed on your webserver's PATH.
196# [Default: highlight]
197our $highlight_bin = "/usr/bin/highlight";
198
199# information about snapshot formats that gitweb is capable of serving
200our %known_snapshot_formats = (
201 # name => {
202 # 'display' => display name,
203 # 'type' => mime type,
204 # 'suffix' => filename suffix,
205 # 'format' => --format for git-archive,
206 # 'compressor' => [compressor command and arguments]
207 # (array reference, optional)
208 # 'disabled' => boolean (optional)}
209 #
210 'tgz' => {
211 'display' => 'tar.gz',
212 'type' => 'application/x-gzip',
213 'suffix' => '.tar.gz',
214 'format' => 'tar',
215 'compressor' => ['gzip', '-n']},
216
217 'tbz2' => {
218 'display' => 'tar.bz2',
219 'type' => 'application/x-bzip2',
220 'suffix' => '.tar.bz2',
221 'format' => 'tar',
222 'compressor' => ['bzip2']},
223
224 'txz' => {
225 'display' => 'tar.xz',
226 'type' => 'application/x-xz',
227 'suffix' => '.tar.xz',
228 'format' => 'tar',
229 'compressor' => ['xz'],
230 'disabled' => 1},
231
232 'zip' => {
233 'display' => 'zip',
234 'type' => 'application/x-zip',
235 'suffix' => '.zip',
236 'format' => 'zip'},
237);
238
239# Aliases so we understand old gitweb.snapshot values in repository
240# configuration.
241our %known_snapshot_format_aliases = (
242 'gzip' => 'tgz',
243 'bzip2' => 'tbz2',
244 'xz' => 'txz',
245
246 # backward compatibility: legacy gitweb config support
247 'x-gzip' => undef, 'gz' => undef,
248 'x-bzip2' => undef, 'bz2' => undef,
249 'x-zip' => undef, '' => undef,
250);
251
252# Pixel sizes for icons and avatars. If the default font sizes or lineheights
253# are changed, it may be appropriate to change these values too via
254# $GITWEB_CONFIG.
255our %avatar_size = (
256 'default' => 16,
257 'double' => 32
258);
259
260# Used to set the maximum load that we will still respond to gitweb queries.
261# If server load exceed this value then return "503 server busy" error.
262# If gitweb cannot determined server load, it is taken to be 0.
263# Leave it undefined (or set to 'undef') to turn off load checking.
264our $maxload = 300;
265
266# configuration for 'highlight' (http://www.andre-simon.de/)
267# match by basename
268our %highlight_basename = (
269 #'Program' => 'py',
270 #'Library' => 'py',
271 'SConstruct' => 'py', # SCons equivalent of Makefile
272 'Makefile' => 'make',
273);
274# match by extension
275our %highlight_ext = (
276 # main extensions, defining name of syntax;
277 # see files in /usr/share/highlight/langDefs/ directory
278 (map { $_ => $_ } qw(py rb java css js tex bib xml awk bat ini spec tcl sql)),
279 # alternate extensions, see /etc/highlight/filetypes.conf
280 (map { $_ => 'c' } qw(c h)),
281 (map { $_ => 'sh' } qw(sh bash zsh ksh)),
282 (map { $_ => 'cpp' } qw(cpp cxx c++ cc)),
283 (map { $_ => 'php' } qw(php php3 php4 php5 phps)),
284 (map { $_ => 'pl' } qw(pl perl pm)), # perhaps also 'cgi'
285 (map { $_ => 'make'} qw(make mak mk)),
286 (map { $_ => 'xml' } qw(xml xhtml html htm)),
287);
288
289# You define site-wide feature defaults here; override them with
290# $GITWEB_CONFIG as necessary.
291our %feature = (
292 # feature => {
293 # 'sub' => feature-sub (subroutine),
294 # 'override' => allow-override (boolean),
295 # 'default' => [ default options...] (array reference)}
296 #
297 # if feature is overridable (it means that allow-override has true value),
298 # then feature-sub will be called with default options as parameters;
299 # return value of feature-sub indicates if to enable specified feature
300 #
301 # if there is no 'sub' key (no feature-sub), then feature cannot be
302 # overridden
303 #
304 # use gitweb_get_feature(<feature>) to retrieve the <feature> value
305 # (an array) or gitweb_check_feature(<feature>) to check if <feature>
306 # is enabled
307
308 # Enable the 'blame' blob view, showing the last commit that modified
309 # each line in the file. This can be very CPU-intensive.
310
311 # To enable system wide have in $GITWEB_CONFIG
312 # $feature{'blame'}{'default'} = [1];
313 # To have project specific config enable override in $GITWEB_CONFIG
314 # $feature{'blame'}{'override'} = 1;
315 # and in project config gitweb.blame = 0|1;
316 'blame' => {
317 'sub' => sub { feature_bool('blame', @_) },
318 'override' => 0,
319 'default' => [0]},
320
321 # Enable the 'snapshot' link, providing a compressed archive of any
322 # tree. This can potentially generate high traffic if you have large
323 # project.
324
325 # Value is a list of formats defined in %known_snapshot_formats that
326 # you wish to offer.
327 # To disable system wide have in $GITWEB_CONFIG
328 # $feature{'snapshot'}{'default'} = [];
329 # To have project specific config enable override in $GITWEB_CONFIG
330 # $feature{'snapshot'}{'override'} = 1;
331 # and in project config, a comma-separated list of formats or "none"
332 # to disable. Example: gitweb.snapshot = tbz2,zip;
333 'snapshot' => {
334 'sub' => \&feature_snapshot,
335 'override' => 0,
336 'default' => ['tgz']},
337
338 # Enable text search, which will list the commits which match author,
339 # committer or commit text to a given string. Enabled by default.
340 # Project specific override is not supported.
341 #
342 # Note that this controls all search features, which means that if
343 # it is disabled, then 'grep' and 'pickaxe' search would also be
344 # disabled.
345 'search' => {
346 'override' => 0,
347 'default' => [1]},
348
349 # Enable grep search, which will list the files in currently selected
350 # tree containing the given string. Enabled by default. This can be
351 # potentially CPU-intensive, of course.
352 # Note that you need to have 'search' feature enabled too.
353
354 # To enable system wide have in $GITWEB_CONFIG
355 # $feature{'grep'}{'default'} = [1];
356 # To have project specific config enable override in $GITWEB_CONFIG
357 # $feature{'grep'}{'override'} = 1;
358 # and in project config gitweb.grep = 0|1;
359 'grep' => {
360 'sub' => sub { feature_bool('grep', @_) },
361 'override' => 0,
362 'default' => [1]},
363
364 # Enable the pickaxe search, which will list the commits that modified
365 # a given string in a file. This can be practical and quite faster
366 # alternative to 'blame', but still potentially CPU-intensive.
367 # Note that you need to have 'search' feature enabled too.
368
369 # To enable system wide have in $GITWEB_CONFIG
370 # $feature{'pickaxe'}{'default'} = [1];
371 # To have project specific config enable override in $GITWEB_CONFIG
372 # $feature{'pickaxe'}{'override'} = 1;
373 # and in project config gitweb.pickaxe = 0|1;
374 'pickaxe' => {
375 'sub' => sub { feature_bool('pickaxe', @_) },
376 'override' => 0,
377 'default' => [1]},
378
379 # Enable showing size of blobs in a 'tree' view, in a separate
380 # column, similar to what 'ls -l' does. This cost a bit of IO.
381
382 # To disable system wide have in $GITWEB_CONFIG
383 # $feature{'show-sizes'}{'default'} = [0];
384 # To have project specific config enable override in $GITWEB_CONFIG
385 # $feature{'show-sizes'}{'override'} = 1;
386 # and in project config gitweb.showsizes = 0|1;
387 'show-sizes' => {
388 'sub' => sub { feature_bool('showsizes', @_) },
389 'override' => 0,
390 'default' => [1]},
391
392 # Make gitweb use an alternative format of the URLs which can be
393 # more readable and natural-looking: project name is embedded
394 # directly in the path and the query string contains other
395 # auxiliary information. All gitweb installations recognize
396 # URL in either format; this configures in which formats gitweb
397 # generates links.
398
399 # To enable system wide have in $GITWEB_CONFIG
400 # $feature{'pathinfo'}{'default'} = [1];
401 # Project specific override is not supported.
402
403 # Note that you will need to change the default location of CSS,
404 # favicon, logo and possibly other files to an absolute URL. Also,
405 # if gitweb.cgi serves as your indexfile, you will need to force
406 # $my_uri to contain the script name in your $GITWEB_CONFIG.
407 'pathinfo' => {
408 'override' => 0,
409 'default' => [0]},
410
411 # Make gitweb consider projects in project root subdirectories
412 # to be forks of existing projects. Given project $projname.git,
413 # projects matching $projname/*.git will not be shown in the main
414 # projects list, instead a '+' mark will be added to $projname
415 # there and a 'forks' view will be enabled for the project, listing
416 # all the forks. If project list is taken from a file, forks have
417 # to be listed after the main project.
418
419 # To enable system wide have in $GITWEB_CONFIG
420 # $feature{'forks'}{'default'} = [1];
421 # Project specific override is not supported.
422 'forks' => {
423 'override' => 0,
424 'default' => [0]},
425
426 # Insert custom links to the action bar of all project pages.
427 # This enables you mainly to link to third-party scripts integrating
428 # into gitweb; e.g. git-browser for graphical history representation
429 # or custom web-based repository administration interface.
430
431 # The 'default' value consists of a list of triplets in the form
432 # (label, link, position) where position is the label after which
433 # to insert the link and link is a format string where %n expands
434 # to the project name, %f to the project path within the filesystem,
435 # %h to the current hash (h gitweb parameter) and %b to the current
436 # hash base (hb gitweb parameter); %% expands to %.
437
438 # To enable system wide have in $GITWEB_CONFIG e.g.
439 # $feature{'actions'}{'default'} = [('graphiclog',
440 # '/git-browser/by-commit.html?r=%n', 'summary')];
441 # Project specific override is not supported.
442 'actions' => {
443 'override' => 0,
444 'default' => []},
445
446 # Allow gitweb scan project content tags of project repository,
447 # and display the popular Web 2.0-ish "tag cloud" near the projects
448 # list. Note that this is something COMPLETELY different from the
449 # normal Git tags.
450
451 # gitweb by itself can show existing tags, but it does not handle
452 # tagging itself; you need to do it externally, outside gitweb.
453 # The format is described in git_get_project_ctags() subroutine.
454 # You may want to install the HTML::TagCloud Perl module to get
455 # a pretty tag cloud instead of just a list of tags.
456
457 # To enable system wide have in $GITWEB_CONFIG
458 # $feature{'ctags'}{'default'} = [1];
459 # Project specific override is not supported.
460
461 # In the future whether ctags editing is enabled might depend
462 # on the value, but using 1 should always mean no editing of ctags.
463 'ctags' => {
464 'override' => 0,
465 'default' => [0]},
466
467 # The maximum number of patches in a patchset generated in patch
468 # view. Set this to 0 or undef to disable patch view, or to a
469 # negative number to remove any limit.
470
471 # To disable system wide have in $GITWEB_CONFIG
472 # $feature{'patches'}{'default'} = [0];
473 # To have project specific config enable override in $GITWEB_CONFIG
474 # $feature{'patches'}{'override'} = 1;
475 # and in project config gitweb.patches = 0|n;
476 # where n is the maximum number of patches allowed in a patchset.
477 'patches' => {
478 'sub' => \&feature_patches,
479 'override' => 0,
480 'default' => [16]},
481
482 # Avatar support. When this feature is enabled, views such as
483 # shortlog or commit will display an avatar associated with
484 # the email of the committer(s) and/or author(s).
485
486 # Currently available providers are gravatar and picon.
487 # If an unknown provider is specified, the feature is disabled.
488
489 # Gravatar depends on Digest::MD5.
490 # Picon currently relies on the indiana.edu database.
491
492 # To enable system wide have in $GITWEB_CONFIG
493 # $feature{'avatar'}{'default'} = ['<provider>'];
494 # where <provider> is either gravatar or picon.
495 # To have project specific config enable override in $GITWEB_CONFIG
496 # $feature{'avatar'}{'override'} = 1;
497 # and in project config gitweb.avatar = <provider>;
498 'avatar' => {
499 'sub' => \&feature_avatar,
500 'override' => 0,
501 'default' => ['']},
502
503 # Enable displaying how much time and how many git commands
504 # it took to generate and display page. Disabled by default.
505 # Project specific override is not supported.
506 'timed' => {
507 'override' => 0,
508 'default' => [0]},
509
510 # Enable turning some links into links to actions which require
511 # JavaScript to run (like 'blame_incremental'). Not enabled by
512 # default. Project specific override is currently not supported.
513 'javascript-actions' => {
514 'override' => 0,
515 'default' => [0]},
516
517 # Enable and configure ability to change common timezone for dates
518 # in gitweb output via JavaScript. Enabled by default.
519 # Project specific override is not supported.
520 'javascript-timezone' => {
521 'override' => 0,
522 'default' => [
523 'local', # default timezone: 'utc', 'local', or '(-|+)HHMM' format,
524 # or undef to turn off this feature
525 'gitweb_tz', # name of cookie where to store selected timezone
526 'datetime', # CSS class used to mark up dates for manipulation
527 ]},
528
529 # Syntax highlighting support. This is based on Daniel Svensson's
530 # and Sham Chukoury's work in gitweb-xmms2.git.
531 # It requires the 'highlight' program present in $PATH,
532 # and therefore is disabled by default.
533
534 # To enable system wide have in $GITWEB_CONFIG
535 # $feature{'highlight'}{'default'} = [1];
536
537 'highlight' => {
538 'sub' => sub { feature_bool('highlight', @_) },
539 'override' => 0,
540 'default' => [1]},
541
542 # Enable displaying of remote heads in the heads list
543
544 # To enable system wide have in $GITWEB_CONFIG
545 # $feature{'remote_heads'}{'default'} = [1];
546 # To have project specific config enable override in $GITWEB_CONFIG
547 # $feature{'remote_heads'}{'override'} = 1;
548 # and in project config gitweb.remoteheads = 0|1;
549 'remote_heads' => {
550 'sub' => sub { feature_bool('remote_heads', @_) },
551 'override' => 0,
552 'default' => [0]},
553
554 # Enable showing branches under other refs in addition to heads
555
556 # To set system wide extra branch refs have in $GITWEB_CONFIG
557 # $feature{'extra-branch-refs'}{'default'} = ['dirs', 'of', 'choice'];
558 # To have project specific config enable override in $GITWEB_CONFIG
559 # $feature{'extra-branch-refs'}{'override'} = 1;
560 # and in project config gitweb.extrabranchrefs = dirs of choice
561 # Every directory is separated with whitespace.
562
563 'extra-branch-refs' => {
564 'sub' => \&feature_extra_branch_refs,
565 'override' => 0,
566 'default' => []},
567);
568
569sub gitweb_get_feature {
570 my ($name) = @_;
571 return unless exists $feature{$name};
572 my ($sub, $override, @defaults) = (
573 $feature{$name}{'sub'},
574 $feature{$name}{'override'},
575 @{$feature{$name}{'default'}});
576 # project specific override is possible only if we have project
577 our $git_dir; # global variable, declared later
578 if (!$override || !defined $git_dir) {
579 return @defaults;
580 }
581 if (!defined $sub) {
582 warn "feature $name is not overridable";
583 return @defaults;
584 }
585 return $sub->(@defaults);
586}
587
588# A wrapper to check if a given feature is enabled.
589# With this, you can say
590#
591# my $bool_feat = gitweb_check_feature('bool_feat');
592# gitweb_check_feature('bool_feat') or somecode;
593#
594# instead of
595#
596# my ($bool_feat) = gitweb_get_feature('bool_feat');
597# (gitweb_get_feature('bool_feat'))[0] or somecode;
598#
599sub gitweb_check_feature {
600 return (gitweb_get_feature(@_))[0];
601}
602
603
604sub feature_bool {
605 my $key = shift;
606 my ($val) = git_get_project_config($key, '--bool');
607
608 if (!defined $val) {
609 return ($_[0]);
610 } elsif ($val eq 'true') {
611 return (1);
612 } elsif ($val eq 'false') {
613 return (0);
614 }
615}
616
617sub feature_snapshot {
618 my (@fmts) = @_;
619
620 my ($val) = git_get_project_config('snapshot');
621
622 if ($val) {
623 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
624 }
625
626 return @fmts;
627}
628
629sub feature_patches {
630 my @val = (git_get_project_config('patches', '--int'));
631
632 if (@val) {
633 return @val;
634 }
635
636 return ($_[0]);
637}
638
639sub feature_avatar {
640 my @val = (git_get_project_config('avatar'));
641
642 return @val ? @val : @_;
643}
644
645sub feature_extra_branch_refs {
646 my (@branch_refs) = @_;
647 my $values = git_get_project_config('extrabranchrefs');
648
649 if ($values) {
650 $values = config_to_multi ($values);
651 @branch_refs = ();
652 foreach my $value (@{$values}) {
653 push @branch_refs, split /\s+/, $value;
654 }
655 }
656
657 return @branch_refs;
658}
659
660# checking HEAD file with -e is fragile if the repository was
661# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
662# and then pruned.
663sub check_head_link {
664 my ($dir) = @_;
665 my $headfile = "$dir/HEAD";
666 return ((-e $headfile) ||
667 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
668}
669
670sub check_export_ok {
671 my ($dir) = @_;
672 return (check_head_link($dir) &&
673 (!$export_ok || -e "$dir/$export_ok") &&
674 (!$export_auth_hook || $export_auth_hook->($dir)));
675}
676
677# process alternate names for backward compatibility
678# filter out unsupported (unknown) snapshot formats
679sub filter_snapshot_fmts {
680 my @fmts = @_;
681
682 @fmts = map {
683 exists $known_snapshot_format_aliases{$_} ?
684 $known_snapshot_format_aliases{$_} : $_} @fmts;
685 @fmts = grep {
686 exists $known_snapshot_formats{$_} &&
687 !$known_snapshot_formats{$_}{'disabled'}} @fmts;
688}
689
690sub filter_and_validate_refs {
691 my @refs = @_;
692 my %unique_refs = ();
693
694 foreach my $ref (@refs) {
695 die_error(500, "Invalid ref '$ref' in 'extra-branch-refs' feature") unless (is_valid_ref_format($ref));
696 # 'heads' are added implicitly in get_branch_refs().
697 $unique_refs{$ref} = 1 if ($ref ne 'heads');
698 }
699 return sort keys %unique_refs;
700}
701
702# If it is set to code reference, it is code that it is to be run once per
703# request, allowing updating configurations that change with each request,
704# while running other code in config file only once.
705#
706# Otherwise, if it is false then gitweb would process config file only once;
707# if it is true then gitweb config would be run for each request.
708our $per_request_config = 1;
709
710# read and parse gitweb config file given by its parameter.
711# returns true on success, false on recoverable error, allowing
712# to chain this subroutine, using first file that exists.
713# dies on errors during parsing config file, as it is unrecoverable.
714sub read_config_file {
715 my $filename = shift;
716 return unless defined $filename;
717 # die if there are errors parsing config file
718 if (-e $filename) {
719 do $filename;
720 die $@ if $@;
721 return 1;
722 }
723 return;
724}
725
726our ($GITWEB_CONFIG, $GITWEB_CONFIG_SYSTEM, $GITWEB_CONFIG_COMMON);
727sub evaluate_gitweb_config {
728 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "gitweb_config.perl";
729 our $GITWEB_CONFIG_SYSTEM = $ENV{'GITWEB_CONFIG_SYSTEM'} || "/etc/gitweb.conf";
730 our $GITWEB_CONFIG_COMMON = $ENV{'GITWEB_CONFIG_COMMON'} || "/etc/gitweb-common.conf";
731
732 # Protect against duplications of file names, to not read config twice.
733 # Only one of $GITWEB_CONFIG and $GITWEB_CONFIG_SYSTEM is used, so
734 # there possibility of duplication of filename there doesn't matter.
735 $GITWEB_CONFIG = "" if ($GITWEB_CONFIG eq $GITWEB_CONFIG_COMMON);
736 $GITWEB_CONFIG_SYSTEM = "" if ($GITWEB_CONFIG_SYSTEM eq $GITWEB_CONFIG_COMMON);
737
738 # Common system-wide settings for convenience.
739 # Those settings can be ovverriden by GITWEB_CONFIG or GITWEB_CONFIG_SYSTEM.
740 read_config_file($GITWEB_CONFIG_COMMON);
741
742 # Use first config file that exists. This means use the per-instance
743 # GITWEB_CONFIG if exists, otherwise use GITWEB_SYSTEM_CONFIG.
744 read_config_file($GITWEB_CONFIG) and return;
745 read_config_file($GITWEB_CONFIG_SYSTEM);
746}
747
748# Get loadavg of system, to compare against $maxload.
749# Currently it requires '/proc/loadavg' present to get loadavg;
750# if it is not present it returns 0, which means no load checking.
751sub get_loadavg {
752 if( -e '/proc/loadavg' ){
753 open my $fd, '<', '/proc/loadavg'
754 or return 0;
755 my @load = split(/\s+/, scalar <$fd>);
756 close $fd;
757
758 # The first three columns measure CPU and IO utilization of the last one,
759 # five, and 10 minute periods. The fourth column shows the number of
760 # currently running processes and the total number of processes in the m/n
761 # format. The last column displays the last process ID used.
762 return $load[0] || 0;
763 }
764 # additional checks for load average should go here for things that don't export
765 # /proc/loadavg
766
767 return 0;
768}
769
770# version of the core git binary
771our $git_version;
772sub evaluate_git_version {
773 our $git_version = qx("$GIT" --version) =~ m/git version (.*)$/ ? $1 : "unknown";
774 $number_of_git_cmds++;
775}
776
777sub check_loadavg {
778 if (defined $maxload && get_loadavg() > $maxload) {
779 die_error(503, "The load average on the server is too high");
780 }
781}
782
783# ======================================================================
784# input validation and dispatch
785
786# input parameters can be collected from a variety of sources (presently, CGI
787# and PATH_INFO), so we define an %input_params hash that collects them all
788# together during validation: this allows subsequent uses (e.g. href()) to be
789# agnostic of the parameter origin
790
791our %input_params = ();
792
793# input parameters are stored with the long parameter name as key. This will
794# also be used in the href subroutine to convert parameters to their CGI
795# equivalent, and since the href() usage is the most frequent one, we store
796# the name -> CGI key mapping here, instead of the reverse.
797#
798# XXX: Warning: If you touch this, check the search form for updating,
799# too.
800
801our @cgi_param_mapping = (
802 project => "p",
803 action => "a",
804 file_name => "f",
805 file_parent => "fp",
806 hash => "h",
807 hash_parent => "hp",
808 hash_base => "hb",
809 hash_parent_base => "hpb",
810 page => "pg",
811 order => "o",
812 searchtext => "s",
813 searchtype => "st",
814 snapshot_format => "sf",
815 extra_options => "opt",
816 search_use_regexp => "sr",
817 ctag => "by_tag",
818 diff_style => "ds",
819 project_filter => "pf",
820 # this must be last entry (for manipulation from JavaScript)
821 javascript => "js"
822);
823our %cgi_param_mapping = @cgi_param_mapping;
824
825# we will also need to know the possible actions, for validation
826our %actions = (
827 "blame" => \&git_blame,
828 "blame_incremental" => \&git_blame_incremental,
829 "blame_data" => \&git_blame_data,
830 "blobdiff" => \&git_blobdiff,
831 "blobdiff_plain" => \&git_blobdiff_plain,
832 "blob" => \&git_blob,
833 "blob_plain" => \&git_blob_plain,
834 "commitdiff" => \&git_commitdiff,
835 "commitdiff_plain" => \&git_commitdiff_plain,
836 "commit" => \&git_commit,
837 "forks" => \&git_forks,
838 "heads" => \&git_heads,
839 "history" => \&git_history,
840 "log" => \&git_log,
841 "patch" => \&git_patch,
842 "patches" => \&git_patches,
843 "remotes" => \&git_remotes,
844 "rss" => \&git_rss,
845 "atom" => \&git_atom,
846 "search" => \&git_search,
847 "search_help" => \&git_search_help,
848 "shortlog" => \&git_shortlog,
849 "summary" => \&git_summary,
850 "tag" => \&git_tag,
851 "tags" => \&git_tags,
852 "tree" => \&git_tree,
853 "snapshot" => \&git_snapshot,
854 "object" => \&git_object,
855 # those below don't need $project
856 "opml" => \&git_opml,
857 "project_list" => \&git_project_list,
858 "project_index" => \&git_project_index,
859);
860
861# finally, we have the hash of allowed extra_options for the commands that
862# allow them
863our %allowed_options = (
864 "--no-merges" => [ qw(rss atom log shortlog history) ],
865);
866
867# fill %input_params with the CGI parameters. All values except for 'opt'
868# should be single values, but opt can be an array. We should probably
869# build an array of parameters that can be multi-valued, but since for the time
870# being it's only this one, we just single it out
871sub evaluate_query_params {
872 our $cgi;
873
874 while (my ($name, $symbol) = each %cgi_param_mapping) {
875 if ($symbol eq 'opt') {
876 $input_params{$name} = [ map { decode_utf8($_) } $cgi->multi_param($symbol) ];
877 } else {
878 $input_params{$name} = decode_utf8($cgi->param($symbol));
879 }
880 }
881}
882
883# now read PATH_INFO and update the parameter list for missing parameters
884sub evaluate_path_info {
885 return if defined $input_params{'project'};
886 return if !$path_info;
887 $path_info =~ s,^/+,,;
888 return if !$path_info;
889
890 # find which part of PATH_INFO is project
891 my $project = $path_info;
892 $project =~ s,/+$,,;
893 while ($project && !check_head_link("$projectroot/$project")) {
894 $project =~ s,/*[^/]*$,,;
895 }
896 return unless $project;
897 $input_params{'project'} = $project;
898
899 # do not change any parameters if an action is given using the query string
900 return if $input_params{'action'};
901 $path_info =~ s,^\Q$project\E/*,,;
902
903 # next, check if we have an action
904 my $action = $path_info;
905 $action =~ s,/.*$,,;
906 if (exists $actions{$action}) {
907 $path_info =~ s,^$action/*,,;
908 $input_params{'action'} = $action;
909 }
910
911 # list of actions that want hash_base instead of hash, but can have no
912 # pathname (f) parameter
913 my @wants_base = (
914 'tree',
915 'history',
916 );
917
918 # we want to catch, among others
919 # [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name]
920 my ($parentrefname, $parentpathname, $refname, $pathname) =
921 ($path_info =~ /^(?:(.+?)(?::(.+))?\.\.)?([^:]+?)?(?::(.+))?$/);
922
923 # first, analyze the 'current' part
924 if (defined $pathname) {
925 # we got "branch:filename" or "branch:dir/"
926 # we could use git_get_type(branch:pathname), but:
927 # - it needs $git_dir
928 # - it does a git() call
929 # - the convention of terminating directories with a slash
930 # makes it superfluous
931 # - embedding the action in the PATH_INFO would make it even
932 # more superfluous
933 $pathname =~ s,^/+,,;
934 if (!$pathname || substr($pathname, -1) eq "/") {
935 $input_params{'action'} ||= "tree";
936 $pathname =~ s,/$,,;
937 } else {
938 # the default action depends on whether we had parent info
939 # or not
940 if ($parentrefname) {
941 $input_params{'action'} ||= "blobdiff_plain";
942 } else {
943 $input_params{'action'} ||= "blob_plain";
944 }
945 }
946 $input_params{'hash_base'} ||= $refname;
947 $input_params{'file_name'} ||= $pathname;
948 } elsif (defined $refname) {
949 # we got "branch". In this case we have to choose if we have to
950 # set hash or hash_base.
951 #
952 # Most of the actions without a pathname only want hash to be
953 # set, except for the ones specified in @wants_base that want
954 # hash_base instead. It should also be noted that hand-crafted
955 # links having 'history' as an action and no pathname or hash
956 # set will fail, but that happens regardless of PATH_INFO.
957 if (defined $parentrefname) {
958 # if there is parent let the default be 'shortlog' action
959 # (for http://git.example.com/repo.git/A..B links); if there
960 # is no parent, dispatch will detect type of object and set
961 # action appropriately if required (if action is not set)
962 $input_params{'action'} ||= "shortlog";
963 }
964 if ($input_params{'action'} &&
965 grep { $_ eq $input_params{'action'} } @wants_base) {
966 $input_params{'hash_base'} ||= $refname;
967 } else {
968 $input_params{'hash'} ||= $refname;
969 }
970 }
971
972 # next, handle the 'parent' part, if present
973 if (defined $parentrefname) {
974 # a missing pathspec defaults to the 'current' filename, allowing e.g.
975 # someproject/blobdiff/oldrev..newrev:/filename
976 if ($parentpathname) {
977 $parentpathname =~ s,^/+,,;
978 $parentpathname =~ s,/$,,;
979 $input_params{'file_parent'} ||= $parentpathname;
980 } else {
981 $input_params{'file_parent'} ||= $input_params{'file_name'};
982 }
983 # we assume that hash_parent_base is wanted if a path was specified,
984 # or if the action wants hash_base instead of hash
985 if (defined $input_params{'file_parent'} ||
986 grep { $_ eq $input_params{'action'} } @wants_base) {
987 $input_params{'hash_parent_base'} ||= $parentrefname;
988 } else {
989 $input_params{'hash_parent'} ||= $parentrefname;
990 }
991 }
992
993 # for the snapshot action, we allow URLs in the form
994 # $project/snapshot/$hash.ext
995 # where .ext determines the snapshot and gets removed from the
996 # passed $refname to provide the $hash.
997 #
998 # To be able to tell that $refname includes the format extension, we
999 # require the following two conditions to be satisfied:
1000 # - the hash input parameter MUST have been set from the $refname part
1001 # of the URL (i.e. they must be equal)
1002 # - the snapshot format MUST NOT have been defined already (e.g. from
1003 # CGI parameter sf)
1004 # It's also useless to try any matching unless $refname has a dot,
1005 # so we check for that too
1006 if (defined $input_params{'action'} &&
1007 $input_params{'action'} eq 'snapshot' &&
1008 defined $refname && index($refname, '.') != -1 &&
1009 $refname eq $input_params{'hash'} &&
1010 !defined $input_params{'snapshot_format'}) {
1011 # We loop over the known snapshot formats, checking for
1012 # extensions. Allowed extensions are both the defined suffix
1013 # (which includes the initial dot already) and the snapshot
1014 # format key itself, with a prepended dot
1015 while (my ($fmt, $opt) = each %known_snapshot_formats) {
1016 my $hash = $refname;
1017 unless ($hash =~ s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) {
1018 next;
1019 }
1020 my $sfx = $1;
1021 # a valid suffix was found, so set the snapshot format
1022 # and reset the hash parameter
1023 $input_params{'snapshot_format'} = $fmt;
1024 $input_params{'hash'} = $hash;
1025 # we also set the format suffix to the one requested
1026 # in the URL: this way a request for e.g. .tgz returns
1027 # a .tgz instead of a .tar.gz
1028 $known_snapshot_formats{$fmt}{'suffix'} = $sfx;
1029 last;
1030 }
1031 }
1032}
1033
1034our ($action, $project, $file_name, $file_parent, $hash, $hash_parent, $hash_base,
1035 $hash_parent_base, @extra_options, $page, $searchtype, $search_use_regexp,
1036 $searchtext, $search_regexp, $project_filter);
1037sub evaluate_and_validate_params {
1038 our $action = $input_params{'action'};
1039 if (defined $action) {
1040 if (!is_valid_action($action)) {
1041 die_error(400, "Invalid action parameter");
1042 }
1043 }
1044
1045 # parameters which are pathnames
1046 our $project = $input_params{'project'};
1047 if (defined $project) {
1048 if (!is_valid_project($project)) {
1049 undef $project;
1050 die_error(404, "No such project");
1051 }
1052 }
1053
1054 our $project_filter = $input_params{'project_filter'};
1055 if (defined $project_filter) {
1056 if (!is_valid_pathname($project_filter)) {
1057 die_error(404, "Invalid project_filter parameter");
1058 }
1059 }
1060
1061 our $file_name = $input_params{'file_name'};
1062 if (defined $file_name) {
1063 if (!is_valid_pathname($file_name)) {
1064 die_error(400, "Invalid file parameter");
1065 }
1066 }
1067
1068 our $file_parent = $input_params{'file_parent'};
1069 if (defined $file_parent) {
1070 if (!is_valid_pathname($file_parent)) {
1071 die_error(400, "Invalid file parent parameter");
1072 }
1073 }
1074
1075 # parameters which are refnames
1076 our $hash = $input_params{'hash'};
1077 if (defined $hash) {
1078 if (!is_valid_refname($hash)) {
1079 die_error(400, "Invalid hash parameter");
1080 }
1081 }
1082
1083 our $hash_parent = $input_params{'hash_parent'};
1084 if (defined $hash_parent) {
1085 if (!is_valid_refname($hash_parent)) {
1086 die_error(400, "Invalid hash parent parameter");
1087 }
1088 }
1089
1090 our $hash_base = $input_params{'hash_base'};
1091 if (defined $hash_base) {
1092 if (!is_valid_refname($hash_base)) {
1093 die_error(400, "Invalid hash base parameter");
1094 }
1095 }
1096
1097 our @extra_options = @{$input_params{'extra_options'}};
1098 # @extra_options is always defined, since it can only be (currently) set from
1099 # CGI, and $cgi->param() returns the empty array in array context if the param
1100 # is not set
1101 foreach my $opt (@extra_options) {
1102 if (not exists $allowed_options{$opt}) {
1103 die_error(400, "Invalid option parameter");
1104 }
1105 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
1106 die_error(400, "Invalid option parameter for this action");
1107 }
1108 }
1109
1110 our $hash_parent_base = $input_params{'hash_parent_base'};
1111 if (defined $hash_parent_base) {
1112 if (!is_valid_refname($hash_parent_base)) {
1113 die_error(400, "Invalid hash parent base parameter");
1114 }
1115 }
1116
1117 # other parameters
1118 our $page = $input_params{'page'};
1119 if (defined $page) {
1120 if ($page =~ m/[^0-9]/) {
1121 die_error(400, "Invalid page parameter");
1122 }
1123 }
1124
1125 our $searchtype = $input_params{'searchtype'};
1126 if (defined $searchtype) {
1127 if ($searchtype =~ m/[^a-z]/) {
1128 die_error(400, "Invalid searchtype parameter");
1129 }
1130 }
1131
1132 our $search_use_regexp = $input_params{'search_use_regexp'};
1133
1134 our $searchtext = $input_params{'searchtext'};
1135 our $search_regexp = undef;
1136 if (defined $searchtext) {
1137 if (length($searchtext) < 2) {
1138 die_error(403, "At least two characters are required for search parameter");
1139 }
1140 if ($search_use_regexp) {
1141 $search_regexp = $searchtext;
1142 if (!eval { qr/$search_regexp/; 1; }) {
1143 (my $error = $@) =~ s/ at \S+ line \d+.*\n?//;
1144 die_error(400, "Invalid search regexp '$search_regexp'",
1145 esc_html($error));
1146 }
1147 } else {
1148 $search_regexp = quotemeta $searchtext;
1149 }
1150 }
1151}
1152
1153# path to the current git repository
1154our $git_dir;
1155sub evaluate_git_dir {
1156 our $git_dir = "$projectroot/$project" if $project;
1157}
1158
1159our (@snapshot_fmts, $git_avatar, @extra_branch_refs);
1160sub configure_gitweb_features {
1161 # list of supported snapshot formats
1162 our @snapshot_fmts = gitweb_get_feature('snapshot');
1163 @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
1164
1165 # check that the avatar feature is set to a known provider name,
1166 # and for each provider check if the dependencies are satisfied.
1167 # if the provider name is invalid or the dependencies are not met,
1168 # reset $git_avatar to the empty string.
1169 our ($git_avatar) = gitweb_get_feature('avatar');
1170 if ($git_avatar eq 'gravatar') {
1171 $git_avatar = '' unless (eval { require Digest::MD5; 1; });
1172 } elsif ($git_avatar eq 'picon') {
1173 # no dependencies
1174 } else {
1175 $git_avatar = '';
1176 }
1177
1178 our @extra_branch_refs = gitweb_get_feature('extra-branch-refs');
1179 @extra_branch_refs = filter_and_validate_refs (@extra_branch_refs);
1180}
1181
1182sub get_branch_refs {
1183 return ('heads', @extra_branch_refs);
1184}
1185
1186# custom error handler: 'die <message>' is Internal Server Error
1187sub handle_errors_html {
1188 my $msg = shift; # it is already HTML escaped
1189
1190 # to avoid infinite loop where error occurs in die_error,
1191 # change handler to default handler, disabling handle_errors_html
1192 set_message("Error occurred when inside die_error:\n$msg");
1193
1194 # you cannot jump out of die_error when called as error handler;
1195 # the subroutine set via CGI::Carp::set_message is called _after_
1196 # HTTP headers are already written, so it cannot write them itself
1197 die_error(undef, undef, $msg, -error_handler => 1, -no_http_header => 1);
1198}
1199set_message(\&handle_errors_html);
1200
1201# dispatch
1202sub dispatch {
1203 if (!defined $action) {
1204 if (defined $hash) {
1205 $action = git_get_type($hash);
1206 $action or die_error(404, "Object does not exist");
1207 } elsif (defined $hash_base && defined $file_name) {
1208 $action = git_get_type("$hash_base:$file_name");
1209 $action or die_error(404, "File or directory does not exist");
1210 } elsif (defined $project) {
1211 $action = 'summary';
1212 } else {
1213 $action = 'project_list';
1214 }
1215 }
1216 if (!defined($actions{$action})) {
1217 die_error(400, "Unknown action");
1218 }
1219 if ($action !~ m/^(?:opml|project_list|project_index)$/ &&
1220 !$project) {
1221 die_error(400, "Project needed");
1222 }
1223 $actions{$action}->();
1224}
1225
1226sub reset_timer {
1227 our $t0 = [ gettimeofday() ]
1228 if defined $t0;
1229 our $number_of_git_cmds = 0;
1230}
1231
1232our $first_request = 1;
1233sub run_request {
1234 reset_timer();
1235
1236 evaluate_uri();
1237 if ($first_request) {
1238 evaluate_gitweb_config();
1239 evaluate_git_version();
1240 }
1241 if ($per_request_config) {
1242 if (ref($per_request_config) eq 'CODE') {
1243 $per_request_config->();
1244 } elsif (!$first_request) {
1245 evaluate_gitweb_config();
1246 }
1247 }
1248 check_loadavg();
1249
1250 # $projectroot and $projects_list might be set in gitweb config file
1251 $projects_list ||= $projectroot;
1252
1253 evaluate_query_params();
1254 evaluate_path_info();
1255 evaluate_and_validate_params();
1256 evaluate_git_dir();
1257
1258 configure_gitweb_features();
1259
1260 dispatch();
1261}
1262
1263our $is_last_request = sub { 1 };
1264our ($pre_dispatch_hook, $post_dispatch_hook, $pre_listen_hook);
1265our $CGI = 'CGI';
1266our $cgi;
1267sub configure_as_fcgi {
1268 require CGI::Fast;
1269 our $CGI = 'CGI::Fast';
1270
1271 my $request_number = 0;
1272 # let each child service 100 requests
1273 our $is_last_request = sub { ++$request_number > 100 };
1274}
1275sub evaluate_argv {
1276 my $script_name = $ENV{'SCRIPT_NAME'} || $ENV{'SCRIPT_FILENAME'} || __FILE__;
1277 configure_as_fcgi()
1278 if $script_name =~ /\.fcgi$/;
1279
1280 return unless (@ARGV);
1281
1282 require Getopt::Long;
1283 Getopt::Long::GetOptions(
1284 'fastcgi|fcgi|f' => \&configure_as_fcgi,
1285 'nproc|n=i' => sub {
1286 my ($arg, $val) = @_;
1287 return unless eval { require FCGI::ProcManager; 1; };
1288 my $proc_manager = FCGI::ProcManager->new({
1289 n_processes => $val,
1290 });
1291 our $pre_listen_hook = sub { $proc_manager->pm_manage() };
1292 our $pre_dispatch_hook = sub { $proc_manager->pm_pre_dispatch() };
1293 our $post_dispatch_hook = sub { $proc_manager->pm_post_dispatch() };
1294 },
1295 );
1296}
1297
1298sub run {
1299 evaluate_argv();
1300
1301 $first_request = 1;
1302 $pre_listen_hook->()
1303 if $pre_listen_hook;
1304
1305 REQUEST:
1306 while ($cgi = $CGI->new()) {
1307 $pre_dispatch_hook->()
1308 if $pre_dispatch_hook;
1309
1310 run_request();
1311
1312 $post_dispatch_hook->()
1313 if $post_dispatch_hook;
1314 $first_request = 0;
1315
1316 last REQUEST if ($is_last_request->());
1317 }
1318
1319 DONE_GITWEB:
1320 1;
1321}
1322
1323run();
1324
1325if (defined caller) {
1326 # wrapped in a subroutine processing requests,
1327 # e.g. mod_perl with ModPerl::Registry, or PSGI with Plack::App::WrapCGI
1328 return;
1329} else {
1330 # pure CGI script, serving single request
1331 exit;
1332}
1333
1334## ======================================================================
1335## action links
1336
1337# possible values of extra options
1338# -full => 0|1 - use absolute/full URL ($my_uri/$my_url as base)
1339# -replay => 1 - start from a current view (replay with modifications)
1340# -path_info => 0|1 - don't use/use path_info URL (if possible)
1341# -anchor => ANCHOR - add #ANCHOR to end of URL, implies -replay if used alone
1342sub href {
1343 my %params = @_;
1344 # default is to use -absolute url() i.e. $my_uri
1345 my $href = $params{-full} ? $my_url : $my_uri;
1346
1347 # implicit -replay, must be first of implicit params
1348 $params{-replay} = 1 if (keys %params == 1 && $params{-anchor});
1349
1350 $params{'project'} = $project unless exists $params{'project'};
1351
1352 if ($params{-replay}) {
1353 while (my ($name, $symbol) = each %cgi_param_mapping) {
1354 if (!exists $params{$name}) {
1355 $params{$name} = $input_params{$name};
1356 }
1357 }
1358 }
1359
1360 my $use_pathinfo = gitweb_check_feature('pathinfo');
1361 if (defined $params{'project'} &&
1362 (exists $params{-path_info} ? $params{-path_info} : $use_pathinfo)) {
1363 # try to put as many parameters as possible in PATH_INFO:
1364 # - project name
1365 # - action
1366 # - hash_parent or hash_parent_base:/file_parent
1367 # - hash or hash_base:/filename
1368 # - the snapshot_format as an appropriate suffix
1369
1370 # When the script is the root DirectoryIndex for the domain,
1371 # $href here would be something like http://gitweb.example.com/
1372 # Thus, we strip any trailing / from $href, to spare us double
1373 # slashes in the final URL
1374 $href =~ s,/$,,;
1375
1376 # Then add the project name, if present
1377 $href .= "/".esc_path_info($params{'project'});
1378 delete $params{'project'};
1379
1380 # since we destructively absorb parameters, we keep this
1381 # boolean that remembers if we're handling a snapshot
1382 my $is_snapshot = $params{'action'} eq 'snapshot';
1383
1384 # Summary just uses the project path URL, any other action is
1385 # added to the URL
1386 if (defined $params{'action'}) {
1387 $href .= "/".esc_path_info($params{'action'})
1388 unless $params{'action'} eq 'summary';
1389 delete $params{'action'};
1390 }
1391
1392 # Next, we put hash_parent_base:/file_parent..hash_base:/file_name,
1393 # stripping nonexistent or useless pieces
1394 $href .= "/" if ($params{'hash_base'} || $params{'hash_parent_base'}
1395 || $params{'hash_parent'} || $params{'hash'});
1396 if (defined $params{'hash_base'}) {
1397 if (defined $params{'hash_parent_base'}) {
1398 $href .= esc_path_info($params{'hash_parent_base'});
1399 # skip the file_parent if it's the same as the file_name
1400 if (defined $params{'file_parent'}) {
1401 if (defined $params{'file_name'} && $params{'file_parent'} eq $params{'file_name'}) {
1402 delete $params{'file_parent'};
1403 } elsif ($params{'file_parent'} !~ /\.\./) {
1404 $href .= ":/".esc_path_info($params{'file_parent'});
1405 delete $params{'file_parent'};
1406 }
1407 }
1408 $href .= "..";
1409 delete $params{'hash_parent'};
1410 delete $params{'hash_parent_base'};
1411 } elsif (defined $params{'hash_parent'}) {
1412 $href .= esc_path_info($params{'hash_parent'}). "..";
1413 delete $params{'hash_parent'};
1414 }
1415
1416 $href .= esc_path_info($params{'hash_base'});
1417 if (defined $params{'file_name'} && $params{'file_name'} !~ /\.\./) {
1418 $href .= ":/".esc_path_info($params{'file_name'});
1419 delete $params{'file_name'};
1420 }
1421 delete $params{'hash'};
1422 delete $params{'hash_base'};
1423 } elsif (defined $params{'hash'}) {
1424 $href .= esc_path_info($params{'hash'});
1425 delete $params{'hash'};
1426 }
1427
1428 # If the action was a snapshot, we can absorb the
1429 # snapshot_format parameter too
1430 if ($is_snapshot) {
1431 my $fmt = $params{'snapshot_format'};
1432 # snapshot_format should always be defined when href()
1433 # is called, but just in case some code forgets, we
1434 # fall back to the default
1435 $fmt ||= $snapshot_fmts[0];
1436 $href .= $known_snapshot_formats{$fmt}{'suffix'};
1437 delete $params{'snapshot_format'};
1438 }
1439 }
1440
1441 # now encode the parameters explicitly
1442 my @result = ();
1443 for (my $i = 0; $i < @cgi_param_mapping; $i += 2) {
1444 my ($name, $symbol) = ($cgi_param_mapping[$i], $cgi_param_mapping[$i+1]);
1445 if (defined $params{$name}) {
1446 if (ref($params{$name}) eq "ARRAY") {
1447 foreach my $par (@{$params{$name}}) {
1448 push @result, $symbol . "=" . esc_param($par);
1449 }
1450 } else {
1451 push @result, $symbol . "=" . esc_param($params{$name});
1452 }
1453 }
1454 }
1455 $href .= "?" . join(';', @result) if scalar @result;
1456
1457 # final transformation: trailing spaces must be escaped (URI-encoded)
1458 $href =~ s/(\s+)$/CGI::escape($1)/e;
1459
1460 if ($params{-anchor}) {
1461 $href .= "#".esc_param($params{-anchor});
1462 }
1463
1464 return $href;
1465}
1466
1467
1468## ======================================================================
1469## validation, quoting/unquoting and escaping
1470
1471sub is_valid_action {
1472 my $input = shift;
1473 return undef unless exists $actions{$input};
1474 return 1;
1475}
1476
1477sub is_valid_project {
1478 my $input = shift;
1479
1480 return unless defined $input;
1481 if (!is_valid_pathname($input) ||
1482 !(-d "$projectroot/$input") ||
1483 !check_export_ok("$projectroot/$input") ||
1484 ($strict_export && !project_in_list($input))) {
1485 return undef;
1486 } else {
1487 return 1;
1488 }
1489}
1490
1491sub is_valid_pathname {
1492 my $input = shift;
1493
1494 return undef unless defined $input;
1495 # no '.' or '..' as elements of path, i.e. no '.' or '..'
1496 # at the beginning, at the end, and between slashes.
1497 # also this catches doubled slashes
1498 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
1499 return undef;
1500 }
1501 # no null characters
1502 if ($input =~ m!\0!) {
1503 return undef;
1504 }
1505 return 1;
1506}
1507
1508sub is_valid_ref_format {
1509 my $input = shift;
1510
1511 return undef unless defined $input;
1512 # restrictions on ref name according to git-check-ref-format
1513 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
1514 return undef;
1515 }
1516 return 1;
1517}
1518
1519sub is_valid_refname {
1520 my $input = shift;
1521
1522 return undef unless defined $input;
1523 # textual hashes are O.K.
1524 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
1525 return 1;
1526 }
1527 # it must be correct pathname
1528 is_valid_pathname($input) or return undef;
1529 # check git-check-ref-format restrictions
1530 is_valid_ref_format($input) or return undef;
1531 return 1;
1532}
1533
1534# decode sequences of octets in utf8 into Perl's internal form,
1535# which is utf-8 with utf8 flag set if needed. gitweb writes out
1536# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
1537sub to_utf8 {
1538 my $str = shift;
1539 return undef unless defined $str;
1540
1541 if (utf8::is_utf8($str) || utf8::decode($str)) {
1542 return $str;
1543 } else {
1544 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
1545 }
1546}
1547
1548# quote unsafe chars, but keep the slash, even when it's not
1549# correct, but quoted slashes look too horrible in bookmarks
1550sub esc_param {
1551 my $str = shift;
1552 return undef unless defined $str;
1553 $str =~ s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;
1554 $str =~ s/ /\+/g;
1555 return $str;
1556}
1557
1558# the quoting rules for path_info fragment are slightly different
1559sub esc_path_info {
1560 my $str = shift;
1561 return undef unless defined $str;
1562
1563 # path_info doesn't treat '+' as space (specially), but '?' must be escaped
1564 $str =~ s/([^A-Za-z0-9\-_.~();\/;:@&= +]+)/CGI::escape($1)/eg;
1565
1566 return $str;
1567}
1568
1569# quote unsafe chars in whole URL, so some characters cannot be quoted
1570sub esc_url {
1571 my $str = shift;
1572 return undef unless defined $str;
1573 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&= ]+)/CGI::escape($1)/eg;
1574 $str =~ s/ /\+/g;
1575 return $str;
1576}
1577
1578# quote unsafe characters in HTML attributes
1579sub esc_attr {
1580
1581 # for XHTML conformance escaping '"' to '&quot;' is not enough
1582 return esc_html(@_);
1583}
1584
1585# replace invalid utf8 character with SUBSTITUTION sequence
1586sub esc_html {
1587 my $str = shift;
1588 my %opts = @_;
1589
1590 return undef unless defined $str;
1591
1592 $str = to_utf8($str);
1593 $str = $cgi->escapeHTML($str);
1594 if ($opts{'-nbsp'}) {
1595 $str =~ s/ /&nbsp;/g;
1596 }
1597 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
1598 return $str;
1599}
1600
1601# quote control characters and escape filename to HTML
1602sub esc_path {
1603 my $str = shift;
1604 my %opts = @_;
1605
1606 return undef unless defined $str;
1607
1608 $str = to_utf8($str);
1609 $str = $cgi->escapeHTML($str);
1610 if ($opts{'-nbsp'}) {
1611 $str =~ s/ /&nbsp;/g;
1612 }
1613 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
1614 return $str;
1615}
1616
1617# Sanitize for use in XHTML + application/xml+xhtml (valid XML 1.0)
1618sub sanitize {
1619 my $str = shift;
1620
1621 return undef unless defined $str;
1622
1623 $str = to_utf8($str);
1624 $str =~ s|([[:cntrl:]])|(index("\t\n\r", $1) != -1 ? $1 : quot_cec($1))|eg;
1625 return $str;
1626}
1627
1628# Make control characters "printable", using character escape codes (CEC)
1629sub quot_cec {
1630 my $cntrl = shift;
1631 my %opts = @_;
1632 my %es = ( # character escape codes, aka escape sequences
1633 "\t" => '\t', # tab (HT)
1634 "\n" => '\n', # line feed (LF)
1635 "\r" => '\r', # carrige return (CR)
1636 "\f" => '\f', # form feed (FF)
1637 "\b" => '\b', # backspace (BS)
1638 "\a" => '\a', # alarm (bell) (BEL)
1639 "\e" => '\e', # escape (ESC)
1640 "\013" => '\v', # vertical tab (VT)
1641 "\000" => '\0', # nul character (NUL)
1642 );
1643 my $chr = ( (exists $es{$cntrl})
1644 ? $es{$cntrl}
1645 : sprintf('\%2x', ord($cntrl)) );
1646 if ($opts{-nohtml}) {
1647 return $chr;
1648 } else {
1649 return "<span class=\"cntrl\">$chr</span>";
1650 }
1651}
1652
1653# Alternatively use unicode control pictures codepoints,
1654# Unicode "printable representation" (PR)
1655sub quot_upr {
1656 my $cntrl = shift;
1657 my %opts = @_;
1658
1659 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
1660 if ($opts{-nohtml}) {
1661 return $chr;
1662 } else {
1663 return "<span class=\"cntrl\">$chr</span>";
1664 }
1665}
1666
1667# git may return quoted and escaped filenames
1668sub unquote {
1669 my $str = shift;
1670
1671 sub unq {
1672 my $seq = shift;
1673 my %es = ( # character escape codes, aka escape sequences
1674 't' => "\t", # tab (HT, TAB)
1675 'n' => "\n", # newline (NL)
1676 'r' => "\r", # return (CR)
1677 'f' => "\f", # form feed (FF)
1678 'b' => "\b", # backspace (BS)
1679 'a' => "\a", # alarm (bell) (BEL)
1680 'e' => "\e", # escape (ESC)
1681 'v' => "\013", # vertical tab (VT)
1682 );
1683
1684 if ($seq =~ m/^[0-7]{1,3}$/) {
1685 # octal char sequence
1686 return chr(oct($seq));
1687 } elsif (exists $es{$seq}) {
1688 # C escape sequence, aka character escape code
1689 return $es{$seq};
1690 }
1691 # quoted ordinary character
1692 return $seq;
1693 }
1694
1695 if ($str =~ m/^"(.*)"$/) {
1696 # needs unquoting
1697 $str = $1;
1698 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
1699 }
1700 return $str;
1701}
1702
1703# escape tabs (convert tabs to spaces)
1704sub untabify {
1705 my $line = shift;
1706
1707 while ((my $pos = index($line, "\t")) != -1) {
1708 if (my $count = (8 - ($pos % 8))) {
1709 my $spaces = ' ' x $count;
1710 $line =~ s/\t/$spaces/;
1711 }
1712 }
1713
1714 return $line;
1715}
1716
1717sub project_in_list {
1718 my $project = shift;
1719 my @list = git_get_projects_list();
1720 return @list && scalar(grep { $_->{'path'} eq $project } @list);
1721}
1722
1723## ----------------------------------------------------------------------
1724## HTML aware string manipulation
1725
1726# Try to chop given string on a word boundary between position
1727# $len and $len+$add_len. If there is no word boundary there,
1728# chop at $len+$add_len. Do not chop if chopped part plus ellipsis
1729# (marking chopped part) would be longer than given string.
1730sub chop_str {
1731 my $str = shift;
1732 my $len = shift;
1733 my $add_len = shift || 10;
1734 my $where = shift || 'right'; # 'left' | 'center' | 'right'
1735
1736 # Make sure perl knows it is utf8 encoded so we don't
1737 # cut in the middle of a utf8 multibyte char.
1738 $str = to_utf8($str);
1739
1740 # allow only $len chars, but don't cut a word if it would fit in $add_len
1741 # if it doesn't fit, cut it if it's still longer than the dots we would add
1742 # remove chopped character entities entirely
1743
1744 # when chopping in the middle, distribute $len into left and right part
1745 # return early if chopping wouldn't make string shorter
1746 if ($where eq 'center') {
1747 return $str if ($len + 5 >= length($str)); # filler is length 5
1748 $len = int($len/2);
1749 } else {
1750 return $str if ($len + 4 >= length($str)); # filler is length 4
1751 }
1752
1753 # regexps: ending and beginning with word part up to $add_len
1754 my $endre = qr/.{$len}\w{0,$add_len}/;
1755 my $begre = qr/\w{0,$add_len}.{$len}/;
1756
1757 if ($where eq 'left') {
1758 $str =~ m/^(.*?)($begre)$/;
1759 my ($lead, $body) = ($1, $2);
1760 if (length($lead) > 4) {
1761 $lead = " ...";
1762 }
1763 return "$lead$body";
1764
1765 } elsif ($where eq 'center') {
1766 $str =~ m/^($endre)(.*)$/;
1767 my ($left, $str) = ($1, $2);
1768 $str =~ m/^(.*?)($begre)$/;
1769 my ($mid, $right) = ($1, $2);
1770 if (length($mid) > 5) {
1771 $mid = " ... ";
1772 }
1773 return "$left$mid$right";
1774
1775 } else {
1776 $str =~ m/^($endre)(.*)$/;
1777 my $body = $1;
1778 my $tail = $2;
1779 if (length($tail) > 4) {
1780 $tail = "... ";
1781 }
1782 return "$body$tail";
1783 }
1784}
1785
1786# takes the same arguments as chop_str, but also wraps a <span> around the
1787# result with a title attribute if it does get chopped. Additionally, the
1788# string is HTML-escaped.
1789sub chop_and_escape_str {
1790 my ($str) = @_;
1791
1792 my $chopped = chop_str(@_);
1793 $str = to_utf8($str);
1794 if ($chopped eq $str) {
1795 return esc_html($chopped);
1796 } else {
1797 $str =~ s/[[:cntrl:]]/?/g;
1798 return $cgi->span({-title=>$str}, esc_html($chopped));
1799 }
1800}
1801
1802# Highlight selected fragments of string, using given CSS class,
1803# and escape HTML. It is assumed that fragments do not overlap.
1804# Regions are passed as list of pairs (array references).
1805#
1806# Example: esc_html_hl_regions("foobar", "mark", [ 0, 3 ]) returns
1807# '<span class="mark">foo</span>bar'
1808sub esc_html_hl_regions {
1809 my ($str, $css_class, @sel) = @_;
1810 my %opts = grep { ref($_) ne 'ARRAY' } @sel;
1811 @sel = grep { ref($_) eq 'ARRAY' } @sel;
1812 return esc_html($str, %opts) unless @sel;
1813
1814 my $out = '';
1815 my $pos = 0;
1816
1817 for my $s (@sel) {
1818 my ($begin, $end) = @$s;
1819
1820 # Don't create empty <span> elements.
1821 next if $end <= $begin;
1822
1823 my $escaped = esc_html(substr($str, $begin, $end - $begin),
1824 %opts);
1825
1826 $out .= esc_html(substr($str, $pos, $begin - $pos), %opts)
1827 if ($begin - $pos > 0);
1828 $out .= $cgi->span({-class => $css_class}, $escaped);
1829
1830 $pos = $end;
1831 }
1832 $out .= esc_html(substr($str, $pos), %opts)
1833 if ($pos < length($str));
1834
1835 return $out;
1836}
1837
1838# return positions of beginning and end of each match
1839sub matchpos_list {
1840 my ($str, $regexp) = @_;
1841 return unless (defined $str && defined $regexp);
1842
1843 my @matches;
1844 while ($str =~ /$regexp/g) {
1845 push @matches, [$-[0], $+[0]];
1846 }
1847 return @matches;
1848}
1849
1850# highlight match (if any), and escape HTML
1851sub esc_html_match_hl {
1852 my ($str, $regexp) = @_;
1853 return esc_html($str) unless defined $regexp;
1854
1855 my @matches = matchpos_list($str, $regexp);
1856 return esc_html($str) unless @matches;
1857
1858 return esc_html_hl_regions($str, 'match', @matches);
1859}
1860
1861
1862# highlight match (if any) of shortened string, and escape HTML
1863sub esc_html_match_hl_chopped {
1864 my ($str, $chopped, $regexp) = @_;
1865 return esc_html_match_hl($str, $regexp) unless defined $chopped;
1866
1867 my @matches = matchpos_list($str, $regexp);
1868 return esc_html($chopped) unless @matches;
1869
1870 # filter matches so that we mark chopped string
1871 my $tail = "... "; # see chop_str
1872 unless ($chopped =~ s/\Q$tail\E$//) {
1873 $tail = '';
1874 }
1875 my $chop_len = length($chopped);
1876 my $tail_len = length($tail);
1877 my @filtered;
1878
1879 for my $m (@matches) {
1880 if ($m->[0] > $chop_len) {
1881 push @filtered, [ $chop_len, $chop_len + $tail_len ] if ($tail_len > 0);
1882 last;
1883 } elsif ($m->[1] > $chop_len) {
1884 push @filtered, [ $m->[0], $chop_len + $tail_len ];
1885 last;
1886 }
1887 push @filtered, $m;
1888 }
1889
1890 return esc_html_hl_regions($chopped . $tail, 'match', @filtered);
1891}
1892
1893## ----------------------------------------------------------------------
1894## functions returning short strings
1895
1896# CSS class for given age value (in seconds)
1897sub age_class {
1898 my $age = shift;
1899
1900 if (!defined $age) {
1901 return "noage";
1902 } elsif ($age < 60*60*2) {
1903 return "age0";
1904 } elsif ($age < 60*60*24*2) {
1905 return "age1";
1906 } else {
1907 return "age2";
1908 }
1909}
1910
1911# convert age in seconds to "nn units ago" string
1912sub age_string {
1913 my $age = shift;
1914 my $age_str;
1915
1916 if ($age > 60*60*24*365*2) {
1917 $age_str = (int $age/60/60/24/365);
1918 $age_str .= " years ago";
1919 } elsif ($age > 60*60*24*(365/12)*2) {
1920 $age_str = int $age/60/60/24/(365/12);
1921 $age_str .= " months ago";
1922 } elsif ($age > 60*60*24*7*2) {
1923 $age_str = int $age/60/60/24/7;
1924 $age_str .= " weeks ago";
1925 } elsif ($age > 60*60*24*2) {
1926 $age_str = int $age/60/60/24;
1927 $age_str .= " days ago";
1928 } elsif ($age > 60*60*2) {
1929 $age_str = int $age/60/60;
1930 $age_str .= " hours ago";
1931 } elsif ($age > 60*2) {
1932 $age_str = int $age/60;
1933 $age_str .= " min ago";
1934 } elsif ($age > 2) {
1935 $age_str = int $age;
1936 $age_str .= " sec ago";
1937 } else {
1938 $age_str .= " right now";
1939 }
1940 return $age_str;
1941}
1942
1943use constant {
1944 S_IFINVALID => 0030000,
1945 S_IFGITLINK => 0160000,
1946};
1947
1948# submodule/subproject, a commit object reference
1949sub S_ISGITLINK {
1950 my $mode = shift;
1951
1952 return (($mode & S_IFMT) == S_IFGITLINK)
1953}
1954
1955# convert file mode in octal to symbolic file mode string
1956sub mode_str {
1957 my $mode = oct shift;
1958
1959 if (S_ISGITLINK($mode)) {
1960 return 'm---------';
1961 } elsif (S_ISDIR($mode & S_IFMT)) {
1962 return 'drwxr-xr-x';
1963 } elsif (S_ISLNK($mode)) {
1964 return 'lrwxrwxrwx';
1965 } elsif (S_ISREG($mode)) {
1966 # git cares only about the executable bit
1967 if ($mode & S_IXUSR) {
1968 return '-rwxr-xr-x';
1969 } else {
1970 return '-rw-r--r--';
1971 };
1972 } else {
1973 return '----------';
1974 }
1975}
1976
1977# convert file mode in octal to file type string
1978sub file_type {
1979 my $mode = shift;
1980
1981 if ($mode !~ m/^[0-7]+$/) {
1982 return $mode;
1983 } else {
1984 $mode = oct $mode;
1985 }
1986
1987 if (S_ISGITLINK($mode)) {
1988 return "submodule";
1989 } elsif (S_ISDIR($mode & S_IFMT)) {
1990 return "directory";
1991 } elsif (S_ISLNK($mode)) {
1992 return "symlink";
1993 } elsif (S_ISREG($mode)) {
1994 return "file";
1995 } else {
1996 return "unknown";
1997 }
1998}
1999
2000# convert file mode in octal to file type description string
2001sub file_type_long {
2002 my $mode = shift;
2003
2004 if ($mode !~ m/^[0-7]+$/) {
2005 return $mode;
2006 } else {
2007 $mode = oct $mode;
2008 }
2009
2010 if (S_ISGITLINK($mode)) {
2011 return "submodule";
2012 } elsif (S_ISDIR($mode & S_IFMT)) {
2013 return "directory";
2014 } elsif (S_ISLNK($mode)) {
2015 return "symlink";
2016 } elsif (S_ISREG($mode)) {
2017 if ($mode & S_IXUSR) {
2018 return "executable";
2019 } else {
2020 return "file";
2021 };
2022 } else {
2023 return "unknown";
2024 }
2025}
2026
2027
2028## ----------------------------------------------------------------------
2029## functions returning short HTML fragments, or transforming HTML fragments
2030## which don't belong to other sections
2031
2032# format line of commit message.
2033sub format_log_line_html {
2034 my $line = shift;
2035
2036 $line = esc_html($line, -nbsp=>1);
2037 $line =~ s{
2038 \b
2039 (
2040 # The output of "git describe", e.g. v2.10.0-297-gf6727b0
2041 # or hadoop-20160921-113441-20-g094fb7d
2042 (?<!-) # see strbuf_check_tag_ref(). Tags can't start with -
2043 [A-Za-z0-9.-]+
2044 (?!\.) # refs can't end with ".", see check_refname_format()
2045 -g[0-9a-fA-F]{7,40}
2046 |
2047 # Just a normal looking Git SHA1
2048 [0-9a-fA-F]{7,40}
2049 )
2050 \b
2051 }{
2052 $cgi->a({-href => href(action=>"object", hash=>$1),
2053 -class => "text"}, $1);
2054 }egx;
2055
2056 return $line;
2057}
2058
2059# format marker of refs pointing to given object
2060
2061# the destination action is chosen based on object type and current context:
2062# - for annotated tags, we choose the tag view unless it's the current view
2063# already, in which case we go to shortlog view
2064# - for other refs, we keep the current view if we're in history, shortlog or
2065# log view, and select shortlog otherwise
2066sub format_ref_marker {
2067 my ($refs, $id) = @_;
2068 my $markers = '';
2069
2070 if (defined $refs->{$id}) {
2071 foreach my $ref (@{$refs->{$id}}) {
2072 # this code exploits the fact that non-lightweight tags are the
2073 # only indirect objects, and that they are the only objects for which
2074 # we want to use tag instead of shortlog as action
2075 my ($type, $name) = qw();
2076 my $indirect = ($ref =~ s/\^\{\}$//);
2077 # e.g. tags/v2.6.11 or heads/next
2078 if ($ref =~ m!^(.*?)s?/(.*)$!) {
2079 $type = $1;
2080 $name = $2;
2081 } else {
2082 $type = "ref";
2083 $name = $ref;
2084 }
2085
2086 my $class = $type;
2087 $class .= " indirect" if $indirect;
2088
2089 my $dest_action = "shortlog";
2090
2091 if ($indirect) {
2092 $dest_action = "tag" unless $action eq "tag";
2093 } elsif ($action =~ /^(history|(short)?log)$/) {
2094 $dest_action = $action;
2095 }
2096
2097 my $dest = "";
2098 $dest .= "refs/" unless $ref =~ m!^refs/!;
2099 $dest .= $ref;
2100
2101 my $link = $cgi->a({
2102 -href => href(
2103 action=>$dest_action,
2104 hash=>$dest
2105 )}, esc_html($name));
2106
2107 $markers .= " <span class=\"".esc_attr($class)."\" title=\"".esc_attr($ref)."\">" .
2108 $link . "</span>";
2109 }
2110 }
2111
2112 if ($markers) {
2113 return ' <span class="refs">'. $markers . '</span>';
2114 } else {
2115 return "";
2116 }
2117}
2118
2119# format, perhaps shortened and with markers, title line
2120sub format_subject_html {
2121 my ($long, $short, $href, $extra) = @_;
2122 $extra = '' unless defined($extra);
2123
2124 if (length($short) < length($long)) {
2125 $long =~ s/[[:cntrl:]]/?/g;
2126 return $cgi->a({-href => $href, -class => "list subject",
2127 -title => to_utf8($long)},
2128 esc_html($short)) . $extra;
2129 } else {
2130 return $cgi->a({-href => $href, -class => "list subject"},
2131 esc_html($long)) . $extra;
2132 }
2133}
2134
2135# Rather than recomputing the url for an email multiple times, we cache it
2136# after the first hit. This gives a visible benefit in views where the avatar
2137# for the same email is used repeatedly (e.g. shortlog).
2138# The cache is shared by all avatar engines (currently gravatar only), which
2139# are free to use it as preferred. Since only one avatar engine is used for any
2140# given page, there's no risk for cache conflicts.
2141our %avatar_cache = ();
2142
2143# Compute the picon url for a given email, by using the picon search service over at
2144# http://www.cs.indiana.edu/picons/search.html
2145sub picon_url {
2146 my $email = lc shift;
2147 if (!$avatar_cache{$email}) {
2148 my ($user, $domain) = split('@', $email);
2149 $avatar_cache{$email} =
2150 "//www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/" .
2151 "$domain/$user/" .
2152 "users+domains+unknown/up/single";
2153 }
2154 return $avatar_cache{$email};
2155}
2156
2157# Compute the gravatar url for a given email, if it's not in the cache already.
2158# Gravatar stores only the part of the URL before the size, since that's the
2159# one computationally more expensive. This also allows reuse of the cache for
2160# different sizes (for this particular engine).
2161sub gravatar_url {
2162 my $email = lc shift;
2163 my $size = shift;
2164 $avatar_cache{$email} ||=
2165 "//www.gravatar.com/avatar/" .
2166 Digest::MD5::md5_hex($email) . "?s=";
2167 return $avatar_cache{$email} . $size;
2168}
2169
2170# Insert an avatar for the given $email at the given $size if the feature
2171# is enabled.
2172sub git_get_avatar {
2173 my ($email, %opts) = @_;
2174 my $pre_white = ($opts{-pad_before} ? "&nbsp;" : "");
2175 my $post_white = ($opts{-pad_after} ? "&nbsp;" : "");
2176 $opts{-size} ||= 'default';
2177 my $size = $avatar_size{$opts{-size}} || $avatar_size{'default'};
2178 my $url = "";
2179 if ($git_avatar eq 'gravatar') {
2180 $url = gravatar_url($email, $size);
2181 } elsif ($git_avatar eq 'picon') {
2182 $url = picon_url($email);
2183 }
2184 # Other providers can be added by extending the if chain, defining $url
2185 # as needed. If no variant puts something in $url, we assume avatars
2186 # are completely disabled/unavailable.
2187 if ($url) {
2188 return $pre_white .
2189 "<img width=\"$size\" " .
2190 "class=\"avatar\" " .
2191 "src=\"".esc_url($url)."\" " .
2192 "alt=\"\" " .
2193 "/>" . $post_white;
2194 } else {
2195 return "";
2196 }
2197}
2198
2199sub format_search_author {
2200 my ($author, $searchtype, $displaytext) = @_;
2201 my $have_search = gitweb_check_feature('search');
2202
2203 if ($have_search) {
2204 my $performed = "";
2205 if ($searchtype eq 'author') {
2206 $performed = "authored";
2207 } elsif ($searchtype eq 'committer') {
2208 $performed = "committed";
2209 }
2210
2211 return $cgi->a({-href => href(action=>"search", hash=>$hash,
2212 searchtext=>$author,
2213 searchtype=>$searchtype), class=>"list",
2214 title=>"Search for commits $performed by $author"},
2215 $displaytext);
2216
2217 } else {
2218 return $displaytext;
2219 }
2220}
2221
2222# format the author name of the given commit with the given tag
2223# the author name is chopped and escaped according to the other
2224# optional parameters (see chop_str).
2225sub format_author_html {
2226 my $tag = shift;
2227 my $co = shift;
2228 my $author = chop_and_escape_str($co->{'author_name'}, @_);
2229 return "<$tag class=\"author\">" .
2230 format_search_author($co->{'author_name'}, "author",
2231 git_get_avatar($co->{'author_email'}, -pad_after => 1) .
2232 $author) .
2233 "</$tag>";
2234}
2235
2236# format git diff header line, i.e. "diff --(git|combined|cc) ..."
2237sub format_git_diff_header_line {
2238 my $line = shift;
2239 my $diffinfo = shift;
2240 my ($from, $to) = @_;
2241
2242 if ($diffinfo->{'nparents'}) {
2243 # combined diff
2244 $line =~ s!^(diff (.*?) )"?.*$!$1!;
2245 if ($to->{'href'}) {
2246 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
2247 esc_path($to->{'file'}));
2248 } else { # file was deleted (no href)
2249 $line .= esc_path($to->{'file'});
2250 }
2251 } else {
2252 # "ordinary" diff
2253 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
2254 if ($from->{'href'}) {
2255 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
2256 'a/' . esc_path($from->{'file'}));
2257 } else { # file was added (no href)
2258 $line .= 'a/' . esc_path($from->{'file'});
2259 }
2260 $line .= ' ';
2261 if ($to->{'href'}) {
2262 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
2263 'b/' . esc_path($to->{'file'}));
2264 } else { # file was deleted
2265 $line .= 'b/' . esc_path($to->{'file'});
2266 }
2267 }
2268
2269 return "<div class=\"diff header\">$line</div>\n";
2270}
2271
2272# format extended diff header line, before patch itself
2273sub format_extended_diff_header_line {
2274 my $line = shift;
2275 my $diffinfo = shift;
2276 my ($from, $to) = @_;
2277
2278 # match <path>
2279 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
2280 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
2281 esc_path($from->{'file'}));
2282 }
2283 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
2284 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
2285 esc_path($to->{'file'}));
2286 }
2287 # match single <mode>
2288 if ($line =~ m/\s(\d{6})$/) {
2289 $line .= '<span class="info"> (' .
2290 file_type_long($1) .
2291 ')</span>';
2292 }
2293 # match <hash>
2294 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
2295 # can match only for combined diff
2296 $line = 'index ';
2297 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2298 if ($from->{'href'}[$i]) {
2299 $line .= $cgi->a({-href=>$from->{'href'}[$i],
2300 -class=>"hash"},
2301 substr($diffinfo->{'from_id'}[$i],0,7));
2302 } else {
2303 $line .= '0' x 7;
2304 }
2305 # separator
2306 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
2307 }
2308 $line .= '..';
2309 if ($to->{'href'}) {
2310 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
2311 substr($diffinfo->{'to_id'},0,7));
2312 } else {
2313 $line .= '0' x 7;
2314 }
2315
2316 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
2317 # can match only for ordinary diff
2318 my ($from_link, $to_link);
2319 if ($from->{'href'}) {
2320 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
2321 substr($diffinfo->{'from_id'},0,7));
2322 } else {
2323 $from_link = '0' x 7;
2324 }
2325 if ($to->{'href'}) {
2326 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
2327 substr($diffinfo->{'to_id'},0,7));
2328 } else {
2329 $to_link = '0' x 7;
2330 }
2331 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
2332 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
2333 }
2334
2335 return $line . "<br/>\n";
2336}
2337
2338# format from-file/to-file diff header
2339sub format_diff_from_to_header {
2340 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
2341 my $line;
2342 my $result = '';
2343
2344 $line = $from_line;
2345 #assert($line =~ m/^---/) if DEBUG;
2346 # no extra formatting for "^--- /dev/null"
2347 if (! $diffinfo->{'nparents'}) {
2348 # ordinary (single parent) diff
2349 if ($line =~ m!^--- "?a/!) {
2350 if ($from->{'href'}) {
2351 $line = '--- a/' .
2352 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
2353 esc_path($from->{'file'}));
2354 } else {
2355 $line = '--- a/' .
2356 esc_path($from->{'file'});
2357 }
2358 }
2359 $result .= qq!<div class="diff from_file">$line</div>\n!;
2360
2361 } else {
2362 # combined diff (merge commit)
2363 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2364 if ($from->{'href'}[$i]) {
2365 $line = '--- ' .
2366 $cgi->a({-href=>href(action=>"blobdiff",
2367 hash_parent=>$diffinfo->{'from_id'}[$i],
2368 hash_parent_base=>$parents[$i],
2369 file_parent=>$from->{'file'}[$i],
2370 hash=>$diffinfo->{'to_id'},
2371 hash_base=>$hash,
2372 file_name=>$to->{'file'}),
2373 -class=>"path",
2374 -title=>"diff" . ($i+1)},
2375 $i+1) .
2376 '/' .
2377 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
2378 esc_path($from->{'file'}[$i]));
2379 } else {
2380 $line = '--- /dev/null';
2381 }
2382 $result .= qq!<div class="diff from_file">$line</div>\n!;
2383 }
2384 }
2385
2386 $line = $to_line;
2387 #assert($line =~ m/^\+\+\+/) if DEBUG;
2388 # no extra formatting for "^+++ /dev/null"
2389 if ($line =~ m!^\+\+\+ "?b/!) {
2390 if ($to->{'href'}) {
2391 $line = '+++ b/' .
2392 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
2393 esc_path($to->{'file'}));
2394 } else {
2395 $line = '+++ b/' .
2396 esc_path($to->{'file'});
2397 }
2398 }
2399 $result .= qq!<div class="diff to_file">$line</div>\n!;
2400
2401 return $result;
2402}
2403
2404# create note for patch simplified by combined diff
2405sub format_diff_cc_simplified {
2406 my ($diffinfo, @parents) = @_;
2407 my $result = '';
2408
2409 $result .= "<div class=\"diff header\">" .
2410 "diff --cc ";
2411 if (!is_deleted($diffinfo)) {
2412 $result .= $cgi->a({-href => href(action=>"blob",
2413 hash_base=>$hash,
2414 hash=>$diffinfo->{'to_id'},
2415 file_name=>$diffinfo->{'to_file'}),
2416 -class => "path"},
2417 esc_path($diffinfo->{'to_file'}));
2418 } else {
2419 $result .= esc_path($diffinfo->{'to_file'});
2420 }
2421 $result .= "</div>\n" . # class="diff header"
2422 "<div class=\"diff nodifferences\">" .
2423 "Simple merge" .
2424 "</div>\n"; # class="diff nodifferences"
2425
2426 return $result;
2427}
2428
2429sub diff_line_class {
2430 my ($line, $from, $to) = @_;
2431
2432 # ordinary diff
2433 my $num_sign = 1;
2434 # combined diff
2435 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
2436 $num_sign = scalar @{$from->{'href'}};
2437 }
2438
2439 my @diff_line_classifier = (
2440 { regexp => qr/^\@\@{$num_sign} /, class => "chunk_header"},
2441 { regexp => qr/^\\/, class => "incomplete" },
2442 { regexp => qr/^ {$num_sign}/, class => "ctx" },
2443 # classifier for context must come before classifier add/rem,
2444 # or we would have to use more complicated regexp, for example
2445 # qr/(?= {0,$m}\+)[+ ]{$num_sign}/, where $m = $num_sign - 1;
2446 { regexp => qr/^[+ ]{$num_sign}/, class => "add" },
2447 { regexp => qr/^[- ]{$num_sign}/, class => "rem" },
2448 );
2449 for my $clsfy (@diff_line_classifier) {
2450 return $clsfy->{'class'}
2451 if ($line =~ $clsfy->{'regexp'});
2452 }
2453
2454 # fallback
2455 return "";
2456}
2457
2458# assumes that $from and $to are defined and correctly filled,
2459# and that $line holds a line of chunk header for unified diff
2460sub format_unidiff_chunk_header {
2461 my ($line, $from, $to) = @_;
2462
2463 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
2464 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
2465
2466 $from_lines = 0 unless defined $from_lines;
2467 $to_lines = 0 unless defined $to_lines;
2468
2469 if ($from->{'href'}) {
2470 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
2471 -class=>"list"}, $from_text);
2472 }
2473 if ($to->{'href'}) {
2474 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
2475 -class=>"list"}, $to_text);
2476 }
2477 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
2478 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
2479 return $line;
2480}
2481
2482# assumes that $from and $to are defined and correctly filled,
2483# and that $line holds a line of chunk header for combined diff
2484sub format_cc_diff_chunk_header {
2485 my ($line, $from, $to) = @_;
2486
2487 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
2488 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
2489
2490 @from_text = split(' ', $ranges);
2491 for (my $i = 0; $i < @from_text; ++$i) {
2492 ($from_start[$i], $from_nlines[$i]) =
2493 (split(',', substr($from_text[$i], 1)), 0);
2494 }
2495
2496 $to_text = pop @from_text;
2497 $to_start = pop @from_start;
2498 $to_nlines = pop @from_nlines;
2499
2500 $line = "<span class=\"chunk_info\">$prefix ";
2501 for (my $i = 0; $i < @from_text; ++$i) {
2502 if ($from->{'href'}[$i]) {
2503 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
2504 -class=>"list"}, $from_text[$i]);
2505 } else {
2506 $line .= $from_text[$i];
2507 }
2508 $line .= " ";
2509 }
2510 if ($to->{'href'}) {
2511 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
2512 -class=>"list"}, $to_text);
2513 } else {
2514 $line .= $to_text;
2515 }
2516 $line .= " $prefix</span>" .
2517 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
2518 return $line;
2519}
2520
2521# process patch (diff) line (not to be used for diff headers),
2522# returning HTML-formatted (but not wrapped) line.
2523# If the line is passed as a reference, it is treated as HTML and not
2524# esc_html()'ed.
2525sub format_diff_line {
2526 my ($line, $diff_class, $from, $to) = @_;
2527
2528 if (ref($line)) {
2529 $line = $$line;
2530 } else {
2531 chomp $line;
2532 $line = untabify($line);
2533
2534 if ($from && $to && $line =~ m/^\@{2} /) {
2535 $line = format_unidiff_chunk_header($line, $from, $to);
2536 } elsif ($from && $to && $line =~ m/^\@{3}/) {
2537 $line = format_cc_diff_chunk_header($line, $from, $to);
2538 } else {
2539 $line = esc_html($line, -nbsp=>1);
2540 }
2541 }
2542
2543 my $diff_classes = "diff";
2544 $diff_classes .= " $diff_class" if ($diff_class);
2545 $line = "<div class=\"$diff_classes\">$line</div>\n";
2546
2547 return $line;
2548}
2549
2550# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
2551# linked. Pass the hash of the tree/commit to snapshot.
2552sub format_snapshot_links {
2553 my ($hash) = @_;
2554 my $num_fmts = @snapshot_fmts;
2555 if ($num_fmts > 1) {
2556 # A parenthesized list of links bearing format names.
2557 # e.g. "snapshot (_tar.gz_ _zip_)"
2558 return "snapshot (" . join(' ', map
2559 $cgi->a({
2560 -href => href(
2561 action=>"snapshot",
2562 hash=>$hash,
2563 snapshot_format=>$_
2564 )
2565 }, $known_snapshot_formats{$_}{'display'})
2566 , @snapshot_fmts) . ")";
2567 } elsif ($num_fmts == 1) {
2568 # A single "snapshot" link whose tooltip bears the format name.
2569 # i.e. "_snapshot_"
2570 my ($fmt) = @snapshot_fmts;
2571 return
2572 $cgi->a({
2573 -href => href(
2574 action=>"snapshot",
2575 hash=>$hash,
2576 snapshot_format=>$fmt
2577 ),
2578 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
2579 }, "snapshot");
2580 } else { # $num_fmts == 0
2581 return undef;
2582 }
2583}
2584
2585## ......................................................................
2586## functions returning values to be passed, perhaps after some
2587## transformation, to other functions; e.g. returning arguments to href()
2588
2589# returns hash to be passed to href to generate gitweb URL
2590# in -title key it returns description of link
2591sub get_feed_info {
2592 my $format = shift || 'Atom';
2593 my %res = (action => lc($format));
2594 my $matched_ref = 0;
2595
2596 # feed links are possible only for project views
2597 return unless (defined $project);
2598 # some views should link to OPML, or to generic project feed,
2599 # or don't have specific feed yet (so they should use generic)
2600 return if (!$action || $action =~ /^(?:tags|heads|forks|tag|search)$/x);
2601
2602 my $branch = undef;
2603 # branches refs uses 'refs/' + $get_branch_refs()[x] + '/' prefix
2604 # (fullname) to differentiate from tag links; this also makes
2605 # possible to detect branch links
2606 for my $ref (get_branch_refs()) {
2607 if ((defined $hash_base && $hash_base =~ m!^refs/\Q$ref\E/(.*)$!) ||
2608 (defined $hash && $hash =~ m!^refs/\Q$ref\E/(.*)$!)) {
2609 $branch = $1;
2610 $matched_ref = $ref;
2611 last;
2612 }
2613 }
2614 # find log type for feed description (title)
2615 my $type = 'log';
2616 if (defined $file_name) {
2617 $type = "history of $file_name";
2618 $type .= "/" if ($action eq 'tree');
2619 $type .= " on '$branch'" if (defined $branch);
2620 } else {
2621 $type = "log of $branch" if (defined $branch);
2622 }
2623
2624 $res{-title} = $type;
2625 $res{'hash'} = (defined $branch ? "refs/$matched_ref/$branch" : undef);
2626 $res{'file_name'} = $file_name;
2627
2628 return %res;
2629}
2630
2631## ----------------------------------------------------------------------
2632## git utility subroutines, invoking git commands
2633
2634# returns path to the core git executable and the --git-dir parameter as list
2635sub git_cmd {
2636 $number_of_git_cmds++;
2637 return $GIT, '--git-dir='.$git_dir;
2638}
2639
2640# quote the given arguments for passing them to the shell
2641# quote_command("command", "arg 1", "arg with ' and ! characters")
2642# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
2643# Try to avoid using this function wherever possible.
2644sub quote_command {
2645 return join(' ',
2646 map { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ );
2647}
2648
2649# get HEAD ref of given project as hash
2650sub git_get_head_hash {
2651 return git_get_full_hash(shift, 'HEAD');
2652}
2653
2654sub git_get_full_hash {
2655 return git_get_hash(@_);
2656}
2657
2658sub git_get_short_hash {
2659 return git_get_hash(@_, '--short=7');
2660}
2661
2662sub git_get_hash {
2663 my ($project, $hash, @options) = @_;
2664 my $o_git_dir = $git_dir;
2665 my $retval = undef;
2666 $git_dir = "$projectroot/$project";
2667 if (open my $fd, '-|', git_cmd(), 'rev-parse',
2668 '--verify', '-q', @options, $hash) {
2669 $retval = <$fd>;
2670 chomp $retval if defined $retval;
2671 close $fd;
2672 }
2673 if (defined $o_git_dir) {
2674 $git_dir = $o_git_dir;
2675 }
2676 return $retval;
2677}
2678
2679# get type of given object
2680sub git_get_type {
2681 my $hash = shift;
2682
2683 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
2684 my $type = <$fd>;
2685 close $fd or return;
2686 chomp $type;
2687 return $type;
2688}
2689
2690# repository configuration
2691our $config_file = '';
2692our %config;
2693
2694# store multiple values for single key as anonymous array reference
2695# single values stored directly in the hash, not as [ <value> ]
2696sub hash_set_multi {
2697 my ($hash, $key, $value) = @_;
2698
2699 if (!exists $hash->{$key}) {
2700 $hash->{$key} = $value;
2701 } elsif (!ref $hash->{$key}) {
2702 $hash->{$key} = [ $hash->{$key}, $value ];
2703 } else {
2704 push @{$hash->{$key}}, $value;
2705 }
2706}
2707
2708# return hash of git project configuration
2709# optionally limited to some section, e.g. 'gitweb'
2710sub git_parse_project_config {
2711 my $section_regexp = shift;
2712 my %config;
2713
2714 local $/ = "\0";
2715
2716 open my $fh, "-|", git_cmd(), "config", '-z', '-l',
2717 or return;
2718
2719 while (my $keyval = <$fh>) {
2720 chomp $keyval;
2721 my ($key, $value) = split(/\n/, $keyval, 2);
2722
2723 hash_set_multi(\%config, $key, $value)
2724 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
2725 }
2726 close $fh;
2727
2728 return %config;
2729}
2730
2731# convert config value to boolean: 'true' or 'false'
2732# no value, number > 0, 'true' and 'yes' values are true
2733# rest of values are treated as false (never as error)
2734sub config_to_bool {
2735 my $val = shift;
2736
2737 return 1 if !defined $val; # section.key
2738
2739 # strip leading and trailing whitespace
2740 $val =~ s/^\s+//;
2741 $val =~ s/\s+$//;
2742
2743 return (($val =~ /^\d+$/ && $val) || # section.key = 1
2744 ($val =~ /^(?:true|yes)$/i)); # section.key = true
2745}
2746
2747# convert config value to simple decimal number
2748# an optional value suffix of 'k', 'm', or 'g' will cause the value
2749# to be multiplied by 1024, 1048576, or 1073741824
2750sub config_to_int {
2751 my $val = shift;
2752
2753 # strip leading and trailing whitespace
2754 $val =~ s/^\s+//;
2755 $val =~ s/\s+$//;
2756
2757 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
2758 $unit = lc($unit);
2759 # unknown unit is treated as 1
2760 return $num * ($unit eq 'g' ? 1073741824 :
2761 $unit eq 'm' ? 1048576 :
2762 $unit eq 'k' ? 1024 : 1);
2763 }
2764 return $val;
2765}
2766
2767# convert config value to array reference, if needed
2768sub config_to_multi {
2769 my $val = shift;
2770
2771 return ref($val) ? $val : (defined($val) ? [ $val ] : []);
2772}
2773
2774sub git_get_project_config {
2775 my ($key, $type) = @_;
2776
2777 return unless defined $git_dir;
2778
2779 # key sanity check
2780 return unless ($key);
2781 # only subsection, if exists, is case sensitive,
2782 # and not lowercased by 'git config -z -l'
2783 if (my ($hi, $mi, $lo) = ($key =~ /^([^.]*)\.(.*)\.([^.]*)$/)) {
2784 $lo =~ s/_//g;
2785 $key = join(".", lc($hi), $mi, lc($lo));
2786 return if ($lo =~ /\W/ || $hi =~ /\W/);
2787 } else {
2788 $key = lc($key);
2789 $key =~ s/_//g;
2790 return if ($key =~ /\W/);
2791 }
2792 $key =~ s/^gitweb\.//;
2793
2794 # type sanity check
2795 if (defined $type) {
2796 $type =~ s/^--//;
2797 $type = undef
2798 unless ($type eq 'bool' || $type eq 'int');
2799 }
2800
2801 # get config
2802 if (!defined $config_file ||
2803 $config_file ne "$git_dir/config") {
2804 %config = git_parse_project_config('gitweb');
2805 $config_file = "$git_dir/config";
2806 }
2807
2808 # check if config variable (key) exists
2809 return unless exists $config{"gitweb.$key"};
2810
2811 # ensure given type
2812 if (!defined $type) {
2813 return $config{"gitweb.$key"};
2814 } elsif ($type eq 'bool') {
2815 # backward compatibility: 'git config --bool' returns true/false
2816 return config_to_bool($config{"gitweb.$key"}) ? 'true' : 'false';
2817 } elsif ($type eq 'int') {
2818 return config_to_int($config{"gitweb.$key"});
2819 }
2820 return $config{"gitweb.$key"};
2821}
2822
2823# get hash of given path at given ref
2824sub git_get_hash_by_path {
2825 my $base = shift;
2826 my $path = shift || return undef;
2827 my $type = shift;
2828
2829 $path =~ s,/+$,,;
2830
2831 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
2832 or die_error(500, "Open git-ls-tree failed");
2833 my $line = <$fd>;
2834 close $fd or return undef;
2835
2836 if (!defined $line) {
2837 # there is no tree or hash given by $path at $base
2838 return undef;
2839 }
2840
2841 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2842 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
2843 if (defined $type && $type ne $2) {
2844 # type doesn't match
2845 return undef;
2846 }
2847 return $3;
2848}
2849
2850# get path of entry with given hash at given tree-ish (ref)
2851# used to get 'from' filename for combined diff (merge commit) for renames
2852sub git_get_path_by_hash {
2853 my $base = shift || return;
2854 my $hash = shift || return;
2855
2856 local $/ = "\0";
2857
2858 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
2859 or return undef;
2860 while (my $line = <$fd>) {
2861 chomp $line;
2862
2863 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
2864 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
2865 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
2866 close $fd;
2867 return $1;
2868 }
2869 }
2870 close $fd;
2871 return undef;
2872}
2873
2874## ......................................................................
2875## git utility functions, directly accessing git repository
2876
2877# get the value of config variable either from file named as the variable
2878# itself in the repository ($GIT_DIR/$name file), or from gitweb.$name
2879# configuration variable in the repository config file.
2880sub git_get_file_or_project_config {
2881 my ($path, $name) = @_;
2882
2883 $git_dir = "$projectroot/$path";
2884 open my $fd, '<', "$git_dir/$name"
2885 or return git_get_project_config($name);
2886 my $conf = <$fd>;
2887 close $fd;
2888 if (defined $conf) {
2889 chomp $conf;
2890 }
2891 return $conf;
2892}
2893
2894sub git_get_project_description {
2895 my $path = shift;
2896 return git_get_file_or_project_config($path, 'description');
2897}
2898
2899sub git_get_project_category {
2900 my $path = shift;
2901 return git_get_file_or_project_config($path, 'category');
2902}
2903
2904
2905# supported formats:
2906# * $GIT_DIR/ctags/<tagname> file (in 'ctags' subdirectory)
2907# - if its contents is a number, use it as tag weight,
2908# - otherwise add a tag with weight 1
2909# * $GIT_DIR/ctags file, each line is a tag (with weight 1)
2910# the same value multiple times increases tag weight
2911# * `gitweb.ctag' multi-valued repo config variable
2912sub git_get_project_ctags {
2913 my $project = shift;
2914 my $ctags = {};
2915
2916 $git_dir = "$projectroot/$project";
2917 if (opendir my $dh, "$git_dir/ctags") {
2918 my @files = grep { -f $_ } map { "$git_dir/ctags/$_" } readdir($dh);
2919 foreach my $tagfile (@files) {
2920 open my $ct, '<', $tagfile
2921 or next;
2922 my $val = <$ct>;
2923 chomp $val if $val;
2924 close $ct;
2925
2926 (my $ctag = $tagfile) =~ s#.*/##;
2927 if ($val =~ /^\d+$/) {
2928 $ctags->{$ctag} = $val;
2929 } else {
2930 $ctags->{$ctag} = 1;
2931 }
2932 }
2933 closedir $dh;
2934
2935 } elsif (open my $fh, '<', "$git_dir/ctags") {
2936 while (my $line = <$fh>) {
2937 chomp $line;
2938 $ctags->{$line}++ if $line;
2939 }
2940 close $fh;
2941
2942 } else {
2943 my $taglist = config_to_multi(git_get_project_config('ctag'));
2944 foreach my $tag (@$taglist) {
2945 $ctags->{$tag}++;
2946 }
2947 }
2948
2949 return $ctags;
2950}
2951
2952# return hash, where keys are content tags ('ctags'),
2953# and values are sum of weights of given tag in every project
2954sub git_gather_all_ctags {
2955 my $projects = shift;
2956 my $ctags = {};
2957
2958 foreach my $p (@$projects) {
2959 foreach my $ct (keys %{$p->{'ctags'}}) {
2960 $ctags->{$ct} += $p->{'ctags'}->{$ct};
2961 }
2962 }
2963
2964 return $ctags;
2965}
2966
2967sub git_populate_project_tagcloud {
2968 my $ctags = shift;
2969
2970 # First, merge different-cased tags; tags vote on casing
2971 my %ctags_lc;
2972 foreach (keys %$ctags) {
2973 $ctags_lc{lc $_}->{count} += $ctags->{$_};
2974 if (not $ctags_lc{lc $_}->{topcount}
2975 or $ctags_lc{lc $_}->{topcount} < $ctags->{$_}) {
2976 $ctags_lc{lc $_}->{topcount} = $ctags->{$_};
2977 $ctags_lc{lc $_}->{topname} = $_;
2978 }
2979 }
2980
2981 my $cloud;
2982 my $matched = $input_params{'ctag'};
2983 if (eval { require HTML::TagCloud; 1; }) {
2984 $cloud = HTML::TagCloud->new;
2985 foreach my $ctag (sort keys %ctags_lc) {
2986 # Pad the title with spaces so that the cloud looks
2987 # less crammed.
2988 my $title = esc_html($ctags_lc{$ctag}->{topname});
2989 $title =~ s/ /&nbsp;/g;
2990 $title =~ s/^/&nbsp;/g;
2991 $title =~ s/$/&nbsp;/g;
2992 if (defined $matched && $matched eq $ctag) {
2993 $title = qq(<span class="match">$title</span>);
2994 }
2995 $cloud->add($title, href(project=>undef, ctag=>$ctag),
2996 $ctags_lc{$ctag}->{count});
2997 }
2998 } else {
2999 $cloud = {};
3000 foreach my $ctag (keys %ctags_lc) {
3001 my $title = esc_html($ctags_lc{$ctag}->{topname}, -nbsp=>1);
3002 if (defined $matched && $matched eq $ctag) {
3003 $title = qq(<span class="match">$title</span>);
3004 }
3005 $cloud->{$ctag}{count} = $ctags_lc{$ctag}->{count};
3006 $cloud->{$ctag}{ctag} =
3007 $cgi->a({-href=>href(project=>undef, ctag=>$ctag)}, $title);
3008 }
3009 }
3010 return $cloud;
3011}
3012
3013sub git_show_project_tagcloud {
3014 my ($cloud, $count) = @_;
3015 if (ref $cloud eq 'HTML::TagCloud') {
3016 return $cloud->html_and_css($count);
3017 } else {
3018 my @tags = sort { $cloud->{$a}->{'count'} <=> $cloud->{$b}->{'count'} } keys %$cloud;
3019 return
3020 '<div id="htmltagcloud"'.($project ? '' : ' align="center"').'>' .
3021 join (', ', map {
3022 $cloud->{$_}->{'ctag'}
3023 } splice(@tags, 0, $count)) .
3024 '</div>';
3025 }
3026}
3027
3028sub git_get_project_url_list {
3029 my $path = shift;
3030
3031 $git_dir = "$projectroot/$path";
3032 open my $fd, '<', "$git_dir/cloneurl"
3033 or return wantarray ?
3034 @{ config_to_multi(git_get_project_config('url')) } :
3035 config_to_multi(git_get_project_config('url'));
3036 my @git_project_url_list = map { chomp; $_ } <$fd>;
3037 close $fd;
3038
3039 return wantarray ? @git_project_url_list : \@git_project_url_list;
3040}
3041
3042sub git_get_projects_list {
3043 my $filter = shift || '';
3044 my $paranoid = shift;
3045 my @list;
3046
3047 if (-d $projects_list) {
3048 # search in directory
3049 my $dir = $projects_list;
3050 # remove the trailing "/"
3051 $dir =~ s!/+$!!;
3052 my $pfxlen = length("$dir");
3053 my $pfxdepth = ($dir =~ tr!/!!);
3054 # when filtering, search only given subdirectory
3055 if ($filter && !$paranoid) {
3056 $dir .= "/$filter";
3057 $dir =~ s!/+$!!;
3058 }
3059
3060 File::Find::find({
3061 follow_fast => 1, # follow symbolic links
3062 follow_skip => 2, # ignore duplicates
3063 dangling_symlinks => 0, # ignore dangling symlinks, silently
3064 wanted => sub {
3065 # global variables
3066 our $project_maxdepth;
3067 our $projectroot;
3068 # skip project-list toplevel, if we get it.
3069 return if (m!^[/.]$!);
3070 # only directories can be git repositories
3071 return unless (-d $_);
3072 # don't traverse too deep (Find is super slow on os x)
3073 # $project_maxdepth excludes depth of $projectroot
3074 if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
3075 $File::Find::prune = 1;
3076 return;
3077 }
3078
3079 my $path = substr($File::Find::name, $pfxlen + 1);
3080 # paranoidly only filter here
3081 if ($paranoid && $filter && $path !~ m!^\Q$filter\E/!) {
3082 next;
3083 }
3084 # we check related file in $projectroot
3085 if (check_export_ok("$projectroot/$path")) {
3086 push @list, { path => $path };
3087 $File::Find::prune = 1;
3088 }
3089 },
3090 }, "$dir");
3091
3092 } elsif (-f $projects_list) {
3093 # read from file(url-encoded):
3094 # 'git%2Fgit.git Linus+Torvalds'
3095 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
3096 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
3097 open my $fd, '<', $projects_list or return;
3098 PROJECT:
3099 while (my $line = <$fd>) {
3100 chomp $line;
3101 my ($path, $owner) = split ' ', $line;
3102 $path = unescape($path);
3103 $owner = unescape($owner);
3104 if (!defined $path) {
3105 next;
3106 }
3107 # if $filter is rpovided, check if $path begins with $filter
3108 if ($filter && $path !~ m!^\Q$filter\E/!) {
3109 next;
3110 }
3111 if (check_export_ok("$projectroot/$path")) {
3112 my $pr = {
3113 path => $path
3114 };
3115 if ($owner) {
3116 $pr->{'owner'} = to_utf8($owner);
3117 }
3118 push @list, $pr;
3119 }
3120 }
3121 close $fd;
3122 }
3123 return @list;
3124}
3125
3126# written with help of Tree::Trie module (Perl Artistic License, GPL compatibile)
3127# as side effects it sets 'forks' field to list of forks for forked projects
3128sub filter_forks_from_projects_list {
3129 my $projects = shift;
3130
3131 my %trie; # prefix tree of directories (path components)
3132 # generate trie out of those directories that might contain forks
3133 foreach my $pr (@$projects) {
3134 my $path = $pr->{'path'};
3135 $path =~ s/\.git$//; # forks of 'repo.git' are in 'repo/' directory
3136 next if ($path =~ m!/$!); # skip non-bare repositories, e.g. 'repo/.git'
3137 next unless ($path); # skip '.git' repository: tests, git-instaweb
3138 next unless (-d "$projectroot/$path"); # containing directory exists
3139 $pr->{'forks'} = []; # there can be 0 or more forks of project
3140
3141 # add to trie
3142 my @dirs = split('/', $path);
3143 # walk the trie, until either runs out of components or out of trie
3144 my $ref = \%trie;
3145 while (scalar @dirs &&
3146 exists($ref->{$dirs[0]})) {
3147 $ref = $ref->{shift @dirs};
3148 }
3149 # create rest of trie structure from rest of components
3150 foreach my $dir (@dirs) {
3151 $ref = $ref->{$dir} = {};
3152 }
3153 # create end marker, store $pr as a data
3154 $ref->{''} = $pr if (!exists $ref->{''});
3155 }
3156
3157 # filter out forks, by finding shortest prefix match for paths
3158 my @filtered;
3159 PROJECT:
3160 foreach my $pr (@$projects) {
3161 # trie lookup
3162 my $ref = \%trie;
3163 DIR:
3164 foreach my $dir (split('/', $pr->{'path'})) {
3165 if (exists $ref->{''}) {
3166 # found [shortest] prefix, is a fork - skip it
3167 push @{$ref->{''}{'forks'}}, $pr;
3168 next PROJECT;
3169 }
3170 if (!exists $ref->{$dir}) {
3171 # not in trie, cannot have prefix, not a fork
3172 push @filtered, $pr;
3173 next PROJECT;
3174 }
3175 # If the dir is there, we just walk one step down the trie.
3176 $ref = $ref->{$dir};
3177 }
3178 # we ran out of trie
3179 # (shouldn't happen: it's either no match, or end marker)
3180 push @filtered, $pr;
3181 }
3182
3183 return @filtered;
3184}
3185
3186# note: fill_project_list_info must be run first,
3187# for 'descr_long' and 'ctags' to be filled
3188sub search_projects_list {
3189 my ($projlist, %opts) = @_;
3190 my $tagfilter = $opts{'tagfilter'};
3191 my $search_re = $opts{'search_regexp'};
3192
3193 return @$projlist
3194 unless ($tagfilter || $search_re);
3195
3196 # searching projects require filling to be run before it;
3197 fill_project_list_info($projlist,
3198 $tagfilter ? 'ctags' : (),
3199 $search_re ? ('path', 'descr') : ());
3200 my @projects;
3201 PROJECT:
3202 foreach my $pr (@$projlist) {
3203
3204 if ($tagfilter) {
3205 next unless ref($pr->{'ctags'}) eq 'HASH';
3206 next unless
3207 grep { lc($_) eq lc($tagfilter) } keys %{$pr->{'ctags'}};
3208 }
3209
3210 if ($search_re) {
3211 next unless
3212 $pr->{'path'} =~ /$search_re/ ||
3213 $pr->{'descr_long'} =~ /$search_re/;
3214 }
3215
3216 push @projects, $pr;
3217 }
3218
3219 return @projects;
3220}
3221
3222our $gitweb_project_owner = undef;
3223sub git_get_project_list_from_file {
3224
3225 return if (defined $gitweb_project_owner);
3226
3227 $gitweb_project_owner = {};
3228 # read from file (url-encoded):
3229 # 'git%2Fgit.git Linus+Torvalds'
3230 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
3231 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
3232 if (-f $projects_list) {
3233 open(my $fd, '<', $projects_list);
3234 while (my $line = <$fd>) {
3235 chomp $line;
3236 my ($pr, $ow) = split ' ', $line;
3237 $pr = unescape($pr);
3238 $ow = unescape($ow);
3239 $gitweb_project_owner->{$pr} = to_utf8($ow);
3240 }
3241 close $fd;
3242 }
3243}
3244
3245sub git_get_project_owner {
3246 my $project = shift;
3247 my $owner;
3248
3249 return undef unless $project;
3250 $git_dir = "$projectroot/$project";
3251
3252 if (!defined $gitweb_project_owner) {
3253 git_get_project_list_from_file();
3254 }
3255
3256 if (exists $gitweb_project_owner->{$project}) {
3257 $owner = $gitweb_project_owner->{$project};
3258 }
3259 if (!defined $owner){
3260 $owner = git_get_project_config('owner');
3261 }
3262 if (!defined $owner) {
3263 $owner = get_file_owner("$git_dir");
3264 }
3265
3266 return $owner;
3267}
3268
3269sub git_get_last_activity {
3270 my ($path) = @_;
3271 my $fd;
3272
3273 $git_dir = "$projectroot/$path";
3274 open($fd, "-|", git_cmd(), 'for-each-ref',
3275 '--format=%(committer)',
3276 '--sort=-committerdate',
3277 '--count=1',
3278 map { "refs/$_" } get_branch_refs ()) or return;
3279 my $most_recent = <$fd>;
3280 close $fd or return;
3281 if (defined $most_recent &&
3282 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
3283 my $timestamp = $1;
3284 my $age = time - $timestamp;
3285 return ($age, age_string($age));
3286 }
3287 return (undef, undef);
3288}
3289
3290# Implementation note: when a single remote is wanted, we cannot use 'git
3291# remote show -n' because that command always work (assuming it's a remote URL
3292# if it's not defined), and we cannot use 'git remote show' because that would
3293# try to make a network roundtrip. So the only way to find if that particular
3294# remote is defined is to walk the list provided by 'git remote -v' and stop if
3295# and when we find what we want.
3296sub git_get_remotes_list {
3297 my $wanted = shift;
3298 my %remotes = ();
3299
3300 open my $fd, '-|' , git_cmd(), 'remote', '-v';
3301 return unless $fd;
3302 while (my $remote = <$fd>) {
3303 chomp $remote;
3304 $remote =~ s!\t(.*?)\s+\((\w+)\)$!!;
3305 next if $wanted and not $remote eq $wanted;
3306 my ($url, $key) = ($1, $2);
3307
3308 $remotes{$remote} ||= { 'heads' => () };
3309 $remotes{$remote}{$key} = $url;
3310 }
3311 close $fd or return;
3312 return wantarray ? %remotes : \%remotes;
3313}
3314
3315# Takes a hash of remotes as first parameter and fills it by adding the
3316# available remote heads for each of the indicated remotes.
3317sub fill_remote_heads {
3318 my $remotes = shift;
3319 my @heads = map { "remotes/$_" } keys %$remotes;
3320 my @remoteheads = git_get_heads_list(undef, @heads);
3321 foreach my $remote (keys %$remotes) {
3322 $remotes->{$remote}{'heads'} = [ grep {
3323 $_->{'name'} =~ s!^$remote/!!
3324 } @remoteheads ];
3325 }
3326}
3327
3328sub git_get_references {
3329 my $type = shift || "";
3330 my %refs;
3331 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
3332 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
3333 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
3334 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
3335 or return;
3336
3337 while (my $line = <$fd>) {
3338 chomp $line;
3339 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {
3340 if (defined $refs{$1}) {
3341 push @{$refs{$1}}, $2;
3342 } else {
3343 $refs{$1} = [ $2 ];
3344 }
3345 }
3346 }
3347 close $fd or return;
3348 return \%refs;
3349}
3350
3351sub git_get_rev_name_tags {
3352 my $hash = shift || return undef;
3353
3354 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
3355 or return;
3356 my $name_rev = <$fd>;
3357 close $fd;
3358
3359 if ($name_rev =~ m|^$hash tags/(.*)$|) {
3360 return $1;
3361 } else {
3362 # catches also '$hash undefined' output
3363 return undef;
3364 }
3365}
3366
3367## ----------------------------------------------------------------------
3368## parse to hash functions
3369
3370sub parse_date {
3371 my $epoch = shift;
3372 my $tz = shift || "-0000";
3373
3374 my %date;
3375 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
3376 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
3377 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
3378 $date{'hour'} = $hour;
3379 $date{'minute'} = $min;
3380 $date{'mday'} = $mday;
3381 $date{'day'} = $days[$wday];
3382 $date{'month'} = $months[$mon];
3383 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
3384 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
3385 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
3386 $mday, $months[$mon], $hour ,$min;
3387 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
3388 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
3389
3390 my ($tz_sign, $tz_hour, $tz_min) =
3391 ($tz =~ m/^([-+])(\d\d)(\d\d)$/);
3392 $tz_sign = ($tz_sign eq '-' ? -1 : +1);
3393 my $local = $epoch + $tz_sign*((($tz_hour*60) + $tz_min)*60);
3394 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
3395 $date{'hour_local'} = $hour;
3396 $date{'minute_local'} = $min;
3397 $date{'tz_local'} = $tz;
3398 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
3399 1900+$year, $mon+1, $mday,
3400 $hour, $min, $sec, $tz);
3401 return %date;
3402}
3403
3404sub parse_tag {
3405 my $tag_id = shift;
3406 my %tag;
3407 my @comment;
3408
3409 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
3410 $tag{'id'} = $tag_id;
3411 while (my $line = <$fd>) {
3412 chomp $line;
3413 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
3414 $tag{'object'} = $1;
3415 } elsif ($line =~ m/^type (.+)$/) {
3416 $tag{'type'} = $1;
3417 } elsif ($line =~ m/^tag (.+)$/) {
3418 $tag{'name'} = $1;
3419 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
3420 $tag{'author'} = $1;
3421 $tag{'author_epoch'} = $2;
3422 $tag{'author_tz'} = $3;
3423 if ($tag{'author'} =~ m/^([^<]+) <([^>]*)>/) {
3424 $tag{'author_name'} = $1;
3425 $tag{'author_email'} = $2;
3426 } else {
3427 $tag{'author_name'} = $tag{'author'};
3428 }
3429 } elsif ($line =~ m/--BEGIN/) {
3430 push @comment, $line;
3431 last;
3432 } elsif ($line eq "") {
3433 last;
3434 }
3435 }
3436 push @comment, <$fd>;
3437 $tag{'comment'} = \@comment;
3438 close $fd or return;
3439 if (!defined $tag{'name'}) {
3440 return
3441 };
3442 return %tag
3443}
3444
3445sub parse_commit_text {
3446 my ($commit_text, $withparents) = @_;
3447 my @commit_lines = split '\n', $commit_text;
3448 my %co;
3449
3450 pop @commit_lines; # Remove '\0'
3451
3452 if (! @commit_lines) {
3453 return;
3454 }
3455
3456 my $header = shift @commit_lines;
3457 if ($header !~ m/^[0-9a-fA-F]{40}/) {
3458 return;
3459 }
3460 ($co{'id'}, my @parents) = split ' ', $header;
3461 while (my $line = shift @commit_lines) {
3462 last if $line eq "\n";
3463 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
3464 $co{'tree'} = $1;
3465 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
3466 push @parents, $1;
3467 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
3468 $co{'author'} = to_utf8($1);
3469 $co{'author_epoch'} = $2;
3470 $co{'author_tz'} = $3;
3471 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
3472 $co{'author_name'} = $1;
3473 $co{'author_email'} = $2;
3474 } else {
3475 $co{'author_name'} = $co{'author'};
3476 }
3477 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
3478 $co{'committer'} = to_utf8($1);
3479 $co{'committer_epoch'} = $2;
3480 $co{'committer_tz'} = $3;
3481 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
3482 $co{'committer_name'} = $1;
3483 $co{'committer_email'} = $2;
3484 } else {
3485 $co{'committer_name'} = $co{'committer'};
3486 }
3487 }
3488 }
3489 if (!defined $co{'tree'}) {
3490 return;
3491 };
3492 $co{'parents'} = \@parents;
3493 $co{'parent'} = $parents[0];
3494
3495 foreach my $title (@commit_lines) {
3496 $title =~ s/^ //;
3497 if ($title ne "") {
3498 $co{'title'} = chop_str($title, 80, 5);
3499 # remove leading stuff of merges to make the interesting part visible
3500 if (length($title) > 50) {
3501 $title =~ s/^Automatic //;
3502 $title =~ s/^merge (of|with) /Merge ... /i;
3503 if (length($title) > 50) {
3504 $title =~ s/(http|rsync):\/\///;
3505 }
3506 if (length($title) > 50) {
3507 $title =~ s/(master|www|rsync)\.//;
3508 }
3509 if (length($title) > 50) {
3510 $title =~ s/kernel.org:?//;
3511 }
3512 if (length($title) > 50) {
3513 $title =~ s/\/pub\/scm//;
3514 }
3515 }
3516 $co{'title_short'} = chop_str($title, 50, 5);
3517 last;
3518 }
3519 }
3520 if (! defined $co{'title'} || $co{'title'} eq "") {
3521 $co{'title'} = $co{'title_short'} = '(no commit message)';
3522 }
3523 # remove added spaces
3524 foreach my $line (@commit_lines) {
3525 $line =~ s/^ //;
3526 }
3527 $co{'comment'} = \@commit_lines;
3528
3529 my $age = time - $co{'committer_epoch'};
3530 $co{'age'} = $age;
3531 $co{'age_string'} = age_string($age);
3532 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
3533 if ($age > 60*60*24*7*2) {
3534 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
3535 $co{'age_string_age'} = $co{'age_string'};
3536 } else {
3537 $co{'age_string_date'} = $co{'age_string'};
3538 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
3539 }
3540 return %co;
3541}
3542
3543sub parse_commit {
3544 my ($commit_id) = @_;
3545 my %co;
3546
3547 local $/ = "\0";
3548
3549 open my $fd, "-|", git_cmd(), "rev-list",
3550 "--parents",
3551 "--header",
3552 "--max-count=1",
3553 $commit_id,
3554 "--",
3555 or die_error(500, "Open git-rev-list failed");
3556 %co = parse_commit_text(<$fd>, 1);
3557 close $fd;
3558
3559 return %co;
3560}
3561
3562sub parse_commits {
3563 my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
3564 my @cos;
3565
3566 $maxcount ||= 1;
3567 $skip ||= 0;
3568
3569 local $/ = "\0";
3570
3571 open my $fd, "-|", git_cmd(), "rev-list",
3572 "--header",
3573 @args,
3574 ("--max-count=" . $maxcount),
3575 ("--skip=" . $skip),
3576 @extra_options,
3577 $commit_id,
3578 "--",
3579 ($filename ? ($filename) : ())
3580 or die_error(500, "Open git-rev-list failed");
3581 while (my $line = <$fd>) {
3582 my %co = parse_commit_text($line);
3583 push @cos, \%co;
3584 }
3585 close $fd;
3586
3587 return wantarray ? @cos : \@cos;
3588}
3589
3590# parse line of git-diff-tree "raw" output
3591sub parse_difftree_raw_line {
3592 my $line = shift;
3593 my %res;
3594
3595 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
3596 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
3597 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
3598 $res{'from_mode'} = $1;
3599 $res{'to_mode'} = $2;
3600 $res{'from_id'} = $3;
3601 $res{'to_id'} = $4;
3602 $res{'status'} = $5;
3603 $res{'similarity'} = $6;
3604 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
3605 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
3606 } else {
3607 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote($7);
3608 }
3609 }
3610 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
3611 # combined diff (for merge commit)
3612 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
3613 $res{'nparents'} = length($1);
3614 $res{'from_mode'} = [ split(' ', $2) ];
3615 $res{'to_mode'} = pop @{$res{'from_mode'}};
3616 $res{'from_id'} = [ split(' ', $3) ];
3617 $res{'to_id'} = pop @{$res{'from_id'}};
3618 $res{'status'} = [ split('', $4) ];
3619 $res{'to_file'} = unquote($5);
3620 }
3621 # 'c512b523472485aef4fff9e57b229d9d243c967f'
3622 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
3623 $res{'commit'} = $1;
3624 }
3625
3626 return wantarray ? %res : \%res;
3627}
3628
3629# wrapper: return parsed line of git-diff-tree "raw" output
3630# (the argument might be raw line, or parsed info)
3631sub parsed_difftree_line {
3632 my $line_or_ref = shift;
3633
3634 if (ref($line_or_ref) eq "HASH") {
3635 # pre-parsed (or generated by hand)
3636 return $line_or_ref;
3637 } else {
3638 return parse_difftree_raw_line($line_or_ref);
3639 }
3640}
3641
3642# parse line of git-ls-tree output
3643sub parse_ls_tree_line {
3644 my $line = shift;
3645 my %opts = @_;
3646 my %res;
3647
3648 if ($opts{'-l'}) {
3649 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'
3650 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;
3651
3652 $res{'mode'} = $1;
3653 $res{'type'} = $2;
3654 $res{'hash'} = $3;
3655 $res{'size'} = $4;
3656 if ($opts{'-z'}) {
3657 $res{'name'} = $5;
3658 } else {
3659 $res{'name'} = unquote($5);
3660 }
3661 } else {
3662 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
3663 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
3664
3665 $res{'mode'} = $1;
3666 $res{'type'} = $2;
3667 $res{'hash'} = $3;
3668 if ($opts{'-z'}) {
3669 $res{'name'} = $4;
3670 } else {
3671 $res{'name'} = unquote($4);
3672 }
3673 }
3674
3675 return wantarray ? %res : \%res;
3676}
3677
3678# generates _two_ hashes, references to which are passed as 2 and 3 argument
3679sub parse_from_to_diffinfo {
3680 my ($diffinfo, $from, $to, @parents) = @_;
3681
3682 if ($diffinfo->{'nparents'}) {
3683 # combined diff
3684 $from->{'file'} = [];
3685 $from->{'href'} = [];
3686 fill_from_file_info($diffinfo, @parents)
3687 unless exists $diffinfo->{'from_file'};
3688 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
3689 $from->{'file'}[$i] =
3690 defined $diffinfo->{'from_file'}[$i] ?
3691 $diffinfo->{'from_file'}[$i] :
3692 $diffinfo->{'to_file'};
3693 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
3694 $from->{'href'}[$i] = href(action=>"blob",
3695 hash_base=>$parents[$i],
3696 hash=>$diffinfo->{'from_id'}[$i],
3697 file_name=>$from->{'file'}[$i]);
3698 } else {
3699 $from->{'href'}[$i] = undef;
3700 }
3701 }
3702 } else {
3703 # ordinary (not combined) diff
3704 $from->{'file'} = $diffinfo->{'from_file'};
3705 if ($diffinfo->{'status'} ne "A") { # not new (added) file
3706 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
3707 hash=>$diffinfo->{'from_id'},
3708 file_name=>$from->{'file'});
3709 } else {
3710 delete $from->{'href'};
3711 }
3712 }
3713
3714 $to->{'file'} = $diffinfo->{'to_file'};
3715 if (!is_deleted($diffinfo)) { # file exists in result
3716 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
3717 hash=>$diffinfo->{'to_id'},
3718 file_name=>$to->{'file'});
3719 } else {
3720 delete $to->{'href'};
3721 }
3722}
3723
3724## ......................................................................
3725## parse to array of hashes functions
3726
3727sub git_get_heads_list {
3728 my ($limit, @classes) = @_;
3729 @classes = get_branch_refs() unless @classes;
3730 my @patterns = map { "refs/$_" } @classes;
3731 my @headslist;
3732
3733 open my $fd, '-|', git_cmd(), 'for-each-ref',
3734 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
3735 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
3736 @patterns
3737 or return;
3738 while (my $line = <$fd>) {
3739 my %ref_item;
3740
3741 chomp $line;
3742 my ($refinfo, $committerinfo) = split(/\0/, $line);
3743 my ($hash, $name, $title) = split(' ', $refinfo, 3);
3744 my ($committer, $epoch, $tz) =
3745 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
3746 $ref_item{'fullname'} = $name;
3747 my $strip_refs = join '|', map { quotemeta } get_branch_refs();
3748 $name =~ s!^refs/($strip_refs|remotes)/!!;
3749 $ref_item{'name'} = $name;
3750 # for refs neither in 'heads' nor 'remotes' we want to
3751 # show their ref dir
3752 my $ref_dir = (defined $1) ? $1 : '';
3753 if ($ref_dir ne '' and $ref_dir ne 'heads' and $ref_dir ne 'remotes') {
3754 $ref_item{'name'} .= ' (' . $ref_dir . ')';
3755 }
3756
3757 $ref_item{'id'} = $hash;
3758 $ref_item{'title'} = $title || '(no commit message)';
3759 $ref_item{'epoch'} = $epoch;
3760 if ($epoch) {
3761 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
3762 } else {
3763 $ref_item{'age'} = "unknown";
3764 }
3765
3766 push @headslist, \%ref_item;
3767 }
3768 close $fd;
3769
3770 return wantarray ? @headslist : \@headslist;
3771}
3772
3773sub git_get_tags_list {
3774 my $limit = shift;
3775 my @tagslist;
3776
3777 open my $fd, '-|', git_cmd(), 'for-each-ref',
3778 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
3779 '--format=%(objectname) %(objecttype) %(refname) '.
3780 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
3781 'refs/tags'
3782 or return;
3783 while (my $line = <$fd>) {
3784 my %ref_item;
3785
3786 chomp $line;
3787 my ($refinfo, $creatorinfo) = split(/\0/, $line);
3788 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
3789 my ($creator, $epoch, $tz) =
3790 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
3791 $ref_item{'fullname'} = $name;
3792 $name =~ s!^refs/tags/!!;
3793
3794 $ref_item{'type'} = $type;
3795 $ref_item{'id'} = $id;
3796 $ref_item{'name'} = $name;
3797 if ($type eq "tag") {
3798 $ref_item{'subject'} = $title;
3799 $ref_item{'reftype'} = $reftype;
3800 $ref_item{'refid'} = $refid;
3801 } else {
3802 $ref_item{'reftype'} = $type;
3803 $ref_item{'refid'} = $id;
3804 }
3805
3806 if ($type eq "tag" || $type eq "commit") {
3807 $ref_item{'epoch'} = $epoch;
3808 if ($epoch) {
3809 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
3810 } else {
3811 $ref_item{'age'} = "unknown";
3812 }
3813 }
3814
3815 push @tagslist, \%ref_item;
3816 }
3817 close $fd;
3818
3819 return wantarray ? @tagslist : \@tagslist;
3820}
3821
3822## ----------------------------------------------------------------------
3823## filesystem-related functions
3824
3825sub get_file_owner {
3826 my $path = shift;
3827
3828 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
3829 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
3830 if (!defined $gcos) {
3831 return undef;
3832 }
3833 my $owner = $gcos;
3834 $owner =~ s/[,;].*$//;
3835 return to_utf8($owner);
3836}
3837
3838# assume that file exists
3839sub insert_file {
3840 my $filename = shift;
3841
3842 open my $fd, '<', $filename;
3843 print map { to_utf8($_) } <$fd>;
3844 close $fd;
3845}
3846
39ef91a3
AT
3847sub insert_html_file {
3848 my $file_name = shift;
3849 insert_file($file_name);
3850}
3851
3852sub insert_text_file {
3853 my $file_name = shift;
3854
3855 open my $fd, $file_name or die_error(500, "Couldn't open $file_name");
3856
3857 print "<pre>";
3858 while (my $line = <$fd>) {
3859 print to_utf8($line);
3860 }
3861 print "</pre>";
3862}
3863
3864sub insert_markdown_file {
3865 my $file_name = shift;
3866
3867 # TODO: Make this a config option?
3868 my $markdown_cmd = "/usr/bin/markdown";
3869
3870 open my $fd, quote_command($markdown_cmd, $file_name)." |"
3871 or die_error(500, "Couldn't open $file_name");
3872
3873 while (my $line = <$fd>) {
3874 print to_utf8($line);
3875 }
3876}
3877
f35f44b7
AT
3878## ......................................................................
3879## mimetype related functions
3880
3881sub mimetype_guess_file {
3882 my $filename = shift;
3883 my $mimemap = shift;
3884 -r $mimemap or return undef;
3885
3886 my %mimemap;
3887 open(my $mh, '<', $mimemap) or return undef;
3888 while (<$mh>) {
3889 next if m/^#/; # skip comments
3890 my ($mimetype, @exts) = split(/\s+/);
3891 foreach my $ext (@exts) {
3892 $mimemap{$ext} = $mimetype;
3893 }
3894 }
3895 close($mh);
3896
3897 $filename =~ /\.([^.]*)$/;
3898 return $mimemap{$1};
3899}
3900
3901sub mimetype_guess {
3902 my $filename = shift;
3903 my $mime;
3904 $filename =~ /\./ or return undef;
3905
3906 if ($mimetypes_file) {
3907 my $file = $mimetypes_file;
3908 if ($file !~ m!^/!) { # if it is relative path
3909 # it is relative to project
3910 $file = "$projectroot/$project/$file";
3911 }
3912 $mime = mimetype_guess_file($filename, $file);
3913 }
3914 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
3915 return $mime;
3916}
3917
3918sub blob_mimetype {
3919 my $fd = shift;
3920 my $filename = shift;
3921
3922 if ($filename) {
3923 my $mime = mimetype_guess($filename);
3924 $mime and return $mime;
3925 }
3926
3927 # just in case
3928 return $default_blob_plain_mimetype unless $fd;
3929
3930 if (-T $fd) {
3931 return 'text/plain';
3932 } elsif (! $filename) {
3933 return 'application/octet-stream';
3934 } elsif ($filename =~ m/\.png$/i) {
3935 return 'image/png';
3936 } elsif ($filename =~ m/\.gif$/i) {
3937 return 'image/gif';
3938 } elsif ($filename =~ m/\.jpe?g$/i) {
3939 return 'image/jpeg';
3940 } else {
3941 return 'application/octet-stream';
3942 }
3943}
3944
3945sub blob_contenttype {
3946 my ($fd, $file_name, $type) = @_;
3947
3948 $type ||= blob_mimetype($fd, $file_name);
3949 if ($type eq 'text/plain' && defined $default_text_plain_charset) {
3950 $type .= "; charset=$default_text_plain_charset";
3951 }
3952
3953 return $type;
3954}
3955
3956# guess file syntax for syntax highlighting; return undef if no highlighting
3957# the name of syntax can (in the future) depend on syntax highlighter used
3958sub guess_file_syntax {
3959 my ($highlight, $file_name) = @_;
3960 return undef unless ($highlight && defined $file_name);
3961 my $basename = basename($file_name, '.in');
3962 return $highlight_basename{$basename}
3963 if exists $highlight_basename{$basename};
3964
3965 $basename =~ /\.([^.]*)$/;
3966 my $ext = $1 or return undef;
3967 return $highlight_ext{$ext}
3968 if exists $highlight_ext{$ext};
3969
3970 return undef;
3971}
3972
3973# run highlighter and return FD of its output,
3974# or return original FD if no highlighting
3975sub run_highlighter {
3976 my ($fd, $highlight, $syntax) = @_;
3977 return $fd unless ($highlight);
3978
3979 close $fd;
3980 my $syntax_arg = (defined $syntax) ? "--syntax $syntax" : "--force";
3981 open $fd, quote_command(git_cmd(), "cat-file", "blob", $hash)." | ".
3982 quote_command($^X, '-CO', '-MEncode=decode,FB_DEFAULT', '-pse',
3983 '$_ = decode($fe, $_, FB_DEFAULT) if !utf8::decode($_);',
3984 '--', "-fe=$fallback_encoding")." | ".
3985 quote_command($highlight_bin).
3986 " --replace-tabs=8 --fragment $syntax_arg |"
3987 or die_error(500, "Couldn't open file or run syntax highlighter");
3988 return $fd;
3989}
3990
3991## ======================================================================
3992## functions printing HTML: header, footer, error page
3993
3994sub get_page_title {
3995 my $title = to_utf8($site_name);
3996
3997 unless (defined $project) {
3998 if (defined $project_filter) {
3999 $title .= " - projects in '" . esc_path($project_filter) . "'";
4000 }
4001 return $title;
4002 }
4003 $title .= " - " . to_utf8($project);
4004
4005 return $title unless (defined $action);
4006 $title .= "/$action"; # $action is US-ASCII (7bit ASCII)
4007
4008 return $title unless (defined $file_name);
4009 $title .= " - " . esc_path($file_name);
4010 if ($action eq "tree" && $file_name !~ m|/$|) {
4011 $title .= "/";
4012 }
4013
4014 return $title;
4015}
4016
4017sub get_content_type_html {
4018 # require explicit support from the UA if we are to send the page as
4019 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
4020 # we have to do this because MSIE sometimes globs '*/*', pretending to
4021 # support xhtml+xml but choking when it gets what it asked for.
4022 if (defined $cgi->http('HTTP_ACCEPT') &&
4023 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
4024 $cgi->Accept('application/xhtml+xml') != 0) {
4025 return 'application/xhtml+xml';
4026 } else {
4027 return 'text/html';
4028 }
4029}
4030
4031sub print_feed_meta {
4032 if (defined $project) {
4033 my %href_params = get_feed_info();
4034 if (!exists $href_params{'-title'}) {
4035 $href_params{'-title'} = 'log';
4036 }
4037
4038 foreach my $format (qw(RSS Atom)) {
4039 my $type = lc($format);
4040 my %link_attr = (
4041 '-rel' => 'alternate',
4042 '-title' => esc_attr("$project - $href_params{'-title'} - $format feed"),
4043 '-type' => "application/$type+xml"
4044 );
4045
4046 $href_params{'extra_options'} = undef;
4047 $href_params{'action'} = $type;
4048 $link_attr{'-href'} = href(%href_params);
4049 print "<link ".
4050 "rel=\"$link_attr{'-rel'}\" ".
4051 "title=\"$link_attr{'-title'}\" ".
4052 "href=\"$link_attr{'-href'}\" ".
4053 "type=\"$link_attr{'-type'}\" ".
4054 "/>\n";
4055
4056 $href_params{'extra_options'} = '--no-merges';
4057 $link_attr{'-href'} = href(%href_params);
4058 $link_attr{'-title'} .= ' (no merges)';
4059 print "<link ".
4060 "rel=\"$link_attr{'-rel'}\" ".
4061 "title=\"$link_attr{'-title'}\" ".
4062 "href=\"$link_attr{'-href'}\" ".
4063 "type=\"$link_attr{'-type'}\" ".
4064 "/>\n";
4065 }
4066
4067 } else {
4068 printf('<link rel="alternate" title="%s projects list" '.
4069 'href="%s" type="text/plain; charset=utf-8" />'."\n",
4070 esc_attr($site_name), href(project=>undef, action=>"project_index"));
4071 printf('<link rel="alternate" title="%s projects feeds" '.
4072 'href="%s" type="text/x-opml" />'."\n",
4073 esc_attr($site_name), href(project=>undef, action=>"opml"));
4074 }
4075}
4076
4077sub print_header_links {
4078 my $status = shift;
4079
4080 # print out each stylesheet that exist, providing backwards capability
4081 # for those people who defined $stylesheet in a config file
4082 if (defined $stylesheet) {
4083 print '<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";
4084 } else {
4085 foreach my $stylesheet (@stylesheets) {
4086 next unless $stylesheet;
4087 print '<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";
4088 }
4089 }
4090 print_feed_meta()
4091 if ($status eq '200 OK');
4092 if (defined $favicon) {
4093 print qq(<link rel="shortcut icon" href=").esc_url($favicon).qq(" type="image/png" />\n);
4094 }
4095}
4096
4097sub print_nav_breadcrumbs_path {
4098 my $dirprefix = undef;
4099 while (my $part = shift) {
4100 $dirprefix .= "/" if defined $dirprefix;
4101 $dirprefix .= $part;
68aeac55
AT
4102 if (scalar @_ != 0) {
4103 print $cgi->a({-href => href(action => "summary")}, esc_html($part)) . " / ";
4104 } else {
4105 print $cgi->a({-href => href(action => "summary")}, esc_html($part));
4106 }
f35f44b7
AT
4107 }
4108}
4109
4110sub print_nav_breadcrumbs {
4111 my %opts = @_;
4112
4113 for my $crumb (@extra_breadcrumbs, [ $home_link_str => $home_link ]) {
4114 print $cgi->a({-href => esc_url($crumb->[1])}, $crumb->[0]) . " / ";
4115 }
4116 if (defined $project) {
4117 my @dirname = split '/', $project;
4118 my $projectbasename = pop @dirname;
4119 print_nav_breadcrumbs_path(@dirname);
68aeac55
AT
4120 if ($projectbasename != ".git") {
4121 print $cgi->a({-href => href(action=>"summary")}, esc_html($projectbasename));
4122 }
f35f44b7
AT
4123 if (defined $action) {
4124 my $action_print = $action ;
4125 if (defined $opts{-action_extra}) {
4126 $action_print = $cgi->a({-href => href(action=>$action)},
4127 $action);
4128 }
4129 print " / $action_print";
4130 }
4131 if (defined $opts{-action_extra}) {
4132 print " / $opts{-action_extra}";
4133 }
4134 print "\n";
4135 } elsif (defined $project_filter) {
4136 print_nav_breadcrumbs_path(split '/', $project_filter);
4137 }
4138}
4139
4140sub print_search_form {
4141 if (!defined $searchtext) {
4142 $searchtext = "";
4143 }
4144 my $search_hash;
4145 if (defined $hash_base) {
4146 $search_hash = $hash_base;
4147 } elsif (defined $hash) {
4148 $search_hash = $hash;
4149 } else {
4150 $search_hash = "HEAD";
4151 }
4152 my $action = $my_uri;
4153 my $use_pathinfo = gitweb_check_feature('pathinfo');
4154 if ($use_pathinfo) {
4155 $action .= "/".esc_url($project);
4156 }
4157 print $cgi->start_form(-method => "get", -action => $action) .
4158 "<div class=\"search\">\n" .
4159 (!$use_pathinfo &&
4160 $cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) . "\n") .
4161 $cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) . "\n" .
4162 $cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) . "\n" .
4163 $cgi->popup_menu(-name => 'st', -default => 'commit',
4164 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
4165 " " . $cgi->a({-href => href(action=>"search_help"),
4166 -title => "search help" }, "?") . " search:\n",
4167 $cgi->textfield(-name => "s", -value => $searchtext, -override => 1) . "\n" .
4168 "<span title=\"Extended regular expression\">" .
4169 $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
4170 -checked => $search_use_regexp) .
4171 "</span>" .
4172 "</div>" .
4173 $cgi->end_form() . "\n";
4174}
4175
4176sub git_header_html {
4177 my $status = shift || "200 OK";
4178 my $expires = shift;
4179 my %opts = @_;
4180
4181 my $title = get_page_title();
4182 my $content_type = get_content_type_html();
4183 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
4184 -status=> $status, -expires => $expires)
4185 unless ($opts{'-no_http_header'});
4186 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
4187 print <<EOF;
4188<?xml version="1.0" encoding="utf-8"?>
4189<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
4190<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
4191<!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
4192<!-- git core binaries version $git_version -->
4193<head>
4194<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
4195<meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
4196<meta name="robots" content="index, nofollow"/>
4197<title>$title</title>
4198EOF
4199 # the stylesheet, favicon etc urls won't work correctly with path_info
4200 # unless we set the appropriate base URL
4201 if ($ENV{'PATH_INFO'}) {
4202