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