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