]> Lady’s Gitweb - Gitweb/blob - gitweb.perl
72634848f3bc1d67adb08502cb3d0bb9702f9b06b93f29ebebc21469a0077a17
[Gitweb] / gitweb.perl
1 #!/usr/bin/perl
2
3 # gitweb - simple web interface to track changes in git repositories
4 #
5 # (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org>
6 # (C) 2005, Christian Gierke
7 #
8 # This program is licensed under the GPLv2
9
10 use strict;
11 use warnings;
12 use CGI qw(:standard :escapeHTML -nosticky);
13 use CGI::Util qw(unescape);
14 use CGI::Carp qw(fatalsToBrowser);
15 use Encode;
16 use Fcntl ':mode';
17 use File::Find qw();
18 use File::Basename qw(basename);
19 binmode STDOUT, ':utf8';
20
21 BEGIN {
22 CGI->compile() if $ENV{'MOD_PERL'};
23 }
24
25 our $cgi = new CGI;
26 our $version = "++GIT_VERSION++";
27 our $my_url = $cgi->url();
28 our $my_uri = $cgi->url(-absolute => 1);
29
30 # core git executable to use
31 # this can just be "git" if your webserver has a sensible PATH
32 our $GIT = "++GIT_BINDIR++/git";
33
34 # absolute fs-path which will be prepended to the project path
35 #our $projectroot = "/pub/scm";
36 our $projectroot = "++GITWEB_PROJECTROOT++";
37
38 # fs traversing limit for getting project list
39 # the number is relative to the projectroot
40 our $project_maxdepth = "++GITWEB_PROJECT_MAXDEPTH++";
41
42 # target of the home link on top of all pages
43 our $home_link = $my_uri || "/";
44
45 # string of the home link on top of all pages
46 our $home_link_str = "++GITWEB_HOME_LINK_STR++";
47
48 # name of your site or organization to appear in page titles
49 # replace this with something more descriptive for clearer bookmarks
50 our $site_name = "++GITWEB_SITENAME++"
51 || ($ENV{'SERVER_NAME'} || "Untitled") . " Git";
52
53 # filename of html text to include at top of each page
54 our $site_header = "++GITWEB_SITE_HEADER++";
55 # html text to include at home page
56 our $home_text = "++GITWEB_HOMETEXT++";
57 # filename of html text to include at bottom of each page
58 our $site_footer = "++GITWEB_SITE_FOOTER++";
59
60 # URI of stylesheets
61 our @stylesheets = ("++GITWEB_CSS++");
62 # URI of a single stylesheet, which can be overridden in GITWEB_CONFIG.
63 our $stylesheet = undef;
64
65 # URI of GIT logo (72x27 size)
66 our $logo = "++GITWEB_LOGO++";
67 # URI of GIT favicon, assumed to be image/png type
68 our $favicon = "++GITWEB_FAVICON++";
69
70 # URI and label (title) of GIT logo link
71 #our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
72 #our $logo_label = "git documentation";
73 our $logo_url = "http://git.or.cz/";
74 our $logo_label = "git homepage";
75
76 # source of projects list
77 our $projects_list = "++GITWEB_LIST++";
78
79 # the width (in characters) of the projects list "Description" column
80 our $projects_list_description_width = 25;
81
82 # default order of projects list
83 # valid values are none, project, descr, owner, and age
84 our $default_projects_order = "project";
85
86 # show repository only if this file exists
87 # (only effective if this variable evaluates to true)
88 our $export_ok = "++GITWEB_EXPORT_OK++";
89
90 # only allow viewing of repositories also shown on the overview page
91 our $strict_export = "++GITWEB_STRICT_EXPORT++";
92
93 # list of git base URLs used for URL to where fetch project from,
94 # i.e. full URL is "$git_base_url/$project"
95 our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
96
97 # default blob_plain mimetype and default charset for text/plain blob
98 our $default_blob_plain_mimetype = 'text/plain';
99 our $default_text_plain_charset = undef;
100
101 # file to use for guessing MIME types before trying /etc/mime.types
102 # (relative to the current git repository)
103 our $mimetypes_file = undef;
104
105 # assume this charset if line contains non-UTF-8 characters;
106 # it should be valid encoding (see Encoding::Supported(3pm) for list),
107 # for which encoding all byte sequences are valid, for example
108 # 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
109 # could be even 'utf-8' for the old behavior)
110 our $fallback_encoding = 'latin1';
111
112 # rename detection options for git-diff and git-diff-tree
113 # - default is '-M', with the cost proportional to
114 # (number of removed files) * (number of new files).
115 # - more costly is '-C' (which implies '-M'), with the cost proportional to
116 # (number of changed files + number of removed files) * (number of new files)
117 # - even more costly is '-C', '--find-copies-harder' with cost
118 # (number of files in the original tree) * (number of new files)
119 # - one might want to include '-B' option, e.g. '-B', '-M'
120 our @diff_opts = ('-M'); # taken from git_commit
121
122 # information about snapshot formats that gitweb is capable of serving
123 our %known_snapshot_formats = (
124 # name => {
125 # 'display' => display name,
126 # 'type' => mime type,
127 # 'suffix' => filename suffix,
128 # 'format' => --format for git-archive,
129 # 'compressor' => [compressor command and arguments]
130 # (array reference, optional)}
131 #
132 'tgz' => {
133 'display' => 'tar.gz',
134 'type' => 'application/x-gzip',
135 'suffix' => '.tar.gz',
136 'format' => 'tar',
137 'compressor' => ['gzip']},
138
139 'tbz2' => {
140 'display' => 'tar.bz2',
141 'type' => 'application/x-bzip2',
142 'suffix' => '.tar.bz2',
143 'format' => 'tar',
144 'compressor' => ['bzip2']},
145
146 'zip' => {
147 'display' => 'zip',
148 'type' => 'application/x-zip',
149 'suffix' => '.zip',
150 'format' => 'zip'},
151 );
152
153 # Aliases so we understand old gitweb.snapshot values in repository
154 # configuration.
155 our %known_snapshot_format_aliases = (
156 'gzip' => 'tgz',
157 'bzip2' => 'tbz2',
158
159 # backward compatibility: legacy gitweb config support
160 'x-gzip' => undef, 'gz' => undef,
161 'x-bzip2' => undef, 'bz2' => undef,
162 'x-zip' => undef, '' => undef,
163 );
164
165 # You define site-wide feature defaults here; override them with
166 # $GITWEB_CONFIG as necessary.
167 our %feature = (
168 # feature => {
169 # 'sub' => feature-sub (subroutine),
170 # 'override' => allow-override (boolean),
171 # 'default' => [ default options...] (array reference)}
172 #
173 # if feature is overridable (it means that allow-override has true value),
174 # then feature-sub will be called with default options as parameters;
175 # return value of feature-sub indicates if to enable specified feature
176 #
177 # if there is no 'sub' key (no feature-sub), then feature cannot be
178 # overriden
179 #
180 # use gitweb_check_feature(<feature>) to check if <feature> is enabled
181
182 # Enable the 'blame' blob view, showing the last commit that modified
183 # each line in the file. This can be very CPU-intensive.
184
185 # To enable system wide have in $GITWEB_CONFIG
186 # $feature{'blame'}{'default'} = [1];
187 # To have project specific config enable override in $GITWEB_CONFIG
188 # $feature{'blame'}{'override'} = 1;
189 # and in project config gitweb.blame = 0|1;
190 'blame' => {
191 'sub' => \&feature_blame,
192 'override' => 0,
193 'default' => [0]},
194
195 # Enable the 'snapshot' link, providing a compressed archive of any
196 # tree. This can potentially generate high traffic if you have large
197 # project.
198
199 # Value is a list of formats defined in %known_snapshot_formats that
200 # you wish to offer.
201 # To disable system wide have in $GITWEB_CONFIG
202 # $feature{'snapshot'}{'default'} = [];
203 # To have project specific config enable override in $GITWEB_CONFIG
204 # $feature{'snapshot'}{'override'} = 1;
205 # and in project config, a comma-separated list of formats or "none"
206 # to disable. Example: gitweb.snapshot = tbz2,zip;
207 'snapshot' => {
208 'sub' => \&feature_snapshot,
209 'override' => 0,
210 'default' => ['tgz']},
211
212 # Enable text search, which will list the commits which match author,
213 # committer or commit text to a given string. Enabled by default.
214 # Project specific override is not supported.
215 'search' => {
216 'override' => 0,
217 'default' => [1]},
218
219 # Enable grep search, which will list the files in currently selected
220 # tree containing the given string. Enabled by default. This can be
221 # potentially CPU-intensive, of course.
222
223 # To enable system wide have in $GITWEB_CONFIG
224 # $feature{'grep'}{'default'} = [1];
225 # To have project specific config enable override in $GITWEB_CONFIG
226 # $feature{'grep'}{'override'} = 1;
227 # and in project config gitweb.grep = 0|1;
228 'grep' => {
229 'override' => 0,
230 'default' => [1]},
231
232 # Enable the pickaxe search, which will list the commits that modified
233 # a given string in a file. This can be practical and quite faster
234 # alternative to 'blame', but still potentially CPU-intensive.
235
236 # To enable system wide have in $GITWEB_CONFIG
237 # $feature{'pickaxe'}{'default'} = [1];
238 # To have project specific config enable override in $GITWEB_CONFIG
239 # $feature{'pickaxe'}{'override'} = 1;
240 # and in project config gitweb.pickaxe = 0|1;
241 'pickaxe' => {
242 'sub' => \&feature_pickaxe,
243 'override' => 0,
244 'default' => [1]},
245
246 # Make gitweb use an alternative format of the URLs which can be
247 # more readable and natural-looking: project name is embedded
248 # directly in the path and the query string contains other
249 # auxiliary information. All gitweb installations recognize
250 # URL in either format; this configures in which formats gitweb
251 # generates links.
252
253 # To enable system wide have in $GITWEB_CONFIG
254 # $feature{'pathinfo'}{'default'} = [1];
255 # Project specific override is not supported.
256
257 # Note that you will need to change the default location of CSS,
258 # favicon, logo and possibly other files to an absolute URL. Also,
259 # if gitweb.cgi serves as your indexfile, you will need to force
260 # $my_uri to contain the script name in your $GITWEB_CONFIG.
261 'pathinfo' => {
262 'override' => 0,
263 'default' => [0]},
264
265 # Make gitweb consider projects in project root subdirectories
266 # to be forks of existing projects. Given project $projname.git,
267 # projects matching $projname/*.git will not be shown in the main
268 # projects list, instead a '+' mark will be added to $projname
269 # there and a 'forks' view will be enabled for the project, listing
270 # all the forks. If project list is taken from a file, forks have
271 # to be listed after the main project.
272
273 # To enable system wide have in $GITWEB_CONFIG
274 # $feature{'forks'}{'default'} = [1];
275 # Project specific override is not supported.
276 'forks' => {
277 'override' => 0,
278 'default' => [0]},
279 );
280
281 sub gitweb_check_feature {
282 my ($name) = @_;
283 return unless exists $feature{$name};
284 my ($sub, $override, @defaults) = (
285 $feature{$name}{'sub'},
286 $feature{$name}{'override'},
287 @{$feature{$name}{'default'}});
288 if (!$override) { return @defaults; }
289 if (!defined $sub) {
290 warn "feature $name is not overrideable";
291 return @defaults;
292 }
293 return $sub->(@defaults);
294 }
295
296 sub feature_blame {
297 my ($val) = git_get_project_config('blame', '--bool');
298
299 if ($val eq 'true') {
300 return 1;
301 } elsif ($val eq 'false') {
302 return 0;
303 }
304
305 return $_[0];
306 }
307
308 sub feature_snapshot {
309 my (@fmts) = @_;
310
311 my ($val) = git_get_project_config('snapshot');
312
313 if ($val) {
314 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
315 }
316
317 return @fmts;
318 }
319
320 sub feature_grep {
321 my ($val) = git_get_project_config('grep', '--bool');
322
323 if ($val eq 'true') {
324 return (1);
325 } elsif ($val eq 'false') {
326 return (0);
327 }
328
329 return ($_[0]);
330 }
331
332 sub feature_pickaxe {
333 my ($val) = git_get_project_config('pickaxe', '--bool');
334
335 if ($val eq 'true') {
336 return (1);
337 } elsif ($val eq 'false') {
338 return (0);
339 }
340
341 return ($_[0]);
342 }
343
344 # checking HEAD file with -e is fragile if the repository was
345 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
346 # and then pruned.
347 sub check_head_link {
348 my ($dir) = @_;
349 my $headfile = "$dir/HEAD";
350 return ((-e $headfile) ||
351 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
352 }
353
354 sub check_export_ok {
355 my ($dir) = @_;
356 return (check_head_link($dir) &&
357 (!$export_ok || -e "$dir/$export_ok"));
358 }
359
360 # process alternate names for backward compatibility
361 # filter out unsupported (unknown) snapshot formats
362 sub filter_snapshot_fmts {
363 my @fmts = @_;
364
365 @fmts = map {
366 exists $known_snapshot_format_aliases{$_} ?
367 $known_snapshot_format_aliases{$_} : $_} @fmts;
368 @fmts = grep(exists $known_snapshot_formats{$_}, @fmts);
369
370 }
371
372 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
373 do $GITWEB_CONFIG if -e $GITWEB_CONFIG;
374
375 # version of the core git binary
376 our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
377
378 $projects_list ||= $projectroot;
379
380 # ======================================================================
381 # input validation and dispatch
382 our $action = $cgi->param('a');
383 if (defined $action) {
384 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
385 die_error(undef, "Invalid action parameter");
386 }
387 }
388
389 # parameters which are pathnames
390 our $project = $cgi->param('p');
391 if (defined $project) {
392 if (!validate_pathname($project) ||
393 !(-d "$projectroot/$project") ||
394 !check_head_link("$projectroot/$project") ||
395 ($export_ok && !(-e "$projectroot/$project/$export_ok")) ||
396 ($strict_export && !project_in_list($project))) {
397 undef $project;
398 die_error(undef, "No such project");
399 }
400 }
401
402 our $file_name = $cgi->param('f');
403 if (defined $file_name) {
404 if (!validate_pathname($file_name)) {
405 die_error(undef, "Invalid file parameter");
406 }
407 }
408
409 our $file_parent = $cgi->param('fp');
410 if (defined $file_parent) {
411 if (!validate_pathname($file_parent)) {
412 die_error(undef, "Invalid file parent parameter");
413 }
414 }
415
416 # parameters which are refnames
417 our $hash = $cgi->param('h');
418 if (defined $hash) {
419 if (!validate_refname($hash)) {
420 die_error(undef, "Invalid hash parameter");
421 }
422 }
423
424 our $hash_parent = $cgi->param('hp');
425 if (defined $hash_parent) {
426 if (!validate_refname($hash_parent)) {
427 die_error(undef, "Invalid hash parent parameter");
428 }
429 }
430
431 our $hash_base = $cgi->param('hb');
432 if (defined $hash_base) {
433 if (!validate_refname($hash_base)) {
434 die_error(undef, "Invalid hash base parameter");
435 }
436 }
437
438 my %allowed_options = (
439 "--no-merges" => [ qw(rss atom log shortlog history) ],
440 );
441
442 our @extra_options = $cgi->param('opt');
443 if (defined @extra_options) {
444 foreach my $opt (@extra_options) {
445 if (not exists $allowed_options{$opt}) {
446 die_error(undef, "Invalid option parameter");
447 }
448 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
449 die_error(undef, "Invalid option parameter for this action");
450 }
451 }
452 }
453
454 our $hash_parent_base = $cgi->param('hpb');
455 if (defined $hash_parent_base) {
456 if (!validate_refname($hash_parent_base)) {
457 die_error(undef, "Invalid hash parent base parameter");
458 }
459 }
460
461 # other parameters
462 our $page = $cgi->param('pg');
463 if (defined $page) {
464 if ($page =~ m/[^0-9]/) {
465 die_error(undef, "Invalid page parameter");
466 }
467 }
468
469 our $searchtype = $cgi->param('st');
470 if (defined $searchtype) {
471 if ($searchtype =~ m/[^a-z]/) {
472 die_error(undef, "Invalid searchtype parameter");
473 }
474 }
475
476 our $searchtext = $cgi->param('s');
477 our $search_regexp;
478 if (defined $searchtext) {
479 if (length($searchtext) < 2) {
480 die_error(undef, "At least two characters are required for search parameter");
481 }
482 $search_regexp = quotemeta $searchtext;
483 }
484
485 # now read PATH_INFO and use it as alternative to parameters
486 sub evaluate_path_info {
487 return if defined $project;
488 my $path_info = $ENV{"PATH_INFO"};
489 return if !$path_info;
490 $path_info =~ s,^/+,,;
491 return if !$path_info;
492 # find which part of PATH_INFO is project
493 $project = $path_info;
494 $project =~ s,/+$,,;
495 while ($project && !check_head_link("$projectroot/$project")) {
496 $project =~ s,/*[^/]*$,,;
497 }
498 # validate project
499 $project = validate_pathname($project);
500 if (!$project ||
501 ($export_ok && !-e "$projectroot/$project/$export_ok") ||
502 ($strict_export && !project_in_list($project))) {
503 undef $project;
504 return;
505 }
506 # do not change any parameters if an action is given using the query string
507 return if $action;
508 $path_info =~ s,^$project/*,,;
509 my ($refname, $pathname) = split(/:/, $path_info, 2);
510 if (defined $pathname) {
511 # we got "project.git/branch:filename" or "project.git/branch:dir/"
512 # we could use git_get_type(branch:pathname), but it needs $git_dir
513 $pathname =~ s,^/+,,;
514 if (!$pathname || substr($pathname, -1) eq "/") {
515 $action ||= "tree";
516 $pathname =~ s,/$,,;
517 } else {
518 $action ||= "blob_plain";
519 }
520 $hash_base ||= validate_refname($refname);
521 $file_name ||= validate_pathname($pathname);
522 } elsif (defined $refname) {
523 # we got "project.git/branch"
524 $action ||= "shortlog";
525 $hash ||= validate_refname($refname);
526 }
527 }
528 evaluate_path_info();
529
530 # path to the current git repository
531 our $git_dir;
532 $git_dir = "$projectroot/$project" if $project;
533
534 # dispatch
535 my %actions = (
536 "blame" => \&git_blame2,
537 "blobdiff" => \&git_blobdiff,
538 "blobdiff_plain" => \&git_blobdiff_plain,
539 "blob" => \&git_blob,
540 "blob_plain" => \&git_blob_plain,
541 "commitdiff" => \&git_commitdiff,
542 "commitdiff_plain" => \&git_commitdiff_plain,
543 "commit" => \&git_commit,
544 "forks" => \&git_forks,
545 "heads" => \&git_heads,
546 "history" => \&git_history,
547 "log" => \&git_log,
548 "rss" => \&git_rss,
549 "atom" => \&git_atom,
550 "search" => \&git_search,
551 "search_help" => \&git_search_help,
552 "shortlog" => \&git_shortlog,
553 "summary" => \&git_summary,
554 "tag" => \&git_tag,
555 "tags" => \&git_tags,
556 "tree" => \&git_tree,
557 "snapshot" => \&git_snapshot,
558 "object" => \&git_object,
559 # those below don't need $project
560 "opml" => \&git_opml,
561 "project_list" => \&git_project_list,
562 "project_index" => \&git_project_index,
563 );
564
565 if (!defined $action) {
566 if (defined $hash) {
567 $action = git_get_type($hash);
568 } elsif (defined $hash_base && defined $file_name) {
569 $action = git_get_type("$hash_base:$file_name");
570 } elsif (defined $project) {
571 $action = 'summary';
572 } else {
573 $action = 'project_list';
574 }
575 }
576 if (!defined($actions{$action})) {
577 die_error(undef, "Unknown action");
578 }
579 if ($action !~ m/^(opml|project_list|project_index)$/ &&
580 !$project) {
581 die_error(undef, "Project needed");
582 }
583 $actions{$action}->();
584 exit;
585
586 ## ======================================================================
587 ## action links
588
589 sub href(%) {
590 my %params = @_;
591 # default is to use -absolute url() i.e. $my_uri
592 my $href = $params{-full} ? $my_url : $my_uri;
593
594 # XXX: Warning: If you touch this, check the search form for updating,
595 # too.
596
597 my @mapping = (
598 project => "p",
599 action => "a",
600 file_name => "f",
601 file_parent => "fp",
602 hash => "h",
603 hash_parent => "hp",
604 hash_base => "hb",
605 hash_parent_base => "hpb",
606 page => "pg",
607 order => "o",
608 searchtext => "s",
609 searchtype => "st",
610 snapshot_format => "sf",
611 extra_options => "opt",
612 );
613 my %mapping = @mapping;
614
615 $params{'project'} = $project unless exists $params{'project'};
616
617 if ($params{-replay}) {
618 while (my ($name, $symbol) = each %mapping) {
619 if (!exists $params{$name}) {
620 # to allow for multivalued params we use arrayref form
621 $params{$name} = [ $cgi->param($symbol) ];
622 }
623 }
624 }
625
626 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
627 if ($use_pathinfo) {
628 # use PATH_INFO for project name
629 $href .= "/$params{'project'}" if defined $params{'project'};
630 delete $params{'project'};
631
632 # Summary just uses the project path URL
633 if (defined $params{'action'} && $params{'action'} eq 'summary') {
634 delete $params{'action'};
635 }
636 }
637
638 # now encode the parameters explicitly
639 my @result = ();
640 for (my $i = 0; $i < @mapping; $i += 2) {
641 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
642 if (defined $params{$name}) {
643 if (ref($params{$name}) eq "ARRAY") {
644 foreach my $par (@{$params{$name}}) {
645 push @result, $symbol . "=" . esc_param($par);
646 }
647 } else {
648 push @result, $symbol . "=" . esc_param($params{$name});
649 }
650 }
651 }
652 $href .= "?" . join(';', @result) if scalar @result;
653
654 return $href;
655 }
656
657
658 ## ======================================================================
659 ## validation, quoting/unquoting and escaping
660
661 sub validate_pathname {
662 my $input = shift || return undef;
663
664 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
665 # at the beginning, at the end, and between slashes.
666 # also this catches doubled slashes
667 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
668 return undef;
669 }
670 # no null characters
671 if ($input =~ m!\0!) {
672 return undef;
673 }
674 return $input;
675 }
676
677 sub validate_refname {
678 my $input = shift || return undef;
679
680 # textual hashes are O.K.
681 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
682 return $input;
683 }
684 # it must be correct pathname
685 $input = validate_pathname($input)
686 or return undef;
687 # restrictions on ref name according to git-check-ref-format
688 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
689 return undef;
690 }
691 return $input;
692 }
693
694 # decode sequences of octets in utf8 into Perl's internal form,
695 # which is utf-8 with utf8 flag set if needed. gitweb writes out
696 # in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
697 sub to_utf8 {
698 my $str = shift;
699 if (utf8::valid($str)) {
700 utf8::decode($str);
701 return $str;
702 } else {
703 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
704 }
705 }
706
707 # quote unsafe chars, but keep the slash, even when it's not
708 # correct, but quoted slashes look too horrible in bookmarks
709 sub esc_param {
710 my $str = shift;
711 $str =~ s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X", ord($1))/eg;
712 $str =~ s/\+/%2B/g;
713 $str =~ s/ /\+/g;
714 return $str;
715 }
716
717 # quote unsafe chars in whole URL, so some charactrs cannot be quoted
718 sub esc_url {
719 my $str = shift;
720 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
721 $str =~ s/\+/%2B/g;
722 $str =~ s/ /\+/g;
723 return $str;
724 }
725
726 # replace invalid utf8 character with SUBSTITUTION sequence
727 sub esc_html ($;%) {
728 my $str = shift;
729 my %opts = @_;
730
731 $str = to_utf8($str);
732 $str = $cgi->escapeHTML($str);
733 if ($opts{'-nbsp'}) {
734 $str =~ s/ /&nbsp;/g;
735 }
736 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
737 return $str;
738 }
739
740 # quote control characters and escape filename to HTML
741 sub esc_path {
742 my $str = shift;
743 my %opts = @_;
744
745 $str = to_utf8($str);
746 $str = $cgi->escapeHTML($str);
747 if ($opts{'-nbsp'}) {
748 $str =~ s/ /&nbsp;/g;
749 }
750 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
751 return $str;
752 }
753
754 # Make control characters "printable", using character escape codes (CEC)
755 sub quot_cec {
756 my $cntrl = shift;
757 my %opts = @_;
758 my %es = ( # character escape codes, aka escape sequences
759 "\t" => '\t', # tab (HT)
760 "\n" => '\n', # line feed (LF)
761 "\r" => '\r', # carrige return (CR)
762 "\f" => '\f', # form feed (FF)
763 "\b" => '\b', # backspace (BS)
764 "\a" => '\a', # alarm (bell) (BEL)
765 "\e" => '\e', # escape (ESC)
766 "\013" => '\v', # vertical tab (VT)
767 "\000" => '\0', # nul character (NUL)
768 );
769 my $chr = ( (exists $es{$cntrl})
770 ? $es{$cntrl}
771 : sprintf('\%03o', ord($cntrl)) );
772 if ($opts{-nohtml}) {
773 return $chr;
774 } else {
775 return "<span class=\"cntrl\">$chr</span>";
776 }
777 }
778
779 # Alternatively use unicode control pictures codepoints,
780 # Unicode "printable representation" (PR)
781 sub quot_upr {
782 my $cntrl = shift;
783 my %opts = @_;
784
785 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
786 if ($opts{-nohtml}) {
787 return $chr;
788 } else {
789 return "<span class=\"cntrl\">$chr</span>";
790 }
791 }
792
793 # git may return quoted and escaped filenames
794 sub unquote {
795 my $str = shift;
796
797 sub unq {
798 my $seq = shift;
799 my %es = ( # character escape codes, aka escape sequences
800 't' => "\t", # tab (HT, TAB)
801 'n' => "\n", # newline (NL)
802 'r' => "\r", # return (CR)
803 'f' => "\f", # form feed (FF)
804 'b' => "\b", # backspace (BS)
805 'a' => "\a", # alarm (bell) (BEL)
806 'e' => "\e", # escape (ESC)
807 'v' => "\013", # vertical tab (VT)
808 );
809
810 if ($seq =~ m/^[0-7]{1,3}$/) {
811 # octal char sequence
812 return chr(oct($seq));
813 } elsif (exists $es{$seq}) {
814 # C escape sequence, aka character escape code
815 return $es{$seq};
816 }
817 # quoted ordinary character
818 return $seq;
819 }
820
821 if ($str =~ m/^"(.*)"$/) {
822 # needs unquoting
823 $str = $1;
824 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
825 }
826 return $str;
827 }
828
829 # escape tabs (convert tabs to spaces)
830 sub untabify {
831 my $line = shift;
832
833 while ((my $pos = index($line, "\t")) != -1) {
834 if (my $count = (8 - ($pos % 8))) {
835 my $spaces = ' ' x $count;
836 $line =~ s/\t/$spaces/;
837 }
838 }
839
840 return $line;
841 }
842
843 sub project_in_list {
844 my $project = shift;
845 my @list = git_get_projects_list();
846 return @list && scalar(grep { $_->{'path'} eq $project } @list);
847 }
848
849 ## ----------------------------------------------------------------------
850 ## HTML aware string manipulation
851
852 # Try to chop given string on a word boundary between position
853 # $len and $len+$add_len. If there is no word boundary there,
854 # chop at $len+$add_len. Do not chop if chopped part plus ellipsis
855 # (marking chopped part) would be longer than given string.
856 sub chop_str {
857 my $str = shift;
858 my $len = shift;
859 my $add_len = shift || 10;
860 my $where = shift || 'right'; # 'left' | 'center' | 'right'
861
862 # allow only $len chars, but don't cut a word if it would fit in $add_len
863 # if it doesn't fit, cut it if it's still longer than the dots we would add
864 # remove chopped character entities entirely
865
866 # when chopping in the middle, distribute $len into left and right part
867 # return early if chopping wouldn't make string shorter
868 if ($where eq 'center') {
869 return $str if ($len + 5 >= length($str)); # filler is length 5
870 $len = int($len/2);
871 } else {
872 return $str if ($len + 4 >= length($str)); # filler is length 4
873 }
874
875 # regexps: ending and beginning with word part up to $add_len
876 my $endre = qr/.{$len}\w{0,$add_len}/;
877 my $begre = qr/\w{0,$add_len}.{$len}/;
878
879 if ($where eq 'left') {
880 $str =~ m/^(.*?)($begre)$/;
881 my ($lead, $body) = ($1, $2);
882 if (length($lead) > 4) {
883 $body =~ s/^[^;]*;// if ($lead =~ m/&[^;]*$/);
884 $lead = " ...";
885 }
886 return "$lead$body";
887
888 } elsif ($where eq 'center') {
889 $str =~ m/^($endre)(.*)$/;
890 my ($left, $str) = ($1, $2);
891 $str =~ m/^(.*?)($begre)$/;
892 my ($mid, $right) = ($1, $2);
893 if (length($mid) > 5) {
894 $left =~ s/&[^;]*$//;
895 $right =~ s/^[^;]*;// if ($mid =~ m/&[^;]*$/);
896 $mid = " ... ";
897 }
898 return "$left$mid$right";
899
900 } else {
901 $str =~ m/^($endre)(.*)$/;
902 my $body = $1;
903 my $tail = $2;
904 if (length($tail) > 4) {
905 $body =~ s/&[^;]*$//;
906 $tail = "... ";
907 }
908 return "$body$tail";
909 }
910 }
911
912 # takes the same arguments as chop_str, but also wraps a <span> around the
913 # result with a title attribute if it does get chopped. Additionally, the
914 # string is HTML-escaped.
915 sub chop_and_escape_str {
916 my ($str) = @_;
917
918 my $chopped = chop_str(@_);
919 if ($chopped eq $str) {
920 return esc_html($chopped);
921 } else {
922 $str =~ s/([[:cntrl:]])/?/g;
923 return $cgi->span({-title=>$str}, esc_html($chopped));
924 }
925 }
926
927 ## ----------------------------------------------------------------------
928 ## functions returning short strings
929
930 # CSS class for given age value (in seconds)
931 sub age_class {
932 my $age = shift;
933
934 if (!defined $age) {
935 return "noage";
936 } elsif ($age < 60*60*2) {
937 return "age0";
938 } elsif ($age < 60*60*24*2) {
939 return "age1";
940 } else {
941 return "age2";
942 }
943 }
944
945 # convert age in seconds to "nn units ago" string
946 sub age_string {
947 my $age = shift;
948 my $age_str;
949
950 if ($age > 60*60*24*365*2) {
951 $age_str = (int $age/60/60/24/365);
952 $age_str .= " years ago";
953 } elsif ($age > 60*60*24*(365/12)*2) {
954 $age_str = int $age/60/60/24/(365/12);
955 $age_str .= " months ago";
956 } elsif ($age > 60*60*24*7*2) {
957 $age_str = int $age/60/60/24/7;
958 $age_str .= " weeks ago";
959 } elsif ($age > 60*60*24*2) {
960 $age_str = int $age/60/60/24;
961 $age_str .= " days ago";
962 } elsif ($age > 60*60*2) {
963 $age_str = int $age/60/60;
964 $age_str .= " hours ago";
965 } elsif ($age > 60*2) {
966 $age_str = int $age/60;
967 $age_str .= " min ago";
968 } elsif ($age > 2) {
969 $age_str = int $age;
970 $age_str .= " sec ago";
971 } else {
972 $age_str .= " right now";
973 }
974 return $age_str;
975 }
976
977 use constant {
978 S_IFINVALID => 0030000,
979 S_IFGITLINK => 0160000,
980 };
981
982 # submodule/subproject, a commit object reference
983 sub S_ISGITLINK($) {
984 my $mode = shift;
985
986 return (($mode & S_IFMT) == S_IFGITLINK)
987 }
988
989 # convert file mode in octal to symbolic file mode string
990 sub mode_str {
991 my $mode = oct shift;
992
993 if (S_ISGITLINK($mode)) {
994 return 'm---------';
995 } elsif (S_ISDIR($mode & S_IFMT)) {
996 return 'drwxr-xr-x';
997 } elsif (S_ISLNK($mode)) {
998 return 'lrwxrwxrwx';
999 } elsif (S_ISREG($mode)) {
1000 # git cares only about the executable bit
1001 if ($mode & S_IXUSR) {
1002 return '-rwxr-xr-x';
1003 } else {
1004 return '-rw-r--r--';
1005 };
1006 } else {
1007 return '----------';
1008 }
1009 }
1010
1011 # convert file mode in octal to file type string
1012 sub file_type {
1013 my $mode = shift;
1014
1015 if ($mode !~ m/^[0-7]+$/) {
1016 return $mode;
1017 } else {
1018 $mode = oct $mode;
1019 }
1020
1021 if (S_ISGITLINK($mode)) {
1022 return "submodule";
1023 } elsif (S_ISDIR($mode & S_IFMT)) {
1024 return "directory";
1025 } elsif (S_ISLNK($mode)) {
1026 return "symlink";
1027 } elsif (S_ISREG($mode)) {
1028 return "file";
1029 } else {
1030 return "unknown";
1031 }
1032 }
1033
1034 # convert file mode in octal to file type description string
1035 sub file_type_long {
1036 my $mode = shift;
1037
1038 if ($mode !~ m/^[0-7]+$/) {
1039 return $mode;
1040 } else {
1041 $mode = oct $mode;
1042 }
1043
1044 if (S_ISGITLINK($mode)) {
1045 return "submodule";
1046 } elsif (S_ISDIR($mode & S_IFMT)) {
1047 return "directory";
1048 } elsif (S_ISLNK($mode)) {
1049 return "symlink";
1050 } elsif (S_ISREG($mode)) {
1051 if ($mode & S_IXUSR) {
1052 return "executable";
1053 } else {
1054 return "file";
1055 };
1056 } else {
1057 return "unknown";
1058 }
1059 }
1060
1061
1062 ## ----------------------------------------------------------------------
1063 ## functions returning short HTML fragments, or transforming HTML fragments
1064 ## which don't belong to other sections
1065
1066 # format line of commit message.
1067 sub format_log_line_html {
1068 my $line = shift;
1069
1070 $line = esc_html($line, -nbsp=>1);
1071 if ($line =~ m/([0-9a-fA-F]{8,40})/) {
1072 my $hash_text = $1;
1073 my $link =
1074 $cgi->a({-href => href(action=>"object", hash=>$hash_text),
1075 -class => "text"}, $hash_text);
1076 $line =~ s/$hash_text/$link/;
1077 }
1078 return $line;
1079 }
1080
1081 # format marker of refs pointing to given object
1082 sub format_ref_marker {
1083 my ($refs, $id) = @_;
1084 my $markers = '';
1085
1086 if (defined $refs->{$id}) {
1087 foreach my $ref (@{$refs->{$id}}) {
1088 my ($type, $name) = qw();
1089 # e.g. tags/v2.6.11 or heads/next
1090 if ($ref =~ m!^(.*?)s?/(.*)$!) {
1091 $type = $1;
1092 $name = $2;
1093 } else {
1094 $type = "ref";
1095 $name = $ref;
1096 }
1097
1098 $markers .= " <span class=\"$type\" title=\"$ref\">" .
1099 esc_html($name) . "</span>";
1100 }
1101 }
1102
1103 if ($markers) {
1104 return ' <span class="refs">'. $markers . '</span>';
1105 } else {
1106 return "";
1107 }
1108 }
1109
1110 # format, perhaps shortened and with markers, title line
1111 sub format_subject_html {
1112 my ($long, $short, $href, $extra) = @_;
1113 $extra = '' unless defined($extra);
1114
1115 if (length($short) < length($long)) {
1116 return $cgi->a({-href => $href, -class => "list subject",
1117 -title => to_utf8($long)},
1118 esc_html($short) . $extra);
1119 } else {
1120 return $cgi->a({-href => $href, -class => "list subject"},
1121 esc_html($long) . $extra);
1122 }
1123 }
1124
1125 # format git diff header line, i.e. "diff --(git|combined|cc) ..."
1126 sub format_git_diff_header_line {
1127 my $line = shift;
1128 my $diffinfo = shift;
1129 my ($from, $to) = @_;
1130
1131 if ($diffinfo->{'nparents'}) {
1132 # combined diff
1133 $line =~ s!^(diff (.*?) )"?.*$!$1!;
1134 if ($to->{'href'}) {
1135 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1136 esc_path($to->{'file'}));
1137 } else { # file was deleted (no href)
1138 $line .= esc_path($to->{'file'});
1139 }
1140 } else {
1141 # "ordinary" diff
1142 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
1143 if ($from->{'href'}) {
1144 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
1145 'a/' . esc_path($from->{'file'}));
1146 } else { # file was added (no href)
1147 $line .= 'a/' . esc_path($from->{'file'});
1148 }
1149 $line .= ' ';
1150 if ($to->{'href'}) {
1151 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1152 'b/' . esc_path($to->{'file'}));
1153 } else { # file was deleted
1154 $line .= 'b/' . esc_path($to->{'file'});
1155 }
1156 }
1157
1158 return "<div class=\"diff header\">$line</div>\n";
1159 }
1160
1161 # format extended diff header line, before patch itself
1162 sub format_extended_diff_header_line {
1163 my $line = shift;
1164 my $diffinfo = shift;
1165 my ($from, $to) = @_;
1166
1167 # match <path>
1168 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
1169 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1170 esc_path($from->{'file'}));
1171 }
1172 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
1173 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1174 esc_path($to->{'file'}));
1175 }
1176 # match single <mode>
1177 if ($line =~ m/\s(\d{6})$/) {
1178 $line .= '<span class="info"> (' .
1179 file_type_long($1) .
1180 ')</span>';
1181 }
1182 # match <hash>
1183 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
1184 # can match only for combined diff
1185 $line = 'index ';
1186 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1187 if ($from->{'href'}[$i]) {
1188 $line .= $cgi->a({-href=>$from->{'href'}[$i],
1189 -class=>"hash"},
1190 substr($diffinfo->{'from_id'}[$i],0,7));
1191 } else {
1192 $line .= '0' x 7;
1193 }
1194 # separator
1195 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
1196 }
1197 $line .= '..';
1198 if ($to->{'href'}) {
1199 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1200 substr($diffinfo->{'to_id'},0,7));
1201 } else {
1202 $line .= '0' x 7;
1203 }
1204
1205 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
1206 # can match only for ordinary diff
1207 my ($from_link, $to_link);
1208 if ($from->{'href'}) {
1209 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
1210 substr($diffinfo->{'from_id'},0,7));
1211 } else {
1212 $from_link = '0' x 7;
1213 }
1214 if ($to->{'href'}) {
1215 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1216 substr($diffinfo->{'to_id'},0,7));
1217 } else {
1218 $to_link = '0' x 7;
1219 }
1220 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
1221 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
1222 }
1223
1224 return $line . "<br/>\n";
1225 }
1226
1227 # format from-file/to-file diff header
1228 sub format_diff_from_to_header {
1229 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
1230 my $line;
1231 my $result = '';
1232
1233 $line = $from_line;
1234 #assert($line =~ m/^---/) if DEBUG;
1235 # no extra formatting for "^--- /dev/null"
1236 if (! $diffinfo->{'nparents'}) {
1237 # ordinary (single parent) diff
1238 if ($line =~ m!^--- "?a/!) {
1239 if ($from->{'href'}) {
1240 $line = '--- a/' .
1241 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1242 esc_path($from->{'file'}));
1243 } else {
1244 $line = '--- a/' .
1245 esc_path($from->{'file'});
1246 }
1247 }
1248 $result .= qq!<div class="diff from_file">$line</div>\n!;
1249
1250 } else {
1251 # combined diff (merge commit)
1252 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1253 if ($from->{'href'}[$i]) {
1254 $line = '--- ' .
1255 $cgi->a({-href=>href(action=>"blobdiff",
1256 hash_parent=>$diffinfo->{'from_id'}[$i],
1257 hash_parent_base=>$parents[$i],
1258 file_parent=>$from->{'file'}[$i],
1259 hash=>$diffinfo->{'to_id'},
1260 hash_base=>$hash,
1261 file_name=>$to->{'file'}),
1262 -class=>"path",
1263 -title=>"diff" . ($i+1)},
1264 $i+1) .
1265 '/' .
1266 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
1267 esc_path($from->{'file'}[$i]));
1268 } else {
1269 $line = '--- /dev/null';
1270 }
1271 $result .= qq!<div class="diff from_file">$line</div>\n!;
1272 }
1273 }
1274
1275 $line = $to_line;
1276 #assert($line =~ m/^\+\+\+/) if DEBUG;
1277 # no extra formatting for "^+++ /dev/null"
1278 if ($line =~ m!^\+\+\+ "?b/!) {
1279 if ($to->{'href'}) {
1280 $line = '+++ b/' .
1281 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1282 esc_path($to->{'file'}));
1283 } else {
1284 $line = '+++ b/' .
1285 esc_path($to->{'file'});
1286 }
1287 }
1288 $result .= qq!<div class="diff to_file">$line</div>\n!;
1289
1290 return $result;
1291 }
1292
1293 # create note for patch simplified by combined diff
1294 sub format_diff_cc_simplified {
1295 my ($diffinfo, @parents) = @_;
1296 my $result = '';
1297
1298 $result .= "<div class=\"diff header\">" .
1299 "diff --cc ";
1300 if (!is_deleted($diffinfo)) {
1301 $result .= $cgi->a({-href => href(action=>"blob",
1302 hash_base=>$hash,
1303 hash=>$diffinfo->{'to_id'},
1304 file_name=>$diffinfo->{'to_file'}),
1305 -class => "path"},
1306 esc_path($diffinfo->{'to_file'}));
1307 } else {
1308 $result .= esc_path($diffinfo->{'to_file'});
1309 }
1310 $result .= "</div>\n" . # class="diff header"
1311 "<div class=\"diff nodifferences\">" .
1312 "Simple merge" .
1313 "</div>\n"; # class="diff nodifferences"
1314
1315 return $result;
1316 }
1317
1318 # format patch (diff) line (not to be used for diff headers)
1319 sub format_diff_line {
1320 my $line = shift;
1321 my ($from, $to) = @_;
1322 my $diff_class = "";
1323
1324 chomp $line;
1325
1326 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
1327 # combined diff
1328 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
1329 if ($line =~ m/^\@{3}/) {
1330 $diff_class = " chunk_header";
1331 } elsif ($line =~ m/^\\/) {
1332 $diff_class = " incomplete";
1333 } elsif ($prefix =~ tr/+/+/) {
1334 $diff_class = " add";
1335 } elsif ($prefix =~ tr/-/-/) {
1336 $diff_class = " rem";
1337 }
1338 } else {
1339 # assume ordinary diff
1340 my $char = substr($line, 0, 1);
1341 if ($char eq '+') {
1342 $diff_class = " add";
1343 } elsif ($char eq '-') {
1344 $diff_class = " rem";
1345 } elsif ($char eq '@') {
1346 $diff_class = " chunk_header";
1347 } elsif ($char eq "\\") {
1348 $diff_class = " incomplete";
1349 }
1350 }
1351 $line = untabify($line);
1352 if ($from && $to && $line =~ m/^\@{2} /) {
1353 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
1354 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
1355
1356 $from_lines = 0 unless defined $from_lines;
1357 $to_lines = 0 unless defined $to_lines;
1358
1359 if ($from->{'href'}) {
1360 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
1361 -class=>"list"}, $from_text);
1362 }
1363 if ($to->{'href'}) {
1364 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
1365 -class=>"list"}, $to_text);
1366 }
1367 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
1368 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1369 return "<div class=\"diff$diff_class\">$line</div>\n";
1370 } elsif ($from && $to && $line =~ m/^\@{3}/) {
1371 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
1372 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
1373
1374 @from_text = split(' ', $ranges);
1375 for (my $i = 0; $i < @from_text; ++$i) {
1376 ($from_start[$i], $from_nlines[$i]) =
1377 (split(',', substr($from_text[$i], 1)), 0);
1378 }
1379
1380 $to_text = pop @from_text;
1381 $to_start = pop @from_start;
1382 $to_nlines = pop @from_nlines;
1383
1384 $line = "<span class=\"chunk_info\">$prefix ";
1385 for (my $i = 0; $i < @from_text; ++$i) {
1386 if ($from->{'href'}[$i]) {
1387 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
1388 -class=>"list"}, $from_text[$i]);
1389 } else {
1390 $line .= $from_text[$i];
1391 }
1392 $line .= " ";
1393 }
1394 if ($to->{'href'}) {
1395 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
1396 -class=>"list"}, $to_text);
1397 } else {
1398 $line .= $to_text;
1399 }
1400 $line .= " $prefix</span>" .
1401 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1402 return "<div class=\"diff$diff_class\">$line</div>\n";
1403 }
1404 return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
1405 }
1406
1407 # Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
1408 # linked. Pass the hash of the tree/commit to snapshot.
1409 sub format_snapshot_links {
1410 my ($hash) = @_;
1411 my @snapshot_fmts = gitweb_check_feature('snapshot');
1412 @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
1413 my $num_fmts = @snapshot_fmts;
1414 if ($num_fmts > 1) {
1415 # A parenthesized list of links bearing format names.
1416 # e.g. "snapshot (_tar.gz_ _zip_)"
1417 return "snapshot (" . join(' ', map
1418 $cgi->a({
1419 -href => href(
1420 action=>"snapshot",
1421 hash=>$hash,
1422 snapshot_format=>$_
1423 )
1424 }, $known_snapshot_formats{$_}{'display'})
1425 , @snapshot_fmts) . ")";
1426 } elsif ($num_fmts == 1) {
1427 # A single "snapshot" link whose tooltip bears the format name.
1428 # i.e. "_snapshot_"
1429 my ($fmt) = @snapshot_fmts;
1430 return
1431 $cgi->a({
1432 -href => href(
1433 action=>"snapshot",
1434 hash=>$hash,
1435 snapshot_format=>$fmt
1436 ),
1437 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
1438 }, "snapshot");
1439 } else { # $num_fmts == 0
1440 return undef;
1441 }
1442 }
1443
1444 ## ----------------------------------------------------------------------
1445 ## git utility subroutines, invoking git commands
1446
1447 # returns path to the core git executable and the --git-dir parameter as list
1448 sub git_cmd {
1449 return $GIT, '--git-dir='.$git_dir;
1450 }
1451
1452 # returns path to the core git executable and the --git-dir parameter as string
1453 sub git_cmd_str {
1454 return join(' ', git_cmd());
1455 }
1456
1457 # get HEAD ref of given project as hash
1458 sub git_get_head_hash {
1459 my $project = shift;
1460 my $o_git_dir = $git_dir;
1461 my $retval = undef;
1462 $git_dir = "$projectroot/$project";
1463 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
1464 my $head = <$fd>;
1465 close $fd;
1466 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
1467 $retval = $1;
1468 }
1469 }
1470 if (defined $o_git_dir) {
1471 $git_dir = $o_git_dir;
1472 }
1473 return $retval;
1474 }
1475
1476 # get type of given object
1477 sub git_get_type {
1478 my $hash = shift;
1479
1480 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
1481 my $type = <$fd>;
1482 close $fd or return;
1483 chomp $type;
1484 return $type;
1485 }
1486
1487 # repository configuration
1488 our $config_file = '';
1489 our %config;
1490
1491 # store multiple values for single key as anonymous array reference
1492 # single values stored directly in the hash, not as [ <value> ]
1493 sub hash_set_multi {
1494 my ($hash, $key, $value) = @_;
1495
1496 if (!exists $hash->{$key}) {
1497 $hash->{$key} = $value;
1498 } elsif (!ref $hash->{$key}) {
1499 $hash->{$key} = [ $hash->{$key}, $value ];
1500 } else {
1501 push @{$hash->{$key}}, $value;
1502 }
1503 }
1504
1505 # return hash of git project configuration
1506 # optionally limited to some section, e.g. 'gitweb'
1507 sub git_parse_project_config {
1508 my $section_regexp = shift;
1509 my %config;
1510
1511 local $/ = "\0";
1512
1513 open my $fh, "-|", git_cmd(), "config", '-z', '-l',
1514 or return;
1515
1516 while (my $keyval = <$fh>) {
1517 chomp $keyval;
1518 my ($key, $value) = split(/\n/, $keyval, 2);
1519
1520 hash_set_multi(\%config, $key, $value)
1521 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
1522 }
1523 close $fh;
1524
1525 return %config;
1526 }
1527
1528 # convert config value to boolean, 'true' or 'false'
1529 # no value, number > 0, 'true' and 'yes' values are true
1530 # rest of values are treated as false (never as error)
1531 sub config_to_bool {
1532 my $val = shift;
1533
1534 # strip leading and trailing whitespace
1535 $val =~ s/^\s+//;
1536 $val =~ s/\s+$//;
1537
1538 return (!defined $val || # section.key
1539 ($val =~ /^\d+$/ && $val) || # section.key = 1
1540 ($val =~ /^(?:true|yes)$/i)); # section.key = true
1541 }
1542
1543 # convert config value to simple decimal number
1544 # an optional value suffix of 'k', 'm', or 'g' will cause the value
1545 # to be multiplied by 1024, 1048576, or 1073741824
1546 sub config_to_int {
1547 my $val = shift;
1548
1549 # strip leading and trailing whitespace
1550 $val =~ s/^\s+//;
1551 $val =~ s/\s+$//;
1552
1553 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
1554 $unit = lc($unit);
1555 # unknown unit is treated as 1
1556 return $num * ($unit eq 'g' ? 1073741824 :
1557 $unit eq 'm' ? 1048576 :
1558 $unit eq 'k' ? 1024 : 1);
1559 }
1560 return $val;
1561 }
1562
1563 # convert config value to array reference, if needed
1564 sub config_to_multi {
1565 my $val = shift;
1566
1567 return ref($val) ? $val : (defined($val) ? [ $val ] : []);
1568 }
1569
1570 sub git_get_project_config {
1571 my ($key, $type) = @_;
1572
1573 # key sanity check
1574 return unless ($key);
1575 $key =~ s/^gitweb\.//;
1576 return if ($key =~ m/\W/);
1577
1578 # type sanity check
1579 if (defined $type) {
1580 $type =~ s/^--//;
1581 $type = undef
1582 unless ($type eq 'bool' || $type eq 'int');
1583 }
1584
1585 # get config
1586 if (!defined $config_file ||
1587 $config_file ne "$git_dir/config") {
1588 %config = git_parse_project_config('gitweb');
1589 $config_file = "$git_dir/config";
1590 }
1591
1592 # ensure given type
1593 if (!defined $type) {
1594 return $config{"gitweb.$key"};
1595 } elsif ($type eq 'bool') {
1596 # backward compatibility: 'git config --bool' returns true/false
1597 return config_to_bool($config{"gitweb.$key"}) ? 'true' : 'false';
1598 } elsif ($type eq 'int') {
1599 return config_to_int($config{"gitweb.$key"});
1600 }
1601 return $config{"gitweb.$key"};
1602 }
1603
1604 # get hash of given path at given ref
1605 sub git_get_hash_by_path {
1606 my $base = shift;
1607 my $path = shift || return undef;
1608 my $type = shift;
1609
1610 $path =~ s,/+$,,;
1611
1612 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
1613 or die_error(undef, "Open git-ls-tree failed");
1614 my $line = <$fd>;
1615 close $fd or return undef;
1616
1617 if (!defined $line) {
1618 # there is no tree or hash given by $path at $base
1619 return undef;
1620 }
1621
1622 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1623 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
1624 if (defined $type && $type ne $2) {
1625 # type doesn't match
1626 return undef;
1627 }
1628 return $3;
1629 }
1630
1631 # get path of entry with given hash at given tree-ish (ref)
1632 # used to get 'from' filename for combined diff (merge commit) for renames
1633 sub git_get_path_by_hash {
1634 my $base = shift || return;
1635 my $hash = shift || return;
1636
1637 local $/ = "\0";
1638
1639 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
1640 or return undef;
1641 while (my $line = <$fd>) {
1642 chomp $line;
1643
1644 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
1645 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
1646 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
1647 close $fd;
1648 return $1;
1649 }
1650 }
1651 close $fd;
1652 return undef;
1653 }
1654
1655 ## ......................................................................
1656 ## git utility functions, directly accessing git repository
1657
1658 sub git_get_project_description {
1659 my $path = shift;
1660
1661 $git_dir = "$projectroot/$path";
1662 open my $fd, "$git_dir/description"
1663 or return git_get_project_config('description');
1664 my $descr = <$fd>;
1665 close $fd;
1666 if (defined $descr) {
1667 chomp $descr;
1668 }
1669 return $descr;
1670 }
1671
1672 sub git_get_project_url_list {
1673 my $path = shift;
1674
1675 $git_dir = "$projectroot/$path";
1676 open my $fd, "$git_dir/cloneurl"
1677 or return wantarray ?
1678 @{ config_to_multi(git_get_project_config('url')) } :
1679 config_to_multi(git_get_project_config('url'));
1680 my @git_project_url_list = map { chomp; $_ } <$fd>;
1681 close $fd;
1682
1683 return wantarray ? @git_project_url_list : \@git_project_url_list;
1684 }
1685
1686 sub git_get_projects_list {
1687 my ($filter) = @_;
1688 my @list;
1689
1690 $filter ||= '';
1691 $filter =~ s/\.git$//;
1692
1693 my ($check_forks) = gitweb_check_feature('forks');
1694
1695 if (-d $projects_list) {
1696 # search in directory
1697 my $dir = $projects_list . ($filter ? "/$filter" : '');
1698 # remove the trailing "/"
1699 $dir =~ s!/+$!!;
1700 my $pfxlen = length("$dir");
1701 my $pfxdepth = ($dir =~ tr!/!!);
1702
1703 File::Find::find({
1704 follow_fast => 1, # follow symbolic links
1705 follow_skip => 2, # ignore duplicates
1706 dangling_symlinks => 0, # ignore dangling symlinks, silently
1707 wanted => sub {
1708 # skip project-list toplevel, if we get it.
1709 return if (m!^[/.]$!);
1710 # only directories can be git repositories
1711 return unless (-d $_);
1712 # don't traverse too deep (Find is super slow on os x)
1713 if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
1714 $File::Find::prune = 1;
1715 return;
1716 }
1717
1718 my $subdir = substr($File::Find::name, $pfxlen + 1);
1719 # we check related file in $projectroot
1720 if ($check_forks and $subdir =~ m#/.#) {
1721 $File::Find::prune = 1;
1722 } elsif (check_export_ok("$projectroot/$filter/$subdir")) {
1723 push @list, { path => ($filter ? "$filter/" : '') . $subdir };
1724 $File::Find::prune = 1;
1725 }
1726 },
1727 }, "$dir");
1728
1729 } elsif (-f $projects_list) {
1730 # read from file(url-encoded):
1731 # 'git%2Fgit.git Linus+Torvalds'
1732 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1733 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1734 my %paths;
1735 open my ($fd), $projects_list or return;
1736 PROJECT:
1737 while (my $line = <$fd>) {
1738 chomp $line;
1739 my ($path, $owner) = split ' ', $line;
1740 $path = unescape($path);
1741 $owner = unescape($owner);
1742 if (!defined $path) {
1743 next;
1744 }
1745 if ($filter ne '') {
1746 # looking for forks;
1747 my $pfx = substr($path, 0, length($filter));
1748 if ($pfx ne $filter) {
1749 next PROJECT;
1750 }
1751 my $sfx = substr($path, length($filter));
1752 if ($sfx !~ /^\/.*\.git$/) {
1753 next PROJECT;
1754 }
1755 } elsif ($check_forks) {
1756 PATH:
1757 foreach my $filter (keys %paths) {
1758 # looking for forks;
1759 my $pfx = substr($path, 0, length($filter));
1760 if ($pfx ne $filter) {
1761 next PATH;
1762 }
1763 my $sfx = substr($path, length($filter));
1764 if ($sfx !~ /^\/.*\.git$/) {
1765 next PATH;
1766 }
1767 # is a fork, don't include it in
1768 # the list
1769 next PROJECT;
1770 }
1771 }
1772 if (check_export_ok("$projectroot/$path")) {
1773 my $pr = {
1774 path => $path,
1775 owner => to_utf8($owner),
1776 };
1777 push @list, $pr;
1778 (my $forks_path = $path) =~ s/\.git$//;
1779 $paths{$forks_path}++;
1780 }
1781 }
1782 close $fd;
1783 }
1784 return @list;
1785 }
1786
1787 our $gitweb_project_owner = undef;
1788 sub git_get_project_list_from_file {
1789
1790 return if (defined $gitweb_project_owner);
1791
1792 $gitweb_project_owner = {};
1793 # read from file (url-encoded):
1794 # 'git%2Fgit.git Linus+Torvalds'
1795 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1796 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1797 if (-f $projects_list) {
1798 open (my $fd , $projects_list);
1799 while (my $line = <$fd>) {
1800 chomp $line;
1801 my ($pr, $ow) = split ' ', $line;
1802 $pr = unescape($pr);
1803 $ow = unescape($ow);
1804 $gitweb_project_owner->{$pr} = to_utf8($ow);
1805 }
1806 close $fd;
1807 }
1808 }
1809
1810 sub git_get_project_owner {
1811 my $project = shift;
1812 my $owner;
1813
1814 return undef unless $project;
1815 $git_dir = "$projectroot/$project";
1816
1817 if (!defined $gitweb_project_owner) {
1818 git_get_project_list_from_file();
1819 }
1820
1821 if (exists $gitweb_project_owner->{$project}) {
1822 $owner = $gitweb_project_owner->{$project};
1823 }
1824 if (!defined $owner){
1825 $owner = git_get_project_config('owner');
1826 }
1827 if (!defined $owner) {
1828 $owner = get_file_owner("$git_dir");
1829 }
1830
1831 return $owner;
1832 }
1833
1834 sub git_get_last_activity {
1835 my ($path) = @_;
1836 my $fd;
1837
1838 $git_dir = "$projectroot/$path";
1839 open($fd, "-|", git_cmd(), 'for-each-ref',
1840 '--format=%(committer)',
1841 '--sort=-committerdate',
1842 '--count=1',
1843 'refs/heads') or return;
1844 my $most_recent = <$fd>;
1845 close $fd or return;
1846 if (defined $most_recent &&
1847 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
1848 my $timestamp = $1;
1849 my $age = time - $timestamp;
1850 return ($age, age_string($age));
1851 }
1852 return (undef, undef);
1853 }
1854
1855 sub git_get_references {
1856 my $type = shift || "";
1857 my %refs;
1858 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
1859 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
1860 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
1861 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
1862 or return;
1863
1864 while (my $line = <$fd>) {
1865 chomp $line;
1866 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type/?[^^]+)!) {
1867 if (defined $refs{$1}) {
1868 push @{$refs{$1}}, $2;
1869 } else {
1870 $refs{$1} = [ $2 ];
1871 }
1872 }
1873 }
1874 close $fd or return;
1875 return \%refs;
1876 }
1877
1878 sub git_get_rev_name_tags {
1879 my $hash = shift || return undef;
1880
1881 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
1882 or return;
1883 my $name_rev = <$fd>;
1884 close $fd;
1885
1886 if ($name_rev =~ m|^$hash tags/(.*)$|) {
1887 return $1;
1888 } else {
1889 # catches also '$hash undefined' output
1890 return undef;
1891 }
1892 }
1893
1894 ## ----------------------------------------------------------------------
1895 ## parse to hash functions
1896
1897 sub parse_date {
1898 my $epoch = shift;
1899 my $tz = shift || "-0000";
1900
1901 my %date;
1902 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
1903 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
1904 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
1905 $date{'hour'} = $hour;
1906 $date{'minute'} = $min;
1907 $date{'mday'} = $mday;
1908 $date{'day'} = $days[$wday];
1909 $date{'month'} = $months[$mon];
1910 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
1911 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
1912 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
1913 $mday, $months[$mon], $hour ,$min;
1914 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
1915 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
1916
1917 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
1918 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
1919 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
1920 $date{'hour_local'} = $hour;
1921 $date{'minute_local'} = $min;
1922 $date{'tz_local'} = $tz;
1923 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
1924 1900+$year, $mon+1, $mday,
1925 $hour, $min, $sec, $tz);
1926 return %date;
1927 }
1928
1929 sub parse_tag {
1930 my $tag_id = shift;
1931 my %tag;
1932 my @comment;
1933
1934 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
1935 $tag{'id'} = $tag_id;
1936 while (my $line = <$fd>) {
1937 chomp $line;
1938 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
1939 $tag{'object'} = $1;
1940 } elsif ($line =~ m/^type (.+)$/) {
1941 $tag{'type'} = $1;
1942 } elsif ($line =~ m/^tag (.+)$/) {
1943 $tag{'name'} = $1;
1944 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
1945 $tag{'author'} = $1;
1946 $tag{'epoch'} = $2;
1947 $tag{'tz'} = $3;
1948 } elsif ($line =~ m/--BEGIN/) {
1949 push @comment, $line;
1950 last;
1951 } elsif ($line eq "") {
1952 last;
1953 }
1954 }
1955 push @comment, <$fd>;
1956 $tag{'comment'} = \@comment;
1957 close $fd or return;
1958 if (!defined $tag{'name'}) {
1959 return
1960 };
1961 return %tag
1962 }
1963
1964 sub parse_commit_text {
1965 my ($commit_text, $withparents) = @_;
1966 my @commit_lines = split '\n', $commit_text;
1967 my %co;
1968
1969 pop @commit_lines; # Remove '\0'
1970
1971 if (! @commit_lines) {
1972 return;
1973 }
1974
1975 my $header = shift @commit_lines;
1976 if ($header !~ m/^[0-9a-fA-F]{40}/) {
1977 return;
1978 }
1979 ($co{'id'}, my @parents) = split ' ', $header;
1980 while (my $line = shift @commit_lines) {
1981 last if $line eq "\n";
1982 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
1983 $co{'tree'} = $1;
1984 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
1985 push @parents, $1;
1986 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
1987 $co{'author'} = $1;
1988 $co{'author_epoch'} = $2;
1989 $co{'author_tz'} = $3;
1990 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
1991 $co{'author_name'} = $1;
1992 $co{'author_email'} = $2;
1993 } else {
1994 $co{'author_name'} = $co{'author'};
1995 }
1996 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
1997 $co{'committer'} = $1;
1998 $co{'committer_epoch'} = $2;
1999 $co{'committer_tz'} = $3;
2000 $co{'committer_name'} = $co{'committer'};
2001 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
2002 $co{'committer_name'} = $1;
2003 $co{'committer_email'} = $2;
2004 } else {
2005 $co{'committer_name'} = $co{'committer'};
2006 }
2007 }
2008 }
2009 if (!defined $co{'tree'}) {
2010 return;
2011 };
2012 $co{'parents'} = \@parents;
2013 $co{'parent'} = $parents[0];
2014
2015 foreach my $title (@commit_lines) {
2016 $title =~ s/^ //;
2017 if ($title ne "") {
2018 $co{'title'} = chop_str($title, 80, 5);
2019 # remove leading stuff of merges to make the interesting part visible
2020 if (length($title) > 50) {
2021 $title =~ s/^Automatic //;
2022 $title =~ s/^merge (of|with) /Merge ... /i;
2023 if (length($title) > 50) {
2024 $title =~ s/(http|rsync):\/\///;
2025 }
2026 if (length($title) > 50) {
2027 $title =~ s/(master|www|rsync)\.//;
2028 }
2029 if (length($title) > 50) {
2030 $title =~ s/kernel.org:?//;
2031 }
2032 if (length($title) > 50) {
2033 $title =~ s/\/pub\/scm//;
2034 }
2035 }
2036 $co{'title_short'} = chop_str($title, 50, 5);
2037 last;
2038 }
2039 }
2040 if ($co{'title'} eq "") {
2041 $co{'title'} = $co{'title_short'} = '(no commit message)';
2042 }
2043 # remove added spaces
2044 foreach my $line (@commit_lines) {
2045 $line =~ s/^ //;
2046 }
2047 $co{'comment'} = \@commit_lines;
2048
2049 my $age = time - $co{'committer_epoch'};
2050 $co{'age'} = $age;
2051 $co{'age_string'} = age_string($age);
2052 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
2053 if ($age > 60*60*24*7*2) {
2054 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2055 $co{'age_string_age'} = $co{'age_string'};
2056 } else {
2057 $co{'age_string_date'} = $co{'age_string'};
2058 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2059 }
2060 return %co;
2061 }
2062
2063 sub parse_commit {
2064 my ($commit_id) = @_;
2065 my %co;
2066
2067 local $/ = "\0";
2068
2069 open my $fd, "-|", git_cmd(), "rev-list",
2070 "--parents",
2071 "--header",
2072 "--max-count=1",
2073 $commit_id,
2074 "--",
2075 or die_error(undef, "Open git-rev-list failed");
2076 %co = parse_commit_text(<$fd>, 1);
2077 close $fd;
2078
2079 return %co;
2080 }
2081
2082 sub parse_commits {
2083 my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
2084 my @cos;
2085
2086 $maxcount ||= 1;
2087 $skip ||= 0;
2088
2089 local $/ = "\0";
2090
2091 open my $fd, "-|", git_cmd(), "rev-list",
2092 "--header",
2093 @args,
2094 ("--max-count=" . $maxcount),
2095 ("--skip=" . $skip),
2096 @extra_options,
2097 $commit_id,
2098 "--",
2099 ($filename ? ($filename) : ())
2100 or die_error(undef, "Open git-rev-list failed");
2101 while (my $line = <$fd>) {
2102 my %co = parse_commit_text($line);
2103 push @cos, \%co;
2104 }
2105 close $fd;
2106
2107 return wantarray ? @cos : \@cos;
2108 }
2109
2110 # parse ref from ref_file, given by ref_id, with given type
2111 sub parse_ref {
2112 my $ref_file = shift;
2113 my $ref_id = shift;
2114 my $type = shift || git_get_type($ref_id);
2115 my %ref_item;
2116
2117 $ref_item{'type'} = $type;
2118 $ref_item{'id'} = $ref_id;
2119 $ref_item{'epoch'} = 0;
2120 $ref_item{'age'} = "unknown";
2121 if ($type eq "tag") {
2122 my %tag = parse_tag($ref_id);
2123 $ref_item{'comment'} = $tag{'comment'};
2124 if ($tag{'type'} eq "commit") {
2125 my %co = parse_commit($tag{'object'});
2126 $ref_item{'epoch'} = $co{'committer_epoch'};
2127 $ref_item{'age'} = $co{'age_string'};
2128 } elsif (defined($tag{'epoch'})) {
2129 my $age = time - $tag{'epoch'};
2130 $ref_item{'epoch'} = $tag{'epoch'};
2131 $ref_item{'age'} = age_string($age);
2132 }
2133 $ref_item{'reftype'} = $tag{'type'};
2134 $ref_item{'name'} = $tag{'name'};
2135 $ref_item{'refid'} = $tag{'object'};
2136 } elsif ($type eq "commit"){
2137 my %co = parse_commit($ref_id);
2138 $ref_item{'reftype'} = "commit";
2139 $ref_item{'name'} = $ref_file;
2140 $ref_item{'title'} = $co{'title'};
2141 $ref_item{'refid'} = $ref_id;
2142 $ref_item{'epoch'} = $co{'committer_epoch'};
2143 $ref_item{'age'} = $co{'age_string'};
2144 } else {
2145 $ref_item{'reftype'} = $type;
2146 $ref_item{'name'} = $ref_file;
2147 $ref_item{'refid'} = $ref_id;
2148 }
2149
2150 return %ref_item;
2151 }
2152
2153 # parse line of git-diff-tree "raw" output
2154 sub parse_difftree_raw_line {
2155 my $line = shift;
2156 my %res;
2157
2158 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
2159 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
2160 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
2161 $res{'from_mode'} = $1;
2162 $res{'to_mode'} = $2;
2163 $res{'from_id'} = $3;
2164 $res{'to_id'} = $4;
2165 $res{'status'} = $res{'status_str'} = $5;
2166 $res{'similarity'} = $6;
2167 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
2168 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
2169 } else {
2170 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote($7);
2171 }
2172 }
2173 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
2174 # combined diff (for merge commit)
2175 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
2176 $res{'nparents'} = length($1);
2177 $res{'from_mode'} = [ split(' ', $2) ];
2178 $res{'to_mode'} = pop @{$res{'from_mode'}};
2179 $res{'from_id'} = [ split(' ', $3) ];
2180 $res{'to_id'} = pop @{$res{'from_id'}};
2181 $res{'status_str'} = $4;
2182 $res{'status'} = [ split('', $4) ];
2183 $res{'to_file'} = unquote($5);
2184 }
2185 # 'c512b523472485aef4fff9e57b229d9d243c967f'
2186 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
2187 $res{'commit'} = $1;
2188 }
2189
2190 return wantarray ? %res : \%res;
2191 }
2192
2193 # wrapper: return parsed line of git-diff-tree "raw" output
2194 # (the argument might be raw line, or parsed info)
2195 sub parsed_difftree_line {
2196 my $line_or_ref = shift;
2197
2198 if (ref($line_or_ref) eq "HASH") {
2199 # pre-parsed (or generated by hand)
2200 return $line_or_ref;
2201 } else {
2202 return parse_difftree_raw_line($line_or_ref);
2203 }
2204 }
2205
2206 # parse line of git-ls-tree output
2207 sub parse_ls_tree_line ($;%) {
2208 my $line = shift;
2209 my %opts = @_;
2210 my %res;
2211
2212 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2213 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
2214
2215 $res{'mode'} = $1;
2216 $res{'type'} = $2;
2217 $res{'hash'} = $3;
2218 if ($opts{'-z'}) {
2219 $res{'name'} = $4;
2220 } else {
2221 $res{'name'} = unquote($4);
2222 }
2223
2224 return wantarray ? %res : \%res;
2225 }
2226
2227 # generates _two_ hashes, references to which are passed as 2 and 3 argument
2228 sub parse_from_to_diffinfo {
2229 my ($diffinfo, $from, $to, @parents) = @_;
2230
2231 if ($diffinfo->{'nparents'}) {
2232 # combined diff
2233 $from->{'file'} = [];
2234 $from->{'href'} = [];
2235 fill_from_file_info($diffinfo, @parents)
2236 unless exists $diffinfo->{'from_file'};
2237 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2238 $from->{'file'}[$i] =
2239 defined $diffinfo->{'from_file'}[$i] ?
2240 $diffinfo->{'from_file'}[$i] :
2241 $diffinfo->{'to_file'};
2242 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
2243 $from->{'href'}[$i] = href(action=>"blob",
2244 hash_base=>$parents[$i],
2245 hash=>$diffinfo->{'from_id'}[$i],
2246 file_name=>$from->{'file'}[$i]);
2247 } else {
2248 $from->{'href'}[$i] = undef;
2249 }
2250 }
2251 } else {
2252 # ordinary (not combined) diff
2253 $from->{'file'} = $diffinfo->{'from_file'};
2254 if ($diffinfo->{'status'} ne "A") { # not new (added) file
2255 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2256 hash=>$diffinfo->{'from_id'},
2257 file_name=>$from->{'file'});
2258 } else {
2259 delete $from->{'href'};
2260 }
2261 }
2262
2263 $to->{'file'} = $diffinfo->{'to_file'};
2264 if (!is_deleted($diffinfo)) { # file exists in result
2265 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
2266 hash=>$diffinfo->{'to_id'},
2267 file_name=>$to->{'file'});
2268 } else {
2269 delete $to->{'href'};
2270 }
2271 }
2272
2273 ## ......................................................................
2274 ## parse to array of hashes functions
2275
2276 sub git_get_heads_list {
2277 my $limit = shift;
2278 my @headslist;
2279
2280 open my $fd, '-|', git_cmd(), 'for-each-ref',
2281 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
2282 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
2283 'refs/heads'
2284 or return;
2285 while (my $line = <$fd>) {
2286 my %ref_item;
2287
2288 chomp $line;
2289 my ($refinfo, $committerinfo) = split(/\0/, $line);
2290 my ($hash, $name, $title) = split(' ', $refinfo, 3);
2291 my ($committer, $epoch, $tz) =
2292 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
2293 $ref_item{'fullname'} = $name;
2294 $name =~ s!^refs/heads/!!;
2295
2296 $ref_item{'name'} = $name;
2297 $ref_item{'id'} = $hash;
2298 $ref_item{'title'} = $title || '(no commit message)';
2299 $ref_item{'epoch'} = $epoch;
2300 if ($epoch) {
2301 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2302 } else {
2303 $ref_item{'age'} = "unknown";
2304 }
2305
2306 push @headslist, \%ref_item;
2307 }
2308 close $fd;
2309
2310 return wantarray ? @headslist : \@headslist;
2311 }
2312
2313 sub git_get_tags_list {
2314 my $limit = shift;
2315 my @tagslist;
2316
2317 open my $fd, '-|', git_cmd(), 'for-each-ref',
2318 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
2319 '--format=%(objectname) %(objecttype) %(refname) '.
2320 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
2321 'refs/tags'
2322 or return;
2323 while (my $line = <$fd>) {
2324 my %ref_item;
2325
2326 chomp $line;
2327 my ($refinfo, $creatorinfo) = split(/\0/, $line);
2328 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
2329 my ($creator, $epoch, $tz) =
2330 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
2331 $ref_item{'fullname'} = $name;
2332 $name =~ s!^refs/tags/!!;
2333
2334 $ref_item{'type'} = $type;
2335 $ref_item{'id'} = $id;
2336 $ref_item{'name'} = $name;
2337 if ($type eq "tag") {
2338 $ref_item{'subject'} = $title;
2339 $ref_item{'reftype'} = $reftype;
2340 $ref_item{'refid'} = $refid;
2341 } else {
2342 $ref_item{'reftype'} = $type;
2343 $ref_item{'refid'} = $id;
2344 }
2345
2346 if ($type eq "tag" || $type eq "commit") {
2347 $ref_item{'epoch'} = $epoch;
2348 if ($epoch) {
2349 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2350 } else {
2351 $ref_item{'age'} = "unknown";
2352 }
2353 }
2354
2355 push @tagslist, \%ref_item;
2356 }
2357 close $fd;
2358
2359 return wantarray ? @tagslist : \@tagslist;
2360 }
2361
2362 ## ----------------------------------------------------------------------
2363 ## filesystem-related functions
2364
2365 sub get_file_owner {
2366 my $path = shift;
2367
2368 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
2369 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
2370 if (!defined $gcos) {
2371 return undef;
2372 }
2373 my $owner = $gcos;
2374 $owner =~ s/[,;].*$//;
2375 return to_utf8($owner);
2376 }
2377
2378 ## ......................................................................
2379 ## mimetype related functions
2380
2381 sub mimetype_guess_file {
2382 my $filename = shift;
2383 my $mimemap = shift;
2384 -r $mimemap or return undef;
2385
2386 my %mimemap;
2387 open(MIME, $mimemap) or return undef;
2388 while (<MIME>) {
2389 next if m/^#/; # skip comments
2390 my ($mime, $exts) = split(/\t+/);
2391 if (defined $exts) {
2392 my @exts = split(/\s+/, $exts);
2393 foreach my $ext (@exts) {
2394 $mimemap{$ext} = $mime;
2395 }
2396 }
2397 }
2398 close(MIME);
2399
2400 $filename =~ /\.([^.]*)$/;
2401 return $mimemap{$1};
2402 }
2403
2404 sub mimetype_guess {
2405 my $filename = shift;
2406 my $mime;
2407 $filename =~ /\./ or return undef;
2408
2409 if ($mimetypes_file) {
2410 my $file = $mimetypes_file;
2411 if ($file !~ m!^/!) { # if it is relative path
2412 # it is relative to project
2413 $file = "$projectroot/$project/$file";
2414 }
2415 $mime = mimetype_guess_file($filename, $file);
2416 }
2417 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
2418 return $mime;
2419 }
2420
2421 sub blob_mimetype {
2422 my $fd = shift;
2423 my $filename = shift;
2424
2425 if ($filename) {
2426 my $mime = mimetype_guess($filename);
2427 $mime and return $mime;
2428 }
2429
2430 # just in case
2431 return $default_blob_plain_mimetype unless $fd;
2432
2433 if (-T $fd) {
2434 return 'text/plain' .
2435 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
2436 } elsif (! $filename) {
2437 return 'application/octet-stream';
2438 } elsif ($filename =~ m/\.png$/i) {
2439 return 'image/png';
2440 } elsif ($filename =~ m/\.gif$/i) {
2441 return 'image/gif';
2442 } elsif ($filename =~ m/\.jpe?g$/i) {
2443 return 'image/jpeg';
2444 } else {
2445 return 'application/octet-stream';
2446 }
2447 }
2448
2449 ## ======================================================================
2450 ## functions printing HTML: header, footer, error page
2451
2452 sub git_header_html {
2453 my $status = shift || "200 OK";
2454 my $expires = shift;
2455
2456 my $title = "$site_name";
2457 if (defined $project) {
2458 $title .= " - " . to_utf8($project);
2459 if (defined $action) {
2460 $title .= "/$action";
2461 if (defined $file_name) {
2462 $title .= " - " . esc_path($file_name);
2463 if ($action eq "tree" && $file_name !~ m|/$|) {
2464 $title .= "/";
2465 }
2466 }
2467 }
2468 }
2469 my $content_type;
2470 # require explicit support from the UA if we are to send the page as
2471 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
2472 # we have to do this because MSIE sometimes globs '*/*', pretending to
2473 # support xhtml+xml but choking when it gets what it asked for.
2474 if (defined $cgi->http('HTTP_ACCEPT') &&
2475 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
2476 $cgi->Accept('application/xhtml+xml') != 0) {
2477 $content_type = 'application/xhtml+xml';
2478 } else {
2479 $content_type = 'text/html';
2480 }
2481 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
2482 -status=> $status, -expires => $expires);
2483 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
2484 print <<EOF;
2485 <?xml version="1.0" encoding="utf-8"?>
2486 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2487 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
2488 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
2489 <!-- git core binaries version $git_version -->
2490 <head>
2491 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
2492 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
2493 <meta name="robots" content="index, nofollow"/>
2494 <title>$title</title>
2495 EOF
2496 # print out each stylesheet that exist
2497 if (defined $stylesheet) {
2498 #provides backwards capability for those people who define style sheet in a config file
2499 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2500 } else {
2501 foreach my $stylesheet (@stylesheets) {
2502 next unless $stylesheet;
2503 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2504 }
2505 }
2506 if (defined $project) {
2507 printf('<link rel="alternate" title="%s log RSS feed" '.
2508 'href="%s" type="application/rss+xml" />'."\n",
2509 esc_param($project), href(action=>"rss"));
2510 printf('<link rel="alternate" title="%s log RSS feed (no merges)" '.
2511 'href="%s" type="application/rss+xml" />'."\n",
2512 esc_param($project), href(action=>"rss",
2513 extra_options=>"--no-merges"));
2514 printf('<link rel="alternate" title="%s log Atom feed" '.
2515 'href="%s" type="application/atom+xml" />'."\n",
2516 esc_param($project), href(action=>"atom"));
2517 printf('<link rel="alternate" title="%s log Atom feed (no merges)" '.
2518 'href="%s" type="application/atom+xml" />'."\n",
2519 esc_param($project), href(action=>"atom",
2520 extra_options=>"--no-merges"));
2521 } else {
2522 printf('<link rel="alternate" title="%s projects list" '.
2523 'href="%s" type="text/plain; charset=utf-8"/>'."\n",
2524 $site_name, href(project=>undef, action=>"project_index"));
2525 printf('<link rel="alternate" title="%s projects feeds" '.
2526 'href="%s" type="text/x-opml"/>'."\n",
2527 $site_name, href(project=>undef, action=>"opml"));
2528 }
2529 if (defined $favicon) {
2530 print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n);
2531 }
2532
2533 print "</head>\n" .
2534 "<body>\n";
2535
2536 if (-f $site_header) {
2537 open (my $fd, $site_header);
2538 print <$fd>;
2539 close $fd;
2540 }
2541
2542 print "<div class=\"page_header\">\n" .
2543 $cgi->a({-href => esc_url($logo_url),
2544 -title => $logo_label},
2545 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
2546 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
2547 if (defined $project) {
2548 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
2549 if (defined $action) {
2550 print " / $action";
2551 }
2552 print "\n";
2553 }
2554 print "</div>\n";
2555
2556 my ($have_search) = gitweb_check_feature('search');
2557 if ((defined $project) && ($have_search)) {
2558 if (!defined $searchtext) {
2559 $searchtext = "";
2560 }
2561 my $search_hash;
2562 if (defined $hash_base) {
2563 $search_hash = $hash_base;
2564 } elsif (defined $hash) {
2565 $search_hash = $hash;
2566 } else {
2567 $search_hash = "HEAD";
2568 }
2569 my $action = $my_uri;
2570 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
2571 if ($use_pathinfo) {
2572 $action .= "/$project";
2573 } else {
2574 $cgi->param("p", $project);
2575 }
2576 $cgi->param("a", "search");
2577 $cgi->param("h", $search_hash);
2578 print $cgi->startform(-method => "get", -action => $action) .
2579 "<div class=\"search\">\n" .
2580 (!$use_pathinfo && $cgi->hidden(-name => "p") . "\n") .
2581 $cgi->hidden(-name => "a") . "\n" .
2582 $cgi->hidden(-name => "h") . "\n" .
2583 $cgi->popup_menu(-name => 'st', -default => 'commit',
2584 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
2585 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
2586 " search:\n",
2587 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
2588 "</div>" .
2589 $cgi->end_form() . "\n";
2590 }
2591 }
2592
2593 sub git_footer_html {
2594 print "<div class=\"page_footer\">\n";
2595 if (defined $project) {
2596 my $descr = git_get_project_description($project);
2597 if (defined $descr) {
2598 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
2599 }
2600 print $cgi->a({-href => href(action=>"rss"),
2601 -class => "rss_logo"}, "RSS") . " ";
2602 print $cgi->a({-href => href(action=>"atom"),
2603 -class => "rss_logo"}, "Atom") . "\n";
2604 } else {
2605 print $cgi->a({-href => href(project=>undef, action=>"opml"),
2606 -class => "rss_logo"}, "OPML") . " ";
2607 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
2608 -class => "rss_logo"}, "TXT") . "\n";
2609 }
2610 print "</div>\n" ;
2611
2612 if (-f $site_footer) {
2613 open (my $fd, $site_footer);
2614 print <$fd>;
2615 close $fd;
2616 }
2617
2618 print "</body>\n" .
2619 "</html>";
2620 }
2621
2622 sub die_error {
2623 my $status = shift || "403 Forbidden";
2624 my $error = shift || "Malformed query, file missing or permission denied";
2625
2626 git_header_html($status);
2627 print <<EOF;
2628 <div class="page_body">
2629 <br /><br />
2630 $status - $error
2631 <br />
2632 </div>
2633 EOF
2634 git_footer_html();
2635 exit;
2636 }
2637
2638 ## ----------------------------------------------------------------------
2639 ## functions printing or outputting HTML: navigation
2640
2641 sub git_print_page_nav {
2642 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
2643 $extra = '' if !defined $extra; # pager or formats
2644
2645 my @navs = qw(summary shortlog log commit commitdiff tree);
2646 if ($suppress) {
2647 @navs = grep { $_ ne $suppress } @navs;
2648 }
2649
2650 my %arg = map { $_ => {action=>$_} } @navs;
2651 if (defined $head) {
2652 for (qw(commit commitdiff)) {
2653 $arg{$_}{'hash'} = $head;
2654 }
2655 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
2656 for (qw(shortlog log)) {
2657 $arg{$_}{'hash'} = $head;
2658 }
2659 }
2660 }
2661 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
2662 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
2663
2664 print "<div class=\"page_nav\">\n" .
2665 (join " | ",
2666 map { $_ eq $current ?
2667 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
2668 } @navs);
2669 print "<br/>\n$extra<br/>\n" .
2670 "</div>\n";
2671 }
2672
2673 sub format_paging_nav {
2674 my ($action, $hash, $head, $page, $nrevs) = @_;
2675 my $paging_nav;
2676
2677
2678 if ($hash ne $head || $page) {
2679 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
2680 } else {
2681 $paging_nav .= "HEAD";
2682 }
2683
2684 if ($page > 0) {
2685 $paging_nav .= " &sdot; " .
2686 $cgi->a({-href => href(-replay=>1, page=>$page-1),
2687 -accesskey => "p", -title => "Alt-p"}, "prev");
2688 } else {
2689 $paging_nav .= " &sdot; prev";
2690 }
2691
2692 if ($nrevs >= (100 * ($page+1)-1)) {
2693 $paging_nav .= " &sdot; " .
2694 $cgi->a({-href => href(-replay=>1, page=>$page+1),
2695 -accesskey => "n", -title => "Alt-n"}, "next");
2696 } else {
2697 $paging_nav .= " &sdot; next";
2698 }
2699
2700 return $paging_nav;
2701 }
2702
2703 ## ......................................................................
2704 ## functions printing or outputting HTML: div
2705
2706 sub git_print_header_div {
2707 my ($action, $title, $hash, $hash_base) = @_;
2708 my %args = ();
2709
2710 $args{'action'} = $action;
2711 $args{'hash'} = $hash if $hash;
2712 $args{'hash_base'} = $hash_base if $hash_base;
2713
2714 print "<div class=\"header\">\n" .
2715 $cgi->a({-href => href(%args), -class => "title"},
2716 $title ? $title : $action) .
2717 "\n</div>\n";
2718 }
2719
2720 #sub git_print_authorship (\%) {
2721 sub git_print_authorship {
2722 my $co = shift;
2723
2724 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
2725 print "<div class=\"author_date\">" .
2726 esc_html($co->{'author_name'}) .
2727 " [$ad{'rfc2822'}";
2728 if ($ad{'hour_local'} < 6) {
2729 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2730 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2731 } else {
2732 printf(" (%02d:%02d %s)",
2733 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2734 }
2735 print "]</div>\n";
2736 }
2737
2738 sub git_print_page_path {
2739 my $name = shift;
2740 my $type = shift;
2741 my $hb = shift;
2742
2743
2744 print "<div class=\"page_path\">";
2745 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
2746 -title => 'tree root'}, to_utf8("[$project]"));
2747 print " / ";
2748 if (defined $name) {
2749 my @dirname = split '/', $name;
2750 my $basename = pop @dirname;
2751 my $fullname = '';
2752
2753 foreach my $dir (@dirname) {
2754 $fullname .= ($fullname ? '/' : '') . $dir;
2755 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
2756 hash_base=>$hb),
2757 -title => $fullname}, esc_path($dir));
2758 print " / ";
2759 }
2760 if (defined $type && $type eq 'blob') {
2761 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
2762 hash_base=>$hb),
2763 -title => $name}, esc_path($basename));
2764 } elsif (defined $type && $type eq 'tree') {
2765 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
2766 hash_base=>$hb),
2767 -title => $name}, esc_path($basename));
2768 print " / ";
2769 } else {
2770 print esc_path($basename);
2771 }
2772 }
2773 print "<br/></div>\n";
2774 }
2775
2776 # sub git_print_log (\@;%) {
2777 sub git_print_log ($;%) {
2778 my $log = shift;
2779 my %opts = @_;
2780
2781 if ($opts{'-remove_title'}) {
2782 # remove title, i.e. first line of log
2783 shift @$log;
2784 }
2785 # remove leading empty lines
2786 while (defined $log->[0] && $log->[0] eq "") {
2787 shift @$log;
2788 }
2789
2790 # print log
2791 my $signoff = 0;
2792 my $empty = 0;
2793 foreach my $line (@$log) {
2794 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
2795 $signoff = 1;
2796 $empty = 0;
2797 if (! $opts{'-remove_signoff'}) {
2798 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
2799 next;
2800 } else {
2801 # remove signoff lines
2802 next;
2803 }
2804 } else {
2805 $signoff = 0;
2806 }
2807
2808 # print only one empty line
2809 # do not print empty line after signoff
2810 if ($line eq "") {
2811 next if ($empty || $signoff);
2812 $empty = 1;
2813 } else {
2814 $empty = 0;
2815 }
2816
2817 print format_log_line_html($line) . "<br/>\n";
2818 }
2819
2820 if ($opts{'-final_empty_line'}) {
2821 # end with single empty line
2822 print "<br/>\n" unless $empty;
2823 }
2824 }
2825
2826 # return link target (what link points to)
2827 sub git_get_link_target {
2828 my $hash = shift;
2829 my $link_target;
2830
2831 # read link
2832 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2833 or return;
2834 {
2835 local $/;
2836 $link_target = <$fd>;
2837 }
2838 close $fd
2839 or return;
2840
2841 return $link_target;
2842 }
2843
2844 # given link target, and the directory (basedir) the link is in,
2845 # return target of link relative to top directory (top tree);
2846 # return undef if it is not possible (including absolute links).
2847 sub normalize_link_target {
2848 my ($link_target, $basedir, $hash_base) = @_;
2849
2850 # we can normalize symlink target only if $hash_base is provided
2851 return unless $hash_base;
2852
2853 # absolute symlinks (beginning with '/') cannot be normalized
2854 return if (substr($link_target, 0, 1) eq '/');
2855
2856 # normalize link target to path from top (root) tree (dir)
2857 my $path;
2858 if ($basedir) {
2859 $path = $basedir . '/' . $link_target;
2860 } else {
2861 # we are in top (root) tree (dir)
2862 $path = $link_target;
2863 }
2864
2865 # remove //, /./, and /../
2866 my @path_parts;
2867 foreach my $part (split('/', $path)) {
2868 # discard '.' and ''
2869 next if (!$part || $part eq '.');
2870 # handle '..'
2871 if ($part eq '..') {
2872 if (@path_parts) {
2873 pop @path_parts;
2874 } else {
2875 # link leads outside repository (outside top dir)
2876 return;
2877 }
2878 } else {
2879 push @path_parts, $part;
2880 }
2881 }
2882 $path = join('/', @path_parts);
2883
2884 return $path;
2885 }
2886
2887 # print tree entry (row of git_tree), but without encompassing <tr> element
2888 sub git_print_tree_entry {
2889 my ($t, $basedir, $hash_base, $have_blame) = @_;
2890
2891 my %base_key = ();
2892 $base_key{'hash_base'} = $hash_base if defined $hash_base;
2893
2894 # The format of a table row is: mode list link. Where mode is
2895 # the mode of the entry, list is the name of the entry, an href,
2896 # and link is the action links of the entry.
2897
2898 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
2899 if ($t->{'type'} eq "blob") {
2900 print "<td class=\"list\">" .
2901 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
2902 file_name=>"$basedir$t->{'name'}", %base_key),
2903 -class => "list"}, esc_path($t->{'name'}));
2904 if (S_ISLNK(oct $t->{'mode'})) {
2905 my $link_target = git_get_link_target($t->{'hash'});
2906 if ($link_target) {
2907 my $norm_target = normalize_link_target($link_target, $basedir, $hash_base);
2908 if (defined $norm_target) {
2909 print " -> " .
2910 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
2911 file_name=>$norm_target),
2912 -title => $norm_target}, esc_path($link_target));
2913 } else {
2914 print " -> " . esc_path($link_target);
2915 }
2916 }
2917 }
2918 print "</td>\n";
2919 print "<td class=\"link\">";
2920 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
2921 file_name=>"$basedir$t->{'name'}", %base_key)},
2922 "blob");
2923 if ($have_blame) {
2924 print " | " .
2925 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
2926 file_name=>"$basedir$t->{'name'}", %base_key)},
2927 "blame");
2928 }
2929 if (defined $hash_base) {
2930 print " | " .
2931 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2932 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
2933 "history");
2934 }
2935 print " | " .
2936 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
2937 file_name=>"$basedir$t->{'name'}")},
2938 "raw");
2939 print "</td>\n";
2940
2941 } elsif ($t->{'type'} eq "tree") {
2942 print "<td class=\"list\">";
2943 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
2944 file_name=>"$basedir$t->{'name'}", %base_key)},
2945 esc_path($t->{'name'}));
2946 print "</td>\n";
2947 print "<td class=\"link\">";
2948 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
2949 file_name=>"$basedir$t->{'name'}", %base_key)},
2950 "tree");
2951 if (defined $hash_base) {
2952 print " | " .
2953 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2954 file_name=>"$basedir$t->{'name'}")},
2955 "history");
2956 }
2957 print "</td>\n";
2958 } else {
2959 # unknown object: we can only present history for it
2960 # (this includes 'commit' object, i.e. submodule support)
2961 print "<td class=\"list\">" .
2962 esc_path($t->{'name'}) .
2963 "</td>\n";
2964 print "<td class=\"link\">";
2965 if (defined $hash_base) {
2966 print $cgi->a({-href => href(action=>"history",
2967 hash_base=>$hash_base,
2968 file_name=>"$basedir$t->{'name'}")},
2969 "history");
2970 }
2971 print "</td>\n";
2972 }
2973 }
2974
2975 ## ......................................................................
2976 ## functions printing large fragments of HTML
2977
2978 # get pre-image filenames for merge (combined) diff
2979 sub fill_from_file_info {
2980 my ($diff, @parents) = @_;
2981
2982 $diff->{'from_file'} = [ ];
2983 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
2984 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
2985 if ($diff->{'status'}[$i] eq 'R' ||
2986 $diff->{'status'}[$i] eq 'C') {
2987 $diff->{'from_file'}[$i] =
2988 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
2989 }
2990 }
2991
2992 return $diff;
2993 }
2994
2995 # is current raw difftree line of file deletion
2996 sub is_deleted {
2997 my $diffinfo = shift;
2998
2999 return $diffinfo->{'status_str'} =~ /D/;
3000 }
3001
3002 # does patch correspond to [previous] difftree raw line
3003 # $diffinfo - hashref of parsed raw diff format
3004 # $patchinfo - hashref of parsed patch diff format
3005 # (the same keys as in $diffinfo)
3006 sub is_patch_split {
3007 my ($diffinfo, $patchinfo) = @_;
3008
3009 return defined $diffinfo && defined $patchinfo
3010 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
3011 }
3012
3013
3014 sub git_difftree_body {
3015 my ($difftree, $hash, @parents) = @_;
3016 my ($parent) = $parents[0];
3017 my ($have_blame) = gitweb_check_feature('blame');
3018 print "<div class=\"list_head\">\n";
3019 if ($#{$difftree} > 10) {
3020 print(($#{$difftree} + 1) . " files changed:\n");
3021 }
3022 print "</div>\n";
3023
3024 print "<table class=\"" .
3025 (@parents > 1 ? "combined " : "") .
3026 "diff_tree\">\n";
3027
3028 # header only for combined diff in 'commitdiff' view
3029 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
3030 if ($has_header) {
3031 # table header
3032 print "<thead><tr>\n" .
3033 "<th></th><th></th>\n"; # filename, patchN link
3034 for (my $i = 0; $i < @parents; $i++) {
3035 my $par = $parents[$i];
3036 print "<th>" .
3037 $cgi->a({-href => href(action=>"commitdiff",
3038 hash=>$hash, hash_parent=>$par),
3039 -title => 'commitdiff to parent number ' .
3040 ($i+1) . ': ' . substr($par,0,7)},
3041 $i+1) .
3042 "&nbsp;</th>\n";
3043 }
3044 print "</tr></thead>\n<tbody>\n";
3045 }
3046
3047 my $alternate = 1;
3048 my $patchno = 0;
3049 foreach my $line (@{$difftree}) {
3050 my $diff = parsed_difftree_line($line);
3051
3052 if ($alternate) {
3053 print "<tr class=\"dark\">\n";
3054 } else {
3055 print "<tr class=\"light\">\n";
3056 }
3057 $alternate ^= 1;
3058
3059 if (exists $diff->{'nparents'}) { # combined diff
3060
3061 fill_from_file_info($diff, @parents)
3062 unless exists $diff->{'from_file'};
3063
3064 if (!is_deleted($diff)) {
3065 # file exists in the result (child) commit
3066 print "<td>" .
3067 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3068 file_name=>$diff->{'to_file'},
3069 hash_base=>$hash),
3070 -class => "list"}, esc_path($diff->{'to_file'})) .
3071 "</td>\n";
3072 } else {
3073 print "<td>" .
3074 esc_path($diff->{'to_file'}) .
3075 "</td>\n";
3076 }
3077
3078 if ($action eq 'commitdiff') {
3079 # link to patch
3080 $patchno++;
3081 print "<td class=\"link\">" .
3082 $cgi->a({-href => "#patch$patchno"}, "patch") .
3083 " | " .
3084 "</td>\n";
3085 }
3086
3087 my $has_history = 0;
3088 my $not_deleted = 0;
3089 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
3090 my $hash_parent = $parents[$i];
3091 my $from_hash = $diff->{'from_id'}[$i];
3092 my $from_path = $diff->{'from_file'}[$i];
3093 my $status = $diff->{'status'}[$i];
3094
3095 $has_history ||= ($status ne 'A');
3096 $not_deleted ||= ($status ne 'D');
3097
3098 if ($status eq 'A') {
3099 print "<td class=\"link\" align=\"right\"> | </td>\n";
3100 } elsif ($status eq 'D') {
3101 print "<td class=\"link\">" .
3102 $cgi->a({-href => href(action=>"blob",
3103 hash_base=>$hash,
3104 hash=>$from_hash,
3105 file_name=>$from_path)},
3106 "blob" . ($i+1)) .
3107 " | </td>\n";
3108 } else {
3109 if ($diff->{'to_id'} eq $from_hash) {
3110 print "<td class=\"link nochange\">";
3111 } else {
3112 print "<td class=\"link\">";
3113 }
3114 print $cgi->a({-href => href(action=>"blobdiff",
3115 hash=>$diff->{'to_id'},
3116 hash_parent=>$from_hash,
3117 hash_base=>$hash,
3118 hash_parent_base=>$hash_parent,
3119 file_name=>$diff->{'to_file'},
3120 file_parent=>$from_path)},
3121 "diff" . ($i+1)) .
3122 " | </td>\n";
3123 }
3124 }
3125
3126 print "<td class=\"link\">";
3127 if ($not_deleted) {
3128 print $cgi->a({-href => href(action=>"blob",
3129 hash=>$diff->{'to_id'},
3130 file_name=>$diff->{'to_file'},
3131 hash_base=>$hash)},
3132 "blob");
3133 print " | " if ($has_history);
3134 }
3135 if ($has_history) {
3136 print $cgi->a({-href => href(action=>"history",
3137 file_name=>$diff->{'to_file'},
3138 hash_base=>$hash)},
3139 "history");
3140 }
3141 print "</td>\n";
3142
3143 print "</tr>\n";
3144 next; # instead of 'else' clause, to avoid extra indent
3145 }
3146 # else ordinary diff
3147
3148 my ($to_mode_oct, $to_mode_str, $to_file_type);
3149 my ($from_mode_oct, $from_mode_str, $from_file_type);
3150 if ($diff->{'to_mode'} ne ('0' x 6)) {
3151 $to_mode_oct = oct $diff->{'to_mode'};
3152 if (S_ISREG($to_mode_oct)) { # only for regular file
3153 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
3154 }
3155 $to_file_type = file_type($diff->{'to_mode'});
3156 }
3157 if ($diff->{'from_mode'} ne ('0' x 6)) {
3158 $from_mode_oct = oct $diff->{'from_mode'};
3159 if (S_ISREG($to_mode_oct)) { # only for regular file
3160 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
3161 }
3162 $from_file_type = file_type($diff->{'from_mode'});
3163 }
3164
3165 if ($diff->{'status'} eq "A") { # created
3166 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
3167 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
3168 $mode_chng .= "]</span>";
3169 print "<td>";
3170 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3171 hash_base=>$hash, file_name=>$diff->{'file'}),
3172 -class => "list"}, esc_path($diff->{'file'}));
3173 print "</td>\n";
3174 print "<td>$mode_chng</td>\n";
3175 print "<td class=\"link\">";
3176 if ($action eq 'commitdiff') {
3177 # link to patch
3178 $patchno++;
3179 print $cgi->a({-href => "#patch$patchno"}, "patch");
3180 print " | ";
3181 }
3182 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3183 hash_base=>$hash, file_name=>$diff->{'file'})},
3184 "blob");
3185 print "</td>\n";
3186
3187 } elsif ($diff->{'status'} eq "D") { # deleted
3188 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
3189 print "<td>";
3190 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3191 hash_base=>$parent, file_name=>$diff->{'file'}),
3192 -class => "list"}, esc_path($diff->{'file'}));
3193 print "</td>\n";
3194 print "<td>$mode_chng</td>\n";
3195 print "<td class=\"link\">";
3196 if ($action eq 'commitdiff') {
3197 # link to patch
3198 $patchno++;
3199 print $cgi->a({-href => "#patch$patchno"}, "patch");
3200 print " | ";
3201 }
3202 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3203 hash_base=>$parent, file_name=>$diff->{'file'})},
3204 "blob") . " | ";
3205 if ($have_blame) {
3206 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
3207 file_name=>$diff->{'file'})},
3208 "blame") . " | ";
3209 }
3210 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
3211 file_name=>$diff->{'file'})},
3212 "history");
3213 print "</td>\n";
3214
3215 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
3216 my $mode_chnge = "";
3217 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3218 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
3219 if ($from_file_type ne $to_file_type) {
3220 $mode_chnge .= " from $from_file_type to $to_file_type";
3221 }
3222 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
3223 if ($from_mode_str && $to_mode_str) {
3224 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
3225 } elsif ($to_mode_str) {
3226 $mode_chnge .= " mode: $to_mode_str";
3227 }
3228 }
3229 $mode_chnge .= "]</span>\n";
3230 }
3231 print "<td>";
3232 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3233 hash_base=>$hash, file_name=>$diff->{'file'}),
3234 -class => "list"}, esc_path($diff->{'file'}));
3235 print "</td>\n";
3236 print "<td>$mode_chnge</td>\n";
3237 print "<td class=\"link\">";
3238 if ($action eq 'commitdiff') {
3239 # link to patch
3240 $patchno++;
3241 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3242 " | ";
3243 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
3244 # "commit" view and modified file (not onlu mode changed)
3245 print $cgi->a({-href => href(action=>"blobdiff",
3246 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
3247 hash_base=>$hash, hash_parent_base=>$parent,
3248 file_name=>$diff->{'file'})},
3249 "diff") .
3250 " | ";
3251 }
3252 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3253 hash_base=>$hash, file_name=>$diff->{'file'})},
3254 "blob") . " | ";
3255 if ($have_blame) {
3256 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
3257 file_name=>$diff->{'file'})},
3258 "blame") . " | ";
3259 }
3260 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
3261 file_name=>$diff->{'file'})},
3262 "history");
3263 print "</td>\n";
3264
3265 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
3266 my %status_name = ('R' => 'moved', 'C' => 'copied');
3267 my $nstatus = $status_name{$diff->{'status'}};
3268 my $mode_chng = "";
3269 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3270 # mode also for directories, so we cannot use $to_mode_str
3271 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
3272 }
3273 print "<td>" .
3274 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
3275 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
3276 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
3277 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
3278 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
3279 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
3280 -class => "list"}, esc_path($diff->{'from_file'})) .
3281 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
3282 "<td class=\"link\">";
3283 if ($action eq 'commitdiff') {
3284 # link to patch
3285 $patchno++;
3286 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3287 " | ";
3288 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
3289 # "commit" view and modified file (not only pure rename or copy)
3290 print $cgi->a({-href => href(action=>"blobdiff",
3291 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
3292 hash_base=>$hash, hash_parent_base=>$parent,
3293 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
3294 "diff") .
3295 " | ";
3296 }
3297 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3298 hash_base=>$parent, file_name=>$diff->{'to_file'})},
3299 "blob") . " | ";
3300 if ($have_blame) {
3301 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
3302 file_name=>$diff->{'to_file'})},
3303 "blame") . " | ";
3304 }
3305 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
3306 file_name=>$diff->{'to_file'})},
3307 "history");
3308 print "</td>\n";
3309
3310 } # we should not encounter Unmerged (U) or Unknown (X) status
3311 print "</tr>\n";
3312 }
3313 print "</tbody>" if $has_header;
3314 print "</table>\n";
3315 }
3316
3317 sub git_patchset_body {
3318 my ($fd, $difftree, $hash, @hash_parents) = @_;
3319 my ($hash_parent) = $hash_parents[0];
3320
3321 my $is_combined = (@hash_parents > 1);
3322 my $patch_idx = 0;
3323 my $patch_number = 0;
3324 my $patch_line;
3325 my $diffinfo;
3326 my $to_name;
3327 my (%from, %to);
3328
3329 print "<div class=\"patchset\">\n";
3330
3331 # skip to first patch
3332 while ($patch_line = <$fd>) {
3333 chomp $patch_line;
3334
3335 last if ($patch_line =~ m/^diff /);
3336 }
3337
3338 PATCH:
3339 while ($patch_line) {
3340
3341 # parse "git diff" header line
3342 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
3343 # $1 is from_name, which we do not use
3344 $to_name = unquote($2);
3345 $to_name =~ s!^b/!!;
3346 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
3347 # $1 is 'cc' or 'combined', which we do not use
3348 $to_name = unquote($2);
3349 } else {
3350 $to_name = undef;
3351 }
3352
3353 # check if current patch belong to current raw line
3354 # and parse raw git-diff line if needed
3355 if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
3356 # this is continuation of a split patch
3357 print "<div class=\"patch cont\">\n";
3358 } else {
3359 # advance raw git-diff output if needed
3360 $patch_idx++ if defined $diffinfo;
3361
3362 # read and prepare patch information
3363 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3364
3365 # compact combined diff output can have some patches skipped
3366 # find which patch (using pathname of result) we are at now;
3367 if ($is_combined) {
3368 while ($to_name ne $diffinfo->{'to_file'}) {
3369 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3370 format_diff_cc_simplified($diffinfo, @hash_parents) .
3371 "</div>\n"; # class="patch"
3372
3373 $patch_idx++;
3374 $patch_number++;
3375
3376 last if $patch_idx > $#$difftree;
3377 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3378 }
3379 }
3380
3381 # modifies %from, %to hashes
3382 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
3383
3384 # this is first patch for raw difftree line with $patch_idx index
3385 # we index @$difftree array from 0, but number patches from 1
3386 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
3387 }
3388
3389 # git diff header
3390 #assert($patch_line =~ m/^diff /) if DEBUG;
3391 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
3392 $patch_number++;
3393 # print "git diff" header
3394 print format_git_diff_header_line($patch_line, $diffinfo,
3395 \%from, \%to);
3396
3397 # print extended diff header
3398 print "<div class=\"diff extended_header\">\n";
3399 EXTENDED_HEADER:
3400 while ($patch_line = <$fd>) {
3401 chomp $patch_line;
3402
3403 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
3404
3405 print format_extended_diff_header_line($patch_line, $diffinfo,
3406 \%from, \%to);
3407 }
3408 print "</div>\n"; # class="diff extended_header"
3409
3410 # from-file/to-file diff header
3411 if (! $patch_line) {
3412 print "</div>\n"; # class="patch"
3413 last PATCH;
3414 }
3415 next PATCH if ($patch_line =~ m/^diff /);
3416 #assert($patch_line =~ m/^---/) if DEBUG;
3417
3418 my $last_patch_line = $patch_line;
3419 $patch_line = <$fd>;
3420 chomp $patch_line;
3421 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
3422
3423 print format_diff_from_to_header($last_patch_line, $patch_line,
3424 $diffinfo, \%from, \%to,
3425 @hash_parents);
3426
3427 # the patch itself
3428 LINE:
3429 while ($patch_line = <$fd>) {
3430 chomp $patch_line;
3431
3432 next PATCH if ($patch_line =~ m/^diff /);
3433
3434 print format_diff_line($patch_line, \%from, \%to);
3435 }
3436
3437 } continue {
3438 print "</div>\n"; # class="patch"
3439 }
3440
3441 # for compact combined (--cc) format, with chunk and patch simpliciaction
3442 # patchset might be empty, but there might be unprocessed raw lines
3443 for (++$patch_idx if $patch_number > 0;
3444 $patch_idx < @$difftree;
3445 ++$patch_idx) {
3446 # read and prepare patch information
3447 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3448
3449 # generate anchor for "patch" links in difftree / whatchanged part
3450 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3451 format_diff_cc_simplified($diffinfo, @hash_parents) .
3452 "</div>\n"; # class="patch"
3453
3454 $patch_number++;
3455 }
3456
3457 if ($patch_number == 0) {
3458 if (@hash_parents > 1) {
3459 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
3460 } else {
3461 print "<div class=\"diff nodifferences\">No differences found</div>\n";
3462 }
3463 }
3464
3465 print "</div>\n"; # class="patchset"
3466 }
3467
3468 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3469
3470 sub git_project_list_body {
3471 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
3472
3473 my ($check_forks) = gitweb_check_feature('forks');
3474
3475 my @projects;
3476 foreach my $pr (@$projlist) {
3477 my (@aa) = git_get_last_activity($pr->{'path'});
3478 unless (@aa) {
3479 next;
3480 }
3481 ($pr->{'age'}, $pr->{'age_string'}) = @aa;
3482 if (!defined $pr->{'descr'}) {
3483 my $descr = git_get_project_description($pr->{'path'}) || "";
3484 $pr->{'descr_long'} = to_utf8($descr);
3485 $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
3486 }
3487 if (!defined $pr->{'owner'}) {
3488 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
3489 }
3490 if ($check_forks) {
3491 my $pname = $pr->{'path'};
3492 if (($pname =~ s/\.git$//) &&
3493 ($pname !~ /\/$/) &&
3494 (-d "$projectroot/$pname")) {
3495 $pr->{'forks'} = "-d $projectroot/$pname";
3496 }
3497 else {
3498 $pr->{'forks'} = 0;
3499 }
3500 }
3501 push @projects, $pr;
3502 }
3503
3504 $order ||= $default_projects_order;
3505 $from = 0 unless defined $from;
3506 $to = $#projects if (!defined $to || $#projects < $to);
3507
3508 print "<table class=\"project_list\">\n";
3509 unless ($no_header) {
3510 print "<tr>\n";
3511 if ($check_forks) {
3512 print "<th></th>\n";
3513 }
3514 if ($order eq "project") {
3515 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
3516 print "<th>Project</th>\n";
3517 } else {
3518 print "<th>" .
3519 $cgi->a({-href => href(project=>undef, order=>'project'),
3520 -class => "header"}, "Project") .
3521 "</th>\n";
3522 }
3523 if ($order eq "descr") {
3524 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
3525 print "<th>Description</th>\n";
3526 } else {
3527 print "<th>" .
3528 $cgi->a({-href => href(project=>undef, order=>'descr'),
3529 -class => "header"}, "Description") .
3530 "</th>\n";
3531 }
3532 if ($order eq "owner") {
3533 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
3534 print "<th>Owner</th>\n";
3535 } else {
3536 print "<th>" .
3537 $cgi->a({-href => href(project=>undef, order=>'owner'),
3538 -class => "header"}, "Owner") .
3539 "</th>\n";
3540 }
3541 if ($order eq "age") {
3542 @projects = sort {$a->{'age'} <=> $b->{'age'}} @projects;
3543 print "<th>Last Change</th>\n";
3544 } else {
3545 print "<th>" .
3546 $cgi->a({-href => href(project=>undef, order=>'age'),
3547 -class => "header"}, "Last Change") .
3548 "</th>\n";
3549 }
3550 print "<th></th>\n" .
3551 "</tr>\n";
3552 }
3553 my $alternate = 1;
3554 for (my $i = $from; $i <= $to; $i++) {
3555 my $pr = $projects[$i];
3556 if ($alternate) {
3557 print "<tr class=\"dark\">\n";
3558 } else {
3559 print "<tr class=\"light\">\n";
3560 }
3561 $alternate ^= 1;
3562 if ($check_forks) {
3563 print "<td>";
3564 if ($pr->{'forks'}) {
3565 print "<!-- $pr->{'forks'} -->\n";
3566 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
3567 }
3568 print "</td>\n";
3569 }
3570 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3571 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
3572 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3573 -class => "list", -title => $pr->{'descr_long'}},
3574 esc_html($pr->{'descr'})) . "</td>\n" .
3575 "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n";
3576 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
3577 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
3578 "<td class=\"link\">" .
3579 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
3580 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
3581 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
3582 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
3583 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
3584 "</td>\n" .
3585 "</tr>\n";
3586 }
3587 if (defined $extra) {
3588 print "<tr>\n";
3589 if ($check_forks) {
3590 print "<td></td>\n";
3591 }
3592 print "<td colspan=\"5\">$extra</td>\n" .
3593 "</tr>\n";
3594 }
3595 print "</table>\n";
3596 }
3597
3598 sub git_shortlog_body {
3599 # uses global variable $project
3600 my ($commitlist, $from, $to, $refs, $extra) = @_;
3601
3602 $from = 0 unless defined $from;
3603 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3604
3605 print "<table class=\"shortlog\">\n";
3606 my $alternate = 1;
3607 for (my $i = $from; $i <= $to; $i++) {
3608 my %co = %{$commitlist->[$i]};
3609 my $commit = $co{'id'};
3610 my $ref = format_ref_marker($refs, $commit);
3611 if ($alternate) {
3612 print "<tr class=\"dark\">\n";
3613 } else {
3614 print "<tr class=\"light\">\n";
3615 }
3616 $alternate ^= 1;
3617 my $author = chop_and_escape_str($co{'author_name'}, 10);
3618 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
3619 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3620 "<td><i>" . $author . "</i></td>\n" .
3621 "<td>";
3622 print format_subject_html($co{'title'}, $co{'title_short'},
3623 href(action=>"commit", hash=>$commit), $ref);
3624 print "</td>\n" .
3625 "<td class=\"link\">" .
3626 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
3627 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
3628 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
3629 my $snapshot_links = format_snapshot_links($commit);
3630 if (defined $snapshot_links) {
3631 print " | " . $snapshot_links;
3632 }
3633 print "</td>\n" .
3634 "</tr>\n";
3635 }
3636 if (defined $extra) {
3637 print "<tr>\n" .
3638 "<td colspan=\"4\">$extra</td>\n" .
3639 "</tr>\n";
3640 }
3641 print "</table>\n";
3642 }
3643
3644 sub git_history_body {
3645 # Warning: assumes constant type (blob or tree) during history
3646 my ($commitlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
3647
3648 $from = 0 unless defined $from;
3649 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
3650
3651 print "<table class=\"history\">\n";
3652 my $alternate = 1;
3653 for (my $i = $from; $i <= $to; $i++) {
3654 my %co = %{$commitlist->[$i]};
3655 if (!%co) {
3656 next;
3657 }
3658 my $commit = $co{'id'};
3659
3660 my $ref = format_ref_marker($refs, $commit);
3661
3662 if ($alternate) {
3663 print "<tr class=\"dark\">\n";
3664 } else {
3665 print "<tr class=\"light\">\n";
3666 }
3667 $alternate ^= 1;
3668 # shortlog uses chop_str($co{'author_name'}, 10)
3669 my $author = chop_and_escape_str($co{'author_name'}, 15, 3);
3670 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3671 "<td><i>" . $author . "</i></td>\n" .
3672 "<td>";
3673 # originally git_history used chop_str($co{'title'}, 50)
3674 print format_subject_html($co{'title'}, $co{'title_short'},
3675 href(action=>"commit", hash=>$commit), $ref);
3676 print "</td>\n" .
3677 "<td class=\"link\">" .
3678 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
3679 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
3680
3681 if ($ftype eq 'blob') {
3682 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
3683 my $blob_parent = git_get_hash_by_path($commit, $file_name);
3684 if (defined $blob_current && defined $blob_parent &&
3685 $blob_current ne $blob_parent) {
3686 print " | " .
3687 $cgi->a({-href => href(action=>"blobdiff",
3688 hash=>$blob_current, hash_parent=>$blob_parent,
3689 hash_base=>$hash_base, hash_parent_base=>$commit,
3690 file_name=>$file_name)},
3691 "diff to current");
3692 }
3693 }
3694 print "</td>\n" .
3695 "</tr>\n";
3696 }
3697 if (defined $extra) {
3698 print "<tr>\n" .
3699 "<td colspan=\"4\">$extra</td>\n" .
3700 "</tr>\n";
3701 }
3702 print "</table>\n";
3703 }
3704
3705 sub git_tags_body {
3706 # uses global variable $project
3707 my ($taglist, $from, $to, $extra) = @_;
3708 $from = 0 unless defined $from;
3709 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
3710
3711 print "<table class=\"tags\">\n";
3712 my $alternate = 1;
3713 for (my $i = $from; $i <= $to; $i++) {
3714 my $entry = $taglist->[$i];
3715 my %tag = %$entry;
3716 my $comment = $tag{'subject'};
3717 my $comment_short;
3718 if (defined $comment) {
3719 $comment_short = chop_str($comment, 30, 5);
3720 }
3721 if ($alternate) {
3722 print "<tr class=\"dark\">\n";
3723 } else {
3724 print "<tr class=\"light\">\n";
3725 }
3726 $alternate ^= 1;
3727 if (defined $tag{'age'}) {
3728 print "<td><i>$tag{'age'}</i></td>\n";
3729 } else {
3730 print "<td></td>\n";
3731 }
3732 print "<td>" .
3733 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
3734 -class => "list name"}, esc_html($tag{'name'})) .
3735 "</td>\n" .
3736 "<td>";
3737 if (defined $comment) {
3738 print format_subject_html($comment, $comment_short,
3739 href(action=>"tag", hash=>$tag{'id'}));
3740 }
3741 print "</td>\n" .
3742 "<td class=\"selflink\">";
3743 if ($tag{'type'} eq "tag") {
3744 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
3745 } else {
3746 print "&nbsp;";
3747 }
3748 print "</td>\n" .
3749 "<td class=\"link\">" . " | " .
3750 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
3751 if ($tag{'reftype'} eq "commit") {
3752 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})}, "shortlog") .
3753 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})}, "log");
3754 } elsif ($tag{'reftype'} eq "blob") {
3755 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
3756 }
3757 print "</td>\n" .
3758 "</tr>";
3759 }
3760 if (defined $extra) {
3761 print "<tr>\n" .
3762 "<td colspan=\"5\">$extra</td>\n" .
3763 "</tr>\n";
3764 }
3765 print "</table>\n";
3766 }
3767
3768 sub git_heads_body {
3769 # uses global variable $project
3770 my ($headlist, $head, $from, $to, $extra) = @_;
3771 $from = 0 unless defined $from;
3772 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
3773
3774 print "<table class=\"heads\">\n";
3775 my $alternate = 1;
3776 for (my $i = $from; $i <= $to; $i++) {
3777 my $entry = $headlist->[$i];
3778 my %ref = %$entry;
3779 my $curr = $ref{'id'} eq $head;
3780 if ($alternate) {
3781 print "<tr class=\"dark\">\n";
3782 } else {
3783 print "<tr class=\"light\">\n";
3784 }
3785 $alternate ^= 1;
3786 print "<td><i>$ref{'age'}</i></td>\n" .
3787 ($curr ? "<td class=\"current_head\">" : "<td>") .
3788 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),
3789 -class => "list name"},esc_html($ref{'name'})) .
3790 "</td>\n" .
3791 "<td class=\"link\">" .
3792 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})}, "shortlog") . " | " .
3793 $cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})}, "log") . " | " .
3794 $cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})}, "tree") .
3795 "</td>\n" .
3796 "</tr>";
3797 }
3798 if (defined $extra) {
3799 print "<tr>\n" .
3800 "<td colspan=\"3\">$extra</td>\n" .
3801 "</tr>\n";
3802 }
3803 print "</table>\n";
3804 }
3805
3806 sub git_search_grep_body {
3807 my ($commitlist, $from, $to, $extra) = @_;
3808 $from = 0 unless defined $from;
3809 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3810
3811 print "<table class=\"commit_search\">\n";
3812 my $alternate = 1;
3813 for (my $i = $from; $i <= $to; $i++) {
3814 my %co = %{$commitlist->[$i]};
3815 if (!%co) {
3816 next;
3817 }
3818 my $commit = $co{'id'};
3819 if ($alternate) {
3820 print "<tr class=\"dark\">\n";
3821 } else {
3822 print "<tr class=\"light\">\n";
3823 }
3824 $alternate ^= 1;
3825 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
3826 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3827 "<td><i>" . $author . "</i></td>\n" .
3828 "<td>" .
3829 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
3830 -class => "list subject"},
3831 chop_and_escape_str($co{'title'}, 50) . "<br/>");
3832 my $comment = $co{'comment'};
3833 foreach my $line (@$comment) {
3834 if ($line =~ m/^(.*)($search_regexp)(.*)$/i) {
3835 my ($lead, $match, $trail) = ($1, $2, $3);
3836 $match = chop_str($match, 70, 5, 'center');
3837 my $contextlen = int((80 - length($match))/2);
3838 $contextlen = 30 if ($contextlen > 30);
3839 $lead = chop_str($lead, $contextlen, 10, 'left');
3840 $trail = chop_str($trail, $contextlen, 10, 'right');
3841
3842 $lead = esc_html($lead);
3843 $match = esc_html($match);
3844 $trail = esc_html($trail);
3845
3846 print "$lead<span class=\"match\">$match</span>$trail<br />";
3847 }
3848 }
3849 print "</td>\n" .
3850 "<td class=\"link\">" .
3851 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3852 " | " .
3853 $cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})}, "commitdiff") .
3854 " | " .
3855 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3856 print "</td>\n" .
3857 "</tr>\n";
3858 }
3859 if (defined $extra) {
3860 print "<tr>\n" .
3861 "<td colspan=\"3\">$extra</td>\n" .
3862 "</tr>\n";
3863 }
3864 print "</table>\n";
3865 }
3866
3867 ## ======================================================================
3868 ## ======================================================================
3869 ## actions
3870
3871 sub git_project_list {
3872 my $order = $cgi->param('o');
3873 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
3874 die_error(undef, "Unknown order parameter");
3875 }
3876
3877 my @list = git_get_projects_list();
3878 if (!@list) {
3879 die_error(undef, "No projects found");
3880 }
3881
3882 git_header_html();
3883 if (-f $home_text) {
3884 print "<div class=\"index_include\">\n";
3885 open (my $fd, $home_text);
3886 print <$fd>;
3887 close $fd;
3888 print "</div>\n";
3889 }
3890 git_project_list_body(\@list, $order);
3891 git_footer_html();
3892 }
3893
3894 sub git_forks {
3895 my $order = $cgi->param('o');
3896 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
3897 die_error(undef, "Unknown order parameter");
3898 }
3899
3900 my @list = git_get_projects_list($project);
3901 if (!@list) {
3902 die_error(undef, "No forks found");
3903 }
3904
3905 git_header_html();
3906 git_print_page_nav('','');
3907 git_print_header_div('summary', "$project forks");
3908 git_project_list_body(\@list, $order);
3909 git_footer_html();
3910 }
3911
3912 sub git_project_index {
3913 my @projects = git_get_projects_list($project);
3914
3915 print $cgi->header(
3916 -type => 'text/plain',
3917 -charset => 'utf-8',
3918 -content_disposition => 'inline; filename="index.aux"');
3919
3920 foreach my $pr (@projects) {
3921 if (!exists $pr->{'owner'}) {
3922 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
3923 }
3924
3925 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
3926 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
3927 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
3928 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
3929 $path =~ s/ /\+/g;
3930 $owner =~ s/ /\+/g;
3931
3932 print "$path $owner\n";
3933 }
3934 }
3935
3936 sub git_summary {
3937 my $descr = git_get_project_description($project) || "none";
3938 my %co = parse_commit("HEAD");
3939 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
3940 my $head = $co{'id'};
3941
3942 my $owner = git_get_project_owner($project);
3943
3944 my $refs = git_get_references();
3945 # These get_*_list functions return one more to allow us to see if
3946 # there are more ...
3947 my @taglist = git_get_tags_list(16);
3948 my @headlist = git_get_heads_list(16);
3949 my @forklist;
3950 my ($check_forks) = gitweb_check_feature('forks');
3951
3952 if ($check_forks) {
3953 @forklist = git_get_projects_list($project);
3954 }
3955
3956 git_header_html();
3957 git_print_page_nav('summary','', $head);
3958
3959 print "<div class=\"title\">&nbsp;</div>\n";
3960 print "<table class=\"projects_list\">\n" .
3961 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
3962 "<tr><td>owner</td><td>" . esc_html($owner) . "</td></tr>\n";
3963 if (defined $cd{'rfc2822'}) {
3964 print "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
3965 }
3966
3967 # use per project git URL list in $projectroot/$project/cloneurl
3968 # or make project git URL from git base URL and project name
3969 my $url_tag = "URL";
3970 my @url_list = git_get_project_url_list($project);
3971 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
3972 foreach my $git_url (@url_list) {
3973 next unless $git_url;
3974 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
3975 $url_tag = "";
3976 }
3977 print "</table>\n";
3978
3979 if (-s "$projectroot/$project/README.html") {
3980 if (open my $fd, "$projectroot/$project/README.html") {
3981 print "<div class=\"title\">readme</div>\n" .
3982 "<div class=\"readme\">\n";
3983 print $_ while (<$fd>);
3984 print "\n</div>\n"; # class="readme"
3985 close $fd;
3986 }
3987 }
3988
3989 # we need to request one more than 16 (0..15) to check if
3990 # those 16 are all
3991 my @commitlist = $head ? parse_commits($head, 17) : ();
3992 if (@commitlist) {
3993 git_print_header_div('shortlog');
3994 git_shortlog_body(\@commitlist, 0, 15, $refs,
3995 $#commitlist <= 15 ? undef :
3996 $cgi->a({-href => href(action=>"shortlog")}, "..."));
3997 }
3998
3999 if (@taglist) {
4000 git_print_header_div('tags');
4001 git_tags_body(\@taglist, 0, 15,
4002 $#taglist <= 15 ? undef :
4003 $cgi->a({-href => href(action=>"tags")}, "..."));
4004 }
4005
4006 if (@headlist) {
4007 git_print_header_div('heads');
4008 git_heads_body(\@headlist, $head, 0, 15,
4009 $#headlist <= 15 ? undef :
4010 $cgi->a({-href => href(action=>"heads")}, "..."));
4011 }
4012
4013 if (@forklist) {
4014 git_print_header_div('forks');
4015 git_project_list_body(\@forklist, undef, 0, 15,
4016 $#forklist <= 15 ? undef :
4017 $cgi->a({-href => href(action=>"forks")}, "..."),
4018 'noheader');
4019 }
4020
4021 git_footer_html();
4022 }
4023
4024 sub git_tag {
4025 my $head = git_get_head_hash($project);
4026 git_header_html();
4027 git_print_page_nav('','', $head,undef,$head);
4028 my %tag = parse_tag($hash);
4029
4030 if (! %tag) {
4031 die_error(undef, "Unknown tag object");
4032 }
4033
4034 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
4035 print "<div class=\"title_text\">\n" .
4036 "<table class=\"object_header\">\n" .
4037 "<tr>\n" .
4038 "<td>object</td>\n" .
4039 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
4040 $tag{'object'}) . "</td>\n" .
4041 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
4042 $tag{'type'}) . "</td>\n" .
4043 "</tr>\n";
4044 if (defined($tag{'author'})) {
4045 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
4046 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
4047 print "<tr><td></td><td>" . $ad{'rfc2822'} .
4048 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
4049 "</td></tr>\n";
4050 }
4051 print "</table>\n\n" .
4052 "</div>\n";
4053 print "<div class=\"page_body\">";
4054 my $comment = $tag{'comment'};
4055 foreach my $line (@$comment) {
4056 chomp $line;
4057 print esc_html($line, -nbsp=>1) . "<br/>\n";
4058 }
4059 print "</div>\n";
4060 git_footer_html();
4061 }
4062
4063 sub git_blame2 {
4064 my $fd;
4065 my $ftype;
4066
4067 my ($have_blame) = gitweb_check_feature('blame');
4068 if (!$have_blame) {
4069 die_error('403 Permission denied', "Permission denied");
4070 }
4071 die_error('404 Not Found', "File name not defined") if (!$file_name);
4072 $hash_base ||= git_get_head_hash($project);
4073 die_error(undef, "Couldn't find base commit") unless ($hash_base);
4074 my %co = parse_commit($hash_base)
4075 or die_error(undef, "Reading commit failed");
4076 if (!defined $hash) {
4077 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
4078 or die_error(undef, "Error looking up file");
4079 }
4080 $ftype = git_get_type($hash);
4081 if ($ftype !~ "blob") {
4082 die_error('400 Bad Request', "Object is not a blob");
4083 }
4084 open ($fd, "-|", git_cmd(), "blame", '-p', '--',
4085 $file_name, $hash_base)
4086 or die_error(undef, "Open git-blame failed");
4087 git_header_html();
4088 my $formats_nav =
4089 $cgi->a({-href => href(action=>"blob", -replay=>1)},
4090 "blob") .
4091 " | " .
4092 $cgi->a({-href => href(action=>"history", -replay=>1)},
4093 "history") .
4094 " | " .
4095 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
4096 "HEAD");
4097 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4098 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4099 git_print_page_path($file_name, $ftype, $hash_base);
4100 my @rev_color = (qw(light2 dark2));
4101 my $num_colors = scalar(@rev_color);
4102 my $current_color = 0;
4103 my $last_rev;
4104 print <<HTML;
4105 <div class="page_body">
4106 <table class="blame">
4107 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
4108 HTML
4109 my %metainfo = ();
4110 while (1) {
4111 $_ = <$fd>;
4112 last unless defined $_;
4113 my ($full_rev, $orig_lineno, $lineno, $group_size) =
4114 /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;
4115 if (!exists $metainfo{$full_rev}) {
4116 $metainfo{$full_rev} = {};
4117 }
4118 my $meta = $metainfo{$full_rev};
4119 while (<$fd>) {
4120 last if (s/^\t//);
4121 if (/^(\S+) (.*)$/) {
4122 $meta->{$1} = $2;
4123 }
4124 }
4125 my $data = $_;
4126 chomp $data;
4127 my $rev = substr($full_rev, 0, 8);
4128 my $author = $meta->{'author'};
4129 my %date = parse_date($meta->{'author-time'},
4130 $meta->{'author-tz'});
4131 my $date = $date{'iso-tz'};
4132 if ($group_size) {
4133 $current_color = ++$current_color % $num_colors;
4134 }
4135 print "<tr class=\"$rev_color[$current_color]\">\n";
4136 if ($group_size) {
4137 print "<td class=\"sha1\"";
4138 print " title=\"". esc_html($author) . ", $date\"";
4139 print " rowspan=\"$group_size\"" if ($group_size > 1);
4140 print ">";
4141 print $cgi->a({-href => href(action=>"commit",
4142 hash=>$full_rev,
4143 file_name=>$file_name)},
4144 esc_html($rev));
4145 print "</td>\n";
4146 }
4147 open (my $dd, "-|", git_cmd(), "rev-parse", "$full_rev^")
4148 or die_error(undef, "Open git-rev-parse failed");
4149 my $parent_commit = <$dd>;
4150 close $dd;
4151 chomp($parent_commit);
4152 my $blamed = href(action => 'blame',
4153 file_name => $meta->{'filename'},
4154 hash_base => $parent_commit);
4155 print "<td class=\"linenr\">";
4156 print $cgi->a({ -href => "$blamed#l$orig_lineno",
4157 -id => "l$lineno",
4158 -class => "linenr" },
4159 esc_html($lineno));
4160 print "</td>";
4161 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
4162 print "</tr>\n";
4163 }
4164 print "</table>\n";
4165 print "</div>";
4166 close $fd
4167 or print "Reading blob failed\n";
4168 git_footer_html();
4169 }
4170
4171 sub git_blame {
4172 my $fd;
4173
4174 my ($have_blame) = gitweb_check_feature('blame');
4175 if (!$have_blame) {
4176 die_error('403 Permission denied', "Permission denied");
4177 }
4178 die_error('404 Not Found', "File name not defined") if (!$file_name);
4179 $hash_base ||= git_get_head_hash($project);
4180 die_error(undef, "Couldn't find base commit") unless ($hash_base);
4181 my %co = parse_commit($hash_base)
4182 or die_error(undef, "Reading commit failed");
4183 if (!defined $hash) {
4184 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
4185 or die_error(undef, "Error lookup file");
4186 }
4187 open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
4188 or die_error(undef, "Open git-annotate failed");
4189 git_header_html();
4190 my $formats_nav =
4191 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
4192 "blob") .
4193 " | " .
4194 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
4195 "history") .
4196 " | " .
4197 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
4198 "HEAD");
4199 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4200 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4201 git_print_page_path($file_name, 'blob', $hash_base);
4202 print "<div class=\"page_body\">\n";
4203 print <<HTML;
4204 <table class="blame">
4205 <tr>
4206 <th>Commit</th>
4207 <th>Age</th>
4208 <th>Author</th>
4209 <th>Line</th>
4210 <th>Data</th>
4211 </tr>
4212 HTML
4213 my @line_class = (qw(light dark));
4214 my $line_class_len = scalar (@line_class);
4215 my $line_class_num = $#line_class;
4216 while (my $line = <$fd>) {
4217 my $long_rev;
4218 my $short_rev;
4219 my $author;
4220 my $time;
4221 my $lineno;
4222 my $data;
4223 my $age;
4224 my $age_str;
4225 my $age_class;
4226
4227 chomp $line;
4228 $line_class_num = ($line_class_num + 1) % $line_class_len;
4229
4230 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
4231 $long_rev = $1;
4232 $author = $2;
4233 $time = $3;
4234 $lineno = $4;
4235 $data = $5;
4236 } else {
4237 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
4238 next;
4239 }
4240 $short_rev = substr ($long_rev, 0, 8);
4241 $age = time () - $time;
4242 $age_str = age_string ($age);
4243 $age_str =~ s/ /&nbsp;/g;
4244 $age_class = age_class($age);
4245 $author = esc_html ($author);
4246 $author =~ s/ /&nbsp;/g;
4247
4248 $data = untabify($data);
4249 $data = esc_html ($data);
4250
4251 print <<HTML;
4252 <tr class="$line_class[$line_class_num]">
4253 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
4254 <td class="$age_class">$age_str</td>
4255 <td>$author</td>
4256 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
4257 <td class="pre">$data</td>
4258 </tr>
4259 HTML
4260 } # while (my $line = <$fd>)
4261 print "</table>\n\n";
4262 close $fd
4263 or print "Reading blob failed.\n";
4264 print "</div>";
4265 git_footer_html();
4266 }
4267
4268 sub git_tags {
4269 my $head = git_get_head_hash($project);
4270 git_header_html();
4271 git_print_page_nav('','', $head,undef,$head);
4272 git_print_header_div('summary', $project);
4273
4274 my @tagslist = git_get_tags_list();
4275 if (@tagslist) {
4276 git_tags_body(\@tagslist);
4277 }
4278 git_footer_html();
4279 }
4280
4281 sub git_heads {
4282 my $head = git_get_head_hash($project);
4283 git_header_html();
4284 git_print_page_nav('','', $head,undef,$head);
4285 git_print_header_div('summary', $project);
4286
4287 my @headslist = git_get_heads_list();
4288 if (@headslist) {
4289 git_heads_body(\@headslist, $head);
4290 }
4291 git_footer_html();
4292 }
4293
4294 sub git_blob_plain {
4295 my $expires;
4296
4297 if (!defined $hash) {
4298 if (defined $file_name) {
4299 my $base = $hash_base || git_get_head_hash($project);
4300 $hash = git_get_hash_by_path($base, $file_name, "blob")
4301 or die_error(undef, "Error lookup file");
4302 } else {
4303 die_error(undef, "No file name defined");
4304 }
4305 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4306 # blobs defined by non-textual hash id's can be cached
4307 $expires = "+1d";
4308 }
4309
4310 my $type = shift;
4311 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4312 or die_error(undef, "Couldn't cat $file_name, $hash");
4313
4314 $type ||= blob_mimetype($fd, $file_name);
4315
4316 # save as filename, even when no $file_name is given
4317 my $save_as = "$hash";
4318 if (defined $file_name) {
4319 $save_as = $file_name;
4320 } elsif ($type =~ m/^text\//) {
4321 $save_as .= '.txt';
4322 }
4323
4324 print $cgi->header(
4325 -type => "$type",
4326 -expires=>$expires,
4327 -content_disposition => 'inline; filename="' . "$save_as" . '"');
4328 undef $/;
4329 binmode STDOUT, ':raw';
4330 print <$fd>;
4331 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4332 $/ = "\n";
4333 close $fd;
4334 }
4335
4336 sub git_blob {
4337 my $expires;
4338
4339 if (!defined $hash) {
4340 if (defined $file_name) {
4341 my $base = $hash_base || git_get_head_hash($project);
4342 $hash = git_get_hash_by_path($base, $file_name, "blob")
4343 or die_error(undef, "Error lookup file");
4344 } else {
4345 die_error(undef, "No file name defined");
4346 }
4347 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4348 # blobs defined by non-textual hash id's can be cached
4349 $expires = "+1d";
4350 }
4351
4352 my ($have_blame) = gitweb_check_feature('blame');
4353 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4354 or die_error(undef, "Couldn't cat $file_name, $hash");
4355 my $mimetype = blob_mimetype($fd, $file_name);
4356 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {
4357 close $fd;
4358 return git_blob_plain($mimetype);
4359 }
4360 # we can have blame only for text/* mimetype
4361 $have_blame &&= ($mimetype =~ m!^text/!);
4362
4363 git_header_html(undef, $expires);
4364 my $formats_nav = '';
4365 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4366 if (defined $file_name) {
4367 if ($have_blame) {
4368 $formats_nav .=
4369 $cgi->a({-href => href(action=>"blame", -replay=>1)},
4370 "blame") .
4371 " | ";
4372 }
4373 $formats_nav .=
4374 $cgi->a({-href => href(action=>"history", -replay=>1)},
4375 "history") .
4376 " | " .
4377 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
4378 "raw") .
4379 " | " .
4380 $cgi->a({-href => href(action=>"blob",
4381 hash_base=>"HEAD", file_name=>$file_name)},
4382 "HEAD");
4383 } else {
4384 $formats_nav .=
4385 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
4386 "raw");
4387 }
4388 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4389 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4390 } else {
4391 print "<div class=\"page_nav\">\n" .
4392 "<br/><br/></div>\n" .
4393 "<div class=\"title\">$hash</div>\n";
4394 }
4395 git_print_page_path($file_name, "blob", $hash_base);
4396 print "<div class=\"page_body\">\n";
4397 if ($mimetype =~ m!^image/!) {
4398 print qq!<img type="$mimetype"!;
4399 if ($file_name) {
4400 print qq! alt="$file_name" title="$file_name"!;
4401 }
4402 print qq! src="! .
4403 href(action=>"blob_plain", hash=>$hash,
4404 hash_base=>$hash_base, file_name=>$file_name) .
4405 qq!" />\n!;
4406 } else {
4407 my $nr;
4408 while (my $line = <$fd>) {
4409 chomp $line;
4410 $nr++;
4411 $line = untabify($line);
4412 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
4413 $nr, $nr, $nr, esc_html($line, -nbsp=>1);
4414 }
4415 }
4416 close $fd
4417 or print "Reading blob failed.\n";
4418 print "</div>";
4419 git_footer_html();
4420 }
4421
4422 sub git_tree {
4423 if (!defined $hash_base) {
4424 $hash_base = "HEAD";
4425 }
4426 if (!defined $hash) {
4427 if (defined $file_name) {
4428 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
4429 } else {
4430 $hash = $hash_base;
4431 }
4432 }
4433 $/ = "\0";
4434 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
4435 or die_error(undef, "Open git-ls-tree failed");
4436 my @entries = map { chomp; $_ } <$fd>;
4437 close $fd or die_error(undef, "Reading tree failed");
4438 $/ = "\n";
4439
4440 my $refs = git_get_references();
4441 my $ref = format_ref_marker($refs, $hash_base);
4442 git_header_html();
4443 my $basedir = '';
4444 my ($have_blame) = gitweb_check_feature('blame');
4445 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4446 my @views_nav = ();
4447 if (defined $file_name) {
4448 push @views_nav,
4449 $cgi->a({-href => href(action=>"history", -replay=>1)},
4450 "history"),
4451 $cgi->a({-href => href(action=>"tree",
4452 hash_base=>"HEAD", file_name=>$file_name)},
4453 "HEAD"),
4454 }
4455 my $snapshot_links = format_snapshot_links($hash);
4456 if (defined $snapshot_links) {
4457 # FIXME: Should be available when we have no hash base as well.
4458 push @views_nav, $snapshot_links;
4459 }
4460 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
4461 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
4462 } else {
4463 undef $hash_base;
4464 print "<div class=\"page_nav\">\n";
4465 print "<br/><br/></div>\n";
4466 print "<div class=\"title\">$hash</div>\n";
4467 }
4468 if (defined $file_name) {
4469 $basedir = $file_name;
4470 if ($basedir ne '' && substr($basedir, -1) ne '/') {
4471 $basedir .= '/';
4472 }
4473 }
4474 git_print_page_path($file_name, 'tree', $hash_base);
4475 print "<div class=\"page_body\">\n";
4476 print "<table class=\"tree\">\n";
4477 my $alternate = 1;
4478 # '..' (top directory) link if possible
4479 if (defined $hash_base &&
4480 defined $file_name && $file_name =~ m![^/]+$!) {
4481 if ($alternate) {
4482 print "<tr class=\"dark\">\n";
4483 } else {
4484 print "<tr class=\"light\">\n";
4485 }
4486 $alternate ^= 1;
4487
4488 my $up = $file_name;
4489 $up =~ s!/?[^/]+$!!;
4490 undef $up unless $up;
4491 # based on git_print_tree_entry
4492 print '<td class="mode">' . mode_str('040000') . "</td>\n";
4493 print '<td class="list">';
4494 print $cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,
4495 file_name=>$up)},
4496 "..");
4497 print "</td>\n";
4498 print "<td class=\"link\"></td>\n";
4499
4500 print "</tr>\n";
4501 }
4502 foreach my $line (@entries) {
4503 my %t = parse_ls_tree_line($line, -z => 1);
4504
4505 if ($alternate) {
4506 print "<tr class=\"dark\">\n";
4507 } else {
4508 print "<tr class=\"light\">\n";
4509 }
4510 $alternate ^= 1;
4511
4512 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
4513
4514 print "</tr>\n";
4515 }
4516 print "</table>\n" .
4517 "</div>";
4518 git_footer_html();
4519 }
4520
4521 sub git_snapshot {
4522 my @supported_fmts = gitweb_check_feature('snapshot');
4523 @supported_fmts = filter_snapshot_fmts(@supported_fmts);
4524
4525 my $format = $cgi->param('sf');
4526 if (!@supported_fmts) {
4527 die_error('403 Permission denied', "Permission denied");
4528 }
4529 # default to first supported snapshot format
4530 $format ||= $supported_fmts[0];
4531 if ($format !~ m/^[a-z0-9]+$/) {
4532 die_error(undef, "Invalid snapshot format parameter");
4533 } elsif (!exists($known_snapshot_formats{$format})) {
4534 die_error(undef, "Unknown snapshot format");
4535 } elsif (!grep($_ eq $format, @supported_fmts)) {
4536 die_error(undef, "Unsupported snapshot format");
4537 }
4538
4539 if (!defined $hash) {
4540 $hash = git_get_head_hash($project);
4541 }
4542
4543 my $git_command = git_cmd_str();
4544 my $name = $project;
4545 $name =~ s,([^/])/*\.git$,$1,;
4546 $name = basename($name);
4547 my $filename = to_utf8($name);
4548 $name =~ s/\047/\047\\\047\047/g;
4549 my $cmd;
4550 $filename .= "-$hash$known_snapshot_formats{$format}{'suffix'}";
4551 $cmd = "$git_command archive " .
4552 "--format=$known_snapshot_formats{$format}{'format'} " .
4553 "--prefix=\'$name\'/ $hash";
4554 if (exists $known_snapshot_formats{$format}{'compressor'}) {
4555 $cmd .= ' | ' . join ' ', @{$known_snapshot_formats{$format}{'compressor'}};
4556 }
4557
4558 print $cgi->header(
4559 -type => $known_snapshot_formats{$format}{'type'},
4560 -content_disposition => 'inline; filename="' . "$filename" . '"',
4561 -status => '200 OK');
4562
4563 open my $fd, "-|", $cmd
4564 or die_error(undef, "Execute git-archive failed");
4565 binmode STDOUT, ':raw';
4566 print <$fd>;
4567 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4568 close $fd;
4569 }
4570
4571 sub git_log {
4572 my $head = git_get_head_hash($project);
4573 if (!defined $hash) {
4574 $hash = $head;
4575 }
4576 if (!defined $page) {
4577 $page = 0;
4578 }
4579 my $refs = git_get_references();
4580
4581 my @commitlist = parse_commits($hash, 101, (100 * $page));
4582
4583 my $paging_nav = format_paging_nav('log', $hash, $head, $page, (100 * ($page+1)));
4584
4585 git_header_html();
4586 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
4587
4588 if (!@commitlist) {
4589 my %co = parse_commit($hash);
4590
4591 git_print_header_div('summary', $project);
4592 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
4593 }
4594 my $to = ($#commitlist >= 99) ? (99) : ($#commitlist);
4595 for (my $i = 0; $i <= $to; $i++) {
4596 my %co = %{$commitlist[$i]};
4597 next if !%co;
4598 my $commit = $co{'id'};
4599 my $ref = format_ref_marker($refs, $commit);
4600 my %ad = parse_date($co{'author_epoch'});
4601 git_print_header_div('commit',
4602 "<span class=\"age\">$co{'age_string'}</span>" .
4603 esc_html($co{'title'}) . $ref,
4604 $commit);
4605 print "<div class=\"title_text\">\n" .
4606 "<div class=\"log_link\">\n" .
4607 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
4608 " | " .
4609 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
4610 " | " .
4611 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
4612 "<br/>\n" .
4613 "</div>\n" .
4614 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
4615 "</div>\n";
4616
4617 print "<div class=\"log_body\">\n";
4618 git_print_log($co{'comment'}, -final_empty_line=> 1);
4619 print "</div>\n";
4620 }
4621 if ($#commitlist >= 100) {
4622 print "<div class=\"page_nav\">\n";
4623 print $cgi->a({-href => href(-replay=>1, page=>$page+1),
4624 -accesskey => "n", -title => "Alt-n"}, "next");
4625 print "</div>\n";
4626 }
4627 git_footer_html();
4628 }
4629
4630 sub git_commit {
4631 $hash ||= $hash_base || "HEAD";
4632 my %co = parse_commit($hash);
4633 if (!%co) {
4634 die_error(undef, "Unknown commit object");
4635 }
4636 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4637 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
4638
4639 my $parent = $co{'parent'};
4640 my $parents = $co{'parents'}; # listref
4641
4642 # we need to prepare $formats_nav before any parameter munging
4643 my $formats_nav;
4644 if (!defined $parent) {
4645 # --root commitdiff
4646 $formats_nav .= '(initial)';
4647 } elsif (@$parents == 1) {
4648 # single parent commit
4649 $formats_nav .=
4650 '(parent: ' .
4651 $cgi->a({-href => href(action=>"commit",
4652 hash=>$parent)},
4653 esc_html(substr($parent, 0, 7))) .
4654 ')';
4655 } else {
4656 # merge commit
4657 $formats_nav .=
4658 '(merge: ' .
4659 join(' ', map {
4660 $cgi->a({-href => href(action=>"commit",
4661 hash=>$_)},
4662 esc_html(substr($_, 0, 7)));
4663 } @$parents ) .
4664 ')';
4665 }
4666
4667 if (!defined $parent) {
4668 $parent = "--root";
4669 }
4670 my @difftree;
4671 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
4672 @diff_opts,
4673 (@$parents <= 1 ? $parent : '-c'),
4674 $hash, "--"
4675 or die_error(undef, "Open git-diff-tree failed");
4676 @difftree = map { chomp; $_ } <$fd>;
4677 close $fd or die_error(undef, "Reading git-diff-tree failed");
4678
4679 # non-textual hash id's can be cached
4680 my $expires;
4681 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4682 $expires = "+1d";
4683 }
4684 my $refs = git_get_references();
4685 my $ref = format_ref_marker($refs, $co{'id'});
4686
4687 git_header_html(undef, $expires);
4688 git_print_page_nav('commit', '',
4689 $hash, $co{'tree'}, $hash,
4690 $formats_nav);
4691
4692 if (defined $co{'parent'}) {
4693 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
4694 } else {
4695 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
4696 }
4697 print "<div class=\"title_text\">\n" .
4698 "<table class=\"object_header\">\n";
4699 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
4700 "<tr>" .
4701 "<td></td><td> $ad{'rfc2822'}";
4702 if ($ad{'hour_local'} < 6) {
4703 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
4704 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4705 } else {
4706 printf(" (%02d:%02d %s)",
4707 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4708 }
4709 print "</td>" .
4710 "</tr>\n";
4711 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
4712 print "<tr><td></td><td> $cd{'rfc2822'}" .
4713 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
4714 "</td></tr>\n";
4715 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
4716 print "<tr>" .
4717 "<td>tree</td>" .
4718 "<td class=\"sha1\">" .
4719 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
4720 class => "list"}, $co{'tree'}) .
4721 "</td>" .
4722 "<td class=\"link\">" .
4723 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
4724 "tree");
4725 my $snapshot_links = format_snapshot_links($hash);
4726 if (defined $snapshot_links) {
4727 print " | " . $snapshot_links;
4728 }
4729 print "</td>" .
4730 "</tr>\n";
4731
4732 foreach my $par (@$parents) {
4733 print "<tr>" .
4734 "<td>parent</td>" .
4735 "<td class=\"sha1\">" .
4736 $cgi->a({-href => href(action=>"commit", hash=>$par),
4737 class => "list"}, $par) .
4738 "</td>" .
4739 "<td class=\"link\">" .
4740 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
4741 " | " .
4742 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
4743 "</td>" .
4744 "</tr>\n";
4745 }
4746 print "</table>".
4747 "</div>\n";
4748
4749 print "<div class=\"page_body\">\n";
4750 git_print_log($co{'comment'});
4751 print "</div>\n";
4752
4753 git_difftree_body(\@difftree, $hash, @$parents);
4754
4755 git_footer_html();
4756 }
4757
4758 sub git_object {
4759 # object is defined by:
4760 # - hash or hash_base alone
4761 # - hash_base and file_name
4762 my $type;
4763
4764 # - hash or hash_base alone
4765 if ($hash || ($hash_base && !defined $file_name)) {
4766 my $object_id = $hash || $hash_base;
4767
4768 my $git_command = git_cmd_str();
4769 open my $fd, "-|", "$git_command cat-file -t $object_id 2>/dev/null"
4770 or die_error('404 Not Found', "Object does not exist");
4771 $type = <$fd>;
4772 chomp $type;
4773 close $fd
4774 or die_error('404 Not Found', "Object does not exist");
4775
4776 # - hash_base and file_name
4777 } elsif ($hash_base && defined $file_name) {
4778 $file_name =~ s,/+$,,;
4779
4780 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
4781 or die_error('404 Not Found', "Base object does not exist");
4782
4783 # here errors should not hapen
4784 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
4785 or die_error(undef, "Open git-ls-tree failed");
4786 my $line = <$fd>;
4787 close $fd;
4788
4789 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
4790 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
4791 die_error('404 Not Found', "File or directory for given base does not exist");
4792 }
4793 $type = $2;
4794 $hash = $3;
4795 } else {
4796 die_error('404 Not Found', "Not enough information to find object");
4797 }
4798
4799 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
4800 hash=>$hash, hash_base=>$hash_base,
4801 file_name=>$file_name),
4802 -status => '302 Found');
4803 }
4804
4805 sub git_blobdiff {
4806 my $format = shift || 'html';
4807
4808 my $fd;
4809 my @difftree;
4810 my %diffinfo;
4811 my $expires;
4812
4813 # preparing $fd and %diffinfo for git_patchset_body
4814 # new style URI
4815 if (defined $hash_base && defined $hash_parent_base) {
4816 if (defined $file_name) {
4817 # read raw output
4818 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4819 $hash_parent_base, $hash_base,
4820 "--", (defined $file_parent ? $file_parent : ()), $file_name
4821 or die_error(undef, "Open git-diff-tree failed");
4822 @difftree = map { chomp; $_ } <$fd>;
4823 close $fd
4824 or die_error(undef, "Reading git-diff-tree failed");
4825 @difftree
4826 or die_error('404 Not Found', "Blob diff not found");
4827
4828 } elsif (defined $hash &&
4829 $hash =~ /[0-9a-fA-F]{40}/) {
4830 # try to find filename from $hash
4831
4832 # read filtered raw output
4833 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4834 $hash_parent_base, $hash_base, "--"
4835 or die_error(undef, "Open git-diff-tree failed");
4836 @difftree =
4837 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
4838 # $hash == to_id
4839 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
4840 map { chomp; $_ } <$fd>;
4841 close $fd
4842 or die_error(undef, "Reading git-diff-tree failed");
4843 @difftree
4844 or die_error('404 Not Found', "Blob diff not found");
4845
4846 } else {
4847 die_error('404 Not Found', "Missing one of the blob diff parameters");
4848 }
4849
4850 if (@difftree > 1) {
4851 die_error('404 Not Found', "Ambiguous blob diff specification");
4852 }
4853
4854 %diffinfo = parse_difftree_raw_line($difftree[0]);
4855 $file_parent ||= $diffinfo{'from_file'} || $file_name;
4856 $file_name ||= $diffinfo{'to_file'};
4857
4858 $hash_parent ||= $diffinfo{'from_id'};
4859 $hash ||= $diffinfo{'to_id'};
4860
4861 # non-textual hash id's can be cached
4862 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
4863 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
4864 $expires = '+1d';
4865 }
4866
4867 # open patch output
4868 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4869 '-p', ($format eq 'html' ? "--full-index" : ()),
4870 $hash_parent_base, $hash_base,
4871 "--", (defined $file_parent ? $file_parent : ()), $file_name
4872 or die_error(undef, "Open git-diff-tree failed");
4873 }
4874
4875 # old/legacy style URI
4876 if (!%diffinfo && # if new style URI failed
4877 defined $hash && defined $hash_parent) {
4878 # fake git-diff-tree raw output
4879 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
4880 $diffinfo{'from_id'} = $hash_parent;
4881 $diffinfo{'to_id'} = $hash;
4882 if (defined $file_name) {
4883 if (defined $file_parent) {
4884 $diffinfo{'status'} = '2';
4885 $diffinfo{'from_file'} = $file_parent;
4886 $diffinfo{'to_file'} = $file_name;
4887 } else { # assume not renamed
4888 $diffinfo{'status'} = '1';
4889 $diffinfo{'from_file'} = $file_name;
4890 $diffinfo{'to_file'} = $file_name;
4891 }
4892 } else { # no filename given
4893 $diffinfo{'status'} = '2';
4894 $diffinfo{'from_file'} = $hash_parent;
4895 $diffinfo{'to_file'} = $hash;
4896 }
4897
4898 # non-textual hash id's can be cached
4899 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
4900 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4901 $expires = '+1d';
4902 }
4903
4904 # open patch output
4905 open $fd, "-|", git_cmd(), "diff", @diff_opts,
4906 '-p', ($format eq 'html' ? "--full-index" : ()),
4907 $hash_parent, $hash, "--"
4908 or die_error(undef, "Open git-diff failed");
4909 } else {
4910 die_error('404 Not Found', "Missing one of the blob diff parameters")
4911 unless %diffinfo;
4912 }
4913
4914 # header
4915 if ($format eq 'html') {
4916 my $formats_nav =
4917 $cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},
4918 "raw");
4919 git_header_html(undef, $expires);
4920 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4921 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4922 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4923 } else {
4924 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
4925 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
4926 }
4927 if (defined $file_name) {
4928 git_print_page_path($file_name, "blob", $hash_base);
4929 } else {
4930 print "<div class=\"page_path\"></div>\n";
4931 }
4932
4933 } elsif ($format eq 'plain') {
4934 print $cgi->header(
4935 -type => 'text/plain',
4936 -charset => 'utf-8',
4937 -expires => $expires,
4938 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
4939
4940 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
4941
4942 } else {
4943 die_error(undef, "Unknown blobdiff format");
4944 }
4945
4946 # patch
4947 if ($format eq 'html') {
4948 print "<div class=\"page_body\">\n";
4949
4950 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
4951 close $fd;
4952
4953 print "</div>\n"; # class="page_body"
4954 git_footer_html();
4955
4956 } else {
4957 while (my $line = <$fd>) {
4958 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
4959 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
4960
4961 print $line;
4962
4963 last if $line =~ m!^\+\+\+!;
4964 }
4965 local $/ = undef;
4966 print <$fd>;
4967 close $fd;
4968 }
4969 }
4970
4971 sub git_blobdiff_plain {
4972 git_blobdiff('plain');
4973 }
4974
4975 sub git_commitdiff {
4976 my $format = shift || 'html';
4977 $hash ||= $hash_base || "HEAD";
4978 my %co = parse_commit($hash);
4979 if (!%co) {
4980 die_error(undef, "Unknown commit object");
4981 }
4982
4983 # choose format for commitdiff for merge
4984 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
4985 $hash_parent = '--cc';
4986 }
4987 # we need to prepare $formats_nav before almost any parameter munging
4988 my $formats_nav;
4989 if ($format eq 'html') {
4990 $formats_nav =
4991 $cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
4992 "raw");
4993
4994 if (defined $hash_parent &&
4995 $hash_parent ne '-c' && $hash_parent ne '--cc') {
4996 # commitdiff with two commits given
4997 my $hash_parent_short = $hash_parent;
4998 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4999 $hash_parent_short = substr($hash_parent, 0, 7);
5000 }
5001 $formats_nav .=
5002 ' (from';
5003 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
5004 if ($co{'parents'}[$i] eq $hash_parent) {
5005 $formats_nav .= ' parent ' . ($i+1);
5006 last;
5007 }
5008 }
5009 $formats_nav .= ': ' .
5010 $cgi->a({-href => href(action=>"commitdiff",
5011 hash=>$hash_parent)},
5012 esc_html($hash_parent_short)) .
5013 ')';
5014 } elsif (!$co{'parent'}) {
5015 # --root commitdiff
5016 $formats_nav .= ' (initial)';
5017 } elsif (scalar @{$co{'parents'}} == 1) {
5018 # single parent commit
5019 $formats_nav .=
5020 ' (parent: ' .
5021 $cgi->a({-href => href(action=>"commitdiff",
5022 hash=>$co{'parent'})},
5023 esc_html(substr($co{'parent'}, 0, 7))) .
5024 ')';
5025 } else {
5026 # merge commit
5027 if ($hash_parent eq '--cc') {
5028 $formats_nav .= ' | ' .
5029 $cgi->a({-href => href(action=>"commitdiff",
5030 hash=>$hash, hash_parent=>'-c')},
5031 'combined');
5032 } else { # $hash_parent eq '-c'
5033 $formats_nav .= ' | ' .
5034 $cgi->a({-href => href(action=>"commitdiff",
5035 hash=>$hash, hash_parent=>'--cc')},
5036 'compact');
5037 }
5038 $formats_nav .=
5039 ' (merge: ' .
5040 join(' ', map {
5041 $cgi->a({-href => href(action=>"commitdiff",
5042 hash=>$_)},
5043 esc_html(substr($_, 0, 7)));
5044 } @{$co{'parents'}} ) .
5045 ')';
5046 }
5047 }
5048
5049 my $hash_parent_param = $hash_parent;
5050 if (!defined $hash_parent_param) {
5051 # --cc for multiple parents, --root for parentless
5052 $hash_parent_param =
5053 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
5054 }
5055
5056 # read commitdiff
5057 my $fd;
5058 my @difftree;
5059 if ($format eq 'html') {
5060 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5061 "--no-commit-id", "--patch-with-raw", "--full-index",
5062 $hash_parent_param, $hash, "--"
5063 or die_error(undef, "Open git-diff-tree failed");
5064
5065 while (my $line = <$fd>) {
5066 chomp $line;
5067 # empty line ends raw part of diff-tree output
5068 last unless $line;
5069 push @difftree, scalar parse_difftree_raw_line($line);
5070 }
5071
5072 } elsif ($format eq 'plain') {
5073 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5074 '-p', $hash_parent_param, $hash, "--"
5075 or die_error(undef, "Open git-diff-tree failed");
5076
5077 } else {
5078 die_error(undef, "Unknown commitdiff format");
5079 }
5080
5081 # non-textual hash id's can be cached
5082 my $expires;
5083 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5084 $expires = "+1d";
5085 }
5086
5087 # write commit message
5088 if ($format eq 'html') {
5089 my $refs = git_get_references();
5090 my $ref = format_ref_marker($refs, $co{'id'});
5091
5092 git_header_html(undef, $expires);
5093 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
5094 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
5095 git_print_authorship(\%co);
5096 print "<div class=\"page_body\">\n";
5097 if (@{$co{'comment'}} > 1) {
5098 print "<div class=\"log\">\n";
5099 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
5100 print "</div>\n"; # class="log"
5101 }
5102
5103 } elsif ($format eq 'plain') {
5104 my $refs = git_get_references("tags");
5105 my $tagname = git_get_rev_name_tags($hash);
5106 my $filename = basename($project) . "-$hash.patch";
5107
5108 print $cgi->header(
5109 -type => 'text/plain',
5110 -charset => 'utf-8',
5111 -expires => $expires,
5112 -content_disposition => 'inline; filename="' . "$filename" . '"');
5113 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
5114 print "From: " . to_utf8($co{'author'}) . "\n";
5115 print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
5116 print "Subject: " . to_utf8($co{'title'}) . "\n";
5117
5118 print "X-Git-Tag: $tagname\n" if $tagname;
5119 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
5120
5121 foreach my $line (@{$co{'comment'}}) {
5122 print to_utf8($line) . "\n";
5123 }
5124 print "---\n\n";
5125 }
5126
5127 # write patch
5128 if ($format eq 'html') {
5129 my $use_parents = !defined $hash_parent ||
5130 $hash_parent eq '-c' || $hash_parent eq '--cc';
5131 git_difftree_body(\@difftree, $hash,
5132 $use_parents ? @{$co{'parents'}} : $hash_parent);
5133 print "<br/>\n";
5134
5135 git_patchset_body($fd, \@difftree, $hash,
5136 $use_parents ? @{$co{'parents'}} : $hash_parent);
5137 close $fd;
5138 print "</div>\n"; # class="page_body"
5139 git_footer_html();
5140
5141 } elsif ($format eq 'plain') {
5142 local $/ = undef;
5143 print <$fd>;
5144 close $fd
5145 or print "Reading git-diff-tree failed\n";
5146 }
5147 }
5148
5149 sub git_commitdiff_plain {
5150 git_commitdiff('plain');
5151 }
5152
5153 sub git_history {
5154 if (!defined $hash_base) {
5155 $hash_base = git_get_head_hash($project);
5156 }
5157 if (!defined $page) {
5158 $page = 0;
5159 }
5160 my $ftype;
5161 my %co = parse_commit($hash_base);
5162 if (!%co) {
5163 die_error(undef, "Unknown commit object");
5164 }
5165
5166 my $refs = git_get_references();
5167 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
5168
5169 if (!defined $hash && defined $file_name) {
5170 $hash = git_get_hash_by_path($hash_base, $file_name);
5171 }
5172 if (defined $hash) {
5173 $ftype = git_get_type($hash);
5174 }
5175
5176 my @commitlist = parse_commits($hash_base, 101, (100 * $page), $file_name, "--full-history");
5177
5178 my $paging_nav = '';
5179 if ($page > 0) {
5180 $paging_nav .=
5181 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
5182 file_name=>$file_name)},
5183 "first");
5184 $paging_nav .= " &sdot; " .
5185 $cgi->a({-href => href(-replay=>1, page=>$page-1),
5186 -accesskey => "p", -title => "Alt-p"}, "prev");
5187 } else {
5188 $paging_nav .= "first";
5189 $paging_nav .= " &sdot; prev";
5190 }
5191 my $next_link = '';
5192 if ($#commitlist >= 100) {
5193 $next_link =
5194 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5195 -accesskey => "n", -title => "Alt-n"}, "next");
5196 $paging_nav .= " &sdot; $next_link";
5197 } else {
5198 $paging_nav .= " &sdot; next";
5199 }
5200
5201 git_header_html();
5202 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
5203 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5204 git_print_page_path($file_name, $ftype, $hash_base);
5205
5206 git_history_body(\@commitlist, 0, 99,
5207 $refs, $hash_base, $ftype, $next_link);
5208
5209 git_footer_html();
5210 }
5211
5212 sub git_search {
5213 my ($have_search) = gitweb_check_feature('search');
5214 if (!$have_search) {
5215 die_error('403 Permission denied', "Permission denied");
5216 }
5217 if (!defined $searchtext) {
5218 die_error(undef, "Text field empty");
5219 }
5220 if (!defined $hash) {
5221 $hash = git_get_head_hash($project);
5222 }
5223 my %co = parse_commit($hash);
5224 if (!%co) {
5225 die_error(undef, "Unknown commit object");
5226 }
5227 if (!defined $page) {
5228 $page = 0;
5229 }
5230
5231 $searchtype ||= 'commit';
5232 if ($searchtype eq 'pickaxe') {
5233 # pickaxe may take all resources of your box and run for several minutes
5234 # with every query - so decide by yourself how public you make this feature
5235 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
5236 if (!$have_pickaxe) {
5237 die_error('403 Permission denied', "Permission denied");
5238 }
5239 }
5240 if ($searchtype eq 'grep') {
5241 my ($have_grep) = gitweb_check_feature('grep');
5242 if (!$have_grep) {
5243 die_error('403 Permission denied', "Permission denied");
5244 }
5245 }
5246
5247 git_header_html();
5248
5249 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
5250 my $greptype;
5251 if ($searchtype eq 'commit') {
5252 $greptype = "--grep=";
5253 } elsif ($searchtype eq 'author') {
5254 $greptype = "--author=";
5255 } elsif ($searchtype eq 'committer') {
5256 $greptype = "--committer=";
5257 }
5258 $greptype .= $search_regexp;
5259 my @commitlist = parse_commits($hash, 101, (100 * $page), undef, $greptype);
5260
5261 my $paging_nav = '';
5262 if ($page > 0) {
5263 $paging_nav .=
5264 $cgi->a({-href => href(action=>"search", hash=>$hash,
5265 searchtext=>$searchtext, searchtype=>$searchtype)},
5266 "first");
5267 $paging_nav .= " &sdot; " .
5268 $cgi->a({-href => href(-replay=>1, page=>$page-1),
5269 -accesskey => "p", -title => "Alt-p"}, "prev");
5270 } else {
5271 $paging_nav .= "first";
5272 $paging_nav .= " &sdot; prev";
5273 }
5274 my $next_link = '';
5275 if ($#commitlist >= 100) {
5276 $next_link =
5277 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5278 -accesskey => "n", -title => "Alt-n"}, "next");
5279 $paging_nav .= " &sdot; $next_link";
5280 } else {
5281 $paging_nav .= " &sdot; next";
5282 }
5283
5284 if ($#commitlist >= 100) {
5285 }
5286
5287 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
5288 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5289 git_search_grep_body(\@commitlist, 0, 99, $next_link);
5290 }
5291
5292 if ($searchtype eq 'pickaxe') {
5293 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5294 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5295
5296 print "<table class=\"pickaxe search\">\n";
5297 my $alternate = 1;
5298 $/ = "\n";
5299 my $git_command = git_cmd_str();
5300 my $searchqtext = $searchtext;
5301 $searchqtext =~ s/'/'\\''/;
5302 open my $fd, "-|", "$git_command rev-list $hash | " .
5303 "$git_command diff-tree -r --stdin -S\'$searchqtext\'";
5304 undef %co;
5305 my @files;
5306 while (my $line = <$fd>) {
5307 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
5308 my %set;
5309 $set{'file'} = $6;
5310 $set{'from_id'} = $3;
5311 $set{'to_id'} = $4;
5312 $set{'id'} = $set{'to_id'};
5313 if ($set{'id'} =~ m/0{40}/) {
5314 $set{'id'} = $set{'from_id'};
5315 }
5316 if ($set{'id'} =~ m/0{40}/) {
5317 next;
5318 }
5319 push @files, \%set;
5320 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
5321 if (%co) {
5322 if ($alternate) {
5323 print "<tr class=\"dark\">\n";
5324 } else {
5325 print "<tr class=\"light\">\n";
5326 }
5327 $alternate ^= 1;
5328 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
5329 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5330 "<td><i>" . $author . "</i></td>\n" .
5331 "<td>" .
5332 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
5333 -class => "list subject"},
5334 chop_and_escape_str($co{'title'}, 50) . "<br/>");
5335 while (my $setref = shift @files) {
5336 my %set = %$setref;
5337 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
5338 hash=>$set{'id'}, file_name=>$set{'file'}),
5339 -class => "list"},
5340 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
5341 "<br/>\n";
5342 }
5343 print "</td>\n" .
5344 "<td class=\"link\">" .
5345 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5346 " | " .
5347 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5348 print "</td>\n" .
5349 "</tr>\n";
5350 }
5351 %co = parse_commit($1);
5352 }
5353 }
5354 close $fd;
5355
5356 print "</table>\n";
5357 }
5358
5359 if ($searchtype eq 'grep') {
5360 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5361 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5362
5363 print "<table class=\"grep_search\">\n";
5364 my $alternate = 1;
5365 my $matches = 0;
5366 $/ = "\n";
5367 open my $fd, "-|", git_cmd(), 'grep', '-n', '-i', '-E', $searchtext, $co{'tree'};
5368 my $lastfile = '';
5369 while (my $line = <$fd>) {
5370 chomp $line;
5371 my ($file, $lno, $ltext, $binary);
5372 last if ($matches++ > 1000);
5373 if ($line =~ /^Binary file (.+) matches$/) {
5374 $file = $1;
5375 $binary = 1;
5376 } else {
5377 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
5378 }
5379 if ($file ne $lastfile) {
5380 $lastfile and print "</td></tr>\n";
5381 if ($alternate++) {
5382 print "<tr class=\"dark\">\n";
5383 } else {
5384 print "<tr class=\"light\">\n";
5385 }
5386 print "<td class=\"list\">".
5387 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5388 file_name=>"$file"),
5389 -class => "list"}, esc_path($file));
5390 print "</td><td>\n";
5391 $lastfile = $file;
5392 }
5393 if ($binary) {
5394 print "<div class=\"binary\">Binary file</div>\n";
5395 } else {
5396 $ltext = untabify($ltext);
5397 if ($ltext =~ m/^(.*)($searchtext)(.*)$/i) {
5398 $ltext = esc_html($1, -nbsp=>1);
5399 $ltext .= '<span class="match">';
5400 $ltext .= esc_html($2, -nbsp=>1);
5401 $ltext .= '</span>';
5402 $ltext .= esc_html($3, -nbsp=>1);
5403 } else {
5404 $ltext = esc_html($ltext, -nbsp=>1);
5405 }
5406 print "<div class=\"pre\">" .
5407 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5408 file_name=>"$file").'#l'.$lno,
5409 -class => "linenr"}, sprintf('%4i', $lno))
5410 . ' ' . $ltext . "</div>\n";
5411 }
5412 }
5413 if ($lastfile) {
5414 print "</td></tr>\n";
5415 if ($matches > 1000) {
5416 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
5417 }
5418 } else {
5419 print "<div class=\"diff nodifferences\">No matches found</div>\n";
5420 }
5421 close $fd;
5422
5423 print "</table>\n";
5424 }
5425 git_footer_html();
5426 }
5427
5428 sub git_search_help {
5429 git_header_html();
5430 git_print_page_nav('','', $hash,$hash,$hash);
5431 print <<EOT;
5432 <dl>
5433 <dt><b>commit</b></dt>
5434 <dd>The commit messages and authorship information will be scanned for the given string.</dd>
5435 EOT
5436 my ($have_grep) = gitweb_check_feature('grep');
5437 if ($have_grep) {
5438 print <<EOT;
5439 <dt><b>grep</b></dt>
5440 <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
5441 a different one) are searched for the given
5442 <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a>
5443 (POSIX extended) and the matches are listed. On large
5444 trees, this search can take a while and put some strain on the server, so please use it with
5445 some consideration.</dd>
5446 EOT
5447 }
5448 print <<EOT;
5449 <dt><b>author</b></dt>
5450 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given string.</dd>
5451 <dt><b>committer</b></dt>
5452 <dd>Name and e-mail of the committer and date of commit will be scanned for the given string.</dd>
5453 EOT
5454 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
5455 if ($have_pickaxe) {
5456 print <<EOT;
5457 <dt><b>pickaxe</b></dt>
5458 <dd>All commits that caused the string to appear or disappear from any file (changes that
5459 added, removed or "modified" the string) will be listed. This search can take a while and
5460 takes a lot of strain on the server, so please use it wisely.</dd>
5461 EOT
5462 }
5463 print "</dl>\n";
5464 git_footer_html();
5465 }
5466
5467 sub git_shortlog {
5468 my $head = git_get_head_hash($project);
5469 if (!defined $hash) {
5470 $hash = $head;
5471 }
5472 if (!defined $page) {
5473 $page = 0;
5474 }
5475 my $refs = git_get_references();
5476
5477 my @commitlist = parse_commits($hash, 101, (100 * $page));
5478
5479 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, (100 * ($page+1)));
5480 my $next_link = '';
5481 if ($#commitlist >= 100) {
5482 $next_link =
5483 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5484 -accesskey => "n", -title => "Alt-n"}, "next");
5485 }
5486
5487 git_header_html();
5488 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
5489 git_print_header_div('summary', $project);
5490
5491 git_shortlog_body(\@commitlist, 0, 99, $refs, $next_link);
5492
5493 git_footer_html();
5494 }
5495
5496 ## ......................................................................
5497 ## feeds (RSS, Atom; OPML)
5498
5499 sub git_feed {
5500 my $format = shift || 'atom';
5501 my ($have_blame) = gitweb_check_feature('blame');
5502
5503 # Atom: http://www.atomenabled.org/developers/syndication/
5504 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
5505 if ($format ne 'rss' && $format ne 'atom') {
5506 die_error(undef, "Unknown web feed format");
5507 }
5508
5509 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
5510 my $head = $hash || 'HEAD';
5511 my @commitlist = parse_commits($head, 150, 0, $file_name);
5512
5513 my %latest_commit;
5514 my %latest_date;
5515 my $content_type = "application/$format+xml";
5516 if (defined $cgi->http('HTTP_ACCEPT') &&
5517 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
5518 # browser (feed reader) prefers text/xml
5519 $content_type = 'text/xml';
5520 }
5521 if (defined($commitlist[0])) {
5522 %latest_commit = %{$commitlist[0]};
5523 %latest_date = parse_date($latest_commit{'author_epoch'});
5524 print $cgi->header(
5525 -type => $content_type,
5526 -charset => 'utf-8',
5527 -last_modified => $latest_date{'rfc2822'});
5528 } else {
5529 print $cgi->header(
5530 -type => $content_type,
5531 -charset => 'utf-8');
5532 }
5533
5534 # Optimization: skip generating the body if client asks only
5535 # for Last-Modified date.
5536 return if ($cgi->request_method() eq 'HEAD');
5537
5538 # header variables
5539 my $title = "$site_name - $project/$action";
5540 my $feed_type = 'log';
5541 if (defined $hash) {
5542 $title .= " - '$hash'";
5543 $feed_type = 'branch log';
5544 if (defined $file_name) {
5545 $title .= " :: $file_name";
5546 $feed_type = 'history';
5547 }
5548 } elsif (defined $file_name) {
5549 $title .= " - $file_name";
5550 $feed_type = 'history';
5551 }
5552 $title .= " $feed_type";
5553 my $descr = git_get_project_description($project);
5554 if (defined $descr) {
5555 $descr = esc_html($descr);
5556 } else {
5557 $descr = "$project " .
5558 ($format eq 'rss' ? 'RSS' : 'Atom') .
5559 " feed";
5560 }
5561 my $owner = git_get_project_owner($project);
5562 $owner = esc_html($owner);
5563
5564 #header
5565 my $alt_url;
5566 if (defined $file_name) {
5567 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
5568 } elsif (defined $hash) {
5569 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
5570 } else {
5571 $alt_url = href(-full=>1, action=>"summary");
5572 }
5573 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
5574 if ($format eq 'rss') {
5575 print <<XML;
5576 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
5577 <channel>
5578 XML
5579 print "<title>$title</title>\n" .
5580 "<link>$alt_url</link>\n" .
5581 "<description>$descr</description>\n" .
5582 "<language>en</language>\n";
5583 } elsif ($format eq 'atom') {
5584 print <<XML;
5585 <feed xmlns="http://www.w3.org/2005/Atom">
5586 XML
5587 print "<title>$title</title>\n" .
5588 "<subtitle>$descr</subtitle>\n" .
5589 '<link rel="alternate" type="text/html" href="' .
5590 $alt_url . '" />' . "\n" .
5591 '<link rel="self" type="' . $content_type . '" href="' .
5592 $cgi->self_url() . '" />' . "\n" .
5593 "<id>" . href(-full=>1) . "</id>\n" .
5594 # use project owner for feed author
5595 "<author><name>$owner</name></author>\n";
5596 if (defined $favicon) {
5597 print "<icon>" . esc_url($favicon) . "</icon>\n";
5598 }
5599 if (defined $logo_url) {
5600 # not twice as wide as tall: 72 x 27 pixels
5601 print "<logo>" . esc_url($logo) . "</logo>\n";
5602 }
5603 if (! %latest_date) {
5604 # dummy date to keep the feed valid until commits trickle in:
5605 print "<updated>1970-01-01T00:00:00Z</updated>\n";
5606 } else {
5607 print "<updated>$latest_date{'iso-8601'}</updated>\n";
5608 }
5609 }
5610
5611 # contents
5612 for (my $i = 0; $i <= $#commitlist; $i++) {
5613 my %co = %{$commitlist[$i]};
5614 my $commit = $co{'id'};
5615 # we read 150, we always show 30 and the ones more recent than 48 hours
5616 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
5617 last;
5618 }
5619 my %cd = parse_date($co{'author_epoch'});
5620
5621 # get list of changed files
5622 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5623 $co{'parent'} || "--root",
5624 $co{'id'}, "--", (defined $file_name ? $file_name : ())
5625 or next;
5626 my @difftree = map { chomp; $_ } <$fd>;
5627 close $fd
5628 or next;
5629
5630 # print element (entry, item)
5631 my $co_url = href(-full=>1, action=>"commitdiff", hash=>$commit);
5632 if ($format eq 'rss') {
5633 print "<item>\n" .
5634 "<title>" . esc_html($co{'title'}) . "</title>\n" .
5635 "<author>" . esc_html($co{'author'}) . "</author>\n" .
5636 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
5637 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
5638 "<link>$co_url</link>\n" .
5639 "<description>" . esc_html($co{'title'}) . "</description>\n" .
5640 "<content:encoded>" .
5641 "<![CDATA[\n";
5642 } elsif ($format eq 'atom') {
5643 print "<entry>\n" .
5644 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
5645 "<updated>$cd{'iso-8601'}</updated>\n" .
5646 "<author>\n" .
5647 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
5648 if ($co{'author_email'}) {
5649 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
5650 }
5651 print "</author>\n" .
5652 # use committer for contributor
5653 "<contributor>\n" .
5654 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
5655 if ($co{'committer_email'}) {
5656 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
5657 }
5658 print "</contributor>\n" .
5659 "<published>$cd{'iso-8601'}</published>\n" .
5660 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
5661 "<id>$co_url</id>\n" .
5662 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
5663 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
5664 }
5665 my $comment = $co{'comment'};
5666 print "<pre>\n";
5667 foreach my $line (@$comment) {
5668 $line = esc_html($line);
5669 print "$line\n";
5670 }
5671 print "</pre><ul>\n";
5672 foreach my $difftree_line (@difftree) {
5673 my %difftree = parse_difftree_raw_line($difftree_line);
5674 next if !$difftree{'from_id'};
5675
5676 my $file = $difftree{'file'} || $difftree{'to_file'};
5677
5678 print "<li>" .
5679 "[" .
5680 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
5681 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
5682 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
5683 file_name=>$file, file_parent=>$difftree{'from_file'}),
5684 -title => "diff"}, 'D');
5685 if ($have_blame) {
5686 print $cgi->a({-href => href(-full=>1, action=>"blame",
5687 file_name=>$file, hash_base=>$commit),
5688 -title => "blame"}, 'B');
5689 }
5690 # if this is not a feed of a file history
5691 if (!defined $file_name || $file_name ne $file) {
5692 print $cgi->a({-href => href(-full=>1, action=>"history",
5693 file_name=>$file, hash=>$commit),
5694 -title => "history"}, 'H');
5695 }
5696 $file = esc_path($file);
5697 print "] ".
5698 "$file</li>\n";
5699 }
5700 if ($format eq 'rss') {
5701 print "</ul>]]>\n" .
5702 "</content:encoded>\n" .
5703 "</item>\n";
5704 } elsif ($format eq 'atom') {
5705 print "</ul>\n</div>\n" .
5706 "</content>\n" .
5707 "</entry>\n";
5708 }
5709 }
5710
5711 # end of feed
5712 if ($format eq 'rss') {
5713 print "</channel>\n</rss>\n";
5714 } elsif ($format eq 'atom') {
5715 print "</feed>\n";
5716 }
5717 }
5718
5719 sub git_rss {
5720 git_feed('rss');
5721 }
5722
5723 sub git_atom {
5724 git_feed('atom');
5725 }
5726
5727 sub git_opml {
5728 my @list = git_get_projects_list();
5729
5730 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
5731 print <<XML;
5732 <?xml version="1.0" encoding="utf-8"?>
5733 <opml version="1.0">
5734 <head>
5735 <title>$site_name OPML Export</title>
5736 </head>
5737 <body>
5738 <outline text="git RSS feeds">
5739 XML
5740
5741 foreach my $pr (@list) {
5742 my %proj = %$pr;
5743 my $head = git_get_head_hash($proj{'path'});
5744 if (!defined $head) {
5745 next;
5746 }
5747 $git_dir = "$projectroot/$proj{'path'}";
5748 my %co = parse_commit($head);
5749 if (!%co) {
5750 next;
5751 }
5752
5753 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
5754 my $rss = "$my_url?p=$proj{'path'};a=rss";
5755 my $html = "$my_url?p=$proj{'path'};a=summary";
5756 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
5757 }
5758 print <<XML;
5759 </outline>
5760 </body>
5761 </opml>
5762 XML
5763 }
This page took 5.238402 seconds and 3 git commands to generate.