Code

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