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