]> Lady’s Gitweb - Gitweb/blob - gitweb.perl
gitweb: Fix "Use of uninitialized value" warning in git_feed
[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 # You define site-wide feature defaults here; override them with
99 # $GITWEB_CONFIG as necessary.
100 our %feature = (
101 # feature => {
102 # 'sub' => feature-sub (subroutine),
103 # 'override' => allow-override (boolean),
104 # 'default' => [ default options...] (array reference)}
105 #
106 # if feature is overridable (it means that allow-override has true value),
107 # then feature-sub will be called with default options as parameters;
108 # return value of feature-sub indicates if to enable specified feature
109 #
110 # if there is no 'sub' key (no feature-sub), then feature cannot be
111 # overriden
112 #
113 # use gitweb_check_feature(<feature>) to check if <feature> is enabled
114
115 # Enable the 'blame' blob view, showing the last commit that modified
116 # each line in the file. This can be very CPU-intensive.
117
118 # To enable system wide have in $GITWEB_CONFIG
119 # $feature{'blame'}{'default'} = [1];
120 # To have project specific config enable override in $GITWEB_CONFIG
121 # $feature{'blame'}{'override'} = 1;
122 # and in project config gitweb.blame = 0|1;
123 'blame' => {
124 'sub' => \&feature_blame,
125 'override' => 0,
126 'default' => [0]},
127
128 # Enable the 'snapshot' link, providing a compressed tarball of any
129 # tree. This can potentially generate high traffic if you have large
130 # project.
131
132 # To disable system wide have in $GITWEB_CONFIG
133 # $feature{'snapshot'}{'default'} = [undef];
134 # To have project specific config enable override in $GITWEB_CONFIG
135 # $feature{'snapshot'}{'override'} = 1;
136 # and in project config gitweb.snapshot = none|gzip|bzip2;
137 'snapshot' => {
138 'sub' => \&feature_snapshot,
139 'override' => 0,
140 # => [content-encoding, suffix, program]
141 'default' => ['x-gzip', 'gz', 'gzip']},
142
143 # Enable text search, which will list the commits which match author,
144 # committer or commit text to a given string. Enabled by default.
145 # Project specific override is not supported.
146 'search' => {
147 'override' => 0,
148 'default' => [1]},
149
150 # Enable grep search, which will list the files in currently selected
151 # tree containing the given string. Enabled by default. This can be
152 # potentially CPU-intensive, of course.
153
154 # To enable system wide have in $GITWEB_CONFIG
155 # $feature{'grep'}{'default'} = [1];
156 # To have project specific config enable override in $GITWEB_CONFIG
157 # $feature{'grep'}{'override'} = 1;
158 # and in project config gitweb.grep = 0|1;
159 'grep' => {
160 'override' => 0,
161 'default' => [1]},
162
163 # Enable the pickaxe search, which will list the commits that modified
164 # a given string in a file. This can be practical and quite faster
165 # alternative to 'blame', but still potentially CPU-intensive.
166
167 # To enable system wide have in $GITWEB_CONFIG
168 # $feature{'pickaxe'}{'default'} = [1];
169 # To have project specific config enable override in $GITWEB_CONFIG
170 # $feature{'pickaxe'}{'override'} = 1;
171 # and in project config gitweb.pickaxe = 0|1;
172 'pickaxe' => {
173 'sub' => \&feature_pickaxe,
174 'override' => 0,
175 'default' => [1]},
176
177 # Make gitweb use an alternative format of the URLs which can be
178 # more readable and natural-looking: project name is embedded
179 # directly in the path and the query string contains other
180 # auxiliary information. All gitweb installations recognize
181 # URL in either format; this configures in which formats gitweb
182 # generates links.
183
184 # To enable system wide have in $GITWEB_CONFIG
185 # $feature{'pathinfo'}{'default'} = [1];
186 # Project specific override is not supported.
187
188 # Note that you will need to change the default location of CSS,
189 # favicon, logo and possibly other files to an absolute URL. Also,
190 # if gitweb.cgi serves as your indexfile, you will need to force
191 # $my_uri to contain the script name in your $GITWEB_CONFIG.
192 'pathinfo' => {
193 'override' => 0,
194 'default' => [0]},
195
196 # Make gitweb consider projects in project root subdirectories
197 # to be forks of existing projects. Given project $projname.git,
198 # projects matching $projname/*.git will not be shown in the main
199 # projects list, instead a '+' mark will be added to $projname
200 # there and a 'forks' view will be enabled for the project, listing
201 # all the forks. If project list is taken from a file, forks have
202 # to be listed after the main project.
203
204 # To enable system wide have in $GITWEB_CONFIG
205 # $feature{'forks'}{'default'} = [1];
206 # Project specific override is not supported.
207 'forks' => {
208 'override' => 0,
209 'default' => [0]},
210 );
211
212 sub gitweb_check_feature {
213 my ($name) = @_;
214 return unless exists $feature{$name};
215 my ($sub, $override, @defaults) = (
216 $feature{$name}{'sub'},
217 $feature{$name}{'override'},
218 @{$feature{$name}{'default'}});
219 if (!$override) { return @defaults; }
220 if (!defined $sub) {
221 warn "feature $name is not overrideable";
222 return @defaults;
223 }
224 return $sub->(@defaults);
225 }
226
227 sub feature_blame {
228 my ($val) = git_get_project_config('blame', '--bool');
229
230 if ($val eq 'true') {
231 return 1;
232 } elsif ($val eq 'false') {
233 return 0;
234 }
235
236 return $_[0];
237 }
238
239 sub feature_snapshot {
240 my ($ctype, $suffix, $command) = @_;
241
242 my ($val) = git_get_project_config('snapshot');
243
244 if ($val eq 'gzip') {
245 return ('x-gzip', 'gz', 'gzip');
246 } elsif ($val eq 'bzip2') {
247 return ('x-bzip2', 'bz2', 'bzip2');
248 } elsif ($val eq 'none') {
249 return ();
250 }
251
252 return ($ctype, $suffix, $command);
253 }
254
255 sub gitweb_have_snapshot {
256 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
257 my $have_snapshot = (defined $ctype && defined $suffix);
258
259 return $have_snapshot;
260 }
261
262 sub feature_grep {
263 my ($val) = git_get_project_config('grep', '--bool');
264
265 if ($val eq 'true') {
266 return (1);
267 } elsif ($val eq 'false') {
268 return (0);
269 }
270
271 return ($_[0]);
272 }
273
274 sub feature_pickaxe {
275 my ($val) = git_get_project_config('pickaxe', '--bool');
276
277 if ($val eq 'true') {
278 return (1);
279 } elsif ($val eq 'false') {
280 return (0);
281 }
282
283 return ($_[0]);
284 }
285
286 # checking HEAD file with -e is fragile if the repository was
287 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
288 # and then pruned.
289 sub check_head_link {
290 my ($dir) = @_;
291 my $headfile = "$dir/HEAD";
292 return ((-e $headfile) ||
293 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
294 }
295
296 sub check_export_ok {
297 my ($dir) = @_;
298 return (check_head_link($dir) &&
299 (!$export_ok || -e "$dir/$export_ok"));
300 }
301
302 # rename detection options for git-diff and git-diff-tree
303 # - default is '-M', with the cost proportional to
304 # (number of removed files) * (number of new files).
305 # - more costly is '-C' (or '-C', '-M'), with the cost proportional to
306 # (number of changed files + number of removed files) * (number of new files)
307 # - even more costly is '-C', '--find-copies-harder' with cost
308 # (number of files in the original tree) * (number of new files)
309 # - one might want to include '-B' option, e.g. '-B', '-M'
310 our @diff_opts = ('-M'); # taken from git_commit
311
312 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
313 do $GITWEB_CONFIG if -e $GITWEB_CONFIG;
314
315 # version of the core git binary
316 our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
317
318 $projects_list ||= $projectroot;
319
320 # ======================================================================
321 # input validation and dispatch
322 our $action = $cgi->param('a');
323 if (defined $action) {
324 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
325 die_error(undef, "Invalid action parameter");
326 }
327 }
328
329 # parameters which are pathnames
330 our $project = $cgi->param('p');
331 if (defined $project) {
332 if (!validate_pathname($project) ||
333 !(-d "$projectroot/$project") ||
334 !check_head_link("$projectroot/$project") ||
335 ($export_ok && !(-e "$projectroot/$project/$export_ok")) ||
336 ($strict_export && !project_in_list($project))) {
337 undef $project;
338 die_error(undef, "No such project");
339 }
340 }
341
342 our $file_name = $cgi->param('f');
343 if (defined $file_name) {
344 if (!validate_pathname($file_name)) {
345 die_error(undef, "Invalid file parameter");
346 }
347 }
348
349 our $file_parent = $cgi->param('fp');
350 if (defined $file_parent) {
351 if (!validate_pathname($file_parent)) {
352 die_error(undef, "Invalid file parent parameter");
353 }
354 }
355
356 # parameters which are refnames
357 our $hash = $cgi->param('h');
358 if (defined $hash) {
359 if (!validate_refname($hash)) {
360 die_error(undef, "Invalid hash parameter");
361 }
362 }
363
364 our $hash_parent = $cgi->param('hp');
365 if (defined $hash_parent) {
366 if (!validate_refname($hash_parent)) {
367 die_error(undef, "Invalid hash parent parameter");
368 }
369 }
370
371 our $hash_base = $cgi->param('hb');
372 if (defined $hash_base) {
373 if (!validate_refname($hash_base)) {
374 die_error(undef, "Invalid hash base parameter");
375 }
376 }
377
378 our $hash_parent_base = $cgi->param('hpb');
379 if (defined $hash_parent_base) {
380 if (!validate_refname($hash_parent_base)) {
381 die_error(undef, "Invalid hash parent base parameter");
382 }
383 }
384
385 # other parameters
386 our $page = $cgi->param('pg');
387 if (defined $page) {
388 if ($page =~ m/[^0-9]/) {
389 die_error(undef, "Invalid page parameter");
390 }
391 }
392
393 our $searchtype = $cgi->param('st');
394 if (defined $searchtype) {
395 if ($searchtype =~ m/[^a-z]/) {
396 die_error(undef, "Invalid searchtype parameter");
397 }
398 }
399
400 our $searchtext = $cgi->param('s');
401 our $search_regexp;
402 if (defined $searchtext) {
403 if ($searchtype ne 'grep' and $searchtype ne 'pickaxe' and $searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
404 die_error(undef, "Invalid search parameter");
405 }
406 if (length($searchtext) < 2) {
407 die_error(undef, "At least two characters are required for search parameter");
408 }
409 $search_regexp = quotemeta $searchtext;
410 }
411
412 # now read PATH_INFO and use it as alternative to parameters
413 sub evaluate_path_info {
414 return if defined $project;
415 my $path_info = $ENV{"PATH_INFO"};
416 return if !$path_info;
417 $path_info =~ s,^/+,,;
418 return if !$path_info;
419 # find which part of PATH_INFO is project
420 $project = $path_info;
421 $project =~ s,/+$,,;
422 while ($project && !check_head_link("$projectroot/$project")) {
423 $project =~ s,/*[^/]*$,,;
424 }
425 # validate project
426 $project = validate_pathname($project);
427 if (!$project ||
428 ($export_ok && !-e "$projectroot/$project/$export_ok") ||
429 ($strict_export && !project_in_list($project))) {
430 undef $project;
431 return;
432 }
433 # do not change any parameters if an action is given using the query string
434 return if $action;
435 $path_info =~ s,^$project/*,,;
436 my ($refname, $pathname) = split(/:/, $path_info, 2);
437 if (defined $pathname) {
438 # we got "project.git/branch:filename" or "project.git/branch:dir/"
439 # we could use git_get_type(branch:pathname), but it needs $git_dir
440 $pathname =~ s,^/+,,;
441 if (!$pathname || substr($pathname, -1) eq "/") {
442 $action ||= "tree";
443 $pathname =~ s,/$,,;
444 } else {
445 $action ||= "blob_plain";
446 }
447 $hash_base ||= validate_refname($refname);
448 $file_name ||= validate_pathname($pathname);
449 } elsif (defined $refname) {
450 # we got "project.git/branch"
451 $action ||= "shortlog";
452 $hash ||= validate_refname($refname);
453 }
454 }
455 evaluate_path_info();
456
457 # path to the current git repository
458 our $git_dir;
459 $git_dir = "$projectroot/$project" if $project;
460
461 # dispatch
462 my %actions = (
463 "blame" => \&git_blame2,
464 "blobdiff" => \&git_blobdiff,
465 "blobdiff_plain" => \&git_blobdiff_plain,
466 "blob" => \&git_blob,
467 "blob_plain" => \&git_blob_plain,
468 "commitdiff" => \&git_commitdiff,
469 "commitdiff_plain" => \&git_commitdiff_plain,
470 "commit" => \&git_commit,
471 "forks" => \&git_forks,
472 "heads" => \&git_heads,
473 "history" => \&git_history,
474 "log" => \&git_log,
475 "rss" => \&git_rss,
476 "atom" => \&git_atom,
477 "search" => \&git_search,
478 "search_help" => \&git_search_help,
479 "shortlog" => \&git_shortlog,
480 "summary" => \&git_summary,
481 "tag" => \&git_tag,
482 "tags" => \&git_tags,
483 "tree" => \&git_tree,
484 "snapshot" => \&git_snapshot,
485 "object" => \&git_object,
486 # those below don't need $project
487 "opml" => \&git_opml,
488 "project_list" => \&git_project_list,
489 "project_index" => \&git_project_index,
490 );
491
492 if (!defined $action) {
493 if (defined $hash) {
494 $action = git_get_type($hash);
495 } elsif (defined $hash_base && defined $file_name) {
496 $action = git_get_type("$hash_base:$file_name");
497 } elsif (defined $project) {
498 $action = 'summary';
499 } else {
500 $action = 'project_list';
501 }
502 }
503 if (!defined($actions{$action})) {
504 die_error(undef, "Unknown action");
505 }
506 if ($action !~ m/^(opml|project_list|project_index)$/ &&
507 !$project) {
508 die_error(undef, "Project needed");
509 }
510 $actions{$action}->();
511 exit;
512
513 ## ======================================================================
514 ## action links
515
516 sub href(%) {
517 my %params = @_;
518 # default is to use -absolute url() i.e. $my_uri
519 my $href = $params{-full} ? $my_url : $my_uri;
520
521 # XXX: Warning: If you touch this, check the search form for updating,
522 # too.
523
524 my @mapping = (
525 project => "p",
526 action => "a",
527 file_name => "f",
528 file_parent => "fp",
529 hash => "h",
530 hash_parent => "hp",
531 hash_base => "hb",
532 hash_parent_base => "hpb",
533 page => "pg",
534 order => "o",
535 searchtext => "s",
536 searchtype => "st",
537 );
538 my %mapping = @mapping;
539
540 $params{'project'} = $project unless exists $params{'project'};
541
542 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
543 if ($use_pathinfo) {
544 # use PATH_INFO for project name
545 $href .= "/$params{'project'}" if defined $params{'project'};
546 delete $params{'project'};
547
548 # Summary just uses the project path URL
549 if (defined $params{'action'} && $params{'action'} eq 'summary') {
550 delete $params{'action'};
551 }
552 }
553
554 # now encode the parameters explicitly
555 my @result = ();
556 for (my $i = 0; $i < @mapping; $i += 2) {
557 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
558 if (defined $params{$name}) {
559 push @result, $symbol . "=" . esc_param($params{$name});
560 }
561 }
562 $href .= "?" . join(';', @result) if scalar @result;
563
564 return $href;
565 }
566
567
568 ## ======================================================================
569 ## validation, quoting/unquoting and escaping
570
571 sub validate_pathname {
572 my $input = shift || return undef;
573
574 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
575 # at the beginning, at the end, and between slashes.
576 # also this catches doubled slashes
577 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
578 return undef;
579 }
580 # no null characters
581 if ($input =~ m!\0!) {
582 return undef;
583 }
584 return $input;
585 }
586
587 sub validate_refname {
588 my $input = shift || return undef;
589
590 # textual hashes are O.K.
591 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
592 return $input;
593 }
594 # it must be correct pathname
595 $input = validate_pathname($input)
596 or return undef;
597 # restrictions on ref name according to git-check-ref-format
598 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
599 return undef;
600 }
601 return $input;
602 }
603
604 # quote unsafe chars, but keep the slash, even when it's not
605 # correct, but quoted slashes look too horrible in bookmarks
606 sub esc_param {
607 my $str = shift;
608 $str =~ s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X", ord($1))/eg;
609 $str =~ s/\+/%2B/g;
610 $str =~ s/ /\+/g;
611 return $str;
612 }
613
614 # quote unsafe chars in whole URL, so some charactrs cannot be quoted
615 sub esc_url {
616 my $str = shift;
617 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
618 $str =~ s/\+/%2B/g;
619 $str =~ s/ /\+/g;
620 return $str;
621 }
622
623 # replace invalid utf8 character with SUBSTITUTION sequence
624 sub esc_html ($;%) {
625 my $str = shift;
626 my %opts = @_;
627
628 $str = decode_utf8($str);
629 $str = $cgi->escapeHTML($str);
630 if ($opts{'-nbsp'}) {
631 $str =~ s/ /&nbsp;/g;
632 }
633 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
634 return $str;
635 }
636
637 # quote control characters and escape filename to HTML
638 sub esc_path {
639 my $str = shift;
640 my %opts = @_;
641
642 $str = decode_utf8($str);
643 $str = $cgi->escapeHTML($str);
644 if ($opts{'-nbsp'}) {
645 $str =~ s/ /&nbsp;/g;
646 }
647 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
648 return $str;
649 }
650
651 # Make control characters "printable", using character escape codes (CEC)
652 sub quot_cec {
653 my $cntrl = shift;
654 my %es = ( # character escape codes, aka escape sequences
655 "\t" => '\t', # tab (HT)
656 "\n" => '\n', # line feed (LF)
657 "\r" => '\r', # carrige return (CR)
658 "\f" => '\f', # form feed (FF)
659 "\b" => '\b', # backspace (BS)
660 "\a" => '\a', # alarm (bell) (BEL)
661 "\e" => '\e', # escape (ESC)
662 "\013" => '\v', # vertical tab (VT)
663 "\000" => '\0', # nul character (NUL)
664 );
665 my $chr = ( (exists $es{$cntrl})
666 ? $es{$cntrl}
667 : sprintf('\%03o', ord($cntrl)) );
668 return "<span class=\"cntrl\">$chr</span>";
669 }
670
671 # Alternatively use unicode control pictures codepoints,
672 # Unicode "printable representation" (PR)
673 sub quot_upr {
674 my $cntrl = shift;
675 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
676 return "<span class=\"cntrl\">$chr</span>";
677 }
678
679 # git may return quoted and escaped filenames
680 sub unquote {
681 my $str = shift;
682
683 sub unq {
684 my $seq = shift;
685 my %es = ( # character escape codes, aka escape sequences
686 't' => "\t", # tab (HT, TAB)
687 'n' => "\n", # newline (NL)
688 'r' => "\r", # return (CR)
689 'f' => "\f", # form feed (FF)
690 'b' => "\b", # backspace (BS)
691 'a' => "\a", # alarm (bell) (BEL)
692 'e' => "\e", # escape (ESC)
693 'v' => "\013", # vertical tab (VT)
694 );
695
696 if ($seq =~ m/^[0-7]{1,3}$/) {
697 # octal char sequence
698 return chr(oct($seq));
699 } elsif (exists $es{$seq}) {
700 # C escape sequence, aka character escape code
701 return $es{$seq}
702 }
703 # quoted ordinary character
704 return $seq;
705 }
706
707 if ($str =~ m/^"(.*)"$/) {
708 # needs unquoting
709 $str = $1;
710 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
711 }
712 return $str;
713 }
714
715 # escape tabs (convert tabs to spaces)
716 sub untabify {
717 my $line = shift;
718
719 while ((my $pos = index($line, "\t")) != -1) {
720 if (my $count = (8 - ($pos % 8))) {
721 my $spaces = ' ' x $count;
722 $line =~ s/\t/$spaces/;
723 }
724 }
725
726 return $line;
727 }
728
729 sub project_in_list {
730 my $project = shift;
731 my @list = git_get_projects_list();
732 return @list && scalar(grep { $_->{'path'} eq $project } @list);
733 }
734
735 ## ----------------------------------------------------------------------
736 ## HTML aware string manipulation
737
738 sub chop_str {
739 my $str = shift;
740 my $len = shift;
741 my $add_len = shift || 10;
742
743 # allow only $len chars, but don't cut a word if it would fit in $add_len
744 # if it doesn't fit, cut it if it's still longer than the dots we would add
745 $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
746 my $body = $1;
747 my $tail = $2;
748 if (length($tail) > 4) {
749 $tail = " ...";
750 $body =~ s/&[^;]*$//; # remove chopped character entities
751 }
752 return "$body$tail";
753 }
754
755 ## ----------------------------------------------------------------------
756 ## functions returning short strings
757
758 # CSS class for given age value (in seconds)
759 sub age_class {
760 my $age = shift;
761
762 if (!defined $age) {
763 return "noage";
764 } elsif ($age < 60*60*2) {
765 return "age0";
766 } elsif ($age < 60*60*24*2) {
767 return "age1";
768 } else {
769 return "age2";
770 }
771 }
772
773 # convert age in seconds to "nn units ago" string
774 sub age_string {
775 my $age = shift;
776 my $age_str;
777
778 if ($age > 60*60*24*365*2) {
779 $age_str = (int $age/60/60/24/365);
780 $age_str .= " years ago";
781 } elsif ($age > 60*60*24*(365/12)*2) {
782 $age_str = int $age/60/60/24/(365/12);
783 $age_str .= " months ago";
784 } elsif ($age > 60*60*24*7*2) {
785 $age_str = int $age/60/60/24/7;
786 $age_str .= " weeks ago";
787 } elsif ($age > 60*60*24*2) {
788 $age_str = int $age/60/60/24;
789 $age_str .= " days ago";
790 } elsif ($age > 60*60*2) {
791 $age_str = int $age/60/60;
792 $age_str .= " hours ago";
793 } elsif ($age > 60*2) {
794 $age_str = int $age/60;
795 $age_str .= " min ago";
796 } elsif ($age > 2) {
797 $age_str = int $age;
798 $age_str .= " sec ago";
799 } else {
800 $age_str .= " right now";
801 }
802 return $age_str;
803 }
804
805 # convert file mode in octal to symbolic file mode string
806 sub mode_str {
807 my $mode = oct shift;
808
809 if (S_ISDIR($mode & S_IFMT)) {
810 return 'drwxr-xr-x';
811 } elsif (S_ISLNK($mode)) {
812 return 'lrwxrwxrwx';
813 } elsif (S_ISREG($mode)) {
814 # git cares only about the executable bit
815 if ($mode & S_IXUSR) {
816 return '-rwxr-xr-x';
817 } else {
818 return '-rw-r--r--';
819 };
820 } else {
821 return '----------';
822 }
823 }
824
825 # convert file mode in octal to file type string
826 sub file_type {
827 my $mode = shift;
828
829 if ($mode !~ m/^[0-7]+$/) {
830 return $mode;
831 } else {
832 $mode = oct $mode;
833 }
834
835 if (S_ISDIR($mode & S_IFMT)) {
836 return "directory";
837 } elsif (S_ISLNK($mode)) {
838 return "symlink";
839 } elsif (S_ISREG($mode)) {
840 return "file";
841 } else {
842 return "unknown";
843 }
844 }
845
846 # convert file mode in octal to file type description string
847 sub file_type_long {
848 my $mode = shift;
849
850 if ($mode !~ m/^[0-7]+$/) {
851 return $mode;
852 } else {
853 $mode = oct $mode;
854 }
855
856 if (S_ISDIR($mode & S_IFMT)) {
857 return "directory";
858 } elsif (S_ISLNK($mode)) {
859 return "symlink";
860 } elsif (S_ISREG($mode)) {
861 if ($mode & S_IXUSR) {
862 return "executable";
863 } else {
864 return "file";
865 };
866 } else {
867 return "unknown";
868 }
869 }
870
871
872 ## ----------------------------------------------------------------------
873 ## functions returning short HTML fragments, or transforming HTML fragments
874 ## which don't belong to other sections
875
876 # format line of commit message.
877 sub format_log_line_html {
878 my $line = shift;
879
880 $line = esc_html($line, -nbsp=>1);
881 if ($line =~ m/([0-9a-fA-F]{8,40})/) {
882 my $hash_text = $1;
883 my $link =
884 $cgi->a({-href => href(action=>"object", hash=>$hash_text),
885 -class => "text"}, $hash_text);
886 $line =~ s/$hash_text/$link/;
887 }
888 return $line;
889 }
890
891 # format marker of refs pointing to given object
892 sub format_ref_marker {
893 my ($refs, $id) = @_;
894 my $markers = '';
895
896 if (defined $refs->{$id}) {
897 foreach my $ref (@{$refs->{$id}}) {
898 my ($type, $name) = qw();
899 # e.g. tags/v2.6.11 or heads/next
900 if ($ref =~ m!^(.*?)s?/(.*)$!) {
901 $type = $1;
902 $name = $2;
903 } else {
904 $type = "ref";
905 $name = $ref;
906 }
907
908 $markers .= " <span class=\"$type\" title=\"$ref\">" .
909 esc_html($name) . "</span>";
910 }
911 }
912
913 if ($markers) {
914 return ' <span class="refs">'. $markers . '</span>';
915 } else {
916 return "";
917 }
918 }
919
920 # format, perhaps shortened and with markers, title line
921 sub format_subject_html {
922 my ($long, $short, $href, $extra) = @_;
923 $extra = '' unless defined($extra);
924
925 if (length($short) < length($long)) {
926 return $cgi->a({-href => $href, -class => "list subject",
927 -title => decode_utf8($long)},
928 esc_html($short) . $extra);
929 } else {
930 return $cgi->a({-href => $href, -class => "list subject"},
931 esc_html($long) . $extra);
932 }
933 }
934
935 # format patch (diff) line (rather not to be used for diff headers)
936 sub format_diff_line {
937 my $line = shift;
938 my ($from, $to) = @_;
939 my $diff_class = "";
940
941 chomp $line;
942
943 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
944 # combined diff
945 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
946 if ($line =~ m/^\@{3}/) {
947 $diff_class = " chunk_header";
948 } elsif ($line =~ m/^\\/) {
949 $diff_class = " incomplete";
950 } elsif ($prefix =~ tr/+/+/) {
951 $diff_class = " add";
952 } elsif ($prefix =~ tr/-/-/) {
953 $diff_class = " rem";
954 }
955 } else {
956 # assume ordinary diff
957 my $char = substr($line, 0, 1);
958 if ($char eq '+') {
959 $diff_class = " add";
960 } elsif ($char eq '-') {
961 $diff_class = " rem";
962 } elsif ($char eq '@') {
963 $diff_class = " chunk_header";
964 } elsif ($char eq "\\") {
965 $diff_class = " incomplete";
966 }
967 }
968 $line = untabify($line);
969 if ($from && $to && $line =~ m/^\@{2} /) {
970 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
971 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
972
973 $from_lines = 0 unless defined $from_lines;
974 $to_lines = 0 unless defined $to_lines;
975
976 if ($from->{'href'}) {
977 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
978 -class=>"list"}, $from_text);
979 }
980 if ($to->{'href'}) {
981 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
982 -class=>"list"}, $to_text);
983 }
984 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
985 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
986 return "<div class=\"diff$diff_class\">$line</div>\n";
987 } elsif ($from && $to && $line =~ m/^\@{3}/) {
988 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
989 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
990
991 @from_text = split(' ', $ranges);
992 for (my $i = 0; $i < @from_text; ++$i) {
993 ($from_start[$i], $from_nlines[$i]) =
994 (split(',', substr($from_text[$i], 1)), 0);
995 }
996
997 $to_text = pop @from_text;
998 $to_start = pop @from_start;
999 $to_nlines = pop @from_nlines;
1000
1001 $line = "<span class=\"chunk_info\">$prefix ";
1002 for (my $i = 0; $i < @from_text; ++$i) {
1003 if ($from->{'href'}[$i]) {
1004 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
1005 -class=>"list"}, $from_text[$i]);
1006 } else {
1007 $line .= $from_text[$i];
1008 }
1009 $line .= " ";
1010 }
1011 if ($to->{'href'}) {
1012 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
1013 -class=>"list"}, $to_text);
1014 } else {
1015 $line .= $to_text;
1016 }
1017 $line .= " $prefix</span>" .
1018 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1019 return "<div class=\"diff$diff_class\">$line</div>\n";
1020 }
1021 return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
1022 }
1023
1024 ## ----------------------------------------------------------------------
1025 ## git utility subroutines, invoking git commands
1026
1027 # returns path to the core git executable and the --git-dir parameter as list
1028 sub git_cmd {
1029 return $GIT, '--git-dir='.$git_dir;
1030 }
1031
1032 # returns path to the core git executable and the --git-dir parameter as string
1033 sub git_cmd_str {
1034 return join(' ', git_cmd());
1035 }
1036
1037 # get HEAD ref of given project as hash
1038 sub git_get_head_hash {
1039 my $project = shift;
1040 my $o_git_dir = $git_dir;
1041 my $retval = undef;
1042 $git_dir = "$projectroot/$project";
1043 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
1044 my $head = <$fd>;
1045 close $fd;
1046 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
1047 $retval = $1;
1048 }
1049 }
1050 if (defined $o_git_dir) {
1051 $git_dir = $o_git_dir;
1052 }
1053 return $retval;
1054 }
1055
1056 # get type of given object
1057 sub git_get_type {
1058 my $hash = shift;
1059
1060 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
1061 my $type = <$fd>;
1062 close $fd or return;
1063 chomp $type;
1064 return $type;
1065 }
1066
1067 sub git_get_project_config {
1068 my ($key, $type) = @_;
1069
1070 return unless ($key);
1071 $key =~ s/^gitweb\.//;
1072 return if ($key =~ m/\W/);
1073
1074 my @x = (git_cmd(), 'config');
1075 if (defined $type) { push @x, $type; }
1076 push @x, "--get";
1077 push @x, "gitweb.$key";
1078 my $val = qx(@x);
1079 chomp $val;
1080 return ($val);
1081 }
1082
1083 # get hash of given path at given ref
1084 sub git_get_hash_by_path {
1085 my $base = shift;
1086 my $path = shift || return undef;
1087 my $type = shift;
1088
1089 $path =~ s,/+$,,;
1090
1091 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
1092 or die_error(undef, "Open git-ls-tree failed");
1093 my $line = <$fd>;
1094 close $fd or return undef;
1095
1096 if (!defined $line) {
1097 # there is no tree or hash given by $path at $base
1098 return undef;
1099 }
1100
1101 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1102 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
1103 if (defined $type && $type ne $2) {
1104 # type doesn't match
1105 return undef;
1106 }
1107 return $3;
1108 }
1109
1110 # get path of entry with given hash at given tree-ish (ref)
1111 # used to get 'from' filename for combined diff (merge commit) for renames
1112 sub git_get_path_by_hash {
1113 my $base = shift || return;
1114 my $hash = shift || return;
1115
1116 local $/ = "\0";
1117
1118 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
1119 or return undef;
1120 while (my $line = <$fd>) {
1121 chomp $line;
1122
1123 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
1124 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
1125 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
1126 close $fd;
1127 return $1;
1128 }
1129 }
1130 close $fd;
1131 return undef;
1132 }
1133
1134 ## ......................................................................
1135 ## git utility functions, directly accessing git repository
1136
1137 sub git_get_project_description {
1138 my $path = shift;
1139
1140 open my $fd, "$projectroot/$path/description" or return undef;
1141 my $descr = <$fd>;
1142 close $fd;
1143 if (defined $descr) {
1144 chomp $descr;
1145 }
1146 return $descr;
1147 }
1148
1149 sub git_get_project_url_list {
1150 my $path = shift;
1151
1152 open my $fd, "$projectroot/$path/cloneurl" or return;
1153 my @git_project_url_list = map { chomp; $_ } <$fd>;
1154 close $fd;
1155
1156 return wantarray ? @git_project_url_list : \@git_project_url_list;
1157 }
1158
1159 sub git_get_projects_list {
1160 my ($filter) = @_;
1161 my @list;
1162
1163 $filter ||= '';
1164 $filter =~ s/\.git$//;
1165
1166 my ($check_forks) = gitweb_check_feature('forks');
1167
1168 if (-d $projects_list) {
1169 # search in directory
1170 my $dir = $projects_list . ($filter ? "/$filter" : '');
1171 # remove the trailing "/"
1172 $dir =~ s!/+$!!;
1173 my $pfxlen = length("$dir");
1174
1175 File::Find::find({
1176 follow_fast => 1, # follow symbolic links
1177 dangling_symlinks => 0, # ignore dangling symlinks, silently
1178 wanted => sub {
1179 # skip project-list toplevel, if we get it.
1180 return if (m!^[/.]$!);
1181 # only directories can be git repositories
1182 return unless (-d $_);
1183
1184 my $subdir = substr($File::Find::name, $pfxlen + 1);
1185 # we check related file in $projectroot
1186 if ($check_forks and $subdir =~ m#/.#) {
1187 $File::Find::prune = 1;
1188 } elsif (check_export_ok("$projectroot/$filter/$subdir")) {
1189 push @list, { path => ($filter ? "$filter/" : '') . $subdir };
1190 $File::Find::prune = 1;
1191 }
1192 },
1193 }, "$dir");
1194
1195 } elsif (-f $projects_list) {
1196 # read from file(url-encoded):
1197 # 'git%2Fgit.git Linus+Torvalds'
1198 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1199 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1200 my %paths;
1201 open my ($fd), $projects_list or return;
1202 PROJECT:
1203 while (my $line = <$fd>) {
1204 chomp $line;
1205 my ($path, $owner) = split ' ', $line;
1206 $path = unescape($path);
1207 $owner = unescape($owner);
1208 if (!defined $path) {
1209 next;
1210 }
1211 if ($filter ne '') {
1212 # looking for forks;
1213 my $pfx = substr($path, 0, length($filter));
1214 if ($pfx ne $filter) {
1215 next PROJECT;
1216 }
1217 my $sfx = substr($path, length($filter));
1218 if ($sfx !~ /^\/.*\.git$/) {
1219 next PROJECT;
1220 }
1221 } elsif ($check_forks) {
1222 PATH:
1223 foreach my $filter (keys %paths) {
1224 # looking for forks;
1225 my $pfx = substr($path, 0, length($filter));
1226 if ($pfx ne $filter) {
1227 next PATH;
1228 }
1229 my $sfx = substr($path, length($filter));
1230 if ($sfx !~ /^\/.*\.git$/) {
1231 next PATH;
1232 }
1233 # is a fork, don't include it in
1234 # the list
1235 next PROJECT;
1236 }
1237 }
1238 if (check_export_ok("$projectroot/$path")) {
1239 my $pr = {
1240 path => $path,
1241 owner => decode_utf8($owner),
1242 };
1243 push @list, $pr;
1244 (my $forks_path = $path) =~ s/\.git$//;
1245 $paths{$forks_path}++;
1246 }
1247 }
1248 close $fd;
1249 }
1250 return @list;
1251 }
1252
1253 sub git_get_project_owner {
1254 my $project = shift;
1255 my $owner;
1256
1257 return undef unless $project;
1258
1259 # read from file (url-encoded):
1260 # 'git%2Fgit.git Linus+Torvalds'
1261 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1262 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1263 if (-f $projects_list) {
1264 open (my $fd , $projects_list);
1265 while (my $line = <$fd>) {
1266 chomp $line;
1267 my ($pr, $ow) = split ' ', $line;
1268 $pr = unescape($pr);
1269 $ow = unescape($ow);
1270 if ($pr eq $project) {
1271 $owner = decode_utf8($ow);
1272 last;
1273 }
1274 }
1275 close $fd;
1276 }
1277 if (!defined $owner) {
1278 $owner = get_file_owner("$projectroot/$project");
1279 }
1280
1281 return $owner;
1282 }
1283
1284 sub git_get_last_activity {
1285 my ($path) = @_;
1286 my $fd;
1287
1288 $git_dir = "$projectroot/$path";
1289 open($fd, "-|", git_cmd(), 'for-each-ref',
1290 '--format=%(committer)',
1291 '--sort=-committerdate',
1292 '--count=1',
1293 'refs/heads') or return;
1294 my $most_recent = <$fd>;
1295 close $fd or return;
1296 if (defined $most_recent &&
1297 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
1298 my $timestamp = $1;
1299 my $age = time - $timestamp;
1300 return ($age, age_string($age));
1301 }
1302 }
1303
1304 sub git_get_references {
1305 my $type = shift || "";
1306 my %refs;
1307 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
1308 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
1309 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
1310 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
1311 or return;
1312
1313 while (my $line = <$fd>) {
1314 chomp $line;
1315 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type/?[^^]+)!) {
1316 if (defined $refs{$1}) {
1317 push @{$refs{$1}}, $2;
1318 } else {
1319 $refs{$1} = [ $2 ];
1320 }
1321 }
1322 }
1323 close $fd or return;
1324 return \%refs;
1325 }
1326
1327 sub git_get_rev_name_tags {
1328 my $hash = shift || return undef;
1329
1330 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
1331 or return;
1332 my $name_rev = <$fd>;
1333 close $fd;
1334
1335 if ($name_rev =~ m|^$hash tags/(.*)$|) {
1336 return $1;
1337 } else {
1338 # catches also '$hash undefined' output
1339 return undef;
1340 }
1341 }
1342
1343 ## ----------------------------------------------------------------------
1344 ## parse to hash functions
1345
1346 sub parse_date {
1347 my $epoch = shift;
1348 my $tz = shift || "-0000";
1349
1350 my %date;
1351 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
1352 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
1353 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
1354 $date{'hour'} = $hour;
1355 $date{'minute'} = $min;
1356 $date{'mday'} = $mday;
1357 $date{'day'} = $days[$wday];
1358 $date{'month'} = $months[$mon];
1359 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
1360 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
1361 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
1362 $mday, $months[$mon], $hour ,$min;
1363 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
1364 1900+$year, $mon, $mday, $hour ,$min, $sec;
1365
1366 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
1367 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
1368 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
1369 $date{'hour_local'} = $hour;
1370 $date{'minute_local'} = $min;
1371 $date{'tz_local'} = $tz;
1372 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
1373 1900+$year, $mon+1, $mday,
1374 $hour, $min, $sec, $tz);
1375 return %date;
1376 }
1377
1378 sub parse_tag {
1379 my $tag_id = shift;
1380 my %tag;
1381 my @comment;
1382
1383 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
1384 $tag{'id'} = $tag_id;
1385 while (my $line = <$fd>) {
1386 chomp $line;
1387 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
1388 $tag{'object'} = $1;
1389 } elsif ($line =~ m/^type (.+)$/) {
1390 $tag{'type'} = $1;
1391 } elsif ($line =~ m/^tag (.+)$/) {
1392 $tag{'name'} = $1;
1393 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
1394 $tag{'author'} = $1;
1395 $tag{'epoch'} = $2;
1396 $tag{'tz'} = $3;
1397 } elsif ($line =~ m/--BEGIN/) {
1398 push @comment, $line;
1399 last;
1400 } elsif ($line eq "") {
1401 last;
1402 }
1403 }
1404 push @comment, <$fd>;
1405 $tag{'comment'} = \@comment;
1406 close $fd or return;
1407 if (!defined $tag{'name'}) {
1408 return
1409 };
1410 return %tag
1411 }
1412
1413 sub parse_commit_text {
1414 my ($commit_text, $withparents) = @_;
1415 my @commit_lines = split '\n', $commit_text;
1416 my %co;
1417
1418 pop @commit_lines; # Remove '\0'
1419
1420 if (! @commit_lines) {
1421 return;
1422 }
1423
1424 my $header = shift @commit_lines;
1425 if ($header !~ m/^[0-9a-fA-F]{40}/) {
1426 return;
1427 }
1428 ($co{'id'}, my @parents) = split ' ', $header;
1429 while (my $line = shift @commit_lines) {
1430 last if $line eq "\n";
1431 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
1432 $co{'tree'} = $1;
1433 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
1434 push @parents, $1;
1435 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
1436 $co{'author'} = $1;
1437 $co{'author_epoch'} = $2;
1438 $co{'author_tz'} = $3;
1439 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
1440 $co{'author_name'} = $1;
1441 $co{'author_email'} = $2;
1442 } else {
1443 $co{'author_name'} = $co{'author'};
1444 }
1445 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
1446 $co{'committer'} = $1;
1447 $co{'committer_epoch'} = $2;
1448 $co{'committer_tz'} = $3;
1449 $co{'committer_name'} = $co{'committer'};
1450 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
1451 $co{'committer_name'} = $1;
1452 $co{'committer_email'} = $2;
1453 } else {
1454 $co{'committer_name'} = $co{'committer'};
1455 }
1456 }
1457 }
1458 if (!defined $co{'tree'}) {
1459 return;
1460 };
1461 $co{'parents'} = \@parents;
1462 $co{'parent'} = $parents[0];
1463
1464 foreach my $title (@commit_lines) {
1465 $title =~ s/^ //;
1466 if ($title ne "") {
1467 $co{'title'} = chop_str($title, 80, 5);
1468 # remove leading stuff of merges to make the interesting part visible
1469 if (length($title) > 50) {
1470 $title =~ s/^Automatic //;
1471 $title =~ s/^merge (of|with) /Merge ... /i;
1472 if (length($title) > 50) {
1473 $title =~ s/(http|rsync):\/\///;
1474 }
1475 if (length($title) > 50) {
1476 $title =~ s/(master|www|rsync)\.//;
1477 }
1478 if (length($title) > 50) {
1479 $title =~ s/kernel.org:?//;
1480 }
1481 if (length($title) > 50) {
1482 $title =~ s/\/pub\/scm//;
1483 }
1484 }
1485 $co{'title_short'} = chop_str($title, 50, 5);
1486 last;
1487 }
1488 }
1489 if ($co{'title'} eq "") {
1490 $co{'title'} = $co{'title_short'} = '(no commit message)';
1491 }
1492 # remove added spaces
1493 foreach my $line (@commit_lines) {
1494 $line =~ s/^ //;
1495 }
1496 $co{'comment'} = \@commit_lines;
1497
1498 my $age = time - $co{'committer_epoch'};
1499 $co{'age'} = $age;
1500 $co{'age_string'} = age_string($age);
1501 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
1502 if ($age > 60*60*24*7*2) {
1503 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1504 $co{'age_string_age'} = $co{'age_string'};
1505 } else {
1506 $co{'age_string_date'} = $co{'age_string'};
1507 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1508 }
1509 return %co;
1510 }
1511
1512 sub parse_commit {
1513 my ($commit_id) = @_;
1514 my %co;
1515
1516 local $/ = "\0";
1517
1518 open my $fd, "-|", git_cmd(), "rev-list",
1519 "--parents",
1520 "--header",
1521 "--max-count=1",
1522 $commit_id,
1523 "--",
1524 or die_error(undef, "Open git-rev-list failed");
1525 %co = parse_commit_text(<$fd>, 1);
1526 close $fd;
1527
1528 return %co;
1529 }
1530
1531 sub parse_commits {
1532 my ($commit_id, $maxcount, $skip, $arg, $filename) = @_;
1533 my @cos;
1534
1535 $maxcount ||= 1;
1536 $skip ||= 0;
1537
1538 local $/ = "\0";
1539
1540 open my $fd, "-|", git_cmd(), "rev-list",
1541 "--header",
1542 ($arg ? ($arg) : ()),
1543 ("--max-count=" . $maxcount),
1544 ("--skip=" . $skip),
1545 $commit_id,
1546 "--",
1547 ($filename ? ($filename) : ())
1548 or die_error(undef, "Open git-rev-list failed");
1549 while (my $line = <$fd>) {
1550 my %co = parse_commit_text($line);
1551 push @cos, \%co;
1552 }
1553 close $fd;
1554
1555 return wantarray ? @cos : \@cos;
1556 }
1557
1558 # parse ref from ref_file, given by ref_id, with given type
1559 sub parse_ref {
1560 my $ref_file = shift;
1561 my $ref_id = shift;
1562 my $type = shift || git_get_type($ref_id);
1563 my %ref_item;
1564
1565 $ref_item{'type'} = $type;
1566 $ref_item{'id'} = $ref_id;
1567 $ref_item{'epoch'} = 0;
1568 $ref_item{'age'} = "unknown";
1569 if ($type eq "tag") {
1570 my %tag = parse_tag($ref_id);
1571 $ref_item{'comment'} = $tag{'comment'};
1572 if ($tag{'type'} eq "commit") {
1573 my %co = parse_commit($tag{'object'});
1574 $ref_item{'epoch'} = $co{'committer_epoch'};
1575 $ref_item{'age'} = $co{'age_string'};
1576 } elsif (defined($tag{'epoch'})) {
1577 my $age = time - $tag{'epoch'};
1578 $ref_item{'epoch'} = $tag{'epoch'};
1579 $ref_item{'age'} = age_string($age);
1580 }
1581 $ref_item{'reftype'} = $tag{'type'};
1582 $ref_item{'name'} = $tag{'name'};
1583 $ref_item{'refid'} = $tag{'object'};
1584 } elsif ($type eq "commit"){
1585 my %co = parse_commit($ref_id);
1586 $ref_item{'reftype'} = "commit";
1587 $ref_item{'name'} = $ref_file;
1588 $ref_item{'title'} = $co{'title'};
1589 $ref_item{'refid'} = $ref_id;
1590 $ref_item{'epoch'} = $co{'committer_epoch'};
1591 $ref_item{'age'} = $co{'age_string'};
1592 } else {
1593 $ref_item{'reftype'} = $type;
1594 $ref_item{'name'} = $ref_file;
1595 $ref_item{'refid'} = $ref_id;
1596 }
1597
1598 return %ref_item;
1599 }
1600
1601 # parse line of git-diff-tree "raw" output
1602 sub parse_difftree_raw_line {
1603 my $line = shift;
1604 my %res;
1605
1606 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1607 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
1608 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1609 $res{'from_mode'} = $1;
1610 $res{'to_mode'} = $2;
1611 $res{'from_id'} = $3;
1612 $res{'to_id'} = $4;
1613 $res{'status'} = $5;
1614 $res{'similarity'} = $6;
1615 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
1616 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
1617 } else {
1618 $res{'file'} = unquote($7);
1619 }
1620 }
1621 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
1622 # combined diff (for merge commit)
1623 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
1624 $res{'nparents'} = length($1);
1625 $res{'from_mode'} = [ split(' ', $2) ];
1626 $res{'to_mode'} = pop @{$res{'from_mode'}};
1627 $res{'from_id'} = [ split(' ', $3) ];
1628 $res{'to_id'} = pop @{$res{'from_id'}};
1629 $res{'status'} = [ split('', $4) ];
1630 $res{'to_file'} = unquote($5);
1631 }
1632 # 'c512b523472485aef4fff9e57b229d9d243c967f'
1633 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
1634 $res{'commit'} = $1;
1635 }
1636
1637 return wantarray ? %res : \%res;
1638 }
1639
1640 # parse line of git-ls-tree output
1641 sub parse_ls_tree_line ($;%) {
1642 my $line = shift;
1643 my %opts = @_;
1644 my %res;
1645
1646 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1647 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
1648
1649 $res{'mode'} = $1;
1650 $res{'type'} = $2;
1651 $res{'hash'} = $3;
1652 if ($opts{'-z'}) {
1653 $res{'name'} = $4;
1654 } else {
1655 $res{'name'} = unquote($4);
1656 }
1657
1658 return wantarray ? %res : \%res;
1659 }
1660
1661 ## ......................................................................
1662 ## parse to array of hashes functions
1663
1664 sub git_get_heads_list {
1665 my $limit = shift;
1666 my @headslist;
1667
1668 open my $fd, '-|', git_cmd(), 'for-each-ref',
1669 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
1670 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
1671 'refs/heads'
1672 or return;
1673 while (my $line = <$fd>) {
1674 my %ref_item;
1675
1676 chomp $line;
1677 my ($refinfo, $committerinfo) = split(/\0/, $line);
1678 my ($hash, $name, $title) = split(' ', $refinfo, 3);
1679 my ($committer, $epoch, $tz) =
1680 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
1681 $name =~ s!^refs/heads/!!;
1682
1683 $ref_item{'name'} = $name;
1684 $ref_item{'id'} = $hash;
1685 $ref_item{'title'} = $title || '(no commit message)';
1686 $ref_item{'epoch'} = $epoch;
1687 if ($epoch) {
1688 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
1689 } else {
1690 $ref_item{'age'} = "unknown";
1691 }
1692
1693 push @headslist, \%ref_item;
1694 }
1695 close $fd;
1696
1697 return wantarray ? @headslist : \@headslist;
1698 }
1699
1700 sub git_get_tags_list {
1701 my $limit = shift;
1702 my @tagslist;
1703
1704 open my $fd, '-|', git_cmd(), 'for-each-ref',
1705 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
1706 '--format=%(objectname) %(objecttype) %(refname) '.
1707 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
1708 'refs/tags'
1709 or return;
1710 while (my $line = <$fd>) {
1711 my %ref_item;
1712
1713 chomp $line;
1714 my ($refinfo, $creatorinfo) = split(/\0/, $line);
1715 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
1716 my ($creator, $epoch, $tz) =
1717 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
1718 $name =~ s!^refs/tags/!!;
1719
1720 $ref_item{'type'} = $type;
1721 $ref_item{'id'} = $id;
1722 $ref_item{'name'} = $name;
1723 if ($type eq "tag") {
1724 $ref_item{'subject'} = $title;
1725 $ref_item{'reftype'} = $reftype;
1726 $ref_item{'refid'} = $refid;
1727 } else {
1728 $ref_item{'reftype'} = $type;
1729 $ref_item{'refid'} = $id;
1730 }
1731
1732 if ($type eq "tag" || $type eq "commit") {
1733 $ref_item{'epoch'} = $epoch;
1734 if ($epoch) {
1735 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
1736 } else {
1737 $ref_item{'age'} = "unknown";
1738 }
1739 }
1740
1741 push @tagslist, \%ref_item;
1742 }
1743 close $fd;
1744
1745 return wantarray ? @tagslist : \@tagslist;
1746 }
1747
1748 ## ----------------------------------------------------------------------
1749 ## filesystem-related functions
1750
1751 sub get_file_owner {
1752 my $path = shift;
1753
1754 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
1755 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
1756 if (!defined $gcos) {
1757 return undef;
1758 }
1759 my $owner = $gcos;
1760 $owner =~ s/[,;].*$//;
1761 return decode_utf8($owner);
1762 }
1763
1764 ## ......................................................................
1765 ## mimetype related functions
1766
1767 sub mimetype_guess_file {
1768 my $filename = shift;
1769 my $mimemap = shift;
1770 -r $mimemap or return undef;
1771
1772 my %mimemap;
1773 open(MIME, $mimemap) or return undef;
1774 while (<MIME>) {
1775 next if m/^#/; # skip comments
1776 my ($mime, $exts) = split(/\t+/);
1777 if (defined $exts) {
1778 my @exts = split(/\s+/, $exts);
1779 foreach my $ext (@exts) {
1780 $mimemap{$ext} = $mime;
1781 }
1782 }
1783 }
1784 close(MIME);
1785
1786 $filename =~ /\.([^.]*)$/;
1787 return $mimemap{$1};
1788 }
1789
1790 sub mimetype_guess {
1791 my $filename = shift;
1792 my $mime;
1793 $filename =~ /\./ or return undef;
1794
1795 if ($mimetypes_file) {
1796 my $file = $mimetypes_file;
1797 if ($file !~ m!^/!) { # if it is relative path
1798 # it is relative to project
1799 $file = "$projectroot/$project/$file";
1800 }
1801 $mime = mimetype_guess_file($filename, $file);
1802 }
1803 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1804 return $mime;
1805 }
1806
1807 sub blob_mimetype {
1808 my $fd = shift;
1809 my $filename = shift;
1810
1811 if ($filename) {
1812 my $mime = mimetype_guess($filename);
1813 $mime and return $mime;
1814 }
1815
1816 # just in case
1817 return $default_blob_plain_mimetype unless $fd;
1818
1819 if (-T $fd) {
1820 return 'text/plain' .
1821 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1822 } elsif (! $filename) {
1823 return 'application/octet-stream';
1824 } elsif ($filename =~ m/\.png$/i) {
1825 return 'image/png';
1826 } elsif ($filename =~ m/\.gif$/i) {
1827 return 'image/gif';
1828 } elsif ($filename =~ m/\.jpe?g$/i) {
1829 return 'image/jpeg';
1830 } else {
1831 return 'application/octet-stream';
1832 }
1833 }
1834
1835 ## ======================================================================
1836 ## functions printing HTML: header, footer, error page
1837
1838 sub git_header_html {
1839 my $status = shift || "200 OK";
1840 my $expires = shift;
1841
1842 my $title = "$site_name";
1843 if (defined $project) {
1844 $title .= " - " . decode_utf8($project);
1845 if (defined $action) {
1846 $title .= "/$action";
1847 if (defined $file_name) {
1848 $title .= " - " . esc_path($file_name);
1849 if ($action eq "tree" && $file_name !~ m|/$|) {
1850 $title .= "/";
1851 }
1852 }
1853 }
1854 }
1855 my $content_type;
1856 # require explicit support from the UA if we are to send the page as
1857 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
1858 # we have to do this because MSIE sometimes globs '*/*', pretending to
1859 # support xhtml+xml but choking when it gets what it asked for.
1860 if (defined $cgi->http('HTTP_ACCEPT') &&
1861 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
1862 $cgi->Accept('application/xhtml+xml') != 0) {
1863 $content_type = 'application/xhtml+xml';
1864 } else {
1865 $content_type = 'text/html';
1866 }
1867 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
1868 -status=> $status, -expires => $expires);
1869 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
1870 print <<EOF;
1871 <?xml version="1.0" encoding="utf-8"?>
1872 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1873 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
1874 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
1875 <!-- git core binaries version $git_version -->
1876 <head>
1877 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
1878 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
1879 <meta name="robots" content="index, nofollow"/>
1880 <title>$title</title>
1881 EOF
1882 # print out each stylesheet that exist
1883 if (defined $stylesheet) {
1884 #provides backwards capability for those people who define style sheet in a config file
1885 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
1886 } else {
1887 foreach my $stylesheet (@stylesheets) {
1888 next unless $stylesheet;
1889 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
1890 }
1891 }
1892 if (defined $project) {
1893 printf('<link rel="alternate" title="%s log RSS feed" '.
1894 'href="%s" type="application/rss+xml" />'."\n",
1895 esc_param($project), href(action=>"rss"));
1896 printf('<link rel="alternate" title="%s log Atom feed" '.
1897 'href="%s" type="application/atom+xml" />'."\n",
1898 esc_param($project), href(action=>"atom"));
1899 } else {
1900 printf('<link rel="alternate" title="%s projects list" '.
1901 'href="%s" type="text/plain; charset=utf-8"/>'."\n",
1902 $site_name, href(project=>undef, action=>"project_index"));
1903 printf('<link rel="alternate" title="%s projects feeds" '.
1904 'href="%s" type="text/x-opml"/>'."\n",
1905 $site_name, href(project=>undef, action=>"opml"));
1906 }
1907 if (defined $favicon) {
1908 print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n);
1909 }
1910
1911 print "</head>\n" .
1912 "<body>\n";
1913
1914 if (-f $site_header) {
1915 open (my $fd, $site_header);
1916 print <$fd>;
1917 close $fd;
1918 }
1919
1920 print "<div class=\"page_header\">\n" .
1921 $cgi->a({-href => esc_url($logo_url),
1922 -title => $logo_label},
1923 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
1924 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
1925 if (defined $project) {
1926 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
1927 if (defined $action) {
1928 print " / $action";
1929 }
1930 print "\n";
1931 }
1932 print "</div>\n";
1933
1934 my ($have_search) = gitweb_check_feature('search');
1935 if ((defined $project) && ($have_search)) {
1936 if (!defined $searchtext) {
1937 $searchtext = "";
1938 }
1939 my $search_hash;
1940 if (defined $hash_base) {
1941 $search_hash = $hash_base;
1942 } elsif (defined $hash) {
1943 $search_hash = $hash;
1944 } else {
1945 $search_hash = "HEAD";
1946 }
1947 $cgi->param("a", "search");
1948 $cgi->param("h", $search_hash);
1949 $cgi->param("p", $project);
1950 print $cgi->startform(-method => "get", -action => $my_uri) .
1951 "<div class=\"search\">\n" .
1952 $cgi->hidden(-name => "p") . "\n" .
1953 $cgi->hidden(-name => "a") . "\n" .
1954 $cgi->hidden(-name => "h") . "\n" .
1955 $cgi->popup_menu(-name => 'st', -default => 'commit',
1956 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
1957 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
1958 " search:\n",
1959 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
1960 "</div>" .
1961 $cgi->end_form() . "\n";
1962 }
1963 }
1964
1965 sub git_footer_html {
1966 print "<div class=\"page_footer\">\n";
1967 if (defined $project) {
1968 my $descr = git_get_project_description($project);
1969 if (defined $descr) {
1970 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
1971 }
1972 print $cgi->a({-href => href(action=>"rss"),
1973 -class => "rss_logo"}, "RSS") . " ";
1974 print $cgi->a({-href => href(action=>"atom"),
1975 -class => "rss_logo"}, "Atom") . "\n";
1976 } else {
1977 print $cgi->a({-href => href(project=>undef, action=>"opml"),
1978 -class => "rss_logo"}, "OPML") . " ";
1979 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
1980 -class => "rss_logo"}, "TXT") . "\n";
1981 }
1982 print "</div>\n" ;
1983
1984 if (-f $site_footer) {
1985 open (my $fd, $site_footer);
1986 print <$fd>;
1987 close $fd;
1988 }
1989
1990 print "</body>\n" .
1991 "</html>";
1992 }
1993
1994 sub die_error {
1995 my $status = shift || "403 Forbidden";
1996 my $error = shift || "Malformed query, file missing or permission denied";
1997
1998 git_header_html($status);
1999 print <<EOF;
2000 <div class="page_body">
2001 <br /><br />
2002 $status - $error
2003 <br />
2004 </div>
2005 EOF
2006 git_footer_html();
2007 exit;
2008 }
2009
2010 ## ----------------------------------------------------------------------
2011 ## functions printing or outputting HTML: navigation
2012
2013 sub git_print_page_nav {
2014 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
2015 $extra = '' if !defined $extra; # pager or formats
2016
2017 my @navs = qw(summary shortlog log commit commitdiff tree);
2018 if ($suppress) {
2019 @navs = grep { $_ ne $suppress } @navs;
2020 }
2021
2022 my %arg = map { $_ => {action=>$_} } @navs;
2023 if (defined $head) {
2024 for (qw(commit commitdiff)) {
2025 $arg{$_}{'hash'} = $head;
2026 }
2027 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
2028 for (qw(shortlog log)) {
2029 $arg{$_}{'hash'} = $head;
2030 }
2031 }
2032 }
2033 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
2034 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
2035
2036 print "<div class=\"page_nav\">\n" .
2037 (join " | ",
2038 map { $_ eq $current ?
2039 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
2040 } @navs);
2041 print "<br/>\n$extra<br/>\n" .
2042 "</div>\n";
2043 }
2044
2045 sub format_paging_nav {
2046 my ($action, $hash, $head, $page, $nrevs) = @_;
2047 my $paging_nav;
2048
2049
2050 if ($hash ne $head || $page) {
2051 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
2052 } else {
2053 $paging_nav .= "HEAD";
2054 }
2055
2056 if ($page > 0) {
2057 $paging_nav .= " &sdot; " .
2058 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
2059 -accesskey => "p", -title => "Alt-p"}, "prev");
2060 } else {
2061 $paging_nav .= " &sdot; prev";
2062 }
2063
2064 if ($nrevs >= (100 * ($page+1)-1)) {
2065 $paging_nav .= " &sdot; " .
2066 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
2067 -accesskey => "n", -title => "Alt-n"}, "next");
2068 } else {
2069 $paging_nav .= " &sdot; next";
2070 }
2071
2072 return $paging_nav;
2073 }
2074
2075 ## ......................................................................
2076 ## functions printing or outputting HTML: div
2077
2078 sub git_print_header_div {
2079 my ($action, $title, $hash, $hash_base) = @_;
2080 my %args = ();
2081
2082 $args{'action'} = $action;
2083 $args{'hash'} = $hash if $hash;
2084 $args{'hash_base'} = $hash_base if $hash_base;
2085
2086 print "<div class=\"header\">\n" .
2087 $cgi->a({-href => href(%args), -class => "title"},
2088 $title ? $title : $action) .
2089 "\n</div>\n";
2090 }
2091
2092 #sub git_print_authorship (\%) {
2093 sub git_print_authorship {
2094 my $co = shift;
2095
2096 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
2097 print "<div class=\"author_date\">" .
2098 esc_html($co->{'author_name'}) .
2099 " [$ad{'rfc2822'}";
2100 if ($ad{'hour_local'} < 6) {
2101 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2102 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2103 } else {
2104 printf(" (%02d:%02d %s)",
2105 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2106 }
2107 print "]</div>\n";
2108 }
2109
2110 sub git_print_page_path {
2111 my $name = shift;
2112 my $type = shift;
2113 my $hb = shift;
2114
2115
2116 print "<div class=\"page_path\">";
2117 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
2118 -title => 'tree root'}, decode_utf8("[$project]"));
2119 print " / ";
2120 if (defined $name) {
2121 my @dirname = split '/', $name;
2122 my $basename = pop @dirname;
2123 my $fullname = '';
2124
2125 foreach my $dir (@dirname) {
2126 $fullname .= ($fullname ? '/' : '') . $dir;
2127 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
2128 hash_base=>$hb),
2129 -title => $fullname}, esc_path($dir));
2130 print " / ";
2131 }
2132 if (defined $type && $type eq 'blob') {
2133 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
2134 hash_base=>$hb),
2135 -title => $name}, esc_path($basename));
2136 } elsif (defined $type && $type eq 'tree') {
2137 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
2138 hash_base=>$hb),
2139 -title => $name}, esc_path($basename));
2140 print " / ";
2141 } else {
2142 print esc_path($basename);
2143 }
2144 }
2145 print "<br/></div>\n";
2146 }
2147
2148 # sub git_print_log (\@;%) {
2149 sub git_print_log ($;%) {
2150 my $log = shift;
2151 my %opts = @_;
2152
2153 if ($opts{'-remove_title'}) {
2154 # remove title, i.e. first line of log
2155 shift @$log;
2156 }
2157 # remove leading empty lines
2158 while (defined $log->[0] && $log->[0] eq "") {
2159 shift @$log;
2160 }
2161
2162 # print log
2163 my $signoff = 0;
2164 my $empty = 0;
2165 foreach my $line (@$log) {
2166 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
2167 $signoff = 1;
2168 $empty = 0;
2169 if (! $opts{'-remove_signoff'}) {
2170 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
2171 next;
2172 } else {
2173 # remove signoff lines
2174 next;
2175 }
2176 } else {
2177 $signoff = 0;
2178 }
2179
2180 # print only one empty line
2181 # do not print empty line after signoff
2182 if ($line eq "") {
2183 next if ($empty || $signoff);
2184 $empty = 1;
2185 } else {
2186 $empty = 0;
2187 }
2188
2189 print format_log_line_html($line) . "<br/>\n";
2190 }
2191
2192 if ($opts{'-final_empty_line'}) {
2193 # end with single empty line
2194 print "<br/>\n" unless $empty;
2195 }
2196 }
2197
2198 # return link target (what link points to)
2199 sub git_get_link_target {
2200 my $hash = shift;
2201 my $link_target;
2202
2203 # read link
2204 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2205 or return;
2206 {
2207 local $/;
2208 $link_target = <$fd>;
2209 }
2210 close $fd
2211 or return;
2212
2213 return $link_target;
2214 }
2215
2216 # given link target, and the directory (basedir) the link is in,
2217 # return target of link relative to top directory (top tree);
2218 # return undef if it is not possible (including absolute links).
2219 sub normalize_link_target {
2220 my ($link_target, $basedir, $hash_base) = @_;
2221
2222 # we can normalize symlink target only if $hash_base is provided
2223 return unless $hash_base;
2224
2225 # absolute symlinks (beginning with '/') cannot be normalized
2226 return if (substr($link_target, 0, 1) eq '/');
2227
2228 # normalize link target to path from top (root) tree (dir)
2229 my $path;
2230 if ($basedir) {
2231 $path = $basedir . '/' . $link_target;
2232 } else {
2233 # we are in top (root) tree (dir)
2234 $path = $link_target;
2235 }
2236
2237 # remove //, /./, and /../
2238 my @path_parts;
2239 foreach my $part (split('/', $path)) {
2240 # discard '.' and ''
2241 next if (!$part || $part eq '.');
2242 # handle '..'
2243 if ($part eq '..') {
2244 if (@path_parts) {
2245 pop @path_parts;
2246 } else {
2247 # link leads outside repository (outside top dir)
2248 return;
2249 }
2250 } else {
2251 push @path_parts, $part;
2252 }
2253 }
2254 $path = join('/', @path_parts);
2255
2256 return $path;
2257 }
2258
2259 # print tree entry (row of git_tree), but without encompassing <tr> element
2260 sub git_print_tree_entry {
2261 my ($t, $basedir, $hash_base, $have_blame) = @_;
2262
2263 my %base_key = ();
2264 $base_key{'hash_base'} = $hash_base if defined $hash_base;
2265
2266 # The format of a table row is: mode list link. Where mode is
2267 # the mode of the entry, list is the name of the entry, an href,
2268 # and link is the action links of the entry.
2269
2270 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
2271 if ($t->{'type'} eq "blob") {
2272 print "<td class=\"list\">" .
2273 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
2274 file_name=>"$basedir$t->{'name'}", %base_key),
2275 -class => "list"}, esc_path($t->{'name'}));
2276 if (S_ISLNK(oct $t->{'mode'})) {
2277 my $link_target = git_get_link_target($t->{'hash'});
2278 if ($link_target) {
2279 my $norm_target = normalize_link_target($link_target, $basedir, $hash_base);
2280 if (defined $norm_target) {
2281 print " -> " .
2282 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
2283 file_name=>$norm_target),
2284 -title => $norm_target}, esc_path($link_target));
2285 } else {
2286 print " -> " . esc_path($link_target);
2287 }
2288 }
2289 }
2290 print "</td>\n";
2291 print "<td class=\"link\">";
2292 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
2293 file_name=>"$basedir$t->{'name'}", %base_key)},
2294 "blob");
2295 if ($have_blame) {
2296 print " | " .
2297 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
2298 file_name=>"$basedir$t->{'name'}", %base_key)},
2299 "blame");
2300 }
2301 if (defined $hash_base) {
2302 print " | " .
2303 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2304 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
2305 "history");
2306 }
2307 print " | " .
2308 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
2309 file_name=>"$basedir$t->{'name'}")},
2310 "raw");
2311 print "</td>\n";
2312
2313 } elsif ($t->{'type'} eq "tree") {
2314 print "<td class=\"list\">";
2315 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
2316 file_name=>"$basedir$t->{'name'}", %base_key)},
2317 esc_path($t->{'name'}));
2318 print "</td>\n";
2319 print "<td class=\"link\">";
2320 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
2321 file_name=>"$basedir$t->{'name'}", %base_key)},
2322 "tree");
2323 if (defined $hash_base) {
2324 print " | " .
2325 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2326 file_name=>"$basedir$t->{'name'}")},
2327 "history");
2328 }
2329 print "</td>\n";
2330 }
2331 }
2332
2333 ## ......................................................................
2334 ## functions printing large fragments of HTML
2335
2336 sub fill_from_file_info {
2337 my ($diff, @parents) = @_;
2338
2339 $diff->{'from_file'} = [ ];
2340 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
2341 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
2342 if ($diff->{'status'}[$i] eq 'R' ||
2343 $diff->{'status'}[$i] eq 'C') {
2344 $diff->{'from_file'}[$i] =
2345 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
2346 }
2347 }
2348
2349 return $diff;
2350 }
2351
2352 # parameters can be strings, or references to arrays of strings
2353 sub from_ids_eq {
2354 my ($a, $b) = @_;
2355
2356 if (ref($a) eq "ARRAY" && ref($b) eq "ARRAY" && @$a == @$b) {
2357 for (my $i = 0; $i < @$a; ++$i) {
2358 return 0 unless ($a->[$i] eq $b->[$i]);
2359 }
2360 return 1;
2361 } elsif (!ref($a) && !ref($b)) {
2362 return $a eq $b;
2363 } else {
2364 return 0;
2365 }
2366 }
2367
2368
2369 sub git_difftree_body {
2370 my ($difftree, $hash, @parents) = @_;
2371 my ($parent) = $parents[0];
2372 my ($have_blame) = gitweb_check_feature('blame');
2373 print "<div class=\"list_head\">\n";
2374 if ($#{$difftree} > 10) {
2375 print(($#{$difftree} + 1) . " files changed:\n");
2376 }
2377 print "</div>\n";
2378
2379 print "<table class=\"" .
2380 (@parents > 1 ? "combined " : "") .
2381 "diff_tree\">\n";
2382 my $alternate = 1;
2383 my $patchno = 0;
2384 foreach my $line (@{$difftree}) {
2385 my $diff;
2386 if (ref($line) eq "HASH") {
2387 # pre-parsed (or generated by hand)
2388 $diff = $line;
2389 } else {
2390 $diff = parse_difftree_raw_line($line);
2391 }
2392
2393 if ($alternate) {
2394 print "<tr class=\"dark\">\n";
2395 } else {
2396 print "<tr class=\"light\">\n";
2397 }
2398 $alternate ^= 1;
2399
2400 if (exists $diff->{'nparents'}) { # combined diff
2401
2402 fill_from_file_info($diff, @parents)
2403 unless exists $diff->{'from_file'};
2404
2405 if ($diff->{'to_id'} ne ('0' x 40)) {
2406 # file exists in the result (child) commit
2407 print "<td>" .
2408 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2409 file_name=>$diff->{'to_file'},
2410 hash_base=>$hash),
2411 -class => "list"}, esc_path($diff->{'to_file'})) .
2412 "</td>\n";
2413 } else {
2414 print "<td>" .
2415 esc_path($diff->{'to_file'}) .
2416 "</td>\n";
2417 }
2418
2419 if ($action eq 'commitdiff') {
2420 # link to patch
2421 $patchno++;
2422 print "<td class=\"link\">" .
2423 $cgi->a({-href => "#patch$patchno"}, "patch") .
2424 " | " .
2425 "</td>\n";
2426 }
2427
2428 my $has_history = 0;
2429 my $not_deleted = 0;
2430 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
2431 my $hash_parent = $parents[$i];
2432 my $from_hash = $diff->{'from_id'}[$i];
2433 my $from_path = $diff->{'from_file'}[$i];
2434 my $status = $diff->{'status'}[$i];
2435
2436 $has_history ||= ($status ne 'A');
2437 $not_deleted ||= ($status ne 'D');
2438
2439 if ($status eq 'A') {
2440 print "<td class=\"link\" align=\"right\"> | </td>\n";
2441 } elsif ($status eq 'D') {
2442 print "<td class=\"link\">" .
2443 $cgi->a({-href => href(action=>"blob",
2444 hash_base=>$hash,
2445 hash=>$from_hash,
2446 file_name=>$from_path)},
2447 "blob" . ($i+1)) .
2448 " | </td>\n";
2449 } else {
2450 if ($diff->{'to_id'} eq $from_hash) {
2451 print "<td class=\"link nochange\">";
2452 } else {
2453 print "<td class=\"link\">";
2454 }
2455 print $cgi->a({-href => href(action=>"blobdiff",
2456 hash=>$diff->{'to_id'},
2457 hash_parent=>$from_hash,
2458 hash_base=>$hash,
2459 hash_parent_base=>$hash_parent,
2460 file_name=>$diff->{'to_file'},
2461 file_parent=>$from_path)},
2462 "diff" . ($i+1)) .
2463 " | </td>\n";
2464 }
2465 }
2466
2467 print "<td class=\"link\">";
2468 if ($not_deleted) {
2469 print $cgi->a({-href => href(action=>"blob",
2470 hash=>$diff->{'to_id'},
2471 file_name=>$diff->{'to_file'},
2472 hash_base=>$hash)},
2473 "blob");
2474 print " | " if ($has_history);
2475 }
2476 if ($has_history) {
2477 print $cgi->a({-href => href(action=>"history",
2478 file_name=>$diff->{'to_file'},
2479 hash_base=>$hash)},
2480 "history");
2481 }
2482 print "</td>\n";
2483
2484 print "</tr>\n";
2485 next; # instead of 'else' clause, to avoid extra indent
2486 }
2487 # else ordinary diff
2488
2489 my ($to_mode_oct, $to_mode_str, $to_file_type);
2490 my ($from_mode_oct, $from_mode_str, $from_file_type);
2491 if ($diff->{'to_mode'} ne ('0' x 6)) {
2492 $to_mode_oct = oct $diff->{'to_mode'};
2493 if (S_ISREG($to_mode_oct)) { # only for regular file
2494 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
2495 }
2496 $to_file_type = file_type($diff->{'to_mode'});
2497 }
2498 if ($diff->{'from_mode'} ne ('0' x 6)) {
2499 $from_mode_oct = oct $diff->{'from_mode'};
2500 if (S_ISREG($to_mode_oct)) { # only for regular file
2501 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
2502 }
2503 $from_file_type = file_type($diff->{'from_mode'});
2504 }
2505
2506 if ($diff->{'status'} eq "A") { # created
2507 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
2508 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
2509 $mode_chng .= "]</span>";
2510 print "<td>";
2511 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2512 hash_base=>$hash, file_name=>$diff->{'file'}),
2513 -class => "list"}, esc_path($diff->{'file'}));
2514 print "</td>\n";
2515 print "<td>$mode_chng</td>\n";
2516 print "<td class=\"link\">";
2517 if ($action eq 'commitdiff') {
2518 # link to patch
2519 $patchno++;
2520 print $cgi->a({-href => "#patch$patchno"}, "patch");
2521 print " | ";
2522 }
2523 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2524 hash_base=>$hash, file_name=>$diff->{'file'})},
2525 "blob");
2526 print "</td>\n";
2527
2528 } elsif ($diff->{'status'} eq "D") { # deleted
2529 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
2530 print "<td>";
2531 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
2532 hash_base=>$parent, file_name=>$diff->{'file'}),
2533 -class => "list"}, esc_path($diff->{'file'}));
2534 print "</td>\n";
2535 print "<td>$mode_chng</td>\n";
2536 print "<td class=\"link\">";
2537 if ($action eq 'commitdiff') {
2538 # link to patch
2539 $patchno++;
2540 print $cgi->a({-href => "#patch$patchno"}, "patch");
2541 print " | ";
2542 }
2543 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
2544 hash_base=>$parent, file_name=>$diff->{'file'})},
2545 "blob") . " | ";
2546 if ($have_blame) {
2547 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
2548 file_name=>$diff->{'file'})},
2549 "blame") . " | ";
2550 }
2551 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
2552 file_name=>$diff->{'file'})},
2553 "history");
2554 print "</td>\n";
2555
2556 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
2557 my $mode_chnge = "";
2558 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
2559 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
2560 if ($from_file_type ne $to_file_type) {
2561 $mode_chnge .= " from $from_file_type to $to_file_type";
2562 }
2563 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
2564 if ($from_mode_str && $to_mode_str) {
2565 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
2566 } elsif ($to_mode_str) {
2567 $mode_chnge .= " mode: $to_mode_str";
2568 }
2569 }
2570 $mode_chnge .= "]</span>\n";
2571 }
2572 print "<td>";
2573 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2574 hash_base=>$hash, file_name=>$diff->{'file'}),
2575 -class => "list"}, esc_path($diff->{'file'}));
2576 print "</td>\n";
2577 print "<td>$mode_chnge</td>\n";
2578 print "<td class=\"link\">";
2579 if ($action eq 'commitdiff') {
2580 # link to patch
2581 $patchno++;
2582 print $cgi->a({-href => "#patch$patchno"}, "patch") .
2583 " | ";
2584 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
2585 # "commit" view and modified file (not onlu mode changed)
2586 print $cgi->a({-href => href(action=>"blobdiff",
2587 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
2588 hash_base=>$hash, hash_parent_base=>$parent,
2589 file_name=>$diff->{'file'})},
2590 "diff") .
2591 " | ";
2592 }
2593 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2594 hash_base=>$hash, file_name=>$diff->{'file'})},
2595 "blob") . " | ";
2596 if ($have_blame) {
2597 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
2598 file_name=>$diff->{'file'})},
2599 "blame") . " | ";
2600 }
2601 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
2602 file_name=>$diff->{'file'})},
2603 "history");
2604 print "</td>\n";
2605
2606 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
2607 my %status_name = ('R' => 'moved', 'C' => 'copied');
2608 my $nstatus = $status_name{$diff->{'status'}};
2609 my $mode_chng = "";
2610 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
2611 # mode also for directories, so we cannot use $to_mode_str
2612 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
2613 }
2614 print "<td>" .
2615 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2616 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
2617 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
2618 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
2619 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
2620 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
2621 -class => "list"}, esc_path($diff->{'from_file'})) .
2622 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
2623 "<td class=\"link\">";
2624 if ($action eq 'commitdiff') {
2625 # link to patch
2626 $patchno++;
2627 print $cgi->a({-href => "#patch$patchno"}, "patch") .
2628 " | ";
2629 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
2630 # "commit" view and modified file (not only pure rename or copy)
2631 print $cgi->a({-href => href(action=>"blobdiff",
2632 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
2633 hash_base=>$hash, hash_parent_base=>$parent,
2634 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
2635 "diff") .
2636 " | ";
2637 }
2638 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2639 hash_base=>$parent, file_name=>$diff->{'to_file'})},
2640 "blob") . " | ";
2641 if ($have_blame) {
2642 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
2643 file_name=>$diff->{'to_file'})},
2644 "blame") . " | ";
2645 }
2646 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
2647 file_name=>$diff->{'to_file'})},
2648 "history");
2649 print "</td>\n";
2650
2651 } # we should not encounter Unmerged (U) or Unknown (X) status
2652 print "</tr>\n";
2653 }
2654 print "</table>\n";
2655 }
2656
2657 sub git_patchset_body {
2658 my ($fd, $difftree, $hash, @hash_parents) = @_;
2659 my ($hash_parent) = $hash_parents[0];
2660
2661 my $patch_idx = 0;
2662 my $patch_number = 0;
2663 my $patch_line;
2664 my $diffinfo;
2665 my (%from, %to);
2666
2667 print "<div class=\"patchset\">\n";
2668
2669 # skip to first patch
2670 while ($patch_line = <$fd>) {
2671 chomp $patch_line;
2672
2673 last if ($patch_line =~ m/^diff /);
2674 }
2675
2676 PATCH:
2677 while ($patch_line) {
2678 my @diff_header;
2679 my ($from_id, $to_id);
2680
2681 # git diff header
2682 #assert($patch_line =~ m/^diff /) if DEBUG;
2683 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
2684 $patch_number++;
2685 push @diff_header, $patch_line;
2686
2687 # extended diff header
2688 EXTENDED_HEADER:
2689 while ($patch_line = <$fd>) {
2690 chomp $patch_line;
2691
2692 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
2693
2694 if ($patch_line =~ m/^index ([0-9a-fA-F]{40})..([0-9a-fA-F]{40})/) {
2695 $from_id = $1;
2696 $to_id = $2;
2697 } elsif ($patch_line =~ m/^index ((?:[0-9a-fA-F]{40},)+[0-9a-fA-F]{40})..([0-9a-fA-F]{40})/) {
2698 $from_id = [ split(',', $1) ];
2699 $to_id = $2;
2700 }
2701
2702 push @diff_header, $patch_line;
2703 }
2704 my $last_patch_line = $patch_line;
2705
2706 # check if current patch belong to current raw line
2707 # and parse raw git-diff line if needed
2708 if (defined $diffinfo &&
2709 defined $from_id && defined $to_id &&
2710 from_ids_eq($diffinfo->{'from_id'}, $from_id) &&
2711 $diffinfo->{'to_id'} eq $to_id) {
2712 # this is continuation of a split patch
2713 print "<div class=\"patch cont\">\n";
2714 } else {
2715 # advance raw git-diff output if needed
2716 $patch_idx++ if defined $diffinfo;
2717
2718 # read and prepare patch information
2719 if (ref($difftree->[$patch_idx]) eq "HASH") {
2720 # pre-parsed (or generated by hand)
2721 $diffinfo = $difftree->[$patch_idx];
2722 } else {
2723 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
2724 }
2725 if ($diffinfo->{'nparents'}) {
2726 # combined diff
2727 $from{'file'} = [];
2728 $from{'href'} = [];
2729 fill_from_file_info($diffinfo, @hash_parents)
2730 unless exists $diffinfo->{'from_file'};
2731 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2732 $from{'file'}[$i] = $diffinfo->{'from_file'}[$i] || $diffinfo->{'to_file'};
2733 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
2734 $from{'href'}[$i] = href(action=>"blob",
2735 hash_base=>$hash_parents[$i],
2736 hash=>$diffinfo->{'from_id'}[$i],
2737 file_name=>$from{'file'}[$i]);
2738 } else {
2739 $from{'href'}[$i] = undef;
2740 }
2741 }
2742 } else {
2743 $from{'file'} = $diffinfo->{'from_file'} || $diffinfo->{'file'};
2744 if ($diffinfo->{'status'} ne "A") { # not new (added) file
2745 $from{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2746 hash=>$diffinfo->{'from_id'},
2747 file_name=>$from{'file'});
2748 } else {
2749 delete $from{'href'};
2750 }
2751 }
2752
2753 $to{'file'} = $diffinfo->{'to_file'} || $diffinfo->{'file'};
2754 if ($diffinfo->{'to_id'} ne ('0' x 40)) { # file exists in result
2755 $to{'href'} = href(action=>"blob", hash_base=>$hash,
2756 hash=>$diffinfo->{'to_id'},
2757 file_name=>$to{'file'});
2758 } else {
2759 delete $to{'href'};
2760 }
2761 # this is first patch for raw difftree line with $patch_idx index
2762 # we index @$difftree array from 0, but number patches from 1
2763 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
2764 }
2765
2766 # print "git diff" header
2767 $patch_line = shift @diff_header;
2768 if ($diffinfo->{'nparents'}) {
2769
2770 # combined diff
2771 $patch_line =~ s!^(diff (.*?) )"?.*$!$1!;
2772 if ($to{'href'}) {
2773 $patch_line .= $cgi->a({-href => $to{'href'}, -class => "path"},
2774 esc_path($to{'file'}));
2775 } else { # file was deleted
2776 $patch_line .= esc_path($to{'file'});
2777 }
2778
2779 } else {
2780
2781 $patch_line =~ s!^(diff (.*?) )"?a/.*$!$1!;
2782 if ($from{'href'}) {
2783 $patch_line .= $cgi->a({-href => $from{'href'}, -class => "path"},
2784 'a/' . esc_path($from{'file'}));
2785 } else { # file was added
2786 $patch_line .= 'a/' . esc_path($from{'file'});
2787 }
2788 $patch_line .= ' ';
2789 if ($to{'href'}) {
2790 $patch_line .= $cgi->a({-href => $to{'href'}, -class => "path"},
2791 'b/' . esc_path($to{'file'}));
2792 } else { # file was deleted
2793 $patch_line .= 'b/' . esc_path($to{'file'});
2794 }
2795
2796 }
2797 print "<div class=\"diff header\">$patch_line</div>\n";
2798
2799 # print extended diff header
2800 print "<div class=\"diff extended_header\">\n" if (@diff_header > 0);
2801 EXTENDED_HEADER:
2802 foreach $patch_line (@diff_header) {
2803 # match <path>
2804 if ($patch_line =~ s!^((copy|rename) from ).*$!$1! && $from{'href'}) {
2805 $patch_line .= $cgi->a({-href=>$from{'href'}, -class=>"path"},
2806 esc_path($from{'file'}));
2807 }
2808 if ($patch_line =~ s!^((copy|rename) to ).*$!$1! && $to{'href'}) {
2809 $patch_line .= $cgi->a({-href=>$to{'href'}, -class=>"path"},
2810 esc_path($to{'file'}));
2811 }
2812 # match single <mode>
2813 if ($patch_line =~ m/\s(\d{6})$/) {
2814 $patch_line .= '<span class="info"> (' .
2815 file_type_long($1) .
2816 ')</span>';
2817 }
2818 # match <hash>
2819 if ($patch_line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
2820 # can match only for combined diff
2821 $patch_line = 'index ';
2822 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2823 if ($from{'href'}[$i]) {
2824 $patch_line .= $cgi->a({-href=>$from{'href'}[$i],
2825 -class=>"hash"},
2826 substr($diffinfo->{'from_id'}[$i],0,7));
2827 } else {
2828 $patch_line .= '0' x 7;
2829 }
2830 # separator
2831 $patch_line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
2832 }
2833 $patch_line .= '..';
2834 if ($to{'href'}) {
2835 $patch_line .= $cgi->a({-href=>$to{'href'}, -class=>"hash"},
2836 substr($diffinfo->{'to_id'},0,7));
2837 } else {
2838 $patch_line .= '0' x 7;
2839 }
2840
2841 } elsif ($patch_line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
2842 # can match only for ordinary diff
2843 my ($from_link, $to_link);
2844 if ($from{'href'}) {
2845 $from_link = $cgi->a({-href=>$from{'href'}, -class=>"hash"},
2846 substr($diffinfo->{'from_id'},0,7));
2847 } else {
2848 $from_link = '0' x 7;
2849 }
2850 if ($to{'href'}) {
2851 $to_link = $cgi->a({-href=>$to{'href'}, -class=>"hash"},
2852 substr($diffinfo->{'to_id'},0,7));
2853 } else {
2854 $to_link = '0' x 7;
2855 }
2856 #affirm {
2857 # my ($from_hash, $to_hash) =
2858 # ($patch_line =~ m/^index ([0-9a-fA-F]{40})..([0-9a-fA-F]{40})/);
2859 # my ($from_id, $to_id) =
2860 # ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
2861 # ($from_hash eq $from_id) && ($to_hash eq $to_id);
2862 #} if DEBUG;
2863 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
2864 $patch_line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
2865 }
2866 print $patch_line . "<br/>\n";
2867 }
2868 print "</div>\n" if (@diff_header > 0); # class="diff extended_header"
2869
2870 # from-file/to-file diff header
2871 $patch_line = $last_patch_line;
2872 if (! $patch_line) {
2873 print "</div>\n"; # class="patch"
2874 last PATCH;
2875 }
2876 next PATCH if ($patch_line =~ m/^diff /);
2877 #assert($patch_line =~ m/^---/) if DEBUG;
2878 if (!$diffinfo->{'nparents'} && # not from-file line for combined diff
2879 $from{'href'} && $patch_line =~ m!^--- "?a/!) {
2880 $patch_line = '--- a/' .
2881 $cgi->a({-href=>$from{'href'}, -class=>"path"},
2882 esc_path($from{'file'}));
2883 }
2884 print "<div class=\"diff from_file\">$patch_line</div>\n";
2885
2886 $patch_line = <$fd>;
2887 chomp $patch_line;
2888
2889 #assert($patch_line =~ m/^+++/) if DEBUG;
2890 if ($to{'href'} && $patch_line =~ m!^\+\+\+ "?b/!) {
2891 $patch_line = '+++ b/' .
2892 $cgi->a({-href=>$to{'href'}, -class=>"path"},
2893 esc_path($to{'file'}));
2894 }
2895 print "<div class=\"diff to_file\">$patch_line</div>\n";
2896
2897 # the patch itself
2898 LINE:
2899 while ($patch_line = <$fd>) {
2900 chomp $patch_line;
2901
2902 next PATCH if ($patch_line =~ m/^diff /);
2903
2904 print format_diff_line($patch_line, \%from, \%to);
2905 }
2906
2907 } continue {
2908 print "</div>\n"; # class="patch"
2909 }
2910
2911 if ($patch_number == 0) {
2912 if (@hash_parents > 1) {
2913 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
2914 } else {
2915 print "<div class=\"diff nodifferences\">No differences found</div>\n";
2916 }
2917 }
2918
2919 print "</div>\n"; # class="patchset"
2920 }
2921
2922 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2923
2924 sub git_project_list_body {
2925 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
2926
2927 my ($check_forks) = gitweb_check_feature('forks');
2928
2929 my @projects;
2930 foreach my $pr (@$projlist) {
2931 my (@aa) = git_get_last_activity($pr->{'path'});
2932 unless (@aa) {
2933 next;
2934 }
2935 ($pr->{'age'}, $pr->{'age_string'}) = @aa;
2936 if (!defined $pr->{'descr'}) {
2937 my $descr = git_get_project_description($pr->{'path'}) || "";
2938 $pr->{'descr_long'} = decode_utf8($descr);
2939 $pr->{'descr'} = chop_str($descr, 25, 5);
2940 }
2941 if (!defined $pr->{'owner'}) {
2942 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
2943 }
2944 if ($check_forks) {
2945 my $pname = $pr->{'path'};
2946 if (($pname =~ s/\.git$//) &&
2947 ($pname !~ /\/$/) &&
2948 (-d "$projectroot/$pname")) {
2949 $pr->{'forks'} = "-d $projectroot/$pname";
2950 }
2951 else {
2952 $pr->{'forks'} = 0;
2953 }
2954 }
2955 push @projects, $pr;
2956 }
2957
2958 $order ||= $default_projects_order;
2959 $from = 0 unless defined $from;
2960 $to = $#projects if (!defined $to || $#projects < $to);
2961
2962 print "<table class=\"project_list\">\n";
2963 unless ($no_header) {
2964 print "<tr>\n";
2965 if ($check_forks) {
2966 print "<th></th>\n";
2967 }
2968 if ($order eq "project") {
2969 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
2970 print "<th>Project</th>\n";
2971 } else {
2972 print "<th>" .
2973 $cgi->a({-href => href(project=>undef, order=>'project'),
2974 -class => "header"}, "Project") .
2975 "</th>\n";
2976 }
2977 if ($order eq "descr") {
2978 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
2979 print "<th>Description</th>\n";
2980 } else {
2981 print "<th>" .
2982 $cgi->a({-href => href(project=>undef, order=>'descr'),
2983 -class => "header"}, "Description") .
2984 "</th>\n";
2985 }
2986 if ($order eq "owner") {
2987 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
2988 print "<th>Owner</th>\n";
2989 } else {
2990 print "<th>" .
2991 $cgi->a({-href => href(project=>undef, order=>'owner'),
2992 -class => "header"}, "Owner") .
2993 "</th>\n";
2994 }
2995 if ($order eq "age") {
2996 @projects = sort {$a->{'age'} <=> $b->{'age'}} @projects;
2997 print "<th>Last Change</th>\n";
2998 } else {
2999 print "<th>" .
3000 $cgi->a({-href => href(project=>undef, order=>'age'),
3001 -class => "header"}, "Last Change") .
3002 "</th>\n";
3003 }
3004 print "<th></th>\n" .
3005 "</tr>\n";
3006 }
3007 my $alternate = 1;
3008 for (my $i = $from; $i <= $to; $i++) {
3009 my $pr = $projects[$i];
3010 if ($alternate) {
3011 print "<tr class=\"dark\">\n";
3012 } else {
3013 print "<tr class=\"light\">\n";
3014 }
3015 $alternate ^= 1;
3016 if ($check_forks) {
3017 print "<td>";
3018 if ($pr->{'forks'}) {
3019 print "<!-- $pr->{'forks'} -->\n";
3020 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
3021 }
3022 print "</td>\n";
3023 }
3024 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3025 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
3026 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3027 -class => "list", -title => $pr->{'descr_long'}},
3028 esc_html($pr->{'descr'})) . "</td>\n" .
3029 "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
3030 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
3031 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
3032 "<td class=\"link\">" .
3033 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
3034 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
3035 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
3036 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
3037 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
3038 "</td>\n" .
3039 "</tr>\n";
3040 }
3041 if (defined $extra) {
3042 print "<tr>\n";
3043 if ($check_forks) {
3044 print "<td></td>\n";
3045 }
3046 print "<td colspan=\"5\">$extra</td>\n" .
3047 "</tr>\n";
3048 }
3049 print "</table>\n";
3050 }
3051
3052 sub git_shortlog_body {
3053 # uses global variable $project
3054 my ($commitlist, $from, $to, $refs, $extra) = @_;
3055
3056 my $have_snapshot = gitweb_have_snapshot();
3057
3058 $from = 0 unless defined $from;
3059 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3060
3061 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
3062 my $alternate = 1;
3063 for (my $i = $from; $i <= $to; $i++) {
3064 my %co = %{$commitlist->[$i]};
3065 my $commit = $co{'id'};
3066 my $ref = format_ref_marker($refs, $commit);
3067 if ($alternate) {
3068 print "<tr class=\"dark\">\n";
3069 } else {
3070 print "<tr class=\"light\">\n";
3071 }
3072 $alternate ^= 1;
3073 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
3074 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3075 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
3076 "<td>";
3077 print format_subject_html($co{'title'}, $co{'title_short'},
3078 href(action=>"commit", hash=>$commit), $ref);
3079 print "</td>\n" .
3080 "<td class=\"link\">" .
3081 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
3082 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
3083 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
3084 if ($have_snapshot) {
3085 print " | " . $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
3086 }
3087 print "</td>\n" .
3088 "</tr>\n";
3089 }
3090 if (defined $extra) {
3091 print "<tr>\n" .
3092 "<td colspan=\"4\">$extra</td>\n" .
3093 "</tr>\n";
3094 }
3095 print "</table>\n";
3096 }
3097
3098 sub git_history_body {
3099 # Warning: assumes constant type (blob or tree) during history
3100 my ($commitlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
3101
3102 $from = 0 unless defined $from;
3103 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
3104
3105 print "<table class=\"history\" cellspacing=\"0\">\n";
3106 my $alternate = 1;
3107 for (my $i = $from; $i <= $to; $i++) {
3108 my %co = %{$commitlist->[$i]};
3109 if (!%co) {
3110 next;
3111 }
3112 my $commit = $co{'id'};
3113
3114 my $ref = format_ref_marker($refs, $commit);
3115
3116 if ($alternate) {
3117 print "<tr class=\"dark\">\n";
3118 } else {
3119 print "<tr class=\"light\">\n";
3120 }
3121 $alternate ^= 1;
3122 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3123 # shortlog uses chop_str($co{'author_name'}, 10)
3124 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
3125 "<td>";
3126 # originally git_history used chop_str($co{'title'}, 50)
3127 print format_subject_html($co{'title'}, $co{'title_short'},
3128 href(action=>"commit", hash=>$commit), $ref);
3129 print "</td>\n" .
3130 "<td class=\"link\">" .
3131 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
3132 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
3133
3134 if ($ftype eq 'blob') {
3135 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
3136 my $blob_parent = git_get_hash_by_path($commit, $file_name);
3137 if (defined $blob_current && defined $blob_parent &&
3138 $blob_current ne $blob_parent) {
3139 print " | " .
3140 $cgi->a({-href => href(action=>"blobdiff",
3141 hash=>$blob_current, hash_parent=>$blob_parent,
3142 hash_base=>$hash_base, hash_parent_base=>$commit,
3143 file_name=>$file_name)},
3144 "diff to current");
3145 }
3146 }
3147 print "</td>\n" .
3148 "</tr>\n";
3149 }
3150 if (defined $extra) {
3151 print "<tr>\n" .
3152 "<td colspan=\"4\">$extra</td>\n" .
3153 "</tr>\n";
3154 }
3155 print "</table>\n";
3156 }
3157
3158 sub git_tags_body {
3159 # uses global variable $project
3160 my ($taglist, $from, $to, $extra) = @_;
3161 $from = 0 unless defined $from;
3162 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
3163
3164 print "<table class=\"tags\" cellspacing=\"0\">\n";
3165 my $alternate = 1;
3166 for (my $i = $from; $i <= $to; $i++) {
3167 my $entry = $taglist->[$i];
3168 my %tag = %$entry;
3169 my $comment = $tag{'subject'};
3170 my $comment_short;
3171 if (defined $comment) {
3172 $comment_short = chop_str($comment, 30, 5);
3173 }
3174 if ($alternate) {
3175 print "<tr class=\"dark\">\n";
3176 } else {
3177 print "<tr class=\"light\">\n";
3178 }
3179 $alternate ^= 1;
3180 if (defined $tag{'age'}) {
3181 print "<td><i>$tag{'age'}</i></td>\n";
3182 } else {
3183 print "<td></td>\n";
3184 }
3185 print "<td>" .
3186 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
3187 -class => "list name"}, esc_html($tag{'name'})) .
3188 "</td>\n" .
3189 "<td>";
3190 if (defined $comment) {
3191 print format_subject_html($comment, $comment_short,
3192 href(action=>"tag", hash=>$tag{'id'}));
3193 }
3194 print "</td>\n" .
3195 "<td class=\"selflink\">";
3196 if ($tag{'type'} eq "tag") {
3197 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
3198 } else {
3199 print "&nbsp;";
3200 }
3201 print "</td>\n" .
3202 "<td class=\"link\">" . " | " .
3203 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
3204 if ($tag{'reftype'} eq "commit") {
3205 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
3206 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log");
3207 } elsif ($tag{'reftype'} eq "blob") {
3208 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
3209 }
3210 print "</td>\n" .
3211 "</tr>";
3212 }
3213 if (defined $extra) {
3214 print "<tr>\n" .
3215 "<td colspan=\"5\">$extra</td>\n" .
3216 "</tr>\n";
3217 }
3218 print "</table>\n";
3219 }
3220
3221 sub git_heads_body {
3222 # uses global variable $project
3223 my ($headlist, $head, $from, $to, $extra) = @_;
3224 $from = 0 unless defined $from;
3225 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
3226
3227 print "<table class=\"heads\" cellspacing=\"0\">\n";
3228 my $alternate = 1;
3229 for (my $i = $from; $i <= $to; $i++) {
3230 my $entry = $headlist->[$i];
3231 my %ref = %$entry;
3232 my $curr = $ref{'id'} eq $head;
3233 if ($alternate) {
3234 print "<tr class=\"dark\">\n";
3235 } else {
3236 print "<tr class=\"light\">\n";
3237 }
3238 $alternate ^= 1;
3239 print "<td><i>$ref{'age'}</i></td>\n" .
3240 ($curr ? "<td class=\"current_head\">" : "<td>") .
3241 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'}),
3242 -class => "list name"},esc_html($ref{'name'})) .
3243 "</td>\n" .
3244 "<td class=\"link\">" .
3245 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'})}, "shortlog") . " | " .
3246 $cgi->a({-href => href(action=>"log", hash=>$ref{'name'})}, "log") . " | " .
3247 $cgi->a({-href => href(action=>"tree", hash=>$ref{'name'}, hash_base=>$ref{'name'})}, "tree") .
3248 "</td>\n" .
3249 "</tr>";
3250 }
3251 if (defined $extra) {
3252 print "<tr>\n" .
3253 "<td colspan=\"3\">$extra</td>\n" .
3254 "</tr>\n";
3255 }
3256 print "</table>\n";
3257 }
3258
3259 sub git_search_grep_body {
3260 my ($commitlist, $from, $to, $extra) = @_;
3261 $from = 0 unless defined $from;
3262 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3263
3264 print "<table class=\"grep\" cellspacing=\"0\">\n";
3265 my $alternate = 1;
3266 for (my $i = $from; $i <= $to; $i++) {
3267 my %co = %{$commitlist->[$i]};
3268 if (!%co) {
3269 next;
3270 }
3271 my $commit = $co{'id'};
3272 if ($alternate) {
3273 print "<tr class=\"dark\">\n";
3274 } else {
3275 print "<tr class=\"light\">\n";
3276 }
3277 $alternate ^= 1;
3278 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3279 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3280 "<td>" .
3281 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
3282 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3283 my $comment = $co{'comment'};
3284 foreach my $line (@$comment) {
3285 if ($line =~ m/^(.*)($search_regexp)(.*)$/i) {
3286 my $lead = esc_html($1) || "";
3287 $lead = chop_str($lead, 30, 10);
3288 my $match = esc_html($2) || "";
3289 my $trail = esc_html($3) || "";
3290 $trail = chop_str($trail, 30, 10);
3291 my $text = "$lead<span class=\"match\">$match</span>$trail";
3292 print chop_str($text, 80, 5) . "<br/>\n";
3293 }
3294 }
3295 print "</td>\n" .
3296 "<td class=\"link\">" .
3297 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3298 " | " .
3299 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3300 print "</td>\n" .
3301 "</tr>\n";
3302 }
3303 if (defined $extra) {
3304 print "<tr>\n" .
3305 "<td colspan=\"3\">$extra</td>\n" .
3306 "</tr>\n";
3307 }
3308 print "</table>\n";
3309 }
3310
3311 ## ======================================================================
3312 ## ======================================================================
3313 ## actions
3314
3315 sub git_project_list {
3316 my $order = $cgi->param('o');
3317 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
3318 die_error(undef, "Unknown order parameter");
3319 }
3320
3321 my @list = git_get_projects_list();
3322 if (!@list) {
3323 die_error(undef, "No projects found");
3324 }
3325
3326 git_header_html();
3327 if (-f $home_text) {
3328 print "<div class=\"index_include\">\n";
3329 open (my $fd, $home_text);
3330 print <$fd>;
3331 close $fd;
3332 print "</div>\n";
3333 }
3334 git_project_list_body(\@list, $order);
3335 git_footer_html();
3336 }
3337
3338 sub git_forks {
3339 my $order = $cgi->param('o');
3340 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
3341 die_error(undef, "Unknown order parameter");
3342 }
3343
3344 my @list = git_get_projects_list($project);
3345 if (!@list) {
3346 die_error(undef, "No forks found");
3347 }
3348
3349 git_header_html();
3350 git_print_page_nav('','');
3351 git_print_header_div('summary', "$project forks");
3352 git_project_list_body(\@list, $order);
3353 git_footer_html();
3354 }
3355
3356 sub git_project_index {
3357 my @projects = git_get_projects_list($project);
3358
3359 print $cgi->header(
3360 -type => 'text/plain',
3361 -charset => 'utf-8',
3362 -content_disposition => 'inline; filename="index.aux"');
3363
3364 foreach my $pr (@projects) {
3365 if (!exists $pr->{'owner'}) {
3366 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}");
3367 }
3368
3369 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
3370 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
3371 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
3372 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
3373 $path =~ s/ /\+/g;
3374 $owner =~ s/ /\+/g;
3375
3376 print "$path $owner\n";
3377 }
3378 }
3379
3380 sub git_summary {
3381 my $descr = git_get_project_description($project) || "none";
3382 my %co = parse_commit("HEAD");
3383 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
3384 my $head = $co{'id'};
3385
3386 my $owner = git_get_project_owner($project);
3387
3388 my $refs = git_get_references();
3389 # These get_*_list functions return one more to allow us to see if
3390 # there are more ...
3391 my @taglist = git_get_tags_list(16);
3392 my @headlist = git_get_heads_list(16);
3393 my @forklist;
3394 my ($check_forks) = gitweb_check_feature('forks');
3395
3396 if ($check_forks) {
3397 @forklist = git_get_projects_list($project);
3398 }
3399
3400 git_header_html();
3401 git_print_page_nav('summary','', $head);
3402
3403 print "<div class=\"title\">&nbsp;</div>\n";
3404 print "<table cellspacing=\"0\">\n" .
3405 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
3406 "<tr><td>owner</td><td>$owner</td></tr>\n";
3407 if (defined $cd{'rfc2822'}) {
3408 print "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
3409 }
3410
3411 # use per project git URL list in $projectroot/$project/cloneurl
3412 # or make project git URL from git base URL and project name
3413 my $url_tag = "URL";
3414 my @url_list = git_get_project_url_list($project);
3415 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
3416 foreach my $git_url (@url_list) {
3417 next unless $git_url;
3418 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
3419 $url_tag = "";
3420 }
3421 print "</table>\n";
3422
3423 if (-s "$projectroot/$project/README.html") {
3424 if (open my $fd, "$projectroot/$project/README.html") {
3425 print "<div class=\"title\">readme</div>\n";
3426 print $_ while (<$fd>);
3427 close $fd;
3428 }
3429 }
3430
3431 # we need to request one more than 16 (0..15) to check if
3432 # those 16 are all
3433 my @commitlist = $head ? parse_commits($head, 17) : ();
3434 if (@commitlist) {
3435 git_print_header_div('shortlog');
3436 git_shortlog_body(\@commitlist, 0, 15, $refs,
3437 $#commitlist <= 15 ? undef :
3438 $cgi->a({-href => href(action=>"shortlog")}, "..."));
3439 }
3440
3441 if (@taglist) {
3442 git_print_header_div('tags');
3443 git_tags_body(\@taglist, 0, 15,
3444 $#taglist <= 15 ? undef :
3445 $cgi->a({-href => href(action=>"tags")}, "..."));
3446 }
3447
3448 if (@headlist) {
3449 git_print_header_div('heads');
3450 git_heads_body(\@headlist, $head, 0, 15,
3451 $#headlist <= 15 ? undef :
3452 $cgi->a({-href => href(action=>"heads")}, "..."));
3453 }
3454
3455 if (@forklist) {
3456 git_print_header_div('forks');
3457 git_project_list_body(\@forklist, undef, 0, 15,
3458 $#forklist <= 15 ? undef :
3459 $cgi->a({-href => href(action=>"forks")}, "..."),
3460 'noheader');
3461 }
3462
3463 git_footer_html();
3464 }
3465
3466 sub git_tag {
3467 my $head = git_get_head_hash($project);
3468 git_header_html();
3469 git_print_page_nav('','', $head,undef,$head);
3470 my %tag = parse_tag($hash);
3471
3472 if (! %tag) {
3473 die_error(undef, "Unknown tag object");
3474 }
3475
3476 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
3477 print "<div class=\"title_text\">\n" .
3478 "<table cellspacing=\"0\">\n" .
3479 "<tr>\n" .
3480 "<td>object</td>\n" .
3481 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
3482 $tag{'object'}) . "</td>\n" .
3483 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
3484 $tag{'type'}) . "</td>\n" .
3485 "</tr>\n";
3486 if (defined($tag{'author'})) {
3487 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
3488 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
3489 print "<tr><td></td><td>" . $ad{'rfc2822'} .
3490 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
3491 "</td></tr>\n";
3492 }
3493 print "</table>\n\n" .
3494 "</div>\n";
3495 print "<div class=\"page_body\">";
3496 my $comment = $tag{'comment'};
3497 foreach my $line (@$comment) {
3498 chomp $line;
3499 print esc_html($line, -nbsp=>1) . "<br/>\n";
3500 }
3501 print "</div>\n";
3502 git_footer_html();
3503 }
3504
3505 sub git_blame2 {
3506 my $fd;
3507 my $ftype;
3508
3509 my ($have_blame) = gitweb_check_feature('blame');
3510 if (!$have_blame) {
3511 die_error('403 Permission denied', "Permission denied");
3512 }
3513 die_error('404 Not Found', "File name not defined") if (!$file_name);
3514 $hash_base ||= git_get_head_hash($project);
3515 die_error(undef, "Couldn't find base commit") unless ($hash_base);
3516 my %co = parse_commit($hash_base)
3517 or die_error(undef, "Reading commit failed");
3518 if (!defined $hash) {
3519 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
3520 or die_error(undef, "Error looking up file");
3521 }
3522 $ftype = git_get_type($hash);
3523 if ($ftype !~ "blob") {
3524 die_error('400 Bad Request', "Object is not a blob");
3525 }
3526 open ($fd, "-|", git_cmd(), "blame", '-p', '--',
3527 $file_name, $hash_base)
3528 or die_error(undef, "Open git-blame failed");
3529 git_header_html();
3530 my $formats_nav =
3531 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3532 "blob") .
3533 " | " .
3534 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3535 "history") .
3536 " | " .
3537 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
3538 "HEAD");
3539 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3540 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3541 git_print_page_path($file_name, $ftype, $hash_base);
3542 my @rev_color = (qw(light2 dark2));
3543 my $num_colors = scalar(@rev_color);
3544 my $current_color = 0;
3545 my $last_rev;
3546 print <<HTML;
3547 <div class="page_body">
3548 <table class="blame">
3549 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
3550 HTML
3551 my %metainfo = ();
3552 while (1) {
3553 $_ = <$fd>;
3554 last unless defined $_;
3555 my ($full_rev, $orig_lineno, $lineno, $group_size) =
3556 /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;
3557 if (!exists $metainfo{$full_rev}) {
3558 $metainfo{$full_rev} = {};
3559 }
3560 my $meta = $metainfo{$full_rev};
3561 while (<$fd>) {
3562 last if (s/^\t//);
3563 if (/^(\S+) (.*)$/) {
3564 $meta->{$1} = $2;
3565 }
3566 }
3567 my $data = $_;
3568 chomp $data;
3569 my $rev = substr($full_rev, 0, 8);
3570 my $author = $meta->{'author'};
3571 my %date = parse_date($meta->{'author-time'},
3572 $meta->{'author-tz'});
3573 my $date = $date{'iso-tz'};
3574 if ($group_size) {
3575 $current_color = ++$current_color % $num_colors;
3576 }
3577 print "<tr class=\"$rev_color[$current_color]\">\n";
3578 if ($group_size) {
3579 print "<td class=\"sha1\"";
3580 print " title=\"". esc_html($author) . ", $date\"";
3581 print " rowspan=\"$group_size\"" if ($group_size > 1);
3582 print ">";
3583 print $cgi->a({-href => href(action=>"commit",
3584 hash=>$full_rev,
3585 file_name=>$file_name)},
3586 esc_html($rev));
3587 print "</td>\n";
3588 }
3589 open (my $dd, "-|", git_cmd(), "rev-parse", "$full_rev^")
3590 or die_error(undef, "Open git-rev-parse failed");
3591 my $parent_commit = <$dd>;
3592 close $dd;
3593 chomp($parent_commit);
3594 my $blamed = href(action => 'blame',
3595 file_name => $meta->{'filename'},
3596 hash_base => $parent_commit);
3597 print "<td class=\"linenr\">";
3598 print $cgi->a({ -href => "$blamed#l$orig_lineno",
3599 -id => "l$lineno",
3600 -class => "linenr" },
3601 esc_html($lineno));
3602 print "</td>";
3603 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
3604 print "</tr>\n";
3605 }
3606 print "</table>\n";
3607 print "</div>";
3608 close $fd
3609 or print "Reading blob failed\n";
3610 git_footer_html();
3611 }
3612
3613 sub git_blame {
3614 my $fd;
3615
3616 my ($have_blame) = gitweb_check_feature('blame');
3617 if (!$have_blame) {
3618 die_error('403 Permission denied', "Permission denied");
3619 }
3620 die_error('404 Not Found', "File name not defined") if (!$file_name);
3621 $hash_base ||= git_get_head_hash($project);
3622 die_error(undef, "Couldn't find base commit") unless ($hash_base);
3623 my %co = parse_commit($hash_base)
3624 or die_error(undef, "Reading commit failed");
3625 if (!defined $hash) {
3626 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
3627 or die_error(undef, "Error lookup file");
3628 }
3629 open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
3630 or die_error(undef, "Open git-annotate failed");
3631 git_header_html();
3632 my $formats_nav =
3633 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3634 "blob") .
3635 " | " .
3636 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3637 "history") .
3638 " | " .
3639 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
3640 "HEAD");
3641 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3642 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3643 git_print_page_path($file_name, 'blob', $hash_base);
3644 print "<div class=\"page_body\">\n";
3645 print <<HTML;
3646 <table class="blame">
3647 <tr>
3648 <th>Commit</th>
3649 <th>Age</th>
3650 <th>Author</th>
3651 <th>Line</th>
3652 <th>Data</th>
3653 </tr>
3654 HTML
3655 my @line_class = (qw(light dark));
3656 my $line_class_len = scalar (@line_class);
3657 my $line_class_num = $#line_class;
3658 while (my $line = <$fd>) {
3659 my $long_rev;
3660 my $short_rev;
3661 my $author;
3662 my $time;
3663 my $lineno;
3664 my $data;
3665 my $age;
3666 my $age_str;
3667 my $age_class;
3668
3669 chomp $line;
3670 $line_class_num = ($line_class_num + 1) % $line_class_len;
3671
3672 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
3673 $long_rev = $1;
3674 $author = $2;
3675 $time = $3;
3676 $lineno = $4;
3677 $data = $5;
3678 } else {
3679 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
3680 next;
3681 }
3682 $short_rev = substr ($long_rev, 0, 8);
3683 $age = time () - $time;
3684 $age_str = age_string ($age);
3685 $age_str =~ s/ /&nbsp;/g;
3686 $age_class = age_class($age);
3687 $author = esc_html ($author);
3688 $author =~ s/ /&nbsp;/g;
3689
3690 $data = untabify($data);
3691 $data = esc_html ($data);
3692
3693 print <<HTML;
3694 <tr class="$line_class[$line_class_num]">
3695 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
3696 <td class="$age_class">$age_str</td>
3697 <td>$author</td>
3698 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
3699 <td class="pre">$data</td>
3700 </tr>
3701 HTML
3702 } # while (my $line = <$fd>)
3703 print "</table>\n\n";
3704 close $fd
3705 or print "Reading blob failed.\n";
3706 print "</div>";
3707 git_footer_html();
3708 }
3709
3710 sub git_tags {
3711 my $head = git_get_head_hash($project);
3712 git_header_html();
3713 git_print_page_nav('','', $head,undef,$head);
3714 git_print_header_div('summary', $project);
3715
3716 my @tagslist = git_get_tags_list();
3717 if (@tagslist) {
3718 git_tags_body(\@tagslist);
3719 }
3720 git_footer_html();
3721 }
3722
3723 sub git_heads {
3724 my $head = git_get_head_hash($project);
3725 git_header_html();
3726 git_print_page_nav('','', $head,undef,$head);
3727 git_print_header_div('summary', $project);
3728
3729 my @headslist = git_get_heads_list();
3730 if (@headslist) {
3731 git_heads_body(\@headslist, $head);
3732 }
3733 git_footer_html();
3734 }
3735
3736 sub git_blob_plain {
3737 my $expires;
3738
3739 if (!defined $hash) {
3740 if (defined $file_name) {
3741 my $base = $hash_base || git_get_head_hash($project);
3742 $hash = git_get_hash_by_path($base, $file_name, "blob")
3743 or die_error(undef, "Error lookup file");
3744 } else {
3745 die_error(undef, "No file name defined");
3746 }
3747 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3748 # blobs defined by non-textual hash id's can be cached
3749 $expires = "+1d";
3750 }
3751
3752 my $type = shift;
3753 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
3754 or die_error(undef, "Couldn't cat $file_name, $hash");
3755
3756 $type ||= blob_mimetype($fd, $file_name);
3757
3758 # save as filename, even when no $file_name is given
3759 my $save_as = "$hash";
3760 if (defined $file_name) {
3761 $save_as = $file_name;
3762 } elsif ($type =~ m/^text\//) {
3763 $save_as .= '.txt';
3764 }
3765
3766 print $cgi->header(
3767 -type => "$type",
3768 -expires=>$expires,
3769 -content_disposition => 'inline; filename="' . "$save_as" . '"');
3770 undef $/;
3771 binmode STDOUT, ':raw';
3772 print <$fd>;
3773 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
3774 $/ = "\n";
3775 close $fd;
3776 }
3777
3778 sub git_blob {
3779 my $expires;
3780
3781 if (!defined $hash) {
3782 if (defined $file_name) {
3783 my $base = $hash_base || git_get_head_hash($project);
3784 $hash = git_get_hash_by_path($base, $file_name, "blob")
3785 or die_error(undef, "Error lookup file");
3786 } else {
3787 die_error(undef, "No file name defined");
3788 }
3789 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3790 # blobs defined by non-textual hash id's can be cached
3791 $expires = "+1d";
3792 }
3793
3794 my ($have_blame) = gitweb_check_feature('blame');
3795 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
3796 or die_error(undef, "Couldn't cat $file_name, $hash");
3797 my $mimetype = blob_mimetype($fd, $file_name);
3798 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)!) {
3799 close $fd;
3800 return git_blob_plain($mimetype);
3801 }
3802 # we can have blame only for text/* mimetype
3803 $have_blame &&= ($mimetype =~ m!^text/!);
3804
3805 git_header_html(undef, $expires);
3806 my $formats_nav = '';
3807 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3808 if (defined $file_name) {
3809 if ($have_blame) {
3810 $formats_nav .=
3811 $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
3812 hash=>$hash, file_name=>$file_name)},
3813 "blame") .
3814 " | ";
3815 }
3816 $formats_nav .=
3817 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3818 hash=>$hash, file_name=>$file_name)},
3819 "history") .
3820 " | " .
3821 $cgi->a({-href => href(action=>"blob_plain",
3822 hash=>$hash, file_name=>$file_name)},
3823 "raw") .
3824 " | " .
3825 $cgi->a({-href => href(action=>"blob",
3826 hash_base=>"HEAD", file_name=>$file_name)},
3827 "HEAD");
3828 } else {
3829 $formats_nav .=
3830 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "raw");
3831 }
3832 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3833 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3834 } else {
3835 print "<div class=\"page_nav\">\n" .
3836 "<br/><br/></div>\n" .
3837 "<div class=\"title\">$hash</div>\n";
3838 }
3839 git_print_page_path($file_name, "blob", $hash_base);
3840 print "<div class=\"page_body\">\n";
3841 if ($mimetype =~ m!^text/!) {
3842 my $nr;
3843 while (my $line = <$fd>) {
3844 chomp $line;
3845 $nr++;
3846 $line = untabify($line);
3847 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
3848 $nr, $nr, $nr, esc_html($line, -nbsp=>1);
3849 }
3850 } elsif ($mimetype =~ m!^image/!) {
3851 print qq!<img type="$mimetype"!;
3852 if ($file_name) {
3853 print qq! alt="$file_name" title="$file_name"!;
3854 }
3855 print qq! src="! .
3856 href(action=>"blob_plain", hash=>$hash,
3857 hash_base=>$hash_base, file_name=>$file_name) .
3858 qq!" />\n!;
3859 }
3860 close $fd
3861 or print "Reading blob failed.\n";
3862 print "</div>";
3863 git_footer_html();
3864 }
3865
3866 sub git_tree {
3867 my $have_snapshot = gitweb_have_snapshot();
3868
3869 if (!defined $hash_base) {
3870 $hash_base = "HEAD";
3871 }
3872 if (!defined $hash) {
3873 if (defined $file_name) {
3874 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
3875 } else {
3876 $hash = $hash_base;
3877 }
3878 }
3879 $/ = "\0";
3880 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
3881 or die_error(undef, "Open git-ls-tree failed");
3882 my @entries = map { chomp; $_ } <$fd>;
3883 close $fd or die_error(undef, "Reading tree failed");
3884 $/ = "\n";
3885
3886 my $refs = git_get_references();
3887 my $ref = format_ref_marker($refs, $hash_base);
3888 git_header_html();
3889 my $basedir = '';
3890 my ($have_blame) = gitweb_check_feature('blame');
3891 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3892 my @views_nav = ();
3893 if (defined $file_name) {
3894 push @views_nav,
3895 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3896 hash=>$hash, file_name=>$file_name)},
3897 "history"),
3898 $cgi->a({-href => href(action=>"tree",
3899 hash_base=>"HEAD", file_name=>$file_name)},
3900 "HEAD"),
3901 }
3902 if ($have_snapshot) {
3903 # FIXME: Should be available when we have no hash base as well.
3904 push @views_nav,
3905 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)},
3906 "snapshot");
3907 }
3908 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
3909 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
3910 } else {
3911 undef $hash_base;
3912 print "<div class=\"page_nav\">\n";
3913 print "<br/><br/></div>\n";
3914 print "<div class=\"title\">$hash</div>\n";
3915 }
3916 if (defined $file_name) {
3917 $basedir = $file_name;
3918 if ($basedir ne '' && substr($basedir, -1) ne '/') {
3919 $basedir .= '/';
3920 }
3921 }
3922 git_print_page_path($file_name, 'tree', $hash_base);
3923 print "<div class=\"page_body\">\n";
3924 print "<table cellspacing=\"0\">\n";
3925 my $alternate = 1;
3926 # '..' (top directory) link if possible
3927 if (defined $hash_base &&
3928 defined $file_name && $file_name =~ m![^/]+$!) {
3929 if ($alternate) {
3930 print "<tr class=\"dark\">\n";
3931 } else {
3932 print "<tr class=\"light\">\n";
3933 }
3934 $alternate ^= 1;
3935
3936 my $up = $file_name;
3937 $up =~ s!/?[^/]+$!!;
3938 undef $up unless $up;
3939 # based on git_print_tree_entry
3940 print '<td class="mode">' . mode_str('040000') . "</td>\n";
3941 print '<td class="list">';
3942 print $cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,
3943 file_name=>$up)},
3944 "..");
3945 print "</td>\n";
3946 print "<td class=\"link\"></td>\n";
3947
3948 print "</tr>\n";
3949 }
3950 foreach my $line (@entries) {
3951 my %t = parse_ls_tree_line($line, -z => 1);
3952
3953 if ($alternate) {
3954 print "<tr class=\"dark\">\n";
3955 } else {
3956 print "<tr class=\"light\">\n";
3957 }
3958 $alternate ^= 1;
3959
3960 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
3961
3962 print "</tr>\n";
3963 }
3964 print "</table>\n" .
3965 "</div>";
3966 git_footer_html();
3967 }
3968
3969 sub git_snapshot {
3970 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
3971 my $have_snapshot = (defined $ctype && defined $suffix);
3972 if (!$have_snapshot) {
3973 die_error('403 Permission denied', "Permission denied");
3974 }
3975
3976 if (!defined $hash) {
3977 $hash = git_get_head_hash($project);
3978 }
3979
3980 my $filename = decode_utf8(basename($project)) . "-$hash.tar.$suffix";
3981
3982 print $cgi->header(
3983 -type => "application/$ctype",
3984 -content_disposition => 'inline; filename="' . "$filename" . '"',
3985 -status => '200 OK');
3986
3987 my $git = git_cmd_str();
3988 my $name = $project;
3989 $name =~ s/\047/\047\\\047\047/g;
3990 open my $fd, "-|",
3991 "$git archive --format=tar --prefix=\'$name\'/ $hash | $command"
3992 or die_error(undef, "Execute git-tar-tree failed");
3993 binmode STDOUT, ':raw';
3994 print <$fd>;
3995 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
3996 close $fd;
3997
3998 }
3999
4000 sub git_log {
4001 my $head = git_get_head_hash($project);
4002 if (!defined $hash) {
4003 $hash = $head;
4004 }
4005 if (!defined $page) {
4006 $page = 0;
4007 }
4008 my $refs = git_get_references();
4009
4010 my @commitlist = parse_commits($hash, 101, (100 * $page));
4011
4012 my $paging_nav = format_paging_nav('log', $hash, $head, $page, (100 * ($page+1)));
4013
4014 git_header_html();
4015 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
4016
4017 if (!@commitlist) {
4018 my %co = parse_commit($hash);
4019
4020 git_print_header_div('summary', $project);
4021 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
4022 }
4023 my $to = ($#commitlist >= 99) ? (99) : ($#commitlist);
4024 for (my $i = 0; $i <= $to; $i++) {
4025 my %co = %{$commitlist[$i]};
4026 next if !%co;
4027 my $commit = $co{'id'};
4028 my $ref = format_ref_marker($refs, $commit);
4029 my %ad = parse_date($co{'author_epoch'});
4030 git_print_header_div('commit',
4031 "<span class=\"age\">$co{'age_string'}</span>" .
4032 esc_html($co{'title'}) . $ref,
4033 $commit);
4034 print "<div class=\"title_text\">\n" .
4035 "<div class=\"log_link\">\n" .
4036 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
4037 " | " .
4038 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
4039 " | " .
4040 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
4041 "<br/>\n" .
4042 "</div>\n" .
4043 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
4044 "</div>\n";
4045
4046 print "<div class=\"log_body\">\n";
4047 git_print_log($co{'comment'}, -final_empty_line=> 1);
4048 print "</div>\n";
4049 }
4050 if ($#commitlist >= 100) {
4051 print "<div class=\"page_nav\">\n";
4052 print $cgi->a({-href => href(action=>"log", hash=>$hash, page=>$page+1),
4053 -accesskey => "n", -title => "Alt-n"}, "next");
4054 print "</div>\n";
4055 }
4056 git_footer_html();
4057 }
4058
4059 sub git_commit {
4060 $hash ||= $hash_base || "HEAD";
4061 my %co = parse_commit($hash);
4062 if (!%co) {
4063 die_error(undef, "Unknown commit object");
4064 }
4065 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4066 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
4067
4068 my $parent = $co{'parent'};
4069 my $parents = $co{'parents'}; # listref
4070
4071 # we need to prepare $formats_nav before any parameter munging
4072 my $formats_nav;
4073 if (!defined $parent) {
4074 # --root commitdiff
4075 $formats_nav .= '(initial)';
4076 } elsif (@$parents == 1) {
4077 # single parent commit
4078 $formats_nav .=
4079 '(parent: ' .
4080 $cgi->a({-href => href(action=>"commit",
4081 hash=>$parent)},
4082 esc_html(substr($parent, 0, 7))) .
4083 ')';
4084 } else {
4085 # merge commit
4086 $formats_nav .=
4087 '(merge: ' .
4088 join(' ', map {
4089 $cgi->a({-href => href(action=>"commit",
4090 hash=>$_)},
4091 esc_html(substr($_, 0, 7)));
4092 } @$parents ) .
4093 ')';
4094 }
4095
4096 if (!defined $parent) {
4097 $parent = "--root";
4098 }
4099 my @difftree;
4100 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
4101 @diff_opts,
4102 (@$parents <= 1 ? $parent : '-c'),
4103 $hash, "--"
4104 or die_error(undef, "Open git-diff-tree failed");
4105 @difftree = map { chomp; $_ } <$fd>;
4106 close $fd or die_error(undef, "Reading git-diff-tree failed");
4107
4108 # non-textual hash id's can be cached
4109 my $expires;
4110 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4111 $expires = "+1d";
4112 }
4113 my $refs = git_get_references();
4114 my $ref = format_ref_marker($refs, $co{'id'});
4115
4116 my $have_snapshot = gitweb_have_snapshot();
4117
4118 git_header_html(undef, $expires);
4119 git_print_page_nav('commit', '',
4120 $hash, $co{'tree'}, $hash,
4121 $formats_nav);
4122
4123 if (defined $co{'parent'}) {
4124 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
4125 } else {
4126 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
4127 }
4128 print "<div class=\"title_text\">\n" .
4129 "<table cellspacing=\"0\">\n";
4130 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
4131 "<tr>" .
4132 "<td></td><td> $ad{'rfc2822'}";
4133 if ($ad{'hour_local'} < 6) {
4134 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
4135 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4136 } else {
4137 printf(" (%02d:%02d %s)",
4138 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4139 }
4140 print "</td>" .
4141 "</tr>\n";
4142 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
4143 print "<tr><td></td><td> $cd{'rfc2822'}" .
4144 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
4145 "</td></tr>\n";
4146 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
4147 print "<tr>" .
4148 "<td>tree</td>" .
4149 "<td class=\"sha1\">" .
4150 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
4151 class => "list"}, $co{'tree'}) .
4152 "</td>" .
4153 "<td class=\"link\">" .
4154 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
4155 "tree");
4156 if ($have_snapshot) {
4157 print " | " .
4158 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
4159 }
4160 print "</td>" .
4161 "</tr>\n";
4162
4163 foreach my $par (@$parents) {
4164 print "<tr>" .
4165 "<td>parent</td>" .
4166 "<td class=\"sha1\">" .
4167 $cgi->a({-href => href(action=>"commit", hash=>$par),
4168 class => "list"}, $par) .
4169 "</td>" .
4170 "<td class=\"link\">" .
4171 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
4172 " | " .
4173 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
4174 "</td>" .
4175 "</tr>\n";
4176 }
4177 print "</table>".
4178 "</div>\n";
4179
4180 print "<div class=\"page_body\">\n";
4181 git_print_log($co{'comment'});
4182 print "</div>\n";
4183
4184 git_difftree_body(\@difftree, $hash, @$parents);
4185
4186 git_footer_html();
4187 }
4188
4189 sub git_object {
4190 # object is defined by:
4191 # - hash or hash_base alone
4192 # - hash_base and file_name
4193 my $type;
4194
4195 # - hash or hash_base alone
4196 if ($hash || ($hash_base && !defined $file_name)) {
4197 my $object_id = $hash || $hash_base;
4198
4199 my $git_command = git_cmd_str();
4200 open my $fd, "-|", "$git_command cat-file -t $object_id 2>/dev/null"
4201 or die_error('404 Not Found', "Object does not exist");
4202 $type = <$fd>;
4203 chomp $type;
4204 close $fd
4205 or die_error('404 Not Found', "Object does not exist");
4206
4207 # - hash_base and file_name
4208 } elsif ($hash_base && defined $file_name) {
4209 $file_name =~ s,/+$,,;
4210
4211 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
4212 or die_error('404 Not Found', "Base object does not exist");
4213
4214 # here errors should not hapen
4215 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
4216 or die_error(undef, "Open git-ls-tree failed");
4217 my $line = <$fd>;
4218 close $fd;
4219
4220 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
4221 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
4222 die_error('404 Not Found', "File or directory for given base does not exist");
4223 }
4224 $type = $2;
4225 $hash = $3;
4226 } else {
4227 die_error('404 Not Found', "Not enough information to find object");
4228 }
4229
4230 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
4231 hash=>$hash, hash_base=>$hash_base,
4232 file_name=>$file_name),
4233 -status => '302 Found');
4234 }
4235
4236 sub git_blobdiff {
4237 my $format = shift || 'html';
4238
4239 my $fd;
4240 my @difftree;
4241 my %diffinfo;
4242 my $expires;
4243
4244 # preparing $fd and %diffinfo for git_patchset_body
4245 # new style URI
4246 if (defined $hash_base && defined $hash_parent_base) {
4247 if (defined $file_name) {
4248 # read raw output
4249 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4250 $hash_parent_base, $hash_base,
4251 "--", (defined $file_parent ? $file_parent : ()), $file_name
4252 or die_error(undef, "Open git-diff-tree failed");
4253 @difftree = map { chomp; $_ } <$fd>;
4254 close $fd
4255 or die_error(undef, "Reading git-diff-tree failed");
4256 @difftree
4257 or die_error('404 Not Found', "Blob diff not found");
4258
4259 } elsif (defined $hash &&
4260 $hash =~ /[0-9a-fA-F]{40}/) {
4261 # try to find filename from $hash
4262
4263 # read filtered raw output
4264 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4265 $hash_parent_base, $hash_base, "--"
4266 or die_error(undef, "Open git-diff-tree failed");
4267 @difftree =
4268 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
4269 # $hash == to_id
4270 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
4271 map { chomp; $_ } <$fd>;
4272 close $fd
4273 or die_error(undef, "Reading git-diff-tree failed");
4274 @difftree
4275 or die_error('404 Not Found', "Blob diff not found");
4276
4277 } else {
4278 die_error('404 Not Found', "Missing one of the blob diff parameters");
4279 }
4280
4281 if (@difftree > 1) {
4282 die_error('404 Not Found', "Ambiguous blob diff specification");
4283 }
4284
4285 %diffinfo = parse_difftree_raw_line($difftree[0]);
4286 $file_parent ||= $diffinfo{'from_file'} || $file_name || $diffinfo{'file'};
4287 $file_name ||= $diffinfo{'to_file'} || $diffinfo{'file'};
4288
4289 $hash_parent ||= $diffinfo{'from_id'};
4290 $hash ||= $diffinfo{'to_id'};
4291
4292 # non-textual hash id's can be cached
4293 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
4294 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
4295 $expires = '+1d';
4296 }
4297
4298 # open patch output
4299 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4300 '-p', ($format eq 'html' ? "--full-index" : ()),
4301 $hash_parent_base, $hash_base,
4302 "--", (defined $file_parent ? $file_parent : ()), $file_name
4303 or die_error(undef, "Open git-diff-tree failed");
4304 }
4305
4306 # old/legacy style URI
4307 if (!%diffinfo && # if new style URI failed
4308 defined $hash && defined $hash_parent) {
4309 # fake git-diff-tree raw output
4310 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
4311 $diffinfo{'from_id'} = $hash_parent;
4312 $diffinfo{'to_id'} = $hash;
4313 if (defined $file_name) {
4314 if (defined $file_parent) {
4315 $diffinfo{'status'} = '2';
4316 $diffinfo{'from_file'} = $file_parent;
4317 $diffinfo{'to_file'} = $file_name;
4318 } else { # assume not renamed
4319 $diffinfo{'status'} = '1';
4320 $diffinfo{'from_file'} = $file_name;
4321 $diffinfo{'to_file'} = $file_name;
4322 }
4323 } else { # no filename given
4324 $diffinfo{'status'} = '2';
4325 $diffinfo{'from_file'} = $hash_parent;
4326 $diffinfo{'to_file'} = $hash;
4327 }
4328
4329 # non-textual hash id's can be cached
4330 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
4331 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4332 $expires = '+1d';
4333 }
4334
4335 # open patch output
4336 open $fd, "-|", git_cmd(), "diff", @diff_opts,
4337 '-p', ($format eq 'html' ? "--full-index" : ()),
4338 $hash_parent, $hash, "--"
4339 or die_error(undef, "Open git-diff failed");
4340 } else {
4341 die_error('404 Not Found', "Missing one of the blob diff parameters")
4342 unless %diffinfo;
4343 }
4344
4345 # header
4346 if ($format eq 'html') {
4347 my $formats_nav =
4348 $cgi->a({-href => href(action=>"blobdiff_plain",
4349 hash=>$hash, hash_parent=>$hash_parent,
4350 hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
4351 file_name=>$file_name, file_parent=>$file_parent)},
4352 "raw");
4353 git_header_html(undef, $expires);
4354 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4355 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4356 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4357 } else {
4358 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
4359 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
4360 }
4361 if (defined $file_name) {
4362 git_print_page_path($file_name, "blob", $hash_base);
4363 } else {
4364 print "<div class=\"page_path\"></div>\n";
4365 }
4366
4367 } elsif ($format eq 'plain') {
4368 print $cgi->header(
4369 -type => 'text/plain',
4370 -charset => 'utf-8',
4371 -expires => $expires,
4372 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
4373
4374 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
4375
4376 } else {
4377 die_error(undef, "Unknown blobdiff format");
4378 }
4379
4380 # patch
4381 if ($format eq 'html') {
4382 print "<div class=\"page_body\">\n";
4383
4384 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
4385 close $fd;
4386
4387 print "</div>\n"; # class="page_body"
4388 git_footer_html();
4389
4390 } else {
4391 while (my $line = <$fd>) {
4392 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
4393 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
4394
4395 print $line;
4396
4397 last if $line =~ m!^\+\+\+!;
4398 }
4399 local $/ = undef;
4400 print <$fd>;
4401 close $fd;
4402 }
4403 }
4404
4405 sub git_blobdiff_plain {
4406 git_blobdiff('plain');
4407 }
4408
4409 sub git_commitdiff {
4410 my $format = shift || 'html';
4411 $hash ||= $hash_base || "HEAD";
4412 my %co = parse_commit($hash);
4413 if (!%co) {
4414 die_error(undef, "Unknown commit object");
4415 }
4416
4417 # we need to prepare $formats_nav before any parameter munging
4418 my $formats_nav;
4419 if ($format eq 'html') {
4420 $formats_nav =
4421 $cgi->a({-href => href(action=>"commitdiff_plain",
4422 hash=>$hash, hash_parent=>$hash_parent)},
4423 "raw");
4424
4425 if (defined $hash_parent) {
4426 # commitdiff with two commits given
4427 my $hash_parent_short = $hash_parent;
4428 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4429 $hash_parent_short = substr($hash_parent, 0, 7);
4430 }
4431 $formats_nav .=
4432 ' (from: ' .
4433 $cgi->a({-href => href(action=>"commitdiff",
4434 hash=>$hash_parent)},
4435 esc_html($hash_parent_short)) .
4436 ')';
4437 } elsif (!$co{'parent'}) {
4438 # --root commitdiff
4439 $formats_nav .= ' (initial)';
4440 } elsif (scalar @{$co{'parents'}} == 1) {
4441 # single parent commit
4442 $formats_nav .=
4443 ' (parent: ' .
4444 $cgi->a({-href => href(action=>"commitdiff",
4445 hash=>$co{'parent'})},
4446 esc_html(substr($co{'parent'}, 0, 7))) .
4447 ')';
4448 } else {
4449 # merge commit
4450 $formats_nav .=
4451 ' (merge: ' .
4452 join(' ', map {
4453 $cgi->a({-href => href(action=>"commitdiff",
4454 hash=>$_)},
4455 esc_html(substr($_, 0, 7)));
4456 } @{$co{'parents'}} ) .
4457 ')';
4458 }
4459 }
4460
4461 my $hash_parent_param = $hash_parent;
4462 if (!defined $hash_parent) {
4463 $hash_parent_param =
4464 @{$co{'parents'}} > 1 ? '-c' : $co{'parent'} || '--root';
4465 }
4466
4467 # read commitdiff
4468 my $fd;
4469 my @difftree;
4470 if ($format eq 'html') {
4471 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4472 "--no-commit-id", "--patch-with-raw", "--full-index",
4473 $hash_parent_param, $hash, "--"
4474 or die_error(undef, "Open git-diff-tree failed");
4475
4476 while (my $line = <$fd>) {
4477 chomp $line;
4478 # empty line ends raw part of diff-tree output
4479 last unless $line;
4480 push @difftree, scalar parse_difftree_raw_line($line);
4481 }
4482
4483 } elsif ($format eq 'plain') {
4484 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4485 '-p', $hash_parent_param, $hash, "--"
4486 or die_error(undef, "Open git-diff-tree failed");
4487
4488 } else {
4489 die_error(undef, "Unknown commitdiff format");
4490 }
4491
4492 # non-textual hash id's can be cached
4493 my $expires;
4494 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4495 $expires = "+1d";
4496 }
4497
4498 # write commit message
4499 if ($format eq 'html') {
4500 my $refs = git_get_references();
4501 my $ref = format_ref_marker($refs, $co{'id'});
4502
4503 git_header_html(undef, $expires);
4504 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
4505 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
4506 git_print_authorship(\%co);
4507 print "<div class=\"page_body\">\n";
4508 if (@{$co{'comment'}} > 1) {
4509 print "<div class=\"log\">\n";
4510 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
4511 print "</div>\n"; # class="log"
4512 }
4513
4514 } elsif ($format eq 'plain') {
4515 my $refs = git_get_references("tags");
4516 my $tagname = git_get_rev_name_tags($hash);
4517 my $filename = basename($project) . "-$hash.patch";
4518
4519 print $cgi->header(
4520 -type => 'text/plain',
4521 -charset => 'utf-8',
4522 -expires => $expires,
4523 -content_disposition => 'inline; filename="' . "$filename" . '"');
4524 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4525 print <<TEXT;
4526 From: $co{'author'}
4527 Date: $ad{'rfc2822'} ($ad{'tz_local'})
4528 Subject: $co{'title'}
4529 TEXT
4530 print "X-Git-Tag: $tagname\n" if $tagname;
4531 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
4532
4533 foreach my $line (@{$co{'comment'}}) {
4534 print "$line\n";
4535 }
4536 print "---\n\n";
4537 }
4538
4539 # write patch
4540 if ($format eq 'html') {
4541 git_difftree_body(\@difftree, $hash, $hash_parent || @{$co{'parents'}});
4542 print "<br/>\n";
4543
4544 git_patchset_body($fd, \@difftree, $hash, $hash_parent || @{$co{'parents'}});
4545 close $fd;
4546 print "</div>\n"; # class="page_body"
4547 git_footer_html();
4548
4549 } elsif ($format eq 'plain') {
4550 local $/ = undef;
4551 print <$fd>;
4552 close $fd
4553 or print "Reading git-diff-tree failed\n";
4554 }
4555 }
4556
4557 sub git_commitdiff_plain {
4558 git_commitdiff('plain');
4559 }
4560
4561 sub git_history {
4562 if (!defined $hash_base) {
4563 $hash_base = git_get_head_hash($project);
4564 }
4565 if (!defined $page) {
4566 $page = 0;
4567 }
4568 my $ftype;
4569 my %co = parse_commit($hash_base);
4570 if (!%co) {
4571 die_error(undef, "Unknown commit object");
4572 }
4573
4574 my $refs = git_get_references();
4575 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
4576
4577 if (!defined $hash && defined $file_name) {
4578 $hash = git_get_hash_by_path($hash_base, $file_name);
4579 }
4580 if (defined $hash) {
4581 $ftype = git_get_type($hash);
4582 }
4583
4584 my @commitlist = parse_commits($hash_base, 101, (100 * $page), "--full-history", $file_name);
4585
4586 my $paging_nav = '';
4587 if ($page > 0) {
4588 $paging_nav .=
4589 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4590 file_name=>$file_name)},
4591 "first");
4592 $paging_nav .= " &sdot; " .
4593 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4594 file_name=>$file_name, page=>$page-1),
4595 -accesskey => "p", -title => "Alt-p"}, "prev");
4596 } else {
4597 $paging_nav .= "first";
4598 $paging_nav .= " &sdot; prev";
4599 }
4600 if ($#commitlist >= 100) {
4601 $paging_nav .= " &sdot; " .
4602 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4603 file_name=>$file_name, page=>$page+1),
4604 -accesskey => "n", -title => "Alt-n"}, "next");
4605 } else {
4606 $paging_nav .= " &sdot; next";
4607 }
4608 my $next_link = '';
4609 if ($#commitlist >= 100) {
4610 $next_link =
4611 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4612 file_name=>$file_name, page=>$page+1),
4613 -accesskey => "n", -title => "Alt-n"}, "next");
4614 }
4615
4616 git_header_html();
4617 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
4618 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4619 git_print_page_path($file_name, $ftype, $hash_base);
4620
4621 git_history_body(\@commitlist, 0, 99,
4622 $refs, $hash_base, $ftype, $next_link);
4623
4624 git_footer_html();
4625 }
4626
4627 sub git_search {
4628 my ($have_search) = gitweb_check_feature('search');
4629 if (!$have_search) {
4630 die_error('403 Permission denied', "Permission denied");
4631 }
4632 if (!defined $searchtext) {
4633 die_error(undef, "Text field empty");
4634 }
4635 if (!defined $hash) {
4636 $hash = git_get_head_hash($project);
4637 }
4638 my %co = parse_commit($hash);
4639 if (!%co) {
4640 die_error(undef, "Unknown commit object");
4641 }
4642 if (!defined $page) {
4643 $page = 0;
4644 }
4645
4646 $searchtype ||= 'commit';
4647 if ($searchtype eq 'pickaxe') {
4648 # pickaxe may take all resources of your box and run for several minutes
4649 # with every query - so decide by yourself how public you make this feature
4650 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
4651 if (!$have_pickaxe) {
4652 die_error('403 Permission denied', "Permission denied");
4653 }
4654 }
4655 if ($searchtype eq 'grep') {
4656 my ($have_grep) = gitweb_check_feature('grep');
4657 if (!$have_grep) {
4658 die_error('403 Permission denied', "Permission denied");
4659 }
4660 }
4661
4662 git_header_html();
4663
4664 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
4665 my $greptype;
4666 if ($searchtype eq 'commit') {
4667 $greptype = "--grep=";
4668 } elsif ($searchtype eq 'author') {
4669 $greptype = "--author=";
4670 } elsif ($searchtype eq 'committer') {
4671 $greptype = "--committer=";
4672 }
4673 $greptype .= $search_regexp;
4674 my @commitlist = parse_commits($hash, 101, (100 * $page), $greptype);
4675
4676 my $paging_nav = '';
4677 if ($page > 0) {
4678 $paging_nav .=
4679 $cgi->a({-href => href(action=>"search", hash=>$hash,
4680 searchtext=>$searchtext, searchtype=>$searchtype)},
4681 "first");
4682 $paging_nav .= " &sdot; " .
4683 $cgi->a({-href => href(action=>"search", hash=>$hash,
4684 searchtext=>$searchtext, searchtype=>$searchtype,
4685 page=>$page-1),
4686 -accesskey => "p", -title => "Alt-p"}, "prev");
4687 } else {
4688 $paging_nav .= "first";
4689 $paging_nav .= " &sdot; prev";
4690 }
4691 if ($#commitlist >= 100) {
4692 $paging_nav .= " &sdot; " .
4693 $cgi->a({-href => href(action=>"search", hash=>$hash,
4694 searchtext=>$searchtext, searchtype=>$searchtype,
4695 page=>$page+1),
4696 -accesskey => "n", -title => "Alt-n"}, "next");
4697 } else {
4698 $paging_nav .= " &sdot; next";
4699 }
4700 my $next_link = '';
4701 if ($#commitlist >= 100) {
4702 $next_link =
4703 $cgi->a({-href => href(action=>"search", hash=>$hash,
4704 searchtext=>$searchtext, searchtype=>$searchtype,
4705 page=>$page+1),
4706 -accesskey => "n", -title => "Alt-n"}, "next");
4707 }
4708
4709 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
4710 git_print_header_div('commit', esc_html($co{'title'}), $hash);
4711 git_search_grep_body(\@commitlist, 0, 99, $next_link);
4712 }
4713
4714 if ($searchtype eq 'pickaxe') {
4715 git_print_page_nav('','', $hash,$co{'tree'},$hash);
4716 git_print_header_div('commit', esc_html($co{'title'}), $hash);
4717
4718 print "<table cellspacing=\"0\">\n";
4719 my $alternate = 1;
4720 $/ = "\n";
4721 my $git_command = git_cmd_str();
4722 my $searchqtext = $searchtext;
4723 $searchqtext =~ s/'/'\\''/;
4724 open my $fd, "-|", "$git_command rev-list $hash | " .
4725 "$git_command diff-tree -r --stdin -S\'$searchqtext\'";
4726 undef %co;
4727 my @files;
4728 while (my $line = <$fd>) {
4729 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
4730 my %set;
4731 $set{'file'} = $6;
4732 $set{'from_id'} = $3;
4733 $set{'to_id'} = $4;
4734 $set{'id'} = $set{'to_id'};
4735 if ($set{'id'} =~ m/0{40}/) {
4736 $set{'id'} = $set{'from_id'};
4737 }
4738 if ($set{'id'} =~ m/0{40}/) {
4739 next;
4740 }
4741 push @files, \%set;
4742 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
4743 if (%co) {
4744 if ($alternate) {
4745 print "<tr class=\"dark\">\n";
4746 } else {
4747 print "<tr class=\"light\">\n";
4748 }
4749 $alternate ^= 1;
4750 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4751 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
4752 "<td>" .
4753 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
4754 -class => "list subject"},
4755 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
4756 while (my $setref = shift @files) {
4757 my %set = %$setref;
4758 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
4759 hash=>$set{'id'}, file_name=>$set{'file'}),
4760 -class => "list"},
4761 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
4762 "<br/>\n";
4763 }
4764 print "</td>\n" .
4765 "<td class=\"link\">" .
4766 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
4767 " | " .
4768 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
4769 print "</td>\n" .
4770 "</tr>\n";
4771 }
4772 %co = parse_commit($1);
4773 }
4774 }
4775 close $fd;
4776
4777 print "</table>\n";
4778 }
4779
4780 if ($searchtype eq 'grep') {
4781 git_print_page_nav('','', $hash,$co{'tree'},$hash);
4782 git_print_header_div('commit', esc_html($co{'title'}), $hash);
4783
4784 print "<table cellspacing=\"0\">\n";
4785 my $alternate = 1;
4786 my $matches = 0;
4787 $/ = "\n";
4788 open my $fd, "-|", git_cmd(), 'grep', '-n', '-i', '-E', $searchtext, $co{'tree'};
4789 my $lastfile = '';
4790 while (my $line = <$fd>) {
4791 chomp $line;
4792 my ($file, $lno, $ltext, $binary);
4793 last if ($matches++ > 1000);
4794 if ($line =~ /^Binary file (.+) matches$/) {
4795 $file = $1;
4796 $binary = 1;
4797 } else {
4798 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
4799 }
4800 if ($file ne $lastfile) {
4801 $lastfile and print "</td></tr>\n";
4802 if ($alternate++) {
4803 print "<tr class=\"dark\">\n";
4804 } else {
4805 print "<tr class=\"light\">\n";
4806 }
4807 print "<td class=\"list\">".
4808 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
4809 file_name=>"$file"),
4810 -class => "list"}, esc_path($file));
4811 print "</td><td>\n";
4812 $lastfile = $file;
4813 }
4814 if ($binary) {
4815 print "<div class=\"binary\">Binary file</div>\n";
4816 } else {
4817 $ltext = untabify($ltext);
4818 if ($ltext =~ m/^(.*)($searchtext)(.*)$/i) {
4819 $ltext = esc_html($1, -nbsp=>1);
4820 $ltext .= '<span class="match">';
4821 $ltext .= esc_html($2, -nbsp=>1);
4822 $ltext .= '</span>';
4823 $ltext .= esc_html($3, -nbsp=>1);
4824 } else {
4825 $ltext = esc_html($ltext, -nbsp=>1);
4826 }
4827 print "<div class=\"pre\">" .
4828 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
4829 file_name=>"$file").'#l'.$lno,
4830 -class => "linenr"}, sprintf('%4i', $lno))
4831 . ' ' . $ltext . "</div>\n";
4832 }
4833 }
4834 if ($lastfile) {
4835 print "</td></tr>\n";
4836 if ($matches > 1000) {
4837 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
4838 }
4839 } else {
4840 print "<div class=\"diff nodifferences\">No matches found</div>\n";
4841 }
4842 close $fd;
4843
4844 print "</table>\n";
4845 }
4846 git_footer_html();
4847 }
4848
4849 sub git_search_help {
4850 git_header_html();
4851 git_print_page_nav('','', $hash,$hash,$hash);
4852 print <<EOT;
4853 <dl>
4854 <dt><b>commit</b></dt>
4855 <dd>The commit messages and authorship information will be scanned for the given string.</dd>
4856 EOT
4857 my ($have_grep) = gitweb_check_feature('grep');
4858 if ($have_grep) {
4859 print <<EOT;
4860 <dt><b>grep</b></dt>
4861 <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
4862 a different one) are searched for the given
4863 <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a>
4864 (POSIX extended) and the matches are listed. On large
4865 trees, this search can take a while and put some strain on the server, so please use it with
4866 some consideration.</dd>
4867 EOT
4868 }
4869 print <<EOT;
4870 <dt><b>author</b></dt>
4871 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given string.</dd>
4872 <dt><b>committer</b></dt>
4873 <dd>Name and e-mail of the committer and date of commit will be scanned for the given string.</dd>
4874 EOT
4875 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
4876 if ($have_pickaxe) {
4877 print <<EOT;
4878 <dt><b>pickaxe</b></dt>
4879 <dd>All commits that caused the string to appear or disappear from any file (changes that
4880 added, removed or "modified" the string) will be listed. This search can take a while and
4881 takes a lot of strain on the server, so please use it wisely.</dd>
4882 EOT
4883 }
4884 print "</dl>\n";
4885 git_footer_html();
4886 }
4887
4888 sub git_shortlog {
4889 my $head = git_get_head_hash($project);
4890 if (!defined $hash) {
4891 $hash = $head;
4892 }
4893 if (!defined $page) {
4894 $page = 0;
4895 }
4896 my $refs = git_get_references();
4897
4898 my @commitlist = parse_commits($hash, 101, (100 * $page));
4899
4900 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, (100 * ($page+1)));
4901 my $next_link = '';
4902 if ($#commitlist >= 100) {
4903 $next_link =
4904 $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
4905 -accesskey => "n", -title => "Alt-n"}, "next");
4906 }
4907
4908 git_header_html();
4909 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
4910 git_print_header_div('summary', $project);
4911
4912 git_shortlog_body(\@commitlist, 0, 99, $refs, $next_link);
4913
4914 git_footer_html();
4915 }
4916
4917 ## ......................................................................
4918 ## feeds (RSS, Atom; OPML)
4919
4920 sub git_feed {
4921 my $format = shift || 'atom';
4922 my ($have_blame) = gitweb_check_feature('blame');
4923
4924 # Atom: http://www.atomenabled.org/developers/syndication/
4925 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
4926 if ($format ne 'rss' && $format ne 'atom') {
4927 die_error(undef, "Unknown web feed format");
4928 }
4929
4930 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
4931 my $head = $hash || 'HEAD';
4932 my @commitlist = parse_commits($head, 150);
4933
4934 my %latest_commit;
4935 my %latest_date;
4936 my $content_type = "application/$format+xml";
4937 if (defined $cgi->http('HTTP_ACCEPT') &&
4938 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
4939 # browser (feed reader) prefers text/xml
4940 $content_type = 'text/xml';
4941 }
4942 if (defined($commitlist[0])) {
4943 %latest_commit = %{$commitlist[0]};
4944 %latest_date = parse_date($latest_commit{'author_epoch'});
4945 print $cgi->header(
4946 -type => $content_type,
4947 -charset => 'utf-8',
4948 -last_modified => $latest_date{'rfc2822'});
4949 } else {
4950 print $cgi->header(
4951 -type => $content_type,
4952 -charset => 'utf-8');
4953 }
4954
4955 # Optimization: skip generating the body if client asks only
4956 # for Last-Modified date.
4957 return if ($cgi->request_method() eq 'HEAD');
4958
4959 # header variables
4960 my $title = "$site_name - $project/$action";
4961 my $feed_type = 'log';
4962 if (defined $hash) {
4963 $title .= " - '$hash'";
4964 $feed_type = 'branch log';
4965 if (defined $file_name) {
4966 $title .= " :: $file_name";
4967 $feed_type = 'history';
4968 }
4969 } elsif (defined $file_name) {
4970 $title .= " - $file_name";
4971 $feed_type = 'history';
4972 }
4973 $title .= " $feed_type";
4974 my $descr = git_get_project_description($project);
4975 if (defined $descr) {
4976 $descr = esc_html($descr);
4977 } else {
4978 $descr = "$project " .
4979 ($format eq 'rss' ? 'RSS' : 'Atom') .
4980 " feed";
4981 }
4982 my $owner = git_get_project_owner($project);
4983 $owner = esc_html($owner);
4984
4985 #header
4986 my $alt_url;
4987 if (defined $file_name) {
4988 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
4989 } elsif (defined $hash) {
4990 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
4991 } else {
4992 $alt_url = href(-full=>1, action=>"summary");
4993 }
4994 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
4995 if ($format eq 'rss') {
4996 print <<XML;
4997 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
4998 <channel>
4999 XML
5000 print "<title>$title</title>\n" .
5001 "<link>$alt_url</link>\n" .
5002 "<description>$descr</description>\n" .
5003 "<language>en</language>\n";
5004 } elsif ($format eq 'atom') {
5005 print <<XML;
5006 <feed xmlns="http://www.w3.org/2005/Atom">
5007 XML
5008 print "<title>$title</title>\n" .
5009 "<subtitle>$descr</subtitle>\n" .
5010 '<link rel="alternate" type="text/html" href="' .
5011 $alt_url . '" />' . "\n" .
5012 '<link rel="self" type="' . $content_type . '" href="' .
5013 $cgi->self_url() . '" />' . "\n" .
5014 "<id>" . href(-full=>1) . "</id>\n" .
5015 # use project owner for feed author
5016 "<author><name>$owner</name></author>\n";
5017 if (defined $favicon) {
5018 print "<icon>" . esc_url($favicon) . "</icon>\n";
5019 }
5020 if (defined $logo_url) {
5021 # not twice as wide as tall: 72 x 27 pixels
5022 print "<logo>" . esc_url($logo) . "</logo>\n";
5023 }
5024 if (! %latest_date) {
5025 # dummy date to keep the feed valid until commits trickle in:
5026 print "<updated>1970-01-01T00:00:00Z</updated>\n";
5027 } else {
5028 print "<updated>$latest_date{'iso-8601'}</updated>\n";
5029 }
5030 }
5031
5032 # contents
5033 for (my $i = 0; $i <= $#commitlist; $i++) {
5034 my %co = %{$commitlist[$i]};
5035 my $commit = $co{'id'};
5036 # we read 150, we always show 30 and the ones more recent than 48 hours
5037 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
5038 last;
5039 }
5040 my %cd = parse_date($co{'author_epoch'});
5041
5042 # get list of changed files
5043 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5044 $co{'parent'} || "--root",
5045 $co{'id'}, "--", (defined $file_name ? $file_name : ())
5046 or next;
5047 my @difftree = map { chomp; $_ } <$fd>;
5048 close $fd
5049 or next;
5050
5051 # print element (entry, item)
5052 my $co_url = href(-full=>1, action=>"commit", hash=>$commit);
5053 if ($format eq 'rss') {
5054 print "<item>\n" .
5055 "<title>" . esc_html($co{'title'}) . "</title>\n" .
5056 "<author>" . esc_html($co{'author'}) . "</author>\n" .
5057 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
5058 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
5059 "<link>$co_url</link>\n" .
5060 "<description>" . esc_html($co{'title'}) . "</description>\n" .
5061 "<content:encoded>" .
5062 "<![CDATA[\n";
5063 } elsif ($format eq 'atom') {
5064 print "<entry>\n" .
5065 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
5066 "<updated>$cd{'iso-8601'}</updated>\n" .
5067 "<author>\n" .
5068 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
5069 if ($co{'author_email'}) {
5070 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
5071 }
5072 print "</author>\n" .
5073 # use committer for contributor
5074 "<contributor>\n" .
5075 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
5076 if ($co{'committer_email'}) {
5077 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
5078 }
5079 print "</contributor>\n" .
5080 "<published>$cd{'iso-8601'}</published>\n" .
5081 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
5082 "<id>$co_url</id>\n" .
5083 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
5084 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
5085 }
5086 my $comment = $co{'comment'};
5087 print "<pre>\n";
5088 foreach my $line (@$comment) {
5089 $line = esc_html($line);
5090 print "$line\n";
5091 }
5092 print "</pre><ul>\n";
5093 foreach my $difftree_line (@difftree) {
5094 my %difftree = parse_difftree_raw_line($difftree_line);
5095 next if !$difftree{'from_id'};
5096
5097 my $file = $difftree{'file'} || $difftree{'to_file'};
5098
5099 print "<li>" .
5100 "[" .
5101 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
5102 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
5103 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
5104 file_name=>$file, file_parent=>$difftree{'from_file'}),
5105 -title => "diff"}, 'D');
5106 if ($have_blame) {
5107 print $cgi->a({-href => href(-full=>1, action=>"blame",
5108 file_name=>$file, hash_base=>$commit),
5109 -title => "blame"}, 'B');
5110 }
5111 # if this is not a feed of a file history
5112 if (!defined $file_name || $file_name ne $file) {
5113 print $cgi->a({-href => href(-full=>1, action=>"history",
5114 file_name=>$file, hash=>$commit),
5115 -title => "history"}, 'H');
5116 }
5117 $file = esc_path($file);
5118 print "] ".
5119 "$file</li>\n";
5120 }
5121 if ($format eq 'rss') {
5122 print "</ul>]]>\n" .
5123 "</content:encoded>\n" .
5124 "</item>\n";
5125 } elsif ($format eq 'atom') {
5126 print "</ul>\n</div>\n" .
5127 "</content>\n" .
5128 "</entry>\n";
5129 }
5130 }
5131
5132 # end of feed
5133 if ($format eq 'rss') {
5134 print "</channel>\n</rss>\n";
5135 } elsif ($format eq 'atom') {
5136 print "</feed>\n";
5137 }
5138 }
5139
5140 sub git_rss {
5141 git_feed('rss');
5142 }
5143
5144 sub git_atom {
5145 git_feed('atom');
5146 }
5147
5148 sub git_opml {
5149 my @list = git_get_projects_list();
5150
5151 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
5152 print <<XML;
5153 <?xml version="1.0" encoding="utf-8"?>
5154 <opml version="1.0">
5155 <head>
5156 <title>$site_name OPML Export</title>
5157 </head>
5158 <body>
5159 <outline text="git RSS feeds">
5160 XML
5161
5162 foreach my $pr (@list) {
5163 my %proj = %$pr;
5164 my $head = git_get_head_hash($proj{'path'});
5165 if (!defined $head) {
5166 next;
5167 }
5168 $git_dir = "$projectroot/$proj{'path'}";
5169 my %co = parse_commit($head);
5170 if (!%co) {
5171 next;
5172 }
5173
5174 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
5175 my $rss = "$my_url?p=$proj{'path'};a=rss";
5176 my $html = "$my_url?p=$proj{'path'};a=summary";
5177 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
5178 }
5179 print <<XML;
5180 </outline>
5181 </body>
5182 </opml>
5183 XML
5184 }
This page took 4.696359 seconds and 5 git commands to generate.