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