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