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