Code

Merge branch 'jc/maint-reset-unmerged-path'
[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);
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++";
673         # use first config file that exists
674         read_config_file($GITWEB_CONFIG) or
675         read_config_file($GITWEB_CONFIG_SYSTEM);
678 # Get loadavg of system, to compare against $maxload.
679 # Currently it requires '/proc/loadavg' present to get loadavg;
680 # if it is not present it returns 0, which means no load checking.
681 sub get_loadavg {
682         if( -e '/proc/loadavg' ){
683                 open my $fd, '<', '/proc/loadavg'
684                         or return 0;
685                 my @load = split(/\s+/, scalar <$fd>);
686                 close $fd;
688                 # The first three columns measure CPU and IO utilization of the last one,
689                 # five, and 10 minute periods.  The fourth column shows the number of
690                 # currently running processes and the total number of processes in the m/n
691                 # format.  The last column displays the last process ID used.
692                 return $load[0] || 0;
693         }
694         # additional checks for load average should go here for things that don't export
695         # /proc/loadavg
697         return 0;
700 # version of the core git binary
701 our $git_version;
702 sub evaluate_git_version {
703         our $git_version = qx("$GIT" --version) =~ m/git version (.*)$/ ? $1 : "unknown";
704         $number_of_git_cmds++;
707 sub check_loadavg {
708         if (defined $maxload && get_loadavg() > $maxload) {
709                 die_error(503, "The load average on the server is too high");
710         }
713 # ======================================================================
714 # input validation and dispatch
716 # input parameters can be collected from a variety of sources (presently, CGI
717 # and PATH_INFO), so we define an %input_params hash that collects them all
718 # together during validation: this allows subsequent uses (e.g. href()) to be
719 # agnostic of the parameter origin
721 our %input_params = ();
723 # input parameters are stored with the long parameter name as key. This will
724 # also be used in the href subroutine to convert parameters to their CGI
725 # equivalent, and since the href() usage is the most frequent one, we store
726 # the name -> CGI key mapping here, instead of the reverse.
728 # XXX: Warning: If you touch this, check the search form for updating,
729 # too.
731 our @cgi_param_mapping = (
732         project => "p",
733         action => "a",
734         file_name => "f",
735         file_parent => "fp",
736         hash => "h",
737         hash_parent => "hp",
738         hash_base => "hb",
739         hash_parent_base => "hpb",
740         page => "pg",
741         order => "o",
742         searchtext => "s",
743         searchtype => "st",
744         snapshot_format => "sf",
745         extra_options => "opt",
746         search_use_regexp => "sr",
747         ctag => "by_tag",
748         # this must be last entry (for manipulation from JavaScript)
749         javascript => "js"
750 );
751 our %cgi_param_mapping = @cgi_param_mapping;
753 # we will also need to know the possible actions, for validation
754 our %actions = (
755         "blame" => \&git_blame,
756         "blame_incremental" => \&git_blame_incremental,
757         "blame_data" => \&git_blame_data,
758         "blobdiff" => \&git_blobdiff,
759         "blobdiff_plain" => \&git_blobdiff_plain,
760         "blob" => \&git_blob,
761         "blob_plain" => \&git_blob_plain,
762         "commitdiff" => \&git_commitdiff,
763         "commitdiff_plain" => \&git_commitdiff_plain,
764         "commit" => \&git_commit,
765         "forks" => \&git_forks,
766         "heads" => \&git_heads,
767         "history" => \&git_history,
768         "log" => \&git_log,
769         "patch" => \&git_patch,
770         "patches" => \&git_patches,
771         "remotes" => \&git_remotes,
772         "rss" => \&git_rss,
773         "atom" => \&git_atom,
774         "search" => \&git_search,
775         "search_help" => \&git_search_help,
776         "shortlog" => \&git_shortlog,
777         "summary" => \&git_summary,
778         "tag" => \&git_tag,
779         "tags" => \&git_tags,
780         "tree" => \&git_tree,
781         "snapshot" => \&git_snapshot,
782         "object" => \&git_object,
783         # those below don't need $project
784         "opml" => \&git_opml,
785         "project_list" => \&git_project_list,
786         "project_index" => \&git_project_index,
787 );
789 # finally, we have the hash of allowed extra_options for the commands that
790 # allow them
791 our %allowed_options = (
792         "--no-merges" => [ qw(rss atom log shortlog history) ],
793 );
795 # fill %input_params with the CGI parameters. All values except for 'opt'
796 # should be single values, but opt can be an array. We should probably
797 # build an array of parameters that can be multi-valued, but since for the time
798 # being it's only this one, we just single it out
799 sub evaluate_query_params {
800         our $cgi;
802         while (my ($name, $symbol) = each %cgi_param_mapping) {
803                 if ($symbol eq 'opt') {
804                         $input_params{$name} = [ $cgi->param($symbol) ];
805                 } else {
806                         $input_params{$name} = $cgi->param($symbol);
807                 }
808         }
811 # now read PATH_INFO and update the parameter list for missing parameters
812 sub evaluate_path_info {
813         return if defined $input_params{'project'};
814         return if !$path_info;
815         $path_info =~ s,^/+,,;
816         return if !$path_info;
818         # find which part of PATH_INFO is project
819         my $project = $path_info;
820         $project =~ s,/+$,,;
821         while ($project && !check_head_link("$projectroot/$project")) {
822                 $project =~ s,/*[^/]*$,,;
823         }
824         return unless $project;
825         $input_params{'project'} = $project;
827         # do not change any parameters if an action is given using the query string
828         return if $input_params{'action'};
829         $path_info =~ s,^\Q$project\E/*,,;
831         # next, check if we have an action
832         my $action = $path_info;
833         $action =~ s,/.*$,,;
834         if (exists $actions{$action}) {
835                 $path_info =~ s,^$action/*,,;
836                 $input_params{'action'} = $action;
837         }
839         # list of actions that want hash_base instead of hash, but can have no
840         # pathname (f) parameter
841         my @wants_base = (
842                 'tree',
843                 'history',
844         );
846         # we want to catch, among others
847         # [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name]
848         my ($parentrefname, $parentpathname, $refname, $pathname) =
849                 ($path_info =~ /^(?:(.+?)(?::(.+))?\.\.)?([^:]+?)?(?::(.+))?$/);
851         # first, analyze the 'current' part
852         if (defined $pathname) {
853                 # we got "branch:filename" or "branch:dir/"
854                 # we could use git_get_type(branch:pathname), but:
855                 # - it needs $git_dir
856                 # - it does a git() call
857                 # - the convention of terminating directories with a slash
858                 #   makes it superfluous
859                 # - embedding the action in the PATH_INFO would make it even
860                 #   more superfluous
861                 $pathname =~ s,^/+,,;
862                 if (!$pathname || substr($pathname, -1) eq "/") {
863                         $input_params{'action'} ||= "tree";
864                         $pathname =~ s,/$,,;
865                 } else {
866                         # the default action depends on whether we had parent info
867                         # or not
868                         if ($parentrefname) {
869                                 $input_params{'action'} ||= "blobdiff_plain";
870                         } else {
871                                 $input_params{'action'} ||= "blob_plain";
872                         }
873                 }
874                 $input_params{'hash_base'} ||= $refname;
875                 $input_params{'file_name'} ||= $pathname;
876         } elsif (defined $refname) {
877                 # we got "branch". In this case we have to choose if we have to
878                 # set hash or hash_base.
879                 #
880                 # Most of the actions without a pathname only want hash to be
881                 # set, except for the ones specified in @wants_base that want
882                 # hash_base instead. It should also be noted that hand-crafted
883                 # links having 'history' as an action and no pathname or hash
884                 # set will fail, but that happens regardless of PATH_INFO.
885                 if (defined $parentrefname) {
886                         # if there is parent let the default be 'shortlog' action
887                         # (for http://git.example.com/repo.git/A..B links); if there
888                         # is no parent, dispatch will detect type of object and set
889                         # action appropriately if required (if action is not set)
890                         $input_params{'action'} ||= "shortlog";
891                 }
892                 if ($input_params{'action'} &&
893                     grep { $_ eq $input_params{'action'} } @wants_base) {
894                         $input_params{'hash_base'} ||= $refname;
895                 } else {
896                         $input_params{'hash'} ||= $refname;
897                 }
898         }
900         # next, handle the 'parent' part, if present
901         if (defined $parentrefname) {
902                 # a missing pathspec defaults to the 'current' filename, allowing e.g.
903                 # someproject/blobdiff/oldrev..newrev:/filename
904                 if ($parentpathname) {
905                         $parentpathname =~ s,^/+,,;
906                         $parentpathname =~ s,/$,,;
907                         $input_params{'file_parent'} ||= $parentpathname;
908                 } else {
909                         $input_params{'file_parent'} ||= $input_params{'file_name'};
910                 }
911                 # we assume that hash_parent_base is wanted if a path was specified,
912                 # or if the action wants hash_base instead of hash
913                 if (defined $input_params{'file_parent'} ||
914                         grep { $_ eq $input_params{'action'} } @wants_base) {
915                         $input_params{'hash_parent_base'} ||= $parentrefname;
916                 } else {
917                         $input_params{'hash_parent'} ||= $parentrefname;
918                 }
919         }
921         # for the snapshot action, we allow URLs in the form
922         # $project/snapshot/$hash.ext
923         # where .ext determines the snapshot and gets removed from the
924         # passed $refname to provide the $hash.
925         #
926         # To be able to tell that $refname includes the format extension, we
927         # require the following two conditions to be satisfied:
928         # - the hash input parameter MUST have been set from the $refname part
929         #   of the URL (i.e. they must be equal)
930         # - the snapshot format MUST NOT have been defined already (e.g. from
931         #   CGI parameter sf)
932         # It's also useless to try any matching unless $refname has a dot,
933         # so we check for that too
934         if (defined $input_params{'action'} &&
935                 $input_params{'action'} eq 'snapshot' &&
936                 defined $refname && index($refname, '.') != -1 &&
937                 $refname eq $input_params{'hash'} &&
938                 !defined $input_params{'snapshot_format'}) {
939                 # We loop over the known snapshot formats, checking for
940                 # extensions. Allowed extensions are both the defined suffix
941                 # (which includes the initial dot already) and the snapshot
942                 # format key itself, with a prepended dot
943                 while (my ($fmt, $opt) = each %known_snapshot_formats) {
944                         my $hash = $refname;
945                         unless ($hash =~ s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) {
946                                 next;
947                         }
948                         my $sfx = $1;
949                         # a valid suffix was found, so set the snapshot format
950                         # and reset the hash parameter
951                         $input_params{'snapshot_format'} = $fmt;
952                         $input_params{'hash'} = $hash;
953                         # we also set the format suffix to the one requested
954                         # in the URL: this way a request for e.g. .tgz returns
955                         # a .tgz instead of a .tar.gz
956                         $known_snapshot_formats{$fmt}{'suffix'} = $sfx;
957                         last;
958                 }
959         }
962 our ($action, $project, $file_name, $file_parent, $hash, $hash_parent, $hash_base,
963      $hash_parent_base, @extra_options, $page, $searchtype, $search_use_regexp,
964      $searchtext, $search_regexp);
965 sub evaluate_and_validate_params {
966         our $action = $input_params{'action'};
967         if (defined $action) {
968                 if (!validate_action($action)) {
969                         die_error(400, "Invalid action parameter");
970                 }
971         }
973         # parameters which are pathnames
974         our $project = $input_params{'project'};
975         if (defined $project) {
976                 if (!validate_project($project)) {
977                         undef $project;
978                         die_error(404, "No such project");
979                 }
980         }
982         our $file_name = $input_params{'file_name'};
983         if (defined $file_name) {
984                 if (!validate_pathname($file_name)) {
985                         die_error(400, "Invalid file parameter");
986                 }
987         }
989         our $file_parent = $input_params{'file_parent'};
990         if (defined $file_parent) {
991                 if (!validate_pathname($file_parent)) {
992                         die_error(400, "Invalid file parent parameter");
993                 }
994         }
996         # parameters which are refnames
997         our $hash = $input_params{'hash'};
998         if (defined $hash) {
999                 if (!validate_refname($hash)) {
1000                         die_error(400, "Invalid hash parameter");
1001                 }
1002         }
1004         our $hash_parent = $input_params{'hash_parent'};
1005         if (defined $hash_parent) {
1006                 if (!validate_refname($hash_parent)) {
1007                         die_error(400, "Invalid hash parent parameter");
1008                 }
1009         }
1011         our $hash_base = $input_params{'hash_base'};
1012         if (defined $hash_base) {
1013                 if (!validate_refname($hash_base)) {
1014                         die_error(400, "Invalid hash base parameter");
1015                 }
1016         }
1018         our @extra_options = @{$input_params{'extra_options'}};
1019         # @extra_options is always defined, since it can only be (currently) set from
1020         # CGI, and $cgi->param() returns the empty array in array context if the param
1021         # is not set
1022         foreach my $opt (@extra_options) {
1023                 if (not exists $allowed_options{$opt}) {
1024                         die_error(400, "Invalid option parameter");
1025                 }
1026                 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
1027                         die_error(400, "Invalid option parameter for this action");
1028                 }
1029         }
1031         our $hash_parent_base = $input_params{'hash_parent_base'};
1032         if (defined $hash_parent_base) {
1033                 if (!validate_refname($hash_parent_base)) {
1034                         die_error(400, "Invalid hash parent base parameter");
1035                 }
1036         }
1038         # other parameters
1039         our $page = $input_params{'page'};
1040         if (defined $page) {
1041                 if ($page =~ m/[^0-9]/) {
1042                         die_error(400, "Invalid page parameter");
1043                 }
1044         }
1046         our $searchtype = $input_params{'searchtype'};
1047         if (defined $searchtype) {
1048                 if ($searchtype =~ m/[^a-z]/) {
1049                         die_error(400, "Invalid searchtype parameter");
1050                 }
1051         }
1053         our $search_use_regexp = $input_params{'search_use_regexp'};
1055         our $searchtext = $input_params{'searchtext'};
1056         our $search_regexp;
1057         if (defined $searchtext) {
1058                 if (length($searchtext) < 2) {
1059                         die_error(403, "At least two characters are required for search parameter");
1060                 }
1061                 $search_regexp = $search_use_regexp ? $searchtext : quotemeta $searchtext;
1062         }
1065 # path to the current git repository
1066 our $git_dir;
1067 sub evaluate_git_dir {
1068         our $git_dir = "$projectroot/$project" if $project;
1071 our (@snapshot_fmts, $git_avatar);
1072 sub configure_gitweb_features {
1073         # list of supported snapshot formats
1074         our @snapshot_fmts = gitweb_get_feature('snapshot');
1075         @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
1077         # check that the avatar feature is set to a known provider name,
1078         # and for each provider check if the dependencies are satisfied.
1079         # if the provider name is invalid or the dependencies are not met,
1080         # reset $git_avatar to the empty string.
1081         our ($git_avatar) = gitweb_get_feature('avatar');
1082         if ($git_avatar eq 'gravatar') {
1083                 $git_avatar = '' unless (eval { require Digest::MD5; 1; });
1084         } elsif ($git_avatar eq 'picon') {
1085                 # no dependencies
1086         } else {
1087                 $git_avatar = '';
1088         }
1091 # custom error handler: 'die <message>' is Internal Server Error
1092 sub handle_errors_html {
1093         my $msg = shift; # it is already HTML escaped
1095         # to avoid infinite loop where error occurs in die_error,
1096         # change handler to default handler, disabling handle_errors_html
1097         set_message("Error occured when inside die_error:\n$msg");
1099         # you cannot jump out of die_error when called as error handler;
1100         # the subroutine set via CGI::Carp::set_message is called _after_
1101         # HTTP headers are already written, so it cannot write them itself
1102         die_error(undef, undef, $msg, -error_handler => 1, -no_http_header => 1);
1104 set_message(\&handle_errors_html);
1106 # dispatch
1107 sub dispatch {
1108         if (!defined $action) {
1109                 if (defined $hash) {
1110                         $action = git_get_type($hash);
1111                 } elsif (defined $hash_base && defined $file_name) {
1112                         $action = git_get_type("$hash_base:$file_name");
1113                 } elsif (defined $project) {
1114                         $action = 'summary';
1115                 } else {
1116                         $action = 'project_list';
1117                 }
1118         }
1119         if (!defined($actions{$action})) {
1120                 die_error(400, "Unknown action");
1121         }
1122         if ($action !~ m/^(?:opml|project_list|project_index)$/ &&
1123             !$project) {
1124                 die_error(400, "Project needed");
1125         }
1126         $actions{$action}->();
1129 sub reset_timer {
1130         our $t0 = [ gettimeofday() ]
1131                 if defined $t0;
1132         our $number_of_git_cmds = 0;
1135 our $first_request = 1;
1136 sub run_request {
1137         reset_timer();
1139         evaluate_uri();
1140         if ($first_request) {
1141                 evaluate_gitweb_config();
1142                 evaluate_git_version();
1143         }
1144         if ($per_request_config) {
1145                 if (ref($per_request_config) eq 'CODE') {
1146                         $per_request_config->();
1147                 } elsif (!$first_request) {
1148                         evaluate_gitweb_config();
1149                 }
1150         }
1151         check_loadavg();
1153         # $projectroot and $projects_list might be set in gitweb config file
1154         $projects_list ||= $projectroot;
1156         evaluate_query_params();
1157         evaluate_path_info();
1158         evaluate_and_validate_params();
1159         evaluate_git_dir();
1161         configure_gitweb_features();
1163         dispatch();
1166 our $is_last_request = sub { 1 };
1167 our ($pre_dispatch_hook, $post_dispatch_hook, $pre_listen_hook);
1168 our $CGI = 'CGI';
1169 our $cgi;
1170 sub configure_as_fcgi {
1171         require CGI::Fast;
1172         our $CGI = 'CGI::Fast';
1174         my $request_number = 0;
1175         # let each child service 100 requests
1176         our $is_last_request = sub { ++$request_number > 100 };
1178 sub evaluate_argv {
1179         my $script_name = $ENV{'SCRIPT_NAME'} || $ENV{'SCRIPT_FILENAME'} || __FILE__;
1180         configure_as_fcgi()
1181                 if $script_name =~ /\.fcgi$/;
1183         return unless (@ARGV);
1185         require Getopt::Long;
1186         Getopt::Long::GetOptions(
1187                 'fastcgi|fcgi|f' => \&configure_as_fcgi,
1188                 'nproc|n=i' => sub {
1189                         my ($arg, $val) = @_;
1190                         return unless eval { require FCGI::ProcManager; 1; };
1191                         my $proc_manager = FCGI::ProcManager->new({
1192                                 n_processes => $val,
1193                         });
1194                         our $pre_listen_hook    = sub { $proc_manager->pm_manage()        };
1195                         our $pre_dispatch_hook  = sub { $proc_manager->pm_pre_dispatch()  };
1196                         our $post_dispatch_hook = sub { $proc_manager->pm_post_dispatch() };
1197                 },
1198         );
1201 sub run {
1202         evaluate_argv();
1204         $first_request = 1;
1205         $pre_listen_hook->()
1206                 if $pre_listen_hook;
1208  REQUEST:
1209         while ($cgi = $CGI->new()) {
1210                 $pre_dispatch_hook->()
1211                         if $pre_dispatch_hook;
1213                 run_request();
1215                 $post_dispatch_hook->()
1216                         if $post_dispatch_hook;
1217                 $first_request = 0;
1219                 last REQUEST if ($is_last_request->());
1220         }
1222  DONE_GITWEB:
1223         1;
1226 run();
1228 if (defined caller) {
1229         # wrapped in a subroutine processing requests,
1230         # e.g. mod_perl with ModPerl::Registry, or PSGI with Plack::App::WrapCGI
1231         return;
1232 } else {
1233         # pure CGI script, serving single request
1234         exit;
1237 ## ======================================================================
1238 ## action links
1240 # possible values of extra options
1241 # -full => 0|1      - use absolute/full URL ($my_uri/$my_url as base)
1242 # -replay => 1      - start from a current view (replay with modifications)
1243 # -path_info => 0|1 - don't use/use path_info URL (if possible)
1244 # -anchor => ANCHOR - add #ANCHOR to end of URL, implies -replay if used alone
1245 sub href {
1246         my %params = @_;
1247         # default is to use -absolute url() i.e. $my_uri
1248         my $href = $params{-full} ? $my_url : $my_uri;
1250         # implicit -replay, must be first of implicit params
1251         $params{-replay} = 1 if (keys %params == 1 && $params{-anchor});
1253         $params{'project'} = $project unless exists $params{'project'};
1255         if ($params{-replay}) {
1256                 while (my ($name, $symbol) = each %cgi_param_mapping) {
1257                         if (!exists $params{$name}) {
1258                                 $params{$name} = $input_params{$name};
1259                         }
1260                 }
1261         }
1263         my $use_pathinfo = gitweb_check_feature('pathinfo');
1264         if (defined $params{'project'} &&
1265             (exists $params{-path_info} ? $params{-path_info} : $use_pathinfo)) {
1266                 # try to put as many parameters as possible in PATH_INFO:
1267                 #   - project name
1268                 #   - action
1269                 #   - hash_parent or hash_parent_base:/file_parent
1270                 #   - hash or hash_base:/filename
1271                 #   - the snapshot_format as an appropriate suffix
1273                 # When the script is the root DirectoryIndex for the domain,
1274                 # $href here would be something like http://gitweb.example.com/
1275                 # Thus, we strip any trailing / from $href, to spare us double
1276                 # slashes in the final URL
1277                 $href =~ s,/$,,;
1279                 # Then add the project name, if present
1280                 $href .= "/".esc_path_info($params{'project'});
1281                 delete $params{'project'};
1283                 # since we destructively absorb parameters, we keep this
1284                 # boolean that remembers if we're handling a snapshot
1285                 my $is_snapshot = $params{'action'} eq 'snapshot';
1287                 # Summary just uses the project path URL, any other action is
1288                 # added to the URL
1289                 if (defined $params{'action'}) {
1290                         $href .= "/".esc_path_info($params{'action'})
1291                                 unless $params{'action'} eq 'summary';
1292                         delete $params{'action'};
1293                 }
1295                 # Next, we put hash_parent_base:/file_parent..hash_base:/file_name,
1296                 # stripping nonexistent or useless pieces
1297                 $href .= "/" if ($params{'hash_base'} || $params{'hash_parent_base'}
1298                         || $params{'hash_parent'} || $params{'hash'});
1299                 if (defined $params{'hash_base'}) {
1300                         if (defined $params{'hash_parent_base'}) {
1301                                 $href .= esc_path_info($params{'hash_parent_base'});
1302                                 # skip the file_parent if it's the same as the file_name
1303                                 if (defined $params{'file_parent'}) {
1304                                         if (defined $params{'file_name'} && $params{'file_parent'} eq $params{'file_name'}) {
1305                                                 delete $params{'file_parent'};
1306                                         } elsif ($params{'file_parent'} !~ /\.\./) {
1307                                                 $href .= ":/".esc_path_info($params{'file_parent'});
1308                                                 delete $params{'file_parent'};
1309                                         }
1310                                 }
1311                                 $href .= "..";
1312                                 delete $params{'hash_parent'};
1313                                 delete $params{'hash_parent_base'};
1314                         } elsif (defined $params{'hash_parent'}) {
1315                                 $href .= esc_path_info($params{'hash_parent'}). "..";
1316                                 delete $params{'hash_parent'};
1317                         }
1319                         $href .= esc_path_info($params{'hash_base'});
1320                         if (defined $params{'file_name'} && $params{'file_name'} !~ /\.\./) {
1321                                 $href .= ":/".esc_path_info($params{'file_name'});
1322                                 delete $params{'file_name'};
1323                         }
1324                         delete $params{'hash'};
1325                         delete $params{'hash_base'};
1326                 } elsif (defined $params{'hash'}) {
1327                         $href .= esc_path_info($params{'hash'});
1328                         delete $params{'hash'};
1329                 }
1331                 # If the action was a snapshot, we can absorb the
1332                 # snapshot_format parameter too
1333                 if ($is_snapshot) {
1334                         my $fmt = $params{'snapshot_format'};
1335                         # snapshot_format should always be defined when href()
1336                         # is called, but just in case some code forgets, we
1337                         # fall back to the default
1338                         $fmt ||= $snapshot_fmts[0];
1339                         $href .= $known_snapshot_formats{$fmt}{'suffix'};
1340                         delete $params{'snapshot_format'};
1341                 }
1342         }
1344         # now encode the parameters explicitly
1345         my @result = ();
1346         for (my $i = 0; $i < @cgi_param_mapping; $i += 2) {
1347                 my ($name, $symbol) = ($cgi_param_mapping[$i], $cgi_param_mapping[$i+1]);
1348                 if (defined $params{$name}) {
1349                         if (ref($params{$name}) eq "ARRAY") {
1350                                 foreach my $par (@{$params{$name}}) {
1351                                         push @result, $symbol . "=" . esc_param($par);
1352                                 }
1353                         } else {
1354                                 push @result, $symbol . "=" . esc_param($params{$name});
1355                         }
1356                 }
1357         }
1358         $href .= "?" . join(';', @result) if scalar @result;
1360         # final transformation: trailing spaces must be escaped (URI-encoded)
1361         $href =~ s/(\s+)$/CGI::escape($1)/e;
1363         if ($params{-anchor}) {
1364                 $href .= "#".esc_param($params{-anchor});
1365         }
1367         return $href;
1371 ## ======================================================================
1372 ## validation, quoting/unquoting and escaping
1374 sub validate_action {
1375         my $input = shift || return undef;
1376         return undef unless exists $actions{$input};
1377         return $input;
1380 sub validate_project {
1381         my $input = shift || return undef;
1382         if (!validate_pathname($input) ||
1383                 !(-d "$projectroot/$input") ||
1384                 !check_export_ok("$projectroot/$input") ||
1385                 ($strict_export && !project_in_list($input))) {
1386                 return undef;
1387         } else {
1388                 return $input;
1389         }
1392 sub validate_pathname {
1393         my $input = shift || return undef;
1395         # no '.' or '..' as elements of path, i.e. no '.' nor '..'
1396         # at the beginning, at the end, and between slashes.
1397         # also this catches doubled slashes
1398         if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
1399                 return undef;
1400         }
1401         # no null characters
1402         if ($input =~ m!\0!) {
1403                 return undef;
1404         }
1405         return $input;
1408 sub validate_refname {
1409         my $input = shift || return undef;
1411         # textual hashes are O.K.
1412         if ($input =~ m/^[0-9a-fA-F]{40}$/) {
1413                 return $input;
1414         }
1415         # it must be correct pathname
1416         $input = validate_pathname($input)
1417                 or return undef;
1418         # restrictions on ref name according to git-check-ref-format
1419         if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
1420                 return undef;
1421         }
1422         return $input;
1425 # decode sequences of octets in utf8 into Perl's internal form,
1426 # which is utf-8 with utf8 flag set if needed.  gitweb writes out
1427 # in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
1428 sub to_utf8 {
1429         my $str = shift;
1430         return undef unless defined $str;
1431         if (utf8::valid($str)) {
1432                 utf8::decode($str);
1433                 return $str;
1434         } else {
1435                 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
1436         }
1439 # quote unsafe chars, but keep the slash, even when it's not
1440 # correct, but quoted slashes look too horrible in bookmarks
1441 sub esc_param {
1442         my $str = shift;
1443         return undef unless defined $str;
1444         $str =~ s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;
1445         $str =~ s/ /\+/g;
1446         return $str;
1449 # the quoting rules for path_info fragment are slightly different
1450 sub esc_path_info {
1451         my $str = shift;
1452         return undef unless defined $str;
1454         # path_info doesn't treat '+' as space (specially), but '?' must be escaped
1455         $str =~ s/([^A-Za-z0-9\-_.~();\/;:@&= +]+)/CGI::escape($1)/eg;
1457         return $str;
1460 # quote unsafe chars in whole URL, so some characters cannot be quoted
1461 sub esc_url {
1462         my $str = shift;
1463         return undef unless defined $str;
1464         $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&= ]+)/CGI::escape($1)/eg;
1465         $str =~ s/ /\+/g;
1466         return $str;
1469 # quote unsafe characters in HTML attributes
1470 sub esc_attr {
1472         # for XHTML conformance escaping '"' to '&quot;' is not enough
1473         return esc_html(@_);
1476 # replace invalid utf8 character with SUBSTITUTION sequence
1477 sub esc_html {
1478         my $str = shift;
1479         my %opts = @_;
1481         return undef unless defined $str;
1483         $str = to_utf8($str);
1484         $str = $cgi->escapeHTML($str);
1485         if ($opts{'-nbsp'}) {
1486                 $str =~ s/ /&nbsp;/g;
1487         }
1488         $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
1489         return $str;
1492 # quote control characters and escape filename to HTML
1493 sub esc_path {
1494         my $str = shift;
1495         my %opts = @_;
1497         return undef unless defined $str;
1499         $str = to_utf8($str);
1500         $str = $cgi->escapeHTML($str);
1501         if ($opts{'-nbsp'}) {
1502                 $str =~ s/ /&nbsp;/g;
1503         }
1504         $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
1505         return $str;
1508 # Make control characters "printable", using character escape codes (CEC)
1509 sub quot_cec {
1510         my $cntrl = shift;
1511         my %opts = @_;
1512         my %es = ( # character escape codes, aka escape sequences
1513                 "\t" => '\t',   # tab            (HT)
1514                 "\n" => '\n',   # line feed      (LF)
1515                 "\r" => '\r',   # carrige return (CR)
1516                 "\f" => '\f',   # form feed      (FF)
1517                 "\b" => '\b',   # backspace      (BS)
1518                 "\a" => '\a',   # alarm (bell)   (BEL)
1519                 "\e" => '\e',   # escape         (ESC)
1520                 "\013" => '\v', # vertical tab   (VT)
1521                 "\000" => '\0', # nul character  (NUL)
1522         );
1523         my $chr = ( (exists $es{$cntrl})
1524                     ? $es{$cntrl}
1525                     : sprintf('\%2x', ord($cntrl)) );
1526         if ($opts{-nohtml}) {
1527                 return $chr;
1528         } else {
1529                 return "<span class=\"cntrl\">$chr</span>";
1530         }
1533 # Alternatively use unicode control pictures codepoints,
1534 # Unicode "printable representation" (PR)
1535 sub quot_upr {
1536         my $cntrl = shift;
1537         my %opts = @_;
1539         my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
1540         if ($opts{-nohtml}) {
1541                 return $chr;
1542         } else {
1543                 return "<span class=\"cntrl\">$chr</span>";
1544         }
1547 # git may return quoted and escaped filenames
1548 sub unquote {
1549         my $str = shift;
1551         sub unq {
1552                 my $seq = shift;
1553                 my %es = ( # character escape codes, aka escape sequences
1554                         't' => "\t",   # tab            (HT, TAB)
1555                         'n' => "\n",   # newline        (NL)
1556                         'r' => "\r",   # return         (CR)
1557                         'f' => "\f",   # form feed      (FF)
1558                         'b' => "\b",   # backspace      (BS)
1559                         'a' => "\a",   # alarm (bell)   (BEL)
1560                         'e' => "\e",   # escape         (ESC)
1561                         'v' => "\013", # vertical tab   (VT)
1562                 );
1564                 if ($seq =~ m/^[0-7]{1,3}$/) {
1565                         # octal char sequence
1566                         return chr(oct($seq));
1567                 } elsif (exists $es{$seq}) {
1568                         # C escape sequence, aka character escape code
1569                         return $es{$seq};
1570                 }
1571                 # quoted ordinary character
1572                 return $seq;
1573         }
1575         if ($str =~ m/^"(.*)"$/) {
1576                 # needs unquoting
1577                 $str = $1;
1578                 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
1579         }
1580         return $str;
1583 # escape tabs (convert tabs to spaces)
1584 sub untabify {
1585         my $line = shift;
1587         while ((my $pos = index($line, "\t")) != -1) {
1588                 if (my $count = (8 - ($pos % 8))) {
1589                         my $spaces = ' ' x $count;
1590                         $line =~ s/\t/$spaces/;
1591                 }
1592         }
1594         return $line;
1597 sub project_in_list {
1598         my $project = shift;
1599         my @list = git_get_projects_list();
1600         return @list && scalar(grep { $_->{'path'} eq $project } @list);
1603 ## ----------------------------------------------------------------------
1604 ## HTML aware string manipulation
1606 # Try to chop given string on a word boundary between position
1607 # $len and $len+$add_len. If there is no word boundary there,
1608 # chop at $len+$add_len. Do not chop if chopped part plus ellipsis
1609 # (marking chopped part) would be longer than given string.
1610 sub chop_str {
1611         my $str = shift;
1612         my $len = shift;
1613         my $add_len = shift || 10;
1614         my $where = shift || 'right'; # 'left' | 'center' | 'right'
1616         # Make sure perl knows it is utf8 encoded so we don't
1617         # cut in the middle of a utf8 multibyte char.
1618         $str = to_utf8($str);
1620         # allow only $len chars, but don't cut a word if it would fit in $add_len
1621         # if it doesn't fit, cut it if it's still longer than the dots we would add
1622         # remove chopped character entities entirely
1624         # when chopping in the middle, distribute $len into left and right part
1625         # return early if chopping wouldn't make string shorter
1626         if ($where eq 'center') {
1627                 return $str if ($len + 5 >= length($str)); # filler is length 5
1628                 $len = int($len/2);
1629         } else {
1630                 return $str if ($len + 4 >= length($str)); # filler is length 4
1631         }
1633         # regexps: ending and beginning with word part up to $add_len
1634         my $endre = qr/.{$len}\w{0,$add_len}/;
1635         my $begre = qr/\w{0,$add_len}.{$len}/;
1637         if ($where eq 'left') {
1638                 $str =~ m/^(.*?)($begre)$/;
1639                 my ($lead, $body) = ($1, $2);
1640                 if (length($lead) > 4) {
1641                         $lead = " ...";
1642                 }
1643                 return "$lead$body";
1645         } elsif ($where eq 'center') {
1646                 $str =~ m/^($endre)(.*)$/;
1647                 my ($left, $str)  = ($1, $2);
1648                 $str =~ m/^(.*?)($begre)$/;
1649                 my ($mid, $right) = ($1, $2);
1650                 if (length($mid) > 5) {
1651                         $mid = " ... ";
1652                 }
1653                 return "$left$mid$right";
1655         } else {
1656                 $str =~ m/^($endre)(.*)$/;
1657                 my $body = $1;
1658                 my $tail = $2;
1659                 if (length($tail) > 4) {
1660                         $tail = "... ";
1661                 }
1662                 return "$body$tail";
1663         }
1666 # takes the same arguments as chop_str, but also wraps a <span> around the
1667 # result with a title attribute if it does get chopped. Additionally, the
1668 # string is HTML-escaped.
1669 sub chop_and_escape_str {
1670         my ($str) = @_;
1672         my $chopped = chop_str(@_);
1673         if ($chopped eq $str) {
1674                 return esc_html($chopped);
1675         } else {
1676                 $str =~ s/[[:cntrl:]]/?/g;
1677                 return $cgi->span({-title=>$str}, esc_html($chopped));
1678         }
1681 ## ----------------------------------------------------------------------
1682 ## functions returning short strings
1684 # CSS class for given age value (in seconds)
1685 sub age_class {
1686         my $age = shift;
1688         if (!defined $age) {
1689                 return "noage";
1690         } elsif ($age < 60*60*2) {
1691                 return "age0";
1692         } elsif ($age < 60*60*24*2) {
1693                 return "age1";
1694         } else {
1695                 return "age2";
1696         }
1699 # convert age in seconds to "nn units ago" string
1700 sub age_string {
1701         my $age = shift;
1702         my $age_str;
1704         if ($age > 60*60*24*365*2) {
1705                 $age_str = (int $age/60/60/24/365);
1706                 $age_str .= " years ago";
1707         } elsif ($age > 60*60*24*(365/12)*2) {
1708                 $age_str = int $age/60/60/24/(365/12);
1709                 $age_str .= " months ago";
1710         } elsif ($age > 60*60*24*7*2) {
1711                 $age_str = int $age/60/60/24/7;
1712                 $age_str .= " weeks ago";
1713         } elsif ($age > 60*60*24*2) {
1714                 $age_str = int $age/60/60/24;
1715                 $age_str .= " days ago";
1716         } elsif ($age > 60*60*2) {
1717                 $age_str = int $age/60/60;
1718                 $age_str .= " hours ago";
1719         } elsif ($age > 60*2) {
1720                 $age_str = int $age/60;
1721                 $age_str .= " min ago";
1722         } elsif ($age > 2) {
1723                 $age_str = int $age;
1724                 $age_str .= " sec ago";
1725         } else {
1726                 $age_str .= " right now";
1727         }
1728         return $age_str;
1731 use constant {
1732         S_IFINVALID => 0030000,
1733         S_IFGITLINK => 0160000,
1734 };
1736 # submodule/subproject, a commit object reference
1737 sub S_ISGITLINK {
1738         my $mode = shift;
1740         return (($mode & S_IFMT) == S_IFGITLINK)
1743 # convert file mode in octal to symbolic file mode string
1744 sub mode_str {
1745         my $mode = oct shift;
1747         if (S_ISGITLINK($mode)) {
1748                 return 'm---------';
1749         } elsif (S_ISDIR($mode & S_IFMT)) {
1750                 return 'drwxr-xr-x';
1751         } elsif (S_ISLNK($mode)) {
1752                 return 'lrwxrwxrwx';
1753         } elsif (S_ISREG($mode)) {
1754                 # git cares only about the executable bit
1755                 if ($mode & S_IXUSR) {
1756                         return '-rwxr-xr-x';
1757                 } else {
1758                         return '-rw-r--r--';
1759                 };
1760         } else {
1761                 return '----------';
1762         }
1765 # convert file mode in octal to file type string
1766 sub file_type {
1767         my $mode = shift;
1769         if ($mode !~ m/^[0-7]+$/) {
1770                 return $mode;
1771         } else {
1772                 $mode = oct $mode;
1773         }
1775         if (S_ISGITLINK($mode)) {
1776                 return "submodule";
1777         } elsif (S_ISDIR($mode & S_IFMT)) {
1778                 return "directory";
1779         } elsif (S_ISLNK($mode)) {
1780                 return "symlink";
1781         } elsif (S_ISREG($mode)) {
1782                 return "file";
1783         } else {
1784                 return "unknown";
1785         }
1788 # convert file mode in octal to file type description string
1789 sub file_type_long {
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                 if ($mode & S_IXUSR) {
1806                         return "executable";
1807                 } else {
1808                         return "file";
1809                 };
1810         } else {
1811                 return "unknown";
1812         }
1816 ## ----------------------------------------------------------------------
1817 ## functions returning short HTML fragments, or transforming HTML fragments
1818 ## which don't belong to other sections
1820 # format line of commit message.
1821 sub format_log_line_html {
1822         my $line = shift;
1824         $line = esc_html($line, -nbsp=>1);
1825         $line =~ s{\b([0-9a-fA-F]{8,40})\b}{
1826                 $cgi->a({-href => href(action=>"object", hash=>$1),
1827                                         -class => "text"}, $1);
1828         }eg;
1830         return $line;
1833 # format marker of refs pointing to given object
1835 # the destination action is chosen based on object type and current context:
1836 # - for annotated tags, we choose the tag view unless it's the current view
1837 #   already, in which case we go to shortlog view
1838 # - for other refs, we keep the current view if we're in history, shortlog or
1839 #   log view, and select shortlog otherwise
1840 sub format_ref_marker {
1841         my ($refs, $id) = @_;
1842         my $markers = '';
1844         if (defined $refs->{$id}) {
1845                 foreach my $ref (@{$refs->{$id}}) {
1846                         # this code exploits the fact that non-lightweight tags are the
1847                         # only indirect objects, and that they are the only objects for which
1848                         # we want to use tag instead of shortlog as action
1849                         my ($type, $name) = qw();
1850                         my $indirect = ($ref =~ s/\^\{\}$//);
1851                         # e.g. tags/v2.6.11 or heads/next
1852                         if ($ref =~ m!^(.*?)s?/(.*)$!) {
1853                                 $type = $1;
1854                                 $name = $2;
1855                         } else {
1856                                 $type = "ref";
1857                                 $name = $ref;
1858                         }
1860                         my $class = $type;
1861                         $class .= " indirect" if $indirect;
1863                         my $dest_action = "shortlog";
1865                         if ($indirect) {
1866                                 $dest_action = "tag" unless $action eq "tag";
1867                         } elsif ($action =~ /^(history|(short)?log)$/) {
1868                                 $dest_action = $action;
1869                         }
1871                         my $dest = "";
1872                         $dest .= "refs/" unless $ref =~ m!^refs/!;
1873                         $dest .= $ref;
1875                         my $link = $cgi->a({
1876                                 -href => href(
1877                                         action=>$dest_action,
1878                                         hash=>$dest
1879                                 )}, $name);
1881                         $markers .= " <span class=\"".esc_attr($class)."\" title=\"".esc_attr($ref)."\">" .
1882                                 $link . "</span>";
1883                 }
1884         }
1886         if ($markers) {
1887                 return ' <span class="refs">'. $markers . '</span>';
1888         } else {
1889                 return "";
1890         }
1893 # format, perhaps shortened and with markers, title line
1894 sub format_subject_html {
1895         my ($long, $short, $href, $extra) = @_;
1896         $extra = '' unless defined($extra);
1898         if (length($short) < length($long)) {
1899                 $long =~ s/[[:cntrl:]]/?/g;
1900                 return $cgi->a({-href => $href, -class => "list subject",
1901                                 -title => to_utf8($long)},
1902                        esc_html($short)) . $extra;
1903         } else {
1904                 return $cgi->a({-href => $href, -class => "list subject"},
1905                        esc_html($long)) . $extra;
1906         }
1909 # Rather than recomputing the url for an email multiple times, we cache it
1910 # after the first hit. This gives a visible benefit in views where the avatar
1911 # for the same email is used repeatedly (e.g. shortlog).
1912 # The cache is shared by all avatar engines (currently gravatar only), which
1913 # are free to use it as preferred. Since only one avatar engine is used for any
1914 # given page, there's no risk for cache conflicts.
1915 our %avatar_cache = ();
1917 # Compute the picon url for a given email, by using the picon search service over at
1918 # http://www.cs.indiana.edu/picons/search.html
1919 sub picon_url {
1920         my $email = lc shift;
1921         if (!$avatar_cache{$email}) {
1922                 my ($user, $domain) = split('@', $email);
1923                 $avatar_cache{$email} =
1924                         "http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/" .
1925                         "$domain/$user/" .
1926                         "users+domains+unknown/up/single";
1927         }
1928         return $avatar_cache{$email};
1931 # Compute the gravatar url for a given email, if it's not in the cache already.
1932 # Gravatar stores only the part of the URL before the size, since that's the
1933 # one computationally more expensive. This also allows reuse of the cache for
1934 # different sizes (for this particular engine).
1935 sub gravatar_url {
1936         my $email = lc shift;
1937         my $size = shift;
1938         $avatar_cache{$email} ||=
1939                 "http://www.gravatar.com/avatar/" .
1940                         Digest::MD5::md5_hex($email) . "?s=";
1941         return $avatar_cache{$email} . $size;
1944 # Insert an avatar for the given $email at the given $size if the feature
1945 # is enabled.
1946 sub git_get_avatar {
1947         my ($email, %opts) = @_;
1948         my $pre_white  = ($opts{-pad_before} ? "&nbsp;" : "");
1949         my $post_white = ($opts{-pad_after}  ? "&nbsp;" : "");
1950         $opts{-size} ||= 'default';
1951         my $size = $avatar_size{$opts{-size}} || $avatar_size{'default'};
1952         my $url = "";
1953         if ($git_avatar eq 'gravatar') {
1954                 $url = gravatar_url($email, $size);
1955         } elsif ($git_avatar eq 'picon') {
1956                 $url = picon_url($email);
1957         }
1958         # Other providers can be added by extending the if chain, defining $url
1959         # as needed. If no variant puts something in $url, we assume avatars
1960         # are completely disabled/unavailable.
1961         if ($url) {
1962                 return $pre_white .
1963                        "<img width=\"$size\" " .
1964                             "class=\"avatar\" " .
1965                             "src=\"".esc_url($url)."\" " .
1966                             "alt=\"\" " .
1967                        "/>" . $post_white;
1968         } else {
1969                 return "";
1970         }
1973 sub format_search_author {
1974         my ($author, $searchtype, $displaytext) = @_;
1975         my $have_search = gitweb_check_feature('search');
1977         if ($have_search) {
1978                 my $performed = "";
1979                 if ($searchtype eq 'author') {
1980                         $performed = "authored";
1981                 } elsif ($searchtype eq 'committer') {
1982                         $performed = "committed";
1983                 }
1985                 return $cgi->a({-href => href(action=>"search", hash=>$hash,
1986                                 searchtext=>$author,
1987                                 searchtype=>$searchtype), class=>"list",
1988                                 title=>"Search for commits $performed by $author"},
1989                                 $displaytext);
1991         } else {
1992                 return $displaytext;
1993         }
1996 # format the author name of the given commit with the given tag
1997 # the author name is chopped and escaped according to the other
1998 # optional parameters (see chop_str).
1999 sub format_author_html {
2000         my $tag = shift;
2001         my $co = shift;
2002         my $author = chop_and_escape_str($co->{'author_name'}, @_);
2003         return "<$tag class=\"author\">" .
2004                format_search_author($co->{'author_name'}, "author",
2005                        git_get_avatar($co->{'author_email'}, -pad_after => 1) .
2006                        $author) .
2007                "</$tag>";
2010 # format git diff header line, i.e. "diff --(git|combined|cc) ..."
2011 sub format_git_diff_header_line {
2012         my $line = shift;
2013         my $diffinfo = shift;
2014         my ($from, $to) = @_;
2016         if ($diffinfo->{'nparents'}) {
2017                 # combined diff
2018                 $line =~ s!^(diff (.*?) )"?.*$!$1!;
2019                 if ($to->{'href'}) {
2020                         $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
2021                                          esc_path($to->{'file'}));
2022                 } else { # file was deleted (no href)
2023                         $line .= esc_path($to->{'file'});
2024                 }
2025         } else {
2026                 # "ordinary" diff
2027                 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
2028                 if ($from->{'href'}) {
2029                         $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
2030                                          'a/' . esc_path($from->{'file'}));
2031                 } else { # file was added (no href)
2032                         $line .= 'a/' . esc_path($from->{'file'});
2033                 }
2034                 $line .= ' ';
2035                 if ($to->{'href'}) {
2036                         $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
2037                                          'b/' . esc_path($to->{'file'}));
2038                 } else { # file was deleted
2039                         $line .= 'b/' . esc_path($to->{'file'});
2040                 }
2041         }
2043         return "<div class=\"diff header\">$line</div>\n";
2046 # format extended diff header line, before patch itself
2047 sub format_extended_diff_header_line {
2048         my $line = shift;
2049         my $diffinfo = shift;
2050         my ($from, $to) = @_;
2052         # match <path>
2053         if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
2054                 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
2055                                        esc_path($from->{'file'}));
2056         }
2057         if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
2058                 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
2059                                  esc_path($to->{'file'}));
2060         }
2061         # match single <mode>
2062         if ($line =~ m/\s(\d{6})$/) {
2063                 $line .= '<span class="info"> (' .
2064                          file_type_long($1) .
2065                          ')</span>';
2066         }
2067         # match <hash>
2068         if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
2069                 # can match only for combined diff
2070                 $line = 'index ';
2071                 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2072                         if ($from->{'href'}[$i]) {
2073                                 $line .= $cgi->a({-href=>$from->{'href'}[$i],
2074                                                   -class=>"hash"},
2075                                                  substr($diffinfo->{'from_id'}[$i],0,7));
2076                         } else {
2077                                 $line .= '0' x 7;
2078                         }
2079                         # separator
2080                         $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
2081                 }
2082                 $line .= '..';
2083                 if ($to->{'href'}) {
2084                         $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
2085                                          substr($diffinfo->{'to_id'},0,7));
2086                 } else {
2087                         $line .= '0' x 7;
2088                 }
2090         } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
2091                 # can match only for ordinary diff
2092                 my ($from_link, $to_link);
2093                 if ($from->{'href'}) {
2094                         $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
2095                                              substr($diffinfo->{'from_id'},0,7));
2096                 } else {
2097                         $from_link = '0' x 7;
2098                 }
2099                 if ($to->{'href'}) {
2100                         $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
2101                                            substr($diffinfo->{'to_id'},0,7));
2102                 } else {
2103                         $to_link = '0' x 7;
2104                 }
2105                 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
2106                 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
2107         }
2109         return $line . "<br/>\n";
2112 # format from-file/to-file diff header
2113 sub format_diff_from_to_header {
2114         my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
2115         my $line;
2116         my $result = '';
2118         $line = $from_line;
2119         #assert($line =~ m/^---/) if DEBUG;
2120         # no extra formatting for "^--- /dev/null"
2121         if (! $diffinfo->{'nparents'}) {
2122                 # ordinary (single parent) diff
2123                 if ($line =~ m!^--- "?a/!) {
2124                         if ($from->{'href'}) {
2125                                 $line = '--- a/' .
2126                                         $cgi->a({-href=>$from->{'href'}, -class=>"path"},
2127                                                 esc_path($from->{'file'}));
2128                         } else {
2129                                 $line = '--- a/' .
2130                                         esc_path($from->{'file'});
2131                         }
2132                 }
2133                 $result .= qq!<div class="diff from_file">$line</div>\n!;
2135         } else {
2136                 # combined diff (merge commit)
2137                 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2138                         if ($from->{'href'}[$i]) {
2139                                 $line = '--- ' .
2140                                         $cgi->a({-href=>href(action=>"blobdiff",
2141                                                              hash_parent=>$diffinfo->{'from_id'}[$i],
2142                                                              hash_parent_base=>$parents[$i],
2143                                                              file_parent=>$from->{'file'}[$i],
2144                                                              hash=>$diffinfo->{'to_id'},
2145                                                              hash_base=>$hash,
2146                                                              file_name=>$to->{'file'}),
2147                                                  -class=>"path",
2148                                                  -title=>"diff" . ($i+1)},
2149                                                 $i+1) .
2150                                         '/' .
2151                                         $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
2152                                                 esc_path($from->{'file'}[$i]));
2153                         } else {
2154                                 $line = '--- /dev/null';
2155                         }
2156                         $result .= qq!<div class="diff from_file">$line</div>\n!;
2157                 }
2158         }
2160         $line = $to_line;
2161         #assert($line =~ m/^\+\+\+/) if DEBUG;
2162         # no extra formatting for "^+++ /dev/null"
2163         if ($line =~ m!^\+\+\+ "?b/!) {
2164                 if ($to->{'href'}) {
2165                         $line = '+++ b/' .
2166                                 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
2167                                         esc_path($to->{'file'}));
2168                 } else {
2169                         $line = '+++ b/' .
2170                                 esc_path($to->{'file'});
2171                 }
2172         }
2173         $result .= qq!<div class="diff to_file">$line</div>\n!;
2175         return $result;
2178 # create note for patch simplified by combined diff
2179 sub format_diff_cc_simplified {
2180         my ($diffinfo, @parents) = @_;
2181         my $result = '';
2183         $result .= "<div class=\"diff header\">" .
2184                    "diff --cc ";
2185         if (!is_deleted($diffinfo)) {
2186                 $result .= $cgi->a({-href => href(action=>"blob",
2187                                                   hash_base=>$hash,
2188                                                   hash=>$diffinfo->{'to_id'},
2189                                                   file_name=>$diffinfo->{'to_file'}),
2190                                     -class => "path"},
2191                                    esc_path($diffinfo->{'to_file'}));
2192         } else {
2193                 $result .= esc_path($diffinfo->{'to_file'});
2194         }
2195         $result .= "</div>\n" . # class="diff header"
2196                    "<div class=\"diff nodifferences\">" .
2197                    "Simple merge" .
2198                    "</div>\n"; # class="diff nodifferences"
2200         return $result;
2203 # format patch (diff) line (not to be used for diff headers)
2204 sub format_diff_line {
2205         my $line = shift;
2206         my ($from, $to) = @_;
2207         my $diff_class = "";
2209         chomp $line;
2211         if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
2212                 # combined diff
2213                 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
2214                 if ($line =~ m/^\@{3}/) {
2215                         $diff_class = " chunk_header";
2216                 } elsif ($line =~ m/^\\/) {
2217                         $diff_class = " incomplete";
2218                 } elsif ($prefix =~ tr/+/+/) {
2219                         $diff_class = " add";
2220                 } elsif ($prefix =~ tr/-/-/) {
2221                         $diff_class = " rem";
2222                 }
2223         } else {
2224                 # assume ordinary diff
2225                 my $char = substr($line, 0, 1);
2226                 if ($char eq '+') {
2227                         $diff_class = " add";
2228                 } elsif ($char eq '-') {
2229                         $diff_class = " rem";
2230                 } elsif ($char eq '@') {
2231                         $diff_class = " chunk_header";
2232                 } elsif ($char eq "\\") {
2233                         $diff_class = " incomplete";
2234                 }
2235         }
2236         $line = untabify($line);
2237         if ($from && $to && $line =~ m/^\@{2} /) {
2238                 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
2239                         $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
2241                 $from_lines = 0 unless defined $from_lines;
2242                 $to_lines   = 0 unless defined $to_lines;
2244                 if ($from->{'href'}) {
2245                         $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
2246                                              -class=>"list"}, $from_text);
2247                 }
2248                 if ($to->{'href'}) {
2249                         $to_text   = $cgi->a({-href=>"$to->{'href'}#l$to_start",
2250                                              -class=>"list"}, $to_text);
2251                 }
2252                 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
2253                         "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
2254                 return "<div class=\"diff$diff_class\">$line</div>\n";
2255         } elsif ($from && $to && $line =~ m/^\@{3}/) {
2256                 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
2257                 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
2259                 @from_text = split(' ', $ranges);
2260                 for (my $i = 0; $i < @from_text; ++$i) {
2261                         ($from_start[$i], $from_nlines[$i]) =
2262                                 (split(',', substr($from_text[$i], 1)), 0);
2263                 }
2265                 $to_text   = pop @from_text;
2266                 $to_start  = pop @from_start;
2267                 $to_nlines = pop @from_nlines;
2269                 $line = "<span class=\"chunk_info\">$prefix ";
2270                 for (my $i = 0; $i < @from_text; ++$i) {
2271                         if ($from->{'href'}[$i]) {
2272                                 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
2273                                                   -class=>"list"}, $from_text[$i]);
2274                         } else {
2275                                 $line .= $from_text[$i];
2276                         }
2277                         $line .= " ";
2278                 }
2279                 if ($to->{'href'}) {
2280                         $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
2281                                           -class=>"list"}, $to_text);
2282                 } else {
2283                         $line .= $to_text;
2284                 }
2285                 $line .= " $prefix</span>" .
2286                          "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
2287                 return "<div class=\"diff$diff_class\">$line</div>\n";
2288         }
2289         return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
2292 # Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
2293 # linked.  Pass the hash of the tree/commit to snapshot.
2294 sub format_snapshot_links {
2295         my ($hash) = @_;
2296         my $num_fmts = @snapshot_fmts;
2297         if ($num_fmts > 1) {
2298                 # A parenthesized list of links bearing format names.
2299                 # e.g. "snapshot (_tar.gz_ _zip_)"
2300                 return "snapshot (" . join(' ', map
2301                         $cgi->a({
2302                                 -href => href(
2303                                         action=>"snapshot",
2304                                         hash=>$hash,
2305                                         snapshot_format=>$_
2306                                 )
2307                         }, $known_snapshot_formats{$_}{'display'})
2308                 , @snapshot_fmts) . ")";
2309         } elsif ($num_fmts == 1) {
2310                 # A single "snapshot" link whose tooltip bears the format name.
2311                 # i.e. "_snapshot_"
2312                 my ($fmt) = @snapshot_fmts;
2313                 return
2314                         $cgi->a({
2315                                 -href => href(
2316                                         action=>"snapshot",
2317                                         hash=>$hash,
2318                                         snapshot_format=>$fmt
2319                                 ),
2320                                 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
2321                         }, "snapshot");
2322         } else { # $num_fmts == 0
2323                 return undef;
2324         }
2327 ## ......................................................................
2328 ## functions returning values to be passed, perhaps after some
2329 ## transformation, to other functions; e.g. returning arguments to href()
2331 # returns hash to be passed to href to generate gitweb URL
2332 # in -title key it returns description of link
2333 sub get_feed_info {
2334         my $format = shift || 'Atom';
2335         my %res = (action => lc($format));
2337         # feed links are possible only for project views
2338         return unless (defined $project);
2339         # some views should link to OPML, or to generic project feed,
2340         # or don't have specific feed yet (so they should use generic)
2341         return if ($action =~ /^(?:tags|heads|forks|tag|search)$/x);
2343         my $branch;
2344         # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
2345         # from tag links; this also makes possible to detect branch links
2346         if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
2347             (defined $hash      && $hash      =~ m!^refs/heads/(.*)$!)) {
2348                 $branch = $1;
2349         }
2350         # find log type for feed description (title)
2351         my $type = 'log';
2352         if (defined $file_name) {
2353                 $type  = "history of $file_name";
2354                 $type .= "/" if ($action eq 'tree');
2355                 $type .= " on '$branch'" if (defined $branch);
2356         } else {
2357                 $type = "log of $branch" if (defined $branch);
2358         }
2360         $res{-title} = $type;
2361         $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
2362         $res{'file_name'} = $file_name;
2364         return %res;
2367 ## ----------------------------------------------------------------------
2368 ## git utility subroutines, invoking git commands
2370 # returns path to the core git executable and the --git-dir parameter as list
2371 sub git_cmd {
2372         $number_of_git_cmds++;
2373         return $GIT, '--git-dir='.$git_dir;
2376 # quote the given arguments for passing them to the shell
2377 # quote_command("command", "arg 1", "arg with ' and ! characters")
2378 # => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
2379 # Try to avoid using this function wherever possible.
2380 sub quote_command {
2381         return join(' ',
2382                 map { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ );
2385 # get HEAD ref of given project as hash
2386 sub git_get_head_hash {
2387         return git_get_full_hash(shift, 'HEAD');
2390 sub git_get_full_hash {
2391         return git_get_hash(@_);
2394 sub git_get_short_hash {
2395         return git_get_hash(@_, '--short=7');
2398 sub git_get_hash {
2399         my ($project, $hash, @options) = @_;
2400         my $o_git_dir = $git_dir;
2401         my $retval = undef;
2402         $git_dir = "$projectroot/$project";
2403         if (open my $fd, '-|', git_cmd(), 'rev-parse',
2404             '--verify', '-q', @options, $hash) {
2405                 $retval = <$fd>;
2406                 chomp $retval if defined $retval;
2407                 close $fd;
2408         }
2409         if (defined $o_git_dir) {
2410                 $git_dir = $o_git_dir;
2411         }
2412         return $retval;
2415 # get type of given object
2416 sub git_get_type {
2417         my $hash = shift;
2419         open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
2420         my $type = <$fd>;
2421         close $fd or return;
2422         chomp $type;
2423         return $type;
2426 # repository configuration
2427 our $config_file = '';
2428 our %config;
2430 # store multiple values for single key as anonymous array reference
2431 # single values stored directly in the hash, not as [ <value> ]
2432 sub hash_set_multi {
2433         my ($hash, $key, $value) = @_;
2435         if (!exists $hash->{$key}) {
2436                 $hash->{$key} = $value;
2437         } elsif (!ref $hash->{$key}) {
2438                 $hash->{$key} = [ $hash->{$key}, $value ];
2439         } else {
2440                 push @{$hash->{$key}}, $value;
2441         }
2444 # return hash of git project configuration
2445 # optionally limited to some section, e.g. 'gitweb'
2446 sub git_parse_project_config {
2447         my $section_regexp = shift;
2448         my %config;
2450         local $/ = "\0";
2452         open my $fh, "-|", git_cmd(), "config", '-z', '-l',
2453                 or return;
2455         while (my $keyval = <$fh>) {
2456                 chomp $keyval;
2457                 my ($key, $value) = split(/\n/, $keyval, 2);
2459                 hash_set_multi(\%config, $key, $value)
2460                         if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
2461         }
2462         close $fh;
2464         return %config;
2467 # convert config value to boolean: 'true' or 'false'
2468 # no value, number > 0, 'true' and 'yes' values are true
2469 # rest of values are treated as false (never as error)
2470 sub config_to_bool {
2471         my $val = shift;
2473         return 1 if !defined $val;             # section.key
2475         # strip leading and trailing whitespace
2476         $val =~ s/^\s+//;
2477         $val =~ s/\s+$//;
2479         return (($val =~ /^\d+$/ && $val) ||   # section.key = 1
2480                 ($val =~ /^(?:true|yes)$/i));  # section.key = true
2483 # convert config value to simple decimal number
2484 # an optional value suffix of 'k', 'm', or 'g' will cause the value
2485 # to be multiplied by 1024, 1048576, or 1073741824
2486 sub config_to_int {
2487         my $val = shift;
2489         # strip leading and trailing whitespace
2490         $val =~ s/^\s+//;
2491         $val =~ s/\s+$//;
2493         if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
2494                 $unit = lc($unit);
2495                 # unknown unit is treated as 1
2496                 return $num * ($unit eq 'g' ? 1073741824 :
2497                                $unit eq 'm' ?    1048576 :
2498                                $unit eq 'k' ?       1024 : 1);
2499         }
2500         return $val;
2503 # convert config value to array reference, if needed
2504 sub config_to_multi {
2505         my $val = shift;
2507         return ref($val) ? $val : (defined($val) ? [ $val ] : []);
2510 sub git_get_project_config {
2511         my ($key, $type) = @_;
2513         return unless defined $git_dir;
2515         # key sanity check
2516         return unless ($key);
2517         $key =~ s/^gitweb\.//;
2518         return if ($key =~ m/\W/);
2520         # type sanity check
2521         if (defined $type) {
2522                 $type =~ s/^--//;
2523                 $type = undef
2524                         unless ($type eq 'bool' || $type eq 'int');
2525         }
2527         # get config
2528         if (!defined $config_file ||
2529             $config_file ne "$git_dir/config") {
2530                 %config = git_parse_project_config('gitweb');
2531                 $config_file = "$git_dir/config";
2532         }
2534         # check if config variable (key) exists
2535         return unless exists $config{"gitweb.$key"};
2537         # ensure given type
2538         if (!defined $type) {
2539                 return $config{"gitweb.$key"};
2540         } elsif ($type eq 'bool') {
2541                 # backward compatibility: 'git config --bool' returns true/false
2542                 return config_to_bool($config{"gitweb.$key"}) ? 'true' : 'false';
2543         } elsif ($type eq 'int') {
2544                 return config_to_int($config{"gitweb.$key"});
2545         }
2546         return $config{"gitweb.$key"};
2549 # get hash of given path at given ref
2550 sub git_get_hash_by_path {
2551         my $base = shift;
2552         my $path = shift || return undef;
2553         my $type = shift;
2555         $path =~ s,/+$,,;
2557         open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
2558                 or die_error(500, "Open git-ls-tree failed");
2559         my $line = <$fd>;
2560         close $fd or return undef;
2562         if (!defined $line) {
2563                 # there is no tree or hash given by $path at $base
2564                 return undef;
2565         }
2567         #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa  panic.c'
2568         $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
2569         if (defined $type && $type ne $2) {
2570                 # type doesn't match
2571                 return undef;
2572         }
2573         return $3;
2576 # get path of entry with given hash at given tree-ish (ref)
2577 # used to get 'from' filename for combined diff (merge commit) for renames
2578 sub git_get_path_by_hash {
2579         my $base = shift || return;
2580         my $hash = shift || return;
2582         local $/ = "\0";
2584         open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
2585                 or return undef;
2586         while (my $line = <$fd>) {
2587                 chomp $line;
2589                 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423  gitweb'
2590                 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f  gitweb/README'
2591                 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
2592                         close $fd;
2593                         return $1;
2594                 }
2595         }
2596         close $fd;
2597         return undef;
2600 ## ......................................................................
2601 ## git utility functions, directly accessing git repository
2603 # get the value of config variable either from file named as the variable
2604 # itself in the repository ($GIT_DIR/$name file), or from gitweb.$name
2605 # configuration variable in the repository config file.
2606 sub git_get_file_or_project_config {
2607         my ($path, $name) = @_;
2609         $git_dir = "$projectroot/$path";
2610         open my $fd, '<', "$git_dir/$name"
2611                 or return git_get_project_config($name);
2612         my $conf = <$fd>;
2613         close $fd;
2614         if (defined $conf) {
2615                 chomp $conf;
2616         }
2617         return $conf;
2620 sub git_get_project_description {
2621         my $path = shift;
2622         return git_get_file_or_project_config($path, 'description');
2625 sub git_get_project_category {
2626         my $path = shift;
2627         return git_get_file_or_project_config($path, 'category');
2631 # supported formats:
2632 # * $GIT_DIR/ctags/<tagname> file (in 'ctags' subdirectory)
2633 #   - if its contents is a number, use it as tag weight,
2634 #   - otherwise add a tag with weight 1
2635 # * $GIT_DIR/ctags file, each line is a tag (with weight 1)
2636 #   the same value multiple times increases tag weight
2637 # * `gitweb.ctag' multi-valued repo config variable
2638 sub git_get_project_ctags {
2639         my $project = shift;
2640         my $ctags = {};
2642         $git_dir = "$projectroot/$project";
2643         if (opendir my $dh, "$git_dir/ctags") {
2644                 my @files = grep { -f $_ } map { "$git_dir/ctags/$_" } readdir($dh);
2645                 foreach my $tagfile (@files) {
2646                         open my $ct, '<', $tagfile
2647                                 or next;
2648                         my $val = <$ct>;
2649                         chomp $val if $val;
2650                         close $ct;
2652                         (my $ctag = $tagfile) =~ s#.*/##;
2653                         if ($val =~ /^\d+$/) {
2654                                 $ctags->{$ctag} = $val;
2655                         } else {
2656                                 $ctags->{$ctag} = 1;
2657                         }
2658                 }
2659                 closedir $dh;
2661         } elsif (open my $fh, '<', "$git_dir/ctags") {
2662                 while (my $line = <$fh>) {
2663                         chomp $line;
2664                         $ctags->{$line}++ if $line;
2665                 }
2666                 close $fh;
2668         } else {
2669                 my $taglist = config_to_multi(git_get_project_config('ctag'));
2670                 foreach my $tag (@$taglist) {
2671                         $ctags->{$tag}++;
2672                 }
2673         }
2675         return $ctags;
2678 # return hash, where keys are content tags ('ctags'),
2679 # and values are sum of weights of given tag in every project
2680 sub git_gather_all_ctags {
2681         my $projects = shift;
2682         my $ctags = {};
2684         foreach my $p (@$projects) {
2685                 foreach my $ct (keys %{$p->{'ctags'}}) {
2686                         $ctags->{$ct} += $p->{'ctags'}->{$ct};
2687                 }
2688         }
2690         return $ctags;
2693 sub git_populate_project_tagcloud {
2694         my $ctags = shift;
2696         # First, merge different-cased tags; tags vote on casing
2697         my %ctags_lc;
2698         foreach (keys %$ctags) {
2699                 $ctags_lc{lc $_}->{count} += $ctags->{$_};
2700                 if (not $ctags_lc{lc $_}->{topcount}
2701                     or $ctags_lc{lc $_}->{topcount} < $ctags->{$_}) {
2702                         $ctags_lc{lc $_}->{topcount} = $ctags->{$_};
2703                         $ctags_lc{lc $_}->{topname} = $_;
2704                 }
2705         }
2707         my $cloud;
2708         my $matched = $cgi->param('by_tag');
2709         if (eval { require HTML::TagCloud; 1; }) {
2710                 $cloud = HTML::TagCloud->new;
2711                 foreach my $ctag (sort keys %ctags_lc) {
2712                         # Pad the title with spaces so that the cloud looks
2713                         # less crammed.
2714                         my $title = esc_html($ctags_lc{$ctag}->{topname});
2715                         $title =~ s/ /&nbsp;/g;
2716                         $title =~ s/^/&nbsp;/g;
2717                         $title =~ s/$/&nbsp;/g;
2718                         if (defined $matched && $matched eq $ctag) {
2719                                 $title = qq(<span class="match">$title</span>);
2720                         }
2721                         $cloud->add($title, href(project=>undef, ctag=>$ctag),
2722                                     $ctags_lc{$ctag}->{count});
2723                 }
2724         } else {
2725                 $cloud = {};
2726                 foreach my $ctag (keys %ctags_lc) {
2727                         my $title = esc_html($ctags_lc{$ctag}->{topname}, -nbsp=>1);
2728                         if (defined $matched && $matched eq $ctag) {
2729                                 $title = qq(<span class="match">$title</span>);
2730                         }
2731                         $cloud->{$ctag}{count} = $ctags_lc{$ctag}->{count};
2732                         $cloud->{$ctag}{ctag} =
2733                                 $cgi->a({-href=>href(project=>undef, ctag=>$ctag)}, $title);
2734                 }
2735         }
2736         return $cloud;
2739 sub git_show_project_tagcloud {
2740         my ($cloud, $count) = @_;
2741         if (ref $cloud eq 'HTML::TagCloud') {
2742                 return $cloud->html_and_css($count);
2743         } else {
2744                 my @tags = sort { $cloud->{$a}->{'count'} <=> $cloud->{$b}->{'count'} } keys %$cloud;
2745                 return
2746                         '<div id="htmltagcloud"'.($project ? '' : ' align="center"').'>' .
2747                         join (', ', map {
2748                                 $cloud->{$_}->{'ctag'}
2749                         } splice(@tags, 0, $count)) .
2750                         '</div>';
2751         }
2754 sub git_get_project_url_list {
2755         my $path = shift;
2757         $git_dir = "$projectroot/$path";
2758         open my $fd, '<', "$git_dir/cloneurl"
2759                 or return wantarray ?
2760                 @{ config_to_multi(git_get_project_config('url')) } :
2761                    config_to_multi(git_get_project_config('url'));
2762         my @git_project_url_list = map { chomp; $_ } <$fd>;
2763         close $fd;
2765         return wantarray ? @git_project_url_list : \@git_project_url_list;
2768 sub git_get_projects_list {
2769         my $filter = shift || '';
2770         my @list;
2772         $filter =~ s/\.git$//;
2774         if (-d $projects_list) {
2775                 # search in directory
2776                 my $dir = $projects_list;
2777                 # remove the trailing "/"
2778                 $dir =~ s!/+$!!;
2779                 my $pfxlen = length("$projects_list");
2780                 my $pfxdepth = ($projects_list =~ tr!/!!);
2781                 # when filtering, search only given subdirectory
2782                 if ($filter) {
2783                         $dir .= "/$filter";
2784                         $dir =~ s!/+$!!;
2785                 }
2787                 File::Find::find({
2788                         follow_fast => 1, # follow symbolic links
2789                         follow_skip => 2, # ignore duplicates
2790                         dangling_symlinks => 0, # ignore dangling symlinks, silently
2791                         wanted => sub {
2792                                 # global variables
2793                                 our $project_maxdepth;
2794                                 our $projectroot;
2795                                 # skip project-list toplevel, if we get it.
2796                                 return if (m!^[/.]$!);
2797                                 # only directories can be git repositories
2798                                 return unless (-d $_);
2799                                 # don't traverse too deep (Find is super slow on os x)
2800                                 # $project_maxdepth excludes depth of $projectroot
2801                                 if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
2802                                         $File::Find::prune = 1;
2803                                         return;
2804                                 }
2806                                 my $path = substr($File::Find::name, $pfxlen + 1);
2807                                 # we check related file in $projectroot
2808                                 if (check_export_ok("$projectroot/$path")) {
2809                                         push @list, { path => $path };
2810                                         $File::Find::prune = 1;
2811                                 }
2812                         },
2813                 }, "$dir");
2815         } elsif (-f $projects_list) {
2816                 # read from file(url-encoded):
2817                 # 'git%2Fgit.git Linus+Torvalds'
2818                 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2819                 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2820                 open my $fd, '<', $projects_list or return;
2821         PROJECT:
2822                 while (my $line = <$fd>) {
2823                         chomp $line;
2824                         my ($path, $owner) = split ' ', $line;
2825                         $path = unescape($path);
2826                         $owner = unescape($owner);
2827                         if (!defined $path) {
2828                                 next;
2829                         }
2830                         # if $filter is rpovided, check if $path begins with $filter
2831                         if ($filter && $path !~ m!^\Q$filter\E/!) {
2832                                 next;
2833                         }
2834                         if (check_export_ok("$projectroot/$path")) {
2835                                 my $pr = {
2836                                         path => $path,
2837                                         owner => to_utf8($owner),
2838                                 };
2839                                 push @list, $pr;
2840                         }
2841                 }
2842                 close $fd;
2843         }
2844         return @list;
2847 # written with help of Tree::Trie module (Perl Artistic License, GPL compatibile)
2848 # as side effects it sets 'forks' field to list of forks for forked projects
2849 sub filter_forks_from_projects_list {
2850         my $projects = shift;
2852         my %trie; # prefix tree of directories (path components)
2853         # generate trie out of those directories that might contain forks
2854         foreach my $pr (@$projects) {
2855                 my $path = $pr->{'path'};
2856                 $path =~ s/\.git$//;      # forks of 'repo.git' are in 'repo/' directory
2857                 next if ($path =~ m!/$!); # skip non-bare repositories, e.g. 'repo/.git'
2858                 next unless ($path);      # skip '.git' repository: tests, git-instaweb
2859                 next unless (-d $path);   # containing directory exists
2860                 $pr->{'forks'} = [];      # there can be 0 or more forks of project
2862                 # add to trie
2863                 my @dirs = split('/', $path);
2864                 # walk the trie, until either runs out of components or out of trie
2865                 my $ref = \%trie;
2866                 while (scalar @dirs &&
2867                        exists($ref->{$dirs[0]})) {
2868                         $ref = $ref->{shift @dirs};
2869                 }
2870                 # create rest of trie structure from rest of components
2871                 foreach my $dir (@dirs) {
2872                         $ref = $ref->{$dir} = {};
2873                 }
2874                 # create end marker, store $pr as a data
2875                 $ref->{''} = $pr if (!exists $ref->{''});
2876         }
2878         # filter out forks, by finding shortest prefix match for paths
2879         my @filtered;
2880  PROJECT:
2881         foreach my $pr (@$projects) {
2882                 # trie lookup
2883                 my $ref = \%trie;
2884         DIR:
2885                 foreach my $dir (split('/', $pr->{'path'})) {
2886                         if (exists $ref->{''}) {
2887                                 # found [shortest] prefix, is a fork - skip it
2888                                 push @{$ref->{''}{'forks'}}, $pr;
2889                                 next PROJECT;
2890                         }
2891                         if (!exists $ref->{$dir}) {
2892                                 # not in trie, cannot have prefix, not a fork
2893                                 push @filtered, $pr;
2894                                 next PROJECT;
2895                         }
2896                         # If the dir is there, we just walk one step down the trie.
2897                         $ref = $ref->{$dir};
2898                 }
2899                 # we ran out of trie
2900                 # (shouldn't happen: it's either no match, or end marker)
2901                 push @filtered, $pr;
2902         }
2904         return @filtered;
2907 # note: fill_project_list_info must be run first,
2908 # for 'descr_long' and 'ctags' to be filled
2909 sub search_projects_list {
2910         my ($projlist, %opts) = @_;
2911         my $tagfilter  = $opts{'tagfilter'};
2912         my $searchtext = $opts{'searchtext'};
2914         return @$projlist
2915                 unless ($tagfilter || $searchtext);
2917         my @projects;
2918  PROJECT:
2919         foreach my $pr (@$projlist) {
2921                 if ($tagfilter) {
2922                         next unless ref($pr->{'ctags'}) eq 'HASH';
2923                         next unless
2924                                 grep { lc($_) eq lc($tagfilter) } keys %{$pr->{'ctags'}};
2925                 }
2927                 if ($searchtext) {
2928                         next unless
2929                                 $pr->{'path'} =~ /$searchtext/ ||
2930                                 $pr->{'descr_long'} =~ /$searchtext/;
2931                 }
2933                 push @projects, $pr;
2934         }
2936         return @projects;
2939 our $gitweb_project_owner = undef;
2940 sub git_get_project_list_from_file {
2942         return if (defined $gitweb_project_owner);
2944         $gitweb_project_owner = {};
2945         # read from file (url-encoded):
2946         # 'git%2Fgit.git Linus+Torvalds'
2947         # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2948         # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2949         if (-f $projects_list) {
2950                 open(my $fd, '<', $projects_list);
2951                 while (my $line = <$fd>) {
2952                         chomp $line;
2953                         my ($pr, $ow) = split ' ', $line;
2954                         $pr = unescape($pr);
2955                         $ow = unescape($ow);
2956                         $gitweb_project_owner->{$pr} = to_utf8($ow);
2957                 }
2958                 close $fd;
2959         }
2962 sub git_get_project_owner {
2963         my $project = shift;
2964         my $owner;
2966         return undef unless $project;
2967         $git_dir = "$projectroot/$project";
2969         if (!defined $gitweb_project_owner) {
2970                 git_get_project_list_from_file();
2971         }
2973         if (exists $gitweb_project_owner->{$project}) {
2974                 $owner = $gitweb_project_owner->{$project};
2975         }
2976         if (!defined $owner){
2977                 $owner = git_get_project_config('owner');
2978         }
2979         if (!defined $owner) {
2980                 $owner = get_file_owner("$git_dir");
2981         }
2983         return $owner;
2986 sub git_get_last_activity {
2987         my ($path) = @_;
2988         my $fd;
2990         $git_dir = "$projectroot/$path";
2991         open($fd, "-|", git_cmd(), 'for-each-ref',
2992              '--format=%(committer)',
2993              '--sort=-committerdate',
2994              '--count=1',
2995              'refs/heads') or return;
2996         my $most_recent = <$fd>;
2997         close $fd or return;
2998         if (defined $most_recent &&
2999             $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
3000                 my $timestamp = $1;
3001                 my $age = time - $timestamp;
3002                 return ($age, age_string($age));
3003         }
3004         return (undef, undef);
3007 # Implementation note: when a single remote is wanted, we cannot use 'git
3008 # remote show -n' because that command always work (assuming it's a remote URL
3009 # if it's not defined), and we cannot use 'git remote show' because that would
3010 # try to make a network roundtrip. So the only way to find if that particular
3011 # remote is defined is to walk the list provided by 'git remote -v' and stop if
3012 # and when we find what we want.
3013 sub git_get_remotes_list {
3014         my $wanted = shift;
3015         my %remotes = ();
3017         open my $fd, '-|' , git_cmd(), 'remote', '-v';
3018         return unless $fd;
3019         while (my $remote = <$fd>) {
3020                 chomp $remote;
3021                 $remote =~ s!\t(.*?)\s+\((\w+)\)$!!;
3022                 next if $wanted and not $remote eq $wanted;
3023                 my ($url, $key) = ($1, $2);
3025                 $remotes{$remote} ||= { 'heads' => () };
3026                 $remotes{$remote}{$key} = $url;
3027         }
3028         close $fd or return;
3029         return wantarray ? %remotes : \%remotes;
3032 # Takes a hash of remotes as first parameter and fills it by adding the
3033 # available remote heads for each of the indicated remotes.
3034 sub fill_remote_heads {
3035         my $remotes = shift;
3036         my @heads = map { "remotes/$_" } keys %$remotes;
3037         my @remoteheads = git_get_heads_list(undef, @heads);
3038         foreach my $remote (keys %$remotes) {
3039                 $remotes->{$remote}{'heads'} = [ grep {
3040                         $_->{'name'} =~ s!^$remote/!!
3041                         } @remoteheads ];
3042         }
3045 sub git_get_references {
3046         my $type = shift || "";
3047         my %refs;
3048         # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
3049         # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
3050         open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
3051                 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
3052                 or return;
3054         while (my $line = <$fd>) {
3055                 chomp $line;
3056                 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {
3057                         if (defined $refs{$1}) {
3058                                 push @{$refs{$1}}, $2;
3059                         } else {
3060                                 $refs{$1} = [ $2 ];
3061                         }
3062                 }
3063         }
3064         close $fd or return;
3065         return \%refs;
3068 sub git_get_rev_name_tags {
3069         my $hash = shift || return undef;
3071         open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
3072                 or return;
3073         my $name_rev = <$fd>;
3074         close $fd;
3076         if ($name_rev =~ m|^$hash tags/(.*)$|) {
3077                 return $1;
3078         } else {
3079                 # catches also '$hash undefined' output
3080                 return undef;
3081         }
3084 ## ----------------------------------------------------------------------
3085 ## parse to hash functions
3087 sub parse_date {
3088         my $epoch = shift;
3089         my $tz = shift || "-0000";
3091         my %date;
3092         my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
3093         my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
3094         my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
3095         $date{'hour'} = $hour;
3096         $date{'minute'} = $min;
3097         $date{'mday'} = $mday;
3098         $date{'day'} = $days[$wday];
3099         $date{'month'} = $months[$mon];
3100         $date{'rfc2822'}   = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
3101                              $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
3102         $date{'mday-time'} = sprintf "%d %s %02d:%02d",
3103                              $mday, $months[$mon], $hour ,$min;
3104         $date{'iso-8601'}  = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
3105                              1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
3107         my ($tz_sign, $tz_hour, $tz_min) =
3108                 ($tz =~ m/^([-+])(\d\d)(\d\d)$/);
3109         $tz_sign = ($tz_sign eq '-' ? -1 : +1);
3110         my $local = $epoch + $tz_sign*((($tz_hour*60) + $tz_min)*60);
3111         ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
3112         $date{'hour_local'} = $hour;
3113         $date{'minute_local'} = $min;
3114         $date{'tz_local'} = $tz;
3115         $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
3116                                   1900+$year, $mon+1, $mday,
3117                                   $hour, $min, $sec, $tz);
3118         return %date;
3121 sub parse_tag {
3122         my $tag_id = shift;
3123         my %tag;
3124         my @comment;
3126         open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
3127         $tag{'id'} = $tag_id;
3128         while (my $line = <$fd>) {
3129                 chomp $line;
3130                 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
3131                         $tag{'object'} = $1;
3132                 } elsif ($line =~ m/^type (.+)$/) {
3133                         $tag{'type'} = $1;
3134                 } elsif ($line =~ m/^tag (.+)$/) {
3135                         $tag{'name'} = $1;
3136                 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
3137                         $tag{'author'} = $1;
3138                         $tag{'author_epoch'} = $2;
3139                         $tag{'author_tz'} = $3;
3140                         if ($tag{'author'} =~ m/^([^<]+) <([^>]*)>/) {
3141                                 $tag{'author_name'}  = $1;
3142                                 $tag{'author_email'} = $2;
3143                         } else {
3144                                 $tag{'author_name'} = $tag{'author'};
3145                         }
3146                 } elsif ($line =~ m/--BEGIN/) {
3147                         push @comment, $line;
3148                         last;
3149                 } elsif ($line eq "") {
3150                         last;
3151                 }
3152         }
3153         push @comment, <$fd>;
3154         $tag{'comment'} = \@comment;
3155         close $fd or return;
3156         if (!defined $tag{'name'}) {
3157                 return
3158         };
3159         return %tag
3162 sub parse_commit_text {
3163         my ($commit_text, $withparents) = @_;
3164         my @commit_lines = split '\n', $commit_text;
3165         my %co;
3167         pop @commit_lines; # Remove '\0'
3169         if (! @commit_lines) {
3170                 return;
3171         }
3173         my $header = shift @commit_lines;
3174         if ($header !~ m/^[0-9a-fA-F]{40}/) {
3175                 return;
3176         }
3177         ($co{'id'}, my @parents) = split ' ', $header;
3178         while (my $line = shift @commit_lines) {
3179                 last if $line eq "\n";
3180                 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
3181                         $co{'tree'} = $1;
3182                 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
3183                         push @parents, $1;
3184                 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
3185                         $co{'author'} = to_utf8($1);
3186                         $co{'author_epoch'} = $2;
3187                         $co{'author_tz'} = $3;
3188                         if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
3189                                 $co{'author_name'}  = $1;
3190                                 $co{'author_email'} = $2;
3191                         } else {
3192                                 $co{'author_name'} = $co{'author'};
3193                         }
3194                 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
3195                         $co{'committer'} = to_utf8($1);
3196                         $co{'committer_epoch'} = $2;
3197                         $co{'committer_tz'} = $3;
3198                         if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
3199                                 $co{'committer_name'}  = $1;
3200                                 $co{'committer_email'} = $2;
3201                         } else {
3202                                 $co{'committer_name'} = $co{'committer'};
3203                         }
3204                 }
3205         }
3206         if (!defined $co{'tree'}) {
3207                 return;
3208         };
3209         $co{'parents'} = \@parents;
3210         $co{'parent'} = $parents[0];
3212         foreach my $title (@commit_lines) {
3213                 $title =~ s/^    //;
3214                 if ($title ne "") {
3215                         $co{'title'} = chop_str($title, 80, 5);
3216                         # remove leading stuff of merges to make the interesting part visible
3217                         if (length($title) > 50) {
3218                                 $title =~ s/^Automatic //;
3219                                 $title =~ s/^merge (of|with) /Merge ... /i;
3220                                 if (length($title) > 50) {
3221                                         $title =~ s/(http|rsync):\/\///;
3222                                 }
3223                                 if (length($title) > 50) {
3224                                         $title =~ s/(master|www|rsync)\.//;
3225                                 }
3226                                 if (length($title) > 50) {
3227                                         $title =~ s/kernel.org:?//;
3228                                 }
3229                                 if (length($title) > 50) {
3230                                         $title =~ s/\/pub\/scm//;
3231                                 }
3232                         }
3233                         $co{'title_short'} = chop_str($title, 50, 5);
3234                         last;
3235                 }
3236         }
3237         if (! defined $co{'title'} || $co{'title'} eq "") {
3238                 $co{'title'} = $co{'title_short'} = '(no commit message)';
3239         }
3240         # remove added spaces
3241         foreach my $line (@commit_lines) {
3242                 $line =~ s/^    //;
3243         }
3244         $co{'comment'} = \@commit_lines;
3246         my $age = time - $co{'committer_epoch'};
3247         $co{'age'} = $age;
3248         $co{'age_string'} = age_string($age);
3249         my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
3250         if ($age > 60*60*24*7*2) {
3251                 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
3252                 $co{'age_string_age'} = $co{'age_string'};
3253         } else {
3254                 $co{'age_string_date'} = $co{'age_string'};
3255                 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
3256         }
3257         return %co;
3260 sub parse_commit {
3261         my ($commit_id) = @_;
3262         my %co;
3264         local $/ = "\0";
3266         open my $fd, "-|", git_cmd(), "rev-list",
3267                 "--parents",
3268                 "--header",
3269                 "--max-count=1",
3270                 $commit_id,
3271                 "--",
3272                 or die_error(500, "Open git-rev-list failed");
3273         %co = parse_commit_text(<$fd>, 1);
3274         close $fd;
3276         return %co;
3279 sub parse_commits {
3280         my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
3281         my @cos;
3283         $maxcount ||= 1;
3284         $skip ||= 0;
3286         local $/ = "\0";
3288         open my $fd, "-|", git_cmd(), "rev-list",
3289                 "--header",
3290                 @args,
3291                 ("--max-count=" . $maxcount),
3292                 ("--skip=" . $skip),
3293                 @extra_options,
3294                 $commit_id,
3295                 "--",
3296                 ($filename ? ($filename) : ())
3297                 or die_error(500, "Open git-rev-list failed");
3298         while (my $line = <$fd>) {
3299                 my %co = parse_commit_text($line);
3300                 push @cos, \%co;
3301         }
3302         close $fd;
3304         return wantarray ? @cos : \@cos;
3307 # parse line of git-diff-tree "raw" output
3308 sub parse_difftree_raw_line {
3309         my $line = shift;
3310         my %res;
3312         # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M   ls-files.c'
3313         # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M   rev-tree.c'
3314         if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
3315                 $res{'from_mode'} = $1;
3316                 $res{'to_mode'} = $2;
3317                 $res{'from_id'} = $3;
3318                 $res{'to_id'} = $4;
3319                 $res{'status'} = $5;
3320                 $res{'similarity'} = $6;
3321                 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
3322                         ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
3323                 } else {
3324                         $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote($7);
3325                 }
3326         }
3327         # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
3328         # combined diff (for merge commit)
3329         elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
3330                 $res{'nparents'}  = length($1);
3331                 $res{'from_mode'} = [ split(' ', $2) ];
3332                 $res{'to_mode'} = pop @{$res{'from_mode'}};
3333                 $res{'from_id'} = [ split(' ', $3) ];
3334                 $res{'to_id'} = pop @{$res{'from_id'}};
3335                 $res{'status'} = [ split('', $4) ];
3336                 $res{'to_file'} = unquote($5);
3337         }
3338         # 'c512b523472485aef4fff9e57b229d9d243c967f'
3339         elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
3340                 $res{'commit'} = $1;
3341         }
3343         return wantarray ? %res : \%res;
3346 # wrapper: return parsed line of git-diff-tree "raw" output
3347 # (the argument might be raw line, or parsed info)
3348 sub parsed_difftree_line {
3349         my $line_or_ref = shift;
3351         if (ref($line_or_ref) eq "HASH") {
3352                 # pre-parsed (or generated by hand)
3353                 return $line_or_ref;
3354         } else {
3355                 return parse_difftree_raw_line($line_or_ref);
3356         }
3359 # parse line of git-ls-tree output
3360 sub parse_ls_tree_line {
3361         my $line = shift;
3362         my %opts = @_;
3363         my %res;
3365         if ($opts{'-l'}) {
3366                 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa   16717  panic.c'
3367                 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;
3369                 $res{'mode'} = $1;
3370                 $res{'type'} = $2;
3371                 $res{'hash'} = $3;
3372                 $res{'size'} = $4;
3373                 if ($opts{'-z'}) {
3374                         $res{'name'} = $5;
3375                 } else {
3376                         $res{'name'} = unquote($5);
3377                 }
3378         } else {
3379                 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa  panic.c'
3380                 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
3382                 $res{'mode'} = $1;
3383                 $res{'type'} = $2;
3384                 $res{'hash'} = $3;
3385                 if ($opts{'-z'}) {
3386                         $res{'name'} = $4;
3387                 } else {
3388                         $res{'name'} = unquote($4);
3389                 }
3390         }
3392         return wantarray ? %res : \%res;
3395 # generates _two_ hashes, references to which are passed as 2 and 3 argument
3396 sub parse_from_to_diffinfo {
3397         my ($diffinfo, $from, $to, @parents) = @_;
3399         if ($diffinfo->{'nparents'}) {
3400                 # combined diff
3401                 $from->{'file'} = [];
3402                 $from->{'href'} = [];
3403                 fill_from_file_info($diffinfo, @parents)
3404                         unless exists $diffinfo->{'from_file'};
3405                 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
3406                         $from->{'file'}[$i] =
3407                                 defined $diffinfo->{'from_file'}[$i] ?
3408                                         $diffinfo->{'from_file'}[$i] :
3409                                         $diffinfo->{'to_file'};
3410                         if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
3411                                 $from->{'href'}[$i] = href(action=>"blob",
3412                                                            hash_base=>$parents[$i],
3413                                                            hash=>$diffinfo->{'from_id'}[$i],
3414                                                            file_name=>$from->{'file'}[$i]);
3415                         } else {
3416                                 $from->{'href'}[$i] = undef;
3417                         }
3418                 }
3419         } else {
3420                 # ordinary (not combined) diff
3421                 $from->{'file'} = $diffinfo->{'from_file'};
3422                 if ($diffinfo->{'status'} ne "A") { # not new (added) file
3423                         $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
3424                                                hash=>$diffinfo->{'from_id'},
3425                                                file_name=>$from->{'file'});
3426                 } else {
3427                         delete $from->{'href'};
3428                 }
3429         }
3431         $to->{'file'} = $diffinfo->{'to_file'};
3432         if (!is_deleted($diffinfo)) { # file exists in result
3433                 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
3434                                      hash=>$diffinfo->{'to_id'},
3435                                      file_name=>$to->{'file'});
3436         } else {
3437                 delete $to->{'href'};
3438         }
3441 ## ......................................................................
3442 ## parse to array of hashes functions
3444 sub git_get_heads_list {
3445         my ($limit, @classes) = @_;
3446         @classes = ('heads') unless @classes;
3447         my @patterns = map { "refs/$_" } @classes;
3448         my @headslist;
3450         open my $fd, '-|', git_cmd(), 'for-each-ref',
3451                 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
3452                 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
3453                 @patterns
3454                 or return;
3455         while (my $line = <$fd>) {
3456                 my %ref_item;
3458                 chomp $line;
3459                 my ($refinfo, $committerinfo) = split(/\0/, $line);
3460                 my ($hash, $name, $title) = split(' ', $refinfo, 3);
3461                 my ($committer, $epoch, $tz) =
3462                         ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
3463                 $ref_item{'fullname'}  = $name;
3464                 $name =~ s!^refs/(?:head|remote)s/!!;
3466                 $ref_item{'name'}  = $name;
3467                 $ref_item{'id'}    = $hash;
3468                 $ref_item{'title'} = $title || '(no commit message)';
3469                 $ref_item{'epoch'} = $epoch;
3470                 if ($epoch) {
3471                         $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
3472                 } else {
3473                         $ref_item{'age'} = "unknown";
3474                 }
3476                 push @headslist, \%ref_item;
3477         }
3478         close $fd;
3480         return wantarray ? @headslist : \@headslist;
3483 sub git_get_tags_list {
3484         my $limit = shift;
3485         my @tagslist;
3487         open my $fd, '-|', git_cmd(), 'for-each-ref',
3488                 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
3489                 '--format=%(objectname) %(objecttype) %(refname) '.
3490                 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
3491                 'refs/tags'
3492                 or return;
3493         while (my $line = <$fd>) {
3494                 my %ref_item;
3496                 chomp $line;
3497                 my ($refinfo, $creatorinfo) = split(/\0/, $line);
3498                 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
3499                 my ($creator, $epoch, $tz) =
3500                         ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
3501                 $ref_item{'fullname'} = $name;
3502                 $name =~ s!^refs/tags/!!;
3504                 $ref_item{'type'} = $type;
3505                 $ref_item{'id'} = $id;
3506                 $ref_item{'name'} = $name;
3507                 if ($type eq "tag") {
3508                         $ref_item{'subject'} = $title;
3509                         $ref_item{'reftype'} = $reftype;
3510                         $ref_item{'refid'}   = $refid;
3511                 } else {
3512                         $ref_item{'reftype'} = $type;
3513                         $ref_item{'refid'}   = $id;
3514                 }
3516                 if ($type eq "tag" || $type eq "commit") {
3517                         $ref_item{'epoch'} = $epoch;
3518                         if ($epoch) {
3519                                 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
3520                         } else {
3521                                 $ref_item{'age'} = "unknown";
3522                         }
3523                 }
3525                 push @tagslist, \%ref_item;
3526         }
3527         close $fd;
3529         return wantarray ? @tagslist : \@tagslist;
3532 ## ----------------------------------------------------------------------
3533 ## filesystem-related functions
3535 sub get_file_owner {
3536         my $path = shift;
3538         my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
3539         my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
3540         if (!defined $gcos) {
3541                 return undef;
3542         }
3543         my $owner = $gcos;
3544         $owner =~ s/[,;].*$//;
3545         return to_utf8($owner);
3548 # assume that file exists
3549 sub insert_file {
3550         my $filename = shift;
3552         open my $fd, '<', $filename;
3553         print map { to_utf8($_) } <$fd>;
3554         close $fd;
3557 ## ......................................................................
3558 ## mimetype related functions
3560 sub mimetype_guess_file {
3561         my $filename = shift;
3562         my $mimemap = shift;
3563         -r $mimemap or return undef;
3565         my %mimemap;
3566         open(my $mh, '<', $mimemap) or return undef;
3567         while (<$mh>) {
3568                 next if m/^#/; # skip comments
3569                 my ($mimetype, @exts) = split(/\s+/);
3570                 foreach my $ext (@exts) {
3571                         $mimemap{$ext} = $mimetype;
3572                 }
3573         }
3574         close($mh);
3576         $filename =~ /\.([^.]*)$/;
3577         return $mimemap{$1};
3580 sub mimetype_guess {
3581         my $filename = shift;
3582         my $mime;
3583         $filename =~ /\./ or return undef;
3585         if ($mimetypes_file) {
3586                 my $file = $mimetypes_file;
3587                 if ($file !~ m!^/!) { # if it is relative path
3588                         # it is relative to project
3589                         $file = "$projectroot/$project/$file";
3590                 }
3591                 $mime = mimetype_guess_file($filename, $file);
3592         }
3593         $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
3594         return $mime;
3597 sub blob_mimetype {
3598         my $fd = shift;
3599         my $filename = shift;
3601         if ($filename) {
3602                 my $mime = mimetype_guess($filename);
3603                 $mime and return $mime;
3604         }
3606         # just in case
3607         return $default_blob_plain_mimetype unless $fd;
3609         if (-T $fd) {
3610                 return 'text/plain';
3611         } elsif (! $filename) {
3612                 return 'application/octet-stream';
3613         } elsif ($filename =~ m/\.png$/i) {
3614                 return 'image/png';
3615         } elsif ($filename =~ m/\.gif$/i) {
3616                 return 'image/gif';
3617         } elsif ($filename =~ m/\.jpe?g$/i) {
3618                 return 'image/jpeg';
3619         } else {
3620                 return 'application/octet-stream';
3621         }
3624 sub blob_contenttype {
3625         my ($fd, $file_name, $type) = @_;
3627         $type ||= blob_mimetype($fd, $file_name);
3628         if ($type eq 'text/plain' && defined $default_text_plain_charset) {
3629                 $type .= "; charset=$default_text_plain_charset";
3630         }
3632         return $type;
3635 # guess file syntax for syntax highlighting; return undef if no highlighting
3636 # the name of syntax can (in the future) depend on syntax highlighter used
3637 sub guess_file_syntax {
3638         my ($highlight, $mimetype, $file_name) = @_;
3639         return undef unless ($highlight && defined $file_name);
3640         my $basename = basename($file_name, '.in');
3641         return $highlight_basename{$basename}
3642                 if exists $highlight_basename{$basename};
3644         $basename =~ /\.([^.]*)$/;
3645         my $ext = $1 or return undef;
3646         return $highlight_ext{$ext}
3647                 if exists $highlight_ext{$ext};
3649         return undef;
3652 # run highlighter and return FD of its output,
3653 # or return original FD if no highlighting
3654 sub run_highlighter {
3655         my ($fd, $highlight, $syntax) = @_;
3656         return $fd unless ($highlight && defined $syntax);
3658         close $fd;
3659         open $fd, quote_command(git_cmd(), "cat-file", "blob", $hash)." | ".
3660                   quote_command($highlight_bin).
3661                   " --replace-tabs=8 --fragment --syntax $syntax |"
3662                 or die_error(500, "Couldn't open file or run syntax highlighter");
3663         return $fd;
3666 ## ======================================================================
3667 ## functions printing HTML: header, footer, error page
3669 sub get_page_title {
3670         my $title = to_utf8($site_name);
3672         return $title unless (defined $project);
3673         $title .= " - " . to_utf8($project);
3675         return $title unless (defined $action);
3676         $title .= "/$action"; # $action is US-ASCII (7bit ASCII)
3678         return $title unless (defined $file_name);
3679         $title .= " - " . esc_path($file_name);
3680         if ($action eq "tree" && $file_name !~ m|/$|) {
3681                 $title .= "/";
3682         }
3684         return $title;
3687 sub get_content_type_html {
3688         # require explicit support from the UA if we are to send the page as
3689         # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
3690         # we have to do this because MSIE sometimes globs '*/*', pretending to
3691         # support xhtml+xml but choking when it gets what it asked for.
3692         if (defined $cgi->http('HTTP_ACCEPT') &&
3693             $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
3694             $cgi->Accept('application/xhtml+xml') != 0) {
3695                 return 'application/xhtml+xml';
3696         } else {
3697                 return 'text/html';
3698         }
3701 sub print_feed_meta {
3702         if (defined $project) {
3703                 my %href_params = get_feed_info();
3704                 if (!exists $href_params{'-title'}) {
3705                         $href_params{'-title'} = 'log';
3706                 }
3708                 foreach my $format (qw(RSS Atom)) {
3709                         my $type = lc($format);
3710                         my %link_attr = (
3711                                 '-rel' => 'alternate',
3712                                 '-title' => esc_attr("$project - $href_params{'-title'} - $format feed"),
3713                                 '-type' => "application/$type+xml"
3714                         );
3716                         $href_params{'action'} = $type;
3717                         $link_attr{'-href'} = href(%href_params);
3718                         print "<link ".
3719                               "rel=\"$link_attr{'-rel'}\" ".
3720                               "title=\"$link_attr{'-title'}\" ".
3721                               "href=\"$link_attr{'-href'}\" ".
3722                               "type=\"$link_attr{'-type'}\" ".
3723                               "/>\n";
3725                         $href_params{'extra_options'} = '--no-merges';
3726                         $link_attr{'-href'} = href(%href_params);
3727                         $link_attr{'-title'} .= ' (no merges)';
3728                         print "<link ".
3729                               "rel=\"$link_attr{'-rel'}\" ".
3730                               "title=\"$link_attr{'-title'}\" ".
3731                               "href=\"$link_attr{'-href'}\" ".
3732                               "type=\"$link_attr{'-type'}\" ".
3733                               "/>\n";
3734                 }
3736         } else {
3737                 printf('<link rel="alternate" title="%s projects list" '.
3738                        'href="%s" type="text/plain; charset=utf-8" />'."\n",
3739                        esc_attr($site_name), href(project=>undef, action=>"project_index"));
3740                 printf('<link rel="alternate" title="%s projects feeds" '.
3741                        'href="%s" type="text/x-opml" />'."\n",
3742                        esc_attr($site_name), href(project=>undef, action=>"opml"));
3743         }
3746 sub print_header_links {
3747         my $status = shift;
3749         # print out each stylesheet that exist, providing backwards capability
3750         # for those people who defined $stylesheet in a config file
3751         if (defined $stylesheet) {
3752                 print '<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";
3753         } else {
3754                 foreach my $stylesheet (@stylesheets) {
3755                         next unless $stylesheet;
3756                         print '<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";
3757                 }
3758         }
3759         print_feed_meta()
3760                 if ($status eq '200 OK');
3761         if (defined $favicon) {
3762                 print qq(<link rel="shortcut icon" href=").esc_url($favicon).qq(" type="image/png" />\n);
3763         }
3766 sub print_nav_breadcrumbs {
3767         my %opts = @_;
3769         print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
3770         if (defined $project) {
3771                 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
3772                 if (defined $action) {
3773                         my $action_print = $action ;
3774                         if (defined $opts{-action_extra}) {
3775                                 $action_print = $cgi->a({-href => href(action=>$action)},
3776                                         $action);
3777                         }
3778                         print " / $action_print";
3779                 }
3780                 if (defined $opts{-action_extra}) {
3781                         print " / $opts{-action_extra}";
3782                 }
3783                 print "\n";
3784         }
3787 sub print_search_form {
3788         if (!defined $searchtext) {
3789                 $searchtext = "";
3790         }
3791         my $search_hash;
3792         if (defined $hash_base) {
3793                 $search_hash = $hash_base;
3794         } elsif (defined $hash) {
3795                 $search_hash = $hash;
3796         } else {
3797                 $search_hash = "HEAD";
3798         }
3799         my $action = $my_uri;
3800         my $use_pathinfo = gitweb_check_feature('pathinfo');
3801         if ($use_pathinfo) {
3802                 $action .= "/".esc_url($project);
3803         }
3804         print $cgi->startform(-method => "get", -action => $action) .
3805               "<div class=\"search\">\n" .
3806               (!$use_pathinfo &&
3807               $cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) . "\n") .
3808               $cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) . "\n" .
3809               $cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) . "\n" .
3810               $cgi->popup_menu(-name => 'st', -default => 'commit',
3811                                -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
3812               $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
3813               " search:\n",
3814               $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
3815               "<span title=\"Extended regular expression\">" .
3816               $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
3817                              -checked => $search_use_regexp) .
3818               "</span>" .
3819               "</div>" .
3820               $cgi->end_form() . "\n";
3823 sub git_header_html {
3824         my $status = shift || "200 OK";
3825         my $expires = shift;
3826         my %opts = @_;
3828         my $title = get_page_title();
3829         my $content_type = get_content_type_html();
3830         print $cgi->header(-type=>$content_type, -charset => 'utf-8',
3831                            -status=> $status, -expires => $expires)
3832                 unless ($opts{'-no_http_header'});
3833         my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
3834         print <<EOF;
3835 <?xml version="1.0" encoding="utf-8"?>
3836 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3837 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
3838 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
3839 <!-- git core binaries version $git_version -->
3840 <head>
3841 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
3842 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
3843 <meta name="robots" content="index, nofollow"/>
3844 <title>$title</title>
3845 EOF
3846         # the stylesheet, favicon etc urls won't work correctly with path_info
3847         # unless we set the appropriate base URL
3848         if ($ENV{'PATH_INFO'}) {
3849                 print "<base href=\"".esc_url($base_url)."\" />\n";
3850         }
3851         print_header_links($status);
3852         print "</head>\n" .
3853               "<body>\n";
3855         if (defined $site_header && -f $site_header) {
3856                 insert_file($site_header);
3857         }
3859         print "<div class=\"page_header\">\n";
3860         if (defined $logo) {
3861                 print $cgi->a({-href => esc_url($logo_url),
3862                                -title => $logo_label},
3863                               $cgi->img({-src => esc_url($logo),
3864                                          -width => 72, -height => 27,
3865                                          -alt => "git",
3866                                          -class => "logo"}));
3867         }
3868         print_nav_breadcrumbs(%opts);
3869         print "</div>\n";
3871         my $have_search = gitweb_check_feature('search');
3872         if (defined $project && $have_search) {
3873                 print_search_form();
3874         }
3877 sub git_footer_html {
3878         my $feed_class = 'rss_logo';
3880         print "<div class=\"page_footer\">\n";
3881         if (defined $project) {
3882                 my $descr = git_get_project_description($project);
3883                 if (defined $descr) {
3884                         print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
3885                 }
3887                 my %href_params = get_feed_info();
3888                 if (!%href_params) {
3889                         $feed_class .= ' generic';
3890                 }
3891                 $href_params{'-title'} ||= 'log';
3893                 foreach my $format (qw(RSS Atom)) {
3894                         $href_params{'action'} = lc($format);
3895                         print $cgi->a({-href => href(%href_params),
3896                                       -title => "$href_params{'-title'} $format feed",
3897                                       -class => $feed_class}, $format)."\n";
3898                 }
3900         } else {
3901                 print $cgi->a({-href => href(project=>undef, action=>"opml"),
3902                               -class => $feed_class}, "OPML") . " ";
3903                 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
3904                               -class => $feed_class}, "TXT") . "\n";
3905         }
3906         print "</div>\n"; # class="page_footer"
3908         if (defined $t0 && gitweb_check_feature('timed')) {
3909                 print "<div id=\"generating_info\">\n";
3910                 print 'This page took '.
3911                       '<span id="generating_time" class="time_span">'.
3912                       tv_interval($t0, [ gettimeofday() ]).
3913                       ' seconds </span>'.
3914                       ' and '.
3915                       '<span id="generating_cmd">'.
3916                       $number_of_git_cmds.
3917                       '</span> git commands '.
3918                       " to generate.\n";
3919                 print "</div>\n"; # class="page_footer"
3920         }
3922         if (defined $site_footer && -f $site_footer) {
3923                 insert_file($site_footer);
3924         }
3926         print qq!<script type="text/javascript" src="!.esc_url($javascript).qq!"></script>\n!;
3927         if (defined $action &&
3928             $action eq 'blame_incremental') {
3929                 print qq!<script type="text/javascript">\n!.
3930                       qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.
3931                       qq!           "!. href() .qq!");\n!.
3932                       qq!</script>\n!;
3933         } else {
3934                 my ($jstimezone, $tz_cookie, $datetime_class) =
3935                         gitweb_get_feature('javascript-timezone');
3937                 print qq!<script type="text/javascript">\n!.
3938                       qq!window.onload = function () {\n!;
3939                 if (gitweb_check_feature('javascript-actions')) {
3940                         print qq!       fixLinks();\n!;
3941                 }
3942                 if ($jstimezone && $tz_cookie && $datetime_class) {
3943                         print qq!       var tz_cookie = { name: '$tz_cookie', expires: 14, path: '/' };\n!. # in days
3944                               qq!       onloadTZSetup('$jstimezone', tz_cookie, '$datetime_class');\n!;
3945                 }
3946                 print qq!};\n!.
3947                       qq!</script>\n!;
3948         }
3950         print "</body>\n" .
3951               "</html>";
3954 # die_error(<http_status_code>, <error_message>[, <detailed_html_description>])
3955 # Example: die_error(404, 'Hash not found')
3956 # By convention, use the following status codes (as defined in RFC 2616):
3957 # 400: Invalid or missing CGI parameters, or
3958 #      requested object exists but has wrong type.
3959 # 403: Requested feature (like "pickaxe" or "snapshot") not enabled on
3960 #      this server or project.
3961 # 404: Requested object/revision/project doesn't exist.
3962 # 500: The server isn't configured properly, or
3963 #      an internal error occurred (e.g. failed assertions caused by bugs), or
3964 #      an unknown error occurred (e.g. the git binary died unexpectedly).
3965 # 503: The server is currently unavailable (because it is overloaded,
3966 #      or down for maintenance).  Generally, this is a temporary state.
3967 sub die_error {
3968         my $status = shift || 500;
3969         my $error = esc_html(shift) || "Internal Server Error";
3970         my $extra = shift;
3971         my %opts = @_;
3973         my %http_responses = (
3974                 400 => '400 Bad Request',
3975                 403 => '403 Forbidden',
3976                 404 => '404 Not Found',
3977                 500 => '500 Internal Server Error',
3978                 503 => '503 Service Unavailable',
3979         );
3980         git_header_html($http_responses{$status}, undef, %opts);
3981         print <<EOF;
3982 <div class="page_body">
3983 <br /><br />
3984 $status - $error
3985 <br />
3986 EOF
3987         if (defined $extra) {
3988                 print "<hr />\n" .
3989                       "$extra\n";
3990         }
3991         print "</div>\n";
3993         git_footer_html();
3994         goto DONE_GITWEB
3995                 unless ($opts{'-error_handler'});
3998 ## ----------------------------------------------------------------------
3999 ## functions printing or outputting HTML: navigation
4001 sub git_print_page_nav {
4002         my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
4003         $extra = '' if !defined $extra; # pager or formats
4005         my @navs = qw(summary shortlog log commit commitdiff tree);
4006         if ($suppress) {
4007                 @navs = grep { $_ ne $suppress } @navs;
4008         }
4010         my %arg = map { $_ => {action=>$_} } @navs;
4011         if (defined $head) {
4012                 for (qw(commit commitdiff)) {
4013                         $arg{$_}{'hash'} = $head;
4014                 }
4015                 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
4016                         for (qw(shortlog log)) {
4017                                 $arg{$_}{'hash'} = $head;
4018                         }
4019                 }
4020         }
4022         $arg{'tree'}{'hash'} = $treehead if defined $treehead;
4023         $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
4025         my @actions = gitweb_get_feature('actions');
4026         my %repl = (
4027                 '%' => '%',
4028                 'n' => $project,         # project name
4029                 'f' => $git_dir,         # project path within filesystem
4030                 'h' => $treehead || '',  # current hash ('h' parameter)
4031                 'b' => $treebase || '',  # hash base ('hb' parameter)
4032         );
4033         while (@actions) {
4034                 my ($label, $link, $pos) = splice(@actions,0,3);
4035                 # insert
4036                 @navs = map { $_ eq $pos ? ($_, $label) : $_ } @navs;
4037                 # munch munch
4038                 $link =~ s/%([%nfhb])/$repl{$1}/g;
4039                 $arg{$label}{'_href'} = $link;
4040         }
4042         print "<div class=\"page_nav\">\n" .
4043                 (join " | ",
4044                  map { $_ eq $current ?
4045                        $_ : $cgi->a({-href => ($arg{$_}{_href} ? $arg{$_}{_href} : href(%{$arg{$_}}))}, "$_")
4046                  } @navs);
4047         print "<br/>\n$extra<br/>\n" .
4048               "</div>\n";
4051 # returns a submenu for the nagivation of the refs views (tags, heads,
4052 # remotes) with the current view disabled and the remotes view only
4053 # available if the feature is enabled
4054 sub format_ref_views {
4055         my ($current) = @_;
4056         my @ref_views = qw{tags heads};
4057         push @ref_views, 'remotes' if gitweb_check_feature('remote_heads');
4058         return join " | ", map {
4059                 $_ eq $current ? $_ :
4060                 $cgi->a({-href => href(action=>$_)}, $_)
4061         } @ref_views
4064 sub format_paging_nav {
4065         my ($action, $page, $has_next_link) = @_;
4066         my $paging_nav;
4069         if ($page > 0) {
4070                 $paging_nav .=
4071                         $cgi->a({-href => href(-replay=>1, page=>undef)}, "first") .
4072                         " &sdot; " .
4073                         $cgi->a({-href => href(-replay=>1, page=>$page-1),
4074                                  -accesskey => "p", -title => "Alt-p"}, "prev");
4075         } else {
4076                 $paging_nav .= "first &sdot; prev";
4077         }
4079         if ($has_next_link) {
4080                 $paging_nav .= " &sdot; " .
4081                         $cgi->a({-href => href(-replay=>1, page=>$page+1),
4082                                  -accesskey => "n", -title => "Alt-n"}, "next");
4083         } else {
4084                 $paging_nav .= " &sdot; next";
4085         }
4087         return $paging_nav;
4090 ## ......................................................................
4091 ## functions printing or outputting HTML: div
4093 sub git_print_header_div {
4094         my ($action, $title, $hash, $hash_base) = @_;
4095         my %args = ();
4097         $args{'action'} = $action;
4098         $args{'hash'} = $hash if $hash;
4099         $args{'hash_base'} = $hash_base if $hash_base;
4101         print "<div class=\"header\">\n" .
4102               $cgi->a({-href => href(%args), -class => "title"},
4103               $title ? $title : $action) .
4104               "\n</div>\n";
4107 sub format_repo_url {
4108         my ($name, $url) = @_;
4109         return "<tr class=\"metadata_url\"><td>$name</td><td>$url</td></tr>\n";
4112 # Group output by placing it in a DIV element and adding a header.
4113 # Options for start_div() can be provided by passing a hash reference as the
4114 # first parameter to the function.
4115 # Options to git_print_header_div() can be provided by passing an array
4116 # reference. This must follow the options to start_div if they are present.
4117 # The content can be a scalar, which is output as-is, a scalar reference, which
4118 # is output after html escaping, an IO handle passed either as *handle or
4119 # *handle{IO}, or a function reference. In the latter case all following
4120 # parameters will be taken as argument to the content function call.
4121 sub git_print_section {
4122         my ($div_args, $header_args, $content);
4123         my $arg = shift;
4124         if (ref($arg) eq 'HASH') {
4125                 $div_args = $arg;
4126                 $arg = shift;
4127         }
4128         if (ref($arg) eq 'ARRAY') {
4129                 $header_args = $arg;
4130                 $arg = shift;
4131         }
4132         $content = $arg;
4134         print $cgi->start_div($div_args);
4135         git_print_header_div(@$header_args);
4137         if (ref($content) eq 'CODE') {
4138                 $content->(@_);
4139         } elsif (ref($content) eq 'SCALAR') {
4140                 print esc_html($$content);
4141         } elsif (ref($content) eq 'GLOB' or ref($content) eq 'IO::Handle') {
4142                 print <$content>;
4143         } elsif (!ref($content) && defined($content)) {
4144                 print $content;
4145         }
4147         print $cgi->end_div;
4150 sub format_timestamp_html {
4151         my $date = shift;
4152         my $strtime = $date->{'rfc2822'};
4154         my (undef, undef, $datetime_class) =
4155                 gitweb_get_feature('javascript-timezone');
4156         if ($datetime_class) {
4157                 $strtime = qq!<span class="$datetime_class">$strtime</span>!;
4158         }
4160         my $localtime_format = '(%02d:%02d %s)';
4161         if ($date->{'hour_local'} < 6) {
4162                 $localtime_format = '(<span class="atnight">%02d:%02d</span> %s)';
4163         }
4164         $strtime .= ' ' .
4165                     sprintf($localtime_format,
4166                             $date->{'hour_local'}, $date->{'minute_local'}, $date->{'tz_local'});
4168         return $strtime;
4171 # Outputs the author name and date in long form
4172 sub git_print_authorship {
4173         my $co = shift;
4174         my %opts = @_;
4175         my $tag = $opts{-tag} || 'div';
4176         my $author = $co->{'author_name'};
4178         my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
4179         print "<$tag class=\"author_date\">" .
4180               format_search_author($author, "author", esc_html($author)) .
4181               " [".format_timestamp_html(\%ad)."]".
4182               git_get_avatar($co->{'author_email'}, -pad_before => 1) .
4183               "</$tag>\n";
4186 # Outputs table rows containing the full author or committer information,
4187 # in the format expected for 'commit' view (& similar).
4188 # Parameters are a commit hash reference, followed by the list of people
4189 # to output information for. If the list is empty it defaults to both
4190 # author and committer.
4191 sub git_print_authorship_rows {
4192         my $co = shift;
4193         # too bad we can't use @people = @_ || ('author', 'committer')
4194         my @people = @_;
4195         @people = ('author', 'committer') unless @people;
4196         foreach my $who (@people) {
4197                 my %wd = parse_date($co->{"${who}_epoch"}, $co->{"${who}_tz"});
4198                 print "<tr><td>$who</td><td>" .
4199                       format_search_author($co->{"${who}_name"}, $who,
4200                                            esc_html($co->{"${who}_name"})) . " " .
4201                       format_search_author($co->{"${who}_email"}, $who,
4202                                            esc_html("<" . $co->{"${who}_email"} . ">")) .
4203                       "</td><td rowspan=\"2\">" .
4204                       git_get_avatar($co->{"${who}_email"}, -size => 'double') .
4205                       "</td></tr>\n" .
4206                       "<tr>" .
4207                       "<td></td><td>" .
4208                       format_timestamp_html(\%wd) .
4209                       "</td>" .
4210                       "</tr>\n";
4211         }
4214 sub git_print_page_path {
4215         my $name = shift;
4216         my $type = shift;
4217         my $hb = shift;
4220         print "<div class=\"page_path\">";
4221         print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
4222                       -title => 'tree root'}, to_utf8("[$project]"));
4223         print " / ";
4224         if (defined $name) {
4225                 my @dirname = split '/', $name;
4226                 my $basename = pop @dirname;
4227                 my $fullname = '';
4229                 foreach my $dir (@dirname) {
4230                         $fullname .= ($fullname ? '/' : '') . $dir;
4231                         print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
4232                                                      hash_base=>$hb),
4233                                       -title => $fullname}, esc_path($dir));
4234                         print " / ";
4235                 }
4236                 if (defined $type && $type eq 'blob') {
4237                         print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
4238                                                      hash_base=>$hb),
4239                                       -title => $name}, esc_path($basename));
4240                 } elsif (defined $type && $type eq 'tree') {
4241                         print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
4242                                                      hash_base=>$hb),
4243                                       -title => $name}, esc_path($basename));
4244                         print " / ";
4245                 } else {
4246                         print esc_path($basename);
4247                 }
4248         }
4249         print "<br/></div>\n";
4252 sub git_print_log {
4253         my $log = shift;
4254         my %opts = @_;
4256         if ($opts{'-remove_title'}) {
4257                 # remove title, i.e. first line of log
4258                 shift @$log;
4259         }
4260         # remove leading empty lines
4261         while (defined $log->[0] && $log->[0] eq "") {
4262                 shift @$log;
4263         }
4265         # print log
4266         my $signoff = 0;
4267         my $empty = 0;
4268         foreach my $line (@$log) {
4269                 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
4270                         $signoff = 1;
4271                         $empty = 0;
4272                         if (! $opts{'-remove_signoff'}) {
4273                                 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
4274                                 next;
4275                         } else {
4276                                 # remove signoff lines
4277                                 next;
4278                         }
4279                 } else {
4280                         $signoff = 0;
4281                 }
4283                 # print only one empty line
4284                 # do not print empty line after signoff
4285                 if ($line eq "") {
4286                         next if ($empty || $signoff);
4287                         $empty = 1;
4288                 } else {
4289                         $empty = 0;
4290                 }
4292                 print format_log_line_html($line) . "<br/>\n";
4293         }
4295         if ($opts{'-final_empty_line'}) {
4296                 # end with single empty line
4297                 print "<br/>\n" unless $empty;
4298         }
4301 # return link target (what link points to)
4302 sub git_get_link_target {
4303         my $hash = shift;
4304         my $link_target;
4306         # read link
4307         open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4308                 or return;
4309         {
4310                 local $/ = undef;
4311                 $link_target = <$fd>;
4312         }
4313         close $fd
4314                 or return;
4316         return $link_target;
4319 # given link target, and the directory (basedir) the link is in,
4320 # return target of link relative to top directory (top tree);
4321 # return undef if it is not possible (including absolute links).
4322 sub normalize_link_target {
4323         my ($link_target, $basedir) = @_;
4325         # absolute symlinks (beginning with '/') cannot be normalized
4326         return if (substr($link_target, 0, 1) eq '/');
4328         # normalize link target to path from top (root) tree (dir)
4329         my $path;
4330         if ($basedir) {
4331                 $path = $basedir . '/' . $link_target;
4332         } else {
4333                 # we are in top (root) tree (dir)
4334                 $path = $link_target;
4335         }
4337         # remove //, /./, and /../
4338         my @path_parts;
4339         foreach my $part (split('/', $path)) {
4340                 # discard '.' and ''
4341                 next if (!$part || $part eq '.');
4342                 # handle '..'
4343                 if ($part eq '..') {
4344                         if (@path_parts) {
4345                                 pop @path_parts;
4346                         } else {
4347                                 # link leads outside repository (outside top dir)
4348                                 return;
4349                         }
4350                 } else {
4351                         push @path_parts, $part;
4352                 }
4353         }
4354         $path = join('/', @path_parts);
4356         return $path;
4359 # print tree entry (row of git_tree), but without encompassing <tr> element
4360 sub git_print_tree_entry {
4361         my ($t, $basedir, $hash_base, $have_blame) = @_;
4363         my %base_key = ();
4364         $base_key{'hash_base'} = $hash_base if defined $hash_base;
4366         # The format of a table row is: mode list link.  Where mode is
4367         # the mode of the entry, list is the name of the entry, an href,
4368         # and link is the action links of the entry.
4370         print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
4371         if (exists $t->{'size'}) {
4372                 print "<td class=\"size\">$t->{'size'}</td>\n";
4373         }
4374         if ($t->{'type'} eq "blob") {
4375                 print "<td class=\"list\">" .
4376                         $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
4377                                                file_name=>"$basedir$t->{'name'}", %base_key),
4378                                 -class => "list"}, esc_path($t->{'name'}));
4379                 if (S_ISLNK(oct $t->{'mode'})) {
4380                         my $link_target = git_get_link_target($t->{'hash'});
4381                         if ($link_target) {
4382                                 my $norm_target = normalize_link_target($link_target, $basedir);
4383                                 if (defined $norm_target) {
4384                                         print " -> " .
4385                                               $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
4386                                                                      file_name=>$norm_target),
4387                                                        -title => $norm_target}, esc_path($link_target));
4388                                 } else {
4389                                         print " -> " . esc_path($link_target);
4390                                 }
4391                         }
4392                 }
4393                 print "</td>\n";
4394                 print "<td class=\"link\">";
4395                 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
4396                                              file_name=>"$basedir$t->{'name'}", %base_key)},
4397                               "blob");
4398                 if ($have_blame) {
4399                         print " | " .
4400                               $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
4401                                                      file_name=>"$basedir$t->{'name'}", %base_key)},
4402                                       "blame");
4403                 }
4404                 if (defined $hash_base) {
4405                         print " | " .
4406                               $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
4407                                                      hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
4408                                       "history");
4409                 }
4410                 print " | " .
4411                         $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
4412                                                file_name=>"$basedir$t->{'name'}")},
4413                                 "raw");
4414                 print "</td>\n";
4416         } elsif ($t->{'type'} eq "tree") {
4417                 print "<td class=\"list\">";
4418                 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
4419                                              file_name=>"$basedir$t->{'name'}",
4420                                              %base_key)},
4421                               esc_path($t->{'name'}));
4422                 print "</td>\n";
4423                 print "<td class=\"link\">";
4424                 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
4425                                              file_name=>"$basedir$t->{'name'}",
4426                                              %base_key)},
4427                               "tree");
4428                 if (defined $hash_base) {
4429                         print " | " .
4430                               $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
4431                                                      file_name=>"$basedir$t->{'name'}")},
4432                                       "history");
4433                 }
4434                 print "</td>\n";
4435         } else {
4436                 # unknown object: we can only present history for it
4437                 # (this includes 'commit' object, i.e. submodule support)
4438                 print "<td class=\"list\">" .
4439                       esc_path($t->{'name'}) .
4440                       "</td>\n";
4441                 print "<td class=\"link\">";
4442                 if (defined $hash_base) {
4443                         print $cgi->a({-href => href(action=>"history",
4444                                                      hash_base=>$hash_base,
4445                                                      file_name=>"$basedir$t->{'name'}")},
4446                                       "history");
4447                 }
4448                 print "</td>\n";
4449         }
4452 ## ......................................................................
4453 ## functions printing large fragments of HTML
4455 # get pre-image filenames for merge (combined) diff
4456 sub fill_from_file_info {
4457         my ($diff, @parents) = @_;
4459         $diff->{'from_file'} = [ ];
4460         $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
4461         for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
4462                 if ($diff->{'status'}[$i] eq 'R' ||
4463                     $diff->{'status'}[$i] eq 'C') {
4464                         $diff->{'from_file'}[$i] =
4465                                 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
4466                 }
4467         }
4469         return $diff;
4472 # is current raw difftree line of file deletion
4473 sub is_deleted {
4474         my $diffinfo = shift;
4476         return $diffinfo->{'to_id'} eq ('0' x 40);
4479 # does patch correspond to [previous] difftree raw line
4480 # $diffinfo  - hashref of parsed raw diff format
4481 # $patchinfo - hashref of parsed patch diff format
4482 #              (the same keys as in $diffinfo)
4483 sub is_patch_split {
4484         my ($diffinfo, $patchinfo) = @_;
4486         return defined $diffinfo && defined $patchinfo
4487                 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
4491 sub git_difftree_body {
4492         my ($difftree, $hash, @parents) = @_;
4493         my ($parent) = $parents[0];
4494         my $have_blame = gitweb_check_feature('blame');
4495         print "<div class=\"list_head\">\n";
4496         if ($#{$difftree} > 10) {
4497                 print(($#{$difftree} + 1) . " files changed:\n");
4498         }
4499         print "</div>\n";
4501         print "<table class=\"" .
4502               (@parents > 1 ? "combined " : "") .
4503               "diff_tree\">\n";
4505         # header only for combined diff in 'commitdiff' view
4506         my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
4507         if ($has_header) {
4508                 # table header
4509                 print "<thead><tr>\n" .
4510                        "<th></th><th></th>\n"; # filename, patchN link
4511                 for (my $i = 0; $i < @parents; $i++) {
4512                         my $par = $parents[$i];
4513                         print "<th>" .
4514                               $cgi->a({-href => href(action=>"commitdiff",
4515                                                      hash=>$hash, hash_parent=>$par),
4516                                        -title => 'commitdiff to parent number ' .
4517                                                   ($i+1) . ': ' . substr($par,0,7)},
4518                                       $i+1) .
4519                               "&nbsp;</th>\n";
4520                 }
4521                 print "</tr></thead>\n<tbody>\n";
4522         }
4524         my $alternate = 1;
4525         my $patchno = 0;
4526         foreach my $line (@{$difftree}) {
4527                 my $diff = parsed_difftree_line($line);
4529                 if ($alternate) {
4530                         print "<tr class=\"dark\">\n";
4531                 } else {
4532                         print "<tr class=\"light\">\n";
4533                 }
4534                 $alternate ^= 1;
4536                 if (exists $diff->{'nparents'}) { # combined diff
4538                         fill_from_file_info($diff, @parents)
4539                                 unless exists $diff->{'from_file'};
4541                         if (!is_deleted($diff)) {
4542                                 # file exists in the result (child) commit
4543                                 print "<td>" .
4544                                       $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4545                                                              file_name=>$diff->{'to_file'},
4546                                                              hash_base=>$hash),
4547                                               -class => "list"}, esc_path($diff->{'to_file'})) .
4548                                       "</td>\n";
4549                         } else {
4550                                 print "<td>" .
4551                                       esc_path($diff->{'to_file'}) .
4552                                       "</td>\n";
4553                         }
4555                         if ($action eq 'commitdiff') {
4556                                 # link to patch
4557                                 $patchno++;
4558                                 print "<td class=\"link\">" .
4559                                       $cgi->a({-href => href(-anchor=>"patch$patchno")},
4560                                               "patch") .
4561                                       " | " .
4562                                       "</td>\n";
4563                         }
4565                         my $has_history = 0;
4566                         my $not_deleted = 0;
4567                         for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
4568                                 my $hash_parent = $parents[$i];
4569                                 my $from_hash = $diff->{'from_id'}[$i];
4570                                 my $from_path = $diff->{'from_file'}[$i];
4571                                 my $status = $diff->{'status'}[$i];
4573                                 $has_history ||= ($status ne 'A');
4574                                 $not_deleted ||= ($status ne 'D');
4576                                 if ($status eq 'A') {
4577                                         print "<td  class=\"link\" align=\"right\"> | </td>\n";
4578                                 } elsif ($status eq 'D') {
4579                                         print "<td class=\"link\">" .
4580                                               $cgi->a({-href => href(action=>"blob",
4581                                                                      hash_base=>$hash,
4582                                                                      hash=>$from_hash,
4583                                                                      file_name=>$from_path)},
4584                                                       "blob" . ($i+1)) .
4585                                               " | </td>\n";
4586                                 } else {
4587                                         if ($diff->{'to_id'} eq $from_hash) {
4588                                                 print "<td class=\"link nochange\">";
4589                                         } else {
4590                                                 print "<td class=\"link\">";
4591                                         }
4592                                         print $cgi->a({-href => href(action=>"blobdiff",
4593                                                                      hash=>$diff->{'to_id'},
4594                                                                      hash_parent=>$from_hash,
4595                                                                      hash_base=>$hash,
4596                                                                      hash_parent_base=>$hash_parent,
4597                                                                      file_name=>$diff->{'to_file'},
4598                                                                      file_parent=>$from_path)},
4599                                                       "diff" . ($i+1)) .
4600                                               " | </td>\n";
4601                                 }
4602                         }
4604                         print "<td class=\"link\">";
4605                         if ($not_deleted) {
4606                                 print $cgi->a({-href => href(action=>"blob",
4607                                                              hash=>$diff->{'to_id'},
4608                                                              file_name=>$diff->{'to_file'},
4609                                                              hash_base=>$hash)},
4610                                               "blob");
4611                                 print " | " if ($has_history);
4612                         }
4613                         if ($has_history) {
4614                                 print $cgi->a({-href => href(action=>"history",
4615                                                              file_name=>$diff->{'to_file'},
4616                                                              hash_base=>$hash)},
4617                                               "history");
4618                         }
4619                         print "</td>\n";
4621                         print "</tr>\n";
4622                         next; # instead of 'else' clause, to avoid extra indent
4623                 }
4624                 # else ordinary diff
4626                 my ($to_mode_oct, $to_mode_str, $to_file_type);
4627                 my ($from_mode_oct, $from_mode_str, $from_file_type);
4628                 if ($diff->{'to_mode'} ne ('0' x 6)) {
4629                         $to_mode_oct = oct $diff->{'to_mode'};
4630                         if (S_ISREG($to_mode_oct)) { # only for regular file
4631                                 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
4632                         }
4633                         $to_file_type = file_type($diff->{'to_mode'});
4634                 }
4635                 if ($diff->{'from_mode'} ne ('0' x 6)) {
4636                         $from_mode_oct = oct $diff->{'from_mode'};
4637                         if (S_ISREG($from_mode_oct)) { # only for regular file
4638                                 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
4639                         }
4640                         $from_file_type = file_type($diff->{'from_mode'});
4641                 }
4643                 if ($diff->{'status'} eq "A") { # created
4644                         my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
4645                         $mode_chng   .= " with mode: $to_mode_str" if $to_mode_str;
4646                         $mode_chng   .= "]</span>";
4647                         print "<td>";
4648                         print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4649                                                      hash_base=>$hash, file_name=>$diff->{'file'}),
4650                                       -class => "list"}, esc_path($diff->{'file'}));
4651                         print "</td>\n";
4652                         print "<td>$mode_chng</td>\n";
4653                         print "<td class=\"link\">";
4654                         if ($action eq 'commitdiff') {
4655                                 # link to patch
4656                                 $patchno++;
4657                                 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
4658                                               "patch") .
4659                                       " | ";
4660                         }
4661                         print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4662                                                      hash_base=>$hash, file_name=>$diff->{'file'})},
4663                                       "blob");
4664                         print "</td>\n";
4666                 } elsif ($diff->{'status'} eq "D") { # deleted
4667                         my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
4668                         print "<td>";
4669                         print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
4670                                                      hash_base=>$parent, file_name=>$diff->{'file'}),
4671                                        -class => "list"}, esc_path($diff->{'file'}));
4672                         print "</td>\n";
4673                         print "<td>$mode_chng</td>\n";
4674                         print "<td class=\"link\">";
4675                         if ($action eq 'commitdiff') {
4676                                 # link to patch
4677                                 $patchno++;
4678                                 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
4679                                               "patch") .
4680                                       " | ";
4681                         }
4682                         print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
4683                                                      hash_base=>$parent, file_name=>$diff->{'file'})},
4684                                       "blob") . " | ";
4685                         if ($have_blame) {
4686                                 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
4687                                                              file_name=>$diff->{'file'})},
4688                                               "blame") . " | ";
4689                         }
4690                         print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
4691                                                      file_name=>$diff->{'file'})},
4692                                       "history");
4693                         print "</td>\n";
4695                 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
4696                         my $mode_chnge = "";
4697                         if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
4698                                 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
4699                                 if ($from_file_type ne $to_file_type) {
4700                                         $mode_chnge .= " from $from_file_type to $to_file_type";
4701                                 }
4702                                 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
4703                                         if ($from_mode_str && $to_mode_str) {
4704                                                 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
4705                                         } elsif ($to_mode_str) {
4706                                                 $mode_chnge .= " mode: $to_mode_str";
4707                                         }
4708                                 }
4709                                 $mode_chnge .= "]</span>\n";
4710                         }
4711                         print "<td>";
4712                         print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4713                                                      hash_base=>$hash, file_name=>$diff->{'file'}),
4714                                       -class => "list"}, esc_path($diff->{'file'}));
4715                         print "</td>\n";
4716                         print "<td>$mode_chnge</td>\n";
4717                         print "<td class=\"link\">";
4718                         if ($action eq 'commitdiff') {
4719                                 # link to patch
4720                                 $patchno++;
4721                                 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
4722                                               "patch") .
4723                                       " | ";
4724                         } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
4725                                 # "commit" view and modified file (not onlu mode changed)
4726                                 print $cgi->a({-href => href(action=>"blobdiff",
4727                                                              hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
4728                                                              hash_base=>$hash, hash_parent_base=>$parent,
4729                                                              file_name=>$diff->{'file'})},
4730                                               "diff") .
4731                                       " | ";
4732                         }
4733                         print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4734                                                      hash_base=>$hash, file_name=>$diff->{'file'})},
4735                                        "blob") . " | ";
4736                         if ($have_blame) {
4737                                 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
4738                                                              file_name=>$diff->{'file'})},
4739                                               "blame") . " | ";
4740                         }
4741                         print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
4742                                                      file_name=>$diff->{'file'})},
4743                                       "history");
4744                         print "</td>\n";
4746                 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
4747                         my %status_name = ('R' => 'moved', 'C' => 'copied');
4748                         my $nstatus = $status_name{$diff->{'status'}};
4749                         my $mode_chng = "";
4750                         if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
4751                                 # mode also for directories, so we cannot use $to_mode_str
4752                                 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
4753                         }
4754                         print "<td>" .
4755                               $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
4756                                                      hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
4757                                       -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
4758                               "<td><span class=\"file_status $nstatus\">[$nstatus from " .
4759                               $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
4760                                                      hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
4761                                       -class => "list"}, esc_path($diff->{'from_file'})) .
4762                               " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
4763                               "<td class=\"link\">";
4764                         if ($action eq 'commitdiff') {
4765                                 # link to patch
4766                                 $patchno++;
4767                                 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
4768                                               "patch") .
4769                                       " | ";
4770                         } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
4771                                 # "commit" view and modified file (not only pure rename or copy)
4772                                 print $cgi->a({-href => href(action=>"blobdiff",
4773                                                              hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
4774                                                              hash_base=>$hash, hash_parent_base=>$parent,
4775                                                              file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
4776                                               "diff") .
4777                                       " | ";
4778                         }
4779                         print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4780                                                      hash_base=>$parent, file_name=>$diff->{'to_file'})},
4781                                       "blob") . " | ";
4782                         if ($have_blame) {
4783                                 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
4784                                                              file_name=>$diff->{'to_file'})},
4785                                               "blame") . " | ";
4786                         }
4787                         print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
4788                                                     file_name=>$diff->{'to_file'})},
4789                                       "history");
4790                         print "</td>\n";
4792                 } # we should not encounter Unmerged (U) or Unknown (X) status
4793                 print "</tr>\n";
4794         }
4795         print "</tbody>" if $has_header;
4796         print "</table>\n";
4799 sub git_patchset_body {
4800         my ($fd, $difftree, $hash, @hash_parents) = @_;
4801         my ($hash_parent) = $hash_parents[0];
4803         my $is_combined = (@hash_parents > 1);
4804         my $patch_idx = 0;
4805         my $patch_number = 0;
4806         my $patch_line;
4807         my $diffinfo;
4808         my $to_name;
4809         my (%from, %to);
4811         print "<div class=\"patchset\">\n";
4813         # skip to first patch
4814         while ($patch_line = <$fd>) {
4815                 chomp $patch_line;
4817                 last if ($patch_line =~ m/^diff /);
4818         }
4820  PATCH:
4821         while ($patch_line) {
4823                 # parse "git diff" header line
4824                 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
4825                         # $1 is from_name, which we do not use
4826                         $to_name = unquote($2);
4827                         $to_name =~ s!^b/!!;
4828                 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
4829                         # $1 is 'cc' or 'combined', which we do not use
4830                         $to_name = unquote($2);
4831                 } else {
4832                         $to_name = undef;
4833                 }
4835                 # check if current patch belong to current raw line
4836                 # and parse raw git-diff line if needed
4837                 if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
4838                         # this is continuation of a split patch
4839                         print "<div class=\"patch cont\">\n";
4840                 } else {
4841                         # advance raw git-diff output if needed
4842                         $patch_idx++ if defined $diffinfo;
4844                         # read and prepare patch information
4845                         $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
4847                         # compact combined diff output can have some patches skipped
4848                         # find which patch (using pathname of result) we are at now;
4849                         if ($is_combined) {
4850                                 while ($to_name ne $diffinfo->{'to_file'}) {
4851                                         print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
4852                                               format_diff_cc_simplified($diffinfo, @hash_parents) .
4853                                               "</div>\n";  # class="patch"
4855                                         $patch_idx++;
4856                                         $patch_number++;
4858                                         last if $patch_idx > $#$difftree;
4859                                         $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
4860                                 }
4861                         }
4863                         # modifies %from, %to hashes
4864                         parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
4866                         # this is first patch for raw difftree line with $patch_idx index
4867                         # we index @$difftree array from 0, but number patches from 1
4868                         print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
4869                 }
4871                 # git diff header
4872                 #assert($patch_line =~ m/^diff /) if DEBUG;
4873                 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
4874                 $patch_number++;
4875                 # print "git diff" header
4876                 print format_git_diff_header_line($patch_line, $diffinfo,
4877                                                   \%from, \%to);
4879                 # print extended diff header
4880                 print "<div class=\"diff extended_header\">\n";
4881         EXTENDED_HEADER:
4882                 while ($patch_line = <$fd>) {
4883                         chomp $patch_line;
4885                         last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
4887                         print format_extended_diff_header_line($patch_line, $diffinfo,
4888                                                                \%from, \%to);
4889                 }
4890                 print "</div>\n"; # class="diff extended_header"
4892                 # from-file/to-file diff header
4893                 if (! $patch_line) {
4894                         print "</div>\n"; # class="patch"
4895                         last PATCH;
4896                 }
4897                 next PATCH if ($patch_line =~ m/^diff /);
4898                 #assert($patch_line =~ m/^---/) if DEBUG;
4900                 my $last_patch_line = $patch_line;
4901                 $patch_line = <$fd>;
4902                 chomp $patch_line;
4903                 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
4905                 print format_diff_from_to_header($last_patch_line, $patch_line,
4906                                                  $diffinfo, \%from, \%to,
4907                                                  @hash_parents);
4909                 # the patch itself
4910         LINE:
4911                 while ($patch_line = <$fd>) {
4912                         chomp $patch_line;
4914                         next PATCH if ($patch_line =~ m/^diff /);
4916                         print format_diff_line($patch_line, \%from, \%to);
4917                 }
4919         } continue {
4920                 print "</div>\n"; # class="patch"
4921         }
4923         # for compact combined (--cc) format, with chunk and patch simplification
4924         # the patchset might be empty, but there might be unprocessed raw lines
4925         for (++$patch_idx if $patch_number > 0;
4926              $patch_idx < @$difftree;
4927              ++$patch_idx) {
4928                 # read and prepare patch information
4929                 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
4931                 # generate anchor for "patch" links in difftree / whatchanged part
4932                 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
4933                       format_diff_cc_simplified($diffinfo, @hash_parents) .
4934                       "</div>\n";  # class="patch"
4936                 $patch_number++;
4937         }
4939         if ($patch_number == 0) {
4940                 if (@hash_parents > 1) {
4941                         print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
4942                 } else {
4943                         print "<div class=\"diff nodifferences\">No differences found</div>\n";
4944                 }
4945         }
4947         print "</div>\n"; # class="patchset"
4950 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4952 # fills project list info (age, description, owner, category, forks)
4953 # for each project in the list, removing invalid projects from
4954 # returned list
4955 # NOTE: modifies $projlist, but does not remove entries from it
4956 sub fill_project_list_info {
4957         my $projlist = shift;
4958         my @projects;
4960         my $show_ctags = gitweb_check_feature('ctags');
4961  PROJECT:
4962         foreach my $pr (@$projlist) {
4963                 my (@activity) = git_get_last_activity($pr->{'path'});
4964                 unless (@activity) {
4965                         next PROJECT;
4966                 }
4967                 ($pr->{'age'}, $pr->{'age_string'}) = @activity;
4968                 if (!defined $pr->{'descr'}) {
4969                         my $descr = git_get_project_description($pr->{'path'}) || "";
4970                         $descr = to_utf8($descr);
4971                         $pr->{'descr_long'} = $descr;
4972                         $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
4973                 }
4974                 if (!defined $pr->{'owner'}) {
4975                         $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
4976                 }
4977                 if ($show_ctags) {
4978                         $pr->{'ctags'} = git_get_project_ctags($pr->{'path'});
4979                 }
4980                 if ($projects_list_group_categories && !defined $pr->{'category'}) {
4981                         my $cat = git_get_project_category($pr->{'path'}) ||
4982                                                            $project_list_default_category;
4983                         $pr->{'category'} = to_utf8($cat);
4984                 }
4986                 push @projects, $pr;
4987         }
4989         return @projects;
4992 sub sort_projects_list {
4993         my ($projlist, $order) = @_;
4994         my @projects;
4996         my %order_info = (
4997                 project => { key => 'path', type => 'str' },
4998                 descr => { key => 'descr_long', type => 'str' },
4999                 owner => { key => 'owner', type => 'str' },
5000                 age => { key => 'age', type => 'num' }
5001         );
5002         my $oi = $order_info{$order};
5003         return @$projlist unless defined $oi;
5004         if ($oi->{'type'} eq 'str') {
5005                 @projects = sort {$a->{$oi->{'key'}} cmp $b->{$oi->{'key'}}} @$projlist;
5006         } else {
5007                 @projects = sort {$a->{$oi->{'key'}} <=> $b->{$oi->{'key'}}} @$projlist;
5008         }
5010         return @projects;
5013 # returns a hash of categories, containing the list of project
5014 # belonging to each category
5015 sub build_projlist_by_category {
5016         my ($projlist, $from, $to) = @_;
5017         my %categories;
5019         $from = 0 unless defined $from;
5020         $to = $#$projlist if (!defined $to || $#$projlist < $to);
5022         for (my $i = $from; $i <= $to; $i++) {
5023                 my $pr = $projlist->[$i];
5024                 push @{$categories{ $pr->{'category'} }}, $pr;
5025         }
5027         return wantarray ? %categories : \%categories;
5030 # print 'sort by' <th> element, generating 'sort by $name' replay link
5031 # if that order is not selected
5032 sub print_sort_th {
5033         print format_sort_th(@_);
5036 sub format_sort_th {
5037         my ($name, $order, $header) = @_;
5038         my $sort_th = "";
5039         $header ||= ucfirst($name);
5041         if ($order eq $name) {
5042                 $sort_th .= "<th>$header</th>\n";
5043         } else {
5044                 $sort_th .= "<th>" .
5045                             $cgi->a({-href => href(-replay=>1, order=>$name),
5046                                      -class => "header"}, $header) .
5047                             "</th>\n";
5048         }
5050         return $sort_th;
5053 sub git_project_list_rows {
5054         my ($projlist, $from, $to, $check_forks) = @_;
5056         $from = 0 unless defined $from;
5057         $to = $#$projlist if (!defined $to || $#$projlist < $to);
5059         my $alternate = 1;
5060         for (my $i = $from; $i <= $to; $i++) {
5061                 my $pr = $projlist->[$i];
5063                 if ($alternate) {
5064                         print "<tr class=\"dark\">\n";
5065                 } else {
5066                         print "<tr class=\"light\">\n";
5067                 }
5068                 $alternate ^= 1;
5070                 if ($check_forks) {
5071                         print "<td>";
5072                         if ($pr->{'forks'}) {
5073                                 my $nforks = scalar @{$pr->{'forks'}};
5074                                 if ($nforks > 0) {
5075                                         print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks"),
5076                                                        -title => "$nforks forks"}, "+");
5077                                 } else {
5078                                         print $cgi->span({-title => "$nforks forks"}, "+");
5079                                 }
5080                         }
5081                         print "</td>\n";
5082                 }
5083                 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
5084                                         -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
5085                       "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
5086                                         -class => "list", -title => $pr->{'descr_long'}},
5087                                         esc_html($pr->{'descr'})) . "</td>\n" .
5088                       "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n";
5089                 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
5090                       (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
5091                       "<td class=\"link\">" .
5092                       $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary")   . " | " .
5093                       $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
5094                       $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
5095                       $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
5096                       ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
5097                       "</td>\n" .
5098                       "</tr>\n";
5099         }
5102 sub git_project_list_body {
5103         # actually uses global variable $project
5104         my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
5105         my @projects = @$projlist;
5107         my $check_forks = gitweb_check_feature('forks');
5108         my $show_ctags  = gitweb_check_feature('ctags');
5109         my $tagfilter = $show_ctags ? $cgi->param('by_tag') : undef;
5110         $check_forks = undef
5111                 if ($tagfilter || $searchtext);
5113         # filtering out forks before filling info allows to do less work
5114         @projects = filter_forks_from_projects_list(\@projects)
5115                 if ($check_forks);
5116         @projects = fill_project_list_info(\@projects);
5117         # searching projects require filling to be run before it
5118         @projects = search_projects_list(\@projects,
5119                                          'searchtext' => $searchtext,
5120                                          'tagfilter'  => $tagfilter)
5121                 if ($tagfilter || $searchtext);
5123         $order ||= $default_projects_order;
5124         $from = 0 unless defined $from;
5125         $to = $#projects if (!defined $to || $#projects < $to);
5127         # short circuit
5128         if ($from > $to) {
5129                 print "<center>\n".
5130                       "<b>No such projects found</b><br />\n".
5131                       "Click ".$cgi->a({-href=>href(project=>undef)},"here")." to view all projects<br />\n".
5132                       "</center>\n<br />\n";
5133                 return;
5134         }
5136         @projects = sort_projects_list(\@projects, $order);
5138         if ($show_ctags) {
5139                 my $ctags = git_gather_all_ctags(\@projects);
5140                 my $cloud = git_populate_project_tagcloud($ctags);
5141                 print git_show_project_tagcloud($cloud, 64);
5142         }
5144         print "<table class=\"project_list\">\n";
5145         unless ($no_header) {
5146                 print "<tr>\n";
5147                 if ($check_forks) {
5148                         print "<th></th>\n";
5149                 }
5150                 print_sort_th('project', $order, 'Project');
5151                 print_sort_th('descr', $order, 'Description');
5152                 print_sort_th('owner', $order, 'Owner');
5153                 print_sort_th('age', $order, 'Last Change');
5154                 print "<th></th>\n" . # for links
5155                       "</tr>\n";
5156         }
5158         if ($projects_list_group_categories) {
5159                 # only display categories with projects in the $from-$to window
5160                 @projects = sort {$a->{'category'} cmp $b->{'category'}} @projects[$from..$to];
5161                 my %categories = build_projlist_by_category(\@projects, $from, $to);
5162                 foreach my $cat (sort keys %categories) {
5163                         unless ($cat eq "") {
5164                                 print "<tr>\n";
5165                                 if ($check_forks) {
5166                                         print "<td></td>\n";
5167                                 }
5168                                 print "<td class=\"category\" colspan=\"5\">".esc_html($cat)."</td>\n";
5169                                 print "</tr>\n";
5170                         }
5172                         git_project_list_rows($categories{$cat}, undef, undef, $check_forks);
5173                 }
5174         } else {
5175                 git_project_list_rows(\@projects, $from, $to, $check_forks);
5176         }
5178         if (defined $extra) {
5179                 print "<tr>\n";
5180                 if ($check_forks) {
5181                         print "<td></td>\n";
5182                 }
5183                 print "<td colspan=\"5\">$extra</td>\n" .
5184                       "</tr>\n";
5185         }
5186         print "</table>\n";
5189 sub git_log_body {
5190         # uses global variable $project
5191         my ($commitlist, $from, $to, $refs, $extra) = @_;
5193         $from = 0 unless defined $from;
5194         $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
5196         for (my $i = 0; $i <= $to; $i++) {
5197                 my %co = %{$commitlist->[$i]};
5198                 next if !%co;
5199                 my $commit = $co{'id'};
5200                 my $ref = format_ref_marker($refs, $commit);
5201                 git_print_header_div('commit',
5202                                "<span class=\"age\">$co{'age_string'}</span>" .
5203                                esc_html($co{'title'}) . $ref,
5204                                $commit);
5205                 print "<div class=\"title_text\">\n" .
5206                       "<div class=\"log_link\">\n" .
5207                       $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
5208                       " | " .
5209                       $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
5210                       " | " .
5211                       $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
5212                       "<br/>\n" .
5213                       "</div>\n";
5214                       git_print_authorship(\%co, -tag => 'span');
5215                       print "<br/>\n</div>\n";
5217                 print "<div class=\"log_body\">\n";
5218                 git_print_log($co{'comment'}, -final_empty_line=> 1);
5219                 print "</div>\n";
5220         }
5221         if ($extra) {
5222                 print "<div class=\"page_nav\">\n";
5223                 print "$extra\n";
5224                 print "</div>\n";
5225         }
5228 sub git_shortlog_body {
5229         # uses global variable $project
5230         my ($commitlist, $from, $to, $refs, $extra) = @_;
5232         $from = 0 unless defined $from;
5233         $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
5235         print "<table class=\"shortlog\">\n";
5236         my $alternate = 1;
5237         for (my $i = $from; $i <= $to; $i++) {
5238                 my %co = %{$commitlist->[$i]};
5239                 my $commit = $co{'id'};
5240                 my $ref = format_ref_marker($refs, $commit);
5241                 if ($alternate) {
5242                         print "<tr class=\"dark\">\n";
5243                 } else {
5244                         print "<tr class=\"light\">\n";
5245                 }
5246                 $alternate ^= 1;
5247                 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
5248                 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5249                       format_author_html('td', \%co, 10) . "<td>";
5250                 print format_subject_html($co{'title'}, $co{'title_short'},
5251                                           href(action=>"commit", hash=>$commit), $ref);
5252                 print "</td>\n" .
5253                       "<td class=\"link\">" .
5254                       $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
5255                       $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
5256                       $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
5257                 my $snapshot_links = format_snapshot_links($commit);
5258                 if (defined $snapshot_links) {
5259                         print " | " . $snapshot_links;
5260                 }
5261                 print "</td>\n" .
5262                       "</tr>\n";
5263         }
5264         if (defined $extra) {
5265                 print "<tr>\n" .
5266                       "<td colspan=\"4\">$extra</td>\n" .
5267                       "</tr>\n";
5268         }
5269         print "</table>\n";
5272 sub git_history_body {
5273         # Warning: assumes constant type (blob or tree) during history
5274         my ($commitlist, $from, $to, $refs, $extra,
5275             $file_name, $file_hash, $ftype) = @_;
5277         $from = 0 unless defined $from;
5278         $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
5280         print "<table class=\"history\">\n";
5281         my $alternate = 1;
5282         for (my $i = $from; $i <= $to; $i++) {
5283                 my %co = %{$commitlist->[$i]};
5284                 if (!%co) {
5285                         next;
5286                 }
5287                 my $commit = $co{'id'};
5289                 my $ref = format_ref_marker($refs, $commit);
5291                 if ($alternate) {
5292                         print "<tr class=\"dark\">\n";
5293                 } else {
5294                         print "<tr class=\"light\">\n";
5295                 }
5296                 $alternate ^= 1;
5297                 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5298         # shortlog:   format_author_html('td', \%co, 10)
5299                       format_author_html('td', \%co, 15, 3) . "<td>";
5300                 # originally git_history used chop_str($co{'title'}, 50)
5301                 print format_subject_html($co{'title'}, $co{'title_short'},
5302                                           href(action=>"commit", hash=>$commit), $ref);
5303                 print "</td>\n" .
5304                       "<td class=\"link\">" .
5305                       $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
5306                       $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
5308                 if ($ftype eq 'blob') {
5309                         my $blob_current = $file_hash;
5310                         my $blob_parent  = git_get_hash_by_path($commit, $file_name);
5311                         if (defined $blob_current && defined $blob_parent &&
5312                                         $blob_current ne $blob_parent) {
5313                                 print " | " .
5314                                         $cgi->a({-href => href(action=>"blobdiff",
5315                                                                hash=>$blob_current, hash_parent=>$blob_parent,
5316                                                                hash_base=>$hash_base, hash_parent_base=>$commit,
5317                                                                file_name=>$file_name)},
5318                                                 "diff to current");
5319                         }
5320                 }
5321                 print "</td>\n" .
5322                       "</tr>\n";
5323         }
5324         if (defined $extra) {
5325                 print "<tr>\n" .
5326                       "<td colspan=\"4\">$extra</td>\n" .
5327                       "</tr>\n";
5328         }
5329         print "</table>\n";
5332 sub git_tags_body {
5333         # uses global variable $project
5334         my ($taglist, $from, $to, $extra) = @_;
5335         $from = 0 unless defined $from;
5336         $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
5338         print "<table class=\"tags\">\n";
5339         my $alternate = 1;
5340         for (my $i = $from; $i <= $to; $i++) {
5341                 my $entry = $taglist->[$i];
5342                 my %tag = %$entry;
5343                 my $comment = $tag{'subject'};
5344                 my $comment_short;
5345                 if (defined $comment) {
5346                         $comment_short = chop_str($comment, 30, 5);
5347                 }
5348                 if ($alternate) {
5349                         print "<tr class=\"dark\">\n";
5350                 } else {
5351                         print "<tr class=\"light\">\n";
5352                 }
5353                 $alternate ^= 1;
5354                 if (defined $tag{'age'}) {
5355                         print "<td><i>$tag{'age'}</i></td>\n";
5356                 } else {
5357                         print "<td></td>\n";
5358                 }
5359                 print "<td>" .
5360                       $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
5361                                -class => "list name"}, esc_html($tag{'name'})) .
5362                       "</td>\n" .
5363                       "<td>";
5364                 if (defined $comment) {
5365                         print format_subject_html($comment, $comment_short,
5366                                                   href(action=>"tag", hash=>$tag{'id'}));
5367                 }
5368                 print "</td>\n" .
5369                       "<td class=\"selflink\">";
5370                 if ($tag{'type'} eq "tag") {
5371                         print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
5372                 } else {
5373                         print "&nbsp;";
5374                 }
5375                 print "</td>\n" .
5376                       "<td class=\"link\">" . " | " .
5377                       $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
5378                 if ($tag{'reftype'} eq "commit") {
5379                         print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})}, "shortlog") .
5380                               " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})}, "log");
5381                 } elsif ($tag{'reftype'} eq "blob") {
5382                         print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
5383                 }
5384                 print "</td>\n" .
5385                       "</tr>";
5386         }
5387         if (defined $extra) {
5388                 print "<tr>\n" .
5389                       "<td colspan=\"5\">$extra</td>\n" .
5390                       "</tr>\n";
5391         }
5392         print "</table>\n";
5395 sub git_heads_body {
5396         # uses global variable $project
5397         my ($headlist, $head, $from, $to, $extra) = @_;
5398         $from = 0 unless defined $from;
5399         $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
5401         print "<table class=\"heads\">\n";
5402         my $alternate = 1;
5403         for (my $i = $from; $i <= $to; $i++) {
5404                 my $entry = $headlist->[$i];
5405                 my %ref = %$entry;
5406                 my $curr = $ref{'id'} eq $head;
5407                 if ($alternate) {
5408                         print "<tr class=\"dark\">\n";
5409                 } else {
5410                         print "<tr class=\"light\">\n";
5411                 }
5412                 $alternate ^= 1;
5413                 print "<td><i>$ref{'age'}</i></td>\n" .
5414                       ($curr ? "<td class=\"current_head\">" : "<td>") .
5415                       $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),
5416                                -class => "list name"},esc_html($ref{'name'})) .
5417                       "</td>\n" .
5418                       "<td class=\"link\">" .
5419                       $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})}, "shortlog") . " | " .
5420                       $cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})}, "log") . " | " .
5421                       $cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'fullname'})}, "tree") .
5422                       "</td>\n" .
5423                       "</tr>";
5424         }
5425         if (defined $extra) {
5426                 print "<tr>\n" .
5427                       "<td colspan=\"3\">$extra</td>\n" .
5428                       "</tr>\n";
5429         }
5430         print "</table>\n";
5433 # Display a single remote block
5434 sub git_remote_block {
5435         my ($remote, $rdata, $limit, $head) = @_;
5437         my $heads = $rdata->{'heads'};
5438         my $fetch = $rdata->{'fetch'};
5439         my $push = $rdata->{'push'};
5441         my $urls_table = "<table class=\"projects_list\">\n" ;
5443         if (defined $fetch) {
5444                 if ($fetch eq $push) {
5445                         $urls_table .= format_repo_url("URL", $fetch);
5446                 } else {
5447                         $urls_table .= format_repo_url("Fetch URL", $fetch);
5448                         $urls_table .= format_repo_url("Push URL", $push) if defined $push;
5449                 }
5450         } elsif (defined $push) {
5451                 $urls_table .= format_repo_url("Push URL", $push);
5452         } else {
5453                 $urls_table .= format_repo_url("", "No remote URL");
5454         }
5456         $urls_table .= "</table>\n";
5458         my $dots;
5459         if (defined $limit && $limit < @$heads) {
5460                 $dots = $cgi->a({-href => href(action=>"remotes", hash=>$remote)}, "...");
5461         }
5463         print $urls_table;
5464         git_heads_body($heads, $head, 0, $limit, $dots);
5467 # Display a list of remote names with the respective fetch and push URLs
5468 sub git_remotes_list {
5469         my ($remotedata, $limit) = @_;
5470         print "<table class=\"heads\">\n";
5471         my $alternate = 1;
5472         my @remotes = sort keys %$remotedata;
5474         my $limited = $limit && $limit < @remotes;
5476         $#remotes = $limit - 1 if $limited;
5478         while (my $remote = shift @remotes) {
5479                 my $rdata = $remotedata->{$remote};
5480                 my $fetch = $rdata->{'fetch'};
5481                 my $push = $rdata->{'push'};
5482                 if ($alternate) {
5483                         print "<tr class=\"dark\">\n";
5484                 } else {
5485                         print "<tr class=\"light\">\n";
5486                 }
5487                 $alternate ^= 1;
5488                 print "<td>" .
5489                       $cgi->a({-href=> href(action=>'remotes', hash=>$remote),
5490                                -class=> "list name"},esc_html($remote)) .
5491                       "</td>";
5492                 print "<td class=\"link\">" .
5493                       (defined $fetch ? $cgi->a({-href=> $fetch}, "fetch") : "fetch") .
5494                       " | " .
5495                       (defined $push ? $cgi->a({-href=> $push}, "push") : "push") .
5496                       "</td>";
5498                 print "</tr>\n";
5499         }
5501         if ($limited) {
5502                 print "<tr>\n" .
5503                       "<td colspan=\"3\">" .
5504                       $cgi->a({-href => href(action=>"remotes")}, "...") .
5505                       "</td>\n" . "</tr>\n";
5506         }
5508         print "</table>";
5511 # Display remote heads grouped by remote, unless there are too many
5512 # remotes, in which case we only display the remote names
5513 sub git_remotes_body {
5514         my ($remotedata, $limit, $head) = @_;
5515         if ($limit and $limit < keys %$remotedata) {
5516                 git_remotes_list($remotedata, $limit);
5517         } else {
5518                 fill_remote_heads($remotedata);
5519                 while (my ($remote, $rdata) = each %$remotedata) {
5520                         git_print_section({-class=>"remote", -id=>$remote},
5521                                 ["remotes", $remote, $remote], sub {
5522                                         git_remote_block($remote, $rdata, $limit, $head);
5523                                 });
5524                 }
5525         }
5528 sub git_search_message {
5529         my %co = @_;
5531         my $greptype;
5532         if ($searchtype eq 'commit') {
5533                 $greptype = "--grep=";
5534         } elsif ($searchtype eq 'author') {
5535                 $greptype = "--author=";
5536         } elsif ($searchtype eq 'committer') {
5537                 $greptype = "--committer=";
5538         }
5539         $greptype .= $searchtext;
5540         my @commitlist = parse_commits($hash, 101, (100 * $page), undef,
5541                                        $greptype, '--regexp-ignore-case',
5542                                        $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
5544         my $paging_nav = '';
5545         if ($page > 0) {
5546                 $paging_nav .=
5547                         $cgi->a({-href => href(-replay=>1, page=>undef)},
5548                                 "first") .
5549                         " &sdot; " .
5550                         $cgi->a({-href => href(-replay=>1, page=>$page-1),
5551                                  -accesskey => "p", -title => "Alt-p"}, "prev");
5552         } else {
5553                 $paging_nav .= "first &sdot; prev";
5554         }
5555         my $next_link = '';
5556         if ($#commitlist >= 100) {
5557                 $next_link =
5558                         $cgi->a({-href => href(-replay=>1, page=>$page+1),
5559                                  -accesskey => "n", -title => "Alt-n"}, "next");
5560                 $paging_nav .= " &sdot; $next_link";
5561         } else {
5562                 $paging_nav .= " &sdot; next";
5563         }
5565         git_header_html();
5567         git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
5568         git_print_header_div('commit', esc_html($co{'title'}), $hash);
5569         if ($page == 0 && !@commitlist) {
5570                 print "<p>No match.</p>\n";
5571         } else {
5572                 git_search_grep_body(\@commitlist, 0, 99, $next_link);
5573         }
5575         git_footer_html();
5578 sub git_search_changes {
5579         my %co = @_;
5581         local $/ = "\n";
5582         open my $fd, '-|', git_cmd(), '--no-pager', 'log', @diff_opts,
5583                 '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
5584                 ($search_use_regexp ? '--pickaxe-regex' : ())
5585                         or die_error(500, "Open git-log failed");
5587         git_header_html();
5589         git_print_page_nav('','', $hash,$co{'tree'},$hash);
5590         git_print_header_div('commit', esc_html($co{'title'}), $hash);
5592         print "<table class=\"pickaxe search\">\n";
5593         my $alternate = 1;
5594         undef %co;
5595         my @files;
5596         while (my $line = <$fd>) {
5597                 chomp $line;
5598                 next unless $line;
5600                 my %set = parse_difftree_raw_line($line);
5601                 if (defined $set{'commit'}) {
5602                         # finish previous commit
5603                         if (%co) {
5604                                 print "</td>\n" .
5605                                       "<td class=\"link\">" .
5606                                       $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},
5607                                               "commit") .
5608                                       " | " .
5609                                       $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'},
5610                                                              hash_base=>$co{'id'})},
5611                                               "tree") .
5612                                       "</td>\n" .
5613                                       "</tr>\n";
5614                         }
5616                         if ($alternate) {
5617                                 print "<tr class=\"dark\">\n";
5618                         } else {
5619                                 print "<tr class=\"light\">\n";
5620                         }
5621                         $alternate ^= 1;
5622                         %co = parse_commit($set{'commit'});
5623                         my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
5624                         print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5625                               "<td><i>$author</i></td>\n" .
5626                               "<td>" .
5627                               $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
5628                                       -class => "list subject"},
5629                                       chop_and_escape_str($co{'title'}, 50) . "<br/>");
5630                 } elsif (defined $set{'to_id'}) {
5631                         next if ($set{'to_id'} =~ m/^0{40}$/);
5633                         print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
5634                                                      hash=>$set{'to_id'}, file_name=>$set{'to_file'}),
5635                                       -class => "list"},
5636                                       "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
5637                               "<br/>\n";
5638                 }
5639         }
5640         close $fd;
5642         # finish last commit (warning: repetition!)
5643         if (%co) {
5644                 print "</td>\n" .
5645                       "<td class=\"link\">" .
5646                       $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},
5647                               "commit") .
5648                       " | " .
5649                       $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'},
5650                                              hash_base=>$co{'id'})},
5651                               "tree") .
5652                       "</td>\n" .
5653                       "</tr>\n";
5654         }
5656         print "</table>\n";
5658         git_footer_html();
5661 sub git_search_files {
5662         my %co = @_;
5664         local $/ = "\n";
5665         open my $fd, "-|", git_cmd(), 'grep', '-n',
5666                 $search_use_regexp ? ('-E', '-i') : '-F',
5667                 $searchtext, $co{'tree'}
5668                         or die_error(500, "Open git-grep failed");
5670         git_header_html();
5672         git_print_page_nav('','', $hash,$co{'tree'},$hash);
5673         git_print_header_div('commit', esc_html($co{'title'}), $hash);
5675         print "<table class=\"grep_search\">\n";
5676         my $alternate = 1;
5677         my $matches = 0;
5678         my $lastfile = '';
5679         while (my $line = <$fd>) {
5680                 chomp $line;
5681                 my ($file, $lno, $ltext, $binary);
5682                 last if ($matches++ > 1000);
5683                 if ($line =~ /^Binary file (.+) matches$/) {
5684                         $file = $1;
5685                         $binary = 1;
5686                 } else {
5687                         (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
5688                 }
5689                 if ($file ne $lastfile) {
5690                         $lastfile and print "</td></tr>\n";
5691                         if ($alternate++) {
5692                                 print "<tr class=\"dark\">\n";
5693                         } else {
5694                                 print "<tr class=\"light\">\n";
5695                         }
5696                         print "<td class=\"list\">".
5697                                 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5698                                                        file_name=>"$file"),
5699                                         -class => "list"}, esc_path($file));
5700                         print "</td><td>\n";
5701                         $lastfile = $file;
5702                 }
5703                 if ($binary) {
5704                         print "<div class=\"binary\">Binary file</div>\n";
5705                 } else {
5706                         $ltext = untabify($ltext);
5707                         if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
5708                                 $ltext = esc_html($1, -nbsp=>1);
5709                                 $ltext .= '<span class="match">';
5710                                 $ltext .= esc_html($2, -nbsp=>1);
5711                                 $ltext .= '</span>';
5712                                 $ltext .= esc_html($3, -nbsp=>1);
5713                         } else {
5714                                 $ltext = esc_html($ltext, -nbsp=>1);
5715                         }
5716                         print "<div class=\"pre\">" .
5717                                 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5718                                                        file_name=>"$file").'#l'.$lno,
5719                                         -class => "linenr"}, sprintf('%4i', $lno))
5720                                 . ' ' .  $ltext . "</div>\n";
5721                 }
5722         }
5723         if ($lastfile) {
5724                 print "</td></tr>\n";
5725                 if ($matches > 1000) {
5726                         print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
5727                 }
5728         } else {
5729                 print "<div class=\"diff nodifferences\">No matches found</div>\n";
5730         }
5731         close $fd;
5733         print "</table>\n";
5735         git_footer_html();
5738 sub git_search_grep_body {
5739         my ($commitlist, $from, $to, $extra) = @_;
5740         $from = 0 unless defined $from;
5741         $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
5743         print "<table class=\"commit_search\">\n";
5744         my $alternate = 1;
5745         for (my $i = $from; $i <= $to; $i++) {
5746                 my %co = %{$commitlist->[$i]};
5747                 if (!%co) {
5748                         next;
5749                 }
5750                 my $commit = $co{'id'};
5751                 if ($alternate) {
5752                         print "<tr class=\"dark\">\n";
5753                 } else {
5754                         print "<tr class=\"light\">\n";
5755                 }
5756                 $alternate ^= 1;
5757                 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5758                       format_author_html('td', \%co, 15, 5) .
5759                       "<td>" .
5760                       $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
5761                                -class => "list subject"},
5762                               chop_and_escape_str($co{'title'}, 50) . "<br/>");
5763                 my $comment = $co{'comment'};
5764                 foreach my $line (@$comment) {
5765                         if ($line =~ m/^(.*?)($search_regexp)(.*)$/i) {
5766                                 my ($lead, $match, $trail) = ($1, $2, $3);
5767                                 $match = chop_str($match, 70, 5, 'center');
5768                                 my $contextlen = int((80 - length($match))/2);
5769                                 $contextlen = 30 if ($contextlen > 30);
5770                                 $lead  = chop_str($lead,  $contextlen, 10, 'left');
5771                                 $trail = chop_str($trail, $contextlen, 10, 'right');
5773                                 $lead  = esc_html($lead);
5774                                 $match = esc_html($match);
5775                                 $trail = esc_html($trail);
5777                                 print "$lead<span class=\"match\">$match</span>$trail<br />";
5778                         }
5779                 }
5780                 print "</td>\n" .
5781                       "<td class=\"link\">" .
5782                       $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5783                       " | " .
5784                       $cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})}, "commitdiff") .
5785                       " | " .
5786                       $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5787                 print "</td>\n" .
5788                       "</tr>\n";
5789         }
5790         if (defined $extra) {
5791                 print "<tr>\n" .
5792                       "<td colspan=\"3\">$extra</td>\n" .
5793                       "</tr>\n";
5794         }
5795         print "</table>\n";
5798 ## ======================================================================
5799 ## ======================================================================
5800 ## actions
5802 sub git_project_list {
5803         my $order = $input_params{'order'};
5804         if (defined $order && $order !~ m/none|project|descr|owner|age/) {
5805                 die_error(400, "Unknown order parameter");
5806         }
5808         my @list = git_get_projects_list();
5809         if (!@list) {
5810                 die_error(404, "No projects found");
5811         }
5813         git_header_html();
5814         if (defined $home_text && -f $home_text) {
5815                 print "<div class=\"index_include\">\n";
5816                 insert_file($home_text);
5817                 print "</div>\n";
5818         }
5819         print $cgi->startform(-method => "get") .
5820               "<p class=\"projsearch\">Search:\n" .
5821               $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
5822               "</p>" .
5823               $cgi->end_form() . "\n";
5824         git_project_list_body(\@list, $order);
5825         git_footer_html();
5828 sub git_forks {
5829         my $order = $input_params{'order'};
5830         if (defined $order && $order !~ m/none|project|descr|owner|age/) {
5831                 die_error(400, "Unknown order parameter");
5832         }
5834         my @list = git_get_projects_list($project);
5835         if (!@list) {
5836                 die_error(404, "No forks found");
5837         }
5839         git_header_html();
5840         git_print_page_nav('','');
5841         git_print_header_div('summary', "$project forks");
5842         git_project_list_body(\@list, $order);
5843         git_footer_html();
5846 sub git_project_index {
5847         my @projects = git_get_projects_list();
5848         if (!@projects) {
5849                 die_error(404, "No projects found");
5850         }
5852         print $cgi->header(
5853                 -type => 'text/plain',
5854                 -charset => 'utf-8',
5855                 -content_disposition => 'inline; filename="index.aux"');
5857         foreach my $pr (@projects) {
5858                 if (!exists $pr->{'owner'}) {
5859                         $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
5860                 }
5862                 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
5863                 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
5864                 $path  =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
5865                 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
5866                 $path  =~ s/ /\+/g;
5867                 $owner =~ s/ /\+/g;
5869                 print "$path $owner\n";
5870         }
5873 sub git_summary {
5874         my $descr = git_get_project_description($project) || "none";
5875         my %co = parse_commit("HEAD");
5876         my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
5877         my $head = $co{'id'};
5878         my $remote_heads = gitweb_check_feature('remote_heads');
5880         my $owner = git_get_project_owner($project);
5882         my $refs = git_get_references();
5883         # These get_*_list functions return one more to allow us to see if
5884         # there are more ...
5885         my @taglist  = git_get_tags_list(16);
5886         my @headlist = git_get_heads_list(16);
5887         my %remotedata = $remote_heads ? git_get_remotes_list() : ();
5888         my @forklist;
5889         my $check_forks = gitweb_check_feature('forks');
5891         if ($check_forks) {
5892                 # find forks of a project
5893                 @forklist = git_get_projects_list($project);
5894                 # filter out forks of forks
5895                 @forklist = filter_forks_from_projects_list(\@forklist)
5896                         if (@forklist);
5897         }
5899         git_header_html();
5900         git_print_page_nav('summary','', $head);
5902         print "<div class=\"title\">&nbsp;</div>\n";
5903         print "<table class=\"projects_list\">\n" .
5904               "<tr id=\"metadata_desc\"><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
5905               "<tr id=\"metadata_owner\"><td>owner</td><td>" . esc_html($owner) . "</td></tr>\n";
5906         if (defined $cd{'rfc2822'}) {
5907                 print "<tr id=\"metadata_lchange\"><td>last change</td>" .
5908                       "<td>".format_timestamp_html(\%cd)."</td></tr>\n";
5909         }
5911         # use per project git URL list in $projectroot/$project/cloneurl
5912         # or make project git URL from git base URL and project name
5913         my $url_tag = "URL";
5914         my @url_list = git_get_project_url_list($project);
5915         @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
5916         foreach my $git_url (@url_list) {
5917                 next unless $git_url;
5918                 print format_repo_url($url_tag, $git_url);
5919                 $url_tag = "";
5920         }
5922         # Tag cloud
5923         my $show_ctags = gitweb_check_feature('ctags');
5924         if ($show_ctags) {
5925                 my $ctags = git_get_project_ctags($project);
5926                 if (%$ctags) {
5927                         # without ability to add tags, don't show if there are none
5928                         my $cloud = git_populate_project_tagcloud($ctags);
5929                         print "<tr id=\"metadata_ctags\">" .
5930                               "<td>content tags</td>" .
5931                               "<td>".git_show_project_tagcloud($cloud, 48)."</td>" .
5932                               "</tr>\n";
5933                 }
5934         }
5936         print "</table>\n";
5938         # If XSS prevention is on, we don't include README.html.
5939         # TODO: Allow a readme in some safe format.
5940         if (!$prevent_xss && -s "$projectroot/$project/README.html") {
5941                 print "<div class=\"title\">readme</div>\n" .
5942                       "<div class=\"readme\">\n";
5943                 insert_file("$projectroot/$project/README.html");
5944                 print "\n</div>\n"; # class="readme"
5945         }
5947         # we need to request one more than 16 (0..15) to check if
5948         # those 16 are all
5949         my @commitlist = $head ? parse_commits($head, 17) : ();
5950         if (@commitlist) {
5951                 git_print_header_div('shortlog');
5952                 git_shortlog_body(\@commitlist, 0, 15, $refs,
5953                                   $#commitlist <=  15 ? undef :
5954                                   $cgi->a({-href => href(action=>"shortlog")}, "..."));
5955         }
5957         if (@taglist) {
5958                 git_print_header_div('tags');
5959                 git_tags_body(\@taglist, 0, 15,
5960                               $#taglist <=  15 ? undef :
5961                               $cgi->a({-href => href(action=>"tags")}, "..."));
5962         }
5964         if (@headlist) {
5965                 git_print_header_div('heads');
5966                 git_heads_body(\@headlist, $head, 0, 15,
5967                                $#headlist <= 15 ? undef :
5968                                $cgi->a({-href => href(action=>"heads")}, "..."));
5969         }
5971         if (%remotedata) {
5972                 git_print_header_div('remotes');
5973                 git_remotes_body(\%remotedata, 15, $head);
5974         }
5976         if (@forklist) {
5977                 git_print_header_div('forks');
5978                 git_project_list_body(\@forklist, 'age', 0, 15,
5979                                       $#forklist <= 15 ? undef :
5980                                       $cgi->a({-href => href(action=>"forks")}, "..."),
5981                                       'no_header');
5982         }
5984         git_footer_html();
5987 sub git_tag {
5988         my %tag = parse_tag($hash);
5990         if (! %tag) {
5991                 die_error(404, "Unknown tag object");
5992         }
5994         my $head = git_get_head_hash($project);
5995         git_header_html();
5996         git_print_page_nav('','', $head,undef,$head);
5997         git_print_header_div('commit', esc_html($tag{'name'}), $hash);
5998         print "<div class=\"title_text\">\n" .
5999               "<table class=\"object_header\">\n" .
6000               "<tr>\n" .
6001               "<td>object</td>\n" .
6002               "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
6003                                $tag{'object'}) . "</td>\n" .
6004               "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
6005                                               $tag{'type'}) . "</td>\n" .
6006               "</tr>\n";
6007         if (defined($tag{'author'})) {
6008                 git_print_authorship_rows(\%tag, 'author');
6009         }
6010         print "</table>\n\n" .
6011               "</div>\n";
6012         print "<div class=\"page_body\">";
6013         my $comment = $tag{'comment'};
6014         foreach my $line (@$comment) {
6015                 chomp $line;
6016                 print esc_html($line, -nbsp=>1) . "<br/>\n";
6017         }
6018         print "</div>\n";
6019         git_footer_html();
6022 sub git_blame_common {
6023         my $format = shift || 'porcelain';
6024         if ($format eq 'porcelain' && $cgi->param('js')) {
6025                 $format = 'incremental';
6026                 $action = 'blame_incremental'; # for page title etc
6027         }
6029         # permissions
6030         gitweb_check_feature('blame')
6031                 or die_error(403, "Blame view not allowed");
6033         # error checking
6034         die_error(400, "No file name given") unless $file_name;
6035         $hash_base ||= git_get_head_hash($project);
6036         die_error(404, "Couldn't find base commit") unless $hash_base;
6037         my %co = parse_commit($hash_base)
6038                 or die_error(404, "Commit not found");
6039         my $ftype = "blob";
6040         if (!defined $hash) {
6041                 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
6042                         or die_error(404, "Error looking up file");
6043         } else {
6044                 $ftype = git_get_type($hash);
6045                 if ($ftype !~ "blob") {
6046                         die_error(400, "Object is not a blob");
6047                 }
6048         }
6050         my $fd;
6051         if ($format eq 'incremental') {
6052                 # get file contents (as base)
6053                 open $fd, "-|", git_cmd(), 'cat-file', 'blob', $hash
6054                         or die_error(500, "Open git-cat-file failed");
6055         } elsif ($format eq 'data') {
6056                 # run git-blame --incremental
6057                 open $fd, "-|", git_cmd(), "blame", "--incremental",
6058                         $hash_base, "--", $file_name
6059                         or die_error(500, "Open git-blame --incremental failed");
6060         } else {
6061                 # run git-blame --porcelain
6062                 open $fd, "-|", git_cmd(), "blame", '-p',
6063                         $hash_base, '--', $file_name
6064                         or die_error(500, "Open git-blame --porcelain failed");
6065         }
6067         # incremental blame data returns early
6068         if ($format eq 'data') {
6069                 print $cgi->header(
6070                         -type=>"text/plain", -charset => "utf-8",
6071                         -status=> "200 OK");
6072                 local $| = 1; # output autoflush
6073                 print while <$fd>;
6074                 close $fd
6075                         or print "ERROR $!\n";
6077                 print 'END';
6078                 if (defined $t0 && gitweb_check_feature('timed')) {
6079                         print ' '.
6080                               tv_interval($t0, [ gettimeofday() ]).
6081                               ' '.$number_of_git_cmds;
6082                 }
6083                 print "\n";
6085                 return;
6086         }
6088         # page header
6089         git_header_html();
6090         my $formats_nav =
6091                 $cgi->a({-href => href(action=>"blob", -replay=>1)},
6092                         "blob") .
6093                 " | ";
6094         if ($format eq 'incremental') {
6095                 $formats_nav .=
6096                         $cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},
6097                                 "blame") . " (non-incremental)";
6098         } else {
6099                 $formats_nav .=
6100                         $cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},
6101                                 "blame") . " (incremental)";
6102         }
6103         $formats_nav .=
6104                 " | " .
6105                 $cgi->a({-href => href(action=>"history", -replay=>1)},
6106                         "history") .
6107                 " | " .
6108                 $cgi->a({-href => href(action=>$action, file_name=>$file_name)},
6109                         "HEAD");
6110         git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
6111         git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
6112         git_print_page_path($file_name, $ftype, $hash_base);
6114         # page body
6115         if ($format eq 'incremental') {
6116                 print "<noscript>\n<div class=\"error\"><center><b>\n".
6117                       "This page requires JavaScript to run.\n Use ".
6118                       $cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},
6119                               'this page').
6120                       " instead.\n".
6121                       "</b></center></div>\n</noscript>\n";
6123                 print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;
6124         }
6126         print qq!<div class="page_body">\n!;
6127         print qq!<div id="progress_info">... / ...</div>\n!
6128                 if ($format eq 'incremental');
6129         print qq!<table id="blame_table" class="blame" width="100%">\n!.
6130               #qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.
6131               qq!<thead>\n!.
6132               qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.
6133               qq!</thead>\n!.
6134               qq!<tbody>\n!;
6136         my @rev_color = qw(light dark);
6137         my $num_colors = scalar(@rev_color);
6138         my $current_color = 0;
6140         if ($format eq 'incremental') {
6141                 my $color_class = $rev_color[$current_color];
6143                 #contents of a file
6144                 my $linenr = 0;
6145         LINE:
6146                 while (my $line = <$fd>) {
6147                         chomp $line;
6148                         $linenr++;
6150                         print qq!<tr id="l$linenr" class="$color_class">!.
6151                               qq!<td class="sha1"><a href=""> </a></td>!.
6152                               qq!<td class="linenr">!.
6153                               qq!<a class="linenr" href="">$linenr</a></td>!;
6154                         print qq!<td class="pre">! . esc_html($line) . "</td>\n";
6155                         print qq!</tr>\n!;
6156                 }
6158         } else { # porcelain, i.e. ordinary blame
6159                 my %metainfo = (); # saves information about commits
6161                 # blame data
6162         LINE:
6163                 while (my $line = <$fd>) {
6164                         chomp $line;
6165                         # the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]
6166                         # no <lines in group> for subsequent lines in group of lines
6167                         my ($full_rev, $orig_lineno, $lineno, $group_size) =
6168                            ($line =~ /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);
6169                         if (!exists $metainfo{$full_rev}) {
6170                                 $metainfo{$full_rev} = { 'nprevious' => 0 };
6171                         }
6172                         my $meta = $metainfo{$full_rev};
6173                         my $data;
6174                         while ($data = <$fd>) {
6175                                 chomp $data;
6176                                 last if ($data =~ s/^\t//); # contents of line
6177                                 if ($data =~ /^(\S+)(?: (.*))?$/) {
6178                                         $meta->{$1} = $2 unless exists $meta->{$1};
6179                                 }
6180                                 if ($data =~ /^previous /) {
6181                                         $meta->{'nprevious'}++;
6182                                 }
6183                         }
6184                         my $short_rev = substr($full_rev, 0, 8);
6185                         my $author = $meta->{'author'};
6186                         my %date =
6187                                 parse_date($meta->{'author-time'}, $meta->{'author-tz'});
6188                         my $date = $date{'iso-tz'};
6189                         if ($group_size) {
6190                                 $current_color = ($current_color + 1) % $num_colors;
6191                         }
6192                         my $tr_class = $rev_color[$current_color];
6193                         $tr_class .= ' boundary' if (exists $meta->{'boundary'});
6194                         $tr_class .= ' no-previous' if ($meta->{'nprevious'} == 0);
6195                         $tr_class .= ' multiple-previous' if ($meta->{'nprevious'} > 1);
6196                         print "<tr id=\"l$lineno\" class=\"$tr_class\">\n";
6197                         if ($group_size) {
6198                                 print "<td class=\"sha1\"";
6199                                 print " title=\"". esc_html($author) . ", $date\"";
6200                                 print " rowspan=\"$group_size\"" if ($group_size > 1);
6201                                 print ">";
6202                                 print $cgi->a({-href => href(action=>"commit",
6203                                                              hash=>$full_rev,
6204                                                              file_name=>$file_name)},
6205                                               esc_html($short_rev));
6206                                 if ($group_size >= 2) {
6207                                         my @author_initials = ($author =~ /\b([[:upper:]])\B/g);
6208                                         if (@author_initials) {
6209                                                 print "<br />" .
6210                                                       esc_html(join('', @author_initials));
6211                                                 #           or join('.', ...)
6212                                         }
6213                                 }
6214                                 print "</td>\n";
6215                         }
6216                         # 'previous' <sha1 of parent commit> <filename at commit>
6217                         if (exists $meta->{'previous'} &&
6218                             $meta->{'previous'} =~ /^([a-fA-F0-9]{40}) (.*)$/) {
6219                                 $meta->{'parent'} = $1;
6220                                 $meta->{'file_parent'} = unquote($2);
6221                         }
6222                         my $linenr_commit =
6223                                 exists($meta->{'parent'}) ?
6224                                 $meta->{'parent'} : $full_rev;
6225                         my $linenr_filename =
6226                                 exists($meta->{'file_parent'}) ?
6227                                 $meta->{'file_parent'} : unquote($meta->{'filename'});
6228                         my $blamed = href(action => 'blame',
6229                                           file_name => $linenr_filename,
6230                                           hash_base => $linenr_commit);
6231                         print "<td class=\"linenr\">";
6232                         print $cgi->a({ -href => "$blamed#l$orig_lineno",
6233                                         -class => "linenr" },
6234                                       esc_html($lineno));
6235                         print "</td>";
6236                         print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
6237                         print "</tr>\n";
6238                 } # end while
6240         }
6242         # footer
6243         print "</tbody>\n".
6244               "</table>\n"; # class="blame"
6245         print "</div>\n";   # class="blame_body"
6246         close $fd
6247                 or print "Reading blob failed\n";
6249         git_footer_html();
6252 sub git_blame {
6253         git_blame_common();
6256 sub git_blame_incremental {
6257         git_blame_common('incremental');
6260 sub git_blame_data {
6261         git_blame_common('data');
6264 sub git_tags {
6265         my $head = git_get_head_hash($project);
6266         git_header_html();
6267         git_print_page_nav('','', $head,undef,$head,format_ref_views('tags'));
6268         git_print_header_div('summary', $project);
6270         my @tagslist = git_get_tags_list();
6271         if (@tagslist) {
6272                 git_tags_body(\@tagslist);
6273         }
6274         git_footer_html();
6277 sub git_heads {
6278         my $head = git_get_head_hash($project);
6279         git_header_html();
6280         git_print_page_nav('','', $head,undef,$head,format_ref_views('heads'));
6281         git_print_header_div('summary', $project);
6283         my @headslist = git_get_heads_list();
6284         if (@headslist) {
6285                 git_heads_body(\@headslist, $head);
6286         }
6287         git_footer_html();
6290 # used both for single remote view and for list of all the remotes
6291 sub git_remotes {
6292         gitweb_check_feature('remote_heads')
6293                 or die_error(403, "Remote heads view is disabled");
6295         my $head = git_get_head_hash($project);
6296         my $remote = $input_params{'hash'};
6298         my $remotedata = git_get_remotes_list($remote);
6299         die_error(500, "Unable to get remote information") unless defined $remotedata;
6301         unless (%$remotedata) {
6302                 die_error(404, defined $remote ?
6303                         "Remote $remote not found" :
6304                         "No remotes found");
6305         }
6307         git_header_html(undef, undef, -action_extra => $remote);
6308         git_print_page_nav('', '',  $head, undef, $head,
6309                 format_ref_views($remote ? '' : 'remotes'));
6311         fill_remote_heads($remotedata);
6312         if (defined $remote) {
6313                 git_print_header_div('remotes', "$remote remote for $project");
6314                 git_remote_block($remote, $remotedata->{$remote}, undef, $head);
6315         } else {
6316                 git_print_header_div('summary', "$project remotes");
6317                 git_remotes_body($remotedata, undef, $head);
6318         }
6320         git_footer_html();
6323 sub git_blob_plain {
6324         my $type = shift;
6325         my $expires;
6327         if (!defined $hash) {
6328                 if (defined $file_name) {
6329                         my $base = $hash_base || git_get_head_hash($project);
6330                         $hash = git_get_hash_by_path($base, $file_name, "blob")
6331                                 or die_error(404, "Cannot find file");
6332                 } else {
6333                         die_error(400, "No file name defined");
6334                 }
6335         } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6336                 # blobs defined by non-textual hash id's can be cached
6337                 $expires = "+1d";
6338         }
6340         open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
6341                 or die_error(500, "Open git-cat-file blob '$hash' failed");
6343         # content-type (can include charset)
6344         $type = blob_contenttype($fd, $file_name, $type);
6346         # "save as" filename, even when no $file_name is given
6347         my $save_as = "$hash";
6348         if (defined $file_name) {
6349                 $save_as = $file_name;
6350         } elsif ($type =~ m/^text\//) {
6351                 $save_as .= '.txt';
6352         }
6354         # With XSS prevention on, blobs of all types except a few known safe
6355         # ones are served with "Content-Disposition: attachment" to make sure
6356         # they don't run in our security domain.  For certain image types,
6357         # blob view writes an <img> tag referring to blob_plain view, and we
6358         # want to be sure not to break that by serving the image as an
6359         # attachment (though Firefox 3 doesn't seem to care).
6360         my $sandbox = $prevent_xss &&
6361                 $type !~ m!^(?:text/[a-z]+|image/(?:gif|png|jpeg))(?:[ ;]|$)!;
6363         # serve text/* as text/plain
6364         if ($prevent_xss &&
6365             ($type =~ m!^text/[a-z]+\b(.*)$! ||
6366              ($type =~ m!^[a-z]+/[a-z]\+xml\b(.*)$! && -T $fd))) {
6367                 my $rest = $1;
6368                 $rest = defined $rest ? $rest : '';
6369                 $type = "text/plain$rest";
6370         }
6372         print $cgi->header(
6373                 -type => $type,
6374                 -expires => $expires,
6375                 -content_disposition =>
6376                         ($sandbox ? 'attachment' : 'inline')
6377                         . '; filename="' . $save_as . '"');
6378         local $/ = undef;
6379         binmode STDOUT, ':raw';
6380         print <$fd>;
6381         binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
6382         close $fd;
6385 sub git_blob {
6386         my $expires;
6388         if (!defined $hash) {
6389                 if (defined $file_name) {
6390                         my $base = $hash_base || git_get_head_hash($project);
6391                         $hash = git_get_hash_by_path($base, $file_name, "blob")
6392                                 or die_error(404, "Cannot find file");
6393                 } else {
6394                         die_error(400, "No file name defined");
6395                 }
6396         } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6397                 # blobs defined by non-textual hash id's can be cached
6398                 $expires = "+1d";
6399         }
6401         my $have_blame = gitweb_check_feature('blame');
6402         open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
6403                 or die_error(500, "Couldn't cat $file_name, $hash");
6404         my $mimetype = blob_mimetype($fd, $file_name);
6405         # use 'blob_plain' (aka 'raw') view for files that cannot be displayed
6406         if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {
6407                 close $fd;
6408                 return git_blob_plain($mimetype);
6409         }
6410         # we can have blame only for text/* mimetype
6411         $have_blame &&= ($mimetype =~ m!^text/!);
6413         my $highlight = gitweb_check_feature('highlight');
6414         my $syntax = guess_file_syntax($highlight, $mimetype, $file_name);
6415         $fd = run_highlighter($fd, $highlight, $syntax)
6416                 if $syntax;
6418         git_header_html(undef, $expires);
6419         my $formats_nav = '';
6420         if (defined $hash_base && (my %co = parse_commit($hash_base))) {
6421                 if (defined $file_name) {
6422                         if ($have_blame) {
6423                                 $formats_nav .=
6424                                         $cgi->a({-href => href(action=>"blame", -replay=>1)},
6425                                                 "blame") .
6426                                         " | ";
6427                         }
6428                         $formats_nav .=
6429                                 $cgi->a({-href => href(action=>"history", -replay=>1)},
6430                                         "history") .
6431                                 " | " .
6432                                 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
6433                                         "raw") .
6434                                 " | " .
6435                                 $cgi->a({-href => href(action=>"blob",
6436                                                        hash_base=>"HEAD", file_name=>$file_name)},
6437                                         "HEAD");
6438                 } else {
6439                         $formats_nav .=
6440                                 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
6441                                         "raw");
6442                 }
6443                 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
6444                 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
6445         } else {
6446                 print "<div class=\"page_nav\">\n" .
6447                       "<br/><br/></div>\n" .
6448                       "<div class=\"title\">".esc_html($hash)."</div>\n";
6449         }
6450         git_print_page_path($file_name, "blob", $hash_base);
6451         print "<div class=\"page_body\">\n";
6452         if ($mimetype =~ m!^image/!) {
6453                 print qq!<img type="!.esc_attr($mimetype).qq!"!;
6454                 if ($file_name) {
6455                         print qq! alt="!.esc_attr($file_name).qq!" title="!.esc_attr($file_name).qq!"!;
6456                 }
6457                 print qq! src="! .
6458                       href(action=>"blob_plain", hash=>$hash,
6459                            hash_base=>$hash_base, file_name=>$file_name) .
6460                       qq!" />\n!;
6461         } else {
6462                 my $nr;
6463                 while (my $line = <$fd>) {
6464                         chomp $line;
6465                         $nr++;
6466                         $line = untabify($line);
6467                         printf qq!<div class="pre"><a id="l%i" href="%s#l%i" class="linenr">%4i</a> %s</div>\n!,
6468                                $nr, esc_attr(href(-replay => 1)), $nr, $nr, $syntax ? $line : esc_html($line, -nbsp=>1);
6469                 }
6470         }
6471         close $fd
6472                 or print "Reading blob failed.\n";
6473         print "</div>";
6474         git_footer_html();
6477 sub git_tree {
6478         if (!defined $hash_base) {
6479                 $hash_base = "HEAD";
6480         }
6481         if (!defined $hash) {
6482                 if (defined $file_name) {
6483                         $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
6484                 } else {
6485                         $hash = $hash_base;
6486                 }
6487         }
6488         die_error(404, "No such tree") unless defined($hash);
6490         my $show_sizes = gitweb_check_feature('show-sizes');
6491         my $have_blame = gitweb_check_feature('blame');
6493         my @entries = ();
6494         {
6495                 local $/ = "\0";
6496                 open my $fd, "-|", git_cmd(), "ls-tree", '-z',
6497                         ($show_sizes ? '-l' : ()), @extra_options, $hash
6498                         or die_error(500, "Open git-ls-tree failed");
6499                 @entries = map { chomp; $_ } <$fd>;
6500                 close $fd
6501                         or die_error(404, "Reading tree failed");
6502         }
6504         my $refs = git_get_references();
6505         my $ref = format_ref_marker($refs, $hash_base);
6506         git_header_html();
6507         my $basedir = '';
6508         if (defined $hash_base && (my %co = parse_commit($hash_base))) {
6509                 my @views_nav = ();
6510                 if (defined $file_name) {
6511                         push @views_nav,
6512                                 $cgi->a({-href => href(action=>"history", -replay=>1)},
6513                                         "history"),
6514                                 $cgi->a({-href => href(action=>"tree",
6515                                                        hash_base=>"HEAD", file_name=>$file_name)},
6516                                         "HEAD"),
6517                 }
6518                 my $snapshot_links = format_snapshot_links($hash);
6519                 if (defined $snapshot_links) {
6520                         # FIXME: Should be available when we have no hash base as well.
6521                         push @views_nav, $snapshot_links;
6522                 }
6523                 git_print_page_nav('tree','', $hash_base, undef, undef,
6524                                    join(' | ', @views_nav));
6525                 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
6526         } else {
6527                 undef $hash_base;
6528                 print "<div class=\"page_nav\">\n";
6529                 print "<br/><br/></div>\n";
6530                 print "<div class=\"title\">".esc_html($hash)."</div>\n";
6531         }
6532         if (defined $file_name) {
6533                 $basedir = $file_name;
6534                 if ($basedir ne '' && substr($basedir, -1) ne '/') {
6535                         $basedir .= '/';
6536                 }
6537                 git_print_page_path($file_name, 'tree', $hash_base);
6538         }
6539         print "<div class=\"page_body\">\n";
6540         print "<table class=\"tree\">\n";
6541         my $alternate = 1;
6542         # '..' (top directory) link if possible
6543         if (defined $hash_base &&
6544             defined $file_name && $file_name =~ m![^/]+$!) {
6545                 if ($alternate) {
6546                         print "<tr class=\"dark\">\n";
6547                 } else {
6548                         print "<tr class=\"light\">\n";
6549                 }
6550                 $alternate ^= 1;
6552                 my $up = $file_name;
6553                 $up =~ s!/?[^/]+$!!;
6554                 undef $up unless $up;
6555                 # based on git_print_tree_entry
6556                 print '<td class="mode">' . mode_str('040000') . "</td>\n";
6557                 print '<td class="size">&nbsp;</td>'."\n" if $show_sizes;
6558                 print '<td class="list">';
6559                 print $cgi->a({-href => href(action=>"tree",
6560                                              hash_base=>$hash_base,
6561                                              file_name=>$up)},
6562                               "..");
6563                 print "</td>\n";
6564                 print "<td class=\"link\"></td>\n";
6566                 print "</tr>\n";
6567         }
6568         foreach my $line (@entries) {
6569                 my %t = parse_ls_tree_line($line, -z => 1, -l => $show_sizes);
6571                 if ($alternate) {
6572                         print "<tr class=\"dark\">\n";
6573                 } else {
6574                         print "<tr class=\"light\">\n";
6575                 }
6576                 $alternate ^= 1;
6578                 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
6580                 print "</tr>\n";
6581         }
6582         print "</table>\n" .
6583               "</div>";
6584         git_footer_html();
6587 sub snapshot_name {
6588         my ($project, $hash) = @_;
6590         # path/to/project.git  -> project
6591         # path/to/project/.git -> project
6592         my $name = to_utf8($project);
6593         $name =~ s,([^/])/*\.git$,$1,;
6594         $name = basename($name);
6595         # sanitize name
6596         $name =~ s/[[:cntrl:]]/?/g;
6598         my $ver = $hash;
6599         if ($hash =~ /^[0-9a-fA-F]+$/) {
6600                 # shorten SHA-1 hash
6601                 my $full_hash = git_get_full_hash($project, $hash);
6602                 if ($full_hash =~ /^$hash/ && length($hash) > 7) {
6603                         $ver = git_get_short_hash($project, $hash);
6604                 }
6605         } elsif ($hash =~ m!^refs/tags/(.*)$!) {
6606                 # tags don't need shortened SHA-1 hash
6607                 $ver = $1;
6608         } else {
6609                 # branches and other need shortened SHA-1 hash
6610                 if ($hash =~ m!^refs/(?:heads|remotes)/(.*)$!) {
6611                         $ver = $1;
6612                 }
6613                 $ver .= '-' . git_get_short_hash($project, $hash);
6614         }
6615         # in case of hierarchical branch names
6616         $ver =~ s!/!.!g;
6618         # name = project-version_string
6619         $name = "$name-$ver";
6621         return wantarray ? ($name, $name) : $name;
6624 sub git_snapshot {
6625         my $format = $input_params{'snapshot_format'};
6626         if (!@snapshot_fmts) {
6627                 die_error(403, "Snapshots not allowed");
6628         }
6629         # default to first supported snapshot format
6630         $format ||= $snapshot_fmts[0];
6631         if ($format !~ m/^[a-z0-9]+$/) {
6632                 die_error(400, "Invalid snapshot format parameter");
6633         } elsif (!exists($known_snapshot_formats{$format})) {
6634                 die_error(400, "Unknown snapshot format");
6635         } elsif ($known_snapshot_formats{$format}{'disabled'}) {
6636                 die_error(403, "Snapshot format not allowed");
6637         } elsif (!grep($_ eq $format, @snapshot_fmts)) {
6638                 die_error(403, "Unsupported snapshot format");
6639         }
6641         my $type = git_get_type("$hash^{}");
6642         if (!$type) {
6643                 die_error(404, 'Object does not exist');
6644         }  elsif ($type eq 'blob') {
6645                 die_error(400, 'Object is not a tree-ish');
6646         }
6648         my ($name, $prefix) = snapshot_name($project, $hash);
6649         my $filename = "$name$known_snapshot_formats{$format}{'suffix'}";
6650         my $cmd = quote_command(
6651                 git_cmd(), 'archive',
6652                 "--format=$known_snapshot_formats{$format}{'format'}",
6653                 "--prefix=$prefix/", $hash);
6654         if (exists $known_snapshot_formats{$format}{'compressor'}) {
6655                 $cmd .= ' | ' . quote_command(@{$known_snapshot_formats{$format}{'compressor'}});
6656         }
6658         $filename =~ s/(["\\])/\\$1/g;
6659         print $cgi->header(
6660                 -type => $known_snapshot_formats{$format}{'type'},
6661                 -content_disposition => 'inline; filename="' . $filename . '"',
6662                 -status => '200 OK');
6664         open my $fd, "-|", $cmd
6665                 or die_error(500, "Execute git-archive failed");
6666         binmode STDOUT, ':raw';
6667         print <$fd>;
6668         binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
6669         close $fd;
6672 sub git_log_generic {
6673         my ($fmt_name, $body_subr, $base, $parent, $file_name, $file_hash) = @_;
6675         my $head = git_get_head_hash($project);
6676         if (!defined $base) {
6677                 $base = $head;
6678         }
6679         if (!defined $page) {
6680                 $page = 0;
6681         }
6682         my $refs = git_get_references();
6684         my $commit_hash = $base;
6685         if (defined $parent) {
6686                 $commit_hash = "$parent..$base";
6687         }
6688         my @commitlist =
6689                 parse_commits($commit_hash, 101, (100 * $page),
6690                               defined $file_name ? ($file_name, "--full-history") : ());
6692         my $ftype;
6693         if (!defined $file_hash && defined $file_name) {
6694                 # some commits could have deleted file in question,
6695                 # and not have it in tree, but one of them has to have it
6696                 for (my $i = 0; $i < @commitlist; $i++) {
6697                         $file_hash = git_get_hash_by_path($commitlist[$i]{'id'}, $file_name);
6698                         last if defined $file_hash;
6699                 }
6700         }
6701         if (defined $file_hash) {
6702                 $ftype = git_get_type($file_hash);
6703         }
6704         if (defined $file_name && !defined $ftype) {
6705                 die_error(500, "Unknown type of object");
6706         }
6707         my %co;
6708         if (defined $file_name) {
6709                 %co = parse_commit($base)
6710                         or die_error(404, "Unknown commit object");
6711         }
6714         my $paging_nav = format_paging_nav($fmt_name, $page, $#commitlist >= 100);
6715         my $next_link = '';
6716         if ($#commitlist >= 100) {
6717                 $next_link =
6718                         $cgi->a({-href => href(-replay=>1, page=>$page+1),
6719                                  -accesskey => "n", -title => "Alt-n"}, "next");
6720         }
6721         my $patch_max = gitweb_get_feature('patches');
6722         if ($patch_max && !defined $file_name) {
6723                 if ($patch_max < 0 || @commitlist <= $patch_max) {
6724                         $paging_nav .= " &sdot; " .
6725                                 $cgi->a({-href => href(action=>"patches", -replay=>1)},
6726                                         "patches");
6727                 }
6728         }
6730         git_header_html();
6731         git_print_page_nav($fmt_name,'', $hash,$hash,$hash, $paging_nav);
6732         if (defined $file_name) {
6733                 git_print_header_div('commit', esc_html($co{'title'}), $base);
6734         } else {
6735                 git_print_header_div('summary', $project)
6736         }
6737         git_print_page_path($file_name, $ftype, $hash_base)
6738                 if (defined $file_name);
6740         $body_subr->(\@commitlist, 0, 99, $refs, $next_link,
6741                      $file_name, $file_hash, $ftype);
6743         git_footer_html();
6746 sub git_log {
6747         git_log_generic('log', \&git_log_body,
6748                         $hash, $hash_parent);
6751 sub git_commit {
6752         $hash ||= $hash_base || "HEAD";
6753         my %co = parse_commit($hash)
6754             or die_error(404, "Unknown commit object");
6756         my $parent  = $co{'parent'};
6757         my $parents = $co{'parents'}; # listref
6759         # we need to prepare $formats_nav before any parameter munging
6760         my $formats_nav;
6761         if (!defined $parent) {
6762                 # --root commitdiff
6763                 $formats_nav .= '(initial)';
6764         } elsif (@$parents == 1) {
6765                 # single parent commit
6766                 $formats_nav .=
6767                         '(parent: ' .
6768                         $cgi->a({-href => href(action=>"commit",
6769                                                hash=>$parent)},
6770                                 esc_html(substr($parent, 0, 7))) .
6771                         ')';
6772         } else {
6773                 # merge commit
6774                 $formats_nav .=
6775                         '(merge: ' .
6776                         join(' ', map {
6777                                 $cgi->a({-href => href(action=>"commit",
6778                                                        hash=>$_)},
6779                                         esc_html(substr($_, 0, 7)));
6780                         } @$parents ) .
6781                         ')';
6782         }
6783         if (gitweb_check_feature('patches') && @$parents <= 1) {
6784                 $formats_nav .= " | " .
6785                         $cgi->a({-href => href(action=>"patch", -replay=>1)},
6786                                 "patch");
6787         }
6789         if (!defined $parent) {
6790                 $parent = "--root";
6791         }
6792         my @difftree;
6793         open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
6794                 @diff_opts,
6795                 (@$parents <= 1 ? $parent : '-c'),
6796                 $hash, "--"
6797                 or die_error(500, "Open git-diff-tree failed");
6798         @difftree = map { chomp; $_ } <$fd>;
6799         close $fd or die_error(404, "Reading git-diff-tree failed");
6801         # non-textual hash id's can be cached
6802         my $expires;
6803         if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6804                 $expires = "+1d";
6805         }
6806         my $refs = git_get_references();
6807         my $ref = format_ref_marker($refs, $co{'id'});
6809         git_header_html(undef, $expires);
6810         git_print_page_nav('commit', '',
6811                            $hash, $co{'tree'}, $hash,
6812                            $formats_nav);
6814         if (defined $co{'parent'}) {
6815                 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
6816         } else {
6817                 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
6818         }
6819         print "<div class=\"title_text\">\n" .
6820               "<table class=\"object_header\">\n";
6821         git_print_authorship_rows(\%co);
6822         print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
6823         print "<tr>" .
6824               "<td>tree</td>" .
6825               "<td class=\"sha1\">" .
6826               $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
6827                        class => "list"}, $co{'tree'}) .
6828               "</td>" .
6829               "<td class=\"link\">" .
6830               $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
6831                       "tree");
6832         my $snapshot_links = format_snapshot_links($hash);
6833         if (defined $snapshot_links) {
6834                 print " | " . $snapshot_links;
6835         }
6836         print "</td>" .
6837               "</tr>\n";
6839         foreach my $par (@$parents) {
6840                 print "<tr>" .
6841                       "<td>parent</td>" .
6842                       "<td class=\"sha1\">" .
6843                       $cgi->a({-href => href(action=>"commit", hash=>$par),
6844                                class => "list"}, $par) .
6845                       "</td>" .
6846                       "<td class=\"link\">" .
6847                       $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
6848                       " | " .
6849                       $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
6850                       "</td>" .
6851                       "</tr>\n";
6852         }
6853         print "</table>".
6854               "</div>\n";
6856         print "<div class=\"page_body\">\n";
6857         git_print_log($co{'comment'});
6858         print "</div>\n";
6860         git_difftree_body(\@difftree, $hash, @$parents);
6862         git_footer_html();
6865 sub git_object {
6866         # object is defined by:
6867         # - hash or hash_base alone
6868         # - hash_base and file_name
6869         my $type;
6871         # - hash or hash_base alone
6872         if ($hash || ($hash_base && !defined $file_name)) {
6873                 my $object_id = $hash || $hash_base;
6875                 open my $fd, "-|", quote_command(
6876                         git_cmd(), 'cat-file', '-t', $object_id) . ' 2> /dev/null'
6877                         or die_error(404, "Object does not exist");
6878                 $type = <$fd>;
6879                 chomp $type;
6880                 close $fd
6881                         or die_error(404, "Object does not exist");
6883         # - hash_base and file_name
6884         } elsif ($hash_base && defined $file_name) {
6885                 $file_name =~ s,/+$,,;
6887                 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
6888                         or die_error(404, "Base object does not exist");
6890                 # here errors should not hapen
6891                 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
6892                         or die_error(500, "Open git-ls-tree failed");
6893                 my $line = <$fd>;
6894                 close $fd;
6896                 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa  panic.c'
6897                 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
6898                         die_error(404, "File or directory for given base does not exist");
6899                 }
6900                 $type = $2;
6901                 $hash = $3;
6902         } else {
6903                 die_error(400, "Not enough information to find object");
6904         }
6906         print $cgi->redirect(-uri => href(action=>$type, -full=>1,
6907                                           hash=>$hash, hash_base=>$hash_base,
6908                                           file_name=>$file_name),
6909                              -status => '302 Found');
6912 sub git_blobdiff {
6913         my $format = shift || 'html';
6915         my $fd;
6916         my @difftree;
6917         my %diffinfo;
6918         my $expires;
6920         # preparing $fd and %diffinfo for git_patchset_body
6921         # new style URI
6922         if (defined $hash_base && defined $hash_parent_base) {
6923                 if (defined $file_name) {
6924                         # read raw output
6925                         open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6926                                 $hash_parent_base, $hash_base,
6927                                 "--", (defined $file_parent ? $file_parent : ()), $file_name
6928                                 or die_error(500, "Open git-diff-tree failed");
6929                         @difftree = map { chomp; $_ } <$fd>;
6930                         close $fd
6931                                 or die_error(404, "Reading git-diff-tree failed");
6932                         @difftree
6933                                 or die_error(404, "Blob diff not found");
6935                 } elsif (defined $hash &&
6936                          $hash =~ /[0-9a-fA-F]{40}/) {
6937                         # try to find filename from $hash
6939                         # read filtered raw output
6940                         open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6941                                 $hash_parent_base, $hash_base, "--"
6942                                 or die_error(500, "Open git-diff-tree failed");
6943                         @difftree =
6944                                 # ':100644 100644 03b21826... 3b93d5e7... M     ls-files.c'
6945                                 # $hash == to_id
6946                                 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
6947                                 map { chomp; $_ } <$fd>;
6948                         close $fd
6949                                 or die_error(404, "Reading git-diff-tree failed");
6950                         @difftree
6951                                 or die_error(404, "Blob diff not found");
6953                 } else {
6954                         die_error(400, "Missing one of the blob diff parameters");
6955                 }
6957                 if (@difftree > 1) {
6958                         die_error(400, "Ambiguous blob diff specification");
6959                 }
6961                 %diffinfo = parse_difftree_raw_line($difftree[0]);
6962                 $file_parent ||= $diffinfo{'from_file'} || $file_name;
6963                 $file_name   ||= $diffinfo{'to_file'};
6965                 $hash_parent ||= $diffinfo{'from_id'};
6966                 $hash        ||= $diffinfo{'to_id'};
6968                 # non-textual hash id's can be cached
6969                 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
6970                     $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
6971                         $expires = '+1d';
6972                 }
6974                 # open patch output
6975                 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6976                         '-p', ($format eq 'html' ? "--full-index" : ()),
6977                         $hash_parent_base, $hash_base,
6978                         "--", (defined $file_parent ? $file_parent : ()), $file_name
6979                         or die_error(500, "Open git-diff-tree failed");
6980         }
6982         # old/legacy style URI -- not generated anymore since 1.4.3.
6983         if (!%diffinfo) {
6984                 die_error('404 Not Found', "Missing one of the blob diff parameters")
6985         }
6987         # header
6988         if ($format eq 'html') {
6989                 my $formats_nav =
6990                         $cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},
6991                                 "raw");
6992                 git_header_html(undef, $expires);
6993                 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
6994                         git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
6995                         git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
6996                 } else {
6997                         print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
6998                         print "<div class=\"title\">".esc_html("$hash vs $hash_parent")."</div>\n";
6999                 }
7000                 if (defined $file_name) {
7001                         git_print_page_path($file_name, "blob", $hash_base);
7002                 } else {
7003                         print "<div class=\"page_path\"></div>\n";
7004                 }
7006         } elsif ($format eq 'plain') {
7007                 print $cgi->header(
7008                         -type => 'text/plain',
7009                         -charset => 'utf-8',
7010                         -expires => $expires,
7011                         -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
7013                 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
7015         } else {
7016                 die_error(400, "Unknown blobdiff format");
7017         }
7019         # patch
7020         if ($format eq 'html') {
7021                 print "<div class=\"page_body\">\n";
7023                 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
7024                 close $fd;
7026                 print "</div>\n"; # class="page_body"
7027                 git_footer_html();
7029         } else {
7030                 while (my $line = <$fd>) {
7031                         $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
7032                         $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
7034                         print $line;
7036                         last if $line =~ m!^\+\+\+!;
7037                 }
7038                 local $/ = undef;
7039                 print <$fd>;
7040                 close $fd;
7041         }
7044 sub git_blobdiff_plain {
7045         git_blobdiff('plain');
7048 sub git_commitdiff {
7049         my %params = @_;
7050         my $format = $params{-format} || 'html';
7052         my ($patch_max) = gitweb_get_feature('patches');
7053         if ($format eq 'patch') {
7054                 die_error(403, "Patch view not allowed") unless $patch_max;
7055         }
7057         $hash ||= $hash_base || "HEAD";
7058         my %co = parse_commit($hash)
7059             or die_error(404, "Unknown commit object");
7061         # choose format for commitdiff for merge
7062         if (! defined $hash_parent && @{$co{'parents'}} > 1) {
7063                 $hash_parent = '--cc';
7064         }
7065         # we need to prepare $formats_nav before almost any parameter munging
7066         my $formats_nav;
7067         if ($format eq 'html') {
7068                 $formats_nav =
7069                         $cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
7070                                 "raw");
7071                 if ($patch_max && @{$co{'parents'}} <= 1) {
7072                         $formats_nav .= " | " .
7073                                 $cgi->a({-href => href(action=>"patch", -replay=>1)},
7074                                         "patch");
7075                 }
7077                 if (defined $hash_parent &&
7078                     $hash_parent ne '-c' && $hash_parent ne '--cc') {
7079                         # commitdiff with two commits given
7080                         my $hash_parent_short = $hash_parent;
7081                         if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
7082                                 $hash_parent_short = substr($hash_parent, 0, 7);
7083                         }
7084                         $formats_nav .=
7085                                 ' (from';
7086                         for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
7087                                 if ($co{'parents'}[$i] eq $hash_parent) {
7088                                         $formats_nav .= ' parent ' . ($i+1);
7089                                         last;
7090                                 }
7091                         }
7092                         $formats_nav .= ': ' .
7093                                 $cgi->a({-href => href(action=>"commitdiff",
7094                                                        hash=>$hash_parent)},
7095                                         esc_html($hash_parent_short)) .
7096                                 ')';
7097                 } elsif (!$co{'parent'}) {
7098                         # --root commitdiff
7099                         $formats_nav .= ' (initial)';
7100                 } elsif (scalar @{$co{'parents'}} == 1) {
7101                         # single parent commit
7102                         $formats_nav .=
7103                                 ' (parent: ' .
7104                                 $cgi->a({-href => href(action=>"commitdiff",
7105                                                        hash=>$co{'parent'})},
7106                                         esc_html(substr($co{'parent'}, 0, 7))) .
7107                                 ')';
7108                 } else {
7109                         # merge commit
7110                         if ($hash_parent eq '--cc') {
7111                                 $formats_nav .= ' | ' .
7112                                         $cgi->a({-href => href(action=>"commitdiff",
7113                                                                hash=>$hash, hash_parent=>'-c')},
7114                                                 'combined');
7115                         } else { # $hash_parent eq '-c'
7116                                 $formats_nav .= ' | ' .
7117                                         $cgi->a({-href => href(action=>"commitdiff",
7118                                                                hash=>$hash, hash_parent=>'--cc')},
7119                                                 'compact');
7120                         }
7121                         $formats_nav .=
7122                                 ' (merge: ' .
7123                                 join(' ', map {
7124                                         $cgi->a({-href => href(action=>"commitdiff",
7125                                                                hash=>$_)},
7126                                                 esc_html(substr($_, 0, 7)));
7127                                 } @{$co{'parents'}} ) .
7128                                 ')';
7129                 }
7130         }
7132         my $hash_parent_param = $hash_parent;
7133         if (!defined $hash_parent_param) {
7134                 # --cc for multiple parents, --root for parentless
7135                 $hash_parent_param =
7136                         @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
7137         }
7139         # read commitdiff
7140         my $fd;
7141         my @difftree;
7142         if ($format eq 'html') {
7143                 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
7144                         "--no-commit-id", "--patch-with-raw", "--full-index",
7145                         $hash_parent_param, $hash, "--"
7146                         or die_error(500, "Open git-diff-tree failed");
7148                 while (my $line = <$fd>) {
7149                         chomp $line;
7150                         # empty line ends raw part of diff-tree output
7151                         last unless $line;
7152                         push @difftree, scalar parse_difftree_raw_line($line);
7153                 }
7155         } elsif ($format eq 'plain') {
7156                 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
7157                         '-p', $hash_parent_param, $hash, "--"
7158                         or die_error(500, "Open git-diff-tree failed");
7159         } elsif ($format eq 'patch') {
7160                 # For commit ranges, we limit the output to the number of
7161                 # patches specified in the 'patches' feature.
7162                 # For single commits, we limit the output to a single patch,
7163                 # diverging from the git-format-patch default.
7164                 my @commit_spec = ();
7165                 if ($hash_parent) {
7166                         if ($patch_max > 0) {
7167                                 push @commit_spec, "-$patch_max";
7168                         }
7169                         push @commit_spec, '-n', "$hash_parent..$hash";
7170                 } else {
7171                         if ($params{-single}) {
7172                                 push @commit_spec, '-1';
7173                         } else {
7174                                 if ($patch_max > 0) {
7175                                         push @commit_spec, "-$patch_max";
7176                                 }
7177                                 push @commit_spec, "-n";
7178                         }
7179                         push @commit_spec, '--root', $hash;
7180                 }
7181                 open $fd, "-|", git_cmd(), "format-patch", @diff_opts,
7182                         '--encoding=utf8', '--stdout', @commit_spec
7183                         or die_error(500, "Open git-format-patch failed");
7184         } else {
7185                 die_error(400, "Unknown commitdiff format");
7186         }
7188         # non-textual hash id's can be cached
7189         my $expires;
7190         if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
7191                 $expires = "+1d";
7192         }
7194         # write commit message
7195         if ($format eq 'html') {
7196                 my $refs = git_get_references();
7197                 my $ref = format_ref_marker($refs, $co{'id'});
7199                 git_header_html(undef, $expires);
7200                 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
7201                 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
7202                 print "<div class=\"title_text\">\n" .
7203                       "<table class=\"object_header\">\n";
7204                 git_print_authorship_rows(\%co);
7205                 print "</table>".
7206                       "</div>\n";
7207                 print "<div class=\"page_body\">\n";
7208                 if (@{$co{'comment'}} > 1) {
7209                         print "<div class=\"log\">\n";
7210                         git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
7211                         print "</div>\n"; # class="log"
7212                 }
7214         } elsif ($format eq 'plain') {
7215                 my $refs = git_get_references("tags");
7216                 my $tagname = git_get_rev_name_tags($hash);
7217                 my $filename = basename($project) . "-$hash.patch";
7219                 print $cgi->header(
7220                         -type => 'text/plain',
7221                         -charset => 'utf-8',
7222                         -expires => $expires,
7223                         -content_disposition => 'inline; filename="' . "$filename" . '"');
7224                 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
7225                 print "From: " . to_utf8($co{'author'}) . "\n";
7226                 print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
7227                 print "Subject: " . to_utf8($co{'title'}) . "\n";
7229                 print "X-Git-Tag: $tagname\n" if $tagname;
7230                 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
7232                 foreach my $line (@{$co{'comment'}}) {
7233                         print to_utf8($line) . "\n";
7234                 }
7235                 print "---\n\n";
7236         } elsif ($format eq 'patch') {
7237                 my $filename = basename($project) . "-$hash.patch";
7239                 print $cgi->header(
7240                         -type => 'text/plain',
7241                         -charset => 'utf-8',
7242                         -expires => $expires,
7243                         -content_disposition => 'inline; filename="' . "$filename" . '"');
7244         }
7246         # write patch
7247         if ($format eq 'html') {
7248                 my $use_parents = !defined $hash_parent ||
7249                         $hash_parent eq '-c' || $hash_parent eq '--cc';
7250                 git_difftree_body(\@difftree, $hash,
7251                                   $use_parents ? @{$co{'parents'}} : $hash_parent);
7252                 print "<br/>\n";
7254                 git_patchset_body($fd, \@difftree, $hash,
7255                                   $use_parents ? @{$co{'parents'}} : $hash_parent);
7256                 close $fd;
7257                 print "</div>\n"; # class="page_body"
7258                 git_footer_html();
7260         } elsif ($format eq 'plain') {
7261                 local $/ = undef;
7262                 print <$fd>;
7263                 close $fd
7264                         or print "Reading git-diff-tree failed\n";
7265         } elsif ($format eq 'patch') {
7266                 local $/ = undef;
7267                 print <$fd>;
7268                 close $fd
7269                         or print "Reading git-format-patch failed\n";
7270         }
7273 sub git_commitdiff_plain {
7274         git_commitdiff(-format => 'plain');
7277 # format-patch-style patches
7278 sub git_patch {
7279         git_commitdiff(-format => 'patch', -single => 1);
7282 sub git_patches {
7283         git_commitdiff(-format => 'patch');
7286 sub git_history {
7287         git_log_generic('history', \&git_history_body,
7288                         $hash_base, $hash_parent_base,
7289                         $file_name, $hash);
7292 sub git_search {
7293         $searchtype ||= 'commit';
7295         # check if appropriate features are enabled
7296         gitweb_check_feature('search')
7297                 or die_error(403, "Search is disabled");
7298         if ($searchtype eq 'pickaxe') {
7299                 # pickaxe may take all resources of your box and run for several minutes
7300                 # with every query - so decide by yourself how public you make this feature
7301                 gitweb_check_feature('pickaxe')
7302                         or die_error(403, "Pickaxe search is disabled");
7303         }
7304         if ($searchtype eq 'grep') {
7305                 # grep search might be potentially CPU-intensive, too
7306                 gitweb_check_feature('grep')
7307                         or die_error(403, "Grep search is disabled");
7308         }
7310         if (!defined $searchtext) {
7311                 die_error(400, "Text field is empty");
7312         }
7313         if (!defined $hash) {
7314                 $hash = git_get_head_hash($project);
7315         }
7316         my %co = parse_commit($hash);
7317         if (!%co) {
7318                 die_error(404, "Unknown commit object");
7319         }
7320         if (!defined $page) {
7321                 $page = 0;
7322         }
7324         if ($searchtype eq 'commit' ||
7325             $searchtype eq 'author' ||
7326             $searchtype eq 'committer') {
7327                 git_search_message(%co);
7328         } elsif ($searchtype eq 'pickaxe') {
7329                 git_search_changes(%co);
7330         } elsif ($searchtype eq 'grep') {
7331                 git_search_files(%co);
7332         } else {
7333                 die_error(400, "Unknown search type");
7334         }
7337 sub git_search_help {
7338         git_header_html();
7339         git_print_page_nav('','', $hash,$hash,$hash);
7340         print <<EOT;
7341 <p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without
7342 regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,
7343 the pattern entered is recognized as the POSIX extended
7344 <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case
7345 insensitive).</p>
7346 <dl>
7347 <dt><b>commit</b></dt>
7348 <dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
7349 EOT
7350         my $have_grep = gitweb_check_feature('grep');
7351         if ($have_grep) {
7352                 print <<EOT;
7353 <dt><b>grep</b></dt>
7354 <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
7355     a different one) are searched for the given pattern. On large trees, this search can take
7356 a while and put some strain on the server, so please use it with some consideration. Note that
7357 due to git-grep peculiarity, currently if regexp mode is turned off, the matches are
7358 case-sensitive.</dd>
7359 EOT
7360         }
7361         print <<EOT;
7362 <dt><b>author</b></dt>
7363 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>
7364 <dt><b>committer</b></dt>
7365 <dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>
7366 EOT
7367         my $have_pickaxe = gitweb_check_feature('pickaxe');
7368         if ($have_pickaxe) {
7369                 print <<EOT;
7370 <dt><b>pickaxe</b></dt>
7371 <dd>All commits that caused the string to appear or disappear from any file (changes that
7372 added, removed or "modified" the string) will be listed. This search can take a while and
7373 takes a lot of strain on the server, so please use it wisely. Note that since you may be
7374 interested even in changes just changing the case as well, this search is case sensitive.</dd>
7375 EOT
7376         }
7377         print "</dl>\n";
7378         git_footer_html();
7381 sub git_shortlog {
7382         git_log_generic('shortlog', \&git_shortlog_body,
7383                         $hash, $hash_parent);
7386 ## ......................................................................
7387 ## feeds (RSS, Atom; OPML)
7389 sub git_feed {
7390         my $format = shift || 'atom';
7391         my $have_blame = gitweb_check_feature('blame');
7393         # Atom: http://www.atomenabled.org/developers/syndication/
7394         # RSS:  http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
7395         if ($format ne 'rss' && $format ne 'atom') {
7396                 die_error(400, "Unknown web feed format");
7397         }
7399         # log/feed of current (HEAD) branch, log of given branch, history of file/directory
7400         my $head = $hash || 'HEAD';
7401         my @commitlist = parse_commits($head, 150, 0, $file_name);
7403         my %latest_commit;
7404         my %latest_date;
7405         my $content_type = "application/$format+xml";
7406         if (defined $cgi->http('HTTP_ACCEPT') &&
7407                  $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
7408                 # browser (feed reader) prefers text/xml
7409                 $content_type = 'text/xml';
7410         }
7411         if (defined($commitlist[0])) {
7412                 %latest_commit = %{$commitlist[0]};
7413                 my $latest_epoch = $latest_commit{'committer_epoch'};
7414                 %latest_date   = parse_date($latest_epoch, $latest_commit{'comitter_tz'});
7415                 my $if_modified = $cgi->http('IF_MODIFIED_SINCE');
7416                 if (defined $if_modified) {
7417                         my $since;
7418                         if (eval { require HTTP::Date; 1; }) {
7419                                 $since = HTTP::Date::str2time($if_modified);
7420                         } elsif (eval { require Time::ParseDate; 1; }) {
7421                                 $since = Time::ParseDate::parsedate($if_modified, GMT => 1);
7422                         }
7423                         if (defined $since && $latest_epoch <= $since) {
7424                                 print $cgi->header(
7425                                         -type => $content_type,
7426                                         -charset => 'utf-8',
7427                                         -last_modified => $latest_date{'rfc2822'},
7428                                         -status => '304 Not Modified');
7429                                 return;
7430                         }
7431                 }
7432                 print $cgi->header(
7433                         -type => $content_type,
7434                         -charset => 'utf-8',
7435                         -last_modified => $latest_date{'rfc2822'});
7436         } else {
7437                 print $cgi->header(
7438                         -type => $content_type,
7439                         -charset => 'utf-8');
7440         }
7442         # Optimization: skip generating the body if client asks only
7443         # for Last-Modified date.
7444         return if ($cgi->request_method() eq 'HEAD');
7446         # header variables
7447         my $title = "$site_name - $project/$action";
7448         my $feed_type = 'log';
7449         if (defined $hash) {
7450                 $title .= " - '$hash'";
7451                 $feed_type = 'branch log';
7452                 if (defined $file_name) {
7453                         $title .= " :: $file_name";
7454                         $feed_type = 'history';
7455                 }
7456         } elsif (defined $file_name) {
7457                 $title .= " - $file_name";
7458                 $feed_type = 'history';
7459         }
7460         $title .= " $feed_type";
7461         my $descr = git_get_project_description($project);
7462         if (defined $descr) {
7463                 $descr = esc_html($descr);
7464         } else {
7465                 $descr = "$project " .
7466                          ($format eq 'rss' ? 'RSS' : 'Atom') .
7467                          " feed";
7468         }
7469         my $owner = git_get_project_owner($project);
7470         $owner = esc_html($owner);
7472         #header
7473         my $alt_url;
7474         if (defined $file_name) {
7475                 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
7476         } elsif (defined $hash) {
7477                 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
7478         } else {
7479                 $alt_url = href(-full=>1, action=>"summary");
7480         }
7481         print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
7482         if ($format eq 'rss') {
7483                 print <<XML;
7484 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
7485 <channel>
7486 XML
7487                 print "<title>$title</title>\n" .
7488                       "<link>$alt_url</link>\n" .
7489                       "<description>$descr</description>\n" .
7490                       "<language>en</language>\n" .
7491                       # project owner is responsible for 'editorial' content
7492                       "<managingEditor>$owner</managingEditor>\n";
7493                 if (defined $logo || defined $favicon) {
7494                         # prefer the logo to the favicon, since RSS
7495                         # doesn't allow both
7496                         my $img = esc_url($logo || $favicon);
7497                         print "<image>\n" .
7498                               "<url>$img</url>\n" .
7499                               "<title>$title</title>\n" .
7500                               "<link>$alt_url</link>\n" .
7501                               "</image>\n";
7502                 }
7503                 if (%latest_date) {
7504                         print "<pubDate>$latest_date{'rfc2822'}</pubDate>\n";
7505                         print "<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";
7506                 }
7507                 print "<generator>gitweb v.$version/$git_version</generator>\n";
7508         } elsif ($format eq 'atom') {
7509                 print <<XML;
7510 <feed xmlns="http://www.w3.org/2005/Atom">
7511 XML
7512                 print "<title>$title</title>\n" .
7513                       "<subtitle>$descr</subtitle>\n" .
7514                       '<link rel="alternate" type="text/html" href="' .
7515                       $alt_url . '" />' . "\n" .
7516                       '<link rel="self" type="' . $content_type . '" href="' .
7517                       $cgi->self_url() . '" />' . "\n" .
7518                       "<id>" . href(-full=>1) . "</id>\n" .
7519                       # use project owner for feed author
7520                       "<author><name>$owner</name></author>\n";
7521                 if (defined $favicon) {
7522                         print "<icon>" . esc_url($favicon) . "</icon>\n";
7523                 }
7524                 if (defined $logo) {
7525                         # not twice as wide as tall: 72 x 27 pixels
7526                         print "<logo>" . esc_url($logo) . "</logo>\n";
7527                 }
7528                 if (! %latest_date) {
7529                         # dummy date to keep the feed valid until commits trickle in:
7530                         print "<updated>1970-01-01T00:00:00Z</updated>\n";
7531                 } else {
7532                         print "<updated>$latest_date{'iso-8601'}</updated>\n";
7533                 }
7534                 print "<generator version='$version/$git_version'>gitweb</generator>\n";
7535         }
7537         # contents
7538         for (my $i = 0; $i <= $#commitlist; $i++) {
7539                 my %co = %{$commitlist[$i]};
7540                 my $commit = $co{'id'};
7541                 # we read 150, we always show 30 and the ones more recent than 48 hours
7542                 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
7543                         last;
7544                 }
7545                 my %cd = parse_date($co{'author_epoch'}, $co{'author_tz'});
7547                 # get list of changed files
7548                 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
7549                         $co{'parent'} || "--root",
7550                         $co{'id'}, "--", (defined $file_name ? $file_name : ())
7551                         or next;
7552                 my @difftree = map { chomp; $_ } <$fd>;
7553                 close $fd
7554                         or next;
7556                 # print element (entry, item)
7557                 my $co_url = href(-full=>1, action=>"commitdiff", hash=>$commit);
7558                 if ($format eq 'rss') {
7559                         print "<item>\n" .
7560                               "<title>" . esc_html($co{'title'}) . "</title>\n" .
7561                               "<author>" . esc_html($co{'author'}) . "</author>\n" .
7562                               "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
7563                               "<guid isPermaLink=\"true\">$co_url</guid>\n" .
7564                               "<link>$co_url</link>\n" .
7565                               "<description>" . esc_html($co{'title'}) . "</description>\n" .
7566                               "<content:encoded>" .
7567                               "<![CDATA[\n";
7568                 } elsif ($format eq 'atom') {
7569                         print "<entry>\n" .
7570                               "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
7571                               "<updated>$cd{'iso-8601'}</updated>\n" .
7572                               "<author>\n" .
7573                               "  <name>" . esc_html($co{'author_name'}) . "</name>\n";
7574                         if ($co{'author_email'}) {
7575                                 print "  <email>" . esc_html($co{'author_email'}) . "</email>\n";
7576                         }
7577                         print "</author>\n" .
7578                               # use committer for contributor
7579                               "<contributor>\n" .
7580                               "  <name>" . esc_html($co{'committer_name'}) . "</name>\n";
7581                         if ($co{'committer_email'}) {
7582                                 print "  <email>" . esc_html($co{'committer_email'}) . "</email>\n";
7583                         }
7584                         print "</contributor>\n" .
7585                               "<published>$cd{'iso-8601'}</published>\n" .
7586                               "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
7587                               "<id>$co_url</id>\n" .
7588                               "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
7589                               "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
7590                 }
7591                 my $comment = $co{'comment'};
7592                 print "<pre>\n";
7593                 foreach my $line (@$comment) {
7594                         $line = esc_html($line);
7595                         print "$line\n";
7596                 }
7597                 print "</pre><ul>\n";
7598                 foreach my $difftree_line (@difftree) {
7599                         my %difftree = parse_difftree_raw_line($difftree_line);
7600                         next if !$difftree{'from_id'};
7602                         my $file = $difftree{'file'} || $difftree{'to_file'};
7604                         print "<li>" .
7605                               "[" .
7606                               $cgi->a({-href => href(-full=>1, action=>"blobdiff",
7607                                                      hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
7608                                                      hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
7609                                                      file_name=>$file, file_parent=>$difftree{'from_file'}),
7610                                       -title => "diff"}, 'D');
7611                         if ($have_blame) {
7612                                 print $cgi->a({-href => href(-full=>1, action=>"blame",
7613                                                              file_name=>$file, hash_base=>$commit),
7614                                               -title => "blame"}, 'B');
7615                         }
7616                         # if this is not a feed of a file history
7617                         if (!defined $file_name || $file_name ne $file) {
7618                                 print $cgi->a({-href => href(-full=>1, action=>"history",
7619                                                              file_name=>$file, hash=>$commit),
7620                                               -title => "history"}, 'H');
7621                         }
7622                         $file = esc_path($file);
7623                         print "] ".
7624                               "$file</li>\n";
7625                 }
7626                 if ($format eq 'rss') {
7627                         print "</ul>]]>\n" .
7628                               "</content:encoded>\n" .
7629                               "</item>\n";
7630                 } elsif ($format eq 'atom') {
7631                         print "</ul>\n</div>\n" .
7632                               "</content>\n" .
7633                               "</entry>\n";
7634                 }
7635         }
7637         # end of feed
7638         if ($format eq 'rss') {
7639                 print "</channel>\n</rss>\n";
7640         } elsif ($format eq 'atom') {
7641                 print "</feed>\n";
7642         }
7645 sub git_rss {
7646         git_feed('rss');
7649 sub git_atom {
7650         git_feed('atom');
7653 sub git_opml {
7654         my @list = git_get_projects_list();
7655         if (!@list) {
7656                 die_error(404, "No projects found");
7657         }
7659         print $cgi->header(
7660                 -type => 'text/xml',
7661                 -charset => 'utf-8',
7662                 -content_disposition => 'inline; filename="opml.xml"');
7664         print <<XML;
7665 <?xml version="1.0" encoding="utf-8"?>
7666 <opml version="1.0">
7667 <head>
7668   <title>$site_name OPML Export</title>
7669 </head>
7670 <body>
7671 <outline text="git RSS feeds">
7672 XML
7674         foreach my $pr (@list) {
7675                 my %proj = %$pr;
7676                 my $head = git_get_head_hash($proj{'path'});
7677                 if (!defined $head) {
7678                         next;
7679                 }
7680                 $git_dir = "$projectroot/$proj{'path'}";
7681                 my %co = parse_commit($head);
7682                 if (!%co) {
7683                         next;
7684                 }
7686                 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
7687                 my $rss  = href('project' => $proj{'path'}, 'action' => 'rss', -full => 1);
7688                 my $html = href('project' => $proj{'path'}, 'action' => 'summary', -full => 1);
7689                 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
7690         }
7691         print <<XML;
7692 </outline>
7693 </body>
7694 </opml>
7695 XML