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