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