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.
326 # Note that this controls all search features, which means that if
327 # it is disabled, then 'grep' and 'pickaxe' search would also be
333 # Enable grep search, which will list the files in currently selected
334 # tree containing the given string. Enabled by default. This can be
335 # potentially CPU-intensive, of course.
336 # Note that you need to have 'search' feature enabled too.
338 # To enable system wide have in $GITWEB_CONFIG
339 # $feature{'grep'}{'default'} = [1];
340 # To have project specific config enable override in $GITWEB_CONFIG
341 # $feature{'grep'}{'override'} = 1;
342 # and in project config gitweb.grep = 0|1;
344 'sub' => sub { feature_bool
('grep', @_) },
348 # Enable the pickaxe search, which will list the commits that modified
349 # a given string in a file. This can be practical and quite faster
350 # alternative to 'blame', but still potentially CPU-intensive.
351 # Note that you need to have 'search' feature enabled too.
353 # To enable system wide have in $GITWEB_CONFIG
354 # $feature{'pickaxe'}{'default'} = [1];
355 # To have project specific config enable override in $GITWEB_CONFIG
356 # $feature{'pickaxe'}{'override'} = 1;
357 # and in project config gitweb.pickaxe = 0|1;
359 'sub' => sub { feature_bool
('pickaxe', @_) },
363 # Enable showing size of blobs in a 'tree' view, in a separate
364 # column, similar to what 'ls -l' does. This cost a bit of IO.
366 # To disable system wide have in $GITWEB_CONFIG
367 # $feature{'show-sizes'}{'default'} = [0];
368 # To have project specific config enable override in $GITWEB_CONFIG
369 # $feature{'show-sizes'}{'override'} = 1;
370 # and in project config gitweb.showsizes = 0|1;
372 'sub' => sub { feature_bool
('showsizes', @_) },
376 # Make gitweb use an alternative format of the URLs which can be
377 # more readable and natural-looking: project name is embedded
378 # directly in the path and the query string contains other
379 # auxiliary information. All gitweb installations recognize
380 # URL in either format; this configures in which formats gitweb
383 # To enable system wide have in $GITWEB_CONFIG
384 # $feature{'pathinfo'}{'default'} = [1];
385 # Project specific override is not supported.
387 # Note that you will need to change the default location of CSS,
388 # favicon, logo and possibly other files to an absolute URL. Also,
389 # if gitweb.cgi serves as your indexfile, you will need to force
390 # $my_uri to contain the script name in your $GITWEB_CONFIG.
395 # Make gitweb consider projects in project root subdirectories
396 # to be forks of existing projects. Given project $projname.git,
397 # projects matching $projname/*.git will not be shown in the main
398 # projects list, instead a '+' mark will be added to $projname
399 # there and a 'forks' view will be enabled for the project, listing
400 # all the forks. If project list is taken from a file, forks have
401 # to be listed after the main project.
403 # To enable system wide have in $GITWEB_CONFIG
404 # $feature{'forks'}{'default'} = [1];
405 # Project specific override is not supported.
410 # Insert custom links to the action bar of all project pages.
411 # This enables you mainly to link to third-party scripts integrating
412 # into gitweb; e.g. git-browser for graphical history representation
413 # or custom web-based repository administration interface.
415 # The 'default' value consists of a list of triplets in the form
416 # (label, link, position) where position is the label after which
417 # to insert the link and link is a format string where %n expands
418 # to the project name, %f to the project path within the filesystem,
419 # %h to the current hash (h gitweb parameter) and %b to the current
420 # hash base (hb gitweb parameter); %% expands to %.
422 # To enable system wide have in $GITWEB_CONFIG e.g.
423 # $feature{'actions'}{'default'} = [('graphiclog',
424 # '/git-browser/by-commit.html?r=%n', 'summary')];
425 # Project specific override is not supported.
430 # Allow gitweb scan project content tags of project repository,
431 # and display the popular Web 2.0-ish "tag cloud" near the projects
432 # list. Note that this is something COMPLETELY different from the
435 # gitweb by itself can show existing tags, but it does not handle
436 # tagging itself; you need to do it externally, outside gitweb.
437 # The format is described in git_get_project_ctags() subroutine.
438 # You may want to install the HTML::TagCloud Perl module to get
439 # a pretty tag cloud instead of just a list of tags.
441 # To enable system wide have in $GITWEB_CONFIG
442 # $feature{'ctags'}{'default'} = [1];
443 # Project specific override is not supported.
445 # In the future whether ctags editing is enabled might depend
446 # on the value, but using 1 should always mean no editing of ctags.
451 # The maximum number of patches in a patchset generated in patch
452 # view. Set this to 0 or undef to disable patch view, or to a
453 # negative number to remove any limit.
455 # To disable system wide have in $GITWEB_CONFIG
456 # $feature{'patches'}{'default'} = [0];
457 # To have project specific config enable override in $GITWEB_CONFIG
458 # $feature{'patches'}{'override'} = 1;
459 # and in project config gitweb.patches = 0|n;
460 # where n is the maximum number of patches allowed in a patchset.
462 'sub' => \
&feature_patches
,
466 # Avatar support. When this feature is enabled, views such as
467 # shortlog or commit will display an avatar associated with
468 # the email of the committer(s) and/or author(s).
470 # Currently available providers are gravatar and picon.
471 # If an unknown provider is specified, the feature is disabled.
473 # Gravatar depends on Digest::MD5.
474 # Picon currently relies on the indiana.edu database.
476 # To enable system wide have in $GITWEB_CONFIG
477 # $feature{'avatar'}{'default'} = ['<provider>'];
478 # where <provider> is either gravatar or picon.
479 # To have project specific config enable override in $GITWEB_CONFIG
480 # $feature{'avatar'}{'override'} = 1;
481 # and in project config gitweb.avatar = <provider>;
483 'sub' => \
&feature_avatar
,
487 # Enable displaying how much time and how many git commands
488 # it took to generate and display page. Disabled by default.
489 # Project specific override is not supported.
494 # Enable turning some links into links to actions which require
495 # JavaScript to run (like 'blame_incremental'). Not enabled by
496 # default. Project specific override is currently not supported.
497 'javascript-actions' => {
501 # Enable and configure ability to change common timezone for dates
502 # in gitweb output via JavaScript. Enabled by default.
503 # Project specific override is not supported.
504 'javascript-timezone' => {
507 'local', # default timezone: 'utc', 'local', or '(-|+)HHMM' format,
508 # or undef to turn off this feature
509 'gitweb_tz', # name of cookie where to store selected timezone
510 'datetime', # CSS class used to mark up dates for manipulation
513 # Syntax highlighting support. This is based on Daniel Svensson's
514 # and Sham Chukoury's work in gitweb-xmms2.git.
515 # It requires the 'highlight' program present in $PATH,
516 # and therefore is disabled by default.
518 # To enable system wide have in $GITWEB_CONFIG
519 # $feature{'highlight'}{'default'} = [1];
522 'sub' => sub { feature_bool
('highlight', @_) },
526 # Enable displaying of remote heads in the heads list
528 # To enable system wide have in $GITWEB_CONFIG
529 # $feature{'remote_heads'}{'default'} = [1];
530 # To have project specific config enable override in $GITWEB_CONFIG
531 # $feature{'remote_heads'}{'override'} = 1;
532 # and in project config gitweb.remote_heads = 0|1;
534 'sub' => sub { feature_bool
('remote_heads', @_) },
539 sub gitweb_get_feature
{
541 return unless exists $feature{$name};
542 my ($sub, $override, @defaults) = (
543 $feature{$name}{'sub'},
544 $feature{$name}{'override'},
545 @{$feature{$name}{'default'}});
546 # project specific override is possible only if we have project
547 our $git_dir; # global variable, declared later
548 if (!$override || !defined $git_dir) {
552 warn "feature $name is not overridable";
555 return $sub->(@defaults);
558 # A wrapper to check if a given feature is enabled.
559 # With this, you can say
561 # my $bool_feat = gitweb_check_feature('bool_feat');
562 # gitweb_check_feature('bool_feat') or somecode;
566 # my ($bool_feat) = gitweb_get_feature('bool_feat');
567 # (gitweb_get_feature('bool_feat'))[0] or somecode;
569 sub gitweb_check_feature
{
570 return (gitweb_get_feature
(@_))[0];
576 my ($val) = git_get_project_config
($key, '--bool');
580 } elsif ($val eq 'true') {
582 } elsif ($val eq 'false') {
587 sub feature_snapshot
{
590 my ($val) = git_get_project_config
('snapshot');
593 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
599 sub feature_patches
{
600 my @val = (git_get_project_config
('patches', '--int'));
610 my @val = (git_get_project_config
('avatar'));
612 return @val ? @val : @_;
615 # checking HEAD file with -e is fragile if the repository was
616 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
618 sub check_head_link
{
620 my $headfile = "$dir/HEAD";
621 return ((-e
$headfile) ||
622 (-l
$headfile && readlink($headfile) =~ /^refs\/heads\
//));
625 sub check_export_ok
{
627 return (check_head_link
($dir) &&
628 (!$export_ok || -e
"$dir/$export_ok") &&
629 (!$export_auth_hook || $export_auth_hook->($dir)));
632 # process alternate names for backward compatibility
633 # filter out unsupported (unknown) snapshot formats
634 sub filter_snapshot_fmts
{
638 exists $known_snapshot_format_aliases{$_} ?
639 $known_snapshot_format_aliases{$_} : $_} @fmts;
641 exists $known_snapshot_formats{$_} &&
642 !$known_snapshot_formats{$_}{'disabled'}} @fmts;
645 # If it is set to code reference, it is code that it is to be run once per
646 # request, allowing updating configurations that change with each request,
647 # while running other code in config file only once.
649 # Otherwise, if it is false then gitweb would process config file only once;
650 # if it is true then gitweb config would be run for each request.
651 our $per_request_config = 1;
653 # read and parse gitweb config file given by its parameter.
654 # returns true on success, false on recoverable error, allowing
655 # to chain this subroutine, using first file that exists.
656 # dies on errors during parsing config file, as it is unrecoverable.
657 sub read_config_file
{
658 my $filename = shift;
659 return unless defined $filename;
660 # die if there are errors parsing config file
669 our ($GITWEB_CONFIG, $GITWEB_CONFIG_SYSTEM, $GITWEB_CONFIG_COMMON);
670 sub evaluate_gitweb_config
{
671 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
672 our $GITWEB_CONFIG_SYSTEM = $ENV{'GITWEB_CONFIG_SYSTEM'} || "++GITWEB_CONFIG_SYSTEM++";
673 our $GITWEB_CONFIG_COMMON = $ENV{'GITWEB_CONFIG_COMMON'} || "++GITWEB_CONFIG_COMMON++";
675 # Protect agains duplications of file names, to not read config twice.
676 # Only one of $GITWEB_CONFIG and $GITWEB_CONFIG_SYSTEM is used, so
677 # there possibility of duplication of filename there doesn't matter.
678 $GITWEB_CONFIG = "" if ($GITWEB_CONFIG eq $GITWEB_CONFIG_COMMON);
679 $GITWEB_CONFIG_SYSTEM = "" if ($GITWEB_CONFIG_SYSTEM eq $GITWEB_CONFIG_COMMON);
681 # Common system-wide settings for convenience.
682 # Those settings can be ovverriden by GITWEB_CONFIG or GITWEB_CONFIG_SYSTEM.
683 read_config_file
($GITWEB_CONFIG_COMMON);
685 # Use first config file that exists. This means use the per-instance
686 # GITWEB_CONFIG if exists, otherwise use GITWEB_SYSTEM_CONFIG.
687 read_config_file
($GITWEB_CONFIG) and return;
688 read_config_file
($GITWEB_CONFIG_SYSTEM);
691 # Get loadavg of system, to compare against $maxload.
692 # Currently it requires '/proc/loadavg' present to get loadavg;
693 # if it is not present it returns 0, which means no load checking.
695 if( -e
'/proc/loadavg' ){
696 open my $fd, '<', '/proc/loadavg'
698 my @load = split(/\s+/, scalar <$fd>);
701 # The first three columns measure CPU and IO utilization of the last one,
702 # five, and 10 minute periods. The fourth column shows the number of
703 # currently running processes and the total number of processes in the m/n
704 # format. The last column displays the last process ID used.
705 return $load[0] || 0;
707 # additional checks for load average should go here for things that don't export
713 # version of the core git binary
715 sub evaluate_git_version
{
716 our $git_version = qx("$GIT" --version) =~ m/git version (.*)$/ ? $1 : "unknown";
717 $number_of_git_cmds++;
721 if (defined $maxload && get_loadavg
() > $maxload) {
722 die_error
(503, "The load average on the server is too high");
726 # ======================================================================
727 # input validation and dispatch
729 # input parameters can be collected from a variety of sources (presently, CGI
730 # and PATH_INFO), so we define an %input_params hash that collects them all
731 # together during validation: this allows subsequent uses (e.g. href()) to be
732 # agnostic of the parameter origin
734 our %input_params = ();
736 # input parameters are stored with the long parameter name as key. This will
737 # also be used in the href subroutine to convert parameters to their CGI
738 # equivalent, and since the href() usage is the most frequent one, we store
739 # the name -> CGI key mapping here, instead of the reverse.
741 # XXX: Warning: If you touch this, check the search form for updating,
744 our @cgi_param_mapping = (
752 hash_parent_base
=> "hpb",
757 snapshot_format
=> "sf",
758 extra_options
=> "opt",
759 search_use_regexp
=> "sr",
761 # this must be last entry (for manipulation from JavaScript)
764 our %cgi_param_mapping = @cgi_param_mapping;
766 # we will also need to know the possible actions, for validation
768 "blame" => \
&git_blame
,
769 "blame_incremental" => \
&git_blame_incremental
,
770 "blame_data" => \
&git_blame_data
,
771 "blobdiff" => \
&git_blobdiff
,
772 "blobdiff_plain" => \
&git_blobdiff_plain
,
773 "blob" => \
&git_blob
,
774 "blob_plain" => \
&git_blob_plain
,
775 "commitdiff" => \
&git_commitdiff
,
776 "commitdiff_plain" => \
&git_commitdiff_plain
,
777 "commit" => \
&git_commit
,
778 "forks" => \
&git_forks
,
779 "heads" => \
&git_heads
,
780 "history" => \
&git_history
,
782 "patch" => \
&git_patch
,
783 "patches" => \
&git_patches
,
784 "remotes" => \
&git_remotes
,
786 "atom" => \
&git_atom
,
787 "search" => \
&git_search
,
788 "search_help" => \
&git_search_help
,
789 "shortlog" => \
&git_shortlog
,
790 "summary" => \
&git_summary
,
792 "tags" => \
&git_tags
,
793 "tree" => \
&git_tree
,
794 "snapshot" => \
&git_snapshot
,
795 "object" => \
&git_object
,
796 # those below don't need $project
797 "opml" => \
&git_opml
,
798 "project_list" => \
&git_project_list
,
799 "project_index" => \
&git_project_index
,
802 # finally, we have the hash of allowed extra_options for the commands that
804 our %allowed_options = (
805 "--no-merges" => [ qw(rss atom log shortlog history) ],
808 # fill %input_params with the CGI parameters. All values except for 'opt'
809 # should be single values, but opt can be an array. We should probably
810 # build an array of parameters that can be multi-valued, but since for the time
811 # being it's only this one, we just single it out
812 sub evaluate_query_params
{
815 while (my ($name, $symbol) = each %cgi_param_mapping) {
816 if ($symbol eq 'opt') {
817 $input_params{$name} = [ $cgi->param($symbol) ];
819 $input_params{$name} = $cgi->param($symbol);
824 # now read PATH_INFO and update the parameter list for missing parameters
825 sub evaluate_path_info
{
826 return if defined $input_params{'project'};
827 return if !$path_info;
828 $path_info =~ s
,^/+,,;
829 return if !$path_info;
831 # find which part of PATH_INFO is project
832 my $project = $path_info;
834 while ($project && !check_head_link
("$projectroot/$project")) {
835 $project =~ s
,/*[^/]*$,,;
837 return unless $project;
838 $input_params{'project'} = $project;
840 # do not change any parameters if an action is given using the query string
841 return if $input_params{'action'};
842 $path_info =~ s
,^\Q
$project\E
/*,,;
844 # next, check if we have an action
845 my $action = $path_info;
847 if (exists $actions{$action}) {
848 $path_info =~ s
,^$action/*,,;
849 $input_params{'action'} = $action;
852 # list of actions that want hash_base instead of hash, but can have no
853 # pathname (f) parameter
859 # we want to catch, among others
860 # [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name]
861 my ($parentrefname, $parentpathname, $refname, $pathname) =
862 ($path_info =~ /^(?:(.+?)(?::(.+))?\.\.)?([^:]+?)?(?::(.+))?$/);
864 # first, analyze the 'current' part
865 if (defined $pathname) {
866 # we got "branch:filename" or "branch:dir/"
867 # we could use git_get_type(branch:pathname), but:
868 # - it needs $git_dir
869 # - it does a git() call
870 # - the convention of terminating directories with a slash
871 # makes it superfluous
872 # - embedding the action in the PATH_INFO would make it even
874 $pathname =~ s
,^/+,,;
875 if (!$pathname || substr($pathname, -1) eq "/") {
876 $input_params{'action'} ||= "tree";
879 # the default action depends on whether we had parent info
881 if ($parentrefname) {
882 $input_params{'action'} ||= "blobdiff_plain";
884 $input_params{'action'} ||= "blob_plain";
887 $input_params{'hash_base'} ||= $refname;
888 $input_params{'file_name'} ||= $pathname;
889 } elsif (defined $refname) {
890 # we got "branch". In this case we have to choose if we have to
891 # set hash or hash_base.
893 # Most of the actions without a pathname only want hash to be
894 # set, except for the ones specified in @wants_base that want
895 # hash_base instead. It should also be noted that hand-crafted
896 # links having 'history' as an action and no pathname or hash
897 # set will fail, but that happens regardless of PATH_INFO.
898 if (defined $parentrefname) {
899 # if there is parent let the default be 'shortlog' action
900 # (for http://git.example.com/repo.git/A..B links); if there
901 # is no parent, dispatch will detect type of object and set
902 # action appropriately if required (if action is not set)
903 $input_params{'action'} ||= "shortlog";
905 if ($input_params{'action'} &&
906 grep { $_ eq $input_params{'action'} } @wants_base) {
907 $input_params{'hash_base'} ||= $refname;
909 $input_params{'hash'} ||= $refname;
913 # next, handle the 'parent' part, if present
914 if (defined $parentrefname) {
915 # a missing pathspec defaults to the 'current' filename, allowing e.g.
916 # someproject/blobdiff/oldrev..newrev:/filename
917 if ($parentpathname) {
918 $parentpathname =~ s
,^/+,,;
919 $parentpathname =~ s
,/$,,;
920 $input_params{'file_parent'} ||= $parentpathname;
922 $input_params{'file_parent'} ||= $input_params{'file_name'};
924 # we assume that hash_parent_base is wanted if a path was specified,
925 # or if the action wants hash_base instead of hash
926 if (defined $input_params{'file_parent'} ||
927 grep { $_ eq $input_params{'action'} } @wants_base) {
928 $input_params{'hash_parent_base'} ||= $parentrefname;
930 $input_params{'hash_parent'} ||= $parentrefname;
934 # for the snapshot action, we allow URLs in the form
935 # $project/snapshot/$hash.ext
936 # where .ext determines the snapshot and gets removed from the
937 # passed $refname to provide the $hash.
939 # To be able to tell that $refname includes the format extension, we
940 # require the following two conditions to be satisfied:
941 # - the hash input parameter MUST have been set from the $refname part
942 # of the URL (i.e. they must be equal)
943 # - the snapshot format MUST NOT have been defined already (e.g. from
945 # It's also useless to try any matching unless $refname has a dot,
946 # so we check for that too
947 if (defined $input_params{'action'} &&
948 $input_params{'action'} eq 'snapshot' &&
949 defined $refname && index($refname, '.') != -1 &&
950 $refname eq $input_params{'hash'} &&
951 !defined $input_params{'snapshot_format'}) {
952 # We loop over the known snapshot formats, checking for
953 # extensions. Allowed extensions are both the defined suffix
954 # (which includes the initial dot already) and the snapshot
955 # format key itself, with a prepended dot
956 while (my ($fmt, $opt) = each %known_snapshot_formats) {
958 unless ($hash =~ s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) {
962 # a valid suffix was found, so set the snapshot format
963 # and reset the hash parameter
964 $input_params{'snapshot_format'} = $fmt;
965 $input_params{'hash'} = $hash;
966 # we also set the format suffix to the one requested
967 # in the URL: this way a request for e.g. .tgz returns
968 # a .tgz instead of a .tar.gz
969 $known_snapshot_formats{$fmt}{'suffix'} = $sfx;
975 our ($action, $project, $file_name, $file_parent, $hash, $hash_parent, $hash_base,
976 $hash_parent_base, @extra_options, $page, $searchtype, $search_use_regexp,
977 $searchtext, $search_regexp);
978 sub evaluate_and_validate_params
{
979 our $action = $input_params{'action'};
980 if (defined $action) {
981 if (!validate_action
($action)) {
982 die_error
(400, "Invalid action parameter");
986 # parameters which are pathnames
987 our $project = $input_params{'project'};
988 if (defined $project) {
989 if (!validate_project
($project)) {
991 die_error
(404, "No such project");
995 our $file_name = $input_params{'file_name'};
996 if (defined $file_name) {
997 if (!validate_pathname
($file_name)) {
998 die_error
(400, "Invalid file parameter");
1002 our $file_parent = $input_params{'file_parent'};
1003 if (defined $file_parent) {
1004 if (!validate_pathname
($file_parent)) {
1005 die_error
(400, "Invalid file parent parameter");
1009 # parameters which are refnames
1010 our $hash = $input_params{'hash'};
1011 if (defined $hash) {
1012 if (!validate_refname
($hash)) {
1013 die_error
(400, "Invalid hash parameter");
1017 our $hash_parent = $input_params{'hash_parent'};
1018 if (defined $hash_parent) {
1019 if (!validate_refname
($hash_parent)) {
1020 die_error
(400, "Invalid hash parent parameter");
1024 our $hash_base = $input_params{'hash_base'};
1025 if (defined $hash_base) {
1026 if (!validate_refname
($hash_base)) {
1027 die_error
(400, "Invalid hash base parameter");
1031 our @extra_options = @{$input_params{'extra_options'}};
1032 # @extra_options is always defined, since it can only be (currently) set from
1033 # CGI, and $cgi->param() returns the empty array in array context if the param
1035 foreach my $opt (@extra_options) {
1036 if (not exists $allowed_options{$opt}) {
1037 die_error
(400, "Invalid option parameter");
1039 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
1040 die_error
(400, "Invalid option parameter for this action");
1044 our $hash_parent_base = $input_params{'hash_parent_base'};
1045 if (defined $hash_parent_base) {
1046 if (!validate_refname
($hash_parent_base)) {
1047 die_error
(400, "Invalid hash parent base parameter");
1052 our $page = $input_params{'page'};
1053 if (defined $page) {
1054 if ($page =~ m/[^0-9]/) {
1055 die_error
(400, "Invalid page parameter");
1059 our $searchtype = $input_params{'searchtype'};
1060 if (defined $searchtype) {
1061 if ($searchtype =~ m/[^a-z]/) {
1062 die_error
(400, "Invalid searchtype parameter");
1066 our $search_use_regexp = $input_params{'search_use_regexp'};
1068 our $searchtext = $input_params{'searchtext'};
1070 if (defined $searchtext) {
1071 if (length($searchtext) < 2) {
1072 die_error
(403, "At least two characters are required for search parameter");
1074 $search_regexp = $search_use_regexp ? $searchtext : quotemeta $searchtext;
1078 # path to the current git repository
1080 sub evaluate_git_dir
{
1081 our $git_dir = "$projectroot/$project" if $project;
1084 our (@snapshot_fmts, $git_avatar);
1085 sub configure_gitweb_features
{
1086 # list of supported snapshot formats
1087 our @snapshot_fmts = gitweb_get_feature
('snapshot');
1088 @snapshot_fmts = filter_snapshot_fmts
(@snapshot_fmts);
1090 # check that the avatar feature is set to a known provider name,
1091 # and for each provider check if the dependencies are satisfied.
1092 # if the provider name is invalid or the dependencies are not met,
1093 # reset $git_avatar to the empty string.
1094 our ($git_avatar) = gitweb_get_feature
('avatar');
1095 if ($git_avatar eq 'gravatar') {
1096 $git_avatar = '' unless (eval { require Digest
::MD5
; 1; });
1097 } elsif ($git_avatar eq 'picon') {
1104 # custom error handler: 'die <message>' is Internal Server Error
1105 sub handle_errors_html
{
1106 my $msg = shift; # it is already HTML escaped
1108 # to avoid infinite loop where error occurs in die_error,
1109 # change handler to default handler, disabling handle_errors_html
1110 set_message
("Error occured when inside die_error:\n$msg");
1112 # you cannot jump out of die_error when called as error handler;
1113 # the subroutine set via CGI::Carp::set_message is called _after_
1114 # HTTP headers are already written, so it cannot write them itself
1115 die_error
(undef, undef, $msg, -error_handler
=> 1, -no_http_header
=> 1);
1117 set_message
(\
&handle_errors_html
);
1121 if (!defined $action) {
1122 if (defined $hash) {
1123 $action = git_get_type
($hash);
1124 } elsif (defined $hash_base && defined $file_name) {
1125 $action = git_get_type
("$hash_base:$file_name");
1126 } elsif (defined $project) {
1127 $action = 'summary';
1129 $action = 'project_list';
1132 if (!defined($actions{$action})) {
1133 die_error
(400, "Unknown action");
1135 if ($action !~ m/^(?:opml|project_list|project_index)$/ &&
1137 die_error
(400, "Project needed");
1139 $actions{$action}->();
1143 our $t0 = [ gettimeofday
() ]
1145 our $number_of_git_cmds = 0;
1148 our $first_request = 1;
1153 if ($first_request) {
1154 evaluate_gitweb_config
();
1155 evaluate_git_version
();
1157 if ($per_request_config) {
1158 if (ref($per_request_config) eq 'CODE') {
1159 $per_request_config->();
1160 } elsif (!$first_request) {
1161 evaluate_gitweb_config
();
1166 # $projectroot and $projects_list might be set in gitweb config file
1167 $projects_list ||= $projectroot;
1169 evaluate_query_params
();
1170 evaluate_path_info
();
1171 evaluate_and_validate_params
();
1174 configure_gitweb_features
();
1179 our $is_last_request = sub { 1 };
1180 our ($pre_dispatch_hook, $post_dispatch_hook, $pre_listen_hook);
1183 sub configure_as_fcgi
{
1185 our $CGI = 'CGI::Fast';
1187 my $request_number = 0;
1188 # let each child service 100 requests
1189 our $is_last_request = sub { ++$request_number > 100 };
1192 my $script_name = $ENV{'SCRIPT_NAME'} || $ENV{'SCRIPT_FILENAME'} || __FILE__
;
1194 if $script_name =~ /\.fcgi$/;
1196 return unless (@ARGV);
1198 require Getopt
::Long
;
1199 Getopt
::Long
::GetOptions
(
1200 'fastcgi|fcgi|f' => \
&configure_as_fcgi
,
1201 'nproc|n=i' => sub {
1202 my ($arg, $val) = @_;
1203 return unless eval { require FCGI
::ProcManager
; 1; };
1204 my $proc_manager = FCGI
::ProcManager-
>new({
1205 n_processes
=> $val,
1207 our $pre_listen_hook = sub { $proc_manager->pm_manage() };
1208 our $pre_dispatch_hook = sub { $proc_manager->pm_pre_dispatch() };
1209 our $post_dispatch_hook = sub { $proc_manager->pm_post_dispatch() };
1218 $pre_listen_hook->()
1219 if $pre_listen_hook;
1222 while ($cgi = $CGI->new()) {
1223 $pre_dispatch_hook->()
1224 if $pre_dispatch_hook;
1228 $post_dispatch_hook->()
1229 if $post_dispatch_hook;
1232 last REQUEST
if ($is_last_request->());
1241 if (defined caller) {
1242 # wrapped in a subroutine processing requests,
1243 # e.g. mod_perl with ModPerl::Registry, or PSGI with Plack::App::WrapCGI
1246 # pure CGI script, serving single request
1250 ## ======================================================================
1253 # possible values of extra options
1254 # -full => 0|1 - use absolute/full URL ($my_uri/$my_url as base)
1255 # -replay => 1 - start from a current view (replay with modifications)
1256 # -path_info => 0|1 - don't use/use path_info URL (if possible)
1257 # -anchor => ANCHOR - add #ANCHOR to end of URL, implies -replay if used alone
1260 # default is to use -absolute url() i.e. $my_uri
1261 my $href = $params{-full
} ? $my_url : $my_uri;
1263 # implicit -replay, must be first of implicit params
1264 $params{-replay
} = 1 if (keys %params == 1 && $params{-anchor
});
1266 $params{'project'} = $project unless exists $params{'project'};
1268 if ($params{-replay
}) {
1269 while (my ($name, $symbol) = each %cgi_param_mapping) {
1270 if (!exists $params{$name}) {
1271 $params{$name} = $input_params{$name};
1276 my $use_pathinfo = gitweb_check_feature
('pathinfo');
1277 if (defined $params{'project'} &&
1278 (exists $params{-path_info
} ? $params{-path_info
} : $use_pathinfo)) {
1279 # try to put as many parameters as possible in PATH_INFO:
1282 # - hash_parent or hash_parent_base:/file_parent
1283 # - hash or hash_base:/filename
1284 # - the snapshot_format as an appropriate suffix
1286 # When the script is the root DirectoryIndex for the domain,
1287 # $href here would be something like http://gitweb.example.com/
1288 # Thus, we strip any trailing / from $href, to spare us double
1289 # slashes in the final URL
1292 # Then add the project name, if present
1293 $href .= "/".esc_path_info
($params{'project'});
1294 delete $params{'project'};
1296 # since we destructively absorb parameters, we keep this
1297 # boolean that remembers if we're handling a snapshot
1298 my $is_snapshot = $params{'action'} eq 'snapshot';
1300 # Summary just uses the project path URL, any other action is
1302 if (defined $params{'action'}) {
1303 $href .= "/".esc_path_info
($params{'action'})
1304 unless $params{'action'} eq 'summary';
1305 delete $params{'action'};
1308 # Next, we put hash_parent_base:/file_parent..hash_base:/file_name,
1309 # stripping nonexistent or useless pieces
1310 $href .= "/" if ($params{'hash_base'} || $params{'hash_parent_base'}
1311 || $params{'hash_parent'} || $params{'hash'});
1312 if (defined $params{'hash_base'}) {
1313 if (defined $params{'hash_parent_base'}) {
1314 $href .= esc_path_info
($params{'hash_parent_base'});
1315 # skip the file_parent if it's the same as the file_name
1316 if (defined $params{'file_parent'}) {
1317 if (defined $params{'file_name'} && $params{'file_parent'} eq $params{'file_name'}) {
1318 delete $params{'file_parent'};
1319 } elsif ($params{'file_parent'} !~ /\.\./) {
1320 $href .= ":/".esc_path_info
($params{'file_parent'});
1321 delete $params{'file_parent'};
1325 delete $params{'hash_parent'};
1326 delete $params{'hash_parent_base'};
1327 } elsif (defined $params{'hash_parent'}) {
1328 $href .= esc_path_info
($params{'hash_parent'}). "..";
1329 delete $params{'hash_parent'};
1332 $href .= esc_path_info
($params{'hash_base'});
1333 if (defined $params{'file_name'} && $params{'file_name'} !~ /\.\./) {
1334 $href .= ":/".esc_path_info
($params{'file_name'});
1335 delete $params{'file_name'};
1337 delete $params{'hash'};
1338 delete $params{'hash_base'};
1339 } elsif (defined $params{'hash'}) {
1340 $href .= esc_path_info
($params{'hash'});
1341 delete $params{'hash'};
1344 # If the action was a snapshot, we can absorb the
1345 # snapshot_format parameter too
1347 my $fmt = $params{'snapshot_format'};
1348 # snapshot_format should always be defined when href()
1349 # is called, but just in case some code forgets, we
1350 # fall back to the default
1351 $fmt ||= $snapshot_fmts[0];
1352 $href .= $known_snapshot_formats{$fmt}{'suffix'};
1353 delete $params{'snapshot_format'};
1357 # now encode the parameters explicitly
1359 for (my $i = 0; $i < @cgi_param_mapping; $i += 2) {
1360 my ($name, $symbol) = ($cgi_param_mapping[$i], $cgi_param_mapping[$i+1]);
1361 if (defined $params{$name}) {
1362 if (ref($params{$name}) eq "ARRAY") {
1363 foreach my $par (@{$params{$name}}) {
1364 push @result, $symbol . "=" . esc_param
($par);
1367 push @result, $symbol . "=" . esc_param
($params{$name});
1371 $href .= "?" . join(';', @result) if scalar @result;
1373 # final transformation: trailing spaces must be escaped (URI-encoded)
1374 $href =~ s/(\s+)$/CGI::escape($1)/e;
1376 if ($params{-anchor
}) {
1377 $href .= "#".esc_param
($params{-anchor
});
1384 ## ======================================================================
1385 ## validation, quoting/unquoting and escaping
1387 sub validate_action
{
1388 my $input = shift || return undef;
1389 return undef unless exists $actions{$input};
1393 sub validate_project
{
1394 my $input = shift || return undef;
1395 if (!validate_pathname
($input) ||
1396 !(-d
"$projectroot/$input") ||
1397 !check_export_ok
("$projectroot/$input") ||
1398 ($strict_export && !project_in_list
($input))) {
1405 sub validate_pathname
{
1406 my $input = shift || return undef;
1408 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
1409 # at the beginning, at the end, and between slashes.
1410 # also this catches doubled slashes
1411 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
1414 # no null characters
1415 if ($input =~ m!\0!) {
1421 sub validate_refname
{
1422 my $input = shift || return undef;
1424 # textual hashes are O.K.
1425 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
1428 # it must be correct pathname
1429 $input = validate_pathname
($input)
1431 # restrictions on ref name according to git-check-ref-format
1432 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
1438 # decode sequences of octets in utf8 into Perl's internal form,
1439 # which is utf-8 with utf8 flag set if needed. gitweb writes out
1440 # in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
1443 return undef unless defined $str;
1444 if (utf8
::valid
($str)) {
1448 return decode
($fallback_encoding, $str, Encode
::FB_DEFAULT
);
1452 # quote unsafe chars, but keep the slash, even when it's not
1453 # correct, but quoted slashes look too horrible in bookmarks
1456 return undef unless defined $str;
1457 $str =~ s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI
::escape
($1)/eg
;
1462 # the quoting rules for path_info fragment are slightly different
1465 return undef unless defined $str;
1467 # path_info doesn't treat '+' as space (specially), but '?' must be escaped
1468 $str =~ s/([^A-Za-z0-9\-_.~();\/;:@&= +]+)/CGI
::escape
($1)/eg
;
1473 # quote unsafe chars in whole URL, so some characters cannot be quoted
1476 return undef unless defined $str;
1477 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&= ]+)/CGI
::escape
($1)/eg
;
1482 # quote unsafe characters in HTML attributes
1485 # for XHTML conformance escaping '"' to '"' is not enough
1486 return esc_html
(@_);
1489 # replace invalid utf8 character with SUBSTITUTION sequence
1494 return undef unless defined $str;
1496 $str = to_utf8
($str);
1497 $str = $cgi->escapeHTML($str);
1498 if ($opts{'-nbsp'}) {
1499 $str =~ s/ / /g;
1501 $str =~ s
|([[:cntrl
:]])|(($1 ne "\t") ? quot_cec
($1) : $1)|eg
;
1505 # quote control characters and escape filename to HTML
1510 return undef unless defined $str;
1512 $str = to_utf8
($str);
1513 $str = $cgi->escapeHTML($str);
1514 if ($opts{'-nbsp'}) {
1515 $str =~ s/ / /g;
1517 $str =~ s
|([[:cntrl
:]])|quot_cec
($1)|eg
;
1521 # Make control characters "printable", using character escape codes (CEC)
1525 my %es = ( # character escape codes, aka escape sequences
1526 "\t" => '\t', # tab (HT)
1527 "\n" => '\n', # line feed (LF)
1528 "\r" => '\r', # carrige return (CR)
1529 "\f" => '\f', # form feed (FF)
1530 "\b" => '\b', # backspace (BS)
1531 "\a" => '\a', # alarm (bell) (BEL)
1532 "\e" => '\e', # escape (ESC)
1533 "\013" => '\v', # vertical tab (VT)
1534 "\000" => '\0', # nul character (NUL)
1536 my $chr = ( (exists $es{$cntrl})
1538 : sprintf('\%2x', ord($cntrl)) );
1539 if ($opts{-nohtml
}) {
1542 return "<span class=\"cntrl\">$chr</span>";
1546 # Alternatively use unicode control pictures codepoints,
1547 # Unicode "printable representation" (PR)
1552 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
1553 if ($opts{-nohtml
}) {
1556 return "<span class=\"cntrl\">$chr</span>";
1560 # git may return quoted and escaped filenames
1566 my %es = ( # character escape codes, aka escape sequences
1567 't' => "\t", # tab (HT, TAB)
1568 'n' => "\n", # newline (NL)
1569 'r' => "\r", # return (CR)
1570 'f' => "\f", # form feed (FF)
1571 'b' => "\b", # backspace (BS)
1572 'a' => "\a", # alarm (bell) (BEL)
1573 'e' => "\e", # escape (ESC)
1574 'v' => "\013", # vertical tab (VT)
1577 if ($seq =~ m/^[0-7]{1,3}$/) {
1578 # octal char sequence
1579 return chr(oct($seq));
1580 } elsif (exists $es{$seq}) {
1581 # C escape sequence, aka character escape code
1584 # quoted ordinary character
1588 if ($str =~ m/^"(.*)"$/) {
1591 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
1596 # escape tabs (convert tabs to spaces)
1600 while ((my $pos = index($line, "\t")) != -1) {
1601 if (my $count = (8 - ($pos % 8))) {
1602 my $spaces = ' ' x
$count;
1603 $line =~ s/\t/$spaces/;
1610 sub project_in_list
{
1611 my $project = shift;
1612 my @list = git_get_projects_list
();
1613 return @list && scalar(grep { $_->{'path'} eq $project } @list);
1616 ## ----------------------------------------------------------------------
1617 ## HTML aware string manipulation
1619 # Try to chop given string on a word boundary between position
1620 # $len and $len+$add_len. If there is no word boundary there,
1621 # chop at $len+$add_len. Do not chop if chopped part plus ellipsis
1622 # (marking chopped part) would be longer than given string.
1626 my $add_len = shift || 10;
1627 my $where = shift || 'right'; # 'left' | 'center' | 'right'
1629 # Make sure perl knows it is utf8 encoded so we don't
1630 # cut in the middle of a utf8 multibyte char.
1631 $str = to_utf8
($str);
1633 # allow only $len chars, but don't cut a word if it would fit in $add_len
1634 # if it doesn't fit, cut it if it's still longer than the dots we would add
1635 # remove chopped character entities entirely
1637 # when chopping in the middle, distribute $len into left and right part
1638 # return early if chopping wouldn't make string shorter
1639 if ($where eq 'center') {
1640 return $str if ($len + 5 >= length($str)); # filler is length 5
1643 return $str if ($len + 4 >= length($str)); # filler is length 4
1646 # regexps: ending and beginning with word part up to $add_len
1647 my $endre = qr/.{$len}\w{0,$add_len}/;
1648 my $begre = qr/\w{0,$add_len}.{$len}/;
1650 if ($where eq 'left') {
1651 $str =~ m/^(.*?)($begre)$/;
1652 my ($lead, $body) = ($1, $2);
1653 if (length($lead) > 4) {
1656 return "$lead$body";
1658 } elsif ($where eq 'center') {
1659 $str =~ m/^($endre)(.*)$/;
1660 my ($left, $str) = ($1, $2);
1661 $str =~ m/^(.*?)($begre)$/;
1662 my ($mid, $right) = ($1, $2);
1663 if (length($mid) > 5) {
1666 return "$left$mid$right";
1669 $str =~ m/^($endre)(.*)$/;
1672 if (length($tail) > 4) {
1675 return "$body$tail";
1679 # takes the same arguments as chop_str, but also wraps a <span> around the
1680 # result with a title attribute if it does get chopped. Additionally, the
1681 # string is HTML-escaped.
1682 sub chop_and_escape_str
{
1685 my $chopped = chop_str
(@_);
1686 if ($chopped eq $str) {
1687 return esc_html
($chopped);
1689 $str =~ s/[[:cntrl:]]/?/g;
1690 return $cgi->span({-title
=>$str}, esc_html
($chopped));
1694 ## ----------------------------------------------------------------------
1695 ## functions returning short strings
1697 # CSS class for given age value (in seconds)
1701 if (!defined $age) {
1703 } elsif ($age < 60*60*2) {
1705 } elsif ($age < 60*60*24*2) {
1712 # convert age in seconds to "nn units ago" string
1717 if ($age > 60*60*24*365*2) {
1718 $age_str = (int $age/60/60/24/365);
1719 $age_str .= " years ago";
1720 } elsif ($age > 60*60*24*(365/12)*2) {
1721 $age_str = int $age/60/60/24/(365/12);
1722 $age_str .= " months ago";
1723 } elsif ($age > 60*60*24*7*2) {
1724 $age_str = int $age/60/60/24/7;
1725 $age_str .= " weeks ago";
1726 } elsif ($age > 60*60*24*2) {
1727 $age_str = int $age/60/60/24;
1728 $age_str .= " days ago";
1729 } elsif ($age > 60*60*2) {
1730 $age_str = int $age/60/60;
1731 $age_str .= " hours ago";
1732 } elsif ($age > 60*2) {
1733 $age_str = int $age/60;
1734 $age_str .= " min ago";
1735 } elsif ($age > 2) {
1736 $age_str = int $age;
1737 $age_str .= " sec ago";
1739 $age_str .= " right now";
1745 S_IFINVALID
=> 0030000,
1746 S_IFGITLINK
=> 0160000,
1749 # submodule/subproject, a commit object reference
1753 return (($mode & S_IFMT
) == S_IFGITLINK
)
1756 # convert file mode in octal to symbolic file mode string
1758 my $mode = oct shift;
1760 if (S_ISGITLINK
($mode)) {
1761 return 'm---------';
1762 } elsif (S_ISDIR
($mode & S_IFMT
)) {
1763 return 'drwxr-xr-x';
1764 } elsif (S_ISLNK
($mode)) {
1765 return 'lrwxrwxrwx';
1766 } elsif (S_ISREG
($mode)) {
1767 # git cares only about the executable bit
1768 if ($mode & S_IXUSR
) {
1769 return '-rwxr-xr-x';
1771 return '-rw-r--r--';
1774 return '----------';
1778 # convert file mode in octal to file type string
1782 if ($mode !~ m/^[0-7]+$/) {
1788 if (S_ISGITLINK
($mode)) {
1790 } elsif (S_ISDIR
($mode & S_IFMT
)) {
1792 } elsif (S_ISLNK
($mode)) {
1794 } elsif (S_ISREG
($mode)) {
1801 # convert file mode in octal to file type description string
1802 sub file_type_long
{
1805 if ($mode !~ m/^[0-7]+$/) {
1811 if (S_ISGITLINK
($mode)) {
1813 } elsif (S_ISDIR
($mode & S_IFMT
)) {
1815 } elsif (S_ISLNK
($mode)) {
1817 } elsif (S_ISREG
($mode)) {
1818 if ($mode & S_IXUSR
) {
1819 return "executable";
1829 ## ----------------------------------------------------------------------
1830 ## functions returning short HTML fragments, or transforming HTML fragments
1831 ## which don't belong to other sections
1833 # format line of commit message.
1834 sub format_log_line_html
{
1837 $line = esc_html
($line, -nbsp
=>1);
1838 $line =~ s
{\b([0-9a-fA-F
]{8,40})\b}{
1839 $cgi->a({-href
=> href
(action
=>"object", hash
=>$1),
1840 -class => "text"}, $1);
1846 # format marker of refs pointing to given object
1848 # the destination action is chosen based on object type and current context:
1849 # - for annotated tags, we choose the tag view unless it's the current view
1850 # already, in which case we go to shortlog view
1851 # - for other refs, we keep the current view if we're in history, shortlog or
1852 # log view, and select shortlog otherwise
1853 sub format_ref_marker
{
1854 my ($refs, $id) = @_;
1857 if (defined $refs->{$id}) {
1858 foreach my $ref (@{$refs->{$id}}) {
1859 # this code exploits the fact that non-lightweight tags are the
1860 # only indirect objects, and that they are the only objects for which
1861 # we want to use tag instead of shortlog as action
1862 my ($type, $name) = qw();
1863 my $indirect = ($ref =~ s/\^\{\}$//);
1864 # e.g. tags/v2.6.11 or heads/next
1865 if ($ref =~ m!^(.*?)s?/(.*)$!) {
1874 $class .= " indirect" if $indirect;
1876 my $dest_action = "shortlog";
1879 $dest_action = "tag" unless $action eq "tag";
1880 } elsif ($action =~ /^(history|(short)?log)$/) {
1881 $dest_action = $action;
1885 $dest .= "refs/" unless $ref =~ m
!^refs
/!;
1888 my $link = $cgi->a({
1890 action
=>$dest_action,
1894 $markers .= " <span class=\"".esc_attr
($class)."\" title=\"".esc_attr
($ref)."\">" .
1900 return ' <span class="refs">'. $markers . '</span>';
1906 # format, perhaps shortened and with markers, title line
1907 sub format_subject_html
{
1908 my ($long, $short, $href, $extra) = @_;
1909 $extra = '' unless defined($extra);
1911 if (length($short) < length($long)) {
1912 $long =~ s/[[:cntrl:]]/?/g;
1913 return $cgi->a({-href
=> $href, -class => "list subject",
1914 -title
=> to_utf8
($long)},
1915 esc_html
($short)) . $extra;
1917 return $cgi->a({-href
=> $href, -class => "list subject"},
1918 esc_html
($long)) . $extra;
1922 # Rather than recomputing the url for an email multiple times, we cache it
1923 # after the first hit. This gives a visible benefit in views where the avatar
1924 # for the same email is used repeatedly (e.g. shortlog).
1925 # The cache is shared by all avatar engines (currently gravatar only), which
1926 # are free to use it as preferred. Since only one avatar engine is used for any
1927 # given page, there's no risk for cache conflicts.
1928 our %avatar_cache = ();
1930 # Compute the picon url for a given email, by using the picon search service over at
1931 # http://www.cs.indiana.edu/picons/search.html
1933 my $email = lc shift;
1934 if (!$avatar_cache{$email}) {
1935 my ($user, $domain) = split('@', $email);
1936 $avatar_cache{$email} =
1937 "http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/" .
1939 "users+domains+unknown/up/single";
1941 return $avatar_cache{$email};
1944 # Compute the gravatar url for a given email, if it's not in the cache already.
1945 # Gravatar stores only the part of the URL before the size, since that's the
1946 # one computationally more expensive. This also allows reuse of the cache for
1947 # different sizes (for this particular engine).
1949 my $email = lc shift;
1951 $avatar_cache{$email} ||=
1952 "http://www.gravatar.com/avatar/" .
1953 Digest
::MD5
::md5_hex
($email) . "?s=";
1954 return $avatar_cache{$email} . $size;
1957 # Insert an avatar for the given $email at the given $size if the feature
1959 sub git_get_avatar
{
1960 my ($email, %opts) = @_;
1961 my $pre_white = ($opts{-pad_before
} ? " " : "");
1962 my $post_white = ($opts{-pad_after
} ? " " : "");
1963 $opts{-size
} ||= 'default';
1964 my $size = $avatar_size{$opts{-size
}} || $avatar_size{'default'};
1966 if ($git_avatar eq 'gravatar') {
1967 $url = gravatar_url
($email, $size);
1968 } elsif ($git_avatar eq 'picon') {
1969 $url = picon_url
($email);
1971 # Other providers can be added by extending the if chain, defining $url
1972 # as needed. If no variant puts something in $url, we assume avatars
1973 # are completely disabled/unavailable.
1976 "<img width=\"$size\" " .
1977 "class=\"avatar\" " .
1978 "src=\"".esc_url
($url)."\" " .
1986 sub format_search_author
{
1987 my ($author, $searchtype, $displaytext) = @_;
1988 my $have_search = gitweb_check_feature
('search');
1992 if ($searchtype eq 'author') {
1993 $performed = "authored";
1994 } elsif ($searchtype eq 'committer') {
1995 $performed = "committed";
1998 return $cgi->a({-href
=> href
(action
=>"search", hash
=>$hash,
1999 searchtext
=>$author,
2000 searchtype
=>$searchtype), class=>"list",
2001 title
=>"Search for commits $performed by $author"},
2005 return $displaytext;
2009 # format the author name of the given commit with the given tag
2010 # the author name is chopped and escaped according to the other
2011 # optional parameters (see chop_str).
2012 sub format_author_html
{
2015 my $author = chop_and_escape_str
($co->{'author_name'}, @_);
2016 return "<$tag class=\"author\">" .
2017 format_search_author
($co->{'author_name'}, "author",
2018 git_get_avatar
($co->{'author_email'}, -pad_after
=> 1) .
2023 # format git diff header line, i.e. "diff --(git|combined|cc) ..."
2024 sub format_git_diff_header_line
{
2026 my $diffinfo = shift;
2027 my ($from, $to) = @_;
2029 if ($diffinfo->{'nparents'}) {
2031 $line =~ s!^(diff (.*?) )"?.*$!$1!;
2032 if ($to->{'href'}) {
2033 $line .= $cgi->a({-href
=> $to->{'href'}, -class => "path"},
2034 esc_path
($to->{'file'}));
2035 } else { # file was deleted (no href)
2036 $line .= esc_path
($to->{'file'});
2040 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
2041 if ($from->{'href'}) {
2042 $line .= $cgi->a({-href
=> $from->{'href'}, -class => "path"},
2043 'a/' . esc_path
($from->{'file'}));
2044 } else { # file was added (no href)
2045 $line .= 'a/' . esc_path
($from->{'file'});
2048 if ($to->{'href'}) {
2049 $line .= $cgi->a({-href
=> $to->{'href'}, -class => "path"},
2050 'b/' . esc_path
($to->{'file'}));
2051 } else { # file was deleted
2052 $line .= 'b/' . esc_path
($to->{'file'});
2056 return "<div class=\"diff header\">$line</div>\n";
2059 # format extended diff header line, before patch itself
2060 sub format_extended_diff_header_line
{
2062 my $diffinfo = shift;
2063 my ($from, $to) = @_;
2066 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
2067 $line .= $cgi->a({-href
=>$from->{'href'}, -class=>"path"},
2068 esc_path
($from->{'file'}));
2070 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
2071 $line .= $cgi->a({-href
=>$to->{'href'}, -class=>"path"},
2072 esc_path
($to->{'file'}));
2074 # match single <mode>
2075 if ($line =~ m/\s(\d{6})$/) {
2076 $line .= '<span class="info"> (' .
2077 file_type_long
($1) .
2081 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
2082 # can match only for combined diff
2084 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2085 if ($from->{'href'}[$i]) {
2086 $line .= $cgi->a({-href
=>$from->{'href'}[$i],
2088 substr($diffinfo->{'from_id'}[$i],0,7));
2093 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
2096 if ($to->{'href'}) {
2097 $line .= $cgi->a({-href
=>$to->{'href'}, -class=>"hash"},
2098 substr($diffinfo->{'to_id'},0,7));
2103 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
2104 # can match only for ordinary diff
2105 my ($from_link, $to_link);
2106 if ($from->{'href'}) {
2107 $from_link = $cgi->a({-href
=>$from->{'href'}, -class=>"hash"},
2108 substr($diffinfo->{'from_id'},0,7));
2110 $from_link = '0' x
7;
2112 if ($to->{'href'}) {
2113 $to_link = $cgi->a({-href
=>$to->{'href'}, -class=>"hash"},
2114 substr($diffinfo->{'to_id'},0,7));
2118 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
2119 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
2122 return $line . "<br/>\n";
2125 # format from-file/to-file diff header
2126 sub format_diff_from_to_header
{
2127 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
2132 #assert($line =~ m/^---/) if DEBUG;
2133 # no extra formatting for "^--- /dev/null"
2134 if (! $diffinfo->{'nparents'}) {
2135 # ordinary (single parent) diff
2136 if ($line =~ m!^--- "?a/!) {
2137 if ($from->{'href'}) {
2139 $cgi->a({-href
=>$from->{'href'}, -class=>"path"},
2140 esc_path
($from->{'file'}));
2143 esc_path
($from->{'file'});
2146 $result .= qq
!<div
class="diff from_file">$line</div
>\n!;
2149 # combined diff (merge commit)
2150 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2151 if ($from->{'href'}[$i]) {
2153 $cgi->a({-href
=>href
(action
=>"blobdiff",
2154 hash_parent
=>$diffinfo->{'from_id'}[$i],
2155 hash_parent_base
=>$parents[$i],
2156 file_parent
=>$from->{'file'}[$i],
2157 hash
=>$diffinfo->{'to_id'},
2159 file_name
=>$to->{'file'}),
2161 -title
=>"diff" . ($i+1)},
2164 $cgi->a({-href
=>$from->{'href'}[$i], -class=>"path"},
2165 esc_path
($from->{'file'}[$i]));
2167 $line = '--- /dev/null';
2169 $result .= qq
!<div
class="diff from_file">$line</div
>\n!;
2174 #assert($line =~ m/^\+\+\+/) if DEBUG;
2175 # no extra formatting for "^+++ /dev/null"
2176 if ($line =~ m!^\+\+\+ "?b/!) {
2177 if ($to->{'href'}) {
2179 $cgi->a({-href
=>$to->{'href'}, -class=>"path"},
2180 esc_path
($to->{'file'}));
2183 esc_path
($to->{'file'});
2186 $result .= qq
!<div
class="diff to_file">$line</div
>\n!;
2191 # create note for patch simplified by combined diff
2192 sub format_diff_cc_simplified
{
2193 my ($diffinfo, @parents) = @_;
2196 $result .= "<div class=\"diff header\">" .
2198 if (!is_deleted
($diffinfo)) {
2199 $result .= $cgi->a({-href
=> href
(action
=>"blob",
2201 hash
=>$diffinfo->{'to_id'},
2202 file_name
=>$diffinfo->{'to_file'}),
2204 esc_path
($diffinfo->{'to_file'}));
2206 $result .= esc_path
($diffinfo->{'to_file'});
2208 $result .= "</div>\n" . # class="diff header"
2209 "<div class=\"diff nodifferences\">" .
2211 "</div>\n"; # class="diff nodifferences"
2216 # format patch (diff) line (not to be used for diff headers)
2217 sub format_diff_line
{
2219 my ($from, $to) = @_;
2220 my $diff_class = "";
2224 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
2226 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
2227 if ($line =~ m/^\@{3}/) {
2228 $diff_class = " chunk_header";
2229 } elsif ($line =~ m/^\\/) {
2230 $diff_class = " incomplete";
2231 } elsif ($prefix =~ tr/+/+/) {
2232 $diff_class = " add";
2233 } elsif ($prefix =~ tr/-/-/) {
2234 $diff_class = " rem";
2237 # assume ordinary diff
2238 my $char = substr($line, 0, 1);
2240 $diff_class = " add";
2241 } elsif ($char eq '-') {
2242 $diff_class = " rem";
2243 } elsif ($char eq '@') {
2244 $diff_class = " chunk_header";
2245 } elsif ($char eq "\\") {
2246 $diff_class = " incomplete";
2249 $line = untabify
($line);
2250 if ($from && $to && $line =~ m/^\@{2} /) {
2251 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
2252 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
2254 $from_lines = 0 unless defined $from_lines;
2255 $to_lines = 0 unless defined $to_lines;
2257 if ($from->{'href'}) {
2258 $from_text = $cgi->a({-href
=>"$from->{'href'}#l$from_start",
2259 -class=>"list"}, $from_text);
2261 if ($to->{'href'}) {
2262 $to_text = $cgi->a({-href
=>"$to->{'href'}#l$to_start",
2263 -class=>"list"}, $to_text);
2265 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
2266 "<span class=\"section\">" . esc_html
($section, -nbsp
=>1) . "</span>";
2267 return "<div class=\"diff$diff_class\">$line</div>\n";
2268 } elsif ($from && $to && $line =~ m/^\@{3}/) {
2269 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
2270 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
2272 @from_text = split(' ', $ranges);
2273 for (my $i = 0; $i < @from_text; ++$i) {
2274 ($from_start[$i], $from_nlines[$i]) =
2275 (split(',', substr($from_text[$i], 1)), 0);
2278 $to_text = pop @from_text;
2279 $to_start = pop @from_start;
2280 $to_nlines = pop @from_nlines;
2282 $line = "<span class=\"chunk_info\">$prefix ";
2283 for (my $i = 0; $i < @from_text; ++$i) {
2284 if ($from->{'href'}[$i]) {
2285 $line .= $cgi->a({-href
=>"$from->{'href'}[$i]#l$from_start[$i]",
2286 -class=>"list"}, $from_text[$i]);
2288 $line .= $from_text[$i];
2292 if ($to->{'href'}) {
2293 $line .= $cgi->a({-href
=>"$to->{'href'}#l$to_start",
2294 -class=>"list"}, $to_text);
2298 $line .= " $prefix</span>" .
2299 "<span class=\"section\">" . esc_html
($section, -nbsp
=>1) . "</span>";
2300 return "<div class=\"diff$diff_class\">$line</div>\n";
2302 return "<div class=\"diff$diff_class\">" . esc_html
($line, -nbsp
=>1) . "</div>\n";
2305 # Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
2306 # linked. Pass the hash of the tree/commit to snapshot.
2307 sub format_snapshot_links
{
2309 my $num_fmts = @snapshot_fmts;
2310 if ($num_fmts > 1) {
2311 # A parenthesized list of links bearing format names.
2312 # e.g. "snapshot (_tar.gz_ _zip_)"
2313 return "snapshot (" . join(' ', map
2320 }, $known_snapshot_formats{$_}{'display'})
2321 , @snapshot_fmts) . ")";
2322 } elsif ($num_fmts == 1) {
2323 # A single "snapshot" link whose tooltip bears the format name.
2325 my ($fmt) = @snapshot_fmts;
2331 snapshot_format
=>$fmt
2333 -title
=> "in format: $known_snapshot_formats{$fmt}{'display'}"
2335 } else { # $num_fmts == 0
2340 ## ......................................................................
2341 ## functions returning values to be passed, perhaps after some
2342 ## transformation, to other functions; e.g. returning arguments to href()
2344 # returns hash to be passed to href to generate gitweb URL
2345 # in -title key it returns description of link
2347 my $format = shift || 'Atom';
2348 my %res = (action
=> lc($format));
2350 # feed links are possible only for project views
2351 return unless (defined $project);
2352 # some views should link to OPML, or to generic project feed,
2353 # or don't have specific feed yet (so they should use generic)
2354 return if ($action =~ /^(?:tags|heads|forks|tag|search)$/x);
2357 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
2358 # from tag links; this also makes possible to detect branch links
2359 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
2360 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
2363 # find log type for feed description (title)
2365 if (defined $file_name) {
2366 $type = "history of $file_name";
2367 $type .= "/" if ($action eq 'tree');
2368 $type .= " on '$branch'" if (defined $branch);
2370 $type = "log of $branch" if (defined $branch);
2373 $res{-title
} = $type;
2374 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
2375 $res{'file_name'} = $file_name;
2380 ## ----------------------------------------------------------------------
2381 ## git utility subroutines, invoking git commands
2383 # returns path to the core git executable and the --git-dir parameter as list
2385 $number_of_git_cmds++;
2386 return $GIT, '--git-dir='.$git_dir;
2389 # quote the given arguments for passing them to the shell
2390 # quote_command("command", "arg 1", "arg with ' and ! characters")
2391 # => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
2392 # Try to avoid using this function wherever possible.
2395 map { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ );
2398 # get HEAD ref of given project as hash
2399 sub git_get_head_hash
{
2400 return git_get_full_hash
(shift, 'HEAD');
2403 sub git_get_full_hash
{
2404 return git_get_hash
(@_);
2407 sub git_get_short_hash
{
2408 return git_get_hash
(@_, '--short=7');
2412 my ($project, $hash, @options) = @_;
2413 my $o_git_dir = $git_dir;
2415 $git_dir = "$projectroot/$project";
2416 if (open my $fd, '-|', git_cmd
(), 'rev-parse',
2417 '--verify', '-q', @options, $hash) {
2419 chomp $retval if defined $retval;
2422 if (defined $o_git_dir) {
2423 $git_dir = $o_git_dir;
2428 # get type of given object
2432 open my $fd, "-|", git_cmd
(), "cat-file", '-t', $hash or return;
2434 close $fd or return;
2439 # repository configuration
2440 our $config_file = '';
2443 # store multiple values for single key as anonymous array reference
2444 # single values stored directly in the hash, not as [ <value> ]
2445 sub hash_set_multi
{
2446 my ($hash, $key, $value) = @_;
2448 if (!exists $hash->{$key}) {
2449 $hash->{$key} = $value;
2450 } elsif (!ref $hash->{$key}) {
2451 $hash->{$key} = [ $hash->{$key}, $value ];
2453 push @{$hash->{$key}}, $value;
2457 # return hash of git project configuration
2458 # optionally limited to some section, e.g. 'gitweb'
2459 sub git_parse_project_config
{
2460 my $section_regexp = shift;
2465 open my $fh, "-|", git_cmd
(), "config", '-z', '-l',
2468 while (my $keyval = <$fh>) {
2470 my ($key, $value) = split(/\n/, $keyval, 2);
2472 hash_set_multi
(\
%config, $key, $value)
2473 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
2480 # convert config value to boolean: 'true' or 'false'
2481 # no value, number > 0, 'true' and 'yes' values are true
2482 # rest of values are treated as false (never as error)
2483 sub config_to_bool
{
2486 return 1 if !defined $val; # section.key
2488 # strip leading and trailing whitespace
2492 return (($val =~ /^\d+$/ && $val) || # section.key = 1
2493 ($val =~ /^(?:true|yes)$/i)); # section.key = true
2496 # convert config value to simple decimal number
2497 # an optional value suffix of 'k', 'm', or 'g' will cause the value
2498 # to be multiplied by 1024, 1048576, or 1073741824
2502 # strip leading and trailing whitespace
2506 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
2508 # unknown unit is treated as 1
2509 return $num * ($unit eq 'g' ? 1073741824 :
2510 $unit eq 'm' ? 1048576 :
2511 $unit eq 'k' ? 1024 : 1);
2516 # convert config value to array reference, if needed
2517 sub config_to_multi
{
2520 return ref($val) ? $val : (defined($val) ? [ $val ] : []);
2523 sub git_get_project_config
{
2524 my ($key, $type) = @_;
2526 return unless defined $git_dir;
2529 return unless ($key);
2530 $key =~ s/^gitweb\.//;
2531 return if ($key =~ m/\W/);
2534 if (defined $type) {
2537 unless ($type eq 'bool' || $type eq 'int');
2541 if (!defined $config_file ||
2542 $config_file ne "$git_dir/config") {
2543 %config = git_parse_project_config
('gitweb');
2544 $config_file = "$git_dir/config";
2547 # check if config variable (key) exists
2548 return unless exists $config{"gitweb.$key"};
2551 if (!defined $type) {
2552 return $config{"gitweb.$key"};
2553 } elsif ($type eq 'bool') {
2554 # backward compatibility: 'git config --bool' returns true/false
2555 return config_to_bool
($config{"gitweb.$key"}) ? 'true' : 'false';
2556 } elsif ($type eq 'int') {
2557 return config_to_int
($config{"gitweb.$key"});
2559 return $config{"gitweb.$key"};
2562 # get hash of given path at given ref
2563 sub git_get_hash_by_path
{
2565 my $path = shift || return undef;
2570 open my $fd, "-|", git_cmd
(), "ls-tree", $base, "--", $path
2571 or die_error
(500, "Open git-ls-tree failed");
2573 close $fd or return undef;
2575 if (!defined $line) {
2576 # there is no tree or hash given by $path at $base
2580 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2581 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
2582 if (defined $type && $type ne $2) {
2583 # type doesn't match
2589 # get path of entry with given hash at given tree-ish (ref)
2590 # used to get 'from' filename for combined diff (merge commit) for renames
2591 sub git_get_path_by_hash
{
2592 my $base = shift || return;
2593 my $hash = shift || return;
2597 open my $fd, "-|", git_cmd
(), "ls-tree", '-r', '-t', '-z', $base
2599 while (my $line = <$fd>) {
2602 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
2603 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
2604 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
2613 ## ......................................................................
2614 ## git utility functions, directly accessing git repository
2616 # get the value of config variable either from file named as the variable
2617 # itself in the repository ($GIT_DIR/$name file), or from gitweb.$name
2618 # configuration variable in the repository config file.
2619 sub git_get_file_or_project_config
{
2620 my ($path, $name) = @_;
2622 $git_dir = "$projectroot/$path";
2623 open my $fd, '<', "$git_dir/$name"
2624 or return git_get_project_config
($name);
2627 if (defined $conf) {
2633 sub git_get_project_description
{
2635 return git_get_file_or_project_config
($path, 'description');
2638 sub git_get_project_category
{
2640 return git_get_file_or_project_config
($path, 'category');
2644 # supported formats:
2645 # * $GIT_DIR/ctags/<tagname> file (in 'ctags' subdirectory)
2646 # - if its contents is a number, use it as tag weight,
2647 # - otherwise add a tag with weight 1
2648 # * $GIT_DIR/ctags file, each line is a tag (with weight 1)
2649 # the same value multiple times increases tag weight
2650 # * `gitweb.ctag' multi-valued repo config variable
2651 sub git_get_project_ctags
{
2652 my $project = shift;
2655 $git_dir = "$projectroot/$project";
2656 if (opendir my $dh, "$git_dir/ctags") {
2657 my @files = grep { -f
$_ } map { "$git_dir/ctags/$_" } readdir($dh);
2658 foreach my $tagfile (@files) {
2659 open my $ct, '<', $tagfile
2665 (my $ctag = $tagfile) =~ s
#.*/##;
2666 if ($val =~ /^\d+$/) {
2667 $ctags->{$ctag} = $val;
2669 $ctags->{$ctag} = 1;
2674 } elsif (open my $fh, '<', "$git_dir/ctags") {
2675 while (my $line = <$fh>) {
2677 $ctags->{$line}++ if $line;
2682 my $taglist = config_to_multi
(git_get_project_config
('ctag'));
2683 foreach my $tag (@$taglist) {
2691 # return hash, where keys are content tags ('ctags'),
2692 # and values are sum of weights of given tag in every project
2693 sub git_gather_all_ctags
{
2694 my $projects = shift;
2697 foreach my $p (@$projects) {
2698 foreach my $ct (keys %{$p->{'ctags'}}) {
2699 $ctags->{$ct} += $p->{'ctags'}->{$ct};
2706 sub git_populate_project_tagcloud
{
2709 # First, merge different-cased tags; tags vote on casing
2711 foreach (keys %$ctags) {
2712 $ctags_lc{lc $_}->{count
} += $ctags->{$_};
2713 if (not $ctags_lc{lc $_}->{topcount
}
2714 or $ctags_lc{lc $_}->{topcount
} < $ctags->{$_}) {
2715 $ctags_lc{lc $_}->{topcount
} = $ctags->{$_};
2716 $ctags_lc{lc $_}->{topname
} = $_;
2721 my $matched = $cgi->param('by_tag');
2722 if (eval { require HTML
::TagCloud
; 1; }) {
2723 $cloud = HTML
::TagCloud-
>new;
2724 foreach my $ctag (sort keys %ctags_lc) {
2725 # Pad the title with spaces so that the cloud looks
2727 my $title = esc_html
($ctags_lc{$ctag}->{topname
});
2728 $title =~ s/ / /g;
2729 $title =~ s/^/ /g;
2730 $title =~ s/$/ /g;
2731 if (defined $matched && $matched eq $ctag) {
2732 $title = qq(<span class="match">$title</span>);
2734 $cloud->add($title, href
(project
=>undef, ctag
=>$ctag),
2735 $ctags_lc{$ctag}->{count
});
2739 foreach my $ctag (keys %ctags_lc) {
2740 my $title = esc_html
($ctags_lc{$ctag}->{topname
}, -nbsp
=>1);
2741 if (defined $matched && $matched eq $ctag) {
2742 $title = qq(<span class="match">$title</span>);
2744 $cloud->{$ctag}{count
} = $ctags_lc{$ctag}->{count
};
2745 $cloud->{$ctag}{ctag
} =
2746 $cgi->a({-href
=>href
(project
=>undef, ctag
=>$ctag)}, $title);
2752 sub git_show_project_tagcloud
{
2753 my ($cloud, $count) = @_;
2754 if (ref $cloud eq 'HTML::TagCloud') {
2755 return $cloud->html_and_css($count);
2757 my @tags = sort { $cloud->{$a}->{'count'} <=> $cloud->{$b}->{'count'} } keys %$cloud;
2759 '<div id="htmltagcloud"'.($project ? '' : ' align="center"').'>' .
2761 $cloud->{$_}->{'ctag'}
2762 } splice(@tags, 0, $count)) .
2767 sub git_get_project_url_list
{
2770 $git_dir = "$projectroot/$path";
2771 open my $fd, '<', "$git_dir/cloneurl"
2772 or return wantarray ?
2773 @{ config_to_multi
(git_get_project_config
('url')) } :
2774 config_to_multi
(git_get_project_config
('url'));
2775 my @git_project_url_list = map { chomp; $_ } <$fd>;
2778 return wantarray ? @git_project_url_list : \
@git_project_url_list;
2781 sub git_get_projects_list
{
2782 my $filter = shift || '';
2785 $filter =~ s/\.git$//;
2787 if (-d
$projects_list) {
2788 # search in directory
2789 my $dir = $projects_list;
2790 # remove the trailing "/"
2792 my $pfxlen = length("$projects_list");
2793 my $pfxdepth = ($projects_list =~ tr!/!!);
2794 # when filtering, search only given subdirectory
2801 follow_fast
=> 1, # follow symbolic links
2802 follow_skip
=> 2, # ignore duplicates
2803 dangling_symlinks
=> 0, # ignore dangling symlinks, silently
2806 our $project_maxdepth;
2808 # skip project-list toplevel, if we get it.
2809 return if (m!^[/.]$!);
2810 # only directories can be git repositories
2811 return unless (-d
$_);
2812 # don't traverse too deep (Find is super slow on os x)
2813 # $project_maxdepth excludes depth of $projectroot
2814 if (($File::Find
::name
=~ tr!/!!) - $pfxdepth > $project_maxdepth) {
2815 $File::Find
::prune
= 1;
2819 my $path = substr($File::Find
::name
, $pfxlen + 1);
2820 # we check related file in $projectroot
2821 if (check_export_ok
("$projectroot/$path")) {
2822 push @list, { path
=> $path };
2823 $File::Find
::prune
= 1;
2828 } elsif (-f
$projects_list) {
2829 # read from file(url-encoded):
2830 # 'git%2Fgit.git Linus+Torvalds'
2831 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2832 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2833 open my $fd, '<', $projects_list or return;
2835 while (my $line = <$fd>) {
2837 my ($path, $owner) = split ' ', $line;
2838 $path = unescape
($path);
2839 $owner = unescape
($owner);
2840 if (!defined $path) {
2843 # if $filter is rpovided, check if $path begins with $filter
2844 if ($filter && $path !~ m!^\Q$filter\E/!) {
2847 if (check_export_ok
("$projectroot/$path")) {
2850 owner
=> to_utf8
($owner),
2860 # written with help of Tree::Trie module (Perl Artistic License, GPL compatibile)
2861 # as side effects it sets 'forks' field to list of forks for forked projects
2862 sub filter_forks_from_projects_list
{
2863 my $projects = shift;
2865 my %trie; # prefix tree of directories (path components)
2866 # generate trie out of those directories that might contain forks
2867 foreach my $pr (@$projects) {
2868 my $path = $pr->{'path'};
2869 $path =~ s/\.git$//; # forks of 'repo.git' are in 'repo/' directory
2870 next if ($path =~ m!/$!); # skip non-bare repositories, e.g. 'repo/.git'
2871 next unless ($path); # skip '.git' repository: tests, git-instaweb
2872 next unless (-d
$path); # containing directory exists
2873 $pr->{'forks'} = []; # there can be 0 or more forks of project
2876 my @dirs = split('/', $path);
2877 # walk the trie, until either runs out of components or out of trie
2879 while (scalar @dirs &&
2880 exists($ref->{$dirs[0]})) {
2881 $ref = $ref->{shift @dirs};
2883 # create rest of trie structure from rest of components
2884 foreach my $dir (@dirs) {
2885 $ref = $ref->{$dir} = {};
2887 # create end marker, store $pr as a data
2888 $ref->{''} = $pr if (!exists $ref->{''});
2891 # filter out forks, by finding shortest prefix match for paths
2894 foreach my $pr (@$projects) {
2898 foreach my $dir (split('/', $pr->{'path'})) {
2899 if (exists $ref->{''}) {
2900 # found [shortest] prefix, is a fork - skip it
2901 push @{$ref->{''}{'forks'}}, $pr;
2904 if (!exists $ref->{$dir}) {
2905 # not in trie, cannot have prefix, not a fork
2906 push @filtered, $pr;
2909 # If the dir is there, we just walk one step down the trie.
2910 $ref = $ref->{$dir};
2912 # we ran out of trie
2913 # (shouldn't happen: it's either no match, or end marker)
2914 push @filtered, $pr;
2920 # note: fill_project_list_info must be run first,
2921 # for 'descr_long' and 'ctags' to be filled
2922 sub search_projects_list
{
2923 my ($projlist, %opts) = @_;
2924 my $tagfilter = $opts{'tagfilter'};
2925 my $searchtext = $opts{'searchtext'};
2928 unless ($tagfilter || $searchtext);
2932 foreach my $pr (@$projlist) {
2935 next unless ref($pr->{'ctags'}) eq 'HASH';
2937 grep { lc($_) eq lc($tagfilter) } keys %{$pr->{'ctags'}};
2942 $pr->{'path'} =~ /$searchtext/ ||
2943 $pr->{'descr_long'} =~ /$searchtext/;
2946 push @projects, $pr;
2952 our $gitweb_project_owner = undef;
2953 sub git_get_project_list_from_file
{
2955 return if (defined $gitweb_project_owner);
2957 $gitweb_project_owner = {};
2958 # read from file (url-encoded):
2959 # 'git%2Fgit.git Linus+Torvalds'
2960 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2961 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2962 if (-f
$projects_list) {
2963 open(my $fd, '<', $projects_list);
2964 while (my $line = <$fd>) {
2966 my ($pr, $ow) = split ' ', $line;
2967 $pr = unescape
($pr);
2968 $ow = unescape
($ow);
2969 $gitweb_project_owner->{$pr} = to_utf8
($ow);
2975 sub git_get_project_owner
{
2976 my $project = shift;
2979 return undef unless $project;
2980 $git_dir = "$projectroot/$project";
2982 if (!defined $gitweb_project_owner) {
2983 git_get_project_list_from_file
();
2986 if (exists $gitweb_project_owner->{$project}) {
2987 $owner = $gitweb_project_owner->{$project};
2989 if (!defined $owner){
2990 $owner = git_get_project_config
('owner');
2992 if (!defined $owner) {
2993 $owner = get_file_owner
("$git_dir");
2999 sub git_get_last_activity
{
3003 $git_dir = "$projectroot/$path";
3004 open($fd, "-|", git_cmd
(), 'for-each-ref',
3005 '--format=%(committer)',
3006 '--sort=-committerdate',
3008 'refs/heads') or return;
3009 my $most_recent = <$fd>;
3010 close $fd or return;
3011 if (defined $most_recent &&
3012 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
3014 my $age = time - $timestamp;
3015 return ($age, age_string
($age));
3017 return (undef, undef);
3020 # Implementation note: when a single remote is wanted, we cannot use 'git
3021 # remote show -n' because that command always work (assuming it's a remote URL
3022 # if it's not defined), and we cannot use 'git remote show' because that would
3023 # try to make a network roundtrip. So the only way to find if that particular
3024 # remote is defined is to walk the list provided by 'git remote -v' and stop if
3025 # and when we find what we want.
3026 sub git_get_remotes_list
{
3030 open my $fd, '-|' , git_cmd
(), 'remote', '-v';
3032 while (my $remote = <$fd>) {
3034 $remote =~ s!\t(.*?)\s+\((\w+)\)$!!;
3035 next if $wanted and not $remote eq $wanted;
3036 my ($url, $key) = ($1, $2);
3038 $remotes{$remote} ||= { 'heads' => () };
3039 $remotes{$remote}{$key} = $url;
3041 close $fd or return;
3042 return wantarray ? %remotes : \
%remotes;
3045 # Takes a hash of remotes as first parameter and fills it by adding the
3046 # available remote heads for each of the indicated remotes.
3047 sub fill_remote_heads
{
3048 my $remotes = shift;
3049 my @heads = map { "remotes/$_" } keys %$remotes;
3050 my @remoteheads = git_get_heads_list
(undef, @heads);
3051 foreach my $remote (keys %$remotes) {
3052 $remotes->{$remote}{'heads'} = [ grep {
3053 $_->{'name'} =~ s!^$remote/!!
3058 sub git_get_references
{
3059 my $type = shift || "";
3061 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
3062 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
3063 open my $fd, "-|", git_cmd
(), "show-ref", "--dereference",
3064 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
3067 while (my $line = <$fd>) {
3069 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {
3070 if (defined $refs{$1}) {
3071 push @{$refs{$1}}, $2;
3077 close $fd or return;
3081 sub git_get_rev_name_tags
{
3082 my $hash = shift || return undef;
3084 open my $fd, "-|", git_cmd
(), "name-rev", "--tags", $hash
3086 my $name_rev = <$fd>;
3089 if ($name_rev =~ m
|^$hash tags
/(.*)$|) {
3092 # catches also '$hash undefined' output
3097 ## ----------------------------------------------------------------------
3098 ## parse to hash functions
3102 my $tz = shift || "-0000";
3105 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
3106 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
3107 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
3108 $date{'hour'} = $hour;
3109 $date{'minute'} = $min;
3110 $date{'mday'} = $mday;
3111 $date{'day'} = $days[$wday];
3112 $date{'month'} = $months[$mon];
3113 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
3114 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
3115 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
3116 $mday, $months[$mon], $hour ,$min;
3117 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
3118 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
3120 my ($tz_sign, $tz_hour, $tz_min) =
3121 ($tz =~ m/^([-+])(\d\d)(\d\d)$/);
3122 $tz_sign = ($tz_sign eq '-' ? -1 : +1);
3123 my $local = $epoch + $tz_sign*((($tz_hour*60) + $tz_min)*60);
3124 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
3125 $date{'hour_local'} = $hour;
3126 $date{'minute_local'} = $min;
3127 $date{'tz_local'} = $tz;
3128 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
3129 1900+$year, $mon+1, $mday,
3130 $hour, $min, $sec, $tz);
3139 open my $fd, "-|", git_cmd
(), "cat-file", "tag", $tag_id or return;
3140 $tag{'id'} = $tag_id;
3141 while (my $line = <$fd>) {
3143 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
3144 $tag{'object'} = $1;
3145 } elsif ($line =~ m/^type (.+)$/) {
3147 } elsif ($line =~ m/^tag (.+)$/) {
3149 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
3150 $tag{'author'} = $1;
3151 $tag{'author_epoch'} = $2;
3152 $tag{'author_tz'} = $3;
3153 if ($tag{'author'} =~ m/^([^<]+) <([^>]*)>/) {
3154 $tag{'author_name'} = $1;
3155 $tag{'author_email'} = $2;
3157 $tag{'author_name'} = $tag{'author'};
3159 } elsif ($line =~ m/--BEGIN/) {
3160 push @comment, $line;
3162 } elsif ($line eq "") {
3166 push @comment, <$fd>;
3167 $tag{'comment'} = \
@comment;
3168 close $fd or return;
3169 if (!defined $tag{'name'}) {
3175 sub parse_commit_text
{
3176 my ($commit_text, $withparents) = @_;
3177 my @commit_lines = split '\n', $commit_text;
3180 pop @commit_lines; # Remove '\0'
3182 if (! @commit_lines) {
3186 my $header = shift @commit_lines;
3187 if ($header !~ m/^[0-9a-fA-F]{40}/) {
3190 ($co{'id'}, my @parents) = split ' ', $header;
3191 while (my $line = shift @commit_lines) {
3192 last if $line eq "\n";
3193 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
3195 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
3197 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
3198 $co{'author'} = to_utf8
($1);
3199 $co{'author_epoch'} = $2;
3200 $co{'author_tz'} = $3;
3201 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
3202 $co{'author_name'} = $1;
3203 $co{'author_email'} = $2;
3205 $co{'author_name'} = $co{'author'};
3207 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
3208 $co{'committer'} = to_utf8
($1);
3209 $co{'committer_epoch'} = $2;
3210 $co{'committer_tz'} = $3;
3211 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
3212 $co{'committer_name'} = $1;
3213 $co{'committer_email'} = $2;
3215 $co{'committer_name'} = $co{'committer'};
3219 if (!defined $co{'tree'}) {
3222 $co{'parents'} = \
@parents;
3223 $co{'parent'} = $parents[0];
3225 foreach my $title (@commit_lines) {
3228 $co{'title'} = chop_str
($title, 80, 5);
3229 # remove leading stuff of merges to make the interesting part visible
3230 if (length($title) > 50) {
3231 $title =~ s/^Automatic //;
3232 $title =~ s/^merge (of|with) /Merge ... /i;
3233 if (length($title) > 50) {
3234 $title =~ s/(http|rsync):\/\///;
3236 if (length($title) > 50) {
3237 $title =~ s/(master|www|rsync)\.//;
3239 if (length($title) > 50) {
3240 $title =~ s/kernel.org:?//;
3242 if (length($title) > 50) {
3243 $title =~ s/\/pub\/scm//;
3246 $co{'title_short'} = chop_str
($title, 50, 5);
3250 if (! defined $co{'title'} || $co{'title'} eq "") {
3251 $co{'title'} = $co{'title_short'} = '(no commit message)';
3253 # remove added spaces
3254 foreach my $line (@commit_lines) {
3257 $co{'comment'} = \
@commit_lines;
3259 my $age = time - $co{'committer_epoch'};
3261 $co{'age_string'} = age_string
($age);
3262 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
3263 if ($age > 60*60*24*7*2) {
3264 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
3265 $co{'age_string_age'} = $co{'age_string'};
3267 $co{'age_string_date'} = $co{'age_string'};
3268 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
3274 my ($commit_id) = @_;
3279 open my $fd, "-|", git_cmd
(), "rev-list",
3285 or die_error
(500, "Open git-rev-list failed");
3286 %co = parse_commit_text
(<$fd>, 1);
3293 my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
3301 open my $fd, "-|", git_cmd
(), "rev-list",
3304 ("--max-count=" . $maxcount),
3305 ("--skip=" . $skip),
3309 ($filename ? ($filename) : ())
3310 or die_error
(500, "Open git-rev-list failed");
3311 while (my $line = <$fd>) {
3312 my %co = parse_commit_text
($line);
3317 return wantarray ? @cos : \
@cos;
3320 # parse line of git-diff-tree "raw" output
3321 sub parse_difftree_raw_line
{
3325 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
3326 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
3327 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
3328 $res{'from_mode'} = $1;
3329 $res{'to_mode'} = $2;
3330 $res{'from_id'} = $3;
3332 $res{'status'} = $5;
3333 $res{'similarity'} = $6;
3334 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
3335 ($res{'from_file'}, $res{'to_file'}) = map { unquote
($_) } split("\t", $7);
3337 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote
($7);
3340 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
3341 # combined diff (for merge commit)
3342 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
3343 $res{'nparents'} = length($1);
3344 $res{'from_mode'} = [ split(' ', $2) ];
3345 $res{'to_mode'} = pop @{$res{'from_mode'}};
3346 $res{'from_id'} = [ split(' ', $3) ];
3347 $res{'to_id'} = pop @{$res{'from_id'}};
3348 $res{'status'} = [ split('', $4) ];
3349 $res{'to_file'} = unquote
($5);
3351 # 'c512b523472485aef4fff9e57b229d9d243c967f'
3352 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
3353 $res{'commit'} = $1;
3356 return wantarray ? %res : \
%res;
3359 # wrapper: return parsed line of git-diff-tree "raw" output
3360 # (the argument might be raw line, or parsed info)
3361 sub parsed_difftree_line
{
3362 my $line_or_ref = shift;
3364 if (ref($line_or_ref) eq "HASH") {
3365 # pre-parsed (or generated by hand)
3366 return $line_or_ref;
3368 return parse_difftree_raw_line
($line_or_ref);
3372 # parse line of git-ls-tree output
3373 sub parse_ls_tree_line
{
3379 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'
3380 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;
3389 $res{'name'} = unquote
($5);
3392 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
3393 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
3401 $res{'name'} = unquote
($4);
3405 return wantarray ? %res : \
%res;
3408 # generates _two_ hashes, references to which are passed as 2 and 3 argument
3409 sub parse_from_to_diffinfo
{
3410 my ($diffinfo, $from, $to, @parents) = @_;
3412 if ($diffinfo->{'nparents'}) {
3414 $from->{'file'} = [];
3415 $from->{'href'} = [];
3416 fill_from_file_info
($diffinfo, @parents)
3417 unless exists $diffinfo->{'from_file'};
3418 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
3419 $from->{'file'}[$i] =
3420 defined $diffinfo->{'from_file'}[$i] ?
3421 $diffinfo->{'from_file'}[$i] :
3422 $diffinfo->{'to_file'};
3423 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
3424 $from->{'href'}[$i] = href
(action
=>"blob",
3425 hash_base
=>$parents[$i],
3426 hash
=>$diffinfo->{'from_id'}[$i],
3427 file_name
=>$from->{'file'}[$i]);
3429 $from->{'href'}[$i] = undef;
3433 # ordinary (not combined) diff
3434 $from->{'file'} = $diffinfo->{'from_file'};
3435 if ($diffinfo->{'status'} ne "A") { # not new (added) file
3436 $from->{'href'} = href
(action
=>"blob", hash_base
=>$hash_parent,
3437 hash
=>$diffinfo->{'from_id'},
3438 file_name
=>$from->{'file'});
3440 delete $from->{'href'};
3444 $to->{'file'} = $diffinfo->{'to_file'};
3445 if (!is_deleted
($diffinfo)) { # file exists in result
3446 $to->{'href'} = href
(action
=>"blob", hash_base
=>$hash,
3447 hash
=>$diffinfo->{'to_id'},
3448 file_name
=>$to->{'file'});
3450 delete $to->{'href'};
3454 ## ......................................................................
3455 ## parse to array of hashes functions
3457 sub git_get_heads_list
{
3458 my ($limit, @classes) = @_;
3459 @classes = ('heads') unless @classes;
3460 my @patterns = map { "refs/$_" } @classes;
3463 open my $fd, '-|', git_cmd
(), 'for-each-ref',
3464 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
3465 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
3468 while (my $line = <$fd>) {
3472 my ($refinfo, $committerinfo) = split(/\0/, $line);
3473 my ($hash, $name, $title) = split(' ', $refinfo, 3);
3474 my ($committer, $epoch, $tz) =
3475 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
3476 $ref_item{'fullname'} = $name;
3477 $name =~ s!^refs/(?:head|remote)s/!!;
3479 $ref_item{'name'} = $name;
3480 $ref_item{'id'} = $hash;
3481 $ref_item{'title'} = $title || '(no commit message)';
3482 $ref_item{'epoch'} = $epoch;
3484 $ref_item{'age'} = age_string
(time - $ref_item{'epoch'});
3486 $ref_item{'age'} = "unknown";
3489 push @headslist, \
%ref_item;
3493 return wantarray ? @headslist : \
@headslist;
3496 sub git_get_tags_list
{
3500 open my $fd, '-|', git_cmd
(), 'for-each-ref',
3501 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
3502 '--format=%(objectname) %(objecttype) %(refname) '.
3503 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
3506 while (my $line = <$fd>) {
3510 my ($refinfo, $creatorinfo) = split(/\0/, $line);
3511 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
3512 my ($creator, $epoch, $tz) =
3513 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
3514 $ref_item{'fullname'} = $name;
3515 $name =~ s!^refs/tags/!!;
3517 $ref_item{'type'} = $type;
3518 $ref_item{'id'} = $id;
3519 $ref_item{'name'} = $name;
3520 if ($type eq "tag") {
3521 $ref_item{'subject'} = $title;
3522 $ref_item{'reftype'} = $reftype;
3523 $ref_item{'refid'} = $refid;
3525 $ref_item{'reftype'} = $type;
3526 $ref_item{'refid'} = $id;
3529 if ($type eq "tag" || $type eq "commit") {
3530 $ref_item{'epoch'} = $epoch;
3532 $ref_item{'age'} = age_string
(time - $ref_item{'epoch'});
3534 $ref_item{'age'} = "unknown";
3538 push @tagslist, \
%ref_item;
3542 return wantarray ? @tagslist : \
@tagslist;
3545 ## ----------------------------------------------------------------------
3546 ## filesystem-related functions
3548 sub get_file_owner
{
3551 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
3552 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
3553 if (!defined $gcos) {
3557 $owner =~ s/[,;].*$//;
3558 return to_utf8
($owner);
3561 # assume that file exists
3563 my $filename = shift;
3565 open my $fd, '<', $filename;
3566 print map { to_utf8
($_) } <$fd>;
3570 ## ......................................................................
3571 ## mimetype related functions
3573 sub mimetype_guess_file
{
3574 my $filename = shift;
3575 my $mimemap = shift;
3576 -r
$mimemap or return undef;
3579 open(my $mh, '<', $mimemap) or return undef;
3581 next if m/^#/; # skip comments
3582 my ($mimetype, @exts) = split(/\s+/);
3583 foreach my $ext (@exts) {
3584 $mimemap{$ext} = $mimetype;
3589 $filename =~ /\.([^.]*)$/;
3590 return $mimemap{$1};
3593 sub mimetype_guess
{
3594 my $filename = shift;
3596 $filename =~ /\./ or return undef;
3598 if ($mimetypes_file) {
3599 my $file = $mimetypes_file;
3600 if ($file !~ m!^/!) { # if it is relative path
3601 # it is relative to project
3602 $file = "$projectroot/$project/$file";
3604 $mime = mimetype_guess_file
($filename, $file);
3606 $mime ||= mimetype_guess_file
($filename, '/etc/mime.types');
3612 my $filename = shift;
3615 my $mime = mimetype_guess
($filename);
3616 $mime and return $mime;
3620 return $default_blob_plain_mimetype unless $fd;
3623 return 'text/plain';
3624 } elsif (! $filename) {
3625 return 'application/octet-stream';
3626 } elsif ($filename =~ m/\.png$/i) {
3628 } elsif ($filename =~ m/\.gif$/i) {
3630 } elsif ($filename =~ m/\.jpe?g$/i) {
3631 return 'image/jpeg';
3633 return 'application/octet-stream';
3637 sub blob_contenttype
{
3638 my ($fd, $file_name, $type) = @_;
3640 $type ||= blob_mimetype
($fd, $file_name);
3641 if ($type eq 'text/plain' && defined $default_text_plain_charset) {
3642 $type .= "; charset=$default_text_plain_charset";
3648 # guess file syntax for syntax highlighting; return undef if no highlighting
3649 # the name of syntax can (in the future) depend on syntax highlighter used
3650 sub guess_file_syntax
{
3651 my ($highlight, $mimetype, $file_name) = @_;
3652 return undef unless ($highlight && defined $file_name);
3653 my $basename = basename
($file_name, '.in');
3654 return $highlight_basename{$basename}
3655 if exists $highlight_basename{$basename};
3657 $basename =~ /\.([^.]*)$/;
3658 my $ext = $1 or return undef;
3659 return $highlight_ext{$ext}
3660 if exists $highlight_ext{$ext};
3665 # run highlighter and return FD of its output,
3666 # or return original FD if no highlighting
3667 sub run_highlighter
{
3668 my ($fd, $highlight, $syntax) = @_;
3669 return $fd unless ($highlight && defined $syntax);
3672 open $fd, quote_command
(git_cmd
(), "cat-file", "blob", $hash)." | ".
3673 quote_command
($highlight_bin).
3674 " --replace-tabs=8 --fragment --syntax $syntax |"
3675 or die_error
(500, "Couldn't open file or run syntax highlighter");
3679 ## ======================================================================
3680 ## functions printing HTML: header, footer, error page
3682 sub get_page_title
{
3683 my $title = to_utf8
($site_name);
3685 return $title unless (defined $project);
3686 $title .= " - " . to_utf8
($project);
3688 return $title unless (defined $action);
3689 $title .= "/$action"; # $action is US-ASCII (7bit ASCII)
3691 return $title unless (defined $file_name);
3692 $title .= " - " . esc_path
($file_name);
3693 if ($action eq "tree" && $file_name !~ m
|/$|) {
3700 sub get_content_type_html
{
3701 # require explicit support from the UA if we are to send the page as
3702 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
3703 # we have to do this because MSIE sometimes globs '*/*', pretending to
3704 # support xhtml+xml but choking when it gets what it asked for.
3705 if (defined $cgi->http('HTTP_ACCEPT') &&
3706 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\
+xml
(,|;|\s
|$)/ &&
3707 $cgi->Accept('application/xhtml+xml') != 0) {
3708 return 'application/xhtml+xml';
3714 sub print_feed_meta
{
3715 if (defined $project) {
3716 my %href_params = get_feed_info
();
3717 if (!exists $href_params{'-title'}) {
3718 $href_params{'-title'} = 'log';
3721 foreach my $format (qw(RSS Atom)) {
3722 my $type = lc($format);
3724 '-rel' => 'alternate',
3725 '-title' => esc_attr
("$project - $href_params{'-title'} - $format feed"),
3726 '-type' => "application/$type+xml"
3729 $href_params{'action'} = $type;
3730 $link_attr{'-href'} = href
(%href_params);
3732 "rel=\"$link_attr{'-rel'}\" ".
3733 "title=\"$link_attr{'-title'}\" ".
3734 "href=\"$link_attr{'-href'}\" ".
3735 "type=\"$link_attr{'-type'}\" ".
3738 $href_params{'extra_options'} = '--no-merges';
3739 $link_attr{'-href'} = href
(%href_params);
3740 $link_attr{'-title'} .= ' (no merges)';
3742 "rel=\"$link_attr{'-rel'}\" ".
3743 "title=\"$link_attr{'-title'}\" ".
3744 "href=\"$link_attr{'-href'}\" ".
3745 "type=\"$link_attr{'-type'}\" ".
3750 printf('<link rel="alternate" title="%s projects list" '.
3751 'href="%s" type="text/plain; charset=utf-8" />'."\n",
3752 esc_attr
($site_name), href
(project
=>undef, action
=>"project_index"));
3753 printf('<link rel="alternate" title="%s projects feeds" '.
3754 'href="%s" type="text/x-opml" />'."\n",
3755 esc_attr
($site_name), href
(project
=>undef, action
=>"opml"));
3759 sub print_header_links
{
3762 # print out each stylesheet that exist, providing backwards capability
3763 # for those people who defined $stylesheet in a config file
3764 if (defined $stylesheet) {
3765 print '<link rel="stylesheet" type="text/css" href="'.esc_url
($stylesheet).'"/>'."\n";
3767 foreach my $stylesheet (@stylesheets) {
3768 next unless $stylesheet;
3769 print '<link rel="stylesheet" type="text/css" href="'.esc_url
($stylesheet).'"/>'."\n";
3773 if ($status eq '200 OK');
3774 if (defined $favicon) {
3775 print qq(<link rel="shortcut icon" href=").esc_url
($favicon).qq(" type="image/png" />\n);
3779 sub print_nav_breadcrumbs
{
3782 print $cgi->a({-href
=> esc_url
($home_link)}, $home_link_str) . " / ";
3783 if (defined $project) {
3784 print $cgi->a({-href
=> href
(action
=>"summary")}, esc_html
($project));
3785 if (defined $action) {
3786 my $action_print = $action ;
3787 if (defined $opts{-action_extra
}) {
3788 $action_print = $cgi->a({-href
=> href
(action
=>$action)},
3791 print " / $action_print";
3793 if (defined $opts{-action_extra
}) {
3794 print " / $opts{-action_extra}";
3800 sub print_search_form
{
3801 if (!defined $searchtext) {
3805 if (defined $hash_base) {
3806 $search_hash = $hash_base;
3807 } elsif (defined $hash) {
3808 $search_hash = $hash;
3810 $search_hash = "HEAD";
3812 my $action = $my_uri;
3813 my $use_pathinfo = gitweb_check_feature
('pathinfo');
3814 if ($use_pathinfo) {
3815 $action .= "/".esc_url
($project);
3817 print $cgi->startform(-method => "get", -action
=> $action) .
3818 "<div class=\"search\">\n" .
3820 $cgi->input({-name
=>"p", -value
=>$project, -type
=>"hidden"}) . "\n") .
3821 $cgi->input({-name
=>"a", -value
=>"search", -type
=>"hidden"}) . "\n" .
3822 $cgi->input({-name
=>"h", -value
=>$search_hash, -type
=>"hidden"}) . "\n" .
3823 $cgi->popup_menu(-name
=> 'st', -default => 'commit',
3824 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
3825 $cgi->sup($cgi->a({-href
=> href
(action
=>"search_help")}, "?")) .
3827 $cgi->textfield(-name
=> "s", -value
=> $searchtext) . "\n" .
3828 "<span title=\"Extended regular expression\">" .
3829 $cgi->checkbox(-name
=> 'sr', -value
=> 1, -label
=> 're',
3830 -checked
=> $search_use_regexp) .
3833 $cgi->end_form() . "\n";
3836 sub git_header_html
{
3837 my $status = shift || "200 OK";
3838 my $expires = shift;
3841 my $title = get_page_title
();
3842 my $content_type = get_content_type_html
();
3843 print $cgi->header(-type
=>$content_type, -charset
=> 'utf-8',
3844 -status
=> $status, -expires
=> $expires)
3845 unless ($opts{'-no_http_header'});
3846 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
3848 <?xml version="1.0" encoding="utf-8"?>
3849 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3850 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
3851 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
3852 <!-- git core binaries version $git_version -->
3854 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
3855 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
3856 <meta name="robots" content="index, nofollow"/>
3857 <title>$title</title>
3859 # the stylesheet, favicon etc urls won't work correctly with path_info
3860 # unless we set the appropriate base URL
3861 if ($ENV{'PATH_INFO'}) {
3862 print "<base href=\"".esc_url
($base_url)."\" />\n";
3864 print_header_links
($status);
3868 if (defined $site_header && -f
$site_header) {
3869 insert_file
($site_header);
3872 print "<div class=\"page_header\">\n";
3873 if (defined $logo) {
3874 print $cgi->a({-href
=> esc_url
($logo_url),
3875 -title
=> $logo_label},
3876 $cgi->img({-src
=> esc_url
($logo),
3877 -width
=> 72, -height
=> 27,
3879 -class => "logo"}));
3881 print_nav_breadcrumbs
(%opts);
3884 my $have_search = gitweb_check_feature
('search');
3885 if (defined $project && $have_search) {
3886 print_search_form
();
3890 sub git_footer_html
{
3891 my $feed_class = 'rss_logo';
3893 print "<div class=\"page_footer\">\n";
3894 if (defined $project) {
3895 my $descr = git_get_project_description
($project);
3896 if (defined $descr) {
3897 print "<div class=\"page_footer_text\">" . esc_html
($descr) . "</div>\n";
3900 my %href_params = get_feed_info
();
3901 if (!%href_params) {
3902 $feed_class .= ' generic';
3904 $href_params{'-title'} ||= 'log';
3906 foreach my $format (qw(RSS Atom)) {
3907 $href_params{'action'} = lc($format);
3908 print $cgi->a({-href
=> href
(%href_params),
3909 -title
=> "$href_params{'-title'} $format feed",
3910 -class => $feed_class}, $format)."\n";
3914 print $cgi->a({-href
=> href
(project
=>undef, action
=>"opml"),
3915 -class => $feed_class}, "OPML") . " ";
3916 print $cgi->a({-href
=> href
(project
=>undef, action
=>"project_index"),
3917 -class => $feed_class}, "TXT") . "\n";
3919 print "</div>\n"; # class="page_footer"
3921 if (defined $t0 && gitweb_check_feature
('timed')) {
3922 print "<div id=\"generating_info\">\n";
3923 print 'This page took '.
3924 '<span id="generating_time" class="time_span">'.
3925 tv_interval
($t0, [ gettimeofday
() ]).
3928 '<span id="generating_cmd">'.
3929 $number_of_git_cmds.
3930 '</span> git commands '.
3932 print "</div>\n"; # class="page_footer"
3935 if (defined $site_footer && -f
$site_footer) {
3936 insert_file
($site_footer);
3939 print qq
!<script type
="text/javascript" src
="!.esc_url($javascript).qq!"></script
>\n!;
3940 if (defined $action &&
3941 $action eq 'blame_incremental') {
3942 print qq
!<script type
="text/javascript">\n!.
3943 qq
!startBlame
("!. href(action=>"blame_data
", -replay=>1) .qq!",\n!.
3944 qq
! "!. href() .qq!");\n!.
3947 my ($jstimezone, $tz_cookie, $datetime_class) =
3948 gitweb_get_feature
('javascript-timezone');
3950 print qq
!<script type
="text/javascript">\n!.
3951 qq
!window
.onload
= function
() {\n!;
3952 if (gitweb_check_feature
('javascript-actions')) {
3953 print qq
! fixLinks
();\n!;
3955 if ($jstimezone && $tz_cookie && $datetime_class) {
3956 print qq
! var tz_cookie
= { name
: '$tz_cookie', expires
: 14, path
: '/' };\n!. # in days
3957 qq
! onloadTZSetup
('$jstimezone', tz_cookie
, '$datetime_class');\n!;
3967 # die_error(<http_status_code>, <error_message>[, <detailed_html_description>])
3968 # Example: die_error(404, 'Hash not found')
3969 # By convention, use the following status codes (as defined in RFC 2616):
3970 # 400: Invalid or missing CGI parameters, or
3971 # requested object exists but has wrong type.
3972 # 403: Requested feature (like "pickaxe" or "snapshot") not enabled on
3973 # this server or project.
3974 # 404: Requested object/revision/project doesn't exist.
3975 # 500: The server isn't configured properly, or
3976 # an internal error occurred (e.g. failed assertions caused by bugs), or
3977 # an unknown error occurred (e.g. the git binary died unexpectedly).
3978 # 503: The server is currently unavailable (because it is overloaded,
3979 # or down for maintenance). Generally, this is a temporary state.
3981 my $status = shift || 500;
3982 my $error = esc_html
(shift) || "Internal Server Error";
3986 my %http_responses = (
3987 400 => '400 Bad Request',
3988 403 => '403 Forbidden',
3989 404 => '404 Not Found',
3990 500 => '500 Internal Server Error',
3991 503 => '503 Service Unavailable',
3993 git_header_html
($http_responses{$status}, undef, %opts);
3995 <div class="page_body">
4000 if (defined $extra) {
4008 unless ($opts{'-error_handler'});
4011 ## ----------------------------------------------------------------------
4012 ## functions printing or outputting HTML: navigation
4014 sub git_print_page_nav
{
4015 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
4016 $extra = '' if !defined $extra; # pager or formats
4018 my @navs = qw(summary shortlog log commit commitdiff tree);
4020 @navs = grep { $_ ne $suppress } @navs;
4023 my %arg = map { $_ => {action
=>$_} } @navs;
4024 if (defined $head) {
4025 for (qw(commit commitdiff)) {
4026 $arg{$_}{'hash'} = $head;
4028 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
4029 for (qw(shortlog log)) {
4030 $arg{$_}{'hash'} = $head;
4035 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
4036 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
4038 my @actions = gitweb_get_feature
('actions');
4041 'n' => $project, # project name
4042 'f' => $git_dir, # project path within filesystem
4043 'h' => $treehead || '', # current hash ('h' parameter)
4044 'b' => $treebase || '', # hash base ('hb' parameter)
4047 my ($label, $link, $pos) = splice(@actions,0,3);
4049 @navs = map { $_ eq $pos ? ($_, $label) : $_ } @navs;
4051 $link =~ s/%([%nfhb])/$repl{$1}/g;
4052 $arg{$label}{'_href'} = $link;
4055 print "<div class=\"page_nav\">\n" .
4057 map { $_ eq $current ?
4058 $_ : $cgi->a({-href
=> ($arg{$_}{_href
} ? $arg{$_}{_href
} : href
(%{$arg{$_}}))}, "$_")
4060 print "<br/>\n$extra<br/>\n" .
4064 # returns a submenu for the nagivation of the refs views (tags, heads,
4065 # remotes) with the current view disabled and the remotes view only
4066 # available if the feature is enabled
4067 sub format_ref_views
{
4069 my @ref_views = qw{tags heads};
4070 push @ref_views, 'remotes' if gitweb_check_feature
('remote_heads');
4071 return join " | ", map {
4072 $_ eq $current ? $_ :
4073 $cgi->a({-href
=> href
(action
=>$_)}, $_)
4077 sub format_paging_nav
{
4078 my ($action, $page, $has_next_link) = @_;
4084 $cgi->a({-href
=> href
(-replay
=>1, page
=>undef)}, "first") .
4086 $cgi->a({-href
=> href
(-replay
=>1, page
=>$page-1),
4087 -accesskey
=> "p", -title
=> "Alt-p"}, "prev");
4089 $paging_nav .= "first ⋅ prev";
4092 if ($has_next_link) {
4093 $paging_nav .= " ⋅ " .
4094 $cgi->a({-href
=> href
(-replay
=>1, page
=>$page+1),
4095 -accesskey
=> "n", -title
=> "Alt-n"}, "next");
4097 $paging_nav .= " ⋅ next";
4103 ## ......................................................................
4104 ## functions printing or outputting HTML: div
4106 sub git_print_header_div
{
4107 my ($action, $title, $hash, $hash_base) = @_;
4110 $args{'action'} = $action;
4111 $args{'hash'} = $hash if $hash;
4112 $args{'hash_base'} = $hash_base if $hash_base;
4114 print "<div class=\"header\">\n" .
4115 $cgi->a({-href
=> href
(%args), -class => "title"},
4116 $title ? $title : $action) .
4120 sub format_repo_url
{
4121 my ($name, $url) = @_;
4122 return "<tr class=\"metadata_url\"><td>$name</td><td>$url</td></tr>\n";
4125 # Group output by placing it in a DIV element and adding a header.
4126 # Options for start_div() can be provided by passing a hash reference as the
4127 # first parameter to the function.
4128 # Options to git_print_header_div() can be provided by passing an array
4129 # reference. This must follow the options to start_div if they are present.
4130 # The content can be a scalar, which is output as-is, a scalar reference, which
4131 # is output after html escaping, an IO handle passed either as *handle or
4132 # *handle{IO}, or a function reference. In the latter case all following
4133 # parameters will be taken as argument to the content function call.
4134 sub git_print_section
{
4135 my ($div_args, $header_args, $content);
4137 if (ref($arg) eq 'HASH') {
4141 if (ref($arg) eq 'ARRAY') {
4142 $header_args = $arg;
4147 print $cgi->start_div($div_args);
4148 git_print_header_div
(@$header_args);
4150 if (ref($content) eq 'CODE') {
4152 } elsif (ref($content) eq 'SCALAR') {
4153 print esc_html
($$content);
4154 } elsif (ref($content) eq 'GLOB' or ref($content) eq 'IO::Handle') {
4156 } elsif (!ref($content) && defined($content)) {
4160 print $cgi->end_div;
4163 sub format_timestamp_html
{
4165 my $strtime = $date->{'rfc2822'};
4167 my (undef, undef, $datetime_class) =
4168 gitweb_get_feature
('javascript-timezone');
4169 if ($datetime_class) {
4170 $strtime = qq
!<span
class="$datetime_class">$strtime</span
>!;
4173 my $localtime_format = '(%02d:%02d %s)';
4174 if ($date->{'hour_local'} < 6) {
4175 $localtime_format = '(<span class="atnight">%02d:%02d</span> %s)';
4178 sprintf($localtime_format,
4179 $date->{'hour_local'}, $date->{'minute_local'}, $date->{'tz_local'});
4184 # Outputs the author name and date in long form
4185 sub git_print_authorship
{
4188 my $tag = $opts{-tag
} || 'div';
4189 my $author = $co->{'author_name'};
4191 my %ad = parse_date
($co->{'author_epoch'}, $co->{'author_tz'});
4192 print "<$tag class=\"author_date\">" .
4193 format_search_author
($author, "author", esc_html
($author)) .
4194 " [".format_timestamp_html
(\
%ad)."]".
4195 git_get_avatar
($co->{'author_email'}, -pad_before
=> 1) .
4199 # Outputs table rows containing the full author or committer information,
4200 # in the format expected for 'commit' view (& similar).
4201 # Parameters are a commit hash reference, followed by the list of people
4202 # to output information for. If the list is empty it defaults to both
4203 # author and committer.
4204 sub git_print_authorship_rows
{
4206 # too bad we can't use @people = @_ || ('author', 'committer')
4208 @people = ('author', 'committer') unless @people;
4209 foreach my $who (@people) {
4210 my %wd = parse_date
($co->{"${who}_epoch"}, $co->{"${who}_tz"});
4211 print "<tr><td>$who</td><td>" .
4212 format_search_author
($co->{"${who}_name"}, $who,
4213 esc_html
($co->{"${who}_name"})) . " " .
4214 format_search_author
($co->{"${who}_email"}, $who,
4215 esc_html
("<" . $co->{"${who}_email"} . ">")) .
4216 "</td><td rowspan=\"2\">" .
4217 git_get_avatar
($co->{"${who}_email"}, -size
=> 'double') .
4221 format_timestamp_html
(\
%wd) .
4227 sub git_print_page_path
{
4233 print "<div class=\"page_path\">";
4234 print $cgi->a({-href
=> href
(action
=>"tree", hash_base
=>$hb),
4235 -title
=> 'tree root'}, to_utf8
("[$project]"));
4237 if (defined $name) {
4238 my @dirname = split '/', $name;
4239 my $basename = pop @dirname;
4242 foreach my $dir (@dirname) {
4243 $fullname .= ($fullname ? '/' : '') . $dir;
4244 print $cgi->a({-href
=> href
(action
=>"tree", file_name
=>$fullname,
4246 -title
=> $fullname}, esc_path
($dir));
4249 if (defined $type && $type eq 'blob') {
4250 print $cgi->a({-href
=> href
(action
=>"blob_plain", file_name
=>$file_name,
4252 -title
=> $name}, esc_path
($basename));
4253 } elsif (defined $type && $type eq 'tree') {
4254 print $cgi->a({-href
=> href
(action
=>"tree", file_name
=>$file_name,
4256 -title
=> $name}, esc_path
($basename));
4259 print esc_path
($basename);
4262 print "<br/></div>\n";
4269 if ($opts{'-remove_title'}) {
4270 # remove title, i.e. first line of log
4273 # remove leading empty lines
4274 while (defined $log->[0] && $log->[0] eq "") {
4281 foreach my $line (@$log) {
4282 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
4285 if (! $opts{'-remove_signoff'}) {
4286 print "<span class=\"signoff\">" . esc_html
($line) . "</span><br/>\n";
4289 # remove signoff lines
4296 # print only one empty line
4297 # do not print empty line after signoff
4299 next if ($empty || $signoff);
4305 print format_log_line_html
($line) . "<br/>\n";
4308 if ($opts{'-final_empty_line'}) {
4309 # end with single empty line
4310 print "<br/>\n" unless $empty;
4314 # return link target (what link points to)
4315 sub git_get_link_target
{
4320 open my $fd, "-|", git_cmd
(), "cat-file", "blob", $hash
4324 $link_target = <$fd>;
4329 return $link_target;
4332 # given link target, and the directory (basedir) the link is in,
4333 # return target of link relative to top directory (top tree);
4334 # return undef if it is not possible (including absolute links).
4335 sub normalize_link_target
{
4336 my ($link_target, $basedir) = @_;
4338 # absolute symlinks (beginning with '/') cannot be normalized
4339 return if (substr($link_target, 0, 1) eq '/');
4341 # normalize link target to path from top (root) tree (dir)
4344 $path = $basedir . '/' . $link_target;
4346 # we are in top (root) tree (dir)
4347 $path = $link_target;
4350 # remove //, /./, and /../
4352 foreach my $part (split('/', $path)) {
4353 # discard '.' and ''
4354 next if (!$part || $part eq '.');
4356 if ($part eq '..') {
4360 # link leads outside repository (outside top dir)
4364 push @path_parts, $part;
4367 $path = join('/', @path_parts);
4372 # print tree entry (row of git_tree), but without encompassing <tr> element
4373 sub git_print_tree_entry
{
4374 my ($t, $basedir, $hash_base, $have_blame) = @_;
4377 $base_key{'hash_base'} = $hash_base if defined $hash_base;
4379 # The format of a table row is: mode list link. Where mode is
4380 # the mode of the entry, list is the name of the entry, an href,
4381 # and link is the action links of the entry.
4383 print "<td class=\"mode\">" . mode_str
($t->{'mode'}) . "</td>\n";
4384 if (exists $t->{'size'}) {
4385 print "<td class=\"size\">$t->{'size'}</td>\n";
4387 if ($t->{'type'} eq "blob") {
4388 print "<td class=\"list\">" .
4389 $cgi->a({-href
=> href
(action
=>"blob", hash
=>$t->{'hash'},
4390 file_name
=>"$basedir$t->{'name'}", %base_key),
4391 -class => "list"}, esc_path
($t->{'name'}));
4392 if (S_ISLNK
(oct $t->{'mode'})) {
4393 my $link_target = git_get_link_target
($t->{'hash'});
4395 my $norm_target = normalize_link_target
($link_target, $basedir);
4396 if (defined $norm_target) {
4398 $cgi->a({-href
=> href
(action
=>"object", hash_base
=>$hash_base,
4399 file_name
=>$norm_target),
4400 -title
=> $norm_target}, esc_path
($link_target));
4402 print " -> " . esc_path
($link_target);
4407 print "<td class=\"link\">";
4408 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$t->{'hash'},
4409 file_name
=>"$basedir$t->{'name'}", %base_key)},
4413 $cgi->a({-href
=> href
(action
=>"blame", hash
=>$t->{'hash'},
4414 file_name
=>"$basedir$t->{'name'}", %base_key)},
4417 if (defined $hash_base) {
4419 $cgi->a({-href
=> href
(action
=>"history", hash_base
=>$hash_base,
4420 hash
=>$t->{'hash'}, file_name
=>"$basedir$t->{'name'}")},
4424 $cgi->a({-href
=> href
(action
=>"blob_plain", hash_base
=>$hash_base,
4425 file_name
=>"$basedir$t->{'name'}")},
4429 } elsif ($t->{'type'} eq "tree") {
4430 print "<td class=\"list\">";
4431 print $cgi->a({-href
=> href
(action
=>"tree", hash
=>$t->{'hash'},
4432 file_name
=>"$basedir$t->{'name'}",
4434 esc_path
($t->{'name'}));
4436 print "<td class=\"link\">";
4437 print $cgi->a({-href
=> href
(action
=>"tree", hash
=>$t->{'hash'},
4438 file_name
=>"$basedir$t->{'name'}",
4441 if (defined $hash_base) {
4443 $cgi->a({-href
=> href
(action
=>"history", hash_base
=>$hash_base,
4444 file_name
=>"$basedir$t->{'name'}")},
4449 # unknown object: we can only present history for it
4450 # (this includes 'commit' object, i.e. submodule support)
4451 print "<td class=\"list\">" .
4452 esc_path
($t->{'name'}) .
4454 print "<td class=\"link\">";
4455 if (defined $hash_base) {
4456 print $cgi->a({-href
=> href
(action
=>"history",
4457 hash_base
=>$hash_base,
4458 file_name
=>"$basedir$t->{'name'}")},
4465 ## ......................................................................
4466 ## functions printing large fragments of HTML
4468 # get pre-image filenames for merge (combined) diff
4469 sub fill_from_file_info
{
4470 my ($diff, @parents) = @_;
4472 $diff->{'from_file'} = [ ];
4473 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
4474 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
4475 if ($diff->{'status'}[$i] eq 'R' ||
4476 $diff->{'status'}[$i] eq 'C') {
4477 $diff->{'from_file'}[$i] =
4478 git_get_path_by_hash
($parents[$i], $diff->{'from_id'}[$i]);
4485 # is current raw difftree line of file deletion
4487 my $diffinfo = shift;
4489 return $diffinfo->{'to_id'} eq ('0' x
40);
4492 # does patch correspond to [previous] difftree raw line
4493 # $diffinfo - hashref of parsed raw diff format
4494 # $patchinfo - hashref of parsed patch diff format
4495 # (the same keys as in $diffinfo)
4496 sub is_patch_split
{
4497 my ($diffinfo, $patchinfo) = @_;
4499 return defined $diffinfo && defined $patchinfo
4500 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
4504 sub git_difftree_body
{
4505 my ($difftree, $hash, @parents) = @_;
4506 my ($parent) = $parents[0];
4507 my $have_blame = gitweb_check_feature
('blame');
4508 print "<div class=\"list_head\">\n";
4509 if ($#{$difftree} > 10) {
4510 print(($#{$difftree} + 1) . " files changed:\n");
4514 print "<table class=\"" .
4515 (@parents > 1 ? "combined " : "") .
4518 # header only for combined diff in 'commitdiff' view
4519 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
4522 print "<thead><tr>\n" .
4523 "<th></th><th></th>\n"; # filename, patchN link
4524 for (my $i = 0; $i < @parents; $i++) {
4525 my $par = $parents[$i];
4527 $cgi->a({-href
=> href
(action
=>"commitdiff",
4528 hash
=>$hash, hash_parent
=>$par),
4529 -title
=> 'commitdiff to parent number ' .
4530 ($i+1) . ': ' . substr($par,0,7)},
4534 print "</tr></thead>\n<tbody>\n";
4539 foreach my $line (@{$difftree}) {
4540 my $diff = parsed_difftree_line
($line);
4543 print "<tr class=\"dark\">\n";
4545 print "<tr class=\"light\">\n";
4549 if (exists $diff->{'nparents'}) { # combined diff
4551 fill_from_file_info
($diff, @parents)
4552 unless exists $diff->{'from_file'};
4554 if (!is_deleted
($diff)) {
4555 # file exists in the result (child) commit
4557 $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'to_id'},
4558 file_name
=>$diff->{'to_file'},
4560 -class => "list"}, esc_path
($diff->{'to_file'})) .
4564 esc_path
($diff->{'to_file'}) .
4568 if ($action eq 'commitdiff') {
4571 print "<td class=\"link\">" .
4572 $cgi->a({-href
=> href
(-anchor
=>"patch$patchno")},
4578 my $has_history = 0;
4579 my $not_deleted = 0;
4580 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
4581 my $hash_parent = $parents[$i];
4582 my $from_hash = $diff->{'from_id'}[$i];
4583 my $from_path = $diff->{'from_file'}[$i];
4584 my $status = $diff->{'status'}[$i];
4586 $has_history ||= ($status ne 'A');
4587 $not_deleted ||= ($status ne 'D');
4589 if ($status eq 'A') {
4590 print "<td class=\"link\" align=\"right\"> | </td>\n";
4591 } elsif ($status eq 'D') {
4592 print "<td class=\"link\">" .
4593 $cgi->a({-href
=> href
(action
=>"blob",
4596 file_name
=>$from_path)},
4600 if ($diff->{'to_id'} eq $from_hash) {
4601 print "<td class=\"link nochange\">";
4603 print "<td class=\"link\">";
4605 print $cgi->a({-href
=> href
(action
=>"blobdiff",
4606 hash
=>$diff->{'to_id'},
4607 hash_parent
=>$from_hash,
4609 hash_parent_base
=>$hash_parent,
4610 file_name
=>$diff->{'to_file'},
4611 file_parent
=>$from_path)},
4617 print "<td class=\"link\">";
4619 print $cgi->a({-href
=> href
(action
=>"blob",
4620 hash
=>$diff->{'to_id'},
4621 file_name
=>$diff->{'to_file'},
4624 print " | " if ($has_history);
4627 print $cgi->a({-href
=> href
(action
=>"history",
4628 file_name
=>$diff->{'to_file'},
4635 next; # instead of 'else' clause, to avoid extra indent
4637 # else ordinary diff
4639 my ($to_mode_oct, $to_mode_str, $to_file_type);
4640 my ($from_mode_oct, $from_mode_str, $from_file_type);
4641 if ($diff->{'to_mode'} ne ('0' x
6)) {
4642 $to_mode_oct = oct $diff->{'to_mode'};
4643 if (S_ISREG
($to_mode_oct)) { # only for regular file
4644 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
4646 $to_file_type = file_type
($diff->{'to_mode'});
4648 if ($diff->{'from_mode'} ne ('0' x
6)) {
4649 $from_mode_oct = oct $diff->{'from_mode'};
4650 if (S_ISREG
($from_mode_oct)) { # only for regular file
4651 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
4653 $from_file_type = file_type
($diff->{'from_mode'});
4656 if ($diff->{'status'} eq "A") { # created
4657 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
4658 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
4659 $mode_chng .= "]</span>";
4661 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'to_id'},
4662 hash_base
=>$hash, file_name
=>$diff->{'file'}),
4663 -class => "list"}, esc_path
($diff->{'file'}));
4665 print "<td>$mode_chng</td>\n";
4666 print "<td class=\"link\">";
4667 if ($action eq 'commitdiff') {
4670 print $cgi->a({-href
=> href
(-anchor
=>"patch$patchno")},
4674 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'to_id'},
4675 hash_base
=>$hash, file_name
=>$diff->{'file'})},
4679 } elsif ($diff->{'status'} eq "D") { # deleted
4680 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
4682 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'from_id'},
4683 hash_base
=>$parent, file_name
=>$diff->{'file'}),
4684 -class => "list"}, esc_path
($diff->{'file'}));
4686 print "<td>$mode_chng</td>\n";
4687 print "<td class=\"link\">";
4688 if ($action eq 'commitdiff') {
4691 print $cgi->a({-href
=> href
(-anchor
=>"patch$patchno")},
4695 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'from_id'},
4696 hash_base
=>$parent, file_name
=>$diff->{'file'})},
4699 print $cgi->a({-href
=> href
(action
=>"blame", hash_base
=>$parent,
4700 file_name
=>$diff->{'file'})},
4703 print $cgi->a({-href
=> href
(action
=>"history", hash_base
=>$parent,
4704 file_name
=>$diff->{'file'})},
4708 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
4709 my $mode_chnge = "";
4710 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
4711 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
4712 if ($from_file_type ne $to_file_type) {
4713 $mode_chnge .= " from $from_file_type to $to_file_type";
4715 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
4716 if ($from_mode_str && $to_mode_str) {
4717 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
4718 } elsif ($to_mode_str) {
4719 $mode_chnge .= " mode: $to_mode_str";
4722 $mode_chnge .= "]</span>\n";
4725 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'to_id'},
4726 hash_base
=>$hash, file_name
=>$diff->{'file'}),
4727 -class => "list"}, esc_path
($diff->{'file'}));
4729 print "<td>$mode_chnge</td>\n";
4730 print "<td class=\"link\">";
4731 if ($action eq 'commitdiff') {
4734 print $cgi->a({-href
=> href
(-anchor
=>"patch$patchno")},
4737 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
4738 # "commit" view and modified file (not onlu mode changed)
4739 print $cgi->a({-href
=> href
(action
=>"blobdiff",
4740 hash
=>$diff->{'to_id'}, hash_parent
=>$diff->{'from_id'},
4741 hash_base
=>$hash, hash_parent_base
=>$parent,
4742 file_name
=>$diff->{'file'})},
4746 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'to_id'},
4747 hash_base
=>$hash, file_name
=>$diff->{'file'})},
4750 print $cgi->a({-href
=> href
(action
=>"blame", hash_base
=>$hash,
4751 file_name
=>$diff->{'file'})},
4754 print $cgi->a({-href
=> href
(action
=>"history", hash_base
=>$hash,
4755 file_name
=>$diff->{'file'})},
4759 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
4760 my %status_name = ('R' => 'moved', 'C' => 'copied');
4761 my $nstatus = $status_name{$diff->{'status'}};
4763 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
4764 # mode also for directories, so we cannot use $to_mode_str
4765 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
4768 $cgi->a({-href
=> href
(action
=>"blob", hash_base
=>$hash,
4769 hash
=>$diff->{'to_id'}, file_name
=>$diff->{'to_file'}),
4770 -class => "list"}, esc_path
($diff->{'to_file'})) . "</td>\n" .
4771 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
4772 $cgi->a({-href
=> href
(action
=>"blob", hash_base
=>$parent,
4773 hash
=>$diff->{'from_id'}, file_name
=>$diff->{'from_file'}),
4774 -class => "list"}, esc_path
($diff->{'from_file'})) .
4775 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
4776 "<td class=\"link\">";
4777 if ($action eq 'commitdiff') {
4780 print $cgi->a({-href
=> href
(-anchor
=>"patch$patchno")},
4783 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
4784 # "commit" view and modified file (not only pure rename or copy)
4785 print $cgi->a({-href
=> href
(action
=>"blobdiff",
4786 hash
=>$diff->{'to_id'}, hash_parent
=>$diff->{'from_id'},
4787 hash_base
=>$hash, hash_parent_base
=>$parent,
4788 file_name
=>$diff->{'to_file'}, file_parent
=>$diff->{'from_file'})},
4792 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'to_id'},
4793 hash_base
=>$parent, file_name
=>$diff->{'to_file'})},
4796 print $cgi->a({-href
=> href
(action
=>"blame", hash_base
=>$hash,
4797 file_name
=>$diff->{'to_file'})},
4800 print $cgi->a({-href
=> href
(action
=>"history", hash_base
=>$hash,
4801 file_name
=>$diff->{'to_file'})},
4805 } # we should not encounter Unmerged (U) or Unknown (X) status
4808 print "</tbody>" if $has_header;
4812 sub git_patchset_body
{
4813 my ($fd, $difftree, $hash, @hash_parents) = @_;
4814 my ($hash_parent) = $hash_parents[0];
4816 my $is_combined = (@hash_parents > 1);
4818 my $patch_number = 0;
4824 print "<div class=\"patchset\">\n";
4826 # skip to first patch
4827 while ($patch_line = <$fd>) {
4830 last if ($patch_line =~ m/^diff /);
4834 while ($patch_line) {
4836 # parse "git diff" header line
4837 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
4838 # $1 is from_name, which we do not use
4839 $to_name = unquote
($2);
4840 $to_name =~ s!^b/!!;
4841 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
4842 # $1 is 'cc' or 'combined', which we do not use
4843 $to_name = unquote
($2);
4848 # check if current patch belong to current raw line
4849 # and parse raw git-diff line if needed
4850 if (is_patch_split
($diffinfo, { 'to_file' => $to_name })) {
4851 # this is continuation of a split patch
4852 print "<div class=\"patch cont\">\n";
4854 # advance raw git-diff output if needed
4855 $patch_idx++ if defined $diffinfo;
4857 # read and prepare patch information
4858 $diffinfo = parsed_difftree_line
($difftree->[$patch_idx]);
4860 # compact combined diff output can have some patches skipped
4861 # find which patch (using pathname of result) we are at now;
4863 while ($to_name ne $diffinfo->{'to_file'}) {
4864 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
4865 format_diff_cc_simplified
($diffinfo, @hash_parents) .
4866 "</div>\n"; # class="patch"
4871 last if $patch_idx > $#$difftree;
4872 $diffinfo = parsed_difftree_line
($difftree->[$patch_idx]);
4876 # modifies %from, %to hashes
4877 parse_from_to_diffinfo
($diffinfo, \
%from, \
%to, @hash_parents);
4879 # this is first patch for raw difftree line with $patch_idx index
4880 # we index @$difftree array from 0, but number patches from 1
4881 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
4885 #assert($patch_line =~ m/^diff /) if DEBUG;
4886 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
4888 # print "git diff" header
4889 print format_git_diff_header_line
($patch_line, $diffinfo,
4892 # print extended diff header
4893 print "<div class=\"diff extended_header\">\n";
4895 while ($patch_line = <$fd>) {
4898 last EXTENDED_HEADER
if ($patch_line =~ m/^--- |^diff /);
4900 print format_extended_diff_header_line
($patch_line, $diffinfo,
4903 print "</div>\n"; # class="diff extended_header"
4905 # from-file/to-file diff header
4906 if (! $patch_line) {
4907 print "</div>\n"; # class="patch"
4910 next PATCH
if ($patch_line =~ m/^diff /);
4911 #assert($patch_line =~ m/^---/) if DEBUG;
4913 my $last_patch_line = $patch_line;
4914 $patch_line = <$fd>;
4916 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
4918 print format_diff_from_to_header
($last_patch_line, $patch_line,
4919 $diffinfo, \
%from, \
%to,
4924 while ($patch_line = <$fd>) {
4927 next PATCH
if ($patch_line =~ m/^diff /);
4929 print format_diff_line
($patch_line, \
%from, \
%to);
4933 print "</div>\n"; # class="patch"
4936 # for compact combined (--cc) format, with chunk and patch simplification
4937 # the patchset might be empty, but there might be unprocessed raw lines
4938 for (++$patch_idx if $patch_number > 0;
4939 $patch_idx < @$difftree;
4941 # read and prepare patch information
4942 $diffinfo = parsed_difftree_line
($difftree->[$patch_idx]);
4944 # generate anchor for "patch" links in difftree / whatchanged part
4945 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
4946 format_diff_cc_simplified
($diffinfo, @hash_parents) .
4947 "</div>\n"; # class="patch"
4952 if ($patch_number == 0) {
4953 if (@hash_parents > 1) {
4954 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
4956 print "<div class=\"diff nodifferences\">No differences found</div>\n";
4960 print "</div>\n"; # class="patchset"
4963 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4965 # fills project list info (age, description, owner, category, forks)
4966 # for each project in the list, removing invalid projects from
4968 # NOTE: modifies $projlist, but does not remove entries from it
4969 sub fill_project_list_info
{
4970 my $projlist = shift;
4973 my $show_ctags = gitweb_check_feature
('ctags');
4975 foreach my $pr (@$projlist) {
4976 my (@activity) = git_get_last_activity
($pr->{'path'});
4977 unless (@activity) {
4980 ($pr->{'age'}, $pr->{'age_string'}) = @activity;
4981 if (!defined $pr->{'descr'}) {
4982 my $descr = git_get_project_description
($pr->{'path'}) || "";
4983 $descr = to_utf8
($descr);
4984 $pr->{'descr_long'} = $descr;
4985 $pr->{'descr'} = chop_str
($descr, $projects_list_description_width, 5);
4987 if (!defined $pr->{'owner'}) {
4988 $pr->{'owner'} = git_get_project_owner
("$pr->{'path'}") || "";
4991 $pr->{'ctags'} = git_get_project_ctags
($pr->{'path'});
4993 if ($projects_list_group_categories && !defined $pr->{'category'}) {
4994 my $cat = git_get_project_category
($pr->{'path'}) ||
4995 $project_list_default_category;
4996 $pr->{'category'} = to_utf8
($cat);
4999 push @projects, $pr;
5005 sub sort_projects_list
{
5006 my ($projlist, $order) = @_;
5010 project
=> { key
=> 'path', type
=> 'str' },
5011 descr
=> { key
=> 'descr_long', type
=> 'str' },
5012 owner
=> { key
=> 'owner', type
=> 'str' },
5013 age
=> { key
=> 'age', type
=> 'num' }
5015 my $oi = $order_info{$order};
5016 return @$projlist unless defined $oi;
5017 if ($oi->{'type'} eq 'str') {
5018 @projects = sort {$a->{$oi->{'key'}} cmp $b->{$oi->{'key'}}} @$projlist;
5020 @projects = sort {$a->{$oi->{'key'}} <=> $b->{$oi->{'key'}}} @$projlist;
5026 # returns a hash of categories, containing the list of project
5027 # belonging to each category
5028 sub build_projlist_by_category
{
5029 my ($projlist, $from, $to) = @_;
5032 $from = 0 unless defined $from;
5033 $to = $#$projlist if (!defined $to || $#$projlist < $to);
5035 for (my $i = $from; $i <= $to; $i++) {
5036 my $pr = $projlist->[$i];
5037 push @{$categories{ $pr->{'category'} }}, $pr;
5040 return wantarray ? %categories : \
%categories;
5043 # print 'sort by' <th> element, generating 'sort by $name' replay link
5044 # if that order is not selected
5046 print format_sort_th
(@_);
5049 sub format_sort_th
{
5050 my ($name, $order, $header) = @_;
5052 $header ||= ucfirst($name);
5054 if ($order eq $name) {
5055 $sort_th .= "<th>$header</th>\n";
5057 $sort_th .= "<th>" .
5058 $cgi->a({-href
=> href
(-replay
=>1, order
=>$name),
5059 -class => "header"}, $header) .
5066 sub git_project_list_rows
{
5067 my ($projlist, $from, $to, $check_forks) = @_;
5069 $from = 0 unless defined $from;
5070 $to = $#$projlist if (!defined $to || $#$projlist < $to);
5073 for (my $i = $from; $i <= $to; $i++) {
5074 my $pr = $projlist->[$i];
5077 print "<tr class=\"dark\">\n";
5079 print "<tr class=\"light\">\n";
5085 if ($pr->{'forks'}) {
5086 my $nforks = scalar @{$pr->{'forks'}};
5088 print $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"forks"),
5089 -title
=> "$nforks forks"}, "+");
5091 print $cgi->span({-title
=> "$nforks forks"}, "+");
5096 print "<td>" . $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"summary"),
5097 -class => "list"}, esc_html
($pr->{'path'})) . "</td>\n" .
5098 "<td>" . $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"summary"),
5099 -class => "list", -title
=> $pr->{'descr_long'}},
5100 esc_html
($pr->{'descr'})) . "</td>\n" .
5101 "<td><i>" . chop_and_escape_str
($pr->{'owner'}, 15) . "</i></td>\n";
5102 print "<td class=\"". age_class
($pr->{'age'}) . "\">" .
5103 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
5104 "<td class=\"link\">" .
5105 $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"summary")}, "summary") . " | " .
5106 $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"shortlog")}, "shortlog") . " | " .
5107 $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"log")}, "log") . " | " .
5108 $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"tree")}, "tree") .
5109 ($pr->{'forks'} ? " | " . $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"forks")}, "forks") : '') .
5115 sub git_project_list_body
{
5116 # actually uses global variable $project
5117 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
5118 my @projects = @$projlist;
5120 my $check_forks = gitweb_check_feature
('forks');
5121 my $show_ctags = gitweb_check_feature
('ctags');
5122 my $tagfilter = $show_ctags ? $cgi->param('by_tag') : undef;
5123 $check_forks = undef
5124 if ($tagfilter || $searchtext);
5126 # filtering out forks before filling info allows to do less work
5127 @projects = filter_forks_from_projects_list
(\
@projects)
5129 @projects = fill_project_list_info
(\
@projects);
5130 # searching projects require filling to be run before it
5131 @projects = search_projects_list
(\
@projects,
5132 'searchtext' => $searchtext,
5133 'tagfilter' => $tagfilter)
5134 if ($tagfilter || $searchtext);
5136 $order ||= $default_projects_order;
5137 $from = 0 unless defined $from;
5138 $to = $#projects if (!defined $to || $#projects < $to);
5143 "<b>No such projects found</b><br />\n".
5144 "Click ".$cgi->a({-href
=>href
(project
=>undef)},"here")." to view all projects<br />\n".
5145 "</center>\n<br />\n";
5149 @projects = sort_projects_list
(\
@projects, $order);
5152 my $ctags = git_gather_all_ctags
(\
@projects);
5153 my $cloud = git_populate_project_tagcloud
($ctags);
5154 print git_show_project_tagcloud
($cloud, 64);
5157 print "<table class=\"project_list\">\n";
5158 unless ($no_header) {
5161 print "<th></th>\n";
5163 print_sort_th
('project', $order, 'Project');
5164 print_sort_th
('descr', $order, 'Description');
5165 print_sort_th
('owner', $order, 'Owner');
5166 print_sort_th
('age', $order, 'Last Change');
5167 print "<th></th>\n" . # for links
5171 if ($projects_list_group_categories) {
5172 # only display categories with projects in the $from-$to window
5173 @projects = sort {$a->{'category'} cmp $b->{'category'}} @projects[$from..$to];
5174 my %categories = build_projlist_by_category
(\
@projects, $from, $to);
5175 foreach my $cat (sort keys %categories) {
5176 unless ($cat eq "") {
5179 print "<td></td>\n";
5181 print "<td class=\"category\" colspan=\"5\">".esc_html
($cat)."</td>\n";
5185 git_project_list_rows
($categories{$cat}, undef, undef, $check_forks);
5188 git_project_list_rows
(\
@projects, $from, $to, $check_forks);
5191 if (defined $extra) {
5194 print "<td></td>\n";
5196 print "<td colspan=\"5\">$extra</td>\n" .
5203 # uses global variable $project
5204 my ($commitlist, $from, $to, $refs, $extra) = @_;
5206 $from = 0 unless defined $from;
5207 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
5209 for (my $i = 0; $i <= $to; $i++) {
5210 my %co = %{$commitlist->[$i]};
5212 my $commit = $co{'id'};
5213 my $ref = format_ref_marker
($refs, $commit);
5214 git_print_header_div
('commit',
5215 "<span class=\"age\">$co{'age_string'}</span>" .
5216 esc_html
($co{'title'}) . $ref,
5218 print "<div class=\"title_text\">\n" .
5219 "<div class=\"log_link\">\n" .
5220 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$commit)}, "commit") .
5222 $cgi->a({-href
=> href
(action
=>"commitdiff", hash
=>$commit)}, "commitdiff") .
5224 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$commit, hash_base
=>$commit)}, "tree") .
5227 git_print_authorship
(\
%co, -tag
=> 'span');
5228 print "<br/>\n</div>\n";
5230 print "<div class=\"log_body\">\n";
5231 git_print_log
($co{'comment'}, -final_empty_line
=> 1);
5235 print "<div class=\"page_nav\">\n";
5241 sub git_shortlog_body
{
5242 # uses global variable $project
5243 my ($commitlist, $from, $to, $refs, $extra) = @_;
5245 $from = 0 unless defined $from;
5246 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
5248 print "<table class=\"shortlog\">\n";
5250 for (my $i = $from; $i <= $to; $i++) {
5251 my %co = %{$commitlist->[$i]};
5252 my $commit = $co{'id'};
5253 my $ref = format_ref_marker
($refs, $commit);
5255 print "<tr class=\"dark\">\n";
5257 print "<tr class=\"light\">\n";
5260 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
5261 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5262 format_author_html
('td', \
%co, 10) . "<td>";
5263 print format_subject_html
($co{'title'}, $co{'title_short'},
5264 href
(action
=>"commit", hash
=>$commit), $ref);
5266 "<td class=\"link\">" .
5267 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$commit)}, "commit") . " | " .
5268 $cgi->a({-href
=> href
(action
=>"commitdiff", hash
=>$commit)}, "commitdiff") . " | " .
5269 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$commit, hash_base
=>$commit)}, "tree");
5270 my $snapshot_links = format_snapshot_links
($commit);
5271 if (defined $snapshot_links) {
5272 print " | " . $snapshot_links;
5277 if (defined $extra) {
5279 "<td colspan=\"4\">$extra</td>\n" .
5285 sub git_history_body
{
5286 # Warning: assumes constant type (blob or tree) during history
5287 my ($commitlist, $from, $to, $refs, $extra,
5288 $file_name, $file_hash, $ftype) = @_;
5290 $from = 0 unless defined $from;
5291 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
5293 print "<table class=\"history\">\n";
5295 for (my $i = $from; $i <= $to; $i++) {
5296 my %co = %{$commitlist->[$i]};
5300 my $commit = $co{'id'};
5302 my $ref = format_ref_marker
($refs, $commit);
5305 print "<tr class=\"dark\">\n";
5307 print "<tr class=\"light\">\n";
5310 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5311 # shortlog: format_author_html('td', \%co, 10)
5312 format_author_html
('td', \
%co, 15, 3) . "<td>";
5313 # originally git_history used chop_str($co{'title'}, 50)
5314 print format_subject_html
($co{'title'}, $co{'title_short'},
5315 href
(action
=>"commit", hash
=>$commit), $ref);
5317 "<td class=\"link\">" .
5318 $cgi->a({-href
=> href
(action
=>$ftype, hash_base
=>$commit, file_name
=>$file_name)}, $ftype) . " | " .
5319 $cgi->a({-href
=> href
(action
=>"commitdiff", hash
=>$commit)}, "commitdiff");
5321 if ($ftype eq 'blob') {
5322 my $blob_current = $file_hash;
5323 my $blob_parent = git_get_hash_by_path
($commit, $file_name);
5324 if (defined $blob_current && defined $blob_parent &&
5325 $blob_current ne $blob_parent) {
5327 $cgi->a({-href
=> href
(action
=>"blobdiff",
5328 hash
=>$blob_current, hash_parent
=>$blob_parent,
5329 hash_base
=>$hash_base, hash_parent_base
=>$commit,
5330 file_name
=>$file_name)},
5337 if (defined $extra) {
5339 "<td colspan=\"4\">$extra</td>\n" .
5346 # uses global variable $project
5347 my ($taglist, $from, $to, $extra) = @_;
5348 $from = 0 unless defined $from;
5349 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
5351 print "<table class=\"tags\">\n";
5353 for (my $i = $from; $i <= $to; $i++) {
5354 my $entry = $taglist->[$i];
5356 my $comment = $tag{'subject'};
5358 if (defined $comment) {
5359 $comment_short = chop_str
($comment, 30, 5);
5362 print "<tr class=\"dark\">\n";
5364 print "<tr class=\"light\">\n";
5367 if (defined $tag{'age'}) {
5368 print "<td><i>$tag{'age'}</i></td>\n";
5370 print "<td></td>\n";
5373 $cgi->a({-href
=> href
(action
=>$tag{'reftype'}, hash
=>$tag{'refid'}),
5374 -class => "list name"}, esc_html
($tag{'name'})) .
5377 if (defined $comment) {
5378 print format_subject_html
($comment, $comment_short,
5379 href
(action
=>"tag", hash
=>$tag{'id'}));
5382 "<td class=\"selflink\">";
5383 if ($tag{'type'} eq "tag") {
5384 print $cgi->a({-href
=> href
(action
=>"tag", hash
=>$tag{'id'})}, "tag");
5389 "<td class=\"link\">" . " | " .
5390 $cgi->a({-href
=> href
(action
=>$tag{'reftype'}, hash
=>$tag{'refid'})}, $tag{'reftype'});
5391 if ($tag{'reftype'} eq "commit") {
5392 print " | " . $cgi->a({-href
=> href
(action
=>"shortlog", hash
=>$tag{'fullname'})}, "shortlog") .
5393 " | " . $cgi->a({-href
=> href
(action
=>"log", hash
=>$tag{'fullname'})}, "log");
5394 } elsif ($tag{'reftype'} eq "blob") {
5395 print " | " . $cgi->a({-href
=> href
(action
=>"blob_plain", hash
=>$tag{'refid'})}, "raw");
5400 if (defined $extra) {
5402 "<td colspan=\"5\">$extra</td>\n" .
5408 sub git_heads_body
{
5409 # uses global variable $project
5410 my ($headlist, $head, $from, $to, $extra) = @_;
5411 $from = 0 unless defined $from;
5412 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
5414 print "<table class=\"heads\">\n";
5416 for (my $i = $from; $i <= $to; $i++) {
5417 my $entry = $headlist->[$i];
5419 my $curr = $ref{'id'} eq $head;
5421 print "<tr class=\"dark\">\n";
5423 print "<tr class=\"light\">\n";
5426 print "<td><i>$ref{'age'}</i></td>\n" .
5427 ($curr ? "<td class=\"current_head\">" : "<td>") .
5428 $cgi->a({-href
=> href
(action
=>"shortlog", hash
=>$ref{'fullname'}),
5429 -class => "list name"},esc_html
($ref{'name'})) .
5431 "<td class=\"link\">" .
5432 $cgi->a({-href
=> href
(action
=>"shortlog", hash
=>$ref{'fullname'})}, "shortlog") . " | " .
5433 $cgi->a({-href
=> href
(action
=>"log", hash
=>$ref{'fullname'})}, "log") . " | " .
5434 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$ref{'fullname'}, hash_base
=>$ref{'fullname'})}, "tree") .
5438 if (defined $extra) {
5440 "<td colspan=\"3\">$extra</td>\n" .
5446 # Display a single remote block
5447 sub git_remote_block
{
5448 my ($remote, $rdata, $limit, $head) = @_;
5450 my $heads = $rdata->{'heads'};
5451 my $fetch = $rdata->{'fetch'};
5452 my $push = $rdata->{'push'};
5454 my $urls_table = "<table class=\"projects_list\">\n" ;
5456 if (defined $fetch) {
5457 if ($fetch eq $push) {
5458 $urls_table .= format_repo_url
("URL", $fetch);
5460 $urls_table .= format_repo_url
("Fetch URL", $fetch);
5461 $urls_table .= format_repo_url
("Push URL", $push) if defined $push;
5463 } elsif (defined $push) {
5464 $urls_table .= format_repo_url
("Push URL", $push);
5466 $urls_table .= format_repo_url
("", "No remote URL");
5469 $urls_table .= "</table>\n";
5472 if (defined $limit && $limit < @$heads) {
5473 $dots = $cgi->a({-href
=> href
(action
=>"remotes", hash
=>$remote)}, "...");
5477 git_heads_body
($heads, $head, 0, $limit, $dots);
5480 # Display a list of remote names with the respective fetch and push URLs
5481 sub git_remotes_list
{
5482 my ($remotedata, $limit) = @_;
5483 print "<table class=\"heads\">\n";
5485 my @remotes = sort keys %$remotedata;
5487 my $limited = $limit && $limit < @remotes;
5489 $#remotes = $limit - 1 if $limited;
5491 while (my $remote = shift @remotes) {
5492 my $rdata = $remotedata->{$remote};
5493 my $fetch = $rdata->{'fetch'};
5494 my $push = $rdata->{'push'};
5496 print "<tr class=\"dark\">\n";
5498 print "<tr class=\"light\">\n";
5502 $cgi->a({-href
=> href
(action
=>'remotes', hash
=>$remote),
5503 -class=> "list name"},esc_html
($remote)) .
5505 print "<td class=\"link\">" .
5506 (defined $fetch ? $cgi->a({-href
=> $fetch}, "fetch") : "fetch") .
5508 (defined $push ? $cgi->a({-href
=> $push}, "push") : "push") .
5516 "<td colspan=\"3\">" .
5517 $cgi->a({-href
=> href
(action
=>"remotes")}, "...") .
5518 "</td>\n" . "</tr>\n";
5524 # Display remote heads grouped by remote, unless there are too many
5525 # remotes, in which case we only display the remote names
5526 sub git_remotes_body
{
5527 my ($remotedata, $limit, $head) = @_;
5528 if ($limit and $limit < keys %$remotedata) {
5529 git_remotes_list
($remotedata, $limit);
5531 fill_remote_heads
($remotedata);
5532 while (my ($remote, $rdata) = each %$remotedata) {
5533 git_print_section
({-class=>"remote", -id
=>$remote},
5534 ["remotes", $remote, $remote], sub {
5535 git_remote_block
($remote, $rdata, $limit, $head);
5541 sub git_search_message
{
5545 if ($searchtype eq 'commit') {
5546 $greptype = "--grep=";
5547 } elsif ($searchtype eq 'author') {
5548 $greptype = "--author=";
5549 } elsif ($searchtype eq 'committer') {
5550 $greptype = "--committer=";
5552 $greptype .= $searchtext;
5553 my @commitlist = parse_commits
($hash, 101, (100 * $page), undef,
5554 $greptype, '--regexp-ignore-case',
5555 $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
5557 my $paging_nav = '';
5560 $cgi->a({-href
=> href
(-replay
=>1, page
=>undef)},
5563 $cgi->a({-href
=> href
(-replay
=>1, page
=>$page-1),
5564 -accesskey
=> "p", -title
=> "Alt-p"}, "prev");
5566 $paging_nav .= "first ⋅ prev";
5569 if ($#commitlist >= 100) {
5571 $cgi->a({-href
=> href
(-replay
=>1, page
=>$page+1),
5572 -accesskey
=> "n", -title
=> "Alt-n"}, "next");
5573 $paging_nav .= " ⋅ $next_link";
5575 $paging_nav .= " ⋅ next";
5580 git_print_page_nav
('','', $hash,$co{'tree'},$hash, $paging_nav);
5581 git_print_header_div
('commit', esc_html
($co{'title'}), $hash);
5582 if ($page == 0 && !@commitlist) {
5583 print "<p>No match.</p>\n";
5585 git_search_grep_body
(\
@commitlist, 0, 99, $next_link);
5591 sub git_search_changes
{
5595 open my $fd, '-|', git_cmd
(), '--no-pager', 'log', @diff_opts,
5596 '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
5597 ($search_use_regexp ? '--pickaxe-regex' : ())
5598 or die_error
(500, "Open git-log failed");
5602 git_print_page_nav
('','', $hash,$co{'tree'},$hash);
5603 git_print_header_div
('commit', esc_html
($co{'title'}), $hash);
5605 print "<table class=\"pickaxe search\">\n";
5609 while (my $line = <$fd>) {
5613 my %set = parse_difftree_raw_line
($line);
5614 if (defined $set{'commit'}) {
5615 # finish previous commit
5618 "<td class=\"link\">" .
5619 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$co{'id'})},
5622 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$co{'tree'},
5623 hash_base
=>$co{'id'})},
5630 print "<tr class=\"dark\">\n";
5632 print "<tr class=\"light\">\n";
5635 %co = parse_commit
($set{'commit'});
5636 my $author = chop_and_escape_str
($co{'author_name'}, 15, 5);
5637 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5638 "<td><i>$author</i></td>\n" .
5640 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$co{'id'}),
5641 -class => "list subject"},
5642 chop_and_escape_str
($co{'title'}, 50) . "<br/>");
5643 } elsif (defined $set{'to_id'}) {
5644 next if ($set{'to_id'} =~ m/^0{40}$/);
5646 print $cgi->a({-href
=> href
(action
=>"blob", hash_base
=>$co{'id'},
5647 hash
=>$set{'to_id'}, file_name
=>$set{'to_file'}),
5649 "<span class=\"match\">" . esc_path
($set{'file'}) . "</span>") .
5655 # finish last commit (warning: repetition!)
5658 "<td class=\"link\">" .
5659 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$co{'id'})},
5662 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$co{'tree'},
5663 hash_base
=>$co{'id'})},
5674 sub git_search_files
{
5678 open my $fd, "-|", git_cmd
(), 'grep', '-n',
5679 $search_use_regexp ? ('-E', '-i') : '-F',
5680 $searchtext, $co{'tree'}
5681 or die_error
(500, "Open git-grep failed");
5685 git_print_page_nav
('','', $hash,$co{'tree'},$hash);
5686 git_print_header_div
('commit', esc_html
($co{'title'}), $hash);
5688 print "<table class=\"grep_search\">\n";
5692 while (my $line = <$fd>) {
5694 my ($file, $lno, $ltext, $binary);
5695 last if ($matches++ > 1000);
5696 if ($line =~ /^Binary file (.+) matches$/) {
5700 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
5702 if ($file ne $lastfile) {
5703 $lastfile and print "</td></tr>\n";
5705 print "<tr class=\"dark\">\n";
5707 print "<tr class=\"light\">\n";
5709 print "<td class=\"list\">".
5710 $cgi->a({-href
=> href
(action
=>"blob", hash
=>$co{'hash'},
5711 file_name
=>"$file"),
5712 -class => "list"}, esc_path
($file));
5713 print "</td><td>\n";
5717 print "<div class=\"binary\">Binary file</div>\n";
5719 $ltext = untabify
($ltext);
5720 if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
5721 $ltext = esc_html
($1, -nbsp
=>1);
5722 $ltext .= '<span class="match">';
5723 $ltext .= esc_html
($2, -nbsp
=>1);
5724 $ltext .= '</span>';
5725 $ltext .= esc_html
($3, -nbsp
=>1);
5727 $ltext = esc_html
($ltext, -nbsp
=>1);
5729 print "<div class=\"pre\">" .
5730 $cgi->a({-href
=> href
(action
=>"blob", hash
=>$co{'hash'},
5731 file_name
=>"$file").'#l'.$lno,
5732 -class => "linenr"}, sprintf('%4i', $lno))
5733 . ' ' . $ltext . "</div>\n";
5737 print "</td></tr>\n";
5738 if ($matches > 1000) {
5739 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
5742 print "<div class=\"diff nodifferences\">No matches found</div>\n";
5751 sub git_search_grep_body
{
5752 my ($commitlist, $from, $to, $extra) = @_;
5753 $from = 0 unless defined $from;
5754 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
5756 print "<table class=\"commit_search\">\n";
5758 for (my $i = $from; $i <= $to; $i++) {
5759 my %co = %{$commitlist->[$i]};
5763 my $commit = $co{'id'};
5765 print "<tr class=\"dark\">\n";
5767 print "<tr class=\"light\">\n";
5770 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5771 format_author_html
('td', \
%co, 15, 5) .
5773 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$co{'id'}),
5774 -class => "list subject"},
5775 chop_and_escape_str
($co{'title'}, 50) . "<br/>");
5776 my $comment = $co{'comment'};
5777 foreach my $line (@$comment) {
5778 if ($line =~ m/^(.*?)($search_regexp)(.*)$/i) {
5779 my ($lead, $match, $trail) = ($1, $2, $3);
5780 $match = chop_str
($match, 70, 5, 'center');
5781 my $contextlen = int((80 - length($match))/2);
5782 $contextlen = 30 if ($contextlen > 30);
5783 $lead = chop_str
($lead, $contextlen, 10, 'left');
5784 $trail = chop_str
($trail, $contextlen, 10, 'right');
5786 $lead = esc_html
($lead);
5787 $match = esc_html
($match);
5788 $trail = esc_html
($trail);
5790 print "$lead<span class=\"match\">$match</span>$trail<br />";
5794 "<td class=\"link\">" .
5795 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$co{'id'})}, "commit") .
5797 $cgi->a({-href
=> href
(action
=>"commitdiff", hash
=>$co{'id'})}, "commitdiff") .
5799 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$co{'tree'}, hash_base
=>$co{'id'})}, "tree");
5803 if (defined $extra) {
5805 "<td colspan=\"3\">$extra</td>\n" .
5811 ## ======================================================================
5812 ## ======================================================================
5815 sub git_project_list
{
5816 my $order = $input_params{'order'};
5817 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
5818 die_error
(400, "Unknown order parameter");
5821 my @list = git_get_projects_list
();
5823 die_error
(404, "No projects found");
5827 if (defined $home_text && -f
$home_text) {
5828 print "<div class=\"index_include\">\n";
5829 insert_file
($home_text);
5832 print $cgi->startform(-method => "get") .
5833 "<p class=\"projsearch\">Search:\n" .
5834 $cgi->textfield(-name
=> "s", -value
=> $searchtext) . "\n" .
5836 $cgi->end_form() . "\n";
5837 git_project_list_body
(\
@list, $order);
5842 my $order = $input_params{'order'};
5843 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
5844 die_error
(400, "Unknown order parameter");
5847 my @list = git_get_projects_list
($project);
5849 die_error
(404, "No forks found");
5853 git_print_page_nav
('','');
5854 git_print_header_div
('summary', "$project forks");
5855 git_project_list_body
(\
@list, $order);
5859 sub git_project_index
{
5860 my @projects = git_get_projects_list
();
5862 die_error
(404, "No projects found");
5866 -type
=> 'text/plain',
5867 -charset
=> 'utf-8',
5868 -content_disposition
=> 'inline; filename="index.aux"');
5870 foreach my $pr (@projects) {
5871 if (!exists $pr->{'owner'}) {
5872 $pr->{'owner'} = git_get_project_owner
("$pr->{'path'}");
5875 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
5876 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
5877 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf
("%%%02X", ord($1))/eg
;
5878 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf
("%%%02X", ord($1))/eg
;
5882 print "$path $owner\n";
5887 my $descr = git_get_project_description
($project) || "none";
5888 my %co = parse_commit
("HEAD");
5889 my %cd = %co ? parse_date
($co{'committer_epoch'}, $co{'committer_tz'}) : ();
5890 my $head = $co{'id'};
5891 my $remote_heads = gitweb_check_feature
('remote_heads');
5893 my $owner = git_get_project_owner
($project);
5895 my $refs = git_get_references
();
5896 # These get_*_list functions return one more to allow us to see if
5897 # there are more ...
5898 my @taglist = git_get_tags_list
(16);
5899 my @headlist = git_get_heads_list
(16);
5900 my %remotedata = $remote_heads ? git_get_remotes_list
() : ();
5902 my $check_forks = gitweb_check_feature
('forks');
5905 # find forks of a project
5906 @forklist = git_get_projects_list
($project);
5907 # filter out forks of forks
5908 @forklist = filter_forks_from_projects_list
(\
@forklist)
5913 git_print_page_nav
('summary','', $head);
5915 print "<div class=\"title\"> </div>\n";
5916 print "<table class=\"projects_list\">\n" .
5917 "<tr id=\"metadata_desc\"><td>description</td><td>" . esc_html
($descr) . "</td></tr>\n" .
5918 "<tr id=\"metadata_owner\"><td>owner</td><td>" . esc_html
($owner) . "</td></tr>\n";
5919 if (defined $cd{'rfc2822'}) {
5920 print "<tr id=\"metadata_lchange\"><td>last change</td>" .
5921 "<td>".format_timestamp_html
(\
%cd)."</td></tr>\n";
5924 # use per project git URL list in $projectroot/$project/cloneurl
5925 # or make project git URL from git base URL and project name
5926 my $url_tag = "URL";
5927 my @url_list = git_get_project_url_list
($project);
5928 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
5929 foreach my $git_url (@url_list) {
5930 next unless $git_url;
5931 print format_repo_url
($url_tag, $git_url);
5936 my $show_ctags = gitweb_check_feature
('ctags');
5938 my $ctags = git_get_project_ctags
($project);
5940 # without ability to add tags, don't show if there are none
5941 my $cloud = git_populate_project_tagcloud
($ctags);
5942 print "<tr id=\"metadata_ctags\">" .
5943 "<td>content tags</td>" .
5944 "<td>".git_show_project_tagcloud
($cloud, 48)."</td>" .
5951 # If XSS prevention is on, we don't include README.html.
5952 # TODO: Allow a readme in some safe format.
5953 if (!$prevent_xss && -s
"$projectroot/$project/README.html") {
5954 print "<div class=\"title\">readme</div>\n" .
5955 "<div class=\"readme\">\n";
5956 insert_file
("$projectroot/$project/README.html");
5957 print "\n</div>\n"; # class="readme"
5960 # we need to request one more than 16 (0..15) to check if
5962 my @commitlist = $head ? parse_commits
($head, 17) : ();
5964 git_print_header_div
('shortlog');
5965 git_shortlog_body
(\
@commitlist, 0, 15, $refs,
5966 $#commitlist <= 15 ? undef :
5967 $cgi->a({-href
=> href
(action
=>"shortlog")}, "..."));
5971 git_print_header_div
('tags');
5972 git_tags_body
(\
@taglist, 0, 15,
5973 $#taglist <= 15 ? undef :
5974 $cgi->a({-href
=> href
(action
=>"tags")}, "..."));
5978 git_print_header_div
('heads');
5979 git_heads_body
(\
@headlist, $head, 0, 15,
5980 $#headlist <= 15 ? undef :
5981 $cgi->a({-href
=> href
(action
=>"heads")}, "..."));
5985 git_print_header_div
('remotes');
5986 git_remotes_body
(\
%remotedata, 15, $head);
5990 git_print_header_div
('forks');
5991 git_project_list_body
(\
@forklist, 'age', 0, 15,
5992 $#forklist <= 15 ? undef :
5993 $cgi->a({-href
=> href
(action
=>"forks")}, "..."),
6001 my %tag = parse_tag
($hash);
6004 die_error
(404, "Unknown tag object");
6007 my $head = git_get_head_hash
($project);
6009 git_print_page_nav
('','', $head,undef,$head);
6010 git_print_header_div
('commit', esc_html
($tag{'name'}), $hash);
6011 print "<div class=\"title_text\">\n" .
6012 "<table class=\"object_header\">\n" .
6014 "<td>object</td>\n" .
6015 "<td>" . $cgi->a({-class => "list", -href
=> href
(action
=>$tag{'type'}, hash
=>$tag{'object'})},
6016 $tag{'object'}) . "</td>\n" .
6017 "<td class=\"link\">" . $cgi->a({-href
=> href
(action
=>$tag{'type'}, hash
=>$tag{'object'})},
6018 $tag{'type'}) . "</td>\n" .
6020 if (defined($tag{'author'})) {
6021 git_print_authorship_rows
(\
%tag, 'author');
6023 print "</table>\n\n" .
6025 print "<div class=\"page_body\">";
6026 my $comment = $tag{'comment'};
6027 foreach my $line (@$comment) {
6029 print esc_html
($line, -nbsp
=>1) . "<br/>\n";
6035 sub git_blame_common
{
6036 my $format = shift || 'porcelain';
6037 if ($format eq 'porcelain' && $cgi->param('js')) {
6038 $format = 'incremental';
6039 $action = 'blame_incremental'; # for page title etc
6043 gitweb_check_feature
('blame')
6044 or die_error
(403, "Blame view not allowed");
6047 die_error
(400, "No file name given") unless $file_name;
6048 $hash_base ||= git_get_head_hash
($project);
6049 die_error
(404, "Couldn't find base commit") unless $hash_base;
6050 my %co = parse_commit
($hash_base)
6051 or die_error
(404, "Commit not found");
6053 if (!defined $hash) {
6054 $hash = git_get_hash_by_path
($hash_base, $file_name, "blob")
6055 or die_error
(404, "Error looking up file");
6057 $ftype = git_get_type
($hash);
6058 if ($ftype !~ "blob") {
6059 die_error
(400, "Object is not a blob");
6064 if ($format eq 'incremental') {
6065 # get file contents (as base)
6066 open $fd, "-|", git_cmd
(), 'cat-file', 'blob', $hash
6067 or die_error
(500, "Open git-cat-file failed");
6068 } elsif ($format eq 'data') {
6069 # run git-blame --incremental
6070 open $fd, "-|", git_cmd
(), "blame", "--incremental",
6071 $hash_base, "--", $file_name
6072 or die_error
(500, "Open git-blame --incremental failed");
6074 # run git-blame --porcelain
6075 open $fd, "-|", git_cmd
(), "blame", '-p',
6076 $hash_base, '--', $file_name
6077 or die_error
(500, "Open git-blame --porcelain failed");
6080 # incremental blame data returns early
6081 if ($format eq 'data') {
6083 -type
=>"text/plain", -charset
=> "utf-8",
6084 -status
=> "200 OK");
6085 local $| = 1; # output autoflush
6088 or print "ERROR $!\n";
6091 if (defined $t0 && gitweb_check_feature
('timed')) {
6093 tv_interval
($t0, [ gettimeofday
() ]).
6094 ' '.$number_of_git_cmds;
6104 $cgi->a({-href
=> href
(action
=>"blob", -replay
=>1)},
6107 if ($format eq 'incremental') {
6109 $cgi->a({-href
=> href
(action
=>"blame", javascript
=>0, -replay
=>1)},
6110 "blame") . " (non-incremental)";
6113 $cgi->a({-href
=> href
(action
=>"blame_incremental", -replay
=>1)},
6114 "blame") . " (incremental)";
6118 $cgi->a({-href
=> href
(action
=>"history", -replay
=>1)},
6121 $cgi->a({-href
=> href
(action
=>$action, file_name
=>$file_name)},
6123 git_print_page_nav
('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
6124 git_print_header_div
('commit', esc_html
($co{'title'}), $hash_base);
6125 git_print_page_path
($file_name, $ftype, $hash_base);
6128 if ($format eq 'incremental') {
6129 print "<noscript>\n<div class=\"error\"><center><b>\n".
6130 "This page requires JavaScript to run.\n Use ".
6131 $cgi->a({-href
=> href
(action
=>'blame',javascript
=>0,-replay
=>1)},
6134 "</b></center></div>\n</noscript>\n";
6136 print qq
!<div id
="progress_bar" style
="width: 100%; background-color: yellow"></div
>\n!;
6139 print qq
!<div
class="page_body">\n!;
6140 print qq
!<div id
="progress_info">... / ...</div
>\n!
6141 if ($format eq 'incremental');
6142 print qq
!<table id
="blame_table" class="blame" width
="100%">\n!.
6143 #qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.
6145 qq
!<tr
><th
>Commit
</th><th>Line</th
><th
>Data
</th></tr
>\n!.
6149 my @rev_color = qw(light dark);
6150 my $num_colors = scalar(@rev_color);
6151 my $current_color = 0;
6153 if ($format eq 'incremental') {
6154 my $color_class = $rev_color[$current_color];
6159 while (my $line = <$fd>) {
6163 print qq
!<tr id
="l$linenr" class="$color_class">!.
6164 qq
!<td
class="sha1"><a href
=""> </a></td
>!.
6165 qq
!<td
class="linenr">!.
6166 qq
!<a
class="linenr" href
="">$linenr</a></td
>!;
6167 print qq
!<td
class="pre">! . esc_html
($line) . "</td>\n";
6171 } else { # porcelain, i.e. ordinary blame
6172 my %metainfo = (); # saves information about commits
6176 while (my $line = <$fd>) {
6178 # the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]
6179 # no <lines in group> for subsequent lines in group of lines
6180 my ($full_rev, $orig_lineno, $lineno, $group_size) =
6181 ($line =~ /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);
6182 if (!exists $metainfo{$full_rev}) {
6183 $metainfo{$full_rev} = { 'nprevious' => 0 };
6185 my $meta = $metainfo{$full_rev};
6187 while ($data = <$fd>) {
6189 last if ($data =~ s/^\t//); # contents of line
6190 if ($data =~ /^(\S+)(?: (.*))?$/) {
6191 $meta->{$1} = $2 unless exists $meta->{$1};
6193 if ($data =~ /^previous /) {
6194 $meta->{'nprevious'}++;
6197 my $short_rev = substr($full_rev, 0, 8);
6198 my $author = $meta->{'author'};
6200 parse_date
($meta->{'author-time'}, $meta->{'author-tz'});
6201 my $date = $date{'iso-tz'};
6203 $current_color = ($current_color + 1) % $num_colors;
6205 my $tr_class = $rev_color[$current_color];
6206 $tr_class .= ' boundary' if (exists $meta->{'boundary'});
6207 $tr_class .= ' no-previous' if ($meta->{'nprevious'} == 0);
6208 $tr_class .= ' multiple-previous' if ($meta->{'nprevious'} > 1);
6209 print "<tr id=\"l$lineno\" class=\"$tr_class\">\n";
6211 print "<td class=\"sha1\"";
6212 print " title=\"". esc_html
($author) . ", $date\"";
6213 print " rowspan=\"$group_size\"" if ($group_size > 1);
6215 print $cgi->a({-href
=> href
(action
=>"commit",
6217 file_name
=>$file_name)},
6218 esc_html
($short_rev));
6219 if ($group_size >= 2) {
6220 my @author_initials = ($author =~ /\b([[:upper:]])\B/g);
6221 if (@author_initials) {
6223 esc_html
(join('', @author_initials));
6229 # 'previous' <sha1 of parent commit> <filename at commit>
6230 if (exists $meta->{'previous'} &&
6231 $meta->{'previous'} =~ /^([a-fA-F0-9]{40}) (.*)$/) {
6232 $meta->{'parent'} = $1;
6233 $meta->{'file_parent'} = unquote
($2);
6236 exists($meta->{'parent'}) ?
6237 $meta->{'parent'} : $full_rev;
6238 my $linenr_filename =
6239 exists($meta->{'file_parent'}) ?
6240 $meta->{'file_parent'} : unquote
($meta->{'filename'});
6241 my $blamed = href
(action
=> 'blame',
6242 file_name
=> $linenr_filename,
6243 hash_base
=> $linenr_commit);
6244 print "<td class=\"linenr\">";
6245 print $cgi->a({ -href
=> "$blamed#l$orig_lineno",
6246 -class => "linenr" },
6249 print "<td class=\"pre\">" . esc_html
($data) . "</td>\n";
6257 "</table>\n"; # class="blame"
6258 print "</div>\n"; # class="blame_body"
6260 or print "Reading blob failed\n";
6269 sub git_blame_incremental
{
6270 git_blame_common
('incremental');
6273 sub git_blame_data
{
6274 git_blame_common
('data');
6278 my $head = git_get_head_hash
($project);
6280 git_print_page_nav
('','', $head,undef,$head,format_ref_views
('tags'));
6281 git_print_header_div
('summary', $project);
6283 my @tagslist = git_get_tags_list
();
6285 git_tags_body
(\
@tagslist);
6291 my $head = git_get_head_hash
($project);
6293 git_print_page_nav
('','', $head,undef,$head,format_ref_views
('heads'));
6294 git_print_header_div
('summary', $project);
6296 my @headslist = git_get_heads_list
();
6298 git_heads_body
(\
@headslist, $head);
6303 # used both for single remote view and for list of all the remotes
6305 gitweb_check_feature
('remote_heads')
6306 or die_error
(403, "Remote heads view is disabled");
6308 my $head = git_get_head_hash
($project);
6309 my $remote = $input_params{'hash'};
6311 my $remotedata = git_get_remotes_list
($remote);
6312 die_error
(500, "Unable to get remote information") unless defined $remotedata;
6314 unless (%$remotedata) {
6315 die_error
(404, defined $remote ?
6316 "Remote $remote not found" :
6317 "No remotes found");
6320 git_header_html
(undef, undef, -action_extra
=> $remote);
6321 git_print_page_nav
('', '', $head, undef, $head,
6322 format_ref_views
($remote ? '' : 'remotes'));
6324 fill_remote_heads
($remotedata);
6325 if (defined $remote) {
6326 git_print_header_div
('remotes', "$remote remote for $project");
6327 git_remote_block
($remote, $remotedata->{$remote}, undef, $head);
6329 git_print_header_div
('summary', "$project remotes");
6330 git_remotes_body
($remotedata, undef, $head);
6336 sub git_blob_plain
{
6340 if (!defined $hash) {
6341 if (defined $file_name) {
6342 my $base = $hash_base || git_get_head_hash
($project);
6343 $hash = git_get_hash_by_path
($base, $file_name, "blob")
6344 or die_error
(404, "Cannot find file");
6346 die_error
(400, "No file name defined");
6348 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6349 # blobs defined by non-textual hash id's can be cached
6353 open my $fd, "-|", git_cmd
(), "cat-file", "blob", $hash
6354 or die_error
(500, "Open git-cat-file blob '$hash' failed");
6356 # content-type (can include charset)
6357 $type = blob_contenttype
($fd, $file_name, $type);
6359 # "save as" filename, even when no $file_name is given
6360 my $save_as = "$hash";
6361 if (defined $file_name) {
6362 $save_as = $file_name;
6363 } elsif ($type =~ m/^text\//) {
6367 # With XSS prevention on, blobs of all types except a few known safe
6368 # ones are served with "Content-Disposition: attachment" to make sure
6369 # they don't run in our security domain. For certain image types,
6370 # blob view writes an <img> tag referring to blob_plain view, and we
6371 # want to be sure not to break that by serving the image as an
6372 # attachment (though Firefox 3 doesn't seem to care).
6373 my $sandbox = $prevent_xss &&
6374 $type !~ m!^(?:text/[a-z]+|image/(?:gif|png|jpeg))(?:[ ;]|$)!;
6376 # serve text/* as text/plain
6378 ($type =~ m!^text/[a-z]+\b(.*)$! ||
6379 ($type =~ m!^[a-z]+/[a-z]\+xml\b(.*)$! && -T
$fd))) {
6381 $rest = defined $rest ? $rest : '';
6382 $type = "text/plain$rest";
6387 -expires
=> $expires,
6388 -content_disposition
=>
6389 ($sandbox ? 'attachment' : 'inline')
6390 . '; filename="' . $save_as . '"');
6392 binmode STDOUT
, ':raw';
6394 binmode STDOUT
, ':utf8'; # as set at the beginning of gitweb.cgi
6401 if (!defined $hash) {
6402 if (defined $file_name) {
6403 my $base = $hash_base || git_get_head_hash
($project);
6404 $hash = git_get_hash_by_path
($base, $file_name, "blob")
6405 or die_error
(404, "Cannot find file");
6407 die_error
(400, "No file name defined");
6409 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6410 # blobs defined by non-textual hash id's can be cached
6414 my $have_blame = gitweb_check_feature
('blame');
6415 open my $fd, "-|", git_cmd
(), "cat-file", "blob", $hash
6416 or die_error
(500, "Couldn't cat $file_name, $hash");
6417 my $mimetype = blob_mimetype
($fd, $file_name);
6418 # use 'blob_plain' (aka 'raw') view for files that cannot be displayed
6419 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B
$fd) {
6421 return git_blob_plain
($mimetype);
6423 # we can have blame only for text/* mimetype
6424 $have_blame &&= ($mimetype =~ m!^text/!);
6426 my $highlight = gitweb_check_feature
('highlight');
6427 my $syntax = guess_file_syntax
($highlight, $mimetype, $file_name);
6428 $fd = run_highlighter
($fd, $highlight, $syntax)
6431 git_header_html
(undef, $expires);
6432 my $formats_nav = '';
6433 if (defined $hash_base && (my %co = parse_commit
($hash_base))) {
6434 if (defined $file_name) {
6437 $cgi->a({-href
=> href
(action
=>"blame", -replay
=>1)},
6442 $cgi->a({-href
=> href
(action
=>"history", -replay
=>1)},
6445 $cgi->a({-href
=> href
(action
=>"blob_plain", -replay
=>1)},
6448 $cgi->a({-href
=> href
(action
=>"blob",
6449 hash_base
=>"HEAD", file_name
=>$file_name)},
6453 $cgi->a({-href
=> href
(action
=>"blob_plain", -replay
=>1)},
6456 git_print_page_nav
('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
6457 git_print_header_div
('commit', esc_html
($co{'title'}), $hash_base);
6459 print "<div class=\"page_nav\">\n" .
6460 "<br/><br/></div>\n" .
6461 "<div class=\"title\">".esc_html
($hash)."</div>\n";
6463 git_print_page_path
($file_name, "blob", $hash_base);
6464 print "<div class=\"page_body\">\n";
6465 if ($mimetype =~ m!^image/!) {
6466 print qq
!<img type
="!.esc_attr($mimetype).qq!"!;
6468 print qq
! alt
="!.esc_attr($file_name).qq!" title
="!.esc_attr($file_name).qq!"!;
6471 href(action=>"blob_plain
", hash=>$hash,
6472 hash_base=>$hash_base, file_name=>$file_name) .
6476 while (my $line = <$fd>) {
6479 $line = untabify
($line);
6480 printf qq
!<div
class="pre"><a id
="l%i" href
="%s#l%i" class="linenr">%4i</a> %s</div
>\n!,
6481 $nr, href
(-replay
=> 1), $nr, $nr, $syntax ? $line : esc_html
($line, -nbsp
=>1);
6485 or print "Reading blob failed.\n";
6491 if (!defined $hash_base) {
6492 $hash_base = "HEAD";
6494 if (!defined $hash) {
6495 if (defined $file_name) {
6496 $hash = git_get_hash_by_path
($hash_base, $file_name, "tree");
6501 die_error
(404, "No such tree") unless defined($hash);
6503 my $show_sizes = gitweb_check_feature
('show-sizes');
6504 my $have_blame = gitweb_check_feature
('blame');
6509 open my $fd, "-|", git_cmd
(), "ls-tree", '-z',
6510 ($show_sizes ? '-l' : ()), @extra_options, $hash
6511 or die_error
(500, "Open git-ls-tree failed");
6512 @entries = map { chomp; $_ } <$fd>;
6514 or die_error
(404, "Reading tree failed");
6517 my $refs = git_get_references
();
6518 my $ref = format_ref_marker
($refs, $hash_base);
6521 if (defined $hash_base && (my %co = parse_commit
($hash_base))) {
6523 if (defined $file_name) {
6525 $cgi->a({-href
=> href
(action
=>"history", -replay
=>1)},
6527 $cgi->a({-href
=> href
(action
=>"tree",
6528 hash_base
=>"HEAD", file_name
=>$file_name)},
6531 my $snapshot_links = format_snapshot_links
($hash);
6532 if (defined $snapshot_links) {
6533 # FIXME: Should be available when we have no hash base as well.
6534 push @views_nav, $snapshot_links;
6536 git_print_page_nav
('tree','', $hash_base, undef, undef,
6537 join(' | ', @views_nav));
6538 git_print_header_div
('commit', esc_html
($co{'title'}) . $ref, $hash_base);
6541 print "<div class=\"page_nav\">\n";
6542 print "<br/><br/></div>\n";
6543 print "<div class=\"title\">".esc_html
($hash)."</div>\n";
6545 if (defined $file_name) {
6546 $basedir = $file_name;
6547 if ($basedir ne '' && substr($basedir, -1) ne '/') {
6550 git_print_page_path
($file_name, 'tree', $hash_base);
6552 print "<div class=\"page_body\">\n";
6553 print "<table class=\"tree\">\n";
6555 # '..' (top directory) link if possible
6556 if (defined $hash_base &&
6557 defined $file_name && $file_name =~ m![^/]+$!) {
6559 print "<tr class=\"dark\">\n";
6561 print "<tr class=\"light\">\n";
6565 my $up = $file_name;
6566 $up =~ s!/?[^/]+$!!;
6567 undef $up unless $up;
6568 # based on git_print_tree_entry
6569 print '<td class="mode">' . mode_str
('040000') . "</td>\n";
6570 print '<td class="size"> </td>'."\n" if $show_sizes;
6571 print '<td class="list">';
6572 print $cgi->a({-href
=> href
(action
=>"tree",
6573 hash_base
=>$hash_base,
6577 print "<td class=\"link\"></td>\n";
6581 foreach my $line (@entries) {
6582 my %t = parse_ls_tree_line
($line, -z
=> 1, -l
=> $show_sizes);
6585 print "<tr class=\"dark\">\n";
6587 print "<tr class=\"light\">\n";
6591 git_print_tree_entry
(\
%t, $basedir, $hash_base, $have_blame);
6595 print "</table>\n" .
6601 my ($project, $hash) = @_;
6603 # path/to/project.git -> project
6604 # path/to/project/.git -> project
6605 my $name = to_utf8
($project);
6606 $name =~ s
,([^/])/*\
.git
$,$1,;
6607 $name = basename
($name);
6609 $name =~ s/[[:cntrl:]]/?/g;
6612 if ($hash =~ /^[0-9a-fA-F]+$/) {
6613 # shorten SHA-1 hash
6614 my $full_hash = git_get_full_hash
($project, $hash);
6615 if ($full_hash =~ /^$hash/ && length($hash) > 7) {
6616 $ver = git_get_short_hash
($project, $hash);
6618 } elsif ($hash =~ m!^refs/tags/(.*)$!) {
6619 # tags don't need shortened SHA-1 hash
6622 # branches and other need shortened SHA-1 hash
6623 if ($hash =~ m!^refs/(?:heads|remotes)/(.*)$!) {
6626 $ver .= '-' . git_get_short_hash
($project, $hash);
6628 # in case of hierarchical branch names
6631 # name = project-version_string
6632 $name = "$name-$ver";
6634 return wantarray ? ($name, $name) : $name;
6638 my $format = $input_params{'snapshot_format'};
6639 if (!@snapshot_fmts) {
6640 die_error
(403, "Snapshots not allowed");
6642 # default to first supported snapshot format
6643 $format ||= $snapshot_fmts[0];
6644 if ($format !~ m/^[a-z0-9]+$/) {
6645 die_error
(400, "Invalid snapshot format parameter");
6646 } elsif (!exists($known_snapshot_formats{$format})) {
6647 die_error
(400, "Unknown snapshot format");
6648 } elsif ($known_snapshot_formats{$format}{'disabled'}) {
6649 die_error
(403, "Snapshot format not allowed");
6650 } elsif (!grep($_ eq $format, @snapshot_fmts)) {
6651 die_error
(403, "Unsupported snapshot format");
6654 my $type = git_get_type
("$hash^{}");
6656 die_error
(404, 'Object does not exist');
6657 } elsif ($type eq 'blob') {
6658 die_error
(400, 'Object is not a tree-ish');
6661 my ($name, $prefix) = snapshot_name
($project, $hash);
6662 my $filename = "$name$known_snapshot_formats{$format}{'suffix'}";
6663 my $cmd = quote_command
(
6664 git_cmd
(), 'archive',
6665 "--format=$known_snapshot_formats{$format}{'format'}",
6666 "--prefix=$prefix/", $hash);
6667 if (exists $known_snapshot_formats{$format}{'compressor'}) {
6668 $cmd .= ' | ' . quote_command
(@{$known_snapshot_formats{$format}{'compressor'}});
6671 $filename =~ s/(["\\])/\\$1/g;
6673 -type
=> $known_snapshot_formats{$format}{'type'},
6674 -content_disposition
=> 'inline; filename="' . $filename . '"',
6675 -status
=> '200 OK');
6677 open my $fd, "-|", $cmd
6678 or die_error
(500, "Execute git-archive failed");
6679 binmode STDOUT
, ':raw';
6681 binmode STDOUT
, ':utf8'; # as set at the beginning of gitweb.cgi
6685 sub git_log_generic
{
6686 my ($fmt_name, $body_subr, $base, $parent, $file_name, $file_hash) = @_;
6688 my $head = git_get_head_hash
($project);
6689 if (!defined $base) {
6692 if (!defined $page) {
6695 my $refs = git_get_references
();
6697 my $commit_hash = $base;
6698 if (defined $parent) {
6699 $commit_hash = "$parent..$base";
6702 parse_commits
($commit_hash, 101, (100 * $page),
6703 defined $file_name ? ($file_name, "--full-history") : ());
6706 if (!defined $file_hash && defined $file_name) {
6707 # some commits could have deleted file in question,
6708 # and not have it in tree, but one of them has to have it
6709 for (my $i = 0; $i < @commitlist; $i++) {
6710 $file_hash = git_get_hash_by_path
($commitlist[$i]{'id'}, $file_name);
6711 last if defined $file_hash;
6714 if (defined $file_hash) {
6715 $ftype = git_get_type
($file_hash);
6717 if (defined $file_name && !defined $ftype) {
6718 die_error
(500, "Unknown type of object");
6721 if (defined $file_name) {
6722 %co = parse_commit
($base)
6723 or die_error
(404, "Unknown commit object");
6727 my $paging_nav = format_paging_nav
($fmt_name, $page, $#commitlist >= 100);
6729 if ($#commitlist >= 100) {
6731 $cgi->a({-href
=> href
(-replay
=>1, page
=>$page+1),
6732 -accesskey
=> "n", -title
=> "Alt-n"}, "next");
6734 my $patch_max = gitweb_get_feature
('patches');
6735 if ($patch_max && !defined $file_name) {
6736 if ($patch_max < 0 || @commitlist <= $patch_max) {
6737 $paging_nav .= " ⋅ " .
6738 $cgi->a({-href
=> href
(action
=>"patches", -replay
=>1)},
6744 git_print_page_nav
($fmt_name,'', $hash,$hash,$hash, $paging_nav);
6745 if (defined $file_name) {
6746 git_print_header_div
('commit', esc_html
($co{'title'}), $base);
6748 git_print_header_div
('summary', $project)
6750 git_print_page_path
($file_name, $ftype, $hash_base)
6751 if (defined $file_name);
6753 $body_subr->(\
@commitlist, 0, 99, $refs, $next_link,
6754 $file_name, $file_hash, $ftype);
6760 git_log_generic
('log', \
&git_log_body
,
6761 $hash, $hash_parent);
6765 $hash ||= $hash_base || "HEAD";
6766 my %co = parse_commit
($hash)
6767 or die_error
(404, "Unknown commit object");
6769 my $parent = $co{'parent'};
6770 my $parents = $co{'parents'}; # listref
6772 # we need to prepare $formats_nav before any parameter munging
6774 if (!defined $parent) {
6776 $formats_nav .= '(initial)';
6777 } elsif (@$parents == 1) {
6778 # single parent commit
6781 $cgi->a({-href
=> href
(action
=>"commit",
6783 esc_html
(substr($parent, 0, 7))) .
6790 $cgi->a({-href
=> href
(action
=>"commit",
6792 esc_html
(substr($_, 0, 7)));
6796 if (gitweb_check_feature
('patches') && @$parents <= 1) {
6797 $formats_nav .= " | " .
6798 $cgi->a({-href
=> href
(action
=>"patch", -replay
=>1)},
6802 if (!defined $parent) {
6806 open my $fd, "-|", git_cmd
(), "diff-tree", '-r', "--no-commit-id",
6808 (@$parents <= 1 ? $parent : '-c'),
6810 or die_error
(500, "Open git-diff-tree failed");
6811 @difftree = map { chomp; $_ } <$fd>;
6812 close $fd or die_error
(404, "Reading git-diff-tree failed");
6814 # non-textual hash id's can be cached
6816 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6819 my $refs = git_get_references
();
6820 my $ref = format_ref_marker
($refs, $co{'id'});
6822 git_header_html
(undef, $expires);
6823 git_print_page_nav
('commit', '',
6824 $hash, $co{'tree'}, $hash,
6827 if (defined $co{'parent'}) {
6828 git_print_header_div
('commitdiff', esc_html
($co{'title'}) . $ref, $hash);
6830 git_print_header_div
('tree', esc_html
($co{'title'}) . $ref, $co{'tree'}, $hash);
6832 print "<div class=\"title_text\">\n" .
6833 "<table class=\"object_header\">\n";
6834 git_print_authorship_rows
(\
%co);
6835 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
6838 "<td class=\"sha1\">" .
6839 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$co{'tree'}, hash_base
=>$hash),
6840 class => "list"}, $co{'tree'}) .
6842 "<td class=\"link\">" .
6843 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$co{'tree'}, hash_base
=>$hash)},
6845 my $snapshot_links = format_snapshot_links
($hash);
6846 if (defined $snapshot_links) {
6847 print " | " . $snapshot_links;
6852 foreach my $par (@$parents) {
6855 "<td class=\"sha1\">" .
6856 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$par),
6857 class => "list"}, $par) .
6859 "<td class=\"link\">" .
6860 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$par)}, "commit") .
6862 $cgi->a({-href
=> href
(action
=>"commitdiff", hash
=>$hash, hash_parent
=>$par)}, "diff") .
6869 print "<div class=\"page_body\">\n";
6870 git_print_log
($co{'comment'});
6873 git_difftree_body
(\
@difftree, $hash, @$parents);
6879 # object is defined by:
6880 # - hash or hash_base alone
6881 # - hash_base and file_name
6884 # - hash or hash_base alone
6885 if ($hash || ($hash_base && !defined $file_name)) {
6886 my $object_id = $hash || $hash_base;
6888 open my $fd, "-|", quote_command
(
6889 git_cmd
(), 'cat-file', '-t', $object_id) . ' 2> /dev/null'
6890 or die_error
(404, "Object does not exist");
6894 or die_error
(404, "Object does not exist");
6896 # - hash_base and file_name
6897 } elsif ($hash_base && defined $file_name) {
6898 $file_name =~ s
,/+$,,;
6900 system(git_cmd
(), "cat-file", '-e', $hash_base) == 0
6901 or die_error
(404, "Base object does not exist");
6903 # here errors should not hapen
6904 open my $fd, "-|", git_cmd
(), "ls-tree", $hash_base, "--", $file_name
6905 or die_error
(500, "Open git-ls-tree failed");
6909 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
6910 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
6911 die_error
(404, "File or directory for given base does not exist");
6916 die_error
(400, "Not enough information to find object");
6919 print $cgi->redirect(-uri
=> href
(action
=>$type, -full
=>1,
6920 hash
=>$hash, hash_base
=>$hash_base,
6921 file_name
=>$file_name),
6922 -status
=> '302 Found');
6926 my $format = shift || 'html';
6933 # preparing $fd and %diffinfo for git_patchset_body
6935 if (defined $hash_base && defined $hash_parent_base) {
6936 if (defined $file_name) {
6938 open $fd, "-|", git_cmd
(), "diff-tree", '-r', @diff_opts,
6939 $hash_parent_base, $hash_base,
6940 "--", (defined $file_parent ? $file_parent : ()), $file_name
6941 or die_error
(500, "Open git-diff-tree failed");
6942 @difftree = map { chomp; $_ } <$fd>;
6944 or die_error
(404, "Reading git-diff-tree failed");
6946 or die_error
(404, "Blob diff not found");
6948 } elsif (defined $hash &&
6949 $hash =~ /[0-9a-fA-F]{40}/) {
6950 # try to find filename from $hash
6952 # read filtered raw output
6953 open $fd, "-|", git_cmd
(), "diff-tree", '-r', @diff_opts,
6954 $hash_parent_base, $hash_base, "--"
6955 or die_error
(500, "Open git-diff-tree failed");
6957 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
6959 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
6960 map { chomp; $_ } <$fd>;
6962 or die_error
(404, "Reading git-diff-tree failed");
6964 or die_error
(404, "Blob diff not found");
6967 die_error
(400, "Missing one of the blob diff parameters");
6970 if (@difftree > 1) {
6971 die_error
(400, "Ambiguous blob diff specification");
6974 %diffinfo = parse_difftree_raw_line
($difftree[0]);
6975 $file_parent ||= $diffinfo{'from_file'} || $file_name;
6976 $file_name ||= $diffinfo{'to_file'};
6978 $hash_parent ||= $diffinfo{'from_id'};
6979 $hash ||= $diffinfo{'to_id'};
6981 # non-textual hash id's can be cached
6982 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
6983 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
6988 open $fd, "-|", git_cmd
(), "diff-tree", '-r', @diff_opts,
6989 '-p', ($format eq 'html' ? "--full-index" : ()),
6990 $hash_parent_base, $hash_base,
6991 "--", (defined $file_parent ? $file_parent : ()), $file_name
6992 or die_error
(500, "Open git-diff-tree failed");
6995 # old/legacy style URI -- not generated anymore since 1.4.3.
6997 die_error
('404 Not Found', "Missing one of the blob diff parameters")
7001 if ($format eq 'html') {
7003 $cgi->a({-href
=> href
(action
=>"blobdiff_plain", -replay
=>1)},
7005 git_header_html
(undef, $expires);
7006 if (defined $hash_base && (my %co = parse_commit
($hash_base))) {
7007 git_print_page_nav
('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
7008 git_print_header_div
('commit', esc_html
($co{'title'}), $hash_base);
7010 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
7011 print "<div class=\"title\">".esc_html
("$hash vs $hash_parent")."</div>\n";
7013 if (defined $file_name) {
7014 git_print_page_path
($file_name, "blob", $hash_base);
7016 print "<div class=\"page_path\"></div>\n";
7019 } elsif ($format eq 'plain') {
7021 -type
=> 'text/plain',
7022 -charset
=> 'utf-8',
7023 -expires
=> $expires,
7024 -content_disposition
=> 'inline; filename="' . "$file_name" . '.patch"');
7026 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
7029 die_error
(400, "Unknown blobdiff format");
7033 if ($format eq 'html') {
7034 print "<div class=\"page_body\">\n";
7036 git_patchset_body
($fd, [ \
%diffinfo ], $hash_base, $hash_parent_base);
7039 print "</div>\n"; # class="page_body"
7043 while (my $line = <$fd>) {
7044 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
7045 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
7049 last if $line =~ m!^\+\+\+!;
7057 sub git_blobdiff_plain
{
7058 git_blobdiff
('plain');
7061 sub git_commitdiff
{
7063 my $format = $params{-format
} || 'html';
7065 my ($patch_max) = gitweb_get_feature
('patches');
7066 if ($format eq 'patch') {
7067 die_error
(403, "Patch view not allowed") unless $patch_max;
7070 $hash ||= $hash_base || "HEAD";
7071 my %co = parse_commit
($hash)
7072 or die_error
(404, "Unknown commit object");
7074 # choose format for commitdiff for merge
7075 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
7076 $hash_parent = '--cc';
7078 # we need to prepare $formats_nav before almost any parameter munging
7080 if ($format eq 'html') {
7082 $cgi->a({-href
=> href
(action
=>"commitdiff_plain", -replay
=>1)},
7084 if ($patch_max && @{$co{'parents'}} <= 1) {
7085 $formats_nav .= " | " .
7086 $cgi->a({-href
=> href
(action
=>"patch", -replay
=>1)},
7090 if (defined $hash_parent &&
7091 $hash_parent ne '-c' && $hash_parent ne '--cc') {
7092 # commitdiff with two commits given
7093 my $hash_parent_short = $hash_parent;
7094 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
7095 $hash_parent_short = substr($hash_parent, 0, 7);
7099 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
7100 if ($co{'parents'}[$i] eq $hash_parent) {
7101 $formats_nav .= ' parent ' . ($i+1);
7105 $formats_nav .= ': ' .
7106 $cgi->a({-href
=> href
(action
=>"commitdiff",
7107 hash
=>$hash_parent)},
7108 esc_html
($hash_parent_short)) .
7110 } elsif (!$co{'parent'}) {
7112 $formats_nav .= ' (initial)';
7113 } elsif (scalar @{$co{'parents'}} == 1) {
7114 # single parent commit
7117 $cgi->a({-href
=> href
(action
=>"commitdiff",
7118 hash
=>$co{'parent'})},
7119 esc_html
(substr($co{'parent'}, 0, 7))) .
7123 if ($hash_parent eq '--cc') {
7124 $formats_nav .= ' | ' .
7125 $cgi->a({-href
=> href
(action
=>"commitdiff",
7126 hash
=>$hash, hash_parent
=>'-c')},
7128 } else { # $hash_parent eq '-c'
7129 $formats_nav .= ' | ' .
7130 $cgi->a({-href
=> href
(action
=>"commitdiff",
7131 hash
=>$hash, hash_parent
=>'--cc')},
7137 $cgi->a({-href
=> href
(action
=>"commitdiff",
7139 esc_html
(substr($_, 0, 7)));
7140 } @{$co{'parents'}} ) .
7145 my $hash_parent_param = $hash_parent;
7146 if (!defined $hash_parent_param) {
7147 # --cc for multiple parents, --root for parentless
7148 $hash_parent_param =
7149 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
7155 if ($format eq 'html') {
7156 open $fd, "-|", git_cmd
(), "diff-tree", '-r', @diff_opts,
7157 "--no-commit-id", "--patch-with-raw", "--full-index",
7158 $hash_parent_param, $hash, "--"
7159 or die_error
(500, "Open git-diff-tree failed");
7161 while (my $line = <$fd>) {
7163 # empty line ends raw part of diff-tree output
7165 push @difftree, scalar parse_difftree_raw_line
($line);
7168 } elsif ($format eq 'plain') {
7169 open $fd, "-|", git_cmd
(), "diff-tree", '-r', @diff_opts,
7170 '-p', $hash_parent_param, $hash, "--"
7171 or die_error
(500, "Open git-diff-tree failed");
7172 } elsif ($format eq 'patch') {
7173 # For commit ranges, we limit the output to the number of
7174 # patches specified in the 'patches' feature.
7175 # For single commits, we limit the output to a single patch,
7176 # diverging from the git-format-patch default.
7177 my @commit_spec = ();
7179 if ($patch_max > 0) {
7180 push @commit_spec, "-$patch_max";
7182 push @commit_spec, '-n', "$hash_parent..$hash";
7184 if ($params{-single
}) {
7185 push @commit_spec, '-1';
7187 if ($patch_max > 0) {
7188 push @commit_spec, "-$patch_max";
7190 push @commit_spec, "-n";
7192 push @commit_spec, '--root', $hash;
7194 open $fd, "-|", git_cmd
(), "format-patch", @diff_opts,
7195 '--encoding=utf8', '--stdout', @commit_spec
7196 or die_error
(500, "Open git-format-patch failed");
7198 die_error
(400, "Unknown commitdiff format");
7201 # non-textual hash id's can be cached
7203 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
7207 # write commit message
7208 if ($format eq 'html') {
7209 my $refs = git_get_references
();
7210 my $ref = format_ref_marker
($refs, $co{'id'});
7212 git_header_html
(undef, $expires);
7213 git_print_page_nav
('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
7214 git_print_header_div
('commit', esc_html
($co{'title'}) . $ref, $hash);
7215 print "<div class=\"title_text\">\n" .
7216 "<table class=\"object_header\">\n";
7217 git_print_authorship_rows
(\
%co);
7220 print "<div class=\"page_body\">\n";
7221 if (@{$co{'comment'}} > 1) {
7222 print "<div class=\"log\">\n";
7223 git_print_log
($co{'comment'}, -final_empty_line
=> 1, -remove_title
=> 1);
7224 print "</div>\n"; # class="log"
7227 } elsif ($format eq 'plain') {
7228 my $refs = git_get_references
("tags");
7229 my $tagname = git_get_rev_name_tags
($hash);
7230 my $filename = basename
($project) . "-$hash.patch";
7233 -type
=> 'text/plain',
7234 -charset
=> 'utf-8',
7235 -expires
=> $expires,
7236 -content_disposition
=> 'inline; filename="' . "$filename" . '"');
7237 my %ad = parse_date
($co{'author_epoch'}, $co{'author_tz'});
7238 print "From: " . to_utf8
($co{'author'}) . "\n";
7239 print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
7240 print "Subject: " . to_utf8
($co{'title'}) . "\n";
7242 print "X-Git-Tag: $tagname\n" if $tagname;
7243 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
7245 foreach my $line (@{$co{'comment'}}) {
7246 print to_utf8
($line) . "\n";
7249 } elsif ($format eq 'patch') {
7250 my $filename = basename
($project) . "-$hash.patch";
7253 -type
=> 'text/plain',
7254 -charset
=> 'utf-8',
7255 -expires
=> $expires,
7256 -content_disposition
=> 'inline; filename="' . "$filename" . '"');
7260 if ($format eq 'html') {
7261 my $use_parents = !defined $hash_parent ||
7262 $hash_parent eq '-c' || $hash_parent eq '--cc';
7263 git_difftree_body
(\
@difftree, $hash,
7264 $use_parents ? @{$co{'parents'}} : $hash_parent);
7267 git_patchset_body
($fd, \
@difftree, $hash,
7268 $use_parents ? @{$co{'parents'}} : $hash_parent);
7270 print "</div>\n"; # class="page_body"
7273 } elsif ($format eq 'plain') {
7277 or print "Reading git-diff-tree failed\n";
7278 } elsif ($format eq 'patch') {
7282 or print "Reading git-format-patch failed\n";
7286 sub git_commitdiff_plain
{
7287 git_commitdiff
(-format
=> 'plain');
7290 # format-patch-style patches
7292 git_commitdiff
(-format
=> 'patch', -single
=> 1);
7296 git_commitdiff
(-format
=> 'patch');
7300 git_log_generic
('history', \
&git_history_body
,
7301 $hash_base, $hash_parent_base,
7306 $searchtype ||= 'commit';
7308 # check if appropriate features are enabled
7309 gitweb_check_feature
('search')
7310 or die_error
(403, "Search is disabled");
7311 if ($searchtype eq 'pickaxe') {
7312 # pickaxe may take all resources of your box and run for several minutes
7313 # with every query - so decide by yourself how public you make this feature
7314 gitweb_check_feature
('pickaxe')
7315 or die_error
(403, "Pickaxe search is disabled");
7317 if ($searchtype eq 'grep') {
7318 # grep search might be potentially CPU-intensive, too
7319 gitweb_check_feature
('grep')
7320 or die_error
(403, "Grep search is disabled");
7323 if (!defined $searchtext) {
7324 die_error
(400, "Text field is empty");
7326 if (!defined $hash) {
7327 $hash = git_get_head_hash
($project);
7329 my %co = parse_commit
($hash);
7331 die_error
(404, "Unknown commit object");
7333 if (!defined $page) {
7337 if ($searchtype eq 'commit' ||
7338 $searchtype eq 'author' ||
7339 $searchtype eq 'committer') {
7340 git_search_message
(%co);
7341 } elsif ($searchtype eq 'pickaxe') {
7342 git_search_changes
(%co);
7343 } elsif ($searchtype eq 'grep') {
7344 git_search_files
(%co);
7346 die_error
(400, "Unknown search type");
7350 sub git_search_help
{
7352 git_print_page_nav
('','', $hash,$hash,$hash);
7354 <p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without
7355 regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,
7356 the pattern entered is recognized as the POSIX extended
7357 <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case
7360 <dt><b>commit</b></dt>
7361 <dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
7363 my $have_grep = gitweb_check_feature
('grep');
7366 <dt><b>grep</b></dt>
7367 <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
7368 a different one) are searched for the given pattern. On large trees, this search can take
7369 a while and put some strain on the server, so please use it with some consideration. Note that
7370 due to git-grep peculiarity, currently if regexp mode is turned off, the matches are
7371 case-sensitive.</dd>
7375 <dt><b>author</b></dt>
7376 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>
7377 <dt><b>committer</b></dt>
7378 <dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>
7380 my $have_pickaxe = gitweb_check_feature
('pickaxe');
7381 if ($have_pickaxe) {
7383 <dt><b>pickaxe</b></dt>
7384 <dd>All commits that caused the string to appear or disappear from any file (changes that
7385 added, removed or "modified" the string) will be listed. This search can take a while and
7386 takes a lot of strain on the server, so please use it wisely. Note that since you may be
7387 interested even in changes just changing the case as well, this search is case sensitive.</dd>
7395 git_log_generic
('shortlog', \
&git_shortlog_body
,
7396 $hash, $hash_parent);
7399 ## ......................................................................
7400 ## feeds (RSS, Atom; OPML)
7403 my $format = shift || 'atom';
7404 my $have_blame = gitweb_check_feature
('blame');
7406 # Atom: http://www.atomenabled.org/developers/syndication/
7407 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
7408 if ($format ne 'rss' && $format ne 'atom') {
7409 die_error
(400, "Unknown web feed format");
7412 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
7413 my $head = $hash || 'HEAD';
7414 my @commitlist = parse_commits
($head, 150, 0, $file_name);
7418 my $content_type = "application/$format+xml";
7419 if (defined $cgi->http('HTTP_ACCEPT') &&
7420 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
7421 # browser (feed reader) prefers text/xml
7422 $content_type = 'text/xml';
7424 if (defined($commitlist[0])) {
7425 %latest_commit = %{$commitlist[0]};
7426 my $latest_epoch = $latest_commit{'committer_epoch'};
7427 %latest_date = parse_date
($latest_epoch, $latest_commit{'comitter_tz'});
7428 my $if_modified = $cgi->http('IF_MODIFIED_SINCE');
7429 if (defined $if_modified) {
7431 if (eval { require HTTP
::Date
; 1; }) {
7432 $since = HTTP
::Date
::str2time
($if_modified);
7433 } elsif (eval { require Time
::ParseDate
; 1; }) {
7434 $since = Time
::ParseDate
::parsedate
($if_modified, GMT
=> 1);
7436 if (defined $since && $latest_epoch <= $since) {
7438 -type
=> $content_type,
7439 -charset
=> 'utf-8',
7440 -last_modified
=> $latest_date{'rfc2822'},
7441 -status
=> '304 Not Modified');
7446 -type
=> $content_type,
7447 -charset
=> 'utf-8',
7448 -last_modified
=> $latest_date{'rfc2822'});
7451 -type
=> $content_type,
7452 -charset
=> 'utf-8');
7455 # Optimization: skip generating the body if client asks only
7456 # for Last-Modified date.
7457 return if ($cgi->request_method() eq 'HEAD');
7460 my $title = "$site_name - $project/$action";
7461 my $feed_type = 'log';
7462 if (defined $hash) {
7463 $title .= " - '$hash'";
7464 $feed_type = 'branch log';
7465 if (defined $file_name) {
7466 $title .= " :: $file_name";
7467 $feed_type = 'history';
7469 } elsif (defined $file_name) {
7470 $title .= " - $file_name";
7471 $feed_type = 'history';
7473 $title .= " $feed_type";
7474 my $descr = git_get_project_description
($project);
7475 if (defined $descr) {
7476 $descr = esc_html
($descr);
7478 $descr = "$project " .
7479 ($format eq 'rss' ? 'RSS' : 'Atom') .
7482 my $owner = git_get_project_owner
($project);
7483 $owner = esc_html
($owner);
7487 if (defined $file_name) {
7488 $alt_url = href
(-full
=>1, action
=>"history", hash
=>$hash, file_name
=>$file_name);
7489 } elsif (defined $hash) {
7490 $alt_url = href
(-full
=>1, action
=>"log", hash
=>$hash);
7492 $alt_url = href
(-full
=>1, action
=>"summary");
7494 print qq
!<?xml version
="1.0" encoding
="utf-8"?>\n!;
7495 if ($format eq 'rss') {
7497 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
7500 print "<title>$title</title>\n" .
7501 "<link>$alt_url</link>\n" .
7502 "<description>$descr</description>\n" .
7503 "<language>en</language>\n" .
7504 # project owner is responsible for 'editorial' content
7505 "<managingEditor>$owner</managingEditor>\n";
7506 if (defined $logo || defined $favicon) {
7507 # prefer the logo to the favicon, since RSS
7508 # doesn't allow both
7509 my $img = esc_url
($logo || $favicon);
7511 "<url>$img</url>\n" .
7512 "<title>$title</title>\n" .
7513 "<link>$alt_url</link>\n" .
7517 print "<pubDate>$latest_date{'rfc2822'}</pubDate>\n";
7518 print "<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";
7520 print "<generator>gitweb v.$version/$git_version</generator>\n";
7521 } elsif ($format eq 'atom') {
7523 <feed xmlns="http://www.w3.org/2005/Atom">
7525 print "<title>$title</title>\n" .
7526 "<subtitle>$descr</subtitle>\n" .
7527 '<link rel="alternate" type="text/html" href="' .
7528 $alt_url . '" />' . "\n" .
7529 '<link rel="self" type="' . $content_type . '" href="' .
7530 $cgi->self_url() . '" />' . "\n" .
7531 "<id>" . href
(-full
=>1) . "</id>\n" .
7532 # use project owner for feed author
7533 "<author><name>$owner</name></author>\n";
7534 if (defined $favicon) {
7535 print "<icon>" . esc_url
($favicon) . "</icon>\n";
7537 if (defined $logo) {
7538 # not twice as wide as tall: 72 x 27 pixels
7539 print "<logo>" . esc_url
($logo) . "</logo>\n";
7541 if (! %latest_date) {
7542 # dummy date to keep the feed valid until commits trickle in:
7543 print "<updated>1970-01-01T00:00:00Z</updated>\n";
7545 print "<updated>$latest_date{'iso-8601'}</updated>\n";
7547 print "<generator version='$version/$git_version'>gitweb</generator>\n";
7551 for (my $i = 0; $i <= $#commitlist; $i++) {
7552 my %co = %{$commitlist[$i]};
7553 my $commit = $co{'id'};
7554 # we read 150, we always show 30 and the ones more recent than 48 hours
7555 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
7558 my %cd = parse_date
($co{'author_epoch'}, $co{'author_tz'});
7560 # get list of changed files
7561 open my $fd, "-|", git_cmd
(), "diff-tree", '-r', @diff_opts,
7562 $co{'parent'} || "--root",
7563 $co{'id'}, "--", (defined $file_name ? $file_name : ())
7565 my @difftree = map { chomp; $_ } <$fd>;
7569 # print element (entry, item)
7570 my $co_url = href
(-full
=>1, action
=>"commitdiff", hash
=>$commit);
7571 if ($format eq 'rss') {
7573 "<title>" . esc_html
($co{'title'}) . "</title>\n" .
7574 "<author>" . esc_html
($co{'author'}) . "</author>\n" .
7575 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
7576 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
7577 "<link>$co_url</link>\n" .
7578 "<description>" . esc_html
($co{'title'}) . "</description>\n" .
7579 "<content:encoded>" .
7581 } elsif ($format eq 'atom') {
7583 "<title type=\"html\">" . esc_html
($co{'title'}) . "</title>\n" .
7584 "<updated>$cd{'iso-8601'}</updated>\n" .
7586 " <name>" . esc_html
($co{'author_name'}) . "</name>\n";
7587 if ($co{'author_email'}) {
7588 print " <email>" . esc_html
($co{'author_email'}) . "</email>\n";
7590 print "</author>\n" .
7591 # use committer for contributor
7593 " <name>" . esc_html
($co{'committer_name'}) . "</name>\n";
7594 if ($co{'committer_email'}) {
7595 print " <email>" . esc_html
($co{'committer_email'}) . "</email>\n";
7597 print "</contributor>\n" .
7598 "<published>$cd{'iso-8601'}</published>\n" .
7599 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
7600 "<id>$co_url</id>\n" .
7601 "<content type=\"xhtml\" xml:base=\"" . esc_url
($my_url) . "\">\n" .
7602 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
7604 my $comment = $co{'comment'};
7606 foreach my $line (@$comment) {
7607 $line = esc_html
($line);
7610 print "</pre><ul>\n";
7611 foreach my $difftree_line (@difftree) {
7612 my %difftree = parse_difftree_raw_line
($difftree_line);
7613 next if !$difftree{'from_id'};
7615 my $file = $difftree{'file'} || $difftree{'to_file'};
7619 $cgi->a({-href
=> href
(-full
=>1, action
=>"blobdiff",
7620 hash
=>$difftree{'to_id'}, hash_parent
=>$difftree{'from_id'},
7621 hash_base
=>$co{'id'}, hash_parent_base
=>$co{'parent'},
7622 file_name
=>$file, file_parent
=>$difftree{'from_file'}),
7623 -title
=> "diff"}, 'D');
7625 print $cgi->a({-href
=> href
(-full
=>1, action
=>"blame",
7626 file_name
=>$file, hash_base
=>$commit),
7627 -title
=> "blame"}, 'B');
7629 # if this is not a feed of a file history
7630 if (!defined $file_name || $file_name ne $file) {
7631 print $cgi->a({-href
=> href
(-full
=>1, action
=>"history",
7632 file_name
=>$file, hash
=>$commit),
7633 -title
=> "history"}, 'H');
7635 $file = esc_path
($file);
7639 if ($format eq 'rss') {
7640 print "</ul>]]>\n" .
7641 "</content:encoded>\n" .
7643 } elsif ($format eq 'atom') {
7644 print "</ul>\n</div>\n" .
7651 if ($format eq 'rss') {
7652 print "</channel>\n</rss>\n";
7653 } elsif ($format eq 'atom') {
7667 my @list = git_get_projects_list
();
7669 die_error
(404, "No projects found");
7673 -type
=> 'text/xml',
7674 -charset
=> 'utf-8',
7675 -content_disposition
=> 'inline; filename="opml.xml"');
7678 <?xml version="1.0" encoding="utf-8"?>
7679 <opml version="1.0">
7681 <title>$site_name OPML Export</title>
7684 <outline text="git RSS feeds">
7687 foreach my $pr (@list) {
7689 my $head = git_get_head_hash
($proj{'path'});
7690 if (!defined $head) {
7693 $git_dir = "$projectroot/$proj{'path'}";
7694 my %co = parse_commit
($head);
7699 my $path = esc_html
(chop_str
($proj{'path'}, 25, 5));
7700 my $rss = href
('project' => $proj{'path'}, 'action' => 'rss', -full
=> 1);
7701 my $html = href
('project' => $proj{'path'}, 'action' => 'summary', -full
=> 1);
7702 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";