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