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