3 # gitweb - simple web interface to track changes in git repositories
5 # (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org>
6 # (C) 2005, Christian Gierke
8 # This program is licensed under the GPLv2
13 use CGI
qw(:standard :escapeHTML -nosticky);
14 use CGI
::Util
qw(unescape);
15 use CGI
::Carp
qw(fatalsToBrowser set_message);
19 use File
::Basename
qw(basename);
20 use Time
::HiRes
qw(gettimeofday tv_interval);
21 binmode STDOUT
, ':utf8';
23 our $t0 = [ gettimeofday
() ];
24 our $number_of_git_cmds = 0;
27 CGI-
>compile() if $ENV{'MOD_PERL'};
30 our $version = "++GIT_VERSION++";
32 our ($my_url, $my_uri, $base_url, $path_info, $home_link);
36 our $my_url = $cgi->url();
37 our $my_uri = $cgi->url(-absolute
=> 1);
39 # Base URL for relative URLs in gitweb ($logo, $favicon, ...),
40 # needed and used only for URLs with nonempty PATH_INFO
41 our $base_url = $my_url;
43 # When the script is used as DirectoryIndex, the URL does not contain the name
44 # of the script file itself, and $cgi->url() fails to strip PATH_INFO, so we
45 # have to do it ourselves. We make $path_info global because it's also used
48 # Another issue with the script being the DirectoryIndex is that the resulting
49 # $my_url data is not the full script URL: this is good, because we want
50 # generated links to keep implying the script name if it wasn't explicitly
51 # indicated in the URL we're handling, but it means that $my_url cannot be used
53 # Therefore, if we needed to strip PATH_INFO, then we know that we have
54 # to build the base URL ourselves:
55 our $path_info = $ENV{"PATH_INFO"};
57 if ($my_url =~ s
,\Q
$path_info\E
$,, &&
58 $my_uri =~ s
,\Q
$path_info\E
$,, &&
59 defined $ENV{'SCRIPT_NAME'}) {
60 $base_url = $cgi->url(-base
=> 1) . $ENV{'SCRIPT_NAME'};
64 # target of the home link on top of all pages
65 our $home_link = $my_uri || "/";
68 # core git executable to use
69 # this can just be "git" if your webserver has a sensible PATH
70 our $GIT = "++GIT_BINDIR++/git";
72 # absolute fs-path which will be prepended to the project path
73 #our $projectroot = "/pub/scm";
74 our $projectroot = "++GITWEB_PROJECTROOT++";
76 # fs traversing limit for getting project list
77 # the number is relative to the projectroot
78 our $project_maxdepth = "++GITWEB_PROJECT_MAXDEPTH++";
80 # string of the home link on top of all pages
81 our $home_link_str = "++GITWEB_HOME_LINK_STR++";
83 # name of your site or organization to appear in page titles
84 # replace this with something more descriptive for clearer bookmarks
85 our $site_name = "++GITWEB_SITENAME++"
86 || ($ENV{'SERVER_NAME'} || "Untitled") . " Git";
88 # filename of html text to include at top of each page
89 our $site_header = "++GITWEB_SITE_HEADER++";
90 # html text to include at home page
91 our $home_text = "++GITWEB_HOMETEXT++";
92 # filename of html text to include at bottom of each page
93 our $site_footer = "++GITWEB_SITE_FOOTER++";
96 our @stylesheets = ("++GITWEB_CSS++");
97 # URI of a single stylesheet, which can be overridden in GITWEB_CONFIG.
98 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
166 our $prevent_xss = 0;
168 # Path to the highlight executable to use (must be the one from
169 # http://www.andre-simon.de due to assumptions about parameters and output).
170 # Useful if highlight is not installed on your webserver's PATH.
171 # [Default: highlight]
172 our $highlight_bin = "++HIGHLIGHT_BIN++";
174 # information about snapshot formats that gitweb is capable of serving
175 our %known_snapshot_formats = (
177 # 'display' => display name,
178 # 'type' => mime type,
179 # 'suffix' => filename suffix,
180 # 'format' => --format for git-archive,
181 # 'compressor' => [compressor command and arguments]
182 # (array reference, optional)
183 # 'disabled' => boolean (optional)}
186 'display' => 'tar.gz',
187 'type' => 'application/x-gzip',
188 'suffix' => '.tar.gz',
190 'compressor' => ['gzip', '-n']},
193 'display' => 'tar.bz2',
194 'type' => 'application/x-bzip2',
195 'suffix' => '.tar.bz2',
197 'compressor' => ['bzip2']},
200 'display' => 'tar.xz',
201 'type' => 'application/x-xz',
202 'suffix' => '.tar.xz',
204 'compressor' => ['xz'],
209 'type' => 'application/x-zip',
214 # Aliases so we understand old gitweb.snapshot values in repository
216 our %known_snapshot_format_aliases = (
221 # backward compatibility: legacy gitweb config support
222 'x-gzip' => undef, 'gz' => undef,
223 'x-bzip2' => undef, 'bz2' => undef,
224 'x-zip' => undef, '' => undef,
227 # Pixel sizes for icons and avatars. If the default font sizes or lineheights
228 # are changed, it may be appropriate to change these values too via
235 # Used to set the maximum load that we will still respond to gitweb queries.
236 # If server load exceed this value then return "503 server busy" error.
237 # If gitweb cannot determined server load, it is taken to be 0.
238 # Leave it undefined (or set to 'undef') to turn off load checking.
241 # configuration for 'highlight' (http://www.andre-simon.de/)
243 our %highlight_basename = (
246 'SConstruct' => 'py', # SCons equivalent of Makefile
247 'Makefile' => 'make',
250 our %highlight_ext = (
251 # main extensions, defining name of syntax;
252 # see files in /usr/share/highlight/langDefs/ directory
254 qw(py c cpp rb java css php sh pl js tex bib xml awk bat ini spec tcl sql make),
255 # alternate extensions, see /etc/highlight/filetypes.conf
257 map { $_ => 'sh' } qw(bash zsh ksh),
258 map { $_ => 'cpp' } qw(cxx c++ cc),
259 map { $_ => 'php' } qw(php3 php4 php5 phps),
260 map { $_ => 'pl' } qw(perl pm), # perhaps also 'cgi'
261 map { $_ => 'make'} qw(mak mk),
262 map { $_ => 'xml' } qw(xhtml html htm),
265 # You define site-wide feature defaults here; override them with
266 # $GITWEB_CONFIG as necessary.
269 # 'sub' => feature-sub (subroutine),
270 # 'override' => allow-override (boolean),
271 # 'default' => [ default options...] (array reference)}
273 # if feature is overridable (it means that allow-override has true value),
274 # then feature-sub will be called with default options as parameters;
275 # return value of feature-sub indicates if to enable specified feature
277 # if there is no 'sub' key (no feature-sub), then feature cannot be
280 # use gitweb_get_feature(<feature>) to retrieve the <feature> value
281 # (an array) or gitweb_check_feature(<feature>) to check if <feature>
284 # Enable the 'blame' blob view, showing the last commit that modified
285 # each line in the file. This can be very CPU-intensive.
287 # To enable system wide have in $GITWEB_CONFIG
288 # $feature{'blame'}{'default'} = [1];
289 # To have project specific config enable override in $GITWEB_CONFIG
290 # $feature{'blame'}{'override'} = 1;
291 # and in project config gitweb.blame = 0|1;
293 'sub' => sub { feature_bool
('blame', @_) },
297 # Enable the 'snapshot' link, providing a compressed archive of any
298 # tree. This can potentially generate high traffic if you have large
301 # Value is a list of formats defined in %known_snapshot_formats that
303 # To disable system wide have in $GITWEB_CONFIG
304 # $feature{'snapshot'}{'default'} = [];
305 # To have project specific config enable override in $GITWEB_CONFIG
306 # $feature{'snapshot'}{'override'} = 1;
307 # and in project config, a comma-separated list of formats or "none"
308 # to disable. Example: gitweb.snapshot = tbz2,zip;
310 'sub' => \
&feature_snapshot
,
312 'default' => ['tgz']},
314 # Enable text search, which will list the commits which match author,
315 # committer or commit text to a given string. Enabled by default.
316 # Project specific override is not supported.
321 # Enable grep search, which will list the files in currently selected
322 # tree containing the given string. Enabled by default. This can be
323 # potentially CPU-intensive, of course.
325 # To enable system wide have in $GITWEB_CONFIG
326 # $feature{'grep'}{'default'} = [1];
327 # To have project specific config enable override in $GITWEB_CONFIG
328 # $feature{'grep'}{'override'} = 1;
329 # and in project config gitweb.grep = 0|1;
331 'sub' => sub { feature_bool
('grep', @_) },
335 # Enable the pickaxe search, which will list the commits that modified
336 # a given string in a file. This can be practical and quite faster
337 # alternative to 'blame', but still potentially CPU-intensive.
339 # To enable system wide have in $GITWEB_CONFIG
340 # $feature{'pickaxe'}{'default'} = [1];
341 # To have project specific config enable override in $GITWEB_CONFIG
342 # $feature{'pickaxe'}{'override'} = 1;
343 # and in project config gitweb.pickaxe = 0|1;
345 'sub' => sub { feature_bool
('pickaxe', @_) },
349 # Enable showing size of blobs in a 'tree' view, in a separate
350 # column, similar to what 'ls -l' does. This cost a bit of IO.
352 # To disable system wide have in $GITWEB_CONFIG
353 # $feature{'show-sizes'}{'default'} = [0];
354 # To have project specific config enable override in $GITWEB_CONFIG
355 # $feature{'show-sizes'}{'override'} = 1;
356 # and in project config gitweb.showsizes = 0|1;
358 'sub' => sub { feature_bool
('showsizes', @_) },
362 # Make gitweb use an alternative format of the URLs which can be
363 # more readable and natural-looking: project name is embedded
364 # directly in the path and the query string contains other
365 # auxiliary information. All gitweb installations recognize
366 # URL in either format; this configures in which formats gitweb
369 # To enable system wide have in $GITWEB_CONFIG
370 # $feature{'pathinfo'}{'default'} = [1];
371 # Project specific override is not supported.
373 # Note that you will need to change the default location of CSS,
374 # favicon, logo and possibly other files to an absolute URL. Also,
375 # if gitweb.cgi serves as your indexfile, you will need to force
376 # $my_uri to contain the script name in your $GITWEB_CONFIG.
381 # Make gitweb consider projects in project root subdirectories
382 # to be forks of existing projects. Given project $projname.git,
383 # projects matching $projname/*.git will not be shown in the main
384 # projects list, instead a '+' mark will be added to $projname
385 # there and a 'forks' view will be enabled for the project, listing
386 # all the forks. If project list is taken from a file, forks have
387 # to be listed after the main project.
389 # To enable system wide have in $GITWEB_CONFIG
390 # $feature{'forks'}{'default'} = [1];
391 # Project specific override is not supported.
396 # Insert custom links to the action bar of all project pages.
397 # This enables you mainly to link to third-party scripts integrating
398 # into gitweb; e.g. git-browser for graphical history representation
399 # or custom web-based repository administration interface.
401 # The 'default' value consists of a list of triplets in the form
402 # (label, link, position) where position is the label after which
403 # to insert the link and link is a format string where %n expands
404 # to the project name, %f to the project path within the filesystem,
405 # %h to the current hash (h gitweb parameter) and %b to the current
406 # hash base (hb gitweb parameter); %% expands to %.
408 # To enable system wide have in $GITWEB_CONFIG e.g.
409 # $feature{'actions'}{'default'} = [('graphiclog',
410 # '/git-browser/by-commit.html?r=%n', 'summary')];
411 # Project specific override is not supported.
416 # Allow gitweb scan project content tags of project repository,
417 # and display the popular Web 2.0-ish "tag cloud" near the projects
418 # list. Note that this is something COMPLETELY different from the
421 # gitweb by itself can show existing tags, but it does not handle
422 # tagging itself; you need to do it externally, outside gitweb.
423 # The format is described in git_get_project_ctags() subroutine.
424 # You may want to install the HTML::TagCloud Perl module to get
425 # a pretty tag cloud instead of just a list of tags.
427 # To enable system wide have in $GITWEB_CONFIG
428 # $feature{'ctags'}{'default'} = [1];
429 # Project specific override is not supported.
431 # In the future whether ctags editing is enabled might depend
432 # on the value, but using 1 should always mean no editing of ctags.
437 # The maximum number of patches in a patchset generated in patch
438 # view. Set this to 0 or undef to disable patch view, or to a
439 # negative number to remove any limit.
441 # To disable system wide have in $GITWEB_CONFIG
442 # $feature{'patches'}{'default'} = [0];
443 # To have project specific config enable override in $GITWEB_CONFIG
444 # $feature{'patches'}{'override'} = 1;
445 # and in project config gitweb.patches = 0|n;
446 # where n is the maximum number of patches allowed in a patchset.
448 'sub' => \
&feature_patches
,
452 # Avatar support. When this feature is enabled, views such as
453 # shortlog or commit will display an avatar associated with
454 # the email of the committer(s) and/or author(s).
456 # Currently available providers are gravatar and picon.
457 # If an unknown provider is specified, the feature is disabled.
459 # Gravatar depends on Digest::MD5.
460 # Picon currently relies on the indiana.edu database.
462 # To enable system wide have in $GITWEB_CONFIG
463 # $feature{'avatar'}{'default'} = ['<provider>'];
464 # where <provider> is either gravatar or picon.
465 # To have project specific config enable override in $GITWEB_CONFIG
466 # $feature{'avatar'}{'override'} = 1;
467 # and in project config gitweb.avatar = <provider>;
469 'sub' => \
&feature_avatar
,
473 # Enable displaying how much time and how many git commands
474 # it took to generate and display page. Disabled by default.
475 # Project specific override is not supported.
480 # Enable turning some links into links to actions which require
481 # JavaScript to run (like 'blame_incremental'). Not enabled by
482 # default. Project specific override is currently not supported.
483 'javascript-actions' => {
487 # Syntax highlighting support. This is based on Daniel Svensson's
488 # and Sham Chukoury's work in gitweb-xmms2.git.
489 # It requires the 'highlight' program present in $PATH,
490 # and therefore is disabled by default.
492 # To enable system wide have in $GITWEB_CONFIG
493 # $feature{'highlight'}{'default'} = [1];
496 'sub' => sub { feature_bool
('highlight', @_) },
500 # Enable displaying of remote heads in the heads list
502 # To enable system wide have in $GITWEB_CONFIG
503 # $feature{'remote_heads'}{'default'} = [1];
504 # To have project specific config enable override in $GITWEB_CONFIG
505 # $feature{'remote_heads'}{'override'} = 1;
506 # and in project config gitweb.remote_heads = 0|1;
508 'sub' => sub { feature_bool
('remote_heads', @_) },
513 sub gitweb_get_feature
{
515 return unless exists $feature{$name};
516 my ($sub, $override, @defaults) = (
517 $feature{$name}{'sub'},
518 $feature{$name}{'override'},
519 @{$feature{$name}{'default'}});
520 # project specific override is possible only if we have project
521 our $git_dir; # global variable, declared later
522 if (!$override || !defined $git_dir) {
526 warn "feature $name is not overridable";
529 return $sub->(@defaults);
532 # A wrapper to check if a given feature is enabled.
533 # With this, you can say
535 # my $bool_feat = gitweb_check_feature('bool_feat');
536 # gitweb_check_feature('bool_feat') or somecode;
540 # my ($bool_feat) = gitweb_get_feature('bool_feat');
541 # (gitweb_get_feature('bool_feat'))[0] or somecode;
543 sub gitweb_check_feature
{
544 return (gitweb_get_feature
(@_))[0];
550 my ($val) = git_get_project_config
($key, '--bool');
554 } elsif ($val eq 'true') {
556 } elsif ($val eq 'false') {
561 sub feature_snapshot
{
564 my ($val) = git_get_project_config
('snapshot');
567 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
573 sub feature_patches
{
574 my @val = (git_get_project_config
('patches', '--int'));
584 my @val = (git_get_project_config
('avatar'));
586 return @val ? @val : @_;
589 # checking HEAD file with -e is fragile if the repository was
590 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
592 sub check_head_link
{
594 my $headfile = "$dir/HEAD";
595 return ((-e
$headfile) ||
596 (-l
$headfile && readlink($headfile) =~ /^refs\/heads\
//));
599 sub check_export_ok
{
601 return (check_head_link
($dir) &&
602 (!$export_ok || -e
"$dir/$export_ok") &&
603 (!$export_auth_hook || $export_auth_hook->($dir)));
606 # process alternate names for backward compatibility
607 # filter out unsupported (unknown) snapshot formats
608 sub filter_snapshot_fmts
{
612 exists $known_snapshot_format_aliases{$_} ?
613 $known_snapshot_format_aliases{$_} : $_} @fmts;
615 exists $known_snapshot_formats{$_} &&
616 !$known_snapshot_formats{$_}{'disabled'}} @fmts;
619 # If it is set to code reference, it is code that it is to be run once per
620 # request, allowing updating configurations that change with each request,
621 # while running other code in config file only once.
623 # Otherwise, if it is false then gitweb would process config file only once;
624 # if it is true then gitweb config would be run for each request.
625 our $per_request_config = 1;
627 our ($GITWEB_CONFIG, $GITWEB_CONFIG_SYSTEM);
628 sub evaluate_gitweb_config
{
629 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
630 our $GITWEB_CONFIG_SYSTEM = $ENV{'GITWEB_CONFIG_SYSTEM'} || "++GITWEB_CONFIG_SYSTEM++";
631 # die if there are errors parsing config file
632 if (-e
$GITWEB_CONFIG) {
635 } elsif (-e
$GITWEB_CONFIG_SYSTEM) {
636 do $GITWEB_CONFIG_SYSTEM;
641 # Get loadavg of system, to compare against $maxload.
642 # Currently it requires '/proc/loadavg' present to get loadavg;
643 # if it is not present it returns 0, which means no load checking.
645 if( -e
'/proc/loadavg' ){
646 open my $fd, '<', '/proc/loadavg'
648 my @load = split(/\s+/, scalar <$fd>);
651 # The first three columns measure CPU and IO utilization of the last one,
652 # five, and 10 minute periods. The fourth column shows the number of
653 # currently running processes and the total number of processes in the m/n
654 # format. The last column displays the last process ID used.
655 return $load[0] || 0;
657 # additional checks for load average should go here for things that don't export
663 # version of the core git binary
665 sub evaluate_git_version
{
666 our $git_version = qx("$GIT" --version) =~ m/git version (.*)$/ ? $1 : "unknown";
667 $number_of_git_cmds++;
671 if (defined $maxload && get_loadavg
() > $maxload) {
672 die_error
(503, "The load average on the server is too high");
676 # ======================================================================
677 # input validation and dispatch
679 # input parameters can be collected from a variety of sources (presently, CGI
680 # and PATH_INFO), so we define an %input_params hash that collects them all
681 # together during validation: this allows subsequent uses (e.g. href()) to be
682 # agnostic of the parameter origin
684 our %input_params = ();
686 # input parameters are stored with the long parameter name as key. This will
687 # also be used in the href subroutine to convert parameters to their CGI
688 # equivalent, and since the href() usage is the most frequent one, we store
689 # the name -> CGI key mapping here, instead of the reverse.
691 # XXX: Warning: If you touch this, check the search form for updating,
694 our @cgi_param_mapping = (
702 hash_parent_base
=> "hpb",
707 snapshot_format
=> "sf",
708 extra_options
=> "opt",
709 search_use_regexp
=> "sr",
711 # this must be last entry (for manipulation from JavaScript)
714 our %cgi_param_mapping = @cgi_param_mapping;
716 # we will also need to know the possible actions, for validation
718 "blame" => \
&git_blame
,
719 "blame_incremental" => \
&git_blame_incremental
,
720 "blame_data" => \
&git_blame_data
,
721 "blobdiff" => \
&git_blobdiff
,
722 "blobdiff_plain" => \
&git_blobdiff_plain
,
723 "blob" => \
&git_blob
,
724 "blob_plain" => \
&git_blob_plain
,
725 "commitdiff" => \
&git_commitdiff
,
726 "commitdiff_plain" => \
&git_commitdiff_plain
,
727 "commit" => \
&git_commit
,
728 "forks" => \
&git_forks
,
729 "heads" => \
&git_heads
,
730 "history" => \
&git_history
,
732 "patch" => \
&git_patch
,
733 "patches" => \
&git_patches
,
734 "remotes" => \
&git_remotes
,
736 "atom" => \
&git_atom
,
737 "search" => \
&git_search
,
738 "search_help" => \
&git_search_help
,
739 "shortlog" => \
&git_shortlog
,
740 "summary" => \
&git_summary
,
742 "tags" => \
&git_tags
,
743 "tree" => \
&git_tree
,
744 "snapshot" => \
&git_snapshot
,
745 "object" => \
&git_object
,
746 # those below don't need $project
747 "opml" => \
&git_opml
,
748 "project_list" => \
&git_project_list
,
749 "project_index" => \
&git_project_index
,
752 # finally, we have the hash of allowed extra_options for the commands that
754 our %allowed_options = (
755 "--no-merges" => [ qw(rss atom log shortlog history) ],
758 # fill %input_params with the CGI parameters. All values except for 'opt'
759 # should be single values, but opt can be an array. We should probably
760 # build an array of parameters that can be multi-valued, but since for the time
761 # being it's only this one, we just single it out
762 sub evaluate_query_params
{
765 while (my ($name, $symbol) = each %cgi_param_mapping) {
766 if ($symbol eq 'opt') {
767 $input_params{$name} = [ $cgi->param($symbol) ];
769 $input_params{$name} = $cgi->param($symbol);
774 # now read PATH_INFO and update the parameter list for missing parameters
775 sub evaluate_path_info
{
776 return if defined $input_params{'project'};
777 return if !$path_info;
778 $path_info =~ s
,^/+,,;
779 return if !$path_info;
781 # find which part of PATH_INFO is project
782 my $project = $path_info;
784 while ($project && !check_head_link
("$projectroot/$project")) {
785 $project =~ s
,/*[^/]*$,,;
787 return unless $project;
788 $input_params{'project'} = $project;
790 # do not change any parameters if an action is given using the query string
791 return if $input_params{'action'};
792 $path_info =~ s
,^\Q
$project\E
/*,,;
794 # next, check if we have an action
795 my $action = $path_info;
797 if (exists $actions{$action}) {
798 $path_info =~ s
,^$action/*,,;
799 $input_params{'action'} = $action;
802 # list of actions that want hash_base instead of hash, but can have no
803 # pathname (f) parameter
809 # we want to catch, among others
810 # [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name]
811 my ($parentrefname, $parentpathname, $refname, $pathname) =
812 ($path_info =~ /^(?:(.+?)(?::(.+))?\.\.)?([^:]+?)?(?::(.+))?$/);
814 # first, analyze the 'current' part
815 if (defined $pathname) {
816 # we got "branch:filename" or "branch:dir/"
817 # we could use git_get_type(branch:pathname), but:
818 # - it needs $git_dir
819 # - it does a git() call
820 # - the convention of terminating directories with a slash
821 # makes it superfluous
822 # - embedding the action in the PATH_INFO would make it even
824 $pathname =~ s
,^/+,,;
825 if (!$pathname || substr($pathname, -1) eq "/") {
826 $input_params{'action'} ||= "tree";
829 # the default action depends on whether we had parent info
831 if ($parentrefname) {
832 $input_params{'action'} ||= "blobdiff_plain";
834 $input_params{'action'} ||= "blob_plain";
837 $input_params{'hash_base'} ||= $refname;
838 $input_params{'file_name'} ||= $pathname;
839 } elsif (defined $refname) {
840 # we got "branch". In this case we have to choose if we have to
841 # set hash or hash_base.
843 # Most of the actions without a pathname only want hash to be
844 # set, except for the ones specified in @wants_base that want
845 # hash_base instead. It should also be noted that hand-crafted
846 # links having 'history' as an action and no pathname or hash
847 # set will fail, but that happens regardless of PATH_INFO.
848 if (defined $parentrefname) {
849 # if there is parent let the default be 'shortlog' action
850 # (for http://git.example.com/repo.git/A..B links); if there
851 # is no parent, dispatch will detect type of object and set
852 # action appropriately if required (if action is not set)
853 $input_params{'action'} ||= "shortlog";
855 if ($input_params{'action'} &&
856 grep { $_ eq $input_params{'action'} } @wants_base) {
857 $input_params{'hash_base'} ||= $refname;
859 $input_params{'hash'} ||= $refname;
863 # next, handle the 'parent' part, if present
864 if (defined $parentrefname) {
865 # a missing pathspec defaults to the 'current' filename, allowing e.g.
866 # someproject/blobdiff/oldrev..newrev:/filename
867 if ($parentpathname) {
868 $parentpathname =~ s
,^/+,,;
869 $parentpathname =~ s
,/$,,;
870 $input_params{'file_parent'} ||= $parentpathname;
872 $input_params{'file_parent'} ||= $input_params{'file_name'};
874 # we assume that hash_parent_base is wanted if a path was specified,
875 # or if the action wants hash_base instead of hash
876 if (defined $input_params{'file_parent'} ||
877 grep { $_ eq $input_params{'action'} } @wants_base) {
878 $input_params{'hash_parent_base'} ||= $parentrefname;
880 $input_params{'hash_parent'} ||= $parentrefname;
884 # for the snapshot action, we allow URLs in the form
885 # $project/snapshot/$hash.ext
886 # where .ext determines the snapshot and gets removed from the
887 # passed $refname to provide the $hash.
889 # To be able to tell that $refname includes the format extension, we
890 # require the following two conditions to be satisfied:
891 # - the hash input parameter MUST have been set from the $refname part
892 # of the URL (i.e. they must be equal)
893 # - the snapshot format MUST NOT have been defined already (e.g. from
895 # It's also useless to try any matching unless $refname has a dot,
896 # so we check for that too
897 if (defined $input_params{'action'} &&
898 $input_params{'action'} eq 'snapshot' &&
899 defined $refname && index($refname, '.') != -1 &&
900 $refname eq $input_params{'hash'} &&
901 !defined $input_params{'snapshot_format'}) {
902 # We loop over the known snapshot formats, checking for
903 # extensions. Allowed extensions are both the defined suffix
904 # (which includes the initial dot already) and the snapshot
905 # format key itself, with a prepended dot
906 while (my ($fmt, $opt) = each %known_snapshot_formats) {
908 unless ($hash =~ s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) {
912 # a valid suffix was found, so set the snapshot format
913 # and reset the hash parameter
914 $input_params{'snapshot_format'} = $fmt;
915 $input_params{'hash'} = $hash;
916 # we also set the format suffix to the one requested
917 # in the URL: this way a request for e.g. .tgz returns
918 # a .tgz instead of a .tar.gz
919 $known_snapshot_formats{$fmt}{'suffix'} = $sfx;
925 our ($action, $project, $file_name, $file_parent, $hash, $hash_parent, $hash_base,
926 $hash_parent_base, @extra_options, $page, $searchtype, $search_use_regexp,
927 $searchtext, $search_regexp);
928 sub evaluate_and_validate_params
{
929 our $action = $input_params{'action'};
930 if (defined $action) {
931 if (!validate_action
($action)) {
932 die_error
(400, "Invalid action parameter");
936 # parameters which are pathnames
937 our $project = $input_params{'project'};
938 if (defined $project) {
939 if (!validate_project
($project)) {
941 die_error
(404, "No such project");
945 our $file_name = $input_params{'file_name'};
946 if (defined $file_name) {
947 if (!validate_pathname
($file_name)) {
948 die_error
(400, "Invalid file parameter");
952 our $file_parent = $input_params{'file_parent'};
953 if (defined $file_parent) {
954 if (!validate_pathname
($file_parent)) {
955 die_error
(400, "Invalid file parent parameter");
959 # parameters which are refnames
960 our $hash = $input_params{'hash'};
962 if (!validate_refname
($hash)) {
963 die_error
(400, "Invalid hash parameter");
967 our $hash_parent = $input_params{'hash_parent'};
968 if (defined $hash_parent) {
969 if (!validate_refname
($hash_parent)) {
970 die_error
(400, "Invalid hash parent parameter");
974 our $hash_base = $input_params{'hash_base'};
975 if (defined $hash_base) {
976 if (!validate_refname
($hash_base)) {
977 die_error
(400, "Invalid hash base parameter");
981 our @extra_options = @{$input_params{'extra_options'}};
982 # @extra_options is always defined, since it can only be (currently) set from
983 # CGI, and $cgi->param() returns the empty array in array context if the param
985 foreach my $opt (@extra_options) {
986 if (not exists $allowed_options{$opt}) {
987 die_error
(400, "Invalid option parameter");
989 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
990 die_error
(400, "Invalid option parameter for this action");
994 our $hash_parent_base = $input_params{'hash_parent_base'};
995 if (defined $hash_parent_base) {
996 if (!validate_refname
($hash_parent_base)) {
997 die_error
(400, "Invalid hash parent base parameter");
1002 our $page = $input_params{'page'};
1003 if (defined $page) {
1004 if ($page =~ m/[^0-9]/) {
1005 die_error
(400, "Invalid page parameter");
1009 our $searchtype = $input_params{'searchtype'};
1010 if (defined $searchtype) {
1011 if ($searchtype =~ m/[^a-z]/) {
1012 die_error
(400, "Invalid searchtype parameter");
1016 our $search_use_regexp = $input_params{'search_use_regexp'};
1018 our $searchtext = $input_params{'searchtext'};
1020 if (defined $searchtext) {
1021 if (length($searchtext) < 2) {
1022 die_error
(403, "At least two characters are required for search parameter");
1024 $search_regexp = $search_use_regexp ? $searchtext : quotemeta $searchtext;
1028 # path to the current git repository
1030 sub evaluate_git_dir
{
1031 our $git_dir = "$projectroot/$project" if $project;
1034 our (@snapshot_fmts, $git_avatar);
1035 sub configure_gitweb_features
{
1036 # list of supported snapshot formats
1037 our @snapshot_fmts = gitweb_get_feature
('snapshot');
1038 @snapshot_fmts = filter_snapshot_fmts
(@snapshot_fmts);
1040 # check that the avatar feature is set to a known provider name,
1041 # and for each provider check if the dependencies are satisfied.
1042 # if the provider name is invalid or the dependencies are not met,
1043 # reset $git_avatar to the empty string.
1044 our ($git_avatar) = gitweb_get_feature
('avatar');
1045 if ($git_avatar eq 'gravatar') {
1046 $git_avatar = '' unless (eval { require Digest
::MD5
; 1; });
1047 } elsif ($git_avatar eq 'picon') {
1054 # custom error handler: 'die <message>' is Internal Server Error
1055 sub handle_errors_html
{
1056 my $msg = shift; # it is already HTML escaped
1058 # to avoid infinite loop where error occurs in die_error,
1059 # change handler to default handler, disabling handle_errors_html
1060 set_message
("Error occured when inside die_error:\n$msg");
1062 # you cannot jump out of die_error when called as error handler;
1063 # the subroutine set via CGI::Carp::set_message is called _after_
1064 # HTTP headers are already written, so it cannot write them itself
1065 die_error
(undef, undef, $msg, -error_handler
=> 1, -no_http_header
=> 1);
1067 set_message
(\
&handle_errors_html
);
1071 if (!defined $action) {
1072 if (defined $hash) {
1073 $action = git_get_type
($hash);
1074 } elsif (defined $hash_base && defined $file_name) {
1075 $action = git_get_type
("$hash_base:$file_name");
1076 } elsif (defined $project) {
1077 $action = 'summary';
1079 $action = 'project_list';
1082 if (!defined($actions{$action})) {
1083 die_error
(400, "Unknown action");
1085 if ($action !~ m/^(?:opml|project_list|project_index)$/ &&
1087 die_error
(400, "Project needed");
1089 $actions{$action}->();
1093 our $t0 = [ gettimeofday
() ]
1095 our $number_of_git_cmds = 0;
1098 our $first_request = 1;
1103 if ($first_request) {
1104 evaluate_gitweb_config
();
1105 evaluate_git_version
();
1107 if ($per_request_config) {
1108 if (ref($per_request_config) eq 'CODE') {
1109 $per_request_config->();
1110 } elsif (!$first_request) {
1111 evaluate_gitweb_config
();
1116 # $projectroot and $projects_list might be set in gitweb config file
1117 $projects_list ||= $projectroot;
1119 evaluate_query_params
();
1120 evaluate_path_info
();
1121 evaluate_and_validate_params
();
1124 configure_gitweb_features
();
1129 our $is_last_request = sub { 1 };
1130 our ($pre_dispatch_hook, $post_dispatch_hook, $pre_listen_hook);
1133 sub configure_as_fcgi
{
1135 our $CGI = 'CGI::Fast';
1137 my $request_number = 0;
1138 # let each child service 100 requests
1139 our $is_last_request = sub { ++$request_number > 100 };
1142 my $script_name = $ENV{'SCRIPT_NAME'} || $ENV{'SCRIPT_FILENAME'} || __FILE__
;
1144 if $script_name =~ /\.fcgi$/;
1146 return unless (@ARGV);
1148 require Getopt
::Long
;
1149 Getopt
::Long
::GetOptions
(
1150 'fastcgi|fcgi|f' => \
&configure_as_fcgi
,
1151 'nproc|n=i' => sub {
1152 my ($arg, $val) = @_;
1153 return unless eval { require FCGI
::ProcManager
; 1; };
1154 my $proc_manager = FCGI
::ProcManager-
>new({
1155 n_processes
=> $val,
1157 our $pre_listen_hook = sub { $proc_manager->pm_manage() };
1158 our $pre_dispatch_hook = sub { $proc_manager->pm_pre_dispatch() };
1159 our $post_dispatch_hook = sub { $proc_manager->pm_post_dispatch() };
1168 $pre_listen_hook->()
1169 if $pre_listen_hook;
1172 while ($cgi = $CGI->new()) {
1173 $pre_dispatch_hook->()
1174 if $pre_dispatch_hook;
1178 $post_dispatch_hook->()
1179 if $post_dispatch_hook;
1182 last REQUEST
if ($is_last_request->());
1191 if (defined caller) {
1192 # wrapped in a subroutine processing requests,
1193 # e.g. mod_perl with ModPerl::Registry, or PSGI with Plack::App::WrapCGI
1196 # pure CGI script, serving single request
1200 ## ======================================================================
1203 # possible values of extra options
1204 # -full => 0|1 - use absolute/full URL ($my_uri/$my_url as base)
1205 # -replay => 1 - start from a current view (replay with modifications)
1206 # -path_info => 0|1 - don't use/use path_info URL (if possible)
1207 # -anchor => ANCHOR - add #ANCHOR to end of URL, implies -replay if used alone
1210 # default is to use -absolute url() i.e. $my_uri
1211 my $href = $params{-full
} ? $my_url : $my_uri;
1213 # implicit -replay, must be first of implicit params
1214 $params{-replay
} = 1 if (keys %params == 1 && $params{-anchor
});
1216 $params{'project'} = $project unless exists $params{'project'};
1218 if ($params{-replay
}) {
1219 while (my ($name, $symbol) = each %cgi_param_mapping) {
1220 if (!exists $params{$name}) {
1221 $params{$name} = $input_params{$name};
1226 my $use_pathinfo = gitweb_check_feature
('pathinfo');
1227 if (defined $params{'project'} &&
1228 (exists $params{-path_info
} ? $params{-path_info
} : $use_pathinfo)) {
1229 # try to put as many parameters as possible in PATH_INFO:
1232 # - hash_parent or hash_parent_base:/file_parent
1233 # - hash or hash_base:/filename
1234 # - the snapshot_format as an appropriate suffix
1236 # When the script is the root DirectoryIndex for the domain,
1237 # $href here would be something like http://gitweb.example.com/
1238 # Thus, we strip any trailing / from $href, to spare us double
1239 # slashes in the final URL
1242 # Then add the project name, if present
1243 $href .= "/".esc_path_info
($params{'project'});
1244 delete $params{'project'};
1246 # since we destructively absorb parameters, we keep this
1247 # boolean that remembers if we're handling a snapshot
1248 my $is_snapshot = $params{'action'} eq 'snapshot';
1250 # Summary just uses the project path URL, any other action is
1252 if (defined $params{'action'}) {
1253 $href .= "/".esc_path_info
($params{'action'})
1254 unless $params{'action'} eq 'summary';
1255 delete $params{'action'};
1258 # Next, we put hash_parent_base:/file_parent..hash_base:/file_name,
1259 # stripping nonexistent or useless pieces
1260 $href .= "/" if ($params{'hash_base'} || $params{'hash_parent_base'}
1261 || $params{'hash_parent'} || $params{'hash'});
1262 if (defined $params{'hash_base'}) {
1263 if (defined $params{'hash_parent_base'}) {
1264 $href .= esc_path_info
($params{'hash_parent_base'});
1265 # skip the file_parent if it's the same as the file_name
1266 if (defined $params{'file_parent'}) {
1267 if (defined $params{'file_name'} && $params{'file_parent'} eq $params{'file_name'}) {
1268 delete $params{'file_parent'};
1269 } elsif ($params{'file_parent'} !~ /\.\./) {
1270 $href .= ":/".esc_path_info
($params{'file_parent'});
1271 delete $params{'file_parent'};
1275 delete $params{'hash_parent'};
1276 delete $params{'hash_parent_base'};
1277 } elsif (defined $params{'hash_parent'}) {
1278 $href .= esc_path_info
($params{'hash_parent'}). "..";
1279 delete $params{'hash_parent'};
1282 $href .= esc_path_info
($params{'hash_base'});
1283 if (defined $params{'file_name'} && $params{'file_name'} !~ /\.\./) {
1284 $href .= ":/".esc_path_info
($params{'file_name'});
1285 delete $params{'file_name'};
1287 delete $params{'hash'};
1288 delete $params{'hash_base'};
1289 } elsif (defined $params{'hash'}) {
1290 $href .= esc_path_info
($params{'hash'});
1291 delete $params{'hash'};
1294 # If the action was a snapshot, we can absorb the
1295 # snapshot_format parameter too
1297 my $fmt = $params{'snapshot_format'};
1298 # snapshot_format should always be defined when href()
1299 # is called, but just in case some code forgets, we
1300 # fall back to the default
1301 $fmt ||= $snapshot_fmts[0];
1302 $href .= $known_snapshot_formats{$fmt}{'suffix'};
1303 delete $params{'snapshot_format'};
1307 # now encode the parameters explicitly
1309 for (my $i = 0; $i < @cgi_param_mapping; $i += 2) {
1310 my ($name, $symbol) = ($cgi_param_mapping[$i], $cgi_param_mapping[$i+1]);
1311 if (defined $params{$name}) {
1312 if (ref($params{$name}) eq "ARRAY") {
1313 foreach my $par (@{$params{$name}}) {
1314 push @result, $symbol . "=" . esc_param
($par);
1317 push @result, $symbol . "=" . esc_param
($params{$name});
1321 $href .= "?" . join(';', @result) if scalar @result;
1323 # final transformation: trailing spaces must be escaped (URI-encoded)
1324 $href =~ s/(\s+)$/CGI::escape($1)/e;
1326 if ($params{-anchor
}) {
1327 $href .= "#".esc_param
($params{-anchor
});
1334 ## ======================================================================
1335 ## validation, quoting/unquoting and escaping
1337 sub validate_action
{
1338 my $input = shift || return undef;
1339 return undef unless exists $actions{$input};
1343 sub validate_project
{
1344 my $input = shift || return undef;
1345 if (!validate_pathname
($input) ||
1346 !(-d
"$projectroot/$input") ||
1347 !check_export_ok
("$projectroot/$input") ||
1348 ($strict_export && !project_in_list
($input))) {
1355 sub validate_pathname
{
1356 my $input = shift || return undef;
1358 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
1359 # at the beginning, at the end, and between slashes.
1360 # also this catches doubled slashes
1361 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
1364 # no null characters
1365 if ($input =~ m!\0!) {
1371 sub validate_refname
{
1372 my $input = shift || return undef;
1374 # textual hashes are O.K.
1375 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
1378 # it must be correct pathname
1379 $input = validate_pathname
($input)
1381 # restrictions on ref name according to git-check-ref-format
1382 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
1388 # decode sequences of octets in utf8 into Perl's internal form,
1389 # which is utf-8 with utf8 flag set if needed. gitweb writes out
1390 # in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
1393 return undef unless defined $str;
1394 if (utf8
::valid
($str)) {
1398 return decode
($fallback_encoding, $str, Encode
::FB_DEFAULT
);
1402 # quote unsafe chars, but keep the slash, even when it's not
1403 # correct, but quoted slashes look too horrible in bookmarks
1406 return undef unless defined $str;
1407 $str =~ s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI
::escape
($1)/eg
;
1412 # the quoting rules for path_info fragment are slightly different
1415 return undef unless defined $str;
1417 # path_info doesn't treat '+' as space (specially), but '?' must be escaped
1418 $str =~ s/([^A-Za-z0-9\-_.~();\/;:@&= +]+)/CGI
::escape
($1)/eg
;
1423 # quote unsafe chars in whole URL, so some characters cannot be quoted
1426 return undef unless defined $str;
1427 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&= ]+)/CGI
::escape
($1)/eg
;
1432 # quote unsafe characters in HTML attributes
1435 # for XHTML conformance escaping '"' to '"' is not enough
1436 return esc_html
(@_);
1439 # replace invalid utf8 character with SUBSTITUTION sequence
1444 return undef unless defined $str;
1446 $str = to_utf8
($str);
1447 $str = $cgi->escapeHTML($str);
1448 if ($opts{'-nbsp'}) {
1449 $str =~ s/ / /g;
1451 $str =~ s
|([[:cntrl
:]])|(($1 ne "\t") ? quot_cec
($1) : $1)|eg
;
1455 # quote control characters and escape filename to HTML
1460 return undef unless defined $str;
1462 $str = to_utf8
($str);
1463 $str = $cgi->escapeHTML($str);
1464 if ($opts{'-nbsp'}) {
1465 $str =~ s/ / /g;
1467 $str =~ s
|([[:cntrl
:]])|quot_cec
($1)|eg
;
1471 # Make control characters "printable", using character escape codes (CEC)
1475 my %es = ( # character escape codes, aka escape sequences
1476 "\t" => '\t', # tab (HT)
1477 "\n" => '\n', # line feed (LF)
1478 "\r" => '\r', # carrige return (CR)
1479 "\f" => '\f', # form feed (FF)
1480 "\b" => '\b', # backspace (BS)
1481 "\a" => '\a', # alarm (bell) (BEL)
1482 "\e" => '\e', # escape (ESC)
1483 "\013" => '\v', # vertical tab (VT)
1484 "\000" => '\0', # nul character (NUL)
1486 my $chr = ( (exists $es{$cntrl})
1488 : sprintf('\%2x', ord($cntrl)) );
1489 if ($opts{-nohtml
}) {
1492 return "<span class=\"cntrl\">$chr</span>";
1496 # Alternatively use unicode control pictures codepoints,
1497 # Unicode "printable representation" (PR)
1502 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
1503 if ($opts{-nohtml
}) {
1506 return "<span class=\"cntrl\">$chr</span>";
1510 # git may return quoted and escaped filenames
1516 my %es = ( # character escape codes, aka escape sequences
1517 't' => "\t", # tab (HT, TAB)
1518 'n' => "\n", # newline (NL)
1519 'r' => "\r", # return (CR)
1520 'f' => "\f", # form feed (FF)
1521 'b' => "\b", # backspace (BS)
1522 'a' => "\a", # alarm (bell) (BEL)
1523 'e' => "\e", # escape (ESC)
1524 'v' => "\013", # vertical tab (VT)
1527 if ($seq =~ m/^[0-7]{1,3}$/) {
1528 # octal char sequence
1529 return chr(oct($seq));
1530 } elsif (exists $es{$seq}) {
1531 # C escape sequence, aka character escape code
1534 # quoted ordinary character
1538 if ($str =~ m/^"(.*)"$/) {
1541 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
1546 # escape tabs (convert tabs to spaces)
1550 while ((my $pos = index($line, "\t")) != -1) {
1551 if (my $count = (8 - ($pos % 8))) {
1552 my $spaces = ' ' x
$count;
1553 $line =~ s/\t/$spaces/;
1560 sub project_in_list
{
1561 my $project = shift;
1562 my @list = git_get_projects_list
();
1563 return @list && scalar(grep { $_->{'path'} eq $project } @list);
1566 ## ----------------------------------------------------------------------
1567 ## HTML aware string manipulation
1569 # Try to chop given string on a word boundary between position
1570 # $len and $len+$add_len. If there is no word boundary there,
1571 # chop at $len+$add_len. Do not chop if chopped part plus ellipsis
1572 # (marking chopped part) would be longer than given string.
1576 my $add_len = shift || 10;
1577 my $where = shift || 'right'; # 'left' | 'center' | 'right'
1579 # Make sure perl knows it is utf8 encoded so we don't
1580 # cut in the middle of a utf8 multibyte char.
1581 $str = to_utf8
($str);
1583 # allow only $len chars, but don't cut a word if it would fit in $add_len
1584 # if it doesn't fit, cut it if it's still longer than the dots we would add
1585 # remove chopped character entities entirely
1587 # when chopping in the middle, distribute $len into left and right part
1588 # return early if chopping wouldn't make string shorter
1589 if ($where eq 'center') {
1590 return $str if ($len + 5 >= length($str)); # filler is length 5
1593 return $str if ($len + 4 >= length($str)); # filler is length 4
1596 # regexps: ending and beginning with word part up to $add_len
1597 my $endre = qr/.{$len}\w{0,$add_len}/;
1598 my $begre = qr/\w{0,$add_len}.{$len}/;
1600 if ($where eq 'left') {
1601 $str =~ m/^(.*?)($begre)$/;
1602 my ($lead, $body) = ($1, $2);
1603 if (length($lead) > 4) {
1606 return "$lead$body";
1608 } elsif ($where eq 'center') {
1609 $str =~ m/^($endre)(.*)$/;
1610 my ($left, $str) = ($1, $2);
1611 $str =~ m/^(.*?)($begre)$/;
1612 my ($mid, $right) = ($1, $2);
1613 if (length($mid) > 5) {
1616 return "$left$mid$right";
1619 $str =~ m/^($endre)(.*)$/;
1622 if (length($tail) > 4) {
1625 return "$body$tail";
1629 # takes the same arguments as chop_str, but also wraps a <span> around the
1630 # result with a title attribute if it does get chopped. Additionally, the
1631 # string is HTML-escaped.
1632 sub chop_and_escape_str
{
1635 my $chopped = chop_str
(@_);
1636 if ($chopped eq $str) {
1637 return esc_html
($chopped);
1639 $str =~ s/[[:cntrl:]]/?/g;
1640 return $cgi->span({-title
=>$str}, esc_html
($chopped));
1644 ## ----------------------------------------------------------------------
1645 ## functions returning short strings
1647 # CSS class for given age value (in seconds)
1651 if (!defined $age) {
1653 } elsif ($age < 60*60*2) {
1655 } elsif ($age < 60*60*24*2) {
1662 # convert age in seconds to "nn units ago" string
1667 if ($age > 60*60*24*365*2) {
1668 $age_str = (int $age/60/60/24/365);
1669 $age_str .= " years ago";
1670 } elsif ($age > 60*60*24*(365/12)*2) {
1671 $age_str = int $age/60/60/24/(365/12);
1672 $age_str .= " months ago";
1673 } elsif ($age > 60*60*24*7*2) {
1674 $age_str = int $age/60/60/24/7;
1675 $age_str .= " weeks ago";
1676 } elsif ($age > 60*60*24*2) {
1677 $age_str = int $age/60/60/24;
1678 $age_str .= " days ago";
1679 } elsif ($age > 60*60*2) {
1680 $age_str = int $age/60/60;
1681 $age_str .= " hours ago";
1682 } elsif ($age > 60*2) {
1683 $age_str = int $age/60;
1684 $age_str .= " min ago";
1685 } elsif ($age > 2) {
1686 $age_str = int $age;
1687 $age_str .= " sec ago";
1689 $age_str .= " right now";
1695 S_IFINVALID
=> 0030000,
1696 S_IFGITLINK
=> 0160000,
1699 # submodule/subproject, a commit object reference
1703 return (($mode & S_IFMT
) == S_IFGITLINK
)
1706 # convert file mode in octal to symbolic file mode string
1708 my $mode = oct shift;
1710 if (S_ISGITLINK
($mode)) {
1711 return 'm---------';
1712 } elsif (S_ISDIR
($mode & S_IFMT
)) {
1713 return 'drwxr-xr-x';
1714 } elsif (S_ISLNK
($mode)) {
1715 return 'lrwxrwxrwx';
1716 } elsif (S_ISREG
($mode)) {
1717 # git cares only about the executable bit
1718 if ($mode & S_IXUSR
) {
1719 return '-rwxr-xr-x';
1721 return '-rw-r--r--';
1724 return '----------';
1728 # convert file mode in octal to file type string
1732 if ($mode !~ m/^[0-7]+$/) {
1738 if (S_ISGITLINK
($mode)) {
1740 } elsif (S_ISDIR
($mode & S_IFMT
)) {
1742 } elsif (S_ISLNK
($mode)) {
1744 } elsif (S_ISREG
($mode)) {
1751 # convert file mode in octal to file type description string
1752 sub file_type_long
{
1755 if ($mode !~ m/^[0-7]+$/) {
1761 if (S_ISGITLINK
($mode)) {
1763 } elsif (S_ISDIR
($mode & S_IFMT
)) {
1765 } elsif (S_ISLNK
($mode)) {
1767 } elsif (S_ISREG
($mode)) {
1768 if ($mode & S_IXUSR
) {
1769 return "executable";
1779 ## ----------------------------------------------------------------------
1780 ## functions returning short HTML fragments, or transforming HTML fragments
1781 ## which don't belong to other sections
1783 # format line of commit message.
1784 sub format_log_line_html
{
1787 $line = esc_html
($line, -nbsp
=>1);
1788 $line =~ s
{\b([0-9a-fA-F
]{8,40})\b}{
1789 $cgi->a({-href
=> href
(action
=>"object", hash
=>$1),
1790 -class => "text"}, $1);
1796 # format marker of refs pointing to given object
1798 # the destination action is chosen based on object type and current context:
1799 # - for annotated tags, we choose the tag view unless it's the current view
1800 # already, in which case we go to shortlog view
1801 # - for other refs, we keep the current view if we're in history, shortlog or
1802 # log view, and select shortlog otherwise
1803 sub format_ref_marker
{
1804 my ($refs, $id) = @_;
1807 if (defined $refs->{$id}) {
1808 foreach my $ref (@{$refs->{$id}}) {
1809 # this code exploits the fact that non-lightweight tags are the
1810 # only indirect objects, and that they are the only objects for which
1811 # we want to use tag instead of shortlog as action
1812 my ($type, $name) = qw();
1813 my $indirect = ($ref =~ s/\^\{\}$//);
1814 # e.g. tags/v2.6.11 or heads/next
1815 if ($ref =~ m!^(.*?)s?/(.*)$!) {
1824 $class .= " indirect" if $indirect;
1826 my $dest_action = "shortlog";
1829 $dest_action = "tag" unless $action eq "tag";
1830 } elsif ($action =~ /^(history|(short)?log)$/) {
1831 $dest_action = $action;
1835 $dest .= "refs/" unless $ref =~ m
!^refs
/!;
1838 my $link = $cgi->a({
1840 action
=>$dest_action,
1844 $markers .= " <span class=\"".esc_attr
($class)."\" title=\"".esc_attr
($ref)."\">" .
1850 return ' <span class="refs">'. $markers . '</span>';
1856 # format, perhaps shortened and with markers, title line
1857 sub format_subject_html
{
1858 my ($long, $short, $href, $extra) = @_;
1859 $extra = '' unless defined($extra);
1861 if (length($short) < length($long)) {
1862 $long =~ s/[[:cntrl:]]/?/g;
1863 return $cgi->a({-href
=> $href, -class => "list subject",
1864 -title
=> to_utf8
($long)},
1865 esc_html
($short)) . $extra;
1867 return $cgi->a({-href
=> $href, -class => "list subject"},
1868 esc_html
($long)) . $extra;
1872 # Rather than recomputing the url for an email multiple times, we cache it
1873 # after the first hit. This gives a visible benefit in views where the avatar
1874 # for the same email is used repeatedly (e.g. shortlog).
1875 # The cache is shared by all avatar engines (currently gravatar only), which
1876 # are free to use it as preferred. Since only one avatar engine is used for any
1877 # given page, there's no risk for cache conflicts.
1878 our %avatar_cache = ();
1880 # Compute the picon url for a given email, by using the picon search service over at
1881 # http://www.cs.indiana.edu/picons/search.html
1883 my $email = lc shift;
1884 if (!$avatar_cache{$email}) {
1885 my ($user, $domain) = split('@', $email);
1886 $avatar_cache{$email} =
1887 "http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/" .
1889 "users+domains+unknown/up/single";
1891 return $avatar_cache{$email};
1894 # Compute the gravatar url for a given email, if it's not in the cache already.
1895 # Gravatar stores only the part of the URL before the size, since that's the
1896 # one computationally more expensive. This also allows reuse of the cache for
1897 # different sizes (for this particular engine).
1899 my $email = lc shift;
1901 $avatar_cache{$email} ||=
1902 "http://www.gravatar.com/avatar/" .
1903 Digest
::MD5
::md5_hex
($email) . "?s=";
1904 return $avatar_cache{$email} . $size;
1907 # Insert an avatar for the given $email at the given $size if the feature
1909 sub git_get_avatar
{
1910 my ($email, %opts) = @_;
1911 my $pre_white = ($opts{-pad_before
} ? " " : "");
1912 my $post_white = ($opts{-pad_after
} ? " " : "");
1913 $opts{-size
} ||= 'default';
1914 my $size = $avatar_size{$opts{-size
}} || $avatar_size{'default'};
1916 if ($git_avatar eq 'gravatar') {
1917 $url = gravatar_url
($email, $size);
1918 } elsif ($git_avatar eq 'picon') {
1919 $url = picon_url
($email);
1921 # Other providers can be added by extending the if chain, defining $url
1922 # as needed. If no variant puts something in $url, we assume avatars
1923 # are completely disabled/unavailable.
1926 "<img width=\"$size\" " .
1927 "class=\"avatar\" " .
1928 "src=\"".esc_url
($url)."\" " .
1936 sub format_search_author
{
1937 my ($author, $searchtype, $displaytext) = @_;
1938 my $have_search = gitweb_check_feature
('search');
1942 if ($searchtype eq 'author') {
1943 $performed = "authored";
1944 } elsif ($searchtype eq 'committer') {
1945 $performed = "committed";
1948 return $cgi->a({-href
=> href
(action
=>"search", hash
=>$hash,
1949 searchtext
=>$author,
1950 searchtype
=>$searchtype), class=>"list",
1951 title
=>"Search for commits $performed by $author"},
1955 return $displaytext;
1959 # format the author name of the given commit with the given tag
1960 # the author name is chopped and escaped according to the other
1961 # optional parameters (see chop_str).
1962 sub format_author_html
{
1965 my $author = chop_and_escape_str
($co->{'author_name'}, @_);
1966 return "<$tag class=\"author\">" .
1967 format_search_author
($co->{'author_name'}, "author",
1968 git_get_avatar
($co->{'author_email'}, -pad_after
=> 1) .
1973 # format git diff header line, i.e. "diff --(git|combined|cc) ..."
1974 sub format_git_diff_header_line
{
1976 my $diffinfo = shift;
1977 my ($from, $to) = @_;
1979 if ($diffinfo->{'nparents'}) {
1981 $line =~ s!^(diff (.*?) )"?.*$!$1!;
1982 if ($to->{'href'}) {
1983 $line .= $cgi->a({-href
=> $to->{'href'}, -class => "path"},
1984 esc_path
($to->{'file'}));
1985 } else { # file was deleted (no href)
1986 $line .= esc_path
($to->{'file'});
1990 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
1991 if ($from->{'href'}) {
1992 $line .= $cgi->a({-href
=> $from->{'href'}, -class => "path"},
1993 'a/' . esc_path
($from->{'file'}));
1994 } else { # file was added (no href)
1995 $line .= 'a/' . esc_path
($from->{'file'});
1998 if ($to->{'href'}) {
1999 $line .= $cgi->a({-href
=> $to->{'href'}, -class => "path"},
2000 'b/' . esc_path
($to->{'file'}));
2001 } else { # file was deleted
2002 $line .= 'b/' . esc_path
($to->{'file'});
2006 return "<div class=\"diff header\">$line</div>\n";
2009 # format extended diff header line, before patch itself
2010 sub format_extended_diff_header_line
{
2012 my $diffinfo = shift;
2013 my ($from, $to) = @_;
2016 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
2017 $line .= $cgi->a({-href
=>$from->{'href'}, -class=>"path"},
2018 esc_path
($from->{'file'}));
2020 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
2021 $line .= $cgi->a({-href
=>$to->{'href'}, -class=>"path"},
2022 esc_path
($to->{'file'}));
2024 # match single <mode>
2025 if ($line =~ m/\s(\d{6})$/) {
2026 $line .= '<span class="info"> (' .
2027 file_type_long
($1) .
2031 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
2032 # can match only for combined diff
2034 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2035 if ($from->{'href'}[$i]) {
2036 $line .= $cgi->a({-href
=>$from->{'href'}[$i],
2038 substr($diffinfo->{'from_id'}[$i],0,7));
2043 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
2046 if ($to->{'href'}) {
2047 $line .= $cgi->a({-href
=>$to->{'href'}, -class=>"hash"},
2048 substr($diffinfo->{'to_id'},0,7));
2053 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
2054 # can match only for ordinary diff
2055 my ($from_link, $to_link);
2056 if ($from->{'href'}) {
2057 $from_link = $cgi->a({-href
=>$from->{'href'}, -class=>"hash"},
2058 substr($diffinfo->{'from_id'},0,7));
2060 $from_link = '0' x
7;
2062 if ($to->{'href'}) {
2063 $to_link = $cgi->a({-href
=>$to->{'href'}, -class=>"hash"},
2064 substr($diffinfo->{'to_id'},0,7));
2068 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
2069 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
2072 return $line . "<br/>\n";
2075 # format from-file/to-file diff header
2076 sub format_diff_from_to_header
{
2077 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
2082 #assert($line =~ m/^---/) if DEBUG;
2083 # no extra formatting for "^--- /dev/null"
2084 if (! $diffinfo->{'nparents'}) {
2085 # ordinary (single parent) diff
2086 if ($line =~ m!^--- "?a/!) {
2087 if ($from->{'href'}) {
2089 $cgi->a({-href
=>$from->{'href'}, -class=>"path"},
2090 esc_path
($from->{'file'}));
2093 esc_path
($from->{'file'});
2096 $result .= qq
!<div
class="diff from_file">$line</div
>\n!;
2099 # combined diff (merge commit)
2100 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2101 if ($from->{'href'}[$i]) {
2103 $cgi->a({-href
=>href
(action
=>"blobdiff",
2104 hash_parent
=>$diffinfo->{'from_id'}[$i],
2105 hash_parent_base
=>$parents[$i],
2106 file_parent
=>$from->{'file'}[$i],
2107 hash
=>$diffinfo->{'to_id'},
2109 file_name
=>$to->{'file'}),
2111 -title
=>"diff" . ($i+1)},
2114 $cgi->a({-href
=>$from->{'href'}[$i], -class=>"path"},
2115 esc_path
($from->{'file'}[$i]));
2117 $line = '--- /dev/null';
2119 $result .= qq
!<div
class="diff from_file">$line</div
>\n!;
2124 #assert($line =~ m/^\+\+\+/) if DEBUG;
2125 # no extra formatting for "^+++ /dev/null"
2126 if ($line =~ m!^\+\+\+ "?b/!) {
2127 if ($to->{'href'}) {
2129 $cgi->a({-href
=>$to->{'href'}, -class=>"path"},
2130 esc_path
($to->{'file'}));
2133 esc_path
($to->{'file'});
2136 $result .= qq
!<div
class="diff to_file">$line</div
>\n!;
2141 # create note for patch simplified by combined diff
2142 sub format_diff_cc_simplified
{
2143 my ($diffinfo, @parents) = @_;
2146 $result .= "<div class=\"diff header\">" .
2148 if (!is_deleted
($diffinfo)) {
2149 $result .= $cgi->a({-href
=> href
(action
=>"blob",
2151 hash
=>$diffinfo->{'to_id'},
2152 file_name
=>$diffinfo->{'to_file'}),
2154 esc_path
($diffinfo->{'to_file'}));
2156 $result .= esc_path
($diffinfo->{'to_file'});
2158 $result .= "</div>\n" . # class="diff header"
2159 "<div class=\"diff nodifferences\">" .
2161 "</div>\n"; # class="diff nodifferences"
2166 # format patch (diff) line (not to be used for diff headers)
2167 sub format_diff_line
{
2169 my ($from, $to) = @_;
2170 my $diff_class = "";
2174 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
2176 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
2177 if ($line =~ m/^\@{3}/) {
2178 $diff_class = " chunk_header";
2179 } elsif ($line =~ m/^\\/) {
2180 $diff_class = " incomplete";
2181 } elsif ($prefix =~ tr/+/+/) {
2182 $diff_class = " add";
2183 } elsif ($prefix =~ tr/-/-/) {
2184 $diff_class = " rem";
2187 # assume ordinary diff
2188 my $char = substr($line, 0, 1);
2190 $diff_class = " add";
2191 } elsif ($char eq '-') {
2192 $diff_class = " rem";
2193 } elsif ($char eq '@') {
2194 $diff_class = " chunk_header";
2195 } elsif ($char eq "\\") {
2196 $diff_class = " incomplete";
2199 $line = untabify
($line);
2200 if ($from && $to && $line =~ m/^\@{2} /) {
2201 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
2202 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
2204 $from_lines = 0 unless defined $from_lines;
2205 $to_lines = 0 unless defined $to_lines;
2207 if ($from->{'href'}) {
2208 $from_text = $cgi->a({-href
=>"$from->{'href'}#l$from_start",
2209 -class=>"list"}, $from_text);
2211 if ($to->{'href'}) {
2212 $to_text = $cgi->a({-href
=>"$to->{'href'}#l$to_start",
2213 -class=>"list"}, $to_text);
2215 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
2216 "<span class=\"section\">" . esc_html
($section, -nbsp
=>1) . "</span>";
2217 return "<div class=\"diff$diff_class\">$line</div>\n";
2218 } elsif ($from && $to && $line =~ m/^\@{3}/) {
2219 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
2220 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
2222 @from_text = split(' ', $ranges);
2223 for (my $i = 0; $i < @from_text; ++$i) {
2224 ($from_start[$i], $from_nlines[$i]) =
2225 (split(',', substr($from_text[$i], 1)), 0);
2228 $to_text = pop @from_text;
2229 $to_start = pop @from_start;
2230 $to_nlines = pop @from_nlines;
2232 $line = "<span class=\"chunk_info\">$prefix ";
2233 for (my $i = 0; $i < @from_text; ++$i) {
2234 if ($from->{'href'}[$i]) {
2235 $line .= $cgi->a({-href
=>"$from->{'href'}[$i]#l$from_start[$i]",
2236 -class=>"list"}, $from_text[$i]);
2238 $line .= $from_text[$i];
2242 if ($to->{'href'}) {
2243 $line .= $cgi->a({-href
=>"$to->{'href'}#l$to_start",
2244 -class=>"list"}, $to_text);
2248 $line .= " $prefix</span>" .
2249 "<span class=\"section\">" . esc_html
($section, -nbsp
=>1) . "</span>";
2250 return "<div class=\"diff$diff_class\">$line</div>\n";
2252 return "<div class=\"diff$diff_class\">" . esc_html
($line, -nbsp
=>1) . "</div>\n";
2255 # Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
2256 # linked. Pass the hash of the tree/commit to snapshot.
2257 sub format_snapshot_links
{
2259 my $num_fmts = @snapshot_fmts;
2260 if ($num_fmts > 1) {
2261 # A parenthesized list of links bearing format names.
2262 # e.g. "snapshot (_tar.gz_ _zip_)"
2263 return "snapshot (" . join(' ', map
2270 }, $known_snapshot_formats{$_}{'display'})
2271 , @snapshot_fmts) . ")";
2272 } elsif ($num_fmts == 1) {
2273 # A single "snapshot" link whose tooltip bears the format name.
2275 my ($fmt) = @snapshot_fmts;
2281 snapshot_format
=>$fmt
2283 -title
=> "in format: $known_snapshot_formats{$fmt}{'display'}"
2285 } else { # $num_fmts == 0
2290 ## ......................................................................
2291 ## functions returning values to be passed, perhaps after some
2292 ## transformation, to other functions; e.g. returning arguments to href()
2294 # returns hash to be passed to href to generate gitweb URL
2295 # in -title key it returns description of link
2297 my $format = shift || 'Atom';
2298 my %res = (action
=> lc($format));
2300 # feed links are possible only for project views
2301 return unless (defined $project);
2302 # some views should link to OPML, or to generic project feed,
2303 # or don't have specific feed yet (so they should use generic)
2304 return if ($action =~ /^(?:tags|heads|forks|tag|search)$/x);
2307 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
2308 # from tag links; this also makes possible to detect branch links
2309 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
2310 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
2313 # find log type for feed description (title)
2315 if (defined $file_name) {
2316 $type = "history of $file_name";
2317 $type .= "/" if ($action eq 'tree');
2318 $type .= " on '$branch'" if (defined $branch);
2320 $type = "log of $branch" if (defined $branch);
2323 $res{-title
} = $type;
2324 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
2325 $res{'file_name'} = $file_name;
2330 ## ----------------------------------------------------------------------
2331 ## git utility subroutines, invoking git commands
2333 # returns path to the core git executable and the --git-dir parameter as list
2335 $number_of_git_cmds++;
2336 return $GIT, '--git-dir='.$git_dir;
2339 # quote the given arguments for passing them to the shell
2340 # quote_command("command", "arg 1", "arg with ' and ! characters")
2341 # => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
2342 # Try to avoid using this function wherever possible.
2345 map { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ );
2348 # get HEAD ref of given project as hash
2349 sub git_get_head_hash
{
2350 return git_get_full_hash
(shift, 'HEAD');
2353 sub git_get_full_hash
{
2354 return git_get_hash
(@_);
2357 sub git_get_short_hash
{
2358 return git_get_hash
(@_, '--short=7');
2362 my ($project, $hash, @options) = @_;
2363 my $o_git_dir = $git_dir;
2365 $git_dir = "$projectroot/$project";
2366 if (open my $fd, '-|', git_cmd
(), 'rev-parse',
2367 '--verify', '-q', @options, $hash) {
2369 chomp $retval if defined $retval;
2372 if (defined $o_git_dir) {
2373 $git_dir = $o_git_dir;
2378 # get type of given object
2382 open my $fd, "-|", git_cmd
(), "cat-file", '-t', $hash or return;
2384 close $fd or return;
2389 # repository configuration
2390 our $config_file = '';
2393 # store multiple values for single key as anonymous array reference
2394 # single values stored directly in the hash, not as [ <value> ]
2395 sub hash_set_multi
{
2396 my ($hash, $key, $value) = @_;
2398 if (!exists $hash->{$key}) {
2399 $hash->{$key} = $value;
2400 } elsif (!ref $hash->{$key}) {
2401 $hash->{$key} = [ $hash->{$key}, $value ];
2403 push @{$hash->{$key}}, $value;
2407 # return hash of git project configuration
2408 # optionally limited to some section, e.g. 'gitweb'
2409 sub git_parse_project_config
{
2410 my $section_regexp = shift;
2415 open my $fh, "-|", git_cmd
(), "config", '-z', '-l',
2418 while (my $keyval = <$fh>) {
2420 my ($key, $value) = split(/\n/, $keyval, 2);
2422 hash_set_multi
(\
%config, $key, $value)
2423 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
2430 # convert config value to boolean: 'true' or 'false'
2431 # no value, number > 0, 'true' and 'yes' values are true
2432 # rest of values are treated as false (never as error)
2433 sub config_to_bool
{
2436 return 1 if !defined $val; # section.key
2438 # strip leading and trailing whitespace
2442 return (($val =~ /^\d+$/ && $val) || # section.key = 1
2443 ($val =~ /^(?:true|yes)$/i)); # section.key = true
2446 # convert config value to simple decimal number
2447 # an optional value suffix of 'k', 'm', or 'g' will cause the value
2448 # to be multiplied by 1024, 1048576, or 1073741824
2452 # strip leading and trailing whitespace
2456 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
2458 # unknown unit is treated as 1
2459 return $num * ($unit eq 'g' ? 1073741824 :
2460 $unit eq 'm' ? 1048576 :
2461 $unit eq 'k' ? 1024 : 1);
2466 # convert config value to array reference, if needed
2467 sub config_to_multi
{
2470 return ref($val) ? $val : (defined($val) ? [ $val ] : []);
2473 sub git_get_project_config
{
2474 my ($key, $type) = @_;
2476 return unless defined $git_dir;
2479 return unless ($key);
2480 $key =~ s/^gitweb\.//;
2481 return if ($key =~ m/\W/);
2484 if (defined $type) {
2487 unless ($type eq 'bool' || $type eq 'int');
2491 if (!defined $config_file ||
2492 $config_file ne "$git_dir/config") {
2493 %config = git_parse_project_config
('gitweb');
2494 $config_file = "$git_dir/config";
2497 # check if config variable (key) exists
2498 return unless exists $config{"gitweb.$key"};
2501 if (!defined $type) {
2502 return $config{"gitweb.$key"};
2503 } elsif ($type eq 'bool') {
2504 # backward compatibility: 'git config --bool' returns true/false
2505 return config_to_bool
($config{"gitweb.$key"}) ? 'true' : 'false';
2506 } elsif ($type eq 'int') {
2507 return config_to_int
($config{"gitweb.$key"});
2509 return $config{"gitweb.$key"};
2512 # get hash of given path at given ref
2513 sub git_get_hash_by_path
{
2515 my $path = shift || return undef;
2520 open my $fd, "-|", git_cmd
(), "ls-tree", $base, "--", $path
2521 or die_error
(500, "Open git-ls-tree failed");
2523 close $fd or return undef;
2525 if (!defined $line) {
2526 # there is no tree or hash given by $path at $base
2530 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2531 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
2532 if (defined $type && $type ne $2) {
2533 # type doesn't match
2539 # get path of entry with given hash at given tree-ish (ref)
2540 # used to get 'from' filename for combined diff (merge commit) for renames
2541 sub git_get_path_by_hash
{
2542 my $base = shift || return;
2543 my $hash = shift || return;
2547 open my $fd, "-|", git_cmd
(), "ls-tree", '-r', '-t', '-z', $base
2549 while (my $line = <$fd>) {
2552 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
2553 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
2554 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
2563 ## ......................................................................
2564 ## git utility functions, directly accessing git repository
2566 # get the value of config variable either from file named as the variable
2567 # itself in the repository ($GIT_DIR/$name file), or from gitweb.$name
2568 # configuration variable in the repository config file.
2569 sub git_get_file_or_project_config
{
2570 my ($path, $name) = @_;
2572 $git_dir = "$projectroot/$path";
2573 open my $fd, '<', "$git_dir/$name"
2574 or return git_get_project_config
($name);
2577 if (defined $conf) {
2583 sub git_get_project_description
{
2585 return git_get_file_or_project_config
($path, 'description');
2588 # supported formats:
2589 # * $GIT_DIR/ctags/<tagname> file (in 'ctags' subdirectory)
2590 # - if its contents is a number, use it as tag weight,
2591 # - otherwise add a tag with weight 1
2592 # * $GIT_DIR/ctags file, each line is a tag (with weight 1)
2593 # the same value multiple times increases tag weight
2594 # * `gitweb.ctag' multi-valued repo config variable
2595 sub git_get_project_ctags
{
2596 my $project = shift;
2599 $git_dir = "$projectroot/$project";
2600 if (opendir my $dh, "$git_dir/ctags") {
2601 my @files = grep { -f
$_ } map { "$git_dir/ctags/$_" } readdir($dh);
2602 foreach my $tagfile (@files) {
2603 open my $ct, '<', $tagfile
2609 (my $ctag = $tagfile) =~ s
#.*/##;
2610 if ($val =~ /\d+/) {
2611 $ctags->{$ctag} = $val;
2613 $ctags->{$ctag} = 1;
2618 } elsif (open my $fh, '<', "$git_dir/ctags") {
2619 while (my $line = <$fh>) {
2621 $ctags->{$line}++ if $line;
2626 my $taglist = config_to_multi
(git_get_project_config
('ctag'));
2627 foreach my $tag (@$taglist) {
2635 # return hash, where keys are content tags ('ctags'),
2636 # and values are sum of weights of given tag in every project
2637 sub git_gather_all_ctags
{
2638 my $projects = shift;
2641 foreach my $p (@$projects) {
2642 foreach my $ct (keys %{$p->{'ctags'}}) {
2643 $ctags->{$ct} += $p->{'ctags'}->{$ct};
2650 sub git_populate_project_tagcloud
{
2653 # First, merge different-cased tags; tags vote on casing
2655 foreach (keys %$ctags) {
2656 $ctags_lc{lc $_}->{count
} += $ctags->{$_};
2657 if (not $ctags_lc{lc $_}->{topcount
}
2658 or $ctags_lc{lc $_}->{topcount
} < $ctags->{$_}) {
2659 $ctags_lc{lc $_}->{topcount
} = $ctags->{$_};
2660 $ctags_lc{lc $_}->{topname
} = $_;
2665 if (eval { require HTML
::TagCloud
; 1; }) {
2666 $cloud = HTML
::TagCloud-
>new;
2667 foreach my $ctag (sort keys %ctags_lc) {
2668 # Pad the title with spaces so that the cloud looks
2670 my $title = esc_html
($ctags_lc{$ctag}->{topname
});
2671 $title =~ s/ / /g;
2672 $title =~ s/^/ /g;
2673 $title =~ s/$/ /g;
2674 $cloud->add($title, href
(project
=>undef, ctag
=>$ctag),
2675 $ctags_lc{$ctag}->{count
});
2679 foreach my $ctag (keys %ctags_lc) {
2680 my $title = $ctags_lc{$ctag}->{topname
};
2681 $cloud->{$ctag}{count
} = $ctags_lc{$ctag}->{count
};
2682 $cloud->{$ctag}{ctag
} =
2683 $cgi->a({-href
=>href
(project
=>undef, ctag
=>$ctag)},
2684 esc_html
($title, -nbsp
=>1));
2690 sub git_show_project_tagcloud
{
2691 my ($cloud, $count) = @_;
2692 if (ref $cloud eq 'HTML::TagCloud') {
2693 return $cloud->html_and_css($count);
2695 my @tags = sort { $cloud->{$a}->{'count'} <=> $cloud->{$b}->{'count'} } keys %$cloud;
2697 '<div id="htmltagcloud"'.($project ? '' : ' align="center"').'>' .
2699 $cloud->{$_}->{'ctag'}
2700 } splice(@tags, 0, $count)) .
2705 sub git_get_project_url_list
{
2708 $git_dir = "$projectroot/$path";
2709 open my $fd, '<', "$git_dir/cloneurl"
2710 or return wantarray ?
2711 @{ config_to_multi
(git_get_project_config
('url')) } :
2712 config_to_multi
(git_get_project_config
('url'));
2713 my @git_project_url_list = map { chomp; $_ } <$fd>;
2716 return wantarray ? @git_project_url_list : \
@git_project_url_list;
2719 sub git_get_projects_list
{
2724 $filter =~ s/\.git$//;
2726 my $check_forks = gitweb_check_feature
('forks');
2728 if (-d
$projects_list) {
2729 # search in directory
2730 my $dir = $projects_list . ($filter ? "/$filter" : '');
2731 # remove the trailing "/"
2733 my $pfxlen = length("$dir");
2734 my $pfxdepth = ($dir =~ tr!/!!);
2737 follow_fast
=> 1, # follow symbolic links
2738 follow_skip
=> 2, # ignore duplicates
2739 dangling_symlinks
=> 0, # ignore dangling symlinks, silently
2742 our $project_maxdepth;
2744 # skip project-list toplevel, if we get it.
2745 return if (m!^[/.]$!);
2746 # only directories can be git repositories
2747 return unless (-d
$_);
2748 # don't traverse too deep (Find is super slow on os x)
2749 if (($File::Find
::name
=~ tr!/!!) - $pfxdepth > $project_maxdepth) {
2750 $File::Find
::prune
= 1;
2754 my $subdir = substr($File::Find
::name
, $pfxlen + 1);
2755 # we check related file in $projectroot
2756 my $path = ($filter ? "$filter/" : '') . $subdir;
2757 if (check_export_ok
("$projectroot/$path")) {
2758 push @list, { path
=> $path };
2759 $File::Find
::prune
= 1;
2764 } elsif (-f
$projects_list) {
2765 # read from file(url-encoded):
2766 # 'git%2Fgit.git Linus+Torvalds'
2767 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2768 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2770 open my $fd, '<', $projects_list or return;
2772 while (my $line = <$fd>) {
2774 my ($path, $owner) = split ' ', $line;
2775 $path = unescape
($path);
2776 $owner = unescape
($owner);
2777 if (!defined $path) {
2780 if ($filter ne '') {
2781 # looking for forks;
2782 my $pfx = substr($path, 0, length($filter));
2783 if ($pfx ne $filter) {
2786 my $sfx = substr($path, length($filter));
2787 if ($sfx !~ /^\/.*\
.git
$/) {
2790 } elsif ($check_forks) {
2792 foreach my $filter (keys %paths) {
2793 # looking for forks;
2794 my $pfx = substr($path, 0, length($filter));
2795 if ($pfx ne $filter) {
2798 my $sfx = substr($path, length($filter));
2799 if ($sfx !~ /^\/.*\
.git
$/) {
2802 # is a fork, don't include it in
2807 if (check_export_ok
("$projectroot/$path")) {
2810 owner
=> to_utf8
($owner),
2813 (my $forks_path = $path) =~ s/\.git$//;
2814 $paths{$forks_path}++;
2822 our $gitweb_project_owner = undef;
2823 sub git_get_project_list_from_file
{
2825 return if (defined $gitweb_project_owner);
2827 $gitweb_project_owner = {};
2828 # read from file (url-encoded):
2829 # 'git%2Fgit.git Linus+Torvalds'
2830 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2831 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2832 if (-f
$projects_list) {
2833 open(my $fd, '<', $projects_list);
2834 while (my $line = <$fd>) {
2836 my ($pr, $ow) = split ' ', $line;
2837 $pr = unescape
($pr);
2838 $ow = unescape
($ow);
2839 $gitweb_project_owner->{$pr} = to_utf8
($ow);
2845 sub git_get_project_owner
{
2846 my $project = shift;
2849 return undef unless $project;
2850 $git_dir = "$projectroot/$project";
2852 if (!defined $gitweb_project_owner) {
2853 git_get_project_list_from_file
();
2856 if (exists $gitweb_project_owner->{$project}) {
2857 $owner = $gitweb_project_owner->{$project};
2859 if (!defined $owner){
2860 $owner = git_get_project_config
('owner');
2862 if (!defined $owner) {
2863 $owner = get_file_owner
("$git_dir");
2869 sub git_get_last_activity
{
2873 $git_dir = "$projectroot/$path";
2874 open($fd, "-|", git_cmd
(), 'for-each-ref',
2875 '--format=%(committer)',
2876 '--sort=-committerdate',
2878 'refs/heads') or return;
2879 my $most_recent = <$fd>;
2880 close $fd or return;
2881 if (defined $most_recent &&
2882 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
2884 my $age = time - $timestamp;
2885 return ($age, age_string
($age));
2887 return (undef, undef);
2890 # Implementation note: when a single remote is wanted, we cannot use 'git
2891 # remote show -n' because that command always work (assuming it's a remote URL
2892 # if it's not defined), and we cannot use 'git remote show' because that would
2893 # try to make a network roundtrip. So the only way to find if that particular
2894 # remote is defined is to walk the list provided by 'git remote -v' and stop if
2895 # and when we find what we want.
2896 sub git_get_remotes_list
{
2900 open my $fd, '-|' , git_cmd
(), 'remote', '-v';
2902 while (my $remote = <$fd>) {
2904 $remote =~ s!\t(.*?)\s+\((\w+)\)$!!;
2905 next if $wanted and not $remote eq $wanted;
2906 my ($url, $key) = ($1, $2);
2908 $remotes{$remote} ||= { 'heads' => () };
2909 $remotes{$remote}{$key} = $url;
2911 close $fd or return;
2912 return wantarray ? %remotes : \
%remotes;
2915 # Takes a hash of remotes as first parameter and fills it by adding the
2916 # available remote heads for each of the indicated remotes.
2917 sub fill_remote_heads
{
2918 my $remotes = shift;
2919 my @heads = map { "remotes/$_" } keys %$remotes;
2920 my @remoteheads = git_get_heads_list
(undef, @heads);
2921 foreach my $remote (keys %$remotes) {
2922 $remotes->{$remote}{'heads'} = [ grep {
2923 $_->{'name'} =~ s!^$remote/!!
2928 sub git_get_references
{
2929 my $type = shift || "";
2931 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
2932 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
2933 open my $fd, "-|", git_cmd
(), "show-ref", "--dereference",
2934 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
2937 while (my $line = <$fd>) {
2939 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {
2940 if (defined $refs{$1}) {
2941 push @{$refs{$1}}, $2;
2947 close $fd or return;
2951 sub git_get_rev_name_tags
{
2952 my $hash = shift || return undef;
2954 open my $fd, "-|", git_cmd
(), "name-rev", "--tags", $hash
2956 my $name_rev = <$fd>;
2959 if ($name_rev =~ m
|^$hash tags
/(.*)$|) {
2962 # catches also '$hash undefined' output
2967 ## ----------------------------------------------------------------------
2968 ## parse to hash functions
2972 my $tz = shift || "-0000";
2975 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
2976 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
2977 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
2978 $date{'hour'} = $hour;
2979 $date{'minute'} = $min;
2980 $date{'mday'} = $mday;
2981 $date{'day'} = $days[$wday];
2982 $date{'month'} = $months[$mon];
2983 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
2984 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
2985 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
2986 $mday, $months[$mon], $hour ,$min;
2987 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
2988 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
2990 my ($tz_sign, $tz_hour, $tz_min) =
2991 ($tz =~ m/^([-+])(\d\d)(\d\d)$/);
2992 $tz_sign = ($tz_sign eq '-' ? -1 : +1);
2993 my $local = $epoch + $tz_sign*((($tz_hour*60) + $tz_min)*60);
2994 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
2995 $date{'hour_local'} = $hour;
2996 $date{'minute_local'} = $min;
2997 $date{'tz_local'} = $tz;
2998 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
2999 1900+$year, $mon+1, $mday,
3000 $hour, $min, $sec, $tz);
3009 open my $fd, "-|", git_cmd
(), "cat-file", "tag", $tag_id or return;
3010 $tag{'id'} = $tag_id;
3011 while (my $line = <$fd>) {
3013 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
3014 $tag{'object'} = $1;
3015 } elsif ($line =~ m/^type (.+)$/) {
3017 } elsif ($line =~ m/^tag (.+)$/) {
3019 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
3020 $tag{'author'} = $1;
3021 $tag{'author_epoch'} = $2;
3022 $tag{'author_tz'} = $3;
3023 if ($tag{'author'} =~ m/^([^<]+) <([^>]*)>/) {
3024 $tag{'author_name'} = $1;
3025 $tag{'author_email'} = $2;
3027 $tag{'author_name'} = $tag{'author'};
3029 } elsif ($line =~ m/--BEGIN/) {
3030 push @comment, $line;
3032 } elsif ($line eq "") {
3036 push @comment, <$fd>;
3037 $tag{'comment'} = \
@comment;
3038 close $fd or return;
3039 if (!defined $tag{'name'}) {
3045 sub parse_commit_text
{
3046 my ($commit_text, $withparents) = @_;
3047 my @commit_lines = split '\n', $commit_text;
3050 pop @commit_lines; # Remove '\0'
3052 if (! @commit_lines) {
3056 my $header = shift @commit_lines;
3057 if ($header !~ m/^[0-9a-fA-F]{40}/) {
3060 ($co{'id'}, my @parents) = split ' ', $header;
3061 while (my $line = shift @commit_lines) {
3062 last if $line eq "\n";
3063 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
3065 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
3067 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
3068 $co{'author'} = to_utf8
($1);
3069 $co{'author_epoch'} = $2;
3070 $co{'author_tz'} = $3;
3071 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
3072 $co{'author_name'} = $1;
3073 $co{'author_email'} = $2;
3075 $co{'author_name'} = $co{'author'};
3077 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
3078 $co{'committer'} = to_utf8
($1);
3079 $co{'committer_epoch'} = $2;
3080 $co{'committer_tz'} = $3;
3081 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
3082 $co{'committer_name'} = $1;
3083 $co{'committer_email'} = $2;
3085 $co{'committer_name'} = $co{'committer'};
3089 if (!defined $co{'tree'}) {
3092 $co{'parents'} = \
@parents;
3093 $co{'parent'} = $parents[0];
3095 foreach my $title (@commit_lines) {
3098 $co{'title'} = chop_str
($title, 80, 5);
3099 # remove leading stuff of merges to make the interesting part visible
3100 if (length($title) > 50) {
3101 $title =~ s/^Automatic //;
3102 $title =~ s/^merge (of|with) /Merge ... /i;
3103 if (length($title) > 50) {
3104 $title =~ s/(http|rsync):\/\///;
3106 if (length($title) > 50) {
3107 $title =~ s/(master|www|rsync)\.//;
3109 if (length($title) > 50) {
3110 $title =~ s/kernel.org:?//;
3112 if (length($title) > 50) {
3113 $title =~ s/\/pub\/scm//;
3116 $co{'title_short'} = chop_str
($title, 50, 5);
3120 if (! defined $co{'title'} || $co{'title'} eq "") {
3121 $co{'title'} = $co{'title_short'} = '(no commit message)';
3123 # remove added spaces
3124 foreach my $line (@commit_lines) {
3127 $co{'comment'} = \
@commit_lines;
3129 my $age = time - $co{'committer_epoch'};
3131 $co{'age_string'} = age_string
($age);
3132 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
3133 if ($age > 60*60*24*7*2) {
3134 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
3135 $co{'age_string_age'} = $co{'age_string'};
3137 $co{'age_string_date'} = $co{'age_string'};
3138 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
3144 my ($commit_id) = @_;
3149 open my $fd, "-|", git_cmd
(), "rev-list",
3155 or die_error
(500, "Open git-rev-list failed");
3156 %co = parse_commit_text
(<$fd>, 1);
3163 my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
3171 open my $fd, "-|", git_cmd
(), "rev-list",
3174 ("--max-count=" . $maxcount),
3175 ("--skip=" . $skip),
3179 ($filename ? ($filename) : ())
3180 or die_error
(500, "Open git-rev-list failed");
3181 while (my $line = <$fd>) {
3182 my %co = parse_commit_text
($line);
3187 return wantarray ? @cos : \
@cos;
3190 # parse line of git-diff-tree "raw" output
3191 sub parse_difftree_raw_line
{
3195 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
3196 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
3197 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
3198 $res{'from_mode'} = $1;
3199 $res{'to_mode'} = $2;
3200 $res{'from_id'} = $3;
3202 $res{'status'} = $5;
3203 $res{'similarity'} = $6;
3204 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
3205 ($res{'from_file'}, $res{'to_file'}) = map { unquote
($_) } split("\t", $7);
3207 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote
($7);
3210 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
3211 # combined diff (for merge commit)
3212 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
3213 $res{'nparents'} = length($1);
3214 $res{'from_mode'} = [ split(' ', $2) ];
3215 $res{'to_mode'} = pop @{$res{'from_mode'}};
3216 $res{'from_id'} = [ split(' ', $3) ];
3217 $res{'to_id'} = pop @{$res{'from_id'}};
3218 $res{'status'} = [ split('', $4) ];
3219 $res{'to_file'} = unquote
($5);
3221 # 'c512b523472485aef4fff9e57b229d9d243c967f'
3222 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
3223 $res{'commit'} = $1;
3226 return wantarray ? %res : \
%res;
3229 # wrapper: return parsed line of git-diff-tree "raw" output
3230 # (the argument might be raw line, or parsed info)
3231 sub parsed_difftree_line
{
3232 my $line_or_ref = shift;
3234 if (ref($line_or_ref) eq "HASH") {
3235 # pre-parsed (or generated by hand)
3236 return $line_or_ref;
3238 return parse_difftree_raw_line
($line_or_ref);
3242 # parse line of git-ls-tree output
3243 sub parse_ls_tree_line
{
3249 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'
3250 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;
3259 $res{'name'} = unquote
($5);
3262 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
3263 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
3271 $res{'name'} = unquote
($4);
3275 return wantarray ? %res : \
%res;
3278 # generates _two_ hashes, references to which are passed as 2 and 3 argument
3279 sub parse_from_to_diffinfo
{
3280 my ($diffinfo, $from, $to, @parents) = @_;
3282 if ($diffinfo->{'nparents'}) {
3284 $from->{'file'} = [];
3285 $from->{'href'} = [];
3286 fill_from_file_info
($diffinfo, @parents)
3287 unless exists $diffinfo->{'from_file'};
3288 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
3289 $from->{'file'}[$i] =
3290 defined $diffinfo->{'from_file'}[$i] ?
3291 $diffinfo->{'from_file'}[$i] :
3292 $diffinfo->{'to_file'};
3293 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
3294 $from->{'href'}[$i] = href
(action
=>"blob",
3295 hash_base
=>$parents[$i],
3296 hash
=>$diffinfo->{'from_id'}[$i],
3297 file_name
=>$from->{'file'}[$i]);
3299 $from->{'href'}[$i] = undef;
3303 # ordinary (not combined) diff
3304 $from->{'file'} = $diffinfo->{'from_file'};
3305 if ($diffinfo->{'status'} ne "A") { # not new (added) file
3306 $from->{'href'} = href
(action
=>"blob", hash_base
=>$hash_parent,
3307 hash
=>$diffinfo->{'from_id'},
3308 file_name
=>$from->{'file'});
3310 delete $from->{'href'};
3314 $to->{'file'} = $diffinfo->{'to_file'};
3315 if (!is_deleted
($diffinfo)) { # file exists in result
3316 $to->{'href'} = href
(action
=>"blob", hash_base
=>$hash,
3317 hash
=>$diffinfo->{'to_id'},
3318 file_name
=>$to->{'file'});
3320 delete $to->{'href'};
3324 ## ......................................................................
3325 ## parse to array of hashes functions
3327 sub git_get_heads_list
{
3328 my ($limit, @classes) = @_;
3329 @classes = ('heads') unless @classes;
3330 my @patterns = map { "refs/$_" } @classes;
3333 open my $fd, '-|', git_cmd
(), 'for-each-ref',
3334 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
3335 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
3338 while (my $line = <$fd>) {
3342 my ($refinfo, $committerinfo) = split(/\0/, $line);
3343 my ($hash, $name, $title) = split(' ', $refinfo, 3);
3344 my ($committer, $epoch, $tz) =
3345 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
3346 $ref_item{'fullname'} = $name;
3347 $name =~ s!^refs/(?:head|remote)s/!!;
3349 $ref_item{'name'} = $name;
3350 $ref_item{'id'} = $hash;
3351 $ref_item{'title'} = $title || '(no commit message)';
3352 $ref_item{'epoch'} = $epoch;
3354 $ref_item{'age'} = age_string
(time - $ref_item{'epoch'});
3356 $ref_item{'age'} = "unknown";
3359 push @headslist, \
%ref_item;
3363 return wantarray ? @headslist : \
@headslist;
3366 sub git_get_tags_list
{
3370 open my $fd, '-|', git_cmd
(), 'for-each-ref',
3371 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
3372 '--format=%(objectname) %(objecttype) %(refname) '.
3373 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
3376 while (my $line = <$fd>) {
3380 my ($refinfo, $creatorinfo) = split(/\0/, $line);
3381 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
3382 my ($creator, $epoch, $tz) =
3383 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
3384 $ref_item{'fullname'} = $name;
3385 $name =~ s!^refs/tags/!!;
3387 $ref_item{'type'} = $type;
3388 $ref_item{'id'} = $id;
3389 $ref_item{'name'} = $name;
3390 if ($type eq "tag") {
3391 $ref_item{'subject'} = $title;
3392 $ref_item{'reftype'} = $reftype;
3393 $ref_item{'refid'} = $refid;
3395 $ref_item{'reftype'} = $type;
3396 $ref_item{'refid'} = $id;
3399 if ($type eq "tag" || $type eq "commit") {
3400 $ref_item{'epoch'} = $epoch;
3402 $ref_item{'age'} = age_string
(time - $ref_item{'epoch'});
3404 $ref_item{'age'} = "unknown";
3408 push @tagslist, \
%ref_item;
3412 return wantarray ? @tagslist : \
@tagslist;
3415 ## ----------------------------------------------------------------------
3416 ## filesystem-related functions
3418 sub get_file_owner
{
3421 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
3422 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
3423 if (!defined $gcos) {
3427 $owner =~ s/[,;].*$//;
3428 return to_utf8
($owner);
3431 # assume that file exists
3433 my $filename = shift;
3435 open my $fd, '<', $filename;
3436 print map { to_utf8
($_) } <$fd>;
3440 ## ......................................................................
3441 ## mimetype related functions
3443 sub mimetype_guess_file
{
3444 my $filename = shift;
3445 my $mimemap = shift;
3446 -r
$mimemap or return undef;
3449 open(my $mh, '<', $mimemap) or return undef;
3451 next if m/^#/; # skip comments
3452 my ($mimetype, $exts) = split(/\t+/);
3453 if (defined $exts) {
3454 my @exts = split(/\s+/, $exts);
3455 foreach my $ext (@exts) {
3456 $mimemap{$ext} = $mimetype;
3462 $filename =~ /\.([^.]*)$/;
3463 return $mimemap{$1};
3466 sub mimetype_guess
{
3467 my $filename = shift;
3469 $filename =~ /\./ or return undef;
3471 if ($mimetypes_file) {
3472 my $file = $mimetypes_file;
3473 if ($file !~ m!^/!) { # if it is relative path
3474 # it is relative to project
3475 $file = "$projectroot/$project/$file";
3477 $mime = mimetype_guess_file
($filename, $file);
3479 $mime ||= mimetype_guess_file
($filename, '/etc/mime.types');
3485 my $filename = shift;
3488 my $mime = mimetype_guess
($filename);
3489 $mime and return $mime;
3493 return $default_blob_plain_mimetype unless $fd;
3496 return 'text/plain';
3497 } elsif (! $filename) {
3498 return 'application/octet-stream';
3499 } elsif ($filename =~ m/\.png$/i) {
3501 } elsif ($filename =~ m/\.gif$/i) {
3503 } elsif ($filename =~ m/\.jpe?g$/i) {
3504 return 'image/jpeg';
3506 return 'application/octet-stream';
3510 sub blob_contenttype
{
3511 my ($fd, $file_name, $type) = @_;
3513 $type ||= blob_mimetype
($fd, $file_name);
3514 if ($type eq 'text/plain' && defined $default_text_plain_charset) {
3515 $type .= "; charset=$default_text_plain_charset";
3521 # guess file syntax for syntax highlighting; return undef if no highlighting
3522 # the name of syntax can (in the future) depend on syntax highlighter used
3523 sub guess_file_syntax
{
3524 my ($highlight, $mimetype, $file_name) = @_;
3525 return undef unless ($highlight && defined $file_name);
3526 my $basename = basename
($file_name, '.in');
3527 return $highlight_basename{$basename}
3528 if exists $highlight_basename{$basename};
3530 $basename =~ /\.([^.]*)$/;
3531 my $ext = $1 or return undef;
3532 return $highlight_ext{$ext}
3533 if exists $highlight_ext{$ext};
3538 # run highlighter and return FD of its output,
3539 # or return original FD if no highlighting
3540 sub run_highlighter
{
3541 my ($fd, $highlight, $syntax) = @_;
3542 return $fd unless ($highlight && defined $syntax);
3545 open $fd, quote_command
(git_cmd
(), "cat-file", "blob", $hash)." | ".
3546 quote_command
($highlight_bin).
3547 " --replace-tabs=8 --fragment --syntax $syntax |"
3548 or die_error
(500, "Couldn't open file or run syntax highlighter");
3552 ## ======================================================================
3553 ## functions printing HTML: header, footer, error page
3555 sub get_page_title
{
3556 my $title = to_utf8
($site_name);
3558 return $title unless (defined $project);
3559 $title .= " - " . to_utf8
($project);
3561 return $title unless (defined $action);
3562 $title .= "/$action"; # $action is US-ASCII (7bit ASCII)
3564 return $title unless (defined $file_name);
3565 $title .= " - " . esc_path
($file_name);
3566 if ($action eq "tree" && $file_name !~ m
|/$|) {
3573 sub print_feed_meta
{
3574 if (defined $project) {
3575 my %href_params = get_feed_info
();
3576 if (!exists $href_params{'-title'}) {
3577 $href_params{'-title'} = 'log';
3580 foreach my $format (qw(RSS Atom)) {
3581 my $type = lc($format);
3583 '-rel' => 'alternate',
3584 '-title' => esc_attr
("$project - $href_params{'-title'} - $format feed"),
3585 '-type' => "application/$type+xml"
3588 $href_params{'action'} = $type;
3589 $link_attr{'-href'} = href
(%href_params);
3591 "rel=\"$link_attr{'-rel'}\" ".
3592 "title=\"$link_attr{'-title'}\" ".
3593 "href=\"$link_attr{'-href'}\" ".
3594 "type=\"$link_attr{'-type'}\" ".
3597 $href_params{'extra_options'} = '--no-merges';
3598 $link_attr{'-href'} = href
(%href_params);
3599 $link_attr{'-title'} .= ' (no merges)';
3601 "rel=\"$link_attr{'-rel'}\" ".
3602 "title=\"$link_attr{'-title'}\" ".
3603 "href=\"$link_attr{'-href'}\" ".
3604 "type=\"$link_attr{'-type'}\" ".
3609 printf('<link rel="alternate" title="%s projects list" '.
3610 'href="%s" type="text/plain; charset=utf-8" />'."\n",
3611 esc_attr
($site_name), href
(project
=>undef, action
=>"project_index"));
3612 printf('<link rel="alternate" title="%s projects feeds" '.
3613 'href="%s" type="text/x-opml" />'."\n",
3614 esc_attr
($site_name), href
(project
=>undef, action
=>"opml"));
3618 sub git_header_html
{
3619 my $status = shift || "200 OK";
3620 my $expires = shift;
3623 my $title = get_page_title
();
3625 # require explicit support from the UA if we are to send the page as
3626 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
3627 # we have to do this because MSIE sometimes globs '*/*', pretending to
3628 # support xhtml+xml but choking when it gets what it asked for.
3629 if (defined $cgi->http('HTTP_ACCEPT') &&
3630 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\
+xml
(,|;|\s
|$)/ &&
3631 $cgi->Accept('application/xhtml+xml') != 0) {
3632 $content_type = 'application/xhtml+xml';
3634 $content_type = 'text/html';
3636 print $cgi->header(-type
=>$content_type, -charset
=> 'utf-8',
3637 -status
=> $status, -expires
=> $expires)
3638 unless ($opts{'-no_http_header'});
3639 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
3641 <?xml version="1.0" encoding="utf-8"?>
3642 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3643 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
3644 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
3645 <!-- git core binaries version $git_version -->
3647 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
3648 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
3649 <meta name="robots" content="index, nofollow"/>
3650 <title>$title</title>
3652 # the stylesheet, favicon etc urls won't work correctly with path_info
3653 # unless we set the appropriate base URL
3654 if ($ENV{'PATH_INFO'}) {
3655 print "<base href=\"".esc_url
($base_url)."\" />\n";
3657 # print out each stylesheet that exist, providing backwards capability
3658 # for those people who defined $stylesheet in a config file
3659 if (defined $stylesheet) {
3660 print '<link rel="stylesheet" type="text/css" href="'.esc_url
($stylesheet).'"/>'."\n";
3662 foreach my $stylesheet (@stylesheets) {
3663 next unless $stylesheet;
3664 print '<link rel="stylesheet" type="text/css" href="'.esc_url
($stylesheet).'"/>'."\n";
3668 if ($status eq '200 OK');
3669 if (defined $favicon) {
3670 print qq(<link rel="shortcut icon" href=").esc_url
($favicon).qq(" type="image/png" />\n);
3676 if (defined $site_header && -f
$site_header) {
3677 insert_file
($site_header);
3680 print "<div class=\"page_header\">\n";
3681 if (defined $logo) {
3682 print $cgi->a({-href
=> esc_url
($logo_url),
3683 -title
=> $logo_label},
3684 $cgi->img({-src
=> esc_url
($logo),
3685 -width
=> 72, -height
=> 27,
3687 -class => "logo"}));
3689 print $cgi->a({-href
=> esc_url
($home_link)}, $home_link_str) . " / ";
3690 if (defined $project) {
3691 print $cgi->a({-href
=> href
(action
=>"summary")}, esc_html
($project));
3692 if (defined $action) {
3693 my $action_print = $action ;
3694 if (defined $opts{-action_extra
}) {
3695 $action_print = $cgi->a({-href
=> href
(action
=>$action)},
3698 print " / $action_print";
3700 if (defined $opts{-action_extra
}) {
3701 print " / $opts{-action_extra}";
3707 my $have_search = gitweb_check_feature
('search');
3708 if (defined $project && $have_search) {
3709 if (!defined $searchtext) {
3713 if (defined $hash_base) {
3714 $search_hash = $hash_base;
3715 } elsif (defined $hash) {
3716 $search_hash = $hash;
3718 $search_hash = "HEAD";
3720 my $action = $my_uri;
3721 my $use_pathinfo = gitweb_check_feature
('pathinfo');
3722 if ($use_pathinfo) {
3723 $action .= "/".esc_url
($project);
3725 print $cgi->startform(-method => "get", -action
=> $action) .
3726 "<div class=\"search\">\n" .
3728 $cgi->input({-name
=>"p", -value
=>$project, -type
=>"hidden"}) . "\n") .
3729 $cgi->input({-name
=>"a", -value
=>"search", -type
=>"hidden"}) . "\n" .
3730 $cgi->input({-name
=>"h", -value
=>$search_hash, -type
=>"hidden"}) . "\n" .
3731 $cgi->popup_menu(-name
=> 'st', -default => 'commit',
3732 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
3733 $cgi->sup($cgi->a({-href
=> href
(action
=>"search_help")}, "?")) .
3735 $cgi->textfield(-name
=> "s", -value
=> $searchtext) . "\n" .
3736 "<span title=\"Extended regular expression\">" .
3737 $cgi->checkbox(-name
=> 'sr', -value
=> 1, -label
=> 're',
3738 -checked
=> $search_use_regexp) .
3741 $cgi->end_form() . "\n";
3745 sub git_footer_html
{
3746 my $feed_class = 'rss_logo';
3748 print "<div class=\"page_footer\">\n";
3749 if (defined $project) {
3750 my $descr = git_get_project_description
($project);
3751 if (defined $descr) {
3752 print "<div class=\"page_footer_text\">" . esc_html
($descr) . "</div>\n";
3755 my %href_params = get_feed_info
();
3756 if (!%href_params) {
3757 $feed_class .= ' generic';
3759 $href_params{'-title'} ||= 'log';
3761 foreach my $format (qw(RSS Atom)) {
3762 $href_params{'action'} = lc($format);
3763 print $cgi->a({-href
=> href
(%href_params),
3764 -title
=> "$href_params{'-title'} $format feed",
3765 -class => $feed_class}, $format)."\n";
3769 print $cgi->a({-href
=> href
(project
=>undef, action
=>"opml"),
3770 -class => $feed_class}, "OPML") . " ";
3771 print $cgi->a({-href
=> href
(project
=>undef, action
=>"project_index"),
3772 -class => $feed_class}, "TXT") . "\n";
3774 print "</div>\n"; # class="page_footer"
3776 if (defined $t0 && gitweb_check_feature
('timed')) {
3777 print "<div id=\"generating_info\">\n";
3778 print 'This page took '.
3779 '<span id="generating_time" class="time_span">'.
3780 tv_interval
($t0, [ gettimeofday
() ]).
3783 '<span id="generating_cmd">'.
3784 $number_of_git_cmds.
3785 '</span> git commands '.
3787 print "</div>\n"; # class="page_footer"
3790 if (defined $site_footer && -f
$site_footer) {
3791 insert_file
($site_footer);
3794 print qq
!<script type
="text/javascript" src
="!.esc_url($javascript).qq!"></script
>\n!;
3795 if (defined $action &&
3796 $action eq 'blame_incremental') {
3797 print qq
!<script type
="text/javascript">\n!.
3798 qq
!startBlame
("!. href(action=>"blame_data
", -replay=>1) .qq!",\n!.
3799 qq
! "!. href() .qq!");\n!.
3801 } elsif (gitweb_check_feature
('javascript-actions')) {
3802 print qq
!<script type
="text/javascript">\n!.
3803 qq
!window
.onload
= fixLinks
;\n!.
3811 # die_error(<http_status_code>, <error_message>[, <detailed_html_description>])
3812 # Example: die_error(404, 'Hash not found')
3813 # By convention, use the following status codes (as defined in RFC 2616):
3814 # 400: Invalid or missing CGI parameters, or
3815 # requested object exists but has wrong type.
3816 # 403: Requested feature (like "pickaxe" or "snapshot") not enabled on
3817 # this server or project.
3818 # 404: Requested object/revision/project doesn't exist.
3819 # 500: The server isn't configured properly, or
3820 # an internal error occurred (e.g. failed assertions caused by bugs), or
3821 # an unknown error occurred (e.g. the git binary died unexpectedly).
3822 # 503: The server is currently unavailable (because it is overloaded,
3823 # or down for maintenance). Generally, this is a temporary state.
3825 my $status = shift || 500;
3826 my $error = esc_html
(shift) || "Internal Server Error";
3830 my %http_responses = (
3831 400 => '400 Bad Request',
3832 403 => '403 Forbidden',
3833 404 => '404 Not Found',
3834 500 => '500 Internal Server Error',
3835 503 => '503 Service Unavailable',
3837 git_header_html
($http_responses{$status}, undef, %opts);
3839 <div class="page_body">
3844 if (defined $extra) {
3852 unless ($opts{'-error_handler'});
3855 ## ----------------------------------------------------------------------
3856 ## functions printing or outputting HTML: navigation
3858 sub git_print_page_nav
{
3859 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
3860 $extra = '' if !defined $extra; # pager or formats
3862 my @navs = qw(summary shortlog log commit commitdiff tree);
3864 @navs = grep { $_ ne $suppress } @navs;
3867 my %arg = map { $_ => {action
=>$_} } @navs;
3868 if (defined $head) {
3869 for (qw(commit commitdiff)) {
3870 $arg{$_}{'hash'} = $head;
3872 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
3873 for (qw(shortlog log)) {
3874 $arg{$_}{'hash'} = $head;
3879 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
3880 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
3882 my @actions = gitweb_get_feature
('actions');
3885 'n' => $project, # project name
3886 'f' => $git_dir, # project path within filesystem
3887 'h' => $treehead || '', # current hash ('h' parameter)
3888 'b' => $treebase || '', # hash base ('hb' parameter)
3891 my ($label, $link, $pos) = splice(@actions,0,3);
3893 @navs = map { $_ eq $pos ? ($_, $label) : $_ } @navs;
3895 $link =~ s/%([%nfhb])/$repl{$1}/g;
3896 $arg{$label}{'_href'} = $link;
3899 print "<div class=\"page_nav\">\n" .
3901 map { $_ eq $current ?
3902 $_ : $cgi->a({-href
=> ($arg{$_}{_href
} ? $arg{$_}{_href
} : href
(%{$arg{$_}}))}, "$_")
3904 print "<br/>\n$extra<br/>\n" .
3908 # returns a submenu for the nagivation of the refs views (tags, heads,
3909 # remotes) with the current view disabled and the remotes view only
3910 # available if the feature is enabled
3911 sub format_ref_views
{
3913 my @ref_views = qw{tags heads};
3914 push @ref_views, 'remotes' if gitweb_check_feature
('remote_heads');
3915 return join " | ", map {
3916 $_ eq $current ? $_ :
3917 $cgi->a({-href
=> href
(action
=>$_)}, $_)
3921 sub format_paging_nav
{
3922 my ($action, $page, $has_next_link) = @_;
3928 $cgi->a({-href
=> href
(-replay
=>1, page
=>undef)}, "first") .
3930 $cgi->a({-href
=> href
(-replay
=>1, page
=>$page-1),
3931 -accesskey
=> "p", -title
=> "Alt-p"}, "prev");
3933 $paging_nav .= "first ⋅ prev";
3936 if ($has_next_link) {
3937 $paging_nav .= " ⋅ " .
3938 $cgi->a({-href
=> href
(-replay
=>1, page
=>$page+1),
3939 -accesskey
=> "n", -title
=> "Alt-n"}, "next");
3941 $paging_nav .= " ⋅ next";
3947 ## ......................................................................
3948 ## functions printing or outputting HTML: div
3950 sub git_print_header_div
{
3951 my ($action, $title, $hash, $hash_base) = @_;
3954 $args{'action'} = $action;
3955 $args{'hash'} = $hash if $hash;
3956 $args{'hash_base'} = $hash_base if $hash_base;
3958 print "<div class=\"header\">\n" .
3959 $cgi->a({-href
=> href
(%args), -class => "title"},
3960 $title ? $title : $action) .
3964 sub format_repo_url
{
3965 my ($name, $url) = @_;
3966 return "<tr class=\"metadata_url\"><td>$name</td><td>$url</td></tr>\n";
3969 # Group output by placing it in a DIV element and adding a header.
3970 # Options for start_div() can be provided by passing a hash reference as the
3971 # first parameter to the function.
3972 # Options to git_print_header_div() can be provided by passing an array
3973 # reference. This must follow the options to start_div if they are present.
3974 # The content can be a scalar, which is output as-is, a scalar reference, which
3975 # is output after html escaping, an IO handle passed either as *handle or
3976 # *handle{IO}, or a function reference. In the latter case all following
3977 # parameters will be taken as argument to the content function call.
3978 sub git_print_section
{
3979 my ($div_args, $header_args, $content);
3981 if (ref($arg) eq 'HASH') {
3985 if (ref($arg) eq 'ARRAY') {
3986 $header_args = $arg;
3991 print $cgi->start_div($div_args);
3992 git_print_header_div
(@$header_args);
3994 if (ref($content) eq 'CODE') {
3996 } elsif (ref($content) eq 'SCALAR') {
3997 print esc_html
($$content);
3998 } elsif (ref($content) eq 'GLOB' or ref($content) eq 'IO::Handle') {
4000 } elsif (!ref($content) && defined($content)) {
4004 print $cgi->end_div;
4007 sub print_local_time
{
4008 print format_local_time
(@_);
4011 sub format_local_time
{
4014 if ($date{'hour_local'} < 6) {
4015 $localtime .= sprintf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
4016 $date{'hour_local'}, $date{'minute_local'}, $date{'tz_local'});
4018 $localtime .= sprintf(" (%02d:%02d %s)",
4019 $date{'hour_local'}, $date{'minute_local'}, $date{'tz_local'});
4025 # Outputs the author name and date in long form
4026 sub git_print_authorship
{
4029 my $tag = $opts{-tag
} || 'div';
4030 my $author = $co->{'author_name'};
4032 my %ad = parse_date
($co->{'author_epoch'}, $co->{'author_tz'});
4033 print "<$tag class=\"author_date\">" .
4034 format_search_author
($author, "author", esc_html
($author)) .
4036 print_local_time
(%ad) if ($opts{-localtime});
4037 print "]" . git_get_avatar
($co->{'author_email'}, -pad_before
=> 1)
4041 # Outputs table rows containing the full author or committer information,
4042 # in the format expected for 'commit' view (& similar).
4043 # Parameters are a commit hash reference, followed by the list of people
4044 # to output information for. If the list is empty it defaults to both
4045 # author and committer.
4046 sub git_print_authorship_rows
{
4048 # too bad we can't use @people = @_ || ('author', 'committer')
4050 @people = ('author', 'committer') unless @people;
4051 foreach my $who (@people) {
4052 my %wd = parse_date
($co->{"${who}_epoch"}, $co->{"${who}_tz"});
4053 print "<tr><td>$who</td><td>" .
4054 format_search_author
($co->{"${who}_name"}, $who,
4055 esc_html
($co->{"${who}_name"})) . " " .
4056 format_search_author
($co->{"${who}_email"}, $who,
4057 esc_html
("<" . $co->{"${who}_email"} . ">")) .
4058 "</td><td rowspan=\"2\">" .
4059 git_get_avatar
($co->{"${who}_email"}, -size
=> 'double') .
4062 "<td></td><td> $wd{'rfc2822'}";
4063 print_local_time
(%wd);
4069 sub git_print_page_path
{
4075 print "<div class=\"page_path\">";
4076 print $cgi->a({-href
=> href
(action
=>"tree", hash_base
=>$hb),
4077 -title
=> 'tree root'}, to_utf8
("[$project]"));
4079 if (defined $name) {
4080 my @dirname = split '/', $name;
4081 my $basename = pop @dirname;
4084 foreach my $dir (@dirname) {
4085 $fullname .= ($fullname ? '/' : '') . $dir;
4086 print $cgi->a({-href
=> href
(action
=>"tree", file_name
=>$fullname,
4088 -title
=> $fullname}, esc_path
($dir));
4091 if (defined $type && $type eq 'blob') {
4092 print $cgi->a({-href
=> href
(action
=>"blob_plain", file_name
=>$file_name,
4094 -title
=> $name}, esc_path
($basename));
4095 } elsif (defined $type && $type eq 'tree') {
4096 print $cgi->a({-href
=> href
(action
=>"tree", file_name
=>$file_name,
4098 -title
=> $name}, esc_path
($basename));
4101 print esc_path
($basename);
4104 print "<br/></div>\n";
4111 if ($opts{'-remove_title'}) {
4112 # remove title, i.e. first line of log
4115 # remove leading empty lines
4116 while (defined $log->[0] && $log->[0] eq "") {
4123 foreach my $line (@$log) {
4124 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
4127 if (! $opts{'-remove_signoff'}) {
4128 print "<span class=\"signoff\">" . esc_html
($line) . "</span><br/>\n";
4131 # remove signoff lines
4138 # print only one empty line
4139 # do not print empty line after signoff
4141 next if ($empty || $signoff);
4147 print format_log_line_html
($line) . "<br/>\n";
4150 if ($opts{'-final_empty_line'}) {
4151 # end with single empty line
4152 print "<br/>\n" unless $empty;
4156 # return link target (what link points to)
4157 sub git_get_link_target
{
4162 open my $fd, "-|", git_cmd
(), "cat-file", "blob", $hash
4166 $link_target = <$fd>;
4171 return $link_target;
4174 # given link target, and the directory (basedir) the link is in,
4175 # return target of link relative to top directory (top tree);
4176 # return undef if it is not possible (including absolute links).
4177 sub normalize_link_target
{
4178 my ($link_target, $basedir) = @_;
4180 # absolute symlinks (beginning with '/') cannot be normalized
4181 return if (substr($link_target, 0, 1) eq '/');
4183 # normalize link target to path from top (root) tree (dir)
4186 $path = $basedir . '/' . $link_target;
4188 # we are in top (root) tree (dir)
4189 $path = $link_target;
4192 # remove //, /./, and /../
4194 foreach my $part (split('/', $path)) {
4195 # discard '.' and ''
4196 next if (!$part || $part eq '.');
4198 if ($part eq '..') {
4202 # link leads outside repository (outside top dir)
4206 push @path_parts, $part;
4209 $path = join('/', @path_parts);
4214 # print tree entry (row of git_tree), but without encompassing <tr> element
4215 sub git_print_tree_entry
{
4216 my ($t, $basedir, $hash_base, $have_blame) = @_;
4219 $base_key{'hash_base'} = $hash_base if defined $hash_base;
4221 # The format of a table row is: mode list link. Where mode is
4222 # the mode of the entry, list is the name of the entry, an href,
4223 # and link is the action links of the entry.
4225 print "<td class=\"mode\">" . mode_str
($t->{'mode'}) . "</td>\n";
4226 if (exists $t->{'size'}) {
4227 print "<td class=\"size\">$t->{'size'}</td>\n";
4229 if ($t->{'type'} eq "blob") {
4230 print "<td class=\"list\">" .
4231 $cgi->a({-href
=> href
(action
=>"blob", hash
=>$t->{'hash'},
4232 file_name
=>"$basedir$t->{'name'}", %base_key),
4233 -class => "list"}, esc_path
($t->{'name'}));
4234 if (S_ISLNK
(oct $t->{'mode'})) {
4235 my $link_target = git_get_link_target
($t->{'hash'});
4237 my $norm_target = normalize_link_target
($link_target, $basedir);
4238 if (defined $norm_target) {
4240 $cgi->a({-href
=> href
(action
=>"object", hash_base
=>$hash_base,
4241 file_name
=>$norm_target),
4242 -title
=> $norm_target}, esc_path
($link_target));
4244 print " -> " . esc_path
($link_target);
4249 print "<td class=\"link\">";
4250 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$t->{'hash'},
4251 file_name
=>"$basedir$t->{'name'}", %base_key)},
4255 $cgi->a({-href
=> href
(action
=>"blame", hash
=>$t->{'hash'},
4256 file_name
=>"$basedir$t->{'name'}", %base_key)},
4259 if (defined $hash_base) {
4261 $cgi->a({-href
=> href
(action
=>"history", hash_base
=>$hash_base,
4262 hash
=>$t->{'hash'}, file_name
=>"$basedir$t->{'name'}")},
4266 $cgi->a({-href
=> href
(action
=>"blob_plain", hash_base
=>$hash_base,
4267 file_name
=>"$basedir$t->{'name'}")},
4271 } elsif ($t->{'type'} eq "tree") {
4272 print "<td class=\"list\">";
4273 print $cgi->a({-href
=> href
(action
=>"tree", hash
=>$t->{'hash'},
4274 file_name
=>"$basedir$t->{'name'}",
4276 esc_path
($t->{'name'}));
4278 print "<td class=\"link\">";
4279 print $cgi->a({-href
=> href
(action
=>"tree", hash
=>$t->{'hash'},
4280 file_name
=>"$basedir$t->{'name'}",
4283 if (defined $hash_base) {
4285 $cgi->a({-href
=> href
(action
=>"history", hash_base
=>$hash_base,
4286 file_name
=>"$basedir$t->{'name'}")},
4291 # unknown object: we can only present history for it
4292 # (this includes 'commit' object, i.e. submodule support)
4293 print "<td class=\"list\">" .
4294 esc_path
($t->{'name'}) .
4296 print "<td class=\"link\">";
4297 if (defined $hash_base) {
4298 print $cgi->a({-href
=> href
(action
=>"history",
4299 hash_base
=>$hash_base,
4300 file_name
=>"$basedir$t->{'name'}")},
4307 ## ......................................................................
4308 ## functions printing large fragments of HTML
4310 # get pre-image filenames for merge (combined) diff
4311 sub fill_from_file_info
{
4312 my ($diff, @parents) = @_;
4314 $diff->{'from_file'} = [ ];
4315 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
4316 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
4317 if ($diff->{'status'}[$i] eq 'R' ||
4318 $diff->{'status'}[$i] eq 'C') {
4319 $diff->{'from_file'}[$i] =
4320 git_get_path_by_hash
($parents[$i], $diff->{'from_id'}[$i]);
4327 # is current raw difftree line of file deletion
4329 my $diffinfo = shift;
4331 return $diffinfo->{'to_id'} eq ('0' x
40);
4334 # does patch correspond to [previous] difftree raw line
4335 # $diffinfo - hashref of parsed raw diff format
4336 # $patchinfo - hashref of parsed patch diff format
4337 # (the same keys as in $diffinfo)
4338 sub is_patch_split
{
4339 my ($diffinfo, $patchinfo) = @_;
4341 return defined $diffinfo && defined $patchinfo
4342 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
4346 sub git_difftree_body
{
4347 my ($difftree, $hash, @parents) = @_;
4348 my ($parent) = $parents[0];
4349 my $have_blame = gitweb_check_feature
('blame');
4350 print "<div class=\"list_head\">\n";
4351 if ($#{$difftree} > 10) {
4352 print(($#{$difftree} + 1) . " files changed:\n");
4356 print "<table class=\"" .
4357 (@parents > 1 ? "combined " : "") .
4360 # header only for combined diff in 'commitdiff' view
4361 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
4364 print "<thead><tr>\n" .
4365 "<th></th><th></th>\n"; # filename, patchN link
4366 for (my $i = 0; $i < @parents; $i++) {
4367 my $par = $parents[$i];
4369 $cgi->a({-href
=> href
(action
=>"commitdiff",
4370 hash
=>$hash, hash_parent
=>$par),
4371 -title
=> 'commitdiff to parent number ' .
4372 ($i+1) . ': ' . substr($par,0,7)},
4376 print "</tr></thead>\n<tbody>\n";
4381 foreach my $line (@{$difftree}) {
4382 my $diff = parsed_difftree_line
($line);
4385 print "<tr class=\"dark\">\n";
4387 print "<tr class=\"light\">\n";
4391 if (exists $diff->{'nparents'}) { # combined diff
4393 fill_from_file_info
($diff, @parents)
4394 unless exists $diff->{'from_file'};
4396 if (!is_deleted
($diff)) {
4397 # file exists in the result (child) commit
4399 $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'to_id'},
4400 file_name
=>$diff->{'to_file'},
4402 -class => "list"}, esc_path
($diff->{'to_file'})) .
4406 esc_path
($diff->{'to_file'}) .
4410 if ($action eq 'commitdiff') {
4413 print "<td class=\"link\">" .
4414 $cgi->a({-href
=> href
(-anchor
=>"patch$patchno")},
4420 my $has_history = 0;
4421 my $not_deleted = 0;
4422 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
4423 my $hash_parent = $parents[$i];
4424 my $from_hash = $diff->{'from_id'}[$i];
4425 my $from_path = $diff->{'from_file'}[$i];
4426 my $status = $diff->{'status'}[$i];
4428 $has_history ||= ($status ne 'A');
4429 $not_deleted ||= ($status ne 'D');
4431 if ($status eq 'A') {
4432 print "<td class=\"link\" align=\"right\"> | </td>\n";
4433 } elsif ($status eq 'D') {
4434 print "<td class=\"link\">" .
4435 $cgi->a({-href
=> href
(action
=>"blob",
4438 file_name
=>$from_path)},
4442 if ($diff->{'to_id'} eq $from_hash) {
4443 print "<td class=\"link nochange\">";
4445 print "<td class=\"link\">";
4447 print $cgi->a({-href
=> href
(action
=>"blobdiff",
4448 hash
=>$diff->{'to_id'},
4449 hash_parent
=>$from_hash,
4451 hash_parent_base
=>$hash_parent,
4452 file_name
=>$diff->{'to_file'},
4453 file_parent
=>$from_path)},
4459 print "<td class=\"link\">";
4461 print $cgi->a({-href
=> href
(action
=>"blob",
4462 hash
=>$diff->{'to_id'},
4463 file_name
=>$diff->{'to_file'},
4466 print " | " if ($has_history);
4469 print $cgi->a({-href
=> href
(action
=>"history",
4470 file_name
=>$diff->{'to_file'},
4477 next; # instead of 'else' clause, to avoid extra indent
4479 # else ordinary diff
4481 my ($to_mode_oct, $to_mode_str, $to_file_type);
4482 my ($from_mode_oct, $from_mode_str, $from_file_type);
4483 if ($diff->{'to_mode'} ne ('0' x
6)) {
4484 $to_mode_oct = oct $diff->{'to_mode'};
4485 if (S_ISREG
($to_mode_oct)) { # only for regular file
4486 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
4488 $to_file_type = file_type
($diff->{'to_mode'});
4490 if ($diff->{'from_mode'} ne ('0' x
6)) {
4491 $from_mode_oct = oct $diff->{'from_mode'};
4492 if (S_ISREG
($from_mode_oct)) { # only for regular file
4493 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
4495 $from_file_type = file_type
($diff->{'from_mode'});
4498 if ($diff->{'status'} eq "A") { # created
4499 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
4500 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
4501 $mode_chng .= "]</span>";
4503 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'to_id'},
4504 hash_base
=>$hash, file_name
=>$diff->{'file'}),
4505 -class => "list"}, esc_path
($diff->{'file'}));
4507 print "<td>$mode_chng</td>\n";
4508 print "<td class=\"link\">";
4509 if ($action eq 'commitdiff') {
4512 print $cgi->a({-href
=> href
(-anchor
=>"patch$patchno")},
4516 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'to_id'},
4517 hash_base
=>$hash, file_name
=>$diff->{'file'})},
4521 } elsif ($diff->{'status'} eq "D") { # deleted
4522 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
4524 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'from_id'},
4525 hash_base
=>$parent, file_name
=>$diff->{'file'}),
4526 -class => "list"}, esc_path
($diff->{'file'}));
4528 print "<td>$mode_chng</td>\n";
4529 print "<td class=\"link\">";
4530 if ($action eq 'commitdiff') {
4533 print $cgi->a({-href
=> href
(-anchor
=>"patch$patchno")},
4537 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'from_id'},
4538 hash_base
=>$parent, file_name
=>$diff->{'file'})},
4541 print $cgi->a({-href
=> href
(action
=>"blame", hash_base
=>$parent,
4542 file_name
=>$diff->{'file'})},
4545 print $cgi->a({-href
=> href
(action
=>"history", hash_base
=>$parent,
4546 file_name
=>$diff->{'file'})},
4550 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
4551 my $mode_chnge = "";
4552 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
4553 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
4554 if ($from_file_type ne $to_file_type) {
4555 $mode_chnge .= " from $from_file_type to $to_file_type";
4557 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
4558 if ($from_mode_str && $to_mode_str) {
4559 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
4560 } elsif ($to_mode_str) {
4561 $mode_chnge .= " mode: $to_mode_str";
4564 $mode_chnge .= "]</span>\n";
4567 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'to_id'},
4568 hash_base
=>$hash, file_name
=>$diff->{'file'}),
4569 -class => "list"}, esc_path
($diff->{'file'}));
4571 print "<td>$mode_chnge</td>\n";
4572 print "<td class=\"link\">";
4573 if ($action eq 'commitdiff') {
4576 print $cgi->a({-href
=> href
(-anchor
=>"patch$patchno")},
4579 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
4580 # "commit" view and modified file (not onlu mode changed)
4581 print $cgi->a({-href
=> href
(action
=>"blobdiff",
4582 hash
=>$diff->{'to_id'}, hash_parent
=>$diff->{'from_id'},
4583 hash_base
=>$hash, hash_parent_base
=>$parent,
4584 file_name
=>$diff->{'file'})},
4588 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'to_id'},
4589 hash_base
=>$hash, file_name
=>$diff->{'file'})},
4592 print $cgi->a({-href
=> href
(action
=>"blame", hash_base
=>$hash,
4593 file_name
=>$diff->{'file'})},
4596 print $cgi->a({-href
=> href
(action
=>"history", hash_base
=>$hash,
4597 file_name
=>$diff->{'file'})},
4601 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
4602 my %status_name = ('R' => 'moved', 'C' => 'copied');
4603 my $nstatus = $status_name{$diff->{'status'}};
4605 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
4606 # mode also for directories, so we cannot use $to_mode_str
4607 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
4610 $cgi->a({-href
=> href
(action
=>"blob", hash_base
=>$hash,
4611 hash
=>$diff->{'to_id'}, file_name
=>$diff->{'to_file'}),
4612 -class => "list"}, esc_path
($diff->{'to_file'})) . "</td>\n" .
4613 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
4614 $cgi->a({-href
=> href
(action
=>"blob", hash_base
=>$parent,
4615 hash
=>$diff->{'from_id'}, file_name
=>$diff->{'from_file'}),
4616 -class => "list"}, esc_path
($diff->{'from_file'})) .
4617 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
4618 "<td class=\"link\">";
4619 if ($action eq 'commitdiff') {
4622 print $cgi->a({-href
=> href
(-anchor
=>"patch$patchno")},
4625 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
4626 # "commit" view and modified file (not only pure rename or copy)
4627 print $cgi->a({-href
=> href
(action
=>"blobdiff",
4628 hash
=>$diff->{'to_id'}, hash_parent
=>$diff->{'from_id'},
4629 hash_base
=>$hash, hash_parent_base
=>$parent,
4630 file_name
=>$diff->{'to_file'}, file_parent
=>$diff->{'from_file'})},
4634 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'to_id'},
4635 hash_base
=>$parent, file_name
=>$diff->{'to_file'})},
4638 print $cgi->a({-href
=> href
(action
=>"blame", hash_base
=>$hash,
4639 file_name
=>$diff->{'to_file'})},
4642 print $cgi->a({-href
=> href
(action
=>"history", hash_base
=>$hash,
4643 file_name
=>$diff->{'to_file'})},
4647 } # we should not encounter Unmerged (U) or Unknown (X) status
4650 print "</tbody>" if $has_header;
4654 sub git_patchset_body
{
4655 my ($fd, $difftree, $hash, @hash_parents) = @_;
4656 my ($hash_parent) = $hash_parents[0];
4658 my $is_combined = (@hash_parents > 1);
4660 my $patch_number = 0;
4666 print "<div class=\"patchset\">\n";
4668 # skip to first patch
4669 while ($patch_line = <$fd>) {
4672 last if ($patch_line =~ m/^diff /);
4676 while ($patch_line) {
4678 # parse "git diff" header line
4679 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
4680 # $1 is from_name, which we do not use
4681 $to_name = unquote
($2);
4682 $to_name =~ s!^b/!!;
4683 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
4684 # $1 is 'cc' or 'combined', which we do not use
4685 $to_name = unquote
($2);
4690 # check if current patch belong to current raw line
4691 # and parse raw git-diff line if needed
4692 if (is_patch_split
($diffinfo, { 'to_file' => $to_name })) {
4693 # this is continuation of a split patch
4694 print "<div class=\"patch cont\">\n";
4696 # advance raw git-diff output if needed
4697 $patch_idx++ if defined $diffinfo;
4699 # read and prepare patch information
4700 $diffinfo = parsed_difftree_line
($difftree->[$patch_idx]);
4702 # compact combined diff output can have some patches skipped
4703 # find which patch (using pathname of result) we are at now;
4705 while ($to_name ne $diffinfo->{'to_file'}) {
4706 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
4707 format_diff_cc_simplified
($diffinfo, @hash_parents) .
4708 "</div>\n"; # class="patch"
4713 last if $patch_idx > $#$difftree;
4714 $diffinfo = parsed_difftree_line
($difftree->[$patch_idx]);
4718 # modifies %from, %to hashes
4719 parse_from_to_diffinfo
($diffinfo, \
%from, \
%to, @hash_parents);
4721 # this is first patch for raw difftree line with $patch_idx index
4722 # we index @$difftree array from 0, but number patches from 1
4723 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
4727 #assert($patch_line =~ m/^diff /) if DEBUG;
4728 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
4730 # print "git diff" header
4731 print format_git_diff_header_line
($patch_line, $diffinfo,
4734 # print extended diff header
4735 print "<div class=\"diff extended_header\">\n";
4737 while ($patch_line = <$fd>) {
4740 last EXTENDED_HEADER
if ($patch_line =~ m/^--- |^diff /);
4742 print format_extended_diff_header_line
($patch_line, $diffinfo,
4745 print "</div>\n"; # class="diff extended_header"
4747 # from-file/to-file diff header
4748 if (! $patch_line) {
4749 print "</div>\n"; # class="patch"
4752 next PATCH
if ($patch_line =~ m/^diff /);
4753 #assert($patch_line =~ m/^---/) if DEBUG;
4755 my $last_patch_line = $patch_line;
4756 $patch_line = <$fd>;
4758 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
4760 print format_diff_from_to_header
($last_patch_line, $patch_line,
4761 $diffinfo, \
%from, \
%to,
4766 while ($patch_line = <$fd>) {
4769 next PATCH
if ($patch_line =~ m/^diff /);
4771 print format_diff_line
($patch_line, \
%from, \
%to);
4775 print "</div>\n"; # class="patch"
4778 # for compact combined (--cc) format, with chunk and patch simplification
4779 # the patchset might be empty, but there might be unprocessed raw lines
4780 for (++$patch_idx if $patch_number > 0;
4781 $patch_idx < @$difftree;
4783 # read and prepare patch information
4784 $diffinfo = parsed_difftree_line
($difftree->[$patch_idx]);
4786 # generate anchor for "patch" links in difftree / whatchanged part
4787 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
4788 format_diff_cc_simplified
($diffinfo, @hash_parents) .
4789 "</div>\n"; # class="patch"
4794 if ($patch_number == 0) {
4795 if (@hash_parents > 1) {
4796 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
4798 print "<div class=\"diff nodifferences\">No differences found</div>\n";
4802 print "</div>\n"; # class="patchset"
4805 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4807 # fills project list info (age, description, owner, forks) for each
4808 # project in the list, removing invalid projects from returned list
4809 # NOTE: modifies $projlist, but does not remove entries from it
4810 sub fill_project_list_info
{
4811 my ($projlist, $check_forks) = @_;
4814 my $show_ctags = gitweb_check_feature
('ctags');
4816 foreach my $pr (@$projlist) {
4817 my (@activity) = git_get_last_activity
($pr->{'path'});
4818 unless (@activity) {
4821 ($pr->{'age'}, $pr->{'age_string'}) = @activity;
4822 if (!defined $pr->{'descr'}) {
4823 my $descr = git_get_project_description
($pr->{'path'}) || "";
4824 $descr = to_utf8
($descr);
4825 $pr->{'descr_long'} = $descr;
4826 $pr->{'descr'} = chop_str
($descr, $projects_list_description_width, 5);
4828 if (!defined $pr->{'owner'}) {
4829 $pr->{'owner'} = git_get_project_owner
("$pr->{'path'}") || "";
4832 my $pname = $pr->{'path'};
4833 if (($pname =~ s/\.git$//) &&
4834 ($pname !~ /\/$/) &&
4835 (-d
"$projectroot/$pname")) {
4836 $pr->{'forks'} = "-d $projectroot/$pname";
4841 $show_ctags and $pr->{'ctags'} = git_get_project_ctags
($pr->{'path'});
4842 push @projects, $pr;
4848 # print 'sort by' <th> element, generating 'sort by $name' replay link
4849 # if that order is not selected
4851 print format_sort_th
(@_);
4854 sub format_sort_th
{
4855 my ($name, $order, $header) = @_;
4857 $header ||= ucfirst($name);
4859 if ($order eq $name) {
4860 $sort_th .= "<th>$header</th>\n";
4862 $sort_th .= "<th>" .
4863 $cgi->a({-href
=> href
(-replay
=>1, order
=>$name),
4864 -class => "header"}, $header) .
4871 sub git_project_list_rows
{
4872 my ($projlist, $from, $to, $check_forks) = @_;
4874 $from = 0 unless defined $from;
4875 $to = $#$projlist if (!defined $to || $#$projlist < $to);
4878 my $tagfilter = $cgi->param('by_tag');
4879 for (my $i = $from; $i <= $to; $i++) {
4880 my $pr = $projlist->[$i];
4882 next if $tagfilter and $show_ctags and not grep { lc $_ eq lc $tagfilter } keys %{$pr->{'ctags'}};
4883 next if $searchtext and not $pr->{'path'} =~ /$searchtext/
4884 and not $pr->{'descr_long'} =~ /$searchtext/;
4885 # Weed out forks or non-matching entries of search
4887 my $forkbase = $project; $forkbase ||= ''; $forkbase =~ s
#\.git$#/#;
4888 $forkbase="^$forkbase" if $forkbase;
4889 next if not $searchtext and not $tagfilter and $show_ctags
4890 and $pr->{'path'} =~ m
#$forkbase.*/.*#; # regexp-safe
4894 print "<tr class=\"dark\">\n";
4896 print "<tr class=\"light\">\n";
4901 if ($pr->{'forks'}) {
4902 print "<!-- $pr->{'forks'} -->\n";
4903 print $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"forks")}, "+");
4907 print "<td>" . $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"summary"),
4908 -class => "list"}, esc_html
($pr->{'path'})) . "</td>\n" .
4909 "<td>" . $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"summary"),
4910 -class => "list", -title
=> $pr->{'descr_long'}},
4911 esc_html
($pr->{'descr'})) . "</td>\n" .
4912 "<td><i>" . chop_and_escape_str
($pr->{'owner'}, 15) . "</i></td>\n";
4913 print "<td class=\"". age_class
($pr->{'age'}) . "\">" .
4914 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
4915 "<td class=\"link\">" .
4916 $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"summary")}, "summary") . " | " .
4917 $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"shortlog")}, "shortlog") . " | " .
4918 $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"log")}, "log") . " | " .
4919 $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"tree")}, "tree") .
4920 ($pr->{'forks'} ? " | " . $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"forks")}, "forks") : '') .
4926 sub git_project_list_body
{
4927 # actually uses global variable $project
4928 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
4930 my $check_forks = gitweb_check_feature
('forks');
4931 my @projects = fill_project_list_info
($projlist, $check_forks);
4933 $order ||= $default_projects_order;
4934 $from = 0 unless defined $from;
4935 $to = $#projects if (!defined $to || $#projects < $to);
4938 project
=> { key
=> 'path', type
=> 'str' },
4939 descr
=> { key
=> 'descr_long', type
=> 'str' },
4940 owner
=> { key
=> 'owner', type
=> 'str' },
4941 age
=> { key
=> 'age', type
=> 'num' }
4943 my $oi = $order_info{$order};
4944 if ($oi->{'type'} eq 'str') {
4945 @projects = sort {$a->{$oi->{'key'}} cmp $b->{$oi->{'key'}}} @projects;
4947 @projects = sort {$a->{$oi->{'key'}} <=> $b->{$oi->{'key'}}} @projects;
4950 my $show_ctags = gitweb_check_feature
('ctags');
4952 my $ctags = git_gather_all_ctags
(\
@projects);
4953 my $cloud = git_populate_project_tagcloud
($ctags);
4954 print git_show_project_tagcloud
($cloud, 64);
4957 print "<table class=\"project_list\">\n";
4958 unless ($no_header) {
4961 print "<th></th>\n";
4963 print_sort_th
('project', $order, 'Project');
4964 print_sort_th
('descr', $order, 'Description');
4965 print_sort_th
('owner', $order, 'Owner');
4966 print_sort_th
('age', $order, 'Last Change');
4967 print "<th></th>\n" . # for links
4970 git_project_list_rows
(\
@projects, $from, $to, $check_forks);
4972 if (defined $extra) {
4975 print "<td></td>\n";
4977 print "<td colspan=\"5\">$extra</td>\n" .
4984 # uses global variable $project
4985 my ($commitlist, $from, $to, $refs, $extra) = @_;
4987 $from = 0 unless defined $from;
4988 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
4990 for (my $i = 0; $i <= $to; $i++) {
4991 my %co = %{$commitlist->[$i]};
4993 my $commit = $co{'id'};
4994 my $ref = format_ref_marker
($refs, $commit);
4995 git_print_header_div
('commit',
4996 "<span class=\"age\">$co{'age_string'}</span>" .
4997 esc_html
($co{'title'}) . $ref,
4999 print "<div class=\"title_text\">\n" .
5000 "<div class=\"log_link\">\n" .
5001 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$commit)}, "commit") .
5003 $cgi->a({-href
=> href
(action
=>"commitdiff", hash
=>$commit)}, "commitdiff") .
5005 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$commit, hash_base
=>$commit)}, "tree") .
5008 git_print_authorship
(\
%co, -tag
=> 'span');
5009 print "<br/>\n</div>\n";
5011 print "<div class=\"log_body\">\n";
5012 git_print_log
($co{'comment'}, -final_empty_line
=> 1);
5016 print "<div class=\"page_nav\">\n";
5022 sub git_shortlog_body
{
5023 # uses global variable $project
5024 my ($commitlist, $from, $to, $refs, $extra) = @_;
5026 $from = 0 unless defined $from;
5027 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
5029 print "<table class=\"shortlog\">\n";
5031 for (my $i = $from; $i <= $to; $i++) {
5032 my %co = %{$commitlist->[$i]};
5033 my $commit = $co{'id'};
5034 my $ref = format_ref_marker
($refs, $commit);
5036 print "<tr class=\"dark\">\n";
5038 print "<tr class=\"light\">\n";
5041 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
5042 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5043 format_author_html
('td', \
%co, 10) . "<td>";
5044 print format_subject_html
($co{'title'}, $co{'title_short'},
5045 href
(action
=>"commit", hash
=>$commit), $ref);
5047 "<td class=\"link\">" .
5048 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$commit)}, "commit") . " | " .
5049 $cgi->a({-href
=> href
(action
=>"commitdiff", hash
=>$commit)}, "commitdiff") . " | " .
5050 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$commit, hash_base
=>$commit)}, "tree");
5051 my $snapshot_links = format_snapshot_links
($commit);
5052 if (defined $snapshot_links) {
5053 print " | " . $snapshot_links;
5058 if (defined $extra) {
5060 "<td colspan=\"4\">$extra</td>\n" .
5066 sub git_history_body
{
5067 # Warning: assumes constant type (blob or tree) during history
5068 my ($commitlist, $from, $to, $refs, $extra,
5069 $file_name, $file_hash, $ftype) = @_;
5071 $from = 0 unless defined $from;
5072 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
5074 print "<table class=\"history\">\n";
5076 for (my $i = $from; $i <= $to; $i++) {
5077 my %co = %{$commitlist->[$i]};
5081 my $commit = $co{'id'};
5083 my $ref = format_ref_marker
($refs, $commit);
5086 print "<tr class=\"dark\">\n";
5088 print "<tr class=\"light\">\n";
5091 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5092 # shortlog: format_author_html('td', \%co, 10)
5093 format_author_html
('td', \
%co, 15, 3) . "<td>";
5094 # originally git_history used chop_str($co{'title'}, 50)
5095 print format_subject_html
($co{'title'}, $co{'title_short'},
5096 href
(action
=>"commit", hash
=>$commit), $ref);
5098 "<td class=\"link\">" .
5099 $cgi->a({-href
=> href
(action
=>$ftype, hash_base
=>$commit, file_name
=>$file_name)}, $ftype) . " | " .
5100 $cgi->a({-href
=> href
(action
=>"commitdiff", hash
=>$commit)}, "commitdiff");
5102 if ($ftype eq 'blob') {
5103 my $blob_current = $file_hash;
5104 my $blob_parent = git_get_hash_by_path
($commit, $file_name);
5105 if (defined $blob_current && defined $blob_parent &&
5106 $blob_current ne $blob_parent) {
5108 $cgi->a({-href
=> href
(action
=>"blobdiff",
5109 hash
=>$blob_current, hash_parent
=>$blob_parent,
5110 hash_base
=>$hash_base, hash_parent_base
=>$commit,
5111 file_name
=>$file_name)},
5118 if (defined $extra) {
5120 "<td colspan=\"4\">$extra</td>\n" .
5127 # uses global variable $project
5128 my ($taglist, $from, $to, $extra) = @_;
5129 $from = 0 unless defined $from;
5130 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
5132 print "<table class=\"tags\">\n";
5134 for (my $i = $from; $i <= $to; $i++) {
5135 my $entry = $taglist->[$i];
5137 my $comment = $tag{'subject'};
5139 if (defined $comment) {
5140 $comment_short = chop_str
($comment, 30, 5);
5143 print "<tr class=\"dark\">\n";
5145 print "<tr class=\"light\">\n";
5148 if (defined $tag{'age'}) {
5149 print "<td><i>$tag{'age'}</i></td>\n";
5151 print "<td></td>\n";
5154 $cgi->a({-href
=> href
(action
=>$tag{'reftype'}, hash
=>$tag{'refid'}),
5155 -class => "list name"}, esc_html
($tag{'name'})) .
5158 if (defined $comment) {
5159 print format_subject_html
($comment, $comment_short,
5160 href
(action
=>"tag", hash
=>$tag{'id'}));
5163 "<td class=\"selflink\">";
5164 if ($tag{'type'} eq "tag") {
5165 print $cgi->a({-href
=> href
(action
=>"tag", hash
=>$tag{'id'})}, "tag");
5170 "<td class=\"link\">" . " | " .
5171 $cgi->a({-href
=> href
(action
=>$tag{'reftype'}, hash
=>$tag{'refid'})}, $tag{'reftype'});
5172 if ($tag{'reftype'} eq "commit") {
5173 print " | " . $cgi->a({-href
=> href
(action
=>"shortlog", hash
=>$tag{'fullname'})}, "shortlog") .
5174 " | " . $cgi->a({-href
=> href
(action
=>"log", hash
=>$tag{'fullname'})}, "log");
5175 } elsif ($tag{'reftype'} eq "blob") {
5176 print " | " . $cgi->a({-href
=> href
(action
=>"blob_plain", hash
=>$tag{'refid'})}, "raw");
5181 if (defined $extra) {
5183 "<td colspan=\"5\">$extra</td>\n" .
5189 sub git_heads_body
{
5190 # uses global variable $project
5191 my ($headlist, $head, $from, $to, $extra) = @_;
5192 $from = 0 unless defined $from;
5193 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
5195 print "<table class=\"heads\">\n";
5197 for (my $i = $from; $i <= $to; $i++) {
5198 my $entry = $headlist->[$i];
5200 my $curr = $ref{'id'} eq $head;
5202 print "<tr class=\"dark\">\n";
5204 print "<tr class=\"light\">\n";
5207 print "<td><i>$ref{'age'}</i></td>\n" .
5208 ($curr ? "<td class=\"current_head\">" : "<td>") .
5209 $cgi->a({-href
=> href
(action
=>"shortlog", hash
=>$ref{'fullname'}),
5210 -class => "list name"},esc_html
($ref{'name'})) .
5212 "<td class=\"link\">" .
5213 $cgi->a({-href
=> href
(action
=>"shortlog", hash
=>$ref{'fullname'})}, "shortlog") . " | " .
5214 $cgi->a({-href
=> href
(action
=>"log", hash
=>$ref{'fullname'})}, "log") . " | " .
5215 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$ref{'fullname'}, hash_base
=>$ref{'fullname'})}, "tree") .
5219 if (defined $extra) {
5221 "<td colspan=\"3\">$extra</td>\n" .
5227 # Display a single remote block
5228 sub git_remote_block
{
5229 my ($remote, $rdata, $limit, $head) = @_;
5231 my $heads = $rdata->{'heads'};
5232 my $fetch = $rdata->{'fetch'};
5233 my $push = $rdata->{'push'};
5235 my $urls_table = "<table class=\"projects_list\">\n" ;
5237 if (defined $fetch) {
5238 if ($fetch eq $push) {
5239 $urls_table .= format_repo_url
("URL", $fetch);
5241 $urls_table .= format_repo_url
("Fetch URL", $fetch);
5242 $urls_table .= format_repo_url
("Push URL", $push) if defined $push;
5244 } elsif (defined $push) {
5245 $urls_table .= format_repo_url
("Push URL", $push);
5247 $urls_table .= format_repo_url
("", "No remote URL");
5250 $urls_table .= "</table>\n";
5253 if (defined $limit && $limit < @$heads) {
5254 $dots = $cgi->a({-href
=> href
(action
=>"remotes", hash
=>$remote)}, "...");
5258 git_heads_body
($heads, $head, 0, $limit, $dots);
5261 # Display a list of remote names with the respective fetch and push URLs
5262 sub git_remotes_list
{
5263 my ($remotedata, $limit) = @_;
5264 print "<table class=\"heads\">\n";
5266 my @remotes = sort keys %$remotedata;
5268 my $limited = $limit && $limit < @remotes;
5270 $#remotes = $limit - 1 if $limited;
5272 while (my $remote = shift @remotes) {
5273 my $rdata = $remotedata->{$remote};
5274 my $fetch = $rdata->{'fetch'};
5275 my $push = $rdata->{'push'};
5277 print "<tr class=\"dark\">\n";
5279 print "<tr class=\"light\">\n";
5283 $cgi->a({-href
=> href
(action
=>'remotes', hash
=>$remote),
5284 -class=> "list name"},esc_html
($remote)) .
5286 print "<td class=\"link\">" .
5287 (defined $fetch ? $cgi->a({-href
=> $fetch}, "fetch") : "fetch") .
5289 (defined $push ? $cgi->a({-href
=> $push}, "push") : "push") .
5297 "<td colspan=\"3\">" .
5298 $cgi->a({-href
=> href
(action
=>"remotes")}, "...") .
5299 "</td>\n" . "</tr>\n";
5305 # Display remote heads grouped by remote, unless there are too many
5306 # remotes, in which case we only display the remote names
5307 sub git_remotes_body
{
5308 my ($remotedata, $limit, $head) = @_;
5309 if ($limit and $limit < keys %$remotedata) {
5310 git_remotes_list
($remotedata, $limit);
5312 fill_remote_heads
($remotedata);
5313 while (my ($remote, $rdata) = each %$remotedata) {
5314 git_print_section
({-class=>"remote", -id
=>$remote},
5315 ["remotes", $remote, $remote], sub {
5316 git_remote_block
($remote, $rdata, $limit, $head);
5322 sub git_search_grep_body
{
5323 my ($commitlist, $from, $to, $extra) = @_;
5324 $from = 0 unless defined $from;
5325 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
5327 print "<table class=\"commit_search\">\n";
5329 for (my $i = $from; $i <= $to; $i++) {
5330 my %co = %{$commitlist->[$i]};
5334 my $commit = $co{'id'};
5336 print "<tr class=\"dark\">\n";
5338 print "<tr class=\"light\">\n";
5341 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5342 format_author_html
('td', \
%co, 15, 5) .
5344 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$co{'id'}),
5345 -class => "list subject"},
5346 chop_and_escape_str
($co{'title'}, 50) . "<br/>");
5347 my $comment = $co{'comment'};
5348 foreach my $line (@$comment) {
5349 if ($line =~ m/^(.*?)($search_regexp)(.*)$/i) {
5350 my ($lead, $match, $trail) = ($1, $2, $3);
5351 $match = chop_str
($match, 70, 5, 'center');
5352 my $contextlen = int((80 - length($match))/2);
5353 $contextlen = 30 if ($contextlen > 30);
5354 $lead = chop_str
($lead, $contextlen, 10, 'left');
5355 $trail = chop_str
($trail, $contextlen, 10, 'right');
5357 $lead = esc_html
($lead);
5358 $match = esc_html
($match);
5359 $trail = esc_html
($trail);
5361 print "$lead<span class=\"match\">$match</span>$trail<br />";
5365 "<td class=\"link\">" .
5366 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$co{'id'})}, "commit") .
5368 $cgi->a({-href
=> href
(action
=>"commitdiff", hash
=>$co{'id'})}, "commitdiff") .
5370 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$co{'tree'}, hash_base
=>$co{'id'})}, "tree");
5374 if (defined $extra) {
5376 "<td colspan=\"3\">$extra</td>\n" .
5382 ## ======================================================================
5383 ## ======================================================================
5386 sub git_project_list
{
5387 my $order = $input_params{'order'};
5388 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
5389 die_error
(400, "Unknown order parameter");
5392 my @list = git_get_projects_list
();
5394 die_error
(404, "No projects found");
5398 if (defined $home_text && -f
$home_text) {
5399 print "<div class=\"index_include\">\n";
5400 insert_file
($home_text);
5403 print $cgi->startform(-method => "get") .
5404 "<p class=\"projsearch\">Search:\n" .
5405 $cgi->textfield(-name
=> "s", -value
=> $searchtext) . "\n" .
5407 $cgi->end_form() . "\n";
5408 git_project_list_body
(\
@list, $order);
5413 my $order = $input_params{'order'};
5414 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
5415 die_error
(400, "Unknown order parameter");
5418 my @list = git_get_projects_list
($project);
5420 die_error
(404, "No forks found");
5424 git_print_page_nav
('','');
5425 git_print_header_div
('summary', "$project forks");
5426 git_project_list_body
(\
@list, $order);
5430 sub git_project_index
{
5431 my @projects = git_get_projects_list
($project);
5434 -type
=> 'text/plain',
5435 -charset
=> 'utf-8',
5436 -content_disposition
=> 'inline; filename="index.aux"');
5438 foreach my $pr (@projects) {
5439 if (!exists $pr->{'owner'}) {
5440 $pr->{'owner'} = git_get_project_owner
("$pr->{'path'}");
5443 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
5444 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
5445 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf
("%%%02X", ord($1))/eg
;
5446 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf
("%%%02X", ord($1))/eg
;
5450 print "$path $owner\n";
5455 my $descr = git_get_project_description
($project) || "none";
5456 my %co = parse_commit
("HEAD");
5457 my %cd = %co ? parse_date
($co{'committer_epoch'}, $co{'committer_tz'}) : ();
5458 my $head = $co{'id'};
5459 my $remote_heads = gitweb_check_feature
('remote_heads');
5461 my $owner = git_get_project_owner
($project);
5463 my $refs = git_get_references
();
5464 # These get_*_list functions return one more to allow us to see if
5465 # there are more ...
5466 my @taglist = git_get_tags_list
(16);
5467 my @headlist = git_get_heads_list
(16);
5468 my %remotedata = $remote_heads ? git_get_remotes_list
() : ();
5470 my $check_forks = gitweb_check_feature
('forks');
5473 @forklist = git_get_projects_list
($project);
5477 git_print_page_nav
('summary','', $head);
5479 print "<div class=\"title\"> </div>\n";
5480 print "<table class=\"projects_list\">\n" .
5481 "<tr id=\"metadata_desc\"><td>description</td><td>" . esc_html
($descr) . "</td></tr>\n" .
5482 "<tr id=\"metadata_owner\"><td>owner</td><td>" . esc_html
($owner) . "</td></tr>\n";
5483 if (defined $cd{'rfc2822'}) {
5484 print "<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
5487 # use per project git URL list in $projectroot/$project/cloneurl
5488 # or make project git URL from git base URL and project name
5489 my $url_tag = "URL";
5490 my @url_list = git_get_project_url_list
($project);
5491 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
5492 foreach my $git_url (@url_list) {
5493 next unless $git_url;
5494 print format_repo_url
($url_tag, $git_url);
5499 my $show_ctags = gitweb_check_feature
('ctags');
5501 my $ctags = git_get_project_ctags
($project);
5503 # without ability to add tags, don't show if there are none
5504 my $cloud = git_populate_project_tagcloud
($ctags);
5505 print "<tr id=\"metadata_ctags\">" .
5506 "<td>content tags</td>" .
5507 "<td>".git_show_project_tagcloud
($cloud, 48)."</td>" .
5514 # If XSS prevention is on, we don't include README.html.
5515 # TODO: Allow a readme in some safe format.
5516 if (!$prevent_xss && -s
"$projectroot/$project/README.html") {
5517 print "<div class=\"title\">readme</div>\n" .
5518 "<div class=\"readme\">\n";
5519 insert_file
("$projectroot/$project/README.html");
5520 print "\n</div>\n"; # class="readme"
5523 # we need to request one more than 16 (0..15) to check if
5525 my @commitlist = $head ? parse_commits
($head, 17) : ();
5527 git_print_header_div
('shortlog');
5528 git_shortlog_body
(\
@commitlist, 0, 15, $refs,
5529 $#commitlist <= 15 ? undef :
5530 $cgi->a({-href
=> href
(action
=>"shortlog")}, "..."));
5534 git_print_header_div
('tags');
5535 git_tags_body
(\
@taglist, 0, 15,
5536 $#taglist <= 15 ? undef :
5537 $cgi->a({-href
=> href
(action
=>"tags")}, "..."));
5541 git_print_header_div
('heads');
5542 git_heads_body
(\
@headlist, $head, 0, 15,
5543 $#headlist <= 15 ? undef :
5544 $cgi->a({-href
=> href
(action
=>"heads")}, "..."));
5548 git_print_header_div
('remotes');
5549 git_remotes_body
(\
%remotedata, 15, $head);
5553 git_print_header_div
('forks');
5554 git_project_list_body
(\
@forklist, 'age', 0, 15,
5555 $#forklist <= 15 ? undef :
5556 $cgi->a({-href
=> href
(action
=>"forks")}, "..."),
5564 my %tag = parse_tag
($hash);
5567 die_error
(404, "Unknown tag object");
5570 my $head = git_get_head_hash
($project);
5572 git_print_page_nav
('','', $head,undef,$head);
5573 git_print_header_div
('commit', esc_html
($tag{'name'}), $hash);
5574 print "<div class=\"title_text\">\n" .
5575 "<table class=\"object_header\">\n" .
5577 "<td>object</td>\n" .
5578 "<td>" . $cgi->a({-class => "list", -href
=> href
(action
=>$tag{'type'}, hash
=>$tag{'object'})},
5579 $tag{'object'}) . "</td>\n" .
5580 "<td class=\"link\">" . $cgi->a({-href
=> href
(action
=>$tag{'type'}, hash
=>$tag{'object'})},
5581 $tag{'type'}) . "</td>\n" .
5583 if (defined($tag{'author'})) {
5584 git_print_authorship_rows
(\
%tag, 'author');
5586 print "</table>\n\n" .
5588 print "<div class=\"page_body\">";
5589 my $comment = $tag{'comment'};
5590 foreach my $line (@$comment) {
5592 print esc_html
($line, -nbsp
=>1) . "<br/>\n";
5598 sub git_blame_common
{
5599 my $format = shift || 'porcelain';
5600 if ($format eq 'porcelain' && $cgi->param('js')) {
5601 $format = 'incremental';
5602 $action = 'blame_incremental'; # for page title etc
5606 gitweb_check_feature
('blame')
5607 or die_error
(403, "Blame view not allowed");
5610 die_error
(400, "No file name given") unless $file_name;
5611 $hash_base ||= git_get_head_hash
($project);
5612 die_error
(404, "Couldn't find base commit") unless $hash_base;
5613 my %co = parse_commit
($hash_base)
5614 or die_error
(404, "Commit not found");
5616 if (!defined $hash) {
5617 $hash = git_get_hash_by_path
($hash_base, $file_name, "blob")
5618 or die_error
(404, "Error looking up file");
5620 $ftype = git_get_type
($hash);
5621 if ($ftype !~ "blob") {
5622 die_error
(400, "Object is not a blob");
5627 if ($format eq 'incremental') {
5628 # get file contents (as base)
5629 open $fd, "-|", git_cmd
(), 'cat-file', 'blob', $hash
5630 or die_error
(500, "Open git-cat-file failed");
5631 } elsif ($format eq 'data') {
5632 # run git-blame --incremental
5633 open $fd, "-|", git_cmd
(), "blame", "--incremental",
5634 $hash_base, "--", $file_name
5635 or die_error
(500, "Open git-blame --incremental failed");
5637 # run git-blame --porcelain
5638 open $fd, "-|", git_cmd
(), "blame", '-p',
5639 $hash_base, '--', $file_name
5640 or die_error
(500, "Open git-blame --porcelain failed");
5643 # incremental blame data returns early
5644 if ($format eq 'data') {
5646 -type
=>"text/plain", -charset
=> "utf-8",
5647 -status
=> "200 OK");
5648 local $| = 1; # output autoflush
5651 or print "ERROR $!\n";
5654 if (defined $t0 && gitweb_check_feature
('timed')) {
5656 tv_interval
($t0, [ gettimeofday
() ]).
5657 ' '.$number_of_git_cmds;
5667 $cgi->a({-href
=> href
(action
=>"blob", -replay
=>1)},
5670 if ($format eq 'incremental') {
5672 $cgi->a({-href
=> href
(action
=>"blame", javascript
=>0, -replay
=>1)},
5673 "blame") . " (non-incremental)";
5676 $cgi->a({-href
=> href
(action
=>"blame_incremental", -replay
=>1)},
5677 "blame") . " (incremental)";
5681 $cgi->a({-href
=> href
(action
=>"history", -replay
=>1)},
5684 $cgi->a({-href
=> href
(action
=>$action, file_name
=>$file_name)},
5686 git_print_page_nav
('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
5687 git_print_header_div
('commit', esc_html
($co{'title'}), $hash_base);
5688 git_print_page_path
($file_name, $ftype, $hash_base);
5691 if ($format eq 'incremental') {
5692 print "<noscript>\n<div class=\"error\"><center><b>\n".
5693 "This page requires JavaScript to run.\n Use ".
5694 $cgi->a({-href
=> href
(action
=>'blame',javascript
=>0,-replay
=>1)},
5697 "</b></center></div>\n</noscript>\n";
5699 print qq
!<div id
="progress_bar" style
="width: 100%; background-color: yellow"></div
>\n!;
5702 print qq
!<div
class="page_body">\n!;
5703 print qq
!<div id
="progress_info">... / ...</div
>\n!
5704 if ($format eq 'incremental');
5705 print qq
!<table id
="blame_table" class="blame" width
="100%">\n!.
5706 #qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.
5708 qq
!<tr
><th
>Commit
</th><th>Line</th
><th
>Data
</th></tr
>\n!.
5712 my @rev_color = qw(light dark);
5713 my $num_colors = scalar(@rev_color);
5714 my $current_color = 0;
5716 if ($format eq 'incremental') {
5717 my $color_class = $rev_color[$current_color];
5722 while (my $line = <$fd>) {
5726 print qq
!<tr id
="l$linenr" class="$color_class">!.
5727 qq
!<td
class="sha1"><a href
=""> </a></td
>!.
5728 qq
!<td
class="linenr">!.
5729 qq
!<a
class="linenr" href
="">$linenr</a></td
>!;
5730 print qq
!<td
class="pre">! . esc_html
($line) . "</td>\n";
5734 } else { # porcelain, i.e. ordinary blame
5735 my %metainfo = (); # saves information about commits
5739 while (my $line = <$fd>) {
5741 # the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]
5742 # no <lines in group> for subsequent lines in group of lines
5743 my ($full_rev, $orig_lineno, $lineno, $group_size) =
5744 ($line =~ /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);
5745 if (!exists $metainfo{$full_rev}) {
5746 $metainfo{$full_rev} = { 'nprevious' => 0 };
5748 my $meta = $metainfo{$full_rev};
5750 while ($data = <$fd>) {
5752 last if ($data =~ s/^\t//); # contents of line
5753 if ($data =~ /^(\S+)(?: (.*))?$/) {
5754 $meta->{$1} = $2 unless exists $meta->{$1};
5756 if ($data =~ /^previous /) {
5757 $meta->{'nprevious'}++;
5760 my $short_rev = substr($full_rev, 0, 8);
5761 my $author = $meta->{'author'};
5763 parse_date
($meta->{'author-time'}, $meta->{'author-tz'});
5764 my $date = $date{'iso-tz'};
5766 $current_color = ($current_color + 1) % $num_colors;
5768 my $tr_class = $rev_color[$current_color];
5769 $tr_class .= ' boundary' if (exists $meta->{'boundary'});
5770 $tr_class .= ' no-previous' if ($meta->{'nprevious'} == 0);
5771 $tr_class .= ' multiple-previous' if ($meta->{'nprevious'} > 1);
5772 print "<tr id=\"l$lineno\" class=\"$tr_class\">\n";
5774 print "<td class=\"sha1\"";
5775 print " title=\"". esc_html
($author) . ", $date\"";
5776 print " rowspan=\"$group_size\"" if ($group_size > 1);
5778 print $cgi->a({-href
=> href
(action
=>"commit",
5780 file_name
=>$file_name)},
5781 esc_html
($short_rev));
5782 if ($group_size >= 2) {
5783 my @author_initials = ($author =~ /\b([[:upper:]])\B/g);
5784 if (@author_initials) {
5786 esc_html
(join('', @author_initials));
5792 # 'previous' <sha1 of parent commit> <filename at commit>
5793 if (exists $meta->{'previous'} &&
5794 $meta->{'previous'} =~ /^([a-fA-F0-9]{40}) (.*)$/) {
5795 $meta->{'parent'} = $1;
5796 $meta->{'file_parent'} = unquote
($2);
5799 exists($meta->{'parent'}) ?
5800 $meta->{'parent'} : $full_rev;
5801 my $linenr_filename =
5802 exists($meta->{'file_parent'}) ?
5803 $meta->{'file_parent'} : unquote
($meta->{'filename'});
5804 my $blamed = href
(action
=> 'blame',
5805 file_name
=> $linenr_filename,
5806 hash_base
=> $linenr_commit);
5807 print "<td class=\"linenr\">";
5808 print $cgi->a({ -href
=> "$blamed#l$orig_lineno",
5809 -class => "linenr" },
5812 print "<td class=\"pre\">" . esc_html
($data) . "</td>\n";
5820 "</table>\n"; # class="blame"
5821 print "</div>\n"; # class="blame_body"
5823 or print "Reading blob failed\n";
5832 sub git_blame_incremental
{
5833 git_blame_common
('incremental');
5836 sub git_blame_data
{
5837 git_blame_common
('data');
5841 my $head = git_get_head_hash
($project);
5843 git_print_page_nav
('','', $head,undef,$head,format_ref_views
('tags'));
5844 git_print_header_div
('summary', $project);
5846 my @tagslist = git_get_tags_list
();
5848 git_tags_body
(\
@tagslist);
5854 my $head = git_get_head_hash
($project);
5856 git_print_page_nav
('','', $head,undef,$head,format_ref_views
('heads'));
5857 git_print_header_div
('summary', $project);
5859 my @headslist = git_get_heads_list
();
5861 git_heads_body
(\
@headslist, $head);
5866 # used both for single remote view and for list of all the remotes
5868 gitweb_check_feature
('remote_heads')
5869 or die_error
(403, "Remote heads view is disabled");
5871 my $head = git_get_head_hash
($project);
5872 my $remote = $input_params{'hash'};
5874 my $remotedata = git_get_remotes_list
($remote);
5875 die_error
(500, "Unable to get remote information") unless defined $remotedata;
5877 unless (%$remotedata) {
5878 die_error
(404, defined $remote ?
5879 "Remote $remote not found" :
5880 "No remotes found");
5883 git_header_html
(undef, undef, -action_extra
=> $remote);
5884 git_print_page_nav
('', '', $head, undef, $head,
5885 format_ref_views
($remote ? '' : 'remotes'));
5887 fill_remote_heads
($remotedata);
5888 if (defined $remote) {
5889 git_print_header_div
('remotes', "$remote remote for $project");
5890 git_remote_block
($remote, $remotedata->{$remote}, undef, $head);
5892 git_print_header_div
('summary', "$project remotes");
5893 git_remotes_body
($remotedata, undef, $head);
5899 sub git_blob_plain
{
5903 if (!defined $hash) {
5904 if (defined $file_name) {
5905 my $base = $hash_base || git_get_head_hash
($project);
5906 $hash = git_get_hash_by_path
($base, $file_name, "blob")
5907 or die_error
(404, "Cannot find file");
5909 die_error
(400, "No file name defined");
5911 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5912 # blobs defined by non-textual hash id's can be cached
5916 open my $fd, "-|", git_cmd
(), "cat-file", "blob", $hash
5917 or die_error
(500, "Open git-cat-file blob '$hash' failed");
5919 # content-type (can include charset)
5920 $type = blob_contenttype
($fd, $file_name, $type);
5922 # "save as" filename, even when no $file_name is given
5923 my $save_as = "$hash";
5924 if (defined $file_name) {
5925 $save_as = $file_name;
5926 } elsif ($type =~ m/^text\//) {
5930 # With XSS prevention on, blobs of all types except a few known safe
5931 # ones are served with "Content-Disposition: attachment" to make sure
5932 # they don't run in our security domain. For certain image types,
5933 # blob view writes an <img> tag referring to blob_plain view, and we
5934 # want to be sure not to break that by serving the image as an
5935 # attachment (though Firefox 3 doesn't seem to care).
5936 my $sandbox = $prevent_xss &&
5937 $type !~ m!^(?:text/plain|image/(?:gif|png|jpeg))$!;
5941 -expires
=> $expires,
5942 -content_disposition
=>
5943 ($sandbox ? 'attachment' : 'inline')
5944 . '; filename="' . $save_as . '"');
5946 binmode STDOUT
, ':raw';
5948 binmode STDOUT
, ':utf8'; # as set at the beginning of gitweb.cgi
5955 if (!defined $hash) {
5956 if (defined $file_name) {
5957 my $base = $hash_base || git_get_head_hash
($project);
5958 $hash = git_get_hash_by_path
($base, $file_name, "blob")
5959 or die_error
(404, "Cannot find file");
5961 die_error
(400, "No file name defined");
5963 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5964 # blobs defined by non-textual hash id's can be cached
5968 my $have_blame = gitweb_check_feature
('blame');
5969 open my $fd, "-|", git_cmd
(), "cat-file", "blob", $hash
5970 or die_error
(500, "Couldn't cat $file_name, $hash");
5971 my $mimetype = blob_mimetype
($fd, $file_name);
5972 # use 'blob_plain' (aka 'raw') view for files that cannot be displayed
5973 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B
$fd) {
5975 return git_blob_plain
($mimetype);
5977 # we can have blame only for text/* mimetype
5978 $have_blame &&= ($mimetype =~ m!^text/!);
5980 my $highlight = gitweb_check_feature
('highlight');
5981 my $syntax = guess_file_syntax
($highlight, $mimetype, $file_name);
5982 $fd = run_highlighter
($fd, $highlight, $syntax)
5985 git_header_html
(undef, $expires);
5986 my $formats_nav = '';
5987 if (defined $hash_base && (my %co = parse_commit
($hash_base))) {
5988 if (defined $file_name) {
5991 $cgi->a({-href
=> href
(action
=>"blame", -replay
=>1)},
5996 $cgi->a({-href
=> href
(action
=>"history", -replay
=>1)},
5999 $cgi->a({-href
=> href
(action
=>"blob_plain", -replay
=>1)},
6002 $cgi->a({-href
=> href
(action
=>"blob",
6003 hash_base
=>"HEAD", file_name
=>$file_name)},
6007 $cgi->a({-href
=> href
(action
=>"blob_plain", -replay
=>1)},
6010 git_print_page_nav
('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
6011 git_print_header_div
('commit', esc_html
($co{'title'}), $hash_base);
6013 print "<div class=\"page_nav\">\n" .
6014 "<br/><br/></div>\n" .
6015 "<div class=\"title\">".esc_html
($hash)."</div>\n";
6017 git_print_page_path
($file_name, "blob", $hash_base);
6018 print "<div class=\"page_body\">\n";
6019 if ($mimetype =~ m!^image/!) {
6020 print qq
!<img type
="!.esc_attr($mimetype).qq!"!;
6022 print qq
! alt
="!.esc_attr($file_name).qq!" title
="!.esc_attr($file_name).qq!"!;
6025 href(action=>"blob_plain
", hash=>$hash,
6026 hash_base=>$hash_base, file_name=>$file_name) .
6030 while (my $line = <$fd>) {
6033 $line = untabify
($line);
6034 printf qq
!<div
class="pre"><a id
="l%i" href
="%s#l%i" class="linenr">%4i</a> %s</div
>\n!,
6035 $nr, href
(-replay
=> 1), $nr, $nr, $syntax ? $line : esc_html
($line, -nbsp
=>1);
6039 or print "Reading blob failed.\n";
6045 if (!defined $hash_base) {
6046 $hash_base = "HEAD";
6048 if (!defined $hash) {
6049 if (defined $file_name) {
6050 $hash = git_get_hash_by_path
($hash_base, $file_name, "tree");
6055 die_error
(404, "No such tree") unless defined($hash);
6057 my $show_sizes = gitweb_check_feature
('show-sizes');
6058 my $have_blame = gitweb_check_feature
('blame');
6063 open my $fd, "-|", git_cmd
(), "ls-tree", '-z',
6064 ($show_sizes ? '-l' : ()), @extra_options, $hash
6065 or die_error
(500, "Open git-ls-tree failed");
6066 @entries = map { chomp; $_ } <$fd>;
6068 or die_error
(404, "Reading tree failed");
6071 my $refs = git_get_references
();
6072 my $ref = format_ref_marker
($refs, $hash_base);
6075 if (defined $hash_base && (my %co = parse_commit
($hash_base))) {
6077 if (defined $file_name) {
6079 $cgi->a({-href
=> href
(action
=>"history", -replay
=>1)},
6081 $cgi->a({-href
=> href
(action
=>"tree",
6082 hash_base
=>"HEAD", file_name
=>$file_name)},
6085 my $snapshot_links = format_snapshot_links
($hash);
6086 if (defined $snapshot_links) {
6087 # FIXME: Should be available when we have no hash base as well.
6088 push @views_nav, $snapshot_links;
6090 git_print_page_nav
('tree','', $hash_base, undef, undef,
6091 join(' | ', @views_nav));
6092 git_print_header_div
('commit', esc_html
($co{'title'}) . $ref, $hash_base);
6095 print "<div class=\"page_nav\">\n";
6096 print "<br/><br/></div>\n";
6097 print "<div class=\"title\">".esc_html
($hash)."</div>\n";
6099 if (defined $file_name) {
6100 $basedir = $file_name;
6101 if ($basedir ne '' && substr($basedir, -1) ne '/') {
6104 git_print_page_path
($file_name, 'tree', $hash_base);
6106 print "<div class=\"page_body\">\n";
6107 print "<table class=\"tree\">\n";
6109 # '..' (top directory) link if possible
6110 if (defined $hash_base &&
6111 defined $file_name && $file_name =~ m![^/]+$!) {
6113 print "<tr class=\"dark\">\n";
6115 print "<tr class=\"light\">\n";
6119 my $up = $file_name;
6120 $up =~ s!/?[^/]+$!!;
6121 undef $up unless $up;
6122 # based on git_print_tree_entry
6123 print '<td class="mode">' . mode_str
('040000') . "</td>\n";
6124 print '<td class="size"> </td>'."\n" if $show_sizes;
6125 print '<td class="list">';
6126 print $cgi->a({-href
=> href
(action
=>"tree",
6127 hash_base
=>$hash_base,
6131 print "<td class=\"link\"></td>\n";
6135 foreach my $line (@entries) {
6136 my %t = parse_ls_tree_line
($line, -z
=> 1, -l
=> $show_sizes);
6139 print "<tr class=\"dark\">\n";
6141 print "<tr class=\"light\">\n";
6145 git_print_tree_entry
(\
%t, $basedir, $hash_base, $have_blame);
6149 print "</table>\n" .
6155 my ($project, $hash) = @_;
6157 # path/to/project.git -> project
6158 # path/to/project/.git -> project
6159 my $name = to_utf8
($project);
6160 $name =~ s
,([^/])/*\
.git
$,$1,;
6161 $name = basename
($name);
6163 $name =~ s/[[:cntrl:]]/?/g;
6166 if ($hash =~ /^[0-9a-fA-F]+$/) {
6167 # shorten SHA-1 hash
6168 my $full_hash = git_get_full_hash
($project, $hash);
6169 if ($full_hash =~ /^$hash/ && length($hash) > 7) {
6170 $ver = git_get_short_hash
($project, $hash);
6172 } elsif ($hash =~ m!^refs/tags/(.*)$!) {
6173 # tags don't need shortened SHA-1 hash
6176 # branches and other need shortened SHA-1 hash
6177 if ($hash =~ m!^refs/(?:heads|remotes)/(.*)$!) {
6180 $ver .= '-' . git_get_short_hash
($project, $hash);
6182 # in case of hierarchical branch names
6185 # name = project-version_string
6186 $name = "$name-$ver";
6188 return wantarray ? ($name, $name) : $name;
6192 my $format = $input_params{'snapshot_format'};
6193 if (!@snapshot_fmts) {
6194 die_error
(403, "Snapshots not allowed");
6196 # default to first supported snapshot format
6197 $format ||= $snapshot_fmts[0];
6198 if ($format !~ m/^[a-z0-9]+$/) {
6199 die_error
(400, "Invalid snapshot format parameter");
6200 } elsif (!exists($known_snapshot_formats{$format})) {
6201 die_error
(400, "Unknown snapshot format");
6202 } elsif ($known_snapshot_formats{$format}{'disabled'}) {
6203 die_error
(403, "Snapshot format not allowed");
6204 } elsif (!grep($_ eq $format, @snapshot_fmts)) {
6205 die_error
(403, "Unsupported snapshot format");
6208 my $type = git_get_type
("$hash^{}");
6210 die_error
(404, 'Object does not exist');
6211 } elsif ($type eq 'blob') {
6212 die_error
(400, 'Object is not a tree-ish');
6215 my ($name, $prefix) = snapshot_name
($project, $hash);
6216 my $filename = "$name$known_snapshot_formats{$format}{'suffix'}";
6217 my $cmd = quote_command
(
6218 git_cmd
(), 'archive',
6219 "--format=$known_snapshot_formats{$format}{'format'}",
6220 "--prefix=$prefix/", $hash);
6221 if (exists $known_snapshot_formats{$format}{'compressor'}) {
6222 $cmd .= ' | ' . quote_command
(@{$known_snapshot_formats{$format}{'compressor'}});
6225 $filename =~ s/(["\\])/\\$1/g;
6227 -type
=> $known_snapshot_formats{$format}{'type'},
6228 -content_disposition
=> 'inline; filename="' . $filename . '"',
6229 -status
=> '200 OK');
6231 open my $fd, "-|", $cmd
6232 or die_error
(500, "Execute git-archive failed");
6233 binmode STDOUT
, ':raw';
6235 binmode STDOUT
, ':utf8'; # as set at the beginning of gitweb.cgi
6239 sub git_log_generic
{
6240 my ($fmt_name, $body_subr, $base, $parent, $file_name, $file_hash) = @_;
6242 my $head = git_get_head_hash
($project);
6243 if (!defined $base) {
6246 if (!defined $page) {
6249 my $refs = git_get_references
();
6251 my $commit_hash = $base;
6252 if (defined $parent) {
6253 $commit_hash = "$parent..$base";
6256 parse_commits
($commit_hash, 101, (100 * $page),
6257 defined $file_name ? ($file_name, "--full-history") : ());
6260 if (!defined $file_hash && defined $file_name) {
6261 # some commits could have deleted file in question,
6262 # and not have it in tree, but one of them has to have it
6263 for (my $i = 0; $i < @commitlist; $i++) {
6264 $file_hash = git_get_hash_by_path
($commitlist[$i]{'id'}, $file_name);
6265 last if defined $file_hash;
6268 if (defined $file_hash) {
6269 $ftype = git_get_type
($file_hash);
6271 if (defined $file_name && !defined $ftype) {
6272 die_error
(500, "Unknown type of object");
6275 if (defined $file_name) {
6276 %co = parse_commit
($base)
6277 or die_error
(404, "Unknown commit object");
6281 my $paging_nav = format_paging_nav
($fmt_name, $page, $#commitlist >= 100);
6283 if ($#commitlist >= 100) {
6285 $cgi->a({-href
=> href
(-replay
=>1, page
=>$page+1),
6286 -accesskey
=> "n", -title
=> "Alt-n"}, "next");
6288 my $patch_max = gitweb_get_feature
('patches');
6289 if ($patch_max && !defined $file_name) {
6290 if ($patch_max < 0 || @commitlist <= $patch_max) {
6291 $paging_nav .= " ⋅ " .
6292 $cgi->a({-href
=> href
(action
=>"patches", -replay
=>1)},
6298 git_print_page_nav
($fmt_name,'', $hash,$hash,$hash, $paging_nav);
6299 if (defined $file_name) {
6300 git_print_header_div
('commit', esc_html
($co{'title'}), $base);
6302 git_print_header_div
('summary', $project)
6304 git_print_page_path
($file_name, $ftype, $hash_base)
6305 if (defined $file_name);
6307 $body_subr->(\
@commitlist, 0, 99, $refs, $next_link,
6308 $file_name, $file_hash, $ftype);
6314 git_log_generic
('log', \
&git_log_body
,
6315 $hash, $hash_parent);
6319 $hash ||= $hash_base || "HEAD";
6320 my %co = parse_commit
($hash)
6321 or die_error
(404, "Unknown commit object");
6323 my $parent = $co{'parent'};
6324 my $parents = $co{'parents'}; # listref
6326 # we need to prepare $formats_nav before any parameter munging
6328 if (!defined $parent) {
6330 $formats_nav .= '(initial)';
6331 } elsif (@$parents == 1) {
6332 # single parent commit
6335 $cgi->a({-href
=> href
(action
=>"commit",
6337 esc_html
(substr($parent, 0, 7))) .
6344 $cgi->a({-href
=> href
(action
=>"commit",
6346 esc_html
(substr($_, 0, 7)));
6350 if (gitweb_check_feature
('patches') && @$parents <= 1) {
6351 $formats_nav .= " | " .
6352 $cgi->a({-href
=> href
(action
=>"patch", -replay
=>1)},
6356 if (!defined $parent) {
6360 open my $fd, "-|", git_cmd
(), "diff-tree", '-r', "--no-commit-id",
6362 (@$parents <= 1 ? $parent : '-c'),
6364 or die_error
(500, "Open git-diff-tree failed");
6365 @difftree = map { chomp; $_ } <$fd>;
6366 close $fd or die_error
(404, "Reading git-diff-tree failed");
6368 # non-textual hash id's can be cached
6370 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6373 my $refs = git_get_references
();
6374 my $ref = format_ref_marker
($refs, $co{'id'});
6376 git_header_html
(undef, $expires);
6377 git_print_page_nav
('commit', '',
6378 $hash, $co{'tree'}, $hash,
6381 if (defined $co{'parent'}) {
6382 git_print_header_div
('commitdiff', esc_html
($co{'title'}) . $ref, $hash);
6384 git_print_header_div
('tree', esc_html
($co{'title'}) . $ref, $co{'tree'}, $hash);
6386 print "<div class=\"title_text\">\n" .
6387 "<table class=\"object_header\">\n";
6388 git_print_authorship_rows
(\
%co);
6389 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
6392 "<td class=\"sha1\">" .
6393 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$co{'tree'}, hash_base
=>$hash),
6394 class => "list"}, $co{'tree'}) .
6396 "<td class=\"link\">" .
6397 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$co{'tree'}, hash_base
=>$hash)},
6399 my $snapshot_links = format_snapshot_links
($hash);
6400 if (defined $snapshot_links) {
6401 print " | " . $snapshot_links;
6406 foreach my $par (@$parents) {
6409 "<td class=\"sha1\">" .
6410 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$par),
6411 class => "list"}, $par) .
6413 "<td class=\"link\">" .
6414 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$par)}, "commit") .
6416 $cgi->a({-href
=> href
(action
=>"commitdiff", hash
=>$hash, hash_parent
=>$par)}, "diff") .
6423 print "<div class=\"page_body\">\n";
6424 git_print_log
($co{'comment'});
6427 git_difftree_body
(\
@difftree, $hash, @$parents);
6433 # object is defined by:
6434 # - hash or hash_base alone
6435 # - hash_base and file_name
6438 # - hash or hash_base alone
6439 if ($hash || ($hash_base && !defined $file_name)) {
6440 my $object_id = $hash || $hash_base;
6442 open my $fd, "-|", quote_command
(
6443 git_cmd
(), 'cat-file', '-t', $object_id) . ' 2> /dev/null'
6444 or die_error
(404, "Object does not exist");
6448 or die_error
(404, "Object does not exist");
6450 # - hash_base and file_name
6451 } elsif ($hash_base && defined $file_name) {
6452 $file_name =~ s
,/+$,,;
6454 system(git_cmd
(), "cat-file", '-e', $hash_base) == 0
6455 or die_error
(404, "Base object does not exist");
6457 # here errors should not hapen
6458 open my $fd, "-|", git_cmd
(), "ls-tree", $hash_base, "--", $file_name
6459 or die_error
(500, "Open git-ls-tree failed");
6463 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
6464 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
6465 die_error
(404, "File or directory for given base does not exist");
6470 die_error
(400, "Not enough information to find object");
6473 print $cgi->redirect(-uri
=> href
(action
=>$type, -full
=>1,
6474 hash
=>$hash, hash_base
=>$hash_base,
6475 file_name
=>$file_name),
6476 -status
=> '302 Found');
6480 my $format = shift || 'html';
6487 # preparing $fd and %diffinfo for git_patchset_body
6489 if (defined $hash_base && defined $hash_parent_base) {
6490 if (defined $file_name) {
6492 open $fd, "-|", git_cmd
(), "diff-tree", '-r', @diff_opts,
6493 $hash_parent_base, $hash_base,
6494 "--", (defined $file_parent ? $file_parent : ()), $file_name
6495 or die_error
(500, "Open git-diff-tree failed");
6496 @difftree = map { chomp; $_ } <$fd>;
6498 or die_error
(404, "Reading git-diff-tree failed");
6500 or die_error
(404, "Blob diff not found");
6502 } elsif (defined $hash &&
6503 $hash =~ /[0-9a-fA-F]{40}/) {
6504 # try to find filename from $hash
6506 # read filtered raw output
6507 open $fd, "-|", git_cmd
(), "diff-tree", '-r', @diff_opts,
6508 $hash_parent_base, $hash_base, "--"
6509 or die_error
(500, "Open git-diff-tree failed");
6511 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
6513 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
6514 map { chomp; $_ } <$fd>;
6516 or die_error
(404, "Reading git-diff-tree failed");
6518 or die_error
(404, "Blob diff not found");
6521 die_error
(400, "Missing one of the blob diff parameters");
6524 if (@difftree > 1) {
6525 die_error
(400, "Ambiguous blob diff specification");
6528 %diffinfo = parse_difftree_raw_line
($difftree[0]);
6529 $file_parent ||= $diffinfo{'from_file'} || $file_name;
6530 $file_name ||= $diffinfo{'to_file'};
6532 $hash_parent ||= $diffinfo{'from_id'};
6533 $hash ||= $diffinfo{'to_id'};
6535 # non-textual hash id's can be cached
6536 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
6537 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
6542 open $fd, "-|", git_cmd
(), "diff-tree", '-r', @diff_opts,
6543 '-p', ($format eq 'html' ? "--full-index" : ()),
6544 $hash_parent_base, $hash_base,
6545 "--", (defined $file_parent ? $file_parent : ()), $file_name
6546 or die_error
(500, "Open git-diff-tree failed");
6549 # old/legacy style URI -- not generated anymore since 1.4.3.
6551 die_error
('404 Not Found', "Missing one of the blob diff parameters")
6555 if ($format eq 'html') {
6557 $cgi->a({-href
=> href
(action
=>"blobdiff_plain", -replay
=>1)},
6559 git_header_html
(undef, $expires);
6560 if (defined $hash_base && (my %co = parse_commit
($hash_base))) {
6561 git_print_page_nav
('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
6562 git_print_header_div
('commit', esc_html
($co{'title'}), $hash_base);
6564 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
6565 print "<div class=\"title\">".esc_html
("$hash vs $hash_parent")."</div>\n";
6567 if (defined $file_name) {
6568 git_print_page_path
($file_name, "blob", $hash_base);
6570 print "<div class=\"page_path\"></div>\n";
6573 } elsif ($format eq 'plain') {
6575 -type
=> 'text/plain',
6576 -charset
=> 'utf-8',
6577 -expires
=> $expires,
6578 -content_disposition
=> 'inline; filename="' . "$file_name" . '.patch"');
6580 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
6583 die_error
(400, "Unknown blobdiff format");
6587 if ($format eq 'html') {
6588 print "<div class=\"page_body\">\n";
6590 git_patchset_body
($fd, [ \
%diffinfo ], $hash_base, $hash_parent_base);
6593 print "</div>\n"; # class="page_body"
6597 while (my $line = <$fd>) {
6598 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
6599 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
6603 last if $line =~ m!^\+\+\+!;
6611 sub git_blobdiff_plain
{
6612 git_blobdiff
('plain');
6615 sub git_commitdiff
{
6617 my $format = $params{-format
} || 'html';
6619 my ($patch_max) = gitweb_get_feature
('patches');
6620 if ($format eq 'patch') {
6621 die_error
(403, "Patch view not allowed") unless $patch_max;
6624 $hash ||= $hash_base || "HEAD";
6625 my %co = parse_commit
($hash)
6626 or die_error
(404, "Unknown commit object");
6628 # choose format for commitdiff for merge
6629 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
6630 $hash_parent = '--cc';
6632 # we need to prepare $formats_nav before almost any parameter munging
6634 if ($format eq 'html') {
6636 $cgi->a({-href
=> href
(action
=>"commitdiff_plain", -replay
=>1)},
6638 if ($patch_max && @{$co{'parents'}} <= 1) {
6639 $formats_nav .= " | " .
6640 $cgi->a({-href
=> href
(action
=>"patch", -replay
=>1)},
6644 if (defined $hash_parent &&
6645 $hash_parent ne '-c' && $hash_parent ne '--cc') {
6646 # commitdiff with two commits given
6647 my $hash_parent_short = $hash_parent;
6648 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
6649 $hash_parent_short = substr($hash_parent, 0, 7);
6653 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
6654 if ($co{'parents'}[$i] eq $hash_parent) {
6655 $formats_nav .= ' parent ' . ($i+1);
6659 $formats_nav .= ': ' .
6660 $cgi->a({-href
=> href
(action
=>"commitdiff",
6661 hash
=>$hash_parent)},
6662 esc_html
($hash_parent_short)) .
6664 } elsif (!$co{'parent'}) {
6666 $formats_nav .= ' (initial)';
6667 } elsif (scalar @{$co{'parents'}} == 1) {
6668 # single parent commit
6671 $cgi->a({-href
=> href
(action
=>"commitdiff",
6672 hash
=>$co{'parent'})},
6673 esc_html
(substr($co{'parent'}, 0, 7))) .
6677 if ($hash_parent eq '--cc') {
6678 $formats_nav .= ' | ' .
6679 $cgi->a({-href
=> href
(action
=>"commitdiff",
6680 hash
=>$hash, hash_parent
=>'-c')},
6682 } else { # $hash_parent eq '-c'
6683 $formats_nav .= ' | ' .
6684 $cgi->a({-href
=> href
(action
=>"commitdiff",
6685 hash
=>$hash, hash_parent
=>'--cc')},
6691 $cgi->a({-href
=> href
(action
=>"commitdiff",
6693 esc_html
(substr($_, 0, 7)));
6694 } @{$co{'parents'}} ) .
6699 my $hash_parent_param = $hash_parent;
6700 if (!defined $hash_parent_param) {
6701 # --cc for multiple parents, --root for parentless
6702 $hash_parent_param =
6703 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
6709 if ($format eq 'html') {
6710 open $fd, "-|", git_cmd
(), "diff-tree", '-r', @diff_opts,
6711 "--no-commit-id", "--patch-with-raw", "--full-index",
6712 $hash_parent_param, $hash, "--"
6713 or die_error
(500, "Open git-diff-tree failed");
6715 while (my $line = <$fd>) {
6717 # empty line ends raw part of diff-tree output
6719 push @difftree, scalar parse_difftree_raw_line
($line);
6722 } elsif ($format eq 'plain') {
6723 open $fd, "-|", git_cmd
(), "diff-tree", '-r', @diff_opts,
6724 '-p', $hash_parent_param, $hash, "--"
6725 or die_error
(500, "Open git-diff-tree failed");
6726 } elsif ($format eq 'patch') {
6727 # For commit ranges, we limit the output to the number of
6728 # patches specified in the 'patches' feature.
6729 # For single commits, we limit the output to a single patch,
6730 # diverging from the git-format-patch default.
6731 my @commit_spec = ();
6733 if ($patch_max > 0) {
6734 push @commit_spec, "-$patch_max";
6736 push @commit_spec, '-n', "$hash_parent..$hash";
6738 if ($params{-single
}) {
6739 push @commit_spec, '-1';
6741 if ($patch_max > 0) {
6742 push @commit_spec, "-$patch_max";
6744 push @commit_spec, "-n";
6746 push @commit_spec, '--root', $hash;
6748 open $fd, "-|", git_cmd
(), "format-patch", @diff_opts,
6749 '--encoding=utf8', '--stdout', @commit_spec
6750 or die_error
(500, "Open git-format-patch failed");
6752 die_error
(400, "Unknown commitdiff format");
6755 # non-textual hash id's can be cached
6757 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6761 # write commit message
6762 if ($format eq 'html') {
6763 my $refs = git_get_references
();
6764 my $ref = format_ref_marker
($refs, $co{'id'});
6766 git_header_html
(undef, $expires);
6767 git_print_page_nav
('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
6768 git_print_header_div
('commit', esc_html
($co{'title'}) . $ref, $hash);
6769 print "<div class=\"title_text\">\n" .
6770 "<table class=\"object_header\">\n";
6771 git_print_authorship_rows
(\
%co);
6774 print "<div class=\"page_body\">\n";
6775 if (@{$co{'comment'}} > 1) {
6776 print "<div class=\"log\">\n";
6777 git_print_log
($co{'comment'}, -final_empty_line
=> 1, -remove_title
=> 1);
6778 print "</div>\n"; # class="log"
6781 } elsif ($format eq 'plain') {
6782 my $refs = git_get_references
("tags");
6783 my $tagname = git_get_rev_name_tags
($hash);
6784 my $filename = basename
($project) . "-$hash.patch";
6787 -type
=> 'text/plain',
6788 -charset
=> 'utf-8',
6789 -expires
=> $expires,
6790 -content_disposition
=> 'inline; filename="' . "$filename" . '"');
6791 my %ad = parse_date
($co{'author_epoch'}, $co{'author_tz'});
6792 print "From: " . to_utf8
($co{'author'}) . "\n";
6793 print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
6794 print "Subject: " . to_utf8
($co{'title'}) . "\n";
6796 print "X-Git-Tag: $tagname\n" if $tagname;
6797 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
6799 foreach my $line (@{$co{'comment'}}) {
6800 print to_utf8
($line) . "\n";
6803 } elsif ($format eq 'patch') {
6804 my $filename = basename
($project) . "-$hash.patch";
6807 -type
=> 'text/plain',
6808 -charset
=> 'utf-8',
6809 -expires
=> $expires,
6810 -content_disposition
=> 'inline; filename="' . "$filename" . '"');
6814 if ($format eq 'html') {
6815 my $use_parents = !defined $hash_parent ||
6816 $hash_parent eq '-c' || $hash_parent eq '--cc';
6817 git_difftree_body
(\
@difftree, $hash,
6818 $use_parents ? @{$co{'parents'}} : $hash_parent);
6821 git_patchset_body
($fd, \
@difftree, $hash,
6822 $use_parents ? @{$co{'parents'}} : $hash_parent);
6824 print "</div>\n"; # class="page_body"
6827 } elsif ($format eq 'plain') {
6831 or print "Reading git-diff-tree failed\n";
6832 } elsif ($format eq 'patch') {
6836 or print "Reading git-format-patch failed\n";
6840 sub git_commitdiff_plain
{
6841 git_commitdiff
(-format
=> 'plain');
6844 # format-patch-style patches
6846 git_commitdiff
(-format
=> 'patch', -single
=> 1);
6850 git_commitdiff
(-format
=> 'patch');
6854 git_log_generic
('history', \
&git_history_body
,
6855 $hash_base, $hash_parent_base,
6860 gitweb_check_feature
('search') or die_error
(403, "Search is disabled");
6861 if (!defined $searchtext) {
6862 die_error
(400, "Text field is empty");
6864 if (!defined $hash) {
6865 $hash = git_get_head_hash
($project);
6867 my %co = parse_commit
($hash);
6869 die_error
(404, "Unknown commit object");
6871 if (!defined $page) {
6875 $searchtype ||= 'commit';
6876 if ($searchtype eq 'pickaxe') {
6877 # pickaxe may take all resources of your box and run for several minutes
6878 # with every query - so decide by yourself how public you make this feature
6879 gitweb_check_feature
('pickaxe')
6880 or die_error
(403, "Pickaxe is disabled");
6882 if ($searchtype eq 'grep') {
6883 gitweb_check_feature
('grep')[0]
6884 or die_error
(403, "Grep is disabled");
6889 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
6891 if ($searchtype eq 'commit') {
6892 $greptype = "--grep=";
6893 } elsif ($searchtype eq 'author') {
6894 $greptype = "--author=";
6895 } elsif ($searchtype eq 'committer') {
6896 $greptype = "--committer=";
6898 $greptype .= $searchtext;
6899 my @commitlist = parse_commits
($hash, 101, (100 * $page), undef,
6900 $greptype, '--regexp-ignore-case',
6901 $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
6903 my $paging_nav = '';
6906 $cgi->a({-href
=> href
(action
=>"search", hash
=>$hash,
6907 searchtext
=>$searchtext,
6908 searchtype
=>$searchtype)},
6910 $paging_nav .= " ⋅ " .
6911 $cgi->a({-href
=> href
(-replay
=>1, page
=>$page-1),
6912 -accesskey
=> "p", -title
=> "Alt-p"}, "prev");
6914 $paging_nav .= "first";
6915 $paging_nav .= " ⋅ prev";
6918 if ($#commitlist >= 100) {
6920 $cgi->a({-href
=> href
(-replay
=>1, page
=>$page+1),
6921 -accesskey
=> "n", -title
=> "Alt-n"}, "next");
6922 $paging_nav .= " ⋅ $next_link";
6924 $paging_nav .= " ⋅ next";
6927 git_print_page_nav
('','', $hash,$co{'tree'},$hash, $paging_nav);
6928 git_print_header_div
('commit', esc_html
($co{'title'}), $hash);
6929 if ($page == 0 && !@commitlist) {
6930 print "<p>No match.</p>\n";
6932 git_search_grep_body
(\
@commitlist, 0, 99, $next_link);
6936 if ($searchtype eq 'pickaxe') {
6937 git_print_page_nav
('','', $hash,$co{'tree'},$hash);
6938 git_print_header_div
('commit', esc_html
($co{'title'}), $hash);
6940 print "<table class=\"pickaxe search\">\n";
6943 open my $fd, '-|', git_cmd
(), '--no-pager', 'log', @diff_opts,
6944 '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
6945 ($search_use_regexp ? '--pickaxe-regex' : ());
6948 while (my $line = <$fd>) {
6952 my %set = parse_difftree_raw_line
($line);
6953 if (defined $set{'commit'}) {
6954 # finish previous commit
6957 "<td class=\"link\">" .
6958 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$co{'id'})}, "commit") .
6960 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$co{'tree'}, hash_base
=>$co{'id'})}, "tree");
6966 print "<tr class=\"dark\">\n";
6968 print "<tr class=\"light\">\n";
6971 %co = parse_commit
($set{'commit'});
6972 my $author = chop_and_escape_str
($co{'author_name'}, 15, 5);
6973 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
6974 "<td><i>$author</i></td>\n" .
6976 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$co{'id'}),
6977 -class => "list subject"},
6978 chop_and_escape_str
($co{'title'}, 50) . "<br/>");
6979 } elsif (defined $set{'to_id'}) {
6980 next if ($set{'to_id'} =~ m/^0{40}$/);
6982 print $cgi->a({-href
=> href
(action
=>"blob", hash_base
=>$co{'id'},
6983 hash
=>$set{'to_id'}, file_name
=>$set{'to_file'}),
6985 "<span class=\"match\">" . esc_path
($set{'file'}) . "</span>") .
6991 # finish last commit (warning: repetition!)
6994 "<td class=\"link\">" .
6995 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$co{'id'})}, "commit") .
6997 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$co{'tree'}, hash_base
=>$co{'id'})}, "tree");
7005 if ($searchtype eq 'grep') {
7006 git_print_page_nav
('','', $hash,$co{'tree'},$hash);
7007 git_print_header_div
('commit', esc_html
($co{'title'}), $hash);
7009 print "<table class=\"grep_search\">\n";
7013 open my $fd, "-|", git_cmd
(), 'grep', '-n',
7014 $search_use_regexp ? ('-E', '-i') : '-F',
7015 $searchtext, $co{'tree'};
7017 while (my $line = <$fd>) {
7019 my ($file, $lno, $ltext, $binary);
7020 last if ($matches++ > 1000);
7021 if ($line =~ /^Binary file (.+) matches$/) {
7025 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
7027 if ($file ne $lastfile) {
7028 $lastfile and print "</td></tr>\n";
7030 print "<tr class=\"dark\">\n";
7032 print "<tr class=\"light\">\n";
7034 print "<td class=\"list\">".
7035 $cgi->a({-href
=> href
(action
=>"blob", hash
=>$co{'hash'},
7036 file_name
=>"$file"),
7037 -class => "list"}, esc_path
($file));
7038 print "</td><td>\n";
7042 print "<div class=\"binary\">Binary file</div>\n";
7044 $ltext = untabify
($ltext);
7045 if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
7046 $ltext = esc_html
($1, -nbsp
=>1);
7047 $ltext .= '<span class="match">';
7048 $ltext .= esc_html
($2, -nbsp
=>1);
7049 $ltext .= '</span>';
7050 $ltext .= esc_html
($3, -nbsp
=>1);
7052 $ltext = esc_html
($ltext, -nbsp
=>1);
7054 print "<div class=\"pre\">" .
7055 $cgi->a({-href
=> href
(action
=>"blob", hash
=>$co{'hash'},
7056 file_name
=>"$file").'#l'.$lno,
7057 -class => "linenr"}, sprintf('%4i', $lno))
7058 . ' ' . $ltext . "</div>\n";
7062 print "</td></tr>\n";
7063 if ($matches > 1000) {
7064 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
7067 print "<div class=\"diff nodifferences\">No matches found</div>\n";
7076 sub git_search_help
{
7078 git_print_page_nav
('','', $hash,$hash,$hash);
7080 <p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without
7081 regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,
7082 the pattern entered is recognized as the POSIX extended
7083 <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case
7086 <dt><b>commit</b></dt>
7087 <dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
7089 my $have_grep = gitweb_check_feature
('grep');
7092 <dt><b>grep</b></dt>
7093 <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
7094 a different one) are searched for the given pattern. On large trees, this search can take
7095 a while and put some strain on the server, so please use it with some consideration. Note that
7096 due to git-grep peculiarity, currently if regexp mode is turned off, the matches are
7097 case-sensitive.</dd>
7101 <dt><b>author</b></dt>
7102 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>
7103 <dt><b>committer</b></dt>
7104 <dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>
7106 my $have_pickaxe = gitweb_check_feature
('pickaxe');
7107 if ($have_pickaxe) {
7109 <dt><b>pickaxe</b></dt>
7110 <dd>All commits that caused the string to appear or disappear from any file (changes that
7111 added, removed or "modified" the string) will be listed. This search can take a while and
7112 takes a lot of strain on the server, so please use it wisely. Note that since you may be
7113 interested even in changes just changing the case as well, this search is case sensitive.</dd>
7121 git_log_generic
('shortlog', \
&git_shortlog_body
,
7122 $hash, $hash_parent);
7125 ## ......................................................................
7126 ## feeds (RSS, Atom; OPML)
7129 my $format = shift || 'atom';
7130 my $have_blame = gitweb_check_feature
('blame');
7132 # Atom: http://www.atomenabled.org/developers/syndication/
7133 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
7134 if ($format ne 'rss' && $format ne 'atom') {
7135 die_error
(400, "Unknown web feed format");
7138 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
7139 my $head = $hash || 'HEAD';
7140 my @commitlist = parse_commits
($head, 150, 0, $file_name);
7144 my $content_type = "application/$format+xml";
7145 if (defined $cgi->http('HTTP_ACCEPT') &&
7146 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
7147 # browser (feed reader) prefers text/xml
7148 $content_type = 'text/xml';
7150 if (defined($commitlist[0])) {
7151 %latest_commit = %{$commitlist[0]};
7152 my $latest_epoch = $latest_commit{'committer_epoch'};
7153 %latest_date = parse_date
($latest_epoch, $latest_commit{'comitter_tz'});
7154 my $if_modified = $cgi->http('IF_MODIFIED_SINCE');
7155 if (defined $if_modified) {
7157 if (eval { require HTTP
::Date
; 1; }) {
7158 $since = HTTP
::Date
::str2time
($if_modified);
7159 } elsif (eval { require Time
::ParseDate
; 1; }) {
7160 $since = Time
::ParseDate
::parsedate
($if_modified, GMT
=> 1);
7162 if (defined $since && $latest_epoch <= $since) {
7164 -type
=> $content_type,
7165 -charset
=> 'utf-8',
7166 -last_modified
=> $latest_date{'rfc2822'},
7167 -status
=> '304 Not Modified');
7172 -type
=> $content_type,
7173 -charset
=> 'utf-8',
7174 -last_modified
=> $latest_date{'rfc2822'});
7177 -type
=> $content_type,
7178 -charset
=> 'utf-8');
7181 # Optimization: skip generating the body if client asks only
7182 # for Last-Modified date.
7183 return if ($cgi->request_method() eq 'HEAD');
7186 my $title = "$site_name - $project/$action";
7187 my $feed_type = 'log';
7188 if (defined $hash) {
7189 $title .= " - '$hash'";
7190 $feed_type = 'branch log';
7191 if (defined $file_name) {
7192 $title .= " :: $file_name";
7193 $feed_type = 'history';
7195 } elsif (defined $file_name) {
7196 $title .= " - $file_name";
7197 $feed_type = 'history';
7199 $title .= " $feed_type";
7200 my $descr = git_get_project_description
($project);
7201 if (defined $descr) {
7202 $descr = esc_html
($descr);
7204 $descr = "$project " .
7205 ($format eq 'rss' ? 'RSS' : 'Atom') .
7208 my $owner = git_get_project_owner
($project);
7209 $owner = esc_html
($owner);
7213 if (defined $file_name) {
7214 $alt_url = href
(-full
=>1, action
=>"history", hash
=>$hash, file_name
=>$file_name);
7215 } elsif (defined $hash) {
7216 $alt_url = href
(-full
=>1, action
=>"log", hash
=>$hash);
7218 $alt_url = href
(-full
=>1, action
=>"summary");
7220 print qq
!<?xml version
="1.0" encoding
="utf-8"?>\n!;
7221 if ($format eq 'rss') {
7223 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
7226 print "<title>$title</title>\n" .
7227 "<link>$alt_url</link>\n" .
7228 "<description>$descr</description>\n" .
7229 "<language>en</language>\n" .
7230 # project owner is responsible for 'editorial' content
7231 "<managingEditor>$owner</managingEditor>\n";
7232 if (defined $logo || defined $favicon) {
7233 # prefer the logo to the favicon, since RSS
7234 # doesn't allow both
7235 my $img = esc_url
($logo || $favicon);
7237 "<url>$img</url>\n" .
7238 "<title>$title</title>\n" .
7239 "<link>$alt_url</link>\n" .
7243 print "<pubDate>$latest_date{'rfc2822'}</pubDate>\n";
7244 print "<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";
7246 print "<generator>gitweb v.$version/$git_version</generator>\n";
7247 } elsif ($format eq 'atom') {
7249 <feed xmlns="http://www.w3.org/2005/Atom">
7251 print "<title>$title</title>\n" .
7252 "<subtitle>$descr</subtitle>\n" .
7253 '<link rel="alternate" type="text/html" href="' .
7254 $alt_url . '" />' . "\n" .
7255 '<link rel="self" type="' . $content_type . '" href="' .
7256 $cgi->self_url() . '" />' . "\n" .
7257 "<id>" . href
(-full
=>1) . "</id>\n" .
7258 # use project owner for feed author
7259 "<author><name>$owner</name></author>\n";
7260 if (defined $favicon) {
7261 print "<icon>" . esc_url
($favicon) . "</icon>\n";
7263 if (defined $logo) {
7264 # not twice as wide as tall: 72 x 27 pixels
7265 print "<logo>" . esc_url
($logo) . "</logo>\n";
7267 if (! %latest_date) {
7268 # dummy date to keep the feed valid until commits trickle in:
7269 print "<updated>1970-01-01T00:00:00Z</updated>\n";
7271 print "<updated>$latest_date{'iso-8601'}</updated>\n";
7273 print "<generator version='$version/$git_version'>gitweb</generator>\n";
7277 for (my $i = 0; $i <= $#commitlist; $i++) {
7278 my %co = %{$commitlist[$i]};
7279 my $commit = $co{'id'};
7280 # we read 150, we always show 30 and the ones more recent than 48 hours
7281 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
7284 my %cd = parse_date
($co{'author_epoch'}, $co{'author_tz'});
7286 # get list of changed files
7287 open my $fd, "-|", git_cmd
(), "diff-tree", '-r', @diff_opts,
7288 $co{'parent'} || "--root",
7289 $co{'id'}, "--", (defined $file_name ? $file_name : ())
7291 my @difftree = map { chomp; $_ } <$fd>;
7295 # print element (entry, item)
7296 my $co_url = href
(-full
=>1, action
=>"commitdiff", hash
=>$commit);
7297 if ($format eq 'rss') {
7299 "<title>" . esc_html
($co{'title'}) . "</title>\n" .
7300 "<author>" . esc_html
($co{'author'}) . "</author>\n" .
7301 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
7302 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
7303 "<link>$co_url</link>\n" .
7304 "<description>" . esc_html
($co{'title'}) . "</description>\n" .
7305 "<content:encoded>" .
7307 } elsif ($format eq 'atom') {
7309 "<title type=\"html\">" . esc_html
($co{'title'}) . "</title>\n" .
7310 "<updated>$cd{'iso-8601'}</updated>\n" .
7312 " <name>" . esc_html
($co{'author_name'}) . "</name>\n";
7313 if ($co{'author_email'}) {
7314 print " <email>" . esc_html
($co{'author_email'}) . "</email>\n";
7316 print "</author>\n" .
7317 # use committer for contributor
7319 " <name>" . esc_html
($co{'committer_name'}) . "</name>\n";
7320 if ($co{'committer_email'}) {
7321 print " <email>" . esc_html
($co{'committer_email'}) . "</email>\n";
7323 print "</contributor>\n" .
7324 "<published>$cd{'iso-8601'}</published>\n" .
7325 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
7326 "<id>$co_url</id>\n" .
7327 "<content type=\"xhtml\" xml:base=\"" . esc_url
($my_url) . "\">\n" .
7328 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
7330 my $comment = $co{'comment'};
7332 foreach my $line (@$comment) {
7333 $line = esc_html
($line);
7336 print "</pre><ul>\n";
7337 foreach my $difftree_line (@difftree) {
7338 my %difftree = parse_difftree_raw_line
($difftree_line);
7339 next if !$difftree{'from_id'};
7341 my $file = $difftree{'file'} || $difftree{'to_file'};
7345 $cgi->a({-href
=> href
(-full
=>1, action
=>"blobdiff",
7346 hash
=>$difftree{'to_id'}, hash_parent
=>$difftree{'from_id'},
7347 hash_base
=>$co{'id'}, hash_parent_base
=>$co{'parent'},
7348 file_name
=>$file, file_parent
=>$difftree{'from_file'}),
7349 -title
=> "diff"}, 'D');
7351 print $cgi->a({-href
=> href
(-full
=>1, action
=>"blame",
7352 file_name
=>$file, hash_base
=>$commit),
7353 -title
=> "blame"}, 'B');
7355 # if this is not a feed of a file history
7356 if (!defined $file_name || $file_name ne $file) {
7357 print $cgi->a({-href
=> href
(-full
=>1, action
=>"history",
7358 file_name
=>$file, hash
=>$commit),
7359 -title
=> "history"}, 'H');
7361 $file = esc_path
($file);
7365 if ($format eq 'rss') {
7366 print "</ul>]]>\n" .
7367 "</content:encoded>\n" .
7369 } elsif ($format eq 'atom') {
7370 print "</ul>\n</div>\n" .
7377 if ($format eq 'rss') {
7378 print "</channel>\n</rss>\n";
7379 } elsif ($format eq 'atom') {
7393 my @list = git_get_projects_list
();
7396 -type
=> 'text/xml',
7397 -charset
=> 'utf-8',
7398 -content_disposition
=> 'inline; filename="opml.xml"');
7401 <?xml version="1.0" encoding="utf-8"?>
7402 <opml version="1.0">
7404 <title>$site_name OPML Export</title>
7407 <outline text="git RSS feeds">
7410 foreach my $pr (@list) {
7412 my $head = git_get_head_hash
($proj{'path'});
7413 if (!defined $head) {
7416 $git_dir = "$projectroot/$proj{'path'}";
7417 my %co = parse_commit
($head);
7422 my $path = esc_html
(chop_str
($proj{'path'}, 25, 5));
7423 my $rss = href
('project' => $proj{'path'}, 'action' => 'rss', -full
=> 1);
7424 my $html = href
('project' => $proj{'path'}, 'action' => 'summary', -full
=> 1);
7425 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";