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