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