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