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