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