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