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