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