]> Lady’s Gitweb - Gitweb/blob - gitweb.perl
gitweb: fix another use of undefined value
[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 if (defined $descr) {
1119 chomp $descr;
1120 }
1121 return $descr;
1122 }
1123
1124 sub git_get_project_url_list {
1125 my $path = shift;
1126
1127 open my $fd, "$projectroot/$path/cloneurl" or return;
1128 my @git_project_url_list = map { chomp; $_ } <$fd>;
1129 close $fd;
1130
1131 return wantarray ? @git_project_url_list : \@git_project_url_list;
1132 }
1133
1134 sub git_get_projects_list {
1135 my ($filter) = @_;
1136 my @list;
1137
1138 $filter ||= '';
1139 $filter =~ s/\.git$//;
1140
1141 my ($check_forks) = gitweb_check_feature('forks');
1142
1143 if (-d $projects_list) {
1144 # search in directory
1145 my $dir = $projects_list . ($filter ? "/$filter" : '');
1146 # remove the trailing "/"
1147 $dir =~ s!/+$!!;
1148 my $pfxlen = length("$dir");
1149
1150 File::Find::find({
1151 follow_fast => 1, # follow symbolic links
1152 dangling_symlinks => 0, # ignore dangling symlinks, silently
1153 wanted => sub {
1154 # skip project-list toplevel, if we get it.
1155 return if (m!^[/.]$!);
1156 # only directories can be git repositories
1157 return unless (-d $_);
1158
1159 my $subdir = substr($File::Find::name, $pfxlen + 1);
1160 # we check related file in $projectroot
1161 if ($check_forks and $subdir =~ m#/.#) {
1162 $File::Find::prune = 1;
1163 } elsif (check_export_ok("$projectroot/$filter/$subdir")) {
1164 push @list, { path => ($filter ? "$filter/" : '') . $subdir };
1165 $File::Find::prune = 1;
1166 }
1167 },
1168 }, "$dir");
1169
1170 } elsif (-f $projects_list) {
1171 # read from file(url-encoded):
1172 # 'git%2Fgit.git Linus+Torvalds'
1173 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1174 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1175 my %paths;
1176 open my ($fd), $projects_list or return;
1177 PROJECT:
1178 while (my $line = <$fd>) {
1179 chomp $line;
1180 my ($path, $owner) = split ' ', $line;
1181 $path = unescape($path);
1182 $owner = unescape($owner);
1183 if (!defined $path) {
1184 next;
1185 }
1186 if ($filter ne '') {
1187 # looking for forks;
1188 my $pfx = substr($path, 0, length($filter));
1189 if ($pfx ne $filter) {
1190 next PROJECT;
1191 }
1192 my $sfx = substr($path, length($filter));
1193 if ($sfx !~ /^\/.*\.git$/) {
1194 next PROJECT;
1195 }
1196 } elsif ($check_forks) {
1197 PATH:
1198 foreach my $filter (keys %paths) {
1199 # looking for forks;
1200 my $pfx = substr($path, 0, length($filter));
1201 if ($pfx ne $filter) {
1202 next PATH;
1203 }
1204 my $sfx = substr($path, length($filter));
1205 if ($sfx !~ /^\/.*\.git$/) {
1206 next PATH;
1207 }
1208 # is a fork, don't include it in
1209 # the list
1210 next PROJECT;
1211 }
1212 }
1213 if (check_export_ok("$projectroot/$path")) {
1214 my $pr = {
1215 path => $path,
1216 owner => decode_utf8($owner),
1217 };
1218 push @list, $pr;
1219 (my $forks_path = $path) =~ s/\.git$//;
1220 $paths{$forks_path}++;
1221 }
1222 }
1223 close $fd;
1224 }
1225 return @list;
1226 }
1227
1228 sub git_get_project_owner {
1229 my $project = shift;
1230 my $owner;
1231
1232 return undef unless $project;
1233
1234 # read from file (url-encoded):
1235 # 'git%2Fgit.git Linus+Torvalds'
1236 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1237 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1238 if (-f $projects_list) {
1239 open (my $fd , $projects_list);
1240 while (my $line = <$fd>) {
1241 chomp $line;
1242 my ($pr, $ow) = split ' ', $line;
1243 $pr = unescape($pr);
1244 $ow = unescape($ow);
1245 if ($pr eq $project) {
1246 $owner = decode_utf8($ow);
1247 last;
1248 }
1249 }
1250 close $fd;
1251 }
1252 if (!defined $owner) {
1253 $owner = get_file_owner("$projectroot/$project");
1254 }
1255
1256 return $owner;
1257 }
1258
1259 sub git_get_last_activity {
1260 my ($path) = @_;
1261 my $fd;
1262
1263 $git_dir = "$projectroot/$path";
1264 open($fd, "-|", git_cmd(), 'for-each-ref',
1265 '--format=%(committer)',
1266 '--sort=-committerdate',
1267 '--count=1',
1268 'refs/heads') or return;
1269 my $most_recent = <$fd>;
1270 close $fd or return;
1271 if (defined $most_recent &&
1272 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
1273 my $timestamp = $1;
1274 my $age = time - $timestamp;
1275 return ($age, age_string($age));
1276 }
1277 }
1278
1279 sub git_get_references {
1280 my $type = shift || "";
1281 my %refs;
1282 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
1283 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
1284 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
1285 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
1286 or return;
1287
1288 while (my $line = <$fd>) {
1289 chomp $line;
1290 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type/?[^^]+)!) {
1291 if (defined $refs{$1}) {
1292 push @{$refs{$1}}, $2;
1293 } else {
1294 $refs{$1} = [ $2 ];
1295 }
1296 }
1297 }
1298 close $fd or return;
1299 return \%refs;
1300 }
1301
1302 sub git_get_rev_name_tags {
1303 my $hash = shift || return undef;
1304
1305 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
1306 or return;
1307 my $name_rev = <$fd>;
1308 close $fd;
1309
1310 if ($name_rev =~ m|^$hash tags/(.*)$|) {
1311 return $1;
1312 } else {
1313 # catches also '$hash undefined' output
1314 return undef;
1315 }
1316 }
1317
1318 ## ----------------------------------------------------------------------
1319 ## parse to hash functions
1320
1321 sub parse_date {
1322 my $epoch = shift;
1323 my $tz = shift || "-0000";
1324
1325 my %date;
1326 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
1327 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
1328 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
1329 $date{'hour'} = $hour;
1330 $date{'minute'} = $min;
1331 $date{'mday'} = $mday;
1332 $date{'day'} = $days[$wday];
1333 $date{'month'} = $months[$mon];
1334 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
1335 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
1336 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
1337 $mday, $months[$mon], $hour ,$min;
1338 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
1339 1900+$year, $mon, $mday, $hour ,$min, $sec;
1340
1341 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
1342 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
1343 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
1344 $date{'hour_local'} = $hour;
1345 $date{'minute_local'} = $min;
1346 $date{'tz_local'} = $tz;
1347 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
1348 1900+$year, $mon+1, $mday,
1349 $hour, $min, $sec, $tz);
1350 return %date;
1351 }
1352
1353 sub parse_tag {
1354 my $tag_id = shift;
1355 my %tag;
1356 my @comment;
1357
1358 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
1359 $tag{'id'} = $tag_id;
1360 while (my $line = <$fd>) {
1361 chomp $line;
1362 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
1363 $tag{'object'} = $1;
1364 } elsif ($line =~ m/^type (.+)$/) {
1365 $tag{'type'} = $1;
1366 } elsif ($line =~ m/^tag (.+)$/) {
1367 $tag{'name'} = $1;
1368 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
1369 $tag{'author'} = $1;
1370 $tag{'epoch'} = $2;
1371 $tag{'tz'} = $3;
1372 } elsif ($line =~ m/--BEGIN/) {
1373 push @comment, $line;
1374 last;
1375 } elsif ($line eq "") {
1376 last;
1377 }
1378 }
1379 push @comment, <$fd>;
1380 $tag{'comment'} = \@comment;
1381 close $fd or return;
1382 if (!defined $tag{'name'}) {
1383 return
1384 };
1385 return %tag
1386 }
1387
1388 sub parse_commit_text {
1389 my ($commit_text, $withparents) = @_;
1390 my @commit_lines = split '\n', $commit_text;
1391 my %co;
1392
1393 pop @commit_lines; # Remove '\0'
1394
1395 if (! @commit_lines) {
1396 return;
1397 }
1398
1399 my $header = shift @commit_lines;
1400 if ($header !~ m/^[0-9a-fA-F]{40}/) {
1401 return;
1402 }
1403 ($co{'id'}, my @parents) = split ' ', $header;
1404 while (my $line = shift @commit_lines) {
1405 last if $line eq "\n";
1406 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
1407 $co{'tree'} = $1;
1408 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
1409 push @parents, $1;
1410 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
1411 $co{'author'} = $1;
1412 $co{'author_epoch'} = $2;
1413 $co{'author_tz'} = $3;
1414 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
1415 $co{'author_name'} = $1;
1416 $co{'author_email'} = $2;
1417 } else {
1418 $co{'author_name'} = $co{'author'};
1419 }
1420 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
1421 $co{'committer'} = $1;
1422 $co{'committer_epoch'} = $2;
1423 $co{'committer_tz'} = $3;
1424 $co{'committer_name'} = $co{'committer'};
1425 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
1426 $co{'committer_name'} = $1;
1427 $co{'committer_email'} = $2;
1428 } else {
1429 $co{'committer_name'} = $co{'committer'};
1430 }
1431 }
1432 }
1433 if (!defined $co{'tree'}) {
1434 return;
1435 };
1436 $co{'parents'} = \@parents;
1437 $co{'parent'} = $parents[0];
1438
1439 foreach my $title (@commit_lines) {
1440 $title =~ s/^ //;
1441 if ($title ne "") {
1442 $co{'title'} = chop_str($title, 80, 5);
1443 # remove leading stuff of merges to make the interesting part visible
1444 if (length($title) > 50) {
1445 $title =~ s/^Automatic //;
1446 $title =~ s/^merge (of|with) /Merge ... /i;
1447 if (length($title) > 50) {
1448 $title =~ s/(http|rsync):\/\///;
1449 }
1450 if (length($title) > 50) {
1451 $title =~ s/(master|www|rsync)\.//;
1452 }
1453 if (length($title) > 50) {
1454 $title =~ s/kernel.org:?//;
1455 }
1456 if (length($title) > 50) {
1457 $title =~ s/\/pub\/scm//;
1458 }
1459 }
1460 $co{'title_short'} = chop_str($title, 50, 5);
1461 last;
1462 }
1463 }
1464 if ($co{'title'} eq "") {
1465 $co{'title'} = $co{'title_short'} = '(no commit message)';
1466 }
1467 # remove added spaces
1468 foreach my $line (@commit_lines) {
1469 $line =~ s/^ //;
1470 }
1471 $co{'comment'} = \@commit_lines;
1472
1473 my $age = time - $co{'committer_epoch'};
1474 $co{'age'} = $age;
1475 $co{'age_string'} = age_string($age);
1476 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
1477 if ($age > 60*60*24*7*2) {
1478 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1479 $co{'age_string_age'} = $co{'age_string'};
1480 } else {
1481 $co{'age_string_date'} = $co{'age_string'};
1482 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1483 }
1484 return %co;
1485 }
1486
1487 sub parse_commit {
1488 my ($commit_id) = @_;
1489 my %co;
1490
1491 local $/ = "\0";
1492
1493 open my $fd, "-|", git_cmd(), "rev-list",
1494 "--parents",
1495 "--header",
1496 "--max-count=1",
1497 $commit_id,
1498 "--",
1499 or die_error(undef, "Open git-rev-list failed");
1500 %co = parse_commit_text(<$fd>, 1);
1501 close $fd;
1502
1503 return %co;
1504 }
1505
1506 sub parse_commits {
1507 my ($commit_id, $maxcount, $skip, $arg, $filename) = @_;
1508 my @cos;
1509
1510 $maxcount ||= 1;
1511 $skip ||= 0;
1512
1513 local $/ = "\0";
1514
1515 open my $fd, "-|", git_cmd(), "rev-list",
1516 "--header",
1517 ($arg ? ($arg) : ()),
1518 ("--max-count=" . $maxcount),
1519 ("--skip=" . $skip),
1520 $commit_id,
1521 "--",
1522 ($filename ? ($filename) : ())
1523 or die_error(undef, "Open git-rev-list failed");
1524 while (my $line = <$fd>) {
1525 my %co = parse_commit_text($line);
1526 push @cos, \%co;
1527 }
1528 close $fd;
1529
1530 return wantarray ? @cos : \@cos;
1531 }
1532
1533 # parse ref from ref_file, given by ref_id, with given type
1534 sub parse_ref {
1535 my $ref_file = shift;
1536 my $ref_id = shift;
1537 my $type = shift || git_get_type($ref_id);
1538 my %ref_item;
1539
1540 $ref_item{'type'} = $type;
1541 $ref_item{'id'} = $ref_id;
1542 $ref_item{'epoch'} = 0;
1543 $ref_item{'age'} = "unknown";
1544 if ($type eq "tag") {
1545 my %tag = parse_tag($ref_id);
1546 $ref_item{'comment'} = $tag{'comment'};
1547 if ($tag{'type'} eq "commit") {
1548 my %co = parse_commit($tag{'object'});
1549 $ref_item{'epoch'} = $co{'committer_epoch'};
1550 $ref_item{'age'} = $co{'age_string'};
1551 } elsif (defined($tag{'epoch'})) {
1552 my $age = time - $tag{'epoch'};
1553 $ref_item{'epoch'} = $tag{'epoch'};
1554 $ref_item{'age'} = age_string($age);
1555 }
1556 $ref_item{'reftype'} = $tag{'type'};
1557 $ref_item{'name'} = $tag{'name'};
1558 $ref_item{'refid'} = $tag{'object'};
1559 } elsif ($type eq "commit"){
1560 my %co = parse_commit($ref_id);
1561 $ref_item{'reftype'} = "commit";
1562 $ref_item{'name'} = $ref_file;
1563 $ref_item{'title'} = $co{'title'};
1564 $ref_item{'refid'} = $ref_id;
1565 $ref_item{'epoch'} = $co{'committer_epoch'};
1566 $ref_item{'age'} = $co{'age_string'};
1567 } else {
1568 $ref_item{'reftype'} = $type;
1569 $ref_item{'name'} = $ref_file;
1570 $ref_item{'refid'} = $ref_id;
1571 }
1572
1573 return %ref_item;
1574 }
1575
1576 # parse line of git-diff-tree "raw" output
1577 sub parse_difftree_raw_line {
1578 my $line = shift;
1579 my %res;
1580
1581 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1582 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
1583 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1584 $res{'from_mode'} = $1;
1585 $res{'to_mode'} = $2;
1586 $res{'from_id'} = $3;
1587 $res{'to_id'} = $4;
1588 $res{'status'} = $5;
1589 $res{'similarity'} = $6;
1590 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
1591 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
1592 } else {
1593 $res{'file'} = unquote($7);
1594 }
1595 }
1596 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
1597 # combined diff (for merge commit)
1598 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
1599 $res{'nparents'} = length($1);
1600 $res{'from_mode'} = [ split(' ', $2) ];
1601 $res{'to_mode'} = pop @{$res{'from_mode'}};
1602 $res{'from_id'} = [ split(' ', $3) ];
1603 $res{'to_id'} = pop @{$res{'from_id'}};
1604 $res{'status'} = [ split('', $4) ];
1605 $res{'to_file'} = unquote($5);
1606 }
1607 # 'c512b523472485aef4fff9e57b229d9d243c967f'
1608 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
1609 $res{'commit'} = $1;
1610 }
1611
1612 return wantarray ? %res : \%res;
1613 }
1614
1615 # parse line of git-ls-tree output
1616 sub parse_ls_tree_line ($;%) {
1617 my $line = shift;
1618 my %opts = @_;
1619 my %res;
1620
1621 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1622 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
1623
1624 $res{'mode'} = $1;
1625 $res{'type'} = $2;
1626 $res{'hash'} = $3;
1627 if ($opts{'-z'}) {
1628 $res{'name'} = $4;
1629 } else {
1630 $res{'name'} = unquote($4);
1631 }
1632
1633 return wantarray ? %res : \%res;
1634 }
1635
1636 ## ......................................................................
1637 ## parse to array of hashes functions
1638
1639 sub git_get_heads_list {
1640 my $limit = shift;
1641 my @headslist;
1642
1643 open my $fd, '-|', git_cmd(), 'for-each-ref',
1644 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
1645 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
1646 'refs/heads'
1647 or return;
1648 while (my $line = <$fd>) {
1649 my %ref_item;
1650
1651 chomp $line;
1652 my ($refinfo, $committerinfo) = split(/\0/, $line);
1653 my ($hash, $name, $title) = split(' ', $refinfo, 3);
1654 my ($committer, $epoch, $tz) =
1655 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
1656 $name =~ s!^refs/heads/!!;
1657
1658 $ref_item{'name'} = $name;
1659 $ref_item{'id'} = $hash;
1660 $ref_item{'title'} = $title || '(no commit message)';
1661 $ref_item{'epoch'} = $epoch;
1662 if ($epoch) {
1663 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
1664 } else {
1665 $ref_item{'age'} = "unknown";
1666 }
1667
1668 push @headslist, \%ref_item;
1669 }
1670 close $fd;
1671
1672 return wantarray ? @headslist : \@headslist;
1673 }
1674
1675 sub git_get_tags_list {
1676 my $limit = shift;
1677 my @tagslist;
1678
1679 open my $fd, '-|', git_cmd(), 'for-each-ref',
1680 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
1681 '--format=%(objectname) %(objecttype) %(refname) '.
1682 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
1683 'refs/tags'
1684 or return;
1685 while (my $line = <$fd>) {
1686 my %ref_item;
1687
1688 chomp $line;
1689 my ($refinfo, $creatorinfo) = split(/\0/, $line);
1690 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
1691 my ($creator, $epoch, $tz) =
1692 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
1693 $name =~ s!^refs/tags/!!;
1694
1695 $ref_item{'type'} = $type;
1696 $ref_item{'id'} = $id;
1697 $ref_item{'name'} = $name;
1698 if ($type eq "tag") {
1699 $ref_item{'subject'} = $title;
1700 $ref_item{'reftype'} = $reftype;
1701 $ref_item{'refid'} = $refid;
1702 } else {
1703 $ref_item{'reftype'} = $type;
1704 $ref_item{'refid'} = $id;
1705 }
1706
1707 if ($type eq "tag" || $type eq "commit") {
1708 $ref_item{'epoch'} = $epoch;
1709 if ($epoch) {
1710 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
1711 } else {
1712 $ref_item{'age'} = "unknown";
1713 }
1714 }
1715
1716 push @tagslist, \%ref_item;
1717 }
1718 close $fd;
1719
1720 return wantarray ? @tagslist : \@tagslist;
1721 }
1722
1723 ## ----------------------------------------------------------------------
1724 ## filesystem-related functions
1725
1726 sub get_file_owner {
1727 my $path = shift;
1728
1729 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
1730 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
1731 if (!defined $gcos) {
1732 return undef;
1733 }
1734 my $owner = $gcos;
1735 $owner =~ s/[,;].*$//;
1736 return decode_utf8($owner);
1737 }
1738
1739 ## ......................................................................
1740 ## mimetype related functions
1741
1742 sub mimetype_guess_file {
1743 my $filename = shift;
1744 my $mimemap = shift;
1745 -r $mimemap or return undef;
1746
1747 my %mimemap;
1748 open(MIME, $mimemap) or return undef;
1749 while (<MIME>) {
1750 next if m/^#/; # skip comments
1751 my ($mime, $exts) = split(/\t+/);
1752 if (defined $exts) {
1753 my @exts = split(/\s+/, $exts);
1754 foreach my $ext (@exts) {
1755 $mimemap{$ext} = $mime;
1756 }
1757 }
1758 }
1759 close(MIME);
1760
1761 $filename =~ /\.([^.]*)$/;
1762 return $mimemap{$1};
1763 }
1764
1765 sub mimetype_guess {
1766 my $filename = shift;
1767 my $mime;
1768 $filename =~ /\./ or return undef;
1769
1770 if ($mimetypes_file) {
1771 my $file = $mimetypes_file;
1772 if ($file !~ m!^/!) { # if it is relative path
1773 # it is relative to project
1774 $file = "$projectroot/$project/$file";
1775 }
1776 $mime = mimetype_guess_file($filename, $file);
1777 }
1778 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1779 return $mime;
1780 }
1781
1782 sub blob_mimetype {
1783 my $fd = shift;
1784 my $filename = shift;
1785
1786 if ($filename) {
1787 my $mime = mimetype_guess($filename);
1788 $mime and return $mime;
1789 }
1790
1791 # just in case
1792 return $default_blob_plain_mimetype unless $fd;
1793
1794 if (-T $fd) {
1795 return 'text/plain' .
1796 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1797 } elsif (! $filename) {
1798 return 'application/octet-stream';
1799 } elsif ($filename =~ m/\.png$/i) {
1800 return 'image/png';
1801 } elsif ($filename =~ m/\.gif$/i) {
1802 return 'image/gif';
1803 } elsif ($filename =~ m/\.jpe?g$/i) {
1804 return 'image/jpeg';
1805 } else {
1806 return 'application/octet-stream';
1807 }
1808 }
1809
1810 ## ======================================================================
1811 ## functions printing HTML: header, footer, error page
1812
1813 sub git_header_html {
1814 my $status = shift || "200 OK";
1815 my $expires = shift;
1816
1817 my $title = "$site_name";
1818 if (defined $project) {
1819 $title .= " - " . decode_utf8($project);
1820 if (defined $action) {
1821 $title .= "/$action";
1822 if (defined $file_name) {
1823 $title .= " - " . esc_path($file_name);
1824 if ($action eq "tree" && $file_name !~ m|/$|) {
1825 $title .= "/";
1826 }
1827 }
1828 }
1829 }
1830 my $content_type;
1831 # require explicit support from the UA if we are to send the page as
1832 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
1833 # we have to do this because MSIE sometimes globs '*/*', pretending to
1834 # support xhtml+xml but choking when it gets what it asked for.
1835 if (defined $cgi->http('HTTP_ACCEPT') &&
1836 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
1837 $cgi->Accept('application/xhtml+xml') != 0) {
1838 $content_type = 'application/xhtml+xml';
1839 } else {
1840 $content_type = 'text/html';
1841 }
1842 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
1843 -status=> $status, -expires => $expires);
1844 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
1845 print <<EOF;
1846 <?xml version="1.0" encoding="utf-8"?>
1847 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1848 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
1849 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
1850 <!-- git core binaries version $git_version -->
1851 <head>
1852 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
1853 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
1854 <meta name="robots" content="index, nofollow"/>
1855 <title>$title</title>
1856 EOF
1857 # print out each stylesheet that exist
1858 if (defined $stylesheet) {
1859 #provides backwards capability for those people who define style sheet in a config file
1860 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
1861 } else {
1862 foreach my $stylesheet (@stylesheets) {
1863 next unless $stylesheet;
1864 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
1865 }
1866 }
1867 if (defined $project) {
1868 printf('<link rel="alternate" title="%s log RSS feed" '.
1869 'href="%s" type="application/rss+xml" />'."\n",
1870 esc_param($project), href(action=>"rss"));
1871 printf('<link rel="alternate" title="%s log Atom feed" '.
1872 'href="%s" type="application/atom+xml" />'."\n",
1873 esc_param($project), href(action=>"atom"));
1874 } else {
1875 printf('<link rel="alternate" title="%s projects list" '.
1876 'href="%s" type="text/plain; charset=utf-8"/>'."\n",
1877 $site_name, href(project=>undef, action=>"project_index"));
1878 printf('<link rel="alternate" title="%s projects feeds" '.
1879 'href="%s" type="text/x-opml"/>'."\n",
1880 $site_name, href(project=>undef, action=>"opml"));
1881 }
1882 if (defined $favicon) {
1883 print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n);
1884 }
1885
1886 print "</head>\n" .
1887 "<body>\n";
1888
1889 if (-f $site_header) {
1890 open (my $fd, $site_header);
1891 print <$fd>;
1892 close $fd;
1893 }
1894
1895 print "<div class=\"page_header\">\n" .
1896 $cgi->a({-href => esc_url($logo_url),
1897 -title => $logo_label},
1898 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
1899 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
1900 if (defined $project) {
1901 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
1902 if (defined $action) {
1903 print " / $action";
1904 }
1905 print "\n";
1906 }
1907 my ($have_search) = gitweb_check_feature('search');
1908 if ((defined $project) && ($have_search)) {
1909 if (!defined $searchtext) {
1910 $searchtext = "";
1911 }
1912 my $search_hash;
1913 if (defined $hash_base) {
1914 $search_hash = $hash_base;
1915 } elsif (defined $hash) {
1916 $search_hash = $hash;
1917 } else {
1918 $search_hash = "HEAD";
1919 }
1920 $cgi->param("a", "search");
1921 $cgi->param("h", $search_hash);
1922 $cgi->param("p", $project);
1923 print $cgi->startform(-method => "get", -action => $my_uri) .
1924 "<div class=\"search\">\n" .
1925 $cgi->hidden(-name => "p") . "\n" .
1926 $cgi->hidden(-name => "a") . "\n" .
1927 $cgi->hidden(-name => "h") . "\n" .
1928 $cgi->popup_menu(-name => 'st', -default => 'commit',
1929 -values => ['commit', 'author', 'committer', 'pickaxe']) .
1930 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
1931 " search:\n",
1932 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
1933 "</div>" .
1934 $cgi->end_form() . "\n";
1935 }
1936 print "</div>\n";
1937 }
1938
1939 sub git_footer_html {
1940 print "<div class=\"page_footer\">\n";
1941 if (defined $project) {
1942 my $descr = git_get_project_description($project);
1943 if (defined $descr) {
1944 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
1945 }
1946 print $cgi->a({-href => href(action=>"rss"),
1947 -class => "rss_logo"}, "RSS") . " ";
1948 print $cgi->a({-href => href(action=>"atom"),
1949 -class => "rss_logo"}, "Atom") . "\n";
1950 } else {
1951 print $cgi->a({-href => href(project=>undef, action=>"opml"),
1952 -class => "rss_logo"}, "OPML") . " ";
1953 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
1954 -class => "rss_logo"}, "TXT") . "\n";
1955 }
1956 print "</div>\n" ;
1957
1958 if (-f $site_footer) {
1959 open (my $fd, $site_footer);
1960 print <$fd>;
1961 close $fd;
1962 }
1963
1964 print "</body>\n" .
1965 "</html>";
1966 }
1967
1968 sub die_error {
1969 my $status = shift || "403 Forbidden";
1970 my $error = shift || "Malformed query, file missing or permission denied";
1971
1972 git_header_html($status);
1973 print <<EOF;
1974 <div class="page_body">
1975 <br /><br />
1976 $status - $error
1977 <br />
1978 </div>
1979 EOF
1980 git_footer_html();
1981 exit;
1982 }
1983
1984 ## ----------------------------------------------------------------------
1985 ## functions printing or outputting HTML: navigation
1986
1987 sub git_print_page_nav {
1988 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
1989 $extra = '' if !defined $extra; # pager or formats
1990
1991 my @navs = qw(summary shortlog log commit commitdiff tree);
1992 if ($suppress) {
1993 @navs = grep { $_ ne $suppress } @navs;
1994 }
1995
1996 my %arg = map { $_ => {action=>$_} } @navs;
1997 if (defined $head) {
1998 for (qw(commit commitdiff)) {
1999 $arg{$_}{'hash'} = $head;
2000 }
2001 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
2002 for (qw(shortlog log)) {
2003 $arg{$_}{'hash'} = $head;
2004 }
2005 }
2006 }
2007 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
2008 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
2009
2010 print "<div class=\"page_nav\">\n" .
2011 (join " | ",
2012 map { $_ eq $current ?
2013 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
2014 } @navs);
2015 print "<br/>\n$extra<br/>\n" .
2016 "</div>\n";
2017 }
2018
2019 sub format_paging_nav {
2020 my ($action, $hash, $head, $page, $nrevs) = @_;
2021 my $paging_nav;
2022
2023
2024 if ($hash ne $head || $page) {
2025 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
2026 } else {
2027 $paging_nav .= "HEAD";
2028 }
2029
2030 if ($page > 0) {
2031 $paging_nav .= " &sdot; " .
2032 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
2033 -accesskey => "p", -title => "Alt-p"}, "prev");
2034 } else {
2035 $paging_nav .= " &sdot; prev";
2036 }
2037
2038 if ($nrevs >= (100 * ($page+1)-1)) {
2039 $paging_nav .= " &sdot; " .
2040 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
2041 -accesskey => "n", -title => "Alt-n"}, "next");
2042 } else {
2043 $paging_nav .= " &sdot; next";
2044 }
2045
2046 return $paging_nav;
2047 }
2048
2049 ## ......................................................................
2050 ## functions printing or outputting HTML: div
2051
2052 sub git_print_header_div {
2053 my ($action, $title, $hash, $hash_base) = @_;
2054 my %args = ();
2055
2056 $args{'action'} = $action;
2057 $args{'hash'} = $hash if $hash;
2058 $args{'hash_base'} = $hash_base if $hash_base;
2059
2060 print "<div class=\"header\">\n" .
2061 $cgi->a({-href => href(%args), -class => "title"},
2062 $title ? $title : $action) .
2063 "\n</div>\n";
2064 }
2065
2066 #sub git_print_authorship (\%) {
2067 sub git_print_authorship {
2068 my $co = shift;
2069
2070 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
2071 print "<div class=\"author_date\">" .
2072 esc_html($co->{'author_name'}) .
2073 " [$ad{'rfc2822'}";
2074 if ($ad{'hour_local'} < 6) {
2075 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2076 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2077 } else {
2078 printf(" (%02d:%02d %s)",
2079 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2080 }
2081 print "]</div>\n";
2082 }
2083
2084 sub git_print_page_path {
2085 my $name = shift;
2086 my $type = shift;
2087 my $hb = shift;
2088
2089
2090 print "<div class=\"page_path\">";
2091 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
2092 -title => 'tree root'}, decode_utf8("[$project]"));
2093 print " / ";
2094 if (defined $name) {
2095 my @dirname = split '/', $name;
2096 my $basename = pop @dirname;
2097 my $fullname = '';
2098
2099 foreach my $dir (@dirname) {
2100 $fullname .= ($fullname ? '/' : '') . $dir;
2101 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
2102 hash_base=>$hb),
2103 -title => $fullname}, esc_path($dir));
2104 print " / ";
2105 }
2106 if (defined $type && $type eq 'blob') {
2107 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
2108 hash_base=>$hb),
2109 -title => $name}, esc_path($basename));
2110 } elsif (defined $type && $type eq 'tree') {
2111 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
2112 hash_base=>$hb),
2113 -title => $name}, esc_path($basename));
2114 print " / ";
2115 } else {
2116 print esc_path($basename);
2117 }
2118 }
2119 print "<br/></div>\n";
2120 }
2121
2122 # sub git_print_log (\@;%) {
2123 sub git_print_log ($;%) {
2124 my $log = shift;
2125 my %opts = @_;
2126
2127 if ($opts{'-remove_title'}) {
2128 # remove title, i.e. first line of log
2129 shift @$log;
2130 }
2131 # remove leading empty lines
2132 while (defined $log->[0] && $log->[0] eq "") {
2133 shift @$log;
2134 }
2135
2136 # print log
2137 my $signoff = 0;
2138 my $empty = 0;
2139 foreach my $line (@$log) {
2140 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
2141 $signoff = 1;
2142 $empty = 0;
2143 if (! $opts{'-remove_signoff'}) {
2144 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
2145 next;
2146 } else {
2147 # remove signoff lines
2148 next;
2149 }
2150 } else {
2151 $signoff = 0;
2152 }
2153
2154 # print only one empty line
2155 # do not print empty line after signoff
2156 if ($line eq "") {
2157 next if ($empty || $signoff);
2158 $empty = 1;
2159 } else {
2160 $empty = 0;
2161 }
2162
2163 print format_log_line_html($line) . "<br/>\n";
2164 }
2165
2166 if ($opts{'-final_empty_line'}) {
2167 # end with single empty line
2168 print "<br/>\n" unless $empty;
2169 }
2170 }
2171
2172 # return link target (what link points to)
2173 sub git_get_link_target {
2174 my $hash = shift;
2175 my $link_target;
2176
2177 # read link
2178 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2179 or return;
2180 {
2181 local $/;
2182 $link_target = <$fd>;
2183 }
2184 close $fd
2185 or return;
2186
2187 return $link_target;
2188 }
2189
2190 # given link target, and the directory (basedir) the link is in,
2191 # return target of link relative to top directory (top tree);
2192 # return undef if it is not possible (including absolute links).
2193 sub normalize_link_target {
2194 my ($link_target, $basedir, $hash_base) = @_;
2195
2196 # we can normalize symlink target only if $hash_base is provided
2197 return unless $hash_base;
2198
2199 # absolute symlinks (beginning with '/') cannot be normalized
2200 return if (substr($link_target, 0, 1) eq '/');
2201
2202 # normalize link target to path from top (root) tree (dir)
2203 my $path;
2204 if ($basedir) {
2205 $path = $basedir . '/' . $link_target;
2206 } else {
2207 # we are in top (root) tree (dir)
2208 $path = $link_target;
2209 }
2210
2211 # remove //, /./, and /../
2212 my @path_parts;
2213 foreach my $part (split('/', $path)) {
2214 # discard '.' and ''
2215 next if (!$part || $part eq '.');
2216 # handle '..'
2217 if ($part eq '..') {
2218 if (@path_parts) {
2219 pop @path_parts;
2220 } else {
2221 # link leads outside repository (outside top dir)
2222 return;
2223 }
2224 } else {
2225 push @path_parts, $part;
2226 }
2227 }
2228 $path = join('/', @path_parts);
2229
2230 return $path;
2231 }
2232
2233 # print tree entry (row of git_tree), but without encompassing <tr> element
2234 sub git_print_tree_entry {
2235 my ($t, $basedir, $hash_base, $have_blame) = @_;
2236
2237 my %base_key = ();
2238 $base_key{'hash_base'} = $hash_base if defined $hash_base;
2239
2240 # The format of a table row is: mode list link. Where mode is
2241 # the mode of the entry, list is the name of the entry, an href,
2242 # and link is the action links of the entry.
2243
2244 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
2245 if ($t->{'type'} eq "blob") {
2246 print "<td class=\"list\">" .
2247 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
2248 file_name=>"$basedir$t->{'name'}", %base_key),
2249 -class => "list"}, esc_path($t->{'name'}));
2250 if (S_ISLNK(oct $t->{'mode'})) {
2251 my $link_target = git_get_link_target($t->{'hash'});
2252 if ($link_target) {
2253 my $norm_target = normalize_link_target($link_target, $basedir, $hash_base);
2254 if (defined $norm_target) {
2255 print " -> " .
2256 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
2257 file_name=>$norm_target),
2258 -title => $norm_target}, esc_path($link_target));
2259 } else {
2260 print " -> " . esc_path($link_target);
2261 }
2262 }
2263 }
2264 print "</td>\n";
2265 print "<td class=\"link\">";
2266 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
2267 file_name=>"$basedir$t->{'name'}", %base_key)},
2268 "blob");
2269 if ($have_blame) {
2270 print " | " .
2271 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
2272 file_name=>"$basedir$t->{'name'}", %base_key)},
2273 "blame");
2274 }
2275 if (defined $hash_base) {
2276 print " | " .
2277 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2278 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
2279 "history");
2280 }
2281 print " | " .
2282 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
2283 file_name=>"$basedir$t->{'name'}")},
2284 "raw");
2285 print "</td>\n";
2286
2287 } elsif ($t->{'type'} eq "tree") {
2288 print "<td class=\"list\">";
2289 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
2290 file_name=>"$basedir$t->{'name'}", %base_key)},
2291 esc_path($t->{'name'}));
2292 print "</td>\n";
2293 print "<td class=\"link\">";
2294 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
2295 file_name=>"$basedir$t->{'name'}", %base_key)},
2296 "tree");
2297 if (defined $hash_base) {
2298 print " | " .
2299 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2300 file_name=>"$basedir$t->{'name'}")},
2301 "history");
2302 }
2303 print "</td>\n";
2304 }
2305 }
2306
2307 ## ......................................................................
2308 ## functions printing large fragments of HTML
2309
2310 sub fill_from_file_info {
2311 my ($diff, @parents) = @_;
2312
2313 $diff->{'from_file'} = [ ];
2314 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
2315 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
2316 if ($diff->{'status'}[$i] eq 'R' ||
2317 $diff->{'status'}[$i] eq 'C') {
2318 $diff->{'from_file'}[$i] =
2319 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
2320 }
2321 }
2322
2323 return $diff;
2324 }
2325
2326 # parameters can be strings, or references to arrays of strings
2327 sub from_ids_eq {
2328 my ($a, $b) = @_;
2329
2330 if (ref($a) eq "ARRAY" && ref($b) eq "ARRAY" && @$a == @$b) {
2331 for (my $i = 0; $i < @$a; ++$i) {
2332 return 0 unless ($a->[$i] eq $b->[$i]);
2333 }
2334 return 1;
2335 } elsif (!ref($a) && !ref($b)) {
2336 return $a eq $b;
2337 } else {
2338 return 0;
2339 }
2340 }
2341
2342
2343 sub git_difftree_body {
2344 my ($difftree, $hash, @parents) = @_;
2345 my ($parent) = $parents[0];
2346 my ($have_blame) = gitweb_check_feature('blame');
2347 print "<div class=\"list_head\">\n";
2348 if ($#{$difftree} > 10) {
2349 print(($#{$difftree} + 1) . " files changed:\n");
2350 }
2351 print "</div>\n";
2352
2353 print "<table class=\"" .
2354 (@parents > 1 ? "combined " : "") .
2355 "diff_tree\">\n";
2356 my $alternate = 1;
2357 my $patchno = 0;
2358 foreach my $line (@{$difftree}) {
2359 my $diff;
2360 if (ref($line) eq "HASH") {
2361 # pre-parsed (or generated by hand)
2362 $diff = $line;
2363 } else {
2364 $diff = parse_difftree_raw_line($line);
2365 }
2366
2367 if ($alternate) {
2368 print "<tr class=\"dark\">\n";
2369 } else {
2370 print "<tr class=\"light\">\n";
2371 }
2372 $alternate ^= 1;
2373
2374 if (exists $diff->{'nparents'}) { # combined diff
2375
2376 fill_from_file_info($diff, @parents)
2377 unless exists $diff->{'from_file'};
2378
2379 if ($diff->{'to_id'} ne ('0' x 40)) {
2380 # file exists in the result (child) commit
2381 print "<td>" .
2382 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2383 file_name=>$diff->{'to_file'},
2384 hash_base=>$hash),
2385 -class => "list"}, esc_path($diff->{'to_file'})) .
2386 "</td>\n";
2387 } else {
2388 print "<td>" .
2389 esc_path($diff->{'to_file'}) .
2390 "</td>\n";
2391 }
2392
2393 if ($action eq 'commitdiff') {
2394 # link to patch
2395 $patchno++;
2396 print "<td class=\"link\">" .
2397 $cgi->a({-href => "#patch$patchno"}, "patch") .
2398 " | " .
2399 "</td>\n";
2400 }
2401
2402 my $has_history = 0;
2403 my $not_deleted = 0;
2404 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
2405 my $hash_parent = $parents[$i];
2406 my $from_hash = $diff->{'from_id'}[$i];
2407 my $from_path = $diff->{'from_file'}[$i];
2408 my $status = $diff->{'status'}[$i];
2409
2410 $has_history ||= ($status ne 'A');
2411 $not_deleted ||= ($status ne 'D');
2412
2413 if ($status eq 'A') {
2414 print "<td class=\"link\" align=\"right\"> | </td>\n";
2415 } elsif ($status eq 'D') {
2416 print "<td class=\"link\">" .
2417 $cgi->a({-href => href(action=>"blob",
2418 hash_base=>$hash,
2419 hash=>$from_hash,
2420 file_name=>$from_path)},
2421 "blob" . ($i+1)) .
2422 " | </td>\n";
2423 } else {
2424 if ($diff->{'to_id'} eq $from_hash) {
2425 print "<td class=\"link nochange\">";
2426 } else {
2427 print "<td class=\"link\">";
2428 }
2429 print $cgi->a({-href => href(action=>"blobdiff",
2430 hash=>$diff->{'to_id'},
2431 hash_parent=>$from_hash,
2432 hash_base=>$hash,
2433 hash_parent_base=>$hash_parent,
2434 file_name=>$diff->{'to_file'},
2435 file_parent=>$from_path)},
2436 "diff" . ($i+1)) .
2437 " | </td>\n";
2438 }
2439 }
2440
2441 print "<td class=\"link\">";
2442 if ($not_deleted) {
2443 print $cgi->a({-href => href(action=>"blob",
2444 hash=>$diff->{'to_id'},
2445 file_name=>$diff->{'to_file'},
2446 hash_base=>$hash)},
2447 "blob");
2448 print " | " if ($has_history);
2449 }
2450 if ($has_history) {
2451 print $cgi->a({-href => href(action=>"history",
2452 file_name=>$diff->{'to_file'},
2453 hash_base=>$hash)},
2454 "history");
2455 }
2456 print "</td>\n";
2457
2458 print "</tr>\n";
2459 next; # instead of 'else' clause, to avoid extra indent
2460 }
2461 # else ordinary diff
2462
2463 my ($to_mode_oct, $to_mode_str, $to_file_type);
2464 my ($from_mode_oct, $from_mode_str, $from_file_type);
2465 if ($diff->{'to_mode'} ne ('0' x 6)) {
2466 $to_mode_oct = oct $diff->{'to_mode'};
2467 if (S_ISREG($to_mode_oct)) { # only for regular file
2468 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
2469 }
2470 $to_file_type = file_type($diff->{'to_mode'});
2471 }
2472 if ($diff->{'from_mode'} ne ('0' x 6)) {
2473 $from_mode_oct = oct $diff->{'from_mode'};
2474 if (S_ISREG($to_mode_oct)) { # only for regular file
2475 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
2476 }
2477 $from_file_type = file_type($diff->{'from_mode'});
2478 }
2479
2480 if ($diff->{'status'} eq "A") { # created
2481 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
2482 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
2483 $mode_chng .= "]</span>";
2484 print "<td>";
2485 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2486 hash_base=>$hash, file_name=>$diff->{'file'}),
2487 -class => "list"}, esc_path($diff->{'file'}));
2488 print "</td>\n";
2489 print "<td>$mode_chng</td>\n";
2490 print "<td class=\"link\">";
2491 if ($action eq 'commitdiff') {
2492 # link to patch
2493 $patchno++;
2494 print $cgi->a({-href => "#patch$patchno"}, "patch");
2495 print " | ";
2496 }
2497 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2498 hash_base=>$hash, file_name=>$diff->{'file'})},
2499 "blob");
2500 print "</td>\n";
2501
2502 } elsif ($diff->{'status'} eq "D") { # deleted
2503 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
2504 print "<td>";
2505 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
2506 hash_base=>$parent, file_name=>$diff->{'file'}),
2507 -class => "list"}, esc_path($diff->{'file'}));
2508 print "</td>\n";
2509 print "<td>$mode_chng</td>\n";
2510 print "<td class=\"link\">";
2511 if ($action eq 'commitdiff') {
2512 # link to patch
2513 $patchno++;
2514 print $cgi->a({-href => "#patch$patchno"}, "patch");
2515 print " | ";
2516 }
2517 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
2518 hash_base=>$parent, file_name=>$diff->{'file'})},
2519 "blob") . " | ";
2520 if ($have_blame) {
2521 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
2522 file_name=>$diff->{'file'})},
2523 "blame") . " | ";
2524 }
2525 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
2526 file_name=>$diff->{'file'})},
2527 "history");
2528 print "</td>\n";
2529
2530 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
2531 my $mode_chnge = "";
2532 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
2533 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
2534 if ($from_file_type ne $to_file_type) {
2535 $mode_chnge .= " from $from_file_type to $to_file_type";
2536 }
2537 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
2538 if ($from_mode_str && $to_mode_str) {
2539 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
2540 } elsif ($to_mode_str) {
2541 $mode_chnge .= " mode: $to_mode_str";
2542 }
2543 }
2544 $mode_chnge .= "]</span>\n";
2545 }
2546 print "<td>";
2547 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2548 hash_base=>$hash, file_name=>$diff->{'file'}),
2549 -class => "list"}, esc_path($diff->{'file'}));
2550 print "</td>\n";
2551 print "<td>$mode_chnge</td>\n";
2552 print "<td class=\"link\">";
2553 if ($action eq 'commitdiff') {
2554 # link to patch
2555 $patchno++;
2556 print $cgi->a({-href => "#patch$patchno"}, "patch") .
2557 " | ";
2558 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
2559 # "commit" view and modified file (not onlu mode changed)
2560 print $cgi->a({-href => href(action=>"blobdiff",
2561 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
2562 hash_base=>$hash, hash_parent_base=>$parent,
2563 file_name=>$diff->{'file'})},
2564 "diff") .
2565 " | ";
2566 }
2567 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2568 hash_base=>$hash, file_name=>$diff->{'file'})},
2569 "blob") . " | ";
2570 if ($have_blame) {
2571 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
2572 file_name=>$diff->{'file'})},
2573 "blame") . " | ";
2574 }
2575 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
2576 file_name=>$diff->{'file'})},
2577 "history");
2578 print "</td>\n";
2579
2580 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
2581 my %status_name = ('R' => 'moved', 'C' => 'copied');
2582 my $nstatus = $status_name{$diff->{'status'}};
2583 my $mode_chng = "";
2584 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
2585 # mode also for directories, so we cannot use $to_mode_str
2586 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
2587 }
2588 print "<td>" .
2589 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2590 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
2591 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
2592 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
2593 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
2594 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
2595 -class => "list"}, esc_path($diff->{'from_file'})) .
2596 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
2597 "<td class=\"link\">";
2598 if ($action eq 'commitdiff') {
2599 # link to patch
2600 $patchno++;
2601 print $cgi->a({-href => "#patch$patchno"}, "patch") .
2602 " | ";
2603 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
2604 # "commit" view and modified file (not only pure rename or copy)
2605 print $cgi->a({-href => href(action=>"blobdiff",
2606 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
2607 hash_base=>$hash, hash_parent_base=>$parent,
2608 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
2609 "diff") .
2610 " | ";
2611 }
2612 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2613 hash_base=>$parent, file_name=>$diff->{'to_file'})},
2614 "blob") . " | ";
2615 if ($have_blame) {
2616 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
2617 file_name=>$diff->{'to_file'})},
2618 "blame") . " | ";
2619 }
2620 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
2621 file_name=>$diff->{'to_file'})},
2622 "history");
2623 print "</td>\n";
2624
2625 } # we should not encounter Unmerged (U) or Unknown (X) status
2626 print "</tr>\n";
2627 }
2628 print "</table>\n";
2629 }
2630
2631 sub git_patchset_body {
2632 my ($fd, $difftree, $hash, @hash_parents) = @_;
2633 my ($hash_parent) = $hash_parents[0];
2634
2635 my $patch_idx = 0;
2636 my $patch_number = 0;
2637 my $patch_line;
2638 my $diffinfo;
2639 my (%from, %to);
2640
2641 print "<div class=\"patchset\">\n";
2642
2643 # skip to first patch
2644 while ($patch_line = <$fd>) {
2645 chomp $patch_line;
2646
2647 last if ($patch_line =~ m/^diff /);
2648 }
2649
2650 PATCH:
2651 while ($patch_line) {
2652 my @diff_header;
2653 my ($from_id, $to_id);
2654
2655 # git diff header
2656 #assert($patch_line =~ m/^diff /) if DEBUG;
2657 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
2658 $patch_number++;
2659 push @diff_header, $patch_line;
2660
2661 # extended diff header
2662 EXTENDED_HEADER:
2663 while ($patch_line = <$fd>) {
2664 chomp $patch_line;
2665
2666 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
2667
2668 if ($patch_line =~ m/^index ([0-9a-fA-F]{40})..([0-9a-fA-F]{40})/) {
2669 $from_id = $1;
2670 $to_id = $2;
2671 } elsif ($patch_line =~ m/^index ((?:[0-9a-fA-F]{40},)+[0-9a-fA-F]{40})..([0-9a-fA-F]{40})/) {
2672 $from_id = [ split(',', $1) ];
2673 $to_id = $2;
2674 }
2675
2676 push @diff_header, $patch_line;
2677 }
2678 my $last_patch_line = $patch_line;
2679
2680 # check if current patch belong to current raw line
2681 # and parse raw git-diff line if needed
2682 if (defined $diffinfo &&
2683 defined $from_id && defined $to_id &&
2684 from_ids_eq($diffinfo->{'from_id'}, $from_id) &&
2685 $diffinfo->{'to_id'} eq $to_id) {
2686 # this is continuation of a split patch
2687 print "<div class=\"patch cont\">\n";
2688 } else {
2689 # advance raw git-diff output if needed
2690 $patch_idx++ if defined $diffinfo;
2691
2692 # read and prepare patch information
2693 if (ref($difftree->[$patch_idx]) eq "HASH") {
2694 # pre-parsed (or generated by hand)
2695 $diffinfo = $difftree->[$patch_idx];
2696 } else {
2697 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
2698 }
2699 if ($diffinfo->{'nparents'}) {
2700 # combined diff
2701 $from{'file'} = [];
2702 $from{'href'} = [];
2703 fill_from_file_info($diffinfo, @hash_parents)
2704 unless exists $diffinfo->{'from_file'};
2705 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2706 $from{'file'}[$i] = $diffinfo->{'from_file'}[$i] || $diffinfo->{'to_file'};
2707 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
2708 $from{'href'}[$i] = href(action=>"blob",
2709 hash_base=>$hash_parents[$i],
2710 hash=>$diffinfo->{'from_id'}[$i],
2711 file_name=>$from{'file'}[$i]);
2712 } else {
2713 $from{'href'}[$i] = undef;
2714 }
2715 }
2716 } else {
2717 $from{'file'} = $diffinfo->{'from_file'} || $diffinfo->{'file'};
2718 if ($diffinfo->{'status'} ne "A") { # not new (added) file
2719 $from{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2720 hash=>$diffinfo->{'from_id'},
2721 file_name=>$from{'file'});
2722 } else {
2723 delete $from{'href'};
2724 }
2725 }
2726 $to{'file'} = $diffinfo->{'to_file'} || $diffinfo->{'file'};
2727 if ($diffinfo->{'status'} ne "D") { # not deleted file
2728 $to{'href'} = href(action=>"blob", hash_base=>$hash,
2729 hash=>$diffinfo->{'to_id'},
2730 file_name=>$to{'file'});
2731 } else {
2732 delete $to{'href'};
2733 }
2734 # this is first patch for raw difftree line with $patch_idx index
2735 # we index @$difftree array from 0, but number patches from 1
2736 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
2737 }
2738
2739 # print "git diff" header
2740 $patch_line = shift @diff_header;
2741 if ($diffinfo->{'nparents'}) {
2742
2743 # combined diff
2744 $patch_line =~ s!^(diff (.*?) )"?.*$!$1!;
2745 if ($to{'href'}) {
2746 $patch_line .= $cgi->a({-href => $to{'href'}, -class => "path"},
2747 esc_path($to{'file'}));
2748 } else { # file was deleted
2749 $patch_line .= esc_path($to{'file'});
2750 }
2751
2752 } else {
2753
2754 $patch_line =~ s!^(diff (.*?) )"?a/.*$!$1!;
2755 if ($from{'href'}) {
2756 $patch_line .= $cgi->a({-href => $from{'href'}, -class => "path"},
2757 'a/' . esc_path($from{'file'}));
2758 } else { # file was added
2759 $patch_line .= 'a/' . esc_path($from{'file'});
2760 }
2761 $patch_line .= ' ';
2762 if ($to{'href'}) {
2763 $patch_line .= $cgi->a({-href => $to{'href'}, -class => "path"},
2764 'b/' . esc_path($to{'file'}));
2765 } else { # file was deleted
2766 $patch_line .= 'b/' . esc_path($to{'file'});
2767 }
2768
2769 }
2770 print "<div class=\"diff header\">$patch_line</div>\n";
2771
2772 # print extended diff header
2773 print "<div class=\"diff extended_header\">\n" if (@diff_header > 0);
2774 EXTENDED_HEADER:
2775 foreach $patch_line (@diff_header) {
2776 # match <path>
2777 if ($patch_line =~ s!^((copy|rename) from ).*$!$1! && $from{'href'}) {
2778 $patch_line .= $cgi->a({-href=>$from{'href'}, -class=>"path"},
2779 esc_path($from{'file'}));
2780 }
2781 if ($patch_line =~ s!^((copy|rename) to ).*$!$1! && $to{'href'}) {
2782 $patch_line .= $cgi->a({-href=>$to{'href'}, -class=>"path"},
2783 esc_path($to{'file'}));
2784 }
2785 # match single <mode>
2786 if ($patch_line =~ m/\s(\d{6})$/) {
2787 $patch_line .= '<span class="info"> (' .
2788 file_type_long($1) .
2789 ')</span>';
2790 }
2791 # match <hash>
2792 if ($patch_line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
2793 # can match only for combined diff
2794 $patch_line = 'index ';
2795 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2796 if ($from{'href'}[$i]) {
2797 $patch_line .= $cgi->a({-href=>$from{'href'}[$i],
2798 -class=>"hash"},
2799 substr($diffinfo->{'from_id'}[$i],0,7));
2800 } else {
2801 $patch_line .= '0' x 7;
2802 }
2803 # separator
2804 $patch_line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
2805 }
2806 $patch_line .= '..';
2807 if ($to{'href'}) {
2808 $patch_line .= $cgi->a({-href=>$to{'href'}, -class=>"hash"},
2809 substr($diffinfo->{'to_id'},0,7));
2810 } else {
2811 $patch_line .= '0' x 7;
2812 }
2813
2814 } elsif ($patch_line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
2815 # can match only for ordinary diff
2816 my ($from_link, $to_link);
2817 if ($from{'href'}) {
2818 $from_link = $cgi->a({-href=>$from{'href'}, -class=>"hash"},
2819 substr($diffinfo->{'from_id'},0,7));
2820 } else {
2821 $from_link = '0' x 7;
2822 }
2823 if ($to{'href'}) {
2824 $to_link = $cgi->a({-href=>$to{'href'}, -class=>"hash"},
2825 substr($diffinfo->{'to_id'},0,7));
2826 } else {
2827 $to_link = '0' x 7;
2828 }
2829 #affirm {
2830 # my ($from_hash, $to_hash) =
2831 # ($patch_line =~ m/^index ([0-9a-fA-F]{40})..([0-9a-fA-F]{40})/);
2832 # my ($from_id, $to_id) =
2833 # ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
2834 # ($from_hash eq $from_id) && ($to_hash eq $to_id);
2835 #} if DEBUG;
2836 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
2837 $patch_line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
2838 }
2839 print $patch_line . "<br/>\n";
2840 }
2841 print "</div>\n" if (@diff_header > 0); # class="diff extended_header"
2842
2843 # from-file/to-file diff header
2844 $patch_line = $last_patch_line;
2845 if (! $patch_line) {
2846 print "</div>\n"; # class="patch"
2847 last PATCH;
2848 }
2849 next PATCH if ($patch_line =~ m/^diff /);
2850 #assert($patch_line =~ m/^---/) if DEBUG;
2851 if (!$diffinfo->{'nparents'} && # not from-file line for combined diff
2852 $from{'href'} && $patch_line =~ m!^--- "?a/!) {
2853 $patch_line = '--- a/' .
2854 $cgi->a({-href=>$from{'href'}, -class=>"path"},
2855 esc_path($from{'file'}));
2856 }
2857 print "<div class=\"diff from_file\">$patch_line</div>\n";
2858
2859 $patch_line = <$fd>;
2860 chomp $patch_line;
2861
2862 #assert($patch_line =~ m/^+++/) if DEBUG;
2863 if ($to{'href'} && $patch_line =~ m!^\+\+\+ "?b/!) {
2864 $patch_line = '+++ b/' .
2865 $cgi->a({-href=>$to{'href'}, -class=>"path"},
2866 esc_path($to{'file'}));
2867 }
2868 print "<div class=\"diff to_file\">$patch_line</div>\n";
2869
2870 # the patch itself
2871 LINE:
2872 while ($patch_line = <$fd>) {
2873 chomp $patch_line;
2874
2875 next PATCH if ($patch_line =~ m/^diff /);
2876
2877 print format_diff_line($patch_line, \%from, \%to);
2878 }
2879
2880 } continue {
2881 print "</div>\n"; # class="patch"
2882 }
2883
2884 if ($patch_number == 0) {
2885 if (@hash_parents > 1) {
2886 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
2887 } else {
2888 print "<div class=\"diff nodifferences\">No differences found</div>\n";
2889 }
2890 }
2891
2892 print "</div>\n"; # class="patchset"
2893 }
2894
2895 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2896
2897 sub git_project_list_body {
2898 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
2899
2900 my ($check_forks) = gitweb_check_feature('forks');
2901
2902 my @projects;
2903 foreach my $pr (@$projlist) {
2904 my (@aa) = git_get_last_activity($pr->{'path'});
2905 unless (@aa) {
2906 next;
2907 }
2908 ($pr->{'age'}, $pr->{'age_string'}) = @aa;
2909 if (!defined $pr->{'descr'}) {
2910 my $descr = git_get_project_description($pr->{'path'}) || "";
2911 $pr->{'descr_long'} = decode_utf8($descr);
2912 $pr->{'descr'} = chop_str($descr, 25, 5);
2913 }
2914 if (!defined $pr->{'owner'}) {
2915 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
2916 }
2917 if ($check_forks) {
2918 my $pname = $pr->{'path'};
2919 if (($pname =~ s/\.git$//) &&
2920 ($pname !~ /\/$/) &&
2921 (-d "$projectroot/$pname")) {
2922 $pr->{'forks'} = "-d $projectroot/$pname";
2923 }
2924 else {
2925 $pr->{'forks'} = 0;
2926 }
2927 }
2928 push @projects, $pr;
2929 }
2930
2931 $order ||= $default_projects_order;
2932 $from = 0 unless defined $from;
2933 $to = $#projects if (!defined $to || $#projects < $to);
2934
2935 print "<table class=\"project_list\">\n";
2936 unless ($no_header) {
2937 print "<tr>\n";
2938 if ($check_forks) {
2939 print "<th></th>\n";
2940 }
2941 if ($order eq "project") {
2942 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
2943 print "<th>Project</th>\n";
2944 } else {
2945 print "<th>" .
2946 $cgi->a({-href => href(project=>undef, order=>'project'),
2947 -class => "header"}, "Project") .
2948 "</th>\n";
2949 }
2950 if ($order eq "descr") {
2951 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
2952 print "<th>Description</th>\n";
2953 } else {
2954 print "<th>" .
2955 $cgi->a({-href => href(project=>undef, order=>'descr'),
2956 -class => "header"}, "Description") .
2957 "</th>\n";
2958 }
2959 if ($order eq "owner") {
2960 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
2961 print "<th>Owner</th>\n";
2962 } else {
2963 print "<th>" .
2964 $cgi->a({-href => href(project=>undef, order=>'owner'),
2965 -class => "header"}, "Owner") .
2966 "</th>\n";
2967 }
2968 if ($order eq "age") {
2969 @projects = sort {$a->{'age'} <=> $b->{'age'}} @projects;
2970 print "<th>Last Change</th>\n";
2971 } else {
2972 print "<th>" .
2973 $cgi->a({-href => href(project=>undef, order=>'age'),
2974 -class => "header"}, "Last Change") .
2975 "</th>\n";
2976 }
2977 print "<th></th>\n" .
2978 "</tr>\n";
2979 }
2980 my $alternate = 1;
2981 for (my $i = $from; $i <= $to; $i++) {
2982 my $pr = $projects[$i];
2983 if ($alternate) {
2984 print "<tr class=\"dark\">\n";
2985 } else {
2986 print "<tr class=\"light\">\n";
2987 }
2988 $alternate ^= 1;
2989 if ($check_forks) {
2990 print "<td>";
2991 if ($pr->{'forks'}) {
2992 print "<!-- $pr->{'forks'} -->\n";
2993 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
2994 }
2995 print "</td>\n";
2996 }
2997 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2998 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
2999 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3000 -class => "list", -title => $pr->{'descr_long'}},
3001 esc_html($pr->{'descr'})) . "</td>\n" .
3002 "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
3003 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
3004 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
3005 "<td class=\"link\">" .
3006 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
3007 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
3008 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
3009 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
3010 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
3011 "</td>\n" .
3012 "</tr>\n";
3013 }
3014 if (defined $extra) {
3015 print "<tr>\n";
3016 if ($check_forks) {
3017 print "<td></td>\n";
3018 }
3019 print "<td colspan=\"5\">$extra</td>\n" .
3020 "</tr>\n";
3021 }
3022 print "</table>\n";
3023 }
3024
3025 sub git_shortlog_body {
3026 # uses global variable $project
3027 my ($commitlist, $from, $to, $refs, $extra) = @_;
3028
3029 my $have_snapshot = gitweb_have_snapshot();
3030
3031 $from = 0 unless defined $from;
3032 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3033
3034 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
3035 my $alternate = 1;
3036 for (my $i = $from; $i <= $to; $i++) {
3037 my %co = %{$commitlist->[$i]};
3038 my $commit = $co{'id'};
3039 my $ref = format_ref_marker($refs, $commit);
3040 if ($alternate) {
3041 print "<tr class=\"dark\">\n";
3042 } else {
3043 print "<tr class=\"light\">\n";
3044 }
3045 $alternate ^= 1;
3046 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
3047 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3048 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
3049 "<td>";
3050 print format_subject_html($co{'title'}, $co{'title_short'},
3051 href(action=>"commit", hash=>$commit), $ref);
3052 print "</td>\n" .
3053 "<td class=\"link\">" .
3054 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
3055 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
3056 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
3057 if ($have_snapshot) {
3058 print " | " . $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
3059 }
3060 print "</td>\n" .
3061 "</tr>\n";
3062 }
3063 if (defined $extra) {
3064 print "<tr>\n" .
3065 "<td colspan=\"4\">$extra</td>\n" .
3066 "</tr>\n";
3067 }
3068 print "</table>\n";
3069 }
3070
3071 sub git_history_body {
3072 # Warning: assumes constant type (blob or tree) during history
3073 my ($commitlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
3074
3075 $from = 0 unless defined $from;
3076 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
3077
3078 print "<table class=\"history\" cellspacing=\"0\">\n";
3079 my $alternate = 1;
3080 for (my $i = $from; $i <= $to; $i++) {
3081 my %co = %{$commitlist->[$i]};
3082 if (!%co) {
3083 next;
3084 }
3085 my $commit = $co{'id'};
3086
3087 my $ref = format_ref_marker($refs, $commit);
3088
3089 if ($alternate) {
3090 print "<tr class=\"dark\">\n";
3091 } else {
3092 print "<tr class=\"light\">\n";
3093 }
3094 $alternate ^= 1;
3095 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3096 # shortlog uses chop_str($co{'author_name'}, 10)
3097 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
3098 "<td>";
3099 # originally git_history used chop_str($co{'title'}, 50)
3100 print format_subject_html($co{'title'}, $co{'title_short'},
3101 href(action=>"commit", hash=>$commit), $ref);
3102 print "</td>\n" .
3103 "<td class=\"link\">" .
3104 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
3105 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
3106
3107 if ($ftype eq 'blob') {
3108 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
3109 my $blob_parent = git_get_hash_by_path($commit, $file_name);
3110 if (defined $blob_current && defined $blob_parent &&
3111 $blob_current ne $blob_parent) {
3112 print " | " .
3113 $cgi->a({-href => href(action=>"blobdiff",
3114 hash=>$blob_current, hash_parent=>$blob_parent,
3115 hash_base=>$hash_base, hash_parent_base=>$commit,
3116 file_name=>$file_name)},
3117 "diff to current");
3118 }
3119 }
3120 print "</td>\n" .
3121 "</tr>\n";
3122 }
3123 if (defined $extra) {
3124 print "<tr>\n" .
3125 "<td colspan=\"4\">$extra</td>\n" .
3126 "</tr>\n";
3127 }
3128 print "</table>\n";
3129 }
3130
3131 sub git_tags_body {
3132 # uses global variable $project
3133 my ($taglist, $from, $to, $extra) = @_;
3134 $from = 0 unless defined $from;
3135 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
3136
3137 print "<table class=\"tags\" cellspacing=\"0\">\n";
3138 my $alternate = 1;
3139 for (my $i = $from; $i <= $to; $i++) {
3140 my $entry = $taglist->[$i];
3141 my %tag = %$entry;
3142 my $comment = $tag{'subject'};
3143 my $comment_short;
3144 if (defined $comment) {
3145 $comment_short = chop_str($comment, 30, 5);
3146 }
3147 if ($alternate) {
3148 print "<tr class=\"dark\">\n";
3149 } else {
3150 print "<tr class=\"light\">\n";
3151 }
3152 $alternate ^= 1;
3153 if (defined $tag{'age'}) {
3154 print "<td><i>$tag{'age'}</i></td>\n";
3155 } else {
3156 print "<td></td>\n";
3157 }
3158 print "<td>" .
3159 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
3160 -class => "list name"}, esc_html($tag{'name'})) .
3161 "</td>\n" .
3162 "<td>";
3163 if (defined $comment) {
3164 print format_subject_html($comment, $comment_short,
3165 href(action=>"tag", hash=>$tag{'id'}));
3166 }
3167 print "</td>\n" .
3168 "<td class=\"selflink\">";
3169 if ($tag{'type'} eq "tag") {
3170 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
3171 } else {
3172 print "&nbsp;";
3173 }
3174 print "</td>\n" .
3175 "<td class=\"link\">" . " | " .
3176 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
3177 if ($tag{'reftype'} eq "commit") {
3178 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
3179 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log");
3180 } elsif ($tag{'reftype'} eq "blob") {
3181 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
3182 }
3183 print "</td>\n" .
3184 "</tr>";
3185 }
3186 if (defined $extra) {
3187 print "<tr>\n" .
3188 "<td colspan=\"5\">$extra</td>\n" .
3189 "</tr>\n";
3190 }
3191 print "</table>\n";
3192 }
3193
3194 sub git_heads_body {
3195 # uses global variable $project
3196 my ($headlist, $head, $from, $to, $extra) = @_;
3197 $from = 0 unless defined $from;
3198 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
3199
3200 print "<table class=\"heads\" cellspacing=\"0\">\n";
3201 my $alternate = 1;
3202 for (my $i = $from; $i <= $to; $i++) {
3203 my $entry = $headlist->[$i];
3204 my %ref = %$entry;
3205 my $curr = $ref{'id'} eq $head;
3206 if ($alternate) {
3207 print "<tr class=\"dark\">\n";
3208 } else {
3209 print "<tr class=\"light\">\n";
3210 }
3211 $alternate ^= 1;
3212 print "<td><i>$ref{'age'}</i></td>\n" .
3213 ($curr ? "<td class=\"current_head\">" : "<td>") .
3214 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'}),
3215 -class => "list name"},esc_html($ref{'name'})) .
3216 "</td>\n" .
3217 "<td class=\"link\">" .
3218 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'})}, "shortlog") . " | " .
3219 $cgi->a({-href => href(action=>"log", hash=>$ref{'name'})}, "log") . " | " .
3220 $cgi->a({-href => href(action=>"tree", hash=>$ref{'name'}, hash_base=>$ref{'name'})}, "tree") .
3221 "</td>\n" .
3222 "</tr>";
3223 }
3224 if (defined $extra) {
3225 print "<tr>\n" .
3226 "<td colspan=\"3\">$extra</td>\n" .
3227 "</tr>\n";
3228 }
3229 print "</table>\n";
3230 }
3231
3232 sub git_search_grep_body {
3233 my ($commitlist, $from, $to, $extra) = @_;
3234 $from = 0 unless defined $from;
3235 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3236
3237 print "<table class=\"grep\" cellspacing=\"0\">\n";
3238 my $alternate = 1;
3239 for (my $i = $from; $i <= $to; $i++) {
3240 my %co = %{$commitlist->[$i]};
3241 if (!%co) {
3242 next;
3243 }
3244 my $commit = $co{'id'};
3245 if ($alternate) {
3246 print "<tr class=\"dark\">\n";
3247 } else {
3248 print "<tr class=\"light\">\n";
3249 }
3250 $alternate ^= 1;
3251 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3252 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3253 "<td>" .
3254 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
3255 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3256 my $comment = $co{'comment'};
3257 foreach my $line (@$comment) {
3258 if ($line =~ m/^(.*)($search_regexp)(.*)$/i) {
3259 my $lead = esc_html($1) || "";
3260 $lead = chop_str($lead, 30, 10);
3261 my $match = esc_html($2) || "";
3262 my $trail = esc_html($3) || "";
3263 $trail = chop_str($trail, 30, 10);
3264 my $text = "$lead<span class=\"match\">$match</span>$trail";
3265 print chop_str($text, 80, 5) . "<br/>\n";
3266 }
3267 }
3268 print "</td>\n" .
3269 "<td class=\"link\">" .
3270 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3271 " | " .
3272 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3273 print "</td>\n" .
3274 "</tr>\n";
3275 }
3276 if (defined $extra) {
3277 print "<tr>\n" .
3278 "<td colspan=\"3\">$extra</td>\n" .
3279 "</tr>\n";
3280 }
3281 print "</table>\n";
3282 }
3283
3284 ## ======================================================================
3285 ## ======================================================================
3286 ## actions
3287
3288 sub git_project_list {
3289 my $order = $cgi->param('o');
3290 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
3291 die_error(undef, "Unknown order parameter");
3292 }
3293
3294 my @list = git_get_projects_list();
3295 if (!@list) {
3296 die_error(undef, "No projects found");
3297 }
3298
3299 git_header_html();
3300 if (-f $home_text) {
3301 print "<div class=\"index_include\">\n";
3302 open (my $fd, $home_text);
3303 print <$fd>;
3304 close $fd;
3305 print "</div>\n";
3306 }
3307 git_project_list_body(\@list, $order);
3308 git_footer_html();
3309 }
3310
3311 sub git_forks {
3312 my $order = $cgi->param('o');
3313 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
3314 die_error(undef, "Unknown order parameter");
3315 }
3316
3317 my @list = git_get_projects_list($project);
3318 if (!@list) {
3319 die_error(undef, "No forks found");
3320 }
3321
3322 git_header_html();
3323 git_print_page_nav('','');
3324 git_print_header_div('summary', "$project forks");
3325 git_project_list_body(\@list, $order);
3326 git_footer_html();
3327 }
3328
3329 sub git_project_index {
3330 my @projects = git_get_projects_list($project);
3331
3332 print $cgi->header(
3333 -type => 'text/plain',
3334 -charset => 'utf-8',
3335 -content_disposition => 'inline; filename="index.aux"');
3336
3337 foreach my $pr (@projects) {
3338 if (!exists $pr->{'owner'}) {
3339 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}");
3340 }
3341
3342 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
3343 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
3344 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
3345 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
3346 $path =~ s/ /\+/g;
3347 $owner =~ s/ /\+/g;
3348
3349 print "$path $owner\n";
3350 }
3351 }
3352
3353 sub git_summary {
3354 my $descr = git_get_project_description($project) || "none";
3355 my %co = parse_commit("HEAD");
3356 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
3357 my $head = $co{'id'};
3358
3359 my $owner = git_get_project_owner($project);
3360
3361 my $refs = git_get_references();
3362 # These get_*_list functions return one more to allow us to see if
3363 # there are more ...
3364 my @taglist = git_get_tags_list(16);
3365 my @headlist = git_get_heads_list(16);
3366 my @forklist;
3367 my ($check_forks) = gitweb_check_feature('forks');
3368
3369 if ($check_forks) {
3370 @forklist = git_get_projects_list($project);
3371 }
3372
3373 git_header_html();
3374 git_print_page_nav('summary','', $head);
3375
3376 print "<div class=\"title\">&nbsp;</div>\n";
3377 print "<table cellspacing=\"0\">\n" .
3378 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
3379 "<tr><td>owner</td><td>$owner</td></tr>\n";
3380 if (defined $cd{'rfc2822'}) {
3381 print "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
3382 }
3383
3384 # use per project git URL list in $projectroot/$project/cloneurl
3385 # or make project git URL from git base URL and project name
3386 my $url_tag = "URL";
3387 my @url_list = git_get_project_url_list($project);
3388 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
3389 foreach my $git_url (@url_list) {
3390 next unless $git_url;
3391 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
3392 $url_tag = "";
3393 }
3394 print "</table>\n";
3395
3396 if (-s "$projectroot/$project/README.html") {
3397 if (open my $fd, "$projectroot/$project/README.html") {
3398 print "<div class=\"title\">readme</div>\n";
3399 print $_ while (<$fd>);
3400 close $fd;
3401 }
3402 }
3403
3404 # we need to request one more than 16 (0..15) to check if
3405 # those 16 are all
3406 my @commitlist = $head ? parse_commits($head, 17) : ();
3407 if (@commitlist) {
3408 git_print_header_div('shortlog');
3409 git_shortlog_body(\@commitlist, 0, 15, $refs,
3410 $#commitlist <= 15 ? undef :
3411 $cgi->a({-href => href(action=>"shortlog")}, "..."));
3412 }
3413
3414 if (@taglist) {
3415 git_print_header_div('tags');
3416 git_tags_body(\@taglist, 0, 15,
3417 $#taglist <= 15 ? undef :
3418 $cgi->a({-href => href(action=>"tags")}, "..."));
3419 }
3420
3421 if (@headlist) {
3422 git_print_header_div('heads');
3423 git_heads_body(\@headlist, $head, 0, 15,
3424 $#headlist <= 15 ? undef :
3425 $cgi->a({-href => href(action=>"heads")}, "..."));
3426 }
3427
3428 if (@forklist) {
3429 git_print_header_div('forks');
3430 git_project_list_body(\@forklist, undef, 0, 15,
3431 $#forklist <= 15 ? undef :
3432 $cgi->a({-href => href(action=>"forks")}, "..."),
3433 'noheader');
3434 }
3435
3436 git_footer_html();
3437 }
3438
3439 sub git_tag {
3440 my $head = git_get_head_hash($project);
3441 git_header_html();
3442 git_print_page_nav('','', $head,undef,$head);
3443 my %tag = parse_tag($hash);
3444
3445 if (! %tag) {
3446 die_error(undef, "Unknown tag object");
3447 }
3448
3449 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
3450 print "<div class=\"title_text\">\n" .
3451 "<table cellspacing=\"0\">\n" .
3452 "<tr>\n" .
3453 "<td>object</td>\n" .
3454 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
3455 $tag{'object'}) . "</td>\n" .
3456 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
3457 $tag{'type'}) . "</td>\n" .
3458 "</tr>\n";
3459 if (defined($tag{'author'})) {
3460 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
3461 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
3462 print "<tr><td></td><td>" . $ad{'rfc2822'} .
3463 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
3464 "</td></tr>\n";
3465 }
3466 print "</table>\n\n" .
3467 "</div>\n";
3468 print "<div class=\"page_body\">";
3469 my $comment = $tag{'comment'};
3470 foreach my $line (@$comment) {
3471 chomp $line;
3472 print esc_html($line, -nbsp=>1) . "<br/>\n";
3473 }
3474 print "</div>\n";
3475 git_footer_html();
3476 }
3477
3478 sub git_blame2 {
3479 my $fd;
3480 my $ftype;
3481
3482 my ($have_blame) = gitweb_check_feature('blame');
3483 if (!$have_blame) {
3484 die_error('403 Permission denied', "Permission denied");
3485 }
3486 die_error('404 Not Found', "File name not defined") if (!$file_name);
3487 $hash_base ||= git_get_head_hash($project);
3488 die_error(undef, "Couldn't find base commit") unless ($hash_base);
3489 my %co = parse_commit($hash_base)
3490 or die_error(undef, "Reading commit failed");
3491 if (!defined $hash) {
3492 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
3493 or die_error(undef, "Error looking up file");
3494 }
3495 $ftype = git_get_type($hash);
3496 if ($ftype !~ "blob") {
3497 die_error('400 Bad Request', "Object is not a blob");
3498 }
3499 open ($fd, "-|", git_cmd(), "blame", '-p', '--',
3500 $file_name, $hash_base)
3501 or die_error(undef, "Open git-blame failed");
3502 git_header_html();
3503 my $formats_nav =
3504 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3505 "blob") .
3506 " | " .
3507 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3508 "history") .
3509 " | " .
3510 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
3511 "HEAD");
3512 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3513 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3514 git_print_page_path($file_name, $ftype, $hash_base);
3515 my @rev_color = (qw(light2 dark2));
3516 my $num_colors = scalar(@rev_color);
3517 my $current_color = 0;
3518 my $last_rev;
3519 print <<HTML;
3520 <div class="page_body">
3521 <table class="blame">
3522 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
3523 HTML
3524 my %metainfo = ();
3525 while (1) {
3526 $_ = <$fd>;
3527 last unless defined $_;
3528 my ($full_rev, $orig_lineno, $lineno, $group_size) =
3529 /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;
3530 if (!exists $metainfo{$full_rev}) {
3531 $metainfo{$full_rev} = {};
3532 }
3533 my $meta = $metainfo{$full_rev};
3534 while (<$fd>) {
3535 last if (s/^\t//);
3536 if (/^(\S+) (.*)$/) {
3537 $meta->{$1} = $2;
3538 }
3539 }
3540 my $data = $_;
3541 chomp $data;
3542 my $rev = substr($full_rev, 0, 8);
3543 my $author = $meta->{'author'};
3544 my %date = parse_date($meta->{'author-time'},
3545 $meta->{'author-tz'});
3546 my $date = $date{'iso-tz'};
3547 if ($group_size) {
3548 $current_color = ++$current_color % $num_colors;
3549 }
3550 print "<tr class=\"$rev_color[$current_color]\">\n";
3551 if ($group_size) {
3552 print "<td class=\"sha1\"";
3553 print " title=\"". esc_html($author) . ", $date\"";
3554 print " rowspan=\"$group_size\"" if ($group_size > 1);
3555 print ">";
3556 print $cgi->a({-href => href(action=>"commit",
3557 hash=>$full_rev,
3558 file_name=>$file_name)},
3559 esc_html($rev));
3560 print "</td>\n";
3561 }
3562 open (my $dd, "-|", git_cmd(), "rev-parse", "$full_rev^")
3563 or die_error(undef, "Open git-rev-parse failed");
3564 my $parent_commit = <$dd>;
3565 close $dd;
3566 chomp($parent_commit);
3567 my $blamed = href(action => 'blame',
3568 file_name => $meta->{'filename'},
3569 hash_base => $parent_commit);
3570 print "<td class=\"linenr\">";
3571 print $cgi->a({ -href => "$blamed#l$orig_lineno",
3572 -id => "l$lineno",
3573 -class => "linenr" },
3574 esc_html($lineno));
3575 print "</td>";
3576 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
3577 print "</tr>\n";
3578 }
3579 print "</table>\n";
3580 print "</div>";
3581 close $fd
3582 or print "Reading blob failed\n";
3583 git_footer_html();
3584 }
3585
3586 sub git_blame {
3587 my $fd;
3588
3589 my ($have_blame) = gitweb_check_feature('blame');
3590 if (!$have_blame) {
3591 die_error('403 Permission denied', "Permission denied");
3592 }
3593 die_error('404 Not Found', "File name not defined") if (!$file_name);
3594 $hash_base ||= git_get_head_hash($project);
3595 die_error(undef, "Couldn't find base commit") unless ($hash_base);
3596 my %co = parse_commit($hash_base)
3597 or die_error(undef, "Reading commit failed");
3598 if (!defined $hash) {
3599 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
3600 or die_error(undef, "Error lookup file");
3601 }
3602 open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
3603 or die_error(undef, "Open git-annotate failed");
3604 git_header_html();
3605 my $formats_nav =
3606 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3607 "blob") .
3608 " | " .
3609 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3610 "history") .
3611 " | " .
3612 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
3613 "HEAD");
3614 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3615 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3616 git_print_page_path($file_name, 'blob', $hash_base);
3617 print "<div class=\"page_body\">\n";
3618 print <<HTML;
3619 <table class="blame">
3620 <tr>
3621 <th>Commit</th>
3622 <th>Age</th>
3623 <th>Author</th>
3624 <th>Line</th>
3625 <th>Data</th>
3626 </tr>
3627 HTML
3628 my @line_class = (qw(light dark));
3629 my $line_class_len = scalar (@line_class);
3630 my $line_class_num = $#line_class;
3631 while (my $line = <$fd>) {
3632 my $long_rev;
3633 my $short_rev;
3634 my $author;
3635 my $time;
3636 my $lineno;
3637 my $data;
3638 my $age;
3639 my $age_str;
3640 my $age_class;
3641
3642 chomp $line;
3643 $line_class_num = ($line_class_num + 1) % $line_class_len;
3644
3645 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
3646 $long_rev = $1;
3647 $author = $2;
3648 $time = $3;
3649 $lineno = $4;
3650 $data = $5;
3651 } else {
3652 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
3653 next;
3654 }
3655 $short_rev = substr ($long_rev, 0, 8);
3656 $age = time () - $time;
3657 $age_str = age_string ($age);
3658 $age_str =~ s/ /&nbsp;/g;
3659 $age_class = age_class($age);
3660 $author = esc_html ($author);
3661 $author =~ s/ /&nbsp;/g;
3662
3663 $data = untabify($data);
3664 $data = esc_html ($data);
3665
3666 print <<HTML;
3667 <tr class="$line_class[$line_class_num]">
3668 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
3669 <td class="$age_class">$age_str</td>
3670 <td>$author</td>
3671 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
3672 <td class="pre">$data</td>
3673 </tr>
3674 HTML
3675 } # while (my $line = <$fd>)
3676 print "</table>\n\n";
3677 close $fd
3678 or print "Reading blob failed.\n";
3679 print "</div>";
3680 git_footer_html();
3681 }
3682
3683 sub git_tags {
3684 my $head = git_get_head_hash($project);
3685 git_header_html();
3686 git_print_page_nav('','', $head,undef,$head);
3687 git_print_header_div('summary', $project);
3688
3689 my @tagslist = git_get_tags_list();
3690 if (@tagslist) {
3691 git_tags_body(\@tagslist);
3692 }
3693 git_footer_html();
3694 }
3695
3696 sub git_heads {
3697 my $head = git_get_head_hash($project);
3698 git_header_html();
3699 git_print_page_nav('','', $head,undef,$head);
3700 git_print_header_div('summary', $project);
3701
3702 my @headslist = git_get_heads_list();
3703 if (@headslist) {
3704 git_heads_body(\@headslist, $head);
3705 }
3706 git_footer_html();
3707 }
3708
3709 sub git_blob_plain {
3710 my $expires;
3711
3712 if (!defined $hash) {
3713 if (defined $file_name) {
3714 my $base = $hash_base || git_get_head_hash($project);
3715 $hash = git_get_hash_by_path($base, $file_name, "blob")
3716 or die_error(undef, "Error lookup file");
3717 } else {
3718 die_error(undef, "No file name defined");
3719 }
3720 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3721 # blobs defined by non-textual hash id's can be cached
3722 $expires = "+1d";
3723 }
3724
3725 my $type = shift;
3726 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
3727 or die_error(undef, "Couldn't cat $file_name, $hash");
3728
3729 $type ||= blob_mimetype($fd, $file_name);
3730
3731 # save as filename, even when no $file_name is given
3732 my $save_as = "$hash";
3733 if (defined $file_name) {
3734 $save_as = $file_name;
3735 } elsif ($type =~ m/^text\//) {
3736 $save_as .= '.txt';
3737 }
3738
3739 print $cgi->header(
3740 -type => "$type",
3741 -expires=>$expires,
3742 -content_disposition => 'inline; filename="' . "$save_as" . '"');
3743 undef $/;
3744 binmode STDOUT, ':raw';
3745 print <$fd>;
3746 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
3747 $/ = "\n";
3748 close $fd;
3749 }
3750
3751 sub git_blob {
3752 my $expires;
3753
3754 if (!defined $hash) {
3755 if (defined $file_name) {
3756 my $base = $hash_base || git_get_head_hash($project);
3757 $hash = git_get_hash_by_path($base, $file_name, "blob")
3758 or die_error(undef, "Error lookup file");
3759 } else {
3760 die_error(undef, "No file name defined");
3761 }
3762 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3763 # blobs defined by non-textual hash id's can be cached
3764 $expires = "+1d";
3765 }
3766
3767 my ($have_blame) = gitweb_check_feature('blame');
3768 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
3769 or die_error(undef, "Couldn't cat $file_name, $hash");
3770 my $mimetype = blob_mimetype($fd, $file_name);
3771 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)!) {
3772 close $fd;
3773 return git_blob_plain($mimetype);
3774 }
3775 # we can have blame only for text/* mimetype
3776 $have_blame &&= ($mimetype =~ m!^text/!);
3777
3778 git_header_html(undef, $expires);
3779 my $formats_nav = '';
3780 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3781 if (defined $file_name) {
3782 if ($have_blame) {
3783 $formats_nav .=
3784 $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
3785 hash=>$hash, file_name=>$file_name)},
3786 "blame") .
3787 " | ";
3788 }
3789 $formats_nav .=
3790 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3791 hash=>$hash, file_name=>$file_name)},
3792 "history") .
3793 " | " .
3794 $cgi->a({-href => href(action=>"blob_plain",
3795 hash=>$hash, file_name=>$file_name)},
3796 "raw") .
3797 " | " .
3798 $cgi->a({-href => href(action=>"blob",
3799 hash_base=>"HEAD", file_name=>$file_name)},
3800 "HEAD");
3801 } else {
3802 $formats_nav .=
3803 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "raw");
3804 }
3805 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3806 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3807 } else {
3808 print "<div class=\"page_nav\">\n" .
3809 "<br/><br/></div>\n" .
3810 "<div class=\"title\">$hash</div>\n";
3811 }
3812 git_print_page_path($file_name, "blob", $hash_base);
3813 print "<div class=\"page_body\">\n";
3814 if ($mimetype =~ m!^text/!) {
3815 my $nr;
3816 while (my $line = <$fd>) {
3817 chomp $line;
3818 $nr++;
3819 $line = untabify($line);
3820 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
3821 $nr, $nr, $nr, esc_html($line, -nbsp=>1);
3822 }
3823 } elsif ($mimetype =~ m!^image/!) {
3824 print qq!<img type="$mimetype"!;
3825 if ($file_name) {
3826 print qq! alt="$file_name" title="$file_name"!;
3827 }
3828 print qq! src="! .
3829 href(action=>"blob_plain", hash=>$hash,
3830 hash_base=>$hash_base, file_name=>$file_name) .
3831 qq!" />\n!;
3832 }
3833 close $fd
3834 or print "Reading blob failed.\n";
3835 print "</div>";
3836 git_footer_html();
3837 }
3838
3839 sub git_tree {
3840 my $have_snapshot = gitweb_have_snapshot();
3841
3842 if (!defined $hash_base) {
3843 $hash_base = "HEAD";
3844 }
3845 if (!defined $hash) {
3846 if (defined $file_name) {
3847 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
3848 } else {
3849 $hash = $hash_base;
3850 }
3851 }
3852 $/ = "\0";
3853 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
3854 or die_error(undef, "Open git-ls-tree failed");
3855 my @entries = map { chomp; $_ } <$fd>;
3856 close $fd or die_error(undef, "Reading tree failed");
3857 $/ = "\n";
3858
3859 my $refs = git_get_references();
3860 my $ref = format_ref_marker($refs, $hash_base);
3861 git_header_html();
3862 my $basedir = '';
3863 my ($have_blame) = gitweb_check_feature('blame');
3864 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3865 my @views_nav = ();
3866 if (defined $file_name) {
3867 push @views_nav,
3868 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3869 hash=>$hash, file_name=>$file_name)},
3870 "history"),
3871 $cgi->a({-href => href(action=>"tree",
3872 hash_base=>"HEAD", file_name=>$file_name)},
3873 "HEAD"),
3874 }
3875 if ($have_snapshot) {
3876 # FIXME: Should be available when we have no hash base as well.
3877 push @views_nav,
3878 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)},
3879 "snapshot");
3880 }
3881 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
3882 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
3883 } else {
3884 undef $hash_base;
3885 print "<div class=\"page_nav\">\n";
3886 print "<br/><br/></div>\n";
3887 print "<div class=\"title\">$hash</div>\n";
3888 }
3889 if (defined $file_name) {
3890 $basedir = $file_name;
3891 if ($basedir ne '' && substr($basedir, -1) ne '/') {
3892 $basedir .= '/';
3893 }
3894 }
3895 git_print_page_path($file_name, 'tree', $hash_base);
3896 print "<div class=\"page_body\">\n";
3897 print "<table cellspacing=\"0\">\n";
3898 my $alternate = 1;
3899 # '..' (top directory) link if possible
3900 if (defined $hash_base &&
3901 defined $file_name && $file_name =~ m![^/]+$!) {
3902 if ($alternate) {
3903 print "<tr class=\"dark\">\n";
3904 } else {
3905 print "<tr class=\"light\">\n";
3906 }
3907 $alternate ^= 1;
3908
3909 my $up = $file_name;
3910 $up =~ s!/?[^/]+$!!;
3911 undef $up unless $up;
3912 # based on git_print_tree_entry
3913 print '<td class="mode">' . mode_str('040000') . "</td>\n";
3914 print '<td class="list">';
3915 print $cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,
3916 file_name=>$up)},
3917 "..");
3918 print "</td>\n";
3919 print "<td class=\"link\"></td>\n";
3920
3921 print "</tr>\n";
3922 }
3923 foreach my $line (@entries) {
3924 my %t = parse_ls_tree_line($line, -z => 1);
3925
3926 if ($alternate) {
3927 print "<tr class=\"dark\">\n";
3928 } else {
3929 print "<tr class=\"light\">\n";
3930 }
3931 $alternate ^= 1;
3932
3933 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
3934
3935 print "</tr>\n";
3936 }
3937 print "</table>\n" .
3938 "</div>";
3939 git_footer_html();
3940 }
3941
3942 sub git_snapshot {
3943 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
3944 my $have_snapshot = (defined $ctype && defined $suffix);
3945 if (!$have_snapshot) {
3946 die_error('403 Permission denied', "Permission denied");
3947 }
3948
3949 if (!defined $hash) {
3950 $hash = git_get_head_hash($project);
3951 }
3952
3953 my $filename = decode_utf8(basename($project)) . "-$hash.tar.$suffix";
3954
3955 print $cgi->header(
3956 -type => "application/$ctype",
3957 -content_disposition => 'inline; filename="' . "$filename" . '"',
3958 -status => '200 OK');
3959
3960 my $git = git_cmd_str();
3961 my $name = $project;
3962 $name =~ s/\047/\047\\\047\047/g;
3963 open my $fd, "-|",
3964 "$git archive --format=tar --prefix=\'$name\'/ $hash | $command"
3965 or die_error(undef, "Execute git-tar-tree failed");
3966 binmode STDOUT, ':raw';
3967 print <$fd>;
3968 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
3969 close $fd;
3970
3971 }
3972
3973 sub git_log {
3974 my $head = git_get_head_hash($project);
3975 if (!defined $hash) {
3976 $hash = $head;
3977 }
3978 if (!defined $page) {
3979 $page = 0;
3980 }
3981 my $refs = git_get_references();
3982
3983 my @commitlist = parse_commits($hash, 101, (100 * $page));
3984
3985 my $paging_nav = format_paging_nav('log', $hash, $head, $page, (100 * ($page+1)));
3986
3987 git_header_html();
3988 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
3989
3990 if (!@commitlist) {
3991 my %co = parse_commit($hash);
3992
3993 git_print_header_div('summary', $project);
3994 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
3995 }
3996 my $to = ($#commitlist >= 99) ? (99) : ($#commitlist);
3997 for (my $i = 0; $i <= $to; $i++) {
3998 my %co = %{$commitlist[$i]};
3999 next if !%co;
4000 my $commit = $co{'id'};
4001 my $ref = format_ref_marker($refs, $commit);
4002 my %ad = parse_date($co{'author_epoch'});
4003 git_print_header_div('commit',
4004 "<span class=\"age\">$co{'age_string'}</span>" .
4005 esc_html($co{'title'}) . $ref,
4006 $commit);
4007 print "<div class=\"title_text\">\n" .
4008 "<div class=\"log_link\">\n" .
4009 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
4010 " | " .
4011 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
4012 " | " .
4013 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
4014 "<br/>\n" .
4015 "</div>\n" .
4016 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
4017 "</div>\n";
4018
4019 print "<div class=\"log_body\">\n";
4020 git_print_log($co{'comment'}, -final_empty_line=> 1);
4021 print "</div>\n";
4022 }
4023 if ($#commitlist >= 100) {
4024 print "<div class=\"page_nav\">\n";
4025 print $cgi->a({-href => href(action=>"log", hash=>$hash, page=>$page+1),
4026 -accesskey => "n", -title => "Alt-n"}, "next");
4027 print "</div>\n";
4028 }
4029 git_footer_html();
4030 }
4031
4032 sub git_commit {
4033 $hash ||= $hash_base || "HEAD";
4034 my %co = parse_commit($hash);
4035 if (!%co) {
4036 die_error(undef, "Unknown commit object");
4037 }
4038 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4039 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
4040
4041 my $parent = $co{'parent'};
4042 my $parents = $co{'parents'}; # listref
4043
4044 # we need to prepare $formats_nav before any parameter munging
4045 my $formats_nav;
4046 if (!defined $parent) {
4047 # --root commitdiff
4048 $formats_nav .= '(initial)';
4049 } elsif (@$parents == 1) {
4050 # single parent commit
4051 $formats_nav .=
4052 '(parent: ' .
4053 $cgi->a({-href => href(action=>"commit",
4054 hash=>$parent)},
4055 esc_html(substr($parent, 0, 7))) .
4056 ')';
4057 } else {
4058 # merge commit
4059 $formats_nav .=
4060 '(merge: ' .
4061 join(' ', map {
4062 $cgi->a({-href => href(action=>"commit",
4063 hash=>$_)},
4064 esc_html(substr($_, 0, 7)));
4065 } @$parents ) .
4066 ')';
4067 }
4068
4069 if (!defined $parent) {
4070 $parent = "--root";
4071 }
4072 my @difftree;
4073 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
4074 @diff_opts,
4075 (@$parents <= 1 ? $parent : '-c'),
4076 $hash, "--"
4077 or die_error(undef, "Open git-diff-tree failed");
4078 @difftree = map { chomp; $_ } <$fd>;
4079 close $fd or die_error(undef, "Reading git-diff-tree failed");
4080
4081 # non-textual hash id's can be cached
4082 my $expires;
4083 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4084 $expires = "+1d";
4085 }
4086 my $refs = git_get_references();
4087 my $ref = format_ref_marker($refs, $co{'id'});
4088
4089 my $have_snapshot = gitweb_have_snapshot();
4090
4091 git_header_html(undef, $expires);
4092 git_print_page_nav('commit', '',
4093 $hash, $co{'tree'}, $hash,
4094 $formats_nav);
4095
4096 if (defined $co{'parent'}) {
4097 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
4098 } else {
4099 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
4100 }
4101 print "<div class=\"title_text\">\n" .
4102 "<table cellspacing=\"0\">\n";
4103 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
4104 "<tr>" .
4105 "<td></td><td> $ad{'rfc2822'}";
4106 if ($ad{'hour_local'} < 6) {
4107 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
4108 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4109 } else {
4110 printf(" (%02d:%02d %s)",
4111 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4112 }
4113 print "</td>" .
4114 "</tr>\n";
4115 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
4116 print "<tr><td></td><td> $cd{'rfc2822'}" .
4117 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
4118 "</td></tr>\n";
4119 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
4120 print "<tr>" .
4121 "<td>tree</td>" .
4122 "<td class=\"sha1\">" .
4123 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
4124 class => "list"}, $co{'tree'}) .
4125 "</td>" .
4126 "<td class=\"link\">" .
4127 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
4128 "tree");
4129 if ($have_snapshot) {
4130 print " | " .
4131 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
4132 }
4133 print "</td>" .
4134 "</tr>\n";
4135
4136 foreach my $par (@$parents) {
4137 print "<tr>" .
4138 "<td>parent</td>" .
4139 "<td class=\"sha1\">" .
4140 $cgi->a({-href => href(action=>"commit", hash=>$par),
4141 class => "list"}, $par) .
4142 "</td>" .
4143 "<td class=\"link\">" .
4144 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
4145 " | " .
4146 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
4147 "</td>" .
4148 "</tr>\n";
4149 }
4150 print "</table>".
4151 "</div>\n";
4152
4153 print "<div class=\"page_body\">\n";
4154 git_print_log($co{'comment'});
4155 print "</div>\n";
4156
4157 git_difftree_body(\@difftree, $hash, @$parents);
4158
4159 git_footer_html();
4160 }
4161
4162 sub git_object {
4163 # object is defined by:
4164 # - hash or hash_base alone
4165 # - hash_base and file_name
4166 my $type;
4167
4168 # - hash or hash_base alone
4169 if ($hash || ($hash_base && !defined $file_name)) {
4170 my $object_id = $hash || $hash_base;
4171
4172 my $git_command = git_cmd_str();
4173 open my $fd, "-|", "$git_command cat-file -t $object_id 2>/dev/null"
4174 or die_error('404 Not Found', "Object does not exist");
4175 $type = <$fd>;
4176 chomp $type;
4177 close $fd
4178 or die_error('404 Not Found', "Object does not exist");
4179
4180 # - hash_base and file_name
4181 } elsif ($hash_base && defined $file_name) {
4182 $file_name =~ s,/+$,,;
4183
4184 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
4185 or die_error('404 Not Found', "Base object does not exist");
4186
4187 # here errors should not hapen
4188 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
4189 or die_error(undef, "Open git-ls-tree failed");
4190 my $line = <$fd>;
4191 close $fd;
4192
4193 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
4194 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
4195 die_error('404 Not Found', "File or directory for given base does not exist");
4196 }
4197 $type = $2;
4198 $hash = $3;
4199 } else {
4200 die_error('404 Not Found', "Not enough information to find object");
4201 }
4202
4203 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
4204 hash=>$hash, hash_base=>$hash_base,
4205 file_name=>$file_name),
4206 -status => '302 Found');
4207 }
4208
4209 sub git_blobdiff {
4210 my $format = shift || 'html';
4211
4212 my $fd;
4213 my @difftree;
4214 my %diffinfo;
4215 my $expires;
4216
4217 # preparing $fd and %diffinfo for git_patchset_body
4218 # new style URI
4219 if (defined $hash_base && defined $hash_parent_base) {
4220 if (defined $file_name) {
4221 # read raw output
4222 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4223 $hash_parent_base, $hash_base,
4224 "--", (defined $file_parent ? $file_parent : ()), $file_name
4225 or die_error(undef, "Open git-diff-tree failed");
4226 @difftree = map { chomp; $_ } <$fd>;
4227 close $fd
4228 or die_error(undef, "Reading git-diff-tree failed");
4229 @difftree
4230 or die_error('404 Not Found', "Blob diff not found");
4231
4232 } elsif (defined $hash &&
4233 $hash =~ /[0-9a-fA-F]{40}/) {
4234 # try to find filename from $hash
4235
4236 # read filtered raw output
4237 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4238 $hash_parent_base, $hash_base, "--"
4239 or die_error(undef, "Open git-diff-tree failed");
4240 @difftree =
4241 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
4242 # $hash == to_id
4243 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
4244 map { chomp; $_ } <$fd>;
4245 close $fd
4246 or die_error(undef, "Reading git-diff-tree failed");
4247 @difftree
4248 or die_error('404 Not Found', "Blob diff not found");
4249
4250 } else {
4251 die_error('404 Not Found', "Missing one of the blob diff parameters");
4252 }
4253
4254 if (@difftree > 1) {
4255 die_error('404 Not Found', "Ambiguous blob diff specification");
4256 }
4257
4258 %diffinfo = parse_difftree_raw_line($difftree[0]);
4259 $file_parent ||= $diffinfo{'from_file'} || $file_name || $diffinfo{'file'};
4260 $file_name ||= $diffinfo{'to_file'} || $diffinfo{'file'};
4261
4262 $hash_parent ||= $diffinfo{'from_id'};
4263 $hash ||= $diffinfo{'to_id'};
4264
4265 # non-textual hash id's can be cached
4266 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
4267 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
4268 $expires = '+1d';
4269 }
4270
4271 # open patch output
4272 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4273 '-p', ($format eq 'html' ? "--full-index" : ()),
4274 $hash_parent_base, $hash_base,
4275 "--", (defined $file_parent ? $file_parent : ()), $file_name
4276 or die_error(undef, "Open git-diff-tree failed");
4277 }
4278
4279 # old/legacy style URI
4280 if (!%diffinfo && # if new style URI failed
4281 defined $hash && defined $hash_parent) {
4282 # fake git-diff-tree raw output
4283 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
4284 $diffinfo{'from_id'} = $hash_parent;
4285 $diffinfo{'to_id'} = $hash;
4286 if (defined $file_name) {
4287 if (defined $file_parent) {
4288 $diffinfo{'status'} = '2';
4289 $diffinfo{'from_file'} = $file_parent;
4290 $diffinfo{'to_file'} = $file_name;
4291 } else { # assume not renamed
4292 $diffinfo{'status'} = '1';
4293 $diffinfo{'from_file'} = $file_name;
4294 $diffinfo{'to_file'} = $file_name;
4295 }
4296 } else { # no filename given
4297 $diffinfo{'status'} = '2';
4298 $diffinfo{'from_file'} = $hash_parent;
4299 $diffinfo{'to_file'} = $hash;
4300 }
4301
4302 # non-textual hash id's can be cached
4303 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
4304 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4305 $expires = '+1d';
4306 }
4307
4308 # open patch output
4309 open $fd, "-|", git_cmd(), "diff", @diff_opts,
4310 '-p', ($format eq 'html' ? "--full-index" : ()),
4311 $hash_parent, $hash, "--"
4312 or die_error(undef, "Open git-diff failed");
4313 } else {
4314 die_error('404 Not Found', "Missing one of the blob diff parameters")
4315 unless %diffinfo;
4316 }
4317
4318 # header
4319 if ($format eq 'html') {
4320 my $formats_nav =
4321 $cgi->a({-href => href(action=>"blobdiff_plain",
4322 hash=>$hash, hash_parent=>$hash_parent,
4323 hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
4324 file_name=>$file_name, file_parent=>$file_parent)},
4325 "raw");
4326 git_header_html(undef, $expires);
4327 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4328 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4329 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4330 } else {
4331 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
4332 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
4333 }
4334 if (defined $file_name) {
4335 git_print_page_path($file_name, "blob", $hash_base);
4336 } else {
4337 print "<div class=\"page_path\"></div>\n";
4338 }
4339
4340 } elsif ($format eq 'plain') {
4341 print $cgi->header(
4342 -type => 'text/plain',
4343 -charset => 'utf-8',
4344 -expires => $expires,
4345 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
4346
4347 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
4348
4349 } else {
4350 die_error(undef, "Unknown blobdiff format");
4351 }
4352
4353 # patch
4354 if ($format eq 'html') {
4355 print "<div class=\"page_body\">\n";
4356
4357 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
4358 close $fd;
4359
4360 print "</div>\n"; # class="page_body"
4361 git_footer_html();
4362
4363 } else {
4364 while (my $line = <$fd>) {
4365 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
4366 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
4367
4368 print $line;
4369
4370 last if $line =~ m!^\+\+\+!;
4371 }
4372 local $/ = undef;
4373 print <$fd>;
4374 close $fd;
4375 }
4376 }
4377
4378 sub git_blobdiff_plain {
4379 git_blobdiff('plain');
4380 }
4381
4382 sub git_commitdiff {
4383 my $format = shift || 'html';
4384 $hash ||= $hash_base || "HEAD";
4385 my %co = parse_commit($hash);
4386 if (!%co) {
4387 die_error(undef, "Unknown commit object");
4388 }
4389
4390 # we need to prepare $formats_nav before any parameter munging
4391 my $formats_nav;
4392 if ($format eq 'html') {
4393 $formats_nav =
4394 $cgi->a({-href => href(action=>"commitdiff_plain",
4395 hash=>$hash, hash_parent=>$hash_parent)},
4396 "raw");
4397
4398 if (defined $hash_parent) {
4399 # commitdiff with two commits given
4400 my $hash_parent_short = $hash_parent;
4401 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4402 $hash_parent_short = substr($hash_parent, 0, 7);
4403 }
4404 $formats_nav .=
4405 ' (from: ' .
4406 $cgi->a({-href => href(action=>"commitdiff",
4407 hash=>$hash_parent)},
4408 esc_html($hash_parent_short)) .
4409 ')';
4410 } elsif (!$co{'parent'}) {
4411 # --root commitdiff
4412 $formats_nav .= ' (initial)';
4413 } elsif (scalar @{$co{'parents'}} == 1) {
4414 # single parent commit
4415 $formats_nav .=
4416 ' (parent: ' .
4417 $cgi->a({-href => href(action=>"commitdiff",
4418 hash=>$co{'parent'})},
4419 esc_html(substr($co{'parent'}, 0, 7))) .
4420 ')';
4421 } else {
4422 # merge commit
4423 $formats_nav .=
4424 ' (merge: ' .
4425 join(' ', map {
4426 $cgi->a({-href => href(action=>"commitdiff",
4427 hash=>$_)},
4428 esc_html(substr($_, 0, 7)));
4429 } @{$co{'parents'}} ) .
4430 ')';
4431 }
4432 }
4433
4434 my $hash_parent_param = $hash_parent;
4435 if (!defined $hash_parent) {
4436 $hash_parent_param =
4437 @{$co{'parents'}} > 1 ? '-c' : $co{'parent'} || '--root';
4438 }
4439
4440 # read commitdiff
4441 my $fd;
4442 my @difftree;
4443 if ($format eq 'html') {
4444 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4445 "--no-commit-id", "--patch-with-raw", "--full-index",
4446 $hash_parent_param, $hash, "--"
4447 or die_error(undef, "Open git-diff-tree failed");
4448
4449 while (my $line = <$fd>) {
4450 chomp $line;
4451 # empty line ends raw part of diff-tree output
4452 last unless $line;
4453 push @difftree, scalar parse_difftree_raw_line($line);
4454 }
4455
4456 } elsif ($format eq 'plain') {
4457 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4458 '-p', $hash_parent_param, $hash, "--"
4459 or die_error(undef, "Open git-diff-tree failed");
4460
4461 } else {
4462 die_error(undef, "Unknown commitdiff format");
4463 }
4464
4465 # non-textual hash id's can be cached
4466 my $expires;
4467 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4468 $expires = "+1d";
4469 }
4470
4471 # write commit message
4472 if ($format eq 'html') {
4473 my $refs = git_get_references();
4474 my $ref = format_ref_marker($refs, $co{'id'});
4475
4476 git_header_html(undef, $expires);
4477 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
4478 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
4479 git_print_authorship(\%co);
4480 print "<div class=\"page_body\">\n";
4481 if (@{$co{'comment'}} > 1) {
4482 print "<div class=\"log\">\n";
4483 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
4484 print "</div>\n"; # class="log"
4485 }
4486
4487 } elsif ($format eq 'plain') {
4488 my $refs = git_get_references("tags");
4489 my $tagname = git_get_rev_name_tags($hash);
4490 my $filename = basename($project) . "-$hash.patch";
4491
4492 print $cgi->header(
4493 -type => 'text/plain',
4494 -charset => 'utf-8',
4495 -expires => $expires,
4496 -content_disposition => 'inline; filename="' . "$filename" . '"');
4497 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4498 print <<TEXT;
4499 From: $co{'author'}
4500 Date: $ad{'rfc2822'} ($ad{'tz_local'})
4501 Subject: $co{'title'}
4502 TEXT
4503 print "X-Git-Tag: $tagname\n" if $tagname;
4504 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
4505
4506 foreach my $line (@{$co{'comment'}}) {
4507 print "$line\n";
4508 }
4509 print "---\n\n";
4510 }
4511
4512 # write patch
4513 if ($format eq 'html') {
4514 git_difftree_body(\@difftree, $hash, $hash_parent || @{$co{'parents'}});
4515 print "<br/>\n";
4516
4517 git_patchset_body($fd, \@difftree, $hash, $hash_parent || @{$co{'parents'}});
4518 close $fd;
4519 print "</div>\n"; # class="page_body"
4520 git_footer_html();
4521
4522 } elsif ($format eq 'plain') {
4523 local $/ = undef;
4524 print <$fd>;
4525 close $fd
4526 or print "Reading git-diff-tree failed\n";
4527 }
4528 }
4529
4530 sub git_commitdiff_plain {
4531 git_commitdiff('plain');
4532 }
4533
4534 sub git_history {
4535 if (!defined $hash_base) {
4536 $hash_base = git_get_head_hash($project);
4537 }
4538 if (!defined $page) {
4539 $page = 0;
4540 }
4541 my $ftype;
4542 my %co = parse_commit($hash_base);
4543 if (!%co) {
4544 die_error(undef, "Unknown commit object");
4545 }
4546
4547 my $refs = git_get_references();
4548 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
4549
4550 if (!defined $hash && defined $file_name) {
4551 $hash = git_get_hash_by_path($hash_base, $file_name);
4552 }
4553 if (defined $hash) {
4554 $ftype = git_get_type($hash);
4555 }
4556
4557 my @commitlist = parse_commits($hash_base, 101, (100 * $page), "--full-history", $file_name);
4558
4559 my $paging_nav = '';
4560 if ($page > 0) {
4561 $paging_nav .=
4562 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4563 file_name=>$file_name)},
4564 "first");
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 => "p", -title => "Alt-p"}, "prev");
4569 } else {
4570 $paging_nav .= "first";
4571 $paging_nav .= " &sdot; prev";
4572 }
4573 if ($#commitlist >= 100) {
4574 $paging_nav .= " &sdot; " .
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 } else {
4579 $paging_nav .= " &sdot; next";
4580 }
4581 my $next_link = '';
4582 if ($#commitlist >= 100) {
4583 $next_link =
4584 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4585 file_name=>$file_name, page=>$page+1),
4586 -accesskey => "n", -title => "Alt-n"}, "next");
4587 }
4588
4589 git_header_html();
4590 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
4591 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4592 git_print_page_path($file_name, $ftype, $hash_base);
4593
4594 git_history_body(\@commitlist, 0, 99,
4595 $refs, $hash_base, $ftype, $next_link);
4596
4597 git_footer_html();
4598 }
4599
4600 sub git_search {
4601 my ($have_search) = gitweb_check_feature('search');
4602 if (!$have_search) {
4603 die_error('403 Permission denied', "Permission denied");
4604 }
4605 if (!defined $searchtext) {
4606 die_error(undef, "Text field empty");
4607 }
4608 if (!defined $hash) {
4609 $hash = git_get_head_hash($project);
4610 }
4611 my %co = parse_commit($hash);
4612 if (!%co) {
4613 die_error(undef, "Unknown commit object");
4614 }
4615 if (!defined $page) {
4616 $page = 0;
4617 }
4618
4619 $searchtype ||= 'commit';
4620 if ($searchtype eq 'pickaxe') {
4621 # pickaxe may take all resources of your box and run for several minutes
4622 # with every query - so decide by yourself how public you make this feature
4623 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
4624 if (!$have_pickaxe) {
4625 die_error('403 Permission denied', "Permission denied");
4626 }
4627 }
4628
4629 git_header_html();
4630
4631 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
4632 my $greptype;
4633 if ($searchtype eq 'commit') {
4634 $greptype = "--grep=";
4635 } elsif ($searchtype eq 'author') {
4636 $greptype = "--author=";
4637 } elsif ($searchtype eq 'committer') {
4638 $greptype = "--committer=";
4639 }
4640 $greptype .= $search_regexp;
4641 my @commitlist = parse_commits($hash, 101, (100 * $page), $greptype);
4642
4643 my $paging_nav = '';
4644 if ($page > 0) {
4645 $paging_nav .=
4646 $cgi->a({-href => href(action=>"search", hash=>$hash,
4647 searchtext=>$searchtext, searchtype=>$searchtype)},
4648 "first");
4649 $paging_nav .= " &sdot; " .
4650 $cgi->a({-href => href(action=>"search", hash=>$hash,
4651 searchtext=>$searchtext, searchtype=>$searchtype,
4652 page=>$page-1),
4653 -accesskey => "p", -title => "Alt-p"}, "prev");
4654 } else {
4655 $paging_nav .= "first";
4656 $paging_nav .= " &sdot; prev";
4657 }
4658 if ($#commitlist >= 100) {
4659 $paging_nav .= " &sdot; " .
4660 $cgi->a({-href => href(action=>"search", hash=>$hash,
4661 searchtext=>$searchtext, searchtype=>$searchtype,
4662 page=>$page+1),
4663 -accesskey => "n", -title => "Alt-n"}, "next");
4664 } else {
4665 $paging_nav .= " &sdot; next";
4666 }
4667 my $next_link = '';
4668 if ($#commitlist >= 100) {
4669 $next_link =
4670 $cgi->a({-href => href(action=>"search", hash=>$hash,
4671 searchtext=>$searchtext, searchtype=>$searchtype,
4672 page=>$page+1),
4673 -accesskey => "n", -title => "Alt-n"}, "next");
4674 }
4675
4676 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
4677 git_print_header_div('commit', esc_html($co{'title'}), $hash);
4678 git_search_grep_body(\@commitlist, 0, 99, $next_link);
4679 }
4680
4681 if ($searchtype eq 'pickaxe') {
4682 git_print_page_nav('','', $hash,$co{'tree'},$hash);
4683 git_print_header_div('commit', esc_html($co{'title'}), $hash);
4684
4685 print "<table cellspacing=\"0\">\n";
4686 my $alternate = 1;
4687 $/ = "\n";
4688 my $git_command = git_cmd_str();
4689 open my $fd, "-|", "$git_command rev-list $hash | " .
4690 "$git_command diff-tree -r --stdin -S\'$searchtext\'";
4691 undef %co;
4692 my @files;
4693 while (my $line = <$fd>) {
4694 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
4695 my %set;
4696 $set{'file'} = $6;
4697 $set{'from_id'} = $3;
4698 $set{'to_id'} = $4;
4699 $set{'id'} = $set{'to_id'};
4700 if ($set{'id'} =~ m/0{40}/) {
4701 $set{'id'} = $set{'from_id'};
4702 }
4703 if ($set{'id'} =~ m/0{40}/) {
4704 next;
4705 }
4706 push @files, \%set;
4707 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
4708 if (%co) {
4709 if ($alternate) {
4710 print "<tr class=\"dark\">\n";
4711 } else {
4712 print "<tr class=\"light\">\n";
4713 }
4714 $alternate ^= 1;
4715 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4716 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
4717 "<td>" .
4718 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
4719 -class => "list subject"},
4720 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
4721 while (my $setref = shift @files) {
4722 my %set = %$setref;
4723 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
4724 hash=>$set{'id'}, file_name=>$set{'file'}),
4725 -class => "list"},
4726 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
4727 "<br/>\n";
4728 }
4729 print "</td>\n" .
4730 "<td class=\"link\">" .
4731 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
4732 " | " .
4733 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
4734 print "</td>\n" .
4735 "</tr>\n";
4736 }
4737 %co = parse_commit($1);
4738 }
4739 }
4740 close $fd;
4741
4742 print "</table>\n";
4743 }
4744 git_footer_html();
4745 }
4746
4747 sub git_search_help {
4748 git_header_html();
4749 git_print_page_nav('','', $hash,$hash,$hash);
4750 print <<EOT;
4751 <dl>
4752 <dt><b>commit</b></dt>
4753 <dd>The commit messages and authorship information will be scanned for the given string.</dd>
4754 <dt><b>author</b></dt>
4755 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given string.</dd>
4756 <dt><b>committer</b></dt>
4757 <dd>Name and e-mail of the committer and date of commit will be scanned for the given string.</dd>
4758 EOT
4759 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
4760 if ($have_pickaxe) {
4761 print <<EOT;
4762 <dt><b>pickaxe</b></dt>
4763 <dd>All commits that caused the string to appear or disappear from any file (changes that
4764 added, removed or "modified" the string) will be listed. This search can take a while and
4765 takes a lot of strain on the server, so please use it wisely.</dd>
4766 EOT
4767 }
4768 print "</dl>\n";
4769 git_footer_html();
4770 }
4771
4772 sub git_shortlog {
4773 my $head = git_get_head_hash($project);
4774 if (!defined $hash) {
4775 $hash = $head;
4776 }
4777 if (!defined $page) {
4778 $page = 0;
4779 }
4780 my $refs = git_get_references();
4781
4782 my @commitlist = parse_commits($hash, 101, (100 * $page));
4783
4784 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, (100 * ($page+1)));
4785 my $next_link = '';
4786 if ($#commitlist >= 100) {
4787 $next_link =
4788 $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
4789 -accesskey => "n", -title => "Alt-n"}, "next");
4790 }
4791
4792 git_header_html();
4793 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
4794 git_print_header_div('summary', $project);
4795
4796 git_shortlog_body(\@commitlist, 0, 99, $refs, $next_link);
4797
4798 git_footer_html();
4799 }
4800
4801 ## ......................................................................
4802 ## feeds (RSS, Atom; OPML)
4803
4804 sub git_feed {
4805 my $format = shift || 'atom';
4806 my ($have_blame) = gitweb_check_feature('blame');
4807
4808 # Atom: http://www.atomenabled.org/developers/syndication/
4809 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
4810 if ($format ne 'rss' && $format ne 'atom') {
4811 die_error(undef, "Unknown web feed format");
4812 }
4813
4814 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
4815 my $head = $hash || 'HEAD';
4816 my @commitlist = parse_commits($head, 150);
4817
4818 my %latest_commit;
4819 my %latest_date;
4820 my $content_type = "application/$format+xml";
4821 if (defined $cgi->http('HTTP_ACCEPT') &&
4822 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
4823 # browser (feed reader) prefers text/xml
4824 $content_type = 'text/xml';
4825 }
4826 if (defined($commitlist[0])) {
4827 %latest_commit = %{$commitlist[0]};
4828 %latest_date = parse_date($latest_commit{'author_epoch'});
4829 print $cgi->header(
4830 -type => $content_type,
4831 -charset => 'utf-8',
4832 -last_modified => $latest_date{'rfc2822'});
4833 } else {
4834 print $cgi->header(
4835 -type => $content_type,
4836 -charset => 'utf-8');
4837 }
4838
4839 # Optimization: skip generating the body if client asks only
4840 # for Last-Modified date.
4841 return if ($cgi->request_method() eq 'HEAD');
4842
4843 # header variables
4844 my $title = "$site_name - $project/$action";
4845 my $feed_type = 'log';
4846 if (defined $hash) {
4847 $title .= " - '$hash'";
4848 $feed_type = 'branch log';
4849 if (defined $file_name) {
4850 $title .= " :: $file_name";
4851 $feed_type = 'history';
4852 }
4853 } elsif (defined $file_name) {
4854 $title .= " - $file_name";
4855 $feed_type = 'history';
4856 }
4857 $title .= " $feed_type";
4858 my $descr = git_get_project_description($project);
4859 if (defined $descr) {
4860 $descr = esc_html($descr);
4861 } else {
4862 $descr = "$project " .
4863 ($format eq 'rss' ? 'RSS' : 'Atom') .
4864 " feed";
4865 }
4866 my $owner = git_get_project_owner($project);
4867 $owner = esc_html($owner);
4868
4869 #header
4870 my $alt_url;
4871 if (defined $file_name) {
4872 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
4873 } elsif (defined $hash) {
4874 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
4875 } else {
4876 $alt_url = href(-full=>1, action=>"summary");
4877 }
4878 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
4879 if ($format eq 'rss') {
4880 print <<XML;
4881 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
4882 <channel>
4883 XML
4884 print "<title>$title</title>\n" .
4885 "<link>$alt_url</link>\n" .
4886 "<description>$descr</description>\n" .
4887 "<language>en</language>\n";
4888 } elsif ($format eq 'atom') {
4889 print <<XML;
4890 <feed xmlns="http://www.w3.org/2005/Atom">
4891 XML
4892 print "<title>$title</title>\n" .
4893 "<subtitle>$descr</subtitle>\n" .
4894 '<link rel="alternate" type="text/html" href="' .
4895 $alt_url . '" />' . "\n" .
4896 '<link rel="self" type="' . $content_type . '" href="' .
4897 $cgi->self_url() . '" />' . "\n" .
4898 "<id>" . href(-full=>1) . "</id>\n" .
4899 # use project owner for feed author
4900 "<author><name>$owner</name></author>\n";
4901 if (defined $favicon) {
4902 print "<icon>" . esc_url($favicon) . "</icon>\n";
4903 }
4904 if (defined $logo_url) {
4905 # not twice as wide as tall: 72 x 27 pixels
4906 print "<logo>" . esc_url($logo) . "</logo>\n";
4907 }
4908 if (! %latest_date) {
4909 # dummy date to keep the feed valid until commits trickle in:
4910 print "<updated>1970-01-01T00:00:00Z</updated>\n";
4911 } else {
4912 print "<updated>$latest_date{'iso-8601'}</updated>\n";
4913 }
4914 }
4915
4916 # contents
4917 for (my $i = 0; $i <= $#commitlist; $i++) {
4918 my %co = %{$commitlist[$i]};
4919 my $commit = $co{'id'};
4920 # we read 150, we always show 30 and the ones more recent than 48 hours
4921 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
4922 last;
4923 }
4924 my %cd = parse_date($co{'author_epoch'});
4925
4926 # get list of changed files
4927 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4928 $co{'parent'}, $co{'id'}, "--", (defined $file_name ? $file_name : ())
4929 or next;
4930 my @difftree = map { chomp; $_ } <$fd>;
4931 close $fd
4932 or next;
4933
4934 # print element (entry, item)
4935 my $co_url = href(-full=>1, action=>"commit", hash=>$commit);
4936 if ($format eq 'rss') {
4937 print "<item>\n" .
4938 "<title>" . esc_html($co{'title'}) . "</title>\n" .
4939 "<author>" . esc_html($co{'author'}) . "</author>\n" .
4940 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
4941 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
4942 "<link>$co_url</link>\n" .
4943 "<description>" . esc_html($co{'title'}) . "</description>\n" .
4944 "<content:encoded>" .
4945 "<![CDATA[\n";
4946 } elsif ($format eq 'atom') {
4947 print "<entry>\n" .
4948 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
4949 "<updated>$cd{'iso-8601'}</updated>\n" .
4950 "<author>\n" .
4951 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
4952 if ($co{'author_email'}) {
4953 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
4954 }
4955 print "</author>\n" .
4956 # use committer for contributor
4957 "<contributor>\n" .
4958 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
4959 if ($co{'committer_email'}) {
4960 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
4961 }
4962 print "</contributor>\n" .
4963 "<published>$cd{'iso-8601'}</published>\n" .
4964 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
4965 "<id>$co_url</id>\n" .
4966 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
4967 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
4968 }
4969 my $comment = $co{'comment'};
4970 print "<pre>\n";
4971 foreach my $line (@$comment) {
4972 $line = esc_html($line);
4973 print "$line\n";
4974 }
4975 print "</pre><ul>\n";
4976 foreach my $difftree_line (@difftree) {
4977 my %difftree = parse_difftree_raw_line($difftree_line);
4978 next if !$difftree{'from_id'};
4979
4980 my $file = $difftree{'file'} || $difftree{'to_file'};
4981
4982 print "<li>" .
4983 "[" .
4984 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
4985 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
4986 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
4987 file_name=>$file, file_parent=>$difftree{'from_file'}),
4988 -title => "diff"}, 'D');
4989 if ($have_blame) {
4990 print $cgi->a({-href => href(-full=>1, action=>"blame",
4991 file_name=>$file, hash_base=>$commit),
4992 -title => "blame"}, 'B');
4993 }
4994 # if this is not a feed of a file history
4995 if (!defined $file_name || $file_name ne $file) {
4996 print $cgi->a({-href => href(-full=>1, action=>"history",
4997 file_name=>$file, hash=>$commit),
4998 -title => "history"}, 'H');
4999 }
5000 $file = esc_path($file);
5001 print "] ".
5002 "$file</li>\n";
5003 }
5004 if ($format eq 'rss') {
5005 print "</ul>]]>\n" .
5006 "</content:encoded>\n" .
5007 "</item>\n";
5008 } elsif ($format eq 'atom') {
5009 print "</ul>\n</div>\n" .
5010 "</content>\n" .
5011 "</entry>\n";
5012 }
5013 }
5014
5015 # end of feed
5016 if ($format eq 'rss') {
5017 print "</channel>\n</rss>\n";
5018 } elsif ($format eq 'atom') {
5019 print "</feed>\n";
5020 }
5021 }
5022
5023 sub git_rss {
5024 git_feed('rss');
5025 }
5026
5027 sub git_atom {
5028 git_feed('atom');
5029 }
5030
5031 sub git_opml {
5032 my @list = git_get_projects_list();
5033
5034 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
5035 print <<XML;
5036 <?xml version="1.0" encoding="utf-8"?>
5037 <opml version="1.0">
5038 <head>
5039 <title>$site_name OPML Export</title>
5040 </head>
5041 <body>
5042 <outline text="git RSS feeds">
5043 XML
5044
5045 foreach my $pr (@list) {
5046 my %proj = %$pr;
5047 my $head = git_get_head_hash($proj{'path'});
5048 if (!defined $head) {
5049 next;
5050 }
5051 $git_dir = "$projectroot/$proj{'path'}";
5052 my %co = parse_commit($head);
5053 if (!%co) {
5054 next;
5055 }
5056
5057 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
5058 my $rss = "$my_url?p=$proj{'path'};a=rss";
5059 my $html = "$my_url?p=$proj{'path'};a=summary";
5060 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
5061 }
5062 print <<XML;
5063 </outline>
5064 </body>
5065 </opml>
5066 XML
5067 }
This page took 4.539606 seconds and 5 git commands to generate.