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