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