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