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