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