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