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