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