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