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