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