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