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