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