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