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