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