Code

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