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