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