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