]> Lady’s Gitweb - Gitweb/blob - gitweb.perl
412ef1208433f5b6af8757940c667d14c7d4b6a0b074ac67d01c1e75ef819c3c
[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 5.008;
11 use strict;
12 use warnings;
13 use CGI qw(:standard :escapeHTML -nosticky);
14 use CGI::Util qw(unescape);
15 use CGI::Carp qw(fatalsToBrowser set_message);
16 use Encode;
17 use Fcntl ':mode';
18 use File::Find qw();
19 use File::Basename qw(basename);
20 use Time::HiRes qw(gettimeofday tv_interval);
21 binmode STDOUT, ':utf8';
22
23 our $t0 = [ gettimeofday() ];
24 our $number_of_git_cmds = 0;
25
26 BEGIN {
27 CGI->compile() if $ENV{'MOD_PERL'};
28 }
29
30 our $version = "++GIT_VERSION++";
31
32 our ($my_url, $my_uri, $base_url, $path_info, $home_link);
33 sub evaluate_uri {
34 our $cgi;
35
36 our $my_url = $cgi->url();
37 our $my_uri = $cgi->url(-absolute => 1);
38
39 # Base URL for relative URLs in gitweb ($logo, $favicon, ...),
40 # needed and used only for URLs with nonempty PATH_INFO
41 our $base_url = $my_url;
42
43 # When the script is used as DirectoryIndex, the URL does not contain the name
44 # of the script file itself, and $cgi->url() fails to strip PATH_INFO, so we
45 # have to do it ourselves. We make $path_info global because it's also used
46 # later on.
47 #
48 # Another issue with the script being the DirectoryIndex is that the resulting
49 # $my_url data is not the full script URL: this is good, because we want
50 # generated links to keep implying the script name if it wasn't explicitly
51 # indicated in the URL we're handling, but it means that $my_url cannot be used
52 # as base URL.
53 # Therefore, if we needed to strip PATH_INFO, then we know that we have
54 # to build the base URL ourselves:
55 our $path_info = decode_utf8($ENV{"PATH_INFO"});
56 if ($path_info) {
57 # $path_info has already been URL-decoded by the web server, but
58 # $my_url and $my_uri have not. URL-decode them so we can properly
59 # strip $path_info.
60 $my_url = unescape($my_url);
61 $my_uri = unescape($my_uri);
62 if ($my_url =~ s,\Q$path_info\E$,, &&
63 $my_uri =~ s,\Q$path_info\E$,, &&
64 defined $ENV{'SCRIPT_NAME'}) {
65 $base_url = $cgi->url(-base => 1) . $ENV{'SCRIPT_NAME'};
66 }
67 }
68
69 # target of the home link on top of all pages
70 our $home_link = $my_uri || "/";
71 }
72
73 # core git executable to use
74 # this can just be "git" if your webserver has a sensible PATH
75 our $GIT = "++GIT_BINDIR++/git";
76
77 # absolute fs-path which will be prepended to the project path
78 #our $projectroot = "/pub/scm";
79 our $projectroot = "++GITWEB_PROJECTROOT++";
80
81 # fs traversing limit for getting project list
82 # the number is relative to the projectroot
83 our $project_maxdepth = "++GITWEB_PROJECT_MAXDEPTH++";
84
85 # string of the home link on top of all pages
86 our $home_link_str = "++GITWEB_HOME_LINK_STR++";
87
88 # name of your site or organization to appear in page titles
89 # replace this with something more descriptive for clearer bookmarks
90 our $site_name = "++GITWEB_SITENAME++"
91 || ($ENV{'SERVER_NAME'} || "Untitled") . " Git";
92
93 # html snippet to include in the <head> section of each page
94 our $site_html_head_string = "++GITWEB_SITE_HTML_HEAD_STRING++";
95 # filename of html text to include at top of each page
96 our $site_header = "++GITWEB_SITE_HEADER++";
97 # html text to include at home page
98 our $home_text = "++GITWEB_HOMETEXT++";
99 # filename of html text to include at bottom of each page
100 our $site_footer = "++GITWEB_SITE_FOOTER++";
101
102 # URI of stylesheets
103 our @stylesheets = ("++GITWEB_CSS++");
104 # URI of a single stylesheet, which can be overridden in GITWEB_CONFIG.
105 our $stylesheet = undef;
106
107 # URI of GIT logo (72x27 size)
108 our $logo = "++GITWEB_LOGO++";
109 # URI of GIT favicon, assumed to be image/png type
110 our $favicon = "++GITWEB_FAVICON++";
111 # URI of gitweb.js (JavaScript code for gitweb)
112 our $javascript = "++GITWEB_JS++";
113
114 # URI and label (title) of GIT logo link
115 #our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
116 #our $logo_label = "git documentation";
117 our $logo_url = "http://git-scm.com/";
118 our $logo_label = "git homepage";
119
120 # source of projects list
121 our $projects_list = "++GITWEB_LIST++";
122
123 # the width (in characters) of the projects list "Description" column
124 our $projects_list_description_width = 25;
125
126 # group projects by category on the projects list
127 # (enabled if this variable evaluates to true)
128 our $projects_list_group_categories = 0;
129
130 # default category if none specified
131 # (leave the empty string for no category)
132 our $project_list_default_category = "";
133
134 # default order of projects list
135 # valid values are none, project, descr, owner, and age
136 our $default_projects_order = "project";
137
138 # show repository only if this file exists
139 # (only effective if this variable evaluates to true)
140 our $export_ok = "++GITWEB_EXPORT_OK++";
141
142 # don't generate age column on the projects list page
143 our $omit_age_column = 0;
144
145 # don't generate information about owners of repositories
146 our $omit_owner=0;
147
148 # show repository only if this subroutine returns true
149 # when given the path to the project, for example:
150 # sub { return -e "$_[0]/git-daemon-export-ok"; }
151 our $export_auth_hook = undef;
152
153 # only allow viewing of repositories also shown on the overview page
154 our $strict_export = "++GITWEB_STRICT_EXPORT++";
155
156 # list of git base URLs used for URL to where fetch project from,
157 # i.e. full URL is "$git_base_url/$project"
158 our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
159
160 # default blob_plain mimetype and default charset for text/plain blob
161 our $default_blob_plain_mimetype = 'text/plain';
162 our $default_text_plain_charset = undef;
163
164 # file to use for guessing MIME types before trying /etc/mime.types
165 # (relative to the current git repository)
166 our $mimetypes_file = undef;
167
168 # assume this charset if line contains non-UTF-8 characters;
169 # it should be valid encoding (see Encoding::Supported(3pm) for list),
170 # for which encoding all byte sequences are valid, for example
171 # 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
172 # could be even 'utf-8' for the old behavior)
173 our $fallback_encoding = 'latin1';
174
175 # rename detection options for git-diff and git-diff-tree
176 # - default is '-M', with the cost proportional to
177 # (number of removed files) * (number of new files).
178 # - more costly is '-C' (which implies '-M'), with the cost proportional to
179 # (number of changed files + number of removed files) * (number of new files)
180 # - even more costly is '-C', '--find-copies-harder' with cost
181 # (number of files in the original tree) * (number of new files)
182 # - one might want to include '-B' option, e.g. '-B', '-M'
183 our @diff_opts = ('-M'); # taken from git_commit
184
185 # Disables features that would allow repository owners to inject script into
186 # the gitweb domain.
187 our $prevent_xss = 0;
188
189 # Path to the highlight executable to use (must be the one from
190 # http://www.andre-simon.de due to assumptions about parameters and output).
191 # Useful if highlight is not installed on your webserver's PATH.
192 # [Default: highlight]
193 our $highlight_bin = "++HIGHLIGHT_BIN++";
194
195 # information about snapshot formats that gitweb is capable of serving
196 our %known_snapshot_formats = (
197 # name => {
198 # 'display' => display name,
199 # 'type' => mime type,
200 # 'suffix' => filename suffix,
201 # 'format' => --format for git-archive,
202 # 'compressor' => [compressor command and arguments]
203 # (array reference, optional)
204 # 'disabled' => boolean (optional)}
205 #
206 'tgz' => {
207 'display' => 'tar.gz',
208 'type' => 'application/x-gzip',
209 'suffix' => '.tar.gz',
210 'format' => 'tar',
211 'compressor' => ['gzip', '-n']},
212
213 'tbz2' => {
214 'display' => 'tar.bz2',
215 'type' => 'application/x-bzip2',
216 'suffix' => '.tar.bz2',
217 'format' => 'tar',
218 'compressor' => ['bzip2']},
219
220 'txz' => {
221 'display' => 'tar.xz',
222 'type' => 'application/x-xz',
223 'suffix' => '.tar.xz',
224 'format' => 'tar',
225 'compressor' => ['xz'],
226 'disabled' => 1},
227
228 'zip' => {
229 'display' => 'zip',
230 'type' => 'application/x-zip',
231 'suffix' => '.zip',
232 'format' => 'zip'},
233 );
234
235 # Aliases so we understand old gitweb.snapshot values in repository
236 # configuration.
237 our %known_snapshot_format_aliases = (
238 'gzip' => 'tgz',
239 'bzip2' => 'tbz2',
240 'xz' => 'txz',
241
242 # backward compatibility: legacy gitweb config support
243 'x-gzip' => undef, 'gz' => undef,
244 'x-bzip2' => undef, 'bz2' => undef,
245 'x-zip' => undef, '' => undef,
246 );
247
248 # Pixel sizes for icons and avatars. If the default font sizes or lineheights
249 # are changed, it may be appropriate to change these values too via
250 # $GITWEB_CONFIG.
251 our %avatar_size = (
252 'default' => 16,
253 'double' => 32
254 );
255
256 # Used to set the maximum load that we will still respond to gitweb queries.
257 # If server load exceed this value then return "503 server busy" error.
258 # If gitweb cannot determined server load, it is taken to be 0.
259 # Leave it undefined (or set to 'undef') to turn off load checking.
260 our $maxload = 300;
261
262 # configuration for 'highlight' (http://www.andre-simon.de/)
263 # match by basename
264 our %highlight_basename = (
265 #'Program' => 'py',
266 #'Library' => 'py',
267 'SConstruct' => 'py', # SCons equivalent of Makefile
268 'Makefile' => 'make',
269 );
270 # match by extension
271 our %highlight_ext = (
272 # main extensions, defining name of syntax;
273 # see files in /usr/share/highlight/langDefs/ directory
274 (map { $_ => $_ } qw(py rb java css js tex bib xml awk bat ini spec tcl sql)),
275 # alternate extensions, see /etc/highlight/filetypes.conf
276 (map { $_ => 'c' } qw(c h)),
277 (map { $_ => 'sh' } qw(sh bash zsh ksh)),
278 (map { $_ => 'cpp' } qw(cpp cxx c++ cc)),
279 (map { $_ => 'php' } qw(php php3 php4 php5 phps)),
280 (map { $_ => 'pl' } qw(pl perl pm)), # perhaps also 'cgi'
281 (map { $_ => 'make'} qw(make mak mk)),
282 (map { $_ => 'xml' } qw(xml xhtml html htm)),
283 );
284
285 # You define site-wide feature defaults here; override them with
286 # $GITWEB_CONFIG as necessary.
287 our %feature = (
288 # feature => {
289 # 'sub' => feature-sub (subroutine),
290 # 'override' => allow-override (boolean),
291 # 'default' => [ default options...] (array reference)}
292 #
293 # if feature is overridable (it means that allow-override has true value),
294 # then feature-sub will be called with default options as parameters;
295 # return value of feature-sub indicates if to enable specified feature
296 #
297 # if there is no 'sub' key (no feature-sub), then feature cannot be
298 # overridden
299 #
300 # use gitweb_get_feature(<feature>) to retrieve the <feature> value
301 # (an array) or gitweb_check_feature(<feature>) to check if <feature>
302 # is enabled
303
304 # Enable the 'blame' blob view, showing the last commit that modified
305 # each line in the file. This can be very CPU-intensive.
306
307 # To enable system wide have in $GITWEB_CONFIG
308 # $feature{'blame'}{'default'} = [1];
309 # To have project specific config enable override in $GITWEB_CONFIG
310 # $feature{'blame'}{'override'} = 1;
311 # and in project config gitweb.blame = 0|1;
312 'blame' => {
313 'sub' => sub { feature_bool('blame', @_) },
314 'override' => 0,
315 'default' => [0]},
316
317 # Enable the 'snapshot' link, providing a compressed archive of any
318 # tree. This can potentially generate high traffic if you have large
319 # project.
320
321 # Value is a list of formats defined in %known_snapshot_formats that
322 # you wish to offer.
323 # To disable system wide have in $GITWEB_CONFIG
324 # $feature{'snapshot'}{'default'} = [];
325 # To have project specific config enable override in $GITWEB_CONFIG
326 # $feature{'snapshot'}{'override'} = 1;
327 # and in project config, a comma-separated list of formats or "none"
328 # to disable. Example: gitweb.snapshot = tbz2,zip;
329 'snapshot' => {
330 'sub' => \&feature_snapshot,
331 'override' => 0,
332 'default' => ['tgz']},
333
334 # Enable text search, which will list the commits which match author,
335 # committer or commit text to a given string. Enabled by default.
336 # Project specific override is not supported.
337 #
338 # Note that this controls all search features, which means that if
339 # it is disabled, then 'grep' and 'pickaxe' search would also be
340 # disabled.
341 'search' => {
342 'override' => 0,
343 'default' => [1]},
344
345 # Enable grep search, which will list the files in currently selected
346 # tree containing the given string. Enabled by default. This can be
347 # potentially CPU-intensive, of course.
348 # Note that you need to have 'search' feature enabled too.
349
350 # To enable system wide have in $GITWEB_CONFIG
351 # $feature{'grep'}{'default'} = [1];
352 # To have project specific config enable override in $GITWEB_CONFIG
353 # $feature{'grep'}{'override'} = 1;
354 # and in project config gitweb.grep = 0|1;
355 'grep' => {
356 'sub' => sub { feature_bool('grep', @_) },
357 'override' => 0,
358 'default' => [1]},
359
360 # Enable the pickaxe search, which will list the commits that modified
361 # a given string in a file. This can be practical and quite faster
362 # alternative to 'blame', but still potentially CPU-intensive.
363 # Note that you need to have 'search' feature enabled too.
364
365 # To enable system wide have in $GITWEB_CONFIG
366 # $feature{'pickaxe'}{'default'} = [1];
367 # To have project specific config enable override in $GITWEB_CONFIG
368 # $feature{'pickaxe'}{'override'} = 1;
369 # and in project config gitweb.pickaxe = 0|1;
370 'pickaxe' => {
371 'sub' => sub { feature_bool('pickaxe', @_) },
372 'override' => 0,
373 'default' => [1]},
374
375 # Enable showing size of blobs in a 'tree' view, in a separate
376 # column, similar to what 'ls -l' does. This cost a bit of IO.
377
378 # To disable system wide have in $GITWEB_CONFIG
379 # $feature{'show-sizes'}{'default'} = [0];
380 # To have project specific config enable override in $GITWEB_CONFIG
381 # $feature{'show-sizes'}{'override'} = 1;
382 # and in project config gitweb.showsizes = 0|1;
383 'show-sizes' => {
384 'sub' => sub { feature_bool('showsizes', @_) },
385 'override' => 0,
386 'default' => [1]},
387
388 # Make gitweb use an alternative format of the URLs which can be
389 # more readable and natural-looking: project name is embedded
390 # directly in the path and the query string contains other
391 # auxiliary information. All gitweb installations recognize
392 # URL in either format; this configures in which formats gitweb
393 # generates links.
394
395 # To enable system wide have in $GITWEB_CONFIG
396 # $feature{'pathinfo'}{'default'} = [1];
397 # Project specific override is not supported.
398
399 # Note that you will need to change the default location of CSS,
400 # favicon, logo and possibly other files to an absolute URL. Also,
401 # if gitweb.cgi serves as your indexfile, you will need to force
402 # $my_uri to contain the script name in your $GITWEB_CONFIG.
403 'pathinfo' => {
404 'override' => 0,
405 'default' => [0]},
406
407 # Make gitweb consider projects in project root subdirectories
408 # to be forks of existing projects. Given project $projname.git,
409 # projects matching $projname/*.git will not be shown in the main
410 # projects list, instead a '+' mark will be added to $projname
411 # there and a 'forks' view will be enabled for the project, listing
412 # all the forks. If project list is taken from a file, forks have
413 # to be listed after the main project.
414
415 # To enable system wide have in $GITWEB_CONFIG
416 # $feature{'forks'}{'default'} = [1];
417 # Project specific override is not supported.
418 'forks' => {
419 'override' => 0,
420 'default' => [0]},
421
422 # Insert custom links to the action bar of all project pages.
423 # This enables you mainly to link to third-party scripts integrating
424 # into gitweb; e.g. git-browser for graphical history representation
425 # or custom web-based repository administration interface.
426
427 # The 'default' value consists of a list of triplets in the form
428 # (label, link, position) where position is the label after which
429 # to insert the link and link is a format string where %n expands
430 # to the project name, %f to the project path within the filesystem,
431 # %h to the current hash (h gitweb parameter) and %b to the current
432 # hash base (hb gitweb parameter); %% expands to %.
433
434 # To enable system wide have in $GITWEB_CONFIG e.g.
435 # $feature{'actions'}{'default'} = [('graphiclog',
436 # '/git-browser/by-commit.html?r=%n', 'summary')];
437 # Project specific override is not supported.
438 'actions' => {
439 'override' => 0,
440 'default' => []},
441
442 # Allow gitweb scan project content tags of project repository,
443 # and display the popular Web 2.0-ish "tag cloud" near the projects
444 # list. Note that this is something COMPLETELY different from the
445 # normal Git tags.
446
447 # gitweb by itself can show existing tags, but it does not handle
448 # tagging itself; you need to do it externally, outside gitweb.
449 # The format is described in git_get_project_ctags() subroutine.
450 # You may want to install the HTML::TagCloud Perl module to get
451 # a pretty tag cloud instead of just a list of tags.
452
453 # To enable system wide have in $GITWEB_CONFIG
454 # $feature{'ctags'}{'default'} = [1];
455 # Project specific override is not supported.
456
457 # In the future whether ctags editing is enabled might depend
458 # on the value, but using 1 should always mean no editing of ctags.
459 'ctags' => {
460 'override' => 0,
461 'default' => [0]},
462
463 # The maximum number of patches in a patchset generated in patch
464 # view. Set this to 0 or undef to disable patch view, or to a
465 # negative number to remove any limit.
466
467 # To disable system wide have in $GITWEB_CONFIG
468 # $feature{'patches'}{'default'} = [0];
469 # To have project specific config enable override in $GITWEB_CONFIG
470 # $feature{'patches'}{'override'} = 1;
471 # and in project config gitweb.patches = 0|n;
472 # where n is the maximum number of patches allowed in a patchset.
473 'patches' => {
474 'sub' => \&feature_patches,
475 'override' => 0,
476 'default' => [16]},
477
478 # Avatar support. When this feature is enabled, views such as
479 # shortlog or commit will display an avatar associated with
480 # the email of the committer(s) and/or author(s).
481
482 # Currently available providers are gravatar and picon.
483 # If an unknown provider is specified, the feature is disabled.
484
485 # Gravatar depends on Digest::MD5.
486 # Picon currently relies on the indiana.edu database.
487
488 # To enable system wide have in $GITWEB_CONFIG
489 # $feature{'avatar'}{'default'} = ['<provider>'];
490 # where <provider> is either gravatar or picon.
491 # To have project specific config enable override in $GITWEB_CONFIG
492 # $feature{'avatar'}{'override'} = 1;
493 # and in project config gitweb.avatar = <provider>;
494 'avatar' => {
495 'sub' => \&feature_avatar,
496 'override' => 0,
497 'default' => ['']},
498
499 # Enable displaying how much time and how many git commands
500 # it took to generate and display page. Disabled by default.
501 # Project specific override is not supported.
502 'timed' => {
503 'override' => 0,
504 'default' => [0]},
505
506 # Enable turning some links into links to actions which require
507 # JavaScript to run (like 'blame_incremental'). Not enabled by
508 # default. Project specific override is currently not supported.
509 'javascript-actions' => {
510 'override' => 0,
511 'default' => [0]},
512
513 # Enable and configure ability to change common timezone for dates
514 # in gitweb output via JavaScript. Enabled by default.
515 # Project specific override is not supported.
516 'javascript-timezone' => {
517 'override' => 0,
518 'default' => [
519 'local', # default timezone: 'utc', 'local', or '(-|+)HHMM' format,
520 # or undef to turn off this feature
521 'gitweb_tz', # name of cookie where to store selected timezone
522 'datetime', # CSS class used to mark up dates for manipulation
523 ]},
524
525 # Syntax highlighting support. This is based on Daniel Svensson's
526 # and Sham Chukoury's work in gitweb-xmms2.git.
527 # It requires the 'highlight' program present in $PATH,
528 # and therefore is disabled by default.
529
530 # To enable system wide have in $GITWEB_CONFIG
531 # $feature{'highlight'}{'default'} = [1];
532
533 'highlight' => {
534 'sub' => sub { feature_bool('highlight', @_) },
535 'override' => 0,
536 'default' => [0]},
537
538 # Enable displaying of remote heads in the heads list
539
540 # To enable system wide have in $GITWEB_CONFIG
541 # $feature{'remote_heads'}{'default'} = [1];
542 # To have project specific config enable override in $GITWEB_CONFIG
543 # $feature{'remote_heads'}{'override'} = 1;
544 # and in project config gitweb.remoteheads = 0|1;
545 'remote_heads' => {
546 'sub' => sub { feature_bool('remote_heads', @_) },
547 'override' => 0,
548 'default' => [0]},
549 );
550
551 sub gitweb_get_feature {
552 my ($name) = @_;
553 return unless exists $feature{$name};
554 my ($sub, $override, @defaults) = (
555 $feature{$name}{'sub'},
556 $feature{$name}{'override'},
557 @{$feature{$name}{'default'}});
558 # project specific override is possible only if we have project
559 our $git_dir; # global variable, declared later
560 if (!$override || !defined $git_dir) {
561 return @defaults;
562 }
563 if (!defined $sub) {
564 warn "feature $name is not overridable";
565 return @defaults;
566 }
567 return $sub->(@defaults);
568 }
569
570 # A wrapper to check if a given feature is enabled.
571 # With this, you can say
572 #
573 # my $bool_feat = gitweb_check_feature('bool_feat');
574 # gitweb_check_feature('bool_feat') or somecode;
575 #
576 # instead of
577 #
578 # my ($bool_feat) = gitweb_get_feature('bool_feat');
579 # (gitweb_get_feature('bool_feat'))[0] or somecode;
580 #
581 sub gitweb_check_feature {
582 return (gitweb_get_feature(@_))[0];
583 }
584
585
586 sub feature_bool {
587 my $key = shift;
588 my ($val) = git_get_project_config($key, '--bool');
589
590 if (!defined $val) {
591 return ($_[0]);
592 } elsif ($val eq 'true') {
593 return (1);
594 } elsif ($val eq 'false') {
595 return (0);
596 }
597 }
598
599 sub feature_snapshot {
600 my (@fmts) = @_;
601
602 my ($val) = git_get_project_config('snapshot');
603
604 if ($val) {
605 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
606 }
607
608 return @fmts;
609 }
610
611 sub feature_patches {
612 my @val = (git_get_project_config('patches', '--int'));
613
614 if (@val) {
615 return @val;
616 }
617
618 return ($_[0]);
619 }
620
621 sub feature_avatar {
622 my @val = (git_get_project_config('avatar'));
623
624 return @val ? @val : @_;
625 }
626
627 # checking HEAD file with -e is fragile if the repository was
628 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
629 # and then pruned.
630 sub check_head_link {
631 my ($dir) = @_;
632 my $headfile = "$dir/HEAD";
633 return ((-e $headfile) ||
634 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
635 }
636
637 sub check_export_ok {
638 my ($dir) = @_;
639 return (check_head_link($dir) &&
640 (!$export_ok || -e "$dir/$export_ok") &&
641 (!$export_auth_hook || $export_auth_hook->($dir)));
642 }
643
644 # process alternate names for backward compatibility
645 # filter out unsupported (unknown) snapshot formats
646 sub filter_snapshot_fmts {
647 my @fmts = @_;
648
649 @fmts = map {
650 exists $known_snapshot_format_aliases{$_} ?
651 $known_snapshot_format_aliases{$_} : $_} @fmts;
652 @fmts = grep {
653 exists $known_snapshot_formats{$_} &&
654 !$known_snapshot_formats{$_}{'disabled'}} @fmts;
655 }
656
657 # If it is set to code reference, it is code that it is to be run once per
658 # request, allowing updating configurations that change with each request,
659 # while running other code in config file only once.
660 #
661 # Otherwise, if it is false then gitweb would process config file only once;
662 # if it is true then gitweb config would be run for each request.
663 our $per_request_config = 1;
664
665 # read and parse gitweb config file given by its parameter.
666 # returns true on success, false on recoverable error, allowing
667 # to chain this subroutine, using first file that exists.
668 # dies on errors during parsing config file, as it is unrecoverable.
669 sub read_config_file {
670 my $filename = shift;
671 return unless defined $filename;
672 # die if there are errors parsing config file
673 if (-e $filename) {
674 do $filename;
675 die $@ if $@;
676 return 1;
677 }
678 return;
679 }
680
681 our ($GITWEB_CONFIG, $GITWEB_CONFIG_SYSTEM, $GITWEB_CONFIG_COMMON);
682 sub evaluate_gitweb_config {
683 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
684 our $GITWEB_CONFIG_SYSTEM = $ENV{'GITWEB_CONFIG_SYSTEM'} || "++GITWEB_CONFIG_SYSTEM++";
685 our $GITWEB_CONFIG_COMMON = $ENV{'GITWEB_CONFIG_COMMON'} || "++GITWEB_CONFIG_COMMON++";
686
687 # Protect against duplications of file names, to not read config twice.
688 # Only one of $GITWEB_CONFIG and $GITWEB_CONFIG_SYSTEM is used, so
689 # there possibility of duplication of filename there doesn't matter.
690 $GITWEB_CONFIG = "" if ($GITWEB_CONFIG eq $GITWEB_CONFIG_COMMON);
691 $GITWEB_CONFIG_SYSTEM = "" if ($GITWEB_CONFIG_SYSTEM eq $GITWEB_CONFIG_COMMON);
692
693 # Common system-wide settings for convenience.
694 # Those settings can be ovverriden by GITWEB_CONFIG or GITWEB_CONFIG_SYSTEM.
695 read_config_file($GITWEB_CONFIG_COMMON);
696
697 # Use first config file that exists. This means use the per-instance
698 # GITWEB_CONFIG if exists, otherwise use GITWEB_SYSTEM_CONFIG.
699 read_config_file($GITWEB_CONFIG) and return;
700 read_config_file($GITWEB_CONFIG_SYSTEM);
701 }
702
703 # Get loadavg of system, to compare against $maxload.
704 # Currently it requires '/proc/loadavg' present to get loadavg;
705 # if it is not present it returns 0, which means no load checking.
706 sub get_loadavg {
707 if( -e '/proc/loadavg' ){
708 open my $fd, '<', '/proc/loadavg'
709 or return 0;
710 my @load = split(/\s+/, scalar <$fd>);
711 close $fd;
712
713 # The first three columns measure CPU and IO utilization of the last one,
714 # five, and 10 minute periods. The fourth column shows the number of
715 # currently running processes and the total number of processes in the m/n
716 # format. The last column displays the last process ID used.
717 return $load[0] || 0;
718 }
719 # additional checks for load average should go here for things that don't export
720 # /proc/loadavg
721
722 return 0;
723 }
724
725 # version of the core git binary
726 our $git_version;
727 sub evaluate_git_version {
728 our $git_version = qx("$GIT" --version) =~ m/git version (.*)$/ ? $1 : "unknown";
729 $number_of_git_cmds++;
730 }
731
732 sub check_loadavg {
733 if (defined $maxload && get_loadavg() > $maxload) {
734 die_error(503, "The load average on the server is too high");
735 }
736 }
737
738 # ======================================================================
739 # input validation and dispatch
740
741 # input parameters can be collected from a variety of sources (presently, CGI
742 # and PATH_INFO), so we define an %input_params hash that collects them all
743 # together during validation: this allows subsequent uses (e.g. href()) to be
744 # agnostic of the parameter origin
745
746 our %input_params = ();
747
748 # input parameters are stored with the long parameter name as key. This will
749 # also be used in the href subroutine to convert parameters to their CGI
750 # equivalent, and since the href() usage is the most frequent one, we store
751 # the name -> CGI key mapping here, instead of the reverse.
752 #
753 # XXX: Warning: If you touch this, check the search form for updating,
754 # too.
755
756 our @cgi_param_mapping = (
757 project => "p",
758 action => "a",
759 file_name => "f",
760 file_parent => "fp",
761 hash => "h",
762 hash_parent => "hp",
763 hash_base => "hb",
764 hash_parent_base => "hpb",
765 page => "pg",
766 order => "o",
767 searchtext => "s",
768 searchtype => "st",
769 snapshot_format => "sf",
770 extra_options => "opt",
771 search_use_regexp => "sr",
772 ctag => "by_tag",
773 diff_style => "ds",
774 project_filter => "pf",
775 # this must be last entry (for manipulation from JavaScript)
776 javascript => "js"
777 );
778 our %cgi_param_mapping = @cgi_param_mapping;
779
780 # we will also need to know the possible actions, for validation
781 our %actions = (
782 "blame" => \&git_blame,
783 "blame_incremental" => \&git_blame_incremental,
784 "blame_data" => \&git_blame_data,
785 "blobdiff" => \&git_blobdiff,
786 "blobdiff_plain" => \&git_blobdiff_plain,
787 "blob" => \&git_blob,
788 "blob_plain" => \&git_blob_plain,
789 "commitdiff" => \&git_commitdiff,
790 "commitdiff_plain" => \&git_commitdiff_plain,
791 "commit" => \&git_commit,
792 "forks" => \&git_forks,
793 "heads" => \&git_heads,
794 "history" => \&git_history,
795 "log" => \&git_log,
796 "patch" => \&git_patch,
797 "patches" => \&git_patches,
798 "remotes" => \&git_remotes,
799 "rss" => \&git_rss,
800 "atom" => \&git_atom,
801 "search" => \&git_search,
802 "search_help" => \&git_search_help,
803 "shortlog" => \&git_shortlog,
804 "summary" => \&git_summary,
805 "tag" => \&git_tag,
806 "tags" => \&git_tags,
807 "tree" => \&git_tree,
808 "snapshot" => \&git_snapshot,
809 "object" => \&git_object,
810 # those below don't need $project
811 "opml" => \&git_opml,
812 "project_list" => \&git_project_list,
813 "project_index" => \&git_project_index,
814 );
815
816 # finally, we have the hash of allowed extra_options for the commands that
817 # allow them
818 our %allowed_options = (
819 "--no-merges" => [ qw(rss atom log shortlog history) ],
820 );
821
822 # fill %input_params with the CGI parameters. All values except for 'opt'
823 # should be single values, but opt can be an array. We should probably
824 # build an array of parameters that can be multi-valued, but since for the time
825 # being it's only this one, we just single it out
826 sub evaluate_query_params {
827 our $cgi;
828
829 while (my ($name, $symbol) = each %cgi_param_mapping) {
830 if ($symbol eq 'opt') {
831 $input_params{$name} = [ map { decode_utf8($_) } $cgi->param($symbol) ];
832 } else {
833 $input_params{$name} = decode_utf8($cgi->param($symbol));
834 }
835 }
836 }
837
838 # now read PATH_INFO and update the parameter list for missing parameters
839 sub evaluate_path_info {
840 return if defined $input_params{'project'};
841 return if !$path_info;
842 $path_info =~ s,^/+,,;
843 return if !$path_info;
844
845 # find which part of PATH_INFO is project
846 my $project = $path_info;
847 $project =~ s,/+$,,;
848 while ($project && !check_head_link("$projectroot/$project")) {
849 $project =~ s,/*[^/]*$,,;
850 }
851 return unless $project;
852 $input_params{'project'} = $project;
853
854 # do not change any parameters if an action is given using the query string
855 return if $input_params{'action'};
856 $path_info =~ s,^\Q$project\E/*,,;
857
858 # next, check if we have an action
859 my $action = $path_info;
860 $action =~ s,/.*$,,;
861 if (exists $actions{$action}) {
862 $path_info =~ s,^$action/*,,;
863 $input_params{'action'} = $action;
864 }
865
866 # list of actions that want hash_base instead of hash, but can have no
867 # pathname (f) parameter
868 my @wants_base = (
869 'tree',
870 'history',
871 );
872
873 # we want to catch, among others
874 # [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name]
875 my ($parentrefname, $parentpathname, $refname, $pathname) =
876 ($path_info =~ /^(?:(.+?)(?::(.+))?\.\.)?([^:]+?)?(?::(.+))?$/);
877
878 # first, analyze the 'current' part
879 if (defined $pathname) {
880 # we got "branch:filename" or "branch:dir/"
881 # we could use git_get_type(branch:pathname), but:
882 # - it needs $git_dir
883 # - it does a git() call
884 # - the convention of terminating directories with a slash
885 # makes it superfluous
886 # - embedding the action in the PATH_INFO would make it even
887 # more superfluous
888 $pathname =~ s,^/+,,;
889 if (!$pathname || substr($pathname, -1) eq "/") {
890 $input_params{'action'} ||= "tree";
891 $pathname =~ s,/$,,;
892 } else {
893 # the default action depends on whether we had parent info
894 # or not
895 if ($parentrefname) {
896 $input_params{'action'} ||= "blobdiff_plain";
897 } else {
898 $input_params{'action'} ||= "blob_plain";
899 }
900 }
901 $input_params{'hash_base'} ||= $refname;
902 $input_params{'file_name'} ||= $pathname;
903 } elsif (defined $refname) {
904 # we got "branch". In this case we have to choose if we have to
905 # set hash or hash_base.
906 #
907 # Most of the actions without a pathname only want hash to be
908 # set, except for the ones specified in @wants_base that want
909 # hash_base instead. It should also be noted that hand-crafted
910 # links having 'history' as an action and no pathname or hash
911 # set will fail, but that happens regardless of PATH_INFO.
912 if (defined $parentrefname) {
913 # if there is parent let the default be 'shortlog' action
914 # (for http://git.example.com/repo.git/A..B links); if there
915 # is no parent, dispatch will detect type of object and set
916 # action appropriately if required (if action is not set)
917 $input_params{'action'} ||= "shortlog";
918 }
919 if ($input_params{'action'} &&
920 grep { $_ eq $input_params{'action'} } @wants_base) {
921 $input_params{'hash_base'} ||= $refname;
922 } else {
923 $input_params{'hash'} ||= $refname;
924 }
925 }
926
927 # next, handle the 'parent' part, if present
928 if (defined $parentrefname) {
929 # a missing pathspec defaults to the 'current' filename, allowing e.g.
930 # someproject/blobdiff/oldrev..newrev:/filename
931 if ($parentpathname) {
932 $parentpathname =~ s,^/+,,;
933 $parentpathname =~ s,/$,,;
934 $input_params{'file_parent'} ||= $parentpathname;
935 } else {
936 $input_params{'file_parent'} ||= $input_params{'file_name'};
937 }
938 # we assume that hash_parent_base is wanted if a path was specified,
939 # or if the action wants hash_base instead of hash
940 if (defined $input_params{'file_parent'} ||
941 grep { $_ eq $input_params{'action'} } @wants_base) {
942 $input_params{'hash_parent_base'} ||= $parentrefname;
943 } else {
944 $input_params{'hash_parent'} ||= $parentrefname;
945 }
946 }
947
948 # for the snapshot action, we allow URLs in the form
949 # $project/snapshot/$hash.ext
950 # where .ext determines the snapshot and gets removed from the
951 # passed $refname to provide the $hash.
952 #
953 # To be able to tell that $refname includes the format extension, we
954 # require the following two conditions to be satisfied:
955 # - the hash input parameter MUST have been set from the $refname part
956 # of the URL (i.e. they must be equal)
957 # - the snapshot format MUST NOT have been defined already (e.g. from
958 # CGI parameter sf)
959 # It's also useless to try any matching unless $refname has a dot,
960 # so we check for that too
961 if (defined $input_params{'action'} &&
962 $input_params{'action'} eq 'snapshot' &&
963 defined $refname && index($refname, '.') != -1 &&
964 $refname eq $input_params{'hash'} &&
965 !defined $input_params{'snapshot_format'}) {
966 # We loop over the known snapshot formats, checking for
967 # extensions. Allowed extensions are both the defined suffix
968 # (which includes the initial dot already) and the snapshot
969 # format key itself, with a prepended dot
970 while (my ($fmt, $opt) = each %known_snapshot_formats) {
971 my $hash = $refname;
972 unless ($hash =~ s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) {
973 next;
974 }
975 my $sfx = $1;
976 # a valid suffix was found, so set the snapshot format
977 # and reset the hash parameter
978 $input_params{'snapshot_format'} = $fmt;
979 $input_params{'hash'} = $hash;
980 # we also set the format suffix to the one requested
981 # in the URL: this way a request for e.g. .tgz returns
982 # a .tgz instead of a .tar.gz
983 $known_snapshot_formats{$fmt}{'suffix'} = $sfx;
984 last;
985 }
986 }
987 }
988
989 our ($action, $project, $file_name, $file_parent, $hash, $hash_parent, $hash_base,
990 $hash_parent_base, @extra_options, $page, $searchtype, $search_use_regexp,
991 $searchtext, $search_regexp, $project_filter);
992 sub evaluate_and_validate_params {
993 our $action = $input_params{'action'};
994 if (defined $action) {
995 if (!validate_action($action)) {
996 die_error(400, "Invalid action parameter");
997 }
998 }
999
1000 # parameters which are pathnames
1001 our $project = $input_params{'project'};
1002 if (defined $project) {
1003 if (!validate_project($project)) {
1004 undef $project;
1005 die_error(404, "No such project");
1006 }
1007 }
1008
1009 our $project_filter = $input_params{'project_filter'};
1010 if (defined $project_filter) {
1011 if (!validate_pathname($project_filter)) {
1012 die_error(404, "Invalid project_filter parameter");
1013 }
1014 }
1015
1016 our $file_name = $input_params{'file_name'};
1017 if (defined $file_name) {
1018 if (!validate_pathname($file_name)) {
1019 die_error(400, "Invalid file parameter");
1020 }
1021 }
1022
1023 our $file_parent = $input_params{'file_parent'};
1024 if (defined $file_parent) {
1025 if (!validate_pathname($file_parent)) {
1026 die_error(400, "Invalid file parent parameter");
1027 }
1028 }
1029
1030 # parameters which are refnames
1031 our $hash = $input_params{'hash'};
1032 if (defined $hash) {
1033 if (!validate_refname($hash)) {
1034 die_error(400, "Invalid hash parameter");
1035 }
1036 }
1037
1038 our $hash_parent = $input_params{'hash_parent'};
1039 if (defined $hash_parent) {
1040 if (!validate_refname($hash_parent)) {
1041 die_error(400, "Invalid hash parent parameter");
1042 }
1043 }
1044
1045 our $hash_base = $input_params{'hash_base'};
1046 if (defined $hash_base) {
1047 if (!validate_refname($hash_base)) {
1048 die_error(400, "Invalid hash base parameter");
1049 }
1050 }
1051
1052 our @extra_options = @{$input_params{'extra_options'}};
1053 # @extra_options is always defined, since it can only be (currently) set from
1054 # CGI, and $cgi->param() returns the empty array in array context if the param
1055 # is not set
1056 foreach my $opt (@extra_options) {
1057 if (not exists $allowed_options{$opt}) {
1058 die_error(400, "Invalid option parameter");
1059 }
1060 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
1061 die_error(400, "Invalid option parameter for this action");
1062 }
1063 }
1064
1065 our $hash_parent_base = $input_params{'hash_parent_base'};
1066 if (defined $hash_parent_base) {
1067 if (!validate_refname($hash_parent_base)) {
1068 die_error(400, "Invalid hash parent base parameter");
1069 }
1070 }
1071
1072 # other parameters
1073 our $page = $input_params{'page'};
1074 if (defined $page) {
1075 if ($page =~ m/[^0-9]/) {
1076 die_error(400, "Invalid page parameter");
1077 }
1078 }
1079
1080 our $searchtype = $input_params{'searchtype'};
1081 if (defined $searchtype) {
1082 if ($searchtype =~ m/[^a-z]/) {
1083 die_error(400, "Invalid searchtype parameter");
1084 }
1085 }
1086
1087 our $search_use_regexp = $input_params{'search_use_regexp'};
1088
1089 our $searchtext = $input_params{'searchtext'};
1090 our $search_regexp = undef;
1091 if (defined $searchtext) {
1092 if (length($searchtext) < 2) {
1093 die_error(403, "At least two characters are required for search parameter");
1094 }
1095 if ($search_use_regexp) {
1096 $search_regexp = $searchtext;
1097 if (!eval { qr/$search_regexp/; 1; }) {
1098 (my $error = $@) =~ s/ at \S+ line \d+.*\n?//;
1099 die_error(400, "Invalid search regexp '$search_regexp'",
1100 esc_html($error));
1101 }
1102 } else {
1103 $search_regexp = quotemeta $searchtext;
1104 }
1105 }
1106 }
1107
1108 # path to the current git repository
1109 our $git_dir;
1110 sub evaluate_git_dir {
1111 our $git_dir = "$projectroot/$project" if $project;
1112 }
1113
1114 our (@snapshot_fmts, $git_avatar);
1115 sub configure_gitweb_features {
1116 # list of supported snapshot formats
1117 our @snapshot_fmts = gitweb_get_feature('snapshot');
1118 @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
1119
1120 # check that the avatar feature is set to a known provider name,
1121 # and for each provider check if the dependencies are satisfied.
1122 # if the provider name is invalid or the dependencies are not met,
1123 # reset $git_avatar to the empty string.
1124 our ($git_avatar) = gitweb_get_feature('avatar');
1125 if ($git_avatar eq 'gravatar') {
1126 $git_avatar = '' unless (eval { require Digest::MD5; 1; });
1127 } elsif ($git_avatar eq 'picon') {
1128 # no dependencies
1129 } else {
1130 $git_avatar = '';
1131 }
1132 }
1133
1134 # custom error handler: 'die <message>' is Internal Server Error
1135 sub handle_errors_html {
1136 my $msg = shift; # it is already HTML escaped
1137
1138 # to avoid infinite loop where error occurs in die_error,
1139 # change handler to default handler, disabling handle_errors_html
1140 set_message("Error occurred when inside die_error:\n$msg");
1141
1142 # you cannot jump out of die_error when called as error handler;
1143 # the subroutine set via CGI::Carp::set_message is called _after_
1144 # HTTP headers are already written, so it cannot write them itself
1145 die_error(undef, undef, $msg, -error_handler => 1, -no_http_header => 1);
1146 }
1147 set_message(\&handle_errors_html);
1148
1149 # dispatch
1150 sub dispatch {
1151 if (!defined $action) {
1152 if (defined $hash) {
1153 $action = git_get_type($hash);
1154 $action or die_error(404, "Object does not exist");
1155 } elsif (defined $hash_base && defined $file_name) {
1156 $action = git_get_type("$hash_base:$file_name");
1157 $action or die_error(404, "File or directory does not exist");
1158 } elsif (defined $project) {
1159 $action = 'summary';
1160 } else {
1161 $action = 'project_list';
1162 }
1163 }
1164 if (!defined($actions{$action})) {
1165 die_error(400, "Unknown action");
1166 }
1167 if ($action !~ m/^(?:opml|project_list|project_index)$/ &&
1168 !$project) {
1169 die_error(400, "Project needed");
1170 }
1171 $actions{$action}->();
1172 }
1173
1174 sub reset_timer {
1175 our $t0 = [ gettimeofday() ]
1176 if defined $t0;
1177 our $number_of_git_cmds = 0;
1178 }
1179
1180 our $first_request = 1;
1181 sub run_request {
1182 reset_timer();
1183
1184 evaluate_uri();
1185 if ($first_request) {
1186 evaluate_gitweb_config();
1187 evaluate_git_version();
1188 }
1189 if ($per_request_config) {
1190 if (ref($per_request_config) eq 'CODE') {
1191 $per_request_config->();
1192 } elsif (!$first_request) {
1193 evaluate_gitweb_config();
1194 }
1195 }
1196 check_loadavg();
1197
1198 # $projectroot and $projects_list might be set in gitweb config file
1199 $projects_list ||= $projectroot;
1200
1201 evaluate_query_params();
1202 evaluate_path_info();
1203 evaluate_and_validate_params();
1204 evaluate_git_dir();
1205
1206 configure_gitweb_features();
1207
1208 dispatch();
1209 }
1210
1211 our $is_last_request = sub { 1 };
1212 our ($pre_dispatch_hook, $post_dispatch_hook, $pre_listen_hook);
1213 our $CGI = 'CGI';
1214 our $cgi;
1215 sub configure_as_fcgi {
1216 require CGI::Fast;
1217 our $CGI = 'CGI::Fast';
1218
1219 my $request_number = 0;
1220 # let each child service 100 requests
1221 our $is_last_request = sub { ++$request_number > 100 };
1222 }
1223 sub evaluate_argv {
1224 my $script_name = $ENV{'SCRIPT_NAME'} || $ENV{'SCRIPT_FILENAME'} || __FILE__;
1225 configure_as_fcgi()
1226 if $script_name =~ /\.fcgi$/;
1227
1228 return unless (@ARGV);
1229
1230 require Getopt::Long;
1231 Getopt::Long::GetOptions(
1232 'fastcgi|fcgi|f' => \&configure_as_fcgi,
1233 'nproc|n=i' => sub {
1234 my ($arg, $val) = @_;
1235 return unless eval { require FCGI::ProcManager; 1; };
1236 my $proc_manager = FCGI::ProcManager->new({
1237 n_processes => $val,
1238 });
1239 our $pre_listen_hook = sub { $proc_manager->pm_manage() };
1240 our $pre_dispatch_hook = sub { $proc_manager->pm_pre_dispatch() };
1241 our $post_dispatch_hook = sub { $proc_manager->pm_post_dispatch() };
1242 },
1243 );
1244 }
1245
1246 sub run {
1247 evaluate_argv();
1248
1249 $first_request = 1;
1250 $pre_listen_hook->()
1251 if $pre_listen_hook;
1252
1253 REQUEST:
1254 while ($cgi = $CGI->new()) {
1255 $pre_dispatch_hook->()
1256 if $pre_dispatch_hook;
1257
1258 run_request();
1259
1260 $post_dispatch_hook->()
1261 if $post_dispatch_hook;
1262 $first_request = 0;
1263
1264 last REQUEST if ($is_last_request->());
1265 }
1266
1267 DONE_GITWEB:
1268 1;
1269 }
1270
1271 run();
1272
1273 if (defined caller) {
1274 # wrapped in a subroutine processing requests,
1275 # e.g. mod_perl with ModPerl::Registry, or PSGI with Plack::App::WrapCGI
1276 return;
1277 } else {
1278 # pure CGI script, serving single request
1279 exit;
1280 }
1281
1282 ## ======================================================================
1283 ## action links
1284
1285 # possible values of extra options
1286 # -full => 0|1 - use absolute/full URL ($my_uri/$my_url as base)
1287 # -replay => 1 - start from a current view (replay with modifications)
1288 # -path_info => 0|1 - don't use/use path_info URL (if possible)
1289 # -anchor => ANCHOR - add #ANCHOR to end of URL, implies -replay if used alone
1290 sub href {
1291 my %params = @_;
1292 # default is to use -absolute url() i.e. $my_uri
1293 my $href = $params{-full} ? $my_url : $my_uri;
1294
1295 # implicit -replay, must be first of implicit params
1296 $params{-replay} = 1 if (keys %params == 1 && $params{-anchor});
1297
1298 $params{'project'} = $project unless exists $params{'project'};
1299
1300 if ($params{-replay}) {
1301 while (my ($name, $symbol) = each %cgi_param_mapping) {
1302 if (!exists $params{$name}) {
1303 $params{$name} = $input_params{$name};
1304 }
1305 }
1306 }
1307
1308 my $use_pathinfo = gitweb_check_feature('pathinfo');
1309 if (defined $params{'project'} &&
1310 (exists $params{-path_info} ? $params{-path_info} : $use_pathinfo)) {
1311 # try to put as many parameters as possible in PATH_INFO:
1312 # - project name
1313 # - action
1314 # - hash_parent or hash_parent_base:/file_parent
1315 # - hash or hash_base:/filename
1316 # - the snapshot_format as an appropriate suffix
1317
1318 # When the script is the root DirectoryIndex for the domain,
1319 # $href here would be something like http://gitweb.example.com/
1320 # Thus, we strip any trailing / from $href, to spare us double
1321 # slashes in the final URL
1322 $href =~ s,/$,,;
1323
1324 # Then add the project name, if present
1325 $href .= "/".esc_path_info($params{'project'});
1326 delete $params{'project'};
1327
1328 # since we destructively absorb parameters, we keep this
1329 # boolean that remembers if we're handling a snapshot
1330 my $is_snapshot = $params{'action'} eq 'snapshot';
1331
1332 # Summary just uses the project path URL, any other action is
1333 # added to the URL
1334 if (defined $params{'action'}) {
1335 $href .= "/".esc_path_info($params{'action'})
1336 unless $params{'action'} eq 'summary';
1337 delete $params{'action'};
1338 }
1339
1340 # Next, we put hash_parent_base:/file_parent..hash_base:/file_name,
1341 # stripping nonexistent or useless pieces
1342 $href .= "/" if ($params{'hash_base'} || $params{'hash_parent_base'}
1343 || $params{'hash_parent'} || $params{'hash'});
1344 if (defined $params{'hash_base'}) {
1345 if (defined $params{'hash_parent_base'}) {
1346 $href .= esc_path_info($params{'hash_parent_base'});
1347 # skip the file_parent if it's the same as the file_name
1348 if (defined $params{'file_parent'}) {
1349 if (defined $params{'file_name'} && $params{'file_parent'} eq $params{'file_name'}) {
1350 delete $params{'file_parent'};
1351 } elsif ($params{'file_parent'} !~ /\.\./) {
1352 $href .= ":/".esc_path_info($params{'file_parent'});
1353 delete $params{'file_parent'};
1354 }
1355 }
1356 $href .= "..";
1357 delete $params{'hash_parent'};
1358 delete $params{'hash_parent_base'};
1359 } elsif (defined $params{'hash_parent'}) {
1360 $href .= esc_path_info($params{'hash_parent'}). "..";
1361 delete $params{'hash_parent'};
1362 }
1363
1364 $href .= esc_path_info($params{'hash_base'});
1365 if (defined $params{'file_name'} && $params{'file_name'} !~ /\.\./) {
1366 $href .= ":/".esc_path_info($params{'file_name'});
1367 delete $params{'file_name'};
1368 }
1369 delete $params{'hash'};
1370 delete $params{'hash_base'};
1371 } elsif (defined $params{'hash'}) {
1372 $href .= esc_path_info($params{'hash'});
1373 delete $params{'hash'};
1374 }
1375
1376 # If the action was a snapshot, we can absorb the
1377 # snapshot_format parameter too
1378 if ($is_snapshot) {
1379 my $fmt = $params{'snapshot_format'};
1380 # snapshot_format should always be defined when href()
1381 # is called, but just in case some code forgets, we
1382 # fall back to the default
1383 $fmt ||= $snapshot_fmts[0];
1384 $href .= $known_snapshot_formats{$fmt}{'suffix'};
1385 delete $params{'snapshot_format'};
1386 }
1387 }
1388
1389 # now encode the parameters explicitly
1390 my @result = ();
1391 for (my $i = 0; $i < @cgi_param_mapping; $i += 2) {
1392 my ($name, $symbol) = ($cgi_param_mapping[$i], $cgi_param_mapping[$i+1]);
1393 if (defined $params{$name}) {
1394 if (ref($params{$name}) eq "ARRAY") {
1395 foreach my $par (@{$params{$name}}) {
1396 push @result, $symbol . "=" . esc_param($par);
1397 }
1398 } else {
1399 push @result, $symbol . "=" . esc_param($params{$name});
1400 }
1401 }
1402 }
1403 $href .= "?" . join(';', @result) if scalar @result;
1404
1405 # final transformation: trailing spaces must be escaped (URI-encoded)
1406 $href =~ s/(\s+)$/CGI::escape($1)/e;
1407
1408 if ($params{-anchor}) {
1409 $href .= "#".esc_param($params{-anchor});
1410 }
1411
1412 return $href;
1413 }
1414
1415
1416 ## ======================================================================
1417 ## validation, quoting/unquoting and escaping
1418
1419 sub validate_action {
1420 my $input = shift || return undef;
1421 return undef unless exists $actions{$input};
1422 return $input;
1423 }
1424
1425 sub validate_project {
1426 my $input = shift || return undef;
1427 if (!validate_pathname($input) ||
1428 !(-d "$projectroot/$input") ||
1429 !check_export_ok("$projectroot/$input") ||
1430 ($strict_export && !project_in_list($input))) {
1431 return undef;
1432 } else {
1433 return $input;
1434 }
1435 }
1436
1437 sub validate_pathname {
1438 my $input = shift || return undef;
1439
1440 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
1441 # at the beginning, at the end, and between slashes.
1442 # also this catches doubled slashes
1443 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
1444 return undef;
1445 }
1446 # no null characters
1447 if ($input =~ m!\0!) {
1448 return undef;
1449 }
1450 return $input;
1451 }
1452
1453 sub validate_refname {
1454 my $input = shift || return undef;
1455
1456 # textual hashes are O.K.
1457 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
1458 return $input;
1459 }
1460 # it must be correct pathname
1461 $input = validate_pathname($input)
1462 or return undef;
1463 # restrictions on ref name according to git-check-ref-format
1464 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
1465 return undef;
1466 }
1467 return $input;
1468 }
1469
1470 # decode sequences of octets in utf8 into Perl's internal form,
1471 # which is utf-8 with utf8 flag set if needed. gitweb writes out
1472 # in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
1473 sub to_utf8 {
1474 my $str = shift;
1475 return undef unless defined $str;
1476
1477 if (utf8::is_utf8($str) || utf8::decode($str)) {
1478 return $str;
1479 } else {
1480 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
1481 }
1482 }
1483
1484 # quote unsafe chars, but keep the slash, even when it's not
1485 # correct, but quoted slashes look too horrible in bookmarks
1486 sub esc_param {
1487 my $str = shift;
1488 return undef unless defined $str;
1489 $str =~ s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;
1490 $str =~ s/ /\+/g;
1491 return $str;
1492 }
1493
1494 # the quoting rules for path_info fragment are slightly different
1495 sub esc_path_info {
1496 my $str = shift;
1497 return undef unless defined $str;
1498
1499 # path_info doesn't treat '+' as space (specially), but '?' must be escaped
1500 $str =~ s/([^A-Za-z0-9\-_.~();\/;:@&= +]+)/CGI::escape($1)/eg;
1501
1502 return $str;
1503 }
1504
1505 # quote unsafe chars in whole URL, so some characters cannot be quoted
1506 sub esc_url {
1507 my $str = shift;
1508 return undef unless defined $str;
1509 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&= ]+)/CGI::escape($1)/eg;
1510 $str =~ s/ /\+/g;
1511 return $str;
1512 }
1513
1514 # quote unsafe characters in HTML attributes
1515 sub esc_attr {
1516
1517 # for XHTML conformance escaping '"' to '&quot;' is not enough
1518 return esc_html(@_);
1519 }
1520
1521 # replace invalid utf8 character with SUBSTITUTION sequence
1522 sub esc_html {
1523 my $str = shift;
1524 my %opts = @_;
1525
1526 return undef unless defined $str;
1527
1528 $str = to_utf8($str);
1529 $str = $cgi->escapeHTML($str);
1530 if ($opts{'-nbsp'}) {
1531 $str =~ s/ /&nbsp;/g;
1532 }
1533 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
1534 return $str;
1535 }
1536
1537 # quote control characters and escape filename to HTML
1538 sub esc_path {
1539 my $str = shift;
1540 my %opts = @_;
1541
1542 return undef unless defined $str;
1543
1544 $str = to_utf8($str);
1545 $str = $cgi->escapeHTML($str);
1546 if ($opts{'-nbsp'}) {
1547 $str =~ s/ /&nbsp;/g;
1548 }
1549 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
1550 return $str;
1551 }
1552
1553 # Sanitize for use in XHTML + application/xml+xhtm (valid XML 1.0)
1554 sub sanitize {
1555 my $str = shift;
1556
1557 return undef unless defined $str;
1558
1559 $str = to_utf8($str);
1560 $str =~ s|([[:cntrl:]])|(index("\t\n\r", $1) != -1 ? $1 : quot_cec($1))|eg;
1561 return $str;
1562 }
1563
1564 # Make control characters "printable", using character escape codes (CEC)
1565 sub quot_cec {
1566 my $cntrl = shift;
1567 my %opts = @_;
1568 my %es = ( # character escape codes, aka escape sequences
1569 "\t" => '\t', # tab (HT)
1570 "\n" => '\n', # line feed (LF)
1571 "\r" => '\r', # carrige return (CR)
1572 "\f" => '\f', # form feed (FF)
1573 "\b" => '\b', # backspace (BS)
1574 "\a" => '\a', # alarm (bell) (BEL)
1575 "\e" => '\e', # escape (ESC)
1576 "\013" => '\v', # vertical tab (VT)
1577 "\000" => '\0', # nul character (NUL)
1578 );
1579 my $chr = ( (exists $es{$cntrl})
1580 ? $es{$cntrl}
1581 : sprintf('\%2x', ord($cntrl)) );
1582 if ($opts{-nohtml}) {
1583 return $chr;
1584 } else {
1585 return "<span class=\"cntrl\">$chr</span>";
1586 }
1587 }
1588
1589 # Alternatively use unicode control pictures codepoints,
1590 # Unicode "printable representation" (PR)
1591 sub quot_upr {
1592 my $cntrl = shift;
1593 my %opts = @_;
1594
1595 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
1596 if ($opts{-nohtml}) {
1597 return $chr;
1598 } else {
1599 return "<span class=\"cntrl\">$chr</span>";
1600 }
1601 }
1602
1603 # git may return quoted and escaped filenames
1604 sub unquote {
1605 my $str = shift;
1606
1607 sub unq {
1608 my $seq = shift;
1609 my %es = ( # character escape codes, aka escape sequences
1610 't' => "\t", # tab (HT, TAB)
1611 'n' => "\n", # newline (NL)
1612 'r' => "\r", # return (CR)
1613 'f' => "\f", # form feed (FF)
1614 'b' => "\b", # backspace (BS)
1615 'a' => "\a", # alarm (bell) (BEL)
1616 'e' => "\e", # escape (ESC)
1617 'v' => "\013", # vertical tab (VT)
1618 );
1619
1620 if ($seq =~ m/^[0-7]{1,3}$/) {
1621 # octal char sequence
1622 return chr(oct($seq));
1623 } elsif (exists $es{$seq}) {
1624 # C escape sequence, aka character escape code
1625 return $es{$seq};
1626 }
1627 # quoted ordinary character
1628 return $seq;
1629 }
1630
1631 if ($str =~ m/^"(.*)"$/) {
1632 # needs unquoting
1633 $str = $1;
1634 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
1635 }
1636 return $str;
1637 }
1638
1639 # escape tabs (convert tabs to spaces)
1640 sub untabify {
1641 my $line = shift;
1642
1643 while ((my $pos = index($line, "\t")) != -1) {
1644 if (my $count = (8 - ($pos % 8))) {
1645 my $spaces = ' ' x $count;
1646 $line =~ s/\t/$spaces/;
1647 }
1648 }
1649
1650 return $line;
1651 }
1652
1653 sub project_in_list {
1654 my $project = shift;
1655 my @list = git_get_projects_list();
1656 return @list && scalar(grep { $_->{'path'} eq $project } @list);
1657 }
1658
1659 ## ----------------------------------------------------------------------
1660 ## HTML aware string manipulation
1661
1662 # Try to chop given string on a word boundary between position
1663 # $len and $len+$add_len. If there is no word boundary there,
1664 # chop at $len+$add_len. Do not chop if chopped part plus ellipsis
1665 # (marking chopped part) would be longer than given string.
1666 sub chop_str {
1667 my $str = shift;
1668 my $len = shift;
1669 my $add_len = shift || 10;
1670 my $where = shift || 'right'; # 'left' | 'center' | 'right'
1671
1672 # Make sure perl knows it is utf8 encoded so we don't
1673 # cut in the middle of a utf8 multibyte char.
1674 $str = to_utf8($str);
1675
1676 # allow only $len chars, but don't cut a word if it would fit in $add_len
1677 # if it doesn't fit, cut it if it's still longer than the dots we would add
1678 # remove chopped character entities entirely
1679
1680 # when chopping in the middle, distribute $len into left and right part
1681 # return early if chopping wouldn't make string shorter
1682 if ($where eq 'center') {
1683 return $str if ($len + 5 >= length($str)); # filler is length 5
1684 $len = int($len/2);
1685 } else {
1686 return $str if ($len + 4 >= length($str)); # filler is length 4
1687 }
1688
1689 # regexps: ending and beginning with word part up to $add_len
1690 my $endre = qr/.{$len}\w{0,$add_len}/;
1691 my $begre = qr/\w{0,$add_len}.{$len}/;
1692
1693 if ($where eq 'left') {
1694 $str =~ m/^(.*?)($begre)$/;
1695 my ($lead, $body) = ($1, $2);
1696 if (length($lead) > 4) {
1697 $lead = " ...";
1698 }
1699 return "$lead$body";
1700
1701 } elsif ($where eq 'center') {
1702 $str =~ m/^($endre)(.*)$/;
1703 my ($left, $str) = ($1, $2);
1704 $str =~ m/^(.*?)($begre)$/;
1705 my ($mid, $right) = ($1, $2);
1706 if (length($mid) > 5) {
1707 $mid = " ... ";
1708 }
1709 return "$left$mid$right";
1710
1711 } else {
1712 $str =~ m/^($endre)(.*)$/;
1713 my $body = $1;
1714 my $tail = $2;
1715 if (length($tail) > 4) {
1716 $tail = "... ";
1717 }
1718 return "$body$tail";
1719 }
1720 }
1721
1722 # takes the same arguments as chop_str, but also wraps a <span> around the
1723 # result with a title attribute if it does get chopped. Additionally, the
1724 # string is HTML-escaped.
1725 sub chop_and_escape_str {
1726 my ($str) = @_;
1727
1728 my $chopped = chop_str(@_);
1729 $str = to_utf8($str);
1730 if ($chopped eq $str) {
1731 return esc_html($chopped);
1732 } else {
1733 $str =~ s/[[:cntrl:]]/?/g;
1734 return $cgi->span({-title=>$str}, esc_html($chopped));
1735 }
1736 }
1737
1738 # Highlight selected fragments of string, using given CSS class,
1739 # and escape HTML. It is assumed that fragments do not overlap.
1740 # Regions are passed as list of pairs (array references).
1741 #
1742 # Example: esc_html_hl_regions("foobar", "mark", [ 0, 3 ]) returns
1743 # '<span class="mark">foo</span>bar'
1744 sub esc_html_hl_regions {
1745 my ($str, $css_class, @sel) = @_;
1746 my %opts = grep { ref($_) ne 'ARRAY' } @sel;
1747 @sel = grep { ref($_) eq 'ARRAY' } @sel;
1748 return esc_html($str, %opts) unless @sel;
1749
1750 my $out = '';
1751 my $pos = 0;
1752
1753 for my $s (@sel) {
1754 my ($begin, $end) = @$s;
1755
1756 # Don't create empty <span> elements.
1757 next if $end <= $begin;
1758
1759 my $escaped = esc_html(substr($str, $begin, $end - $begin),
1760 %opts);
1761
1762 $out .= esc_html(substr($str, $pos, $begin - $pos), %opts)
1763 if ($begin - $pos > 0);
1764 $out .= $cgi->span({-class => $css_class}, $escaped);
1765
1766 $pos = $end;
1767 }
1768 $out .= esc_html(substr($str, $pos), %opts)
1769 if ($pos < length($str));
1770
1771 return $out;
1772 }
1773
1774 # return positions of beginning and end of each match
1775 sub matchpos_list {
1776 my ($str, $regexp) = @_;
1777 return unless (defined $str && defined $regexp);
1778
1779 my @matches;
1780 while ($str =~ /$regexp/g) {
1781 push @matches, [$-[0], $+[0]];
1782 }
1783 return @matches;
1784 }
1785
1786 # highlight match (if any), and escape HTML
1787 sub esc_html_match_hl {
1788 my ($str, $regexp) = @_;
1789 return esc_html($str) unless defined $regexp;
1790
1791 my @matches = matchpos_list($str, $regexp);
1792 return esc_html($str) unless @matches;
1793
1794 return esc_html_hl_regions($str, 'match', @matches);
1795 }
1796
1797
1798 # highlight match (if any) of shortened string, and escape HTML
1799 sub esc_html_match_hl_chopped {
1800 my ($str, $chopped, $regexp) = @_;
1801 return esc_html_match_hl($str, $regexp) unless defined $chopped;
1802
1803 my @matches = matchpos_list($str, $regexp);
1804 return esc_html($chopped) unless @matches;
1805
1806 # filter matches so that we mark chopped string
1807 my $tail = "... "; # see chop_str
1808 unless ($chopped =~ s/\Q$tail\E$//) {
1809 $tail = '';
1810 }
1811 my $chop_len = length($chopped);
1812 my $tail_len = length($tail);
1813 my @filtered;
1814
1815 for my $m (@matches) {
1816 if ($m->[0] > $chop_len) {
1817 push @filtered, [ $chop_len, $chop_len + $tail_len ] if ($tail_len > 0);
1818 last;
1819 } elsif ($m->[1] > $chop_len) {
1820 push @filtered, [ $m->[0], $chop_len + $tail_len ];
1821 last;
1822 }
1823 push @filtered, $m;
1824 }
1825
1826 return esc_html_hl_regions($chopped . $tail, 'match', @filtered);
1827 }
1828
1829 ## ----------------------------------------------------------------------
1830 ## functions returning short strings
1831
1832 # CSS class for given age value (in seconds)
1833 sub age_class {
1834 my $age = shift;
1835
1836 if (!defined $age) {
1837 return "noage";
1838 } elsif ($age < 60*60*2) {
1839 return "age0";
1840 } elsif ($age < 60*60*24*2) {
1841 return "age1";
1842 } else {
1843 return "age2";
1844 }
1845 }
1846
1847 # convert age in seconds to "nn units ago" string
1848 sub age_string {
1849 my $age = shift;
1850 my $age_str;
1851
1852 if ($age > 60*60*24*365*2) {
1853 $age_str = (int $age/60/60/24/365);
1854 $age_str .= " years ago";
1855 } elsif ($age > 60*60*24*(365/12)*2) {
1856 $age_str = int $age/60/60/24/(365/12);
1857 $age_str .= " months ago";
1858 } elsif ($age > 60*60*24*7*2) {
1859 $age_str = int $age/60/60/24/7;
1860 $age_str .= " weeks ago";
1861 } elsif ($age > 60*60*24*2) {
1862 $age_str = int $age/60/60/24;
1863 $age_str .= " days ago";
1864 } elsif ($age > 60*60*2) {
1865 $age_str = int $age/60/60;
1866 $age_str .= " hours ago";
1867 } elsif ($age > 60*2) {
1868 $age_str = int $age/60;
1869 $age_str .= " min ago";
1870 } elsif ($age > 2) {
1871 $age_str = int $age;
1872 $age_str .= " sec ago";
1873 } else {
1874 $age_str .= " right now";
1875 }
1876 return $age_str;
1877 }
1878
1879 use constant {
1880 S_IFINVALID => 0030000,
1881 S_IFGITLINK => 0160000,
1882 };
1883
1884 # submodule/subproject, a commit object reference
1885 sub S_ISGITLINK {
1886 my $mode = shift;
1887
1888 return (($mode & S_IFMT) == S_IFGITLINK)
1889 }
1890
1891 # convert file mode in octal to symbolic file mode string
1892 sub mode_str {
1893 my $mode = oct shift;
1894
1895 if (S_ISGITLINK($mode)) {
1896 return 'm---------';
1897 } elsif (S_ISDIR($mode & S_IFMT)) {
1898 return 'drwxr-xr-x';
1899 } elsif (S_ISLNK($mode)) {
1900 return 'lrwxrwxrwx';
1901 } elsif (S_ISREG($mode)) {
1902 # git cares only about the executable bit
1903 if ($mode & S_IXUSR) {
1904 return '-rwxr-xr-x';
1905 } else {
1906 return '-rw-r--r--';
1907 };
1908 } else {
1909 return '----------';
1910 }
1911 }
1912
1913 # convert file mode in octal to file type string
1914 sub file_type {
1915 my $mode = shift;
1916
1917 if ($mode !~ m/^[0-7]+$/) {
1918 return $mode;
1919 } else {
1920 $mode = oct $mode;
1921 }
1922
1923 if (S_ISGITLINK($mode)) {
1924 return "submodule";
1925 } elsif (S_ISDIR($mode & S_IFMT)) {
1926 return "directory";
1927 } elsif (S_ISLNK($mode)) {
1928 return "symlink";
1929 } elsif (S_ISREG($mode)) {
1930 return "file";
1931 } else {
1932 return "unknown";
1933 }
1934 }
1935
1936 # convert file mode in octal to file type description string
1937 sub file_type_long {
1938 my $mode = shift;
1939
1940 if ($mode !~ m/^[0-7]+$/) {
1941 return $mode;
1942 } else {
1943 $mode = oct $mode;
1944 }
1945
1946 if (S_ISGITLINK($mode)) {
1947 return "submodule";
1948 } elsif (S_ISDIR($mode & S_IFMT)) {
1949 return "directory";
1950 } elsif (S_ISLNK($mode)) {
1951 return "symlink";
1952 } elsif (S_ISREG($mode)) {
1953 if ($mode & S_IXUSR) {
1954 return "executable";
1955 } else {
1956 return "file";
1957 };
1958 } else {
1959 return "unknown";
1960 }
1961 }
1962
1963
1964 ## ----------------------------------------------------------------------
1965 ## functions returning short HTML fragments, or transforming HTML fragments
1966 ## which don't belong to other sections
1967
1968 # format line of commit message.
1969 sub format_log_line_html {
1970 my $line = shift;
1971
1972 $line = esc_html($line, -nbsp=>1);
1973 $line =~ s{\b([0-9a-fA-F]{8,40})\b}{
1974 $cgi->a({-href => href(action=>"object", hash=>$1),
1975 -class => "text"}, $1);
1976 }eg;
1977
1978 return $line;
1979 }
1980
1981 # format marker of refs pointing to given object
1982
1983 # the destination action is chosen based on object type and current context:
1984 # - for annotated tags, we choose the tag view unless it's the current view
1985 # already, in which case we go to shortlog view
1986 # - for other refs, we keep the current view if we're in history, shortlog or
1987 # log view, and select shortlog otherwise
1988 sub format_ref_marker {
1989 my ($refs, $id) = @_;
1990 my $markers = '';
1991
1992 if (defined $refs->{$id}) {
1993 foreach my $ref (@{$refs->{$id}}) {
1994 # this code exploits the fact that non-lightweight tags are the
1995 # only indirect objects, and that they are the only objects for which
1996 # we want to use tag instead of shortlog as action
1997 my ($type, $name) = qw();
1998 my $indirect = ($ref =~ s/\^\{\}$//);
1999 # e.g. tags/v2.6.11 or heads/next
2000 if ($ref =~ m!^(.*?)s?/(.*)$!) {
2001 $type = $1;
2002 $name = $2;
2003 } else {
2004 $type = "ref";
2005 $name = $ref;
2006 }
2007
2008 my $class = $type;
2009 $class .= " indirect" if $indirect;
2010
2011 my $dest_action = "shortlog";
2012
2013 if ($indirect) {
2014 $dest_action = "tag" unless $action eq "tag";
2015 } elsif ($action =~ /^(history|(short)?log)$/) {
2016 $dest_action = $action;
2017 }
2018
2019 my $dest = "";
2020 $dest .= "refs/" unless $ref =~ m!^refs/!;
2021 $dest .= $ref;
2022
2023 my $link = $cgi->a({
2024 -href => href(
2025 action=>$dest_action,
2026 hash=>$dest
2027 )}, $name);
2028
2029 $markers .= " <span class=\"".esc_attr($class)."\" title=\"".esc_attr($ref)."\">" .
2030 $link . "</span>";
2031 }
2032 }
2033
2034 if ($markers) {
2035 return ' <span class="refs">'. $markers . '</span>';
2036 } else {
2037 return "";
2038 }
2039 }
2040
2041 # format, perhaps shortened and with markers, title line
2042 sub format_subject_html {
2043 my ($long, $short, $href, $extra) = @_;
2044 $extra = '' unless defined($extra);
2045
2046 if (length($short) < length($long)) {
2047 $long =~ s/[[:cntrl:]]/?/g;
2048 return $cgi->a({-href => $href, -class => "list subject",
2049 -title => to_utf8($long)},
2050 esc_html($short)) . $extra;
2051 } else {
2052 return $cgi->a({-href => $href, -class => "list subject"},
2053 esc_html($long)) . $extra;
2054 }
2055 }
2056
2057 # Rather than recomputing the url for an email multiple times, we cache it
2058 # after the first hit. This gives a visible benefit in views where the avatar
2059 # for the same email is used repeatedly (e.g. shortlog).
2060 # The cache is shared by all avatar engines (currently gravatar only), which
2061 # are free to use it as preferred. Since only one avatar engine is used for any
2062 # given page, there's no risk for cache conflicts.
2063 our %avatar_cache = ();
2064
2065 # Compute the picon url for a given email, by using the picon search service over at
2066 # http://www.cs.indiana.edu/picons/search.html
2067 sub picon_url {
2068 my $email = lc shift;
2069 if (!$avatar_cache{$email}) {
2070 my ($user, $domain) = split('@', $email);
2071 $avatar_cache{$email} =
2072 "//www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/" .
2073 "$domain/$user/" .
2074 "users+domains+unknown/up/single";
2075 }
2076 return $avatar_cache{$email};
2077 }
2078
2079 # Compute the gravatar url for a given email, if it's not in the cache already.
2080 # Gravatar stores only the part of the URL before the size, since that's the
2081 # one computationally more expensive. This also allows reuse of the cache for
2082 # different sizes (for this particular engine).
2083 sub gravatar_url {
2084 my $email = lc shift;
2085 my $size = shift;
2086 $avatar_cache{$email} ||=
2087 "//www.gravatar.com/avatar/" .
2088 Digest::MD5::md5_hex($email) . "?s=";
2089 return $avatar_cache{$email} . $size;
2090 }
2091
2092 # Insert an avatar for the given $email at the given $size if the feature
2093 # is enabled.
2094 sub git_get_avatar {
2095 my ($email, %opts) = @_;
2096 my $pre_white = ($opts{-pad_before} ? "&nbsp;" : "");
2097 my $post_white = ($opts{-pad_after} ? "&nbsp;" : "");
2098 $opts{-size} ||= 'default';
2099 my $size = $avatar_size{$opts{-size}} || $avatar_size{'default'};
2100 my $url = "";
2101 if ($git_avatar eq 'gravatar') {
2102 $url = gravatar_url($email, $size);
2103 } elsif ($git_avatar eq 'picon') {
2104 $url = picon_url($email);
2105 }
2106 # Other providers can be added by extending the if chain, defining $url
2107 # as needed. If no variant puts something in $url, we assume avatars
2108 # are completely disabled/unavailable.
2109 if ($url) {
2110 return $pre_white .
2111 "<img width=\"$size\" " .
2112 "class=\"avatar\" " .
2113 "src=\"".esc_url($url)."\" " .
2114 "alt=\"\" " .
2115 "/>" . $post_white;
2116 } else {
2117 return "";
2118 }
2119 }
2120
2121 sub format_search_author {
2122 my ($author, $searchtype, $displaytext) = @_;
2123 my $have_search = gitweb_check_feature('search');
2124
2125 if ($have_search) {
2126 my $performed = "";
2127 if ($searchtype eq 'author') {
2128 $performed = "authored";
2129 } elsif ($searchtype eq 'committer') {
2130 $performed = "committed";
2131 }
2132
2133 return $cgi->a({-href => href(action=>"search", hash=>$hash,
2134 searchtext=>$author,
2135 searchtype=>$searchtype), class=>"list",
2136 title=>"Search for commits $performed by $author"},
2137 $displaytext);
2138
2139 } else {
2140 return $displaytext;
2141 }
2142 }
2143
2144 # format the author name of the given commit with the given tag
2145 # the author name is chopped and escaped according to the other
2146 # optional parameters (see chop_str).
2147 sub format_author_html {
2148 my $tag = shift;
2149 my $co = shift;
2150 my $author = chop_and_escape_str($co->{'author_name'}, @_);
2151 return "<$tag class=\"author\">" .
2152 format_search_author($co->{'author_name'}, "author",
2153 git_get_avatar($co->{'author_email'}, -pad_after => 1) .
2154 $author) .
2155 "</$tag>";
2156 }
2157
2158 # format git diff header line, i.e. "diff --(git|combined|cc) ..."
2159 sub format_git_diff_header_line {
2160 my $line = shift;
2161 my $diffinfo = shift;
2162 my ($from, $to) = @_;
2163
2164 if ($diffinfo->{'nparents'}) {
2165 # combined diff
2166 $line =~ s!^(diff (.*?) )"?.*$!$1!;
2167 if ($to->{'href'}) {
2168 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
2169 esc_path($to->{'file'}));
2170 } else { # file was deleted (no href)
2171 $line .= esc_path($to->{'file'});
2172 }
2173 } else {
2174 # "ordinary" diff
2175 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
2176 if ($from->{'href'}) {
2177 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
2178 'a/' . esc_path($from->{'file'}));
2179 } else { # file was added (no href)
2180 $line .= 'a/' . esc_path($from->{'file'});
2181 }
2182 $line .= ' ';
2183 if ($to->{'href'}) {
2184 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
2185 'b/' . esc_path($to->{'file'}));
2186 } else { # file was deleted
2187 $line .= 'b/' . esc_path($to->{'file'});
2188 }
2189 }
2190
2191 return "<div class=\"diff header\">$line</div>\n";
2192 }
2193
2194 # format extended diff header line, before patch itself
2195 sub format_extended_diff_header_line {
2196 my $line = shift;
2197 my $diffinfo = shift;
2198 my ($from, $to) = @_;
2199
2200 # match <path>
2201 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
2202 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
2203 esc_path($from->{'file'}));
2204 }
2205 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
2206 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
2207 esc_path($to->{'file'}));
2208 }
2209 # match single <mode>
2210 if ($line =~ m/\s(\d{6})$/) {
2211 $line .= '<span class="info"> (' .
2212 file_type_long($1) .
2213 ')</span>';
2214 }
2215 # match <hash>
2216 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
2217 # can match only for combined diff
2218 $line = 'index ';
2219 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2220 if ($from->{'href'}[$i]) {
2221 $line .= $cgi->a({-href=>$from->{'href'}[$i],
2222 -class=>"hash"},
2223 substr($diffinfo->{'from_id'}[$i],0,7));
2224 } else {
2225 $line .= '0' x 7;
2226 }
2227 # separator
2228 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
2229 }
2230 $line .= '..';
2231 if ($to->{'href'}) {
2232 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
2233 substr($diffinfo->{'to_id'},0,7));
2234 } else {
2235 $line .= '0' x 7;
2236 }
2237
2238 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
2239 # can match only for ordinary diff
2240 my ($from_link, $to_link);
2241 if ($from->{'href'}) {
2242 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
2243 substr($diffinfo->{'from_id'},0,7));
2244 } else {
2245 $from_link = '0' x 7;
2246 }
2247 if ($to->{'href'}) {
2248 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
2249 substr($diffinfo->{'to_id'},0,7));
2250 } else {
2251 $to_link = '0' x 7;
2252 }
2253 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
2254 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
2255 }
2256
2257 return $line . "<br/>\n";
2258 }
2259
2260 # format from-file/to-file diff header
2261 sub format_diff_from_to_header {
2262 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
2263 my $line;
2264 my $result = '';
2265
2266 $line = $from_line;
2267 #assert($line =~ m/^---/) if DEBUG;
2268 # no extra formatting for "^--- /dev/null"
2269 if (! $diffinfo->{'nparents'}) {
2270 # ordinary (single parent) diff
2271 if ($line =~ m!^--- "?a/!) {
2272 if ($from->{'href'}) {
2273 $line = '--- a/' .
2274 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
2275 esc_path($from->{'file'}));
2276 } else {
2277 $line = '--- a/' .
2278 esc_path($from->{'file'});
2279 }
2280 }
2281 $result .= qq!<div class="diff from_file">$line</div>\n!;
2282
2283 } else {
2284 # combined diff (merge commit)
2285 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2286 if ($from->{'href'}[$i]) {
2287 $line = '--- ' .
2288 $cgi->a({-href=>href(action=>"blobdiff",
2289 hash_parent=>$diffinfo->{'from_id'}[$i],
2290 hash_parent_base=>$parents[$i],
2291 file_parent=>$from->{'file'}[$i],
2292 hash=>$diffinfo->{'to_id'},
2293 hash_base=>$hash,
2294 file_name=>$to->{'file'}),
2295 -class=>"path",
2296 -title=>"diff" . ($i+1)},
2297 $i+1) .
2298 '/' .
2299 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
2300 esc_path($from->{'file'}[$i]));
2301 } else {
2302 $line = '--- /dev/null';
2303 }
2304 $result .= qq!<div class="diff from_file">$line</div>\n!;
2305 }
2306 }
2307
2308 $line = $to_line;
2309 #assert($line =~ m/^\+\+\+/) if DEBUG;
2310 # no extra formatting for "^+++ /dev/null"
2311 if ($line =~ m!^\+\+\+ "?b/!) {
2312 if ($to->{'href'}) {
2313 $line = '+++ b/' .
2314 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
2315 esc_path($to->{'file'}));
2316 } else {
2317 $line = '+++ b/' .
2318 esc_path($to->{'file'});
2319 }
2320 }
2321 $result .= qq!<div class="diff to_file">$line</div>\n!;
2322
2323 return $result;
2324 }
2325
2326 # create note for patch simplified by combined diff
2327 sub format_diff_cc_simplified {
2328 my ($diffinfo, @parents) = @_;
2329 my $result = '';
2330
2331 $result .= "<div class=\"diff header\">" .
2332 "diff --cc ";
2333 if (!is_deleted($diffinfo)) {
2334 $result .= $cgi->a({-href => href(action=>"blob",
2335 hash_base=>$hash,
2336 hash=>$diffinfo->{'to_id'},
2337 file_name=>$diffinfo->{'to_file'}),
2338 -class => "path"},
2339 esc_path($diffinfo->{'to_file'}));
2340 } else {
2341 $result .= esc_path($diffinfo->{'to_file'});
2342 }
2343 $result .= "</div>\n" . # class="diff header"
2344 "<div class=\"diff nodifferences\">" .
2345 "Simple merge" .
2346 "</div>\n"; # class="diff nodifferences"
2347
2348 return $result;
2349 }
2350
2351 sub diff_line_class {
2352 my ($line, $from, $to) = @_;
2353
2354 # ordinary diff
2355 my $num_sign = 1;
2356 # combined diff
2357 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
2358 $num_sign = scalar @{$from->{'href'}};
2359 }
2360
2361 my @diff_line_classifier = (
2362 { regexp => qr/^\@\@{$num_sign} /, class => "chunk_header"},
2363 { regexp => qr/^\\/, class => "incomplete" },
2364 { regexp => qr/^ {$num_sign}/, class => "ctx" },
2365 # classifier for context must come before classifier add/rem,
2366 # or we would have to use more complicated regexp, for example
2367 # qr/(?= {0,$m}\+)[+ ]{$num_sign}/, where $m = $num_sign - 1;
2368 { regexp => qr/^[+ ]{$num_sign}/, class => "add" },
2369 { regexp => qr/^[- ]{$num_sign}/, class => "rem" },
2370 );
2371 for my $clsfy (@diff_line_classifier) {
2372 return $clsfy->{'class'}
2373 if ($line =~ $clsfy->{'regexp'});
2374 }
2375
2376 # fallback
2377 return "";
2378 }
2379
2380 # assumes that $from and $to are defined and correctly filled,
2381 # and that $line holds a line of chunk header for unified diff
2382 sub format_unidiff_chunk_header {
2383 my ($line, $from, $to) = @_;
2384
2385 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
2386 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
2387
2388 $from_lines = 0 unless defined $from_lines;
2389 $to_lines = 0 unless defined $to_lines;
2390
2391 if ($from->{'href'}) {
2392 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
2393 -class=>"list"}, $from_text);
2394 }
2395 if ($to->{'href'}) {
2396 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
2397 -class=>"list"}, $to_text);
2398 }
2399 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
2400 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
2401 return $line;
2402 }
2403
2404 # assumes that $from and $to are defined and correctly filled,
2405 # and that $line holds a line of chunk header for combined diff
2406 sub format_cc_diff_chunk_header {
2407 my ($line, $from, $to) = @_;
2408
2409 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
2410 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
2411
2412 @from_text = split(' ', $ranges);
2413 for (my $i = 0; $i < @from_text; ++$i) {
2414 ($from_start[$i], $from_nlines[$i]) =
2415 (split(',', substr($from_text[$i], 1)), 0);
2416 }
2417
2418 $to_text = pop @from_text;
2419 $to_start = pop @from_start;
2420 $to_nlines = pop @from_nlines;
2421
2422 $line = "<span class=\"chunk_info\">$prefix ";
2423 for (my $i = 0; $i < @from_text; ++$i) {
2424 if ($from->{'href'}[$i]) {
2425 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
2426 -class=>"list"}, $from_text[$i]);
2427 } else {
2428 $line .= $from_text[$i];
2429 }
2430 $line .= " ";
2431 }
2432 if ($to->{'href'}) {
2433 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
2434 -class=>"list"}, $to_text);
2435 } else {
2436 $line .= $to_text;
2437 }
2438 $line .= " $prefix</span>" .
2439 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
2440 return $line;
2441 }
2442
2443 # process patch (diff) line (not to be used for diff headers),
2444 # returning HTML-formatted (but not wrapped) line.
2445 # If the line is passed as a reference, it is treated as HTML and not
2446 # esc_html()'ed.
2447 sub format_diff_line {
2448 my ($line, $diff_class, $from, $to) = @_;
2449
2450 if (ref($line)) {
2451 $line = $$line;
2452 } else {
2453 chomp $line;
2454 $line = untabify($line);
2455
2456 if ($from && $to && $line =~ m/^\@{2} /) {
2457 $line = format_unidiff_chunk_header($line, $from, $to);
2458 } elsif ($from && $to && $line =~ m/^\@{3}/) {
2459 $line = format_cc_diff_chunk_header($line, $from, $to);
2460 } else {
2461 $line = esc_html($line, -nbsp=>1);
2462 }
2463 }
2464
2465 my $diff_classes = "diff";
2466 $diff_classes .= " $diff_class" if ($diff_class);
2467 $line = "<div class=\"$diff_classes\">$line</div>\n";
2468
2469 return $line;
2470 }
2471
2472 # Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
2473 # linked. Pass the hash of the tree/commit to snapshot.
2474 sub format_snapshot_links {
2475 my ($hash) = @_;
2476 my $num_fmts = @snapshot_fmts;
2477 if ($num_fmts > 1) {
2478 # A parenthesized list of links bearing format names.
2479 # e.g. "snapshot (_tar.gz_ _zip_)"
2480 return "snapshot (" . join(' ', map
2481 $cgi->a({
2482 -href => href(
2483 action=>"snapshot",
2484 hash=>$hash,
2485 snapshot_format=>$_
2486 )
2487 }, $known_snapshot_formats{$_}{'display'})
2488 , @snapshot_fmts) . ")";
2489 } elsif ($num_fmts == 1) {
2490 # A single "snapshot" link whose tooltip bears the format name.
2491 # i.e. "_snapshot_"
2492 my ($fmt) = @snapshot_fmts;
2493 return
2494 $cgi->a({
2495 -href => href(
2496 action=>"snapshot",
2497 hash=>$hash,
2498 snapshot_format=>$fmt
2499 ),
2500 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
2501 }, "snapshot");
2502 } else { # $num_fmts == 0
2503 return undef;
2504 }
2505 }
2506
2507 ## ......................................................................
2508 ## functions returning values to be passed, perhaps after some
2509 ## transformation, to other functions; e.g. returning arguments to href()
2510
2511 # returns hash to be passed to href to generate gitweb URL
2512 # in -title key it returns description of link
2513 sub get_feed_info {
2514 my $format = shift || 'Atom';
2515 my %res = (action => lc($format));
2516
2517 # feed links are possible only for project views
2518 return unless (defined $project);
2519 # some views should link to OPML, or to generic project feed,
2520 # or don't have specific feed yet (so they should use generic)
2521 return if (!$action || $action =~ /^(?:tags|heads|forks|tag|search)$/x);
2522
2523 my $branch;
2524 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
2525 # from tag links; this also makes possible to detect branch links
2526 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
2527 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
2528 $branch = $1;
2529 }
2530 # find log type for feed description (title)
2531 my $type = 'log';
2532 if (defined $file_name) {
2533 $type = "history of $file_name";
2534 $type .= "/" if ($action eq 'tree');
2535 $type .= " on '$branch'" if (defined $branch);
2536 } else {
2537 $type = "log of $branch" if (defined $branch);
2538 }
2539
2540 $res{-title} = $type;
2541 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
2542 $res{'file_name'} = $file_name;
2543
2544 return %res;
2545 }
2546
2547 ## ----------------------------------------------------------------------
2548 ## git utility subroutines, invoking git commands
2549
2550 # returns path to the core git executable and the --git-dir parameter as list
2551 sub git_cmd {
2552 $number_of_git_cmds++;
2553 return $GIT, '--git-dir='.$git_dir;
2554 }
2555
2556 # quote the given arguments for passing them to the shell
2557 # quote_command("command", "arg 1", "arg with ' and ! characters")
2558 # => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
2559 # Try to avoid using this function wherever possible.
2560 sub quote_command {
2561 return join(' ',
2562 map { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ );
2563 }
2564
2565 # get HEAD ref of given project as hash
2566 sub git_get_head_hash {
2567 return git_get_full_hash(shift, 'HEAD');
2568 }
2569
2570 sub git_get_full_hash {
2571 return git_get_hash(@_);
2572 }
2573
2574 sub git_get_short_hash {
2575 return git_get_hash(@_, '--short=7');
2576 }
2577
2578 sub git_get_hash {
2579 my ($project, $hash, @options) = @_;
2580 my $o_git_dir = $git_dir;
2581 my $retval = undef;
2582 $git_dir = "$projectroot/$project";
2583 if (open my $fd, '-|', git_cmd(), 'rev-parse',
2584 '--verify', '-q', @options, $hash) {
2585 $retval = <$fd>;
2586 chomp $retval if defined $retval;
2587 close $fd;
2588 }
2589 if (defined $o_git_dir) {
2590 $git_dir = $o_git_dir;
2591 }
2592 return $retval;
2593 }
2594
2595 # get type of given object
2596 sub git_get_type {
2597 my $hash = shift;
2598
2599 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
2600 my $type = <$fd>;
2601 close $fd or return;
2602 chomp $type;
2603 return $type;
2604 }
2605
2606 # repository configuration
2607 our $config_file = '';
2608 our %config;
2609
2610 # store multiple values for single key as anonymous array reference
2611 # single values stored directly in the hash, not as [ <value> ]
2612 sub hash_set_multi {
2613 my ($hash, $key, $value) = @_;
2614
2615 if (!exists $hash->{$key}) {
2616 $hash->{$key} = $value;
2617 } elsif (!ref $hash->{$key}) {
2618 $hash->{$key} = [ $hash->{$key}, $value ];
2619 } else {
2620 push @{$hash->{$key}}, $value;
2621 }
2622 }
2623
2624 # return hash of git project configuration
2625 # optionally limited to some section, e.g. 'gitweb'
2626 sub git_parse_project_config {
2627 my $section_regexp = shift;
2628 my %config;
2629
2630 local $/ = "\0";
2631
2632 open my $fh, "-|", git_cmd(), "config", '-z', '-l',
2633 or return;
2634
2635 while (my $keyval = <$fh>) {
2636 chomp $keyval;
2637 my ($key, $value) = split(/\n/, $keyval, 2);
2638
2639 hash_set_multi(\%config, $key, $value)
2640 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
2641 }
2642 close $fh;
2643
2644 return %config;
2645 }
2646
2647 # convert config value to boolean: 'true' or 'false'
2648 # no value, number > 0, 'true' and 'yes' values are true
2649 # rest of values are treated as false (never as error)
2650 sub config_to_bool {
2651 my $val = shift;
2652
2653 return 1 if !defined $val; # section.key
2654
2655 # strip leading and trailing whitespace
2656 $val =~ s/^\s+//;
2657 $val =~ s/\s+$//;
2658
2659 return (($val =~ /^\d+$/ && $val) || # section.key = 1
2660 ($val =~ /^(?:true|yes)$/i)); # section.key = true
2661 }
2662
2663 # convert config value to simple decimal number
2664 # an optional value suffix of 'k', 'm', or 'g' will cause the value
2665 # to be multiplied by 1024, 1048576, or 1073741824
2666 sub config_to_int {
2667 my $val = shift;
2668
2669 # strip leading and trailing whitespace
2670 $val =~ s/^\s+//;
2671 $val =~ s/\s+$//;
2672
2673 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
2674 $unit = lc($unit);
2675 # unknown unit is treated as 1
2676 return $num * ($unit eq 'g' ? 1073741824 :
2677 $unit eq 'm' ? 1048576 :
2678 $unit eq 'k' ? 1024 : 1);
2679 }
2680 return $val;
2681 }
2682
2683 # convert config value to array reference, if needed
2684 sub config_to_multi {
2685 my $val = shift;
2686
2687 return ref($val) ? $val : (defined($val) ? [ $val ] : []);
2688 }
2689
2690 sub git_get_project_config {
2691 my ($key, $type) = @_;
2692
2693 return unless defined $git_dir;
2694
2695 # key sanity check
2696 return unless ($key);
2697 # only subsection, if exists, is case sensitive,
2698 # and not lowercased by 'git config -z -l'
2699 if (my ($hi, $mi, $lo) = ($key =~ /^([^.]*)\.(.*)\.([^.]*)$/)) {
2700 $lo =~ s/_//g;
2701 $key = join(".", lc($hi), $mi, lc($lo));
2702 return if ($lo =~ /\W/ || $hi =~ /\W/);
2703 } else {
2704 $key = lc($key);
2705 $key =~ s/_//g;
2706 return if ($key =~ /\W/);
2707 }
2708 $key =~ s/^gitweb\.//;
2709
2710 # type sanity check
2711 if (defined $type) {
2712 $type =~ s/^--//;
2713 $type = undef
2714 unless ($type eq 'bool' || $type eq 'int');
2715 }
2716
2717 # get config
2718 if (!defined $config_file ||
2719 $config_file ne "$git_dir/config") {
2720 %config = git_parse_project_config('gitweb');
2721 $config_file = "$git_dir/config";
2722 }
2723
2724 # check if config variable (key) exists
2725 return unless exists $config{"gitweb.$key"};
2726
2727 # ensure given type
2728 if (!defined $type) {
2729 return $config{"gitweb.$key"};
2730 } elsif ($type eq 'bool') {
2731 # backward compatibility: 'git config --bool' returns true/false
2732 return config_to_bool($config{"gitweb.$key"}) ? 'true' : 'false';
2733 } elsif ($type eq 'int') {
2734 return config_to_int($config{"gitweb.$key"});
2735 }
2736 return $config{"gitweb.$key"};
2737 }
2738
2739 # get hash of given path at given ref
2740 sub git_get_hash_by_path {
2741 my $base = shift;
2742 my $path = shift || return undef;
2743 my $type = shift;
2744
2745 $path =~ s,/+$,,;
2746
2747 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
2748 or die_error(500, "Open git-ls-tree failed");
2749 my $line = <$fd>;
2750 close $fd or return undef;
2751
2752 if (!defined $line) {
2753 # there is no tree or hash given by $path at $base
2754 return undef;
2755 }
2756
2757 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2758 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
2759 if (defined $type && $type ne $2) {
2760 # type doesn't match
2761 return undef;
2762 }
2763 return $3;
2764 }
2765
2766 # get path of entry with given hash at given tree-ish (ref)
2767 # used to get 'from' filename for combined diff (merge commit) for renames
2768 sub git_get_path_by_hash {
2769 my $base = shift || return;
2770 my $hash = shift || return;
2771
2772 local $/ = "\0";
2773
2774 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
2775 or return undef;
2776 while (my $line = <$fd>) {
2777 chomp $line;
2778
2779 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
2780 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
2781 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
2782 close $fd;
2783 return $1;
2784 }
2785 }
2786 close $fd;
2787 return undef;
2788 }
2789
2790 ## ......................................................................
2791 ## git utility functions, directly accessing git repository
2792
2793 # get the value of config variable either from file named as the variable
2794 # itself in the repository ($GIT_DIR/$name file), or from gitweb.$name
2795 # configuration variable in the repository config file.
2796 sub git_get_file_or_project_config {
2797 my ($path, $name) = @_;
2798
2799 $git_dir = "$projectroot/$path";
2800 open my $fd, '<', "$git_dir/$name"
2801 or return git_get_project_config($name);
2802 my $conf = <$fd>;
2803 close $fd;
2804 if (defined $conf) {
2805 chomp $conf;
2806 }
2807 return $conf;
2808 }
2809
2810 sub git_get_project_description {
2811 my $path = shift;
2812 return git_get_file_or_project_config($path, 'description');
2813 }
2814
2815 sub git_get_project_category {
2816 my $path = shift;
2817 return git_get_file_or_project_config($path, 'category');
2818 }
2819
2820
2821 # supported formats:
2822 # * $GIT_DIR/ctags/<tagname> file (in 'ctags' subdirectory)
2823 # - if its contents is a number, use it as tag weight,
2824 # - otherwise add a tag with weight 1
2825 # * $GIT_DIR/ctags file, each line is a tag (with weight 1)
2826 # the same value multiple times increases tag weight
2827 # * `gitweb.ctag' multi-valued repo config variable
2828 sub git_get_project_ctags {
2829 my $project = shift;
2830 my $ctags = {};
2831
2832 $git_dir = "$projectroot/$project";
2833 if (opendir my $dh, "$git_dir/ctags") {
2834 my @files = grep { -f $_ } map { "$git_dir/ctags/$_" } readdir($dh);
2835 foreach my $tagfile (@files) {
2836 open my $ct, '<', $tagfile
2837 or next;
2838 my $val = <$ct>;
2839 chomp $val if $val;
2840 close $ct;
2841
2842 (my $ctag = $tagfile) =~ s#.*/##;
2843 if ($val =~ /^\d+$/) {
2844 $ctags->{$ctag} = $val;
2845 } else {
2846 $ctags->{$ctag} = 1;
2847 }
2848 }
2849 closedir $dh;
2850
2851 } elsif (open my $fh, '<', "$git_dir/ctags") {
2852 while (my $line = <$fh>) {
2853 chomp $line;
2854 $ctags->{$line}++ if $line;
2855 }
2856 close $fh;
2857
2858 } else {
2859 my $taglist = config_to_multi(git_get_project_config('ctag'));
2860 foreach my $tag (@$taglist) {
2861 $ctags->{$tag}++;
2862 }
2863 }
2864
2865 return $ctags;
2866 }
2867
2868 # return hash, where keys are content tags ('ctags'),
2869 # and values are sum of weights of given tag in every project
2870 sub git_gather_all_ctags {
2871 my $projects = shift;
2872 my $ctags = {};
2873
2874 foreach my $p (@$projects) {
2875 foreach my $ct (keys %{$p->{'ctags'}}) {
2876 $ctags->{$ct} += $p->{'ctags'}->{$ct};
2877 }
2878 }
2879
2880 return $ctags;
2881 }
2882
2883 sub git_populate_project_tagcloud {
2884 my $ctags = shift;
2885
2886 # First, merge different-cased tags; tags vote on casing
2887 my %ctags_lc;
2888 foreach (keys %$ctags) {
2889 $ctags_lc{lc $_}->{count} += $ctags->{$_};
2890 if (not $ctags_lc{lc $_}->{topcount}
2891 or $ctags_lc{lc $_}->{topcount} < $ctags->{$_}) {
2892 $ctags_lc{lc $_}->{topcount} = $ctags->{$_};
2893 $ctags_lc{lc $_}->{topname} = $_;
2894 }
2895 }
2896
2897 my $cloud;
2898 my $matched = $input_params{'ctag'};
2899 if (eval { require HTML::TagCloud; 1; }) {
2900 $cloud = HTML::TagCloud->new;
2901 foreach my $ctag (sort keys %ctags_lc) {
2902 # Pad the title with spaces so that the cloud looks
2903 # less crammed.
2904 my $title = esc_html($ctags_lc{$ctag}->{topname});
2905 $title =~ s/ /&nbsp;/g;
2906 $title =~ s/^/&nbsp;/g;
2907 $title =~ s/$/&nbsp;/g;
2908 if (defined $matched && $matched eq $ctag) {
2909 $title = qq(<span class="match">$title</span>);
2910 }
2911 $cloud->add($title, href(project=>undef, ctag=>$ctag),
2912 $ctags_lc{$ctag}->{count});
2913 }
2914 } else {
2915 $cloud = {};
2916 foreach my $ctag (keys %ctags_lc) {
2917 my $title = esc_html($ctags_lc{$ctag}->{topname}, -nbsp=>1);
2918 if (defined $matched && $matched eq $ctag) {
2919 $title = qq(<span class="match">$title</span>);
2920 }
2921 $cloud->{$ctag}{count} = $ctags_lc{$ctag}->{count};
2922 $cloud->{$ctag}{ctag} =
2923 $cgi->a({-href=>href(project=>undef, ctag=>$ctag)}, $title);
2924 }
2925 }
2926 return $cloud;
2927 }
2928
2929 sub git_show_project_tagcloud {
2930 my ($cloud, $count) = @_;
2931 if (ref $cloud eq 'HTML::TagCloud') {
2932 return $cloud->html_and_css($count);
2933 } else {
2934 my @tags = sort { $cloud->{$a}->{'count'} <=> $cloud->{$b}->{'count'} } keys %$cloud;
2935 return
2936 '<div id="htmltagcloud"'.($project ? '' : ' align="center"').'>' .
2937 join (', ', map {
2938 $cloud->{$_}->{'ctag'}
2939 } splice(@tags, 0, $count)) .
2940 '</div>';
2941 }
2942 }
2943
2944 sub git_get_project_url_list {
2945 my $path = shift;
2946
2947 $git_dir = "$projectroot/$path";
2948 open my $fd, '<', "$git_dir/cloneurl"
2949 or return wantarray ?
2950 @{ config_to_multi(git_get_project_config('url')) } :
2951 config_to_multi(git_get_project_config('url'));
2952 my @git_project_url_list = map { chomp; $_ } <$fd>;
2953 close $fd;
2954
2955 return wantarray ? @git_project_url_list : \@git_project_url_list;
2956 }
2957
2958 sub git_get_projects_list {
2959 my $filter = shift || '';
2960 my $paranoid = shift;
2961 my @list;
2962
2963 if (-d $projects_list) {
2964 # search in directory
2965 my $dir = $projects_list;
2966 # remove the trailing "/"
2967 $dir =~ s!/+$!!;
2968 my $pfxlen = length("$dir");
2969 my $pfxdepth = ($dir =~ tr!/!!);
2970 # when filtering, search only given subdirectory
2971 if ($filter && !$paranoid) {
2972 $dir .= "/$filter";
2973 $dir =~ s!/+$!!;
2974 }
2975
2976 File::Find::find({
2977 follow_fast => 1, # follow symbolic links
2978 follow_skip => 2, # ignore duplicates
2979 dangling_symlinks => 0, # ignore dangling symlinks, silently
2980 wanted => sub {
2981 # global variables
2982 our $project_maxdepth;
2983 our $projectroot;
2984 # skip project-list toplevel, if we get it.
2985 return if (m!^[/.]$!);
2986 # only directories can be git repositories
2987 return unless (-d $_);
2988 # don't traverse too deep (Find is super slow on os x)
2989 # $project_maxdepth excludes depth of $projectroot
2990 if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
2991 $File::Find::prune = 1;
2992 return;
2993 }
2994
2995 my $path = substr($File::Find::name, $pfxlen + 1);
2996 # paranoidly only filter here
2997 if ($paranoid && $filter && $path !~ m!^\Q$filter\E/!) {
2998 next;
2999 }
3000 # we check related file in $projectroot
3001 if (check_export_ok("$projectroot/$path")) {
3002 push @list, { path => $path };
3003 $File::Find::prune = 1;
3004 }
3005 },
3006 }, "$dir");
3007
3008 } elsif (-f $projects_list) {
3009 # read from file(url-encoded):
3010 # 'git%2Fgit.git Linus+Torvalds'
3011 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
3012 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
3013 open my $fd, '<', $projects_list or return;
3014 PROJECT:
3015 while (my $line = <$fd>) {
3016 chomp $line;
3017 my ($path, $owner) = split ' ', $line;
3018 $path = unescape($path);
3019 $owner = unescape($owner);
3020 if (!defined $path) {
3021 next;
3022 }
3023 # if $filter is rpovided, check if $path begins with $filter
3024 if ($filter && $path !~ m!^\Q$filter\E/!) {
3025 next;
3026 }
3027 if (check_export_ok("$projectroot/$path")) {
3028 my $pr = {
3029 path => $path
3030 };
3031 if ($owner) {
3032 $pr->{'owner'} = to_utf8($owner);
3033 }
3034 push @list, $pr;
3035 }
3036 }
3037 close $fd;
3038 }
3039 return @list;
3040 }
3041
3042 # written with help of Tree::Trie module (Perl Artistic License, GPL compatibile)
3043 # as side effects it sets 'forks' field to list of forks for forked projects
3044 sub filter_forks_from_projects_list {
3045 my $projects = shift;
3046
3047 my %trie; # prefix tree of directories (path components)
3048 # generate trie out of those directories that might contain forks
3049 foreach my $pr (@$projects) {
3050 my $path = $pr->{'path'};
3051 $path =~ s/\.git$//; # forks of 'repo.git' are in 'repo/' directory
3052 next if ($path =~ m!/$!); # skip non-bare repositories, e.g. 'repo/.git'
3053 next unless ($path); # skip '.git' repository: tests, git-instaweb
3054 next unless (-d "$projectroot/$path"); # containing directory exists
3055 $pr->{'forks'} = []; # there can be 0 or more forks of project
3056
3057 # add to trie
3058 my @dirs = split('/', $path);
3059 # walk the trie, until either runs out of components or out of trie
3060 my $ref = \%trie;
3061 while (scalar @dirs &&
3062 exists($ref->{$dirs[0]})) {
3063 $ref = $ref->{shift @dirs};
3064 }
3065 # create rest of trie structure from rest of components
3066 foreach my $dir (@dirs) {
3067 $ref = $ref->{$dir} = {};
3068 }
3069 # create end marker, store $pr as a data
3070 $ref->{''} = $pr if (!exists $ref->{''});
3071 }
3072
3073 # filter out forks, by finding shortest prefix match for paths
3074 my @filtered;
3075 PROJECT:
3076 foreach my $pr (@$projects) {
3077 # trie lookup
3078 my $ref = \%trie;
3079 DIR:
3080 foreach my $dir (split('/', $pr->{'path'})) {
3081 if (exists $ref->{''}) {
3082 # found [shortest] prefix, is a fork - skip it
3083 push @{$ref->{''}{'forks'}}, $pr;
3084 next PROJECT;
3085 }
3086 if (!exists $ref->{$dir}) {
3087 # not in trie, cannot have prefix, not a fork
3088 push @filtered, $pr;
3089 next PROJECT;
3090 }
3091 # If the dir is there, we just walk one step down the trie.
3092 $ref = $ref->{$dir};
3093 }
3094 # we ran out of trie
3095 # (shouldn't happen: it's either no match, or end marker)
3096 push @filtered, $pr;
3097 }
3098
3099 return @filtered;
3100 }
3101
3102 # note: fill_project_list_info must be run first,
3103 # for 'descr_long' and 'ctags' to be filled
3104 sub search_projects_list {
3105 my ($projlist, %opts) = @_;
3106 my $tagfilter = $opts{'tagfilter'};
3107 my $search_re = $opts{'search_regexp'};
3108
3109 return @$projlist
3110 unless ($tagfilter || $search_re);
3111
3112 # searching projects require filling to be run before it;
3113 fill_project_list_info($projlist,
3114 $tagfilter ? 'ctags' : (),
3115 $search_re ? ('path', 'descr') : ());
3116 my @projects;
3117 PROJECT:
3118 foreach my $pr (@$projlist) {
3119
3120 if ($tagfilter) {
3121 next unless ref($pr->{'ctags'}) eq 'HASH';
3122 next unless
3123 grep { lc($_) eq lc($tagfilter) } keys %{$pr->{'ctags'}};
3124 }
3125
3126 if ($search_re) {
3127 next unless
3128 $pr->{'path'} =~ /$search_re/ ||
3129 $pr->{'descr_long'} =~ /$search_re/;
3130 }
3131
3132 push @projects, $pr;
3133 }
3134
3135 return @projects;
3136 }
3137
3138 our $gitweb_project_owner = undef;
3139 sub git_get_project_list_from_file {
3140
3141 return if (defined $gitweb_project_owner);
3142
3143 $gitweb_project_owner = {};
3144 # read from file (url-encoded):
3145 # 'git%2Fgit.git Linus+Torvalds'
3146 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
3147 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
3148 if (-f $projects_list) {
3149 open(my $fd, '<', $projects_list);
3150 while (my $line = <$fd>) {
3151 chomp $line;
3152 my ($pr, $ow) = split ' ', $line;
3153 $pr = unescape($pr);
3154 $ow = unescape($ow);
3155 $gitweb_project_owner->{$pr} = to_utf8($ow);
3156 }
3157 close $fd;
3158 }
3159 }
3160
3161 sub git_get_project_owner {
3162 my $project = shift;
3163 my $owner;
3164
3165 return undef unless $project;
3166 $git_dir = "$projectroot/$project";
3167
3168 if (!defined $gitweb_project_owner) {
3169 git_get_project_list_from_file();
3170 }
3171
3172 if (exists $gitweb_project_owner->{$project}) {
3173 $owner = $gitweb_project_owner->{$project};
3174 }
3175 if (!defined $owner){
3176 $owner = git_get_project_config('owner');
3177 }
3178 if (!defined $owner) {
3179 $owner = get_file_owner("$git_dir");
3180 }
3181
3182 return $owner;
3183 }
3184
3185 sub git_get_last_activity {
3186 my ($path) = @_;
3187 my $fd;
3188
3189 $git_dir = "$projectroot/$path";
3190 open($fd, "-|", git_cmd(), 'for-each-ref',
3191 '--format=%(committer)',
3192 '--sort=-committerdate',
3193 '--count=1',
3194 'refs/heads') or return;
3195 my $most_recent = <$fd>;
3196 close $fd or return;
3197 if (defined $most_recent &&
3198 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
3199 my $timestamp = $1;
3200 my $age = time - $timestamp;
3201 return ($age, age_string($age));
3202 }
3203 return (undef, undef);
3204 }
3205
3206 # Implementation note: when a single remote is wanted, we cannot use 'git
3207 # remote show -n' because that command always work (assuming it's a remote URL
3208 # if it's not defined), and we cannot use 'git remote show' because that would
3209 # try to make a network roundtrip. So the only way to find if that particular
3210 # remote is defined is to walk the list provided by 'git remote -v' and stop if
3211 # and when we find what we want.
3212 sub git_get_remotes_list {
3213 my $wanted = shift;
3214 my %remotes = ();
3215
3216 open my $fd, '-|' , git_cmd(), 'remote', '-v';
3217 return unless $fd;
3218 while (my $remote = <$fd>) {
3219 chomp $remote;
3220 $remote =~ s!\t(.*?)\s+\((\w+)\)$!!;
3221 next if $wanted and not $remote eq $wanted;
3222 my ($url, $key) = ($1, $2);
3223
3224 $remotes{$remote} ||= { 'heads' => () };
3225 $remotes{$remote}{$key} = $url;
3226 }
3227 close $fd or return;
3228 return wantarray ? %remotes : \%remotes;
3229 }
3230
3231 # Takes a hash of remotes as first parameter and fills it by adding the
3232 # available remote heads for each of the indicated remotes.
3233 sub fill_remote_heads {
3234 my $remotes = shift;
3235 my @heads = map { "remotes/$_" } keys %$remotes;
3236 my @remoteheads = git_get_heads_list(undef, @heads);
3237 foreach my $remote (keys %$remotes) {
3238 $remotes->{$remote}{'heads'} = [ grep {
3239 $_->{'name'} =~ s!^$remote/!!
3240 } @remoteheads ];
3241 }
3242 }
3243
3244 sub git_get_references {
3245 my $type = shift || "";
3246 my %refs;
3247 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
3248 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
3249 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
3250 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
3251 or return;
3252
3253 while (my $line = <$fd>) {
3254 chomp $line;
3255 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {
3256 if (defined $refs{$1}) {
3257 push @{$refs{$1}}, $2;
3258 } else {
3259 $refs{$1} = [ $2 ];
3260 }
3261 }
3262 }
3263 close $fd or return;
3264 return \%refs;
3265 }
3266
3267 sub git_get_rev_name_tags {
3268 my $hash = shift || return undef;
3269
3270 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
3271 or return;
3272 my $name_rev = <$fd>;
3273 close $fd;
3274
3275 if ($name_rev =~ m|^$hash tags/(.*)$|) {
3276 return $1;
3277 } else {
3278 # catches also '$hash undefined' output
3279 return undef;
3280 }
3281 }
3282
3283 ## ----------------------------------------------------------------------
3284 ## parse to hash functions
3285
3286 sub parse_date {
3287 my $epoch = shift;
3288 my $tz = shift || "-0000";
3289
3290 my %date;
3291 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
3292 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
3293 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
3294 $date{'hour'} = $hour;
3295 $date{'minute'} = $min;
3296 $date{'mday'} = $mday;
3297 $date{'day'} = $days[$wday];
3298 $date{'month'} = $months[$mon];
3299 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
3300 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
3301 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
3302 $mday, $months[$mon], $hour ,$min;
3303 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
3304 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
3305
3306 my ($tz_sign, $tz_hour, $tz_min) =
3307 ($tz =~ m/^([-+])(\d\d)(\d\d)$/);
3308 $tz_sign = ($tz_sign eq '-' ? -1 : +1);
3309 my $local = $epoch + $tz_sign*((($tz_hour*60) + $tz_min)*60);
3310 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
3311 $date{'hour_local'} = $hour;
3312 $date{'minute_local'} = $min;
3313 $date{'tz_local'} = $tz;
3314 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
3315 1900+$year, $mon+1, $mday,
3316 $hour, $min, $sec, $tz);
3317 return %date;
3318 }
3319
3320 sub parse_tag {
3321 my $tag_id = shift;
3322 my %tag;
3323 my @comment;
3324
3325 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
3326 $tag{'id'} = $tag_id;
3327 while (my $line = <$fd>) {
3328 chomp $line;
3329 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
3330 $tag{'object'} = $1;
3331 } elsif ($line =~ m/^type (.+)$/) {
3332 $tag{'type'} = $1;
3333 } elsif ($line =~ m/^tag (.+)$/) {
3334 $tag{'name'} = $1;
3335 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
3336 $tag{'author'} = $1;
3337 $tag{'author_epoch'} = $2;
3338 $tag{'author_tz'} = $3;
3339 if ($tag{'author'} =~ m/^([^<]+) <([^>]*)>/) {
3340 $tag{'author_name'} = $1;
3341 $tag{'author_email'} = $2;
3342 } else {
3343 $tag{'author_name'} = $tag{'author'};
3344 }
3345 } elsif ($line =~ m/--BEGIN/) {
3346 push @comment, $line;
3347 last;
3348 } elsif ($line eq "") {
3349 last;
3350 }
3351 }
3352 push @comment, <$fd>;
3353 $tag{'comment'} = \@comment;
3354 close $fd or return;
3355 if (!defined $tag{'name'}) {
3356 return
3357 };
3358 return %tag
3359 }
3360
3361 sub parse_commit_text {
3362 my ($commit_text, $withparents) = @_;
3363 my @commit_lines = split '\n', $commit_text;
3364 my %co;
3365
3366 pop @commit_lines; # Remove '\0'
3367
3368 if (! @commit_lines) {
3369 return;
3370 }
3371
3372 my $header = shift @commit_lines;
3373 if ($header !~ m/^[0-9a-fA-F]{40}/) {
3374 return;
3375 }
3376 ($co{'id'}, my @parents) = split ' ', $header;
3377 while (my $line = shift @commit_lines) {
3378 last if $line eq "\n";
3379 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
3380 $co{'tree'} = $1;
3381 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
3382 push @parents, $1;
3383 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
3384 $co{'author'} = to_utf8($1);
3385 $co{'author_epoch'} = $2;
3386 $co{'author_tz'} = $3;
3387 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
3388 $co{'author_name'} = $1;
3389 $co{'author_email'} = $2;
3390 } else {
3391 $co{'author_name'} = $co{'author'};
3392 }
3393 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
3394 $co{'committer'} = to_utf8($1);
3395 $co{'committer_epoch'} = $2;
3396 $co{'committer_tz'} = $3;
3397 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
3398 $co{'committer_name'} = $1;
3399 $co{'committer_email'} = $2;
3400 } else {
3401 $co{'committer_name'} = $co{'committer'};
3402 }
3403 }
3404 }
3405 if (!defined $co{'tree'}) {
3406 return;
3407 };
3408 $co{'parents'} = \@parents;
3409 $co{'parent'} = $parents[0];
3410
3411 foreach my $title (@commit_lines) {
3412 $title =~ s/^ //;
3413 if ($title ne "") {
3414 $co{'title'} = chop_str($title, 80, 5);
3415 # remove leading stuff of merges to make the interesting part visible
3416 if (length($title) > 50) {
3417 $title =~ s/^Automatic //;
3418 $title =~ s/^merge (of|with) /Merge ... /i;
3419 if (length($title) > 50) {
3420 $title =~ s/(http|rsync):\/\///;
3421 }
3422 if (length($title) > 50) {
3423 $title =~ s/(master|www|rsync)\.//;
3424 }
3425 if (length($title) > 50) {
3426 $title =~ s/kernel.org:?//;
3427 }
3428 if (length($title) > 50) {
3429 $title =~ s/\/pub\/scm//;
3430 }
3431 }
3432 $co{'title_short'} = chop_str($title, 50, 5);
3433 last;
3434 }
3435 }
3436 if (! defined $co{'title'} || $co{'title'} eq "") {
3437 $co{'title'} = $co{'title_short'} = '(no commit message)';
3438 }
3439 # remove added spaces
3440 foreach my $line (@commit_lines) {
3441 $line =~ s/^ //;
3442 }
3443 $co{'comment'} = \@commit_lines;
3444
3445 my $age = time - $co{'committer_epoch'};
3446 $co{'age'} = $age;
3447 $co{'age_string'} = age_string($age);
3448 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
3449 if ($age > 60*60*24*7*2) {
3450 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
3451 $co{'age_string_age'} = $co{'age_string'};
3452 } else {
3453 $co{'age_string_date'} = $co{'age_string'};
3454 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
3455 }
3456 return %co;
3457 }
3458
3459 sub parse_commit {
3460 my ($commit_id) = @_;
3461 my %co;
3462
3463 local $/ = "\0";
3464
3465 open my $fd, "-|", git_cmd(), "rev-list",
3466 "--parents",
3467 "--header",
3468 "--max-count=1",
3469 $commit_id,
3470 "--",
3471 or die_error(500, "Open git-rev-list failed");
3472 %co = parse_commit_text(<$fd>, 1);
3473 close $fd;
3474
3475 return %co;
3476 }
3477
3478 sub parse_commits {
3479 my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
3480 my @cos;
3481
3482 $maxcount ||= 1;
3483 $skip ||= 0;
3484
3485 local $/ = "\0";
3486
3487 open my $fd, "-|", git_cmd(), "rev-list",
3488 "--header",
3489 @args,
3490 ("--max-count=" . $maxcount),
3491 ("--skip=" . $skip),
3492 @extra_options,
3493 $commit_id,
3494 "--",
3495 ($filename ? ($filename) : ())
3496 or die_error(500, "Open git-rev-list failed");
3497 while (my $line = <$fd>) {
3498 my %co = parse_commit_text($line);
3499 push @cos, \%co;
3500 }
3501 close $fd;
3502
3503 return wantarray ? @cos : \@cos;
3504 }
3505
3506 # parse line of git-diff-tree "raw" output
3507 sub parse_difftree_raw_line {
3508 my $line = shift;
3509 my %res;
3510
3511 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
3512 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
3513 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
3514 $res{'from_mode'} = $1;
3515 $res{'to_mode'} = $2;
3516 $res{'from_id'} = $3;
3517 $res{'to_id'} = $4;
3518 $res{'status'} = $5;
3519 $res{'similarity'} = $6;
3520 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
3521 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
3522 } else {
3523 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote($7);
3524 }
3525 }
3526 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
3527 # combined diff (for merge commit)
3528 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
3529 $res{'nparents'} = length($1);
3530 $res{'from_mode'} = [ split(' ', $2) ];
3531 $res{'to_mode'} = pop @{$res{'from_mode'}};
3532 $res{'from_id'} = [ split(' ', $3) ];
3533 $res{'to_id'} = pop @{$res{'from_id'}};
3534 $res{'status'} = [ split('', $4) ];
3535 $res{'to_file'} = unquote($5);
3536 }
3537 # 'c512b523472485aef4fff9e57b229d9d243c967f'
3538 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
3539 $res{'commit'} = $1;
3540 }
3541
3542 return wantarray ? %res : \%res;
3543 }
3544
3545 # wrapper: return parsed line of git-diff-tree "raw" output
3546 # (the argument might be raw line, or parsed info)
3547 sub parsed_difftree_line {
3548 my $line_or_ref = shift;
3549
3550 if (ref($line_or_ref) eq "HASH") {
3551 # pre-parsed (or generated by hand)
3552 return $line_or_ref;
3553 } else {
3554 return parse_difftree_raw_line($line_or_ref);
3555 }
3556 }
3557
3558 # parse line of git-ls-tree output
3559 sub parse_ls_tree_line {
3560 my $line = shift;
3561 my %opts = @_;
3562 my %res;
3563
3564 if ($opts{'-l'}) {
3565 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'
3566 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;
3567
3568 $res{'mode'} = $1;
3569 $res{'type'} = $2;
3570 $res{'hash'} = $3;
3571 $res{'size'} = $4;
3572 if ($opts{'-z'}) {
3573 $res{'name'} = $5;
3574 } else {
3575 $res{'name'} = unquote($5);
3576 }
3577 } else {
3578 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
3579 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
3580
3581 $res{'mode'} = $1;
3582 $res{'type'} = $2;
3583 $res{'hash'} = $3;
3584 if ($opts{'-z'}) {
3585 $res{'name'} = $4;
3586 } else {
3587 $res{'name'} = unquote($4);
3588 }
3589 }
3590
3591 return wantarray ? %res : \%res;
3592 }
3593
3594 # generates _two_ hashes, references to which are passed as 2 and 3 argument
3595 sub parse_from_to_diffinfo {
3596 my ($diffinfo, $from, $to, @parents) = @_;
3597
3598 if ($diffinfo->{'nparents'}) {
3599 # combined diff
3600 $from->{'file'} = [];
3601 $from->{'href'} = [];
3602 fill_from_file_info($diffinfo, @parents)
3603 unless exists $diffinfo->{'from_file'};
3604 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
3605 $from->{'file'}[$i] =
3606 defined $diffinfo->{'from_file'}[$i] ?
3607 $diffinfo->{'from_file'}[$i] :
3608 $diffinfo->{'to_file'};
3609 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
3610 $from->{'href'}[$i] = href(action=>"blob",
3611 hash_base=>$parents[$i],
3612 hash=>$diffinfo->{'from_id'}[$i],
3613 file_name=>$from->{'file'}[$i]);
3614 } else {
3615 $from->{'href'}[$i] = undef;
3616 }
3617 }
3618 } else {
3619 # ordinary (not combined) diff
3620 $from->{'file'} = $diffinfo->{'from_file'};
3621 if ($diffinfo->{'status'} ne "A") { # not new (added) file
3622 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
3623 hash=>$diffinfo->{'from_id'},
3624 file_name=>$from->{'file'});
3625 } else {
3626 delete $from->{'href'};
3627 }
3628 }
3629
3630 $to->{'file'} = $diffinfo->{'to_file'};
3631 if (!is_deleted($diffinfo)) { # file exists in result
3632 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
3633 hash=>$diffinfo->{'to_id'},
3634 file_name=>$to->{'file'});
3635 } else {
3636 delete $to->{'href'};
3637 }
3638 }
3639
3640 ## ......................................................................
3641 ## parse to array of hashes functions
3642
3643 sub git_get_heads_list {
3644 my ($limit, @classes) = @_;
3645 @classes = ('heads') unless @classes;
3646 my @patterns = map { "refs/$_" } @classes;
3647 my @headslist;
3648
3649 open my $fd, '-|', git_cmd(), 'for-each-ref',
3650 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
3651 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
3652 @patterns
3653 or return;
3654 while (my $line = <$fd>) {
3655 my %ref_item;
3656
3657 chomp $line;
3658 my ($refinfo, $committerinfo) = split(/\0/, $line);
3659 my ($hash, $name, $title) = split(' ', $refinfo, 3);
3660 my ($committer, $epoch, $tz) =
3661 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
3662 $ref_item{'fullname'} = $name;
3663 $name =~ s!^refs/(?:head|remote)s/!!;
3664
3665 $ref_item{'name'} = $name;
3666 $ref_item{'id'} = $hash;
3667 $ref_item{'title'} = $title || '(no commit message)';
3668 $ref_item{'epoch'} = $epoch;
3669 if ($epoch) {
3670 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
3671 } else {
3672 $ref_item{'age'} = "unknown";
3673 }
3674
3675 push @headslist, \%ref_item;
3676 }
3677 close $fd;
3678
3679 return wantarray ? @headslist : \@headslist;
3680 }
3681
3682 sub git_get_tags_list {
3683 my $limit = shift;
3684 my @tagslist;
3685
3686 open my $fd, '-|', git_cmd(), 'for-each-ref',
3687 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
3688 '--format=%(objectname) %(objecttype) %(refname) '.
3689 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
3690 'refs/tags'
3691 or return;
3692 while (my $line = <$fd>) {
3693 my %ref_item;
3694
3695 chomp $line;
3696 my ($refinfo, $creatorinfo) = split(/\0/, $line);
3697 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
3698 my ($creator, $epoch, $tz) =
3699 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
3700 $ref_item{'fullname'} = $name;
3701 $name =~ s!^refs/tags/!!;
3702
3703 $ref_item{'type'} = $type;
3704 $ref_item{'id'} = $id;
3705 $ref_item{'name'} = $name;
3706 if ($type eq "tag") {
3707 $ref_item{'subject'} = $title;
3708 $ref_item{'reftype'} = $reftype;
3709 $ref_item{'refid'} = $refid;
3710 } else {
3711 $ref_item{'reftype'} = $type;
3712 $ref_item{'refid'} = $id;
3713 }
3714
3715 if ($type eq "tag" || $type eq "commit") {
3716 $ref_item{'epoch'} = $epoch;
3717 if ($epoch) {
3718 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
3719 } else {
3720 $ref_item{'age'} = "unknown";
3721 }
3722 }
3723
3724 push @tagslist, \%ref_item;
3725 }
3726 close $fd;
3727
3728 return wantarray ? @tagslist : \@tagslist;
3729 }
3730
3731 ## ----------------------------------------------------------------------
3732 ## filesystem-related functions
3733
3734 sub get_file_owner {
3735 my $path = shift;
3736
3737 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
3738 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
3739 if (!defined $gcos) {
3740 return undef;
3741 }
3742 my $owner = $gcos;
3743 $owner =~ s/[,;].*$//;
3744 return to_utf8($owner);
3745 }
3746
3747 # assume that file exists
3748 sub insert_file {
3749 my $filename = shift;
3750
3751 open my $fd, '<', $filename;
3752 print map { to_utf8($_) } <$fd>;
3753 close $fd;
3754 }
3755
3756 ## ......................................................................
3757 ## mimetype related functions
3758
3759 sub mimetype_guess_file {
3760 my $filename = shift;
3761 my $mimemap = shift;
3762 -r $mimemap or return undef;
3763
3764 my %mimemap;
3765 open(my $mh, '<', $mimemap) or return undef;
3766 while (<$mh>) {
3767 next if m/^#/; # skip comments
3768 my ($mimetype, @exts) = split(/\s+/);
3769 foreach my $ext (@exts) {
3770 $mimemap{$ext} = $mimetype;
3771 }
3772 }
3773 close($mh);
3774
3775 $filename =~ /\.([^.]*)$/;
3776 return $mimemap{$1};
3777 }
3778
3779 sub mimetype_guess {
3780 my $filename = shift;
3781 my $mime;
3782 $filename =~ /\./ or return undef;
3783
3784 if ($mimetypes_file) {
3785 my $file = $mimetypes_file;
3786 if ($file !~ m!^/!) { # if it is relative path
3787 # it is relative to project
3788 $file = "$projectroot/$project/$file";
3789 }
3790 $mime = mimetype_guess_file($filename, $file);
3791 }
3792 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
3793 return $mime;
3794 }
3795
3796 sub blob_mimetype {
3797 my $fd = shift;
3798 my $filename = shift;
3799
3800 if ($filename) {
3801 my $mime = mimetype_guess($filename);
3802 $mime and return $mime;
3803 }
3804
3805 # just in case
3806 return $default_blob_plain_mimetype unless $fd;
3807
3808 if (-T $fd) {
3809 return 'text/plain';
3810 } elsif (! $filename) {
3811 return 'application/octet-stream';
3812 } elsif ($filename =~ m/\.png$/i) {
3813 return 'image/png';
3814 } elsif ($filename =~ m/\.gif$/i) {
3815 return 'image/gif';
3816 } elsif ($filename =~ m/\.jpe?g$/i) {
3817 return 'image/jpeg';
3818 } else {
3819 return 'application/octet-stream';
3820 }
3821 }
3822
3823 sub blob_contenttype {
3824 my ($fd, $file_name, $type) = @_;
3825
3826 $type ||= blob_mimetype($fd, $file_name);
3827 if ($type eq 'text/plain' && defined $default_text_plain_charset) {
3828 $type .= "; charset=$default_text_plain_charset";
3829 }
3830
3831 return $type;
3832 }
3833
3834 # guess file syntax for syntax highlighting; return undef if no highlighting
3835 # the name of syntax can (in the future) depend on syntax highlighter used
3836 sub guess_file_syntax {
3837 my ($highlight, $mimetype, $file_name) = @_;
3838 return undef unless ($highlight && defined $file_name);
3839 my $basename = basename($file_name, '.in');
3840 return $highlight_basename{$basename}
3841 if exists $highlight_basename{$basename};
3842
3843 $basename =~ /\.([^.]*)$/;
3844 my $ext = $1 or return undef;
3845 return $highlight_ext{$ext}
3846 if exists $highlight_ext{$ext};
3847
3848 return undef;
3849 }
3850
3851 # run highlighter and return FD of its output,
3852 # or return original FD if no highlighting
3853 sub run_highlighter {
3854 my ($fd, $highlight, $syntax) = @_;
3855 return $fd unless ($highlight && defined $syntax);
3856
3857 close $fd;
3858 open $fd, quote_command(git_cmd(), "cat-file", "blob", $hash)." | ".
3859 quote_command($highlight_bin).
3860 " --replace-tabs=8 --fragment --syntax $syntax |"
3861 or die_error(500, "Couldn't open file or run syntax highlighter");
3862 return $fd;
3863 }
3864
3865 ## ======================================================================
3866 ## functions printing HTML: header, footer, error page
3867
3868 sub get_page_title {
3869 my $title = to_utf8($site_name);
3870
3871 unless (defined $project) {
3872 if (defined $project_filter) {
3873 $title .= " - projects in '" . esc_path($project_filter) . "'";
3874 }
3875 return $title;
3876 }
3877 $title .= " - " . to_utf8($project);
3878
3879 return $title unless (defined $action);
3880 $title .= "/$action"; # $action is US-ASCII (7bit ASCII)
3881
3882 return $title unless (defined $file_name);
3883 $title .= " - " . esc_path($file_name);
3884 if ($action eq "tree" && $file_name !~ m|/$|) {
3885 $title .= "/";
3886 }
3887
3888 return $title;
3889 }
3890
3891 sub get_content_type_html {
3892 # require explicit support from the UA if we are to send the page as
3893 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
3894 # we have to do this because MSIE sometimes globs '*/*', pretending to
3895 # support xhtml+xml but choking when it gets what it asked for.
3896 if (defined $cgi->http('HTTP_ACCEPT') &&
3897 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
3898 $cgi->Accept('application/xhtml+xml') != 0) {
3899 return 'application/xhtml+xml';
3900 } else {
3901 return 'text/html';
3902 }
3903 }
3904
3905 sub print_feed_meta {
3906 if (defined $project) {
3907 my %href_params = get_feed_info();
3908 if (!exists $href_params{'-title'}) {
3909 $href_params{'-title'} = 'log';
3910 }
3911
3912 foreach my $format (qw(RSS Atom)) {
3913 my $type = lc($format);
3914 my %link_attr = (
3915 '-rel' => 'alternate',
3916 '-title' => esc_attr("$project - $href_params{'-title'} - $format feed"),
3917 '-type' => "application/$type+xml"
3918 );
3919
3920 $href_params{'extra_options'} = undef;
3921 $href_params{'action'} = $type;
3922 $link_attr{'-href'} = href(%href_params);
3923 print "<link ".
3924 "rel=\"$link_attr{'-rel'}\" ".
3925 "title=\"$link_attr{'-title'}\" ".
3926 "href=\"$link_attr{'-href'}\" ".
3927 "type=\"$link_attr{'-type'}\" ".
3928 "/>\n";
3929
3930 $href_params{'extra_options'} = '--no-merges';
3931 $link_attr{'-href'} = href(%href_params);
3932 $link_attr{'-title'} .= ' (no merges)';
3933 print "<link ".
3934 "rel=\"$link_attr{'-rel'}\" ".
3935 "title=\"$link_attr{'-title'}\" ".
3936 "href=\"$link_attr{'-href'}\" ".
3937 "type=\"$link_attr{'-type'}\" ".
3938 "/>\n";
3939 }
3940
3941 } else {
3942 printf('<link rel="alternate" title="%s projects list" '.
3943 'href="%s" type="text/plain; charset=utf-8" />'."\n",
3944 esc_attr($site_name), href(project=>undef, action=>"project_index"));
3945 printf('<link rel="alternate" title="%s projects feeds" '.
3946 'href="%s" type="text/x-opml" />'."\n",
3947 esc_attr($site_name), href(project=>undef, action=>"opml"));
3948 }
3949 }
3950
3951 sub print_header_links {
3952 my $status = shift;
3953
3954 # print out each stylesheet that exist, providing backwards capability
3955 # for those people who defined $stylesheet in a config file
3956 if (defined $stylesheet) {
3957 print '<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";
3958 } else {
3959 foreach my $stylesheet (@stylesheets) {
3960 next unless $stylesheet;
3961 print '<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";
3962 }
3963 }
3964 print_feed_meta()
3965 if ($status eq '200 OK');
3966 if (defined $favicon) {
3967 print qq(<link rel="shortcut icon" href=").esc_url($favicon).qq(" type="image/png" />\n);
3968 }
3969 }
3970
3971 sub print_nav_breadcrumbs_path {
3972 my $dirprefix = undef;
3973 while (my $part = shift) {
3974 $dirprefix .= "/" if defined $dirprefix;
3975 $dirprefix .= $part;
3976 print $cgi->a({-href => href(project => undef,
3977 project_filter => $dirprefix,
3978 action => "project_list")},
3979 esc_html($part)) . " / ";
3980 }
3981 }
3982
3983 sub print_nav_breadcrumbs {
3984 my %opts = @_;
3985
3986 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
3987 if (defined $project) {
3988 my @dirname = split '/', $project;
3989 my $projectbasename = pop @dirname;
3990 print_nav_breadcrumbs_path(@dirname);
3991 print $cgi->a({-href => href(action=>"summary")}, esc_html($projectbasename));
3992 if (defined $action) {
3993 my $action_print = $action ;
3994 if (defined $opts{-action_extra}) {
3995 $action_print = $cgi->a({-href => href(action=>$action)},
3996 $action);
3997 }
3998 print " / $action_print";
3999 }
4000 if (defined $opts{-action_extra}) {
4001 print " / $opts{-action_extra}";
4002 }
4003 print "\n";
4004 } elsif (defined $project_filter) {
4005 print_nav_breadcrumbs_path(split '/', $project_filter);
4006 }
4007 }
4008
4009 sub print_search_form {
4010 if (!defined $searchtext) {
4011 $searchtext = "";
4012 }
4013 my $search_hash;
4014 if (defined $hash_base) {
4015 $search_hash = $hash_base;
4016 } elsif (defined $hash) {
4017 $search_hash = $hash;
4018 } else {
4019 $search_hash = "HEAD";
4020 }
4021 my $action = $my_uri;
4022 my $use_pathinfo = gitweb_check_feature('pathinfo');
4023 if ($use_pathinfo) {
4024 $action .= "/".esc_url($project);
4025 }
4026 print $cgi->startform(-method => "get", -action => $action) .
4027 "<div class=\"search\">\n" .
4028 (!$use_pathinfo &&
4029 $cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) . "\n") .
4030 $cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) . "\n" .
4031 $cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) . "\n" .
4032 $cgi->popup_menu(-name => 'st', -default => 'commit',
4033 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
4034 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
4035 " search:\n",
4036 $cgi->textfield(-name => "s", -value => $searchtext, -override => 1) . "\n" .
4037 "<span title=\"Extended regular expression\">" .
4038 $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
4039 -checked => $search_use_regexp) .
4040 "</span>" .
4041 "</div>" .
4042 $cgi->end_form() . "\n";
4043 }
4044
4045 sub git_header_html {
4046 my $status = shift || "200 OK";
4047 my $expires = shift;
4048 my %opts = @_;
4049
4050 my $title = get_page_title();
4051 my $content_type = get_content_type_html();
4052 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
4053 -status=> $status, -expires => $expires)
4054 unless ($opts{'-no_http_header'});
4055 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
4056 print <<EOF;
4057 <?xml version="1.0" encoding="utf-8"?>
4058 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
4059 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
4060 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
4061 <!-- git core binaries version $git_version -->
4062 <head>
4063 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
4064 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
4065 <meta name="robots" content="index, nofollow"/>
4066 <title>$title</title>
4067 EOF
4068 # the stylesheet, favicon etc urls won't work correctly with path_info
4069 # unless we set the appropriate base URL
4070 if ($ENV{'PATH_INFO'}) {
4071 print "<base href=\"".esc_url($base_url)."\" />\n";
4072 }
4073 print_header_links($status);
4074
4075 if (defined $site_html_head_string) {
4076 print to_utf8($site_html_head_string);
4077 }
4078
4079 print "</head>\n" .
4080 "<body>\n";
4081
4082 if (defined $site_header && -f $site_header) {
4083 insert_file($site_header);
4084 }
4085
4086 print "<div class=\"page_header\">\n";
4087 if (defined $logo) {
4088 print $cgi->a({-href => esc_url($logo_url),
4089 -title => $logo_label},
4090 $cgi->img({-src => esc_url($logo),
4091 -width => 72, -height => 27,
4092 -alt => "git",
4093 -class => "logo"}));
4094 }
4095 print_nav_breadcrumbs(%opts);
4096 print "</div>\n";
4097
4098 my $have_search = gitweb_check_feature('search');
4099 if (defined $project && $have_search) {
4100 print_search_form();
4101 }
4102 }
4103
4104 sub git_footer_html {
4105 my $feed_class = 'rss_logo';
4106
4107 print "<div class=\"page_footer\">\n";
4108 if (defined $project) {
4109 my $descr = git_get_project_description($project);
4110 if (defined $descr) {
4111 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
4112 }
4113
4114 my %href_params = get_feed_info();
4115 if (!%href_params) {
4116 $feed_class .= ' generic';
4117 }
4118 $href_params{'-title'} ||= 'log';
4119
4120 foreach my $format (qw(RSS Atom)) {
4121 $href_params{'action'} = lc($format);
4122 print $cgi->a({-href => href(%href_params),
4123 -title => "$href_params{'-title'} $format feed",
4124 -class => $feed_class}, $format)."\n";
4125 }
4126
4127 } else {
4128 print $cgi->a({-href => href(project=>undef, action=>"opml",
4129 project_filter => $project_filter),
4130 -class => $feed_class}, "OPML") . " ";
4131 print $cgi->a({-href => href(project=>undef, action=>"project_index",
4132 project_filter => $project_filter),
4133 -class => $feed_class}, "TXT") . "\n";
4134 }
4135 print "</div>\n"; # class="page_footer"
4136
4137 if (defined $t0 && gitweb_check_feature('timed')) {
4138 print "<div id=\"generating_info\">\n";
4139 print 'This page took '.
4140 '<span id="generating_time" class="time_span">'.
4141 tv_interval($t0, [ gettimeofday() ]).
4142 ' seconds </span>'.
4143 ' and '.
4144 '<span id="generating_cmd">'.
4145 $number_of_git_cmds.
4146 '</span> git commands '.
4147 " to generate.\n";
4148 print "</div>\n"; # class="page_footer"
4149 }
4150
4151 if (defined $site_footer && -f $site_footer) {
4152 insert_file($site_footer);
4153 }
4154
4155 print qq!<script type="text/javascript" src="!.esc_url($javascript).qq!"></script>\n!;
4156 if (defined $action &&
4157 $action eq 'blame_incremental') {
4158 print qq!<script type="text/javascript">\n!.
4159 qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.
4160 qq! "!. href() .qq!");\n!.
4161 qq!</script>\n!;
4162 } else {
4163 my ($jstimezone, $tz_cookie, $datetime_class) =
4164 gitweb_get_feature('javascript-timezone');
4165
4166 print qq!<script type="text/javascript">\n!.
4167 qq!window.onload = function () {\n!;
4168 if (gitweb_check_feature('javascript-actions')) {
4169 print qq! fixLinks();\n!;
4170 }
4171 if ($jstimezone && $tz_cookie && $datetime_class) {
4172 print qq! var tz_cookie = { name: '$tz_cookie', expires: 14, path: '/' };\n!. # in days
4173 qq! onloadTZSetup('$jstimezone', tz_cookie, '$datetime_class');\n!;
4174 }
4175 print qq!};\n!.
4176 qq!</script>\n!;
4177 }
4178
4179 print "</body>\n" .
4180 "</html>";
4181 }
4182
4183 # die_error(<http_status_code>, <error_message>[, <detailed_html_description>])
4184 # Example: die_error(404, 'Hash not found')
4185 # By convention, use the following status codes (as defined in RFC 2616):
4186 # 400: Invalid or missing CGI parameters, or
4187 # requested object exists but has wrong type.
4188 # 403: Requested feature (like "pickaxe" or "snapshot") not enabled on
4189 # this server or project.
4190 # 404: Requested object/revision/project doesn't exist.
4191 # 500: The server isn't configured properly, or
4192 # an internal error occurred (e.g. failed assertions caused by bugs), or
4193 # an unknown error occurred (e.g. the git binary died unexpectedly).
4194 # 503: The server is currently unavailable (because it is overloaded,
4195 # or down for maintenance). Generally, this is a temporary state.
4196 sub die_error {
4197 my $status = shift || 500;
4198 my $error = esc_html(shift) || "Internal Server Error";
4199 my $extra = shift;
4200 my %opts = @_;
4201
4202 my %http_responses = (
4203 400 => '400 Bad Request',
4204 403 => '403 Forbidden',
4205 404 => '404 Not Found',
4206 500 => '500 Internal Server Error',
4207 503 => '503 Service Unavailable',
4208 );
4209 git_header_html($http_responses{$status}, undef, %opts);
4210 print <<EOF;
4211 <div class="page_body">
4212 <br /><br />
4213 $status - $error
4214 <br />
4215 EOF
4216 if (defined $extra) {
4217 print "<hr />\n" .
4218 "$extra\n";
4219 }
4220 print "</div>\n";
4221
4222 git_footer_html();
4223 goto DONE_GITWEB
4224 unless ($opts{'-error_handler'});
4225 }
4226
4227 ## ----------------------------------------------------------------------
4228 ## functions printing or outputting HTML: navigation
4229
4230 sub git_print_page_nav {
4231 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
4232 $extra = '' if !defined $extra; # pager or formats
4233
4234 my @navs = qw(summary shortlog log commit commitdiff tree);
4235 if ($suppress) {
4236 @navs = grep { $_ ne $suppress } @navs;
4237 }
4238
4239 my %arg = map { $_ => {action=>$_} } @navs;
4240 if (defined $head) {
4241 for (qw(commit commitdiff)) {
4242 $arg{$_}{'hash'} = $head;
4243 }
4244 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
4245 for (qw(shortlog log)) {
4246 $arg{$_}{'hash'} = $head;
4247 }
4248 }
4249 }
4250
4251 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
4252 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
4253
4254 my @actions = gitweb_get_feature('actions');
4255 my %repl = (
4256 '%' => '%',
4257 'n' => $project, # project name
4258 'f' => $git_dir, # project path within filesystem
4259 'h' => $treehead || '', # current hash ('h' parameter)
4260 'b' => $treebase || '', # hash base ('hb' parameter)
4261 );
4262 while (@actions) {
4263 my ($label, $link, $pos) = splice(@actions,0,3);
4264 # insert
4265 @navs = map { $_ eq $pos ? ($_, $label) : $_ } @navs;
4266 # munch munch
4267 $link =~ s/%([%nfhb])/$repl{$1}/g;
4268 $arg{$label}{'_href'} = $link;
4269 }
4270
4271 print "<div class=\"page_nav\">\n" .
4272 (join " | ",
4273 map { $_ eq $current ?
4274 $_ : $cgi->a({-href => ($arg{$_}{_href} ? $arg{$_}{_href} : href(%{$arg{$_}}))}, "$_")
4275 } @navs);
4276 print "<br/>\n$extra<br/>\n" .
4277 "</div>\n";
4278 }
4279
4280 # returns a submenu for the nagivation of the refs views (tags, heads,
4281 # remotes) with the current view disabled and the remotes view only
4282 # available if the feature is enabled
4283 sub format_ref_views {
4284 my ($current) = @_;
4285 my @ref_views = qw{tags heads};
4286 push @ref_views, 'remotes' if gitweb_check_feature('remote_heads');
4287 return join " | ", map {
4288 $_ eq $current ? $_ :
4289 $cgi->a({-href => href(action=>$_)}, $_)
4290 } @ref_views
4291 }
4292
4293 sub format_paging_nav {
4294 my ($action, $page, $has_next_link) = @_;
4295 my $paging_nav;
4296
4297
4298 if ($page > 0) {
4299 $paging_nav .=
4300 $cgi->a({-href => href(-replay=>1, page=>undef)}, "first") .
4301 " &sdot; " .
4302 $cgi->a({-href => href(-replay=>1, page=>$page-1),
4303 -accesskey => "p", -title => "Alt-p"}, "prev");
4304 } else {
4305 $paging_nav .= "first &sdot; prev";
4306 }
4307
4308 if ($has_next_link) {
4309 $paging_nav .= " &sdot; " .
4310 $cgi->a({-href => href(-replay=>1, page=>$page+1),
4311 -accesskey => "n", -title => "Alt-n"}, "next");
4312 } else {
4313 $paging_nav .= " &sdot; next";
4314 }
4315
4316 return $paging_nav;
4317 }
4318
4319 ## ......................................................................
4320 ## functions printing or outputting HTML: div
4321
4322 sub git_print_header_div {
4323 my ($action, $title, $hash, $hash_base) = @_;
4324 my %args = ();
4325
4326 $args{'action'} = $action;
4327 $args{'hash'} = $hash if $hash;
4328 $args{'hash_base'} = $hash_base if $hash_base;
4329
4330 print "<div class=\"header\">\n" .
4331 $cgi->a({-href => href(%args), -class => "title"},
4332 $title ? $title : $action) .
4333 "\n</div>\n";
4334 }
4335
4336 sub format_repo_url {
4337 my ($name, $url) = @_;
4338 return "<tr class=\"metadata_url\"><td>$name</td><td>$url</td></tr>\n";
4339 }
4340
4341 # Group output by placing it in a DIV element and adding a header.
4342 # Options for start_div() can be provided by passing a hash reference as the
4343 # first parameter to the function.
4344 # Options to git_print_header_div() can be provided by passing an array
4345 # reference. This must follow the options to start_div if they are present.
4346 # The content can be a scalar, which is output as-is, a scalar reference, which
4347 # is output after html escaping, an IO handle passed either as *handle or
4348 # *handle{IO}, or a function reference. In the latter case all following
4349 # parameters will be taken as argument to the content function call.
4350 sub git_print_section {
4351 my ($div_args, $header_args, $content);
4352 my $arg = shift;
4353 if (ref($arg) eq 'HASH') {
4354 $div_args = $arg;
4355 $arg = shift;
4356 }
4357 if (ref($arg) eq 'ARRAY') {
4358 $header_args = $arg;
4359 $arg = shift;
4360 }
4361 $content = $arg;
4362
4363 print $cgi->start_div($div_args);
4364 git_print_header_div(@$header_args);
4365
4366 if (ref($content) eq 'CODE') {
4367 $content->(@_);
4368 } elsif (ref($content) eq 'SCALAR') {
4369 print esc_html($$content);
4370 } elsif (ref($content) eq 'GLOB' or ref($content) eq 'IO::Handle') {
4371 print <$content>;
4372 } elsif (!ref($content) && defined($content)) {
4373 print $content;
4374 }
4375
4376 print $cgi->end_div;
4377 }
4378
4379 sub format_timestamp_html {
4380 my $date = shift;
4381 my $strtime = $date->{'rfc2822'};
4382
4383 my (undef, undef, $datetime_class) =
4384 gitweb_get_feature('javascript-timezone');
4385 if ($datetime_class) {
4386 $strtime = qq!<span class="$datetime_class">$strtime</span>!;
4387 }
4388
4389 my $localtime_format = '(%02d:%02d %s)';
4390 if ($date->{'hour_local'} < 6) {
4391 $localtime_format = '(<span class="atnight">%02d:%02d</span> %s)';
4392 }
4393 $strtime .= ' ' .
4394 sprintf($localtime_format,
4395 $date->{'hour_local'}, $date->{'minute_local'}, $date->{'tz_local'});
4396
4397 return $strtime;
4398 }
4399
4400 # Outputs the author name and date in long form
4401 sub git_print_authorship {
4402 my $co = shift;
4403 my %opts = @_;
4404 my $tag = $opts{-tag} || 'div';
4405 my $author = $co->{'author_name'};
4406
4407 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
4408 print "<$tag class=\"author_date\">" .
4409 format_search_author($author, "author", esc_html($author)) .
4410 " [".format_timestamp_html(\%ad)."]".
4411 git_get_avatar($co->{'author_email'}, -pad_before => 1) .
4412 "</$tag>\n";
4413 }
4414
4415 # Outputs table rows containing the full author or committer information,
4416 # in the format expected for 'commit' view (& similar).
4417 # Parameters are a commit hash reference, followed by the list of people
4418 # to output information for. If the list is empty it defaults to both
4419 # author and committer.
4420 sub git_print_authorship_rows {
4421 my $co = shift;
4422 # too bad we can't use @people = @_ || ('author', 'committer')
4423 my @people = @_;
4424 @people = ('author', 'committer') unless @people;
4425 foreach my $who (@people) {
4426 my %wd = parse_date($co->{"${who}_epoch"}, $co->{"${who}_tz"});
4427 print "<tr><td>$who</td><td>" .
4428 format_search_author($co->{"${who}_name"}, $who,
4429 esc_html($co->{"${who}_name"})) . " " .
4430 format_search_author($co->{"${who}_email"}, $who,
4431 esc_html("<" . $co->{"${who}_email"} . ">")) .
4432 "</td><td rowspan=\"2\">" .
4433 git_get_avatar($co->{"${who}_email"}, -size => 'double') .
4434 "</td></tr>\n" .
4435 "<tr>" .
4436 "<td></td><td>" .
4437 format_timestamp_html(\%wd) .
4438 "</td>" .
4439 "</tr>\n";
4440 }
4441 }
4442
4443 sub git_print_page_path {
4444 my $name = shift;
4445 my $type = shift;
4446 my $hb = shift;
4447
4448
4449 print "<div class=\"page_path\">";
4450 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
4451 -title => 'tree root'}, to_utf8("[$project]"));
4452 print " / ";
4453 if (defined $name) {
4454 my @dirname = split '/', $name;
4455 my $basename = pop @dirname;
4456 my $fullname = '';
4457
4458 foreach my $dir (@dirname) {
4459 $fullname .= ($fullname ? '/' : '') . $dir;
4460 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
4461 hash_base=>$hb),
4462 -title => $fullname}, esc_path($dir));
4463 print " / ";
4464 }
4465 if (defined $type && $type eq 'blob') {
4466 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
4467 hash_base=>$hb),
4468 -title => $name}, esc_path($basename));
4469 } elsif (defined $type && $type eq 'tree') {
4470 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
4471 hash_base=>$hb),
4472 -title => $name}, esc_path($basename));
4473 print " / ";
4474 } else {
4475 print esc_path($basename);
4476 }
4477 }
4478 print "<br/></div>\n";
4479 }
4480
4481 sub git_print_log {
4482 my $log = shift;
4483 my %opts = @_;
4484
4485 if ($opts{'-remove_title'}) {
4486 # remove title, i.e. first line of log
4487 shift @$log;
4488 }
4489 # remove leading empty lines
4490 while (defined $log->[0] && $log->[0] eq "") {
4491 shift @$log;
4492 }
4493
4494 # print log
4495 my $skip_blank_line = 0;
4496 foreach my $line (@$log) {
4497 if ($line =~ m/^\s*([A-Z][-A-Za-z]*-[Bb]y|C[Cc]): /) {
4498 if (! $opts{'-remove_signoff'}) {
4499 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
4500 $skip_blank_line = 1;
4501 }
4502 next;
4503 }
4504
4505 if ($line =~ m,\s*([a-z]*link): (https?://\S+),i) {
4506 if (! $opts{'-remove_signoff'}) {
4507 print "<span class=\"signoff\">" . esc_html($1) . ": " .
4508 "<a href=\"" . esc_html($2) . "\">" . esc_html($2) . "</a>" .
4509 "</span><br/>\n";
4510 $skip_blank_line = 1;
4511 }
4512 next;
4513 }
4514
4515 # print only one empty line
4516 # do not print empty line after signoff
4517 if ($line eq "") {
4518 next if ($skip_blank_line);
4519 $skip_blank_line = 1;
4520 } else {
4521 $skip_blank_line = 0;
4522 }
4523
4524 print format_log_line_html($line) . "<br/>\n";
4525 }
4526
4527 if ($opts{'-final_empty_line'}) {
4528 # end with single empty line
4529 print "<br/>\n" unless $skip_blank_line;
4530 }
4531 }
4532
4533 # return link target (what link points to)
4534 sub git_get_link_target {
4535 my $hash = shift;
4536 my $link_target;
4537
4538 # read link
4539 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4540 or return;
4541 {
4542 local $/ = undef;
4543 $link_target = <$fd>;
4544 }
4545 close $fd
4546 or return;
4547
4548 return $link_target;
4549 }
4550
4551 # given link target, and the directory (basedir) the link is in,
4552 # return target of link relative to top directory (top tree);
4553 # return undef if it is not possible (including absolute links).
4554 sub normalize_link_target {
4555 my ($link_target, $basedir) = @_;
4556
4557 # absolute symlinks (beginning with '/') cannot be normalized
4558 return if (substr($link_target, 0, 1) eq '/');
4559
4560 # normalize link target to path from top (root) tree (dir)
4561 my $path;
4562 if ($basedir) {
4563 $path = $basedir . '/' . $link_target;
4564 } else {
4565 # we are in top (root) tree (dir)
4566 $path = $link_target;
4567 }
4568
4569 # remove //, /./, and /../
4570 my @path_parts;
4571 foreach my $part (split('/', $path)) {
4572 # discard '.' and ''
4573 next if (!$part || $part eq '.');
4574 # handle '..'
4575 if ($part eq '..') {
4576 if (@path_parts) {
4577 pop @path_parts;
4578 } else {
4579 # link leads outside repository (outside top dir)
4580 return;
4581 }
4582 } else {
4583 push @path_parts, $part;
4584 }
4585 }
4586 $path = join('/', @path_parts);
4587
4588 return $path;
4589 }
4590
4591 # print tree entry (row of git_tree), but without encompassing <tr> element
4592 sub git_print_tree_entry {
4593 my ($t, $basedir, $hash_base, $have_blame) = @_;
4594
4595 my %base_key = ();
4596 $base_key{'hash_base'} = $hash_base if defined $hash_base;
4597
4598 # The format of a table row is: mode list link. Where mode is
4599 # the mode of the entry, list is the name of the entry, an href,
4600 # and link is the action links of the entry.
4601
4602 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
4603 if (exists $t->{'size'}) {
4604 print "<td class=\"size\">$t->{'size'}</td>\n";
4605 }
4606 if ($t->{'type'} eq "blob") {
4607 print "<td class=\"list\">" .
4608 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
4609 file_name=>"$basedir$t->{'name'}", %base_key),
4610 -class => "list"}, esc_path($t->{'name'}));
4611 if (S_ISLNK(oct $t->{'mode'})) {
4612 my $link_target = git_get_link_target($t->{'hash'});
4613 if ($link_target) {
4614 my $norm_target = normalize_link_target($link_target, $basedir);
4615 if (defined $norm_target) {
4616 print " -> " .
4617 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
4618 file_name=>$norm_target),
4619 -title => $norm_target}, esc_path($link_target));
4620 } else {
4621 print " -> " . esc_path($link_target);
4622 }
4623 }
4624 }
4625 print "</td>\n";
4626 print "<td class=\"link\">";
4627 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
4628 file_name=>"$basedir$t->{'name'}", %base_key)},
4629 "blob");
4630 if ($have_blame) {
4631 print " | " .
4632 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
4633 file_name=>"$basedir$t->{'name'}", %base_key)},
4634 "blame");
4635 }
4636 if (defined $hash_base) {
4637 print " | " .
4638 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
4639 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
4640 "history");
4641 }
4642 print " | " .
4643 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
4644 file_name=>"$basedir$t->{'name'}")},
4645 "raw");
4646 print "</td>\n";
4647
4648 } elsif ($t->{'type'} eq "tree") {
4649 print "<td class=\"list\">";
4650 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
4651 file_name=>"$basedir$t->{'name'}",
4652 %base_key)},
4653 esc_path($t->{'name'}));
4654 print "</td>\n";
4655 print "<td class=\"link\">";
4656 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
4657 file_name=>"$basedir$t->{'name'}",
4658 %base_key)},
4659 "tree");
4660 if (defined $hash_base) {
4661 print " | " .
4662 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
4663 file_name=>"$basedir$t->{'name'}")},
4664 "history");
4665 }
4666 print "</td>\n";
4667 } else {
4668 # unknown object: we can only present history for it
4669 # (this includes 'commit' object, i.e. submodule support)
4670 print "<td class=\"list\">" .
4671 esc_path($t->{'name'}) .
4672 "</td>\n";
4673 print "<td class=\"link\">";
4674 if (defined $hash_base) {
4675 print $cgi->a({-href => href(action=>"history",
4676 hash_base=>$hash_base,
4677 file_name=>"$basedir$t->{'name'}")},
4678 "history");
4679 }
4680 print "</td>\n";
4681 }
4682 }
4683
4684 ## ......................................................................
4685 ## functions printing large fragments of HTML
4686
4687 # get pre-image filenames for merge (combined) diff
4688 sub fill_from_file_info {
4689 my ($diff, @parents) = @_;
4690
4691 $diff->{'from_file'} = [ ];
4692 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
4693 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
4694 if ($diff->{'status'}[$i] eq 'R' ||
4695 $diff->{'status'}[$i] eq 'C') {
4696 $diff->{'from_file'}[$i] =
4697 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
4698 }
4699 }
4700
4701 return $diff;
4702 }
4703
4704 # is current raw difftree line of file deletion
4705 sub is_deleted {
4706 my $diffinfo = shift;
4707
4708 return $diffinfo->{'to_id'} eq ('0' x 40);
4709 }
4710
4711 # does patch correspond to [previous] difftree raw line
4712 # $diffinfo - hashref of parsed raw diff format
4713 # $patchinfo - hashref of parsed patch diff format
4714 # (the same keys as in $diffinfo)
4715 sub is_patch_split {
4716 my ($diffinfo, $patchinfo) = @_;
4717
4718 return defined $diffinfo && defined $patchinfo
4719 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
4720 }
4721
4722
4723 sub git_difftree_body {
4724 my ($difftree, $hash, @parents) = @_;
4725 my ($parent) = $parents[0];
4726 my $have_blame = gitweb_check_feature('blame');
4727 print "<div class=\"list_head\">\n";
4728 if ($#{$difftree} > 10) {
4729 print(($#{$difftree} + 1) . " files changed:\n");
4730 }
4731 print "</div>\n";
4732
4733 print "<table class=\"" .
4734 (@parents > 1 ? "combined " : "") .
4735 "diff_tree\">\n";
4736
4737 # header only for combined diff in 'commitdiff' view
4738 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
4739 if ($has_header) {
4740 # table header
4741 print "<thead><tr>\n" .
4742 "<th></th><th></th>\n"; # filename, patchN link
4743 for (my $i = 0; $i < @parents; $i++) {
4744 my $par = $parents[$i];
4745 print "<th>" .
4746 $cgi->a({-href => href(action=>"commitdiff",
4747 hash=>$hash, hash_parent=>$par),
4748 -title => 'commitdiff to parent number ' .
4749 ($i+1) . ': ' . substr($par,0,7)},
4750 $i+1) .
4751 "&nbsp;</th>\n";
4752 }
4753 print "</tr></thead>\n<tbody>\n";
4754 }
4755
4756 my $alternate = 1;
4757 my $patchno = 0;
4758 foreach my $line (@{$difftree}) {
4759 my $diff = parsed_difftree_line($line);
4760
4761 if ($alternate) {
4762 print "<tr class=\"dark\">\n";
4763 } else {
4764 print "<tr class=\"light\">\n";
4765 }
4766 $alternate ^= 1;
4767
4768 if (exists $diff->{'nparents'}) { # combined diff
4769
4770 fill_from_file_info($diff, @parents)
4771 unless exists $diff->{'from_file'};
4772
4773 if (!is_deleted($diff)) {
4774 # file exists in the result (child) commit
4775 print "<td>" .
4776 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4777 file_name=>$diff->{'to_file'},
4778 hash_base=>$hash),
4779 -class => "list"}, esc_path($diff->{'to_file'})) .
4780 "</td>\n";
4781 } else {
4782 print "<td>" .
4783 esc_path($diff->{'to_file'}) .
4784 "</td>\n";
4785 }
4786
4787 if ($action eq 'commitdiff') {
4788 # link to patch
4789 $patchno++;
4790 print "<td class=\"link\">" .
4791 $cgi->a({-href => href(-anchor=>"patch$patchno")},
4792 "patch") .
4793 " | " .
4794 "</td>\n";
4795 }
4796
4797 my $has_history = 0;
4798 my $not_deleted = 0;
4799 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
4800 my $hash_parent = $parents[$i];
4801 my $from_hash = $diff->{'from_id'}[$i];
4802 my $from_path = $diff->{'from_file'}[$i];
4803 my $status = $diff->{'status'}[$i];
4804
4805 $has_history ||= ($status ne 'A');
4806 $not_deleted ||= ($status ne 'D');
4807
4808 if ($status eq 'A') {
4809 print "<td class=\"link\" align=\"right\"> | </td>\n";
4810 } elsif ($status eq 'D') {
4811 print "<td class=\"link\">" .
4812 $cgi->a({-href => href(action=>"blob",
4813 hash_base=>$hash,
4814 hash=>$from_hash,
4815 file_name=>$from_path)},
4816 "blob" . ($i+1)) .
4817 " | </td>\n";
4818 } else {
4819 if ($diff->{'to_id'} eq $from_hash) {
4820 print "<td class=\"link nochange\">";
4821 } else {
4822 print "<td class=\"link\">";
4823 }
4824 print $cgi->a({-href => href(action=>"blobdiff",
4825 hash=>$diff->{'to_id'},
4826 hash_parent=>$from_hash,
4827 hash_base=>$hash,
4828 hash_parent_base=>$hash_parent,
4829 file_name=>$diff->{'to_file'},
4830 file_parent=>$from_path)},
4831 "diff" . ($i+1)) .
4832 " | </td>\n";
4833 }
4834 }
4835
4836 print "<td class=\"link\">";
4837 if ($not_deleted) {
4838 print $cgi->a({-href => href(action=>"blob",
4839 hash=>$diff->{'to_id'},
4840 file_name=>$diff->{'to_file'},
4841 hash_base=>$hash)},
4842 "blob");
4843 print " | " if ($has_history);
4844 }
4845 if ($has_history) {
4846 print $cgi->a({-href => href(action=>"history",
4847 file_name=>$diff->{'to_file'},
4848 hash_base=>$hash)},
4849 "history");
4850 }
4851 print "</td>\n";
4852
4853 print "</tr>\n";
4854 next; # instead of 'else' clause, to avoid extra indent
4855 }
4856 # else ordinary diff
4857
4858 my ($to_mode_oct, $to_mode_str, $to_file_type);
4859 my ($from_mode_oct, $from_mode_str, $from_file_type);
4860 if ($diff->{'to_mode'} ne ('0' x 6)) {
4861 $to_mode_oct = oct $diff->{'to_mode'};
4862 if (S_ISREG($to_mode_oct)) { # only for regular file
4863 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
4864 }
4865 $to_file_type = file_type($diff->{'to_mode'});
4866 }
4867 if ($diff->{'from_mode'} ne ('0' x 6)) {
4868 $from_mode_oct = oct $diff->{'from_mode'};
4869 if (S_ISREG($from_mode_oct)) { # only for regular file
4870 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
4871 }
4872 $from_file_type = file_type($diff->{'from_mode'});
4873 }
4874
4875 if ($diff->{'status'} eq "A") { # created
4876 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
4877 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
4878 $mode_chng .= "]</span>";
4879 print "<td>";
4880 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4881 hash_base=>$hash, file_name=>$diff->{'file'}),
4882 -class => "list"}, esc_path($diff->{'file'}));
4883 print "</td>\n";
4884 print "<td>$mode_chng</td>\n";
4885 print "<td class=\"link\">";
4886 if ($action eq 'commitdiff') {
4887 # link to patch
4888 $patchno++;
4889 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
4890 "patch") .
4891 " | ";
4892 }
4893 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4894 hash_base=>$hash, file_name=>$diff->{'file'})},
4895 "blob");
4896 print "</td>\n";
4897
4898 } elsif ($diff->{'status'} eq "D") { # deleted
4899 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
4900 print "<td>";
4901 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
4902 hash_base=>$parent, file_name=>$diff->{'file'}),
4903 -class => "list"}, esc_path($diff->{'file'}));
4904 print "</td>\n";
4905 print "<td>$mode_chng</td>\n";
4906 print "<td class=\"link\">";
4907 if ($action eq 'commitdiff') {
4908 # link to patch
4909 $patchno++;
4910 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
4911 "patch") .
4912 " | ";
4913 }
4914 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
4915 hash_base=>$parent, file_name=>$diff->{'file'})},
4916 "blob") . " | ";
4917 if ($have_blame) {
4918 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
4919 file_name=>$diff->{'file'})},
4920 "blame") . " | ";
4921 }
4922 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
4923 file_name=>$diff->{'file'})},
4924 "history");
4925 print "</td>\n";
4926
4927 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
4928 my $mode_chnge = "";
4929 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
4930 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
4931 if ($from_file_type ne $to_file_type) {
4932 $mode_chnge .= " from $from_file_type to $to_file_type";
4933 }
4934 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
4935 if ($from_mode_str && $to_mode_str) {
4936 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
4937 } elsif ($to_mode_str) {
4938 $mode_chnge .= " mode: $to_mode_str";
4939 }
4940 }
4941 $mode_chnge .= "]</span>\n";
4942 }
4943 print "<td>";
4944 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4945 hash_base=>$hash, file_name=>$diff->{'file'}),
4946 -class => "list"}, esc_path($diff->{'file'}));
4947 print "</td>\n";
4948 print "<td>$mode_chnge</td>\n";
4949 print "<td class=\"link\">";
4950 if ($action eq 'commitdiff') {
4951 # link to patch
4952 $patchno++;
4953 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
4954 "patch") .
4955 " | ";
4956 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
4957 # "commit" view and modified file (not onlu mode changed)
4958 print $cgi->a({-href => href(action=>"blobdiff",
4959 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
4960 hash_base=>$hash, hash_parent_base=>$parent,
4961 file_name=>$diff->{'file'})},
4962 "diff") .
4963 " | ";
4964 }
4965 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4966 hash_base=>$hash, file_name=>$diff->{'file'})},
4967 "blob") . " | ";
4968 if ($have_blame) {
4969 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
4970 file_name=>$diff->{'file'})},
4971 "blame") . " | ";
4972 }
4973 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
4974 file_name=>$diff->{'file'})},
4975 "history");
4976 print "</td>\n";
4977
4978 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
4979 my %status_name = ('R' => 'moved', 'C' => 'copied');
4980 my $nstatus = $status_name{$diff->{'status'}};
4981 my $mode_chng = "";
4982 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
4983 # mode also for directories, so we cannot use $to_mode_str
4984 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
4985 }
4986 print "<td>" .
4987 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
4988 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
4989 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
4990 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
4991 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
4992 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
4993 -class => "list"}, esc_path($diff->{'from_file'})) .
4994 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
4995 "<td class=\"link\">";
4996 if ($action eq 'commitdiff') {
4997 # link to patch
4998 $patchno++;
4999 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
5000 "patch") .
5001 " | ";
5002 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
5003 # "commit" view and modified file (not only pure rename or copy)
5004 print $cgi->a({-href => href(action=>"blobdiff",
5005 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
5006 hash_base=>$hash, hash_parent_base=>$parent,
5007 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
5008 "diff") .
5009 " | ";
5010 }
5011 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
5012 hash_base=>$parent, file_name=>$diff->{'to_file'})},
5013 "blob") . " | ";
5014 if ($have_blame) {
5015 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
5016 file_name=>$diff->{'to_file'})},
5017 "blame") . " | ";
5018 }
5019 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
5020 file_name=>$diff->{'to_file'})},
5021 "history");
5022 print "</td>\n";
5023
5024 } # we should not encounter Unmerged (U) or Unknown (X) status
5025 print "</tr>\n";
5026 }
5027 print "</tbody>" if $has_header;
5028 print "</table>\n";
5029 }
5030
5031 # Print context lines and then rem/add lines in a side-by-side manner.
5032 sub print_sidebyside_diff_lines {
5033 my ($ctx, $rem, $add) = @_;
5034
5035 # print context block before add/rem block
5036 if (@$ctx) {
5037 print join '',
5038 '<div class="chunk_block ctx">',
5039 '<div class="old">',
5040 @$ctx,
5041 '</div>',
5042 '<div class="new">',
5043 @$ctx,
5044 '</div>',
5045 '</div>';
5046 }
5047
5048 if (!@$add) {
5049 # pure removal
5050 print join '',
5051 '<div class="chunk_block rem">',
5052 '<div class="old">',
5053 @$rem,
5054 '</div>',
5055 '</div>';
5056 } elsif (!@$rem) {
5057 # pure addition
5058 print join '',
5059 '<div class="chunk_block add">',
5060 '<div class="new">',
5061 @$add,
5062 '</div>',
5063 '</div>';
5064 } else {
5065 print join '',
5066 '<div class="chunk_block chg">',
5067 '<div class="old">',
5068 @$rem,
5069 '</div>',
5070 '<div class="new">',
5071 @$add,
5072 '</div>',
5073 '</div>';
5074 }
5075 }
5076
5077 # Print context lines and then rem/add lines in inline manner.
5078 sub print_inline_diff_lines {
5079 my ($ctx, $rem, $add) = @_;
5080
5081 print @$ctx, @$rem, @$add;
5082 }
5083
5084 # Format removed and added line, mark changed part and HTML-format them.
5085 # Implementation is based on contrib/diff-highlight
5086 sub format_rem_add_lines_pair {
5087 my ($rem, $add, $num_parents) = @_;
5088
5089 # We need to untabify lines before split()'ing them;
5090 # otherwise offsets would be invalid.
5091 chomp $rem;
5092 chomp $add;
5093 $rem = untabify($rem);
5094 $add = untabify($add);
5095
5096 my @rem = split(//, $rem);
5097 my @add = split(//, $add);
5098 my ($esc_rem, $esc_add);
5099 # Ignore leading +/- characters for each parent.
5100 my ($prefix_len, $suffix_len) = ($num_parents, 0);
5101 my ($prefix_has_nonspace, $suffix_has_nonspace);
5102
5103 my $shorter = (@rem < @add) ? @rem : @add;
5104 while ($prefix_len < $shorter) {
5105 last if ($rem[$prefix_len] ne $add[$prefix_len]);
5106
5107 $prefix_has_nonspace = 1 if ($rem[$prefix_len] !~ /\s/);
5108 $prefix_len++;
5109 }
5110
5111 while ($prefix_len + $suffix_len < $shorter) {
5112 last if ($rem[-1 - $suffix_len] ne $add[-1 - $suffix_len]);
5113
5114 $suffix_has_nonspace = 1 if ($rem[-1 - $suffix_len] !~ /\s/);
5115 $suffix_len++;
5116 }
5117
5118 # Mark lines that are different from each other, but have some common
5119 # part that isn't whitespace. If lines are completely different, don't
5120 # mark them because that would make output unreadable, especially if
5121 # diff consists of multiple lines.
5122 if ($prefix_has_nonspace || $suffix_has_nonspace) {
5123 $esc_rem = esc_html_hl_regions($rem, 'marked',
5124 [$prefix_len, @rem - $suffix_len], -nbsp=>1);
5125 $esc_add = esc_html_hl_regions($add, 'marked',
5126 [$prefix_len, @add - $suffix_len], -nbsp=>1);
5127 } else {
5128 $esc_rem = esc_html($rem, -nbsp=>1);
5129 $esc_add = esc_html($add, -nbsp=>1);
5130 }
5131
5132 return format_diff_line(\$esc_rem, 'rem'),
5133 format_diff_line(\$esc_add, 'add');
5134 }
5135
5136 # HTML-format diff context, removed and added lines.
5137 sub format_ctx_rem_add_lines {
5138 my ($ctx, $rem, $add, $num_parents) = @_;
5139 my (@new_ctx, @new_rem, @new_add);
5140 my $can_highlight = 0;
5141 my $is_combined = ($num_parents > 1);
5142
5143 # Highlight if every removed line has a corresponding added line.
5144 if (@$add > 0 && @$add == @$rem) {
5145 $can_highlight = 1;
5146
5147 # Highlight lines in combined diff only if the chunk contains
5148 # diff between the same version, e.g.
5149 #
5150 # - a
5151 # - b
5152 # + c
5153 # + d
5154 #
5155 # Otherwise the highlightling would be confusing.
5156 if ($is_combined) {
5157 for (my $i = 0; $i < @$add; $i++) {
5158 my $prefix_rem = substr($rem->[$i], 0, $num_parents);
5159 my $prefix_add = substr($add->[$i], 0, $num_parents);
5160
5161 $prefix_rem =~ s/-/+/g;
5162
5163 if ($prefix_rem ne $prefix_add) {
5164 $can_highlight = 0;
5165 last;
5166 }
5167 }
5168 }
5169 }
5170
5171 if ($can_highlight) {
5172 for (my $i = 0; $i < @$add; $i++) {
5173 my ($line_rem, $line_add) = format_rem_add_lines_pair(
5174 $rem->[$i], $add->[$i], $num_parents);
5175 push @new_rem, $line_rem;
5176 push @new_add, $line_add;
5177 }
5178 } else {
5179 @new_rem = map { format_diff_line($_, 'rem') } @$rem;
5180 @new_add = map { format_diff_line($_, 'add') } @$add;
5181 }
5182
5183 @new_ctx = map { format_diff_line($_, 'ctx') } @$ctx;
5184
5185 return (\@new_ctx, \@new_rem, \@new_add);
5186 }
5187
5188 # Print context lines and then rem/add lines.
5189 sub print_diff_lines {
5190 my ($ctx, $rem, $add, $diff_style, $num_parents) = @_;
5191 my $is_combined = $num_parents > 1;
5192
5193 ($ctx, $rem, $add) = format_ctx_rem_add_lines($ctx, $rem, $add,
5194 $num_parents);
5195
5196 if ($diff_style eq 'sidebyside' && !$is_combined) {
5197 print_sidebyside_diff_lines($ctx, $rem, $add);
5198 } else {
5199 # default 'inline' style and unknown styles
5200 print_inline_diff_lines($ctx, $rem, $add);
5201 }
5202 }
5203
5204 sub print_diff_chunk {
5205 my ($diff_style, $num_parents, $from, $to, @chunk) = @_;
5206 my (@ctx, @rem, @add);
5207
5208 # The class of the previous line.
5209 my $prev_class = '';
5210
5211 return unless @chunk;
5212
5213 # incomplete last line might be among removed or added lines,
5214 # or both, or among context lines: find which
5215 for (my $i = 1; $i < @chunk; $i++) {
5216 if ($chunk[$i][0] eq 'incomplete') {
5217 $chunk[$i][0] = $chunk[$i-1][0];
5218 }
5219 }
5220
5221 # guardian
5222 push @chunk, ["", ""];
5223
5224 foreach my $line_info (@chunk) {
5225 my ($class, $line) = @$line_info;
5226
5227 # print chunk headers
5228 if ($class && $class eq 'chunk_header') {
5229 print format_diff_line($line, $class, $from, $to);
5230 next;
5231 }
5232
5233 ## print from accumulator when have some add/rem lines or end
5234 # of chunk (flush context lines), or when have add and rem
5235 # lines and new block is reached (otherwise add/rem lines could
5236 # be reordered)
5237 if (!$class || ((@rem || @add) && $class eq 'ctx') ||
5238 (@rem && @add && $class ne $prev_class)) {
5239 print_diff_lines(\@ctx, \@rem, \@add,
5240 $diff_style, $num_parents);
5241 @ctx = @rem = @add = ();
5242 }
5243
5244 ## adding lines to accumulator
5245 # guardian value
5246 last unless $line;
5247 # rem, add or change
5248 if ($class eq 'rem') {
5249 push @rem, $line;
5250 } elsif ($class eq 'add') {
5251 push @add, $line;
5252 }
5253 # context line
5254 if ($class eq 'ctx') {
5255 push @ctx, $line;
5256 }
5257
5258 $prev_class = $class;
5259 }
5260 }
5261
5262 sub git_patchset_body {
5263 my ($fd, $diff_style, $difftree, $hash, @hash_parents) = @_;
5264 my ($hash_parent) = $hash_parents[0];
5265
5266 my $is_combined = (@hash_parents > 1);
5267 my $patch_idx = 0;
5268 my $patch_number = 0;
5269 my $patch_line;
5270 my $diffinfo;
5271 my $to_name;
5272 my (%from, %to);
5273 my @chunk; # for side-by-side diff
5274
5275 print "<div class=\"patchset\">\n";
5276
5277 # skip to first patch
5278 while ($patch_line = <$fd>) {
5279 chomp $patch_line;
5280
5281 last if ($patch_line =~ m/^diff /);
5282 }
5283
5284 PATCH:
5285 while ($patch_line) {
5286
5287 # parse "git diff" header line
5288 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
5289 # $1 is from_name, which we do not use
5290 $to_name = unquote($2);
5291 $to_name =~ s!^b/!!;
5292 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
5293 # $1 is 'cc' or 'combined', which we do not use
5294 $to_name = unquote($2);
5295 } else {
5296 $to_name = undef;
5297 }
5298
5299 # check if current patch belong to current raw line
5300 # and parse raw git-diff line if needed
5301 if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
5302 # this is continuation of a split patch
5303 print "<div class=\"patch cont\">\n";
5304 } else {
5305 # advance raw git-diff output if needed
5306 $patch_idx++ if defined $diffinfo;
5307
5308 # read and prepare patch information
5309 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
5310
5311 # compact combined diff output can have some patches skipped
5312 # find which patch (using pathname of result) we are at now;
5313 if ($is_combined) {
5314 while ($to_name ne $diffinfo->{'to_file'}) {
5315 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
5316 format_diff_cc_simplified($diffinfo, @hash_parents) .
5317 "</div>\n"; # class="patch"
5318
5319 $patch_idx++;
5320 $patch_number++;
5321
5322 last if $patch_idx > $#$difftree;
5323 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
5324 }
5325 }
5326
5327 # modifies %from, %to hashes
5328 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
5329
5330 # this is first patch for raw difftree line with $patch_idx index
5331 # we index @$difftree array from 0, but number patches from 1
5332 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
5333 }
5334
5335 # git diff header
5336 #assert($patch_line =~ m/^diff /) if DEBUG;
5337 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
5338 $patch_number++;
5339 # print "git diff" header
5340 print format_git_diff_header_line($patch_line, $diffinfo,
5341 \%from, \%to);
5342
5343 # print extended diff header
5344 print "<div class=\"diff extended_header\">\n";
5345 EXTENDED_HEADER:
5346 while ($patch_line = <$fd>) {
5347 chomp $patch_line;
5348
5349 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
5350
5351 print format_extended_diff_header_line($patch_line, $diffinfo,
5352 \%from, \%to);
5353 }
5354 print "</div>\n"; # class="diff extended_header"
5355
5356 # from-file/to-file diff header
5357 if (! $patch_line) {
5358 print "</div>\n"; # class="patch"
5359 last PATCH;
5360 }
5361 next PATCH if ($patch_line =~ m/^diff /);
5362 #assert($patch_line =~ m/^---/) if DEBUG;
5363
5364 my $last_patch_line = $patch_line;
5365 $patch_line = <$fd>;
5366 chomp $patch_line;
5367 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
5368
5369 print format_diff_from_to_header($last_patch_line, $patch_line,
5370 $diffinfo, \%from, \%to,
5371 @hash_parents);
5372
5373 # the patch itself
5374 LINE:
5375 while ($patch_line = <$fd>) {
5376 chomp $patch_line;
5377
5378 next PATCH if ($patch_line =~ m/^diff /);
5379
5380 my $class = diff_line_class($patch_line, \%from, \%to);
5381
5382 if ($class eq 'chunk_header') {
5383 print_diff_chunk($diff_style, scalar @hash_parents, \%from, \%to, @chunk);
5384 @chunk = ();
5385 }
5386
5387 push @chunk, [ $class, $patch_line ];
5388 }
5389
5390 } continue {
5391 if (@chunk) {
5392 print_diff_chunk($diff_style, scalar @hash_parents, \%from, \%to, @chunk);
5393 @chunk = ();
5394 }
5395 print "</div>\n"; # class="patch"
5396 }
5397
5398 # for compact combined (--cc) format, with chunk and patch simplification
5399 # the patchset might be empty, but there might be unprocessed raw lines
5400 for (++$patch_idx if $patch_number > 0;
5401 $patch_idx < @$difftree;
5402 ++$patch_idx) {
5403 # read and prepare patch information
5404 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
5405
5406 # generate anchor for "patch" links in difftree / whatchanged part
5407 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
5408 format_diff_cc_simplified($diffinfo, @hash_parents) .
5409 "</div>\n"; # class="patch"
5410
5411 $patch_number++;
5412 }
5413
5414 if ($patch_number == 0) {
5415 if (@hash_parents > 1) {
5416 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
5417 } else {
5418 print "<div class=\"diff nodifferences\">No differences found</div>\n";
5419 }
5420 }
5421
5422 print "</div>\n"; # class="patchset"
5423 }
5424
5425 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5426
5427 sub git_project_search_form {
5428 my ($searchtext, $search_use_regexp) = @_;
5429
5430 my $limit = '';
5431 if ($project_filter) {
5432 $limit = " in '$project_filter/'";
5433 }
5434
5435 print "<div class=\"projsearch\">\n";
5436 print $cgi->startform(-method => 'get', -action => $my_uri) .
5437 $cgi->hidden(-name => 'a', -value => 'project_list') . "\n";
5438 print $cgi->hidden(-name => 'pf', -value => $project_filter). "\n"
5439 if (defined $project_filter);
5440 print $cgi->textfield(-name => 's', -value => $searchtext,
5441 -title => "Search project by name and description$limit",
5442 -size => 60) . "\n" .
5443 "<span title=\"Extended regular expression\">" .
5444 $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
5445 -checked => $search_use_regexp) .
5446 "</span>\n" .
5447 $cgi->submit(-name => 'btnS', -value => 'Search') .
5448 $cgi->end_form() . "\n" .
5449 $cgi->a({-href => href(project => undef, searchtext => undef,
5450 project_filter => $project_filter)},
5451 esc_html("List all projects$limit")) . "<br />\n";
5452 print "</div>\n";
5453 }
5454
5455 # entry for given @keys needs filling if at least one of keys in list
5456 # is not present in %$project_info
5457 sub project_info_needs_filling {
5458 my ($project_info, @keys) = @_;
5459
5460 # return List::MoreUtils::any { !exists $project_info->{$_} } @keys;
5461 foreach my $key (@keys) {
5462 if (!exists $project_info->{$key}) {
5463 return 1;
5464 }
5465 }
5466 return;
5467 }
5468
5469 # fills project list info (age, description, owner, category, forks, etc.)
5470 # for each project in the list, removing invalid projects from
5471 # returned list, or fill only specified info.
5472 #
5473 # Invalid projects are removed from the returned list if and only if you
5474 # ask 'age' or 'age_string' to be filled, because they are the only fields
5475 # that run unconditionally git command that requires repository, and
5476 # therefore do always check if project repository is invalid.
5477 #
5478 # USAGE:
5479 # * fill_project_list_info(\@project_list, 'descr_long', 'ctags')
5480 # ensures that 'descr_long' and 'ctags' fields are filled
5481 # * @project_list = fill_project_list_info(\@project_list)
5482 # ensures that all fields are filled (and invalid projects removed)
5483 #
5484 # NOTE: modifies $projlist, but does not remove entries from it
5485 sub fill_project_list_info {
5486 my ($projlist, @wanted_keys) = @_;
5487 my @projects;
5488 my $filter_set = sub { return @_; };
5489 if (@wanted_keys) {
5490 my %wanted_keys = map { $_ => 1 } @wanted_keys;
5491 $filter_set = sub { return grep { $wanted_keys{$_} } @_; };
5492 }
5493
5494 my $show_ctags = gitweb_check_feature('ctags');
5495 PROJECT:
5496 foreach my $pr (@$projlist) {
5497 if (project_info_needs_filling($pr, $filter_set->('age', 'age_string'))) {
5498 my (@activity) = git_get_last_activity($pr->{'path'});
5499 unless (@activity) {
5500 next PROJECT;
5501 }
5502 ($pr->{'age'}, $pr->{'age_string'}) = @activity;
5503 }
5504 if (project_info_needs_filling($pr, $filter_set->('descr', 'descr_long'))) {
5505 my $descr = git_get_project_description($pr->{'path'}) || "";
5506 $descr = to_utf8($descr);
5507 $pr->{'descr_long'} = $descr;
5508 $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
5509 }
5510 if (project_info_needs_filling($pr, $filter_set->('owner'))) {
5511 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
5512 }
5513 if ($show_ctags &&
5514 project_info_needs_filling($pr, $filter_set->('ctags'))) {
5515 $pr->{'ctags'} = git_get_project_ctags($pr->{'path'});
5516 }
5517 if ($projects_list_group_categories &&
5518 project_info_needs_filling($pr, $filter_set->('category'))) {
5519 my $cat = git_get_project_category($pr->{'path'}) ||
5520 $project_list_default_category;
5521 $pr->{'category'} = to_utf8($cat);
5522 }
5523
5524 push @projects, $pr;
5525 }
5526
5527 return @projects;
5528 }
5529
5530 sub sort_projects_list {
5531 my ($projlist, $order) = @_;
5532
5533 sub order_str {
5534 my $key = shift;
5535 return sub { $a->{$key} cmp $b->{$key} };
5536 }
5537
5538 sub order_num_then_undef {
5539 my $key = shift;
5540 return sub {
5541 defined $a->{$key} ?
5542 (defined $b->{$key} ? $a->{$key} <=> $b->{$key} : -1) :
5543 (defined $b->{$key} ? 1 : 0)
5544 };
5545 }
5546
5547 my %orderings = (
5548 project => order_str('path'),
5549 descr => order_str('descr_long'),
5550 owner => order_str('owner'),
5551 age => order_num_then_undef('age'),
5552 );
5553
5554 my $ordering = $orderings{$order};
5555 return defined $ordering ? sort $ordering @$projlist : @$projlist;
5556 }
5557
5558 # returns a hash of categories, containing the list of project
5559 # belonging to each category
5560 sub build_projlist_by_category {
5561 my ($projlist, $from, $to) = @_;
5562 my %categories;
5563
5564 $from = 0 unless defined $from;
5565 $to = $#$projlist if (!defined $to || $#$projlist < $to);
5566
5567 for (my $i = $from; $i <= $to; $i++) {
5568 my $pr = $projlist->[$i];
5569 push @{$categories{ $pr->{'category'} }}, $pr;
5570 }
5571
5572 return wantarray ? %categories : \%categories;
5573 }
5574
5575 # print 'sort by' <th> element, generating 'sort by $name' replay link
5576 # if that order is not selected
5577 sub print_sort_th {
5578 print format_sort_th(@_);
5579 }
5580
5581 sub format_sort_th {
5582 my ($name, $order, $header) = @_;
5583 my $sort_th = "";
5584 $header ||= ucfirst($name);
5585
5586 if ($order eq $name) {
5587 $sort_th .= "<th>$header</th>\n";
5588 } else {
5589 $sort_th .= "<th>" .
5590 $cgi->a({-href => href(-replay=>1, order=>$name),
5591 -class => "header"}, $header) .
5592 "</th>\n";
5593 }
5594
5595 return $sort_th;
5596 }
5597
5598 sub git_project_list_rows {
5599 my ($projlist, $from, $to, $check_forks) = @_;
5600
5601 $from = 0 unless defined $from;
5602 $to = $#$projlist if (!defined $to || $#$projlist < $to);
5603
5604 my $alternate = 1;
5605 for (my $i = $from; $i <= $to; $i++) {
5606 my $pr = $projlist->[$i];
5607
5608 if ($alternate) {
5609 print "<tr class=\"dark\">\n";
5610 } else {
5611 print "<tr class=\"light\">\n";
5612 }
5613 $alternate ^= 1;
5614
5615 if ($check_forks) {
5616 print "<td>";
5617 if ($pr->{'forks'}) {
5618 my $nforks = scalar @{$pr->{'forks'}};
5619 if ($nforks > 0) {
5620 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks"),
5621 -title => "$nforks forks"}, "+");
5622 } else {
5623 print $cgi->span({-title => "$nforks forks"}, "+");
5624 }
5625 }
5626 print "</td>\n";
5627 }
5628 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
5629 -class => "list"},
5630 esc_html_match_hl($pr->{'path'}, $search_regexp)) .
5631 "</td>\n" .
5632 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
5633 -class => "list",
5634 -title => $pr->{'descr_long'}},
5635 $search_regexp
5636 ? esc_html_match_hl_chopped($pr->{'descr_long'},
5637 $pr->{'descr'}, $search_regexp)
5638 : esc_html($pr->{'descr'})) .
5639 "</td>\n";
5640 unless ($omit_owner) {
5641 print "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n";
5642 }
5643 unless ($omit_age_column) {
5644 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
5645 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n";
5646 }
5647 print"<td class=\"link\">" .
5648 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
5649 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
5650 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
5651 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
5652 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
5653 "</td>\n" .
5654 "</tr>\n";
5655 }
5656 }
5657
5658 sub git_project_list_body {
5659 # actually uses global variable $project
5660 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
5661 my @projects = @$projlist;
5662
5663 my $check_forks = gitweb_check_feature('forks');
5664 my $show_ctags = gitweb_check_feature('ctags');
5665 my $tagfilter = $show_ctags ? $input_params{'ctag'} : undef;
5666 $check_forks = undef
5667 if ($tagfilter || $search_regexp);
5668
5669 # filtering out forks before filling info allows to do less work
5670 @projects = filter_forks_from_projects_list(\@projects)
5671 if ($check_forks);
5672 # search_projects_list pre-fills required info
5673 @projects = search_projects_list(\@projects,
5674 'search_regexp' => $search_regexp,
5675 'tagfilter' => $tagfilter)
5676 if ($tagfilter || $search_regexp);
5677 # fill the rest
5678 my @all_fields = ('descr', 'descr_long', 'ctags', 'category');
5679 push @all_fields, ('age', 'age_string') unless($omit_age_column);
5680 push @all_fields, 'owner' unless($omit_owner);
5681 @projects = fill_project_list_info(\@projects, @all_fields);
5682
5683 $order ||= $default_projects_order;
5684 $from = 0 unless defined $from;
5685 $to = $#projects if (!defined $to || $#projects < $to);
5686
5687 # short circuit
5688 if ($from > $to) {
5689 print "<center>\n".
5690 "<b>No such projects found</b><br />\n".
5691 "Click ".$cgi->a({-href=>href(project=>undef)},"here")." to view all projects<br />\n".
5692 "</center>\n<br />\n";
5693 return;
5694 }
5695
5696 @projects = sort_projects_list(\@projects, $order);
5697
5698 if ($show_ctags) {
5699 my $ctags = git_gather_all_ctags(\@projects);
5700 my $cloud = git_populate_project_tagcloud($ctags);
5701 print git_show_project_tagcloud($cloud, 64);
5702 }
5703
5704 print "<table class=\"project_list\">\n";
5705 unless ($no_header) {
5706 print "<tr>\n";
5707 if ($check_forks) {
5708 print "<th></th>\n";
5709 }
5710 print_sort_th('project', $order, 'Project');
5711 print_sort_th('descr', $order, 'Description');
5712 print_sort_th('owner', $order, 'Owner') unless $omit_owner;
5713 print_sort_th('age', $order, 'Last Change') unless $omit_age_column;
5714 print "<th></th>\n" . # for links
5715 "</tr>\n";
5716 }
5717
5718 if ($projects_list_group_categories) {
5719 # only display categories with projects in the $from-$to window
5720 @projects = sort {$a->{'category'} cmp $b->{'category'}} @projects[$from..$to];
5721 my %categories = build_projlist_by_category(\@projects, $from, $to);
5722 foreach my $cat (sort keys %categories) {
5723 unless ($cat eq "") {
5724 print "<tr>\n";
5725 if ($check_forks) {
5726 print "<td></td>\n";
5727 }
5728 print "<td class=\"category\" colspan=\"5\">".esc_html($cat)."</td>\n";
5729 print "</tr>\n";
5730 }
5731
5732 git_project_list_rows($categories{$cat}, undef, undef, $check_forks);
5733 }
5734 } else {
5735 git_project_list_rows(\@projects, $from, $to, $check_forks);
5736 }
5737
5738 if (defined $extra) {
5739 print "<tr>\n";
5740 if ($check_forks) {
5741 print "<td></td>\n";
5742 }
5743 print "<td colspan=\"5\">$extra</td>\n" .
5744 "</tr>\n";
5745 }
5746 print "</table>\n";
5747 }
5748
5749 sub git_log_body {
5750 # uses global variable $project
5751 my ($commitlist, $from, $to, $refs, $extra) = @_;
5752
5753 $from = 0 unless defined $from;
5754 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
5755
5756 for (my $i = 0; $i <= $to; $i++) {
5757 my %co = %{$commitlist->[$i]};
5758 next if !%co;
5759 my $commit = $co{'id'};
5760 my $ref = format_ref_marker($refs, $commit);
5761 git_print_header_div('commit',
5762 "<span class=\"age\">$co{'age_string'}</span>" .
5763 esc_html($co{'title'}) . $ref,
5764 $commit);
5765 print "<div class=\"title_text\">\n" .
5766 "<div class=\"log_link\">\n" .
5767 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
5768 " | " .
5769 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
5770 " | " .
5771 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
5772 "<br/>\n" .
5773 "</div>\n";
5774 git_print_authorship(\%co, -tag => 'span');
5775 print "<br/>\n</div>\n";
5776
5777 print "<div class=\"log_body\">\n";
5778 git_print_log($co{'comment'}, -final_empty_line=> 1);
5779 print "</div>\n";
5780 }
5781 if ($extra) {
5782 print "<div class=\"page_nav\">\n";
5783 print "$extra\n";
5784 print "</div>\n";
5785 }
5786 }
5787
5788 sub git_shortlog_body {
5789 # uses global variable $project
5790 my ($commitlist, $from, $to, $refs, $extra) = @_;
5791
5792 $from = 0 unless defined $from;
5793 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
5794
5795 print "<table class=\"shortlog\">\n";
5796 my $alternate = 1;
5797 for (my $i = $from; $i <= $to; $i++) {
5798 my %co = %{$commitlist->[$i]};
5799 my $commit = $co{'id'};
5800 my $ref = format_ref_marker($refs, $commit);
5801 if ($alternate) {
5802 print "<tr class=\"dark\">\n";
5803 } else {
5804 print "<tr class=\"light\">\n";
5805 }
5806 $alternate ^= 1;
5807 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
5808 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5809 format_author_html('td', \%co, 10) . "<td>";
5810 print format_subject_html($co{'title'}, $co{'title_short'},
5811 href(action=>"commit", hash=>$commit), $ref);
5812 print "</td>\n" .
5813 "<td class=\"link\">" .
5814 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
5815 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
5816 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
5817 my $snapshot_links = format_snapshot_links($commit);
5818 if (defined $snapshot_links) {
5819 print " | " . $snapshot_links;
5820 }
5821 print "</td>\n" .
5822 "</tr>\n";
5823 }
5824 if (defined $extra) {
5825 print "<tr>\n" .
5826 "<td colspan=\"4\">$extra</td>\n" .
5827 "</tr>\n";
5828 }
5829 print "</table>\n";
5830 }
5831
5832 sub git_history_body {
5833 # Warning: assumes constant type (blob or tree) during history
5834 my ($commitlist, $from, $to, $refs, $extra,
5835 $file_name, $file_hash, $ftype) = @_;
5836
5837 $from = 0 unless defined $from;
5838 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
5839
5840 print "<table class=\"history\">\n";
5841 my $alternate = 1;
5842 for (my $i = $from; $i <= $to; $i++) {
5843 my %co = %{$commitlist->[$i]};
5844 if (!%co) {
5845 next;
5846 }
5847 my $commit = $co{'id'};
5848
5849 my $ref = format_ref_marker($refs, $commit);
5850
5851 if ($alternate) {
5852 print "<tr class=\"dark\">\n";
5853 } else {
5854 print "<tr class=\"light\">\n";
5855 }
5856 $alternate ^= 1;
5857 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5858 # shortlog: format_author_html('td', \%co, 10)
5859 format_author_html('td', \%co, 15, 3) . "<td>";
5860 # originally git_history used chop_str($co{'title'}, 50)
5861 print format_subject_html($co{'title'}, $co{'title_short'},
5862 href(action=>"commit", hash=>$commit), $ref);
5863 print "</td>\n" .
5864 "<td class=\"link\">" .
5865 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
5866 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
5867
5868 if ($ftype eq 'blob') {
5869 my $blob_current = $file_hash;
5870 my $blob_parent = git_get_hash_by_path($commit, $file_name);
5871 if (defined $blob_current && defined $blob_parent &&
5872 $blob_current ne $blob_parent) {
5873 print " | " .
5874 $cgi->a({-href => href(action=>"blobdiff",
5875 hash=>$blob_current, hash_parent=>$blob_parent,
5876 hash_base=>$hash_base, hash_parent_base=>$commit,
5877 file_name=>$file_name)},
5878 "diff to current");
5879 }
5880 }
5881 print "</td>\n" .
5882 "</tr>\n";
5883 }
5884 if (defined $extra) {
5885 print "<tr>\n" .
5886 "<td colspan=\"4\">$extra</td>\n" .
5887 "</tr>\n";
5888 }
5889 print "</table>\n";
5890 }
5891
5892 sub git_tags_body {
5893 # uses global variable $project
5894 my ($taglist, $from, $to, $extra) = @_;
5895 $from = 0 unless defined $from;
5896 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
5897
5898 print "<table class=\"tags\">\n";
5899 my $alternate = 1;
5900 for (my $i = $from; $i <= $to; $i++) {
5901 my $entry = $taglist->[$i];
5902 my %tag = %$entry;
5903 my $comment = $tag{'subject'};
5904 my $comment_short;
5905 if (defined $comment) {
5906 $comment_short = chop_str($comment, 30, 5);
5907 }
5908 if ($alternate) {
5909 print "<tr class=\"dark\">\n";
5910 } else {
5911 print "<tr class=\"light\">\n";
5912 }
5913 $alternate ^= 1;
5914 if (defined $tag{'age'}) {
5915 print "<td><i>$tag{'age'}</i></td>\n";
5916 } else {
5917 print "<td></td>\n";
5918 }
5919 print "<td>" .
5920 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
5921 -class => "list name"}, esc_html($tag{'name'})) .
5922 "</td>\n" .
5923 "<td>";
5924 if (defined $comment) {
5925 print format_subject_html($comment, $comment_short,
5926 href(action=>"tag", hash=>$tag{'id'}));
5927 }
5928 print "</td>\n" .
5929 "<td class=\"selflink\">";
5930 if ($tag{'type'} eq "tag") {
5931 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
5932 } else {
5933 print "&nbsp;";
5934 }
5935 print "</td>\n" .
5936 "<td class=\"link\">" . " | " .
5937 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
5938 if ($tag{'reftype'} eq "commit") {
5939 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})}, "shortlog") .
5940 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})}, "log");
5941 } elsif ($tag{'reftype'} eq "blob") {
5942 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
5943 }
5944 print "</td>\n" .
5945 "</tr>";
5946 }
5947 if (defined $extra) {
5948 print "<tr>\n" .
5949 "<td colspan=\"5\">$extra</td>\n" .
5950 "</tr>\n";
5951 }
5952 print "</table>\n";
5953 }
5954
5955 sub git_heads_body {
5956 # uses global variable $project
5957 my ($headlist, $head_at, $from, $to, $extra) = @_;
5958 $from = 0 unless defined $from;
5959 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
5960
5961 print "<table class=\"heads\">\n";
5962 my $alternate = 1;
5963 for (my $i = $from; $i <= $to; $i++) {
5964 my $entry = $headlist->[$i];
5965 my %ref = %$entry;
5966 my $curr = defined $head_at && $ref{'id'} eq $head_at;
5967 if ($alternate) {
5968 print "<tr class=\"dark\">\n";
5969 } else {
5970 print "<tr class=\"light\">\n";
5971 }
5972 $alternate ^= 1;
5973 print "<td><i>$ref{'age'}</i></td>\n" .
5974 ($curr ? "<td class=\"current_head\">" : "<td>") .
5975 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),
5976 -class => "list name"},esc_html($ref{'name'})) .
5977 "</td>\n" .
5978 "<td class=\"link\">" .
5979 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})}, "shortlog") . " | " .
5980 $cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})}, "log") . " | " .
5981 $cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'fullname'})}, "tree") .
5982 "</td>\n" .
5983 "</tr>";
5984 }
5985 if (defined $extra) {
5986 print "<tr>\n" .
5987 "<td colspan=\"3\">$extra</td>\n" .
5988 "</tr>\n";
5989 }
5990 print "</table>\n";
5991 }
5992
5993 # Display a single remote block
5994 sub git_remote_block {
5995 my ($remote, $rdata, $limit, $head) = @_;
5996
5997 my $heads = $rdata->{'heads'};
5998 my $fetch = $rdata->{'fetch'};
5999 my $push = $rdata->{'push'};
6000
6001 my $urls_table = "<table class=\"projects_list\">\n" ;
6002
6003 if (defined $fetch) {
6004 if ($fetch eq $push) {
6005 $urls_table .= format_repo_url("URL", $fetch);
6006 } else {
6007 $urls_table .= format_repo_url("Fetch URL", $fetch);
6008 $urls_table .= format_repo_url("Push URL", $push) if defined $push;
6009 }
6010 } elsif (defined $push) {
6011 $urls_table .= format_repo_url("Push URL", $push);
6012 } else {
6013 $urls_table .= format_repo_url("", "No remote URL");
6014 }
6015
6016 $urls_table .= "</table>\n";
6017
6018 my $dots;
6019 if (defined $limit && $limit < @$heads) {
6020 $dots = $cgi->a({-href => href(action=>"remotes", hash=>$remote)}, "...");
6021 }
6022
6023 print $urls_table;
6024 git_heads_body($heads, $head, 0, $limit, $dots);
6025 }
6026
6027 # Display a list of remote names with the respective fetch and push URLs
6028 sub git_remotes_list {
6029 my ($remotedata, $limit) = @_;
6030 print "<table class=\"heads\">\n";
6031 my $alternate = 1;
6032 my @remotes = sort keys %$remotedata;
6033
6034 my $limited = $limit && $limit < @remotes;
6035
6036 $#remotes = $limit - 1 if $limited;
6037
6038 while (my $remote = shift @remotes) {
6039 my $rdata = $remotedata->{$remote};
6040 my $fetch = $rdata->{'fetch'};
6041 my $push = $rdata->{'push'};
6042 if ($alternate) {
6043 print "<tr class=\"dark\">\n";
6044 } else {
6045 print "<tr class=\"light\">\n";
6046 }
6047 $alternate ^= 1;
6048 print "<td>" .
6049 $cgi->a({-href=> href(action=>'remotes', hash=>$remote),
6050 -class=> "list name"},esc_html($remote)) .
6051 "</td>";
6052 print "<td class=\"link\">" .
6053 (defined $fetch ? $cgi->a({-href=> $fetch}, "fetch") : "fetch") .
6054 " | " .
6055 (defined $push ? $cgi->a({-href=> $push}, "push") : "push") .
6056 "</td>";
6057
6058 print "</tr>\n";
6059 }
6060
6061 if ($limited) {
6062 print "<tr>\n" .
6063 "<td colspan=\"3\">" .
6064 $cgi->a({-href => href(action=>"remotes")}, "...") .
6065 "</td>\n" . "</tr>\n";
6066 }
6067
6068 print "</table>";
6069 }
6070
6071 # Display remote heads grouped by remote, unless there are too many
6072 # remotes, in which case we only display the remote names
6073 sub git_remotes_body {
6074 my ($remotedata, $limit, $head) = @_;
6075 if ($limit and $limit < keys %$remotedata) {
6076 git_remotes_list($remotedata, $limit);
6077 } else {
6078 fill_remote_heads($remotedata);
6079 while (my ($remote, $rdata) = each %$remotedata) {
6080 git_print_section({-class=>"remote", -id=>$remote},
6081 ["remotes", $remote, $remote], sub {
6082 git_remote_block($remote, $rdata, $limit, $head);
6083 });
6084 }
6085 }
6086 }
6087
6088 sub git_search_message {
6089 my %co = @_;
6090
6091 my $greptype;
6092 if ($searchtype eq 'commit') {
6093 $greptype = "--grep=";
6094 } elsif ($searchtype eq 'author') {
6095 $greptype = "--author=";
6096 } elsif ($searchtype eq 'committer') {
6097 $greptype = "--committer=";
6098 }
6099 $greptype .= $searchtext;
6100 my @commitlist = parse_commits($hash, 101, (100 * $page), undef,
6101 $greptype, '--regexp-ignore-case',
6102 $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
6103
6104 my $paging_nav = '';
6105 if ($page > 0) {
6106 $paging_nav .=
6107 $cgi->a({-href => href(-replay=>1, page=>undef)},
6108 "first") .
6109 " &sdot; " .
6110 $cgi->a({-href => href(-replay=>1, page=>$page-1),
6111 -accesskey => "p", -title => "Alt-p"}, "prev");
6112 } else {
6113 $paging_nav .= "first &sdot; prev";
6114 }
6115 my $next_link = '';
6116 if ($#commitlist >= 100) {
6117 $next_link =
6118 $cgi->a({-href => href(-replay=>1, page=>$page+1),
6119 -accesskey => "n", -title => "Alt-n"}, "next");
6120 $paging_nav .= " &sdot; $next_link";
6121 } else {
6122 $paging_nav .= " &sdot; next";
6123 }
6124
6125 git_header_html();
6126
6127 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
6128 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6129 if ($page == 0 && !@commitlist) {
6130 print "<p>No match.</p>\n";
6131 } else {
6132 git_search_grep_body(\@commitlist, 0, 99, $next_link);
6133 }
6134
6135 git_footer_html();
6136 }
6137
6138 sub git_search_changes {
6139 my %co = @_;
6140
6141 local $/ = "\n";
6142 open my $fd, '-|', git_cmd(), '--no-pager', 'log', @diff_opts,
6143 '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
6144 ($search_use_regexp ? '--pickaxe-regex' : ())
6145 or die_error(500, "Open git-log failed");
6146
6147 git_header_html();
6148
6149 git_print_page_nav('','', $hash,$co{'tree'},$hash);
6150 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6151
6152 print "<table class=\"pickaxe search\">\n";
6153 my $alternate = 1;
6154 undef %co;
6155 my @files;
6156 while (my $line = <$fd>) {
6157 chomp $line;
6158 next unless $line;
6159
6160 my %set = parse_difftree_raw_line($line);
6161 if (defined $set{'commit'}) {
6162 # finish previous commit
6163 if (%co) {
6164 print "</td>\n" .
6165 "<td class=\"link\">" .
6166 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},
6167 "commit") .
6168 " | " .
6169 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'},
6170 hash_base=>$co{'id'})},
6171 "tree") .
6172 "</td>\n" .
6173 "</tr>\n";
6174 }
6175
6176 if ($alternate) {
6177 print "<tr class=\"dark\">\n";
6178 } else {
6179 print "<tr class=\"light\">\n";
6180 }
6181 $alternate ^= 1;
6182 %co = parse_commit($set{'commit'});
6183 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
6184 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
6185 "<td><i>$author</i></td>\n" .
6186 "<td>" .
6187 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
6188 -class => "list subject"},
6189 chop_and_escape_str($co{'title'}, 50) . "<br/>");
6190 } elsif (defined $set{'to_id'}) {
6191 next if ($set{'to_id'} =~ m/^0{40}$/);
6192
6193 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
6194 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),
6195 -class => "list"},
6196 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
6197 "<br/>\n";
6198 }
6199 }
6200 close $fd;
6201
6202 # finish last commit (warning: repetition!)
6203 if (%co) {
6204 print "</td>\n" .
6205 "<td class=\"link\">" .
6206 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},
6207 "commit") .
6208 " | " .
6209 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'},
6210 hash_base=>$co{'id'})},
6211 "tree") .
6212 "</td>\n" .
6213 "</tr>\n";
6214 }
6215
6216 print "</table>\n";
6217
6218 git_footer_html();
6219 }
6220
6221 sub git_search_files {
6222 my %co = @_;
6223
6224 local $/ = "\n";
6225 open my $fd, "-|", git_cmd(), 'grep', '-n', '-z',
6226 $search_use_regexp ? ('-E', '-i') : '-F',
6227 $searchtext, $co{'tree'}
6228 or die_error(500, "Open git-grep failed");
6229
6230 git_header_html();
6231
6232 git_print_page_nav('','', $hash,$co{'tree'},$hash);
6233 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6234
6235 print "<table class=\"grep_search\">\n";
6236 my $alternate = 1;
6237 my $matches = 0;
6238 my $lastfile = '';
6239 my $file_href;
6240 while (my $line = <$fd>) {
6241 chomp $line;
6242 my ($file, $lno, $ltext, $binary);
6243 last if ($matches++ > 1000);
6244 if ($line =~ /^Binary file (.+) matches$/) {
6245 $file = $1;
6246 $binary = 1;
6247 } else {
6248 ($file, $lno, $ltext) = split(/\0/, $line, 3);
6249 $file =~ s/^$co{'tree'}://;
6250 }
6251 if ($file ne $lastfile) {
6252 $lastfile and print "</td></tr>\n";
6253 if ($alternate++) {
6254 print "<tr class=\"dark\">\n";
6255 } else {
6256 print "<tr class=\"light\">\n";
6257 }
6258 $file_href = href(action=>"blob", hash_base=>$co{'id'},
6259 file_name=>$file);
6260 print "<td class=\"list\">".
6261 $cgi->a({-href => $file_href, -class => "list"}, esc_path($file));
6262 print "</td><td>\n";
6263 $lastfile = $file;
6264 }
6265 if ($binary) {
6266 print "<div class=\"binary\">Binary file</div>\n";
6267 } else {
6268 $ltext = untabify($ltext);
6269 if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
6270 $ltext = esc_html($1, -nbsp=>1);
6271 $ltext .= '<span class="match">';
6272 $ltext .= esc_html($2, -nbsp=>1);
6273 $ltext .= '</span>';
6274 $ltext .= esc_html($3, -nbsp=>1);
6275 } else {
6276 $ltext = esc_html($ltext, -nbsp=>1);
6277 }
6278 print "<div class=\"pre\">" .
6279 $cgi->a({-href => $file_href.'#l'.$lno,
6280 -class => "linenr"}, sprintf('%4i', $lno)) .
6281 ' ' . $ltext . "</div>\n";
6282 }
6283 }
6284 if ($lastfile) {
6285 print "</td></tr>\n";
6286 if ($matches > 1000) {
6287 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
6288 }
6289 } else {
6290 print "<div class=\"diff nodifferences\">No matches found</div>\n";
6291 }
6292 close $fd;
6293
6294 print "</table>\n";
6295
6296 git_footer_html();
6297 }
6298
6299 sub git_search_grep_body {
6300 my ($commitlist, $from, $to, $extra) = @_;
6301 $from = 0 unless defined $from;
6302 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
6303
6304 print "<table class=\"commit_search\">\n";
6305 my $alternate = 1;
6306 for (my $i = $from; $i <= $to; $i++) {
6307 my %co = %{$commitlist->[$i]};
6308 if (!%co) {
6309 next;
6310 }
6311 my $commit = $co{'id'};
6312 if ($alternate) {
6313 print "<tr class=\"dark\">\n";
6314 } else {
6315 print "<tr class=\"light\">\n";
6316 }
6317 $alternate ^= 1;
6318 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
6319 format_author_html('td', \%co, 15, 5) .
6320 "<td>" .
6321 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
6322 -class => "list subject"},
6323 chop_and_escape_str($co{'title'}, 50) . "<br/>");
6324 my $comment = $co{'comment'};
6325 foreach my $line (@$comment) {
6326 if ($line =~ m/^(.*?)($search_regexp)(.*)$/i) {
6327 my ($lead, $match, $trail) = ($1, $2, $3);
6328 $match = chop_str($match, 70, 5, 'center');
6329 my $contextlen = int((80 - length($match))/2);
6330 $contextlen = 30 if ($contextlen > 30);
6331 $lead = chop_str($lead, $contextlen, 10, 'left');
6332 $trail = chop_str($trail, $contextlen, 10, 'right');
6333
6334 $lead = esc_html($lead);
6335 $match = esc_html($match);
6336 $trail = esc_html($trail);
6337
6338 print "$lead<span class=\"match\">$match</span>$trail<br />";
6339 }
6340 }
6341 print "</td>\n" .
6342 "<td class=\"link\">" .
6343 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
6344 " | " .
6345 $cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})}, "commitdiff") .
6346 " | " .
6347 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
6348 print "</td>\n" .
6349 "</tr>\n";
6350 }
6351 if (defined $extra) {
6352 print "<tr>\n" .
6353 "<td colspan=\"3\">$extra</td>\n" .
6354 "</tr>\n";
6355 }
6356 print "</table>\n";
6357 }
6358
6359 ## ======================================================================
6360 ## ======================================================================
6361 ## actions
6362
6363 sub git_project_list {
6364 my $order = $input_params{'order'};
6365 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
6366 die_error(400, "Unknown order parameter");
6367 }
6368
6369 my @list = git_get_projects_list($project_filter, $strict_export);
6370 if (!@list) {
6371 die_error(404, "No projects found");
6372 }
6373
6374 git_header_html();
6375 if (defined $home_text && -f $home_text) {
6376 print "<div class=\"index_include\">\n";
6377 insert_file($home_text);
6378 print "</div>\n";
6379 }
6380
6381 git_project_search_form($searchtext, $search_use_regexp);
6382 git_project_list_body(\@list, $order);
6383 git_footer_html();
6384 }
6385
6386 sub git_forks {
6387 my $order = $input_params{'order'};
6388 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
6389 die_error(400, "Unknown order parameter");
6390 }
6391
6392 my $filter = $project;
6393 $filter =~ s/\.git$//;
6394 my @list = git_get_projects_list($filter);
6395 if (!@list) {
6396 die_error(404, "No forks found");
6397 }
6398
6399 git_header_html();
6400 git_print_page_nav('','');
6401 git_print_header_div('summary', "$project forks");
6402 git_project_list_body(\@list, $order);
6403 git_footer_html();
6404 }
6405
6406 sub git_project_index {
6407 my @projects = git_get_projects_list($project_filter, $strict_export);
6408 if (!@projects) {
6409 die_error(404, "No projects found");
6410 }
6411
6412 print $cgi->header(
6413 -type => 'text/plain',
6414 -charset => 'utf-8',
6415 -content_disposition => 'inline; filename="index.aux"');
6416
6417 foreach my $pr (@projects) {
6418 if (!exists $pr->{'owner'}) {
6419 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
6420 }
6421
6422 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
6423 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
6424 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
6425 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
6426 $path =~ s/ /\+/g;
6427 $owner =~ s/ /\+/g;
6428
6429 print "$path $owner\n";
6430 }
6431 }
6432
6433 sub git_summary {
6434 my $descr = git_get_project_description($project) || "none";
6435 my %co = parse_commit("HEAD");
6436 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
6437 my $head = $co{'id'};
6438 my $remote_heads = gitweb_check_feature('remote_heads');
6439
6440 my $owner = git_get_project_owner($project);
6441
6442 my $refs = git_get_references();
6443 # These get_*_list functions return one more to allow us to see if
6444 # there are more ...
6445 my @taglist = git_get_tags_list(16);
6446 my @headlist = git_get_heads_list(16);
6447 my %remotedata = $remote_heads ? git_get_remotes_list() : ();
6448 my @forklist;
6449 my $check_forks = gitweb_check_feature('forks');
6450
6451 if ($check_forks) {
6452 # find forks of a project
6453 my $filter = $project;
6454 $filter =~ s/\.git$//;
6455 @forklist = git_get_projects_list($filter);
6456 # filter out forks of forks
6457 @forklist = filter_forks_from_projects_list(\@forklist)
6458 if (@forklist);
6459 }
6460
6461 git_header_html();
6462 git_print_page_nav('summary','', $head);
6463
6464 print "<div class=\"title\">&nbsp;</div>\n";
6465 print "<table class=\"projects_list\">\n" .
6466 "<tr id=\"metadata_desc\"><td>description</td><td>" . esc_html($descr) . "</td></tr>\n";
6467 unless ($omit_owner) {
6468 print "<tr id=\"metadata_owner\"><td>owner</td><td>" . esc_html($owner) . "</td></tr>\n";
6469 }
6470 if (defined $cd{'rfc2822'}) {
6471 print "<tr id=\"metadata_lchange\"><td>last change</td>" .
6472 "<td>".format_timestamp_html(\%cd)."</td></tr>\n";
6473 }
6474
6475 # use per project git URL list in $projectroot/$project/cloneurl
6476 # or make project git URL from git base URL and project name
6477 my $url_tag = "URL";
6478 my @url_list = git_get_project_url_list($project);
6479 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
6480 foreach my $git_url (@url_list) {
6481 next unless $git_url;
6482 print format_repo_url($url_tag, $git_url);
6483 $url_tag = "";
6484 }
6485
6486 # Tag cloud
6487 my $show_ctags = gitweb_check_feature('ctags');
6488 if ($show_ctags) {
6489 my $ctags = git_get_project_ctags($project);
6490 if (%$ctags) {
6491 # without ability to add tags, don't show if there are none
6492 my $cloud = git_populate_project_tagcloud($ctags);
6493 print "<tr id=\"metadata_ctags\">" .
6494 "<td>content tags</td>" .
6495 "<td>".git_show_project_tagcloud($cloud, 48)."</td>" .
6496 "</tr>\n";
6497 }
6498 }
6499
6500 print "</table>\n";
6501
6502 # If XSS prevention is on, we don't include README.html.
6503 # TODO: Allow a readme in some safe format.
6504 if (!$prevent_xss && -s "$projectroot/$project/README.html") {
6505 print "<div class=\"title\">readme</div>\n" .
6506 "<div class=\"readme\">\n";
6507 insert_file("$projectroot/$project/README.html");
6508 print "\n</div>\n"; # class="readme"
6509 }
6510
6511 # we need to request one more than 16 (0..15) to check if
6512 # those 16 are all
6513 my @commitlist = $head ? parse_commits($head, 17) : ();
6514 if (@commitlist) {
6515 git_print_header_div('shortlog');
6516 git_shortlog_body(\@commitlist, 0, 15, $refs,
6517 $#commitlist <= 15 ? undef :
6518 $cgi->a({-href => href(action=>"shortlog")}, "..."));
6519 }
6520
6521 if (@taglist) {
6522 git_print_header_div('tags');
6523 git_tags_body(\@taglist, 0, 15,
6524 $#taglist <= 15 ? undef :
6525 $cgi->a({-href => href(action=>"tags")}, "..."));
6526 }
6527
6528 if (@headlist) {
6529 git_print_header_div('heads');
6530 git_heads_body(\@headlist, $head, 0, 15,
6531 $#headlist <= 15 ? undef :
6532 $cgi->a({-href => href(action=>"heads")}, "..."));
6533 }
6534
6535 if (%remotedata) {
6536 git_print_header_div('remotes');
6537 git_remotes_body(\%remotedata, 15, $head);
6538 }
6539
6540 if (@forklist) {
6541 git_print_header_div('forks');
6542 git_project_list_body(\@forklist, 'age', 0, 15,
6543 $#forklist <= 15 ? undef :
6544 $cgi->a({-href => href(action=>"forks")}, "..."),
6545 'no_header');
6546 }
6547
6548 git_footer_html();
6549 }
6550
6551 sub git_tag {
6552 my %tag = parse_tag($hash);
6553
6554 if (! %tag) {
6555 die_error(404, "Unknown tag object");
6556 }
6557
6558 my $head = git_get_head_hash($project);
6559 git_header_html();
6560 git_print_page_nav('','', $head,undef,$head);
6561 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
6562 print "<div class=\"title_text\">\n" .
6563 "<table class=\"object_header\">\n" .
6564 "<tr>\n" .
6565 "<td>object</td>\n" .
6566 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
6567 $tag{'object'}) . "</td>\n" .
6568 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
6569 $tag{'type'}) . "</td>\n" .
6570 "</tr>\n";
6571 if (defined($tag{'author'})) {
6572 git_print_authorship_rows(\%tag, 'author');
6573 }
6574 print "</table>\n\n" .
6575 "</div>\n";
6576 print "<div class=\"page_body\">";
6577 my $comment = $tag{'comment'};
6578 foreach my $line (@$comment) {
6579 chomp $line;
6580 print esc_html($line, -nbsp=>1) . "<br/>\n";
6581 }
6582 print "</div>\n";
6583 git_footer_html();
6584 }
6585
6586 sub git_blame_common {
6587 my $format = shift || 'porcelain';
6588 if ($format eq 'porcelain' && $input_params{'javascript'}) {
6589 $format = 'incremental';
6590 $action = 'blame_incremental'; # for page title etc
6591 }
6592
6593 # permissions
6594 gitweb_check_feature('blame')
6595 or die_error(403, "Blame view not allowed");
6596
6597 # error checking
6598 die_error(400, "No file name given") unless $file_name;
6599 $hash_base ||= git_get_head_hash($project);
6600 die_error(404, "Couldn't find base commit") unless $hash_base;
6601 my %co = parse_commit($hash_base)
6602 or die_error(404, "Commit not found");
6603 my $ftype = "blob";
6604 if (!defined $hash) {
6605 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
6606 or die_error(404, "Error looking up file");
6607 } else {
6608 $ftype = git_get_type($hash);
6609 if ($ftype !~ "blob") {
6610 die_error(400, "Object is not a blob");
6611 }
6612 }
6613
6614 my $fd;
6615 if ($format eq 'incremental') {
6616 # get file contents (as base)
6617 open $fd, "-|", git_cmd(), 'cat-file', 'blob', $hash
6618 or die_error(500, "Open git-cat-file failed");
6619 } elsif ($format eq 'data') {
6620 # run git-blame --incremental
6621 open $fd, "-|", git_cmd(), "blame", "--incremental",
6622 $hash_base, "--", $file_name
6623 or die_error(500, "Open git-blame --incremental failed");
6624 } else {
6625 # run git-blame --porcelain
6626 open $fd, "-|", git_cmd(), "blame", '-p',
6627 $hash_base, '--', $file_name
6628 or die_error(500, "Open git-blame --porcelain failed");
6629 }
6630
6631 # incremental blame data returns early
6632 if ($format eq 'data') {
6633 print $cgi->header(
6634 -type=>"text/plain", -charset => "utf-8",
6635 -status=> "200 OK");
6636 local $| = 1; # output autoflush
6637 while (my $line = <$fd>) {
6638 print to_utf8($line);
6639 }
6640 close $fd
6641 or print "ERROR $!\n";
6642
6643 print 'END';
6644 if (defined $t0 && gitweb_check_feature('timed')) {
6645 print ' '.
6646 tv_interval($t0, [ gettimeofday() ]).
6647 ' '.$number_of_git_cmds;
6648 }
6649 print "\n";
6650
6651 return;
6652 }
6653
6654 # page header
6655 git_header_html();
6656 my $formats_nav =
6657 $cgi->a({-href => href(action=>"blob", -replay=>1)},
6658 "blob") .
6659 " | ";
6660 if ($format eq 'incremental') {
6661 $formats_nav .=
6662 $cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},
6663 "blame") . " (non-incremental)";
6664 } else {
6665 $formats_nav .=
6666 $cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},
6667 "blame") . " (incremental)";
6668 }
6669 $formats_nav .=
6670 " | " .
6671 $cgi->a({-href => href(action=>"history", -replay=>1)},
6672 "history") .
6673 " | " .
6674 $cgi->a({-href => href(action=>$action, file_name=>$file_name)},
6675 "HEAD");
6676 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
6677 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
6678 git_print_page_path($file_name, $ftype, $hash_base);
6679
6680 # page body
6681 if ($format eq 'incremental') {
6682 print "<noscript>\n<div class=\"error\"><center><b>\n".
6683 "This page requires JavaScript to run.\n Use ".
6684 $cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},
6685 'this page').
6686 " instead.\n".
6687 "</b></center></div>\n</noscript>\n";
6688
6689 print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;
6690 }
6691
6692 print qq!<div class="page_body">\n!;
6693 print qq!<div id="progress_info">... / ...</div>\n!
6694 if ($format eq 'incremental');
6695 print qq!<table id="blame_table" class="blame" width="100%">\n!.
6696 #qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.
6697 qq!<thead>\n!.
6698 qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.
6699 qq!</thead>\n!.
6700 qq!<tbody>\n!;
6701
6702 my @rev_color = qw(light dark);
6703 my $num_colors = scalar(@rev_color);
6704 my $current_color = 0;
6705
6706 if ($format eq 'incremental') {
6707 my $color_class = $rev_color[$current_color];
6708
6709 #contents of a file
6710 my $linenr = 0;
6711 LINE:
6712 while (my $line = <$fd>) {
6713 chomp $line;
6714 $linenr++;
6715
6716 print qq!<tr id="l$linenr" class="$color_class">!.
6717 qq!<td class="sha1"><a href=""> </a></td>!.
6718 qq!<td class="linenr">!.
6719 qq!<a class="linenr" href="">$linenr</a></td>!;
6720 print qq!<td class="pre">! . esc_html($line) . "</td>\n";
6721 print qq!</tr>\n!;
6722 }
6723
6724 } else { # porcelain, i.e. ordinary blame
6725 my %metainfo = (); # saves information about commits
6726
6727 # blame data
6728 LINE:
6729 while (my $line = <$fd>) {
6730 chomp $line;
6731 # the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]
6732 # no <lines in group> for subsequent lines in group of lines
6733 my ($full_rev, $orig_lineno, $lineno, $group_size) =
6734 ($line =~ /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);
6735 if (!exists $metainfo{$full_rev}) {
6736 $metainfo{$full_rev} = { 'nprevious' => 0 };
6737 }
6738 my $meta = $metainfo{$full_rev};
6739 my $data;
6740 while ($data = <$fd>) {
6741 chomp $data;
6742 last if ($data =~ s/^\t//); # contents of line
6743 if ($data =~ /^(\S+)(?: (.*))?$/) {
6744 $meta->{$1} = $2 unless exists $meta->{$1};
6745 }
6746 if ($data =~ /^previous /) {
6747 $meta->{'nprevious'}++;
6748 }
6749 }
6750 my $short_rev = substr($full_rev, 0, 8);
6751 my $author = $meta->{'author'};
6752 my %date =
6753 parse_date($meta->{'author-time'}, $meta->{'author-tz'});
6754 my $date = $date{'iso-tz'};
6755 if ($group_size) {
6756 $current_color = ($current_color + 1) % $num_colors;
6757 }
6758 my $tr_class = $rev_color[$current_color];
6759 $tr_class .= ' boundary' if (exists $meta->{'boundary'});
6760 $tr_class .= ' no-previous' if ($meta->{'nprevious'} == 0);
6761 $tr_class .= ' multiple-previous' if ($meta->{'nprevious'} > 1);
6762 print "<tr id=\"l$lineno\" class=\"$tr_class\">\n";
6763 if ($group_size) {
6764 print "<td class=\"sha1\"";
6765 print " title=\"". esc_html($author) . ", $date\"";
6766 print " rowspan=\"$group_size\"" if ($group_size > 1);
6767 print ">";
6768 print $cgi->a({-href => href(action=>"commit",
6769 hash=>$full_rev,
6770 file_name=>$file_name)},
6771 esc_html($short_rev));
6772 if ($group_size >= 2) {
6773 my @author_initials = ($author =~ /\b([[:upper:]])\B/g);
6774 if (@author_initials) {
6775 print "<br />" .
6776 esc_html(join('', @author_initials));
6777 # or join('.', ...)
6778 }
6779 }
6780 print "</td>\n";
6781 }
6782 # 'previous' <sha1 of parent commit> <filename at commit>
6783 if (exists $meta->{'previous'} &&
6784 $meta->{'previous'} =~ /^([a-fA-F0-9]{40}) (.*)$/) {
6785 $meta->{'parent'} = $1;
6786 $meta->{'file_parent'} = unquote($2);
6787 }
6788 my $linenr_commit =
6789 exists($meta->{'parent'}) ?
6790 $meta->{'parent'} : $full_rev;
6791 my $linenr_filename =
6792 exists($meta->{'file_parent'}) ?
6793 $meta->{'file_parent'} : unquote($meta->{'filename'});
6794 my $blamed = href(action => 'blame',
6795 file_name => $linenr_filename,
6796 hash_base => $linenr_commit);
6797 print "<td class=\"linenr\">";
6798 print $cgi->a({ -href => "$blamed#l$orig_lineno",
6799 -class => "linenr" },
6800 esc_html($lineno));
6801 print "</td>";
6802 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
6803 print "</tr>\n";
6804 } # end while
6805
6806 }
6807
6808 # footer
6809 print "</tbody>\n".
6810 "</table>\n"; # class="blame"
6811 print "</div>\n"; # class="blame_body"
6812 close $fd
6813 or print "Reading blob failed\n";
6814
6815 git_footer_html();
6816 }
6817
6818 sub git_blame {
6819 git_blame_common();
6820 }
6821
6822 sub git_blame_incremental {
6823 git_blame_common('incremental');
6824 }
6825
6826 sub git_blame_data {
6827 git_blame_common('data');
6828 }
6829
6830 sub git_tags {
6831 my $head = git_get_head_hash($project);
6832 git_header_html();
6833 git_print_page_nav('','', $head,undef,$head,format_ref_views('tags'));
6834 git_print_header_div('summary', $project);
6835
6836 my @tagslist = git_get_tags_list();
6837 if (@tagslist) {
6838 git_tags_body(\@tagslist);
6839 }
6840 git_footer_html();
6841 }
6842
6843 sub git_heads {
6844 my $head = git_get_head_hash($project);
6845 git_header_html();
6846 git_print_page_nav('','', $head,undef,$head,format_ref_views('heads'));
6847 git_print_header_div('summary', $project);
6848
6849 my @headslist = git_get_heads_list();
6850 if (@headslist) {
6851 git_heads_body(\@headslist, $head);
6852 }
6853 git_footer_html();
6854 }
6855
6856 # used both for single remote view and for list of all the remotes
6857 sub git_remotes {
6858 gitweb_check_feature('remote_heads')
6859 or die_error(403, "Remote heads view is disabled");
6860
6861 my $head = git_get_head_hash($project);
6862 my $remote = $input_params{'hash'};
6863
6864 my $remotedata = git_get_remotes_list($remote);
6865 die_error(500, "Unable to get remote information") unless defined $remotedata;
6866
6867 unless (%$remotedata) {
6868 die_error(404, defined $remote ?
6869 "Remote $remote not found" :
6870 "No remotes found");
6871 }
6872
6873 git_header_html(undef, undef, -action_extra => $remote);
6874 git_print_page_nav('', '', $head, undef, $head,
6875 format_ref_views($remote ? '' : 'remotes'));
6876
6877 fill_remote_heads($remotedata);
6878 if (defined $remote) {
6879 git_print_header_div('remotes', "$remote remote for $project");
6880 git_remote_block($remote, $remotedata->{$remote}, undef, $head);
6881 } else {
6882 git_print_header_div('summary', "$project remotes");
6883 git_remotes_body($remotedata, undef, $head);
6884 }
6885
6886 git_footer_html();
6887 }
6888
6889 sub git_blob_plain {
6890 my $type = shift;
6891 my $expires;
6892
6893 if (!defined $hash) {
6894 if (defined $file_name) {
6895 my $base = $hash_base || git_get_head_hash($project);
6896 $hash = git_get_hash_by_path($base, $file_name, "blob")
6897 or die_error(404, "Cannot find file");
6898 } else {
6899 die_error(400, "No file name defined");
6900 }
6901 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6902 # blobs defined by non-textual hash id's can be cached
6903 $expires = "+1d";
6904 }
6905
6906 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
6907 or die_error(500, "Open git-cat-file blob '$hash' failed");
6908
6909 # content-type (can include charset)
6910 $type = blob_contenttype($fd, $file_name, $type);
6911
6912 # "save as" filename, even when no $file_name is given
6913 my $save_as = "$hash";
6914 if (defined $file_name) {
6915 $save_as = $file_name;
6916 } elsif ($type =~ m/^text\//) {
6917 $save_as .= '.txt';
6918 }
6919
6920 # With XSS prevention on, blobs of all types except a few known safe
6921 # ones are served with "Content-Disposition: attachment" to make sure
6922 # they don't run in our security domain. For certain image types,
6923 # blob view writes an <img> tag referring to blob_plain view, and we
6924 # want to be sure not to break that by serving the image as an
6925 # attachment (though Firefox 3 doesn't seem to care).
6926 my $sandbox = $prevent_xss &&
6927 $type !~ m!^(?:text/[a-z]+|image/(?:gif|png|jpeg))(?:[ ;]|$)!;
6928
6929 # serve text/* as text/plain
6930 if ($prevent_xss &&
6931 ($type =~ m!^text/[a-z]+\b(.*)$! ||
6932 ($type =~ m!^[a-z]+/[a-z]\+xml\b(.*)$! && -T $fd))) {
6933 my $rest = $1;
6934 $rest = defined $rest ? $rest : '';
6935 $type = "text/plain$rest";
6936 }
6937
6938 print $cgi->header(
6939 -type => $type,
6940 -expires => $expires,
6941 -content_disposition =>
6942 ($sandbox ? 'attachment' : 'inline')
6943 . '; filename="' . $save_as . '"');
6944 local $/ = undef;
6945 binmode STDOUT, ':raw';
6946 print <$fd>;
6947 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
6948 close $fd;
6949 }
6950
6951 sub git_blob {
6952 my $expires;
6953
6954 if (!defined $hash) {
6955 if (defined $file_name) {
6956 my $base = $hash_base || git_get_head_hash($project);
6957 $hash = git_get_hash_by_path($base, $file_name, "blob")
6958 or die_error(404, "Cannot find file");
6959 } else {
6960 die_error(400, "No file name defined");
6961 }
6962 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6963 # blobs defined by non-textual hash id's can be cached
6964 $expires = "+1d";
6965 }
6966
6967 my $have_blame = gitweb_check_feature('blame');
6968 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
6969 or die_error(500, "Couldn't cat $file_name, $hash");
6970 my $mimetype = blob_mimetype($fd, $file_name);
6971 # use 'blob_plain' (aka 'raw') view for files that cannot be displayed
6972 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {
6973 close $fd;
6974 return git_blob_plain($mimetype);
6975 }
6976 # we can have blame only for text/* mimetype
6977 $have_blame &&= ($mimetype =~ m!^text/!);
6978
6979 my $highlight = gitweb_check_feature('highlight');
6980 my $syntax = guess_file_syntax($highlight, $mimetype, $file_name);
6981 $fd = run_highlighter($fd, $highlight, $syntax)
6982 if $syntax;
6983
6984 git_header_html(undef, $expires);
6985 my $formats_nav = '';
6986 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
6987 if (defined $file_name) {
6988 if ($have_blame) {
6989 $formats_nav .=
6990 $cgi->a({-href => href(action=>"blame", -replay=>1)},
6991 "blame") .
6992 " | ";
6993 }
6994 $formats_nav .=
6995 $cgi->a({-href => href(action=>"history", -replay=>1)},
6996 "history") .
6997 " | " .
6998 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
6999 "raw") .
7000 " | " .
7001 $cgi->a({-href => href(action=>"blob",
7002 hash_base=>"HEAD", file_name=>$file_name)},
7003 "HEAD");
7004 } else {
7005 $formats_nav .=
7006 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
7007 "raw");
7008 }
7009 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
7010 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
7011 } else {
7012 print "<div class=\"page_nav\">\n" .
7013 "<br/><br/></div>\n" .
7014 "<div class=\"title\">".esc_html($hash)."</div>\n";
7015 }
7016 git_print_page_path($file_name, "blob", $hash_base);
7017 print "<div class=\"page_body\">\n";
7018 if ($mimetype =~ m!^image/!) {
7019 print qq!<img type="!.esc_attr($mimetype).qq!"!;
7020 if ($file_name) {
7021 print qq! alt="!.esc_attr($file_name).qq!" title="!.esc_attr($file_name).qq!"!;
7022 }
7023 print qq! src="! .
7024 href(action=>"blob_plain", hash=>$hash,
7025 hash_base=>$hash_base, file_name=>$file_name) .
7026 qq!" />\n!;
7027 } else {
7028 my $nr;
7029 while (my $line = <$fd>) {
7030 chomp $line;
7031 $nr++;
7032 $line = untabify($line);
7033 printf qq!<div class="pre"><a id="l%i" href="%s#l%i" class="linenr">%4i</a> %s</div>\n!,
7034 $nr, esc_attr(href(-replay => 1)), $nr, $nr,
7035 $syntax ? sanitize($line) : esc_html($line, -nbsp=>1);
7036 }
7037 }
7038 close $fd
7039 or print "Reading blob failed.\n";
7040 print "</div>";
7041 git_footer_html();
7042 }
7043
7044 sub git_tree {
7045 if (!defined $hash_base) {
7046 $hash_base = "HEAD";
7047 }
7048 if (!defined $hash) {
7049 if (defined $file_name) {
7050 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
7051 } else {
7052 $hash = $hash_base;
7053 }
7054 }
7055 die_error(404, "No such tree") unless defined($hash);
7056
7057 my $show_sizes = gitweb_check_feature('show-sizes');
7058 my $have_blame = gitweb_check_feature('blame');
7059
7060 my @entries = ();
7061 {
7062 local $/ = "\0";
7063 open my $fd, "-|", git_cmd(), "ls-tree", '-z',
7064 ($show_sizes ? '-l' : ()), @extra_options, $hash
7065 or die_error(500, "Open git-ls-tree failed");
7066 @entries = map { chomp; $_ } <$fd>;
7067 close $fd
7068 or die_error(404, "Reading tree failed");
7069 }
7070
7071 my $refs = git_get_references();
7072 my $ref = format_ref_marker($refs, $hash_base);
7073 git_header_html();
7074 my $basedir = '';
7075 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
7076 my @views_nav = ();
7077 if (defined $file_name) {
7078 push @views_nav,
7079 $cgi->a({-href => href(action=>"history", -replay=>1)},
7080 "history"),
7081 $cgi->a({-href => href(action=>"tree",
7082 hash_base=>"HEAD", file_name=>$file_name)},
7083 "HEAD"),
7084 }
7085 my $snapshot_links = format_snapshot_links($hash);
7086 if (defined $snapshot_links) {
7087 # FIXME: Should be available when we have no hash base as well.
7088 push @views_nav, $snapshot_links;
7089 }
7090 git_print_page_nav('tree','', $hash_base, undef, undef,
7091 join(' | ', @views_nav));
7092 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
7093 } else {
7094 undef $hash_base;
7095 print "<div class=\"page_nav\">\n";
7096 print "<br/><br/></div>\n";
7097 print "<div class=\"title\">".esc_html($hash)."</div>\n";
7098 }
7099 if (defined $file_name) {
7100 $basedir = $file_name;
7101 if ($basedir ne '' && substr($basedir, -1) ne '/') {
7102 $basedir .= '/';
7103 }
7104 git_print_page_path($file_name, 'tree', $hash_base);
7105 }
7106 print "<div class=\"page_body\">\n";
7107 print "<table class=\"tree\">\n";
7108 my $alternate = 1;
7109 # '..' (top directory) link if possible
7110 if (defined $hash_base &&
7111 defined $file_name && $file_name =~ m![^/]+$!) {
7112 if ($alternate) {
7113 print "<tr class=\"dark\">\n";
7114 } else {
7115 print "<tr class=\"light\">\n";
7116 }
7117 $alternate ^= 1;
7118
7119 my $up = $file_name;
7120 $up =~ s!/?[^/]+$!!;
7121 undef $up unless $up;
7122 # based on git_print_tree_entry
7123 print '<td class="mode">' . mode_str('040000') . "</td>\n";
7124 print '<td class="size">&nbsp;</td>'."\n" if $show_sizes;
7125 print '<td class="list">';
7126 print $cgi->a({-href => href(action=>"tree",
7127 hash_base=>$hash_base,
7128 file_name=>$up)},
7129 "..");
7130 print "</td>\n";
7131 print "<td class=\"link\"></td>\n";
7132
7133 print "</tr>\n";
7134 }
7135 foreach my $line (@entries) {
7136 my %t = parse_ls_tree_line($line, -z => 1, -l => $show_sizes);
7137
7138 if ($alternate) {
7139 print "<tr class=\"dark\">\n";
7140 } else {
7141 print "<tr class=\"light\">\n";
7142 }
7143 $alternate ^= 1;
7144
7145 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
7146
7147 print "</tr>\n";
7148 }
7149 print "</table>\n" .
7150 "</div>";
7151 git_footer_html();
7152 }
7153
7154 sub snapshot_name {
7155 my ($project, $hash) = @_;
7156
7157 # path/to/project.git -> project
7158 # path/to/project/.git -> project
7159 my $name = to_utf8($project);
7160 $name =~ s,([^/])/*\.git$,$1,;
7161 $name = basename($name);
7162 # sanitize name
7163 $name =~ s/[[:cntrl:]]/?/g;
7164
7165 my $ver = $hash;
7166 if ($hash =~ /^[0-9a-fA-F]+$/) {
7167 # shorten SHA-1 hash
7168 my $full_hash = git_get_full_hash($project, $hash);
7169 if ($full_hash =~ /^$hash/ && length($hash) > 7) {
7170 $ver = git_get_short_hash($project, $hash);
7171 }
7172 } elsif ($hash =~ m!^refs/tags/(.*)$!) {
7173 # tags don't need shortened SHA-1 hash
7174 $ver = $1;
7175 } else {
7176 # branches and other need shortened SHA-1 hash
7177 if ($hash =~ m!^refs/(?:heads|remotes)/(.*)$!) {
7178 $ver = $1;
7179 }
7180 $ver .= '-' . git_get_short_hash($project, $hash);
7181 }
7182 # in case of hierarchical branch names
7183 $ver =~ s!/!.!g;
7184
7185 # name = project-version_string
7186 $name = "$name-$ver";
7187
7188 return wantarray ? ($name, $name) : $name;
7189 }
7190
7191 sub exit_if_unmodified_since {
7192 my ($latest_epoch) = @_;
7193 our $cgi;
7194
7195 my $if_modified = $cgi->http('IF_MODIFIED_SINCE');
7196 if (defined $if_modified) {
7197 my $since;
7198 if (eval { require HTTP::Date; 1; }) {
7199 $since = HTTP::Date::str2time($if_modified);
7200 } elsif (eval { require Time::ParseDate; 1; }) {
7201 $since = Time::ParseDate::parsedate($if_modified, GMT => 1);
7202 }
7203 if (defined $since && $latest_epoch <= $since) {
7204 my %latest_date = parse_date($latest_epoch);
7205 print $cgi->header(
7206 -last_modified => $latest_date{'rfc2822'},
7207 -status => '304 Not Modified');
7208 goto DONE_GITWEB;
7209 }
7210 }
7211 }
7212
7213 sub git_snapshot {
7214 my $format = $input_params{'snapshot_format'};
7215 if (!@snapshot_fmts) {
7216 die_error(403, "Snapshots not allowed");
7217 }
7218 # default to first supported snapshot format
7219 $format ||= $snapshot_fmts[0];
7220 if ($format !~ m/^[a-z0-9]+$/) {
7221 die_error(400, "Invalid snapshot format parameter");
7222 } elsif (!exists($known_snapshot_formats{$format})) {
7223 die_error(400, "Unknown snapshot format");
7224 } elsif ($known_snapshot_formats{$format}{'disabled'}) {
7225 die_error(403, "Snapshot format not allowed");
7226 } elsif (!grep($_ eq $format, @snapshot_fmts)) {
7227 die_error(403, "Unsupported snapshot format");
7228 }
7229
7230 my $type = git_get_type("$hash^{}");
7231 if (!$type) {
7232 die_error(404, 'Object does not exist');
7233 } elsif ($type eq 'blob') {
7234 die_error(400, 'Object is not a tree-ish');
7235 }
7236
7237 my ($name, $prefix) = snapshot_name($project, $hash);
7238 my $filename = "$name$known_snapshot_formats{$format}{'suffix'}";
7239
7240 my %co = parse_commit($hash);
7241 exit_if_unmodified_since($co{'committer_epoch'}) if %co;
7242
7243 my $cmd = quote_command(
7244 git_cmd(), 'archive',
7245 "--format=$known_snapshot_formats{$format}{'format'}",
7246 "--prefix=$prefix/", $hash);
7247 if (exists $known_snapshot_formats{$format}{'compressor'}) {
7248 $cmd .= ' | ' . quote_command(@{$known_snapshot_formats{$format}{'compressor'}});
7249 }
7250
7251 $filename =~ s/(["\\])/\\$1/g;
7252 my %latest_date;
7253 if (%co) {
7254 %latest_date = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
7255 }
7256
7257 print $cgi->header(
7258 -type => $known_snapshot_formats{$format}{'type'},
7259 -content_disposition => 'inline; filename="' . $filename . '"',
7260 %co ? (-last_modified => $latest_date{'rfc2822'}) : (),
7261 -status => '200 OK');
7262
7263 open my $fd, "-|", $cmd
7264 or die_error(500, "Execute git-archive failed");
7265 binmode STDOUT, ':raw';
7266 print <$fd>;
7267 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
7268 close $fd;
7269 }
7270
7271 sub git_log_generic {
7272 my ($fmt_name, $body_subr, $base, $parent, $file_name, $file_hash) = @_;
7273
7274 my $head = git_get_head_hash($project);
7275 if (!defined $base) {
7276 $base = $head;
7277 }
7278 if (!defined $page) {
7279 $page = 0;
7280 }
7281 my $refs = git_get_references();
7282
7283 my $commit_hash = $base;
7284 if (defined $parent) {
7285 $commit_hash = "$parent..$base";
7286 }
7287 my @commitlist =
7288 parse_commits($commit_hash, 101, (100 * $page),
7289 defined $file_name ? ($file_name, "--full-history") : ());
7290
7291 my $ftype;
7292 if (!defined $file_hash && defined $file_name) {
7293 # some commits could have deleted file in question,
7294 # and not have it in tree, but one of them has to have it
7295 for (my $i = 0; $i < @commitlist; $i++) {
7296 $file_hash = git_get_hash_by_path($commitlist[$i]{'id'}, $file_name);
7297 last if defined $file_hash;
7298 }
7299 }
7300 if (defined $file_hash) {
7301 $ftype = git_get_type($file_hash);
7302 }
7303 if (defined $file_name && !defined $ftype) {
7304 die_error(500, "Unknown type of object");
7305 }
7306 my %co;
7307 if (defined $file_name) {
7308 %co = parse_commit($base)
7309 or die_error(404, "Unknown commit object");
7310 }
7311
7312
7313 my $paging_nav = format_paging_nav($fmt_name, $page, $#commitlist >= 100);
7314 my $next_link = '';
7315 if ($#commitlist >= 100) {
7316 $next_link =
7317 $cgi->a({-href => href(-replay=>1, page=>$page+1),
7318 -accesskey => "n", -title => "Alt-n"}, "next");
7319 }
7320 my $patch_max = gitweb_get_feature('patches');
7321 if ($patch_max && !defined $file_name) {
7322 if ($patch_max < 0 || @commitlist <= $patch_max) {
7323 $paging_nav .= " &sdot; " .
7324 $cgi->a({-href => href(action=>"patches", -replay=>1)},
7325 "patches");
7326 }
7327 }
7328
7329 git_header_html();
7330 git_print_page_nav($fmt_name,'', $hash,$hash,$hash, $paging_nav);
7331 if (defined $file_name) {
7332 git_print_header_div('commit', esc_html($co{'title'}), $base);
7333 } else {
7334 git_print_header_div('summary', $project)
7335 }
7336 git_print_page_path($file_name, $ftype, $hash_base)
7337 if (defined $file_name);
7338
7339 $body_subr->(\@commitlist, 0, 99, $refs, $next_link,
7340 $file_name, $file_hash, $ftype);
7341
7342 git_footer_html();
7343 }
7344
7345 sub git_log {
7346 git_log_generic('log', \&git_log_body,
7347 $hash, $hash_parent);
7348 }
7349
7350 sub git_commit {
7351 $hash ||= $hash_base || "HEAD";
7352 my %co = parse_commit($hash)
7353 or die_error(404, "Unknown commit object");
7354
7355 my $parent = $co{'parent'};
7356 my $parents = $co{'parents'}; # listref
7357
7358 # we need to prepare $formats_nav before any parameter munging
7359 my $formats_nav;
7360 if (!defined $parent) {
7361 # --root commitdiff
7362 $formats_nav .= '(initial)';
7363 } elsif (@$parents == 1) {
7364 # single parent commit
7365 $formats_nav .=
7366 '(parent: ' .
7367 $cgi->a({-href => href(action=>"commit",
7368 hash=>$parent)},
7369 esc_html(substr($parent, 0, 7))) .
7370 ')';
7371 } else {
7372 # merge commit
7373 $formats_nav .=
7374 '(merge: ' .
7375 join(' ', map {
7376 $cgi->a({-href => href(action=>"commit",
7377 hash=>$_)},
7378 esc_html(substr($_, 0, 7)));
7379 } @$parents ) .
7380 ')';
7381 }
7382 if (gitweb_check_feature('patches') && @$parents <= 1) {
7383 $formats_nav .= " | " .
7384 $cgi->a({-href => href(action=>"patch", -replay=>1)},
7385 "patch");
7386 }
7387
7388 if (!defined $parent) {
7389 $parent = "--root";
7390 }
7391 my @difftree;
7392 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
7393 @diff_opts,
7394 (@$parents <= 1 ? $parent : '-c'),
7395 $hash, "--"
7396 or die_error(500, "Open git-diff-tree failed");
7397 @difftree = map { chomp; $_ } <$fd>;
7398 close $fd or die_error(404, "Reading git-diff-tree failed");
7399
7400 # non-textual hash id's can be cached
7401 my $expires;
7402 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
7403 $expires = "+1d";
7404 }
7405 my $refs = git_get_references();
7406 my $ref = format_ref_marker($refs, $co{'id'});
7407
7408 git_header_html(undef, $expires);
7409 git_print_page_nav('commit', '',
7410 $hash, $co{'tree'}, $hash,
7411 $formats_nav);
7412
7413 if (defined $co{'parent'}) {
7414 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
7415 } else {
7416 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
7417 }
7418 print "<div class=\"title_text\">\n" .
7419 "<table class=\"object_header\">\n";
7420 git_print_authorship_rows(\%co);
7421 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
7422 print "<tr>" .
7423 "<td>tree</td>" .
7424 "<td class=\"sha1\">" .
7425 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
7426 class => "list"}, $co{'tree'}) .
7427 "</td>" .
7428 "<td class=\"link\">" .
7429 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
7430 "tree");
7431 my $snapshot_links = format_snapshot_links($hash);
7432 if (defined $snapshot_links) {
7433 print " | " . $snapshot_links;
7434 }
7435 print "</td>" .
7436 "</tr>\n";
7437
7438 foreach my $par (@$parents) {
7439 print "<tr>" .
7440 "<td>parent</td>" .
7441 "<td class=\"sha1\">" .
7442 $cgi->a({-href => href(action=>"commit", hash=>$par),
7443 class => "list"}, $par) .
7444 "</td>" .
7445 "<td class=\"link\">" .
7446 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
7447 " | " .
7448 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
7449 "</td>" .
7450 "</tr>\n";
7451 }
7452 print "</table>".
7453 "</div>\n";
7454
7455 print "<div class=\"page_body\">\n";
7456 git_print_log($co{'comment'});
7457 print "</div>\n";
7458
7459 git_difftree_body(\@difftree, $hash, @$parents);
7460
7461 git_footer_html();
7462 }
7463
7464 sub git_object {
7465 # object is defined by:
7466 # - hash or hash_base alone
7467 # - hash_base and file_name
7468 my $type;
7469
7470 # - hash or hash_base alone
7471 if ($hash || ($hash_base && !defined $file_name)) {
7472 my $object_id = $hash || $hash_base;
7473
7474 open my $fd, "-|", quote_command(
7475 git_cmd(), 'cat-file', '-t', $object_id) . ' 2> /dev/null'
7476 or die_error(404, "Object does not exist");
7477 $type = <$fd>;
7478 chomp $type;
7479 close $fd
7480 or die_error(404, "Object does not exist");
7481
7482 # - hash_base and file_name
7483 } elsif ($hash_base && defined $file_name) {
7484 $file_name =~ s,/+$,,;
7485
7486 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
7487 or die_error(404, "Base object does not exist");
7488
7489 # here errors should not happen
7490 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
7491 or die_error(500, "Open git-ls-tree failed");
7492 my $line = <$fd>;
7493 close $fd;
7494
7495 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
7496 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
7497 die_error(404, "File or directory for given base does not exist");
7498 }
7499 $type = $2;
7500 $hash = $3;
7501 } else {
7502 die_error(400, "Not enough information to find object");
7503 }
7504
7505 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
7506 hash=>$hash, hash_base=>$hash_base,
7507 file_name=>$file_name),
7508 -status => '302 Found');
7509 }
7510
7511 sub git_blobdiff {
7512 my $format = shift || 'html';
7513 my $diff_style = $input_params{'diff_style'} || 'inline';
7514
7515 my $fd;
7516 my @difftree;
7517 my %diffinfo;
7518 my $expires;
7519
7520 # preparing $fd and %diffinfo for git_patchset_body
7521 # new style URI
7522 if (defined $hash_base && defined $hash_parent_base) {
7523 if (defined $file_name) {
7524 # read raw output
7525 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
7526 $hash_parent_base, $hash_base,
7527 "--", (defined $file_parent ? $file_parent : ()), $file_name
7528 or die_error(500, "Open git-diff-tree failed");
7529 @difftree = map { chomp; $_ } <$fd>;
7530 close $fd
7531 or die_error(404, "Reading git-diff-tree failed");
7532 @difftree
7533 or die_error(404, "Blob diff not found");
7534
7535 } elsif (defined $hash &&
7536 $hash =~ /[0-9a-fA-F]{40}/) {
7537 # try to find filename from $hash
7538
7539 # read filtered raw output
7540 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
7541 $hash_parent_base, $hash_base, "--"
7542 or die_error(500, "Open git-diff-tree failed");
7543 @difftree =
7544 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
7545 # $hash == to_id
7546 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
7547 map { chomp; $_ } <$fd>;
7548 close $fd
7549 or die_error(404, "Reading git-diff-tree failed");
7550 @difftree
7551 or die_error(404, "Blob diff not found");
7552
7553 } else {
7554 die_error(400, "Missing one of the blob diff parameters");
7555 }
7556
7557 if (@difftree > 1) {
7558 die_error(400, "Ambiguous blob diff specification");
7559 }
7560
7561 %diffinfo = parse_difftree_raw_line($difftree[0]);
7562 $file_parent ||= $diffinfo{'from_file'} || $file_name;
7563 $file_name ||= $diffinfo{'to_file'};
7564
7565 $hash_parent ||= $diffinfo{'from_id'};
7566 $hash ||= $diffinfo{'to_id'};
7567
7568 # non-textual hash id's can be cached
7569 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
7570 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
7571 $expires = '+1d';
7572 }
7573
7574 # open patch output
7575 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
7576 '-p', ($format eq 'html' ? "--full-index" : ()),
7577 $hash_parent_base, $hash_base,
7578 "--", (defined $file_parent ? $file_parent : ()), $file_name
7579 or die_error(500, "Open git-diff-tree failed");
7580 }
7581
7582 # old/legacy style URI -- not generated anymore since 1.4.3.
7583 if (!%diffinfo) {
7584 die_error('404 Not Found', "Missing one of the blob diff parameters")
7585 }
7586
7587 # header
7588 if ($format eq 'html') {
7589 my $formats_nav =
7590 $cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},
7591 "raw");
7592 $formats_nav .= diff_style_nav($diff_style);
7593 git_header_html(undef, $expires);
7594 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
7595 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
7596 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
7597 } else {
7598 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
7599 print "<div class=\"title\">".esc_html("$hash vs $hash_parent")."</div>\n";
7600 }
7601 if (defined $file_name) {
7602 git_print_page_path($file_name, "blob", $hash_base);
7603 } else {
7604 print "<div class=\"page_path\"></div>\n";
7605 }
7606
7607 } elsif ($format eq 'plain') {
7608 print $cgi->header(
7609 -type => 'text/plain',
7610 -charset => 'utf-8',
7611 -expires => $expires,
7612 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
7613
7614 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
7615
7616 } else {
7617 die_error(400, "Unknown blobdiff format");
7618 }
7619
7620 # patch
7621 if ($format eq 'html') {
7622 print "<div class=\"page_body\">\n";
7623
7624 git_patchset_body($fd, $diff_style,
7625 [ \%diffinfo ], $hash_base, $hash_parent_base);
7626 close $fd;
7627
7628 print "</div>\n"; # class="page_body"
7629 git_footer_html();
7630
7631 } else {
7632 while (my $line = <$fd>) {
7633 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
7634 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
7635
7636 print $line;
7637
7638 last if $line =~ m!^\+\+\+!;
7639 }
7640 local $/ = undef;
7641 print <$fd>;
7642 close $fd;
7643 }
7644 }
7645
7646 sub git_blobdiff_plain {
7647 git_blobdiff('plain');
7648 }
7649
7650 # assumes that it is added as later part of already existing navigation,
7651 # so it returns "| foo | bar" rather than just "foo | bar"
7652 sub diff_style_nav {
7653 my ($diff_style, $is_combined) = @_;
7654 $diff_style ||= 'inline';
7655
7656 return "" if ($is_combined);
7657
7658 my @styles = (inline => 'inline', 'sidebyside' => 'side by side');
7659 my %styles = @styles;
7660 @styles =
7661 @styles[ map { $_ * 2 } 0..$#styles/2 ];
7662
7663 return join '',
7664 map { " | ".$_ }
7665 map {
7666 $_ eq $diff_style ? $styles{$_} :
7667 $cgi->a({-href => href(-replay=>1, diff_style => $_)}, $styles{$_})
7668 } @styles;
7669 }
7670
7671 sub git_commitdiff {
7672 my %params = @_;
7673 my $format = $params{-format} || 'html';
7674 my $diff_style = $input_params{'diff_style'} || 'inline';
7675
7676 my ($patch_max) = gitweb_get_feature('patches');
7677 if ($format eq 'patch') {
7678 die_error(403, "Patch view not allowed") unless $patch_max;
7679 }
7680
7681 $hash ||= $hash_base || "HEAD";
7682 my %co = parse_commit($hash)
7683 or die_error(404, "Unknown commit object");
7684
7685 # choose format for commitdiff for merge
7686 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
7687 $hash_parent = '--cc';
7688 }
7689 # we need to prepare $formats_nav before almost any parameter munging
7690 my $formats_nav;
7691 if ($format eq 'html') {
7692 $formats_nav =
7693 $cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
7694 "raw");
7695 if ($patch_max && @{$co{'parents'}} <= 1) {
7696 $formats_nav .= " | " .
7697 $cgi->a({-href => href(action=>"patch", -replay=>1)},
7698 "patch");
7699 }
7700 $formats_nav .= diff_style_nav($diff_style, @{$co{'parents'}} > 1);
7701
7702 if (defined $hash_parent &&
7703 $hash_parent ne '-c' && $hash_parent ne '--cc') {
7704 # commitdiff with two commits given
7705 my $hash_parent_short = $hash_parent;
7706 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
7707 $hash_parent_short = substr($hash_parent, 0, 7);
7708 }
7709 $formats_nav .=
7710 ' (from';
7711 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
7712 if ($co{'parents'}[$i] eq $hash_parent) {
7713 $formats_nav .= ' parent ' . ($i+1);
7714 last;
7715 }
7716 }
7717 $formats_nav .= ': ' .
7718 $cgi->a({-href => href(-replay=>1,
7719 hash=>$hash_parent, hash_base=>undef)},
7720 esc_html($hash_parent_short)) .
7721 ')';
7722 } elsif (!$co{'parent'}) {
7723 # --root commitdiff
7724 $formats_nav .= ' (initial)';
7725 } elsif (scalar @{$co{'parents'}} == 1) {
7726 # single parent commit
7727 $formats_nav .=
7728 ' (parent: ' .
7729 $cgi->a({-href => href(-replay=>1,
7730 hash=>$co{'parent'}, hash_base=>undef)},
7731 esc_html(substr($co{'parent'}, 0, 7))) .
7732 ')';
7733 } else {
7734 # merge commit
7735 if ($hash_parent eq '--cc') {
7736 $formats_nav .= ' | ' .
7737 $cgi->a({-href => href(-replay=>1,
7738 hash=>$hash, hash_parent=>'-c')},
7739 'combined');
7740 } else { # $hash_parent eq '-c'
7741 $formats_nav .= ' | ' .
7742 $cgi->a({-href => href(-replay=>1,
7743 hash=>$hash, hash_parent=>'--cc')},
7744 'compact');
7745 }
7746 $formats_nav .=
7747 ' (merge: ' .
7748 join(' ', map {
7749 $cgi->a({-href => href(-replay=>1,
7750 hash=>$_, hash_base=>undef)},
7751 esc_html(substr($_, 0, 7)));
7752 } @{$co{'parents'}} ) .
7753 ')';
7754 }
7755 }
7756
7757 my $hash_parent_param = $hash_parent;
7758 if (!defined $hash_parent_param) {
7759 # --cc for multiple parents, --root for parentless
7760 $hash_parent_param =
7761 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
7762 }
7763
7764 # read commitdiff
7765 my $fd;
7766 my @difftree;
7767 if ($format eq 'html') {
7768 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
7769 "--no-commit-id", "--patch-with-raw", "--full-index",
7770 $hash_parent_param, $hash, "--"
7771 or die_error(500, "Open git-diff-tree failed");
7772
7773 while (my $line = <$fd>) {
7774 chomp $line;
7775 # empty line ends raw part of diff-tree output
7776 last unless $line;
7777 push @difftree, scalar parse_difftree_raw_line($line);
7778 }
7779
7780 } elsif ($format eq 'plain') {
7781 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
7782 '-p', $hash_parent_param, $hash, "--"
7783 or die_error(500, "Open git-diff-tree failed");
7784 } elsif ($format eq 'patch') {
7785 # For commit ranges, we limit the output to the number of
7786 # patches specified in the 'patches' feature.
7787 # For single commits, we limit the output to a single patch,
7788 # diverging from the git-format-patch default.
7789 my @commit_spec = ();
7790 if ($hash_parent) {
7791 if ($patch_max > 0) {
7792 push @commit_spec, "-$patch_max";
7793 }
7794 push @commit_spec, '-n', "$hash_parent..$hash";
7795 } else {
7796 if ($params{-single}) {
7797 push @commit_spec, '-1';
7798 } else {
7799 if ($patch_max > 0) {
7800 push @commit_spec, "-$patch_max";
7801 }
7802 push @commit_spec, "-n";
7803 }
7804 push @commit_spec, '--root', $hash;
7805 }
7806 open $fd, "-|", git_cmd(), "format-patch", @diff_opts,
7807 '--encoding=utf8', '--stdout', @commit_spec
7808 or die_error(500, "Open git-format-patch failed");
7809 } else {
7810 die_error(400, "Unknown commitdiff format");
7811 }
7812
7813 # non-textual hash id's can be cached
7814 my $expires;
7815 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
7816 $expires = "+1d";
7817 }
7818
7819 # write commit message
7820 if ($format eq 'html') {
7821 my $refs = git_get_references();
7822 my $ref = format_ref_marker($refs, $co{'id'});
7823
7824 git_header_html(undef, $expires);
7825 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
7826 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
7827 print "<div class=\"title_text\">\n" .
7828 "<table class=\"object_header\">\n";
7829 git_print_authorship_rows(\%co);
7830 print "</table>".
7831 "</div>\n";
7832 print "<div class=\"page_body\">\n";
7833 if (@{$co{'comment'}} > 1) {
7834 print "<div class=\"log\">\n";
7835 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
7836 print "</div>\n"; # class="log"
7837 }
7838
7839 } elsif ($format eq 'plain') {
7840 my $refs = git_get_references("tags");
7841 my $tagname = git_get_rev_name_tags($hash);
7842 my $filename = basename($project) . "-$hash.patch";
7843
7844 print $cgi->header(
7845 -type => 'text/plain',
7846 -charset => 'utf-8',
7847 -expires => $expires,
7848 -content_disposition => 'inline; filename="' . "$filename" . '"');
7849 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
7850 print "From: " . to_utf8($co{'author'}) . "\n";
7851 print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
7852 print "Subject: " . to_utf8($co{'title'}) . "\n";
7853
7854 print "X-Git-Tag: $tagname\n" if $tagname;
7855 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
7856
7857 foreach my $line (@{$co{'comment'}}) {
7858 print to_utf8($line) . "\n";
7859 }
7860 print "---\n\n";
7861 } elsif ($format eq 'patch') {
7862 my $filename = basename($project) . "-$hash.patch";
7863
7864 print $cgi->header(
7865 -type => 'text/plain',
7866 -charset => 'utf-8',
7867 -expires => $expires,
7868 -content_disposition => 'inline; filename="' . "$filename" . '"');
7869 }
7870
7871 # write patch
7872 if ($format eq 'html') {
7873 my $use_parents = !defined $hash_parent ||
7874 $hash_parent eq '-c' || $hash_parent eq '--cc';
7875 git_difftree_body(\@difftree, $hash,
7876 $use_parents ? @{$co{'parents'}} : $hash_parent);
7877 print "<br/>\n";
7878
7879 git_patchset_body($fd, $diff_style,
7880 \@difftree, $hash,
7881 $use_parents ? @{$co{'parents'}} : $hash_parent);
7882 close $fd;
7883 print "</div>\n"; # class="page_body"
7884 git_footer_html();
7885
7886 } elsif ($format eq 'plain') {
7887 local $/ = undef;
7888 print <$fd>;
7889 close $fd
7890 or print "Reading git-diff-tree failed\n";
7891 } elsif ($format eq 'patch') {
7892 local $/ = undef;
7893 print <$fd>;
7894 close $fd
7895 or print "Reading git-format-patch failed\n";
7896 }
7897 }
7898
7899 sub git_commitdiff_plain {
7900 git_commitdiff(-format => 'plain');
7901 }
7902
7903 # format-patch-style patches
7904 sub git_patch {
7905 git_commitdiff(-format => 'patch', -single => 1);
7906 }
7907
7908 sub git_patches {
7909 git_commitdiff(-format => 'patch');
7910 }
7911
7912 sub git_history {
7913 git_log_generic('history', \&git_history_body,
7914 $hash_base, $hash_parent_base,
7915 $file_name, $hash);
7916 }
7917
7918 sub git_search {
7919 $searchtype ||= 'commit';
7920
7921 # check if appropriate features are enabled
7922 gitweb_check_feature('search')
7923 or die_error(403, "Search is disabled");
7924 if ($searchtype eq 'pickaxe') {
7925 # pickaxe may take all resources of your box and run for several minutes
7926 # with every query - so decide by yourself how public you make this feature
7927 gitweb_check_feature('pickaxe')
7928 or die_error(403, "Pickaxe search is disabled");
7929 }
7930 if ($searchtype eq 'grep') {
7931 # grep search might be potentially CPU-intensive, too
7932 gitweb_check_feature('grep')
7933 or die_error(403, "Grep search is disabled");
7934 }
7935
7936 if (!defined $searchtext) {
7937 die_error(400, "Text field is empty");
7938 }
7939 if (!defined $hash) {
7940 $hash = git_get_head_hash($project);
7941 }
7942 my %co = parse_commit($hash);
7943 if (!%co) {
7944 die_error(404, "Unknown commit object");
7945 }
7946 if (!defined $page) {
7947 $page = 0;
7948 }
7949
7950 if ($searchtype eq 'commit' ||
7951 $searchtype eq 'author' ||
7952 $searchtype eq 'committer') {
7953 git_search_message(%co);
7954 } elsif ($searchtype eq 'pickaxe') {
7955 git_search_changes(%co);
7956 } elsif ($searchtype eq 'grep') {
7957 git_search_files(%co);
7958 } else {
7959 die_error(400, "Unknown search type");
7960 }
7961 }
7962
7963 sub git_search_help {
7964 git_header_html();
7965 git_print_page_nav('','', $hash,$hash,$hash);
7966 print <<EOT;
7967 <p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without
7968 regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,
7969 the pattern entered is recognized as the POSIX extended
7970 <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case
7971 insensitive).</p>
7972 <dl>
7973 <dt><b>commit</b></dt>
7974 <dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
7975 EOT
7976 my $have_grep = gitweb_check_feature('grep');
7977 if ($have_grep) {
7978 print <<EOT;
7979 <dt><b>grep</b></dt>
7980 <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
7981 a different one) are searched for the given pattern. On large trees, this search can take
7982 a while and put some strain on the server, so please use it with some consideration. Note that
7983 due to git-grep peculiarity, currently if regexp mode is turned off, the matches are
7984 case-sensitive.</dd>
7985 EOT
7986 }
7987 print <<EOT;
7988 <dt><b>author</b></dt>
7989 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>
7990 <dt><b>committer</b></dt>
7991 <dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>
7992 EOT
7993 my $have_pickaxe = gitweb_check_feature('pickaxe');
7994 if ($have_pickaxe) {
7995 print <<EOT;
7996 <dt><b>pickaxe</b></dt>
7997 <dd>All commits that caused the string to appear or disappear from any file (changes that
7998 added, removed or "modified" the string) will be listed. This search can take a while and
7999 takes a lot of strain on the server, so please use it wisely. Note that since you may be
8000 interested even in changes just changing the case as well, this search is case sensitive.</dd>
8001 EOT
8002 }
8003 print "</dl>\n";
8004 git_footer_html();
8005 }
8006
8007 sub git_shortlog {
8008 git_log_generic('shortlog', \&git_shortlog_body,
8009 $hash, $hash_parent);
8010 }
8011
8012 ## ......................................................................
8013 ## feeds (RSS, Atom; OPML)
8014
8015 sub git_feed {
8016 my $format = shift || 'atom';
8017 my $have_blame = gitweb_check_feature('blame');
8018
8019 # Atom: http://www.atomenabled.org/developers/syndication/
8020 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
8021 if ($format ne 'rss' && $format ne 'atom') {
8022 die_error(400, "Unknown web feed format");
8023 }
8024
8025 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
8026 my $head = $hash || 'HEAD';
8027 my @commitlist = parse_commits($head, 150, 0, $file_name);
8028
8029 my %latest_commit;
8030 my %latest_date;
8031 my $content_type = "application/$format+xml";
8032 if (defined $cgi->http('HTTP_ACCEPT') &&
8033 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
8034 # browser (feed reader) prefers text/xml
8035 $content_type = 'text/xml';
8036 }
8037 if (defined($commitlist[0])) {
8038 %latest_commit = %{$commitlist[0]};
8039 my $latest_epoch = $latest_commit{'committer_epoch'};
8040 exit_if_unmodified_since($latest_epoch);
8041 %latest_date = parse_date($latest_epoch, $latest_commit{'committer_tz'});
8042 }
8043 print $cgi->header(
8044 -type => $content_type,
8045 -charset => 'utf-8',
8046 %latest_date ? (-last_modified => $latest_date{'rfc2822'}) : (),
8047 -status => '200 OK');
8048
8049 # Optimization: skip generating the body if client asks only
8050 # for Last-Modified date.
8051 return if ($cgi->request_method() eq 'HEAD');
8052
8053 # header variables
8054 my $title = "$site_name - $project/$action";
8055 my $feed_type = 'log';
8056 if (defined $hash) {
8057 $title .= " - '$hash'";
8058 $feed_type = 'branch log';
8059 if (defined $file_name) {
8060 $title .= " :: $file_name";
8061 $feed_type = 'history';
8062 }
8063 } elsif (defined $file_name) {
8064 $title .= " - $file_name";
8065 $feed_type = 'history';
8066 }
8067 $title .= " $feed_type";
8068 $title = esc_html($title);
8069 my $descr = git_get_project_description($project);
8070 if (defined $descr) {
8071 $descr = esc_html($descr);
8072 } else {
8073 $descr = "$project " .
8074 ($format eq 'rss' ? 'RSS' : 'Atom') .
8075 " feed";
8076 }
8077 my $owner = git_get_project_owner($project);
8078 $owner = esc_html($owner);
8079
8080 #header
8081 my $alt_url;
8082 if (defined $file_name) {
8083 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
8084 } elsif (defined $hash) {
8085 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
8086 } else {
8087 $alt_url = href(-full=>1, action=>"summary");
8088 }
8089 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
8090 if ($format eq 'rss') {
8091 print <<XML;
8092 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
8093 <channel>
8094 XML
8095 print "<title>$title</title>\n" .
8096 "<link>$alt_url</link>\n" .
8097 "<description>$descr</description>\n" .
8098 "<language>en</language>\n" .
8099 # project owner is responsible for 'editorial' content
8100 "<managingEditor>$owner</managingEditor>\n";
8101 if (defined $logo || defined $favicon) {
8102 # prefer the logo to the favicon, since RSS
8103 # doesn't allow both
8104 my $img = esc_url($logo || $favicon);
8105 print "<image>\n" .
8106 "<url>$img</url>\n" .
8107 "<title>$title</title>\n" .
8108 "<link>$alt_url</link>\n" .
8109 "</image>\n";
8110 }
8111 if (%latest_date) {
8112 print "<pubDate>$latest_date{'rfc2822'}</pubDate>\n";
8113 print "<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";
8114 }
8115 print "<generator>gitweb v.$version/$git_version</generator>\n";
8116 } elsif ($format eq 'atom') {
8117 print <<XML;
8118 <feed xmlns="http://www.w3.org/2005/Atom">
8119 XML
8120 print "<title>$title</title>\n" .
8121 "<subtitle>$descr</subtitle>\n" .
8122 '<link rel="alternate" type="text/html" href="' .
8123 $alt_url . '" />' . "\n" .
8124 '<link rel="self" type="' . $content_type . '" href="' .
8125 $cgi->self_url() . '" />' . "\n" .
8126 "<id>" . href(-full=>1) . "</id>\n" .
8127 # use project owner for feed author
8128 "<author><name>$owner</name></author>\n";
8129 if (defined $favicon) {
8130 print "<icon>" . esc_url($favicon) . "</icon>\n";
8131 }
8132 if (defined $logo) {
8133 # not twice as wide as tall: 72 x 27 pixels
8134 print "<logo>" . esc_url($logo) . "</logo>\n";
8135 }
8136 if (! %latest_date) {
8137 # dummy date to keep the feed valid until commits trickle in:
8138 print "<updated>1970-01-01T00:00:00Z</updated>\n";
8139 } else {
8140 print "<updated>$latest_date{'iso-8601'}</updated>\n";
8141 }
8142 print "<generator version='$version/$git_version'>gitweb</generator>\n";
8143 }
8144
8145 # contents
8146 for (my $i = 0; $i <= $#commitlist; $i++) {
8147 my %co = %{$commitlist[$i]};
8148 my $commit = $co{'id'};
8149 # we read 150, we always show 30 and the ones more recent than 48 hours
8150 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
8151 last;
8152 }
8153 my %cd = parse_date($co{'author_epoch'}, $co{'author_tz'});
8154
8155 # get list of changed files
8156 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
8157 $co{'parent'} || "--root",
8158 $co{'id'}, "--", (defined $file_name ? $file_name : ())
8159 or next;
8160 my @difftree = map { chomp; $_ } <$fd>;
8161 close $fd
8162 or next;
8163
8164 # print element (entry, item)
8165 my $co_url = href(-full=>1, action=>"commitdiff", hash=>$commit);
8166 if ($format eq 'rss') {
8167 print "<item>\n" .
8168 "<title>" . esc_html($co{'title'}) . "</title>\n" .
8169 "<author>" . esc_html($co{'author'}) . "</author>\n" .
8170 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
8171 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
8172 "<link>$co_url</link>\n" .
8173 "<description>" . esc_html($co{'title'}) . "</description>\n" .
8174 "<content:encoded>" .
8175 "<![CDATA[\n";
8176 } elsif ($format eq 'atom') {
8177 print "<entry>\n" .
8178 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
8179 "<updated>$cd{'iso-8601'}</updated>\n" .
8180 "<author>\n" .
8181 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
8182 if ($co{'author_email'}) {
8183 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
8184 }
8185 print "</author>\n" .
8186 # use committer for contributor
8187 "<contributor>\n" .
8188 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
8189 if ($co{'committer_email'}) {
8190 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
8191 }
8192 print "</contributor>\n" .
8193 "<published>$cd{'iso-8601'}</published>\n" .
8194 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
8195 "<id>$co_url</id>\n" .
8196 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
8197 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
8198 }
8199 my $comment = $co{'comment'};
8200 print "<pre>\n";
8201 foreach my $line (@$comment) {
8202 $line = esc_html($line);
8203 print "$line\n";
8204 }
8205 print "</pre><ul>\n";
8206 foreach my $difftree_line (@difftree) {
8207 my %difftree = parse_difftree_raw_line($difftree_line);
8208 next if !$difftree{'from_id'};
8209
8210 my $file = $difftree{'file'} || $difftree{'to_file'};
8211
8212 print "<li>" .
8213 "[" .
8214 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
8215 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
8216 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
8217 file_name=>$file, file_parent=>$difftree{'from_file'}),
8218 -title => "diff"}, 'D');
8219 if ($have_blame) {
8220 print $cgi->a({-href => href(-full=>1, action=>"blame",
8221 file_name=>$file, hash_base=>$commit),
8222 -title => "blame"}, 'B');
8223 }
8224 # if this is not a feed of a file history
8225 if (!defined $file_name || $file_name ne $file) {
8226 print $cgi->a({-href => href(-full=>1, action=>"history",
8227 file_name=>$file, hash=>$commit),
8228 -title => "history"}, 'H');
8229 }
8230 $file = esc_path($file);
8231 print "] ".
8232 "$file</li>\n";
8233 }
8234 if ($format eq 'rss') {
8235 print "</ul>]]>\n" .
8236 "</content:encoded>\n" .
8237 "</item>\n";
8238 } elsif ($format eq 'atom') {
8239 print "</ul>\n</div>\n" .
8240 "</content>\n" .
8241 "</entry>\n";
8242 }
8243 }
8244
8245 # end of feed
8246 if ($format eq 'rss') {
8247 print "</channel>\n</rss>\n";
8248 } elsif ($format eq 'atom') {
8249 print "</feed>\n";
8250 }
8251 }
8252
8253 sub git_rss {
8254 git_feed('rss');
8255 }
8256
8257 sub git_atom {
8258 git_feed('atom');
8259 }
8260
8261 sub git_opml {
8262 my @list = git_get_projects_list($project_filter, $strict_export);
8263 if (!@list) {
8264 die_error(404, "No projects found");
8265 }
8266
8267 print $cgi->header(
8268 -type => 'text/xml',
8269 -charset => 'utf-8',
8270 -content_disposition => 'inline; filename="opml.xml"');
8271
8272 my $title = esc_html($site_name);
8273 my $filter = " within subdirectory ";
8274 if (defined $project_filter) {
8275 $filter .= esc_html($project_filter);
8276 } else {
8277 $filter = "";
8278 }
8279 print <<XML;
8280 <?xml version="1.0" encoding="utf-8"?>
8281 <opml version="1.0">
8282 <head>
8283 <title>$title OPML Export$filter</title>
8284 </head>
8285 <body>
8286 <outline text="git RSS feeds">
8287 XML
8288
8289 foreach my $pr (@list) {
8290 my %proj = %$pr;
8291 my $head = git_get_head_hash($proj{'path'});
8292 if (!defined $head) {
8293 next;
8294 }
8295 $git_dir = "$projectroot/$proj{'path'}";
8296 my %co = parse_commit($head);
8297 if (!%co) {
8298 next;
8299 }
8300
8301 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
8302 my $rss = href('project' => $proj{'path'}, 'action' => 'rss', -full => 1);
8303 my $html = href('project' => $proj{'path'}, 'action' => 'summary', -full => 1);
8304 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
8305 }
8306 print <<XML;
8307 </outline>
8308 </body>
8309 </opml>
8310 XML
8311 }
This page took 4.497593 seconds and 3 git commands to generate.