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