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