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