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