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