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