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