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