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