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