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