]> Lady’s Gitweb - Gitweb/blob - gitweb.perl
gitweb: make "No commits" in project list gray, not bold green
[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 return (undef, undef);
1531 }
1532
1533 sub git_get_references {
1534 my $type = shift || "";
1535 my %refs;
1536 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
1537 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
1538 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
1539 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
1540 or return;
1541
1542 while (my $line = <$fd>) {
1543 chomp $line;
1544 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type/?[^^]+)!) {
1545 if (defined $refs{$1}) {
1546 push @{$refs{$1}}, $2;
1547 } else {
1548 $refs{$1} = [ $2 ];
1549 }
1550 }
1551 }
1552 close $fd or return;
1553 return \%refs;
1554 }
1555
1556 sub git_get_rev_name_tags {
1557 my $hash = shift || return undef;
1558
1559 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
1560 or return;
1561 my $name_rev = <$fd>;
1562 close $fd;
1563
1564 if ($name_rev =~ m|^$hash tags/(.*)$|) {
1565 return $1;
1566 } else {
1567 # catches also '$hash undefined' output
1568 return undef;
1569 }
1570 }
1571
1572 ## ----------------------------------------------------------------------
1573 ## parse to hash functions
1574
1575 sub parse_date {
1576 my $epoch = shift;
1577 my $tz = shift || "-0000";
1578
1579 my %date;
1580 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
1581 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
1582 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
1583 $date{'hour'} = $hour;
1584 $date{'minute'} = $min;
1585 $date{'mday'} = $mday;
1586 $date{'day'} = $days[$wday];
1587 $date{'month'} = $months[$mon];
1588 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
1589 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
1590 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
1591 $mday, $months[$mon], $hour ,$min;
1592 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
1593 1900+$year, $mon, $mday, $hour ,$min, $sec;
1594
1595 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
1596 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
1597 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
1598 $date{'hour_local'} = $hour;
1599 $date{'minute_local'} = $min;
1600 $date{'tz_local'} = $tz;
1601 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
1602 1900+$year, $mon+1, $mday,
1603 $hour, $min, $sec, $tz);
1604 return %date;
1605 }
1606
1607 sub parse_tag {
1608 my $tag_id = shift;
1609 my %tag;
1610 my @comment;
1611
1612 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
1613 $tag{'id'} = $tag_id;
1614 while (my $line = <$fd>) {
1615 chomp $line;
1616 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
1617 $tag{'object'} = $1;
1618 } elsif ($line =~ m/^type (.+)$/) {
1619 $tag{'type'} = $1;
1620 } elsif ($line =~ m/^tag (.+)$/) {
1621 $tag{'name'} = $1;
1622 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
1623 $tag{'author'} = $1;
1624 $tag{'epoch'} = $2;
1625 $tag{'tz'} = $3;
1626 } elsif ($line =~ m/--BEGIN/) {
1627 push @comment, $line;
1628 last;
1629 } elsif ($line eq "") {
1630 last;
1631 }
1632 }
1633 push @comment, <$fd>;
1634 $tag{'comment'} = \@comment;
1635 close $fd or return;
1636 if (!defined $tag{'name'}) {
1637 return
1638 };
1639 return %tag
1640 }
1641
1642 sub parse_commit_text {
1643 my ($commit_text, $withparents) = @_;
1644 my @commit_lines = split '\n', $commit_text;
1645 my %co;
1646
1647 pop @commit_lines; # Remove '\0'
1648
1649 if (! @commit_lines) {
1650 return;
1651 }
1652
1653 my $header = shift @commit_lines;
1654 if ($header !~ m/^[0-9a-fA-F]{40}/) {
1655 return;
1656 }
1657 ($co{'id'}, my @parents) = split ' ', $header;
1658 while (my $line = shift @commit_lines) {
1659 last if $line eq "\n";
1660 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
1661 $co{'tree'} = $1;
1662 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
1663 push @parents, $1;
1664 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
1665 $co{'author'} = $1;
1666 $co{'author_epoch'} = $2;
1667 $co{'author_tz'} = $3;
1668 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
1669 $co{'author_name'} = $1;
1670 $co{'author_email'} = $2;
1671 } else {
1672 $co{'author_name'} = $co{'author'};
1673 }
1674 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
1675 $co{'committer'} = $1;
1676 $co{'committer_epoch'} = $2;
1677 $co{'committer_tz'} = $3;
1678 $co{'committer_name'} = $co{'committer'};
1679 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
1680 $co{'committer_name'} = $1;
1681 $co{'committer_email'} = $2;
1682 } else {
1683 $co{'committer_name'} = $co{'committer'};
1684 }
1685 }
1686 }
1687 if (!defined $co{'tree'}) {
1688 return;
1689 };
1690 $co{'parents'} = \@parents;
1691 $co{'parent'} = $parents[0];
1692
1693 foreach my $title (@commit_lines) {
1694 $title =~ s/^ //;
1695 if ($title ne "") {
1696 $co{'title'} = chop_str($title, 80, 5);
1697 # remove leading stuff of merges to make the interesting part visible
1698 if (length($title) > 50) {
1699 $title =~ s/^Automatic //;
1700 $title =~ s/^merge (of|with) /Merge ... /i;
1701 if (length($title) > 50) {
1702 $title =~ s/(http|rsync):\/\///;
1703 }
1704 if (length($title) > 50) {
1705 $title =~ s/(master|www|rsync)\.//;
1706 }
1707 if (length($title) > 50) {
1708 $title =~ s/kernel.org:?//;
1709 }
1710 if (length($title) > 50) {
1711 $title =~ s/\/pub\/scm//;
1712 }
1713 }
1714 $co{'title_short'} = chop_str($title, 50, 5);
1715 last;
1716 }
1717 }
1718 if ($co{'title'} eq "") {
1719 $co{'title'} = $co{'title_short'} = '(no commit message)';
1720 }
1721 # remove added spaces
1722 foreach my $line (@commit_lines) {
1723 $line =~ s/^ //;
1724 }
1725 $co{'comment'} = \@commit_lines;
1726
1727 my $age = time - $co{'committer_epoch'};
1728 $co{'age'} = $age;
1729 $co{'age_string'} = age_string($age);
1730 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
1731 if ($age > 60*60*24*7*2) {
1732 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1733 $co{'age_string_age'} = $co{'age_string'};
1734 } else {
1735 $co{'age_string_date'} = $co{'age_string'};
1736 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1737 }
1738 return %co;
1739 }
1740
1741 sub parse_commit {
1742 my ($commit_id) = @_;
1743 my %co;
1744
1745 local $/ = "\0";
1746
1747 open my $fd, "-|", git_cmd(), "rev-list",
1748 "--parents",
1749 "--header",
1750 "--max-count=1",
1751 $commit_id,
1752 "--",
1753 or die_error(undef, "Open git-rev-list failed");
1754 %co = parse_commit_text(<$fd>, 1);
1755 close $fd;
1756
1757 return %co;
1758 }
1759
1760 sub parse_commits {
1761 my ($commit_id, $maxcount, $skip, $arg, $filename) = @_;
1762 my @cos;
1763
1764 $maxcount ||= 1;
1765 $skip ||= 0;
1766
1767 local $/ = "\0";
1768
1769 open my $fd, "-|", git_cmd(), "rev-list",
1770 "--header",
1771 ($arg ? ($arg) : ()),
1772 ("--max-count=" . $maxcount),
1773 ("--skip=" . $skip),
1774 $commit_id,
1775 "--",
1776 ($filename ? ($filename) : ())
1777 or die_error(undef, "Open git-rev-list failed");
1778 while (my $line = <$fd>) {
1779 my %co = parse_commit_text($line);
1780 push @cos, \%co;
1781 }
1782 close $fd;
1783
1784 return wantarray ? @cos : \@cos;
1785 }
1786
1787 # parse ref from ref_file, given by ref_id, with given type
1788 sub parse_ref {
1789 my $ref_file = shift;
1790 my $ref_id = shift;
1791 my $type = shift || git_get_type($ref_id);
1792 my %ref_item;
1793
1794 $ref_item{'type'} = $type;
1795 $ref_item{'id'} = $ref_id;
1796 $ref_item{'epoch'} = 0;
1797 $ref_item{'age'} = "unknown";
1798 if ($type eq "tag") {
1799 my %tag = parse_tag($ref_id);
1800 $ref_item{'comment'} = $tag{'comment'};
1801 if ($tag{'type'} eq "commit") {
1802 my %co = parse_commit($tag{'object'});
1803 $ref_item{'epoch'} = $co{'committer_epoch'};
1804 $ref_item{'age'} = $co{'age_string'};
1805 } elsif (defined($tag{'epoch'})) {
1806 my $age = time - $tag{'epoch'};
1807 $ref_item{'epoch'} = $tag{'epoch'};
1808 $ref_item{'age'} = age_string($age);
1809 }
1810 $ref_item{'reftype'} = $tag{'type'};
1811 $ref_item{'name'} = $tag{'name'};
1812 $ref_item{'refid'} = $tag{'object'};
1813 } elsif ($type eq "commit"){
1814 my %co = parse_commit($ref_id);
1815 $ref_item{'reftype'} = "commit";
1816 $ref_item{'name'} = $ref_file;
1817 $ref_item{'title'} = $co{'title'};
1818 $ref_item{'refid'} = $ref_id;
1819 $ref_item{'epoch'} = $co{'committer_epoch'};
1820 $ref_item{'age'} = $co{'age_string'};
1821 } else {
1822 $ref_item{'reftype'} = $type;
1823 $ref_item{'name'} = $ref_file;
1824 $ref_item{'refid'} = $ref_id;
1825 }
1826
1827 return %ref_item;
1828 }
1829
1830 # parse line of git-diff-tree "raw" output
1831 sub parse_difftree_raw_line {
1832 my $line = shift;
1833 my %res;
1834
1835 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1836 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
1837 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1838 $res{'from_mode'} = $1;
1839 $res{'to_mode'} = $2;
1840 $res{'from_id'} = $3;
1841 $res{'to_id'} = $4;
1842 $res{'status'} = $5;
1843 $res{'similarity'} = $6;
1844 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
1845 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
1846 } else {
1847 $res{'file'} = unquote($7);
1848 }
1849 }
1850 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
1851 # combined diff (for merge commit)
1852 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
1853 $res{'nparents'} = length($1);
1854 $res{'from_mode'} = [ split(' ', $2) ];
1855 $res{'to_mode'} = pop @{$res{'from_mode'}};
1856 $res{'from_id'} = [ split(' ', $3) ];
1857 $res{'to_id'} = pop @{$res{'from_id'}};
1858 $res{'status'} = [ split('', $4) ];
1859 $res{'to_file'} = unquote($5);
1860 }
1861 # 'c512b523472485aef4fff9e57b229d9d243c967f'
1862 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
1863 $res{'commit'} = $1;
1864 }
1865
1866 return wantarray ? %res : \%res;
1867 }
1868
1869 # parse line of git-ls-tree output
1870 sub parse_ls_tree_line ($;%) {
1871 my $line = shift;
1872 my %opts = @_;
1873 my %res;
1874
1875 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1876 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
1877
1878 $res{'mode'} = $1;
1879 $res{'type'} = $2;
1880 $res{'hash'} = $3;
1881 if ($opts{'-z'}) {
1882 $res{'name'} = $4;
1883 } else {
1884 $res{'name'} = unquote($4);
1885 }
1886
1887 return wantarray ? %res : \%res;
1888 }
1889
1890 # generates _two_ hashes, references to which are passed as 2 and 3 argument
1891 sub parse_from_to_diffinfo {
1892 my ($diffinfo, $from, $to, @parents) = @_;
1893
1894 if ($diffinfo->{'nparents'}) {
1895 # combined diff
1896 $from->{'file'} = [];
1897 $from->{'href'} = [];
1898 fill_from_file_info($diffinfo, @parents)
1899 unless exists $diffinfo->{'from_file'};
1900 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1901 $from->{'file'}[$i] = $diffinfo->{'from_file'}[$i] || $diffinfo->{'to_file'};
1902 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
1903 $from->{'href'}[$i] = href(action=>"blob",
1904 hash_base=>$parents[$i],
1905 hash=>$diffinfo->{'from_id'}[$i],
1906 file_name=>$from->{'file'}[$i]);
1907 } else {
1908 $from->{'href'}[$i] = undef;
1909 }
1910 }
1911 } else {
1912 $from->{'file'} = $diffinfo->{'from_file'} || $diffinfo->{'file'};
1913 if ($diffinfo->{'status'} ne "A") { # not new (added) file
1914 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
1915 hash=>$diffinfo->{'from_id'},
1916 file_name=>$from->{'file'});
1917 } else {
1918 delete $from->{'href'};
1919 }
1920 }
1921
1922 $to->{'file'} = $diffinfo->{'to_file'} || $diffinfo->{'file'};
1923 if (!is_deleted($diffinfo)) { # file exists in result
1924 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
1925 hash=>$diffinfo->{'to_id'},
1926 file_name=>$to->{'file'});
1927 } else {
1928 delete $to->{'href'};
1929 }
1930 }
1931
1932 ## ......................................................................
1933 ## parse to array of hashes functions
1934
1935 sub git_get_heads_list {
1936 my $limit = shift;
1937 my @headslist;
1938
1939 open my $fd, '-|', git_cmd(), 'for-each-ref',
1940 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
1941 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
1942 'refs/heads'
1943 or return;
1944 while (my $line = <$fd>) {
1945 my %ref_item;
1946
1947 chomp $line;
1948 my ($refinfo, $committerinfo) = split(/\0/, $line);
1949 my ($hash, $name, $title) = split(' ', $refinfo, 3);
1950 my ($committer, $epoch, $tz) =
1951 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
1952 $name =~ s!^refs/heads/!!;
1953
1954 $ref_item{'name'} = $name;
1955 $ref_item{'id'} = $hash;
1956 $ref_item{'title'} = $title || '(no commit message)';
1957 $ref_item{'epoch'} = $epoch;
1958 if ($epoch) {
1959 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
1960 } else {
1961 $ref_item{'age'} = "unknown";
1962 }
1963
1964 push @headslist, \%ref_item;
1965 }
1966 close $fd;
1967
1968 return wantarray ? @headslist : \@headslist;
1969 }
1970
1971 sub git_get_tags_list {
1972 my $limit = shift;
1973 my @tagslist;
1974
1975 open my $fd, '-|', git_cmd(), 'for-each-ref',
1976 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
1977 '--format=%(objectname) %(objecttype) %(refname) '.
1978 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
1979 'refs/tags'
1980 or return;
1981 while (my $line = <$fd>) {
1982 my %ref_item;
1983
1984 chomp $line;
1985 my ($refinfo, $creatorinfo) = split(/\0/, $line);
1986 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
1987 my ($creator, $epoch, $tz) =
1988 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
1989 $name =~ s!^refs/tags/!!;
1990
1991 $ref_item{'type'} = $type;
1992 $ref_item{'id'} = $id;
1993 $ref_item{'name'} = $name;
1994 if ($type eq "tag") {
1995 $ref_item{'subject'} = $title;
1996 $ref_item{'reftype'} = $reftype;
1997 $ref_item{'refid'} = $refid;
1998 } else {
1999 $ref_item{'reftype'} = $type;
2000 $ref_item{'refid'} = $id;
2001 }
2002
2003 if ($type eq "tag" || $type eq "commit") {
2004 $ref_item{'epoch'} = $epoch;
2005 if ($epoch) {
2006 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2007 } else {
2008 $ref_item{'age'} = "unknown";
2009 }
2010 }
2011
2012 push @tagslist, \%ref_item;
2013 }
2014 close $fd;
2015
2016 return wantarray ? @tagslist : \@tagslist;
2017 }
2018
2019 ## ----------------------------------------------------------------------
2020 ## filesystem-related functions
2021
2022 sub get_file_owner {
2023 my $path = shift;
2024
2025 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
2026 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
2027 if (!defined $gcos) {
2028 return undef;
2029 }
2030 my $owner = $gcos;
2031 $owner =~ s/[,;].*$//;
2032 return to_utf8($owner);
2033 }
2034
2035 ## ......................................................................
2036 ## mimetype related functions
2037
2038 sub mimetype_guess_file {
2039 my $filename = shift;
2040 my $mimemap = shift;
2041 -r $mimemap or return undef;
2042
2043 my %mimemap;
2044 open(MIME, $mimemap) or return undef;
2045 while (<MIME>) {
2046 next if m/^#/; # skip comments
2047 my ($mime, $exts) = split(/\t+/);
2048 if (defined $exts) {
2049 my @exts = split(/\s+/, $exts);
2050 foreach my $ext (@exts) {
2051 $mimemap{$ext} = $mime;
2052 }
2053 }
2054 }
2055 close(MIME);
2056
2057 $filename =~ /\.([^.]*)$/;
2058 return $mimemap{$1};
2059 }
2060
2061 sub mimetype_guess {
2062 my $filename = shift;
2063 my $mime;
2064 $filename =~ /\./ or return undef;
2065
2066 if ($mimetypes_file) {
2067 my $file = $mimetypes_file;
2068 if ($file !~ m!^/!) { # if it is relative path
2069 # it is relative to project
2070 $file = "$projectroot/$project/$file";
2071 }
2072 $mime = mimetype_guess_file($filename, $file);
2073 }
2074 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
2075 return $mime;
2076 }
2077
2078 sub blob_mimetype {
2079 my $fd = shift;
2080 my $filename = shift;
2081
2082 if ($filename) {
2083 my $mime = mimetype_guess($filename);
2084 $mime and return $mime;
2085 }
2086
2087 # just in case
2088 return $default_blob_plain_mimetype unless $fd;
2089
2090 if (-T $fd) {
2091 return 'text/plain' .
2092 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
2093 } elsif (! $filename) {
2094 return 'application/octet-stream';
2095 } elsif ($filename =~ m/\.png$/i) {
2096 return 'image/png';
2097 } elsif ($filename =~ m/\.gif$/i) {
2098 return 'image/gif';
2099 } elsif ($filename =~ m/\.jpe?g$/i) {
2100 return 'image/jpeg';
2101 } else {
2102 return 'application/octet-stream';
2103 }
2104 }
2105
2106 ## ======================================================================
2107 ## functions printing HTML: header, footer, error page
2108
2109 sub git_header_html {
2110 my $status = shift || "200 OK";
2111 my $expires = shift;
2112
2113 my $title = "$site_name";
2114 if (defined $project) {
2115 $title .= " - " . to_utf8($project);
2116 if (defined $action) {
2117 $title .= "/$action";
2118 if (defined $file_name) {
2119 $title .= " - " . esc_path($file_name);
2120 if ($action eq "tree" && $file_name !~ m|/$|) {
2121 $title .= "/";
2122 }
2123 }
2124 }
2125 }
2126 my $content_type;
2127 # require explicit support from the UA if we are to send the page as
2128 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
2129 # we have to do this because MSIE sometimes globs '*/*', pretending to
2130 # support xhtml+xml but choking when it gets what it asked for.
2131 if (defined $cgi->http('HTTP_ACCEPT') &&
2132 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
2133 $cgi->Accept('application/xhtml+xml') != 0) {
2134 $content_type = 'application/xhtml+xml';
2135 } else {
2136 $content_type = 'text/html';
2137 }
2138 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
2139 -status=> $status, -expires => $expires);
2140 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
2141 print <<EOF;
2142 <?xml version="1.0" encoding="utf-8"?>
2143 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2144 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
2145 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
2146 <!-- git core binaries version $git_version -->
2147 <head>
2148 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
2149 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
2150 <meta name="robots" content="index, nofollow"/>
2151 <title>$title</title>
2152 EOF
2153 # print out each stylesheet that exist
2154 if (defined $stylesheet) {
2155 #provides backwards capability for those people who define style sheet in a config file
2156 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2157 } else {
2158 foreach my $stylesheet (@stylesheets) {
2159 next unless $stylesheet;
2160 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2161 }
2162 }
2163 if (defined $project) {
2164 printf('<link rel="alternate" title="%s log RSS feed" '.
2165 'href="%s" type="application/rss+xml" />'."\n",
2166 esc_param($project), href(action=>"rss"));
2167 printf('<link rel="alternate" title="%s log Atom feed" '.
2168 'href="%s" type="application/atom+xml" />'."\n",
2169 esc_param($project), href(action=>"atom"));
2170 } else {
2171 printf('<link rel="alternate" title="%s projects list" '.
2172 'href="%s" type="text/plain; charset=utf-8"/>'."\n",
2173 $site_name, href(project=>undef, action=>"project_index"));
2174 printf('<link rel="alternate" title="%s projects feeds" '.
2175 'href="%s" type="text/x-opml"/>'."\n",
2176 $site_name, href(project=>undef, action=>"opml"));
2177 }
2178 if (defined $favicon) {
2179 print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n);
2180 }
2181
2182 print "</head>\n" .
2183 "<body>\n";
2184
2185 if (-f $site_header) {
2186 open (my $fd, $site_header);
2187 print <$fd>;
2188 close $fd;
2189 }
2190
2191 print "<div class=\"page_header\">\n" .
2192 $cgi->a({-href => esc_url($logo_url),
2193 -title => $logo_label},
2194 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
2195 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
2196 if (defined $project) {
2197 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
2198 if (defined $action) {
2199 print " / $action";
2200 }
2201 print "\n";
2202 }
2203 print "</div>\n";
2204
2205 my ($have_search) = gitweb_check_feature('search');
2206 if ((defined $project) && ($have_search)) {
2207 if (!defined $searchtext) {
2208 $searchtext = "";
2209 }
2210 my $search_hash;
2211 if (defined $hash_base) {
2212 $search_hash = $hash_base;
2213 } elsif (defined $hash) {
2214 $search_hash = $hash;
2215 } else {
2216 $search_hash = "HEAD";
2217 }
2218 my $action = $my_uri;
2219 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
2220 if ($use_pathinfo) {
2221 $action .= "/$project";
2222 } else {
2223 $cgi->param("p", $project);
2224 }
2225 $cgi->param("a", "search");
2226 $cgi->param("h", $search_hash);
2227 print $cgi->startform(-method => "get", -action => $action) .
2228 "<div class=\"search\">\n" .
2229 (!$use_pathinfo && $cgi->hidden(-name => "p") . "\n") .
2230 $cgi->hidden(-name => "a") . "\n" .
2231 $cgi->hidden(-name => "h") . "\n" .
2232 $cgi->popup_menu(-name => 'st', -default => 'commit',
2233 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
2234 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
2235 " search:\n",
2236 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
2237 "</div>" .
2238 $cgi->end_form() . "\n";
2239 }
2240 }
2241
2242 sub git_footer_html {
2243 print "<div class=\"page_footer\">\n";
2244 if (defined $project) {
2245 my $descr = git_get_project_description($project);
2246 if (defined $descr) {
2247 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
2248 }
2249 print $cgi->a({-href => href(action=>"rss"),
2250 -class => "rss_logo"}, "RSS") . " ";
2251 print $cgi->a({-href => href(action=>"atom"),
2252 -class => "rss_logo"}, "Atom") . "\n";
2253 } else {
2254 print $cgi->a({-href => href(project=>undef, action=>"opml"),
2255 -class => "rss_logo"}, "OPML") . " ";
2256 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
2257 -class => "rss_logo"}, "TXT") . "\n";
2258 }
2259 print "</div>\n" ;
2260
2261 if (-f $site_footer) {
2262 open (my $fd, $site_footer);
2263 print <$fd>;
2264 close $fd;
2265 }
2266
2267 print "</body>\n" .
2268 "</html>";
2269 }
2270
2271 sub die_error {
2272 my $status = shift || "403 Forbidden";
2273 my $error = shift || "Malformed query, file missing or permission denied";
2274
2275 git_header_html($status);
2276 print <<EOF;
2277 <div class="page_body">
2278 <br /><br />
2279 $status - $error
2280 <br />
2281 </div>
2282 EOF
2283 git_footer_html();
2284 exit;
2285 }
2286
2287 ## ----------------------------------------------------------------------
2288 ## functions printing or outputting HTML: navigation
2289
2290 sub git_print_page_nav {
2291 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
2292 $extra = '' if !defined $extra; # pager or formats
2293
2294 my @navs = qw(summary shortlog log commit commitdiff tree);
2295 if ($suppress) {
2296 @navs = grep { $_ ne $suppress } @navs;
2297 }
2298
2299 my %arg = map { $_ => {action=>$_} } @navs;
2300 if (defined $head) {
2301 for (qw(commit commitdiff)) {
2302 $arg{$_}{'hash'} = $head;
2303 }
2304 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
2305 for (qw(shortlog log)) {
2306 $arg{$_}{'hash'} = $head;
2307 }
2308 }
2309 }
2310 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
2311 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
2312
2313 print "<div class=\"page_nav\">\n" .
2314 (join " | ",
2315 map { $_ eq $current ?
2316 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
2317 } @navs);
2318 print "<br/>\n$extra<br/>\n" .
2319 "</div>\n";
2320 }
2321
2322 sub format_paging_nav {
2323 my ($action, $hash, $head, $page, $nrevs) = @_;
2324 my $paging_nav;
2325
2326
2327 if ($hash ne $head || $page) {
2328 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
2329 } else {
2330 $paging_nav .= "HEAD";
2331 }
2332
2333 if ($page > 0) {
2334 $paging_nav .= " &sdot; " .
2335 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
2336 -accesskey => "p", -title => "Alt-p"}, "prev");
2337 } else {
2338 $paging_nav .= " &sdot; prev";
2339 }
2340
2341 if ($nrevs >= (100 * ($page+1)-1)) {
2342 $paging_nav .= " &sdot; " .
2343 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
2344 -accesskey => "n", -title => "Alt-n"}, "next");
2345 } else {
2346 $paging_nav .= " &sdot; next";
2347 }
2348
2349 return $paging_nav;
2350 }
2351
2352 ## ......................................................................
2353 ## functions printing or outputting HTML: div
2354
2355 sub git_print_header_div {
2356 my ($action, $title, $hash, $hash_base) = @_;
2357 my %args = ();
2358
2359 $args{'action'} = $action;
2360 $args{'hash'} = $hash if $hash;
2361 $args{'hash_base'} = $hash_base if $hash_base;
2362
2363 print "<div class=\"header\">\n" .
2364 $cgi->a({-href => href(%args), -class => "title"},
2365 $title ? $title : $action) .
2366 "\n</div>\n";
2367 }
2368
2369 #sub git_print_authorship (\%) {
2370 sub git_print_authorship {
2371 my $co = shift;
2372
2373 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
2374 print "<div class=\"author_date\">" .
2375 esc_html($co->{'author_name'}) .
2376 " [$ad{'rfc2822'}";
2377 if ($ad{'hour_local'} < 6) {
2378 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2379 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2380 } else {
2381 printf(" (%02d:%02d %s)",
2382 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2383 }
2384 print "]</div>\n";
2385 }
2386
2387 sub git_print_page_path {
2388 my $name = shift;
2389 my $type = shift;
2390 my $hb = shift;
2391
2392
2393 print "<div class=\"page_path\">";
2394 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
2395 -title => 'tree root'}, to_utf8("[$project]"));
2396 print " / ";
2397 if (defined $name) {
2398 my @dirname = split '/', $name;
2399 my $basename = pop @dirname;
2400 my $fullname = '';
2401
2402 foreach my $dir (@dirname) {
2403 $fullname .= ($fullname ? '/' : '') . $dir;
2404 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
2405 hash_base=>$hb),
2406 -title => $fullname}, esc_path($dir));
2407 print " / ";
2408 }
2409 if (defined $type && $type eq 'blob') {
2410 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
2411 hash_base=>$hb),
2412 -title => $name}, esc_path($basename));
2413 } elsif (defined $type && $type eq 'tree') {
2414 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
2415 hash_base=>$hb),
2416 -title => $name}, esc_path($basename));
2417 print " / ";
2418 } else {
2419 print esc_path($basename);
2420 }
2421 }
2422 print "<br/></div>\n";
2423 }
2424
2425 # sub git_print_log (\@;%) {
2426 sub git_print_log ($;%) {
2427 my $log = shift;
2428 my %opts = @_;
2429
2430 if ($opts{'-remove_title'}) {
2431 # remove title, i.e. first line of log
2432 shift @$log;
2433 }
2434 # remove leading empty lines
2435 while (defined $log->[0] && $log->[0] eq "") {
2436 shift @$log;
2437 }
2438
2439 # print log
2440 my $signoff = 0;
2441 my $empty = 0;
2442 foreach my $line (@$log) {
2443 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
2444 $signoff = 1;
2445 $empty = 0;
2446 if (! $opts{'-remove_signoff'}) {
2447 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
2448 next;
2449 } else {
2450 # remove signoff lines
2451 next;
2452 }
2453 } else {
2454 $signoff = 0;
2455 }
2456
2457 # print only one empty line
2458 # do not print empty line after signoff
2459 if ($line eq "") {
2460 next if ($empty || $signoff);
2461 $empty = 1;
2462 } else {
2463 $empty = 0;
2464 }
2465
2466 print format_log_line_html($line) . "<br/>\n";
2467 }
2468
2469 if ($opts{'-final_empty_line'}) {
2470 # end with single empty line
2471 print "<br/>\n" unless $empty;
2472 }
2473 }
2474
2475 # return link target (what link points to)
2476 sub git_get_link_target {
2477 my $hash = shift;
2478 my $link_target;
2479
2480 # read link
2481 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2482 or return;
2483 {
2484 local $/;
2485 $link_target = <$fd>;
2486 }
2487 close $fd
2488 or return;
2489
2490 return $link_target;
2491 }
2492
2493 # given link target, and the directory (basedir) the link is in,
2494 # return target of link relative to top directory (top tree);
2495 # return undef if it is not possible (including absolute links).
2496 sub normalize_link_target {
2497 my ($link_target, $basedir, $hash_base) = @_;
2498
2499 # we can normalize symlink target only if $hash_base is provided
2500 return unless $hash_base;
2501
2502 # absolute symlinks (beginning with '/') cannot be normalized
2503 return if (substr($link_target, 0, 1) eq '/');
2504
2505 # normalize link target to path from top (root) tree (dir)
2506 my $path;
2507 if ($basedir) {
2508 $path = $basedir . '/' . $link_target;
2509 } else {
2510 # we are in top (root) tree (dir)
2511 $path = $link_target;
2512 }
2513
2514 # remove //, /./, and /../
2515 my @path_parts;
2516 foreach my $part (split('/', $path)) {
2517 # discard '.' and ''
2518 next if (!$part || $part eq '.');
2519 # handle '..'
2520 if ($part eq '..') {
2521 if (@path_parts) {
2522 pop @path_parts;
2523 } else {
2524 # link leads outside repository (outside top dir)
2525 return;
2526 }
2527 } else {
2528 push @path_parts, $part;
2529 }
2530 }
2531 $path = join('/', @path_parts);
2532
2533 return $path;
2534 }
2535
2536 # print tree entry (row of git_tree), but without encompassing <tr> element
2537 sub git_print_tree_entry {
2538 my ($t, $basedir, $hash_base, $have_blame) = @_;
2539
2540 my %base_key = ();
2541 $base_key{'hash_base'} = $hash_base if defined $hash_base;
2542
2543 # The format of a table row is: mode list link. Where mode is
2544 # the mode of the entry, list is the name of the entry, an href,
2545 # and link is the action links of the entry.
2546
2547 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
2548 if ($t->{'type'} eq "blob") {
2549 print "<td class=\"list\">" .
2550 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
2551 file_name=>"$basedir$t->{'name'}", %base_key),
2552 -class => "list"}, esc_path($t->{'name'}));
2553 if (S_ISLNK(oct $t->{'mode'})) {
2554 my $link_target = git_get_link_target($t->{'hash'});
2555 if ($link_target) {
2556 my $norm_target = normalize_link_target($link_target, $basedir, $hash_base);
2557 if (defined $norm_target) {
2558 print " -> " .
2559 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
2560 file_name=>$norm_target),
2561 -title => $norm_target}, esc_path($link_target));
2562 } else {
2563 print " -> " . esc_path($link_target);
2564 }
2565 }
2566 }
2567 print "</td>\n";
2568 print "<td class=\"link\">";
2569 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
2570 file_name=>"$basedir$t->{'name'}", %base_key)},
2571 "blob");
2572 if ($have_blame) {
2573 print " | " .
2574 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
2575 file_name=>"$basedir$t->{'name'}", %base_key)},
2576 "blame");
2577 }
2578 if (defined $hash_base) {
2579 print " | " .
2580 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2581 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
2582 "history");
2583 }
2584 print " | " .
2585 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
2586 file_name=>"$basedir$t->{'name'}")},
2587 "raw");
2588 print "</td>\n";
2589
2590 } elsif ($t->{'type'} eq "tree") {
2591 print "<td class=\"list\">";
2592 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
2593 file_name=>"$basedir$t->{'name'}", %base_key)},
2594 esc_path($t->{'name'}));
2595 print "</td>\n";
2596 print "<td class=\"link\">";
2597 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
2598 file_name=>"$basedir$t->{'name'}", %base_key)},
2599 "tree");
2600 if (defined $hash_base) {
2601 print " | " .
2602 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2603 file_name=>"$basedir$t->{'name'}")},
2604 "history");
2605 }
2606 print "</td>\n";
2607 }
2608 }
2609
2610 ## ......................................................................
2611 ## functions printing large fragments of HTML
2612
2613 sub fill_from_file_info {
2614 my ($diff, @parents) = @_;
2615
2616 $diff->{'from_file'} = [ ];
2617 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
2618 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
2619 if ($diff->{'status'}[$i] eq 'R' ||
2620 $diff->{'status'}[$i] eq 'C') {
2621 $diff->{'from_file'}[$i] =
2622 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
2623 }
2624 }
2625
2626 return $diff;
2627 }
2628
2629 # parameters can be strings, or references to arrays of strings
2630 sub from_ids_eq {
2631 my ($a, $b) = @_;
2632
2633 if (ref($a) eq "ARRAY" && ref($b) eq "ARRAY" && @$a == @$b) {
2634 for (my $i = 0; $i < @$a; ++$i) {
2635 return 0 unless ($a->[$i] eq $b->[$i]);
2636 }
2637 return 1;
2638 } elsif (!ref($a) && !ref($b)) {
2639 return $a eq $b;
2640 } else {
2641 return 0;
2642 }
2643 }
2644
2645 sub is_deleted {
2646 my $diffinfo = shift;
2647
2648 return $diffinfo->{'to_id'} eq ('0' x 40);
2649 }
2650
2651 sub git_difftree_body {
2652 my ($difftree, $hash, @parents) = @_;
2653 my ($parent) = $parents[0];
2654 my ($have_blame) = gitweb_check_feature('blame');
2655 print "<div class=\"list_head\">\n";
2656 if ($#{$difftree} > 10) {
2657 print(($#{$difftree} + 1) . " files changed:\n");
2658 }
2659 print "</div>\n";
2660
2661 print "<table class=\"" .
2662 (@parents > 1 ? "combined " : "") .
2663 "diff_tree\">\n";
2664
2665 # header only for combined diff in 'commitdiff' view
2666 my $has_header = @parents > 1 && $action eq 'commitdiff';
2667 if ($has_header) {
2668 # table header
2669 print "<thead><tr>\n" .
2670 "<th></th><th></th>\n"; # filename, patchN link
2671 for (my $i = 0; $i < @parents; $i++) {
2672 my $par = $parents[$i];
2673 print "<th>" .
2674 $cgi->a({-href => href(action=>"commitdiff",
2675 hash=>$hash, hash_parent=>$par),
2676 -title => 'commitdiff to parent number ' .
2677 ($i+1) . ': ' . substr($par,0,7)},
2678 $i+1) .
2679 "&nbsp;</th>\n";
2680 }
2681 print "</tr></thead>\n<tbody>\n";
2682 }
2683
2684 my $alternate = 1;
2685 my $patchno = 0;
2686 foreach my $line (@{$difftree}) {
2687 my $diff;
2688 if (ref($line) eq "HASH") {
2689 # pre-parsed (or generated by hand)
2690 $diff = $line;
2691 } else {
2692 $diff = parse_difftree_raw_line($line);
2693 }
2694
2695 if ($alternate) {
2696 print "<tr class=\"dark\">\n";
2697 } else {
2698 print "<tr class=\"light\">\n";
2699 }
2700 $alternate ^= 1;
2701
2702 if (exists $diff->{'nparents'}) { # combined diff
2703
2704 fill_from_file_info($diff, @parents)
2705 unless exists $diff->{'from_file'};
2706
2707 if (!is_deleted($diff)) {
2708 # file exists in the result (child) commit
2709 print "<td>" .
2710 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2711 file_name=>$diff->{'to_file'},
2712 hash_base=>$hash),
2713 -class => "list"}, esc_path($diff->{'to_file'})) .
2714 "</td>\n";
2715 } else {
2716 print "<td>" .
2717 esc_path($diff->{'to_file'}) .
2718 "</td>\n";
2719 }
2720
2721 if ($action eq 'commitdiff') {
2722 # link to patch
2723 $patchno++;
2724 print "<td class=\"link\">" .
2725 $cgi->a({-href => "#patch$patchno"}, "patch") .
2726 " | " .
2727 "</td>\n";
2728 }
2729
2730 my $has_history = 0;
2731 my $not_deleted = 0;
2732 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
2733 my $hash_parent = $parents[$i];
2734 my $from_hash = $diff->{'from_id'}[$i];
2735 my $from_path = $diff->{'from_file'}[$i];
2736 my $status = $diff->{'status'}[$i];
2737
2738 $has_history ||= ($status ne 'A');
2739 $not_deleted ||= ($status ne 'D');
2740
2741 if ($status eq 'A') {
2742 print "<td class=\"link\" align=\"right\"> | </td>\n";
2743 } elsif ($status eq 'D') {
2744 print "<td class=\"link\">" .
2745 $cgi->a({-href => href(action=>"blob",
2746 hash_base=>$hash,
2747 hash=>$from_hash,
2748 file_name=>$from_path)},
2749 "blob" . ($i+1)) .
2750 " | </td>\n";
2751 } else {
2752 if ($diff->{'to_id'} eq $from_hash) {
2753 print "<td class=\"link nochange\">";
2754 } else {
2755 print "<td class=\"link\">";
2756 }
2757 print $cgi->a({-href => href(action=>"blobdiff",
2758 hash=>$diff->{'to_id'},
2759 hash_parent=>$from_hash,
2760 hash_base=>$hash,
2761 hash_parent_base=>$hash_parent,
2762 file_name=>$diff->{'to_file'},
2763 file_parent=>$from_path)},
2764 "diff" . ($i+1)) .
2765 " | </td>\n";
2766 }
2767 }
2768
2769 print "<td class=\"link\">";
2770 if ($not_deleted) {
2771 print $cgi->a({-href => href(action=>"blob",
2772 hash=>$diff->{'to_id'},
2773 file_name=>$diff->{'to_file'},
2774 hash_base=>$hash)},
2775 "blob");
2776 print " | " if ($has_history);
2777 }
2778 if ($has_history) {
2779 print $cgi->a({-href => href(action=>"history",
2780 file_name=>$diff->{'to_file'},
2781 hash_base=>$hash)},
2782 "history");
2783 }
2784 print "</td>\n";
2785
2786 print "</tr>\n";
2787 next; # instead of 'else' clause, to avoid extra indent
2788 }
2789 # else ordinary diff
2790
2791 my ($to_mode_oct, $to_mode_str, $to_file_type);
2792 my ($from_mode_oct, $from_mode_str, $from_file_type);
2793 if ($diff->{'to_mode'} ne ('0' x 6)) {
2794 $to_mode_oct = oct $diff->{'to_mode'};
2795 if (S_ISREG($to_mode_oct)) { # only for regular file
2796 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
2797 }
2798 $to_file_type = file_type($diff->{'to_mode'});
2799 }
2800 if ($diff->{'from_mode'} ne ('0' x 6)) {
2801 $from_mode_oct = oct $diff->{'from_mode'};
2802 if (S_ISREG($to_mode_oct)) { # only for regular file
2803 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
2804 }
2805 $from_file_type = file_type($diff->{'from_mode'});
2806 }
2807
2808 if ($diff->{'status'} eq "A") { # created
2809 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
2810 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
2811 $mode_chng .= "]</span>";
2812 print "<td>";
2813 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2814 hash_base=>$hash, file_name=>$diff->{'file'}),
2815 -class => "list"}, esc_path($diff->{'file'}));
2816 print "</td>\n";
2817 print "<td>$mode_chng</td>\n";
2818 print "<td class=\"link\">";
2819 if ($action eq 'commitdiff') {
2820 # link to patch
2821 $patchno++;
2822 print $cgi->a({-href => "#patch$patchno"}, "patch");
2823 print " | ";
2824 }
2825 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2826 hash_base=>$hash, file_name=>$diff->{'file'})},
2827 "blob");
2828 print "</td>\n";
2829
2830 } elsif ($diff->{'status'} eq "D") { # deleted
2831 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
2832 print "<td>";
2833 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
2834 hash_base=>$parent, file_name=>$diff->{'file'}),
2835 -class => "list"}, esc_path($diff->{'file'}));
2836 print "</td>\n";
2837 print "<td>$mode_chng</td>\n";
2838 print "<td class=\"link\">";
2839 if ($action eq 'commitdiff') {
2840 # link to patch
2841 $patchno++;
2842 print $cgi->a({-href => "#patch$patchno"}, "patch");
2843 print " | ";
2844 }
2845 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
2846 hash_base=>$parent, file_name=>$diff->{'file'})},
2847 "blob") . " | ";
2848 if ($have_blame) {
2849 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
2850 file_name=>$diff->{'file'})},
2851 "blame") . " | ";
2852 }
2853 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
2854 file_name=>$diff->{'file'})},
2855 "history");
2856 print "</td>\n";
2857
2858 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
2859 my $mode_chnge = "";
2860 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
2861 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
2862 if ($from_file_type ne $to_file_type) {
2863 $mode_chnge .= " from $from_file_type to $to_file_type";
2864 }
2865 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
2866 if ($from_mode_str && $to_mode_str) {
2867 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
2868 } elsif ($to_mode_str) {
2869 $mode_chnge .= " mode: $to_mode_str";
2870 }
2871 }
2872 $mode_chnge .= "]</span>\n";
2873 }
2874 print "<td>";
2875 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2876 hash_base=>$hash, file_name=>$diff->{'file'}),
2877 -class => "list"}, esc_path($diff->{'file'}));
2878 print "</td>\n";
2879 print "<td>$mode_chnge</td>\n";
2880 print "<td class=\"link\">";
2881 if ($action eq 'commitdiff') {
2882 # link to patch
2883 $patchno++;
2884 print $cgi->a({-href => "#patch$patchno"}, "patch") .
2885 " | ";
2886 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
2887 # "commit" view and modified file (not onlu mode changed)
2888 print $cgi->a({-href => href(action=>"blobdiff",
2889 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
2890 hash_base=>$hash, hash_parent_base=>$parent,
2891 file_name=>$diff->{'file'})},
2892 "diff") .
2893 " | ";
2894 }
2895 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2896 hash_base=>$hash, file_name=>$diff->{'file'})},
2897 "blob") . " | ";
2898 if ($have_blame) {
2899 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
2900 file_name=>$diff->{'file'})},
2901 "blame") . " | ";
2902 }
2903 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
2904 file_name=>$diff->{'file'})},
2905 "history");
2906 print "</td>\n";
2907
2908 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
2909 my %status_name = ('R' => 'moved', 'C' => 'copied');
2910 my $nstatus = $status_name{$diff->{'status'}};
2911 my $mode_chng = "";
2912 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
2913 # mode also for directories, so we cannot use $to_mode_str
2914 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
2915 }
2916 print "<td>" .
2917 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2918 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
2919 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
2920 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
2921 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
2922 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
2923 -class => "list"}, esc_path($diff->{'from_file'})) .
2924 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
2925 "<td class=\"link\">";
2926 if ($action eq 'commitdiff') {
2927 # link to patch
2928 $patchno++;
2929 print $cgi->a({-href => "#patch$patchno"}, "patch") .
2930 " | ";
2931 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
2932 # "commit" view and modified file (not only pure rename or copy)
2933 print $cgi->a({-href => href(action=>"blobdiff",
2934 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
2935 hash_base=>$hash, hash_parent_base=>$parent,
2936 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
2937 "diff") .
2938 " | ";
2939 }
2940 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2941 hash_base=>$parent, file_name=>$diff->{'to_file'})},
2942 "blob") . " | ";
2943 if ($have_blame) {
2944 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
2945 file_name=>$diff->{'to_file'})},
2946 "blame") . " | ";
2947 }
2948 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
2949 file_name=>$diff->{'to_file'})},
2950 "history");
2951 print "</td>\n";
2952
2953 } # we should not encounter Unmerged (U) or Unknown (X) status
2954 print "</tr>\n";
2955 }
2956 print "</tbody>" if $has_header;
2957 print "</table>\n";
2958 }
2959
2960 sub git_patchset_body {
2961 my ($fd, $difftree, $hash, @hash_parents) = @_;
2962 my ($hash_parent) = $hash_parents[0];
2963
2964 my $patch_idx = 0;
2965 my $patch_number = 0;
2966 my $patch_line;
2967 my $diffinfo;
2968 my (%from, %to);
2969
2970 print "<div class=\"patchset\">\n";
2971
2972 # skip to first patch
2973 while ($patch_line = <$fd>) {
2974 chomp $patch_line;
2975
2976 last if ($patch_line =~ m/^diff /);
2977 }
2978
2979 PATCH:
2980 while ($patch_line) {
2981 my @diff_header;
2982 my ($from_id, $to_id);
2983
2984 # git diff header
2985 #assert($patch_line =~ m/^diff /) if DEBUG;
2986 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
2987 $patch_number++;
2988 push @diff_header, $patch_line;
2989
2990 # extended diff header
2991 EXTENDED_HEADER:
2992 while ($patch_line = <$fd>) {
2993 chomp $patch_line;
2994
2995 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
2996
2997 if ($patch_line =~ m/^index ([0-9a-fA-F]{40})..([0-9a-fA-F]{40})/) {
2998 $from_id = $1;
2999 $to_id = $2;
3000 } elsif ($patch_line =~ m/^index ((?:[0-9a-fA-F]{40},)+[0-9a-fA-F]{40})..([0-9a-fA-F]{40})/) {
3001 $from_id = [ split(',', $1) ];
3002 $to_id = $2;
3003 }
3004
3005 push @diff_header, $patch_line;
3006 }
3007 my $last_patch_line = $patch_line;
3008
3009 # check if current patch belong to current raw line
3010 # and parse raw git-diff line if needed
3011 if (defined $diffinfo &&
3012 defined $from_id && defined $to_id &&
3013 from_ids_eq($diffinfo->{'from_id'}, $from_id) &&
3014 $diffinfo->{'to_id'} eq $to_id) {
3015 # this is continuation of a split patch
3016 print "<div class=\"patch cont\">\n";
3017 } else {
3018 # advance raw git-diff output if needed
3019 $patch_idx++ if defined $diffinfo;
3020
3021 # compact combined diff output can have some patches skipped
3022 # find which patch (using pathname of result) we are at now
3023 my $to_name;
3024 if ($diff_header[0] =~ m!^diff --cc "?(.*)"?$!) {
3025 $to_name = $1;
3026 }
3027
3028 do {
3029 # read and prepare patch information
3030 if (ref($difftree->[$patch_idx]) eq "HASH") {
3031 # pre-parsed (or generated by hand)
3032 $diffinfo = $difftree->[$patch_idx];
3033 } else {
3034 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
3035 }
3036
3037 # check if current raw line has no patch (it got simplified)
3038 if (defined $to_name && $to_name ne $diffinfo->{'to_file'}) {
3039 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3040 format_diff_cc_simplified($diffinfo, @hash_parents) .
3041 "</div>\n"; # class="patch"
3042
3043 $patch_idx++;
3044 $patch_number++;
3045 }
3046 } until (!defined $to_name || $to_name eq $diffinfo->{'to_file'} ||
3047 $patch_idx > $#$difftree);
3048 # modifies %from, %to hashes
3049 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
3050 if ($diffinfo->{'nparents'}) {
3051 # combined diff
3052 $from{'file'} = [];
3053 $from{'href'} = [];
3054 fill_from_file_info($diffinfo, @hash_parents)
3055 unless exists $diffinfo->{'from_file'};
3056 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
3057 $from{'file'}[$i] = $diffinfo->{'from_file'}[$i] || $diffinfo->{'to_file'};
3058 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
3059 $from{'href'}[$i] = href(action=>"blob",
3060 hash_base=>$hash_parents[$i],
3061 hash=>$diffinfo->{'from_id'}[$i],
3062 file_name=>$from{'file'}[$i]);
3063 } else {
3064 $from{'href'}[$i] = undef;
3065 }
3066 }
3067 } else {
3068 $from{'file'} = $diffinfo->{'from_file'} || $diffinfo->{'file'};
3069 if ($diffinfo->{'status'} ne "A") { # not new (added) file
3070 $from{'href'} = href(action=>"blob", hash_base=>$hash_parent,
3071 hash=>$diffinfo->{'from_id'},
3072 file_name=>$from{'file'});
3073 } else {
3074 delete $from{'href'};
3075 }
3076 }
3077
3078 $to{'file'} = $diffinfo->{'to_file'} || $diffinfo->{'file'};
3079 if (!is_deleted($diffinfo)) { # file exists in result
3080 $to{'href'} = href(action=>"blob", hash_base=>$hash,
3081 hash=>$diffinfo->{'to_id'},
3082 file_name=>$to{'file'});
3083 } else {
3084 delete $to{'href'};
3085 }
3086 # this is first patch for raw difftree line with $patch_idx index
3087 # we index @$difftree array from 0, but number patches from 1
3088 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
3089 }
3090
3091 # print "git diff" header
3092 $patch_line = shift @diff_header;
3093 print format_git_diff_header_line($patch_line, $diffinfo,
3094 \%from, \%to);
3095
3096 # print extended diff header
3097 print "<div class=\"diff extended_header\">\n" if (@diff_header > 0);
3098 EXTENDED_HEADER:
3099 foreach $patch_line (@diff_header) {
3100 print format_extended_diff_header_line($patch_line, $diffinfo,
3101 \%from, \%to);
3102 }
3103 print "</div>\n" if (@diff_header > 0); # class="diff extended_header"
3104
3105 # from-file/to-file diff header
3106 $patch_line = $last_patch_line;
3107 if (! $patch_line) {
3108 print "</div>\n"; # class="patch"
3109 last PATCH;
3110 }
3111 next PATCH if ($patch_line =~ m/^diff /);
3112 #assert($patch_line =~ m/^---/) if DEBUG;
3113 #assert($patch_line eq $last_patch_line) if DEBUG;
3114
3115 $patch_line = <$fd>;
3116 chomp $patch_line;
3117 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
3118
3119 print format_diff_from_to_header($last_patch_line, $patch_line,
3120 $diffinfo, \%from, \%to,
3121 @hash_parents);
3122
3123 # the patch itself
3124 LINE:
3125 while ($patch_line = <$fd>) {
3126 chomp $patch_line;
3127
3128 next PATCH if ($patch_line =~ m/^diff /);
3129
3130 print format_diff_line($patch_line, \%from, \%to);
3131 }
3132
3133 } continue {
3134 print "</div>\n"; # class="patch"
3135 }
3136
3137 # for compact combined (--cc) format, with chunk and patch simpliciaction
3138 # patchset might be empty, but there might be unprocessed raw lines
3139 for ($patch_idx++ if $patch_number > 0;
3140 $patch_idx < @$difftree;
3141 $patch_idx++) {
3142 # read and prepare patch information
3143 if (ref($difftree->[$patch_idx]) eq "HASH") {
3144 # pre-parsed (or generated by hand)
3145 $diffinfo = $difftree->[$patch_idx];
3146 } else {
3147 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
3148 }
3149
3150 # generate anchor for "patch" links in difftree / whatchanged part
3151 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3152 format_diff_cc_simplified($diffinfo, @hash_parents) .
3153 "</div>\n"; # class="patch"
3154
3155 $patch_number++;
3156 }
3157
3158 if ($patch_number == 0) {
3159 if (@hash_parents > 1) {
3160 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
3161 } else {
3162 print "<div class=\"diff nodifferences\">No differences found</div>\n";
3163 }
3164 }
3165
3166 print "</div>\n"; # class="patchset"
3167 }
3168
3169 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3170
3171 sub git_project_list_body {
3172 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
3173
3174 my ($check_forks) = gitweb_check_feature('forks');
3175
3176 my @projects;
3177 foreach my $pr (@$projlist) {
3178 my (@aa) = git_get_last_activity($pr->{'path'});
3179 unless (@aa) {
3180 next;
3181 }
3182 ($pr->{'age'}, $pr->{'age_string'}) = @aa;
3183 if (!defined $pr->{'descr'}) {
3184 my $descr = git_get_project_description($pr->{'path'}) || "";
3185 $pr->{'descr_long'} = to_utf8($descr);
3186 $pr->{'descr'} = chop_str($descr, 25, 5);
3187 }
3188 if (!defined $pr->{'owner'}) {
3189 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
3190 }
3191 if ($check_forks) {
3192 my $pname = $pr->{'path'};
3193 if (($pname =~ s/\.git$//) &&
3194 ($pname !~ /\/$/) &&
3195 (-d "$projectroot/$pname")) {
3196 $pr->{'forks'} = "-d $projectroot/$pname";
3197 }
3198 else {
3199 $pr->{'forks'} = 0;
3200 }
3201 }
3202 push @projects, $pr;
3203 }
3204
3205 $order ||= $default_projects_order;
3206 $from = 0 unless defined $from;
3207 $to = $#projects if (!defined $to || $#projects < $to);
3208
3209 print "<table class=\"project_list\">\n";
3210 unless ($no_header) {
3211 print "<tr>\n";
3212 if ($check_forks) {
3213 print "<th></th>\n";
3214 }
3215 if ($order eq "project") {
3216 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
3217 print "<th>Project</th>\n";
3218 } else {
3219 print "<th>" .
3220 $cgi->a({-href => href(project=>undef, order=>'project'),
3221 -class => "header"}, "Project") .
3222 "</th>\n";
3223 }
3224 if ($order eq "descr") {
3225 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
3226 print "<th>Description</th>\n";
3227 } else {
3228 print "<th>" .
3229 $cgi->a({-href => href(project=>undef, order=>'descr'),
3230 -class => "header"}, "Description") .
3231 "</th>\n";
3232 }
3233 if ($order eq "owner") {
3234 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
3235 print "<th>Owner</th>\n";
3236 } else {
3237 print "<th>" .
3238 $cgi->a({-href => href(project=>undef, order=>'owner'),
3239 -class => "header"}, "Owner") .
3240 "</th>\n";
3241 }
3242 if ($order eq "age") {
3243 @projects = sort {$a->{'age'} <=> $b->{'age'}} @projects;
3244 print "<th>Last Change</th>\n";
3245 } else {
3246 print "<th>" .
3247 $cgi->a({-href => href(project=>undef, order=>'age'),
3248 -class => "header"}, "Last Change") .
3249 "</th>\n";
3250 }
3251 print "<th></th>\n" .
3252 "</tr>\n";
3253 }
3254 my $alternate = 1;
3255 for (my $i = $from; $i <= $to; $i++) {
3256 my $pr = $projects[$i];
3257 if ($alternate) {
3258 print "<tr class=\"dark\">\n";
3259 } else {
3260 print "<tr class=\"light\">\n";
3261 }
3262 $alternate ^= 1;
3263 if ($check_forks) {
3264 print "<td>";
3265 if ($pr->{'forks'}) {
3266 print "<!-- $pr->{'forks'} -->\n";
3267 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
3268 }
3269 print "</td>\n";
3270 }
3271 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3272 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
3273 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3274 -class => "list", -title => $pr->{'descr_long'}},
3275 esc_html($pr->{'descr'})) . "</td>\n" .
3276 "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
3277 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
3278 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
3279 "<td class=\"link\">" .
3280 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
3281 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
3282 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
3283 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
3284 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
3285 "</td>\n" .
3286 "</tr>\n";
3287 }
3288 if (defined $extra) {
3289 print "<tr>\n";
3290 if ($check_forks) {
3291 print "<td></td>\n";
3292 }
3293 print "<td colspan=\"5\">$extra</td>\n" .
3294 "</tr>\n";
3295 }
3296 print "</table>\n";
3297 }
3298
3299 sub git_shortlog_body {
3300 # uses global variable $project
3301 my ($commitlist, $from, $to, $refs, $extra) = @_;
3302
3303 my $have_snapshot = gitweb_have_snapshot();
3304
3305 $from = 0 unless defined $from;
3306 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3307
3308 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
3309 my $alternate = 1;
3310 for (my $i = $from; $i <= $to; $i++) {
3311 my %co = %{$commitlist->[$i]};
3312 my $commit = $co{'id'};
3313 my $ref = format_ref_marker($refs, $commit);
3314 if ($alternate) {
3315 print "<tr class=\"dark\">\n";
3316 } else {
3317 print "<tr class=\"light\">\n";
3318 }
3319 $alternate ^= 1;
3320 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
3321 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3322 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
3323 "<td>";
3324 print format_subject_html($co{'title'}, $co{'title_short'},
3325 href(action=>"commit", hash=>$commit), $ref);
3326 print "</td>\n" .
3327 "<td class=\"link\">" .
3328 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
3329 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
3330 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
3331 if ($have_snapshot) {
3332 print " | " . $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
3333 }
3334 print "</td>\n" .
3335 "</tr>\n";
3336 }
3337 if (defined $extra) {
3338 print "<tr>\n" .
3339 "<td colspan=\"4\">$extra</td>\n" .
3340 "</tr>\n";
3341 }
3342 print "</table>\n";
3343 }
3344
3345 sub git_history_body {
3346 # Warning: assumes constant type (blob or tree) during history
3347 my ($commitlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
3348
3349 $from = 0 unless defined $from;
3350 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
3351
3352 print "<table class=\"history\" cellspacing=\"0\">\n";
3353 my $alternate = 1;
3354 for (my $i = $from; $i <= $to; $i++) {
3355 my %co = %{$commitlist->[$i]};
3356 if (!%co) {
3357 next;
3358 }
3359 my $commit = $co{'id'};
3360
3361 my $ref = format_ref_marker($refs, $commit);
3362
3363 if ($alternate) {
3364 print "<tr class=\"dark\">\n";
3365 } else {
3366 print "<tr class=\"light\">\n";
3367 }
3368 $alternate ^= 1;
3369 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3370 # shortlog uses chop_str($co{'author_name'}, 10)
3371 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
3372 "<td>";
3373 # originally git_history used chop_str($co{'title'}, 50)
3374 print format_subject_html($co{'title'}, $co{'title_short'},
3375 href(action=>"commit", hash=>$commit), $ref);
3376 print "</td>\n" .
3377 "<td class=\"link\">" .
3378 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
3379 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
3380
3381 if ($ftype eq 'blob') {
3382 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
3383 my $blob_parent = git_get_hash_by_path($commit, $file_name);
3384 if (defined $blob_current && defined $blob_parent &&
3385 $blob_current ne $blob_parent) {
3386 print " | " .
3387 $cgi->a({-href => href(action=>"blobdiff",
3388 hash=>$blob_current, hash_parent=>$blob_parent,
3389 hash_base=>$hash_base, hash_parent_base=>$commit,
3390 file_name=>$file_name)},
3391 "diff to current");
3392 }
3393 }
3394 print "</td>\n" .
3395 "</tr>\n";
3396 }
3397 if (defined $extra) {
3398 print "<tr>\n" .
3399 "<td colspan=\"4\">$extra</td>\n" .
3400 "</tr>\n";
3401 }
3402 print "</table>\n";
3403 }
3404
3405 sub git_tags_body {
3406 # uses global variable $project
3407 my ($taglist, $from, $to, $extra) = @_;
3408 $from = 0 unless defined $from;
3409 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
3410
3411 print "<table class=\"tags\" cellspacing=\"0\">\n";
3412 my $alternate = 1;
3413 for (my $i = $from; $i <= $to; $i++) {
3414 my $entry = $taglist->[$i];
3415 my %tag = %$entry;
3416 my $comment = $tag{'subject'};
3417 my $comment_short;
3418 if (defined $comment) {
3419 $comment_short = chop_str($comment, 30, 5);
3420 }
3421 if ($alternate) {
3422 print "<tr class=\"dark\">\n";
3423 } else {
3424 print "<tr class=\"light\">\n";
3425 }
3426 $alternate ^= 1;
3427 if (defined $tag{'age'}) {
3428 print "<td><i>$tag{'age'}</i></td>\n";
3429 } else {
3430 print "<td></td>\n";
3431 }
3432 print "<td>" .
3433 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
3434 -class => "list name"}, esc_html($tag{'name'})) .
3435 "</td>\n" .
3436 "<td>";
3437 if (defined $comment) {
3438 print format_subject_html($comment, $comment_short,
3439 href(action=>"tag", hash=>$tag{'id'}));
3440 }
3441 print "</td>\n" .
3442 "<td class=\"selflink\">";
3443 if ($tag{'type'} eq "tag") {
3444 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
3445 } else {
3446 print "&nbsp;";
3447 }
3448 print "</td>\n" .
3449 "<td class=\"link\">" . " | " .
3450 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
3451 if ($tag{'reftype'} eq "commit") {
3452 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
3453 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log");
3454 } elsif ($tag{'reftype'} eq "blob") {
3455 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
3456 }
3457 print "</td>\n" .
3458 "</tr>";
3459 }
3460 if (defined $extra) {
3461 print "<tr>\n" .
3462 "<td colspan=\"5\">$extra</td>\n" .
3463 "</tr>\n";
3464 }
3465 print "</table>\n";
3466 }
3467
3468 sub git_heads_body {
3469 # uses global variable $project
3470 my ($headlist, $head, $from, $to, $extra) = @_;
3471 $from = 0 unless defined $from;
3472 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
3473
3474 print "<table class=\"heads\" cellspacing=\"0\">\n";
3475 my $alternate = 1;
3476 for (my $i = $from; $i <= $to; $i++) {
3477 my $entry = $headlist->[$i];
3478 my %ref = %$entry;
3479 my $curr = $ref{'id'} eq $head;
3480 if ($alternate) {
3481 print "<tr class=\"dark\">\n";
3482 } else {
3483 print "<tr class=\"light\">\n";
3484 }
3485 $alternate ^= 1;
3486 print "<td><i>$ref{'age'}</i></td>\n" .
3487 ($curr ? "<td class=\"current_head\">" : "<td>") .
3488 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'}),
3489 -class => "list name"},esc_html($ref{'name'})) .
3490 "</td>\n" .
3491 "<td class=\"link\">" .
3492 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'})}, "shortlog") . " | " .
3493 $cgi->a({-href => href(action=>"log", hash=>$ref{'name'})}, "log") . " | " .
3494 $cgi->a({-href => href(action=>"tree", hash=>$ref{'name'}, hash_base=>$ref{'name'})}, "tree") .
3495 "</td>\n" .
3496 "</tr>";
3497 }
3498 if (defined $extra) {
3499 print "<tr>\n" .
3500 "<td colspan=\"3\">$extra</td>\n" .
3501 "</tr>\n";
3502 }
3503 print "</table>\n";
3504 }
3505
3506 sub git_search_grep_body {
3507 my ($commitlist, $from, $to, $extra) = @_;
3508 $from = 0 unless defined $from;
3509 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3510
3511 print "<table class=\"grep\" cellspacing=\"0\">\n";
3512 my $alternate = 1;
3513 for (my $i = $from; $i <= $to; $i++) {
3514 my %co = %{$commitlist->[$i]};
3515 if (!%co) {
3516 next;
3517 }
3518 my $commit = $co{'id'};
3519 if ($alternate) {
3520 print "<tr class=\"dark\">\n";
3521 } else {
3522 print "<tr class=\"light\">\n";
3523 }
3524 $alternate ^= 1;
3525 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3526 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3527 "<td>" .
3528 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
3529 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3530 my $comment = $co{'comment'};
3531 foreach my $line (@$comment) {
3532 if ($line =~ m/^(.*)($search_regexp)(.*)$/i) {
3533 my $lead = esc_html($1) || "";
3534 $lead = chop_str($lead, 30, 10);
3535 my $match = esc_html($2) || "";
3536 my $trail = esc_html($3) || "";
3537 $trail = chop_str($trail, 30, 10);
3538 my $text = "$lead<span class=\"match\">$match</span>$trail";
3539 print chop_str($text, 80, 5) . "<br/>\n";
3540 }
3541 }
3542 print "</td>\n" .
3543 "<td class=\"link\">" .
3544 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3545 " | " .
3546 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3547 print "</td>\n" .
3548 "</tr>\n";
3549 }
3550 if (defined $extra) {
3551 print "<tr>\n" .
3552 "<td colspan=\"3\">$extra</td>\n" .
3553 "</tr>\n";
3554 }
3555 print "</table>\n";
3556 }
3557
3558 ## ======================================================================
3559 ## ======================================================================
3560 ## actions
3561
3562 sub git_project_list {
3563 my $order = $cgi->param('o');
3564 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
3565 die_error(undef, "Unknown order parameter");
3566 }
3567
3568 my @list = git_get_projects_list();
3569 if (!@list) {
3570 die_error(undef, "No projects found");
3571 }
3572
3573 git_header_html();
3574 if (-f $home_text) {
3575 print "<div class=\"index_include\">\n";
3576 open (my $fd, $home_text);
3577 print <$fd>;
3578 close $fd;
3579 print "</div>\n";
3580 }
3581 git_project_list_body(\@list, $order);
3582 git_footer_html();
3583 }
3584
3585 sub git_forks {
3586 my $order = $cgi->param('o');
3587 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
3588 die_error(undef, "Unknown order parameter");
3589 }
3590
3591 my @list = git_get_projects_list($project);
3592 if (!@list) {
3593 die_error(undef, "No forks found");
3594 }
3595
3596 git_header_html();
3597 git_print_page_nav('','');
3598 git_print_header_div('summary', "$project forks");
3599 git_project_list_body(\@list, $order);
3600 git_footer_html();
3601 }
3602
3603 sub git_project_index {
3604 my @projects = git_get_projects_list($project);
3605
3606 print $cgi->header(
3607 -type => 'text/plain',
3608 -charset => 'utf-8',
3609 -content_disposition => 'inline; filename="index.aux"');
3610
3611 foreach my $pr (@projects) {
3612 if (!exists $pr->{'owner'}) {
3613 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
3614 }
3615
3616 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
3617 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
3618 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
3619 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
3620 $path =~ s/ /\+/g;
3621 $owner =~ s/ /\+/g;
3622
3623 print "$path $owner\n";
3624 }
3625 }
3626
3627 sub git_summary {
3628 my $descr = git_get_project_description($project) || "none";
3629 my %co = parse_commit("HEAD");
3630 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
3631 my $head = $co{'id'};
3632
3633 my $owner = git_get_project_owner($project);
3634
3635 my $refs = git_get_references();
3636 # These get_*_list functions return one more to allow us to see if
3637 # there are more ...
3638 my @taglist = git_get_tags_list(16);
3639 my @headlist = git_get_heads_list(16);
3640 my @forklist;
3641 my ($check_forks) = gitweb_check_feature('forks');
3642
3643 if ($check_forks) {
3644 @forklist = git_get_projects_list($project);
3645 }
3646
3647 git_header_html();
3648 git_print_page_nav('summary','', $head);
3649
3650 print "<div class=\"title\">&nbsp;</div>\n";
3651 print "<table cellspacing=\"0\">\n" .
3652 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
3653 "<tr><td>owner</td><td>$owner</td></tr>\n";
3654 if (defined $cd{'rfc2822'}) {
3655 print "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
3656 }
3657
3658 # use per project git URL list in $projectroot/$project/cloneurl
3659 # or make project git URL from git base URL and project name
3660 my $url_tag = "URL";
3661 my @url_list = git_get_project_url_list($project);
3662 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
3663 foreach my $git_url (@url_list) {
3664 next unless $git_url;
3665 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
3666 $url_tag = "";
3667 }
3668 print "</table>\n";
3669
3670 if (-s "$projectroot/$project/README.html") {
3671 if (open my $fd, "$projectroot/$project/README.html") {
3672 print "<div class=\"title\">readme</div>\n";
3673 print $_ while (<$fd>);
3674 close $fd;
3675 }
3676 }
3677
3678 # we need to request one more than 16 (0..15) to check if
3679 # those 16 are all
3680 my @commitlist = $head ? parse_commits($head, 17) : ();
3681 if (@commitlist) {
3682 git_print_header_div('shortlog');
3683 git_shortlog_body(\@commitlist, 0, 15, $refs,
3684 $#commitlist <= 15 ? undef :
3685 $cgi->a({-href => href(action=>"shortlog")}, "..."));
3686 }
3687
3688 if (@taglist) {
3689 git_print_header_div('tags');
3690 git_tags_body(\@taglist, 0, 15,
3691 $#taglist <= 15 ? undef :
3692 $cgi->a({-href => href(action=>"tags")}, "..."));
3693 }
3694
3695 if (@headlist) {
3696 git_print_header_div('heads');
3697 git_heads_body(\@headlist, $head, 0, 15,
3698 $#headlist <= 15 ? undef :
3699 $cgi->a({-href => href(action=>"heads")}, "..."));
3700 }
3701
3702 if (@forklist) {
3703 git_print_header_div('forks');
3704 git_project_list_body(\@forklist, undef, 0, 15,
3705 $#forklist <= 15 ? undef :
3706 $cgi->a({-href => href(action=>"forks")}, "..."),
3707 'noheader');
3708 }
3709
3710 git_footer_html();
3711 }
3712
3713 sub git_tag {
3714 my $head = git_get_head_hash($project);
3715 git_header_html();
3716 git_print_page_nav('','', $head,undef,$head);
3717 my %tag = parse_tag($hash);
3718
3719 if (! %tag) {
3720 die_error(undef, "Unknown tag object");
3721 }
3722
3723 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
3724 print "<div class=\"title_text\">\n" .
3725 "<table cellspacing=\"0\">\n" .
3726 "<tr>\n" .
3727 "<td>object</td>\n" .
3728 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
3729 $tag{'object'}) . "</td>\n" .
3730 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
3731 $tag{'type'}) . "</td>\n" .
3732 "</tr>\n";
3733 if (defined($tag{'author'})) {
3734 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
3735 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
3736 print "<tr><td></td><td>" . $ad{'rfc2822'} .
3737 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
3738 "</td></tr>\n";
3739 }
3740 print "</table>\n\n" .
3741 "</div>\n";
3742 print "<div class=\"page_body\">";
3743 my $comment = $tag{'comment'};
3744 foreach my $line (@$comment) {
3745 chomp $line;
3746 print esc_html($line, -nbsp=>1) . "<br/>\n";
3747 }
3748 print "</div>\n";
3749 git_footer_html();
3750 }
3751
3752 sub git_blame2 {
3753 my $fd;
3754 my $ftype;
3755
3756 my ($have_blame) = gitweb_check_feature('blame');
3757 if (!$have_blame) {
3758 die_error('403 Permission denied', "Permission denied");
3759 }
3760 die_error('404 Not Found', "File name not defined") if (!$file_name);
3761 $hash_base ||= git_get_head_hash($project);
3762 die_error(undef, "Couldn't find base commit") unless ($hash_base);
3763 my %co = parse_commit($hash_base)
3764 or die_error(undef, "Reading commit failed");
3765 if (!defined $hash) {
3766 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
3767 or die_error(undef, "Error looking up file");
3768 }
3769 $ftype = git_get_type($hash);
3770 if ($ftype !~ "blob") {
3771 die_error('400 Bad Request', "Object is not a blob");
3772 }
3773 open ($fd, "-|", git_cmd(), "blame", '-p', '--',
3774 $file_name, $hash_base)
3775 or die_error(undef, "Open git-blame failed");
3776 git_header_html();
3777 my $formats_nav =
3778 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3779 "blob") .
3780 " | " .
3781 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3782 "history") .
3783 " | " .
3784 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
3785 "HEAD");
3786 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3787 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3788 git_print_page_path($file_name, $ftype, $hash_base);
3789 my @rev_color = (qw(light2 dark2));
3790 my $num_colors = scalar(@rev_color);
3791 my $current_color = 0;
3792 my $last_rev;
3793 print <<HTML;
3794 <div class="page_body">
3795 <table class="blame">
3796 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
3797 HTML
3798 my %metainfo = ();
3799 while (1) {
3800 $_ = <$fd>;
3801 last unless defined $_;
3802 my ($full_rev, $orig_lineno, $lineno, $group_size) =
3803 /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;
3804 if (!exists $metainfo{$full_rev}) {
3805 $metainfo{$full_rev} = {};
3806 }
3807 my $meta = $metainfo{$full_rev};
3808 while (<$fd>) {
3809 last if (s/^\t//);
3810 if (/^(\S+) (.*)$/) {
3811 $meta->{$1} = $2;
3812 }
3813 }
3814 my $data = $_;
3815 chomp $data;
3816 my $rev = substr($full_rev, 0, 8);
3817 my $author = $meta->{'author'};
3818 my %date = parse_date($meta->{'author-time'},
3819 $meta->{'author-tz'});
3820 my $date = $date{'iso-tz'};
3821 if ($group_size) {
3822 $current_color = ++$current_color % $num_colors;
3823 }
3824 print "<tr class=\"$rev_color[$current_color]\">\n";
3825 if ($group_size) {
3826 print "<td class=\"sha1\"";
3827 print " title=\"". esc_html($author) . ", $date\"";
3828 print " rowspan=\"$group_size\"" if ($group_size > 1);
3829 print ">";
3830 print $cgi->a({-href => href(action=>"commit",
3831 hash=>$full_rev,
3832 file_name=>$file_name)},
3833 esc_html($rev));
3834 print "</td>\n";
3835 }
3836 open (my $dd, "-|", git_cmd(), "rev-parse", "$full_rev^")
3837 or die_error(undef, "Open git-rev-parse failed");
3838 my $parent_commit = <$dd>;
3839 close $dd;
3840 chomp($parent_commit);
3841 my $blamed = href(action => 'blame',
3842 file_name => $meta->{'filename'},
3843 hash_base => $parent_commit);
3844 print "<td class=\"linenr\">";
3845 print $cgi->a({ -href => "$blamed#l$orig_lineno",
3846 -id => "l$lineno",
3847 -class => "linenr" },
3848 esc_html($lineno));
3849 print "</td>";
3850 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
3851 print "</tr>\n";
3852 }
3853 print "</table>\n";
3854 print "</div>";
3855 close $fd
3856 or print "Reading blob failed\n";
3857 git_footer_html();
3858 }
3859
3860 sub git_blame {
3861 my $fd;
3862
3863 my ($have_blame) = gitweb_check_feature('blame');
3864 if (!$have_blame) {
3865 die_error('403 Permission denied', "Permission denied");
3866 }
3867 die_error('404 Not Found', "File name not defined") if (!$file_name);
3868 $hash_base ||= git_get_head_hash($project);
3869 die_error(undef, "Couldn't find base commit") unless ($hash_base);
3870 my %co = parse_commit($hash_base)
3871 or die_error(undef, "Reading commit failed");
3872 if (!defined $hash) {
3873 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
3874 or die_error(undef, "Error lookup file");
3875 }
3876 open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
3877 or die_error(undef, "Open git-annotate failed");
3878 git_header_html();
3879 my $formats_nav =
3880 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3881 "blob") .
3882 " | " .
3883 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3884 "history") .
3885 " | " .
3886 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
3887 "HEAD");
3888 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3889 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3890 git_print_page_path($file_name, 'blob', $hash_base);
3891 print "<div class=\"page_body\">\n";
3892 print <<HTML;
3893 <table class="blame">
3894 <tr>
3895 <th>Commit</th>
3896 <th>Age</th>
3897 <th>Author</th>
3898 <th>Line</th>
3899 <th>Data</th>
3900 </tr>
3901 HTML
3902 my @line_class = (qw(light dark));
3903 my $line_class_len = scalar (@line_class);
3904 my $line_class_num = $#line_class;
3905 while (my $line = <$fd>) {
3906 my $long_rev;
3907 my $short_rev;
3908 my $author;
3909 my $time;
3910 my $lineno;
3911 my $data;
3912 my $age;
3913 my $age_str;
3914 my $age_class;
3915
3916 chomp $line;
3917 $line_class_num = ($line_class_num + 1) % $line_class_len;
3918
3919 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
3920 $long_rev = $1;
3921 $author = $2;
3922 $time = $3;
3923 $lineno = $4;
3924 $data = $5;
3925 } else {
3926 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
3927 next;
3928 }
3929 $short_rev = substr ($long_rev, 0, 8);
3930 $age = time () - $time;
3931 $age_str = age_string ($age);
3932 $age_str =~ s/ /&nbsp;/g;
3933 $age_class = age_class($age);
3934 $author = esc_html ($author);
3935 $author =~ s/ /&nbsp;/g;
3936
3937 $data = untabify($data);
3938 $data = esc_html ($data);
3939
3940 print <<HTML;
3941 <tr class="$line_class[$line_class_num]">
3942 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
3943 <td class="$age_class">$age_str</td>
3944 <td>$author</td>
3945 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
3946 <td class="pre">$data</td>
3947 </tr>
3948 HTML
3949 } # while (my $line = <$fd>)
3950 print "</table>\n\n";
3951 close $fd
3952 or print "Reading blob failed.\n";
3953 print "</div>";
3954 git_footer_html();
3955 }
3956
3957 sub git_tags {
3958 my $head = git_get_head_hash($project);
3959 git_header_html();
3960 git_print_page_nav('','', $head,undef,$head);
3961 git_print_header_div('summary', $project);
3962
3963 my @tagslist = git_get_tags_list();
3964 if (@tagslist) {
3965 git_tags_body(\@tagslist);
3966 }
3967 git_footer_html();
3968 }
3969
3970 sub git_heads {
3971 my $head = git_get_head_hash($project);
3972 git_header_html();
3973 git_print_page_nav('','', $head,undef,$head);
3974 git_print_header_div('summary', $project);
3975
3976 my @headslist = git_get_heads_list();
3977 if (@headslist) {
3978 git_heads_body(\@headslist, $head);
3979 }
3980 git_footer_html();
3981 }
3982
3983 sub git_blob_plain {
3984 my $expires;
3985
3986 if (!defined $hash) {
3987 if (defined $file_name) {
3988 my $base = $hash_base || git_get_head_hash($project);
3989 $hash = git_get_hash_by_path($base, $file_name, "blob")
3990 or die_error(undef, "Error lookup file");
3991 } else {
3992 die_error(undef, "No file name defined");
3993 }
3994 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3995 # blobs defined by non-textual hash id's can be cached
3996 $expires = "+1d";
3997 }
3998
3999 my $type = shift;
4000 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4001 or die_error(undef, "Couldn't cat $file_name, $hash");
4002
4003 $type ||= blob_mimetype($fd, $file_name);
4004
4005 # save as filename, even when no $file_name is given
4006 my $save_as = "$hash";
4007 if (defined $file_name) {
4008 $save_as = $file_name;
4009 } elsif ($type =~ m/^text\//) {
4010 $save_as .= '.txt';
4011 }
4012
4013 print $cgi->header(
4014 -type => "$type",
4015 -expires=>$expires,
4016 -content_disposition => 'inline; filename="' . "$save_as" . '"');
4017 undef $/;
4018 binmode STDOUT, ':raw';
4019 print <$fd>;
4020 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4021 $/ = "\n";
4022 close $fd;
4023 }
4024
4025 sub git_blob {
4026 my $expires;
4027
4028 if (!defined $hash) {
4029 if (defined $file_name) {
4030 my $base = $hash_base || git_get_head_hash($project);
4031 $hash = git_get_hash_by_path($base, $file_name, "blob")
4032 or die_error(undef, "Error lookup file");
4033 } else {
4034 die_error(undef, "No file name defined");
4035 }
4036 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4037 # blobs defined by non-textual hash id's can be cached
4038 $expires = "+1d";
4039 }
4040
4041 my ($have_blame) = gitweb_check_feature('blame');
4042 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4043 or die_error(undef, "Couldn't cat $file_name, $hash");
4044 my $mimetype = blob_mimetype($fd, $file_name);
4045 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)!) {
4046 close $fd;
4047 return git_blob_plain($mimetype);
4048 }
4049 # we can have blame only for text/* mimetype
4050 $have_blame &&= ($mimetype =~ m!^text/!);
4051
4052 git_header_html(undef, $expires);
4053 my $formats_nav = '';
4054 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4055 if (defined $file_name) {
4056 if ($have_blame) {
4057 $formats_nav .=
4058 $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
4059 hash=>$hash, file_name=>$file_name)},
4060 "blame") .
4061 " | ";
4062 }
4063 $formats_nav .=
4064 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
4065 hash=>$hash, file_name=>$file_name)},
4066 "history") .
4067 " | " .
4068 $cgi->a({-href => href(action=>"blob_plain",
4069 hash=>$hash, file_name=>$file_name)},
4070 "raw") .
4071 " | " .
4072 $cgi->a({-href => href(action=>"blob",
4073 hash_base=>"HEAD", file_name=>$file_name)},
4074 "HEAD");
4075 } else {
4076 $formats_nav .=
4077 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "raw");
4078 }
4079 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4080 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4081 } else {
4082 print "<div class=\"page_nav\">\n" .
4083 "<br/><br/></div>\n" .
4084 "<div class=\"title\">$hash</div>\n";
4085 }
4086 git_print_page_path($file_name, "blob", $hash_base);
4087 print "<div class=\"page_body\">\n";
4088 if ($mimetype =~ m!^text/!) {
4089 my $nr;
4090 while (my $line = <$fd>) {
4091 chomp $line;
4092 $nr++;
4093 $line = untabify($line);
4094 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
4095 $nr, $nr, $nr, esc_html($line, -nbsp=>1);
4096 }
4097 } elsif ($mimetype =~ m!^image/!) {
4098 print qq!<img type="$mimetype"!;
4099 if ($file_name) {
4100 print qq! alt="$file_name" title="$file_name"!;
4101 }
4102 print qq! src="! .
4103 href(action=>"blob_plain", hash=>$hash,
4104 hash_base=>$hash_base, file_name=>$file_name) .
4105 qq!" />\n!;
4106 }
4107 close $fd
4108 or print "Reading blob failed.\n";
4109 print "</div>";
4110 git_footer_html();
4111 }
4112
4113 sub git_tree {
4114 my $have_snapshot = gitweb_have_snapshot();
4115
4116 if (!defined $hash_base) {
4117 $hash_base = "HEAD";
4118 }
4119 if (!defined $hash) {
4120 if (defined $file_name) {
4121 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
4122 } else {
4123 $hash = $hash_base;
4124 }
4125 }
4126 $/ = "\0";
4127 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
4128 or die_error(undef, "Open git-ls-tree failed");
4129 my @entries = map { chomp; $_ } <$fd>;
4130 close $fd or die_error(undef, "Reading tree failed");
4131 $/ = "\n";
4132
4133 my $refs = git_get_references();
4134 my $ref = format_ref_marker($refs, $hash_base);
4135 git_header_html();
4136 my $basedir = '';
4137 my ($have_blame) = gitweb_check_feature('blame');
4138 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4139 my @views_nav = ();
4140 if (defined $file_name) {
4141 push @views_nav,
4142 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
4143 hash=>$hash, file_name=>$file_name)},
4144 "history"),
4145 $cgi->a({-href => href(action=>"tree",
4146 hash_base=>"HEAD", file_name=>$file_name)},
4147 "HEAD"),
4148 }
4149 if ($have_snapshot) {
4150 # FIXME: Should be available when we have no hash base as well.
4151 push @views_nav,
4152 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)},
4153 "snapshot");
4154 }
4155 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
4156 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
4157 } else {
4158 undef $hash_base;
4159 print "<div class=\"page_nav\">\n";
4160 print "<br/><br/></div>\n";
4161 print "<div class=\"title\">$hash</div>\n";
4162 }
4163 if (defined $file_name) {
4164 $basedir = $file_name;
4165 if ($basedir ne '' && substr($basedir, -1) ne '/') {
4166 $basedir .= '/';
4167 }
4168 }
4169 git_print_page_path($file_name, 'tree', $hash_base);
4170 print "<div class=\"page_body\">\n";
4171 print "<table cellspacing=\"0\">\n";
4172 my $alternate = 1;
4173 # '..' (top directory) link if possible
4174 if (defined $hash_base &&
4175 defined $file_name && $file_name =~ m![^/]+$!) {
4176 if ($alternate) {
4177 print "<tr class=\"dark\">\n";
4178 } else {
4179 print "<tr class=\"light\">\n";
4180 }
4181 $alternate ^= 1;
4182
4183 my $up = $file_name;
4184 $up =~ s!/?[^/]+$!!;
4185 undef $up unless $up;
4186 # based on git_print_tree_entry
4187 print '<td class="mode">' . mode_str('040000') . "</td>\n";
4188 print '<td class="list">';
4189 print $cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,
4190 file_name=>$up)},
4191 "..");
4192 print "</td>\n";
4193 print "<td class=\"link\"></td>\n";
4194
4195 print "</tr>\n";
4196 }
4197 foreach my $line (@entries) {
4198 my %t = parse_ls_tree_line($line, -z => 1);
4199
4200 if ($alternate) {
4201 print "<tr class=\"dark\">\n";
4202 } else {
4203 print "<tr class=\"light\">\n";
4204 }
4205 $alternate ^= 1;
4206
4207 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
4208
4209 print "</tr>\n";
4210 }
4211 print "</table>\n" .
4212 "</div>";
4213 git_footer_html();
4214 }
4215
4216 sub git_snapshot {
4217 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
4218 my $have_snapshot = (defined $ctype && defined $suffix);
4219 if (!$have_snapshot) {
4220 die_error('403 Permission denied', "Permission denied");
4221 }
4222
4223 if (!defined $hash) {
4224 $hash = git_get_head_hash($project);
4225 }
4226
4227 my $git = git_cmd_str();
4228 my $name = $project;
4229 $name =~ s,([^/])/*\.git$,$1,;
4230 $name = basename($name);
4231 my $filename = to_utf8($name);
4232 $name =~ s/\047/\047\\\047\047/g;
4233 my $cmd;
4234 if ($suffix eq 'zip') {
4235 $filename .= "-$hash.$suffix";
4236 $cmd = "$git archive --format=zip --prefix=\'$name\'/ $hash";
4237 } else {
4238 $filename .= "-$hash.tar.$suffix";
4239 $cmd = "$git archive --format=tar --prefix=\'$name\'/ $hash | $command";
4240 }
4241
4242 print $cgi->header(
4243 -type => "application/$ctype",
4244 -content_disposition => 'inline; filename="' . "$filename" . '"',
4245 -status => '200 OK');
4246
4247 open my $fd, "-|", $cmd
4248 or die_error(undef, "Execute git-archive failed");
4249 binmode STDOUT, ':raw';
4250 print <$fd>;
4251 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4252 close $fd;
4253
4254 }
4255
4256 sub git_log {
4257 my $head = git_get_head_hash($project);
4258 if (!defined $hash) {
4259 $hash = $head;
4260 }
4261 if (!defined $page) {
4262 $page = 0;
4263 }
4264 my $refs = git_get_references();
4265
4266 my @commitlist = parse_commits($hash, 101, (100 * $page));
4267
4268 my $paging_nav = format_paging_nav('log', $hash, $head, $page, (100 * ($page+1)));
4269
4270 git_header_html();
4271 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
4272
4273 if (!@commitlist) {
4274 my %co = parse_commit($hash);
4275
4276 git_print_header_div('summary', $project);
4277 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
4278 }
4279 my $to = ($#commitlist >= 99) ? (99) : ($#commitlist);
4280 for (my $i = 0; $i <= $to; $i++) {
4281 my %co = %{$commitlist[$i]};
4282 next if !%co;
4283 my $commit = $co{'id'};
4284 my $ref = format_ref_marker($refs, $commit);
4285 my %ad = parse_date($co{'author_epoch'});
4286 git_print_header_div('commit',
4287 "<span class=\"age\">$co{'age_string'}</span>" .
4288 esc_html($co{'title'}) . $ref,
4289 $commit);
4290 print "<div class=\"title_text\">\n" .
4291 "<div class=\"log_link\">\n" .
4292 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
4293 " | " .
4294 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
4295 " | " .
4296 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
4297 "<br/>\n" .
4298 "</div>\n" .
4299 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
4300 "</div>\n";
4301
4302 print "<div class=\"log_body\">\n";
4303 git_print_log($co{'comment'}, -final_empty_line=> 1);
4304 print "</div>\n";
4305 }
4306 if ($#commitlist >= 100) {
4307 print "<div class=\"page_nav\">\n";
4308 print $cgi->a({-href => href(action=>"log", hash=>$hash, page=>$page+1),
4309 -accesskey => "n", -title => "Alt-n"}, "next");
4310 print "</div>\n";
4311 }
4312 git_footer_html();
4313 }
4314
4315 sub git_commit {
4316 $hash ||= $hash_base || "HEAD";
4317 my %co = parse_commit($hash);
4318 if (!%co) {
4319 die_error(undef, "Unknown commit object");
4320 }
4321 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4322 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
4323
4324 my $parent = $co{'parent'};
4325 my $parents = $co{'parents'}; # listref
4326
4327 # we need to prepare $formats_nav before any parameter munging
4328 my $formats_nav;
4329 if (!defined $parent) {
4330 # --root commitdiff
4331 $formats_nav .= '(initial)';
4332 } elsif (@$parents == 1) {
4333 # single parent commit
4334 $formats_nav .=
4335 '(parent: ' .
4336 $cgi->a({-href => href(action=>"commit",
4337 hash=>$parent)},
4338 esc_html(substr($parent, 0, 7))) .
4339 ')';
4340 } else {
4341 # merge commit
4342 $formats_nav .=
4343 '(merge: ' .
4344 join(' ', map {
4345 $cgi->a({-href => href(action=>"commit",
4346 hash=>$_)},
4347 esc_html(substr($_, 0, 7)));
4348 } @$parents ) .
4349 ')';
4350 }
4351
4352 if (!defined $parent) {
4353 $parent = "--root";
4354 }
4355 my @difftree;
4356 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
4357 @diff_opts,
4358 (@$parents <= 1 ? $parent : '-c'),
4359 $hash, "--"
4360 or die_error(undef, "Open git-diff-tree failed");
4361 @difftree = map { chomp; $_ } <$fd>;
4362 close $fd or die_error(undef, "Reading git-diff-tree failed");
4363
4364 # non-textual hash id's can be cached
4365 my $expires;
4366 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4367 $expires = "+1d";
4368 }
4369 my $refs = git_get_references();
4370 my $ref = format_ref_marker($refs, $co{'id'});
4371
4372 my $have_snapshot = gitweb_have_snapshot();
4373
4374 git_header_html(undef, $expires);
4375 git_print_page_nav('commit', '',
4376 $hash, $co{'tree'}, $hash,
4377 $formats_nav);
4378
4379 if (defined $co{'parent'}) {
4380 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
4381 } else {
4382 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
4383 }
4384 print "<div class=\"title_text\">\n" .
4385 "<table cellspacing=\"0\">\n";
4386 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
4387 "<tr>" .
4388 "<td></td><td> $ad{'rfc2822'}";
4389 if ($ad{'hour_local'} < 6) {
4390 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
4391 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4392 } else {
4393 printf(" (%02d:%02d %s)",
4394 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4395 }
4396 print "</td>" .
4397 "</tr>\n";
4398 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
4399 print "<tr><td></td><td> $cd{'rfc2822'}" .
4400 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
4401 "</td></tr>\n";
4402 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
4403 print "<tr>" .
4404 "<td>tree</td>" .
4405 "<td class=\"sha1\">" .
4406 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
4407 class => "list"}, $co{'tree'}) .
4408 "</td>" .
4409 "<td class=\"link\">" .
4410 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
4411 "tree");
4412 if ($have_snapshot) {
4413 print " | " .
4414 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
4415 }
4416 print "</td>" .
4417 "</tr>\n";
4418
4419 foreach my $par (@$parents) {
4420 print "<tr>" .
4421 "<td>parent</td>" .
4422 "<td class=\"sha1\">" .
4423 $cgi->a({-href => href(action=>"commit", hash=>$par),
4424 class => "list"}, $par) .
4425 "</td>" .
4426 "<td class=\"link\">" .
4427 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
4428 " | " .
4429 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
4430 "</td>" .
4431 "</tr>\n";
4432 }
4433 print "</table>".
4434 "</div>\n";
4435
4436 print "<div class=\"page_body\">\n";
4437 git_print_log($co{'comment'});
4438 print "</div>\n";
4439
4440 git_difftree_body(\@difftree, $hash, @$parents);
4441
4442 git_footer_html();
4443 }
4444
4445 sub git_object {
4446 # object is defined by:
4447 # - hash or hash_base alone
4448 # - hash_base and file_name
4449 my $type;
4450
4451 # - hash or hash_base alone
4452 if ($hash || ($hash_base && !defined $file_name)) {
4453 my $object_id = $hash || $hash_base;
4454
4455 my $git_command = git_cmd_str();
4456 open my $fd, "-|", "$git_command cat-file -t $object_id 2>/dev/null"
4457 or die_error('404 Not Found', "Object does not exist");
4458 $type = <$fd>;
4459 chomp $type;
4460 close $fd
4461 or die_error('404 Not Found', "Object does not exist");
4462
4463 # - hash_base and file_name
4464 } elsif ($hash_base && defined $file_name) {
4465 $file_name =~ s,/+$,,;
4466
4467 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
4468 or die_error('404 Not Found', "Base object does not exist");
4469
4470 # here errors should not hapen
4471 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
4472 or die_error(undef, "Open git-ls-tree failed");
4473 my $line = <$fd>;
4474 close $fd;
4475
4476 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
4477 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
4478 die_error('404 Not Found', "File or directory for given base does not exist");
4479 }
4480 $type = $2;
4481 $hash = $3;
4482 } else {
4483 die_error('404 Not Found', "Not enough information to find object");
4484 }
4485
4486 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
4487 hash=>$hash, hash_base=>$hash_base,
4488 file_name=>$file_name),
4489 -status => '302 Found');
4490 }
4491
4492 sub git_blobdiff {
4493 my $format = shift || 'html';
4494
4495 my $fd;
4496 my @difftree;
4497 my %diffinfo;
4498 my $expires;
4499
4500 # preparing $fd and %diffinfo for git_patchset_body
4501 # new style URI
4502 if (defined $hash_base && defined $hash_parent_base) {
4503 if (defined $file_name) {
4504 # read raw output
4505 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4506 $hash_parent_base, $hash_base,
4507 "--", (defined $file_parent ? $file_parent : ()), $file_name
4508 or die_error(undef, "Open git-diff-tree failed");
4509 @difftree = map { chomp; $_ } <$fd>;
4510 close $fd
4511 or die_error(undef, "Reading git-diff-tree failed");
4512 @difftree
4513 or die_error('404 Not Found', "Blob diff not found");
4514
4515 } elsif (defined $hash &&
4516 $hash =~ /[0-9a-fA-F]{40}/) {
4517 # try to find filename from $hash
4518
4519 # read filtered raw output
4520 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4521 $hash_parent_base, $hash_base, "--"
4522 or die_error(undef, "Open git-diff-tree failed");
4523 @difftree =
4524 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
4525 # $hash == to_id
4526 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
4527 map { chomp; $_ } <$fd>;
4528 close $fd
4529 or die_error(undef, "Reading git-diff-tree failed");
4530 @difftree
4531 or die_error('404 Not Found', "Blob diff not found");
4532
4533 } else {
4534 die_error('404 Not Found', "Missing one of the blob diff parameters");
4535 }
4536
4537 if (@difftree > 1) {
4538 die_error('404 Not Found', "Ambiguous blob diff specification");
4539 }
4540
4541 %diffinfo = parse_difftree_raw_line($difftree[0]);
4542 $file_parent ||= $diffinfo{'from_file'} || $file_name || $diffinfo{'file'};
4543 $file_name ||= $diffinfo{'to_file'} || $diffinfo{'file'};
4544
4545 $hash_parent ||= $diffinfo{'from_id'};
4546 $hash ||= $diffinfo{'to_id'};
4547
4548 # non-textual hash id's can be cached
4549 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
4550 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
4551 $expires = '+1d';
4552 }
4553
4554 # open patch output
4555 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4556 '-p', ($format eq 'html' ? "--full-index" : ()),
4557 $hash_parent_base, $hash_base,
4558 "--", (defined $file_parent ? $file_parent : ()), $file_name
4559 or die_error(undef, "Open git-diff-tree failed");
4560 }
4561
4562 # old/legacy style URI
4563 if (!%diffinfo && # if new style URI failed
4564 defined $hash && defined $hash_parent) {
4565 # fake git-diff-tree raw output
4566 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
4567 $diffinfo{'from_id'} = $hash_parent;
4568 $diffinfo{'to_id'} = $hash;
4569 if (defined $file_name) {
4570 if (defined $file_parent) {
4571 $diffinfo{'status'} = '2';
4572 $diffinfo{'from_file'} = $file_parent;
4573 $diffinfo{'to_file'} = $file_name;
4574 } else { # assume not renamed
4575 $diffinfo{'status'} = '1';
4576 $diffinfo{'from_file'} = $file_name;
4577 $diffinfo{'to_file'} = $file_name;
4578 }
4579 } else { # no filename given
4580 $diffinfo{'status'} = '2';
4581 $diffinfo{'from_file'} = $hash_parent;
4582 $diffinfo{'to_file'} = $hash;
4583 }
4584
4585 # non-textual hash id's can be cached
4586 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
4587 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4588 $expires = '+1d';
4589 }
4590
4591 # open patch output
4592 open $fd, "-|", git_cmd(), "diff", @diff_opts,
4593 '-p', ($format eq 'html' ? "--full-index" : ()),
4594 $hash_parent, $hash, "--"
4595 or die_error(undef, "Open git-diff failed");
4596 } else {
4597 die_error('404 Not Found', "Missing one of the blob diff parameters")
4598 unless %diffinfo;
4599 }
4600
4601 # header
4602 if ($format eq 'html') {
4603 my $formats_nav =
4604 $cgi->a({-href => href(action=>"blobdiff_plain",
4605 hash=>$hash, hash_parent=>$hash_parent,
4606 hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
4607 file_name=>$file_name, file_parent=>$file_parent)},
4608 "raw");
4609 git_header_html(undef, $expires);
4610 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4611 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4612 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4613 } else {
4614 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
4615 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
4616 }
4617 if (defined $file_name) {
4618 git_print_page_path($file_name, "blob", $hash_base);
4619 } else {
4620 print "<div class=\"page_path\"></div>\n";
4621 }
4622
4623 } elsif ($format eq 'plain') {
4624 print $cgi->header(
4625 -type => 'text/plain',
4626 -charset => 'utf-8',
4627 -expires => $expires,
4628 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
4629
4630 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
4631
4632 } else {
4633 die_error(undef, "Unknown blobdiff format");
4634 }
4635
4636 # patch
4637 if ($format eq 'html') {
4638 print "<div class=\"page_body\">\n";
4639
4640 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
4641 close $fd;
4642
4643 print "</div>\n"; # class="page_body"
4644 git_footer_html();
4645
4646 } else {
4647 while (my $line = <$fd>) {
4648 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
4649 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
4650
4651 print $line;
4652
4653 last if $line =~ m!^\+\+\+!;
4654 }
4655 local $/ = undef;
4656 print <$fd>;
4657 close $fd;
4658 }
4659 }
4660
4661 sub git_blobdiff_plain {
4662 git_blobdiff('plain');
4663 }
4664
4665 sub git_commitdiff {
4666 my $format = shift || 'html';
4667 $hash ||= $hash_base || "HEAD";
4668 my %co = parse_commit($hash);
4669 if (!%co) {
4670 die_error(undef, "Unknown commit object");
4671 }
4672
4673 # choose format for commitdiff for merge
4674 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
4675 $hash_parent = '--cc';
4676 }
4677 # we need to prepare $formats_nav before almost any parameter munging
4678 my $formats_nav;
4679 if ($format eq 'html') {
4680 $formats_nav =
4681 $cgi->a({-href => href(action=>"commitdiff_plain",
4682 hash=>$hash, hash_parent=>$hash_parent)},
4683 "raw");
4684
4685 if (defined $hash_parent &&
4686 $hash_parent ne '-c' && $hash_parent ne '--cc') {
4687 # commitdiff with two commits given
4688 my $hash_parent_short = $hash_parent;
4689 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4690 $hash_parent_short = substr($hash_parent, 0, 7);
4691 }
4692 $formats_nav .=
4693 ' (from';
4694 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
4695 if ($co{'parents'}[$i] eq $hash_parent) {
4696 $formats_nav .= ' parent ' . ($i+1);
4697 last;
4698 }
4699 }
4700 $formats_nav .= ': ' .
4701 $cgi->a({-href => href(action=>"commitdiff",
4702 hash=>$hash_parent)},
4703 esc_html($hash_parent_short)) .
4704 ')';
4705 } elsif (!$co{'parent'}) {
4706 # --root commitdiff
4707 $formats_nav .= ' (initial)';
4708 } elsif (scalar @{$co{'parents'}} == 1) {
4709 # single parent commit
4710 $formats_nav .=
4711 ' (parent: ' .
4712 $cgi->a({-href => href(action=>"commitdiff",
4713 hash=>$co{'parent'})},
4714 esc_html(substr($co{'parent'}, 0, 7))) .
4715 ')';
4716 } else {
4717 # merge commit
4718 if ($hash_parent eq '--cc') {
4719 $formats_nav .= ' | ' .
4720 $cgi->a({-href => href(action=>"commitdiff",
4721 hash=>$hash, hash_parent=>'-c')},
4722 'combined');
4723 } else { # $hash_parent eq '-c'
4724 $formats_nav .= ' | ' .
4725 $cgi->a({-href => href(action=>"commitdiff",
4726 hash=>$hash, hash_parent=>'--cc')},
4727 'compact');
4728 }
4729 $formats_nav .=
4730 ' (merge: ' .
4731 join(' ', map {
4732 $cgi->a({-href => href(action=>"commitdiff",
4733 hash=>$_)},
4734 esc_html(substr($_, 0, 7)));
4735 } @{$co{'parents'}} ) .
4736 ')';
4737 }
4738 }
4739
4740 my $hash_parent_param = $hash_parent;
4741 if (!defined $hash_parent_param) {
4742 # --cc for multiple parents, --root for parentless
4743 $hash_parent_param =
4744 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
4745 }
4746
4747 # read commitdiff
4748 my $fd;
4749 my @difftree;
4750 if ($format eq 'html') {
4751 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4752 "--no-commit-id", "--patch-with-raw", "--full-index",
4753 $hash_parent_param, $hash, "--"
4754 or die_error(undef, "Open git-diff-tree failed");
4755
4756 while (my $line = <$fd>) {
4757 chomp $line;
4758 # empty line ends raw part of diff-tree output
4759 last unless $line;
4760 push @difftree, scalar parse_difftree_raw_line($line);
4761 }
4762
4763 } elsif ($format eq 'plain') {
4764 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4765 '-p', $hash_parent_param, $hash, "--"
4766 or die_error(undef, "Open git-diff-tree failed");
4767
4768 } else {
4769 die_error(undef, "Unknown commitdiff format");
4770 }
4771
4772 # non-textual hash id's can be cached
4773 my $expires;
4774 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4775 $expires = "+1d";
4776 }
4777
4778 # write commit message
4779 if ($format eq 'html') {
4780 my $refs = git_get_references();
4781 my $ref = format_ref_marker($refs, $co{'id'});
4782
4783 git_header_html(undef, $expires);
4784 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
4785 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
4786 git_print_authorship(\%co);
4787 print "<div class=\"page_body\">\n";
4788 if (@{$co{'comment'}} > 1) {
4789 print "<div class=\"log\">\n";
4790 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
4791 print "</div>\n"; # class="log"
4792 }
4793
4794 } elsif ($format eq 'plain') {
4795 my $refs = git_get_references("tags");
4796 my $tagname = git_get_rev_name_tags($hash);
4797 my $filename = basename($project) . "-$hash.patch";
4798
4799 print $cgi->header(
4800 -type => 'text/plain',
4801 -charset => 'utf-8',
4802 -expires => $expires,
4803 -content_disposition => 'inline; filename="' . "$filename" . '"');
4804 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4805 print <<TEXT;
4806 From: $co{'author'}
4807 Date: $ad{'rfc2822'} ($ad{'tz_local'})
4808 Subject: $co{'title'}
4809 TEXT
4810 print "X-Git-Tag: $tagname\n" if $tagname;
4811 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
4812
4813 foreach my $line (@{$co{'comment'}}) {
4814 print "$line\n";
4815 }
4816 print "---\n\n";
4817 }
4818
4819 # write patch
4820 if ($format eq 'html') {
4821 my $use_parents = !defined $hash_parent ||
4822 $hash_parent eq '-c' || $hash_parent eq '--cc';
4823 git_difftree_body(\@difftree, $hash,
4824 $use_parents ? @{$co{'parents'}} : $hash_parent);
4825 print "<br/>\n";
4826
4827 git_patchset_body($fd, \@difftree, $hash,
4828 $use_parents ? @{$co{'parents'}} : $hash_parent);
4829 close $fd;
4830 print "</div>\n"; # class="page_body"
4831 git_footer_html();
4832
4833 } elsif ($format eq 'plain') {
4834 local $/ = undef;
4835 print <$fd>;
4836 close $fd
4837 or print "Reading git-diff-tree failed\n";
4838 }
4839 }
4840
4841 sub git_commitdiff_plain {
4842 git_commitdiff('plain');
4843 }
4844
4845 sub git_history {
4846 if (!defined $hash_base) {
4847 $hash_base = git_get_head_hash($project);
4848 }
4849 if (!defined $page) {
4850 $page = 0;
4851 }
4852 my $ftype;
4853 my %co = parse_commit($hash_base);
4854 if (!%co) {
4855 die_error(undef, "Unknown commit object");
4856 }
4857
4858 my $refs = git_get_references();
4859 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
4860
4861 if (!defined $hash && defined $file_name) {
4862 $hash = git_get_hash_by_path($hash_base, $file_name);
4863 }
4864 if (defined $hash) {
4865 $ftype = git_get_type($hash);
4866 }
4867
4868 my @commitlist = parse_commits($hash_base, 101, (100 * $page), "--full-history", $file_name);
4869
4870 my $paging_nav = '';
4871 if ($page > 0) {
4872 $paging_nav .=
4873 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4874 file_name=>$file_name)},
4875 "first");
4876 $paging_nav .= " &sdot; " .
4877 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4878 file_name=>$file_name, page=>$page-1),
4879 -accesskey => "p", -title => "Alt-p"}, "prev");
4880 } else {
4881 $paging_nav .= "first";
4882 $paging_nav .= " &sdot; prev";
4883 }
4884 if ($#commitlist >= 100) {
4885 $paging_nav .= " &sdot; " .
4886 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4887 file_name=>$file_name, page=>$page+1),
4888 -accesskey => "n", -title => "Alt-n"}, "next");
4889 } else {
4890 $paging_nav .= " &sdot; next";
4891 }
4892 my $next_link = '';
4893 if ($#commitlist >= 100) {
4894 $next_link =
4895 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4896 file_name=>$file_name, page=>$page+1),
4897 -accesskey => "n", -title => "Alt-n"}, "next");
4898 }
4899
4900 git_header_html();
4901 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
4902 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4903 git_print_page_path($file_name, $ftype, $hash_base);
4904
4905 git_history_body(\@commitlist, 0, 99,
4906 $refs, $hash_base, $ftype, $next_link);
4907
4908 git_footer_html();
4909 }
4910
4911 sub git_search {
4912 my ($have_search) = gitweb_check_feature('search');
4913 if (!$have_search) {
4914 die_error('403 Permission denied', "Permission denied");
4915 }
4916 if (!defined $searchtext) {
4917 die_error(undef, "Text field empty");
4918 }
4919 if (!defined $hash) {
4920 $hash = git_get_head_hash($project);
4921 }
4922 my %co = parse_commit($hash);
4923 if (!%co) {
4924 die_error(undef, "Unknown commit object");
4925 }
4926 if (!defined $page) {
4927 $page = 0;
4928 }
4929
4930 $searchtype ||= 'commit';
4931 if ($searchtype eq 'pickaxe') {
4932 # pickaxe may take all resources of your box and run for several minutes
4933 # with every query - so decide by yourself how public you make this feature
4934 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
4935 if (!$have_pickaxe) {
4936 die_error('403 Permission denied', "Permission denied");
4937 }
4938 }
4939 if ($searchtype eq 'grep') {
4940 my ($have_grep) = gitweb_check_feature('grep');
4941 if (!$have_grep) {
4942 die_error('403 Permission denied', "Permission denied");
4943 }
4944 }
4945
4946 git_header_html();
4947
4948 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
4949 my $greptype;
4950 if ($searchtype eq 'commit') {
4951 $greptype = "--grep=";
4952 } elsif ($searchtype eq 'author') {
4953 $greptype = "--author=";
4954 } elsif ($searchtype eq 'committer') {
4955 $greptype = "--committer=";
4956 }
4957 $greptype .= $search_regexp;
4958 my @commitlist = parse_commits($hash, 101, (100 * $page), $greptype);
4959
4960 my $paging_nav = '';
4961 if ($page > 0) {
4962 $paging_nav .=
4963 $cgi->a({-href => href(action=>"search", hash=>$hash,
4964 searchtext=>$searchtext, searchtype=>$searchtype)},
4965 "first");
4966 $paging_nav .= " &sdot; " .
4967 $cgi->a({-href => href(action=>"search", hash=>$hash,
4968 searchtext=>$searchtext, searchtype=>$searchtype,
4969 page=>$page-1),
4970 -accesskey => "p", -title => "Alt-p"}, "prev");
4971 } else {
4972 $paging_nav .= "first";
4973 $paging_nav .= " &sdot; prev";
4974 }
4975 if ($#commitlist >= 100) {
4976 $paging_nav .= " &sdot; " .
4977 $cgi->a({-href => href(action=>"search", hash=>$hash,
4978 searchtext=>$searchtext, searchtype=>$searchtype,
4979 page=>$page+1),
4980 -accesskey => "n", -title => "Alt-n"}, "next");
4981 } else {
4982 $paging_nav .= " &sdot; next";
4983 }
4984 my $next_link = '';
4985 if ($#commitlist >= 100) {
4986 $next_link =
4987 $cgi->a({-href => href(action=>"search", hash=>$hash,
4988 searchtext=>$searchtext, searchtype=>$searchtype,
4989 page=>$page+1),
4990 -accesskey => "n", -title => "Alt-n"}, "next");
4991 }
4992
4993 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
4994 git_print_header_div('commit', esc_html($co{'title'}), $hash);
4995 git_search_grep_body(\@commitlist, 0, 99, $next_link);
4996 }
4997
4998 if ($searchtype eq 'pickaxe') {
4999 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5000 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5001
5002 print "<table cellspacing=\"0\">\n";
5003 my $alternate = 1;
5004 $/ = "\n";
5005 my $git_command = git_cmd_str();
5006 my $searchqtext = $searchtext;
5007 $searchqtext =~ s/'/'\\''/;
5008 open my $fd, "-|", "$git_command rev-list $hash | " .
5009 "$git_command diff-tree -r --stdin -S\'$searchqtext\'";
5010 undef %co;
5011 my @files;
5012 while (my $line = <$fd>) {
5013 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
5014 my %set;
5015 $set{'file'} = $6;
5016 $set{'from_id'} = $3;
5017 $set{'to_id'} = $4;
5018 $set{'id'} = $set{'to_id'};
5019 if ($set{'id'} =~ m/0{40}/) {
5020 $set{'id'} = $set{'from_id'};
5021 }
5022 if ($set{'id'} =~ m/0{40}/) {
5023 next;
5024 }
5025 push @files, \%set;
5026 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
5027 if (%co) {
5028 if ($alternate) {
5029 print "<tr class=\"dark\">\n";
5030 } else {
5031 print "<tr class=\"light\">\n";
5032 }
5033 $alternate ^= 1;
5034 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5035 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
5036 "<td>" .
5037 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
5038 -class => "list subject"},
5039 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
5040 while (my $setref = shift @files) {
5041 my %set = %$setref;
5042 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
5043 hash=>$set{'id'}, file_name=>$set{'file'}),
5044 -class => "list"},
5045 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
5046 "<br/>\n";
5047 }
5048 print "</td>\n" .
5049 "<td class=\"link\">" .
5050 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5051 " | " .
5052 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5053 print "</td>\n" .
5054 "</tr>\n";
5055 }
5056 %co = parse_commit($1);
5057 }
5058 }
5059 close $fd;
5060
5061 print "</table>\n";
5062 }
5063
5064 if ($searchtype eq 'grep') {
5065 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5066 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5067
5068 print "<table cellspacing=\"0\">\n";
5069 my $alternate = 1;
5070 my $matches = 0;
5071 $/ = "\n";
5072 open my $fd, "-|", git_cmd(), 'grep', '-n', '-i', '-E', $searchtext, $co{'tree'};
5073 my $lastfile = '';
5074 while (my $line = <$fd>) {
5075 chomp $line;
5076 my ($file, $lno, $ltext, $binary);
5077 last if ($matches++ > 1000);
5078 if ($line =~ /^Binary file (.+) matches$/) {
5079 $file = $1;
5080 $binary = 1;
5081 } else {
5082 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
5083 }
5084 if ($file ne $lastfile) {
5085 $lastfile and print "</td></tr>\n";
5086 if ($alternate++) {
5087 print "<tr class=\"dark\">\n";
5088 } else {
5089 print "<tr class=\"light\">\n";
5090 }
5091 print "<td class=\"list\">".
5092 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5093 file_name=>"$file"),
5094 -class => "list"}, esc_path($file));
5095 print "</td><td>\n";
5096 $lastfile = $file;
5097 }
5098 if ($binary) {
5099 print "<div class=\"binary\">Binary file</div>\n";
5100 } else {
5101 $ltext = untabify($ltext);
5102 if ($ltext =~ m/^(.*)($searchtext)(.*)$/i) {
5103 $ltext = esc_html($1, -nbsp=>1);
5104 $ltext .= '<span class="match">';
5105 $ltext .= esc_html($2, -nbsp=>1);
5106 $ltext .= '</span>';
5107 $ltext .= esc_html($3, -nbsp=>1);
5108 } else {
5109 $ltext = esc_html($ltext, -nbsp=>1);
5110 }
5111 print "<div class=\"pre\">" .
5112 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5113 file_name=>"$file").'#l'.$lno,
5114 -class => "linenr"}, sprintf('%4i', $lno))
5115 . ' ' . $ltext . "</div>\n";
5116 }
5117 }
5118 if ($lastfile) {
5119 print "</td></tr>\n";
5120 if ($matches > 1000) {
5121 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
5122 }
5123 } else {
5124 print "<div class=\"diff nodifferences\">No matches found</div>\n";
5125 }
5126 close $fd;
5127
5128 print "</table>\n";
5129 }
5130 git_footer_html();
5131 }
5132
5133 sub git_search_help {
5134 git_header_html();
5135 git_print_page_nav('','', $hash,$hash,$hash);
5136 print <<EOT;
5137 <dl>
5138 <dt><b>commit</b></dt>
5139 <dd>The commit messages and authorship information will be scanned for the given string.</dd>
5140 EOT
5141 my ($have_grep) = gitweb_check_feature('grep');
5142 if ($have_grep) {
5143 print <<EOT;
5144 <dt><b>grep</b></dt>
5145 <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
5146 a different one) are searched for the given
5147 <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a>
5148 (POSIX extended) and the matches are listed. On large
5149 trees, this search can take a while and put some strain on the server, so please use it with
5150 some consideration.</dd>
5151 EOT
5152 }
5153 print <<EOT;
5154 <dt><b>author</b></dt>
5155 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given string.</dd>
5156 <dt><b>committer</b></dt>
5157 <dd>Name and e-mail of the committer and date of commit will be scanned for the given string.</dd>
5158 EOT
5159 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
5160 if ($have_pickaxe) {
5161 print <<EOT;
5162 <dt><b>pickaxe</b></dt>
5163 <dd>All commits that caused the string to appear or disappear from any file (changes that
5164 added, removed or "modified" the string) will be listed. This search can take a while and
5165 takes a lot of strain on the server, so please use it wisely.</dd>
5166 EOT
5167 }
5168 print "</dl>\n";
5169 git_footer_html();
5170 }
5171
5172 sub git_shortlog {
5173 my $head = git_get_head_hash($project);
5174 if (!defined $hash) {
5175 $hash = $head;
5176 }
5177 if (!defined $page) {
5178 $page = 0;
5179 }
5180 my $refs = git_get_references();
5181
5182 my @commitlist = parse_commits($hash, 101, (100 * $page));
5183
5184 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, (100 * ($page+1)));
5185 my $next_link = '';
5186 if ($#commitlist >= 100) {
5187 $next_link =
5188 $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
5189 -accesskey => "n", -title => "Alt-n"}, "next");
5190 }
5191
5192 git_header_html();
5193 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
5194 git_print_header_div('summary', $project);
5195
5196 git_shortlog_body(\@commitlist, 0, 99, $refs, $next_link);
5197
5198 git_footer_html();
5199 }
5200
5201 ## ......................................................................
5202 ## feeds (RSS, Atom; OPML)
5203
5204 sub git_feed {
5205 my $format = shift || 'atom';
5206 my ($have_blame) = gitweb_check_feature('blame');
5207
5208 # Atom: http://www.atomenabled.org/developers/syndication/
5209 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
5210 if ($format ne 'rss' && $format ne 'atom') {
5211 die_error(undef, "Unknown web feed format");
5212 }
5213
5214 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
5215 my $head = $hash || 'HEAD';
5216 my @commitlist = parse_commits($head, 150);
5217
5218 my %latest_commit;
5219 my %latest_date;
5220 my $content_type = "application/$format+xml";
5221 if (defined $cgi->http('HTTP_ACCEPT') &&
5222 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
5223 # browser (feed reader) prefers text/xml
5224 $content_type = 'text/xml';
5225 }
5226 if (defined($commitlist[0])) {
5227 %latest_commit = %{$commitlist[0]};
5228 %latest_date = parse_date($latest_commit{'author_epoch'});
5229 print $cgi->header(
5230 -type => $content_type,
5231 -charset => 'utf-8',
5232 -last_modified => $latest_date{'rfc2822'});
5233 } else {
5234 print $cgi->header(
5235 -type => $content_type,
5236 -charset => 'utf-8');
5237 }
5238
5239 # Optimization: skip generating the body if client asks only
5240 # for Last-Modified date.
5241 return if ($cgi->request_method() eq 'HEAD');
5242
5243 # header variables
5244 my $title = "$site_name - $project/$action";
5245 my $feed_type = 'log';
5246 if (defined $hash) {
5247 $title .= " - '$hash'";
5248 $feed_type = 'branch log';
5249 if (defined $file_name) {
5250 $title .= " :: $file_name";
5251 $feed_type = 'history';
5252 }
5253 } elsif (defined $file_name) {
5254 $title .= " - $file_name";
5255 $feed_type = 'history';
5256 }
5257 $title .= " $feed_type";
5258 my $descr = git_get_project_description($project);
5259 if (defined $descr) {
5260 $descr = esc_html($descr);
5261 } else {
5262 $descr = "$project " .
5263 ($format eq 'rss' ? 'RSS' : 'Atom') .
5264 " feed";
5265 }
5266 my $owner = git_get_project_owner($project);
5267 $owner = esc_html($owner);
5268
5269 #header
5270 my $alt_url;
5271 if (defined $file_name) {
5272 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
5273 } elsif (defined $hash) {
5274 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
5275 } else {
5276 $alt_url = href(-full=>1, action=>"summary");
5277 }
5278 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
5279 if ($format eq 'rss') {
5280 print <<XML;
5281 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
5282 <channel>
5283 XML
5284 print "<title>$title</title>\n" .
5285 "<link>$alt_url</link>\n" .
5286 "<description>$descr</description>\n" .
5287 "<language>en</language>\n";
5288 } elsif ($format eq 'atom') {
5289 print <<XML;
5290 <feed xmlns="http://www.w3.org/2005/Atom">
5291 XML
5292 print "<title>$title</title>\n" .
5293 "<subtitle>$descr</subtitle>\n" .
5294 '<link rel="alternate" type="text/html" href="' .
5295 $alt_url . '" />' . "\n" .
5296 '<link rel="self" type="' . $content_type . '" href="' .
5297 $cgi->self_url() . '" />' . "\n" .
5298 "<id>" . href(-full=>1) . "</id>\n" .
5299 # use project owner for feed author
5300 "<author><name>$owner</name></author>\n";
5301 if (defined $favicon) {
5302 print "<icon>" . esc_url($favicon) . "</icon>\n";
5303 }
5304 if (defined $logo_url) {
5305 # not twice as wide as tall: 72 x 27 pixels
5306 print "<logo>" . esc_url($logo) . "</logo>\n";
5307 }
5308 if (! %latest_date) {
5309 # dummy date to keep the feed valid until commits trickle in:
5310 print "<updated>1970-01-01T00:00:00Z</updated>\n";
5311 } else {
5312 print "<updated>$latest_date{'iso-8601'}</updated>\n";
5313 }
5314 }
5315
5316 # contents
5317 for (my $i = 0; $i <= $#commitlist; $i++) {
5318 my %co = %{$commitlist[$i]};
5319 my $commit = $co{'id'};
5320 # we read 150, we always show 30 and the ones more recent than 48 hours
5321 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
5322 last;
5323 }
5324 my %cd = parse_date($co{'author_epoch'});
5325
5326 # get list of changed files
5327 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5328 $co{'parent'} || "--root",
5329 $co{'id'}, "--", (defined $file_name ? $file_name : ())
5330 or next;
5331 my @difftree = map { chomp; $_ } <$fd>;
5332 close $fd
5333 or next;
5334
5335 # print element (entry, item)
5336 my $co_url = href(-full=>1, action=>"commit", hash=>$commit);
5337 if ($format eq 'rss') {
5338 print "<item>\n" .
5339 "<title>" . esc_html($co{'title'}) . "</title>\n" .
5340 "<author>" . esc_html($co{'author'}) . "</author>\n" .
5341 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
5342 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
5343 "<link>$co_url</link>\n" .
5344 "<description>" . esc_html($co{'title'}) . "</description>\n" .
5345 "<content:encoded>" .
5346 "<![CDATA[\n";
5347 } elsif ($format eq 'atom') {
5348 print "<entry>\n" .
5349 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
5350 "<updated>$cd{'iso-8601'}</updated>\n" .
5351 "<author>\n" .
5352 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
5353 if ($co{'author_email'}) {
5354 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
5355 }
5356 print "</author>\n" .
5357 # use committer for contributor
5358 "<contributor>\n" .
5359 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
5360 if ($co{'committer_email'}) {
5361 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
5362 }
5363 print "</contributor>\n" .
5364 "<published>$cd{'iso-8601'}</published>\n" .
5365 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
5366 "<id>$co_url</id>\n" .
5367 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
5368 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
5369 }
5370 my $comment = $co{'comment'};
5371 print "<pre>\n";
5372 foreach my $line (@$comment) {
5373 $line = esc_html($line);
5374 print "$line\n";
5375 }
5376 print "</pre><ul>\n";
5377 foreach my $difftree_line (@difftree) {
5378 my %difftree = parse_difftree_raw_line($difftree_line);
5379 next if !$difftree{'from_id'};
5380
5381 my $file = $difftree{'file'} || $difftree{'to_file'};
5382
5383 print "<li>" .
5384 "[" .
5385 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
5386 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
5387 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
5388 file_name=>$file, file_parent=>$difftree{'from_file'}),
5389 -title => "diff"}, 'D');
5390 if ($have_blame) {
5391 print $cgi->a({-href => href(-full=>1, action=>"blame",
5392 file_name=>$file, hash_base=>$commit),
5393 -title => "blame"}, 'B');
5394 }
5395 # if this is not a feed of a file history
5396 if (!defined $file_name || $file_name ne $file) {
5397 print $cgi->a({-href => href(-full=>1, action=>"history",
5398 file_name=>$file, hash=>$commit),
5399 -title => "history"}, 'H');
5400 }
5401 $file = esc_path($file);
5402 print "] ".
5403 "$file</li>\n";
5404 }
5405 if ($format eq 'rss') {
5406 print "</ul>]]>\n" .
5407 "</content:encoded>\n" .
5408 "</item>\n";
5409 } elsif ($format eq 'atom') {
5410 print "</ul>\n</div>\n" .
5411 "</content>\n" .
5412 "</entry>\n";
5413 }
5414 }
5415
5416 # end of feed
5417 if ($format eq 'rss') {
5418 print "</channel>\n</rss>\n";
5419 } elsif ($format eq 'atom') {
5420 print "</feed>\n";
5421 }
5422 }
5423
5424 sub git_rss {
5425 git_feed('rss');
5426 }
5427
5428 sub git_atom {
5429 git_feed('atom');
5430 }
5431
5432 sub git_opml {
5433 my @list = git_get_projects_list();
5434
5435 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
5436 print <<XML;
5437 <?xml version="1.0" encoding="utf-8"?>
5438 <opml version="1.0">
5439 <head>
5440 <title>$site_name OPML Export</title>
5441 </head>
5442 <body>
5443 <outline text="git RSS feeds">
5444 XML
5445
5446 foreach my $pr (@list) {
5447 my %proj = %$pr;
5448 my $head = git_get_head_hash($proj{'path'});
5449 if (!defined $head) {
5450 next;
5451 }
5452 $git_dir = "$projectroot/$proj{'path'}";
5453 my %co = parse_commit($head);
5454 if (!%co) {
5455 next;
5456 }
5457
5458 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
5459 my $rss = "$my_url?p=$proj{'path'};a=rss";
5460 my $html = "$my_url?p=$proj{'path'};a=summary";
5461 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
5462 }
5463 print <<XML;
5464 </outline>
5465 </body>
5466 </opml>
5467 XML
5468 }
This page took 5.053299 seconds and 5 git commands to generate.