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