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 # group projects by category on the projects list
120 # (enabled if this variable evaluates to true)
121 our $projects_list_group_categories = 0;
123 # default category if none specified
124 # (leave the empty string for no category)
125 our $project_list_default_category = "";
127 # default order of projects list
128 # valid values are none, project, descr, owner, and age
129 our $default_projects_order = "project";
131 # show repository only if this file exists
132 # (only effective if this variable evaluates to true)
133 our $export_ok = "++GITWEB_EXPORT_OK++";
135 # show repository only if this subroutine returns true
136 # when given the path to the project, for example:
137 # sub { return -e "$_[0]/git-daemon-export-ok"; }
138 our $export_auth_hook = undef;
140 # only allow viewing of repositories also shown on the overview page
141 our $strict_export = "++GITWEB_STRICT_EXPORT++";
143 # list of git base URLs used for URL to where fetch project from,
144 # i.e. full URL is "$git_base_url/$project"
145 our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
147 # default blob_plain mimetype and default charset for text/plain blob
148 our $default_blob_plain_mimetype = 'text/plain';
149 our $default_text_plain_charset = undef;
151 # file to use for guessing MIME types before trying /etc/mime.types
152 # (relative to the current git repository)
153 our $mimetypes_file = undef;
155 # assume this charset if line contains non-UTF-8 characters;
156 # it should be valid encoding (see Encoding::Supported(3pm) for list),
157 # for which encoding all byte sequences are valid, for example
158 # 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
159 # could be even 'utf-8' for the old behavior)
160 our $fallback_encoding = 'latin1';
162 # rename detection options for git-diff and git-diff-tree
163 # - default is '-M', with the cost proportional to
164 # (number of removed files) * (number of new files).
165 # - more costly is '-C' (which implies '-M'), with the cost proportional to
166 # (number of changed files + number of removed files) * (number of new files)
167 # - even more costly is '-C', '--find-copies-harder' with cost
168 # (number of files in the original tree) * (number of new files)
169 # - one might want to include '-B' option, e.g. '-B', '-M'
170 our @diff_opts = ('-M'); # taken from git_commit
172 # Disables features that would allow repository owners to inject script into
174 our $prevent_xss = 0;
176 # Path to the highlight executable to use (must be the one from
177 # http://www.andre-simon.de due to assumptions about parameters and output).
178 # Useful if highlight is not installed on your webserver's PATH.
179 # [Default: highlight]
180 our $highlight_bin = "++HIGHLIGHT_BIN++";
182 # information about snapshot formats that gitweb is capable of serving
183 our %known_snapshot_formats = (
185 # 'display' => display name,
186 # 'type' => mime type,
187 # 'suffix' => filename suffix,
188 # 'format' => --format for git-archive,
189 # 'compressor' => [compressor command and arguments]
190 # (array reference, optional)
191 # 'disabled' => boolean (optional)}
194 'display' => 'tar.gz',
195 'type' => 'application/x-gzip',
196 'suffix' => '.tar.gz',
198 'compressor' => ['gzip', '-n']},
201 'display' => 'tar.bz2',
202 'type' => 'application/x-bzip2',
203 'suffix' => '.tar.bz2',
205 'compressor' => ['bzip2']},
208 'display' => 'tar.xz',
209 'type' => 'application/x-xz',
210 'suffix' => '.tar.xz',
212 'compressor' => ['xz'],
217 'type' => 'application/x-zip',
222 # Aliases so we understand old gitweb.snapshot values in repository
224 our %known_snapshot_format_aliases = (
229 # backward compatibility: legacy gitweb config support
230 'x-gzip' => undef, 'gz' => undef,
231 'x-bzip2' => undef, 'bz2' => undef,
232 'x-zip' => undef, '' => undef,
235 # Pixel sizes for icons and avatars. If the default font sizes or lineheights
236 # are changed, it may be appropriate to change these values too via
243 # Used to set the maximum load that we will still respond to gitweb queries.
244 # If server load exceed this value then return "503 server busy" error.
245 # If gitweb cannot determined server load, it is taken to be 0.
246 # Leave it undefined (or set to 'undef') to turn off load checking.
249 # configuration for 'highlight' (http://www.andre-simon.de/)
251 our %highlight_basename = (
254 'SConstruct' => 'py', # SCons equivalent of Makefile
255 'Makefile' => 'make',
258 our %highlight_ext = (
259 # main extensions, defining name of syntax;
260 # see files in /usr/share/highlight/langDefs/ directory
262 qw(py c cpp rb java css php sh pl js tex bib xml awk bat ini spec tcl sql make),
263 # alternate extensions, see /etc/highlight/filetypes.conf
265 map { $_ => 'sh' } qw(bash zsh ksh),
266 map { $_ => 'cpp' } qw(cxx c++ cc),
267 map { $_ => 'php' } qw(php3 php4 php5 phps),
268 map { $_ => 'pl' } qw(perl pm), # perhaps also 'cgi'
269 map { $_ => 'make'} qw(mak mk),
270 map { $_ => 'xml' } qw(xhtml html htm),
273 # You define site-wide feature defaults here; override them with
274 # $GITWEB_CONFIG as necessary.
277 # 'sub' => feature-sub (subroutine),
278 # 'override' => allow-override (boolean),
279 # 'default' => [ default options...] (array reference)}
281 # if feature is overridable (it means that allow-override has true value),
282 # then feature-sub will be called with default options as parameters;
283 # return value of feature-sub indicates if to enable specified feature
285 # if there is no 'sub' key (no feature-sub), then feature cannot be
288 # use gitweb_get_feature(<feature>) to retrieve the <feature> value
289 # (an array) or gitweb_check_feature(<feature>) to check if <feature>
292 # Enable the 'blame' blob view, showing the last commit that modified
293 # each line in the file. This can be very CPU-intensive.
295 # To enable system wide have in $GITWEB_CONFIG
296 # $feature{'blame'}{'default'} = [1];
297 # To have project specific config enable override in $GITWEB_CONFIG
298 # $feature{'blame'}{'override'} = 1;
299 # and in project config gitweb.blame = 0|1;
301 'sub' => sub { feature_bool
('blame', @_) },
305 # Enable the 'snapshot' link, providing a compressed archive of any
306 # tree. This can potentially generate high traffic if you have large
309 # Value is a list of formats defined in %known_snapshot_formats that
311 # To disable system wide have in $GITWEB_CONFIG
312 # $feature{'snapshot'}{'default'} = [];
313 # To have project specific config enable override in $GITWEB_CONFIG
314 # $feature{'snapshot'}{'override'} = 1;
315 # and in project config, a comma-separated list of formats or "none"
316 # to disable. Example: gitweb.snapshot = tbz2,zip;
318 'sub' => \
&feature_snapshot
,
320 'default' => ['tgz']},
322 # Enable text search, which will list the commits which match author,
323 # committer or commit text to a given string. Enabled by default.
324 # Project specific override is not supported.
329 # Enable grep search, which will list the files in currently selected
330 # tree containing the given string. Enabled by default. This can be
331 # potentially CPU-intensive, of course.
333 # To enable system wide have in $GITWEB_CONFIG
334 # $feature{'grep'}{'default'} = [1];
335 # To have project specific config enable override in $GITWEB_CONFIG
336 # $feature{'grep'}{'override'} = 1;
337 # and in project config gitweb.grep = 0|1;
339 'sub' => sub { feature_bool
('grep', @_) },
343 # Enable the pickaxe search, which will list the commits that modified
344 # a given string in a file. This can be practical and quite faster
345 # alternative to 'blame', but still potentially CPU-intensive.
347 # To enable system wide have in $GITWEB_CONFIG
348 # $feature{'pickaxe'}{'default'} = [1];
349 # To have project specific config enable override in $GITWEB_CONFIG
350 # $feature{'pickaxe'}{'override'} = 1;
351 # and in project config gitweb.pickaxe = 0|1;
353 'sub' => sub { feature_bool
('pickaxe', @_) },
357 # Enable showing size of blobs in a 'tree' view, in a separate
358 # column, similar to what 'ls -l' does. This cost a bit of IO.
360 # To disable system wide have in $GITWEB_CONFIG
361 # $feature{'show-sizes'}{'default'} = [0];
362 # To have project specific config enable override in $GITWEB_CONFIG
363 # $feature{'show-sizes'}{'override'} = 1;
364 # and in project config gitweb.showsizes = 0|1;
366 'sub' => sub { feature_bool
('showsizes', @_) },
370 # Make gitweb use an alternative format of the URLs which can be
371 # more readable and natural-looking: project name is embedded
372 # directly in the path and the query string contains other
373 # auxiliary information. All gitweb installations recognize
374 # URL in either format; this configures in which formats gitweb
377 # To enable system wide have in $GITWEB_CONFIG
378 # $feature{'pathinfo'}{'default'} = [1];
379 # Project specific override is not supported.
381 # Note that you will need to change the default location of CSS,
382 # favicon, logo and possibly other files to an absolute URL. Also,
383 # if gitweb.cgi serves as your indexfile, you will need to force
384 # $my_uri to contain the script name in your $GITWEB_CONFIG.
389 # Make gitweb consider projects in project root subdirectories
390 # to be forks of existing projects. Given project $projname.git,
391 # projects matching $projname/*.git will not be shown in the main
392 # projects list, instead a '+' mark will be added to $projname
393 # there and a 'forks' view will be enabled for the project, listing
394 # all the forks. If project list is taken from a file, forks have
395 # to be listed after the main project.
397 # To enable system wide have in $GITWEB_CONFIG
398 # $feature{'forks'}{'default'} = [1];
399 # Project specific override is not supported.
404 # Insert custom links to the action bar of all project pages.
405 # This enables you mainly to link to third-party scripts integrating
406 # into gitweb; e.g. git-browser for graphical history representation
407 # or custom web-based repository administration interface.
409 # The 'default' value consists of a list of triplets in the form
410 # (label, link, position) where position is the label after which
411 # to insert the link and link is a format string where %n expands
412 # to the project name, %f to the project path within the filesystem,
413 # %h to the current hash (h gitweb parameter) and %b to the current
414 # hash base (hb gitweb parameter); %% expands to %.
416 # To enable system wide have in $GITWEB_CONFIG e.g.
417 # $feature{'actions'}{'default'} = [('graphiclog',
418 # '/git-browser/by-commit.html?r=%n', 'summary')];
419 # Project specific override is not supported.
424 # Allow gitweb scan project content tags of project repository,
425 # and display the popular Web 2.0-ish "tag cloud" near the projects
426 # list. Note that this is something COMPLETELY different from the
429 # gitweb by itself can show existing tags, but it does not handle
430 # tagging itself; you need to do it externally, outside gitweb.
431 # The format is described in git_get_project_ctags() subroutine.
432 # You may want to install the HTML::TagCloud Perl module to get
433 # a pretty tag cloud instead of just a list of tags.
435 # To enable system wide have in $GITWEB_CONFIG
436 # $feature{'ctags'}{'default'} = [1];
437 # Project specific override is not supported.
439 # In the future whether ctags editing is enabled might depend
440 # on the value, but using 1 should always mean no editing of ctags.
445 # The maximum number of patches in a patchset generated in patch
446 # view. Set this to 0 or undef to disable patch view, or to a
447 # negative number to remove any limit.
449 # To disable system wide have in $GITWEB_CONFIG
450 # $feature{'patches'}{'default'} = [0];
451 # To have project specific config enable override in $GITWEB_CONFIG
452 # $feature{'patches'}{'override'} = 1;
453 # and in project config gitweb.patches = 0|n;
454 # where n is the maximum number of patches allowed in a patchset.
456 'sub' => \
&feature_patches
,
460 # Avatar support. When this feature is enabled, views such as
461 # shortlog or commit will display an avatar associated with
462 # the email of the committer(s) and/or author(s).
464 # Currently available providers are gravatar and picon.
465 # If an unknown provider is specified, the feature is disabled.
467 # Gravatar depends on Digest::MD5.
468 # Picon currently relies on the indiana.edu database.
470 # To enable system wide have in $GITWEB_CONFIG
471 # $feature{'avatar'}{'default'} = ['<provider>'];
472 # where <provider> is either gravatar or picon.
473 # To have project specific config enable override in $GITWEB_CONFIG
474 # $feature{'avatar'}{'override'} = 1;
475 # and in project config gitweb.avatar = <provider>;
477 'sub' => \
&feature_avatar
,
481 # Enable displaying how much time and how many git commands
482 # it took to generate and display page. Disabled by default.
483 # Project specific override is not supported.
488 # Enable turning some links into links to actions which require
489 # JavaScript to run (like 'blame_incremental'). Not enabled by
490 # default. Project specific override is currently not supported.
491 'javascript-actions' => {
495 # Syntax highlighting support. This is based on Daniel Svensson's
496 # and Sham Chukoury's work in gitweb-xmms2.git.
497 # It requires the 'highlight' program present in $PATH,
498 # and therefore is disabled by default.
500 # To enable system wide have in $GITWEB_CONFIG
501 # $feature{'highlight'}{'default'} = [1];
504 'sub' => sub { feature_bool
('highlight', @_) },
508 # Enable displaying of remote heads in the heads list
510 # To enable system wide have in $GITWEB_CONFIG
511 # $feature{'remote_heads'}{'default'} = [1];
512 # To have project specific config enable override in $GITWEB_CONFIG
513 # $feature{'remote_heads'}{'override'} = 1;
514 # and in project config gitweb.remote_heads = 0|1;
516 'sub' => sub { feature_bool
('remote_heads', @_) },
521 sub gitweb_get_feature
{
523 return unless exists $feature{$name};
524 my ($sub, $override, @defaults) = (
525 $feature{$name}{'sub'},
526 $feature{$name}{'override'},
527 @{$feature{$name}{'default'}});
528 # project specific override is possible only if we have project
529 our $git_dir; # global variable, declared later
530 if (!$override || !defined $git_dir) {
534 warn "feature $name is not overridable";
537 return $sub->(@defaults);
540 # A wrapper to check if a given feature is enabled.
541 # With this, you can say
543 # my $bool_feat = gitweb_check_feature('bool_feat');
544 # gitweb_check_feature('bool_feat') or somecode;
548 # my ($bool_feat) = gitweb_get_feature('bool_feat');
549 # (gitweb_get_feature('bool_feat'))[0] or somecode;
551 sub gitweb_check_feature
{
552 return (gitweb_get_feature
(@_))[0];
558 my ($val) = git_get_project_config
($key, '--bool');
562 } elsif ($val eq 'true') {
564 } elsif ($val eq 'false') {
569 sub feature_snapshot
{
572 my ($val) = git_get_project_config
('snapshot');
575 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
581 sub feature_patches
{
582 my @val = (git_get_project_config
('patches', '--int'));
592 my @val = (git_get_project_config
('avatar'));
594 return @val ? @val : @_;
597 # checking HEAD file with -e is fragile if the repository was
598 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
600 sub check_head_link
{
602 my $headfile = "$dir/HEAD";
603 return ((-e
$headfile) ||
604 (-l
$headfile && readlink($headfile) =~ /^refs\/heads\
//));
607 sub check_export_ok
{
609 return (check_head_link
($dir) &&
610 (!$export_ok || -e
"$dir/$export_ok") &&
611 (!$export_auth_hook || $export_auth_hook->($dir)));
614 # process alternate names for backward compatibility
615 # filter out unsupported (unknown) snapshot formats
616 sub filter_snapshot_fmts
{
620 exists $known_snapshot_format_aliases{$_} ?
621 $known_snapshot_format_aliases{$_} : $_} @fmts;
623 exists $known_snapshot_formats{$_} &&
624 !$known_snapshot_formats{$_}{'disabled'}} @fmts;
627 # If it is set to code reference, it is code that it is to be run once per
628 # request, allowing updating configurations that change with each request,
629 # while running other code in config file only once.
631 # Otherwise, if it is false then gitweb would process config file only once;
632 # if it is true then gitweb config would be run for each request.
633 our $per_request_config = 1;
635 our ($GITWEB_CONFIG, $GITWEB_CONFIG_SYSTEM);
636 sub evaluate_gitweb_config
{
637 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
638 our $GITWEB_CONFIG_SYSTEM = $ENV{'GITWEB_CONFIG_SYSTEM'} || "++GITWEB_CONFIG_SYSTEM++";
639 # die if there are errors parsing config file
640 if (-e
$GITWEB_CONFIG) {
643 } elsif (-e
$GITWEB_CONFIG_SYSTEM) {
644 do $GITWEB_CONFIG_SYSTEM;
649 # Get loadavg of system, to compare against $maxload.
650 # Currently it requires '/proc/loadavg' present to get loadavg;
651 # if it is not present it returns 0, which means no load checking.
653 if( -e
'/proc/loadavg' ){
654 open my $fd, '<', '/proc/loadavg'
656 my @load = split(/\s+/, scalar <$fd>);
659 # The first three columns measure CPU and IO utilization of the last one,
660 # five, and 10 minute periods. The fourth column shows the number of
661 # currently running processes and the total number of processes in the m/n
662 # format. The last column displays the last process ID used.
663 return $load[0] || 0;
665 # additional checks for load average should go here for things that don't export
671 # version of the core git binary
673 sub evaluate_git_version
{
674 our $git_version = qx("$GIT" --version) =~ m/git version (.*)$/ ? $1 : "unknown";
675 $number_of_git_cmds++;
679 if (defined $maxload && get_loadavg
() > $maxload) {
680 die_error
(503, "The load average on the server is too high");
684 # ======================================================================
685 # input validation and dispatch
687 # input parameters can be collected from a variety of sources (presently, CGI
688 # and PATH_INFO), so we define an %input_params hash that collects them all
689 # together during validation: this allows subsequent uses (e.g. href()) to be
690 # agnostic of the parameter origin
692 our %input_params = ();
694 # input parameters are stored with the long parameter name as key. This will
695 # also be used in the href subroutine to convert parameters to their CGI
696 # equivalent, and since the href() usage is the most frequent one, we store
697 # the name -> CGI key mapping here, instead of the reverse.
699 # XXX: Warning: If you touch this, check the search form for updating,
702 our @cgi_param_mapping = (
710 hash_parent_base
=> "hpb",
715 snapshot_format
=> "sf",
716 extra_options
=> "opt",
717 search_use_regexp
=> "sr",
719 # this must be last entry (for manipulation from JavaScript)
722 our %cgi_param_mapping = @cgi_param_mapping;
724 # we will also need to know the possible actions, for validation
726 "blame" => \
&git_blame
,
727 "blame_incremental" => \
&git_blame_incremental
,
728 "blame_data" => \
&git_blame_data
,
729 "blobdiff" => \
&git_blobdiff
,
730 "blobdiff_plain" => \
&git_blobdiff_plain
,
731 "blob" => \
&git_blob
,
732 "blob_plain" => \
&git_blob_plain
,
733 "commitdiff" => \
&git_commitdiff
,
734 "commitdiff_plain" => \
&git_commitdiff_plain
,
735 "commit" => \
&git_commit
,
736 "forks" => \
&git_forks
,
737 "heads" => \
&git_heads
,
738 "history" => \
&git_history
,
740 "patch" => \
&git_patch
,
741 "patches" => \
&git_patches
,
742 "remotes" => \
&git_remotes
,
744 "atom" => \
&git_atom
,
745 "search" => \
&git_search
,
746 "search_help" => \
&git_search_help
,
747 "shortlog" => \
&git_shortlog
,
748 "summary" => \
&git_summary
,
750 "tags" => \
&git_tags
,
751 "tree" => \
&git_tree
,
752 "snapshot" => \
&git_snapshot
,
753 "object" => \
&git_object
,
754 # those below don't need $project
755 "opml" => \
&git_opml
,
756 "project_list" => \
&git_project_list
,
757 "project_index" => \
&git_project_index
,
760 # finally, we have the hash of allowed extra_options for the commands that
762 our %allowed_options = (
763 "--no-merges" => [ qw(rss atom log shortlog history) ],
766 # fill %input_params with the CGI parameters. All values except for 'opt'
767 # should be single values, but opt can be an array. We should probably
768 # build an array of parameters that can be multi-valued, but since for the time
769 # being it's only this one, we just single it out
770 sub evaluate_query_params
{
773 while (my ($name, $symbol) = each %cgi_param_mapping) {
774 if ($symbol eq 'opt') {
775 $input_params{$name} = [ $cgi->param($symbol) ];
777 $input_params{$name} = $cgi->param($symbol);
782 # now read PATH_INFO and update the parameter list for missing parameters
783 sub evaluate_path_info
{
784 return if defined $input_params{'project'};
785 return if !$path_info;
786 $path_info =~ s
,^/+,,;
787 return if !$path_info;
789 # find which part of PATH_INFO is project
790 my $project = $path_info;
792 while ($project && !check_head_link
("$projectroot/$project")) {
793 $project =~ s
,/*[^/]*$,,;
795 return unless $project;
796 $input_params{'project'} = $project;
798 # do not change any parameters if an action is given using the query string
799 return if $input_params{'action'};
800 $path_info =~ s
,^\Q
$project\E
/*,,;
802 # next, check if we have an action
803 my $action = $path_info;
805 if (exists $actions{$action}) {
806 $path_info =~ s
,^$action/*,,;
807 $input_params{'action'} = $action;
810 # list of actions that want hash_base instead of hash, but can have no
811 # pathname (f) parameter
817 # we want to catch, among others
818 # [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name]
819 my ($parentrefname, $parentpathname, $refname, $pathname) =
820 ($path_info =~ /^(?:(.+?)(?::(.+))?\.\.)?([^:]+?)?(?::(.+))?$/);
822 # first, analyze the 'current' part
823 if (defined $pathname) {
824 # we got "branch:filename" or "branch:dir/"
825 # we could use git_get_type(branch:pathname), but:
826 # - it needs $git_dir
827 # - it does a git() call
828 # - the convention of terminating directories with a slash
829 # makes it superfluous
830 # - embedding the action in the PATH_INFO would make it even
832 $pathname =~ s
,^/+,,;
833 if (!$pathname || substr($pathname, -1) eq "/") {
834 $input_params{'action'} ||= "tree";
837 # the default action depends on whether we had parent info
839 if ($parentrefname) {
840 $input_params{'action'} ||= "blobdiff_plain";
842 $input_params{'action'} ||= "blob_plain";
845 $input_params{'hash_base'} ||= $refname;
846 $input_params{'file_name'} ||= $pathname;
847 } elsif (defined $refname) {
848 # we got "branch". In this case we have to choose if we have to
849 # set hash or hash_base.
851 # Most of the actions without a pathname only want hash to be
852 # set, except for the ones specified in @wants_base that want
853 # hash_base instead. It should also be noted that hand-crafted
854 # links having 'history' as an action and no pathname or hash
855 # set will fail, but that happens regardless of PATH_INFO.
856 if (defined $parentrefname) {
857 # if there is parent let the default be 'shortlog' action
858 # (for http://git.example.com/repo.git/A..B links); if there
859 # is no parent, dispatch will detect type of object and set
860 # action appropriately if required (if action is not set)
861 $input_params{'action'} ||= "shortlog";
863 if ($input_params{'action'} &&
864 grep { $_ eq $input_params{'action'} } @wants_base) {
865 $input_params{'hash_base'} ||= $refname;
867 $input_params{'hash'} ||= $refname;
871 # next, handle the 'parent' part, if present
872 if (defined $parentrefname) {
873 # a missing pathspec defaults to the 'current' filename, allowing e.g.
874 # someproject/blobdiff/oldrev..newrev:/filename
875 if ($parentpathname) {
876 $parentpathname =~ s
,^/+,,;
877 $parentpathname =~ s
,/$,,;
878 $input_params{'file_parent'} ||= $parentpathname;
880 $input_params{'file_parent'} ||= $input_params{'file_name'};
882 # we assume that hash_parent_base is wanted if a path was specified,
883 # or if the action wants hash_base instead of hash
884 if (defined $input_params{'file_parent'} ||
885 grep { $_ eq $input_params{'action'} } @wants_base) {
886 $input_params{'hash_parent_base'} ||= $parentrefname;
888 $input_params{'hash_parent'} ||= $parentrefname;
892 # for the snapshot action, we allow URLs in the form
893 # $project/snapshot/$hash.ext
894 # where .ext determines the snapshot and gets removed from the
895 # passed $refname to provide the $hash.
897 # To be able to tell that $refname includes the format extension, we
898 # require the following two conditions to be satisfied:
899 # - the hash input parameter MUST have been set from the $refname part
900 # of the URL (i.e. they must be equal)
901 # - the snapshot format MUST NOT have been defined already (e.g. from
903 # It's also useless to try any matching unless $refname has a dot,
904 # so we check for that too
905 if (defined $input_params{'action'} &&
906 $input_params{'action'} eq 'snapshot' &&
907 defined $refname && index($refname, '.') != -1 &&
908 $refname eq $input_params{'hash'} &&
909 !defined $input_params{'snapshot_format'}) {
910 # We loop over the known snapshot formats, checking for
911 # extensions. Allowed extensions are both the defined suffix
912 # (which includes the initial dot already) and the snapshot
913 # format key itself, with a prepended dot
914 while (my ($fmt, $opt) = each %known_snapshot_formats) {
916 unless ($hash =~ s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) {
920 # a valid suffix was found, so set the snapshot format
921 # and reset the hash parameter
922 $input_params{'snapshot_format'} = $fmt;
923 $input_params{'hash'} = $hash;
924 # we also set the format suffix to the one requested
925 # in the URL: this way a request for e.g. .tgz returns
926 # a .tgz instead of a .tar.gz
927 $known_snapshot_formats{$fmt}{'suffix'} = $sfx;
933 our ($action, $project, $file_name, $file_parent, $hash, $hash_parent, $hash_base,
934 $hash_parent_base, @extra_options, $page, $searchtype, $search_use_regexp,
935 $searchtext, $search_regexp);
936 sub evaluate_and_validate_params
{
937 our $action = $input_params{'action'};
938 if (defined $action) {
939 if (!validate_action
($action)) {
940 die_error
(400, "Invalid action parameter");
944 # parameters which are pathnames
945 our $project = $input_params{'project'};
946 if (defined $project) {
947 if (!validate_project
($project)) {
949 die_error
(404, "No such project");
953 our $file_name = $input_params{'file_name'};
954 if (defined $file_name) {
955 if (!validate_pathname
($file_name)) {
956 die_error
(400, "Invalid file parameter");
960 our $file_parent = $input_params{'file_parent'};
961 if (defined $file_parent) {
962 if (!validate_pathname
($file_parent)) {
963 die_error
(400, "Invalid file parent parameter");
967 # parameters which are refnames
968 our $hash = $input_params{'hash'};
970 if (!validate_refname
($hash)) {
971 die_error
(400, "Invalid hash parameter");
975 our $hash_parent = $input_params{'hash_parent'};
976 if (defined $hash_parent) {
977 if (!validate_refname
($hash_parent)) {
978 die_error
(400, "Invalid hash parent parameter");
982 our $hash_base = $input_params{'hash_base'};
983 if (defined $hash_base) {
984 if (!validate_refname
($hash_base)) {
985 die_error
(400, "Invalid hash base parameter");
989 our @extra_options = @{$input_params{'extra_options'}};
990 # @extra_options is always defined, since it can only be (currently) set from
991 # CGI, and $cgi->param() returns the empty array in array context if the param
993 foreach my $opt (@extra_options) {
994 if (not exists $allowed_options{$opt}) {
995 die_error
(400, "Invalid option parameter");
997 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
998 die_error
(400, "Invalid option parameter for this action");
1002 our $hash_parent_base = $input_params{'hash_parent_base'};
1003 if (defined $hash_parent_base) {
1004 if (!validate_refname
($hash_parent_base)) {
1005 die_error
(400, "Invalid hash parent base parameter");
1010 our $page = $input_params{'page'};
1011 if (defined $page) {
1012 if ($page =~ m/[^0-9]/) {
1013 die_error
(400, "Invalid page parameter");
1017 our $searchtype = $input_params{'searchtype'};
1018 if (defined $searchtype) {
1019 if ($searchtype =~ m/[^a-z]/) {
1020 die_error
(400, "Invalid searchtype parameter");
1024 our $search_use_regexp = $input_params{'search_use_regexp'};
1026 our $searchtext = $input_params{'searchtext'};
1028 if (defined $searchtext) {
1029 if (length($searchtext) < 2) {
1030 die_error
(403, "At least two characters are required for search parameter");
1032 $search_regexp = $search_use_regexp ? $searchtext : quotemeta $searchtext;
1036 # path to the current git repository
1038 sub evaluate_git_dir
{
1039 our $git_dir = "$projectroot/$project" if $project;
1042 our (@snapshot_fmts, $git_avatar);
1043 sub configure_gitweb_features
{
1044 # list of supported snapshot formats
1045 our @snapshot_fmts = gitweb_get_feature
('snapshot');
1046 @snapshot_fmts = filter_snapshot_fmts
(@snapshot_fmts);
1048 # check that the avatar feature is set to a known provider name,
1049 # and for each provider check if the dependencies are satisfied.
1050 # if the provider name is invalid or the dependencies are not met,
1051 # reset $git_avatar to the empty string.
1052 our ($git_avatar) = gitweb_get_feature
('avatar');
1053 if ($git_avatar eq 'gravatar') {
1054 $git_avatar = '' unless (eval { require Digest
::MD5
; 1; });
1055 } elsif ($git_avatar eq 'picon') {
1062 # custom error handler: 'die <message>' is Internal Server Error
1063 sub handle_errors_html
{
1064 my $msg = shift; # it is already HTML escaped
1066 # to avoid infinite loop where error occurs in die_error,
1067 # change handler to default handler, disabling handle_errors_html
1068 set_message
("Error occured when inside die_error:\n$msg");
1070 # you cannot jump out of die_error when called as error handler;
1071 # the subroutine set via CGI::Carp::set_message is called _after_
1072 # HTTP headers are already written, so it cannot write them itself
1073 die_error
(undef, undef, $msg, -error_handler
=> 1, -no_http_header
=> 1);
1075 set_message
(\
&handle_errors_html
);
1079 if (!defined $action) {
1080 if (defined $hash) {
1081 $action = git_get_type
($hash);
1082 } elsif (defined $hash_base && defined $file_name) {
1083 $action = git_get_type
("$hash_base:$file_name");
1084 } elsif (defined $project) {
1085 $action = 'summary';
1087 $action = 'project_list';
1090 if (!defined($actions{$action})) {
1091 die_error
(400, "Unknown action");
1093 if ($action !~ m/^(?:opml|project_list|project_index)$/ &&
1095 die_error
(400, "Project needed");
1097 $actions{$action}->();
1101 our $t0 = [ gettimeofday
() ]
1103 our $number_of_git_cmds = 0;
1106 our $first_request = 1;
1111 if ($first_request) {
1112 evaluate_gitweb_config
();
1113 evaluate_git_version
();
1115 if ($per_request_config) {
1116 if (ref($per_request_config) eq 'CODE') {
1117 $per_request_config->();
1118 } elsif (!$first_request) {
1119 evaluate_gitweb_config
();
1124 # $projectroot and $projects_list might be set in gitweb config file
1125 $projects_list ||= $projectroot;
1127 evaluate_query_params
();
1128 evaluate_path_info
();
1129 evaluate_and_validate_params
();
1132 configure_gitweb_features
();
1137 our $is_last_request = sub { 1 };
1138 our ($pre_dispatch_hook, $post_dispatch_hook, $pre_listen_hook);
1141 sub configure_as_fcgi
{
1143 our $CGI = 'CGI::Fast';
1145 my $request_number = 0;
1146 # let each child service 100 requests
1147 our $is_last_request = sub { ++$request_number > 100 };
1150 my $script_name = $ENV{'SCRIPT_NAME'} || $ENV{'SCRIPT_FILENAME'} || __FILE__
;
1152 if $script_name =~ /\.fcgi$/;
1154 return unless (@ARGV);
1156 require Getopt
::Long
;
1157 Getopt
::Long
::GetOptions
(
1158 'fastcgi|fcgi|f' => \
&configure_as_fcgi
,
1159 'nproc|n=i' => sub {
1160 my ($arg, $val) = @_;
1161 return unless eval { require FCGI
::ProcManager
; 1; };
1162 my $proc_manager = FCGI
::ProcManager-
>new({
1163 n_processes
=> $val,
1165 our $pre_listen_hook = sub { $proc_manager->pm_manage() };
1166 our $pre_dispatch_hook = sub { $proc_manager->pm_pre_dispatch() };
1167 our $post_dispatch_hook = sub { $proc_manager->pm_post_dispatch() };
1176 $pre_listen_hook->()
1177 if $pre_listen_hook;
1180 while ($cgi = $CGI->new()) {
1181 $pre_dispatch_hook->()
1182 if $pre_dispatch_hook;
1186 $post_dispatch_hook->()
1187 if $post_dispatch_hook;
1190 last REQUEST
if ($is_last_request->());
1199 if (defined caller) {
1200 # wrapped in a subroutine processing requests,
1201 # e.g. mod_perl with ModPerl::Registry, or PSGI with Plack::App::WrapCGI
1204 # pure CGI script, serving single request
1208 ## ======================================================================
1211 # possible values of extra options
1212 # -full => 0|1 - use absolute/full URL ($my_uri/$my_url as base)
1213 # -replay => 1 - start from a current view (replay with modifications)
1214 # -path_info => 0|1 - don't use/use path_info URL (if possible)
1215 # -anchor => ANCHOR - add #ANCHOR to end of URL, implies -replay if used alone
1218 # default is to use -absolute url() i.e. $my_uri
1219 my $href = $params{-full
} ? $my_url : $my_uri;
1221 # implicit -replay, must be first of implicit params
1222 $params{-replay
} = 1 if (keys %params == 1 && $params{-anchor
});
1224 $params{'project'} = $project unless exists $params{'project'};
1226 if ($params{-replay
}) {
1227 while (my ($name, $symbol) = each %cgi_param_mapping) {
1228 if (!exists $params{$name}) {
1229 $params{$name} = $input_params{$name};
1234 my $use_pathinfo = gitweb_check_feature
('pathinfo');
1235 if (defined $params{'project'} &&
1236 (exists $params{-path_info
} ? $params{-path_info
} : $use_pathinfo)) {
1237 # try to put as many parameters as possible in PATH_INFO:
1240 # - hash_parent or hash_parent_base:/file_parent
1241 # - hash or hash_base:/filename
1242 # - the snapshot_format as an appropriate suffix
1244 # When the script is the root DirectoryIndex for the domain,
1245 # $href here would be something like http://gitweb.example.com/
1246 # Thus, we strip any trailing / from $href, to spare us double
1247 # slashes in the final URL
1250 # Then add the project name, if present
1251 $href .= "/".esc_path_info
($params{'project'});
1252 delete $params{'project'};
1254 # since we destructively absorb parameters, we keep this
1255 # boolean that remembers if we're handling a snapshot
1256 my $is_snapshot = $params{'action'} eq 'snapshot';
1258 # Summary just uses the project path URL, any other action is
1260 if (defined $params{'action'}) {
1261 $href .= "/".esc_path_info
($params{'action'})
1262 unless $params{'action'} eq 'summary';
1263 delete $params{'action'};
1266 # Next, we put hash_parent_base:/file_parent..hash_base:/file_name,
1267 # stripping nonexistent or useless pieces
1268 $href .= "/" if ($params{'hash_base'} || $params{'hash_parent_base'}
1269 || $params{'hash_parent'} || $params{'hash'});
1270 if (defined $params{'hash_base'}) {
1271 if (defined $params{'hash_parent_base'}) {
1272 $href .= esc_path_info
($params{'hash_parent_base'});
1273 # skip the file_parent if it's the same as the file_name
1274 if (defined $params{'file_parent'}) {
1275 if (defined $params{'file_name'} && $params{'file_parent'} eq $params{'file_name'}) {
1276 delete $params{'file_parent'};
1277 } elsif ($params{'file_parent'} !~ /\.\./) {
1278 $href .= ":/".esc_path_info
($params{'file_parent'});
1279 delete $params{'file_parent'};
1283 delete $params{'hash_parent'};
1284 delete $params{'hash_parent_base'};
1285 } elsif (defined $params{'hash_parent'}) {
1286 $href .= esc_path_info
($params{'hash_parent'}). "..";
1287 delete $params{'hash_parent'};
1290 $href .= esc_path_info
($params{'hash_base'});
1291 if (defined $params{'file_name'} && $params{'file_name'} !~ /\.\./) {
1292 $href .= ":/".esc_path_info
($params{'file_name'});
1293 delete $params{'file_name'};
1295 delete $params{'hash'};
1296 delete $params{'hash_base'};
1297 } elsif (defined $params{'hash'}) {
1298 $href .= esc_path_info
($params{'hash'});
1299 delete $params{'hash'};
1302 # If the action was a snapshot, we can absorb the
1303 # snapshot_format parameter too
1305 my $fmt = $params{'snapshot_format'};
1306 # snapshot_format should always be defined when href()
1307 # is called, but just in case some code forgets, we
1308 # fall back to the default
1309 $fmt ||= $snapshot_fmts[0];
1310 $href .= $known_snapshot_formats{$fmt}{'suffix'};
1311 delete $params{'snapshot_format'};
1315 # now encode the parameters explicitly
1317 for (my $i = 0; $i < @cgi_param_mapping; $i += 2) {
1318 my ($name, $symbol) = ($cgi_param_mapping[$i], $cgi_param_mapping[$i+1]);
1319 if (defined $params{$name}) {
1320 if (ref($params{$name}) eq "ARRAY") {
1321 foreach my $par (@{$params{$name}}) {
1322 push @result, $symbol . "=" . esc_param
($par);
1325 push @result, $symbol . "=" . esc_param
($params{$name});
1329 $href .= "?" . join(';', @result) if scalar @result;
1331 # final transformation: trailing spaces must be escaped (URI-encoded)
1332 $href =~ s/(\s+)$/CGI::escape($1)/e;
1334 if ($params{-anchor
}) {
1335 $href .= "#".esc_param
($params{-anchor
});
1342 ## ======================================================================
1343 ## validation, quoting/unquoting and escaping
1345 sub validate_action
{
1346 my $input = shift || return undef;
1347 return undef unless exists $actions{$input};
1351 sub validate_project
{
1352 my $input = shift || return undef;
1353 if (!validate_pathname
($input) ||
1354 !(-d
"$projectroot/$input") ||
1355 !check_export_ok
("$projectroot/$input") ||
1356 ($strict_export && !project_in_list
($input))) {
1363 sub validate_pathname
{
1364 my $input = shift || return undef;
1366 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
1367 # at the beginning, at the end, and between slashes.
1368 # also this catches doubled slashes
1369 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
1372 # no null characters
1373 if ($input =~ m!\0!) {
1379 sub validate_refname
{
1380 my $input = shift || return undef;
1382 # textual hashes are O.K.
1383 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
1386 # it must be correct pathname
1387 $input = validate_pathname
($input)
1389 # restrictions on ref name according to git-check-ref-format
1390 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
1396 # decode sequences of octets in utf8 into Perl's internal form,
1397 # which is utf-8 with utf8 flag set if needed. gitweb writes out
1398 # in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
1401 return undef unless defined $str;
1402 if (utf8
::valid
($str)) {
1406 return decode
($fallback_encoding, $str, Encode
::FB_DEFAULT
);
1410 # quote unsafe chars, but keep the slash, even when it's not
1411 # correct, but quoted slashes look too horrible in bookmarks
1414 return undef unless defined $str;
1415 $str =~ s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI
::escape
($1)/eg
;
1420 # the quoting rules for path_info fragment are slightly different
1423 return undef unless defined $str;
1425 # path_info doesn't treat '+' as space (specially), but '?' must be escaped
1426 $str =~ s/([^A-Za-z0-9\-_.~();\/;:@&= +]+)/CGI
::escape
($1)/eg
;
1431 # quote unsafe chars in whole URL, so some characters cannot be quoted
1434 return undef unless defined $str;
1435 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&= ]+)/CGI
::escape
($1)/eg
;
1440 # quote unsafe characters in HTML attributes
1443 # for XHTML conformance escaping '"' to '"' is not enough
1444 return esc_html
(@_);
1447 # replace invalid utf8 character with SUBSTITUTION sequence
1452 return undef unless defined $str;
1454 $str = to_utf8
($str);
1455 $str = $cgi->escapeHTML($str);
1456 if ($opts{'-nbsp'}) {
1457 $str =~ s/ / /g;
1459 $str =~ s
|([[:cntrl
:]])|(($1 ne "\t") ? quot_cec
($1) : $1)|eg
;
1463 # quote control characters and escape filename to HTML
1468 return undef unless defined $str;
1470 $str = to_utf8
($str);
1471 $str = $cgi->escapeHTML($str);
1472 if ($opts{'-nbsp'}) {
1473 $str =~ s/ / /g;
1475 $str =~ s
|([[:cntrl
:]])|quot_cec
($1)|eg
;
1479 # Make control characters "printable", using character escape codes (CEC)
1483 my %es = ( # character escape codes, aka escape sequences
1484 "\t" => '\t', # tab (HT)
1485 "\n" => '\n', # line feed (LF)
1486 "\r" => '\r', # carrige return (CR)
1487 "\f" => '\f', # form feed (FF)
1488 "\b" => '\b', # backspace (BS)
1489 "\a" => '\a', # alarm (bell) (BEL)
1490 "\e" => '\e', # escape (ESC)
1491 "\013" => '\v', # vertical tab (VT)
1492 "\000" => '\0', # nul character (NUL)
1494 my $chr = ( (exists $es{$cntrl})
1496 : sprintf('\%2x', ord($cntrl)) );
1497 if ($opts{-nohtml
}) {
1500 return "<span class=\"cntrl\">$chr</span>";
1504 # Alternatively use unicode control pictures codepoints,
1505 # Unicode "printable representation" (PR)
1510 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
1511 if ($opts{-nohtml
}) {
1514 return "<span class=\"cntrl\">$chr</span>";
1518 # git may return quoted and escaped filenames
1524 my %es = ( # character escape codes, aka escape sequences
1525 't' => "\t", # tab (HT, TAB)
1526 'n' => "\n", # newline (NL)
1527 'r' => "\r", # return (CR)
1528 'f' => "\f", # form feed (FF)
1529 'b' => "\b", # backspace (BS)
1530 'a' => "\a", # alarm (bell) (BEL)
1531 'e' => "\e", # escape (ESC)
1532 'v' => "\013", # vertical tab (VT)
1535 if ($seq =~ m/^[0-7]{1,3}$/) {
1536 # octal char sequence
1537 return chr(oct($seq));
1538 } elsif (exists $es{$seq}) {
1539 # C escape sequence, aka character escape code
1542 # quoted ordinary character
1546 if ($str =~ m/^"(.*)"$/) {
1549 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
1554 # escape tabs (convert tabs to spaces)
1558 while ((my $pos = index($line, "\t")) != -1) {
1559 if (my $count = (8 - ($pos % 8))) {
1560 my $spaces = ' ' x
$count;
1561 $line =~ s/\t/$spaces/;
1568 sub project_in_list
{
1569 my $project = shift;
1570 my @list = git_get_projects_list
();
1571 return @list && scalar(grep { $_->{'path'} eq $project } @list);
1574 ## ----------------------------------------------------------------------
1575 ## HTML aware string manipulation
1577 # Try to chop given string on a word boundary between position
1578 # $len and $len+$add_len. If there is no word boundary there,
1579 # chop at $len+$add_len. Do not chop if chopped part plus ellipsis
1580 # (marking chopped part) would be longer than given string.
1584 my $add_len = shift || 10;
1585 my $where = shift || 'right'; # 'left' | 'center' | 'right'
1587 # Make sure perl knows it is utf8 encoded so we don't
1588 # cut in the middle of a utf8 multibyte char.
1589 $str = to_utf8
($str);
1591 # allow only $len chars, but don't cut a word if it would fit in $add_len
1592 # if it doesn't fit, cut it if it's still longer than the dots we would add
1593 # remove chopped character entities entirely
1595 # when chopping in the middle, distribute $len into left and right part
1596 # return early if chopping wouldn't make string shorter
1597 if ($where eq 'center') {
1598 return $str if ($len + 5 >= length($str)); # filler is length 5
1601 return $str if ($len + 4 >= length($str)); # filler is length 4
1604 # regexps: ending and beginning with word part up to $add_len
1605 my $endre = qr/.{$len}\w{0,$add_len}/;
1606 my $begre = qr/\w{0,$add_len}.{$len}/;
1608 if ($where eq 'left') {
1609 $str =~ m/^(.*?)($begre)$/;
1610 my ($lead, $body) = ($1, $2);
1611 if (length($lead) > 4) {
1614 return "$lead$body";
1616 } elsif ($where eq 'center') {
1617 $str =~ m/^($endre)(.*)$/;
1618 my ($left, $str) = ($1, $2);
1619 $str =~ m/^(.*?)($begre)$/;
1620 my ($mid, $right) = ($1, $2);
1621 if (length($mid) > 5) {
1624 return "$left$mid$right";
1627 $str =~ m/^($endre)(.*)$/;
1630 if (length($tail) > 4) {
1633 return "$body$tail";
1637 # takes the same arguments as chop_str, but also wraps a <span> around the
1638 # result with a title attribute if it does get chopped. Additionally, the
1639 # string is HTML-escaped.
1640 sub chop_and_escape_str
{
1643 my $chopped = chop_str
(@_);
1644 if ($chopped eq $str) {
1645 return esc_html
($chopped);
1647 $str =~ s/[[:cntrl:]]/?/g;
1648 return $cgi->span({-title
=>$str}, esc_html
($chopped));
1652 ## ----------------------------------------------------------------------
1653 ## functions returning short strings
1655 # CSS class for given age value (in seconds)
1659 if (!defined $age) {
1661 } elsif ($age < 60*60*2) {
1663 } elsif ($age < 60*60*24*2) {
1670 # convert age in seconds to "nn units ago" string
1675 if ($age > 60*60*24*365*2) {
1676 $age_str = (int $age/60/60/24/365);
1677 $age_str .= " years ago";
1678 } elsif ($age > 60*60*24*(365/12)*2) {
1679 $age_str = int $age/60/60/24/(365/12);
1680 $age_str .= " months ago";
1681 } elsif ($age > 60*60*24*7*2) {
1682 $age_str = int $age/60/60/24/7;
1683 $age_str .= " weeks ago";
1684 } elsif ($age > 60*60*24*2) {
1685 $age_str = int $age/60/60/24;
1686 $age_str .= " days ago";
1687 } elsif ($age > 60*60*2) {
1688 $age_str = int $age/60/60;
1689 $age_str .= " hours ago";
1690 } elsif ($age > 60*2) {
1691 $age_str = int $age/60;
1692 $age_str .= " min ago";
1693 } elsif ($age > 2) {
1694 $age_str = int $age;
1695 $age_str .= " sec ago";
1697 $age_str .= " right now";
1703 S_IFINVALID
=> 0030000,
1704 S_IFGITLINK
=> 0160000,
1707 # submodule/subproject, a commit object reference
1711 return (($mode & S_IFMT
) == S_IFGITLINK
)
1714 # convert file mode in octal to symbolic file mode string
1716 my $mode = oct shift;
1718 if (S_ISGITLINK
($mode)) {
1719 return 'm---------';
1720 } elsif (S_ISDIR
($mode & S_IFMT
)) {
1721 return 'drwxr-xr-x';
1722 } elsif (S_ISLNK
($mode)) {
1723 return 'lrwxrwxrwx';
1724 } elsif (S_ISREG
($mode)) {
1725 # git cares only about the executable bit
1726 if ($mode & S_IXUSR
) {
1727 return '-rwxr-xr-x';
1729 return '-rw-r--r--';
1732 return '----------';
1736 # convert file mode in octal to file type string
1740 if ($mode !~ m/^[0-7]+$/) {
1746 if (S_ISGITLINK
($mode)) {
1748 } elsif (S_ISDIR
($mode & S_IFMT
)) {
1750 } elsif (S_ISLNK
($mode)) {
1752 } elsif (S_ISREG
($mode)) {
1759 # convert file mode in octal to file type description string
1760 sub file_type_long
{
1763 if ($mode !~ m/^[0-7]+$/) {
1769 if (S_ISGITLINK
($mode)) {
1771 } elsif (S_ISDIR
($mode & S_IFMT
)) {
1773 } elsif (S_ISLNK
($mode)) {
1775 } elsif (S_ISREG
($mode)) {
1776 if ($mode & S_IXUSR
) {
1777 return "executable";
1787 ## ----------------------------------------------------------------------
1788 ## functions returning short HTML fragments, or transforming HTML fragments
1789 ## which don't belong to other sections
1791 # format line of commit message.
1792 sub format_log_line_html
{
1795 $line = esc_html
($line, -nbsp
=>1);
1796 $line =~ s
{\b([0-9a-fA-F
]{8,40})\b}{
1797 $cgi->a({-href
=> href
(action
=>"object", hash
=>$1),
1798 -class => "text"}, $1);
1804 # format marker of refs pointing to given object
1806 # the destination action is chosen based on object type and current context:
1807 # - for annotated tags, we choose the tag view unless it's the current view
1808 # already, in which case we go to shortlog view
1809 # - for other refs, we keep the current view if we're in history, shortlog or
1810 # log view, and select shortlog otherwise
1811 sub format_ref_marker
{
1812 my ($refs, $id) = @_;
1815 if (defined $refs->{$id}) {
1816 foreach my $ref (@{$refs->{$id}}) {
1817 # this code exploits the fact that non-lightweight tags are the
1818 # only indirect objects, and that they are the only objects for which
1819 # we want to use tag instead of shortlog as action
1820 my ($type, $name) = qw();
1821 my $indirect = ($ref =~ s/\^\{\}$//);
1822 # e.g. tags/v2.6.11 or heads/next
1823 if ($ref =~ m!^(.*?)s?/(.*)$!) {
1832 $class .= " indirect" if $indirect;
1834 my $dest_action = "shortlog";
1837 $dest_action = "tag" unless $action eq "tag";
1838 } elsif ($action =~ /^(history|(short)?log)$/) {
1839 $dest_action = $action;
1843 $dest .= "refs/" unless $ref =~ m
!^refs
/!;
1846 my $link = $cgi->a({
1848 action
=>$dest_action,
1852 $markers .= " <span class=\"".esc_attr
($class)."\" title=\"".esc_attr
($ref)."\">" .
1858 return ' <span class="refs">'. $markers . '</span>';
1864 # format, perhaps shortened and with markers, title line
1865 sub format_subject_html
{
1866 my ($long, $short, $href, $extra) = @_;
1867 $extra = '' unless defined($extra);
1869 if (length($short) < length($long)) {
1870 $long =~ s/[[:cntrl:]]/?/g;
1871 return $cgi->a({-href
=> $href, -class => "list subject",
1872 -title
=> to_utf8
($long)},
1873 esc_html
($short)) . $extra;
1875 return $cgi->a({-href
=> $href, -class => "list subject"},
1876 esc_html
($long)) . $extra;
1880 # Rather than recomputing the url for an email multiple times, we cache it
1881 # after the first hit. This gives a visible benefit in views where the avatar
1882 # for the same email is used repeatedly (e.g. shortlog).
1883 # The cache is shared by all avatar engines (currently gravatar only), which
1884 # are free to use it as preferred. Since only one avatar engine is used for any
1885 # given page, there's no risk for cache conflicts.
1886 our %avatar_cache = ();
1888 # Compute the picon url for a given email, by using the picon search service over at
1889 # http://www.cs.indiana.edu/picons/search.html
1891 my $email = lc shift;
1892 if (!$avatar_cache{$email}) {
1893 my ($user, $domain) = split('@', $email);
1894 $avatar_cache{$email} =
1895 "http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/" .
1897 "users+domains+unknown/up/single";
1899 return $avatar_cache{$email};
1902 # Compute the gravatar url for a given email, if it's not in the cache already.
1903 # Gravatar stores only the part of the URL before the size, since that's the
1904 # one computationally more expensive. This also allows reuse of the cache for
1905 # different sizes (for this particular engine).
1907 my $email = lc shift;
1909 $avatar_cache{$email} ||=
1910 "http://www.gravatar.com/avatar/" .
1911 Digest
::MD5
::md5_hex
($email) . "?s=";
1912 return $avatar_cache{$email} . $size;
1915 # Insert an avatar for the given $email at the given $size if the feature
1917 sub git_get_avatar
{
1918 my ($email, %opts) = @_;
1919 my $pre_white = ($opts{-pad_before
} ? " " : "");
1920 my $post_white = ($opts{-pad_after
} ? " " : "");
1921 $opts{-size
} ||= 'default';
1922 my $size = $avatar_size{$opts{-size
}} || $avatar_size{'default'};
1924 if ($git_avatar eq 'gravatar') {
1925 $url = gravatar_url
($email, $size);
1926 } elsif ($git_avatar eq 'picon') {
1927 $url = picon_url
($email);
1929 # Other providers can be added by extending the if chain, defining $url
1930 # as needed. If no variant puts something in $url, we assume avatars
1931 # are completely disabled/unavailable.
1934 "<img width=\"$size\" " .
1935 "class=\"avatar\" " .
1936 "src=\"".esc_url
($url)."\" " .
1944 sub format_search_author
{
1945 my ($author, $searchtype, $displaytext) = @_;
1946 my $have_search = gitweb_check_feature
('search');
1950 if ($searchtype eq 'author') {
1951 $performed = "authored";
1952 } elsif ($searchtype eq 'committer') {
1953 $performed = "committed";
1956 return $cgi->a({-href
=> href
(action
=>"search", hash
=>$hash,
1957 searchtext
=>$author,
1958 searchtype
=>$searchtype), class=>"list",
1959 title
=>"Search for commits $performed by $author"},
1963 return $displaytext;
1967 # format the author name of the given commit with the given tag
1968 # the author name is chopped and escaped according to the other
1969 # optional parameters (see chop_str).
1970 sub format_author_html
{
1973 my $author = chop_and_escape_str
($co->{'author_name'}, @_);
1974 return "<$tag class=\"author\">" .
1975 format_search_author
($co->{'author_name'}, "author",
1976 git_get_avatar
($co->{'author_email'}, -pad_after
=> 1) .
1981 # format git diff header line, i.e. "diff --(git|combined|cc) ..."
1982 sub format_git_diff_header_line
{
1984 my $diffinfo = shift;
1985 my ($from, $to) = @_;
1987 if ($diffinfo->{'nparents'}) {
1989 $line =~ s!^(diff (.*?) )"?.*$!$1!;
1990 if ($to->{'href'}) {
1991 $line .= $cgi->a({-href
=> $to->{'href'}, -class => "path"},
1992 esc_path
($to->{'file'}));
1993 } else { # file was deleted (no href)
1994 $line .= esc_path
($to->{'file'});
1998 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
1999 if ($from->{'href'}) {
2000 $line .= $cgi->a({-href
=> $from->{'href'}, -class => "path"},
2001 'a/' . esc_path
($from->{'file'}));
2002 } else { # file was added (no href)
2003 $line .= 'a/' . esc_path
($from->{'file'});
2006 if ($to->{'href'}) {
2007 $line .= $cgi->a({-href
=> $to->{'href'}, -class => "path"},
2008 'b/' . esc_path
($to->{'file'}));
2009 } else { # file was deleted
2010 $line .= 'b/' . esc_path
($to->{'file'});
2014 return "<div class=\"diff header\">$line</div>\n";
2017 # format extended diff header line, before patch itself
2018 sub format_extended_diff_header_line
{
2020 my $diffinfo = shift;
2021 my ($from, $to) = @_;
2024 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
2025 $line .= $cgi->a({-href
=>$from->{'href'}, -class=>"path"},
2026 esc_path
($from->{'file'}));
2028 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
2029 $line .= $cgi->a({-href
=>$to->{'href'}, -class=>"path"},
2030 esc_path
($to->{'file'}));
2032 # match single <mode>
2033 if ($line =~ m/\s(\d{6})$/) {
2034 $line .= '<span class="info"> (' .
2035 file_type_long
($1) .
2039 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
2040 # can match only for combined diff
2042 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2043 if ($from->{'href'}[$i]) {
2044 $line .= $cgi->a({-href
=>$from->{'href'}[$i],
2046 substr($diffinfo->{'from_id'}[$i],0,7));
2051 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
2054 if ($to->{'href'}) {
2055 $line .= $cgi->a({-href
=>$to->{'href'}, -class=>"hash"},
2056 substr($diffinfo->{'to_id'},0,7));
2061 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
2062 # can match only for ordinary diff
2063 my ($from_link, $to_link);
2064 if ($from->{'href'}) {
2065 $from_link = $cgi->a({-href
=>$from->{'href'}, -class=>"hash"},
2066 substr($diffinfo->{'from_id'},0,7));
2068 $from_link = '0' x
7;
2070 if ($to->{'href'}) {
2071 $to_link = $cgi->a({-href
=>$to->{'href'}, -class=>"hash"},
2072 substr($diffinfo->{'to_id'},0,7));
2076 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
2077 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
2080 return $line . "<br/>\n";
2083 # format from-file/to-file diff header
2084 sub format_diff_from_to_header
{
2085 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
2090 #assert($line =~ m/^---/) if DEBUG;
2091 # no extra formatting for "^--- /dev/null"
2092 if (! $diffinfo->{'nparents'}) {
2093 # ordinary (single parent) diff
2094 if ($line =~ m!^--- "?a/!) {
2095 if ($from->{'href'}) {
2097 $cgi->a({-href
=>$from->{'href'}, -class=>"path"},
2098 esc_path
($from->{'file'}));
2101 esc_path
($from->{'file'});
2104 $result .= qq
!<div
class="diff from_file">$line</div
>\n!;
2107 # combined diff (merge commit)
2108 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2109 if ($from->{'href'}[$i]) {
2111 $cgi->a({-href
=>href
(action
=>"blobdiff",
2112 hash_parent
=>$diffinfo->{'from_id'}[$i],
2113 hash_parent_base
=>$parents[$i],
2114 file_parent
=>$from->{'file'}[$i],
2115 hash
=>$diffinfo->{'to_id'},
2117 file_name
=>$to->{'file'}),
2119 -title
=>"diff" . ($i+1)},
2122 $cgi->a({-href
=>$from->{'href'}[$i], -class=>"path"},
2123 esc_path
($from->{'file'}[$i]));
2125 $line = '--- /dev/null';
2127 $result .= qq
!<div
class="diff from_file">$line</div
>\n!;
2132 #assert($line =~ m/^\+\+\+/) if DEBUG;
2133 # no extra formatting for "^+++ /dev/null"
2134 if ($line =~ m!^\+\+\+ "?b/!) {
2135 if ($to->{'href'}) {
2137 $cgi->a({-href
=>$to->{'href'}, -class=>"path"},
2138 esc_path
($to->{'file'}));
2141 esc_path
($to->{'file'});
2144 $result .= qq
!<div
class="diff to_file">$line</div
>\n!;
2149 # create note for patch simplified by combined diff
2150 sub format_diff_cc_simplified
{
2151 my ($diffinfo, @parents) = @_;
2154 $result .= "<div class=\"diff header\">" .
2156 if (!is_deleted
($diffinfo)) {
2157 $result .= $cgi->a({-href
=> href
(action
=>"blob",
2159 hash
=>$diffinfo->{'to_id'},
2160 file_name
=>$diffinfo->{'to_file'}),
2162 esc_path
($diffinfo->{'to_file'}));
2164 $result .= esc_path
($diffinfo->{'to_file'});
2166 $result .= "</div>\n" . # class="diff header"
2167 "<div class=\"diff nodifferences\">" .
2169 "</div>\n"; # class="diff nodifferences"
2174 # format patch (diff) line (not to be used for diff headers)
2175 sub format_diff_line
{
2177 my ($from, $to) = @_;
2178 my $diff_class = "";
2182 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
2184 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
2185 if ($line =~ m/^\@{3}/) {
2186 $diff_class = " chunk_header";
2187 } elsif ($line =~ m/^\\/) {
2188 $diff_class = " incomplete";
2189 } elsif ($prefix =~ tr/+/+/) {
2190 $diff_class = " add";
2191 } elsif ($prefix =~ tr/-/-/) {
2192 $diff_class = " rem";
2195 # assume ordinary diff
2196 my $char = substr($line, 0, 1);
2198 $diff_class = " add";
2199 } elsif ($char eq '-') {
2200 $diff_class = " rem";
2201 } elsif ($char eq '@') {
2202 $diff_class = " chunk_header";
2203 } elsif ($char eq "\\") {
2204 $diff_class = " incomplete";
2207 $line = untabify
($line);
2208 if ($from && $to && $line =~ m/^\@{2} /) {
2209 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
2210 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
2212 $from_lines = 0 unless defined $from_lines;
2213 $to_lines = 0 unless defined $to_lines;
2215 if ($from->{'href'}) {
2216 $from_text = $cgi->a({-href
=>"$from->{'href'}#l$from_start",
2217 -class=>"list"}, $from_text);
2219 if ($to->{'href'}) {
2220 $to_text = $cgi->a({-href
=>"$to->{'href'}#l$to_start",
2221 -class=>"list"}, $to_text);
2223 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
2224 "<span class=\"section\">" . esc_html
($section, -nbsp
=>1) . "</span>";
2225 return "<div class=\"diff$diff_class\">$line</div>\n";
2226 } elsif ($from && $to && $line =~ m/^\@{3}/) {
2227 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
2228 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
2230 @from_text = split(' ', $ranges);
2231 for (my $i = 0; $i < @from_text; ++$i) {
2232 ($from_start[$i], $from_nlines[$i]) =
2233 (split(',', substr($from_text[$i], 1)), 0);
2236 $to_text = pop @from_text;
2237 $to_start = pop @from_start;
2238 $to_nlines = pop @from_nlines;
2240 $line = "<span class=\"chunk_info\">$prefix ";
2241 for (my $i = 0; $i < @from_text; ++$i) {
2242 if ($from->{'href'}[$i]) {
2243 $line .= $cgi->a({-href
=>"$from->{'href'}[$i]#l$from_start[$i]",
2244 -class=>"list"}, $from_text[$i]);
2246 $line .= $from_text[$i];
2250 if ($to->{'href'}) {
2251 $line .= $cgi->a({-href
=>"$to->{'href'}#l$to_start",
2252 -class=>"list"}, $to_text);
2256 $line .= " $prefix</span>" .
2257 "<span class=\"section\">" . esc_html
($section, -nbsp
=>1) . "</span>";
2258 return "<div class=\"diff$diff_class\">$line</div>\n";
2260 return "<div class=\"diff$diff_class\">" . esc_html
($line, -nbsp
=>1) . "</div>\n";
2263 # Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
2264 # linked. Pass the hash of the tree/commit to snapshot.
2265 sub format_snapshot_links
{
2267 my $num_fmts = @snapshot_fmts;
2268 if ($num_fmts > 1) {
2269 # A parenthesized list of links bearing format names.
2270 # e.g. "snapshot (_tar.gz_ _zip_)"
2271 return "snapshot (" . join(' ', map
2278 }, $known_snapshot_formats{$_}{'display'})
2279 , @snapshot_fmts) . ")";
2280 } elsif ($num_fmts == 1) {
2281 # A single "snapshot" link whose tooltip bears the format name.
2283 my ($fmt) = @snapshot_fmts;
2289 snapshot_format
=>$fmt
2291 -title
=> "in format: $known_snapshot_formats{$fmt}{'display'}"
2293 } else { # $num_fmts == 0
2298 ## ......................................................................
2299 ## functions returning values to be passed, perhaps after some
2300 ## transformation, to other functions; e.g. returning arguments to href()
2302 # returns hash to be passed to href to generate gitweb URL
2303 # in -title key it returns description of link
2305 my $format = shift || 'Atom';
2306 my %res = (action
=> lc($format));
2308 # feed links are possible only for project views
2309 return unless (defined $project);
2310 # some views should link to OPML, or to generic project feed,
2311 # or don't have specific feed yet (so they should use generic)
2312 return if ($action =~ /^(?:tags|heads|forks|tag|search)$/x);
2315 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
2316 # from tag links; this also makes possible to detect branch links
2317 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
2318 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
2321 # find log type for feed description (title)
2323 if (defined $file_name) {
2324 $type = "history of $file_name";
2325 $type .= "/" if ($action eq 'tree');
2326 $type .= " on '$branch'" if (defined $branch);
2328 $type = "log of $branch" if (defined $branch);
2331 $res{-title
} = $type;
2332 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
2333 $res{'file_name'} = $file_name;
2338 ## ----------------------------------------------------------------------
2339 ## git utility subroutines, invoking git commands
2341 # returns path to the core git executable and the --git-dir parameter as list
2343 $number_of_git_cmds++;
2344 return $GIT, '--git-dir='.$git_dir;
2347 # quote the given arguments for passing them to the shell
2348 # quote_command("command", "arg 1", "arg with ' and ! characters")
2349 # => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
2350 # Try to avoid using this function wherever possible.
2353 map { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ );
2356 # get HEAD ref of given project as hash
2357 sub git_get_head_hash
{
2358 return git_get_full_hash
(shift, 'HEAD');
2361 sub git_get_full_hash
{
2362 return git_get_hash
(@_);
2365 sub git_get_short_hash
{
2366 return git_get_hash
(@_, '--short=7');
2370 my ($project, $hash, @options) = @_;
2371 my $o_git_dir = $git_dir;
2373 $git_dir = "$projectroot/$project";
2374 if (open my $fd, '-|', git_cmd
(), 'rev-parse',
2375 '--verify', '-q', @options, $hash) {
2377 chomp $retval if defined $retval;
2380 if (defined $o_git_dir) {
2381 $git_dir = $o_git_dir;
2386 # get type of given object
2390 open my $fd, "-|", git_cmd
(), "cat-file", '-t', $hash or return;
2392 close $fd or return;
2397 # repository configuration
2398 our $config_file = '';
2401 # store multiple values for single key as anonymous array reference
2402 # single values stored directly in the hash, not as [ <value> ]
2403 sub hash_set_multi
{
2404 my ($hash, $key, $value) = @_;
2406 if (!exists $hash->{$key}) {
2407 $hash->{$key} = $value;
2408 } elsif (!ref $hash->{$key}) {
2409 $hash->{$key} = [ $hash->{$key}, $value ];
2411 push @{$hash->{$key}}, $value;
2415 # return hash of git project configuration
2416 # optionally limited to some section, e.g. 'gitweb'
2417 sub git_parse_project_config
{
2418 my $section_regexp = shift;
2423 open my $fh, "-|", git_cmd
(), "config", '-z', '-l',
2426 while (my $keyval = <$fh>) {
2428 my ($key, $value) = split(/\n/, $keyval, 2);
2430 hash_set_multi
(\
%config, $key, $value)
2431 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
2438 # convert config value to boolean: 'true' or 'false'
2439 # no value, number > 0, 'true' and 'yes' values are true
2440 # rest of values are treated as false (never as error)
2441 sub config_to_bool
{
2444 return 1 if !defined $val; # section.key
2446 # strip leading and trailing whitespace
2450 return (($val =~ /^\d+$/ && $val) || # section.key = 1
2451 ($val =~ /^(?:true|yes)$/i)); # section.key = true
2454 # convert config value to simple decimal number
2455 # an optional value suffix of 'k', 'm', or 'g' will cause the value
2456 # to be multiplied by 1024, 1048576, or 1073741824
2460 # strip leading and trailing whitespace
2464 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
2466 # unknown unit is treated as 1
2467 return $num * ($unit eq 'g' ? 1073741824 :
2468 $unit eq 'm' ? 1048576 :
2469 $unit eq 'k' ? 1024 : 1);
2474 # convert config value to array reference, if needed
2475 sub config_to_multi
{
2478 return ref($val) ? $val : (defined($val) ? [ $val ] : []);
2481 sub git_get_project_config
{
2482 my ($key, $type) = @_;
2484 return unless defined $git_dir;
2487 return unless ($key);
2488 $key =~ s/^gitweb\.//;
2489 return if ($key =~ m/\W/);
2492 if (defined $type) {
2495 unless ($type eq 'bool' || $type eq 'int');
2499 if (!defined $config_file ||
2500 $config_file ne "$git_dir/config") {
2501 %config = git_parse_project_config
('gitweb');
2502 $config_file = "$git_dir/config";
2505 # check if config variable (key) exists
2506 return unless exists $config{"gitweb.$key"};
2509 if (!defined $type) {
2510 return $config{"gitweb.$key"};
2511 } elsif ($type eq 'bool') {
2512 # backward compatibility: 'git config --bool' returns true/false
2513 return config_to_bool
($config{"gitweb.$key"}) ? 'true' : 'false';
2514 } elsif ($type eq 'int') {
2515 return config_to_int
($config{"gitweb.$key"});
2517 return $config{"gitweb.$key"};
2520 # get hash of given path at given ref
2521 sub git_get_hash_by_path
{
2523 my $path = shift || return undef;
2528 open my $fd, "-|", git_cmd
(), "ls-tree", $base, "--", $path
2529 or die_error
(500, "Open git-ls-tree failed");
2531 close $fd or return undef;
2533 if (!defined $line) {
2534 # there is no tree or hash given by $path at $base
2538 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2539 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
2540 if (defined $type && $type ne $2) {
2541 # type doesn't match
2547 # get path of entry with given hash at given tree-ish (ref)
2548 # used to get 'from' filename for combined diff (merge commit) for renames
2549 sub git_get_path_by_hash
{
2550 my $base = shift || return;
2551 my $hash = shift || return;
2555 open my $fd, "-|", git_cmd
(), "ls-tree", '-r', '-t', '-z', $base
2557 while (my $line = <$fd>) {
2560 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
2561 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
2562 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
2571 ## ......................................................................
2572 ## git utility functions, directly accessing git repository
2574 # get the value of config variable either from file named as the variable
2575 # itself in the repository ($GIT_DIR/$name file), or from gitweb.$name
2576 # configuration variable in the repository config file.
2577 sub git_get_file_or_project_config
{
2578 my ($path, $name) = @_;
2580 $git_dir = "$projectroot/$path";
2581 open my $fd, '<', "$git_dir/$name"
2582 or return git_get_project_config
($name);
2585 if (defined $conf) {
2591 sub git_get_project_description
{
2593 return git_get_file_or_project_config
($path, 'description');
2596 sub git_get_project_category
{
2598 return git_get_file_or_project_config
($path, 'category');
2602 # supported formats:
2603 # * $GIT_DIR/ctags/<tagname> file (in 'ctags' subdirectory)
2604 # - if its contents is a number, use it as tag weight,
2605 # - otherwise add a tag with weight 1
2606 # * $GIT_DIR/ctags file, each line is a tag (with weight 1)
2607 # the same value multiple times increases tag weight
2608 # * `gitweb.ctag' multi-valued repo config variable
2609 sub git_get_project_ctags
{
2610 my $project = shift;
2613 $git_dir = "$projectroot/$project";
2614 if (opendir my $dh, "$git_dir/ctags") {
2615 my @files = grep { -f
$_ } map { "$git_dir/ctags/$_" } readdir($dh);
2616 foreach my $tagfile (@files) {
2617 open my $ct, '<', $tagfile
2623 (my $ctag = $tagfile) =~ s
#.*/##;
2624 if ($val =~ /\d+/) {
2625 $ctags->{$ctag} = $val;
2627 $ctags->{$ctag} = 1;
2632 } elsif (open my $fh, '<', "$git_dir/ctags") {
2633 while (my $line = <$fh>) {
2635 $ctags->{$line}++ if $line;
2640 my $taglist = config_to_multi
(git_get_project_config
('ctag'));
2641 foreach my $tag (@$taglist) {
2649 # return hash, where keys are content tags ('ctags'),
2650 # and values are sum of weights of given tag in every project
2651 sub git_gather_all_ctags
{
2652 my $projects = shift;
2655 foreach my $p (@$projects) {
2656 foreach my $ct (keys %{$p->{'ctags'}}) {
2657 $ctags->{$ct} += $p->{'ctags'}->{$ct};
2664 sub git_populate_project_tagcloud
{
2667 # First, merge different-cased tags; tags vote on casing
2669 foreach (keys %$ctags) {
2670 $ctags_lc{lc $_}->{count
} += $ctags->{$_};
2671 if (not $ctags_lc{lc $_}->{topcount
}
2672 or $ctags_lc{lc $_}->{topcount
} < $ctags->{$_}) {
2673 $ctags_lc{lc $_}->{topcount
} = $ctags->{$_};
2674 $ctags_lc{lc $_}->{topname
} = $_;
2679 my $matched = $cgi->param('by_tag');
2680 if (eval { require HTML
::TagCloud
; 1; }) {
2681 $cloud = HTML
::TagCloud-
>new;
2682 foreach my $ctag (sort keys %ctags_lc) {
2683 # Pad the title with spaces so that the cloud looks
2685 my $title = esc_html
($ctags_lc{$ctag}->{topname
});
2686 $title =~ s/ / /g;
2687 $title =~ s/^/ /g;
2688 $title =~ s/$/ /g;
2689 if (defined $matched && $matched eq $ctag) {
2690 $title = qq(<span class="match">$title</span>);
2692 $cloud->add($title, href
(project
=>undef, ctag
=>$ctag),
2693 $ctags_lc{$ctag}->{count
});
2697 foreach my $ctag (keys %ctags_lc) {
2698 my $title = esc_html
($ctags_lc{$ctag}->{topname
}, -nbsp
=>1);
2699 if (defined $matched && $matched eq $ctag) {
2700 $title = qq(<span class="match">$title</span>);
2702 $cloud->{$ctag}{count
} = $ctags_lc{$ctag}->{count
};
2703 $cloud->{$ctag}{ctag
} =
2704 $cgi->a({-href
=>href
(project
=>undef, ctag
=>$ctag)}, $title);
2710 sub git_show_project_tagcloud
{
2711 my ($cloud, $count) = @_;
2712 if (ref $cloud eq 'HTML::TagCloud') {
2713 return $cloud->html_and_css($count);
2715 my @tags = sort { $cloud->{$a}->{'count'} <=> $cloud->{$b}->{'count'} } keys %$cloud;
2717 '<div id="htmltagcloud"'.($project ? '' : ' align="center"').'>' .
2719 $cloud->{$_}->{'ctag'}
2720 } splice(@tags, 0, $count)) .
2725 sub git_get_project_url_list
{
2728 $git_dir = "$projectroot/$path";
2729 open my $fd, '<', "$git_dir/cloneurl"
2730 or return wantarray ?
2731 @{ config_to_multi
(git_get_project_config
('url')) } :
2732 config_to_multi
(git_get_project_config
('url'));
2733 my @git_project_url_list = map { chomp; $_ } <$fd>;
2736 return wantarray ? @git_project_url_list : \
@git_project_url_list;
2739 sub git_get_projects_list
{
2740 my $filter = shift || '';
2743 $filter =~ s/\.git$//;
2745 if (-d
$projects_list) {
2746 # search in directory
2747 my $dir = $projects_list;
2748 # remove the trailing "/"
2750 my $pfxlen = length("$projects_list");
2751 my $pfxdepth = ($projects_list =~ tr!/!!);
2752 # when filtering, search only given subdirectory
2759 follow_fast
=> 1, # follow symbolic links
2760 follow_skip
=> 2, # ignore duplicates
2761 dangling_symlinks
=> 0, # ignore dangling symlinks, silently
2764 our $project_maxdepth;
2766 # skip project-list toplevel, if we get it.
2767 return if (m!^[/.]$!);
2768 # only directories can be git repositories
2769 return unless (-d
$_);
2770 # don't traverse too deep (Find is super slow on os x)
2771 # $project_maxdepth excludes depth of $projectroot
2772 if (($File::Find
::name
=~ tr!/!!) - $pfxdepth > $project_maxdepth) {
2773 $File::Find
::prune
= 1;
2777 my $path = substr($File::Find
::name
, $pfxlen + 1);
2778 # we check related file in $projectroot
2779 if (check_export_ok
("$projectroot/$path")) {
2780 push @list, { path
=> $path };
2781 $File::Find
::prune
= 1;
2786 } elsif (-f
$projects_list) {
2787 # read from file(url-encoded):
2788 # 'git%2Fgit.git Linus+Torvalds'
2789 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2790 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2791 open my $fd, '<', $projects_list or return;
2793 while (my $line = <$fd>) {
2795 my ($path, $owner) = split ' ', $line;
2796 $path = unescape
($path);
2797 $owner = unescape
($owner);
2798 if (!defined $path) {
2801 # if $filter is rpovided, check if $path begins with $filter
2802 if ($filter && $path !~ m!^\Q$filter\E/!) {
2805 if (check_export_ok
("$projectroot/$path")) {
2808 owner
=> to_utf8
($owner),
2818 # written with help of Tree::Trie module (Perl Artistic License, GPL compatibile)
2819 # as side effects it sets 'forks' field to list of forks for forked projects
2820 sub filter_forks_from_projects_list
{
2821 my $projects = shift;
2823 my %trie; # prefix tree of directories (path components)
2824 # generate trie out of those directories that might contain forks
2825 foreach my $pr (@$projects) {
2826 my $path = $pr->{'path'};
2827 $path =~ s/\.git$//; # forks of 'repo.git' are in 'repo/' directory
2828 next if ($path =~ m!/$!); # skip non-bare repositories, e.g. 'repo/.git'
2829 next unless ($path); # skip '.git' repository: tests, git-instaweb
2830 next unless (-d
$path); # containing directory exists
2831 $pr->{'forks'} = []; # there can be 0 or more forks of project
2834 my @dirs = split('/', $path);
2835 # walk the trie, until either runs out of components or out of trie
2837 while (scalar @dirs &&
2838 exists($ref->{$dirs[0]})) {
2839 $ref = $ref->{shift @dirs};
2841 # create rest of trie structure from rest of components
2842 foreach my $dir (@dirs) {
2843 $ref = $ref->{$dir} = {};
2845 # create end marker, store $pr as a data
2846 $ref->{''} = $pr if (!exists $ref->{''});
2849 # filter out forks, by finding shortest prefix match for paths
2852 foreach my $pr (@$projects) {
2856 foreach my $dir (split('/', $pr->{'path'})) {
2857 if (exists $ref->{''}) {
2858 # found [shortest] prefix, is a fork - skip it
2859 push @{$ref->{''}{'forks'}}, $pr;
2862 if (!exists $ref->{$dir}) {
2863 # not in trie, cannot have prefix, not a fork
2864 push @filtered, $pr;
2867 # If the dir is there, we just walk one step down the trie.
2868 $ref = $ref->{$dir};
2870 # we ran out of trie
2871 # (shouldn't happen: it's either no match, or end marker)
2872 push @filtered, $pr;
2878 # note: fill_project_list_info must be run first,
2879 # for 'descr_long' and 'ctags' to be filled
2880 sub search_projects_list
{
2881 my ($projlist, %opts) = @_;
2882 my $tagfilter = $opts{'tagfilter'};
2883 my $searchtext = $opts{'searchtext'};
2886 unless ($tagfilter || $searchtext);
2890 foreach my $pr (@$projlist) {
2893 next unless ref($pr->{'ctags'}) eq 'HASH';
2895 grep { lc($_) eq lc($tagfilter) } keys %{$pr->{'ctags'}};
2900 $pr->{'path'} =~ /$searchtext/ ||
2901 $pr->{'descr_long'} =~ /$searchtext/;
2904 push @projects, $pr;
2910 our $gitweb_project_owner = undef;
2911 sub git_get_project_list_from_file
{
2913 return if (defined $gitweb_project_owner);
2915 $gitweb_project_owner = {};
2916 # read from file (url-encoded):
2917 # 'git%2Fgit.git Linus+Torvalds'
2918 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2919 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2920 if (-f
$projects_list) {
2921 open(my $fd, '<', $projects_list);
2922 while (my $line = <$fd>) {
2924 my ($pr, $ow) = split ' ', $line;
2925 $pr = unescape
($pr);
2926 $ow = unescape
($ow);
2927 $gitweb_project_owner->{$pr} = to_utf8
($ow);
2933 sub git_get_project_owner
{
2934 my $project = shift;
2937 return undef unless $project;
2938 $git_dir = "$projectroot/$project";
2940 if (!defined $gitweb_project_owner) {
2941 git_get_project_list_from_file
();
2944 if (exists $gitweb_project_owner->{$project}) {
2945 $owner = $gitweb_project_owner->{$project};
2947 if (!defined $owner){
2948 $owner = git_get_project_config
('owner');
2950 if (!defined $owner) {
2951 $owner = get_file_owner
("$git_dir");
2957 sub git_get_last_activity
{
2961 $git_dir = "$projectroot/$path";
2962 open($fd, "-|", git_cmd
(), 'for-each-ref',
2963 '--format=%(committer)',
2964 '--sort=-committerdate',
2966 'refs/heads') or return;
2967 my $most_recent = <$fd>;
2968 close $fd or return;
2969 if (defined $most_recent &&
2970 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
2972 my $age = time - $timestamp;
2973 return ($age, age_string
($age));
2975 return (undef, undef);
2978 # Implementation note: when a single remote is wanted, we cannot use 'git
2979 # remote show -n' because that command always work (assuming it's a remote URL
2980 # if it's not defined), and we cannot use 'git remote show' because that would
2981 # try to make a network roundtrip. So the only way to find if that particular
2982 # remote is defined is to walk the list provided by 'git remote -v' and stop if
2983 # and when we find what we want.
2984 sub git_get_remotes_list
{
2988 open my $fd, '-|' , git_cmd
(), 'remote', '-v';
2990 while (my $remote = <$fd>) {
2992 $remote =~ s!\t(.*?)\s+\((\w+)\)$!!;
2993 next if $wanted and not $remote eq $wanted;
2994 my ($url, $key) = ($1, $2);
2996 $remotes{$remote} ||= { 'heads' => () };
2997 $remotes{$remote}{$key} = $url;
2999 close $fd or return;
3000 return wantarray ? %remotes : \
%remotes;
3003 # Takes a hash of remotes as first parameter and fills it by adding the
3004 # available remote heads for each of the indicated remotes.
3005 sub fill_remote_heads
{
3006 my $remotes = shift;
3007 my @heads = map { "remotes/$_" } keys %$remotes;
3008 my @remoteheads = git_get_heads_list
(undef, @heads);
3009 foreach my $remote (keys %$remotes) {
3010 $remotes->{$remote}{'heads'} = [ grep {
3011 $_->{'name'} =~ s!^$remote/!!
3016 sub git_get_references
{
3017 my $type = shift || "";
3019 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
3020 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
3021 open my $fd, "-|", git_cmd
(), "show-ref", "--dereference",
3022 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
3025 while (my $line = <$fd>) {
3027 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {
3028 if (defined $refs{$1}) {
3029 push @{$refs{$1}}, $2;
3035 close $fd or return;
3039 sub git_get_rev_name_tags
{
3040 my $hash = shift || return undef;
3042 open my $fd, "-|", git_cmd
(), "name-rev", "--tags", $hash
3044 my $name_rev = <$fd>;
3047 if ($name_rev =~ m
|^$hash tags
/(.*)$|) {
3050 # catches also '$hash undefined' output
3055 ## ----------------------------------------------------------------------
3056 ## parse to hash functions
3060 my $tz = shift || "-0000";
3063 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
3064 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
3065 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
3066 $date{'hour'} = $hour;
3067 $date{'minute'} = $min;
3068 $date{'mday'} = $mday;
3069 $date{'day'} = $days[$wday];
3070 $date{'month'} = $months[$mon];
3071 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
3072 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
3073 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
3074 $mday, $months[$mon], $hour ,$min;
3075 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
3076 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
3078 my ($tz_sign, $tz_hour, $tz_min) =
3079 ($tz =~ m/^([-+])(\d\d)(\d\d)$/);
3080 $tz_sign = ($tz_sign eq '-' ? -1 : +1);
3081 my $local = $epoch + $tz_sign*((($tz_hour*60) + $tz_min)*60);
3082 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
3083 $date{'hour_local'} = $hour;
3084 $date{'minute_local'} = $min;
3085 $date{'tz_local'} = $tz;
3086 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
3087 1900+$year, $mon+1, $mday,
3088 $hour, $min, $sec, $tz);
3097 open my $fd, "-|", git_cmd
(), "cat-file", "tag", $tag_id or return;
3098 $tag{'id'} = $tag_id;
3099 while (my $line = <$fd>) {
3101 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
3102 $tag{'object'} = $1;
3103 } elsif ($line =~ m/^type (.+)$/) {
3105 } elsif ($line =~ m/^tag (.+)$/) {
3107 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
3108 $tag{'author'} = $1;
3109 $tag{'author_epoch'} = $2;
3110 $tag{'author_tz'} = $3;
3111 if ($tag{'author'} =~ m/^([^<]+) <([^>]*)>/) {
3112 $tag{'author_name'} = $1;
3113 $tag{'author_email'} = $2;
3115 $tag{'author_name'} = $tag{'author'};
3117 } elsif ($line =~ m/--BEGIN/) {
3118 push @comment, $line;
3120 } elsif ($line eq "") {
3124 push @comment, <$fd>;
3125 $tag{'comment'} = \
@comment;
3126 close $fd or return;
3127 if (!defined $tag{'name'}) {
3133 sub parse_commit_text
{
3134 my ($commit_text, $withparents) = @_;
3135 my @commit_lines = split '\n', $commit_text;
3138 pop @commit_lines; # Remove '\0'
3140 if (! @commit_lines) {
3144 my $header = shift @commit_lines;
3145 if ($header !~ m/^[0-9a-fA-F]{40}/) {
3148 ($co{'id'}, my @parents) = split ' ', $header;
3149 while (my $line = shift @commit_lines) {
3150 last if $line eq "\n";
3151 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
3153 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
3155 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
3156 $co{'author'} = to_utf8
($1);
3157 $co{'author_epoch'} = $2;
3158 $co{'author_tz'} = $3;
3159 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
3160 $co{'author_name'} = $1;
3161 $co{'author_email'} = $2;
3163 $co{'author_name'} = $co{'author'};
3165 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
3166 $co{'committer'} = to_utf8
($1);
3167 $co{'committer_epoch'} = $2;
3168 $co{'committer_tz'} = $3;
3169 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
3170 $co{'committer_name'} = $1;
3171 $co{'committer_email'} = $2;
3173 $co{'committer_name'} = $co{'committer'};
3177 if (!defined $co{'tree'}) {
3180 $co{'parents'} = \
@parents;
3181 $co{'parent'} = $parents[0];
3183 foreach my $title (@commit_lines) {
3186 $co{'title'} = chop_str
($title, 80, 5);
3187 # remove leading stuff of merges to make the interesting part visible
3188 if (length($title) > 50) {
3189 $title =~ s/^Automatic //;
3190 $title =~ s/^merge (of|with) /Merge ... /i;
3191 if (length($title) > 50) {
3192 $title =~ s/(http|rsync):\/\///;
3194 if (length($title) > 50) {
3195 $title =~ s/(master|www|rsync)\.//;
3197 if (length($title) > 50) {
3198 $title =~ s/kernel.org:?//;
3200 if (length($title) > 50) {
3201 $title =~ s/\/pub\/scm//;
3204 $co{'title_short'} = chop_str
($title, 50, 5);
3208 if (! defined $co{'title'} || $co{'title'} eq "") {
3209 $co{'title'} = $co{'title_short'} = '(no commit message)';
3211 # remove added spaces
3212 foreach my $line (@commit_lines) {
3215 $co{'comment'} = \
@commit_lines;
3217 my $age = time - $co{'committer_epoch'};
3219 $co{'age_string'} = age_string
($age);
3220 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
3221 if ($age > 60*60*24*7*2) {
3222 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
3223 $co{'age_string_age'} = $co{'age_string'};
3225 $co{'age_string_date'} = $co{'age_string'};
3226 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
3232 my ($commit_id) = @_;
3237 open my $fd, "-|", git_cmd
(), "rev-list",
3243 or die_error
(500, "Open git-rev-list failed");
3244 %co = parse_commit_text
(<$fd>, 1);
3251 my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
3259 open my $fd, "-|", git_cmd
(), "rev-list",
3262 ("--max-count=" . $maxcount),
3263 ("--skip=" . $skip),
3267 ($filename ? ($filename) : ())
3268 or die_error
(500, "Open git-rev-list failed");
3269 while (my $line = <$fd>) {
3270 my %co = parse_commit_text
($line);
3275 return wantarray ? @cos : \
@cos;
3278 # parse line of git-diff-tree "raw" output
3279 sub parse_difftree_raw_line
{
3283 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
3284 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
3285 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
3286 $res{'from_mode'} = $1;
3287 $res{'to_mode'} = $2;
3288 $res{'from_id'} = $3;
3290 $res{'status'} = $5;
3291 $res{'similarity'} = $6;
3292 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
3293 ($res{'from_file'}, $res{'to_file'}) = map { unquote
($_) } split("\t", $7);
3295 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote
($7);
3298 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
3299 # combined diff (for merge commit)
3300 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
3301 $res{'nparents'} = length($1);
3302 $res{'from_mode'} = [ split(' ', $2) ];
3303 $res{'to_mode'} = pop @{$res{'from_mode'}};
3304 $res{'from_id'} = [ split(' ', $3) ];
3305 $res{'to_id'} = pop @{$res{'from_id'}};
3306 $res{'status'} = [ split('', $4) ];
3307 $res{'to_file'} = unquote
($5);
3309 # 'c512b523472485aef4fff9e57b229d9d243c967f'
3310 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
3311 $res{'commit'} = $1;
3314 return wantarray ? %res : \
%res;
3317 # wrapper: return parsed line of git-diff-tree "raw" output
3318 # (the argument might be raw line, or parsed info)
3319 sub parsed_difftree_line
{
3320 my $line_or_ref = shift;
3322 if (ref($line_or_ref) eq "HASH") {
3323 # pre-parsed (or generated by hand)
3324 return $line_or_ref;
3326 return parse_difftree_raw_line
($line_or_ref);
3330 # parse line of git-ls-tree output
3331 sub parse_ls_tree_line
{
3337 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'
3338 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;
3347 $res{'name'} = unquote
($5);
3350 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
3351 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
3359 $res{'name'} = unquote
($4);
3363 return wantarray ? %res : \
%res;
3366 # generates _two_ hashes, references to which are passed as 2 and 3 argument
3367 sub parse_from_to_diffinfo
{
3368 my ($diffinfo, $from, $to, @parents) = @_;
3370 if ($diffinfo->{'nparents'}) {
3372 $from->{'file'} = [];
3373 $from->{'href'} = [];
3374 fill_from_file_info
($diffinfo, @parents)
3375 unless exists $diffinfo->{'from_file'};
3376 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
3377 $from->{'file'}[$i] =
3378 defined $diffinfo->{'from_file'}[$i] ?
3379 $diffinfo->{'from_file'}[$i] :
3380 $diffinfo->{'to_file'};
3381 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
3382 $from->{'href'}[$i] = href
(action
=>"blob",
3383 hash_base
=>$parents[$i],
3384 hash
=>$diffinfo->{'from_id'}[$i],
3385 file_name
=>$from->{'file'}[$i]);
3387 $from->{'href'}[$i] = undef;
3391 # ordinary (not combined) diff
3392 $from->{'file'} = $diffinfo->{'from_file'};
3393 if ($diffinfo->{'status'} ne "A") { # not new (added) file
3394 $from->{'href'} = href
(action
=>"blob", hash_base
=>$hash_parent,
3395 hash
=>$diffinfo->{'from_id'},
3396 file_name
=>$from->{'file'});
3398 delete $from->{'href'};
3402 $to->{'file'} = $diffinfo->{'to_file'};
3403 if (!is_deleted
($diffinfo)) { # file exists in result
3404 $to->{'href'} = href
(action
=>"blob", hash_base
=>$hash,
3405 hash
=>$diffinfo->{'to_id'},
3406 file_name
=>$to->{'file'});
3408 delete $to->{'href'};
3412 ## ......................................................................
3413 ## parse to array of hashes functions
3415 sub git_get_heads_list
{
3416 my ($limit, @classes) = @_;
3417 @classes = ('heads') unless @classes;
3418 my @patterns = map { "refs/$_" } @classes;
3421 open my $fd, '-|', git_cmd
(), 'for-each-ref',
3422 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
3423 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
3426 while (my $line = <$fd>) {
3430 my ($refinfo, $committerinfo) = split(/\0/, $line);
3431 my ($hash, $name, $title) = split(' ', $refinfo, 3);
3432 my ($committer, $epoch, $tz) =
3433 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
3434 $ref_item{'fullname'} = $name;
3435 $name =~ s!^refs/(?:head|remote)s/!!;
3437 $ref_item{'name'} = $name;
3438 $ref_item{'id'} = $hash;
3439 $ref_item{'title'} = $title || '(no commit message)';
3440 $ref_item{'epoch'} = $epoch;
3442 $ref_item{'age'} = age_string
(time - $ref_item{'epoch'});
3444 $ref_item{'age'} = "unknown";
3447 push @headslist, \
%ref_item;
3451 return wantarray ? @headslist : \
@headslist;
3454 sub git_get_tags_list
{
3458 open my $fd, '-|', git_cmd
(), 'for-each-ref',
3459 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
3460 '--format=%(objectname) %(objecttype) %(refname) '.
3461 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
3464 while (my $line = <$fd>) {
3468 my ($refinfo, $creatorinfo) = split(/\0/, $line);
3469 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
3470 my ($creator, $epoch, $tz) =
3471 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
3472 $ref_item{'fullname'} = $name;
3473 $name =~ s!^refs/tags/!!;
3475 $ref_item{'type'} = $type;
3476 $ref_item{'id'} = $id;
3477 $ref_item{'name'} = $name;
3478 if ($type eq "tag") {
3479 $ref_item{'subject'} = $title;
3480 $ref_item{'reftype'} = $reftype;
3481 $ref_item{'refid'} = $refid;
3483 $ref_item{'reftype'} = $type;
3484 $ref_item{'refid'} = $id;
3487 if ($type eq "tag" || $type eq "commit") {
3488 $ref_item{'epoch'} = $epoch;
3490 $ref_item{'age'} = age_string
(time - $ref_item{'epoch'});
3492 $ref_item{'age'} = "unknown";
3496 push @tagslist, \
%ref_item;
3500 return wantarray ? @tagslist : \
@tagslist;
3503 ## ----------------------------------------------------------------------
3504 ## filesystem-related functions
3506 sub get_file_owner
{
3509 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
3510 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
3511 if (!defined $gcos) {
3515 $owner =~ s/[,;].*$//;
3516 return to_utf8
($owner);
3519 # assume that file exists
3521 my $filename = shift;
3523 open my $fd, '<', $filename;
3524 print map { to_utf8
($_) } <$fd>;
3528 ## ......................................................................
3529 ## mimetype related functions
3531 sub mimetype_guess_file
{
3532 my $filename = shift;
3533 my $mimemap = shift;
3534 -r
$mimemap or return undef;
3537 open(my $mh, '<', $mimemap) or return undef;
3539 next if m/^#/; # skip comments
3540 my ($mimetype, $exts) = split(/\t+/);
3541 if (defined $exts) {
3542 my @exts = split(/\s+/, $exts);
3543 foreach my $ext (@exts) {
3544 $mimemap{$ext} = $mimetype;
3550 $filename =~ /\.([^.]*)$/;
3551 return $mimemap{$1};
3554 sub mimetype_guess
{
3555 my $filename = shift;
3557 $filename =~ /\./ or return undef;
3559 if ($mimetypes_file) {
3560 my $file = $mimetypes_file;
3561 if ($file !~ m!^/!) { # if it is relative path
3562 # it is relative to project
3563 $file = "$projectroot/$project/$file";
3565 $mime = mimetype_guess_file
($filename, $file);
3567 $mime ||= mimetype_guess_file
($filename, '/etc/mime.types');
3573 my $filename = shift;
3576 my $mime = mimetype_guess
($filename);
3577 $mime and return $mime;
3581 return $default_blob_plain_mimetype unless $fd;
3584 return 'text/plain';
3585 } elsif (! $filename) {
3586 return 'application/octet-stream';
3587 } elsif ($filename =~ m/\.png$/i) {
3589 } elsif ($filename =~ m/\.gif$/i) {
3591 } elsif ($filename =~ m/\.jpe?g$/i) {
3592 return 'image/jpeg';
3594 return 'application/octet-stream';
3598 sub blob_contenttype
{
3599 my ($fd, $file_name, $type) = @_;
3601 $type ||= blob_mimetype
($fd, $file_name);
3602 if ($type eq 'text/plain' && defined $default_text_plain_charset) {
3603 $type .= "; charset=$default_text_plain_charset";
3609 # guess file syntax for syntax highlighting; return undef if no highlighting
3610 # the name of syntax can (in the future) depend on syntax highlighter used
3611 sub guess_file_syntax
{
3612 my ($highlight, $mimetype, $file_name) = @_;
3613 return undef unless ($highlight && defined $file_name);
3614 my $basename = basename
($file_name, '.in');
3615 return $highlight_basename{$basename}
3616 if exists $highlight_basename{$basename};
3618 $basename =~ /\.([^.]*)$/;
3619 my $ext = $1 or return undef;
3620 return $highlight_ext{$ext}
3621 if exists $highlight_ext{$ext};
3626 # run highlighter and return FD of its output,
3627 # or return original FD if no highlighting
3628 sub run_highlighter
{
3629 my ($fd, $highlight, $syntax) = @_;
3630 return $fd unless ($highlight && defined $syntax);
3633 open $fd, quote_command
(git_cmd
(), "cat-file", "blob", $hash)." | ".
3634 quote_command
($highlight_bin).
3635 " --replace-tabs=8 --fragment --syntax $syntax |"
3636 or die_error
(500, "Couldn't open file or run syntax highlighter");
3640 ## ======================================================================
3641 ## functions printing HTML: header, footer, error page
3643 sub get_page_title
{
3644 my $title = to_utf8
($site_name);
3646 return $title unless (defined $project);
3647 $title .= " - " . to_utf8
($project);
3649 return $title unless (defined $action);
3650 $title .= "/$action"; # $action is US-ASCII (7bit ASCII)
3652 return $title unless (defined $file_name);
3653 $title .= " - " . esc_path
($file_name);
3654 if ($action eq "tree" && $file_name !~ m
|/$|) {
3661 sub print_feed_meta
{
3662 if (defined $project) {
3663 my %href_params = get_feed_info
();
3664 if (!exists $href_params{'-title'}) {
3665 $href_params{'-title'} = 'log';
3668 foreach my $format (qw(RSS Atom)) {
3669 my $type = lc($format);
3671 '-rel' => 'alternate',
3672 '-title' => esc_attr
("$project - $href_params{'-title'} - $format feed"),
3673 '-type' => "application/$type+xml"
3676 $href_params{'action'} = $type;
3677 $link_attr{'-href'} = href
(%href_params);
3679 "rel=\"$link_attr{'-rel'}\" ".
3680 "title=\"$link_attr{'-title'}\" ".
3681 "href=\"$link_attr{'-href'}\" ".
3682 "type=\"$link_attr{'-type'}\" ".
3685 $href_params{'extra_options'} = '--no-merges';
3686 $link_attr{'-href'} = href
(%href_params);
3687 $link_attr{'-title'} .= ' (no merges)';
3689 "rel=\"$link_attr{'-rel'}\" ".
3690 "title=\"$link_attr{'-title'}\" ".
3691 "href=\"$link_attr{'-href'}\" ".
3692 "type=\"$link_attr{'-type'}\" ".
3697 printf('<link rel="alternate" title="%s projects list" '.
3698 'href="%s" type="text/plain; charset=utf-8" />'."\n",
3699 esc_attr
($site_name), href
(project
=>undef, action
=>"project_index"));
3700 printf('<link rel="alternate" title="%s projects feeds" '.
3701 'href="%s" type="text/x-opml" />'."\n",
3702 esc_attr
($site_name), href
(project
=>undef, action
=>"opml"));
3706 sub git_header_html
{
3707 my $status = shift || "200 OK";
3708 my $expires = shift;
3711 my $title = get_page_title
();
3713 # require explicit support from the UA if we are to send the page as
3714 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
3715 # we have to do this because MSIE sometimes globs '*/*', pretending to
3716 # support xhtml+xml but choking when it gets what it asked for.
3717 if (defined $cgi->http('HTTP_ACCEPT') &&
3718 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\
+xml
(,|;|\s
|$)/ &&
3719 $cgi->Accept('application/xhtml+xml') != 0) {
3720 $content_type = 'application/xhtml+xml';
3722 $content_type = 'text/html';
3724 print $cgi->header(-type
=>$content_type, -charset
=> 'utf-8',
3725 -status
=> $status, -expires
=> $expires)
3726 unless ($opts{'-no_http_header'});
3727 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
3729 <?xml version="1.0" encoding="utf-8"?>
3730 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3731 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
3732 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
3733 <!-- git core binaries version $git_version -->
3735 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
3736 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
3737 <meta name="robots" content="index, nofollow"/>
3738 <title>$title</title>
3740 # the stylesheet, favicon etc urls won't work correctly with path_info
3741 # unless we set the appropriate base URL
3742 if ($ENV{'PATH_INFO'}) {
3743 print "<base href=\"".esc_url
($base_url)."\" />\n";
3745 # print out each stylesheet that exist, providing backwards capability
3746 # for those people who defined $stylesheet in a config file
3747 if (defined $stylesheet) {
3748 print '<link rel="stylesheet" type="text/css" href="'.esc_url
($stylesheet).'"/>'."\n";
3750 foreach my $stylesheet (@stylesheets) {
3751 next unless $stylesheet;
3752 print '<link rel="stylesheet" type="text/css" href="'.esc_url
($stylesheet).'"/>'."\n";
3756 if ($status eq '200 OK');
3757 if (defined $favicon) {
3758 print qq(<link rel="shortcut icon" href=").esc_url
($favicon).qq(" type="image/png" />\n);
3764 if (defined $site_header && -f
$site_header) {
3765 insert_file
($site_header);
3768 print "<div class=\"page_header\">\n";
3769 if (defined $logo) {
3770 print $cgi->a({-href
=> esc_url
($logo_url),
3771 -title
=> $logo_label},
3772 $cgi->img({-src
=> esc_url
($logo),
3773 -width
=> 72, -height
=> 27,
3775 -class => "logo"}));
3777 print $cgi->a({-href
=> esc_url
($home_link)}, $home_link_str) . " / ";
3778 if (defined $project) {
3779 print $cgi->a({-href
=> href
(action
=>"summary")}, esc_html
($project));
3780 if (defined $action) {
3781 my $action_print = $action ;
3782 if (defined $opts{-action_extra
}) {
3783 $action_print = $cgi->a({-href
=> href
(action
=>$action)},
3786 print " / $action_print";
3788 if (defined $opts{-action_extra
}) {
3789 print " / $opts{-action_extra}";
3795 my $have_search = gitweb_check_feature
('search');
3796 if (defined $project && $have_search) {
3797 if (!defined $searchtext) {
3801 if (defined $hash_base) {
3802 $search_hash = $hash_base;
3803 } elsif (defined $hash) {
3804 $search_hash = $hash;
3806 $search_hash = "HEAD";
3808 my $action = $my_uri;
3809 my $use_pathinfo = gitweb_check_feature
('pathinfo');
3810 if ($use_pathinfo) {
3811 $action .= "/".esc_url
($project);
3813 print $cgi->startform(-method => "get", -action
=> $action) .
3814 "<div class=\"search\">\n" .
3816 $cgi->input({-name
=>"p", -value
=>$project, -type
=>"hidden"}) . "\n") .
3817 $cgi->input({-name
=>"a", -value
=>"search", -type
=>"hidden"}) . "\n" .
3818 $cgi->input({-name
=>"h", -value
=>$search_hash, -type
=>"hidden"}) . "\n" .
3819 $cgi->popup_menu(-name
=> 'st', -default => 'commit',
3820 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
3821 $cgi->sup($cgi->a({-href
=> href
(action
=>"search_help")}, "?")) .
3823 $cgi->textfield(-name
=> "s", -value
=> $searchtext) . "\n" .
3824 "<span title=\"Extended regular expression\">" .
3825 $cgi->checkbox(-name
=> 'sr', -value
=> 1, -label
=> 're',
3826 -checked
=> $search_use_regexp) .
3829 $cgi->end_form() . "\n";
3833 sub git_footer_html
{
3834 my $feed_class = 'rss_logo';
3836 print "<div class=\"page_footer\">\n";
3837 if (defined $project) {
3838 my $descr = git_get_project_description
($project);
3839 if (defined $descr) {
3840 print "<div class=\"page_footer_text\">" . esc_html
($descr) . "</div>\n";
3843 my %href_params = get_feed_info
();
3844 if (!%href_params) {
3845 $feed_class .= ' generic';
3847 $href_params{'-title'} ||= 'log';
3849 foreach my $format (qw(RSS Atom)) {
3850 $href_params{'action'} = lc($format);
3851 print $cgi->a({-href
=> href
(%href_params),
3852 -title
=> "$href_params{'-title'} $format feed",
3853 -class => $feed_class}, $format)."\n";
3857 print $cgi->a({-href
=> href
(project
=>undef, action
=>"opml"),
3858 -class => $feed_class}, "OPML") . " ";
3859 print $cgi->a({-href
=> href
(project
=>undef, action
=>"project_index"),
3860 -class => $feed_class}, "TXT") . "\n";
3862 print "</div>\n"; # class="page_footer"
3864 if (defined $t0 && gitweb_check_feature
('timed')) {
3865 print "<div id=\"generating_info\">\n";
3866 print 'This page took '.
3867 '<span id="generating_time" class="time_span">'.
3868 tv_interval
($t0, [ gettimeofday
() ]).
3871 '<span id="generating_cmd">'.
3872 $number_of_git_cmds.
3873 '</span> git commands '.
3875 print "</div>\n"; # class="page_footer"
3878 if (defined $site_footer && -f
$site_footer) {
3879 insert_file
($site_footer);
3882 print qq
!<script type
="text/javascript" src
="!.esc_url($javascript).qq!"></script
>\n!;
3883 if (defined $action &&
3884 $action eq 'blame_incremental') {
3885 print qq
!<script type
="text/javascript">\n!.
3886 qq
!startBlame
("!. href(action=>"blame_data
", -replay=>1) .qq!",\n!.
3887 qq
! "!. href() .qq!");\n!.
3890 print qq
!<script type
="text/javascript">\n!.
3891 qq
!window
.onload
= function
() {\n!.
3892 (gitweb_check_feature
('javascript-actions') ?
3893 qq
! fixLinks
();\n! : '').
3894 # last parameter to onloadTZSetup must be CSS class used by format_timestamp_html
3895 qq
! onloadTZSetup
('local', 'gitweb_tz', 'datetime');\n!.
3904 # die_error(<http_status_code>, <error_message>[, <detailed_html_description>])
3905 # Example: die_error(404, 'Hash not found')
3906 # By convention, use the following status codes (as defined in RFC 2616):
3907 # 400: Invalid or missing CGI parameters, or
3908 # requested object exists but has wrong type.
3909 # 403: Requested feature (like "pickaxe" or "snapshot") not enabled on
3910 # this server or project.
3911 # 404: Requested object/revision/project doesn't exist.
3912 # 500: The server isn't configured properly, or
3913 # an internal error occurred (e.g. failed assertions caused by bugs), or
3914 # an unknown error occurred (e.g. the git binary died unexpectedly).
3915 # 503: The server is currently unavailable (because it is overloaded,
3916 # or down for maintenance). Generally, this is a temporary state.
3918 my $status = shift || 500;
3919 my $error = esc_html
(shift) || "Internal Server Error";
3923 my %http_responses = (
3924 400 => '400 Bad Request',
3925 403 => '403 Forbidden',
3926 404 => '404 Not Found',
3927 500 => '500 Internal Server Error',
3928 503 => '503 Service Unavailable',
3930 git_header_html
($http_responses{$status}, undef, %opts);
3932 <div class="page_body">
3937 if (defined $extra) {
3945 unless ($opts{'-error_handler'});
3948 ## ----------------------------------------------------------------------
3949 ## functions printing or outputting HTML: navigation
3951 sub git_print_page_nav
{
3952 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
3953 $extra = '' if !defined $extra; # pager or formats
3955 my @navs = qw(summary shortlog log commit commitdiff tree);
3957 @navs = grep { $_ ne $suppress } @navs;
3960 my %arg = map { $_ => {action
=>$_} } @navs;
3961 if (defined $head) {
3962 for (qw(commit commitdiff)) {
3963 $arg{$_}{'hash'} = $head;
3965 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
3966 for (qw(shortlog log)) {
3967 $arg{$_}{'hash'} = $head;
3972 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
3973 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
3975 my @actions = gitweb_get_feature
('actions');
3978 'n' => $project, # project name
3979 'f' => $git_dir, # project path within filesystem
3980 'h' => $treehead || '', # current hash ('h' parameter)
3981 'b' => $treebase || '', # hash base ('hb' parameter)
3984 my ($label, $link, $pos) = splice(@actions,0,3);
3986 @navs = map { $_ eq $pos ? ($_, $label) : $_ } @navs;
3988 $link =~ s/%([%nfhb])/$repl{$1}/g;
3989 $arg{$label}{'_href'} = $link;
3992 print "<div class=\"page_nav\">\n" .
3994 map { $_ eq $current ?
3995 $_ : $cgi->a({-href
=> ($arg{$_}{_href
} ? $arg{$_}{_href
} : href
(%{$arg{$_}}))}, "$_")
3997 print "<br/>\n$extra<br/>\n" .
4001 # returns a submenu for the nagivation of the refs views (tags, heads,
4002 # remotes) with the current view disabled and the remotes view only
4003 # available if the feature is enabled
4004 sub format_ref_views
{
4006 my @ref_views = qw{tags heads};
4007 push @ref_views, 'remotes' if gitweb_check_feature
('remote_heads');
4008 return join " | ", map {
4009 $_ eq $current ? $_ :
4010 $cgi->a({-href
=> href
(action
=>$_)}, $_)
4014 sub format_paging_nav
{
4015 my ($action, $page, $has_next_link) = @_;
4021 $cgi->a({-href
=> href
(-replay
=>1, page
=>undef)}, "first") .
4023 $cgi->a({-href
=> href
(-replay
=>1, page
=>$page-1),
4024 -accesskey
=> "p", -title
=> "Alt-p"}, "prev");
4026 $paging_nav .= "first ⋅ prev";
4029 if ($has_next_link) {
4030 $paging_nav .= " ⋅ " .
4031 $cgi->a({-href
=> href
(-replay
=>1, page
=>$page+1),
4032 -accesskey
=> "n", -title
=> "Alt-n"}, "next");
4034 $paging_nav .= " ⋅ next";
4040 ## ......................................................................
4041 ## functions printing or outputting HTML: div
4043 sub git_print_header_div
{
4044 my ($action, $title, $hash, $hash_base) = @_;
4047 $args{'action'} = $action;
4048 $args{'hash'} = $hash if $hash;
4049 $args{'hash_base'} = $hash_base if $hash_base;
4051 print "<div class=\"header\">\n" .
4052 $cgi->a({-href
=> href
(%args), -class => "title"},
4053 $title ? $title : $action) .
4057 sub format_repo_url
{
4058 my ($name, $url) = @_;
4059 return "<tr class=\"metadata_url\"><td>$name</td><td>$url</td></tr>\n";
4062 # Group output by placing it in a DIV element and adding a header.
4063 # Options for start_div() can be provided by passing a hash reference as the
4064 # first parameter to the function.
4065 # Options to git_print_header_div() can be provided by passing an array
4066 # reference. This must follow the options to start_div if they are present.
4067 # The content can be a scalar, which is output as-is, a scalar reference, which
4068 # is output after html escaping, an IO handle passed either as *handle or
4069 # *handle{IO}, or a function reference. In the latter case all following
4070 # parameters will be taken as argument to the content function call.
4071 sub git_print_section
{
4072 my ($div_args, $header_args, $content);
4074 if (ref($arg) eq 'HASH') {
4078 if (ref($arg) eq 'ARRAY') {
4079 $header_args = $arg;
4084 print $cgi->start_div($div_args);
4085 git_print_header_div
(@$header_args);
4087 if (ref($content) eq 'CODE') {
4089 } elsif (ref($content) eq 'SCALAR') {
4090 print esc_html
($$content);
4091 } elsif (ref($content) eq 'GLOB' or ref($content) eq 'IO::Handle') {
4093 } elsif (!ref($content) && defined($content)) {
4097 print $cgi->end_div;
4100 sub format_timestamp_html
{
4102 my $strtime = '<span class="datetime">'.$date->{'rfc2822'}.'</span>';
4104 my $localtime_format = '(%02d:%02d %s)';
4105 if ($date->{'hour_local'} < 6) {
4106 $localtime_format = '(<span class="atnight">%02d:%02d</span> %s)';
4109 sprintf($localtime_format,
4110 $date->{'hour_local'}, $date->{'minute_local'}, $date->{'tz_local'});
4115 # Outputs the author name and date in long form
4116 sub git_print_authorship
{
4119 my $tag = $opts{-tag
} || 'div';
4120 my $author = $co->{'author_name'};
4122 my %ad = parse_date
($co->{'author_epoch'}, $co->{'author_tz'});
4123 print "<$tag class=\"author_date\">" .
4124 format_search_author
($author, "author", esc_html
($author)) .
4125 " [".format_timestamp_html
(\
%ad)."]".
4126 git_get_avatar
($co->{'author_email'}, -pad_before
=> 1) .
4130 # Outputs table rows containing the full author or committer information,
4131 # in the format expected for 'commit' view (& similar).
4132 # Parameters are a commit hash reference, followed by the list of people
4133 # to output information for. If the list is empty it defaults to both
4134 # author and committer.
4135 sub git_print_authorship_rows
{
4137 # too bad we can't use @people = @_ || ('author', 'committer')
4139 @people = ('author', 'committer') unless @people;
4140 foreach my $who (@people) {
4141 my %wd = parse_date
($co->{"${who}_epoch"}, $co->{"${who}_tz"});
4142 print "<tr><td>$who</td><td>" .
4143 format_search_author
($co->{"${who}_name"}, $who,
4144 esc_html
($co->{"${who}_name"})) . " " .
4145 format_search_author
($co->{"${who}_email"}, $who,
4146 esc_html
("<" . $co->{"${who}_email"} . ">")) .
4147 "</td><td rowspan=\"2\">" .
4148 git_get_avatar
($co->{"${who}_email"}, -size
=> 'double') .
4152 format_timestamp_html
(\
%wd) .
4158 sub git_print_page_path
{
4164 print "<div class=\"page_path\">";
4165 print $cgi->a({-href
=> href
(action
=>"tree", hash_base
=>$hb),
4166 -title
=> 'tree root'}, to_utf8
("[$project]"));
4168 if (defined $name) {
4169 my @dirname = split '/', $name;
4170 my $basename = pop @dirname;
4173 foreach my $dir (@dirname) {
4174 $fullname .= ($fullname ? '/' : '') . $dir;
4175 print $cgi->a({-href
=> href
(action
=>"tree", file_name
=>$fullname,
4177 -title
=> $fullname}, esc_path
($dir));
4180 if (defined $type && $type eq 'blob') {
4181 print $cgi->a({-href
=> href
(action
=>"blob_plain", file_name
=>$file_name,
4183 -title
=> $name}, esc_path
($basename));
4184 } elsif (defined $type && $type eq 'tree') {
4185 print $cgi->a({-href
=> href
(action
=>"tree", file_name
=>$file_name,
4187 -title
=> $name}, esc_path
($basename));
4190 print esc_path
($basename);
4193 print "<br/></div>\n";
4200 if ($opts{'-remove_title'}) {
4201 # remove title, i.e. first line of log
4204 # remove leading empty lines
4205 while (defined $log->[0] && $log->[0] eq "") {
4212 foreach my $line (@$log) {
4213 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
4216 if (! $opts{'-remove_signoff'}) {
4217 print "<span class=\"signoff\">" . esc_html
($line) . "</span><br/>\n";
4220 # remove signoff lines
4227 # print only one empty line
4228 # do not print empty line after signoff
4230 next if ($empty || $signoff);
4236 print format_log_line_html
($line) . "<br/>\n";
4239 if ($opts{'-final_empty_line'}) {
4240 # end with single empty line
4241 print "<br/>\n" unless $empty;
4245 # return link target (what link points to)
4246 sub git_get_link_target
{
4251 open my $fd, "-|", git_cmd
(), "cat-file", "blob", $hash
4255 $link_target = <$fd>;
4260 return $link_target;
4263 # given link target, and the directory (basedir) the link is in,
4264 # return target of link relative to top directory (top tree);
4265 # return undef if it is not possible (including absolute links).
4266 sub normalize_link_target
{
4267 my ($link_target, $basedir) = @_;
4269 # absolute symlinks (beginning with '/') cannot be normalized
4270 return if (substr($link_target, 0, 1) eq '/');
4272 # normalize link target to path from top (root) tree (dir)
4275 $path = $basedir . '/' . $link_target;
4277 # we are in top (root) tree (dir)
4278 $path = $link_target;
4281 # remove //, /./, and /../
4283 foreach my $part (split('/', $path)) {
4284 # discard '.' and ''
4285 next if (!$part || $part eq '.');
4287 if ($part eq '..') {
4291 # link leads outside repository (outside top dir)
4295 push @path_parts, $part;
4298 $path = join('/', @path_parts);
4303 # print tree entry (row of git_tree), but without encompassing <tr> element
4304 sub git_print_tree_entry
{
4305 my ($t, $basedir, $hash_base, $have_blame) = @_;
4308 $base_key{'hash_base'} = $hash_base if defined $hash_base;
4310 # The format of a table row is: mode list link. Where mode is
4311 # the mode of the entry, list is the name of the entry, an href,
4312 # and link is the action links of the entry.
4314 print "<td class=\"mode\">" . mode_str
($t->{'mode'}) . "</td>\n";
4315 if (exists $t->{'size'}) {
4316 print "<td class=\"size\">$t->{'size'}</td>\n";
4318 if ($t->{'type'} eq "blob") {
4319 print "<td class=\"list\">" .
4320 $cgi->a({-href
=> href
(action
=>"blob", hash
=>$t->{'hash'},
4321 file_name
=>"$basedir$t->{'name'}", %base_key),
4322 -class => "list"}, esc_path
($t->{'name'}));
4323 if (S_ISLNK
(oct $t->{'mode'})) {
4324 my $link_target = git_get_link_target
($t->{'hash'});
4326 my $norm_target = normalize_link_target
($link_target, $basedir);
4327 if (defined $norm_target) {
4329 $cgi->a({-href
=> href
(action
=>"object", hash_base
=>$hash_base,
4330 file_name
=>$norm_target),
4331 -title
=> $norm_target}, esc_path
($link_target));
4333 print " -> " . esc_path
($link_target);
4338 print "<td class=\"link\">";
4339 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$t->{'hash'},
4340 file_name
=>"$basedir$t->{'name'}", %base_key)},
4344 $cgi->a({-href
=> href
(action
=>"blame", hash
=>$t->{'hash'},
4345 file_name
=>"$basedir$t->{'name'}", %base_key)},
4348 if (defined $hash_base) {
4350 $cgi->a({-href
=> href
(action
=>"history", hash_base
=>$hash_base,
4351 hash
=>$t->{'hash'}, file_name
=>"$basedir$t->{'name'}")},
4355 $cgi->a({-href
=> href
(action
=>"blob_plain", hash_base
=>$hash_base,
4356 file_name
=>"$basedir$t->{'name'}")},
4360 } elsif ($t->{'type'} eq "tree") {
4361 print "<td class=\"list\">";
4362 print $cgi->a({-href
=> href
(action
=>"tree", hash
=>$t->{'hash'},
4363 file_name
=>"$basedir$t->{'name'}",
4365 esc_path
($t->{'name'}));
4367 print "<td class=\"link\">";
4368 print $cgi->a({-href
=> href
(action
=>"tree", hash
=>$t->{'hash'},
4369 file_name
=>"$basedir$t->{'name'}",
4372 if (defined $hash_base) {
4374 $cgi->a({-href
=> href
(action
=>"history", hash_base
=>$hash_base,
4375 file_name
=>"$basedir$t->{'name'}")},
4380 # unknown object: we can only present history for it
4381 # (this includes 'commit' object, i.e. submodule support)
4382 print "<td class=\"list\">" .
4383 esc_path
($t->{'name'}) .
4385 print "<td class=\"link\">";
4386 if (defined $hash_base) {
4387 print $cgi->a({-href
=> href
(action
=>"history",
4388 hash_base
=>$hash_base,
4389 file_name
=>"$basedir$t->{'name'}")},
4396 ## ......................................................................
4397 ## functions printing large fragments of HTML
4399 # get pre-image filenames for merge (combined) diff
4400 sub fill_from_file_info
{
4401 my ($diff, @parents) = @_;
4403 $diff->{'from_file'} = [ ];
4404 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
4405 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
4406 if ($diff->{'status'}[$i] eq 'R' ||
4407 $diff->{'status'}[$i] eq 'C') {
4408 $diff->{'from_file'}[$i] =
4409 git_get_path_by_hash
($parents[$i], $diff->{'from_id'}[$i]);
4416 # is current raw difftree line of file deletion
4418 my $diffinfo = shift;
4420 return $diffinfo->{'to_id'} eq ('0' x
40);
4423 # does patch correspond to [previous] difftree raw line
4424 # $diffinfo - hashref of parsed raw diff format
4425 # $patchinfo - hashref of parsed patch diff format
4426 # (the same keys as in $diffinfo)
4427 sub is_patch_split
{
4428 my ($diffinfo, $patchinfo) = @_;
4430 return defined $diffinfo && defined $patchinfo
4431 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
4435 sub git_difftree_body
{
4436 my ($difftree, $hash, @parents) = @_;
4437 my ($parent) = $parents[0];
4438 my $have_blame = gitweb_check_feature
('blame');
4439 print "<div class=\"list_head\">\n";
4440 if ($#{$difftree} > 10) {
4441 print(($#{$difftree} + 1) . " files changed:\n");
4445 print "<table class=\"" .
4446 (@parents > 1 ? "combined " : "") .
4449 # header only for combined diff in 'commitdiff' view
4450 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
4453 print "<thead><tr>\n" .
4454 "<th></th><th></th>\n"; # filename, patchN link
4455 for (my $i = 0; $i < @parents; $i++) {
4456 my $par = $parents[$i];
4458 $cgi->a({-href
=> href
(action
=>"commitdiff",
4459 hash
=>$hash, hash_parent
=>$par),
4460 -title
=> 'commitdiff to parent number ' .
4461 ($i+1) . ': ' . substr($par,0,7)},
4465 print "</tr></thead>\n<tbody>\n";
4470 foreach my $line (@{$difftree}) {
4471 my $diff = parsed_difftree_line
($line);
4474 print "<tr class=\"dark\">\n";
4476 print "<tr class=\"light\">\n";
4480 if (exists $diff->{'nparents'}) { # combined diff
4482 fill_from_file_info
($diff, @parents)
4483 unless exists $diff->{'from_file'};
4485 if (!is_deleted
($diff)) {
4486 # file exists in the result (child) commit
4488 $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'to_id'},
4489 file_name
=>$diff->{'to_file'},
4491 -class => "list"}, esc_path
($diff->{'to_file'})) .
4495 esc_path
($diff->{'to_file'}) .
4499 if ($action eq 'commitdiff') {
4502 print "<td class=\"link\">" .
4503 $cgi->a({-href
=> href
(-anchor
=>"patch$patchno")},
4509 my $has_history = 0;
4510 my $not_deleted = 0;
4511 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
4512 my $hash_parent = $parents[$i];
4513 my $from_hash = $diff->{'from_id'}[$i];
4514 my $from_path = $diff->{'from_file'}[$i];
4515 my $status = $diff->{'status'}[$i];
4517 $has_history ||= ($status ne 'A');
4518 $not_deleted ||= ($status ne 'D');
4520 if ($status eq 'A') {
4521 print "<td class=\"link\" align=\"right\"> | </td>\n";
4522 } elsif ($status eq 'D') {
4523 print "<td class=\"link\">" .
4524 $cgi->a({-href
=> href
(action
=>"blob",
4527 file_name
=>$from_path)},
4531 if ($diff->{'to_id'} eq $from_hash) {
4532 print "<td class=\"link nochange\">";
4534 print "<td class=\"link\">";
4536 print $cgi->a({-href
=> href
(action
=>"blobdiff",
4537 hash
=>$diff->{'to_id'},
4538 hash_parent
=>$from_hash,
4540 hash_parent_base
=>$hash_parent,
4541 file_name
=>$diff->{'to_file'},
4542 file_parent
=>$from_path)},
4548 print "<td class=\"link\">";
4550 print $cgi->a({-href
=> href
(action
=>"blob",
4551 hash
=>$diff->{'to_id'},
4552 file_name
=>$diff->{'to_file'},
4555 print " | " if ($has_history);
4558 print $cgi->a({-href
=> href
(action
=>"history",
4559 file_name
=>$diff->{'to_file'},
4566 next; # instead of 'else' clause, to avoid extra indent
4568 # else ordinary diff
4570 my ($to_mode_oct, $to_mode_str, $to_file_type);
4571 my ($from_mode_oct, $from_mode_str, $from_file_type);
4572 if ($diff->{'to_mode'} ne ('0' x
6)) {
4573 $to_mode_oct = oct $diff->{'to_mode'};
4574 if (S_ISREG
($to_mode_oct)) { # only for regular file
4575 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
4577 $to_file_type = file_type
($diff->{'to_mode'});
4579 if ($diff->{'from_mode'} ne ('0' x
6)) {
4580 $from_mode_oct = oct $diff->{'from_mode'};
4581 if (S_ISREG
($from_mode_oct)) { # only for regular file
4582 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
4584 $from_file_type = file_type
($diff->{'from_mode'});
4587 if ($diff->{'status'} eq "A") { # created
4588 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
4589 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
4590 $mode_chng .= "]</span>";
4592 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'to_id'},
4593 hash_base
=>$hash, file_name
=>$diff->{'file'}),
4594 -class => "list"}, esc_path
($diff->{'file'}));
4596 print "<td>$mode_chng</td>\n";
4597 print "<td class=\"link\">";
4598 if ($action eq 'commitdiff') {
4601 print $cgi->a({-href
=> href
(-anchor
=>"patch$patchno")},
4605 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'to_id'},
4606 hash_base
=>$hash, file_name
=>$diff->{'file'})},
4610 } elsif ($diff->{'status'} eq "D") { # deleted
4611 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
4613 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'from_id'},
4614 hash_base
=>$parent, file_name
=>$diff->{'file'}),
4615 -class => "list"}, esc_path
($diff->{'file'}));
4617 print "<td>$mode_chng</td>\n";
4618 print "<td class=\"link\">";
4619 if ($action eq 'commitdiff') {
4622 print $cgi->a({-href
=> href
(-anchor
=>"patch$patchno")},
4626 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'from_id'},
4627 hash_base
=>$parent, file_name
=>$diff->{'file'})},
4630 print $cgi->a({-href
=> href
(action
=>"blame", hash_base
=>$parent,
4631 file_name
=>$diff->{'file'})},
4634 print $cgi->a({-href
=> href
(action
=>"history", hash_base
=>$parent,
4635 file_name
=>$diff->{'file'})},
4639 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
4640 my $mode_chnge = "";
4641 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
4642 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
4643 if ($from_file_type ne $to_file_type) {
4644 $mode_chnge .= " from $from_file_type to $to_file_type";
4646 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
4647 if ($from_mode_str && $to_mode_str) {
4648 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
4649 } elsif ($to_mode_str) {
4650 $mode_chnge .= " mode: $to_mode_str";
4653 $mode_chnge .= "]</span>\n";
4656 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'to_id'},
4657 hash_base
=>$hash, file_name
=>$diff->{'file'}),
4658 -class => "list"}, esc_path
($diff->{'file'}));
4660 print "<td>$mode_chnge</td>\n";
4661 print "<td class=\"link\">";
4662 if ($action eq 'commitdiff') {
4665 print $cgi->a({-href
=> href
(-anchor
=>"patch$patchno")},
4668 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
4669 # "commit" view and modified file (not onlu mode changed)
4670 print $cgi->a({-href
=> href
(action
=>"blobdiff",
4671 hash
=>$diff->{'to_id'}, hash_parent
=>$diff->{'from_id'},
4672 hash_base
=>$hash, hash_parent_base
=>$parent,
4673 file_name
=>$diff->{'file'})},
4677 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'to_id'},
4678 hash_base
=>$hash, file_name
=>$diff->{'file'})},
4681 print $cgi->a({-href
=> href
(action
=>"blame", hash_base
=>$hash,
4682 file_name
=>$diff->{'file'})},
4685 print $cgi->a({-href
=> href
(action
=>"history", hash_base
=>$hash,
4686 file_name
=>$diff->{'file'})},
4690 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
4691 my %status_name = ('R' => 'moved', 'C' => 'copied');
4692 my $nstatus = $status_name{$diff->{'status'}};
4694 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
4695 # mode also for directories, so we cannot use $to_mode_str
4696 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
4699 $cgi->a({-href
=> href
(action
=>"blob", hash_base
=>$hash,
4700 hash
=>$diff->{'to_id'}, file_name
=>$diff->{'to_file'}),
4701 -class => "list"}, esc_path
($diff->{'to_file'})) . "</td>\n" .
4702 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
4703 $cgi->a({-href
=> href
(action
=>"blob", hash_base
=>$parent,
4704 hash
=>$diff->{'from_id'}, file_name
=>$diff->{'from_file'}),
4705 -class => "list"}, esc_path
($diff->{'from_file'})) .
4706 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
4707 "<td class=\"link\">";
4708 if ($action eq 'commitdiff') {
4711 print $cgi->a({-href
=> href
(-anchor
=>"patch$patchno")},
4714 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
4715 # "commit" view and modified file (not only pure rename or copy)
4716 print $cgi->a({-href
=> href
(action
=>"blobdiff",
4717 hash
=>$diff->{'to_id'}, hash_parent
=>$diff->{'from_id'},
4718 hash_base
=>$hash, hash_parent_base
=>$parent,
4719 file_name
=>$diff->{'to_file'}, file_parent
=>$diff->{'from_file'})},
4723 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'to_id'},
4724 hash_base
=>$parent, file_name
=>$diff->{'to_file'})},
4727 print $cgi->a({-href
=> href
(action
=>"blame", hash_base
=>$hash,
4728 file_name
=>$diff->{'to_file'})},
4731 print $cgi->a({-href
=> href
(action
=>"history", hash_base
=>$hash,
4732 file_name
=>$diff->{'to_file'})},
4736 } # we should not encounter Unmerged (U) or Unknown (X) status
4739 print "</tbody>" if $has_header;
4743 sub git_patchset_body
{
4744 my ($fd, $difftree, $hash, @hash_parents) = @_;
4745 my ($hash_parent) = $hash_parents[0];
4747 my $is_combined = (@hash_parents > 1);
4749 my $patch_number = 0;
4755 print "<div class=\"patchset\">\n";
4757 # skip to first patch
4758 while ($patch_line = <$fd>) {
4761 last if ($patch_line =~ m/^diff /);
4765 while ($patch_line) {
4767 # parse "git diff" header line
4768 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
4769 # $1 is from_name, which we do not use
4770 $to_name = unquote
($2);
4771 $to_name =~ s!^b/!!;
4772 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
4773 # $1 is 'cc' or 'combined', which we do not use
4774 $to_name = unquote
($2);
4779 # check if current patch belong to current raw line
4780 # and parse raw git-diff line if needed
4781 if (is_patch_split
($diffinfo, { 'to_file' => $to_name })) {
4782 # this is continuation of a split patch
4783 print "<div class=\"patch cont\">\n";
4785 # advance raw git-diff output if needed
4786 $patch_idx++ if defined $diffinfo;
4788 # read and prepare patch information
4789 $diffinfo = parsed_difftree_line
($difftree->[$patch_idx]);
4791 # compact combined diff output can have some patches skipped
4792 # find which patch (using pathname of result) we are at now;
4794 while ($to_name ne $diffinfo->{'to_file'}) {
4795 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
4796 format_diff_cc_simplified
($diffinfo, @hash_parents) .
4797 "</div>\n"; # class="patch"
4802 last if $patch_idx > $#$difftree;
4803 $diffinfo = parsed_difftree_line
($difftree->[$patch_idx]);
4807 # modifies %from, %to hashes
4808 parse_from_to_diffinfo
($diffinfo, \
%from, \
%to, @hash_parents);
4810 # this is first patch for raw difftree line with $patch_idx index
4811 # we index @$difftree array from 0, but number patches from 1
4812 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
4816 #assert($patch_line =~ m/^diff /) if DEBUG;
4817 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
4819 # print "git diff" header
4820 print format_git_diff_header_line
($patch_line, $diffinfo,
4823 # print extended diff header
4824 print "<div class=\"diff extended_header\">\n";
4826 while ($patch_line = <$fd>) {
4829 last EXTENDED_HEADER
if ($patch_line =~ m/^--- |^diff /);
4831 print format_extended_diff_header_line
($patch_line, $diffinfo,
4834 print "</div>\n"; # class="diff extended_header"
4836 # from-file/to-file diff header
4837 if (! $patch_line) {
4838 print "</div>\n"; # class="patch"
4841 next PATCH
if ($patch_line =~ m/^diff /);
4842 #assert($patch_line =~ m/^---/) if DEBUG;
4844 my $last_patch_line = $patch_line;
4845 $patch_line = <$fd>;
4847 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
4849 print format_diff_from_to_header
($last_patch_line, $patch_line,
4850 $diffinfo, \
%from, \
%to,
4855 while ($patch_line = <$fd>) {
4858 next PATCH
if ($patch_line =~ m/^diff /);
4860 print format_diff_line
($patch_line, \
%from, \
%to);
4864 print "</div>\n"; # class="patch"
4867 # for compact combined (--cc) format, with chunk and patch simplification
4868 # the patchset might be empty, but there might be unprocessed raw lines
4869 for (++$patch_idx if $patch_number > 0;
4870 $patch_idx < @$difftree;
4872 # read and prepare patch information
4873 $diffinfo = parsed_difftree_line
($difftree->[$patch_idx]);
4875 # generate anchor for "patch" links in difftree / whatchanged part
4876 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
4877 format_diff_cc_simplified
($diffinfo, @hash_parents) .
4878 "</div>\n"; # class="patch"
4883 if ($patch_number == 0) {
4884 if (@hash_parents > 1) {
4885 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
4887 print "<div class=\"diff nodifferences\">No differences found</div>\n";
4891 print "</div>\n"; # class="patchset"
4894 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4896 # fills project list info (age, description, owner, category, forks)
4897 # for each project in the list, removing invalid projects from
4899 # NOTE: modifies $projlist, but does not remove entries from it
4900 sub fill_project_list_info
{
4901 my $projlist = shift;
4904 my $show_ctags = gitweb_check_feature
('ctags');
4906 foreach my $pr (@$projlist) {
4907 my (@activity) = git_get_last_activity
($pr->{'path'});
4908 unless (@activity) {
4911 ($pr->{'age'}, $pr->{'age_string'}) = @activity;
4912 if (!defined $pr->{'descr'}) {
4913 my $descr = git_get_project_description
($pr->{'path'}) || "";
4914 $descr = to_utf8
($descr);
4915 $pr->{'descr_long'} = $descr;
4916 $pr->{'descr'} = chop_str
($descr, $projects_list_description_width, 5);
4918 if (!defined $pr->{'owner'}) {
4919 $pr->{'owner'} = git_get_project_owner
("$pr->{'path'}") || "";
4922 $pr->{'ctags'} = git_get_project_ctags
($pr->{'path'});
4924 if ($projects_list_group_categories && !defined $pr->{'category'}) {
4925 my $cat = git_get_project_category
($pr->{'path'}) ||
4926 $project_list_default_category;
4927 $pr->{'category'} = to_utf8
($cat);
4930 push @projects, $pr;
4936 sub sort_projects_list
{
4937 my ($projlist, $order) = @_;
4941 project
=> { key
=> 'path', type
=> 'str' },
4942 descr
=> { key
=> 'descr_long', type
=> 'str' },
4943 owner
=> { key
=> 'owner', type
=> 'str' },
4944 age
=> { key
=> 'age', type
=> 'num' }
4946 my $oi = $order_info{$order};
4947 return @$projlist unless defined $oi;
4948 if ($oi->{'type'} eq 'str') {
4949 @projects = sort {$a->{$oi->{'key'}} cmp $b->{$oi->{'key'}}} @$projlist;
4951 @projects = sort {$a->{$oi->{'key'}} <=> $b->{$oi->{'key'}}} @$projlist;
4957 # returns a hash of categories, containing the list of project
4958 # belonging to each category
4959 sub build_projlist_by_category
{
4960 my ($projlist, $from, $to) = @_;
4963 $from = 0 unless defined $from;
4964 $to = $#$projlist if (!defined $to || $#$projlist < $to);
4966 for (my $i = $from; $i <= $to; $i++) {
4967 my $pr = $projlist->[$i];
4968 push @{$categories{ $pr->{'category'} }}, $pr;
4971 return wantarray ? %categories : \
%categories;
4974 # print 'sort by' <th> element, generating 'sort by $name' replay link
4975 # if that order is not selected
4977 print format_sort_th
(@_);
4980 sub format_sort_th
{
4981 my ($name, $order, $header) = @_;
4983 $header ||= ucfirst($name);
4985 if ($order eq $name) {
4986 $sort_th .= "<th>$header</th>\n";
4988 $sort_th .= "<th>" .
4989 $cgi->a({-href
=> href
(-replay
=>1, order
=>$name),
4990 -class => "header"}, $header) .
4997 sub git_project_list_rows
{
4998 my ($projlist, $from, $to, $check_forks) = @_;
5000 $from = 0 unless defined $from;
5001 $to = $#$projlist if (!defined $to || $#$projlist < $to);
5004 for (my $i = $from; $i <= $to; $i++) {
5005 my $pr = $projlist->[$i];
5008 print "<tr class=\"dark\">\n";
5010 print "<tr class=\"light\">\n";
5016 if ($pr->{'forks'}) {
5017 my $nforks = scalar @{$pr->{'forks'}};
5019 print $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"forks"),
5020 -title
=> "$nforks forks"}, "+");
5022 print $cgi->span({-title
=> "$nforks forks"}, "+");
5027 print "<td>" . $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"summary"),
5028 -class => "list"}, esc_html
($pr->{'path'})) . "</td>\n" .
5029 "<td>" . $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"summary"),
5030 -class => "list", -title
=> $pr->{'descr_long'}},
5031 esc_html
($pr->{'descr'})) . "</td>\n" .
5032 "<td><i>" . chop_and_escape_str
($pr->{'owner'}, 15) . "</i></td>\n";
5033 print "<td class=\"". age_class
($pr->{'age'}) . "\">" .
5034 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
5035 "<td class=\"link\">" .
5036 $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"summary")}, "summary") . " | " .
5037 $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"shortlog")}, "shortlog") . " | " .
5038 $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"log")}, "log") . " | " .
5039 $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"tree")}, "tree") .
5040 ($pr->{'forks'} ? " | " . $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"forks")}, "forks") : '') .
5046 sub git_project_list_body
{
5047 # actually uses global variable $project
5048 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
5049 my @projects = @$projlist;
5051 my $check_forks = gitweb_check_feature
('forks');
5052 my $show_ctags = gitweb_check_feature
('ctags');
5053 my $tagfilter = $show_ctags ? $cgi->param('by_tag') : undef;
5054 $check_forks = undef
5055 if ($tagfilter || $searchtext);
5057 # filtering out forks before filling info allows to do less work
5058 @projects = filter_forks_from_projects_list
(\
@projects)
5060 @projects = fill_project_list_info
(\
@projects);
5061 # searching projects require filling to be run before it
5062 @projects = search_projects_list
(\
@projects,
5063 'searchtext' => $searchtext,
5064 'tagfilter' => $tagfilter)
5065 if ($tagfilter || $searchtext);
5067 $order ||= $default_projects_order;
5068 $from = 0 unless defined $from;
5069 $to = $#projects if (!defined $to || $#projects < $to);
5074 "<b>No such projects found</b><br />\n".
5075 "Click ".$cgi->a({-href
=>href
(project
=>undef)},"here")." to view all projects<br />\n".
5076 "</center>\n<br />\n";
5080 @projects = sort_projects_list
(\
@projects, $order);
5083 my $ctags = git_gather_all_ctags
(\
@projects);
5084 my $cloud = git_populate_project_tagcloud
($ctags);
5085 print git_show_project_tagcloud
($cloud, 64);
5088 print "<table class=\"project_list\">\n";
5089 unless ($no_header) {
5092 print "<th></th>\n";
5094 print_sort_th
('project', $order, 'Project');
5095 print_sort_th
('descr', $order, 'Description');
5096 print_sort_th
('owner', $order, 'Owner');
5097 print_sort_th
('age', $order, 'Last Change');
5098 print "<th></th>\n" . # for links
5102 if ($projects_list_group_categories) {
5103 # only display categories with projects in the $from-$to window
5104 @projects = sort {$a->{'category'} cmp $b->{'category'}} @projects[$from..$to];
5105 my %categories = build_projlist_by_category
(\
@projects, $from, $to);
5106 foreach my $cat (sort keys %categories) {
5107 unless ($cat eq "") {
5110 print "<td></td>\n";
5112 print "<td class=\"category\" colspan=\"5\">".esc_html
($cat)."</td>\n";
5116 git_project_list_rows
($categories{$cat}, undef, undef, $check_forks);
5119 git_project_list_rows
(\
@projects, $from, $to, $check_forks);
5122 if (defined $extra) {
5125 print "<td></td>\n";
5127 print "<td colspan=\"5\">$extra</td>\n" .
5134 # uses global variable $project
5135 my ($commitlist, $from, $to, $refs, $extra) = @_;
5137 $from = 0 unless defined $from;
5138 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
5140 for (my $i = 0; $i <= $to; $i++) {
5141 my %co = %{$commitlist->[$i]};
5143 my $commit = $co{'id'};
5144 my $ref = format_ref_marker
($refs, $commit);
5145 git_print_header_div
('commit',
5146 "<span class=\"age\">$co{'age_string'}</span>" .
5147 esc_html
($co{'title'}) . $ref,
5149 print "<div class=\"title_text\">\n" .
5150 "<div class=\"log_link\">\n" .
5151 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$commit)}, "commit") .
5153 $cgi->a({-href
=> href
(action
=>"commitdiff", hash
=>$commit)}, "commitdiff") .
5155 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$commit, hash_base
=>$commit)}, "tree") .
5158 git_print_authorship
(\
%co, -tag
=> 'span');
5159 print "<br/>\n</div>\n";
5161 print "<div class=\"log_body\">\n";
5162 git_print_log
($co{'comment'}, -final_empty_line
=> 1);
5166 print "<div class=\"page_nav\">\n";
5172 sub git_shortlog_body
{
5173 # uses global variable $project
5174 my ($commitlist, $from, $to, $refs, $extra) = @_;
5176 $from = 0 unless defined $from;
5177 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
5179 print "<table class=\"shortlog\">\n";
5181 for (my $i = $from; $i <= $to; $i++) {
5182 my %co = %{$commitlist->[$i]};
5183 my $commit = $co{'id'};
5184 my $ref = format_ref_marker
($refs, $commit);
5186 print "<tr class=\"dark\">\n";
5188 print "<tr class=\"light\">\n";
5191 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
5192 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5193 format_author_html
('td', \
%co, 10) . "<td>";
5194 print format_subject_html
($co{'title'}, $co{'title_short'},
5195 href
(action
=>"commit", hash
=>$commit), $ref);
5197 "<td class=\"link\">" .
5198 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$commit)}, "commit") . " | " .
5199 $cgi->a({-href
=> href
(action
=>"commitdiff", hash
=>$commit)}, "commitdiff") . " | " .
5200 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$commit, hash_base
=>$commit)}, "tree");
5201 my $snapshot_links = format_snapshot_links
($commit);
5202 if (defined $snapshot_links) {
5203 print " | " . $snapshot_links;
5208 if (defined $extra) {
5210 "<td colspan=\"4\">$extra</td>\n" .
5216 sub git_history_body
{
5217 # Warning: assumes constant type (blob or tree) during history
5218 my ($commitlist, $from, $to, $refs, $extra,
5219 $file_name, $file_hash, $ftype) = @_;
5221 $from = 0 unless defined $from;
5222 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
5224 print "<table class=\"history\">\n";
5226 for (my $i = $from; $i <= $to; $i++) {
5227 my %co = %{$commitlist->[$i]};
5231 my $commit = $co{'id'};
5233 my $ref = format_ref_marker
($refs, $commit);
5236 print "<tr class=\"dark\">\n";
5238 print "<tr class=\"light\">\n";
5241 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5242 # shortlog: format_author_html('td', \%co, 10)
5243 format_author_html
('td', \
%co, 15, 3) . "<td>";
5244 # originally git_history used chop_str($co{'title'}, 50)
5245 print format_subject_html
($co{'title'}, $co{'title_short'},
5246 href
(action
=>"commit", hash
=>$commit), $ref);
5248 "<td class=\"link\">" .
5249 $cgi->a({-href
=> href
(action
=>$ftype, hash_base
=>$commit, file_name
=>$file_name)}, $ftype) . " | " .
5250 $cgi->a({-href
=> href
(action
=>"commitdiff", hash
=>$commit)}, "commitdiff");
5252 if ($ftype eq 'blob') {
5253 my $blob_current = $file_hash;
5254 my $blob_parent = git_get_hash_by_path
($commit, $file_name);
5255 if (defined $blob_current && defined $blob_parent &&
5256 $blob_current ne $blob_parent) {
5258 $cgi->a({-href
=> href
(action
=>"blobdiff",
5259 hash
=>$blob_current, hash_parent
=>$blob_parent,
5260 hash_base
=>$hash_base, hash_parent_base
=>$commit,
5261 file_name
=>$file_name)},
5268 if (defined $extra) {
5270 "<td colspan=\"4\">$extra</td>\n" .
5277 # uses global variable $project
5278 my ($taglist, $from, $to, $extra) = @_;
5279 $from = 0 unless defined $from;
5280 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
5282 print "<table class=\"tags\">\n";
5284 for (my $i = $from; $i <= $to; $i++) {
5285 my $entry = $taglist->[$i];
5287 my $comment = $tag{'subject'};
5289 if (defined $comment) {
5290 $comment_short = chop_str
($comment, 30, 5);
5293 print "<tr class=\"dark\">\n";
5295 print "<tr class=\"light\">\n";
5298 if (defined $tag{'age'}) {
5299 print "<td><i>$tag{'age'}</i></td>\n";
5301 print "<td></td>\n";
5304 $cgi->a({-href
=> href
(action
=>$tag{'reftype'}, hash
=>$tag{'refid'}),
5305 -class => "list name"}, esc_html
($tag{'name'})) .
5308 if (defined $comment) {
5309 print format_subject_html
($comment, $comment_short,
5310 href
(action
=>"tag", hash
=>$tag{'id'}));
5313 "<td class=\"selflink\">";
5314 if ($tag{'type'} eq "tag") {
5315 print $cgi->a({-href
=> href
(action
=>"tag", hash
=>$tag{'id'})}, "tag");
5320 "<td class=\"link\">" . " | " .
5321 $cgi->a({-href
=> href
(action
=>$tag{'reftype'}, hash
=>$tag{'refid'})}, $tag{'reftype'});
5322 if ($tag{'reftype'} eq "commit") {
5323 print " | " . $cgi->a({-href
=> href
(action
=>"shortlog", hash
=>$tag{'fullname'})}, "shortlog") .
5324 " | " . $cgi->a({-href
=> href
(action
=>"log", hash
=>$tag{'fullname'})}, "log");
5325 } elsif ($tag{'reftype'} eq "blob") {
5326 print " | " . $cgi->a({-href
=> href
(action
=>"blob_plain", hash
=>$tag{'refid'})}, "raw");
5331 if (defined $extra) {
5333 "<td colspan=\"5\">$extra</td>\n" .
5339 sub git_heads_body
{
5340 # uses global variable $project
5341 my ($headlist, $head, $from, $to, $extra) = @_;
5342 $from = 0 unless defined $from;
5343 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
5345 print "<table class=\"heads\">\n";
5347 for (my $i = $from; $i <= $to; $i++) {
5348 my $entry = $headlist->[$i];
5350 my $curr = $ref{'id'} eq $head;
5352 print "<tr class=\"dark\">\n";
5354 print "<tr class=\"light\">\n";
5357 print "<td><i>$ref{'age'}</i></td>\n" .
5358 ($curr ? "<td class=\"current_head\">" : "<td>") .
5359 $cgi->a({-href
=> href
(action
=>"shortlog", hash
=>$ref{'fullname'}),
5360 -class => "list name"},esc_html
($ref{'name'})) .
5362 "<td class=\"link\">" .
5363 $cgi->a({-href
=> href
(action
=>"shortlog", hash
=>$ref{'fullname'})}, "shortlog") . " | " .
5364 $cgi->a({-href
=> href
(action
=>"log", hash
=>$ref{'fullname'})}, "log") . " | " .
5365 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$ref{'fullname'}, hash_base
=>$ref{'fullname'})}, "tree") .
5369 if (defined $extra) {
5371 "<td colspan=\"3\">$extra</td>\n" .
5377 # Display a single remote block
5378 sub git_remote_block
{
5379 my ($remote, $rdata, $limit, $head) = @_;
5381 my $heads = $rdata->{'heads'};
5382 my $fetch = $rdata->{'fetch'};
5383 my $push = $rdata->{'push'};
5385 my $urls_table = "<table class=\"projects_list\">\n" ;
5387 if (defined $fetch) {
5388 if ($fetch eq $push) {
5389 $urls_table .= format_repo_url
("URL", $fetch);
5391 $urls_table .= format_repo_url
("Fetch URL", $fetch);
5392 $urls_table .= format_repo_url
("Push URL", $push) if defined $push;
5394 } elsif (defined $push) {
5395 $urls_table .= format_repo_url
("Push URL", $push);
5397 $urls_table .= format_repo_url
("", "No remote URL");
5400 $urls_table .= "</table>\n";
5403 if (defined $limit && $limit < @$heads) {
5404 $dots = $cgi->a({-href
=> href
(action
=>"remotes", hash
=>$remote)}, "...");
5408 git_heads_body
($heads, $head, 0, $limit, $dots);
5411 # Display a list of remote names with the respective fetch and push URLs
5412 sub git_remotes_list
{
5413 my ($remotedata, $limit) = @_;
5414 print "<table class=\"heads\">\n";
5416 my @remotes = sort keys %$remotedata;
5418 my $limited = $limit && $limit < @remotes;
5420 $#remotes = $limit - 1 if $limited;
5422 while (my $remote = shift @remotes) {
5423 my $rdata = $remotedata->{$remote};
5424 my $fetch = $rdata->{'fetch'};
5425 my $push = $rdata->{'push'};
5427 print "<tr class=\"dark\">\n";
5429 print "<tr class=\"light\">\n";
5433 $cgi->a({-href
=> href
(action
=>'remotes', hash
=>$remote),
5434 -class=> "list name"},esc_html
($remote)) .
5436 print "<td class=\"link\">" .
5437 (defined $fetch ? $cgi->a({-href
=> $fetch}, "fetch") : "fetch") .
5439 (defined $push ? $cgi->a({-href
=> $push}, "push") : "push") .
5447 "<td colspan=\"3\">" .
5448 $cgi->a({-href
=> href
(action
=>"remotes")}, "...") .
5449 "</td>\n" . "</tr>\n";
5455 # Display remote heads grouped by remote, unless there are too many
5456 # remotes, in which case we only display the remote names
5457 sub git_remotes_body
{
5458 my ($remotedata, $limit, $head) = @_;
5459 if ($limit and $limit < keys %$remotedata) {
5460 git_remotes_list
($remotedata, $limit);
5462 fill_remote_heads
($remotedata);
5463 while (my ($remote, $rdata) = each %$remotedata) {
5464 git_print_section
({-class=>"remote", -id
=>$remote},
5465 ["remotes", $remote, $remote], sub {
5466 git_remote_block
($remote, $rdata, $limit, $head);
5472 sub git_search_grep_body
{
5473 my ($commitlist, $from, $to, $extra) = @_;
5474 $from = 0 unless defined $from;
5475 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
5477 print "<table class=\"commit_search\">\n";
5479 for (my $i = $from; $i <= $to; $i++) {
5480 my %co = %{$commitlist->[$i]};
5484 my $commit = $co{'id'};
5486 print "<tr class=\"dark\">\n";
5488 print "<tr class=\"light\">\n";
5491 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5492 format_author_html
('td', \
%co, 15, 5) .
5494 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$co{'id'}),
5495 -class => "list subject"},
5496 chop_and_escape_str
($co{'title'}, 50) . "<br/>");
5497 my $comment = $co{'comment'};
5498 foreach my $line (@$comment) {
5499 if ($line =~ m/^(.*?)($search_regexp)(.*)$/i) {
5500 my ($lead, $match, $trail) = ($1, $2, $3);
5501 $match = chop_str
($match, 70, 5, 'center');
5502 my $contextlen = int((80 - length($match))/2);
5503 $contextlen = 30 if ($contextlen > 30);
5504 $lead = chop_str
($lead, $contextlen, 10, 'left');
5505 $trail = chop_str
($trail, $contextlen, 10, 'right');
5507 $lead = esc_html
($lead);
5508 $match = esc_html
($match);
5509 $trail = esc_html
($trail);
5511 print "$lead<span class=\"match\">$match</span>$trail<br />";
5515 "<td class=\"link\">" .
5516 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$co{'id'})}, "commit") .
5518 $cgi->a({-href
=> href
(action
=>"commitdiff", hash
=>$co{'id'})}, "commitdiff") .
5520 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$co{'tree'}, hash_base
=>$co{'id'})}, "tree");
5524 if (defined $extra) {
5526 "<td colspan=\"3\">$extra</td>\n" .
5532 ## ======================================================================
5533 ## ======================================================================
5536 sub git_project_list
{
5537 my $order = $input_params{'order'};
5538 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
5539 die_error
(400, "Unknown order parameter");
5542 my @list = git_get_projects_list
();
5544 die_error
(404, "No projects found");
5548 if (defined $home_text && -f
$home_text) {
5549 print "<div class=\"index_include\">\n";
5550 insert_file
($home_text);
5553 print $cgi->startform(-method => "get") .
5554 "<p class=\"projsearch\">Search:\n" .
5555 $cgi->textfield(-name
=> "s", -value
=> $searchtext) . "\n" .
5557 $cgi->end_form() . "\n";
5558 git_project_list_body
(\
@list, $order);
5563 my $order = $input_params{'order'};
5564 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
5565 die_error
(400, "Unknown order parameter");
5568 my @list = git_get_projects_list
($project);
5570 die_error
(404, "No forks found");
5574 git_print_page_nav
('','');
5575 git_print_header_div
('summary', "$project forks");
5576 git_project_list_body
(\
@list, $order);
5580 sub git_project_index
{
5581 my @projects = git_get_projects_list
();
5583 die_error
(404, "No projects found");
5587 -type
=> 'text/plain',
5588 -charset
=> 'utf-8',
5589 -content_disposition
=> 'inline; filename="index.aux"');
5591 foreach my $pr (@projects) {
5592 if (!exists $pr->{'owner'}) {
5593 $pr->{'owner'} = git_get_project_owner
("$pr->{'path'}");
5596 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
5597 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
5598 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf
("%%%02X", ord($1))/eg
;
5599 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf
("%%%02X", ord($1))/eg
;
5603 print "$path $owner\n";
5608 my $descr = git_get_project_description
($project) || "none";
5609 my %co = parse_commit
("HEAD");
5610 my %cd = %co ? parse_date
($co{'committer_epoch'}, $co{'committer_tz'}) : ();
5611 my $head = $co{'id'};
5612 my $remote_heads = gitweb_check_feature
('remote_heads');
5614 my $owner = git_get_project_owner
($project);
5616 my $refs = git_get_references
();
5617 # These get_*_list functions return one more to allow us to see if
5618 # there are more ...
5619 my @taglist = git_get_tags_list
(16);
5620 my @headlist = git_get_heads_list
(16);
5621 my %remotedata = $remote_heads ? git_get_remotes_list
() : ();
5623 my $check_forks = gitweb_check_feature
('forks');
5626 # find forks of a project
5627 @forklist = git_get_projects_list
($project);
5628 # filter out forks of forks
5629 @forklist = filter_forks_from_projects_list
(\
@forklist)
5634 git_print_page_nav
('summary','', $head);
5636 print "<div class=\"title\"> </div>\n";
5637 print "<table class=\"projects_list\">\n" .
5638 "<tr id=\"metadata_desc\"><td>description</td><td>" . esc_html
($descr) . "</td></tr>\n" .
5639 "<tr id=\"metadata_owner\"><td>owner</td><td>" . esc_html
($owner) . "</td></tr>\n";
5640 if (defined $cd{'rfc2822'}) {
5641 print "<tr id=\"metadata_lchange\"><td>last change</td>" .
5642 "<td>".format_timestamp_html
(\
%cd)."</td></tr>\n";
5645 # use per project git URL list in $projectroot/$project/cloneurl
5646 # or make project git URL from git base URL and project name
5647 my $url_tag = "URL";
5648 my @url_list = git_get_project_url_list
($project);
5649 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
5650 foreach my $git_url (@url_list) {
5651 next unless $git_url;
5652 print format_repo_url
($url_tag, $git_url);
5657 my $show_ctags = gitweb_check_feature
('ctags');
5659 my $ctags = git_get_project_ctags
($project);
5661 # without ability to add tags, don't show if there are none
5662 my $cloud = git_populate_project_tagcloud
($ctags);
5663 print "<tr id=\"metadata_ctags\">" .
5664 "<td>content tags</td>" .
5665 "<td>".git_show_project_tagcloud
($cloud, 48)."</td>" .
5672 # If XSS prevention is on, we don't include README.html.
5673 # TODO: Allow a readme in some safe format.
5674 if (!$prevent_xss && -s
"$projectroot/$project/README.html") {
5675 print "<div class=\"title\">readme</div>\n" .
5676 "<div class=\"readme\">\n";
5677 insert_file
("$projectroot/$project/README.html");
5678 print "\n</div>\n"; # class="readme"
5681 # we need to request one more than 16 (0..15) to check if
5683 my @commitlist = $head ? parse_commits
($head, 17) : ();
5685 git_print_header_div
('shortlog');
5686 git_shortlog_body
(\
@commitlist, 0, 15, $refs,
5687 $#commitlist <= 15 ? undef :
5688 $cgi->a({-href
=> href
(action
=>"shortlog")}, "..."));
5692 git_print_header_div
('tags');
5693 git_tags_body
(\
@taglist, 0, 15,
5694 $#taglist <= 15 ? undef :
5695 $cgi->a({-href
=> href
(action
=>"tags")}, "..."));
5699 git_print_header_div
('heads');
5700 git_heads_body
(\
@headlist, $head, 0, 15,
5701 $#headlist <= 15 ? undef :
5702 $cgi->a({-href
=> href
(action
=>"heads")}, "..."));
5706 git_print_header_div
('remotes');
5707 git_remotes_body
(\
%remotedata, 15, $head);
5711 git_print_header_div
('forks');
5712 git_project_list_body
(\
@forklist, 'age', 0, 15,
5713 $#forklist <= 15 ? undef :
5714 $cgi->a({-href
=> href
(action
=>"forks")}, "..."),
5722 my %tag = parse_tag
($hash);
5725 die_error
(404, "Unknown tag object");
5728 my $head = git_get_head_hash
($project);
5730 git_print_page_nav
('','', $head,undef,$head);
5731 git_print_header_div
('commit', esc_html
($tag{'name'}), $hash);
5732 print "<div class=\"title_text\">\n" .
5733 "<table class=\"object_header\">\n" .
5735 "<td>object</td>\n" .
5736 "<td>" . $cgi->a({-class => "list", -href
=> href
(action
=>$tag{'type'}, hash
=>$tag{'object'})},
5737 $tag{'object'}) . "</td>\n" .
5738 "<td class=\"link\">" . $cgi->a({-href
=> href
(action
=>$tag{'type'}, hash
=>$tag{'object'})},
5739 $tag{'type'}) . "</td>\n" .
5741 if (defined($tag{'author'})) {
5742 git_print_authorship_rows
(\
%tag, 'author');
5744 print "</table>\n\n" .
5746 print "<div class=\"page_body\">";
5747 my $comment = $tag{'comment'};
5748 foreach my $line (@$comment) {
5750 print esc_html
($line, -nbsp
=>1) . "<br/>\n";
5756 sub git_blame_common
{
5757 my $format = shift || 'porcelain';
5758 if ($format eq 'porcelain' && $cgi->param('js')) {
5759 $format = 'incremental';
5760 $action = 'blame_incremental'; # for page title etc
5764 gitweb_check_feature
('blame')
5765 or die_error
(403, "Blame view not allowed");
5768 die_error
(400, "No file name given") unless $file_name;
5769 $hash_base ||= git_get_head_hash
($project);
5770 die_error
(404, "Couldn't find base commit") unless $hash_base;
5771 my %co = parse_commit
($hash_base)
5772 or die_error
(404, "Commit not found");
5774 if (!defined $hash) {
5775 $hash = git_get_hash_by_path
($hash_base, $file_name, "blob")
5776 or die_error
(404, "Error looking up file");
5778 $ftype = git_get_type
($hash);
5779 if ($ftype !~ "blob") {
5780 die_error
(400, "Object is not a blob");
5785 if ($format eq 'incremental') {
5786 # get file contents (as base)
5787 open $fd, "-|", git_cmd
(), 'cat-file', 'blob', $hash
5788 or die_error
(500, "Open git-cat-file failed");
5789 } elsif ($format eq 'data') {
5790 # run git-blame --incremental
5791 open $fd, "-|", git_cmd
(), "blame", "--incremental",
5792 $hash_base, "--", $file_name
5793 or die_error
(500, "Open git-blame --incremental failed");
5795 # run git-blame --porcelain
5796 open $fd, "-|", git_cmd
(), "blame", '-p',
5797 $hash_base, '--', $file_name
5798 or die_error
(500, "Open git-blame --porcelain failed");
5801 # incremental blame data returns early
5802 if ($format eq 'data') {
5804 -type
=>"text/plain", -charset
=> "utf-8",
5805 -status
=> "200 OK");
5806 local $| = 1; # output autoflush
5809 or print "ERROR $!\n";
5812 if (defined $t0 && gitweb_check_feature
('timed')) {
5814 tv_interval
($t0, [ gettimeofday
() ]).
5815 ' '.$number_of_git_cmds;
5825 $cgi->a({-href
=> href
(action
=>"blob", -replay
=>1)},
5828 if ($format eq 'incremental') {
5830 $cgi->a({-href
=> href
(action
=>"blame", javascript
=>0, -replay
=>1)},
5831 "blame") . " (non-incremental)";
5834 $cgi->a({-href
=> href
(action
=>"blame_incremental", -replay
=>1)},
5835 "blame") . " (incremental)";
5839 $cgi->a({-href
=> href
(action
=>"history", -replay
=>1)},
5842 $cgi->a({-href
=> href
(action
=>$action, file_name
=>$file_name)},
5844 git_print_page_nav
('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
5845 git_print_header_div
('commit', esc_html
($co{'title'}), $hash_base);
5846 git_print_page_path
($file_name, $ftype, $hash_base);
5849 if ($format eq 'incremental') {
5850 print "<noscript>\n<div class=\"error\"><center><b>\n".
5851 "This page requires JavaScript to run.\n Use ".
5852 $cgi->a({-href
=> href
(action
=>'blame',javascript
=>0,-replay
=>1)},
5855 "</b></center></div>\n</noscript>\n";
5857 print qq
!<div id
="progress_bar" style
="width: 100%; background-color: yellow"></div
>\n!;
5860 print qq
!<div
class="page_body">\n!;
5861 print qq
!<div id
="progress_info">... / ...</div
>\n!
5862 if ($format eq 'incremental');
5863 print qq
!<table id
="blame_table" class="blame" width
="100%">\n!.
5864 #qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.
5866 qq
!<tr
><th
>Commit
</th><th>Line</th
><th
>Data
</th></tr
>\n!.
5870 my @rev_color = qw(light dark);
5871 my $num_colors = scalar(@rev_color);
5872 my $current_color = 0;
5874 if ($format eq 'incremental') {
5875 my $color_class = $rev_color[$current_color];
5880 while (my $line = <$fd>) {
5884 print qq
!<tr id
="l$linenr" class="$color_class">!.
5885 qq
!<td
class="sha1"><a href
=""> </a></td
>!.
5886 qq
!<td
class="linenr">!.
5887 qq
!<a
class="linenr" href
="">$linenr</a></td
>!;
5888 print qq
!<td
class="pre">! . esc_html
($line) . "</td>\n";
5892 } else { # porcelain, i.e. ordinary blame
5893 my %metainfo = (); # saves information about commits
5897 while (my $line = <$fd>) {
5899 # the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]
5900 # no <lines in group> for subsequent lines in group of lines
5901 my ($full_rev, $orig_lineno, $lineno, $group_size) =
5902 ($line =~ /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);
5903 if (!exists $metainfo{$full_rev}) {
5904 $metainfo{$full_rev} = { 'nprevious' => 0 };
5906 my $meta = $metainfo{$full_rev};
5908 while ($data = <$fd>) {
5910 last if ($data =~ s/^\t//); # contents of line
5911 if ($data =~ /^(\S+)(?: (.*))?$/) {
5912 $meta->{$1} = $2 unless exists $meta->{$1};
5914 if ($data =~ /^previous /) {
5915 $meta->{'nprevious'}++;
5918 my $short_rev = substr($full_rev, 0, 8);
5919 my $author = $meta->{'author'};
5921 parse_date
($meta->{'author-time'}, $meta->{'author-tz'});
5922 my $date = $date{'iso-tz'};
5924 $current_color = ($current_color + 1) % $num_colors;
5926 my $tr_class = $rev_color[$current_color];
5927 $tr_class .= ' boundary' if (exists $meta->{'boundary'});
5928 $tr_class .= ' no-previous' if ($meta->{'nprevious'} == 0);
5929 $tr_class .= ' multiple-previous' if ($meta->{'nprevious'} > 1);
5930 print "<tr id=\"l$lineno\" class=\"$tr_class\">\n";
5932 print "<td class=\"sha1\"";
5933 print " title=\"". esc_html
($author) . ", $date\"";
5934 print " rowspan=\"$group_size\"" if ($group_size > 1);
5936 print $cgi->a({-href
=> href
(action
=>"commit",
5938 file_name
=>$file_name)},
5939 esc_html
($short_rev));
5940 if ($group_size >= 2) {
5941 my @author_initials = ($author =~ /\b([[:upper:]])\B/g);
5942 if (@author_initials) {
5944 esc_html
(join('', @author_initials));
5950 # 'previous' <sha1 of parent commit> <filename at commit>
5951 if (exists $meta->{'previous'} &&
5952 $meta->{'previous'} =~ /^([a-fA-F0-9]{40}) (.*)$/) {
5953 $meta->{'parent'} = $1;
5954 $meta->{'file_parent'} = unquote
($2);
5957 exists($meta->{'parent'}) ?
5958 $meta->{'parent'} : $full_rev;
5959 my $linenr_filename =
5960 exists($meta->{'file_parent'}) ?
5961 $meta->{'file_parent'} : unquote
($meta->{'filename'});
5962 my $blamed = href
(action
=> 'blame',
5963 file_name
=> $linenr_filename,
5964 hash_base
=> $linenr_commit);
5965 print "<td class=\"linenr\">";
5966 print $cgi->a({ -href
=> "$blamed#l$orig_lineno",
5967 -class => "linenr" },
5970 print "<td class=\"pre\">" . esc_html
($data) . "</td>\n";
5978 "</table>\n"; # class="blame"
5979 print "</div>\n"; # class="blame_body"
5981 or print "Reading blob failed\n";
5990 sub git_blame_incremental
{
5991 git_blame_common
('incremental');
5994 sub git_blame_data
{
5995 git_blame_common
('data');
5999 my $head = git_get_head_hash
($project);
6001 git_print_page_nav
('','', $head,undef,$head,format_ref_views
('tags'));
6002 git_print_header_div
('summary', $project);
6004 my @tagslist = git_get_tags_list
();
6006 git_tags_body
(\
@tagslist);
6012 my $head = git_get_head_hash
($project);
6014 git_print_page_nav
('','', $head,undef,$head,format_ref_views
('heads'));
6015 git_print_header_div
('summary', $project);
6017 my @headslist = git_get_heads_list
();
6019 git_heads_body
(\
@headslist, $head);
6024 # used both for single remote view and for list of all the remotes
6026 gitweb_check_feature
('remote_heads')
6027 or die_error
(403, "Remote heads view is disabled");
6029 my $head = git_get_head_hash
($project);
6030 my $remote = $input_params{'hash'};
6032 my $remotedata = git_get_remotes_list
($remote);
6033 die_error
(500, "Unable to get remote information") unless defined $remotedata;
6035 unless (%$remotedata) {
6036 die_error
(404, defined $remote ?
6037 "Remote $remote not found" :
6038 "No remotes found");
6041 git_header_html
(undef, undef, -action_extra
=> $remote);
6042 git_print_page_nav
('', '', $head, undef, $head,
6043 format_ref_views
($remote ? '' : 'remotes'));
6045 fill_remote_heads
($remotedata);
6046 if (defined $remote) {
6047 git_print_header_div
('remotes', "$remote remote for $project");
6048 git_remote_block
($remote, $remotedata->{$remote}, undef, $head);
6050 git_print_header_div
('summary', "$project remotes");
6051 git_remotes_body
($remotedata, undef, $head);
6057 sub git_blob_plain
{
6061 if (!defined $hash) {
6062 if (defined $file_name) {
6063 my $base = $hash_base || git_get_head_hash
($project);
6064 $hash = git_get_hash_by_path
($base, $file_name, "blob")
6065 or die_error
(404, "Cannot find file");
6067 die_error
(400, "No file name defined");
6069 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6070 # blobs defined by non-textual hash id's can be cached
6074 open my $fd, "-|", git_cmd
(), "cat-file", "blob", $hash
6075 or die_error
(500, "Open git-cat-file blob '$hash' failed");
6077 # content-type (can include charset)
6078 $type = blob_contenttype
($fd, $file_name, $type);
6080 # "save as" filename, even when no $file_name is given
6081 my $save_as = "$hash";
6082 if (defined $file_name) {
6083 $save_as = $file_name;
6084 } elsif ($type =~ m/^text\//) {
6088 # With XSS prevention on, blobs of all types except a few known safe
6089 # ones are served with "Content-Disposition: attachment" to make sure
6090 # they don't run in our security domain. For certain image types,
6091 # blob view writes an <img> tag referring to blob_plain view, and we
6092 # want to be sure not to break that by serving the image as an
6093 # attachment (though Firefox 3 doesn't seem to care).
6094 my $sandbox = $prevent_xss &&
6095 $type !~ m!^(?:text/plain|image/(?:gif|png|jpeg))$!;
6099 -expires
=> $expires,
6100 -content_disposition
=>
6101 ($sandbox ? 'attachment' : 'inline')
6102 . '; filename="' . $save_as . '"');
6104 binmode STDOUT
, ':raw';
6106 binmode STDOUT
, ':utf8'; # as set at the beginning of gitweb.cgi
6113 if (!defined $hash) {
6114 if (defined $file_name) {
6115 my $base = $hash_base || git_get_head_hash
($project);
6116 $hash = git_get_hash_by_path
($base, $file_name, "blob")
6117 or die_error
(404, "Cannot find file");
6119 die_error
(400, "No file name defined");
6121 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6122 # blobs defined by non-textual hash id's can be cached
6126 my $have_blame = gitweb_check_feature
('blame');
6127 open my $fd, "-|", git_cmd
(), "cat-file", "blob", $hash
6128 or die_error
(500, "Couldn't cat $file_name, $hash");
6129 my $mimetype = blob_mimetype
($fd, $file_name);
6130 # use 'blob_plain' (aka 'raw') view for files that cannot be displayed
6131 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B
$fd) {
6133 return git_blob_plain
($mimetype);
6135 # we can have blame only for text/* mimetype
6136 $have_blame &&= ($mimetype =~ m!^text/!);
6138 my $highlight = gitweb_check_feature
('highlight');
6139 my $syntax = guess_file_syntax
($highlight, $mimetype, $file_name);
6140 $fd = run_highlighter
($fd, $highlight, $syntax)
6143 git_header_html
(undef, $expires);
6144 my $formats_nav = '';
6145 if (defined $hash_base && (my %co = parse_commit
($hash_base))) {
6146 if (defined $file_name) {
6149 $cgi->a({-href
=> href
(action
=>"blame", -replay
=>1)},
6154 $cgi->a({-href
=> href
(action
=>"history", -replay
=>1)},
6157 $cgi->a({-href
=> href
(action
=>"blob_plain", -replay
=>1)},
6160 $cgi->a({-href
=> href
(action
=>"blob",
6161 hash_base
=>"HEAD", file_name
=>$file_name)},
6165 $cgi->a({-href
=> href
(action
=>"blob_plain", -replay
=>1)},
6168 git_print_page_nav
('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
6169 git_print_header_div
('commit', esc_html
($co{'title'}), $hash_base);
6171 print "<div class=\"page_nav\">\n" .
6172 "<br/><br/></div>\n" .
6173 "<div class=\"title\">".esc_html
($hash)."</div>\n";
6175 git_print_page_path
($file_name, "blob", $hash_base);
6176 print "<div class=\"page_body\">\n";
6177 if ($mimetype =~ m!^image/!) {
6178 print qq
!<img type
="!.esc_attr($mimetype).qq!"!;
6180 print qq
! alt
="!.esc_attr($file_name).qq!" title
="!.esc_attr($file_name).qq!"!;
6183 href(action=>"blob_plain
", hash=>$hash,
6184 hash_base=>$hash_base, file_name=>$file_name) .
6188 while (my $line = <$fd>) {
6191 $line = untabify
($line);
6192 printf qq
!<div
class="pre"><a id
="l%i" href
="%s#l%i" class="linenr">%4i</a> %s</div
>\n!,
6193 $nr, href
(-replay
=> 1), $nr, $nr, $syntax ? $line : esc_html
($line, -nbsp
=>1);
6197 or print "Reading blob failed.\n";
6203 if (!defined $hash_base) {
6204 $hash_base = "HEAD";
6206 if (!defined $hash) {
6207 if (defined $file_name) {
6208 $hash = git_get_hash_by_path
($hash_base, $file_name, "tree");
6213 die_error
(404, "No such tree") unless defined($hash);
6215 my $show_sizes = gitweb_check_feature
('show-sizes');
6216 my $have_blame = gitweb_check_feature
('blame');
6221 open my $fd, "-|", git_cmd
(), "ls-tree", '-z',
6222 ($show_sizes ? '-l' : ()), @extra_options, $hash
6223 or die_error
(500, "Open git-ls-tree failed");
6224 @entries = map { chomp; $_ } <$fd>;
6226 or die_error
(404, "Reading tree failed");
6229 my $refs = git_get_references
();
6230 my $ref = format_ref_marker
($refs, $hash_base);
6233 if (defined $hash_base && (my %co = parse_commit
($hash_base))) {
6235 if (defined $file_name) {
6237 $cgi->a({-href
=> href
(action
=>"history", -replay
=>1)},
6239 $cgi->a({-href
=> href
(action
=>"tree",
6240 hash_base
=>"HEAD", file_name
=>$file_name)},
6243 my $snapshot_links = format_snapshot_links
($hash);
6244 if (defined $snapshot_links) {
6245 # FIXME: Should be available when we have no hash base as well.
6246 push @views_nav, $snapshot_links;
6248 git_print_page_nav
('tree','', $hash_base, undef, undef,
6249 join(' | ', @views_nav));
6250 git_print_header_div
('commit', esc_html
($co{'title'}) . $ref, $hash_base);
6253 print "<div class=\"page_nav\">\n";
6254 print "<br/><br/></div>\n";
6255 print "<div class=\"title\">".esc_html
($hash)."</div>\n";
6257 if (defined $file_name) {
6258 $basedir = $file_name;
6259 if ($basedir ne '' && substr($basedir, -1) ne '/') {
6262 git_print_page_path
($file_name, 'tree', $hash_base);
6264 print "<div class=\"page_body\">\n";
6265 print "<table class=\"tree\">\n";
6267 # '..' (top directory) link if possible
6268 if (defined $hash_base &&
6269 defined $file_name && $file_name =~ m![^/]+$!) {
6271 print "<tr class=\"dark\">\n";
6273 print "<tr class=\"light\">\n";
6277 my $up = $file_name;
6278 $up =~ s!/?[^/]+$!!;
6279 undef $up unless $up;
6280 # based on git_print_tree_entry
6281 print '<td class="mode">' . mode_str
('040000') . "</td>\n";
6282 print '<td class="size"> </td>'."\n" if $show_sizes;
6283 print '<td class="list">';
6284 print $cgi->a({-href
=> href
(action
=>"tree",
6285 hash_base
=>$hash_base,
6289 print "<td class=\"link\"></td>\n";
6293 foreach my $line (@entries) {
6294 my %t = parse_ls_tree_line
($line, -z
=> 1, -l
=> $show_sizes);
6297 print "<tr class=\"dark\">\n";
6299 print "<tr class=\"light\">\n";
6303 git_print_tree_entry
(\
%t, $basedir, $hash_base, $have_blame);
6307 print "</table>\n" .
6313 my ($project, $hash) = @_;
6315 # path/to/project.git -> project
6316 # path/to/project/.git -> project
6317 my $name = to_utf8
($project);
6318 $name =~ s
,([^/])/*\
.git
$,$1,;
6319 $name = basename
($name);
6321 $name =~ s/[[:cntrl:]]/?/g;
6324 if ($hash =~ /^[0-9a-fA-F]+$/) {
6325 # shorten SHA-1 hash
6326 my $full_hash = git_get_full_hash
($project, $hash);
6327 if ($full_hash =~ /^$hash/ && length($hash) > 7) {
6328 $ver = git_get_short_hash
($project, $hash);
6330 } elsif ($hash =~ m!^refs/tags/(.*)$!) {
6331 # tags don't need shortened SHA-1 hash
6334 # branches and other need shortened SHA-1 hash
6335 if ($hash =~ m!^refs/(?:heads|remotes)/(.*)$!) {
6338 $ver .= '-' . git_get_short_hash
($project, $hash);
6340 # in case of hierarchical branch names
6343 # name = project-version_string
6344 $name = "$name-$ver";
6346 return wantarray ? ($name, $name) : $name;
6350 my $format = $input_params{'snapshot_format'};
6351 if (!@snapshot_fmts) {
6352 die_error
(403, "Snapshots not allowed");
6354 # default to first supported snapshot format
6355 $format ||= $snapshot_fmts[0];
6356 if ($format !~ m/^[a-z0-9]+$/) {
6357 die_error
(400, "Invalid snapshot format parameter");
6358 } elsif (!exists($known_snapshot_formats{$format})) {
6359 die_error
(400, "Unknown snapshot format");
6360 } elsif ($known_snapshot_formats{$format}{'disabled'}) {
6361 die_error
(403, "Snapshot format not allowed");
6362 } elsif (!grep($_ eq $format, @snapshot_fmts)) {
6363 die_error
(403, "Unsupported snapshot format");
6366 my $type = git_get_type
("$hash^{}");
6368 die_error
(404, 'Object does not exist');
6369 } elsif ($type eq 'blob') {
6370 die_error
(400, 'Object is not a tree-ish');
6373 my ($name, $prefix) = snapshot_name
($project, $hash);
6374 my $filename = "$name$known_snapshot_formats{$format}{'suffix'}";
6375 my $cmd = quote_command
(
6376 git_cmd
(), 'archive',
6377 "--format=$known_snapshot_formats{$format}{'format'}",
6378 "--prefix=$prefix/", $hash);
6379 if (exists $known_snapshot_formats{$format}{'compressor'}) {
6380 $cmd .= ' | ' . quote_command
(@{$known_snapshot_formats{$format}{'compressor'}});
6383 $filename =~ s/(["\\])/\\$1/g;
6385 -type
=> $known_snapshot_formats{$format}{'type'},
6386 -content_disposition
=> 'inline; filename="' . $filename . '"',
6387 -status
=> '200 OK');
6389 open my $fd, "-|", $cmd
6390 or die_error
(500, "Execute git-archive failed");
6391 binmode STDOUT
, ':raw';
6393 binmode STDOUT
, ':utf8'; # as set at the beginning of gitweb.cgi
6397 sub git_log_generic
{
6398 my ($fmt_name, $body_subr, $base, $parent, $file_name, $file_hash) = @_;
6400 my $head = git_get_head_hash
($project);
6401 if (!defined $base) {
6404 if (!defined $page) {
6407 my $refs = git_get_references
();
6409 my $commit_hash = $base;
6410 if (defined $parent) {
6411 $commit_hash = "$parent..$base";
6414 parse_commits
($commit_hash, 101, (100 * $page),
6415 defined $file_name ? ($file_name, "--full-history") : ());
6418 if (!defined $file_hash && defined $file_name) {
6419 # some commits could have deleted file in question,
6420 # and not have it in tree, but one of them has to have it
6421 for (my $i = 0; $i < @commitlist; $i++) {
6422 $file_hash = git_get_hash_by_path
($commitlist[$i]{'id'}, $file_name);
6423 last if defined $file_hash;
6426 if (defined $file_hash) {
6427 $ftype = git_get_type
($file_hash);
6429 if (defined $file_name && !defined $ftype) {
6430 die_error
(500, "Unknown type of object");
6433 if (defined $file_name) {
6434 %co = parse_commit
($base)
6435 or die_error
(404, "Unknown commit object");
6439 my $paging_nav = format_paging_nav
($fmt_name, $page, $#commitlist >= 100);
6441 if ($#commitlist >= 100) {
6443 $cgi->a({-href
=> href
(-replay
=>1, page
=>$page+1),
6444 -accesskey
=> "n", -title
=> "Alt-n"}, "next");
6446 my $patch_max = gitweb_get_feature
('patches');
6447 if ($patch_max && !defined $file_name) {
6448 if ($patch_max < 0 || @commitlist <= $patch_max) {
6449 $paging_nav .= " ⋅ " .
6450 $cgi->a({-href
=> href
(action
=>"patches", -replay
=>1)},
6456 git_print_page_nav
($fmt_name,'', $hash,$hash,$hash, $paging_nav);
6457 if (defined $file_name) {
6458 git_print_header_div
('commit', esc_html
($co{'title'}), $base);
6460 git_print_header_div
('summary', $project)
6462 git_print_page_path
($file_name, $ftype, $hash_base)
6463 if (defined $file_name);
6465 $body_subr->(\
@commitlist, 0, 99, $refs, $next_link,
6466 $file_name, $file_hash, $ftype);
6472 git_log_generic
('log', \
&git_log_body
,
6473 $hash, $hash_parent);
6477 $hash ||= $hash_base || "HEAD";
6478 my %co = parse_commit
($hash)
6479 or die_error
(404, "Unknown commit object");
6481 my $parent = $co{'parent'};
6482 my $parents = $co{'parents'}; # listref
6484 # we need to prepare $formats_nav before any parameter munging
6486 if (!defined $parent) {
6488 $formats_nav .= '(initial)';
6489 } elsif (@$parents == 1) {
6490 # single parent commit
6493 $cgi->a({-href
=> href
(action
=>"commit",
6495 esc_html
(substr($parent, 0, 7))) .
6502 $cgi->a({-href
=> href
(action
=>"commit",
6504 esc_html
(substr($_, 0, 7)));
6508 if (gitweb_check_feature
('patches') && @$parents <= 1) {
6509 $formats_nav .= " | " .
6510 $cgi->a({-href
=> href
(action
=>"patch", -replay
=>1)},
6514 if (!defined $parent) {
6518 open my $fd, "-|", git_cmd
(), "diff-tree", '-r', "--no-commit-id",
6520 (@$parents <= 1 ? $parent : '-c'),
6522 or die_error
(500, "Open git-diff-tree failed");
6523 @difftree = map { chomp; $_ } <$fd>;
6524 close $fd or die_error
(404, "Reading git-diff-tree failed");
6526 # non-textual hash id's can be cached
6528 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6531 my $refs = git_get_references
();
6532 my $ref = format_ref_marker
($refs, $co{'id'});
6534 git_header_html
(undef, $expires);
6535 git_print_page_nav
('commit', '',
6536 $hash, $co{'tree'}, $hash,
6539 if (defined $co{'parent'}) {
6540 git_print_header_div
('commitdiff', esc_html
($co{'title'}) . $ref, $hash);
6542 git_print_header_div
('tree', esc_html
($co{'title'}) . $ref, $co{'tree'}, $hash);
6544 print "<div class=\"title_text\">\n" .
6545 "<table class=\"object_header\">\n";
6546 git_print_authorship_rows
(\
%co);
6547 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
6550 "<td class=\"sha1\">" .
6551 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$co{'tree'}, hash_base
=>$hash),
6552 class => "list"}, $co{'tree'}) .
6554 "<td class=\"link\">" .
6555 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$co{'tree'}, hash_base
=>$hash)},
6557 my $snapshot_links = format_snapshot_links
($hash);
6558 if (defined $snapshot_links) {
6559 print " | " . $snapshot_links;
6564 foreach my $par (@$parents) {
6567 "<td class=\"sha1\">" .
6568 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$par),
6569 class => "list"}, $par) .
6571 "<td class=\"link\">" .
6572 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$par)}, "commit") .
6574 $cgi->a({-href
=> href
(action
=>"commitdiff", hash
=>$hash, hash_parent
=>$par)}, "diff") .
6581 print "<div class=\"page_body\">\n";
6582 git_print_log
($co{'comment'});
6585 git_difftree_body
(\
@difftree, $hash, @$parents);
6591 # object is defined by:
6592 # - hash or hash_base alone
6593 # - hash_base and file_name
6596 # - hash or hash_base alone
6597 if ($hash || ($hash_base && !defined $file_name)) {
6598 my $object_id = $hash || $hash_base;
6600 open my $fd, "-|", quote_command
(
6601 git_cmd
(), 'cat-file', '-t', $object_id) . ' 2> /dev/null'
6602 or die_error
(404, "Object does not exist");
6606 or die_error
(404, "Object does not exist");
6608 # - hash_base and file_name
6609 } elsif ($hash_base && defined $file_name) {
6610 $file_name =~ s
,/+$,,;
6612 system(git_cmd
(), "cat-file", '-e', $hash_base) == 0
6613 or die_error
(404, "Base object does not exist");
6615 # here errors should not hapen
6616 open my $fd, "-|", git_cmd
(), "ls-tree", $hash_base, "--", $file_name
6617 or die_error
(500, "Open git-ls-tree failed");
6621 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
6622 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
6623 die_error
(404, "File or directory for given base does not exist");
6628 die_error
(400, "Not enough information to find object");
6631 print $cgi->redirect(-uri
=> href
(action
=>$type, -full
=>1,
6632 hash
=>$hash, hash_base
=>$hash_base,
6633 file_name
=>$file_name),
6634 -status
=> '302 Found');
6638 my $format = shift || 'html';
6645 # preparing $fd and %diffinfo for git_patchset_body
6647 if (defined $hash_base && defined $hash_parent_base) {
6648 if (defined $file_name) {
6650 open $fd, "-|", git_cmd
(), "diff-tree", '-r', @diff_opts,
6651 $hash_parent_base, $hash_base,
6652 "--", (defined $file_parent ? $file_parent : ()), $file_name
6653 or die_error
(500, "Open git-diff-tree failed");
6654 @difftree = map { chomp; $_ } <$fd>;
6656 or die_error
(404, "Reading git-diff-tree failed");
6658 or die_error
(404, "Blob diff not found");
6660 } elsif (defined $hash &&
6661 $hash =~ /[0-9a-fA-F]{40}/) {
6662 # try to find filename from $hash
6664 # read filtered raw output
6665 open $fd, "-|", git_cmd
(), "diff-tree", '-r', @diff_opts,
6666 $hash_parent_base, $hash_base, "--"
6667 or die_error
(500, "Open git-diff-tree failed");
6669 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
6671 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
6672 map { chomp; $_ } <$fd>;
6674 or die_error
(404, "Reading git-diff-tree failed");
6676 or die_error
(404, "Blob diff not found");
6679 die_error
(400, "Missing one of the blob diff parameters");
6682 if (@difftree > 1) {
6683 die_error
(400, "Ambiguous blob diff specification");
6686 %diffinfo = parse_difftree_raw_line
($difftree[0]);
6687 $file_parent ||= $diffinfo{'from_file'} || $file_name;
6688 $file_name ||= $diffinfo{'to_file'};
6690 $hash_parent ||= $diffinfo{'from_id'};
6691 $hash ||= $diffinfo{'to_id'};
6693 # non-textual hash id's can be cached
6694 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
6695 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
6700 open $fd, "-|", git_cmd
(), "diff-tree", '-r', @diff_opts,
6701 '-p', ($format eq 'html' ? "--full-index" : ()),
6702 $hash_parent_base, $hash_base,
6703 "--", (defined $file_parent ? $file_parent : ()), $file_name
6704 or die_error
(500, "Open git-diff-tree failed");
6707 # old/legacy style URI -- not generated anymore since 1.4.3.
6709 die_error
('404 Not Found', "Missing one of the blob diff parameters")
6713 if ($format eq 'html') {
6715 $cgi->a({-href
=> href
(action
=>"blobdiff_plain", -replay
=>1)},
6717 git_header_html
(undef, $expires);
6718 if (defined $hash_base && (my %co = parse_commit
($hash_base))) {
6719 git_print_page_nav
('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
6720 git_print_header_div
('commit', esc_html
($co{'title'}), $hash_base);
6722 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
6723 print "<div class=\"title\">".esc_html
("$hash vs $hash_parent")."</div>\n";
6725 if (defined $file_name) {
6726 git_print_page_path
($file_name, "blob", $hash_base);
6728 print "<div class=\"page_path\"></div>\n";
6731 } elsif ($format eq 'plain') {
6733 -type
=> 'text/plain',
6734 -charset
=> 'utf-8',
6735 -expires
=> $expires,
6736 -content_disposition
=> 'inline; filename="' . "$file_name" . '.patch"');
6738 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
6741 die_error
(400, "Unknown blobdiff format");
6745 if ($format eq 'html') {
6746 print "<div class=\"page_body\">\n";
6748 git_patchset_body
($fd, [ \
%diffinfo ], $hash_base, $hash_parent_base);
6751 print "</div>\n"; # class="page_body"
6755 while (my $line = <$fd>) {
6756 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
6757 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
6761 last if $line =~ m!^\+\+\+!;
6769 sub git_blobdiff_plain
{
6770 git_blobdiff
('plain');
6773 sub git_commitdiff
{
6775 my $format = $params{-format
} || 'html';
6777 my ($patch_max) = gitweb_get_feature
('patches');
6778 if ($format eq 'patch') {
6779 die_error
(403, "Patch view not allowed") unless $patch_max;
6782 $hash ||= $hash_base || "HEAD";
6783 my %co = parse_commit
($hash)
6784 or die_error
(404, "Unknown commit object");
6786 # choose format for commitdiff for merge
6787 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
6788 $hash_parent = '--cc';
6790 # we need to prepare $formats_nav before almost any parameter munging
6792 if ($format eq 'html') {
6794 $cgi->a({-href
=> href
(action
=>"commitdiff_plain", -replay
=>1)},
6796 if ($patch_max && @{$co{'parents'}} <= 1) {
6797 $formats_nav .= " | " .
6798 $cgi->a({-href
=> href
(action
=>"patch", -replay
=>1)},
6802 if (defined $hash_parent &&
6803 $hash_parent ne '-c' && $hash_parent ne '--cc') {
6804 # commitdiff with two commits given
6805 my $hash_parent_short = $hash_parent;
6806 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
6807 $hash_parent_short = substr($hash_parent, 0, 7);
6811 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
6812 if ($co{'parents'}[$i] eq $hash_parent) {
6813 $formats_nav .= ' parent ' . ($i+1);
6817 $formats_nav .= ': ' .
6818 $cgi->a({-href
=> href
(action
=>"commitdiff",
6819 hash
=>$hash_parent)},
6820 esc_html
($hash_parent_short)) .
6822 } elsif (!$co{'parent'}) {
6824 $formats_nav .= ' (initial)';
6825 } elsif (scalar @{$co{'parents'}} == 1) {
6826 # single parent commit
6829 $cgi->a({-href
=> href
(action
=>"commitdiff",
6830 hash
=>$co{'parent'})},
6831 esc_html
(substr($co{'parent'}, 0, 7))) .
6835 if ($hash_parent eq '--cc') {
6836 $formats_nav .= ' | ' .
6837 $cgi->a({-href
=> href
(action
=>"commitdiff",
6838 hash
=>$hash, hash_parent
=>'-c')},
6840 } else { # $hash_parent eq '-c'
6841 $formats_nav .= ' | ' .
6842 $cgi->a({-href
=> href
(action
=>"commitdiff",
6843 hash
=>$hash, hash_parent
=>'--cc')},
6849 $cgi->a({-href
=> href
(action
=>"commitdiff",
6851 esc_html
(substr($_, 0, 7)));
6852 } @{$co{'parents'}} ) .
6857 my $hash_parent_param = $hash_parent;
6858 if (!defined $hash_parent_param) {
6859 # --cc for multiple parents, --root for parentless
6860 $hash_parent_param =
6861 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
6867 if ($format eq 'html') {
6868 open $fd, "-|", git_cmd
(), "diff-tree", '-r', @diff_opts,
6869 "--no-commit-id", "--patch-with-raw", "--full-index",
6870 $hash_parent_param, $hash, "--"
6871 or die_error
(500, "Open git-diff-tree failed");
6873 while (my $line = <$fd>) {
6875 # empty line ends raw part of diff-tree output
6877 push @difftree, scalar parse_difftree_raw_line
($line);
6880 } elsif ($format eq 'plain') {
6881 open $fd, "-|", git_cmd
(), "diff-tree", '-r', @diff_opts,
6882 '-p', $hash_parent_param, $hash, "--"
6883 or die_error
(500, "Open git-diff-tree failed");
6884 } elsif ($format eq 'patch') {
6885 # For commit ranges, we limit the output to the number of
6886 # patches specified in the 'patches' feature.
6887 # For single commits, we limit the output to a single patch,
6888 # diverging from the git-format-patch default.
6889 my @commit_spec = ();
6891 if ($patch_max > 0) {
6892 push @commit_spec, "-$patch_max";
6894 push @commit_spec, '-n', "$hash_parent..$hash";
6896 if ($params{-single
}) {
6897 push @commit_spec, '-1';
6899 if ($patch_max > 0) {
6900 push @commit_spec, "-$patch_max";
6902 push @commit_spec, "-n";
6904 push @commit_spec, '--root', $hash;
6906 open $fd, "-|", git_cmd
(), "format-patch", @diff_opts,
6907 '--encoding=utf8', '--stdout', @commit_spec
6908 or die_error
(500, "Open git-format-patch failed");
6910 die_error
(400, "Unknown commitdiff format");
6913 # non-textual hash id's can be cached
6915 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6919 # write commit message
6920 if ($format eq 'html') {
6921 my $refs = git_get_references
();
6922 my $ref = format_ref_marker
($refs, $co{'id'});
6924 git_header_html
(undef, $expires);
6925 git_print_page_nav
('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
6926 git_print_header_div
('commit', esc_html
($co{'title'}) . $ref, $hash);
6927 print "<div class=\"title_text\">\n" .
6928 "<table class=\"object_header\">\n";
6929 git_print_authorship_rows
(\
%co);
6932 print "<div class=\"page_body\">\n";
6933 if (@{$co{'comment'}} > 1) {
6934 print "<div class=\"log\">\n";
6935 git_print_log
($co{'comment'}, -final_empty_line
=> 1, -remove_title
=> 1);
6936 print "</div>\n"; # class="log"
6939 } elsif ($format eq 'plain') {
6940 my $refs = git_get_references
("tags");
6941 my $tagname = git_get_rev_name_tags
($hash);
6942 my $filename = basename
($project) . "-$hash.patch";
6945 -type
=> 'text/plain',
6946 -charset
=> 'utf-8',
6947 -expires
=> $expires,
6948 -content_disposition
=> 'inline; filename="' . "$filename" . '"');
6949 my %ad = parse_date
($co{'author_epoch'}, $co{'author_tz'});
6950 print "From: " . to_utf8
($co{'author'}) . "\n";
6951 print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
6952 print "Subject: " . to_utf8
($co{'title'}) . "\n";
6954 print "X-Git-Tag: $tagname\n" if $tagname;
6955 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
6957 foreach my $line (@{$co{'comment'}}) {
6958 print to_utf8
($line) . "\n";
6961 } elsif ($format eq 'patch') {
6962 my $filename = basename
($project) . "-$hash.patch";
6965 -type
=> 'text/plain',
6966 -charset
=> 'utf-8',
6967 -expires
=> $expires,
6968 -content_disposition
=> 'inline; filename="' . "$filename" . '"');
6972 if ($format eq 'html') {
6973 my $use_parents = !defined $hash_parent ||
6974 $hash_parent eq '-c' || $hash_parent eq '--cc';
6975 git_difftree_body
(\
@difftree, $hash,
6976 $use_parents ? @{$co{'parents'}} : $hash_parent);
6979 git_patchset_body
($fd, \
@difftree, $hash,
6980 $use_parents ? @{$co{'parents'}} : $hash_parent);
6982 print "</div>\n"; # class="page_body"
6985 } elsif ($format eq 'plain') {
6989 or print "Reading git-diff-tree failed\n";
6990 } elsif ($format eq 'patch') {
6994 or print "Reading git-format-patch failed\n";
6998 sub git_commitdiff_plain
{
6999 git_commitdiff
(-format
=> 'plain');
7002 # format-patch-style patches
7004 git_commitdiff
(-format
=> 'patch', -single
=> 1);
7008 git_commitdiff
(-format
=> 'patch');
7012 git_log_generic
('history', \
&git_history_body
,
7013 $hash_base, $hash_parent_base,
7018 gitweb_check_feature
('search') or die_error
(403, "Search is disabled");
7019 if (!defined $searchtext) {
7020 die_error
(400, "Text field is empty");
7022 if (!defined $hash) {
7023 $hash = git_get_head_hash
($project);
7025 my %co = parse_commit
($hash);
7027 die_error
(404, "Unknown commit object");
7029 if (!defined $page) {
7033 $searchtype ||= 'commit';
7034 if ($searchtype eq 'pickaxe') {
7035 # pickaxe may take all resources of your box and run for several minutes
7036 # with every query - so decide by yourself how public you make this feature
7037 gitweb_check_feature
('pickaxe')
7038 or die_error
(403, "Pickaxe is disabled");
7040 if ($searchtype eq 'grep') {
7041 gitweb_check_feature
('grep')[0]
7042 or die_error
(403, "Grep is disabled");
7047 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
7049 if ($searchtype eq 'commit') {
7050 $greptype = "--grep=";
7051 } elsif ($searchtype eq 'author') {
7052 $greptype = "--author=";
7053 } elsif ($searchtype eq 'committer') {
7054 $greptype = "--committer=";
7056 $greptype .= $searchtext;
7057 my @commitlist = parse_commits
($hash, 101, (100 * $page), undef,
7058 $greptype, '--regexp-ignore-case',
7059 $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
7061 my $paging_nav = '';
7064 $cgi->a({-href
=> href
(action
=>"search", hash
=>$hash,
7065 searchtext
=>$searchtext,
7066 searchtype
=>$searchtype)},
7068 $paging_nav .= " ⋅ " .
7069 $cgi->a({-href
=> href
(-replay
=>1, page
=>$page-1),
7070 -accesskey
=> "p", -title
=> "Alt-p"}, "prev");
7072 $paging_nav .= "first";
7073 $paging_nav .= " ⋅ prev";
7076 if ($#commitlist >= 100) {
7078 $cgi->a({-href
=> href
(-replay
=>1, page
=>$page+1),
7079 -accesskey
=> "n", -title
=> "Alt-n"}, "next");
7080 $paging_nav .= " ⋅ $next_link";
7082 $paging_nav .= " ⋅ next";
7085 git_print_page_nav
('','', $hash,$co{'tree'},$hash, $paging_nav);
7086 git_print_header_div
('commit', esc_html
($co{'title'}), $hash);
7087 if ($page == 0 && !@commitlist) {
7088 print "<p>No match.</p>\n";
7090 git_search_grep_body
(\
@commitlist, 0, 99, $next_link);
7094 if ($searchtype eq 'pickaxe') {
7095 git_print_page_nav
('','', $hash,$co{'tree'},$hash);
7096 git_print_header_div
('commit', esc_html
($co{'title'}), $hash);
7098 print "<table class=\"pickaxe search\">\n";
7101 open my $fd, '-|', git_cmd
(), '--no-pager', 'log', @diff_opts,
7102 '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
7103 ($search_use_regexp ? '--pickaxe-regex' : ());
7106 while (my $line = <$fd>) {
7110 my %set = parse_difftree_raw_line
($line);
7111 if (defined $set{'commit'}) {
7112 # finish previous commit
7115 "<td class=\"link\">" .
7116 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$co{'id'})}, "commit") .
7118 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$co{'tree'}, hash_base
=>$co{'id'})}, "tree");
7124 print "<tr class=\"dark\">\n";
7126 print "<tr class=\"light\">\n";
7129 %co = parse_commit
($set{'commit'});
7130 my $author = chop_and_escape_str
($co{'author_name'}, 15, 5);
7131 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
7132 "<td><i>$author</i></td>\n" .
7134 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$co{'id'}),
7135 -class => "list subject"},
7136 chop_and_escape_str
($co{'title'}, 50) . "<br/>");
7137 } elsif (defined $set{'to_id'}) {
7138 next if ($set{'to_id'} =~ m/^0{40}$/);
7140 print $cgi->a({-href
=> href
(action
=>"blob", hash_base
=>$co{'id'},
7141 hash
=>$set{'to_id'}, file_name
=>$set{'to_file'}),
7143 "<span class=\"match\">" . esc_path
($set{'file'}) . "</span>") .
7149 # finish last commit (warning: repetition!)
7152 "<td class=\"link\">" .
7153 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$co{'id'})}, "commit") .
7155 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$co{'tree'}, hash_base
=>$co{'id'})}, "tree");
7163 if ($searchtype eq 'grep') {
7164 git_print_page_nav
('','', $hash,$co{'tree'},$hash);
7165 git_print_header_div
('commit', esc_html
($co{'title'}), $hash);
7167 print "<table class=\"grep_search\">\n";
7171 open my $fd, "-|", git_cmd
(), 'grep', '-n',
7172 $search_use_regexp ? ('-E', '-i') : '-F',
7173 $searchtext, $co{'tree'};
7175 while (my $line = <$fd>) {
7177 my ($file, $lno, $ltext, $binary);
7178 last if ($matches++ > 1000);
7179 if ($line =~ /^Binary file (.+) matches$/) {
7183 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
7185 if ($file ne $lastfile) {
7186 $lastfile and print "</td></tr>\n";
7188 print "<tr class=\"dark\">\n";
7190 print "<tr class=\"light\">\n";
7192 print "<td class=\"list\">".
7193 $cgi->a({-href
=> href
(action
=>"blob", hash
=>$co{'hash'},
7194 file_name
=>"$file"),
7195 -class => "list"}, esc_path
($file));
7196 print "</td><td>\n";
7200 print "<div class=\"binary\">Binary file</div>\n";
7202 $ltext = untabify
($ltext);
7203 if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
7204 $ltext = esc_html
($1, -nbsp
=>1);
7205 $ltext .= '<span class="match">';
7206 $ltext .= esc_html
($2, -nbsp
=>1);
7207 $ltext .= '</span>';
7208 $ltext .= esc_html
($3, -nbsp
=>1);
7210 $ltext = esc_html
($ltext, -nbsp
=>1);
7212 print "<div class=\"pre\">" .
7213 $cgi->a({-href
=> href
(action
=>"blob", hash
=>$co{'hash'},
7214 file_name
=>"$file").'#l'.$lno,
7215 -class => "linenr"}, sprintf('%4i', $lno))
7216 . ' ' . $ltext . "</div>\n";
7220 print "</td></tr>\n";
7221 if ($matches > 1000) {
7222 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
7225 print "<div class=\"diff nodifferences\">No matches found</div>\n";
7234 sub git_search_help
{
7236 git_print_page_nav
('','', $hash,$hash,$hash);
7238 <p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without
7239 regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,
7240 the pattern entered is recognized as the POSIX extended
7241 <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case
7244 <dt><b>commit</b></dt>
7245 <dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
7247 my $have_grep = gitweb_check_feature
('grep');
7250 <dt><b>grep</b></dt>
7251 <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
7252 a different one) are searched for the given pattern. On large trees, this search can take
7253 a while and put some strain on the server, so please use it with some consideration. Note that
7254 due to git-grep peculiarity, currently if regexp mode is turned off, the matches are
7255 case-sensitive.</dd>
7259 <dt><b>author</b></dt>
7260 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>
7261 <dt><b>committer</b></dt>
7262 <dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>
7264 my $have_pickaxe = gitweb_check_feature
('pickaxe');
7265 if ($have_pickaxe) {
7267 <dt><b>pickaxe</b></dt>
7268 <dd>All commits that caused the string to appear or disappear from any file (changes that
7269 added, removed or "modified" the string) will be listed. This search can take a while and
7270 takes a lot of strain on the server, so please use it wisely. Note that since you may be
7271 interested even in changes just changing the case as well, this search is case sensitive.</dd>
7279 git_log_generic
('shortlog', \
&git_shortlog_body
,
7280 $hash, $hash_parent);
7283 ## ......................................................................
7284 ## feeds (RSS, Atom; OPML)
7287 my $format = shift || 'atom';
7288 my $have_blame = gitweb_check_feature
('blame');
7290 # Atom: http://www.atomenabled.org/developers/syndication/
7291 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
7292 if ($format ne 'rss' && $format ne 'atom') {
7293 die_error
(400, "Unknown web feed format");
7296 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
7297 my $head = $hash || 'HEAD';
7298 my @commitlist = parse_commits
($head, 150, 0, $file_name);
7302 my $content_type = "application/$format+xml";
7303 if (defined $cgi->http('HTTP_ACCEPT') &&
7304 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
7305 # browser (feed reader) prefers text/xml
7306 $content_type = 'text/xml';
7308 if (defined($commitlist[0])) {
7309 %latest_commit = %{$commitlist[0]};
7310 my $latest_epoch = $latest_commit{'committer_epoch'};
7311 %latest_date = parse_date
($latest_epoch, $latest_commit{'comitter_tz'});
7312 my $if_modified = $cgi->http('IF_MODIFIED_SINCE');
7313 if (defined $if_modified) {
7315 if (eval { require HTTP
::Date
; 1; }) {
7316 $since = HTTP
::Date
::str2time
($if_modified);
7317 } elsif (eval { require Time
::ParseDate
; 1; }) {
7318 $since = Time
::ParseDate
::parsedate
($if_modified, GMT
=> 1);
7320 if (defined $since && $latest_epoch <= $since) {
7322 -type
=> $content_type,
7323 -charset
=> 'utf-8',
7324 -last_modified
=> $latest_date{'rfc2822'},
7325 -status
=> '304 Not Modified');
7330 -type
=> $content_type,
7331 -charset
=> 'utf-8',
7332 -last_modified
=> $latest_date{'rfc2822'});
7335 -type
=> $content_type,
7336 -charset
=> 'utf-8');
7339 # Optimization: skip generating the body if client asks only
7340 # for Last-Modified date.
7341 return if ($cgi->request_method() eq 'HEAD');
7344 my $title = "$site_name - $project/$action";
7345 my $feed_type = 'log';
7346 if (defined $hash) {
7347 $title .= " - '$hash'";
7348 $feed_type = 'branch log';
7349 if (defined $file_name) {
7350 $title .= " :: $file_name";
7351 $feed_type = 'history';
7353 } elsif (defined $file_name) {
7354 $title .= " - $file_name";
7355 $feed_type = 'history';
7357 $title .= " $feed_type";
7358 my $descr = git_get_project_description
($project);
7359 if (defined $descr) {
7360 $descr = esc_html
($descr);
7362 $descr = "$project " .
7363 ($format eq 'rss' ? 'RSS' : 'Atom') .
7366 my $owner = git_get_project_owner
($project);
7367 $owner = esc_html
($owner);
7371 if (defined $file_name) {
7372 $alt_url = href
(-full
=>1, action
=>"history", hash
=>$hash, file_name
=>$file_name);
7373 } elsif (defined $hash) {
7374 $alt_url = href
(-full
=>1, action
=>"log", hash
=>$hash);
7376 $alt_url = href
(-full
=>1, action
=>"summary");
7378 print qq
!<?xml version
="1.0" encoding
="utf-8"?>\n!;
7379 if ($format eq 'rss') {
7381 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
7384 print "<title>$title</title>\n" .
7385 "<link>$alt_url</link>\n" .
7386 "<description>$descr</description>\n" .
7387 "<language>en</language>\n" .
7388 # project owner is responsible for 'editorial' content
7389 "<managingEditor>$owner</managingEditor>\n";
7390 if (defined $logo || defined $favicon) {
7391 # prefer the logo to the favicon, since RSS
7392 # doesn't allow both
7393 my $img = esc_url
($logo || $favicon);
7395 "<url>$img</url>\n" .
7396 "<title>$title</title>\n" .
7397 "<link>$alt_url</link>\n" .
7401 print "<pubDate>$latest_date{'rfc2822'}</pubDate>\n";
7402 print "<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";
7404 print "<generator>gitweb v.$version/$git_version</generator>\n";
7405 } elsif ($format eq 'atom') {
7407 <feed xmlns="http://www.w3.org/2005/Atom">
7409 print "<title>$title</title>\n" .
7410 "<subtitle>$descr</subtitle>\n" .
7411 '<link rel="alternate" type="text/html" href="' .
7412 $alt_url . '" />' . "\n" .
7413 '<link rel="self" type="' . $content_type . '" href="' .
7414 $cgi->self_url() . '" />' . "\n" .
7415 "<id>" . href
(-full
=>1) . "</id>\n" .
7416 # use project owner for feed author
7417 "<author><name>$owner</name></author>\n";
7418 if (defined $favicon) {
7419 print "<icon>" . esc_url
($favicon) . "</icon>\n";
7421 if (defined $logo) {
7422 # not twice as wide as tall: 72 x 27 pixels
7423 print "<logo>" . esc_url
($logo) . "</logo>\n";
7425 if (! %latest_date) {
7426 # dummy date to keep the feed valid until commits trickle in:
7427 print "<updated>1970-01-01T00:00:00Z</updated>\n";
7429 print "<updated>$latest_date{'iso-8601'}</updated>\n";
7431 print "<generator version='$version/$git_version'>gitweb</generator>\n";
7435 for (my $i = 0; $i <= $#commitlist; $i++) {
7436 my %co = %{$commitlist[$i]};
7437 my $commit = $co{'id'};
7438 # we read 150, we always show 30 and the ones more recent than 48 hours
7439 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
7442 my %cd = parse_date
($co{'author_epoch'}, $co{'author_tz'});
7444 # get list of changed files
7445 open my $fd, "-|", git_cmd
(), "diff-tree", '-r', @diff_opts,
7446 $co{'parent'} || "--root",
7447 $co{'id'}, "--", (defined $file_name ? $file_name : ())
7449 my @difftree = map { chomp; $_ } <$fd>;
7453 # print element (entry, item)
7454 my $co_url = href
(-full
=>1, action
=>"commitdiff", hash
=>$commit);
7455 if ($format eq 'rss') {
7457 "<title>" . esc_html
($co{'title'}) . "</title>\n" .
7458 "<author>" . esc_html
($co{'author'}) . "</author>\n" .
7459 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
7460 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
7461 "<link>$co_url</link>\n" .
7462 "<description>" . esc_html
($co{'title'}) . "</description>\n" .
7463 "<content:encoded>" .
7465 } elsif ($format eq 'atom') {
7467 "<title type=\"html\">" . esc_html
($co{'title'}) . "</title>\n" .
7468 "<updated>$cd{'iso-8601'}</updated>\n" .
7470 " <name>" . esc_html
($co{'author_name'}) . "</name>\n";
7471 if ($co{'author_email'}) {
7472 print " <email>" . esc_html
($co{'author_email'}) . "</email>\n";
7474 print "</author>\n" .
7475 # use committer for contributor
7477 " <name>" . esc_html
($co{'committer_name'}) . "</name>\n";
7478 if ($co{'committer_email'}) {
7479 print " <email>" . esc_html
($co{'committer_email'}) . "</email>\n";
7481 print "</contributor>\n" .
7482 "<published>$cd{'iso-8601'}</published>\n" .
7483 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
7484 "<id>$co_url</id>\n" .
7485 "<content type=\"xhtml\" xml:base=\"" . esc_url
($my_url) . "\">\n" .
7486 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
7488 my $comment = $co{'comment'};
7490 foreach my $line (@$comment) {
7491 $line = esc_html
($line);
7494 print "</pre><ul>\n";
7495 foreach my $difftree_line (@difftree) {
7496 my %difftree = parse_difftree_raw_line
($difftree_line);
7497 next if !$difftree{'from_id'};
7499 my $file = $difftree{'file'} || $difftree{'to_file'};
7503 $cgi->a({-href
=> href
(-full
=>1, action
=>"blobdiff",
7504 hash
=>$difftree{'to_id'}, hash_parent
=>$difftree{'from_id'},
7505 hash_base
=>$co{'id'}, hash_parent_base
=>$co{'parent'},
7506 file_name
=>$file, file_parent
=>$difftree{'from_file'}),
7507 -title
=> "diff"}, 'D');
7509 print $cgi->a({-href
=> href
(-full
=>1, action
=>"blame",
7510 file_name
=>$file, hash_base
=>$commit),
7511 -title
=> "blame"}, 'B');
7513 # if this is not a feed of a file history
7514 if (!defined $file_name || $file_name ne $file) {
7515 print $cgi->a({-href
=> href
(-full
=>1, action
=>"history",
7516 file_name
=>$file, hash
=>$commit),
7517 -title
=> "history"}, 'H');
7519 $file = esc_path
($file);
7523 if ($format eq 'rss') {
7524 print "</ul>]]>\n" .
7525 "</content:encoded>\n" .
7527 } elsif ($format eq 'atom') {
7528 print "</ul>\n</div>\n" .
7535 if ($format eq 'rss') {
7536 print "</channel>\n</rss>\n";
7537 } elsif ($format eq 'atom') {
7551 my @list = git_get_projects_list
();
7553 die_error
(404, "No projects found");
7557 -type
=> 'text/xml',
7558 -charset
=> 'utf-8',
7559 -content_disposition
=> 'inline; filename="opml.xml"');
7562 <?xml version="1.0" encoding="utf-8"?>
7563 <opml version="1.0">
7565 <title>$site_name OPML Export</title>
7568 <outline text="git RSS feeds">
7571 foreach my $pr (@list) {
7573 my $head = git_get_head_hash
($proj{'path'});
7574 if (!defined $head) {
7577 $git_dir = "$projectroot/$proj{'path'}";
7578 my %co = parse_commit
($head);
7583 my $path = esc_html
(chop_str
($proj{'path'}, 25, 5));
7584 my $rss = href
('project' => $proj{'path'}, 'action' => 'rss', -full
=> 1);
7585 my $html = href
('project' => $proj{'path'}, 'action' => 'summary', -full
=> 1);
7586 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";