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