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