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