Reteach builtin-ls-remote to understand remotes
[git/git.git] / gitweb / gitweb.perl
1 #!/usr/bin/perl
2
3 # gitweb - simple web interface to track changes in git repositories
4 #
5 # (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org>
6 # (C) 2005, Christian Gierke
7 #
8 # This program is licensed under the GPLv2
9
10 use strict;
11 use warnings;
12 use CGI qw(:standard :escapeHTML -nosticky);
13 use CGI::Util qw(unescape);
14 use CGI::Carp qw(fatalsToBrowser);
15 use Encode;
16 use Fcntl ':mode';
17 use File::Find qw();
18 use File::Basename qw(basename);
19 binmode STDOUT, ':utf8';
20
21 BEGIN {
22 CGI->compile() if $ENV{'MOD_PERL'};
23 }
24
25 our $cgi = new CGI;
26 our $version = "++GIT_VERSION++";
27 our $my_url = $cgi->url();
28 our $my_uri = $cgi->url(-absolute => 1);
29
30 # core git executable to use
31 # this can just be "git" if your webserver has a sensible PATH
32 our $GIT = "++GIT_BINDIR++/git";
33
34 # absolute fs-path which will be prepended to the project path
35 #our $projectroot = "/pub/scm";
36 our $projectroot = "++GITWEB_PROJECTROOT++";
37
38 # fs traversing limit for getting project list
39 # the number is relative to the projectroot
40 our $project_maxdepth = "++GITWEB_PROJECT_MAXDEPTH++";
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 our $home_link_str = "++GITWEB_HOME_LINK_STR++";
47
48 # name of your site or organization to appear in page titles
49 # replace this with something more descriptive for clearer bookmarks
50 our $site_name = "++GITWEB_SITENAME++"
51 || ($ENV{'SERVER_NAME'} || "Untitled") . " Git";
52
53 # filename of html text to include at top of each page
54 our $site_header = "++GITWEB_SITE_HEADER++";
55 # html text to include at home page
56 our $home_text = "++GITWEB_HOMETEXT++";
57 # filename of html text to include at bottom of each page
58 our $site_footer = "++GITWEB_SITE_FOOTER++";
59
60 # URI of stylesheets
61 our @stylesheets = ("++GITWEB_CSS++");
62 # URI of a single stylesheet, which can be overridden in GITWEB_CONFIG.
63 our $stylesheet = undef;
64 # URI of GIT logo (72x27 size)
65 our $logo = "++GITWEB_LOGO++";
66 # URI of GIT favicon, assumed to be image/png type
67 our $favicon = "++GITWEB_FAVICON++";
68
69 # URI and label (title) of GIT logo link
70 #our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
71 #our $logo_label = "git documentation";
72 our $logo_url = "http://git.or.cz/";
73 our $logo_label = "git homepage";
74
75 # source of projects list
76 our $projects_list = "++GITWEB_LIST++";
77
78 # the width (in characters) of the projects list "Description" column
79 our $projects_list_description_width = 25;
80
81 # default order of projects list
82 # valid values are none, project, descr, owner, and age
83 our $default_projects_order = "project";
84
85 # show repository only if this file exists
86 # (only effective if this variable evaluates to true)
87 our $export_ok = "++GITWEB_EXPORT_OK++";
88
89 # only allow viewing of repositories also shown on the overview page
90 our $strict_export = "++GITWEB_STRICT_EXPORT++";
91
92 # list of git base URLs used for URL to where fetch project from,
93 # i.e. full URL is "$git_base_url/$project"
94 our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
95
96 # default blob_plain mimetype and default charset for text/plain blob
97 our $default_blob_plain_mimetype = 'text/plain';
98 our $default_text_plain_charset = undef;
99
100 # file to use for guessing MIME types before trying /etc/mime.types
101 # (relative to the current git repository)
102 our $mimetypes_file = undef;
103
104 # assume this charset if line contains non-UTF-8 characters;
105 # it should be valid encoding (see Encoding::Supported(3pm) for list),
106 # for which encoding all byte sequences are valid, for example
107 # 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
108 # could be even 'utf-8' for the old behavior)
109 our $fallback_encoding = 'latin1';
110
111 # rename detection options for git-diff and git-diff-tree
112 # - default is '-M', with the cost proportional to
113 # (number of removed files) * (number of new files).
114 # - more costly is '-C' (which implies '-M'), with the cost proportional to
115 # (number of changed files + number of removed files) * (number of new files)
116 # - even more costly is '-C', '--find-copies-harder' with cost
117 # (number of files in the original tree) * (number of new files)
118 # - one might want to include '-B' option, e.g. '-B', '-M'
119 our @diff_opts = ('-M'); # taken from git_commit
120
121 # information about snapshot formats that gitweb is capable of serving
122 our %known_snapshot_formats = (
123 # name => {
124 # 'display' => display name,
125 # 'type' => mime type,
126 # 'suffix' => filename suffix,
127 # 'format' => --format for git-archive,
128 # 'compressor' => [compressor command and arguments]
129 # (array reference, optional)}
130 #
131 'tgz' => {
132 'display' => 'tar.gz',
133 'type' => 'application/x-gzip',
134 'suffix' => '.tar.gz',
135 'format' => 'tar',
136 'compressor' => ['gzip']},
137
138 'tbz2' => {
139 'display' => 'tar.bz2',
140 'type' => 'application/x-bzip2',
141 'suffix' => '.tar.bz2',
142 'format' => 'tar',
143 'compressor' => ['bzip2']},
144
145 'zip' => {
146 'display' => 'zip',
147 'type' => 'application/x-zip',
148 'suffix' => '.zip',
149 'format' => 'zip'},
150 );
151
152 # Aliases so we understand old gitweb.snapshot values in repository
153 # configuration.
154 our %known_snapshot_format_aliases = (
155 'gzip' => 'tgz',
156 'bzip2' => 'tbz2',
157
158 # backward compatibility: legacy gitweb config support
159 'x-gzip' => undef, 'gz' => undef,
160 'x-bzip2' => undef, 'bz2' => undef,
161 'x-zip' => undef, '' => undef,
162 );
163
164 # You define site-wide feature defaults here; override them with
165 # $GITWEB_CONFIG as necessary.
166 our %feature = (
167 # feature => {
168 # 'sub' => feature-sub (subroutine),
169 # 'override' => allow-override (boolean),
170 # 'default' => [ default options...] (array reference)}
171 #
172 # if feature is overridable (it means that allow-override has true value),
173 # then feature-sub will be called with default options as parameters;
174 # return value of feature-sub indicates if to enable specified feature
175 #
176 # if there is no 'sub' key (no feature-sub), then feature cannot be
177 # overriden
178 #
179 # use gitweb_check_feature(<feature>) to check if <feature> is enabled
180
181 # Enable the 'blame' blob view, showing the last commit that modified
182 # each line in the file. This can be very CPU-intensive.
183
184 # To enable system wide have in $GITWEB_CONFIG
185 # $feature{'blame'}{'default'} = [1];
186 # To have project specific config enable override in $GITWEB_CONFIG
187 # $feature{'blame'}{'override'} = 1;
188 # and in project config gitweb.blame = 0|1;
189 'blame' => {
190 'sub' => \&feature_blame,
191 'override' => 0,
192 'default' => [0]},
193
194 # Enable the 'snapshot' link, providing a compressed archive of any
195 # tree. This can potentially generate high traffic if you have large
196 # project.
197
198 # Value is a list of formats defined in %known_snapshot_formats that
199 # you wish to offer.
200 # To disable system wide have in $GITWEB_CONFIG
201 # $feature{'snapshot'}{'default'} = [];
202 # To have project specific config enable override in $GITWEB_CONFIG
203 # $feature{'snapshot'}{'override'} = 1;
204 # and in project config, a comma-separated list of formats or "none"
205 # to disable. Example: gitweb.snapshot = tbz2,zip;
206 'snapshot' => {
207 'sub' => \&feature_snapshot,
208 'override' => 0,
209 'default' => ['tgz']},
210
211 # Enable text search, which will list the commits which match author,
212 # committer or commit text to a given string. Enabled by default.
213 # Project specific override is not supported.
214 'search' => {
215 'override' => 0,
216 'default' => [1]},
217
218 # Enable grep search, which will list the files in currently selected
219 # tree containing the given string. Enabled by default. This can be
220 # potentially CPU-intensive, of course.
221
222 # To enable system wide have in $GITWEB_CONFIG
223 # $feature{'grep'}{'default'} = [1];
224 # To have project specific config enable override in $GITWEB_CONFIG
225 # $feature{'grep'}{'override'} = 1;
226 # and in project config gitweb.grep = 0|1;
227 'grep' => {
228 'override' => 0,
229 'default' => [1]},
230
231 # Enable the pickaxe search, which will list the commits that modified
232 # a given string in a file. This can be practical and quite faster
233 # alternative to 'blame', but still potentially CPU-intensive.
234
235 # To enable system wide have in $GITWEB_CONFIG
236 # $feature{'pickaxe'}{'default'} = [1];
237 # To have project specific config enable override in $GITWEB_CONFIG
238 # $feature{'pickaxe'}{'override'} = 1;
239 # and in project config gitweb.pickaxe = 0|1;
240 'pickaxe' => {
241 'sub' => \&feature_pickaxe,
242 'override' => 0,
243 'default' => [1]},
244
245 # Make gitweb use an alternative format of the URLs which can be
246 # more readable and natural-looking: project name is embedded
247 # directly in the path and the query string contains other
248 # auxiliary information. All gitweb installations recognize
249 # URL in either format; this configures in which formats gitweb
250 # generates links.
251
252 # To enable system wide have in $GITWEB_CONFIG
253 # $feature{'pathinfo'}{'default'} = [1];
254 # Project specific override is not supported.
255
256 # Note that you will need to change the default location of CSS,
257 # favicon, logo and possibly other files to an absolute URL. Also,
258 # if gitweb.cgi serves as your indexfile, you will need to force
259 # $my_uri to contain the script name in your $GITWEB_CONFIG.
260 'pathinfo' => {
261 'override' => 0,
262 'default' => [0]},
263
264 # Make gitweb consider projects in project root subdirectories
265 # to be forks of existing projects. Given project $projname.git,
266 # projects matching $projname/*.git will not be shown in the main
267 # projects list, instead a '+' mark will be added to $projname
268 # there and a 'forks' view will be enabled for the project, listing
269 # all the forks. If project list is taken from a file, forks have
270 # to be listed after the main project.
271
272 # To enable system wide have in $GITWEB_CONFIG
273 # $feature{'forks'}{'default'} = [1];
274 # Project specific override is not supported.
275 'forks' => {
276 'override' => 0,
277 'default' => [0]},
278 );
279
280 sub gitweb_check_feature {
281 my ($name) = @_;
282 return unless exists $feature{$name};
283 my ($sub, $override, @defaults) = (
284 $feature{$name}{'sub'},
285 $feature{$name}{'override'},
286 @{$feature{$name}{'default'}});
287 if (!$override) { return @defaults; }
288 if (!defined $sub) {
289 warn "feature $name is not overrideable";
290 return @defaults;
291 }
292 return $sub->(@defaults);
293 }
294
295 sub feature_blame {
296 my ($val) = git_get_project_config('blame', '--bool');
297
298 if ($val eq 'true') {
299 return 1;
300 } elsif ($val eq 'false') {
301 return 0;
302 }
303
304 return $_[0];
305 }
306
307 sub feature_snapshot {
308 my (@fmts) = @_;
309
310 my ($val) = git_get_project_config('snapshot');
311
312 if ($val) {
313 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
314 }
315
316 return @fmts;
317 }
318
319 sub feature_grep {
320 my ($val) = git_get_project_config('grep', '--bool');
321
322 if ($val eq 'true') {
323 return (1);
324 } elsif ($val eq 'false') {
325 return (0);
326 }
327
328 return ($_[0]);
329 }
330
331 sub feature_pickaxe {
332 my ($val) = git_get_project_config('pickaxe', '--bool');
333
334 if ($val eq 'true') {
335 return (1);
336 } elsif ($val eq 'false') {
337 return (0);
338 }
339
340 return ($_[0]);
341 }
342
343 # checking HEAD file with -e is fragile if the repository was
344 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
345 # and then pruned.
346 sub check_head_link {
347 my ($dir) = @_;
348 my $headfile = "$dir/HEAD";
349 return ((-e $headfile) ||
350 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
351 }
352
353 sub check_export_ok {
354 my ($dir) = @_;
355 return (check_head_link($dir) &&
356 (!$export_ok || -e "$dir/$export_ok"));
357 }
358
359 # process alternate names for backward compatibility
360 # filter out unsupported (unknown) snapshot formats
361 sub filter_snapshot_fmts {
362 my @fmts = @_;
363
364 @fmts = map {
365 exists $known_snapshot_format_aliases{$_} ?
366 $known_snapshot_format_aliases{$_} : $_} @fmts;
367 @fmts = grep(exists $known_snapshot_formats{$_}, @fmts);
368
369 }
370
371 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
372 do $GITWEB_CONFIG if -e $GITWEB_CONFIG;
373
374 # version of the core git binary
375 our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
376
377 $projects_list ||= $projectroot;
378
379 # ======================================================================
380 # input validation and dispatch
381 our $action = $cgi->param('a');
382 if (defined $action) {
383 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
384 die_error(undef, "Invalid action parameter");
385 }
386 }
387
388 # parameters which are pathnames
389 our $project = $cgi->param('p');
390 if (defined $project) {
391 if (!validate_pathname($project) ||
392 !(-d "$projectroot/$project") ||
393 !check_head_link("$projectroot/$project") ||
394 ($export_ok && !(-e "$projectroot/$project/$export_ok")) ||
395 ($strict_export && !project_in_list($project))) {
396 undef $project;
397 die_error(undef, "No such project");
398 }
399 }
400
401 our $file_name = $cgi->param('f');
402 if (defined $file_name) {
403 if (!validate_pathname($file_name)) {
404 die_error(undef, "Invalid file parameter");
405 }
406 }
407
408 our $file_parent = $cgi->param('fp');
409 if (defined $file_parent) {
410 if (!validate_pathname($file_parent)) {
411 die_error(undef, "Invalid file parent parameter");
412 }
413 }
414
415 # parameters which are refnames
416 our $hash = $cgi->param('h');
417 if (defined $hash) {
418 if (!validate_refname($hash)) {
419 die_error(undef, "Invalid hash parameter");
420 }
421 }
422
423 our $hash_parent = $cgi->param('hp');
424 if (defined $hash_parent) {
425 if (!validate_refname($hash_parent)) {
426 die_error(undef, "Invalid hash parent parameter");
427 }
428 }
429
430 our $hash_base = $cgi->param('hb');
431 if (defined $hash_base) {
432 if (!validate_refname($hash_base)) {
433 die_error(undef, "Invalid hash base parameter");
434 }
435 }
436
437 my %allowed_options = (
438 "--no-merges" => [ qw(rss atom log shortlog history) ],
439 );
440
441 our @extra_options = $cgi->param('opt');
442 if (defined @extra_options) {
443 foreach my $opt (@extra_options) {
444 if (not exists $allowed_options{$opt}) {
445 die_error(undef, "Invalid option parameter");
446 }
447 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
448 die_error(undef, "Invalid option parameter for this action");
449 }
450 }
451 }
452
453 our $hash_parent_base = $cgi->param('hpb');
454 if (defined $hash_parent_base) {
455 if (!validate_refname($hash_parent_base)) {
456 die_error(undef, "Invalid hash parent base parameter");
457 }
458 }
459
460 # other parameters
461 our $page = $cgi->param('pg');
462 if (defined $page) {
463 if ($page =~ m/[^0-9]/) {
464 die_error(undef, "Invalid page parameter");
465 }
466 }
467
468 our $searchtype = $cgi->param('st');
469 if (defined $searchtype) {
470 if ($searchtype =~ m/[^a-z]/) {
471 die_error(undef, "Invalid searchtype parameter");
472 }
473 }
474
475 our $searchtext = $cgi->param('s');
476 our $search_regexp;
477 if (defined $searchtext) {
478 if (length($searchtext) < 2) {
479 die_error(undef, "At least two characters are required for search parameter");
480 }
481 $search_regexp = quotemeta $searchtext;
482 }
483
484 # now read PATH_INFO and use it as alternative to parameters
485 sub evaluate_path_info {
486 return if defined $project;
487 my $path_info = $ENV{"PATH_INFO"};
488 return if !$path_info;
489 $path_info =~ s,^/+,,;
490 return if !$path_info;
491 # find which part of PATH_INFO is project
492 $project = $path_info;
493 $project =~ s,/+$,,;
494 while ($project && !check_head_link("$projectroot/$project")) {
495 $project =~ s,/*[^/]*$,,;
496 }
497 # validate project
498 $project = validate_pathname($project);
499 if (!$project ||
500 ($export_ok && !-e "$projectroot/$project/$export_ok") ||
501 ($strict_export && !project_in_list($project))) {
502 undef $project;
503 return;
504 }
505 # do not change any parameters if an action is given using the query string
506 return if $action;
507 $path_info =~ s,^$project/*,,;
508 my ($refname, $pathname) = split(/:/, $path_info, 2);
509 if (defined $pathname) {
510 # we got "project.git/branch:filename" or "project.git/branch:dir/"
511 # we could use git_get_type(branch:pathname), but it needs $git_dir
512 $pathname =~ s,^/+,,;
513 if (!$pathname || substr($pathname, -1) eq "/") {
514 $action ||= "tree";
515 $pathname =~ s,/$,,;
516 } else {
517 $action ||= "blob_plain";
518 }
519 $hash_base ||= validate_refname($refname);
520 $file_name ||= validate_pathname($pathname);
521 } elsif (defined $refname) {
522 # we got "project.git/branch"
523 $action ||= "shortlog";
524 $hash ||= validate_refname($refname);
525 }
526 }
527 evaluate_path_info();
528
529 # path to the current git repository
530 our $git_dir;
531 $git_dir = "$projectroot/$project" if $project;
532
533 # dispatch
534 my %actions = (
535 "blame" => \&git_blame2,
536 "blobdiff" => \&git_blobdiff,
537 "blobdiff_plain" => \&git_blobdiff_plain,
538 "blob" => \&git_blob,
539 "blob_plain" => \&git_blob_plain,
540 "commitdiff" => \&git_commitdiff,
541 "commitdiff_plain" => \&git_commitdiff_plain,
542 "commit" => \&git_commit,
543 "forks" => \&git_forks,
544 "heads" => \&git_heads,
545 "history" => \&git_history,
546 "log" => \&git_log,
547 "rss" => \&git_rss,
548 "atom" => \&git_atom,
549 "search" => \&git_search,
550 "search_help" => \&git_search_help,
551 "shortlog" => \&git_shortlog,
552 "summary" => \&git_summary,
553 "tag" => \&git_tag,
554 "tags" => \&git_tags,
555 "tree" => \&git_tree,
556 "snapshot" => \&git_snapshot,
557 "object" => \&git_object,
558 # those below don't need $project
559 "opml" => \&git_opml,
560 "project_list" => \&git_project_list,
561 "project_index" => \&git_project_index,
562 );
563
564 if (!defined $action) {
565 if (defined $hash) {
566 $action = git_get_type($hash);
567 } elsif (defined $hash_base && defined $file_name) {
568 $action = git_get_type("$hash_base:$file_name");
569 } elsif (defined $project) {
570 $action = 'summary';
571 } else {
572 $action = 'project_list';
573 }
574 }
575 if (!defined($actions{$action})) {
576 die_error(undef, "Unknown action");
577 }
578 if ($action !~ m/^(opml|project_list|project_index)$/ &&
579 !$project) {
580 die_error(undef, "Project needed");
581 }
582 $actions{$action}->();
583 exit;
584
585 ## ======================================================================
586 ## action links
587
588 sub href(%) {
589 my %params = @_;
590 # default is to use -absolute url() i.e. $my_uri
591 my $href = $params{-full} ? $my_url : $my_uri;
592
593 # XXX: Warning: If you touch this, check the search form for updating,
594 # too.
595
596 my @mapping = (
597 project => "p",
598 action => "a",
599 file_name => "f",
600 file_parent => "fp",
601 hash => "h",
602 hash_parent => "hp",
603 hash_base => "hb",
604 hash_parent_base => "hpb",
605 page => "pg",
606 order => "o",
607 searchtext => "s",
608 searchtype => "st",
609 snapshot_format => "sf",
610 extra_options => "opt",
611 );
612 my %mapping = @mapping;
613
614 $params{'project'} = $project unless exists $params{'project'};
615
616 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
617 if ($use_pathinfo) {
618 # use PATH_INFO for project name
619 $href .= "/$params{'project'}" if defined $params{'project'};
620 delete $params{'project'};
621
622 # Summary just uses the project path URL
623 if (defined $params{'action'} && $params{'action'} eq 'summary') {
624 delete $params{'action'};
625 }
626 }
627
628 # now encode the parameters explicitly
629 my @result = ();
630 for (my $i = 0; $i < @mapping; $i += 2) {
631 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
632 if (defined $params{$name}) {
633 if (ref($params{$name}) eq "ARRAY") {
634 foreach my $par (@{$params{$name}}) {
635 push @result, $symbol . "=" . esc_param($par);
636 }
637 } else {
638 push @result, $symbol . "=" . esc_param($params{$name});
639 }
640 }
641 }
642 $href .= "?" . join(';', @result) if scalar @result;
643
644 return $href;
645 }
646
647
648 ## ======================================================================
649 ## validation, quoting/unquoting and escaping
650
651 sub validate_pathname {
652 my $input = shift || return undef;
653
654 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
655 # at the beginning, at the end, and between slashes.
656 # also this catches doubled slashes
657 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
658 return undef;
659 }
660 # no null characters
661 if ($input =~ m!\0!) {
662 return undef;
663 }
664 return $input;
665 }
666
667 sub validate_refname {
668 my $input = shift || return undef;
669
670 # textual hashes are O.K.
671 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
672 return $input;
673 }
674 # it must be correct pathname
675 $input = validate_pathname($input)
676 or return undef;
677 # restrictions on ref name according to git-check-ref-format
678 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
679 return undef;
680 }
681 return $input;
682 }
683
684 # decode sequences of octets in utf8 into Perl's internal form,
685 # which is utf-8 with utf8 flag set if needed. gitweb writes out
686 # in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
687 sub to_utf8 {
688 my $str = shift;
689 my $res;
690 eval { $res = decode_utf8($str, Encode::FB_CROAK); };
691 if (defined $res) {
692 return $res;
693 } else {
694 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
695 }
696 }
697
698 # quote unsafe chars, but keep the slash, even when it's not
699 # correct, but quoted slashes look too horrible in bookmarks
700 sub esc_param {
701 my $str = shift;
702 $str =~ s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X", ord($1))/eg;
703 $str =~ s/\+/%2B/g;
704 $str =~ s/ /\+/g;
705 return $str;
706 }
707
708 # quote unsafe chars in whole URL, so some charactrs cannot be quoted
709 sub esc_url {
710 my $str = shift;
711 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
712 $str =~ s/\+/%2B/g;
713 $str =~ s/ /\+/g;
714 return $str;
715 }
716
717 # replace invalid utf8 character with SUBSTITUTION sequence
718 sub esc_html ($;%) {
719 my $str = shift;
720 my %opts = @_;
721
722 $str = to_utf8($str);
723 $str = $cgi->escapeHTML($str);
724 if ($opts{'-nbsp'}) {
725 $str =~ s/ /&nbsp;/g;
726 }
727 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
728 return $str;
729 }
730
731 # quote control characters and escape filename to HTML
732 sub esc_path {
733 my $str = shift;
734 my %opts = @_;
735
736 $str = to_utf8($str);
737 $str = $cgi->escapeHTML($str);
738 if ($opts{'-nbsp'}) {
739 $str =~ s/ /&nbsp;/g;
740 }
741 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
742 return $str;
743 }
744
745 # Make control characters "printable", using character escape codes (CEC)
746 sub quot_cec {
747 my $cntrl = shift;
748 my %es = ( # character escape codes, aka escape sequences
749 "\t" => '\t', # tab (HT)
750 "\n" => '\n', # line feed (LF)
751 "\r" => '\r', # carrige return (CR)
752 "\f" => '\f', # form feed (FF)
753 "\b" => '\b', # backspace (BS)
754 "\a" => '\a', # alarm (bell) (BEL)
755 "\e" => '\e', # escape (ESC)
756 "\013" => '\v', # vertical tab (VT)
757 "\000" => '\0', # nul character (NUL)
758 );
759 my $chr = ( (exists $es{$cntrl})
760 ? $es{$cntrl}
761 : sprintf('\%03o', ord($cntrl)) );
762 return "<span class=\"cntrl\">$chr</span>";
763 }
764
765 # Alternatively use unicode control pictures codepoints,
766 # Unicode "printable representation" (PR)
767 sub quot_upr {
768 my $cntrl = shift;
769 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
770 return "<span class=\"cntrl\">$chr</span>";
771 }
772
773 # git may return quoted and escaped filenames
774 sub unquote {
775 my $str = shift;
776
777 sub unq {
778 my $seq = shift;
779 my %es = ( # character escape codes, aka escape sequences
780 't' => "\t", # tab (HT, TAB)
781 'n' => "\n", # newline (NL)
782 'r' => "\r", # return (CR)
783 'f' => "\f", # form feed (FF)
784 'b' => "\b", # backspace (BS)
785 'a' => "\a", # alarm (bell) (BEL)
786 'e' => "\e", # escape (ESC)
787 'v' => "\013", # vertical tab (VT)
788 );
789
790 if ($seq =~ m/^[0-7]{1,3}$/) {
791 # octal char sequence
792 return chr(oct($seq));
793 } elsif (exists $es{$seq}) {
794 # C escape sequence, aka character escape code
795 return $es{$seq}
796 }
797 # quoted ordinary character
798 return $seq;
799 }
800
801 if ($str =~ m/^"(.*)"$/) {
802 # needs unquoting
803 $str = $1;
804 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
805 }
806 return $str;
807 }
808
809 # escape tabs (convert tabs to spaces)
810 sub untabify {
811 my $line = shift;
812
813 while ((my $pos = index($line, "\t")) != -1) {
814 if (my $count = (8 - ($pos % 8))) {
815 my $spaces = ' ' x $count;
816 $line =~ s/\t/$spaces/;
817 }
818 }
819
820 return $line;
821 }
822
823 sub project_in_list {
824 my $project = shift;
825 my @list = git_get_projects_list();
826 return @list && scalar(grep { $_->{'path'} eq $project } @list);
827 }
828
829 ## ----------------------------------------------------------------------
830 ## HTML aware string manipulation
831
832 sub chop_str {
833 my $str = shift;
834 my $len = shift;
835 my $add_len = shift || 10;
836
837 # allow only $len chars, but don't cut a word if it would fit in $add_len
838 # if it doesn't fit, cut it if it's still longer than the dots we would add
839 $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
840 my $body = $1;
841 my $tail = $2;
842 if (length($tail) > 4) {
843 $tail = " ...";
844 $body =~ s/&[^;]*$//; # remove chopped character entities
845 }
846 return "$body$tail";
847 }
848
849 # takes the same arguments as chop_str, but also wraps a <span> around the
850 # result with a title attribute if it does get chopped. Additionally, the
851 # string is HTML-escaped.
852 sub chop_and_escape_str {
853 my $str = shift;
854 my $len = shift;
855 my $add_len = shift || 10;
856
857 my $chopped = chop_str($str, $len, $add_len);
858 if ($chopped eq $str) {
859 return esc_html($chopped);
860 } else {
861 return qq{<span title="} . esc_html($str) . qq{">} .
862 esc_html($chopped) . qq{</span>};
863 }
864 }
865
866 ## ----------------------------------------------------------------------
867 ## functions returning short strings
868
869 # CSS class for given age value (in seconds)
870 sub age_class {
871 my $age = shift;
872
873 if (!defined $age) {
874 return "noage";
875 } elsif ($age < 60*60*2) {
876 return "age0";
877 } elsif ($age < 60*60*24*2) {
878 return "age1";
879 } else {
880 return "age2";
881 }
882 }
883
884 # convert age in seconds to "nn units ago" string
885 sub age_string {
886 my $age = shift;
887 my $age_str;
888
889 if ($age > 60*60*24*365*2) {
890 $age_str = (int $age/60/60/24/365);
891 $age_str .= " years ago";
892 } elsif ($age > 60*60*24*(365/12)*2) {
893 $age_str = int $age/60/60/24/(365/12);
894 $age_str .= " months ago";
895 } elsif ($age > 60*60*24*7*2) {
896 $age_str = int $age/60/60/24/7;
897 $age_str .= " weeks ago";
898 } elsif ($age > 60*60*24*2) {
899 $age_str = int $age/60/60/24;
900 $age_str .= " days ago";
901 } elsif ($age > 60*60*2) {
902 $age_str = int $age/60/60;
903 $age_str .= " hours ago";
904 } elsif ($age > 60*2) {
905 $age_str = int $age/60;
906 $age_str .= " min ago";
907 } elsif ($age > 2) {
908 $age_str = int $age;
909 $age_str .= " sec ago";
910 } else {
911 $age_str .= " right now";
912 }
913 return $age_str;
914 }
915
916 use constant {
917 S_IFINVALID => 0030000,
918 S_IFGITLINK => 0160000,
919 };
920
921 # submodule/subproject, a commit object reference
922 sub S_ISGITLINK($) {
923 my $mode = shift;
924
925 return (($mode & S_IFMT) == S_IFGITLINK)
926 }
927
928 # convert file mode in octal to symbolic file mode string
929 sub mode_str {
930 my $mode = oct shift;
931
932 if (S_ISGITLINK($mode)) {
933 return 'm---------';
934 } elsif (S_ISDIR($mode & S_IFMT)) {
935 return 'drwxr-xr-x';
936 } elsif (S_ISLNK($mode)) {
937 return 'lrwxrwxrwx';
938 } elsif (S_ISREG($mode)) {
939 # git cares only about the executable bit
940 if ($mode & S_IXUSR) {
941 return '-rwxr-xr-x';
942 } else {
943 return '-rw-r--r--';
944 };
945 } else {
946 return '----------';
947 }
948 }
949
950 # convert file mode in octal to file type string
951 sub file_type {
952 my $mode = shift;
953
954 if ($mode !~ m/^[0-7]+$/) {
955 return $mode;
956 } else {
957 $mode = oct $mode;
958 }
959
960 if (S_ISGITLINK($mode)) {
961 return "submodule";
962 } elsif (S_ISDIR($mode & S_IFMT)) {
963 return "directory";
964 } elsif (S_ISLNK($mode)) {
965 return "symlink";
966 } elsif (S_ISREG($mode)) {
967 return "file";
968 } else {
969 return "unknown";
970 }
971 }
972
973 # convert file mode in octal to file type description string
974 sub file_type_long {
975 my $mode = shift;
976
977 if ($mode !~ m/^[0-7]+$/) {
978 return $mode;
979 } else {
980 $mode = oct $mode;
981 }
982
983 if (S_ISGITLINK($mode)) {
984 return "submodule";
985 } elsif (S_ISDIR($mode & S_IFMT)) {
986 return "directory";
987 } elsif (S_ISLNK($mode)) {
988 return "symlink";
989 } elsif (S_ISREG($mode)) {
990 if ($mode & S_IXUSR) {
991 return "executable";
992 } else {
993 return "file";
994 };
995 } else {
996 return "unknown";
997 }
998 }
999
1000
1001 ## ----------------------------------------------------------------------
1002 ## functions returning short HTML fragments, or transforming HTML fragments
1003 ## which don't belong to other sections
1004
1005 # format line of commit message.
1006 sub format_log_line_html {
1007 my $line = shift;
1008
1009 $line = esc_html($line, -nbsp=>1);
1010 if ($line =~ m/([0-9a-fA-F]{8,40})/) {
1011 my $hash_text = $1;
1012 my $link =
1013 $cgi->a({-href => href(action=>"object", hash=>$hash_text),
1014 -class => "text"}, $hash_text);
1015 $line =~ s/$hash_text/$link/;
1016 }
1017 return $line;
1018 }
1019
1020 # format marker of refs pointing to given object
1021 sub format_ref_marker {
1022 my ($refs, $id) = @_;
1023 my $markers = '';
1024
1025 if (defined $refs->{$id}) {
1026 foreach my $ref (@{$refs->{$id}}) {
1027 my ($type, $name) = qw();
1028 # e.g. tags/v2.6.11 or heads/next
1029 if ($ref =~ m!^(.*?)s?/(.*)$!) {
1030 $type = $1;
1031 $name = $2;
1032 } else {
1033 $type = "ref";
1034 $name = $ref;
1035 }
1036
1037 $markers .= " <span class=\"$type\" title=\"$ref\">" .
1038 esc_html($name) . "</span>";
1039 }
1040 }
1041
1042 if ($markers) {
1043 return ' <span class="refs">'. $markers . '</span>';
1044 } else {
1045 return "";
1046 }
1047 }
1048
1049 # format, perhaps shortened and with markers, title line
1050 sub format_subject_html {
1051 my ($long, $short, $href, $extra) = @_;
1052 $extra = '' unless defined($extra);
1053
1054 if (length($short) < length($long)) {
1055 return $cgi->a({-href => $href, -class => "list subject",
1056 -title => to_utf8($long)},
1057 esc_html($short) . $extra);
1058 } else {
1059 return $cgi->a({-href => $href, -class => "list subject"},
1060 esc_html($long) . $extra);
1061 }
1062 }
1063
1064 # format git diff header line, i.e. "diff --(git|combined|cc) ..."
1065 sub format_git_diff_header_line {
1066 my $line = shift;
1067 my $diffinfo = shift;
1068 my ($from, $to) = @_;
1069
1070 if ($diffinfo->{'nparents'}) {
1071 # combined diff
1072 $line =~ s!^(diff (.*?) )"?.*$!$1!;
1073 if ($to->{'href'}) {
1074 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1075 esc_path($to->{'file'}));
1076 } else { # file was deleted (no href)
1077 $line .= esc_path($to->{'file'});
1078 }
1079 } else {
1080 # "ordinary" diff
1081 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
1082 if ($from->{'href'}) {
1083 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
1084 'a/' . esc_path($from->{'file'}));
1085 } else { # file was added (no href)
1086 $line .= 'a/' . esc_path($from->{'file'});
1087 }
1088 $line .= ' ';
1089 if ($to->{'href'}) {
1090 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1091 'b/' . esc_path($to->{'file'}));
1092 } else { # file was deleted
1093 $line .= 'b/' . esc_path($to->{'file'});
1094 }
1095 }
1096
1097 return "<div class=\"diff header\">$line</div>\n";
1098 }
1099
1100 # format extended diff header line, before patch itself
1101 sub format_extended_diff_header_line {
1102 my $line = shift;
1103 my $diffinfo = shift;
1104 my ($from, $to) = @_;
1105
1106 # match <path>
1107 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
1108 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1109 esc_path($from->{'file'}));
1110 }
1111 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
1112 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1113 esc_path($to->{'file'}));
1114 }
1115 # match single <mode>
1116 if ($line =~ m/\s(\d{6})$/) {
1117 $line .= '<span class="info"> (' .
1118 file_type_long($1) .
1119 ')</span>';
1120 }
1121 # match <hash>
1122 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
1123 # can match only for combined diff
1124 $line = 'index ';
1125 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1126 if ($from->{'href'}[$i]) {
1127 $line .= $cgi->a({-href=>$from->{'href'}[$i],
1128 -class=>"hash"},
1129 substr($diffinfo->{'from_id'}[$i],0,7));
1130 } else {
1131 $line .= '0' x 7;
1132 }
1133 # separator
1134 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
1135 }
1136 $line .= '..';
1137 if ($to->{'href'}) {
1138 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1139 substr($diffinfo->{'to_id'},0,7));
1140 } else {
1141 $line .= '0' x 7;
1142 }
1143
1144 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
1145 # can match only for ordinary diff
1146 my ($from_link, $to_link);
1147 if ($from->{'href'}) {
1148 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
1149 substr($diffinfo->{'from_id'},0,7));
1150 } else {
1151 $from_link = '0' x 7;
1152 }
1153 if ($to->{'href'}) {
1154 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1155 substr($diffinfo->{'to_id'},0,7));
1156 } else {
1157 $to_link = '0' x 7;
1158 }
1159 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
1160 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
1161 }
1162
1163 return $line . "<br/>\n";
1164 }
1165
1166 # format from-file/to-file diff header
1167 sub format_diff_from_to_header {
1168 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
1169 my $line;
1170 my $result = '';
1171
1172 $line = $from_line;
1173 #assert($line =~ m/^---/) if DEBUG;
1174 # no extra formatting for "^--- /dev/null"
1175 if (! $diffinfo->{'nparents'}) {
1176 # ordinary (single parent) diff
1177 if ($line =~ m!^--- "?a/!) {
1178 if ($from->{'href'}) {
1179 $line = '--- a/' .
1180 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1181 esc_path($from->{'file'}));
1182 } else {
1183 $line = '--- a/' .
1184 esc_path($from->{'file'});
1185 }
1186 }
1187 $result .= qq!<div class="diff from_file">$line</div>\n!;
1188
1189 } else {
1190 # combined diff (merge commit)
1191 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1192 if ($from->{'href'}[$i]) {
1193 $line = '--- ' .
1194 $cgi->a({-href=>href(action=>"blobdiff",
1195 hash_parent=>$diffinfo->{'from_id'}[$i],
1196 hash_parent_base=>$parents[$i],
1197 file_parent=>$from->{'file'}[$i],
1198 hash=>$diffinfo->{'to_id'},
1199 hash_base=>$hash,
1200 file_name=>$to->{'file'}),
1201 -class=>"path",
1202 -title=>"diff" . ($i+1)},
1203 $i+1) .
1204 '/' .
1205 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
1206 esc_path($from->{'file'}[$i]));
1207 } else {
1208 $line = '--- /dev/null';
1209 }
1210 $result .= qq!<div class="diff from_file">$line</div>\n!;
1211 }
1212 }
1213
1214 $line = $to_line;
1215 #assert($line =~ m/^\+\+\+/) if DEBUG;
1216 # no extra formatting for "^+++ /dev/null"
1217 if ($line =~ m!^\+\+\+ "?b/!) {
1218 if ($to->{'href'}) {
1219 $line = '+++ b/' .
1220 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1221 esc_path($to->{'file'}));
1222 } else {
1223 $line = '+++ b/' .
1224 esc_path($to->{'file'});
1225 }
1226 }
1227 $result .= qq!<div class="diff to_file">$line</div>\n!;
1228
1229 return $result;
1230 }
1231
1232 # create note for patch simplified by combined diff
1233 sub format_diff_cc_simplified {
1234 my ($diffinfo, @parents) = @_;
1235 my $result = '';
1236
1237 $result .= "<div class=\"diff header\">" .
1238 "diff --cc ";
1239 if (!is_deleted($diffinfo)) {
1240 $result .= $cgi->a({-href => href(action=>"blob",
1241 hash_base=>$hash,
1242 hash=>$diffinfo->{'to_id'},
1243 file_name=>$diffinfo->{'to_file'}),
1244 -class => "path"},
1245 esc_path($diffinfo->{'to_file'}));
1246 } else {
1247 $result .= esc_path($diffinfo->{'to_file'});
1248 }
1249 $result .= "</div>\n" . # class="diff header"
1250 "<div class=\"diff nodifferences\">" .
1251 "Simple merge" .
1252 "</div>\n"; # class="diff nodifferences"
1253
1254 return $result;
1255 }
1256
1257 # format patch (diff) line (not to be used for diff headers)
1258 sub format_diff_line {
1259 my $line = shift;
1260 my ($from, $to) = @_;
1261 my $diff_class = "";
1262
1263 chomp $line;
1264
1265 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
1266 # combined diff
1267 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
1268 if ($line =~ m/^\@{3}/) {
1269 $diff_class = " chunk_header";
1270 } elsif ($line =~ m/^\\/) {
1271 $diff_class = " incomplete";
1272 } elsif ($prefix =~ tr/+/+/) {
1273 $diff_class = " add";
1274 } elsif ($prefix =~ tr/-/-/) {
1275 $diff_class = " rem";
1276 }
1277 } else {
1278 # assume ordinary diff
1279 my $char = substr($line, 0, 1);
1280 if ($char eq '+') {
1281 $diff_class = " add";
1282 } elsif ($char eq '-') {
1283 $diff_class = " rem";
1284 } elsif ($char eq '@') {
1285 $diff_class = " chunk_header";
1286 } elsif ($char eq "\\") {
1287 $diff_class = " incomplete";
1288 }
1289 }
1290 $line = untabify($line);
1291 if ($from && $to && $line =~ m/^\@{2} /) {
1292 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
1293 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
1294
1295 $from_lines = 0 unless defined $from_lines;
1296 $to_lines = 0 unless defined $to_lines;
1297
1298 if ($from->{'href'}) {
1299 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
1300 -class=>"list"}, $from_text);
1301 }
1302 if ($to->{'href'}) {
1303 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
1304 -class=>"list"}, $to_text);
1305 }
1306 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
1307 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1308 return "<div class=\"diff$diff_class\">$line</div>\n";
1309 } elsif ($from && $to && $line =~ m/^\@{3}/) {
1310 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
1311 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
1312
1313 @from_text = split(' ', $ranges);
1314 for (my $i = 0; $i < @from_text; ++$i) {
1315 ($from_start[$i], $from_nlines[$i]) =
1316 (split(',', substr($from_text[$i], 1)), 0);
1317 }
1318
1319 $to_text = pop @from_text;
1320 $to_start = pop @from_start;
1321 $to_nlines = pop @from_nlines;
1322
1323 $line = "<span class=\"chunk_info\">$prefix ";
1324 for (my $i = 0; $i < @from_text; ++$i) {
1325 if ($from->{'href'}[$i]) {
1326 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
1327 -class=>"list"}, $from_text[$i]);
1328 } else {
1329 $line .= $from_text[$i];
1330 }
1331 $line .= " ";
1332 }
1333 if ($to->{'href'}) {
1334 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
1335 -class=>"list"}, $to_text);
1336 } else {
1337 $line .= $to_text;
1338 }
1339 $line .= " $prefix</span>" .
1340 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1341 return "<div class=\"diff$diff_class\">$line</div>\n";
1342 }
1343 return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
1344 }
1345
1346 # Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
1347 # linked. Pass the hash of the tree/commit to snapshot.
1348 sub format_snapshot_links {
1349 my ($hash) = @_;
1350 my @snapshot_fmts = gitweb_check_feature('snapshot');
1351 @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
1352 my $num_fmts = @snapshot_fmts;
1353 if ($num_fmts > 1) {
1354 # A parenthesized list of links bearing format names.
1355 # e.g. "snapshot (_tar.gz_ _zip_)"
1356 return "snapshot (" . join(' ', map
1357 $cgi->a({
1358 -href => href(
1359 action=>"snapshot",
1360 hash=>$hash,
1361 snapshot_format=>$_
1362 )
1363 }, $known_snapshot_formats{$_}{'display'})
1364 , @snapshot_fmts) . ")";
1365 } elsif ($num_fmts == 1) {
1366 # A single "snapshot" link whose tooltip bears the format name.
1367 # i.e. "_snapshot_"
1368 my ($fmt) = @snapshot_fmts;
1369 return
1370 $cgi->a({
1371 -href => href(
1372 action=>"snapshot",
1373 hash=>$hash,
1374 snapshot_format=>$fmt
1375 ),
1376 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
1377 }, "snapshot");
1378 } else { # $num_fmts == 0
1379 return undef;
1380 }
1381 }
1382
1383 ## ----------------------------------------------------------------------
1384 ## git utility subroutines, invoking git commands
1385
1386 # returns path to the core git executable and the --git-dir parameter as list
1387 sub git_cmd {
1388 return $GIT, '--git-dir='.$git_dir;
1389 }
1390
1391 # returns path to the core git executable and the --git-dir parameter as string
1392 sub git_cmd_str {
1393 return join(' ', git_cmd());
1394 }
1395
1396 # get HEAD ref of given project as hash
1397 sub git_get_head_hash {
1398 my $project = shift;
1399 my $o_git_dir = $git_dir;
1400 my $retval = undef;
1401 $git_dir = "$projectroot/$project";
1402 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
1403 my $head = <$fd>;
1404 close $fd;
1405 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
1406 $retval = $1;
1407 }
1408 }
1409 if (defined $o_git_dir) {
1410 $git_dir = $o_git_dir;
1411 }
1412 return $retval;
1413 }
1414
1415 # get type of given object
1416 sub git_get_type {
1417 my $hash = shift;
1418
1419 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
1420 my $type = <$fd>;
1421 close $fd or return;
1422 chomp $type;
1423 return $type;
1424 }
1425
1426 sub git_get_project_config {
1427 my ($key, $type) = @_;
1428
1429 return unless ($key);
1430 $key =~ s/^gitweb\.//;
1431 return if ($key =~ m/\W/);
1432
1433 my @x = (git_cmd(), 'config');
1434 if (defined $type) { push @x, $type; }
1435 push @x, "--get";
1436 push @x, "gitweb.$key";
1437 my $val = qx(@x);
1438 chomp $val;
1439 return ($val);
1440 }
1441
1442 # get hash of given path at given ref
1443 sub git_get_hash_by_path {
1444 my $base = shift;
1445 my $path = shift || return undef;
1446 my $type = shift;
1447
1448 $path =~ s,/+$,,;
1449
1450 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
1451 or die_error(undef, "Open git-ls-tree failed");
1452 my $line = <$fd>;
1453 close $fd or return undef;
1454
1455 if (!defined $line) {
1456 # there is no tree or hash given by $path at $base
1457 return undef;
1458 }
1459
1460 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1461 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
1462 if (defined $type && $type ne $2) {
1463 # type doesn't match
1464 return undef;
1465 }
1466 return $3;
1467 }
1468
1469 # get path of entry with given hash at given tree-ish (ref)
1470 # used to get 'from' filename for combined diff (merge commit) for renames
1471 sub git_get_path_by_hash {
1472 my $base = shift || return;
1473 my $hash = shift || return;
1474
1475 local $/ = "\0";
1476
1477 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
1478 or return undef;
1479 while (my $line = <$fd>) {
1480 chomp $line;
1481
1482 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
1483 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
1484 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
1485 close $fd;
1486 return $1;
1487 }
1488 }
1489 close $fd;
1490 return undef;
1491 }
1492
1493 ## ......................................................................
1494 ## git utility functions, directly accessing git repository
1495
1496 sub git_get_project_description {
1497 my $path = shift;
1498
1499 open my $fd, "$projectroot/$path/description" or return undef;
1500 my $descr = <$fd>;
1501 close $fd;
1502 if (defined $descr) {
1503 chomp $descr;
1504 }
1505 return $descr;
1506 }
1507
1508 sub git_get_project_url_list {
1509 my $path = shift;
1510
1511 open my $fd, "$projectroot/$path/cloneurl" or return;
1512 my @git_project_url_list = map { chomp; $_ } <$fd>;
1513 close $fd;
1514
1515 return wantarray ? @git_project_url_list : \@git_project_url_list;
1516 }
1517
1518 sub git_get_projects_list {
1519 my ($filter) = @_;
1520 my @list;
1521
1522 $filter ||= '';
1523 $filter =~ s/\.git$//;
1524
1525 my ($check_forks) = gitweb_check_feature('forks');
1526
1527 if (-d $projects_list) {
1528 # search in directory
1529 my $dir = $projects_list . ($filter ? "/$filter" : '');
1530 # remove the trailing "/"
1531 $dir =~ s!/+$!!;
1532 my $pfxlen = length("$dir");
1533 my $pfxdepth = ($dir =~ tr!/!!);
1534
1535 File::Find::find({
1536 follow_fast => 1, # follow symbolic links
1537 follow_skip => 2, # ignore duplicates
1538 dangling_symlinks => 0, # ignore dangling symlinks, silently
1539 wanted => sub {
1540 # skip project-list toplevel, if we get it.
1541 return if (m!^[/.]$!);
1542 # only directories can be git repositories
1543 return unless (-d $_);
1544 # don't traverse too deep (Find is super slow on os x)
1545 if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
1546 $File::Find::prune = 1;
1547 return;
1548 }
1549
1550 my $subdir = substr($File::Find::name, $pfxlen + 1);
1551 # we check related file in $projectroot
1552 if ($check_forks and $subdir =~ m#/.#) {
1553 $File::Find::prune = 1;
1554 } elsif (check_export_ok("$projectroot/$filter/$subdir")) {
1555 push @list, { path => ($filter ? "$filter/" : '') . $subdir };
1556 $File::Find::prune = 1;
1557 }
1558 },
1559 }, "$dir");
1560
1561 } elsif (-f $projects_list) {
1562 # read from file(url-encoded):
1563 # 'git%2Fgit.git Linus+Torvalds'
1564 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1565 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1566 my %paths;
1567 open my ($fd), $projects_list or return;
1568 PROJECT:
1569 while (my $line = <$fd>) {
1570 chomp $line;
1571 my ($path, $owner) = split ' ', $line;
1572 $path = unescape($path);
1573 $owner = unescape($owner);
1574 if (!defined $path) {
1575 next;
1576 }
1577 if ($filter ne '') {
1578 # looking for forks;
1579 my $pfx = substr($path, 0, length($filter));
1580 if ($pfx ne $filter) {
1581 next PROJECT;
1582 }
1583 my $sfx = substr($path, length($filter));
1584 if ($sfx !~ /^\/.*\.git$/) {
1585 next PROJECT;
1586 }
1587 } elsif ($check_forks) {
1588 PATH:
1589 foreach my $filter (keys %paths) {
1590 # looking for forks;
1591 my $pfx = substr($path, 0, length($filter));
1592 if ($pfx ne $filter) {
1593 next PATH;
1594 }
1595 my $sfx = substr($path, length($filter));
1596 if ($sfx !~ /^\/.*\.git$/) {
1597 next PATH;
1598 }
1599 # is a fork, don't include it in
1600 # the list
1601 next PROJECT;
1602 }
1603 }
1604 if (check_export_ok("$projectroot/$path")) {
1605 my $pr = {
1606 path => $path,
1607 owner => to_utf8($owner),
1608 };
1609 push @list, $pr;
1610 (my $forks_path = $path) =~ s/\.git$//;
1611 $paths{$forks_path}++;
1612 }
1613 }
1614 close $fd;
1615 }
1616 return @list;
1617 }
1618
1619 our $gitweb_project_owner = undef;
1620 sub git_get_project_list_from_file {
1621
1622 return if (defined $gitweb_project_owner);
1623
1624 $gitweb_project_owner = {};
1625 # read from file (url-encoded):
1626 # 'git%2Fgit.git Linus+Torvalds'
1627 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1628 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1629 if (-f $projects_list) {
1630 open (my $fd , $projects_list);
1631 while (my $line = <$fd>) {
1632 chomp $line;
1633 my ($pr, $ow) = split ' ', $line;
1634 $pr = unescape($pr);
1635 $ow = unescape($ow);
1636 $gitweb_project_owner->{$pr} = to_utf8($ow);
1637 }
1638 close $fd;
1639 }
1640 }
1641
1642 sub git_get_project_owner {
1643 my $project = shift;
1644 my $owner;
1645
1646 return undef unless $project;
1647
1648 if (!defined $gitweb_project_owner) {
1649 git_get_project_list_from_file();
1650 }
1651
1652 if (exists $gitweb_project_owner->{$project}) {
1653 $owner = $gitweb_project_owner->{$project};
1654 }
1655 if (!defined $owner) {
1656 $owner = get_file_owner("$projectroot/$project");
1657 }
1658
1659 return $owner;
1660 }
1661
1662 sub git_get_last_activity {
1663 my ($path) = @_;
1664 my $fd;
1665
1666 $git_dir = "$projectroot/$path";
1667 open($fd, "-|", git_cmd(), 'for-each-ref',
1668 '--format=%(committer)',
1669 '--sort=-committerdate',
1670 '--count=1',
1671 'refs/heads') or return;
1672 my $most_recent = <$fd>;
1673 close $fd or return;
1674 if (defined $most_recent &&
1675 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
1676 my $timestamp = $1;
1677 my $age = time - $timestamp;
1678 return ($age, age_string($age));
1679 }
1680 return (undef, undef);
1681 }
1682
1683 sub git_get_references {
1684 my $type = shift || "";
1685 my %refs;
1686 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
1687 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
1688 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
1689 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
1690 or return;
1691
1692 while (my $line = <$fd>) {
1693 chomp $line;
1694 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type/?[^^]+)!) {
1695 if (defined $refs{$1}) {
1696 push @{$refs{$1}}, $2;
1697 } else {
1698 $refs{$1} = [ $2 ];
1699 }
1700 }
1701 }
1702 close $fd or return;
1703 return \%refs;
1704 }
1705
1706 sub git_get_rev_name_tags {
1707 my $hash = shift || return undef;
1708
1709 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
1710 or return;
1711 my $name_rev = <$fd>;
1712 close $fd;
1713
1714 if ($name_rev =~ m|^$hash tags/(.*)$|) {
1715 return $1;
1716 } else {
1717 # catches also '$hash undefined' output
1718 return undef;
1719 }
1720 }
1721
1722 ## ----------------------------------------------------------------------
1723 ## parse to hash functions
1724
1725 sub parse_date {
1726 my $epoch = shift;
1727 my $tz = shift || "-0000";
1728
1729 my %date;
1730 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
1731 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
1732 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
1733 $date{'hour'} = $hour;
1734 $date{'minute'} = $min;
1735 $date{'mday'} = $mday;
1736 $date{'day'} = $days[$wday];
1737 $date{'month'} = $months[$mon];
1738 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
1739 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
1740 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
1741 $mday, $months[$mon], $hour ,$min;
1742 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
1743 1900+$year, $mon, $mday, $hour ,$min, $sec;
1744
1745 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
1746 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
1747 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
1748 $date{'hour_local'} = $hour;
1749 $date{'minute_local'} = $min;
1750 $date{'tz_local'} = $tz;
1751 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
1752 1900+$year, $mon+1, $mday,
1753 $hour, $min, $sec, $tz);
1754 return %date;
1755 }
1756
1757 sub parse_tag {
1758 my $tag_id = shift;
1759 my %tag;
1760 my @comment;
1761
1762 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
1763 $tag{'id'} = $tag_id;
1764 while (my $line = <$fd>) {
1765 chomp $line;
1766 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
1767 $tag{'object'} = $1;
1768 } elsif ($line =~ m/^type (.+)$/) {
1769 $tag{'type'} = $1;
1770 } elsif ($line =~ m/^tag (.+)$/) {
1771 $tag{'name'} = $1;
1772 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
1773 $tag{'author'} = $1;
1774 $tag{'epoch'} = $2;
1775 $tag{'tz'} = $3;
1776 } elsif ($line =~ m/--BEGIN/) {
1777 push @comment, $line;
1778 last;
1779 } elsif ($line eq "") {
1780 last;
1781 }
1782 }
1783 push @comment, <$fd>;
1784 $tag{'comment'} = \@comment;
1785 close $fd or return;
1786 if (!defined $tag{'name'}) {
1787 return
1788 };
1789 return %tag
1790 }
1791
1792 sub parse_commit_text {
1793 my ($commit_text, $withparents) = @_;
1794 my @commit_lines = split '\n', $commit_text;
1795 my %co;
1796
1797 pop @commit_lines; # Remove '\0'
1798
1799 if (! @commit_lines) {
1800 return;
1801 }
1802
1803 my $header = shift @commit_lines;
1804 if ($header !~ m/^[0-9a-fA-F]{40}/) {
1805 return;
1806 }
1807 ($co{'id'}, my @parents) = split ' ', $header;
1808 while (my $line = shift @commit_lines) {
1809 last if $line eq "\n";
1810 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
1811 $co{'tree'} = $1;
1812 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
1813 push @parents, $1;
1814 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
1815 $co{'author'} = $1;
1816 $co{'author_epoch'} = $2;
1817 $co{'author_tz'} = $3;
1818 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
1819 $co{'author_name'} = $1;
1820 $co{'author_email'} = $2;
1821 } else {
1822 $co{'author_name'} = $co{'author'};
1823 }
1824 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
1825 $co{'committer'} = $1;
1826 $co{'committer_epoch'} = $2;
1827 $co{'committer_tz'} = $3;
1828 $co{'committer_name'} = $co{'committer'};
1829 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
1830 $co{'committer_name'} = $1;
1831 $co{'committer_email'} = $2;
1832 } else {
1833 $co{'committer_name'} = $co{'committer'};
1834 }
1835 }
1836 }
1837 if (!defined $co{'tree'}) {
1838 return;
1839 };
1840 $co{'parents'} = \@parents;
1841 $co{'parent'} = $parents[0];
1842
1843 foreach my $title (@commit_lines) {
1844 $title =~ s/^ //;
1845 if ($title ne "") {
1846 $co{'title'} = chop_str($title, 80, 5);
1847 # remove leading stuff of merges to make the interesting part visible
1848 if (length($title) > 50) {
1849 $title =~ s/^Automatic //;
1850 $title =~ s/^merge (of|with) /Merge ... /i;
1851 if (length($title) > 50) {
1852 $title =~ s/(http|rsync):\/\///;
1853 }
1854 if (length($title) > 50) {
1855 $title =~ s/(master|www|rsync)\.//;
1856 }
1857 if (length($title) > 50) {
1858 $title =~ s/kernel.org:?//;
1859 }
1860 if (length($title) > 50) {
1861 $title =~ s/\/pub\/scm//;
1862 }
1863 }
1864 $co{'title_short'} = chop_str($title, 50, 5);
1865 last;
1866 }
1867 }
1868 if ($co{'title'} eq "") {
1869 $co{'title'} = $co{'title_short'} = '(no commit message)';
1870 }
1871 # remove added spaces
1872 foreach my $line (@commit_lines) {
1873 $line =~ s/^ //;
1874 }
1875 $co{'comment'} = \@commit_lines;
1876
1877 my $age = time - $co{'committer_epoch'};
1878 $co{'age'} = $age;
1879 $co{'age_string'} = age_string($age);
1880 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
1881 if ($age > 60*60*24*7*2) {
1882 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1883 $co{'age_string_age'} = $co{'age_string'};
1884 } else {
1885 $co{'age_string_date'} = $co{'age_string'};
1886 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1887 }
1888 return %co;
1889 }
1890
1891 sub parse_commit {
1892 my ($commit_id) = @_;
1893 my %co;
1894
1895 local $/ = "\0";
1896
1897 open my $fd, "-|", git_cmd(), "rev-list",
1898 "--parents",
1899 "--header",
1900 "--max-count=1",
1901 $commit_id,
1902 "--",
1903 or die_error(undef, "Open git-rev-list failed");
1904 %co = parse_commit_text(<$fd>, 1);
1905 close $fd;
1906
1907 return %co;
1908 }
1909
1910 sub parse_commits {
1911 my ($commit_id, $maxcount, $skip, $arg, $filename) = @_;
1912 my @cos;
1913
1914 $maxcount ||= 1;
1915 $skip ||= 0;
1916
1917 local $/ = "\0";
1918
1919 open my $fd, "-|", git_cmd(), "rev-list",
1920 "--header",
1921 ($arg ? ($arg) : ()),
1922 ("--max-count=" . $maxcount),
1923 ("--skip=" . $skip),
1924 @extra_options,
1925 $commit_id,
1926 "--",
1927 ($filename ? ($filename) : ())
1928 or die_error(undef, "Open git-rev-list failed");
1929 while (my $line = <$fd>) {
1930 my %co = parse_commit_text($line);
1931 push @cos, \%co;
1932 }
1933 close $fd;
1934
1935 return wantarray ? @cos : \@cos;
1936 }
1937
1938 # parse ref from ref_file, given by ref_id, with given type
1939 sub parse_ref {
1940 my $ref_file = shift;
1941 my $ref_id = shift;
1942 my $type = shift || git_get_type($ref_id);
1943 my %ref_item;
1944
1945 $ref_item{'type'} = $type;
1946 $ref_item{'id'} = $ref_id;
1947 $ref_item{'epoch'} = 0;
1948 $ref_item{'age'} = "unknown";
1949 if ($type eq "tag") {
1950 my %tag = parse_tag($ref_id);
1951 $ref_item{'comment'} = $tag{'comment'};
1952 if ($tag{'type'} eq "commit") {
1953 my %co = parse_commit($tag{'object'});
1954 $ref_item{'epoch'} = $co{'committer_epoch'};
1955 $ref_item{'age'} = $co{'age_string'};
1956 } elsif (defined($tag{'epoch'})) {
1957 my $age = time - $tag{'epoch'};
1958 $ref_item{'epoch'} = $tag{'epoch'};
1959 $ref_item{'age'} = age_string($age);
1960 }
1961 $ref_item{'reftype'} = $tag{'type'};
1962 $ref_item{'name'} = $tag{'name'};
1963 $ref_item{'refid'} = $tag{'object'};
1964 } elsif ($type eq "commit"){
1965 my %co = parse_commit($ref_id);
1966 $ref_item{'reftype'} = "commit";
1967 $ref_item{'name'} = $ref_file;
1968 $ref_item{'title'} = $co{'title'};
1969 $ref_item{'refid'} = $ref_id;
1970 $ref_item{'epoch'} = $co{'committer_epoch'};
1971 $ref_item{'age'} = $co{'age_string'};
1972 } else {
1973 $ref_item{'reftype'} = $type;
1974 $ref_item{'name'} = $ref_file;
1975 $ref_item{'refid'} = $ref_id;
1976 }
1977
1978 return %ref_item;
1979 }
1980
1981 # parse line of git-diff-tree "raw" output
1982 sub parse_difftree_raw_line {
1983 my $line = shift;
1984 my %res;
1985
1986 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1987 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
1988 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1989 $res{'from_mode'} = $1;
1990 $res{'to_mode'} = $2;
1991 $res{'from_id'} = $3;
1992 $res{'to_id'} = $4;
1993 $res{'status'} = $5;
1994 $res{'similarity'} = $6;
1995 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
1996 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
1997 } else {
1998 $res{'file'} = unquote($7);
1999 }
2000 }
2001 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
2002 # combined diff (for merge commit)
2003 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
2004 $res{'nparents'} = length($1);
2005 $res{'from_mode'} = [ split(' ', $2) ];
2006 $res{'to_mode'} = pop @{$res{'from_mode'}};
2007 $res{'from_id'} = [ split(' ', $3) ];
2008 $res{'to_id'} = pop @{$res{'from_id'}};
2009 $res{'status'} = [ split('', $4) ];
2010 $res{'to_file'} = unquote($5);
2011 }
2012 # 'c512b523472485aef4fff9e57b229d9d243c967f'
2013 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
2014 $res{'commit'} = $1;
2015 }
2016
2017 return wantarray ? %res : \%res;
2018 }
2019
2020 # wrapper: return parsed line of git-diff-tree "raw" output
2021 # (the argument might be raw line, or parsed info)
2022 sub parsed_difftree_line {
2023 my $line_or_ref = shift;
2024
2025 if (ref($line_or_ref) eq "HASH") {
2026 # pre-parsed (or generated by hand)
2027 return $line_or_ref;
2028 } else {
2029 return parse_difftree_raw_line($line_or_ref);
2030 }
2031 }
2032
2033 # parse line of git-ls-tree output
2034 sub parse_ls_tree_line ($;%) {
2035 my $line = shift;
2036 my %opts = @_;
2037 my %res;
2038
2039 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2040 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
2041
2042 $res{'mode'} = $1;
2043 $res{'type'} = $2;
2044 $res{'hash'} = $3;
2045 if ($opts{'-z'}) {
2046 $res{'name'} = $4;
2047 } else {
2048 $res{'name'} = unquote($4);
2049 }
2050
2051 return wantarray ? %res : \%res;
2052 }
2053
2054 # generates _two_ hashes, references to which are passed as 2 and 3 argument
2055 sub parse_from_to_diffinfo {
2056 my ($diffinfo, $from, $to, @parents) = @_;
2057
2058 if ($diffinfo->{'nparents'}) {
2059 # combined diff
2060 $from->{'file'} = [];
2061 $from->{'href'} = [];
2062 fill_from_file_info($diffinfo, @parents)
2063 unless exists $diffinfo->{'from_file'};
2064 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2065 $from->{'file'}[$i] = $diffinfo->{'from_file'}[$i] || $diffinfo->{'to_file'};
2066 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
2067 $from->{'href'}[$i] = href(action=>"blob",
2068 hash_base=>$parents[$i],
2069 hash=>$diffinfo->{'from_id'}[$i],
2070 file_name=>$from->{'file'}[$i]);
2071 } else {
2072 $from->{'href'}[$i] = undef;
2073 }
2074 }
2075 } else {
2076 # ordinary (not combined) diff
2077 $from->{'file'} = $diffinfo->{'from_file'} || $diffinfo->{'file'};
2078 if ($diffinfo->{'status'} ne "A") { # not new (added) file
2079 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2080 hash=>$diffinfo->{'from_id'},
2081 file_name=>$from->{'file'});
2082 } else {
2083 delete $from->{'href'};
2084 }
2085 }
2086
2087 $to->{'file'} = $diffinfo->{'to_file'} || $diffinfo->{'file'};
2088 if (!is_deleted($diffinfo)) { # file exists in result
2089 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
2090 hash=>$diffinfo->{'to_id'},
2091 file_name=>$to->{'file'});
2092 } else {
2093 delete $to->{'href'};
2094 }
2095 }
2096
2097 ## ......................................................................
2098 ## parse to array of hashes functions
2099
2100 sub git_get_heads_list {
2101 my $limit = shift;
2102 my @headslist;
2103
2104 open my $fd, '-|', git_cmd(), 'for-each-ref',
2105 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
2106 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
2107 'refs/heads'
2108 or return;
2109 while (my $line = <$fd>) {
2110 my %ref_item;
2111
2112 chomp $line;
2113 my ($refinfo, $committerinfo) = split(/\0/, $line);
2114 my ($hash, $name, $title) = split(' ', $refinfo, 3);
2115 my ($committer, $epoch, $tz) =
2116 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
2117 $name =~ s!^refs/heads/!!;
2118
2119 $ref_item{'name'} = $name;
2120 $ref_item{'id'} = $hash;
2121 $ref_item{'title'} = $title || '(no commit message)';
2122 $ref_item{'epoch'} = $epoch;
2123 if ($epoch) {
2124 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2125 } else {
2126 $ref_item{'age'} = "unknown";
2127 }
2128
2129 push @headslist, \%ref_item;
2130 }
2131 close $fd;
2132
2133 return wantarray ? @headslist : \@headslist;
2134 }
2135
2136 sub git_get_tags_list {
2137 my $limit = shift;
2138 my @tagslist;
2139
2140 open my $fd, '-|', git_cmd(), 'for-each-ref',
2141 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
2142 '--format=%(objectname) %(objecttype) %(refname) '.
2143 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
2144 'refs/tags'
2145 or return;
2146 while (my $line = <$fd>) {
2147 my %ref_item;
2148
2149 chomp $line;
2150 my ($refinfo, $creatorinfo) = split(/\0/, $line);
2151 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
2152 my ($creator, $epoch, $tz) =
2153 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
2154 $name =~ s!^refs/tags/!!;
2155
2156 $ref_item{'type'} = $type;
2157 $ref_item{'id'} = $id;
2158 $ref_item{'name'} = $name;
2159 if ($type eq "tag") {
2160 $ref_item{'subject'} = $title;
2161 $ref_item{'reftype'} = $reftype;
2162 $ref_item{'refid'} = $refid;
2163 } else {
2164 $ref_item{'reftype'} = $type;
2165 $ref_item{'refid'} = $id;
2166 }
2167
2168 if ($type eq "tag" || $type eq "commit") {
2169 $ref_item{'epoch'} = $epoch;
2170 if ($epoch) {
2171 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2172 } else {
2173 $ref_item{'age'} = "unknown";
2174 }
2175 }
2176
2177 push @tagslist, \%ref_item;
2178 }
2179 close $fd;
2180
2181 return wantarray ? @tagslist : \@tagslist;
2182 }
2183
2184 ## ----------------------------------------------------------------------
2185 ## filesystem-related functions
2186
2187 sub get_file_owner {
2188 my $path = shift;
2189
2190 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
2191 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
2192 if (!defined $gcos) {
2193 return undef;
2194 }
2195 my $owner = $gcos;
2196 $owner =~ s/[,;].*$//;
2197 return to_utf8($owner);
2198 }
2199
2200 ## ......................................................................
2201 ## mimetype related functions
2202
2203 sub mimetype_guess_file {
2204 my $filename = shift;
2205 my $mimemap = shift;
2206 -r $mimemap or return undef;
2207
2208 my %mimemap;
2209 open(MIME, $mimemap) or return undef;
2210 while (<MIME>) {
2211 next if m/^#/; # skip comments
2212 my ($mime, $exts) = split(/\t+/);
2213 if (defined $exts) {
2214 my @exts = split(/\s+/, $exts);
2215 foreach my $ext (@exts) {
2216 $mimemap{$ext} = $mime;
2217 }
2218 }
2219 }
2220 close(MIME);
2221
2222 $filename =~ /\.([^.]*)$/;
2223 return $mimemap{$1};
2224 }
2225
2226 sub mimetype_guess {
2227 my $filename = shift;
2228 my $mime;
2229 $filename =~ /\./ or return undef;
2230
2231 if ($mimetypes_file) {
2232 my $file = $mimetypes_file;
2233 if ($file !~ m!^/!) { # if it is relative path
2234 # it is relative to project
2235 $file = "$projectroot/$project/$file";
2236 }
2237 $mime = mimetype_guess_file($filename, $file);
2238 }
2239 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
2240 return $mime;
2241 }
2242
2243 sub blob_mimetype {
2244 my $fd = shift;
2245 my $filename = shift;
2246
2247 if ($filename) {
2248 my $mime = mimetype_guess($filename);
2249 $mime and return $mime;
2250 }
2251
2252 # just in case
2253 return $default_blob_plain_mimetype unless $fd;
2254
2255 if (-T $fd) {
2256 return 'text/plain' .
2257 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
2258 } elsif (! $filename) {
2259 return 'application/octet-stream';
2260 } elsif ($filename =~ m/\.png$/i) {
2261 return 'image/png';
2262 } elsif ($filename =~ m/\.gif$/i) {
2263 return 'image/gif';
2264 } elsif ($filename =~ m/\.jpe?g$/i) {
2265 return 'image/jpeg';
2266 } else {
2267 return 'application/octet-stream';
2268 }
2269 }
2270
2271 ## ======================================================================
2272 ## functions printing HTML: header, footer, error page
2273
2274 sub git_header_html {
2275 my $status = shift || "200 OK";
2276 my $expires = shift;
2277
2278 my $title = "$site_name";
2279 if (defined $project) {
2280 $title .= " - " . to_utf8($project);
2281 if (defined $action) {
2282 $title .= "/$action";
2283 if (defined $file_name) {
2284 $title .= " - " . esc_path($file_name);
2285 if ($action eq "tree" && $file_name !~ m|/$|) {
2286 $title .= "/";
2287 }
2288 }
2289 }
2290 }
2291 my $content_type;
2292 # require explicit support from the UA if we are to send the page as
2293 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
2294 # we have to do this because MSIE sometimes globs '*/*', pretending to
2295 # support xhtml+xml but choking when it gets what it asked for.
2296 if (defined $cgi->http('HTTP_ACCEPT') &&
2297 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
2298 $cgi->Accept('application/xhtml+xml') != 0) {
2299 $content_type = 'application/xhtml+xml';
2300 } else {
2301 $content_type = 'text/html';
2302 }
2303 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
2304 -status=> $status, -expires => $expires);
2305 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
2306 print <<EOF;
2307 <?xml version="1.0" encoding="utf-8"?>
2308 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2309 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
2310 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
2311 <!-- git core binaries version $git_version -->
2312 <head>
2313 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
2314 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
2315 <meta name="robots" content="index, nofollow"/>
2316 <title>$title</title>
2317 EOF
2318 # print out each stylesheet that exist
2319 if (defined $stylesheet) {
2320 #provides backwards capability for those people who define style sheet in a config file
2321 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2322 } else {
2323 foreach my $stylesheet (@stylesheets) {
2324 next unless $stylesheet;
2325 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2326 }
2327 }
2328 if (defined $project) {
2329 printf('<link rel="alternate" title="%s log RSS feed" '.
2330 'href="%s" type="application/rss+xml" />'."\n",
2331 esc_param($project), href(action=>"rss"));
2332 printf('<link rel="alternate" title="%s log RSS feed (no merges)" '.
2333 'href="%s" type="application/rss+xml" />'."\n",
2334 esc_param($project), href(action=>"rss",
2335 extra_options=>"--no-merges"));
2336 printf('<link rel="alternate" title="%s log Atom feed" '.
2337 'href="%s" type="application/atom+xml" />'."\n",
2338 esc_param($project), href(action=>"atom"));
2339 printf('<link rel="alternate" title="%s log Atom feed (no merges)" '.
2340 'href="%s" type="application/atom+xml" />'."\n",
2341 esc_param($project), href(action=>"atom",
2342 extra_options=>"--no-merges"));
2343 } else {
2344 printf('<link rel="alternate" title="%s projects list" '.
2345 'href="%s" type="text/plain; charset=utf-8"/>'."\n",
2346 $site_name, href(project=>undef, action=>"project_index"));
2347 printf('<link rel="alternate" title="%s projects feeds" '.
2348 'href="%s" type="text/x-opml"/>'."\n",
2349 $site_name, href(project=>undef, action=>"opml"));
2350 }
2351 if (defined $favicon) {
2352 print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n);
2353 }
2354
2355 print "</head>\n" .
2356 "<body>\n";
2357
2358 if (-f $site_header) {
2359 open (my $fd, $site_header);
2360 print <$fd>;
2361 close $fd;
2362 }
2363
2364 print "<div class=\"page_header\">\n" .
2365 $cgi->a({-href => esc_url($logo_url),
2366 -title => $logo_label},
2367 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
2368 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
2369 if (defined $project) {
2370 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
2371 if (defined $action) {
2372 print " / $action";
2373 }
2374 print "\n";
2375 }
2376 print "</div>\n";
2377
2378 my ($have_search) = gitweb_check_feature('search');
2379 if ((defined $project) && ($have_search)) {
2380 if (!defined $searchtext) {
2381 $searchtext = "";
2382 }
2383 my $search_hash;
2384 if (defined $hash_base) {
2385 $search_hash = $hash_base;
2386 } elsif (defined $hash) {
2387 $search_hash = $hash;
2388 } else {
2389 $search_hash = "HEAD";
2390 }
2391 my $action = $my_uri;
2392 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
2393 if ($use_pathinfo) {
2394 $action .= "/$project";
2395 } else {
2396 $cgi->param("p", $project);
2397 }
2398 $cgi->param("a", "search");
2399 $cgi->param("h", $search_hash);
2400 print $cgi->startform(-method => "get", -action => $action) .
2401 "<div class=\"search\">\n" .
2402 (!$use_pathinfo && $cgi->hidden(-name => "p") . "\n") .
2403 $cgi->hidden(-name => "a") . "\n" .
2404 $cgi->hidden(-name => "h") . "\n" .
2405 $cgi->popup_menu(-name => 'st', -default => 'commit',
2406 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
2407 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
2408 " search:\n",
2409 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
2410 "</div>" .
2411 $cgi->end_form() . "\n";
2412 }
2413 }
2414
2415 sub git_footer_html {
2416 print "<div class=\"page_footer\">\n";
2417 if (defined $project) {
2418 my $descr = git_get_project_description($project);
2419 if (defined $descr) {
2420 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
2421 }
2422 print $cgi->a({-href => href(action=>"rss"),
2423 -class => "rss_logo"}, "RSS") . " ";
2424 print $cgi->a({-href => href(action=>"atom"),
2425 -class => "rss_logo"}, "Atom") . "\n";
2426 } else {
2427 print $cgi->a({-href => href(project=>undef, action=>"opml"),
2428 -class => "rss_logo"}, "OPML") . " ";
2429 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
2430 -class => "rss_logo"}, "TXT") . "\n";
2431 }
2432 print "</div>\n" ;
2433
2434 if (-f $site_footer) {
2435 open (my $fd, $site_footer);
2436 print <$fd>;
2437 close $fd;
2438 }
2439
2440 print "</body>\n" .
2441 "</html>";
2442 }
2443
2444 sub die_error {
2445 my $status = shift || "403 Forbidden";
2446 my $error = shift || "Malformed query, file missing or permission denied";
2447
2448 git_header_html($status);
2449 print <<EOF;
2450 <div class="page_body">
2451 <br /><br />
2452 $status - $error
2453 <br />
2454 </div>
2455 EOF
2456 git_footer_html();
2457 exit;
2458 }
2459
2460 ## ----------------------------------------------------------------------
2461 ## functions printing or outputting HTML: navigation
2462
2463 sub git_print_page_nav {
2464 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
2465 $extra = '' if !defined $extra; # pager or formats
2466
2467 my @navs = qw(summary shortlog log commit commitdiff tree);
2468 if ($suppress) {
2469 @navs = grep { $_ ne $suppress } @navs;
2470 }
2471
2472 my %arg = map { $_ => {action=>$_} } @navs;
2473 if (defined $head) {
2474 for (qw(commit commitdiff)) {
2475 $arg{$_}{'hash'} = $head;
2476 }
2477 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
2478 for (qw(shortlog log)) {
2479 $arg{$_}{'hash'} = $head;
2480 }
2481 }
2482 }
2483 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
2484 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
2485
2486 print "<div class=\"page_nav\">\n" .
2487 (join " | ",
2488 map { $_ eq $current ?
2489 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
2490 } @navs);
2491 print "<br/>\n$extra<br/>\n" .
2492 "</div>\n";
2493 }
2494
2495 sub format_paging_nav {
2496 my ($action, $hash, $head, $page, $nrevs) = @_;
2497 my $paging_nav;
2498
2499
2500 if ($hash ne $head || $page) {
2501 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
2502 } else {
2503 $paging_nav .= "HEAD";
2504 }
2505
2506 if ($page > 0) {
2507 $paging_nav .= " &sdot; " .
2508 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
2509 -accesskey => "p", -title => "Alt-p"}, "prev");
2510 } else {
2511 $paging_nav .= " &sdot; prev";
2512 }
2513
2514 if ($nrevs >= (100 * ($page+1)-1)) {
2515 $paging_nav .= " &sdot; " .
2516 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
2517 -accesskey => "n", -title => "Alt-n"}, "next");
2518 } else {
2519 $paging_nav .= " &sdot; next";
2520 }
2521
2522 return $paging_nav;
2523 }
2524
2525 ## ......................................................................
2526 ## functions printing or outputting HTML: div
2527
2528 sub git_print_header_div {
2529 my ($action, $title, $hash, $hash_base) = @_;
2530 my %args = ();
2531
2532 $args{'action'} = $action;
2533 $args{'hash'} = $hash if $hash;
2534 $args{'hash_base'} = $hash_base if $hash_base;
2535
2536 print "<div class=\"header\">\n" .
2537 $cgi->a({-href => href(%args), -class => "title"},
2538 $title ? $title : $action) .
2539 "\n</div>\n";
2540 }
2541
2542 #sub git_print_authorship (\%) {
2543 sub git_print_authorship {
2544 my $co = shift;
2545
2546 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
2547 print "<div class=\"author_date\">" .
2548 esc_html($co->{'author_name'}) .
2549 " [$ad{'rfc2822'}";
2550 if ($ad{'hour_local'} < 6) {
2551 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2552 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2553 } else {
2554 printf(" (%02d:%02d %s)",
2555 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2556 }
2557 print "]</div>\n";
2558 }
2559
2560 sub git_print_page_path {
2561 my $name = shift;
2562 my $type = shift;
2563 my $hb = shift;
2564
2565
2566 print "<div class=\"page_path\">";
2567 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
2568 -title => 'tree root'}, to_utf8("[$project]"));
2569 print " / ";
2570 if (defined $name) {
2571 my @dirname = split '/', $name;
2572 my $basename = pop @dirname;
2573 my $fullname = '';
2574
2575 foreach my $dir (@dirname) {
2576 $fullname .= ($fullname ? '/' : '') . $dir;
2577 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
2578 hash_base=>$hb),
2579 -title => $fullname}, esc_path($dir));
2580 print " / ";
2581 }
2582 if (defined $type && $type eq 'blob') {
2583 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
2584 hash_base=>$hb),
2585 -title => $name}, esc_path($basename));
2586 } elsif (defined $type && $type eq 'tree') {
2587 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
2588 hash_base=>$hb),
2589 -title => $name}, esc_path($basename));
2590 print " / ";
2591 } else {
2592 print esc_path($basename);
2593 }
2594 }
2595 print "<br/></div>\n";
2596 }
2597
2598 # sub git_print_log (\@;%) {
2599 sub git_print_log ($;%) {
2600 my $log = shift;
2601 my %opts = @_;
2602
2603 if ($opts{'-remove_title'}) {
2604 # remove title, i.e. first line of log
2605 shift @$log;
2606 }
2607 # remove leading empty lines
2608 while (defined $log->[0] && $log->[0] eq "") {
2609 shift @$log;
2610 }
2611
2612 # print log
2613 my $signoff = 0;
2614 my $empty = 0;
2615 foreach my $line (@$log) {
2616 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
2617 $signoff = 1;
2618 $empty = 0;
2619 if (! $opts{'-remove_signoff'}) {
2620 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
2621 next;
2622 } else {
2623 # remove signoff lines
2624 next;
2625 }
2626 } else {
2627 $signoff = 0;
2628 }
2629
2630 # print only one empty line
2631 # do not print empty line after signoff
2632 if ($line eq "") {
2633 next if ($empty || $signoff);
2634 $empty = 1;
2635 } else {
2636 $empty = 0;
2637 }
2638
2639 print format_log_line_html($line) . "<br/>\n";
2640 }
2641
2642 if ($opts{'-final_empty_line'}) {
2643 # end with single empty line
2644 print "<br/>\n" unless $empty;
2645 }
2646 }
2647
2648 # return link target (what link points to)
2649 sub git_get_link_target {
2650 my $hash = shift;
2651 my $link_target;
2652
2653 # read link
2654 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2655 or return;
2656 {
2657 local $/;
2658 $link_target = <$fd>;
2659 }
2660 close $fd
2661 or return;
2662
2663 return $link_target;
2664 }
2665
2666 # given link target, and the directory (basedir) the link is in,
2667 # return target of link relative to top directory (top tree);
2668 # return undef if it is not possible (including absolute links).
2669 sub normalize_link_target {
2670 my ($link_target, $basedir, $hash_base) = @_;
2671
2672 # we can normalize symlink target only if $hash_base is provided
2673 return unless $hash_base;
2674
2675 # absolute symlinks (beginning with '/') cannot be normalized
2676 return if (substr($link_target, 0, 1) eq '/');
2677
2678 # normalize link target to path from top (root) tree (dir)
2679 my $path;
2680 if ($basedir) {
2681 $path = $basedir . '/' . $link_target;
2682 } else {
2683 # we are in top (root) tree (dir)
2684 $path = $link_target;
2685 }
2686
2687 # remove //, /./, and /../
2688 my @path_parts;
2689 foreach my $part (split('/', $path)) {
2690 # discard '.' and ''
2691 next if (!$part || $part eq '.');
2692 # handle '..'
2693 if ($part eq '..') {
2694 if (@path_parts) {
2695 pop @path_parts;
2696 } else {
2697 # link leads outside repository (outside top dir)
2698 return;
2699 }
2700 } else {
2701 push @path_parts, $part;
2702 }
2703 }
2704 $path = join('/', @path_parts);
2705
2706 return $path;
2707 }
2708
2709 # print tree entry (row of git_tree), but without encompassing <tr> element
2710 sub git_print_tree_entry {
2711 my ($t, $basedir, $hash_base, $have_blame) = @_;
2712
2713 my %base_key = ();
2714 $base_key{'hash_base'} = $hash_base if defined $hash_base;
2715
2716 # The format of a table row is: mode list link. Where mode is
2717 # the mode of the entry, list is the name of the entry, an href,
2718 # and link is the action links of the entry.
2719
2720 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
2721 if ($t->{'type'} eq "blob") {
2722 print "<td class=\"list\">" .
2723 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
2724 file_name=>"$basedir$t->{'name'}", %base_key),
2725 -class => "list"}, esc_path($t->{'name'}));
2726 if (S_ISLNK(oct $t->{'mode'})) {
2727 my $link_target = git_get_link_target($t->{'hash'});
2728 if ($link_target) {
2729 my $norm_target = normalize_link_target($link_target, $basedir, $hash_base);
2730 if (defined $norm_target) {
2731 print " -> " .
2732 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
2733 file_name=>$norm_target),
2734 -title => $norm_target}, esc_path($link_target));
2735 } else {
2736 print " -> " . esc_path($link_target);
2737 }
2738 }
2739 }
2740 print "</td>\n";
2741 print "<td class=\"link\">";
2742 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
2743 file_name=>"$basedir$t->{'name'}", %base_key)},
2744 "blob");
2745 if ($have_blame) {
2746 print " | " .
2747 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
2748 file_name=>"$basedir$t->{'name'}", %base_key)},
2749 "blame");
2750 }
2751 if (defined $hash_base) {
2752 print " | " .
2753 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2754 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
2755 "history");
2756 }
2757 print " | " .
2758 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
2759 file_name=>"$basedir$t->{'name'}")},
2760 "raw");
2761 print "</td>\n";
2762
2763 } elsif ($t->{'type'} eq "tree") {
2764 print "<td class=\"list\">";
2765 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
2766 file_name=>"$basedir$t->{'name'}", %base_key)},
2767 esc_path($t->{'name'}));
2768 print "</td>\n";
2769 print "<td class=\"link\">";
2770 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
2771 file_name=>"$basedir$t->{'name'}", %base_key)},
2772 "tree");
2773 if (defined $hash_base) {
2774 print " | " .
2775 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2776 file_name=>"$basedir$t->{'name'}")},
2777 "history");
2778 }
2779 print "</td>\n";
2780 } else {
2781 # unknown object: we can only present history for it
2782 # (this includes 'commit' object, i.e. submodule support)
2783 print "<td class=\"list\">" .
2784 esc_path($t->{'name'}) .
2785 "</td>\n";
2786 print "<td class=\"link\">";
2787 if (defined $hash_base) {
2788 print $cgi->a({-href => href(action=>"history",
2789 hash_base=>$hash_base,
2790 file_name=>"$basedir$t->{'name'}")},
2791 "history");
2792 }
2793 print "</td>\n";
2794 }
2795 }
2796
2797 ## ......................................................................
2798 ## functions printing large fragments of HTML
2799
2800 # get pre-image filenames for merge (combined) diff
2801 sub fill_from_file_info {
2802 my ($diff, @parents) = @_;
2803
2804 $diff->{'from_file'} = [ ];
2805 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
2806 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
2807 if ($diff->{'status'}[$i] eq 'R' ||
2808 $diff->{'status'}[$i] eq 'C') {
2809 $diff->{'from_file'}[$i] =
2810 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
2811 }
2812 }
2813
2814 return $diff;
2815 }
2816
2817 # is current raw difftree line of file deletion
2818 sub is_deleted {
2819 my $diffinfo = shift;
2820
2821 return $diffinfo->{'to_id'} eq ('0' x 40);
2822 }
2823
2824 # does patch correspond to [previous] difftree raw line
2825 # $diffinfo - hashref of parsed raw diff format
2826 # $patchinfo - hashref of parsed patch diff format
2827 # (the same keys as in $diffinfo)
2828 sub is_patch_split {
2829 my ($diffinfo, $patchinfo) = @_;
2830
2831 return defined $diffinfo && defined $patchinfo
2832 && ($diffinfo->{'to_file'} || $diffinfo->{'file'}) eq $patchinfo->{'to_file'};
2833 }
2834
2835
2836 sub git_difftree_body {
2837 my ($difftree, $hash, @parents) = @_;
2838 my ($parent) = $parents[0];
2839 my ($have_blame) = gitweb_check_feature('blame');
2840 print "<div class=\"list_head\">\n";
2841 if ($#{$difftree} > 10) {
2842 print(($#{$difftree} + 1) . " files changed:\n");
2843 }
2844 print "</div>\n";
2845
2846 print "<table class=\"" .
2847 (@parents > 1 ? "combined " : "") .
2848 "diff_tree\">\n";
2849
2850 # header only for combined diff in 'commitdiff' view
2851 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
2852 if ($has_header) {
2853 # table header
2854 print "<thead><tr>\n" .
2855 "<th></th><th></th>\n"; # filename, patchN link
2856 for (my $i = 0; $i < @parents; $i++) {
2857 my $par = $parents[$i];
2858 print "<th>" .
2859 $cgi->a({-href => href(action=>"commitdiff",
2860 hash=>$hash, hash_parent=>$par),
2861 -title => 'commitdiff to parent number ' .
2862 ($i+1) . ': ' . substr($par,0,7)},
2863 $i+1) .
2864 "&nbsp;</th>\n";
2865 }
2866 print "</tr></thead>\n<tbody>\n";
2867 }
2868
2869 my $alternate = 1;
2870 my $patchno = 0;
2871 foreach my $line (@{$difftree}) {
2872 my $diff = parsed_difftree_line($line);
2873
2874 if ($alternate) {
2875 print "<tr class=\"dark\">\n";
2876 } else {
2877 print "<tr class=\"light\">\n";
2878 }
2879 $alternate ^= 1;
2880
2881 if (exists $diff->{'nparents'}) { # combined diff
2882
2883 fill_from_file_info($diff, @parents)
2884 unless exists $diff->{'from_file'};
2885
2886 if (!is_deleted($diff)) {
2887 # file exists in the result (child) commit
2888 print "<td>" .
2889 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2890 file_name=>$diff->{'to_file'},
2891 hash_base=>$hash),
2892 -class => "list"}, esc_path($diff->{'to_file'})) .
2893 "</td>\n";
2894 } else {
2895 print "<td>" .
2896 esc_path($diff->{'to_file'}) .
2897 "</td>\n";
2898 }
2899
2900 if ($action eq 'commitdiff') {
2901 # link to patch
2902 $patchno++;
2903 print "<td class=\"link\">" .
2904 $cgi->a({-href => "#patch$patchno"}, "patch") .
2905 " | " .
2906 "</td>\n";
2907 }
2908
2909 my $has_history = 0;
2910 my $not_deleted = 0;
2911 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
2912 my $hash_parent = $parents[$i];
2913 my $from_hash = $diff->{'from_id'}[$i];
2914 my $from_path = $diff->{'from_file'}[$i];
2915 my $status = $diff->{'status'}[$i];
2916
2917 $has_history ||= ($status ne 'A');
2918 $not_deleted ||= ($status ne 'D');
2919
2920 if ($status eq 'A') {
2921 print "<td class=\"link\" align=\"right\"> | </td>\n";
2922 } elsif ($status eq 'D') {
2923 print "<td class=\"link\">" .
2924 $cgi->a({-href => href(action=>"blob",
2925 hash_base=>$hash,
2926 hash=>$from_hash,
2927 file_name=>$from_path)},
2928 "blob" . ($i+1)) .
2929 " | </td>\n";
2930 } else {
2931 if ($diff->{'to_id'} eq $from_hash) {
2932 print "<td class=\"link nochange\">";
2933 } else {
2934 print "<td class=\"link\">";
2935 }
2936 print $cgi->a({-href => href(action=>"blobdiff",
2937 hash=>$diff->{'to_id'},
2938 hash_parent=>$from_hash,
2939 hash_base=>$hash,
2940 hash_parent_base=>$hash_parent,
2941 file_name=>$diff->{'to_file'},
2942 file_parent=>$from_path)},
2943 "diff" . ($i+1)) .
2944 " | </td>\n";
2945 }
2946 }
2947
2948 print "<td class=\"link\">";
2949 if ($not_deleted) {
2950 print $cgi->a({-href => href(action=>"blob",
2951 hash=>$diff->{'to_id'},
2952 file_name=>$diff->{'to_file'},
2953 hash_base=>$hash)},
2954 "blob");
2955 print " | " if ($has_history);
2956 }
2957 if ($has_history) {
2958 print $cgi->a({-href => href(action=>"history",
2959 file_name=>$diff->{'to_file'},
2960 hash_base=>$hash)},
2961 "history");
2962 }
2963 print "</td>\n";
2964
2965 print "</tr>\n";
2966 next; # instead of 'else' clause, to avoid extra indent
2967 }
2968 # else ordinary diff
2969
2970 my ($to_mode_oct, $to_mode_str, $to_file_type);
2971 my ($from_mode_oct, $from_mode_str, $from_file_type);
2972 if ($diff->{'to_mode'} ne ('0' x 6)) {
2973 $to_mode_oct = oct $diff->{'to_mode'};
2974 if (S_ISREG($to_mode_oct)) { # only for regular file
2975 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
2976 }
2977 $to_file_type = file_type($diff->{'to_mode'});
2978 }
2979 if ($diff->{'from_mode'} ne ('0' x 6)) {
2980 $from_mode_oct = oct $diff->{'from_mode'};
2981 if (S_ISREG($to_mode_oct)) { # only for regular file
2982 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
2983 }
2984 $from_file_type = file_type($diff->{'from_mode'});
2985 }
2986
2987 if ($diff->{'status'} eq "A") { # created
2988 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
2989 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
2990 $mode_chng .= "]</span>";
2991 print "<td>";
2992 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2993 hash_base=>$hash, file_name=>$diff->{'file'}),
2994 -class => "list"}, esc_path($diff->{'file'}));
2995 print "</td>\n";
2996 print "<td>$mode_chng</td>\n";
2997 print "<td class=\"link\">";
2998 if ($action eq 'commitdiff') {
2999 # link to patch
3000 $patchno++;
3001 print $cgi->a({-href => "#patch$patchno"}, "patch");
3002 print " | ";
3003 }
3004 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3005 hash_base=>$hash, file_name=>$diff->{'file'})},
3006 "blob");
3007 print "</td>\n";
3008
3009 } elsif ($diff->{'status'} eq "D") { # deleted
3010 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
3011 print "<td>";
3012 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3013 hash_base=>$parent, file_name=>$diff->{'file'}),
3014 -class => "list"}, esc_path($diff->{'file'}));
3015 print "</td>\n";
3016 print "<td>$mode_chng</td>\n";
3017 print "<td class=\"link\">";
3018 if ($action eq 'commitdiff') {
3019 # link to patch
3020 $patchno++;
3021 print $cgi->a({-href => "#patch$patchno"}, "patch");
3022 print " | ";
3023 }
3024 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3025 hash_base=>$parent, file_name=>$diff->{'file'})},
3026 "blob") . " | ";
3027 if ($have_blame) {
3028 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
3029 file_name=>$diff->{'file'})},
3030 "blame") . " | ";
3031 }
3032 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
3033 file_name=>$diff->{'file'})},
3034 "history");
3035 print "</td>\n";
3036
3037 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
3038 my $mode_chnge = "";
3039 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3040 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
3041 if ($from_file_type ne $to_file_type) {
3042 $mode_chnge .= " from $from_file_type to $to_file_type";
3043 }
3044 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
3045 if ($from_mode_str && $to_mode_str) {
3046 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
3047 } elsif ($to_mode_str) {
3048 $mode_chnge .= " mode: $to_mode_str";
3049 }
3050 }
3051 $mode_chnge .= "]</span>\n";
3052 }
3053 print "<td>";
3054 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3055 hash_base=>$hash, file_name=>$diff->{'file'}),
3056 -class => "list"}, esc_path($diff->{'file'}));
3057 print "</td>\n";
3058 print "<td>$mode_chnge</td>\n";
3059 print "<td class=\"link\">";
3060 if ($action eq 'commitdiff') {
3061 # link to patch
3062 $patchno++;
3063 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3064 " | ";
3065 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
3066 # "commit" view and modified file (not onlu mode changed)
3067 print $cgi->a({-href => href(action=>"blobdiff",
3068 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
3069 hash_base=>$hash, hash_parent_base=>$parent,
3070 file_name=>$diff->{'file'})},
3071 "diff") .
3072 " | ";
3073 }
3074 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3075 hash_base=>$hash, file_name=>$diff->{'file'})},
3076 "blob") . " | ";
3077 if ($have_blame) {
3078 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
3079 file_name=>$diff->{'file'})},
3080 "blame") . " | ";
3081 }
3082 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
3083 file_name=>$diff->{'file'})},
3084 "history");
3085 print "</td>\n";
3086
3087 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
3088 my %status_name = ('R' => 'moved', 'C' => 'copied');
3089 my $nstatus = $status_name{$diff->{'status'}};
3090 my $mode_chng = "";
3091 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3092 # mode also for directories, so we cannot use $to_mode_str
3093 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
3094 }
3095 print "<td>" .
3096 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
3097 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
3098 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
3099 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
3100 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
3101 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
3102 -class => "list"}, esc_path($diff->{'from_file'})) .
3103 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
3104 "<td class=\"link\">";
3105 if ($action eq 'commitdiff') {
3106 # link to patch
3107 $patchno++;
3108 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3109 " | ";
3110 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
3111 # "commit" view and modified file (not only pure rename or copy)
3112 print $cgi->a({-href => href(action=>"blobdiff",
3113 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
3114 hash_base=>$hash, hash_parent_base=>$parent,
3115 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
3116 "diff") .
3117 " | ";
3118 }
3119 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3120 hash_base=>$parent, file_name=>$diff->{'to_file'})},
3121 "blob") . " | ";
3122 if ($have_blame) {
3123 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
3124 file_name=>$diff->{'to_file'})},
3125 "blame") . " | ";
3126 }
3127 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
3128 file_name=>$diff->{'to_file'})},
3129 "history");
3130 print "</td>\n";
3131
3132 } # we should not encounter Unmerged (U) or Unknown (X) status
3133 print "</tr>\n";
3134 }
3135 print "</tbody>" if $has_header;
3136 print "</table>\n";
3137 }
3138
3139 sub git_patchset_body {
3140 my ($fd, $difftree, $hash, @hash_parents) = @_;
3141 my ($hash_parent) = $hash_parents[0];
3142
3143 my $is_combined = (@hash_parents > 1);
3144 my $patch_idx = 0;
3145 my $patch_number = 0;
3146 my $patch_line;
3147 my $diffinfo;
3148 my $to_name;
3149 my (%from, %to);
3150
3151 print "<div class=\"patchset\">\n";
3152
3153 # skip to first patch
3154 while ($patch_line = <$fd>) {
3155 chomp $patch_line;
3156
3157 last if ($patch_line =~ m/^diff /);
3158 }
3159
3160 PATCH:
3161 while ($patch_line) {
3162
3163 # parse "git diff" header line
3164 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
3165 # $1 is from_name, which we do not use
3166 $to_name = unquote($2);
3167 $to_name =~ s!^b/!!;
3168 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
3169 # $1 is 'cc' or 'combined', which we do not use
3170 $to_name = unquote($2);
3171 } else {
3172 $to_name = undef;
3173 }
3174
3175 # check if current patch belong to current raw line
3176 # and parse raw git-diff line if needed
3177 if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
3178 # this is continuation of a split patch
3179 print "<div class=\"patch cont\">\n";
3180 } else {
3181 # advance raw git-diff output if needed
3182 $patch_idx++ if defined $diffinfo;
3183
3184 # read and prepare patch information
3185 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3186
3187 # compact combined diff output can have some patches skipped
3188 # find which patch (using pathname of result) we are at now;
3189 if ($is_combined) {
3190 while ($to_name ne $diffinfo->{'to_file'}) {
3191 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3192 format_diff_cc_simplified($diffinfo, @hash_parents) .
3193 "</div>\n"; # class="patch"
3194
3195 $patch_idx++;
3196 $patch_number++;
3197
3198 last if $patch_idx > $#$difftree;
3199 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3200 }
3201 }
3202
3203 # modifies %from, %to hashes
3204 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
3205
3206 # this is first patch for raw difftree line with $patch_idx index
3207 # we index @$difftree array from 0, but number patches from 1
3208 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
3209 }
3210
3211 # git diff header
3212 #assert($patch_line =~ m/^diff /) if DEBUG;
3213 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
3214 $patch_number++;
3215 # print "git diff" header
3216 print format_git_diff_header_line($patch_line, $diffinfo,
3217 \%from, \%to);
3218
3219 # print extended diff header
3220 print "<div class=\"diff extended_header\">\n";
3221 EXTENDED_HEADER:
3222 while ($patch_line = <$fd>) {
3223 chomp $patch_line;
3224
3225 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
3226
3227 print format_extended_diff_header_line($patch_line, $diffinfo,
3228 \%from, \%to);
3229 }
3230 print "</div>\n"; # class="diff extended_header"
3231
3232 # from-file/to-file diff header
3233 if (! $patch_line) {
3234 print "</div>\n"; # class="patch"
3235 last PATCH;
3236 }
3237 next PATCH if ($patch_line =~ m/^diff /);
3238 #assert($patch_line =~ m/^---/) if DEBUG;
3239
3240 my $last_patch_line = $patch_line;
3241 $patch_line = <$fd>;
3242 chomp $patch_line;
3243 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
3244
3245 print format_diff_from_to_header($last_patch_line, $patch_line,
3246 $diffinfo, \%from, \%to,
3247 @hash_parents);
3248
3249 # the patch itself
3250 LINE:
3251 while ($patch_line = <$fd>) {
3252 chomp $patch_line;
3253
3254 next PATCH if ($patch_line =~ m/^diff /);
3255
3256 print format_diff_line($patch_line, \%from, \%to);
3257 }
3258
3259 } continue {
3260 print "</div>\n"; # class="patch"
3261 }
3262
3263 # for compact combined (--cc) format, with chunk and patch simpliciaction
3264 # patchset might be empty, but there might be unprocessed raw lines
3265 for (++$patch_idx if $patch_number > 0;
3266 $patch_idx < @$difftree;
3267 ++$patch_idx) {
3268 # read and prepare patch information
3269 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3270
3271 # generate anchor for "patch" links in difftree / whatchanged part
3272 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3273 format_diff_cc_simplified($diffinfo, @hash_parents) .
3274 "</div>\n"; # class="patch"
3275
3276 $patch_number++;
3277 }
3278
3279 if ($patch_number == 0) {
3280 if (@hash_parents > 1) {
3281 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
3282 } else {
3283 print "<div class=\"diff nodifferences\">No differences found</div>\n";
3284 }
3285 }
3286
3287 print "</div>\n"; # class="patchset"
3288 }
3289
3290 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3291
3292 sub git_project_list_body {
3293 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
3294
3295 my ($check_forks) = gitweb_check_feature('forks');
3296
3297 my @projects;
3298 foreach my $pr (@$projlist) {
3299 my (@aa) = git_get_last