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