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