]> Lady’s Gitweb - Gitweb/blob - gitweb.perl
gitweb: Update config file example for snapshot feature in gitweb/INSTALL
[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'} = $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{'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'} = [ split('', $4) ];
2011 $res{'to_file'} = unquote($5);
2012 }
2013 # 'c512b523472485aef4fff9e57b229d9d243c967f'
2014 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
2015 $res{'commit'} = $1;
2016 }
2017
2018 return wantarray ? %res : \%res;
2019 }
2020
2021 # wrapper: return parsed line of git-diff-tree "raw" output
2022 # (the argument might be raw line, or parsed info)
2023 sub parsed_difftree_line {
2024 my $line_or_ref = shift;
2025
2026 if (ref($line_or_ref) eq "HASH") {
2027 # pre-parsed (or generated by hand)
2028 return $line_or_ref;
2029 } else {
2030 return parse_difftree_raw_line($line_or_ref);
2031 }
2032 }
2033
2034 # parse line of git-ls-tree output
2035 sub parse_ls_tree_line ($;%) {
2036 my $line = shift;
2037 my %opts = @_;
2038 my %res;
2039
2040 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2041 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
2042
2043 $res{'mode'} = $1;
2044 $res{'type'} = $2;
2045 $res{'hash'} = $3;
2046 if ($opts{'-z'}) {
2047 $res{'name'} = $4;
2048 } else {
2049 $res{'name'} = unquote($4);
2050 }
2051
2052 return wantarray ? %res : \%res;
2053 }
2054
2055 # generates _two_ hashes, references to which are passed as 2 and 3 argument
2056 sub parse_from_to_diffinfo {
2057 my ($diffinfo, $from, $to, @parents) = @_;
2058
2059 if ($diffinfo->{'nparents'}) {
2060 # combined diff
2061 $from->{'file'} = [];
2062 $from->{'href'} = [];
2063 fill_from_file_info($diffinfo, @parents)
2064 unless exists $diffinfo->{'from_file'};
2065 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2066 $from->{'file'}[$i] = $diffinfo->{'from_file'}[$i] || $diffinfo->{'to_file'};
2067 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
2068 $from->{'href'}[$i] = href(action=>"blob",
2069 hash_base=>$parents[$i],
2070 hash=>$diffinfo->{'from_id'}[$i],
2071 file_name=>$from->{'file'}[$i]);
2072 } else {
2073 $from->{'href'}[$i] = undef;
2074 }
2075 }
2076 } else {
2077 # ordinary (not combined) diff
2078 $from->{'file'} = $diffinfo->{'from_file'} || $diffinfo->{'file'};
2079 if ($diffinfo->{'status'} ne "A") { # not new (added) file
2080 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2081 hash=>$diffinfo->{'from_id'},
2082 file_name=>$from->{'file'});
2083 } else {
2084 delete $from->{'href'};
2085 }
2086 }
2087
2088 $to->{'file'} = $diffinfo->{'to_file'} || $diffinfo->{'file'};
2089 if (!is_deleted($diffinfo)) { # file exists in result
2090 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
2091 hash=>$diffinfo->{'to_id'},
2092 file_name=>$to->{'file'});
2093 } else {
2094 delete $to->{'href'};
2095 }
2096 }
2097
2098 ## ......................................................................
2099 ## parse to array of hashes functions
2100
2101 sub git_get_heads_list {
2102 my $limit = shift;
2103 my @headslist;
2104
2105 open my $fd, '-|', git_cmd(), 'for-each-ref',
2106 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
2107 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
2108 'refs/heads'
2109 or return;
2110 while (my $line = <$fd>) {
2111 my %ref_item;
2112
2113 chomp $line;
2114 my ($refinfo, $committerinfo) = split(/\0/, $line);
2115 my ($hash, $name, $title) = split(' ', $refinfo, 3);
2116 my ($committer, $epoch, $tz) =
2117 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
2118 $name =~ s!^refs/heads/!!;
2119
2120 $ref_item{'name'} = $name;
2121 $ref_item{'id'} = $hash;
2122 $ref_item{'title'} = $title || '(no commit message)';
2123 $ref_item{'epoch'} = $epoch;
2124 if ($epoch) {
2125 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2126 } else {
2127 $ref_item{'age'} = "unknown";
2128 }
2129
2130 push @headslist, \%ref_item;
2131 }
2132 close $fd;
2133
2134 return wantarray ? @headslist : \@headslist;
2135 }
2136
2137 sub git_get_tags_list {
2138 my $limit = shift;
2139 my @tagslist;
2140
2141 open my $fd, '-|', git_cmd(), 'for-each-ref',
2142 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
2143 '--format=%(objectname) %(objecttype) %(refname) '.
2144 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
2145 'refs/tags'
2146 or return;
2147 while (my $line = <$fd>) {
2148 my %ref_item;
2149
2150 chomp $line;
2151 my ($refinfo, $creatorinfo) = split(/\0/, $line);
2152 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
2153 my ($creator, $epoch, $tz) =
2154 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
2155 $name =~ s!^refs/tags/!!;
2156
2157 $ref_item{'type'} = $type;
2158 $ref_item{'id'} = $id;
2159 $ref_item{'name'} = $name;
2160 if ($type eq "tag") {
2161 $ref_item{'subject'} = $title;
2162 $ref_item{'reftype'} = $reftype;
2163 $ref_item{'refid'} = $refid;
2164 } else {
2165 $ref_item{'reftype'} = $type;
2166 $ref_item{'refid'} = $id;
2167 }
2168
2169 if ($type eq "tag" || $type eq "commit") {
2170 $ref_item{'epoch'} = $epoch;
2171 if ($epoch) {
2172 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2173 } else {
2174 $ref_item{'age'} = "unknown";
2175 }
2176 }
2177
2178 push @tagslist, \%ref_item;
2179 }
2180 close $fd;
2181
2182 return wantarray ? @tagslist : \@tagslist;
2183 }
2184
2185 ## ----------------------------------------------------------------------
2186 ## filesystem-related functions
2187
2188 sub get_file_owner {
2189 my $path = shift;
2190
2191 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
2192 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
2193 if (!defined $gcos) {
2194 return undef;
2195 }
2196 my $owner = $gcos;
2197 $owner =~ s/[,;].*$//;
2198 return to_utf8($owner);
2199 }
2200
2201 ## ......................................................................
2202 ## mimetype related functions
2203
2204 sub mimetype_guess_file {
2205 my $filename = shift;
2206 my $mimemap = shift;
2207 -r $mimemap or return undef;
2208
2209 my %mimemap;
2210 open(MIME, $mimemap) or return undef;
2211 while (<MIME>) {
2212 next if m/^#/; # skip comments
2213 my ($mime, $exts) = split(/\t+/);
2214 if (defined $exts) {
2215 my @exts = split(/\s+/, $exts);
2216 foreach my $ext (@exts) {
2217 $mimemap{$ext} = $mime;
2218 }
2219 }
2220 }
2221 close(MIME);
2222
2223 $filename =~ /\.([^.]*)$/;
2224 return $mimemap{$1};
2225 }
2226
2227 sub mimetype_guess {
2228 my $filename = shift;
2229 my $mime;
2230 $filename =~ /\./ or return undef;
2231
2232 if ($mimetypes_file) {
2233 my $file = $mimetypes_file;
2234 if ($file !~ m!^/!) { # if it is relative path
2235 # it is relative to project
2236 $file = "$projectroot/$project/$file";
2237 }
2238 $mime = mimetype_guess_file($filename, $file);
2239 }
2240 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
2241 return $mime;
2242 }
2243
2244 sub blob_mimetype {
2245 my $fd = shift;
2246 my $filename = shift;
2247
2248 if ($filename) {
2249 my $mime = mimetype_guess($filename);
2250 $mime and return $mime;
2251 }
2252
2253 # just in case
2254 return $default_blob_plain_mimetype unless $fd;
2255
2256 if (-T $fd) {
2257 return 'text/plain' .
2258 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
2259 } elsif (! $filename) {
2260 return 'application/octet-stream';
2261 } elsif ($filename =~ m/\.png$/i) {
2262 return 'image/png';
2263 } elsif ($filename =~ m/\.gif$/i) {
2264 return 'image/gif';
2265 } elsif ($filename =~ m/\.jpe?g$/i) {
2266 return 'image/jpeg';
2267 } else {
2268 return 'application/octet-stream';
2269 }
2270 }
2271
2272 ## ======================================================================
2273 ## functions printing HTML: header, footer, error page
2274
2275 sub git_header_html {
2276 my $status = shift || "200 OK";
2277 my $expires = shift;
2278
2279 my $title = "$site_name";
2280 if (defined $project) {
2281 $title .= " - " . to_utf8($project);
2282 if (defined $action) {
2283 $title .= "/$action";
2284 if (defined $file_name) {
2285 $title .= " - " . esc_path($file_name);
2286 if ($action eq "tree" && $file_name !~ m|/$|) {
2287 $title .= "/";
2288 }
2289 }
2290 }
2291 }
2292 my $content_type;
2293 # require explicit support from the UA if we are to send the page as
2294 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
2295 # we have to do this because MSIE sometimes globs '*/*', pretending to
2296 # support xhtml+xml but choking when it gets what it asked for.
2297 if (defined $cgi->http('HTTP_ACCEPT') &&
2298 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
2299 $cgi->Accept('application/xhtml+xml') != 0) {
2300 $content_type = 'application/xhtml+xml';
2301 } else {
2302 $content_type = 'text/html';
2303 }
2304 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
2305 -status=> $status, -expires => $expires);
2306 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
2307 print <<EOF;
2308 <?xml version="1.0" encoding="utf-8"?>
2309 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2310 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
2311 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
2312 <!-- git core binaries version $git_version -->
2313 <head>
2314 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
2315 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
2316 <meta name="robots" content="index, nofollow"/>
2317 <title>$title</title>
2318 EOF
2319 # print out each stylesheet that exist
2320 if (defined $stylesheet) {
2321 #provides backwards capability for those people who define style sheet in a config file
2322 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2323 } else {
2324 foreach my $stylesheet (@stylesheets) {
2325 next unless $stylesheet;
2326 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2327 }
2328 }
2329 if (defined $project) {
2330 printf('<link rel="alternate" title="%s log RSS feed" '.
2331 'href="%s" type="application/rss+xml" />'."\n",
2332 esc_param($project), href(action=>"rss"));
2333 printf('<link rel="alternate" title="%s log RSS feed (no merges)" '.
2334 'href="%s" type="application/rss+xml" />'."\n",
2335 esc_param($project), href(action=>"rss",
2336 extra_options=>"--no-merges"));
2337 printf('<link rel="alternate" title="%s log Atom feed" '.
2338 'href="%s" type="application/atom+xml" />'."\n",
2339 esc_param($project), href(action=>"atom"));
2340 printf('<link rel="alternate" title="%s log Atom feed (no merges)" '.
2341 'href="%s" type="application/atom+xml" />'."\n",
2342 esc_param($project), href(action=>"atom",
2343 extra_options=>"--no-merges"));
2344 } else {
2345 printf('<link rel="alternate" title="%s projects list" '.
2346 'href="%s" type="text/plain; charset=utf-8"/>'."\n",
2347 $site_name, href(project=>undef, action=>"project_index"));
2348 printf('<link rel="alternate" title="%s projects feeds" '.
2349 'href="%s" type="text/x-opml"/>'."\n",
2350 $site_name, href(project=>undef, action=>"opml"));
2351 }
2352 if (defined $favicon) {
2353 print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n);
2354 }
2355
2356 print "</head>\n" .
2357 "<body>\n";
2358
2359 if (-f $site_header) {
2360 open (my $fd, $site_header);
2361 print <$fd>;
2362 close $fd;
2363 }
2364
2365 print "<div class=\"page_header\">\n" .
2366 $cgi->a({-href => esc_url($logo_url),
2367 -title => $logo_label},
2368 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
2369 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
2370 if (defined $project) {
2371 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
2372 if (defined $action) {
2373 print " / $action";
2374 }
2375 print "\n";
2376 }
2377 print "</div>\n";
2378
2379 my ($have_search) = gitweb_check_feature('search');
2380 if ((defined $project) && ($have_search)) {
2381 if (!defined $searchtext) {
2382 $searchtext = "";
2383 }
2384 my $search_hash;
2385 if (defined $hash_base) {
2386 $search_hash = $hash_base;
2387 } elsif (defined $hash) {
2388 $search_hash = $hash;
2389 } else {
2390 $search_hash = "HEAD";
2391 }
2392 my $action = $my_uri;
2393 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
2394 if ($use_pathinfo) {
2395 $action .= "/$project";
2396 } else {
2397 $cgi->param("p", $project);
2398 }
2399 $cgi->param("a", "search");
2400 $cgi->param("h", $search_hash);
2401 print $cgi->startform(-method => "get", -action => $action) .
2402 "<div class=\"search\">\n" .
2403 (!$use_pathinfo && $cgi->hidden(-name => "p") . "\n") .
2404 $cgi->hidden(-name => "a") . "\n" .
2405 $cgi->hidden(-name => "h") . "\n" .
2406 $cgi->popup_menu(-name => 'st', -default => 'commit',
2407 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
2408 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
2409 " search:\n",
2410 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
2411 "</div>" .
2412 $cgi->end_form() . "\n";
2413 }
2414 }
2415
2416 sub git_footer_html {
2417 print "<div class=\"page_footer\">\n";
2418 if (defined $project) {
2419 my $descr = git_get_project_description($project);
2420 if (defined $descr) {
2421 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
2422 }
2423 print $cgi->a({-href => href(action=>"rss"),
2424 -class => "rss_logo"}, "RSS") . " ";
2425 print $cgi->a({-href => href(action=>"atom"),
2426 -class => "rss_logo"}, "Atom") . "\n";
2427 } else {
2428 print $cgi->a({-href => href(project=>undef, action=>"opml"),
2429 -class => "rss_logo"}, "OPML") . " ";
2430 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
2431 -class => "rss_logo"}, "TXT") . "\n";
2432 }
2433 print "</div>\n" ;
2434
2435 if (-f $site_footer) {
2436 open (my $fd, $site_footer);
2437 print <$fd>;
2438 close $fd;
2439 }
2440
2441 print "</body>\n" .
2442 "</html>";
2443 }
2444
2445 sub die_error {
2446 my $status = shift || "403 Forbidden";
2447 my $error = shift || "Malformed query, file missing or permission denied";
2448
2449 git_header_html($status);
2450 print <<EOF;
2451 <div class="page_body">
2452 <br /><br />
2453 $status - $error
2454 <br />
2455 </div>
2456 EOF
2457 git_footer_html();
2458 exit;
2459 }
2460
2461 ## ----------------------------------------------------------------------
2462 ## functions printing or outputting HTML: navigation
2463
2464 sub git_print_page_nav {
2465 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
2466 $extra = '' if !defined $extra; # pager or formats
2467
2468 my @navs = qw(summary shortlog log commit commitdiff tree);
2469 if ($suppress) {
2470 @navs = grep { $_ ne $suppress } @navs;
2471 }
2472
2473 my %arg = map { $_ => {action=>$_} } @navs;
2474 if (defined $head) {
2475 for (qw(commit commitdiff)) {
2476 $arg{$_}{'hash'} = $head;
2477 }
2478 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
2479 for (qw(shortlog log)) {
2480 $arg{$_}{'hash'} = $head;
2481 }
2482 }
2483 }
2484 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
2485 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
2486
2487 print "<div class=\"page_nav\">\n" .
2488 (join " | ",
2489 map { $_ eq $current ?
2490 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
2491 } @navs);
2492 print "<br/>\n$extra<br/>\n" .
2493 "</div>\n";
2494 }
2495
2496 sub format_paging_nav {
2497 my ($action, $hash, $head, $page, $nrevs) = @_;
2498 my $paging_nav;
2499
2500
2501 if ($hash ne $head || $page) {
2502 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
2503 } else {
2504 $paging_nav .= "HEAD";
2505 }
2506
2507 if ($page > 0) {
2508 $paging_nav .= " &sdot; " .
2509 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
2510 -accesskey => "p", -title => "Alt-p"}, "prev");
2511 } else {
2512 $paging_nav .= " &sdot; prev";
2513 }
2514
2515 if ($nrevs >= (100 * ($page+1)-1)) {
2516 $paging_nav .= " &sdot; " .
2517 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
2518 -accesskey => "n", -title => "Alt-n"}, "next");
2519 } else {
2520 $paging_nav .= " &sdot; next";
2521 }
2522
2523 return $paging_nav;
2524 }
2525
2526 ## ......................................................................
2527 ## functions printing or outputting HTML: div
2528
2529 sub git_print_header_div {
2530 my ($action, $title, $hash, $hash_base) = @_;
2531 my %args = ();
2532
2533 $args{'action'} = $action;
2534 $args{'hash'} = $hash if $hash;
2535 $args{'hash_base'} = $hash_base if $hash_base;
2536
2537 print "<div class=\"header\">\n" .
2538 $cgi->a({-href => href(%args), -class => "title"},
2539 $title ? $title : $action) .
2540 "\n</div>\n";
2541 }
2542
2543 #sub git_print_authorship (\%) {
2544 sub git_print_authorship {
2545 my $co = shift;
2546
2547 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
2548 print "<div class=\"author_date\">" .
2549 esc_html($co->{'author_name'}) .
2550 " [$ad{'rfc2822'}";
2551 if ($ad{'hour_local'} < 6) {
2552 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2553 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2554 } else {
2555 printf(" (%02d:%02d %s)",
2556 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2557 }
2558 print "]</div>\n";
2559 }
2560
2561 sub git_print_page_path {
2562 my $name = shift;
2563 my $type = shift;
2564 my $hb = shift;
2565
2566
2567 print "<div class=\"page_path\">";
2568 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
2569 -title => 'tree root'}, to_utf8("[$project]"));
2570 print " / ";
2571 if (defined $name) {
2572 my @dirname = split '/', $name;
2573 my $basename = pop @dirname;
2574 my $fullname = '';
2575
2576 foreach my $dir (@dirname) {
2577 $fullname .= ($fullname ? '/' : '') . $dir;
2578 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
2579 hash_base=>$hb),
2580 -title => $fullname}, esc_path($dir));
2581 print " / ";
2582 }
2583 if (defined $type && $type eq 'blob') {
2584 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
2585 hash_base=>$hb),
2586 -title => $name}, esc_path($basename));
2587 } elsif (defined $type && $type eq 'tree') {
2588 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
2589 hash_base=>$hb),
2590 -title => $name}, esc_path($basename));
2591 print " / ";
2592 } else {
2593 print esc_path($basename);
2594 }
2595 }
2596 print "<br/></div>\n";
2597 }
2598
2599 # sub git_print_log (\@;%) {
2600 sub git_print_log ($;%) {
2601 my $log = shift;
2602 my %opts = @_;
2603
2604 if ($opts{'-remove_title'}) {
2605 # remove title, i.e. first line of log
2606 shift @$log;
2607 }
2608 # remove leading empty lines
2609 while (defined $log->[0] && $log->[0] eq "") {
2610 shift @$log;
2611 }
2612
2613 # print log
2614 my $signoff = 0;
2615 my $empty = 0;
2616 foreach my $line (@$log) {
2617 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
2618 $signoff = 1;
2619 $empty = 0;
2620 if (! $opts{'-remove_signoff'}) {
2621 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
2622 next;
2623 } else {
2624 # remove signoff lines
2625 next;
2626 }
2627 } else {
2628 $signoff = 0;
2629 }
2630
2631 # print only one empty line
2632 # do not print empty line after signoff
2633 if ($line eq "") {
2634 next if ($empty || $signoff);
2635 $empty = 1;
2636 } else {
2637 $empty = 0;
2638 }
2639
2640 print format_log_line_html($line) . "<br/>\n";
2641 }
2642
2643 if ($opts{'-final_empty_line'}) {
2644 # end with single empty line
2645 print "<br/>\n" unless $empty;
2646 }
2647 }
2648
2649 # return link target (what link points to)
2650 sub git_get_link_target {
2651 my $hash = shift;
2652 my $link_target;
2653
2654 # read link
2655 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2656 or return;
2657 {
2658 local $/;
2659 $link_target = <$fd>;
2660 }
2661 close $fd
2662 or return;
2663
2664 return $link_target;
2665 }
2666
2667 # given link target, and the directory (basedir) the link is in,
2668 # return target of link relative to top directory (top tree);
2669 # return undef if it is not possible (including absolute links).
2670 sub normalize_link_target {
2671 my ($link_target, $basedir, $hash_base) = @_;
2672
2673 # we can normalize symlink target only if $hash_base is provided
2674 return unless $hash_base;
2675
2676 # absolute symlinks (beginning with '/') cannot be normalized
2677 return if (substr($link_target, 0, 1) eq '/');
2678
2679 # normalize link target to path from top (root) tree (dir)
2680 my $path;
2681 if ($basedir) {
2682 $path = $basedir . '/' . $link_target;
2683 } else {
2684 # we are in top (root) tree (dir)
2685 $path = $link_target;
2686 }
2687
2688 # remove //, /./, and /../
2689 my @path_parts;
2690 foreach my $part (split('/', $path)) {
2691 # discard '.' and ''
2692 next if (!$part || $part eq '.');
2693 # handle '..'
2694 if ($part eq '..') {
2695 if (@path_parts) {
2696 pop @path_parts;
2697 } else {
2698 # link leads outside repository (outside top dir)
2699 return;
2700 }
2701 } else {
2702 push @path_parts, $part;
2703 }
2704 }
2705 $path = join('/', @path_parts);
2706
2707 return $path;
2708 }
2709
2710 # print tree entry (row of git_tree), but without encompassing <tr> element
2711 sub git_print_tree_entry {
2712 my ($t, $basedir, $hash_base, $have_blame) = @_;
2713
2714 my %base_key = ();
2715 $base_key{'hash_base'} = $hash_base if defined $hash_base;
2716
2717 # The format of a table row is: mode list link. Where mode is
2718 # the mode of the entry, list is the name of the entry, an href,
2719 # and link is the action links of the entry.
2720
2721 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
2722 if ($t->{'type'} eq "blob") {
2723 print "<td class=\"list\">" .
2724 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
2725 file_name=>"$basedir$t->{'name'}", %base_key),
2726 -class => "list"}, esc_path($t->{'name'}));
2727 if (S_ISLNK(oct $t->{'mode'})) {
2728 my $link_target = git_get_link_target($t->{'hash'});
2729 if ($link_target) {
2730 my $norm_target = normalize_link_target($link_target, $basedir, $hash_base);
2731 if (defined $norm_target) {
2732 print " -> " .
2733 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
2734 file_name=>$norm_target),
2735 -title => $norm_target}, esc_path($link_target));
2736 } else {
2737 print " -> " . esc_path($link_target);
2738 }
2739 }
2740 }
2741 print "</td>\n";
2742 print "<td class=\"link\">";
2743 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
2744 file_name=>"$basedir$t->{'name'}", %base_key)},
2745 "blob");
2746 if ($have_blame) {
2747 print " | " .
2748 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
2749 file_name=>"$basedir$t->{'name'}", %base_key)},
2750 "blame");
2751 }
2752 if (defined $hash_base) {
2753 print " | " .
2754 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2755 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
2756 "history");
2757 }
2758 print " | " .
2759 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
2760 file_name=>"$basedir$t->{'name'}")},
2761 "raw");
2762 print "</td>\n";
2763
2764 } elsif ($t->{'type'} eq "tree") {
2765 print "<td class=\"list\">";
2766 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
2767 file_name=>"$basedir$t->{'name'}", %base_key)},
2768 esc_path($t->{'name'}));
2769 print "</td>\n";
2770 print "<td class=\"link\">";
2771 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
2772 file_name=>"$basedir$t->{'name'}", %base_key)},
2773 "tree");
2774 if (defined $hash_base) {
2775 print " | " .
2776 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2777 file_name=>"$basedir$t->{'name'}")},
2778 "history");
2779 }
2780 print "</td>\n";
2781 } else {
2782 # unknown object: we can only present history for it
2783 # (this includes 'commit' object, i.e. submodule support)
2784 print "<td class=\"list\">" .
2785 esc_path($t->{'name'}) .
2786 "</td>\n";
2787 print "<td class=\"link\">";
2788 if (defined $hash_base) {
2789 print $cgi->a({-href => href(action=>"history",
2790 hash_base=>$hash_base,
2791 file_name=>"$basedir$t->{'name'}")},
2792 "history");
2793 }
2794 print "</td>\n";
2795 }
2796 }
2797
2798 ## ......................................................................
2799 ## functions printing large fragments of HTML
2800
2801 # get pre-image filenames for merge (combined) diff
2802 sub fill_from_file_info {
2803 my ($diff, @parents) = @_;
2804
2805 $diff->{'from_file'} = [ ];
2806 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
2807 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
2808 if ($diff->{'status'}[$i] eq 'R' ||
2809 $diff->{'status'}[$i] eq 'C') {
2810 $diff->{'from_file'}[$i] =
2811 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
2812 }
2813 }
2814
2815 return $diff;
2816 }
2817
2818 # is current raw difftree line of file deletion
2819 sub is_deleted {
2820 my $diffinfo = shift;
2821
2822 return $diffinfo->{'to_id'} eq ('0' x 40);
2823 }
2824
2825 # does patch correspond to [previous] difftree raw line
2826 # $diffinfo - hashref of parsed raw diff format
2827 # $patchinfo - hashref of parsed patch diff format
2828 # (the same keys as in $diffinfo)
2829 sub is_patch_split {
2830 my ($diffinfo, $patchinfo) = @_;
2831
2832 return defined $diffinfo && defined $patchinfo
2833 && ($diffinfo->{'to_file'} || $diffinfo->{'file'}) eq $patchinfo->{'to_file'};
2834 }
2835
2836
2837 sub git_difftree_body {
2838 my ($difftree, $hash, @parents) = @_;
2839 my ($parent) = $parents[0];
2840 my ($have_blame) = gitweb_check_feature('blame');
2841 print "<div class=\"list_head\">\n";
2842 if ($#{$difftree} > 10) {
2843 print(($#{$difftree} + 1) . " files changed:\n");
2844 }
2845 print "</div>\n";
2846
2847 print "<table class=\"" .
2848 (@parents > 1 ? "combined " : "") .
2849 "diff_tree\">\n";
2850
2851 # header only for combined diff in 'commitdiff' view
2852 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
2853 if ($has_header) {
2854 # table header
2855 print "<thead><tr>\n" .
2856 "<th></th><th></th>\n"; # filename, patchN link
2857 for (my $i = 0; $i < @parents; $i++) {
2858 my $par = $parents[$i];
2859 print "<th>" .
2860 $cgi->a({-href => href(action=>"commitdiff",
2861 hash=>$hash, hash_parent=>$par),
2862 -title => 'commitdiff to parent number ' .
2863 ($i+1) . ': ' . substr($par,0,7)},
2864 $i+1) .
2865 "&nbsp;</th>\n";
2866 }
2867 print "</tr></thead>\n<tbody>\n";
2868 }
2869
2870 my $alternate = 1;
2871 my $patchno = 0;
2872 foreach my $line (@{$difftree}) {
2873 my $diff = parsed_difftree_line($line);
2874
2875 if ($alternate) {
2876 print "<tr class=\"dark\">\n";
2877 } else {
2878 print "<tr class=\"light\">\n";
2879 }
2880 $alternate ^= 1;
2881
2882 if (exists $diff->{'nparents'}) { # combined diff
2883
2884 fill_from_file_info($diff, @parents)
2885 unless exists $diff->{'from_file'};
2886
2887 if (!is_deleted($diff)) {
2888 # file exists in the result (child) commit
2889 print "<td>" .
2890 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2891 file_name=>$diff->{'to_file'},
2892 hash_base=>$hash),
2893 -class => "list"}, esc_path($diff->{'to_file'})) .
2894 "</td>\n";
2895 } else {
2896 print "<td>" .
2897 esc_path($diff->{'to_file'}) .
2898 "</td>\n";
2899 }
2900
2901 if ($action eq 'commitdiff') {
2902 # link to patch
2903 $patchno++;
2904 print "<td class=\"link\">" .
2905 $cgi->a({-href => "#patch$patchno"}, "patch") .
2906 " | " .
2907 "</td>\n";
2908 }
2909
2910 my $has_history = 0;
2911 my $not_deleted = 0;
2912 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
2913 my $hash_parent = $parents[$i];
2914 my $from_hash = $diff->{'from_id'}[$i];
2915 my $from_path = $diff->{'from_file'}[$i];
2916 my $status = $diff->{'status'}[$i];
2917
2918 $has_history ||= ($status ne 'A');
2919 $not_deleted ||= ($status ne 'D');
2920
2921 if ($status eq 'A') {
2922 print "<td class=\"link\" align=\"right\"> | </td>\n";
2923 } elsif ($status eq 'D') {
2924 print "<td class=\"link\">" .
2925 $cgi->a({-href => href(action=>"blob",
2926 hash_base=>$hash,
2927 hash=>$from_hash,
2928 file_name=>$from_path)},
2929 "blob" . ($i+1)) .
2930 " | </td>\n";
2931 } else {
2932 if ($diff->{'to_id'} eq $from_hash) {
2933 print "<td class=\"link nochange\">";
2934 } else {
2935 print "<td class=\"link\">";
2936 }
2937 print $cgi->a({-href => href(action=>"blobdiff",
2938 hash=>$diff->{'to_id'},
2939 hash_parent=>$from_hash,
2940 hash_base=>$hash,
2941 hash_parent_base=>$hash_parent,
2942 file_name=>$diff->{'to_file'},
2943 file_parent=>$from_path)},
2944 "diff" . ($i+1)) .
2945 " | </td>\n";
2946 }
2947 }
2948
2949 print "<td class=\"link\">";
2950 if ($not_deleted) {
2951 print $cgi->a({-href => href(action=>"blob",
2952 hash=>$diff->{'to_id'},
2953 file_name=>$diff->{'to_file'},
2954 hash_base=>$hash)},
2955 "blob");
2956 print " | " if ($has_history);
2957 }
2958 if ($has_history) {
2959 print $cgi->a({-href => href(action=>"history",
2960 file_name=>$diff->{'to_file'},
2961 hash_base=>$hash)},
2962 "history");
2963 }
2964 print "</td>\n";
2965
2966 print "</tr>\n";
2967 next; # instead of 'else' clause, to avoid extra indent
2968 }
2969 # else ordinary diff
2970
2971 my ($to_mode_oct, $to_mode_str, $to_file_type);
2972 my ($from_mode_oct, $from_mode_str, $from_file_type);
2973 if ($diff->{'to_mode'} ne ('0' x 6)) {
2974 $to_mode_oct = oct $diff->{'to_mode'};
2975 if (S_ISREG($to_mode_oct)) { # only for regular file
2976 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
2977 }
2978 $to_file_type = file_type($diff->{'to_mode'});
2979 }
2980 if ($diff->{'from_mode'} ne ('0' x 6)) {
2981 $from_mode_oct = oct $diff->{'from_mode'};
2982 if (S_ISREG($to_mode_oct)) { # only for regular file
2983 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
2984 }
2985 $from_file_type = file_type($diff->{'from_mode'});
2986 }
2987
2988 if ($diff->{'status'} eq "A") { # created
2989 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
2990 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
2991 $mode_chng .= "]</span>";
2992 print "<td>";
2993 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2994 hash_base=>$hash, file_name=>$diff->{'file'}),
2995 -class => "list"}, esc_path($diff->{'file'}));
2996 print "</td>\n";
2997 print "<td>$mode_chng</td>\n";
2998 print "<td class=\"link\">";
2999 if ($action eq 'commitdiff') {
3000 # link to patch
3001 $patchno++;
3002 print $cgi->a({-href => "#patch$patchno"}, "patch");
3003 print " | ";
3004 }
3005 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3006 hash_base=>$hash, file_name=>$diff->{'file'})},
3007 "blob");
3008 print "</td>\n";
3009
3010 } elsif ($diff->{'status'} eq "D") { # deleted
3011 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
3012 print "<td>";
3013 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3014 hash_base=>$parent, file_name=>$diff->{'file'}),
3015 -class => "list"}, esc_path($diff->{'file'}));
3016 print "</td>\n";
3017 print "<td>$mode_chng</td>\n";
3018 print "<td class=\"link\">";
3019 if ($action eq 'commitdiff') {
3020 # link to patch
3021 $patchno++;
3022 print $cgi->a({-href => "#patch$patchno"}, "patch");
3023 print " | ";
3024 }
3025 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3026 hash_base=>$parent, file_name=>$diff->{'file'})},
3027 "blob") . " | ";
3028 if ($have_blame) {
3029 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
3030 file_name=>$diff->{'file'})},
3031 "blame") . " | ";
3032 }
3033 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
3034 file_name=>$diff->{'file'})},
3035 "history");
3036 print "</td>\n";
3037
3038 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
3039 my $mode_chnge = "";
3040 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3041 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
3042 if ($from_file_type ne $to_file_type) {
3043 $mode_chnge .= " from $from_file_type to $to_file_type";
3044 }
3045 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
3046 if ($from_mode_str && $to_mode_str) {
3047 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
3048 } elsif ($to_mode_str) {
3049 $mode_chnge .= " mode: $to_mode_str";
3050 }
3051 }
3052 $mode_chnge .= "]</span>\n";
3053 }
3054 print "<td>";
3055 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3056 hash_base=>$hash, file_name=>$diff->{'file'}),
3057 -class => "list"}, esc_path($diff->{'file'}));
3058 print "</td>\n";
3059 print "<td>$mode_chnge</td>\n";
3060 print "<td class=\"link\">";
3061 if ($action eq 'commitdiff') {
3062 # link to patch
3063 $patchno++;
3064 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3065 " | ";
3066 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
3067 # "commit" view and modified file (not onlu mode changed)
3068 print $cgi->a({-href => href(action=>"blobdiff",
3069 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
3070 hash_base=>$hash, hash_parent_base=>$parent,
3071 file_name=>$diff->{'file'})},
3072 "diff") .
3073 " | ";
3074 }
3075 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3076 hash_base=>$hash, file_name=>$diff->{'file'})},
3077 "blob") . " | ";
3078 if ($have_blame) {
3079 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
3080 file_name=>$diff->{'file'})},
3081 "blame") . " | ";
3082 }
3083 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
3084 file_name=>$diff->{'file'})},
3085 "history");
3086 print "</td>\n";
3087
3088 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
3089 my %status_name = ('R' => 'moved', 'C' => 'copied');
3090 my $nstatus = $status_name{$diff->{'status'}};
3091 my $mode_chng = "";
3092 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3093 # mode also for directories, so we cannot use $to_mode_str
3094 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
3095 }
3096 print "<td>" .
3097 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
3098 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
3099 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
3100 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
3101 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
3102 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
3103 -class => "list"}, esc_path($diff->{'from_file'})) .
3104 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
3105 "<td class=\"link\">";
3106 if ($action eq 'commitdiff') {
3107 # link to patch
3108 $patchno++;
3109 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3110 " | ";
3111 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
3112 # "commit" view and modified file (not only pure rename or copy)
3113 print $cgi->a({-href => href(action=>"blobdiff",
3114 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
3115 hash_base=>$hash, hash_parent_base=>$parent,
3116 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
3117 "diff") .
3118 " | ";
3119 }
3120 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3121 hash_base=>$parent, file_name=>$diff->{'to_file'})},
3122 "blob") . " | ";
3123 if ($have_blame) {
3124 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
3125 file_name=>$diff->{'to_file'})},
3126 "blame") . " | ";
3127 }
3128 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
3129 file_name=>$diff->{'to_file'})},
3130 "history");
3131 print "</td>\n";
3132
3133 } # we should not encounter Unmerged (U) or Unknown (X) status
3134 print "</tr>\n";
3135 }
3136 print "</tbody>" if $has_header;
3137 print "</table>\n";
3138 }
3139
3140 sub git_patchset_body {
3141 my ($fd, $difftree, $hash, @hash_parents) = @_;
3142 my ($hash_parent) = $hash_parents[0];
3143
3144 my $is_combined = (@hash_parents > 1);
3145 my $patch_idx = 0;
3146 my $patch_number = 0;
3147 my $patch_line;
3148 my $diffinfo;
3149 my $to_name;
3150 my (%from, %to);
3151
3152 print "<div class=\"patchset\">\n";
3153
3154 # skip to first patch
3155 while ($patch_line = <$fd>) {
3156 chomp $patch_line;
3157
3158 last if ($patch_line =~ m/^diff /);
3159 }
3160
3161 PATCH:
3162 while ($patch_line) {
3163
3164 # parse "git diff" header line
3165 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
3166 # $1 is from_name, which we do not use
3167 $to_name = unquote($2);
3168 $to_name =~ s!^b/!!;
3169 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
3170 # $1 is 'cc' or 'combined', which we do not use
3171 $to_name = unquote($2);
3172 } else {
3173 $to_name = undef;
3174 }
3175
3176 # check if current patch belong to current raw line
3177 # and parse raw git-diff line if needed
3178 if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
3179 # this is continuation of a split patch
3180 print "<div class=\"patch cont\">\n";
3181 } else {
3182 # advance raw git-diff output if needed
3183 $patch_idx++ if defined $diffinfo;
3184
3185 # read and prepare patch information
3186 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3187
3188 # compact combined diff output can have some patches skipped
3189 # find which patch (using pathname of result) we are at now;
3190 if ($is_combined) {
3191 while ($to_name ne $diffinfo->{'to_file'}) {
3192 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3193 format_diff_cc_simplified($diffinfo, @hash_parents) .
3194 "</div>\n"; # class="patch"
3195
3196 $patch_idx++;
3197 $patch_number++;
3198
3199 last if $patch_idx > $#$difftree;
3200 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3201 }
3202 }
3203
3204 # modifies %from, %to hashes
3205 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
3206
3207 # this is first patch for raw difftree line with $patch_idx index
3208 # we index @$difftree array from 0, but number patches from 1
3209 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
3210 }
3211
3212 # git diff header
3213 #assert($patch_line =~ m/^diff /) if DEBUG;
3214 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
3215 $patch_number++;
3216 # print "git diff" header
3217 print format_git_diff_header_line($patch_line, $diffinfo,
3218 \%from, \%to);
3219
3220 # print extended diff header
3221 print "<div class=\"diff extended_header\">\n";
3222 EXTENDED_HEADER:
3223 while ($patch_line = <$fd>) {
3224 chomp $patch_line;
3225
3226 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
3227
3228 print format_extended_diff_header_line($patch_line, $diffinfo,
3229 \%from, \%to);
3230 }
3231 print "</div>\n"; # class="diff extended_header"
3232
3233 # from-file/to-file diff header
3234 if (! $patch_line) {
3235 print "</div>\n"; # class="patch"
3236 last PATCH;
3237 }
3238 next PATCH if ($patch_line =~ m/^diff /);
3239 #assert($patch_line =~ m/^---/) if DEBUG;
3240
3241 my $last_patch_line = $patch_line;
3242 $patch_line = <$fd>;
3243 chomp $patch_line;
3244 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
3245
3246 print format_diff_from_to_header($last_patch_line, $patch_line,
3247 $diffinfo, \%from, \%to,
3248 @hash_parents);
3249
3250 # the patch itself
3251 LINE:
3252 while ($patch_line = <$fd>) {
3253 chomp $patch_line;
3254
3255 next PATCH if ($patch_line =~ m/^diff /);
3256
3257 print format_diff_line($patch_line, \%from, \%to);
3258 }
3259
3260 } continue {
3261 print "</div>\n"; # class="patch"
3262 }
3263
3264 # for compact combined (--cc) format, with chunk and patch simpliciaction
3265 # patchset might be empty, but there might be unprocessed raw lines
3266 for (++$patch_idx if $patch_number > 0;
3267 $patch_idx < @$difftree;
3268 ++$patch_idx) {
3269 # read and prepare patch information
3270 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3271
3272 # generate anchor for "patch" links in difftree / whatchanged part
3273 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3274 format_diff_cc_simplified($diffinfo, @hash_parents) .
3275 "</div>\n"; # class="patch"
3276
3277 $patch_number++;
3278 }
3279
3280 if ($patch_number == 0) {
3281 if (@hash_parents > 1) {
3282 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
3283 } else {
3284 print "<div class=\"diff nodifferences\">No differences found</div>\n";
3285 }
3286 }
3287
3288 print "</div>\n"; # class="patchset"
3289 }
3290
3291 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3292
3293 sub git_project_list_body {
3294 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
3295
3296 my ($check_forks) = gitweb_check_feature('forks');
3297
3298 my @projects;
3299 foreach my $pr (@$projlist) {
3300 my (@aa) = git_get_last_activity($pr->{'path'});
3301 unless (@aa) {
3302 next;
3303 }
3304 ($pr->{'age'}, $pr->{'age_string'}) = @aa;
3305 if (!defined $pr->{'descr'}) {
3306 my $descr = git_get_project_description($pr->{'path'}) || "";
3307 $pr->{'descr_long'} = to_utf8($descr);
3308 $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
3309 }
3310 if (!defined $pr->{'owner'}) {
3311 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
3312 }
3313 if ($check_forks) {
3314 my $pname = $pr->{'path'};
3315 if (($pname =~ s/\.git$//) &&
3316 ($pname !~ /\/$/) &&
3317 (-d "$projectroot/$pname")) {
3318 $pr->{'forks'} = "-d $projectroot/$pname";
3319 }
3320 else {
3321 $pr->{'forks'} = 0;
3322 }
3323 }
3324 push @projects, $pr;
3325 }
3326
3327 $order ||= $default_projects_order;
3328 $from = 0 unless defined $from;
3329 $to = $#projects if (!defined $to || $#projects < $to);
3330
3331 print "<table class=\"project_list\">\n";
3332 unless ($no_header) {
3333 print "<tr>\n";
3334 if ($check_forks) {
3335 print "<th></th>\n";
3336 }
3337 if ($order eq "project") {
3338 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
3339 print "<th>Project</th>\n";
3340 } else {
3341 print "<th>" .
3342 $cgi->a({-href => href(project=>undef, order=>'project'),
3343 -class => "header"}, "Project") .
3344 "</th>\n";
3345 }
3346 if ($order eq "descr") {
3347 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
3348 print "<th>Description</th>\n";
3349 } else {
3350 print "<th>" .
3351 $cgi->a({-href => href(project=>undef, order=>'descr'),
3352 -class => "header"}, "Description") .
3353 "</th>\n";
3354 }
3355 if ($order eq "owner") {
3356 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
3357 print "<th>Owner</th>\n";
3358 } else {
3359 print "<th>" .
3360 $cgi->a({-href => href(project=>undef, order=>'owner'),
3361 -class => "header"}, "Owner") .
3362 "</th>\n";
3363 }
3364 if ($order eq "age") {
3365 @projects = sort {$a->{'age'} <=> $b->{'age'}} @projects;
3366 print "<th>Last Change</th>\n";
3367 } else {
3368 print "<th>" .
3369 $cgi->a({-href => href(project=>undef, order=>'age'),
3370 -class => "header"}, "Last Change") .
3371 "</th>\n";
3372 }
3373 print "<th></th>\n" .
3374 "</tr>\n";
3375 }
3376 my $alternate = 1;
3377 for (my $i = $from; $i <= $to; $i++) {
3378 my $pr = $projects[$i];
3379 if ($alternate) {
3380 print "<tr class=\"dark\">\n";
3381 } else {
3382 print "<tr class=\"light\">\n";
3383 }
3384 $alternate ^= 1;
3385 if ($check_forks) {
3386 print "<td>";
3387 if ($pr->{'forks'}) {
3388 print "<!-- $pr->{'forks'} -->\n";
3389 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
3390 }
3391 print "</td>\n";
3392 }
3393 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3394 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
3395 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3396 -class => "list", -title => $pr->{'descr_long'}},
3397 esc_html($pr->{'descr'})) . "</td>\n" .
3398 "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n";
3399 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
3400 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
3401 "<td class=\"link\">" .
3402 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
3403 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
3404 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
3405 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
3406 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
3407 "</td>\n" .
3408 "</tr>\n";
3409 }
3410 if (defined $extra) {
3411 print "<tr>\n";
3412 if ($check_forks) {
3413 print "<td></td>\n";
3414 }
3415 print "<td colspan=\"5\">$extra</td>\n" .
3416 "</tr>\n";
3417 }
3418 print "</table>\n";
3419 }
3420
3421 sub git_shortlog_body {
3422 # uses global variable $project
3423 my ($commitlist, $from, $to, $refs, $extra) = @_;
3424
3425 $from = 0 unless defined $from;
3426 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3427
3428 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
3429 my $alternate = 1;
3430 for (my $i = $from; $i <= $to; $i++) {
3431 my %co = %{$commitlist->[$i]};
3432 my $commit = $co{'id'};
3433 my $ref = format_ref_marker($refs, $commit);
3434 if ($alternate) {
3435 print "<tr class=\"dark\">\n";
3436 } else {
3437 print "<tr class=\"light\">\n";
3438 }
3439 $alternate ^= 1;
3440 my $author = chop_and_escape_str($co{'author_name'}, 10);
3441 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
3442 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3443 "<td><i>" . $author . "</i></td>\n" .
3444 "<td>";
3445 print format_subject_html($co{'title'}, $co{'title_short'},
3446 href(action=>"commit", hash=>$commit), $ref);
3447 print "</td>\n" .
3448 "<td class=\"link\">" .
3449 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
3450 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
3451 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
3452 my $snapshot_links = format_snapshot_links($commit);
3453 if (defined $snapshot_links) {
3454 print " | " . $snapshot_links;
3455 }
3456 print "</td>\n" .
3457 "</tr>\n";
3458 }
3459 if (defined $extra) {
3460 print "<tr>\n" .
3461 "<td colspan=\"4\">$extra</td>\n" .
3462 "</tr>\n";
3463 }
3464 print "</table>\n";
3465 }
3466
3467 sub git_history_body {
3468 # Warning: assumes constant type (blob or tree) during history
3469 my ($commitlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
3470
3471 $from = 0 unless defined $from;
3472 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
3473
3474 print "<table class=\"history\" cellspacing=\"0\">\n";
3475 my $alternate = 1;
3476 for (my $i = $from; $i <= $to; $i++) {
3477 my %co = %{$commitlist->[$i]};
3478 if (!%co) {
3479 next;
3480 }
3481 my $commit = $co{'id'};
3482
3483 my $ref = format_ref_marker($refs, $commit);
3484
3485 if ($alternate) {
3486 print "<tr class=\"dark\">\n";
3487 } else {
3488 print "<tr class=\"light\">\n";
3489 }
3490 $alternate ^= 1;
3491 # shortlog uses chop_str($co{'author_name'}, 10)
3492 my $author = chop_and_escape_str($co{'author_name'}, 15, 3);
3493 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3494 "<td><i>" . $author . "</i></td>\n" .
3495 "<td>";
3496 # originally git_history used chop_str($co{'title'}, 50)
3497 print format_subject_html($co{'title'}, $co{'title_short'},
3498 href(action=>"commit", hash=>$commit), $ref);
3499 print "</td>\n" .
3500 "<td class=\"link\">" .
3501 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
3502 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
3503
3504 if ($ftype eq 'blob') {
3505 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
3506 my $blob_parent = git_get_hash_by_path($commit, $file_name);
3507 if (defined $blob_current && defined $blob_parent &&
3508 $blob_current ne $blob_parent) {
3509 print " | " .
3510 $cgi->a({-href => href(action=>"blobdiff",
3511 hash=>$blob_current, hash_parent=>$blob_parent,
3512 hash_base=>$hash_base, hash_parent_base=>$commit,
3513 file_name=>$file_name)},
3514 "diff to current");
3515 }
3516 }
3517 print "</td>\n" .
3518 "</tr>\n";
3519 }
3520 if (defined $extra) {
3521 print "<tr>\n" .
3522 "<td colspan=\"4\">$extra</td>\n" .
3523 "</tr>\n";
3524 }
3525 print "</table>\n";
3526 }
3527
3528 sub git_tags_body {
3529 # uses global variable $project
3530 my ($taglist, $from, $to, $extra) = @_;
3531 $from = 0 unless defined $from;
3532 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
3533
3534 print "<table class=\"tags\" cellspacing=\"0\">\n";
3535 my $alternate = 1;
3536 for (my $i = $from; $i <= $to; $i++) {
3537 my $entry = $taglist->[$i];
3538 my %tag = %$entry;
3539 my $comment = $tag{'subject'};
3540 my $comment_short;
3541 if (defined $comment) {
3542 $comment_short = chop_str($comment, 30, 5);
3543 }
3544 if ($alternate) {
3545 print "<tr class=\"dark\">\n";
3546 } else {
3547 print "<tr class=\"light\">\n";
3548 }
3549 $alternate ^= 1;
3550 if (defined $tag{'age'}) {
3551 print "<td><i>$tag{'age'}</i></td>\n";
3552 } else {
3553 print "<td></td>\n";
3554 }
3555 print "<td>" .
3556 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
3557 -class => "list name"}, esc_html($tag{'name'})) .
3558 "</td>\n" .
3559 "<td>";
3560 if (defined $comment) {
3561 print format_subject_html($comment, $comment_short,
3562 href(action=>"tag", hash=>$tag{'id'}));
3563 }
3564 print "</td>\n" .
3565 "<td class=\"selflink\">";
3566 if ($tag{'type'} eq "tag") {
3567 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
3568 } else {
3569 print "&nbsp;";
3570 }
3571 print "</td>\n" .
3572 "<td class=\"link\">" . " | " .
3573 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
3574 if ($tag{'reftype'} eq "commit") {
3575 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
3576 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log");
3577 } elsif ($tag{'reftype'} eq "blob") {
3578 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
3579 }
3580 print "</td>\n" .
3581 "</tr>";
3582 }
3583 if (defined $extra) {
3584 print "<tr>\n" .
3585 "<td colspan=\"5\">$extra</td>\n" .
3586 "</tr>\n";
3587 }
3588 print "</table>\n";
3589 }
3590
3591 sub git_heads_body {
3592 # uses global variable $project
3593 my ($headlist, $head, $from, $to, $extra) = @_;
3594 $from = 0 unless defined $from;
3595 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
3596
3597 print "<table class=\"heads\" cellspacing=\"0\">\n";
3598 my $alternate = 1;
3599 for (my $i = $from; $i <= $to; $i++) {
3600 my $entry = $headlist->[$i];
3601 my %ref = %$entry;
3602 my $curr = $ref{'id'} eq $head;
3603 if ($alternate) {
3604 print "<tr class=\"dark\">\n";
3605 } else {
3606 print "<tr class=\"light\">\n";
3607 }
3608 $alternate ^= 1;
3609 print "<td><i>$ref{'age'}</i></td>\n" .
3610 ($curr ? "<td class=\"current_head\">" : "<td>") .
3611 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'}),
3612 -class => "list name"},esc_html($ref{'name'})) .
3613 "</td>\n" .
3614 "<td class=\"link\">" .
3615 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'})}, "shortlog") . " | " .
3616 $cgi->a({-href => href(action=>"log", hash=>$ref{'name'})}, "log") . " | " .
3617 $cgi->a({-href => href(action=>"tree", hash=>$ref{'name'}, hash_base=>$ref{'name'})}, "tree") .
3618 "</td>\n" .
3619 "</tr>";
3620 }
3621 if (defined $extra) {
3622 print "<tr>\n" .
3623 "<td colspan=\"3\">$extra</td>\n" .
3624 "</tr>\n";
3625 }
3626 print "</table>\n";
3627 }
3628
3629 sub git_search_grep_body {
3630 my ($commitlist, $from, $to, $extra) = @_;
3631 $from = 0 unless defined $from;
3632 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3633
3634 print "<table class=\"grep\" cellspacing=\"0\">\n";
3635 my $alternate = 1;
3636 for (my $i = $from; $i <= $to; $i++) {
3637 my %co = %{$commitlist->[$i]};
3638 if (!%co) {
3639 next;
3640 }
3641 my $commit = $co{'id'};
3642 if ($alternate) {
3643 print "<tr class=\"dark\">\n";
3644 } else {
3645 print "<tr class=\"light\">\n";
3646 }
3647 $alternate ^= 1;
3648 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
3649 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3650 "<td><i>" . $author . "</i></td>\n" .
3651 "<td>" .
3652 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
3653 chop_and_escape_str($co{'title'}, 50) . "<br/>");
3654 my $comment = $co{'comment'};
3655 foreach my $line (@$comment) {
3656 if ($line =~ m/^(.*)($search_regexp)(.*)$/i) {
3657 my $lead = esc_html($1) || "";
3658 $lead = chop_str($lead, 30, 10);
3659 my $match = esc_html($2) || "";
3660 my $trail = esc_html($3) || "";
3661 $trail = chop_str($trail, 30, 10);
3662 my $text = "$lead<span class=\"match\">$match</span>$trail";
3663 print chop_str($text, 80, 5) . "<br/>\n";
3664 }
3665 }
3666 print "</td>\n" .
3667 "<td class=\"link\">" .
3668 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3669 " | " .
3670 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3671 print "</td>\n" .
3672 "</tr>\n";
3673 }
3674 if (defined $extra) {
3675 print "<tr>\n" .
3676 "<td colspan=\"3\">$extra</td>\n" .
3677 "</tr>\n";
3678 }
3679 print "</table>\n";
3680 }
3681
3682 ## ======================================================================
3683 ## ======================================================================
3684 ## actions
3685
3686 sub git_project_list {
3687 my $order = $cgi->param('o');
3688 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
3689 die_error(undef, "Unknown order parameter");
3690 }
3691
3692 my @list = git_get_projects_list();
3693 if (!@list) {
3694 die_error(undef, "No projects found");
3695 }
3696
3697 git_header_html();
3698 if (-f $home_text) {
3699 print "<div class=\"index_include\">\n";
3700 open (my $fd, $home_text);
3701 print <$fd>;
3702 close $fd;
3703 print "</div>\n";
3704 }
3705 git_project_list_body(\@list, $order);
3706 git_footer_html();
3707 }
3708
3709 sub git_forks {
3710 my $order = $cgi->param('o');
3711 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
3712 die_error(undef, "Unknown order parameter");
3713 }
3714
3715 my @list = git_get_projects_list($project);
3716 if (!@list) {
3717 die_error(undef, "No forks found");
3718 }
3719
3720 git_header_html();
3721 git_print_page_nav('','');
3722 git_print_header_div('summary', "$project forks");
3723 git_project_list_body(\@list, $order);
3724 git_footer_html();
3725 }
3726
3727 sub git_project_index {
3728 my @projects = git_get_projects_list($project);
3729
3730 print $cgi->header(
3731 -type => 'text/plain',
3732 -charset => 'utf-8',
3733 -content_disposition => 'inline; filename="index.aux"');
3734
3735 foreach my $pr (@projects) {
3736 if (!exists $pr->{'owner'}) {
3737 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
3738 }
3739
3740 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
3741 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
3742 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
3743 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
3744 $path =~ s/ /\+/g;
3745 $owner =~ s/ /\+/g;
3746
3747 print "$path $owner\n";
3748 }
3749 }
3750
3751 sub git_summary {
3752 my $descr = git_get_project_description($project) || "none";
3753 my %co = parse_commit("HEAD");
3754 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
3755 my $head = $co{'id'};
3756
3757 my $owner = git_get_project_owner($project);
3758
3759 my $refs = git_get_references();
3760 # These get_*_list functions return one more to allow us to see if
3761 # there are more ...
3762 my @taglist = git_get_tags_list(16);
3763 my @headlist = git_get_heads_list(16);
3764 my @forklist;
3765 my ($check_forks) = gitweb_check_feature('forks');
3766
3767 if ($check_forks) {
3768 @forklist = git_get_projects_list($project);
3769 }
3770
3771 git_header_html();
3772 git_print_page_nav('summary','', $head);
3773
3774 print "<div class=\"title\">&nbsp;</div>\n";
3775 print "<table cellspacing=\"0\">\n" .
3776 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
3777 "<tr><td>owner</td><td>" . esc_html($owner) . "</td></tr>\n";
3778 if (defined $cd{'rfc2822'}) {
3779 print "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
3780 }
3781
3782 # use per project git URL list in $projectroot/$project/cloneurl
3783 # or make project git URL from git base URL and project name
3784 my $url_tag = "URL";
3785 my @url_list = git_get_project_url_list($project);
3786 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
3787 foreach my $git_url (@url_list) {
3788 next unless $git_url;
3789 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
3790 $url_tag = "";
3791 }
3792 print "</table>\n";
3793
3794 if (-s "$projectroot/$project/README.html") {
3795 if (open my $fd, "$projectroot/$project/README.html") {
3796 print "<div class=\"title\">readme</div>\n";
3797 print $_ while (<$fd>);
3798 close $fd;
3799 }
3800 }
3801
3802 # we need to request one more than 16 (0..15) to check if
3803 # those 16 are all
3804 my @commitlist = $head ? parse_commits($head, 17) : ();
3805 if (@commitlist) {
3806 git_print_header_div('shortlog');
3807 git_shortlog_body(\@commitlist, 0, 15, $refs,
3808 $#commitlist <= 15 ? undef :
3809 $cgi->a({-href => href(action=>"shortlog")}, "..."));
3810 }
3811
3812 if (@taglist) {
3813 git_print_header_div('tags');
3814 git_tags_body(\@taglist, 0, 15,
3815 $#taglist <= 15 ? undef :
3816 $cgi->a({-href => href(action=>"tags")}, "..."));
3817 }
3818
3819 if (@headlist) {
3820 git_print_header_div('heads');
3821 git_heads_body(\@headlist, $head, 0, 15,
3822 $#headlist <= 15 ? undef :
3823 $cgi->a({-href => href(action=>"heads")}, "..."));
3824 }
3825
3826 if (@forklist) {
3827 git_print_header_div('forks');
3828 git_project_list_body(\@forklist, undef, 0, 15,
3829 $#forklist <= 15 ? undef :
3830 $cgi->a({-href => href(action=>"forks")}, "..."),
3831 'noheader');
3832 }
3833
3834 git_footer_html();
3835 }
3836
3837 sub git_tag {
3838 my $head = git_get_head_hash($project);
3839 git_header_html();
3840 git_print_page_nav('','', $head,undef,$head);
3841 my %tag = parse_tag($hash);
3842
3843 if (! %tag) {
3844 die_error(undef, "Unknown tag object");
3845 }
3846
3847 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
3848 print "<div class=\"title_text\">\n" .
3849 "<table cellspacing=\"0\">\n" .
3850 "<tr>\n" .
3851 "<td>object</td>\n" .
3852 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
3853 $tag{'object'}) . "</td>\n" .
3854 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
3855 $tag{'type'}) . "</td>\n" .
3856 "</tr>\n";
3857 if (defined($tag{'author'})) {
3858 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
3859 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
3860 print "<tr><td></td><td>" . $ad{'rfc2822'} .
3861 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
3862 "</td></tr>\n";
3863 }
3864 print "</table>\n\n" .
3865 "</div>\n";
3866 print "<div class=\"page_body\">";
3867 my $comment = $tag{'comment'};
3868 foreach my $line (@$comment) {
3869 chomp $line;
3870 print esc_html($line, -nbsp=>1) . "<br/>\n";
3871 }
3872 print "</div>\n";
3873 git_footer_html();
3874 }
3875
3876 sub git_blame2 {
3877 my $fd;
3878 my $ftype;
3879
3880 my ($have_blame) = gitweb_check_feature('blame');
3881 if (!$have_blame) {
3882 die_error('403 Permission denied', "Permission denied");
3883 }
3884 die_error('404 Not Found', "File name not defined") if (!$file_name);
3885 $hash_base ||= git_get_head_hash($project);
3886 die_error(undef, "Couldn't find base commit") unless ($hash_base);
3887 my %co = parse_commit($hash_base)
3888 or die_error(undef, "Reading commit failed");
3889 if (!defined $hash) {
3890 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
3891 or die_error(undef, "Error looking up file");
3892 }
3893 $ftype = git_get_type($hash);
3894 if ($ftype !~ "blob") {
3895 die_error('400 Bad Request', "Object is not a blob");
3896 }
3897 open ($fd, "-|", git_cmd(), "blame", '-p', '--',
3898 $file_name, $hash_base)
3899 or die_error(undef, "Open git-blame failed");
3900 git_header_html();
3901 my $formats_nav =
3902 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3903 "blob") .
3904 " | " .
3905 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3906 "history") .
3907 " | " .
3908 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
3909 "HEAD");
3910 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3911 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3912 git_print_page_path($file_name, $ftype, $hash_base);
3913 my @rev_color = (qw(light2 dark2));
3914 my $num_colors = scalar(@rev_color);
3915 my $current_color = 0;
3916 my $last_rev;
3917 print <<HTML;
3918 <div class="page_body">
3919 <table class="blame">
3920 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
3921 HTML
3922 my %metainfo = ();
3923 while (1) {
3924 $_ = <$fd>;
3925 last unless defined $_;
3926 my ($full_rev, $orig_lineno, $lineno, $group_size) =
3927 /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;
3928 if (!exists $metainfo{$full_rev}) {
3929 $metainfo{$full_rev} = {};
3930 }
3931 my $meta = $metainfo{$full_rev};
3932 while (<$fd>) {
3933 last if (s/^\t//);
3934 if (/^(\S+) (.*)$/) {
3935 $meta->{$1} = $2;
3936 }
3937 }
3938 my $data = $_;
3939 chomp $data;
3940 my $rev = substr($full_rev, 0, 8);
3941 my $author = $meta->{'author'};
3942 my %date = parse_date($meta->{'author-time'},
3943 $meta->{'author-tz'});
3944 my $date = $date{'iso-tz'};
3945 if ($group_size) {
3946 $current_color = ++$current_color % $num_colors;
3947 }
3948 print "<tr class=\"$rev_color[$current_color]\">\n";
3949 if ($group_size) {
3950 print "<td class=\"sha1\"";
3951 print " title=\"". esc_html($author) . ", $date\"";
3952 print " rowspan=\"$group_size\"" if ($group_size > 1);
3953 print ">";
3954 print $cgi->a({-href => href(action=>"commit",
3955 hash=>$full_rev,
3956 file_name=>$file_name)},
3957 esc_html($rev));
3958 print "</td>\n";
3959 }
3960 open (my $dd, "-|", git_cmd(), "rev-parse", "$full_rev^")
3961 or die_error(undef, "Open git-rev-parse failed");
3962 my $parent_commit = <$dd>;
3963 close $dd;
3964 chomp($parent_commit);
3965 my $blamed = href(action => 'blame',
3966 file_name => $meta->{'filename'},
3967 hash_base => $parent_commit);
3968 print "<td class=\"linenr\">";
3969 print $cgi->a({ -href => "$blamed#l$orig_lineno",
3970 -id => "l$lineno",
3971 -class => "linenr" },
3972 esc_html($lineno));
3973 print "</td>";
3974 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
3975 print "</tr>\n";
3976 }
3977 print "</table>\n";
3978 print "</div>";
3979 close $fd
3980 or print "Reading blob failed\n";
3981 git_footer_html();
3982 }
3983
3984 sub git_blame {
3985 my $fd;
3986
3987 my ($have_blame) = gitweb_check_feature('blame');
3988 if (!$have_blame) {
3989 die_error('403 Permission denied', "Permission denied");
3990 }
3991 die_error('404 Not Found', "File name not defined") if (!$file_name);
3992 $hash_base ||= git_get_head_hash($project);
3993 die_error(undef, "Couldn't find base commit") unless ($hash_base);
3994 my %co = parse_commit($hash_base)
3995 or die_error(undef, "Reading commit failed");
3996 if (!defined $hash) {
3997 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
3998 or die_error(undef, "Error lookup file");
3999 }
4000 open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
4001 or die_error(undef, "Open git-annotate failed");
4002 git_header_html();
4003 my $formats_nav =
4004 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
4005 "blob") .
4006 " | " .
4007 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
4008 "history") .
4009 " | " .
4010 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
4011 "HEAD");
4012 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4013 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4014 git_print_page_path($file_name, 'blob', $hash_base);
4015 print "<div class=\"page_body\">\n";
4016 print <<HTML;
4017 <table class="blame">
4018 <tr>
4019 <th>Commit</th>
4020 <th>Age</th>
4021 <th>Author</th>
4022 <th>Line</th>
4023 <th>Data</th>
4024 </tr>
4025 HTML
4026 my @line_class = (qw(light dark));
4027 my $line_class_len = scalar (@line_class);
4028 my $line_class_num = $#line_class;
4029 while (my $line = <$fd>) {
4030 my $long_rev;
4031 my $short_rev;
4032 my $author;
4033 my $time;
4034 my $lineno;
4035 my $data;
4036 my $age;
4037 my $age_str;
4038 my $age_class;
4039
4040 chomp $line;
4041 $line_class_num = ($line_class_num + 1) % $line_class_len;
4042
4043 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
4044 $long_rev = $1;
4045 $author = $2;
4046 $time = $3;
4047 $lineno = $4;
4048 $data = $5;
4049 } else {
4050 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
4051 next;
4052 }
4053 $short_rev = substr ($long_rev, 0, 8);
4054 $age = time () - $time;
4055 $age_str = age_string ($age);
4056 $age_str =~ s/ /&nbsp;/g;
4057 $age_class = age_class($age);
4058 $author = esc_html ($author);
4059 $author =~ s/ /&nbsp;/g;
4060
4061 $data = untabify($data);
4062 $data = esc_html ($data);
4063
4064 print <<HTML;
4065 <tr class="$line_class[$line_class_num]">
4066 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
4067 <td class="$age_class">$age_str</td>
4068 <td>$author</td>
4069 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
4070 <td class="pre">$data</td>
4071 </tr>
4072 HTML
4073 } # while (my $line = <$fd>)
4074 print "</table>\n\n";
4075 close $fd
4076 or print "Reading blob failed.\n";
4077 print "</div>";
4078 git_footer_html();
4079 }
4080
4081 sub git_tags {
4082 my $head = git_get_head_hash($project);
4083 git_header_html();
4084 git_print_page_nav('','', $head,undef,$head);
4085 git_print_header_div('summary', $project);
4086
4087 my @tagslist = git_get_tags_list();
4088 if (@tagslist) {
4089 git_tags_body(\@tagslist);
4090 }
4091 git_footer_html();
4092 }
4093
4094 sub git_heads {
4095 my $head = git_get_head_hash($project);
4096 git_header_html();
4097 git_print_page_nav('','', $head,undef,$head);
4098 git_print_header_div('summary', $project);
4099
4100 my @headslist = git_get_heads_list();
4101 if (@headslist) {
4102 git_heads_body(\@headslist, $head);
4103 }
4104 git_footer_html();
4105 }
4106
4107 sub git_blob_plain {
4108 my $expires;
4109
4110 if (!defined $hash) {
4111 if (defined $file_name) {
4112 my $base = $hash_base || git_get_head_hash($project);
4113 $hash = git_get_hash_by_path($base, $file_name, "blob")
4114 or die_error(undef, "Error lookup file");
4115 } else {
4116 die_error(undef, "No file name defined");
4117 }
4118 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4119 # blobs defined by non-textual hash id's can be cached
4120 $expires = "+1d";
4121 }
4122
4123 my $type = shift;
4124 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4125 or die_error(undef, "Couldn't cat $file_name, $hash");
4126
4127 $type ||= blob_mimetype($fd, $file_name);
4128
4129 # save as filename, even when no $file_name is given
4130 my $save_as = "$hash";
4131 if (defined $file_name) {
4132 $save_as = $file_name;
4133 } elsif ($type =~ m/^text\//) {
4134 $save_as .= '.txt';
4135 }
4136
4137 print $cgi->header(
4138 -type => "$type",
4139 -expires=>$expires,
4140 -content_disposition => 'inline; filename="' . "$save_as" . '"');
4141 undef $/;
4142 binmode STDOUT, ':raw';
4143 print <$fd>;
4144 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4145 $/ = "\n";
4146 close $fd;
4147 }
4148
4149 sub git_blob {
4150 my $expires;
4151
4152 if (!defined $hash) {
4153 if (defined $file_name) {
4154 my $base = $hash_base || git_get_head_hash($project);
4155 $hash = git_get_hash_by_path($base, $file_name, "blob")
4156 or die_error(undef, "Error lookup file");
4157 } else {
4158 die_error(undef, "No file name defined");
4159 }
4160 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4161 # blobs defined by non-textual hash id's can be cached
4162 $expires = "+1d";
4163 }
4164
4165 my ($have_blame) = gitweb_check_feature('blame');
4166 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4167 or die_error(undef, "Couldn't cat $file_name, $hash");
4168 my $mimetype = blob_mimetype($fd, $file_name);
4169 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)!) {
4170 close $fd;
4171 return git_blob_plain($mimetype);
4172 }
4173 # we can have blame only for text/* mimetype
4174 $have_blame &&= ($mimetype =~ m!^text/!);
4175
4176 git_header_html(undef, $expires);
4177 my $formats_nav = '';
4178 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4179 if (defined $file_name) {
4180 if ($have_blame) {
4181 $formats_nav .=
4182 $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
4183 hash=>$hash, file_name=>$file_name)},
4184 "blame") .
4185 " | ";
4186 }
4187 $formats_nav .=
4188 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
4189 hash=>$hash, file_name=>$file_name)},
4190 "history") .
4191 " | " .
4192 $cgi->a({-href => href(action=>"blob_plain",
4193 hash=>$hash, file_name=>$file_name)},
4194 "raw") .
4195 " | " .
4196 $cgi->a({-href => href(action=>"blob",
4197 hash_base=>"HEAD", file_name=>$file_name)},
4198 "HEAD");
4199 } else {
4200 $formats_nav .=
4201 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "raw");
4202 }
4203 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4204 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4205 } else {
4206 print "<div class=\"page_nav\">\n" .
4207 "<br/><br/></div>\n" .
4208 "<div class=\"title\">$hash</div>\n";
4209 }
4210 git_print_page_path($file_name, "blob", $hash_base);
4211 print "<div class=\"page_body\">\n";
4212 if ($mimetype =~ m!^text/!) {
4213 my $nr;
4214 while (my $line = <$fd>) {
4215 chomp $line;
4216 $nr++;
4217 $line = untabify($line);
4218 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
4219 $nr, $nr, $nr, esc_html($line, -nbsp=>1);
4220 }
4221 } elsif ($mimetype =~ m!^image/!) {
4222 print qq!<img type="$mimetype"!;
4223 if ($file_name) {
4224 print qq! alt="$file_name" title="$file_name"!;
4225 }
4226 print qq! src="! .
4227 href(action=>"blob_plain", hash=>$hash,
4228 hash_base=>$hash_base, file_name=>$file_name) .
4229 qq!" />\n!;
4230 }
4231 close $fd
4232 or print "Reading blob failed.\n";
4233 print "</div>";
4234 git_footer_html();
4235 }
4236
4237 sub git_tree {
4238 if (!defined $hash_base) {
4239 $hash_base = "HEAD";
4240 }
4241 if (!defined $hash) {
4242 if (defined $file_name) {
4243 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
4244 } else {
4245 $hash = $hash_base;
4246 }
4247 }
4248 $/ = "\0";
4249 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
4250 or die_error(undef, "Open git-ls-tree failed");
4251 my @entries = map { chomp; $_ } <$fd>;
4252 close $fd or die_error(undef, "Reading tree failed");
4253 $/ = "\n";
4254
4255 my $refs = git_get_references();
4256 my $ref = format_ref_marker($refs, $hash_base);
4257 git_header_html();
4258 my $basedir = '';
4259 my ($have_blame) = gitweb_check_feature('blame');
4260 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4261 my @views_nav = ();
4262 if (defined $file_name) {
4263 push @views_nav,
4264 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
4265 hash=>$hash, file_name=>$file_name)},
4266 "history"),
4267 $cgi->a({-href => href(action=>"tree",
4268 hash_base=>"HEAD", file_name=>$file_name)},
4269 "HEAD"),
4270 }
4271 my $snapshot_links = format_snapshot_links($hash);
4272 if (defined $snapshot_links) {
4273 # FIXME: Should be available when we have no hash base as well.
4274 push @views_nav, $snapshot_links;
4275 }
4276 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
4277 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
4278 } else {
4279 undef $hash_base;
4280 print "<div class=\"page_nav\">\n";
4281 print "<br/><br/></div>\n";
4282 print "<div class=\"title\">$hash</div>\n";
4283 }
4284 if (defined $file_name) {
4285 $basedir = $file_name;
4286 if ($basedir ne '' && substr($basedir, -1) ne '/') {
4287 $basedir .= '/';
4288 }
4289 }
4290 git_print_page_path($file_name, 'tree', $hash_base);
4291 print "<div class=\"page_body\">\n";
4292 print "<table cellspacing=\"0\">\n";
4293 my $alternate = 1;
4294 # '..' (top directory) link if possible
4295 if (defined $hash_base &&
4296 defined $file_name && $file_name =~ m![^/]+$!) {
4297 if ($alternate) {
4298 print "<tr class=\"dark\">\n";
4299 } else {
4300 print "<tr class=\"light\">\n";
4301 }
4302 $alternate ^= 1;
4303
4304 my $up = $file_name;
4305 $up =~ s!/?[^/]+$!!;
4306 undef $up unless $up;
4307 # based on git_print_tree_entry
4308 print '<td class="mode">' . mode_str('040000') . "</td>\n";
4309 print '<td class="list">';
4310 print $cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,
4311 file_name=>$up)},
4312 "..");
4313 print "</td>\n";
4314 print "<td class=\"link\"></td>\n";
4315
4316 print "</tr>\n";
4317 }
4318 foreach my $line (@entries) {
4319 my %t = parse_ls_tree_line($line, -z => 1);
4320
4321 if ($alternate) {
4322 print "<tr class=\"dark\">\n";
4323 } else {
4324 print "<tr class=\"light\">\n";
4325 }
4326 $alternate ^= 1;
4327
4328 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
4329
4330 print "</tr>\n";
4331 }
4332 print "</table>\n" .
4333 "</div>";
4334 git_footer_html();
4335 }
4336
4337 sub git_snapshot {
4338 my @supported_fmts = gitweb_check_feature('snapshot');
4339 @supported_fmts = filter_snapshot_fmts(@supported_fmts);
4340
4341 my $format = $cgi->param('sf');
4342 if (!@supported_fmts) {
4343 die_error('403 Permission denied', "Permission denied");
4344 }
4345 # default to first supported snapshot format
4346 $format ||= $supported_fmts[0];
4347 if ($format !~ m/^[a-z0-9]+$/) {
4348 die_error(undef, "Invalid snapshot format parameter");
4349 } elsif (!exists($known_snapshot_formats{$format})) {
4350 die_error(undef, "Unknown snapshot format");
4351 } elsif (!grep($_ eq $format, @supported_fmts)) {
4352 die_error(undef, "Unsupported snapshot format");
4353 }
4354
4355 if (!defined $hash) {
4356 $hash = git_get_head_hash($project);
4357 }
4358
4359 my $git_command = git_cmd_str();
4360 my $name = $project;
4361 $name =~ s,([^/])/*\.git$,$1,;
4362 $name = basename($name);
4363 my $filename = to_utf8($name);
4364 $name =~ s/\047/\047\\\047\047/g;
4365 my $cmd;
4366 $filename .= "-$hash$known_snapshot_formats{$format}{'suffix'}";
4367 $cmd = "$git_command archive " .
4368 "--format=$known_snapshot_formats{$format}{'format'} " .
4369 "--prefix=\'$name\'/ $hash";
4370 if (exists $known_snapshot_formats{$format}{'compressor'}) {
4371 $cmd .= ' | ' . join ' ', @{$known_snapshot_formats{$format}{'compressor'}};
4372 }
4373
4374 print $cgi->header(
4375 -type => $known_snapshot_formats{$format}{'type'},
4376 -content_disposition => 'inline; filename="' . "$filename" . '"',
4377 -status => '200 OK');
4378
4379 open my $fd, "-|", $cmd
4380 or die_error(undef, "Execute git-archive failed");
4381 binmode STDOUT, ':raw';
4382 print <$fd>;
4383 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4384 close $fd;
4385 }
4386
4387 sub git_log {
4388 my $head = git_get_head_hash($project);
4389 if (!defined $hash) {
4390 $hash = $head;
4391 }
4392 if (!defined $page) {
4393 $page = 0;
4394 }
4395 my $refs = git_get_references();
4396
4397 my @commitlist = parse_commits($hash, 101, (100 * $page));
4398
4399 my $paging_nav = format_paging_nav('log', $hash, $head, $page, (100 * ($page+1)));
4400
4401 git_header_html();
4402 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
4403
4404 if (!@commitlist) {
4405 my %co = parse_commit($hash);
4406
4407 git_print_header_div('summary', $project);
4408 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
4409 }
4410 my $to = ($#commitlist >= 99) ? (99) : ($#commitlist);
4411 for (my $i = 0; $i <= $to; $i++) {
4412 my %co = %{$commitlist[$i]};
4413 next if !%co;
4414 my $commit = $co{'id'};
4415 my $ref = format_ref_marker($refs, $commit);
4416 my %ad = parse_date($co{'author_epoch'});
4417 git_print_header_div('commit',
4418 "<span class=\"age\">$co{'age_string'}</span>" .
4419 esc_html($co{'title'}) . $ref,
4420 $commit);
4421 print "<div class=\"title_text\">\n" .
4422 "<div class=\"log_link\">\n" .
4423 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
4424 " | " .
4425 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
4426 " | " .
4427 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
4428 "<br/>\n" .
4429 "</div>\n" .
4430 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
4431 "</div>\n";
4432
4433 print "<div class=\"log_body\">\n";
4434 git_print_log($co{'comment'}, -final_empty_line=> 1);
4435 print "</div>\n";
4436 }
4437 if ($#commitlist >= 100) {
4438 print "<div class=\"page_nav\">\n";
4439 print $cgi->a({-href => href(action=>"log", hash=>$hash, page=>$page+1),
4440 -accesskey => "n", -title => "Alt-n"}, "next");
4441 print "</div>\n";
4442 }
4443 git_footer_html();
4444 }
4445
4446 sub git_commit {
4447 $hash ||= $hash_base || "HEAD";
4448 my %co = parse_commit($hash);
4449 if (!%co) {
4450 die_error(undef, "Unknown commit object");
4451 }
4452 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4453 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
4454
4455 my $parent = $co{'parent'};
4456 my $parents = $co{'parents'}; # listref
4457
4458 # we need to prepare $formats_nav before any parameter munging
4459 my $formats_nav;
4460 if (!defined $parent) {
4461 # --root commitdiff
4462 $formats_nav .= '(initial)';
4463 } elsif (@$parents == 1) {
4464 # single parent commit
4465 $formats_nav .=
4466 '(parent: ' .
4467 $cgi->a({-href => href(action=>"commit",
4468 hash=>$parent)},
4469 esc_html(substr($parent, 0, 7))) .
4470 ')';
4471 } else {
4472 # merge commit
4473 $formats_nav .=
4474 '(merge: ' .
4475 join(' ', map {
4476 $cgi->a({-href => href(action=>"commit",
4477 hash=>$_)},
4478 esc_html(substr($_, 0, 7)));
4479 } @$parents ) .
4480 ')';
4481 }
4482
4483 if (!defined $parent) {
4484 $parent = "--root";
4485 }
4486 my @difftree;
4487 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
4488 @diff_opts,
4489 (@$parents <= 1 ? $parent : '-c'),
4490 $hash, "--"
4491 or die_error(undef, "Open git-diff-tree failed");
4492 @difftree = map { chomp; $_ } <$fd>;
4493 close $fd or die_error(undef, "Reading git-diff-tree failed");
4494
4495 # non-textual hash id's can be cached
4496 my $expires;
4497 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4498 $expires = "+1d";
4499 }
4500 my $refs = git_get_references();
4501 my $ref = format_ref_marker($refs, $co{'id'});
4502
4503 git_header_html(undef, $expires);
4504 git_print_page_nav('commit', '',
4505 $hash, $co{'tree'}, $hash,
4506 $formats_nav);
4507
4508 if (defined $co{'parent'}) {
4509 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
4510 } else {
4511 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
4512 }
4513 print "<div class=\"title_text\">\n" .
4514 "<table cellspacing=\"0\">\n";
4515 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
4516 "<tr>" .
4517 "<td></td><td> $ad{'rfc2822'}";
4518 if ($ad{'hour_local'} < 6) {
4519 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
4520 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4521 } else {
4522 printf(" (%02d:%02d %s)",
4523 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4524 }
4525 print "</td>" .
4526 "</tr>\n";
4527 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
4528 print "<tr><td></td><td> $cd{'rfc2822'}" .
4529 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
4530 "</td></tr>\n";
4531 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
4532 print "<tr>" .
4533 "<td>tree</td>" .
4534 "<td class=\"sha1\">" .
4535 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
4536 class => "list"}, $co{'tree'}) .
4537 "</td>" .
4538 "<td class=\"link\">" .
4539 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
4540 "tree");
4541 my $snapshot_links = format_snapshot_links($hash);
4542 if (defined $snapshot_links) {
4543 print " | " . $snapshot_links;
4544 }
4545 print "</td>" .
4546 "</tr>\n";
4547
4548 foreach my $par (@$parents) {
4549 print "<tr>" .
4550 "<td>parent</td>" .
4551 "<td class=\"sha1\">" .
4552 $cgi->a({-href => href(action=>"commit", hash=>$par),
4553 class => "list"}, $par) .
4554 "</td>" .
4555 "<td class=\"link\">" .
4556 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
4557 " | " .
4558 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
4559 "</td>" .
4560 "</tr>\n";
4561 }
4562 print "</table>".
4563 "</div>\n";
4564
4565 print "<div class=\"page_body\">\n";
4566 git_print_log($co{'comment'});
4567 print "</div>\n";
4568
4569 git_difftree_body(\@difftree, $hash, @$parents);
4570
4571 git_footer_html();
4572 }
4573
4574 sub git_object {
4575 # object is defined by:
4576 # - hash or hash_base alone
4577 # - hash_base and file_name
4578 my $type;
4579
4580 # - hash or hash_base alone
4581 if ($hash || ($hash_base && !defined $file_name)) {
4582 my $object_id = $hash || $hash_base;
4583
4584 my $git_command = git_cmd_str();
4585 open my $fd, "-|", "$git_command cat-file -t $object_id 2>/dev/null"
4586 or die_error('404 Not Found', "Object does not exist");
4587 $type = <$fd>;
4588 chomp $type;
4589 close $fd
4590 or die_error('404 Not Found', "Object does not exist");
4591
4592 # - hash_base and file_name
4593 } elsif ($hash_base && defined $file_name) {
4594 $file_name =~ s,/+$,,;
4595
4596 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
4597 or die_error('404 Not Found', "Base object does not exist");
4598
4599 # here errors should not hapen
4600 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
4601 or die_error(undef, "Open git-ls-tree failed");
4602 my $line = <$fd>;
4603 close $fd;
4604
4605 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
4606 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
4607 die_error('404 Not Found', "File or directory for given base does not exist");
4608 }
4609 $type = $2;
4610 $hash = $3;
4611 } else {
4612 die_error('404 Not Found', "Not enough information to find object");
4613 }
4614
4615 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
4616 hash=>$hash, hash_base=>$hash_base,
4617 file_name=>$file_name),
4618 -status => '302 Found');
4619 }
4620
4621 sub git_blobdiff {
4622 my $format = shift || 'html';
4623
4624 my $fd;
4625 my @difftree;
4626 my %diffinfo;
4627 my $expires;
4628
4629 # preparing $fd and %diffinfo for git_patchset_body
4630 # new style URI
4631 if (defined $hash_base && defined $hash_parent_base) {
4632 if (defined $file_name) {
4633 # read raw output
4634 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4635 $hash_parent_base, $hash_base,
4636 "--", (defined $file_parent ? $file_parent : ()), $file_name
4637 or die_error(undef, "Open git-diff-tree failed");
4638 @difftree = map { chomp; $_ } <$fd>;
4639 close $fd
4640 or die_error(undef, "Reading git-diff-tree failed");
4641 @difftree
4642 or die_error('404 Not Found', "Blob diff not found");
4643
4644 } elsif (defined $hash &&
4645 $hash =~ /[0-9a-fA-F]{40}/) {
4646 # try to find filename from $hash
4647
4648 # read filtered raw output
4649 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4650 $hash_parent_base, $hash_base, "--"
4651 or die_error(undef, "Open git-diff-tree failed");
4652 @difftree =
4653 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
4654 # $hash == to_id
4655 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
4656 map { chomp; $_ } <$fd>;
4657 close $fd
4658 or die_error(undef, "Reading git-diff-tree failed");
4659 @difftree
4660 or die_error('404 Not Found', "Blob diff not found");
4661
4662 } else {
4663 die_error('404 Not Found', "Missing one of the blob diff parameters");
4664 }
4665
4666 if (@difftree > 1) {
4667 die_error('404 Not Found', "Ambiguous blob diff specification");
4668 }
4669
4670 %diffinfo = parse_difftree_raw_line($difftree[0]);
4671 $file_parent ||= $diffinfo{'from_file'} || $file_name || $diffinfo{'file'};
4672 $file_name ||= $diffinfo{'to_file'} || $diffinfo{'file'};
4673
4674 $hash_parent ||= $diffinfo{'from_id'};
4675 $hash ||= $diffinfo{'to_id'};
4676
4677 # non-textual hash id's can be cached
4678 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
4679 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
4680 $expires = '+1d';
4681 }
4682
4683 # open patch output
4684 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4685 '-p', ($format eq 'html' ? "--full-index" : ()),
4686 $hash_parent_base, $hash_base,
4687 "--", (defined $file_parent ? $file_parent : ()), $file_name
4688 or die_error(undef, "Open git-diff-tree failed");
4689 }
4690
4691 # old/legacy style URI
4692 if (!%diffinfo && # if new style URI failed
4693 defined $hash && defined $hash_parent) {
4694 # fake git-diff-tree raw output
4695 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
4696 $diffinfo{'from_id'} = $hash_parent;
4697 $diffinfo{'to_id'} = $hash;
4698 if (defined $file_name) {
4699 if (defined $file_parent) {
4700 $diffinfo{'status'} = '2';
4701 $diffinfo{'from_file'} = $file_parent;
4702 $diffinfo{'to_file'} = $file_name;
4703 } else { # assume not renamed
4704 $diffinfo{'status'} = '1';
4705 $diffinfo{'from_file'} = $file_name;
4706 $diffinfo{'to_file'} = $file_name;
4707 }
4708 } else { # no filename given
4709 $diffinfo{'status'} = '2';
4710 $diffinfo{'from_file'} = $hash_parent;
4711 $diffinfo{'to_file'} = $hash;
4712 }
4713
4714 # non-textual hash id's can be cached
4715 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
4716 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4717 $expires = '+1d';
4718 }
4719
4720 # open patch output
4721 open $fd, "-|", git_cmd(), "diff", @diff_opts,
4722 '-p', ($format eq 'html' ? "--full-index" : ()),
4723 $hash_parent, $hash, "--"
4724 or die_error(undef, "Open git-diff failed");
4725 } else {
4726 die_error('404 Not Found', "Missing one of the blob diff parameters")
4727 unless %diffinfo;
4728 }
4729
4730 # header
4731 if ($format eq 'html') {
4732 my $formats_nav =
4733 $cgi->a({-href => href(action=>"blobdiff_plain",
4734 hash=>$hash, hash_parent=>$hash_parent,
4735 hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
4736 file_name=>$file_name, file_parent=>$file_parent)},
4737 "raw");
4738 git_header_html(undef, $expires);
4739 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4740 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4741 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4742 } else {
4743 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
4744 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
4745 }
4746 if (defined $file_name) {
4747 git_print_page_path($file_name, "blob", $hash_base);
4748 } else {
4749 print "<div class=\"page_path\"></div>\n";
4750 }
4751
4752 } elsif ($format eq 'plain') {
4753 print $cgi->header(
4754 -type => 'text/plain',
4755 -charset => 'utf-8',
4756 -expires => $expires,
4757 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
4758
4759 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
4760
4761 } else {
4762 die_error(undef, "Unknown blobdiff format");
4763 }
4764
4765 # patch
4766 if ($format eq 'html') {
4767 print "<div class=\"page_body\">\n";
4768
4769 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
4770 close $fd;
4771
4772 print "</div>\n"; # class="page_body"
4773 git_footer_html();
4774
4775 } else {
4776 while (my $line = <$fd>) {
4777 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
4778 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
4779
4780 print $line;
4781
4782 last if $line =~ m!^\+\+\+!;
4783 }
4784 local $/ = undef;
4785 print <$fd>;
4786 close $fd;
4787 }
4788 }
4789
4790 sub git_blobdiff_plain {
4791 git_blobdiff('plain');
4792 }
4793
4794 sub git_commitdiff {
4795 my $format = shift || 'html';
4796 $hash ||= $hash_base || "HEAD";
4797 my %co = parse_commit($hash);
4798 if (!%co) {
4799 die_error(undef, "Unknown commit object");
4800 }
4801
4802 # choose format for commitdiff for merge
4803 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
4804 $hash_parent = '--cc';
4805 }
4806 # we need to prepare $formats_nav before almost any parameter munging
4807 my $formats_nav;
4808 if ($format eq 'html') {
4809 $formats_nav =
4810 $cgi->a({-href => href(action=>"commitdiff_plain",
4811 hash=>$hash, hash_parent=>$hash_parent)},
4812 "raw");
4813
4814 if (defined $hash_parent &&
4815 $hash_parent ne '-c' && $hash_parent ne '--cc') {
4816 # commitdiff with two commits given
4817 my $hash_parent_short = $hash_parent;
4818 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4819 $hash_parent_short = substr($hash_parent, 0, 7);
4820 }
4821 $formats_nav .=
4822 ' (from';
4823 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
4824 if ($co{'parents'}[$i] eq $hash_parent) {
4825 $formats_nav .= ' parent ' . ($i+1);
4826 last;
4827 }
4828 }
4829 $formats_nav .= ': ' .
4830 $cgi->a({-href => href(action=>"commitdiff",
4831 hash=>$hash_parent)},
4832 esc_html($hash_parent_short)) .
4833 ')';
4834 } elsif (!$co{'parent'}) {
4835 # --root commitdiff
4836 $formats_nav .= ' (initial)';
4837 } elsif (scalar @{$co{'parents'}} == 1) {
4838 # single parent commit
4839 $formats_nav .=
4840 ' (parent: ' .
4841 $cgi->a({-href => href(action=>"commitdiff",
4842 hash=>$co{'parent'})},
4843 esc_html(substr($co{'parent'}, 0, 7))) .
4844 ')';
4845 } else {
4846 # merge commit
4847 if ($hash_parent eq '--cc') {
4848 $formats_nav .= ' | ' .
4849 $cgi->a({-href => href(action=>"commitdiff",
4850 hash=>$hash, hash_parent=>'-c')},
4851 'combined');
4852 } else { # $hash_parent eq '-c'
4853 $formats_nav .= ' | ' .
4854 $cgi->a({-href => href(action=>"commitdiff",
4855 hash=>$hash, hash_parent=>'--cc')},
4856 'compact');
4857 }
4858 $formats_nav .=
4859 ' (merge: ' .
4860 join(' ', map {
4861 $cgi->a({-href => href(action=>"commitdiff",
4862 hash=>$_)},
4863 esc_html(substr($_, 0, 7)));
4864 } @{$co{'parents'}} ) .
4865 ')';
4866 }
4867 }
4868
4869 my $hash_parent_param = $hash_parent;
4870 if (!defined $hash_parent_param) {
4871 # --cc for multiple parents, --root for parentless
4872 $hash_parent_param =
4873 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
4874 }
4875
4876 # read commitdiff
4877 my $fd;
4878 my @difftree;
4879 if ($format eq 'html') {
4880 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4881 "--no-commit-id", "--patch-with-raw", "--full-index",
4882 $hash_parent_param, $hash, "--"
4883 or die_error(undef, "Open git-diff-tree failed");
4884
4885 while (my $line = <$fd>) {
4886 chomp $line;
4887 # empty line ends raw part of diff-tree output
4888 last unless $line;
4889 push @difftree, scalar parse_difftree_raw_line($line);
4890 }
4891
4892 } elsif ($format eq 'plain') {
4893 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4894 '-p', $hash_parent_param, $hash, "--"
4895 or die_error(undef, "Open git-diff-tree failed");
4896
4897 } else {
4898 die_error(undef, "Unknown commitdiff format");
4899 }
4900
4901 # non-textual hash id's can be cached
4902 my $expires;
4903 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4904 $expires = "+1d";
4905 }
4906
4907 # write commit message
4908 if ($format eq 'html') {
4909 my $refs = git_get_references();
4910 my $ref = format_ref_marker($refs, $co{'id'});
4911
4912 git_header_html(undef, $expires);
4913 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
4914 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
4915 git_print_authorship(\%co);
4916 print "<div class=\"page_body\">\n";
4917 if (@{$co{'comment'}} > 1) {
4918 print "<div class=\"log\">\n";
4919 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
4920 print "</div>\n"; # class="log"
4921 }
4922
4923 } elsif ($format eq 'plain') {
4924 my $refs = git_get_references("tags");
4925 my $tagname = git_get_rev_name_tags($hash);
4926 my $filename = basename($project) . "-$hash.patch";
4927
4928 print $cgi->header(
4929 -type => 'text/plain',
4930 -charset => 'utf-8',
4931 -expires => $expires,
4932 -content_disposition => 'inline; filename="' . "$filename" . '"');
4933 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4934 print <<TEXT;
4935 From: $co{'author'}
4936 Date: $ad{'rfc2822'} ($ad{'tz_local'})
4937 Subject: $co{'title'}
4938 TEXT
4939 print "X-Git-Tag: $tagname\n" if $tagname;
4940 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
4941
4942 foreach my $line (@{$co{'comment'}}) {
4943 print "$line\n";
4944 }
4945 print "---\n\n";
4946 }
4947
4948 # write patch
4949 if ($format eq 'html') {
4950 my $use_parents = !defined $hash_parent ||
4951 $hash_parent eq '-c' || $hash_parent eq '--cc';
4952 git_difftree_body(\@difftree, $hash,
4953 $use_parents ? @{$co{'parents'}} : $hash_parent);
4954 print "<br/>\n";
4955
4956 git_patchset_body($fd, \@difftree, $hash,
4957 $use_parents ? @{$co{'parents'}} : $hash_parent);
4958 close $fd;
4959 print "</div>\n"; # class="page_body"
4960 git_footer_html();
4961
4962 } elsif ($format eq 'plain') {
4963 local $/ = undef;
4964 print <$fd>;
4965 close $fd
4966 or print "Reading git-diff-tree failed\n";
4967 }
4968 }
4969
4970 sub git_commitdiff_plain {
4971 git_commitdiff('plain');
4972 }
4973
4974 sub git_history {
4975 if (!defined $hash_base) {
4976 $hash_base = git_get_head_hash($project);
4977 }
4978 if (!defined $page) {
4979 $page = 0;
4980 }
4981 my $ftype;
4982 my %co = parse_commit($hash_base);
4983 if (!%co) {
4984 die_error(undef, "Unknown commit object");
4985 }
4986
4987 my $refs = git_get_references();
4988 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
4989
4990 if (!defined $hash && defined $file_name) {
4991 $hash = git_get_hash_by_path($hash_base, $file_name);
4992 }
4993 if (defined $hash) {
4994 $ftype = git_get_type($hash);
4995 }
4996
4997 my @commitlist = parse_commits($hash_base, 101, (100 * $page), "--full-history", $file_name);
4998
4999 my $paging_nav = '';
5000 if ($page > 0) {
5001 $paging_nav .=
5002 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
5003 file_name=>$file_name)},
5004 "first");
5005 $paging_nav .= " &sdot; " .
5006 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
5007 file_name=>$file_name, page=>$page-1),
5008 -accesskey => "p", -title => "Alt-p"}, "prev");
5009 } else {
5010 $paging_nav .= "first";
5011 $paging_nav .= " &sdot; prev";
5012 }
5013 if ($#commitlist >= 100) {
5014 $paging_nav .= " &sdot; " .
5015 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
5016 file_name=>$file_name, page=>$page+1),
5017 -accesskey => "n", -title => "Alt-n"}, "next");
5018 } else {
5019 $paging_nav .= " &sdot; next";
5020 }
5021 my $next_link = '';
5022 if ($#commitlist >= 100) {
5023 $next_link =
5024 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
5025 file_name=>$file_name, page=>$page+1),
5026 -accesskey => "n", -title => "Alt-n"}, "next");
5027 }
5028
5029 git_header_html();
5030 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
5031 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5032 git_print_page_path($file_name, $ftype, $hash_base);
5033
5034 git_history_body(\@commitlist, 0, 99,
5035 $refs, $hash_base, $ftype, $next_link);
5036
5037 git_footer_html();
5038 }
5039
5040 sub git_search {
5041 my ($have_search) = gitweb_check_feature('search');
5042 if (!$have_search) {
5043 die_error('403 Permission denied', "Permission denied");
5044 }
5045 if (!defined $searchtext) {
5046 die_error(undef, "Text field empty");
5047 }
5048 if (!defined $hash) {
5049 $hash = git_get_head_hash($project);
5050 }
5051 my %co = parse_commit($hash);
5052 if (!%co) {
5053 die_error(undef, "Unknown commit object");
5054 }
5055 if (!defined $page) {
5056 $page = 0;
5057 }
5058
5059 $searchtype ||= 'commit';
5060 if ($searchtype eq 'pickaxe') {
5061 # pickaxe may take all resources of your box and run for several minutes
5062 # with every query - so decide by yourself how public you make this feature
5063 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
5064 if (!$have_pickaxe) {
5065 die_error('403 Permission denied', "Permission denied");
5066 }
5067 }
5068 if ($searchtype eq 'grep') {
5069 my ($have_grep) = gitweb_check_feature('grep');
5070 if (!$have_grep) {
5071 die_error('403 Permission denied', "Permission denied");
5072 }
5073 }
5074
5075 git_header_html();
5076
5077 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
5078 my $greptype;
5079 if ($searchtype eq 'commit') {
5080 $greptype = "--grep=";
5081 } elsif ($searchtype eq 'author') {
5082 $greptype = "--author=";
5083 } elsif ($searchtype eq 'committer') {
5084 $greptype = "--committer=";
5085 }
5086 $greptype .= $search_regexp;
5087 my @commitlist = parse_commits($hash, 101, (100 * $page), $greptype);
5088
5089 my $paging_nav = '';
5090 if ($page > 0) {
5091 $paging_nav .=
5092 $cgi->a({-href => href(action=>"search", hash=>$hash,
5093 searchtext=>$searchtext, searchtype=>$searchtype)},
5094 "first");
5095 $paging_nav .= " &sdot; " .
5096 $cgi->a({-href => href(action=>"search", hash=>$hash,
5097 searchtext=>$searchtext, searchtype=>$searchtype,
5098 page=>$page-1),
5099 -accesskey => "p", -title => "Alt-p"}, "prev");
5100 } else {
5101 $paging_nav .= "first";
5102 $paging_nav .= " &sdot; prev";
5103 }
5104 if ($#commitlist >= 100) {
5105 $paging_nav .= " &sdot; " .
5106 $cgi->a({-href => href(action=>"search", hash=>$hash,
5107 searchtext=>$searchtext, searchtype=>$searchtype,
5108 page=>$page+1),
5109 -accesskey => "n", -title => "Alt-n"}, "next");
5110 } else {
5111 $paging_nav .= " &sdot; next";
5112 }
5113 my $next_link = '';
5114 if ($#commitlist >= 100) {
5115 $next_link =
5116 $cgi->a({-href => href(action=>"search", hash=>$hash,
5117 searchtext=>$searchtext, searchtype=>$searchtype,
5118 page=>$page+1),
5119 -accesskey => "n", -title => "Alt-n"}, "next");
5120 }
5121
5122 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
5123 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5124 git_search_grep_body(\@commitlist, 0, 99, $next_link);
5125 }
5126
5127 if ($searchtype eq 'pickaxe') {
5128 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5129 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5130
5131 print "<table cellspacing=\"0\">\n";
5132 my $alternate = 1;
5133 $/ = "\n";
5134 my $git_command = git_cmd_str();
5135 my $searchqtext = $searchtext;
5136 $searchqtext =~ s/'/'\\''/;
5137 open my $fd, "-|", "$git_command rev-list $hash | " .
5138 "$git_command diff-tree -r --stdin -S\'$searchqtext\'";
5139 undef %co;
5140 my @files;
5141 while (my $line = <$fd>) {
5142 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
5143 my %set;
5144 $set{'file'} = $6;
5145 $set{'from_id'} = $3;
5146 $set{'to_id'} = $4;
5147 $set{'id'} = $set{'to_id'};
5148 if ($set{'id'} =~ m/0{40}/) {
5149 $set{'id'} = $set{'from_id'};
5150 }
5151 if ($set{'id'} =~ m/0{40}/) {
5152 next;
5153 }
5154 push @files, \%set;
5155 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
5156 if (%co) {
5157 if ($alternate) {
5158 print "<tr class=\"dark\">\n";
5159 } else {
5160 print "<tr class=\"light\">\n";
5161 }
5162 $alternate ^= 1;
5163 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
5164 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5165 "<td><i>" . $author . "</i></td>\n" .
5166 "<td>" .
5167 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
5168 -class => "list subject"},
5169 chop_and_escape_str($co{'title'}, 50) . "<br/>");
5170 while (my $setref = shift @files) {
5171 my %set = %$setref;
5172 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
5173 hash=>$set{'id'}, file_name=>$set{'file'}),
5174 -class => "list"},
5175 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
5176 "<br/>\n";
5177 }
5178 print "</td>\n" .
5179 "<td class=\"link\">" .
5180 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5181 " | " .
5182 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5183 print "</td>\n" .
5184 "</tr>\n";
5185 }
5186 %co = parse_commit($1);
5187 }
5188 }
5189 close $fd;
5190
5191 print "</table>\n";
5192 }
5193
5194 if ($searchtype eq 'grep') {
5195 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5196 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5197
5198 print "<table cellspacing=\"0\">\n";
5199 my $alternate = 1;
5200 my $matches = 0;
5201 $/ = "\n";
5202 open my $fd, "-|", git_cmd(), 'grep', '-n', '-i', '-E', $searchtext, $co{'tree'};
5203 my $lastfile = '';
5204 while (my $line = <$fd>) {
5205 chomp $line;
5206 my ($file, $lno, $ltext, $binary);
5207 last if ($matches++ > 1000);
5208 if ($line =~ /^Binary file (.+) matches$/) {
5209 $file = $1;
5210 $binary = 1;
5211 } else {
5212 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
5213 }
5214 if ($file ne $lastfile) {
5215 $lastfile and print "</td></tr>\n";
5216 if ($alternate++) {
5217 print "<tr class=\"dark\">\n";
5218 } else {
5219 print "<tr class=\"light\">\n";
5220 }
5221 print "<td class=\"list\">".
5222 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5223 file_name=>"$file"),
5224 -class => "list"}, esc_path($file));
5225 print "</td><td>\n";
5226 $lastfile = $file;
5227 }
5228 if ($binary) {
5229 print "<div class=\"binary\">Binary file</div>\n";
5230 } else {
5231 $ltext = untabify($ltext);
5232 if ($ltext =~ m/^(.*)($searchtext)(.*)$/i) {
5233 $ltext = esc_html($1, -nbsp=>1);
5234 $ltext .= '<span class="match">';
5235 $ltext .= esc_html($2, -nbsp=>1);
5236 $ltext .= '</span>';
5237 $ltext .= esc_html($3, -nbsp=>1);
5238 } else {
5239 $ltext = esc_html($ltext, -nbsp=>1);
5240 }
5241 print "<div class=\"pre\">" .
5242 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5243 file_name=>"$file").'#l'.$lno,
5244 -class => "linenr"}, sprintf('%4i', $lno))
5245 . ' ' . $ltext . "</div>\n";
5246 }
5247 }
5248 if ($lastfile) {
5249 print "</td></tr>\n";
5250 if ($matches > 1000) {
5251 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
5252 }
5253 } else {
5254 print "<div class=\"diff nodifferences\">No matches found</div>\n";
5255 }
5256 close $fd;
5257
5258 print "</table>\n";
5259 }
5260 git_footer_html();
5261 }
5262
5263 sub git_search_help {
5264 git_header_html();
5265 git_print_page_nav('','', $hash,$hash,$hash);
5266 print <<EOT;
5267 <dl>
5268 <dt><b>commit</b></dt>
5269 <dd>The commit messages and authorship information will be scanned for the given string.</dd>
5270 EOT
5271 my ($have_grep) = gitweb_check_feature('grep');
5272 if ($have_grep) {
5273 print <<EOT;
5274 <dt><b>grep</b></dt>
5275 <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
5276 a different one) are searched for the given
5277 <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a>
5278 (POSIX extended) and the matches are listed. On large
5279 trees, this search can take a while and put some strain on the server, so please use it with
5280 some consideration.</dd>
5281 EOT
5282 }
5283 print <<EOT;
5284 <dt><b>author</b></dt>
5285 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given string.</dd>
5286 <dt><b>committer</b></dt>
5287 <dd>Name and e-mail of the committer and date of commit will be scanned for the given string.</dd>
5288 EOT
5289 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
5290 if ($have_pickaxe) {
5291 print <<EOT;
5292 <dt><b>pickaxe</b></dt>
5293 <dd>All commits that caused the string to appear or disappear from any file (changes that
5294 added, removed or "modified" the string) will be listed. This search can take a while and
5295 takes a lot of strain on the server, so please use it wisely.</dd>
5296 EOT
5297 }
5298 print "</dl>\n";
5299 git_footer_html();
5300 }
5301
5302 sub git_shortlog {
5303 my $head = git_get_head_hash($project);
5304 if (!defined $hash) {
5305 $hash = $head;
5306 }
5307 if (!defined $page) {
5308 $page = 0;
5309 }
5310 my $refs = git_get_references();
5311
5312 my @commitlist = parse_commits($hash, 101, (100 * $page));
5313
5314 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, (100 * ($page+1)));
5315 my $next_link = '';
5316 if ($#commitlist >= 100) {
5317 $next_link =
5318 $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
5319 -accesskey => "n", -title => "Alt-n"}, "next");
5320 }
5321
5322 git_header_html();
5323 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
5324 git_print_header_div('summary', $project);
5325
5326 git_shortlog_body(\@commitlist, 0, 99, $refs, $next_link);
5327
5328 git_footer_html();
5329 }
5330
5331 ## ......................................................................
5332 ## feeds (RSS, Atom; OPML)
5333
5334 sub git_feed {
5335 my $format = shift || 'atom';
5336 my ($have_blame) = gitweb_check_feature('blame');
5337
5338 # Atom: http://www.atomenabled.org/developers/syndication/
5339 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
5340 if ($format ne 'rss' && $format ne 'atom') {
5341 die_error(undef, "Unknown web feed format");
5342 }
5343
5344 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
5345 my $head = $hash || 'HEAD';
5346 my @commitlist = parse_commits($head, 150, 0, undef, $file_name);
5347
5348 my %latest_commit;
5349 my %latest_date;
5350 my $content_type = "application/$format+xml";
5351 if (defined $cgi->http('HTTP_ACCEPT') &&
5352 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
5353 # browser (feed reader) prefers text/xml
5354 $content_type = 'text/xml';
5355 }
5356 if (defined($commitlist[0])) {
5357 %latest_commit = %{$commitlist[0]};
5358 %latest_date = parse_date($latest_commit{'author_epoch'});
5359 print $cgi->header(
5360 -type => $content_type,
5361 -charset => 'utf-8',
5362 -last_modified => $latest_date{'rfc2822'});
5363 } else {
5364 print $cgi->header(
5365 -type => $content_type,
5366 -charset => 'utf-8');
5367 }
5368
5369 # Optimization: skip generating the body if client asks only
5370 # for Last-Modified date.
5371 return if ($cgi->request_method() eq 'HEAD');
5372
5373 # header variables
5374 my $title = "$site_name - $project/$action";
5375 my $feed_type = 'log';
5376 if (defined $hash) {
5377 $title .= " - '$hash'";
5378 $feed_type = 'branch log';
5379 if (defined $file_name) {
5380 $title .= " :: $file_name";
5381 $feed_type = 'history';
5382 }
5383 } elsif (defined $file_name) {
5384 $title .= " - $file_name";
5385 $feed_type = 'history';
5386 }
5387 $title .= " $feed_type";
5388 my $descr = git_get_project_description($project);
5389 if (defined $descr) {
5390 $descr = esc_html($descr);
5391 } else {
5392 $descr = "$project " .
5393 ($format eq 'rss' ? 'RSS' : 'Atom') .
5394 " feed";
5395 }
5396 my $owner = git_get_project_owner($project);
5397 $owner = esc_html($owner);
5398
5399 #header
5400 my $alt_url;
5401 if (defined $file_name) {
5402 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
5403 } elsif (defined $hash) {
5404 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
5405 } else {
5406 $alt_url = href(-full=>1, action=>"summary");
5407 }
5408 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
5409 if ($format eq 'rss') {
5410 print <<XML;
5411 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
5412 <channel>
5413 XML
5414 print "<title>$title</title>\n" .
5415 "<link>$alt_url</link>\n" .
5416 "<description>$descr</description>\n" .
5417 "<language>en</language>\n";
5418 } elsif ($format eq 'atom') {
5419 print <<XML;
5420 <feed xmlns="http://www.w3.org/2005/Atom">
5421 XML
5422 print "<title>$title</title>\n" .
5423 "<subtitle>$descr</subtitle>\n" .
5424 '<link rel="alternate" type="text/html" href="' .
5425 $alt_url . '" />' . "\n" .
5426 '<link rel="self" type="' . $content_type . '" href="' .
5427 $cgi->self_url() . '" />' . "\n" .
5428 "<id>" . href(-full=>1) . "</id>\n" .
5429 # use project owner for feed author
5430 "<author><name>$owner</name></author>\n";
5431 if (defined $favicon) {
5432 print "<icon>" . esc_url($favicon) . "</icon>\n";
5433 }
5434 if (defined $logo_url) {
5435 # not twice as wide as tall: 72 x 27 pixels
5436 print "<logo>" . esc_url($logo) . "</logo>\n";
5437 }
5438 if (! %latest_date) {
5439 # dummy date to keep the feed valid until commits trickle in:
5440 print "<updated>1970-01-01T00:00:00Z</updated>\n";
5441 } else {
5442 print "<updated>$latest_date{'iso-8601'}</updated>\n";
5443 }
5444 }
5445
5446 # contents
5447 for (my $i = 0; $i <= $#commitlist; $i++) {
5448 my %co = %{$commitlist[$i]};
5449 my $commit = $co{'id'};
5450 # we read 150, we always show 30 and the ones more recent than 48 hours
5451 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
5452 last;
5453 }
5454 my %cd = parse_date($co{'author_epoch'});
5455
5456 # get list of changed files
5457 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5458 $co{'parent'} || "--root",
5459 $co{'id'}, "--", (defined $file_name ? $file_name : ())
5460 or next;
5461 my @difftree = map { chomp; $_ } <$fd>;
5462 close $fd
5463 or next;
5464
5465 # print element (entry, item)
5466 my $co_url = href(-full=>1, action=>"commit", hash=>$commit);
5467 if ($format eq 'rss') {
5468 print "<item>\n" .
5469 "<title>" . esc_html($co{'title'}) . "</title>\n" .
5470 "<author>" . esc_html($co{'author'}) . "</author>\n" .
5471 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
5472 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
5473 "<link>$co_url</link>\n" .
5474 "<description>" . esc_html($co{'title'}) . "</description>\n" .
5475 "<content:encoded>" .
5476 "<![CDATA[\n";
5477 } elsif ($format eq 'atom') {
5478 print "<entry>\n" .
5479 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
5480 "<updated>$cd{'iso-8601'}</updated>\n" .
5481 "<author>\n" .
5482 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
5483 if ($co{'author_email'}) {
5484 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
5485 }
5486 print "</author>\n" .
5487 # use committer for contributor
5488 "<contributor>\n" .
5489 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
5490 if ($co{'committer_email'}) {
5491 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
5492 }
5493 print "</contributor>\n" .
5494 "<published>$cd{'iso-8601'}</published>\n" .
5495 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
5496 "<id>$co_url</id>\n" .
5497 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
5498 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
5499 }
5500 my $comment = $co{'comment'};
5501 print "<pre>\n";
5502 foreach my $line (@$comment) {
5503 $line = esc_html($line);
5504 print "$line\n";
5505 }
5506 print "</pre><ul>\n";
5507 foreach my $difftree_line (@difftree) {
5508 my %difftree = parse_difftree_raw_line($difftree_line);
5509 next if !$difftree{'from_id'};
5510
5511 my $file = $difftree{'file'} || $difftree{'to_file'};
5512
5513 print "<li>" .
5514 "[" .
5515 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
5516 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
5517 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
5518 file_name=>$file, file_parent=>$difftree{'from_file'}),
5519 -title => "diff"}, 'D');
5520 if ($have_blame) {
5521 print $cgi->a({-href => href(-full=>1, action=>"blame",
5522 file_name=>$file, hash_base=>$commit),
5523 -title => "blame"}, 'B');
5524 }
5525 # if this is not a feed of a file history
5526 if (!defined $file_name || $file_name ne $file) {
5527 print $cgi->a({-href => href(-full=>1, action=>"history",
5528 file_name=>$file, hash=>$commit),
5529 -title => "history"}, 'H');
5530 }
5531 $file = esc_path($file);
5532 print "] ".
5533 "$file</li>\n";
5534 }
5535 if ($format eq 'rss') {
5536 print "</ul>]]>\n" .
5537 "</content:encoded>\n" .
5538 "</item>\n";
5539 } elsif ($format eq 'atom') {
5540 print "</ul>\n</div>\n" .
5541 "</content>\n" .
5542 "</entry>\n";
5543 }
5544 }
5545
5546 # end of feed
5547 if ($format eq 'rss') {
5548 print "</channel>\n</rss>\n";
5549 } elsif ($format eq 'atom') {
5550 print "</feed>\n";
5551 }
5552 }
5553
5554 sub git_rss {
5555 git_feed('rss');
5556 }
5557
5558 sub git_atom {
5559 git_feed('atom');
5560 }
5561
5562 sub git_opml {
5563 my @list = git_get_projects_list();
5564
5565 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
5566 print <<XML;
5567 <?xml version="1.0" encoding="utf-8"?>
5568 <opml version="1.0">
5569 <head>
5570 <title>$site_name OPML Export</title>
5571 </head>
5572 <body>
5573 <outline text="git RSS feeds">
5574 XML
5575
5576 foreach my $pr (@list) {
5577 my %proj = %$pr;
5578 my $head = git_get_head_hash($proj{'path'});
5579 if (!defined $head) {
5580 next;
5581 }
5582 $git_dir = "$projectroot/$proj{'path'}";
5583 my %co = parse_commit($head);
5584 if (!%co) {
5585 next;
5586 }
5587
5588 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
5589 my $rss = "$my_url?p=$proj{'path'};a=rss";
5590 my $html = "$my_url?p=$proj{'path'};a=summary";
5591 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
5592 }
5593 print <<XML;
5594 </outline>
5595 </body>
5596 </opml>
5597 XML
5598 }
This page took 4.866222 seconds and 5 git commands to generate.