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