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