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);
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++";
674 # use first config file that exists
675 read_config_file
($GITWEB_CONFIG) or
676 read_config_file
($GITWEB_CONFIG_SYSTEM);
679 # Get loadavg of system, to compare against $maxload.
680 # Currently it requires '/proc/loadavg' present to get loadavg;
681 # if it is not present it returns 0, which means no load checking.
683 if( -e
'/proc/loadavg' ){
684 open my $fd, '<', '/proc/loadavg'
686 my @load = split(/\s+/, scalar <$fd>);
689 # The first three columns measure CPU and IO utilization of the last one,
690 # five, and 10 minute periods. The fourth column shows the number of
691 # currently running processes and the total number of processes in the m/n
692 # format. The last column displays the last process ID used.
693 return $load[0] || 0;
695 # additional checks for load average should go here for things that don't export
701 # version of the core git binary
703 sub evaluate_git_version
{
704 our $git_version = qx("$GIT" --version) =~ m/git version (.*)$/ ? $1 : "unknown";
705 $number_of_git_cmds++;
709 if (defined $maxload && get_loadavg
() > $maxload) {
710 die_error
(503, "The load average on the server is too high");
714 # ======================================================================
715 # input validation and dispatch
717 # input parameters can be collected from a variety of sources (presently, CGI
718 # and PATH_INFO), so we define an %input_params hash that collects them all
719 # together during validation: this allows subsequent uses (e.g. href()) to be
720 # agnostic of the parameter origin
722 our %input_params = ();
724 # input parameters are stored with the long parameter name as key. This will
725 # also be used in the href subroutine to convert parameters to their CGI
726 # equivalent, and since the href() usage is the most frequent one, we store
727 # the name -> CGI key mapping here, instead of the reverse.
729 # XXX: Warning: If you touch this, check the search form for updating,
732 our @cgi_param_mapping = (
740 hash_parent_base
=> "hpb",
745 snapshot_format
=> "sf",
746 extra_options
=> "opt",
747 search_use_regexp
=> "sr",
749 # this must be last entry (for manipulation from JavaScript)
752 our %cgi_param_mapping = @cgi_param_mapping;
754 # we will also need to know the possible actions, for validation
756 "blame" => \
&git_blame
,
757 "blame_incremental" => \
&git_blame_incremental
,
758 "blame_data" => \
&git_blame_data
,
759 "blobdiff" => \
&git_blobdiff
,
760 "blobdiff_plain" => \
&git_blobdiff_plain
,
761 "blob" => \
&git_blob
,
762 "blob_plain" => \
&git_blob_plain
,
763 "commitdiff" => \
&git_commitdiff
,
764 "commitdiff_plain" => \
&git_commitdiff_plain
,
765 "commit" => \
&git_commit
,
766 "forks" => \
&git_forks
,
767 "heads" => \
&git_heads
,
768 "history" => \
&git_history
,
770 "patch" => \
&git_patch
,
771 "patches" => \
&git_patches
,
772 "remotes" => \
&git_remotes
,
774 "atom" => \
&git_atom
,
775 "search" => \
&git_search
,
776 "search_help" => \
&git_search_help
,
777 "shortlog" => \
&git_shortlog
,
778 "summary" => \
&git_summary
,
780 "tags" => \
&git_tags
,
781 "tree" => \
&git_tree
,
782 "snapshot" => \
&git_snapshot
,
783 "object" => \
&git_object
,
784 # those below don't need $project
785 "opml" => \
&git_opml
,
786 "project_list" => \
&git_project_list
,
787 "project_index" => \
&git_project_index
,
790 # finally, we have the hash of allowed extra_options for the commands that
792 our %allowed_options = (
793 "--no-merges" => [ qw(rss atom log shortlog history) ],
796 # fill %input_params with the CGI parameters. All values except for 'opt'
797 # should be single values, but opt can be an array. We should probably
798 # build an array of parameters that can be multi-valued, but since for the time
799 # being it's only this one, we just single it out
800 sub evaluate_query_params
{
803 while (my ($name, $symbol) = each %cgi_param_mapping) {
804 if ($symbol eq 'opt') {
805 $input_params{$name} = [ $cgi->param($symbol) ];
807 $input_params{$name} = $cgi->param($symbol);
812 # now read PATH_INFO and update the parameter list for missing parameters
813 sub evaluate_path_info
{
814 return if defined $input_params{'project'};
815 return if !$path_info;
816 $path_info =~ s
,^/+,,;
817 return if !$path_info;
819 # find which part of PATH_INFO is project
820 my $project = $path_info;
822 while ($project && !check_head_link
("$projectroot/$project")) {
823 $project =~ s
,/*[^/]*$,,;
825 return unless $project;
826 $input_params{'project'} = $project;
828 # do not change any parameters if an action is given using the query string
829 return if $input_params{'action'};
830 $path_info =~ s
,^\Q
$project\E
/*,,;
832 # next, check if we have an action
833 my $action = $path_info;
835 if (exists $actions{$action}) {
836 $path_info =~ s
,^$action/*,,;
837 $input_params{'action'} = $action;
840 # list of actions that want hash_base instead of hash, but can have no
841 # pathname (f) parameter
847 # we want to catch, among others
848 # [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name]
849 my ($parentrefname, $parentpathname, $refname, $pathname) =
850 ($path_info =~ /^(?:(.+?)(?::(.+))?\.\.)?([^:]+?)?(?::(.+))?$/);
852 # first, analyze the 'current' part
853 if (defined $pathname) {
854 # we got "branch:filename" or "branch:dir/"
855 # we could use git_get_type(branch:pathname), but:
856 # - it needs $git_dir
857 # - it does a git() call
858 # - the convention of terminating directories with a slash
859 # makes it superfluous
860 # - embedding the action in the PATH_INFO would make it even
862 $pathname =~ s
,^/+,,;
863 if (!$pathname || substr($pathname, -1) eq "/") {
864 $input_params{'action'} ||= "tree";
867 # the default action depends on whether we had parent info
869 if ($parentrefname) {
870 $input_params{'action'} ||= "blobdiff_plain";
872 $input_params{'action'} ||= "blob_plain";
875 $input_params{'hash_base'} ||= $refname;
876 $input_params{'file_name'} ||= $pathname;
877 } elsif (defined $refname) {
878 # we got "branch". In this case we have to choose if we have to
879 # set hash or hash_base.
881 # Most of the actions without a pathname only want hash to be
882 # set, except for the ones specified in @wants_base that want
883 # hash_base instead. It should also be noted that hand-crafted
884 # links having 'history' as an action and no pathname or hash
885 # set will fail, but that happens regardless of PATH_INFO.
886 if (defined $parentrefname) {
887 # if there is parent let the default be 'shortlog' action
888 # (for http://git.example.com/repo.git/A..B links); if there
889 # is no parent, dispatch will detect type of object and set
890 # action appropriately if required (if action is not set)
891 $input_params{'action'} ||= "shortlog";
893 if ($input_params{'action'} &&
894 grep { $_ eq $input_params{'action'} } @wants_base) {
895 $input_params{'hash_base'} ||= $refname;
897 $input_params{'hash'} ||= $refname;
901 # next, handle the 'parent' part, if present
902 if (defined $parentrefname) {
903 # a missing pathspec defaults to the 'current' filename, allowing e.g.
904 # someproject/blobdiff/oldrev..newrev:/filename
905 if ($parentpathname) {
906 $parentpathname =~ s
,^/+,,;
907 $parentpathname =~ s
,/$,,;
908 $input_params{'file_parent'} ||= $parentpathname;
910 $input_params{'file_parent'} ||= $input_params{'file_name'};
912 # we assume that hash_parent_base is wanted if a path was specified,
913 # or if the action wants hash_base instead of hash
914 if (defined $input_params{'file_parent'} ||
915 grep { $_ eq $input_params{'action'} } @wants_base) {
916 $input_params{'hash_parent_base'} ||= $parentrefname;
918 $input_params{'hash_parent'} ||= $parentrefname;
922 # for the snapshot action, we allow URLs in the form
923 # $project/snapshot/$hash.ext
924 # where .ext determines the snapshot and gets removed from the
925 # passed $refname to provide the $hash.
927 # To be able to tell that $refname includes the format extension, we
928 # require the following two conditions to be satisfied:
929 # - the hash input parameter MUST have been set from the $refname part
930 # of the URL (i.e. they must be equal)
931 # - the snapshot format MUST NOT have been defined already (e.g. from
933 # It's also useless to try any matching unless $refname has a dot,
934 # so we check for that too
935 if (defined $input_params{'action'} &&
936 $input_params{'action'} eq 'snapshot' &&
937 defined $refname && index($refname, '.') != -1 &&
938 $refname eq $input_params{'hash'} &&
939 !defined $input_params{'snapshot_format'}) {
940 # We loop over the known snapshot formats, checking for
941 # extensions. Allowed extensions are both the defined suffix
942 # (which includes the initial dot already) and the snapshot
943 # format key itself, with a prepended dot
944 while (my ($fmt, $opt) = each %known_snapshot_formats) {
946 unless ($hash =~ s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) {
950 # a valid suffix was found, so set the snapshot format
951 # and reset the hash parameter
952 $input_params{'snapshot_format'} = $fmt;
953 $input_params{'hash'} = $hash;
954 # we also set the format suffix to the one requested
955 # in the URL: this way a request for e.g. .tgz returns
956 # a .tgz instead of a .tar.gz
957 $known_snapshot_formats{$fmt}{'suffix'} = $sfx;
963 our ($action, $project, $file_name, $file_parent, $hash, $hash_parent, $hash_base,
964 $hash_parent_base, @extra_options, $page, $searchtype, $search_use_regexp,
965 $searchtext, $search_regexp);
966 sub evaluate_and_validate_params
{
967 our $action = $input_params{'action'};
968 if (defined $action) {
969 if (!validate_action
($action)) {
970 die_error
(400, "Invalid action parameter");
974 # parameters which are pathnames
975 our $project = $input_params{'project'};
976 if (defined $project) {
977 if (!validate_project
($project)) {
979 die_error
(404, "No such project");
983 our $file_name = $input_params{'file_name'};
984 if (defined $file_name) {
985 if (!validate_pathname
($file_name)) {
986 die_error
(400, "Invalid file parameter");
990 our $file_parent = $input_params{'file_parent'};
991 if (defined $file_parent) {
992 if (!validate_pathname
($file_parent)) {
993 die_error
(400, "Invalid file parent parameter");
997 # parameters which are refnames
998 our $hash = $input_params{'hash'};
1000 if (!validate_refname
($hash)) {
1001 die_error
(400, "Invalid hash parameter");
1005 our $hash_parent = $input_params{'hash_parent'};
1006 if (defined $hash_parent) {
1007 if (!validate_refname
($hash_parent)) {
1008 die_error
(400, "Invalid hash parent parameter");
1012 our $hash_base = $input_params{'hash_base'};
1013 if (defined $hash_base) {
1014 if (!validate_refname
($hash_base)) {
1015 die_error
(400, "Invalid hash base parameter");
1019 our @extra_options = @{$input_params{'extra_options'}};
1020 # @extra_options is always defined, since it can only be (currently) set from
1021 # CGI, and $cgi->param() returns the empty array in array context if the param
1023 foreach my $opt (@extra_options) {
1024 if (not exists $allowed_options{$opt}) {
1025 die_error
(400, "Invalid option parameter");
1027 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
1028 die_error
(400, "Invalid option parameter for this action");
1032 our $hash_parent_base = $input_params{'hash_parent_base'};
1033 if (defined $hash_parent_base) {
1034 if (!validate_refname
($hash_parent_base)) {
1035 die_error
(400, "Invalid hash parent base parameter");
1040 our $page = $input_params{'page'};
1041 if (defined $page) {
1042 if ($page =~ m/[^0-9]/) {
1043 die_error
(400, "Invalid page parameter");
1047 our $searchtype = $input_params{'searchtype'};
1048 if (defined $searchtype) {
1049 if ($searchtype =~ m/[^a-z]/) {
1050 die_error
(400, "Invalid searchtype parameter");
1054 our $search_use_regexp = $input_params{'search_use_regexp'};
1056 our $searchtext = $input_params{'searchtext'};
1058 if (defined $searchtext) {
1059 if (length($searchtext) < 2) {
1060 die_error
(403, "At least two characters are required for search parameter");
1062 $search_regexp = $search_use_regexp ? $searchtext : quotemeta $searchtext;
1066 # path to the current git repository
1068 sub evaluate_git_dir
{
1069 our $git_dir = "$projectroot/$project" if $project;
1072 our (@snapshot_fmts, $git_avatar);
1073 sub configure_gitweb_features
{
1074 # list of supported snapshot formats
1075 our @snapshot_fmts = gitweb_get_feature
('snapshot');
1076 @snapshot_fmts = filter_snapshot_fmts
(@snapshot_fmts);
1078 # check that the avatar feature is set to a known provider name,
1079 # and for each provider check if the dependencies are satisfied.
1080 # if the provider name is invalid or the dependencies are not met,
1081 # reset $git_avatar to the empty string.
1082 our ($git_avatar) = gitweb_get_feature
('avatar');
1083 if ($git_avatar eq 'gravatar') {
1084 $git_avatar = '' unless (eval { require Digest
::MD5
; 1; });
1085 } elsif ($git_avatar eq 'picon') {
1092 # custom error handler: 'die <message>' is Internal Server Error
1093 sub handle_errors_html
{
1094 my $msg = shift; # it is already HTML escaped
1096 # to avoid infinite loop where error occurs in die_error,
1097 # change handler to default handler, disabling handle_errors_html
1098 set_message
("Error occured when inside die_error:\n$msg");
1100 # you cannot jump out of die_error when called as error handler;
1101 # the subroutine set via CGI::Carp::set_message is called _after_
1102 # HTTP headers are already written, so it cannot write them itself
1103 die_error
(undef, undef, $msg, -error_handler
=> 1, -no_http_header
=> 1);
1105 set_message
(\
&handle_errors_html
);
1109 if (!defined $action) {
1110 if (defined $hash) {
1111 $action = git_get_type
($hash);
1112 } elsif (defined $hash_base && defined $file_name) {
1113 $action = git_get_type
("$hash_base:$file_name");
1114 } elsif (defined $project) {
1115 $action = 'summary';
1117 $action = 'project_list';
1120 if (!defined($actions{$action})) {
1121 die_error
(400, "Unknown action");
1123 if ($action !~ m/^(?:opml|project_list|project_index)$/ &&
1125 die_error
(400, "Project needed");
1127 $actions{$action}->();
1131 our $t0 = [ gettimeofday
() ]
1133 our $number_of_git_cmds = 0;
1136 our $first_request = 1;
1141 if ($first_request) {
1142 evaluate_gitweb_config
();
1143 evaluate_git_version
();
1145 if ($per_request_config) {
1146 if (ref($per_request_config) eq 'CODE') {
1147 $per_request_config->();
1148 } elsif (!$first_request) {
1149 evaluate_gitweb_config
();
1154 # $projectroot and $projects_list might be set in gitweb config file
1155 $projects_list ||= $projectroot;
1157 evaluate_query_params
();
1158 evaluate_path_info
();
1159 evaluate_and_validate_params
();
1162 configure_gitweb_features
();
1167 our $is_last_request = sub { 1 };
1168 our ($pre_dispatch_hook, $post_dispatch_hook, $pre_listen_hook);
1171 sub configure_as_fcgi
{
1173 our $CGI = 'CGI::Fast';
1175 my $request_number = 0;
1176 # let each child service 100 requests
1177 our $is_last_request = sub { ++$request_number > 100 };
1180 my $script_name = $ENV{'SCRIPT_NAME'} || $ENV{'SCRIPT_FILENAME'} || __FILE__
;
1182 if $script_name =~ /\.fcgi$/;
1184 return unless (@ARGV);
1186 require Getopt
::Long
;
1187 Getopt
::Long
::GetOptions
(
1188 'fastcgi|fcgi|f' => \
&configure_as_fcgi
,
1189 'nproc|n=i' => sub {
1190 my ($arg, $val) = @_;
1191 return unless eval { require FCGI
::ProcManager
; 1; };
1192 my $proc_manager = FCGI
::ProcManager-
>new({
1193 n_processes
=> $val,
1195 our $pre_listen_hook = sub { $proc_manager->pm_manage() };
1196 our $pre_dispatch_hook = sub { $proc_manager->pm_pre_dispatch() };
1197 our $post_dispatch_hook = sub { $proc_manager->pm_post_dispatch() };
1206 $pre_listen_hook->()
1207 if $pre_listen_hook;
1210 while ($cgi = $CGI->new()) {
1211 $pre_dispatch_hook->()
1212 if $pre_dispatch_hook;
1216 $post_dispatch_hook->()
1217 if $post_dispatch_hook;
1220 last REQUEST
if ($is_last_request->());
1229 if (defined caller) {
1230 # wrapped in a subroutine processing requests,
1231 # e.g. mod_perl with ModPerl::Registry, or PSGI with Plack::App::WrapCGI
1234 # pure CGI script, serving single request
1238 ## ======================================================================
1241 # possible values of extra options
1242 # -full => 0|1 - use absolute/full URL ($my_uri/$my_url as base)
1243 # -replay => 1 - start from a current view (replay with modifications)
1244 # -path_info => 0|1 - don't use/use path_info URL (if possible)
1245 # -anchor => ANCHOR - add #ANCHOR to end of URL, implies -replay if used alone
1248 # default is to use -absolute url() i.e. $my_uri
1249 my $href = $params{-full
} ? $my_url : $my_uri;
1251 # implicit -replay, must be first of implicit params
1252 $params{-replay
} = 1 if (keys %params == 1 && $params{-anchor
});
1254 $params{'project'} = $project unless exists $params{'project'};
1256 if ($params{-replay
}) {
1257 while (my ($name, $symbol) = each %cgi_param_mapping) {
1258 if (!exists $params{$name}) {
1259 $params{$name} = $input_params{$name};
1264 my $use_pathinfo = gitweb_check_feature
('pathinfo');
1265 if (defined $params{'project'} &&
1266 (exists $params{-path_info
} ? $params{-path_info
} : $use_pathinfo)) {
1267 # try to put as many parameters as possible in PATH_INFO:
1270 # - hash_parent or hash_parent_base:/file_parent
1271 # - hash or hash_base:/filename
1272 # - the snapshot_format as an appropriate suffix
1274 # When the script is the root DirectoryIndex for the domain,
1275 # $href here would be something like http://gitweb.example.com/
1276 # Thus, we strip any trailing / from $href, to spare us double
1277 # slashes in the final URL
1280 # Then add the project name, if present
1281 $href .= "/".esc_path_info
($params{'project'});
1282 delete $params{'project'};
1284 # since we destructively absorb parameters, we keep this
1285 # boolean that remembers if we're handling a snapshot
1286 my $is_snapshot = $params{'action'} eq 'snapshot';
1288 # Summary just uses the project path URL, any other action is
1290 if (defined $params{'action'}) {
1291 $href .= "/".esc_path_info
($params{'action'})
1292 unless $params{'action'} eq 'summary';
1293 delete $params{'action'};
1296 # Next, we put hash_parent_base:/file_parent..hash_base:/file_name,
1297 # stripping nonexistent or useless pieces
1298 $href .= "/" if ($params{'hash_base'} || $params{'hash_parent_base'}
1299 || $params{'hash_parent'} || $params{'hash'});
1300 if (defined $params{'hash_base'}) {
1301 if (defined $params{'hash_parent_base'}) {
1302 $href .= esc_path_info
($params{'hash_parent_base'});
1303 # skip the file_parent if it's the same as the file_name
1304 if (defined $params{'file_parent'}) {
1305 if (defined $params{'file_name'} && $params{'file_parent'} eq $params{'file_name'}) {
1306 delete $params{'file_parent'};
1307 } elsif ($params{'file_parent'} !~ /\.\./) {
1308 $href .= ":/".esc_path_info
($params{'file_parent'});
1309 delete $params{'file_parent'};
1313 delete $params{'hash_parent'};
1314 delete $params{'hash_parent_base'};
1315 } elsif (defined $params{'hash_parent'}) {
1316 $href .= esc_path_info
($params{'hash_parent'}). "..";
1317 delete $params{'hash_parent'};
1320 $href .= esc_path_info
($params{'hash_base'});
1321 if (defined $params{'file_name'} && $params{'file_name'} !~ /\.\./) {
1322 $href .= ":/".esc_path_info
($params{'file_name'});
1323 delete $params{'file_name'};
1325 delete $params{'hash'};
1326 delete $params{'hash_base'};
1327 } elsif (defined $params{'hash'}) {
1328 $href .= esc_path_info
($params{'hash'});
1329 delete $params{'hash'};
1332 # If the action was a snapshot, we can absorb the
1333 # snapshot_format parameter too
1335 my $fmt = $params{'snapshot_format'};
1336 # snapshot_format should always be defined when href()
1337 # is called, but just in case some code forgets, we
1338 # fall back to the default
1339 $fmt ||= $snapshot_fmts[0];
1340 $href .= $known_snapshot_formats{$fmt}{'suffix'};
1341 delete $params{'snapshot_format'};
1345 # now encode the parameters explicitly
1347 for (my $i = 0; $i < @cgi_param_mapping; $i += 2) {
1348 my ($name, $symbol) = ($cgi_param_mapping[$i], $cgi_param_mapping[$i+1]);
1349 if (defined $params{$name}) {
1350 if (ref($params{$name}) eq "ARRAY") {
1351 foreach my $par (@{$params{$name}}) {
1352 push @result, $symbol . "=" . esc_param
($par);
1355 push @result, $symbol . "=" . esc_param
($params{$name});
1359 $href .= "?" . join(';', @result) if scalar @result;
1361 # final transformation: trailing spaces must be escaped (URI-encoded)
1362 $href =~ s/(\s+)$/CGI::escape($1)/e;
1364 if ($params{-anchor
}) {
1365 $href .= "#".esc_param
($params{-anchor
});
1372 ## ======================================================================
1373 ## validation, quoting/unquoting and escaping
1375 sub validate_action
{
1376 my $input = shift || return undef;
1377 return undef unless exists $actions{$input};
1381 sub validate_project
{
1382 my $input = shift || return undef;
1383 if (!validate_pathname
($input) ||
1384 !(-d
"$projectroot/$input") ||
1385 !check_export_ok
("$projectroot/$input") ||
1386 ($strict_export && !project_in_list
($input))) {
1393 sub validate_pathname
{
1394 my $input = shift || return undef;
1396 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
1397 # at the beginning, at the end, and between slashes.
1398 # also this catches doubled slashes
1399 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
1402 # no null characters
1403 if ($input =~ m!\0!) {
1409 sub validate_refname
{
1410 my $input = shift || return undef;
1412 # textual hashes are O.K.
1413 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
1416 # it must be correct pathname
1417 $input = validate_pathname
($input)
1419 # restrictions on ref name according to git-check-ref-format
1420 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
1426 # decode sequences of octets in utf8 into Perl's internal form,
1427 # which is utf-8 with utf8 flag set if needed. gitweb writes out
1428 # in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
1431 return undef unless defined $str;
1432 if (utf8
::valid
($str)) {
1436 return decode
($fallback_encoding, $str, Encode
::FB_DEFAULT
);
1440 # quote unsafe chars, but keep the slash, even when it's not
1441 # correct, but quoted slashes look too horrible in bookmarks
1444 return undef unless defined $str;
1445 $str =~ s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI
::escape
($1)/eg
;
1450 # the quoting rules for path_info fragment are slightly different
1453 return undef unless defined $str;
1455 # path_info doesn't treat '+' as space (specially), but '?' must be escaped
1456 $str =~ s/([^A-Za-z0-9\-_.~();\/;:@&= +]+)/CGI
::escape
($1)/eg
;
1461 # quote unsafe chars in whole URL, so some characters cannot be quoted
1464 return undef unless defined $str;
1465 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&= ]+)/CGI
::escape
($1)/eg
;
1470 # quote unsafe characters in HTML attributes
1473 # for XHTML conformance escaping '"' to '"' is not enough
1474 return esc_html
(@_);
1477 # replace invalid utf8 character with SUBSTITUTION sequence
1482 return undef unless defined $str;
1484 $str = to_utf8
($str);
1485 $str = $cgi->escapeHTML($str);
1486 if ($opts{'-nbsp'}) {
1487 $str =~ s/ / /g;
1489 $str =~ s
|([[:cntrl
:]])|(($1 ne "\t") ? quot_cec
($1) : $1)|eg
;
1493 # quote control characters and escape filename to HTML
1498 return undef unless defined $str;
1500 $str = to_utf8
($str);
1501 $str = $cgi->escapeHTML($str);
1502 if ($opts{'-nbsp'}) {
1503 $str =~ s/ / /g;
1505 $str =~ s
|([[:cntrl
:]])|quot_cec
($1)|eg
;
1509 # Make control characters "printable", using character escape codes (CEC)
1513 my %es = ( # character escape codes, aka escape sequences
1514 "\t" => '\t', # tab (HT)
1515 "\n" => '\n', # line feed (LF)
1516 "\r" => '\r', # carrige return (CR)
1517 "\f" => '\f', # form feed (FF)
1518 "\b" => '\b', # backspace (BS)
1519 "\a" => '\a', # alarm (bell) (BEL)
1520 "\e" => '\e', # escape (ESC)
1521 "\013" => '\v', # vertical tab (VT)
1522 "\000" => '\0', # nul character (NUL)
1524 my $chr = ( (exists $es{$cntrl})
1526 : sprintf('\%2x', ord($cntrl)) );
1527 if ($opts{-nohtml
}) {
1530 return "<span class=\"cntrl\">$chr</span>";
1534 # Alternatively use unicode control pictures codepoints,
1535 # Unicode "printable representation" (PR)
1540 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
1541 if ($opts{-nohtml
}) {
1544 return "<span class=\"cntrl\">$chr</span>";
1548 # git may return quoted and escaped filenames
1554 my %es = ( # character escape codes, aka escape sequences
1555 't' => "\t", # tab (HT, TAB)
1556 'n' => "\n", # newline (NL)
1557 'r' => "\r", # return (CR)
1558 'f' => "\f", # form feed (FF)
1559 'b' => "\b", # backspace (BS)
1560 'a' => "\a", # alarm (bell) (BEL)
1561 'e' => "\e", # escape (ESC)
1562 'v' => "\013", # vertical tab (VT)
1565 if ($seq =~ m/^[0-7]{1,3}$/) {
1566 # octal char sequence
1567 return chr(oct($seq));
1568 } elsif (exists $es{$seq}) {
1569 # C escape sequence, aka character escape code
1572 # quoted ordinary character
1576 if ($str =~ m/^"(.*)"$/) {
1579 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
1584 # escape tabs (convert tabs to spaces)
1588 while ((my $pos = index($line, "\t")) != -1) {
1589 if (my $count = (8 - ($pos % 8))) {
1590 my $spaces = ' ' x
$count;
1591 $line =~ s/\t/$spaces/;
1598 sub project_in_list
{
1599 my $project = shift;
1600 my @list = git_get_projects_list
();
1601 return @list && scalar(grep { $_->{'path'} eq $project } @list);
1604 ## ----------------------------------------------------------------------
1605 ## HTML aware string manipulation
1607 # Try to chop given string on a word boundary between position
1608 # $len and $len+$add_len. If there is no word boundary there,
1609 # chop at $len+$add_len. Do not chop if chopped part plus ellipsis
1610 # (marking chopped part) would be longer than given string.
1614 my $add_len = shift || 10;
1615 my $where = shift || 'right'; # 'left' | 'center' | 'right'
1617 # Make sure perl knows it is utf8 encoded so we don't
1618 # cut in the middle of a utf8 multibyte char.
1619 $str = to_utf8
($str);
1621 # allow only $len chars, but don't cut a word if it would fit in $add_len
1622 # if it doesn't fit, cut it if it's still longer than the dots we would add
1623 # remove chopped character entities entirely
1625 # when chopping in the middle, distribute $len into left and right part
1626 # return early if chopping wouldn't make string shorter
1627 if ($where eq 'center') {
1628 return $str if ($len + 5 >= length($str)); # filler is length 5
1631 return $str if ($len + 4 >= length($str)); # filler is length 4
1634 # regexps: ending and beginning with word part up to $add_len
1635 my $endre = qr/.{$len}\w{0,$add_len}/;
1636 my $begre = qr/\w{0,$add_len}.{$len}/;
1638 if ($where eq 'left') {
1639 $str =~ m/^(.*?)($begre)$/;
1640 my ($lead, $body) = ($1, $2);
1641 if (length($lead) > 4) {
1644 return "$lead$body";
1646 } elsif ($where eq 'center') {
1647 $str =~ m/^($endre)(.*)$/;
1648 my ($left, $str) = ($1, $2);
1649 $str =~ m/^(.*?)($begre)$/;
1650 my ($mid, $right) = ($1, $2);
1651 if (length($mid) > 5) {
1654 return "$left$mid$right";
1657 $str =~ m/^($endre)(.*)$/;
1660 if (length($tail) > 4) {
1663 return "$body$tail";
1667 # takes the same arguments as chop_str, but also wraps a <span> around the
1668 # result with a title attribute if it does get chopped. Additionally, the
1669 # string is HTML-escaped.
1670 sub chop_and_escape_str
{
1673 my $chopped = chop_str
(@_);
1674 if ($chopped eq $str) {
1675 return esc_html
($chopped);
1677 $str =~ s/[[:cntrl:]]/?/g;
1678 return $cgi->span({-title
=>$str}, esc_html
($chopped));
1682 ## ----------------------------------------------------------------------
1683 ## functions returning short strings
1685 # CSS class for given age value (in seconds)
1689 if (!defined $age) {
1691 } elsif ($age < 60*60*2) {
1693 } elsif ($age < 60*60*24*2) {
1700 # convert age in seconds to "nn units ago" string
1705 if ($age > 60*60*24*365*2) {
1706 $age_str = (int $age/60/60/24/365);
1707 $age_str .= " years ago";
1708 } elsif ($age > 60*60*24*(365/12)*2) {
1709 $age_str = int $age/60/60/24/(365/12);
1710 $age_str .= " months ago";
1711 } elsif ($age > 60*60*24*7*2) {
1712 $age_str = int $age/60/60/24/7;
1713 $age_str .= " weeks ago";
1714 } elsif ($age > 60*60*24*2) {
1715 $age_str = int $age/60/60/24;
1716 $age_str .= " days ago";
1717 } elsif ($age > 60*60*2) {
1718 $age_str = int $age/60/60;
1719 $age_str .= " hours ago";
1720 } elsif ($age > 60*2) {
1721 $age_str = int $age/60;
1722 $age_str .= " min ago";
1723 } elsif ($age > 2) {
1724 $age_str = int $age;
1725 $age_str .= " sec ago";
1727 $age_str .= " right now";
1733 S_IFINVALID
=> 0030000,
1734 S_IFGITLINK
=> 0160000,
1737 # submodule/subproject, a commit object reference
1741 return (($mode & S_IFMT
) == S_IFGITLINK
)
1744 # convert file mode in octal to symbolic file mode string
1746 my $mode = oct shift;
1748 if (S_ISGITLINK
($mode)) {
1749 return 'm---------';
1750 } elsif (S_ISDIR
($mode & S_IFMT
)) {
1751 return 'drwxr-xr-x';
1752 } elsif (S_ISLNK
($mode)) {
1753 return 'lrwxrwxrwx';
1754 } elsif (S_ISREG
($mode)) {
1755 # git cares only about the executable bit
1756 if ($mode & S_IXUSR
) {
1757 return '-rwxr-xr-x';
1759 return '-rw-r--r--';
1762 return '----------';
1766 # convert file mode in octal to file type string
1770 if ($mode !~ m/^[0-7]+$/) {
1776 if (S_ISGITLINK
($mode)) {
1778 } elsif (S_ISDIR
($mode & S_IFMT
)) {
1780 } elsif (S_ISLNK
($mode)) {
1782 } elsif (S_ISREG
($mode)) {
1789 # convert file mode in octal to file type description string
1790 sub file_type_long
{
1793 if ($mode !~ m/^[0-7]+$/) {
1799 if (S_ISGITLINK
($mode)) {
1801 } elsif (S_ISDIR
($mode & S_IFMT
)) {
1803 } elsif (S_ISLNK
($mode)) {
1805 } elsif (S_ISREG
($mode)) {
1806 if ($mode & S_IXUSR
) {
1807 return "executable";
1817 ## ----------------------------------------------------------------------
1818 ## functions returning short HTML fragments, or transforming HTML fragments
1819 ## which don't belong to other sections
1821 # format line of commit message.
1822 sub format_log_line_html
{
1825 $line = esc_html
($line, -nbsp
=>1);
1826 $line =~ s
{\b([0-9a-fA-F
]{8,40})\b}{
1827 $cgi->a({-href
=> href
(action
=>"object", hash
=>$1),
1828 -class => "text"}, $1);
1834 # format marker of refs pointing to given object
1836 # the destination action is chosen based on object type and current context:
1837 # - for annotated tags, we choose the tag view unless it's the current view
1838 # already, in which case we go to shortlog view
1839 # - for other refs, we keep the current view if we're in history, shortlog or
1840 # log view, and select shortlog otherwise
1841 sub format_ref_marker
{
1842 my ($refs, $id) = @_;
1845 if (defined $refs->{$id}) {
1846 foreach my $ref (@{$refs->{$id}}) {
1847 # this code exploits the fact that non-lightweight tags are the
1848 # only indirect objects, and that they are the only objects for which
1849 # we want to use tag instead of shortlog as action
1850 my ($type, $name) = qw();
1851 my $indirect = ($ref =~ s/\^\{\}$//);
1852 # e.g. tags/v2.6.11 or heads/next
1853 if ($ref =~ m!^(.*?)s?/(.*)$!) {
1862 $class .= " indirect" if $indirect;
1864 my $dest_action = "shortlog";
1867 $dest_action = "tag" unless $action eq "tag";
1868 } elsif ($action =~ /^(history|(short)?log)$/) {
1869 $dest_action = $action;
1873 $dest .= "refs/" unless $ref =~ m
!^refs
/!;
1876 my $link = $cgi->a({
1878 action
=>$dest_action,
1882 $markers .= " <span class=\"".esc_attr
($class)."\" title=\"".esc_attr
($ref)."\">" .
1888 return ' <span class="refs">'. $markers . '</span>';
1894 # format, perhaps shortened and with markers, title line
1895 sub format_subject_html
{
1896 my ($long, $short, $href, $extra) = @_;
1897 $extra = '' unless defined($extra);
1899 if (length($short) < length($long)) {
1900 $long =~ s/[[:cntrl:]]/?/g;
1901 return $cgi->a({-href
=> $href, -class => "list subject",
1902 -title
=> to_utf8
($long)},
1903 esc_html
($short)) . $extra;
1905 return $cgi->a({-href
=> $href, -class => "list subject"},
1906 esc_html
($long)) . $extra;
1910 # Rather than recomputing the url for an email multiple times, we cache it
1911 # after the first hit. This gives a visible benefit in views where the avatar
1912 # for the same email is used repeatedly (e.g. shortlog).
1913 # The cache is shared by all avatar engines (currently gravatar only), which
1914 # are free to use it as preferred. Since only one avatar engine is used for any
1915 # given page, there's no risk for cache conflicts.
1916 our %avatar_cache = ();
1918 # Compute the picon url for a given email, by using the picon search service over at
1919 # http://www.cs.indiana.edu/picons/search.html
1921 my $email = lc shift;
1922 if (!$avatar_cache{$email}) {
1923 my ($user, $domain) = split('@', $email);
1924 $avatar_cache{$email} =
1925 "http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/" .
1927 "users+domains+unknown/up/single";
1929 return $avatar_cache{$email};
1932 # Compute the gravatar url for a given email, if it's not in the cache already.
1933 # Gravatar stores only the part of the URL before the size, since that's the
1934 # one computationally more expensive. This also allows reuse of the cache for
1935 # different sizes (for this particular engine).
1937 my $email = lc shift;
1939 $avatar_cache{$email} ||=
1940 "http://www.gravatar.com/avatar/" .
1941 Digest
::MD5
::md5_hex
($email) . "?s=";
1942 return $avatar_cache{$email} . $size;
1945 # Insert an avatar for the given $email at the given $size if the feature
1947 sub git_get_avatar
{
1948 my ($email, %opts) = @_;
1949 my $pre_white = ($opts{-pad_before
} ? " " : "");
1950 my $post_white = ($opts{-pad_after
} ? " " : "");
1951 $opts{-size
} ||= 'default';
1952 my $size = $avatar_size{$opts{-size
}} || $avatar_size{'default'};
1954 if ($git_avatar eq 'gravatar') {
1955 $url = gravatar_url
($email, $size);
1956 } elsif ($git_avatar eq 'picon') {
1957 $url = picon_url
($email);
1959 # Other providers can be added by extending the if chain, defining $url
1960 # as needed. If no variant puts something in $url, we assume avatars
1961 # are completely disabled/unavailable.
1964 "<img width=\"$size\" " .
1965 "class=\"avatar\" " .
1966 "src=\"".esc_url
($url)."\" " .
1974 sub format_search_author
{
1975 my ($author, $searchtype, $displaytext) = @_;
1976 my $have_search = gitweb_check_feature
('search');
1980 if ($searchtype eq 'author') {
1981 $performed = "authored";
1982 } elsif ($searchtype eq 'committer') {
1983 $performed = "committed";
1986 return $cgi->a({-href
=> href
(action
=>"search", hash
=>$hash,
1987 searchtext
=>$author,
1988 searchtype
=>$searchtype), class=>"list",
1989 title
=>"Search for commits $performed by $author"},
1993 return $displaytext;
1997 # format the author name of the given commit with the given tag
1998 # the author name is chopped and escaped according to the other
1999 # optional parameters (see chop_str).
2000 sub format_author_html
{
2003 my $author = chop_and_escape_str
($co->{'author_name'}, @_);
2004 return "<$tag class=\"author\">" .
2005 format_search_author
($co->{'author_name'}, "author",
2006 git_get_avatar
($co->{'author_email'}, -pad_after
=> 1) .
2011 # format git diff header line, i.e. "diff --(git|combined|cc) ..."
2012 sub format_git_diff_header_line
{
2014 my $diffinfo = shift;
2015 my ($from, $to) = @_;
2017 if ($diffinfo->{'nparents'}) {
2019 $line =~ s!^(diff (.*?) )"?.*$!$1!;
2020 if ($to->{'href'}) {
2021 $line .= $cgi->a({-href
=> $to->{'href'}, -class => "path"},
2022 esc_path
($to->{'file'}));
2023 } else { # file was deleted (no href)
2024 $line .= esc_path
($to->{'file'});
2028 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
2029 if ($from->{'href'}) {
2030 $line .= $cgi->a({-href
=> $from->{'href'}, -class => "path"},
2031 'a/' . esc_path
($from->{'file'}));
2032 } else { # file was added (no href)
2033 $line .= 'a/' . esc_path
($from->{'file'});
2036 if ($to->{'href'}) {
2037 $line .= $cgi->a({-href
=> $to->{'href'}, -class => "path"},
2038 'b/' . esc_path
($to->{'file'}));
2039 } else { # file was deleted
2040 $line .= 'b/' . esc_path
($to->{'file'});
2044 return "<div class=\"diff header\">$line</div>\n";
2047 # format extended diff header line, before patch itself
2048 sub format_extended_diff_header_line
{
2050 my $diffinfo = shift;
2051 my ($from, $to) = @_;
2054 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
2055 $line .= $cgi->a({-href
=>$from->{'href'}, -class=>"path"},
2056 esc_path
($from->{'file'}));
2058 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
2059 $line .= $cgi->a({-href
=>$to->{'href'}, -class=>"path"},
2060 esc_path
($to->{'file'}));
2062 # match single <mode>
2063 if ($line =~ m/\s(\d{6})$/) {
2064 $line .= '<span class="info"> (' .
2065 file_type_long
($1) .
2069 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
2070 # can match only for combined diff
2072 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2073 if ($from->{'href'}[$i]) {
2074 $line .= $cgi->a({-href
=>$from->{'href'}[$i],
2076 substr($diffinfo->{'from_id'}[$i],0,7));
2081 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
2084 if ($to->{'href'}) {
2085 $line .= $cgi->a({-href
=>$to->{'href'}, -class=>"hash"},
2086 substr($diffinfo->{'to_id'},0,7));
2091 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
2092 # can match only for ordinary diff
2093 my ($from_link, $to_link);
2094 if ($from->{'href'}) {
2095 $from_link = $cgi->a({-href
=>$from->{'href'}, -class=>"hash"},
2096 substr($diffinfo->{'from_id'},0,7));
2098 $from_link = '0' x
7;
2100 if ($to->{'href'}) {
2101 $to_link = $cgi->a({-href
=>$to->{'href'}, -class=>"hash"},
2102 substr($diffinfo->{'to_id'},0,7));
2106 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
2107 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
2110 return $line . "<br/>\n";
2113 # format from-file/to-file diff header
2114 sub format_diff_from_to_header
{
2115 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
2120 #assert($line =~ m/^---/) if DEBUG;
2121 # no extra formatting for "^--- /dev/null"
2122 if (! $diffinfo->{'nparents'}) {
2123 # ordinary (single parent) diff
2124 if ($line =~ m!^--- "?a/!) {
2125 if ($from->{'href'}) {
2127 $cgi->a({-href
=>$from->{'href'}, -class=>"path"},
2128 esc_path
($from->{'file'}));
2131 esc_path
($from->{'file'});
2134 $result .= qq
!<div
class="diff from_file">$line</div
>\n!;
2137 # combined diff (merge commit)
2138 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2139 if ($from->{'href'}[$i]) {
2141 $cgi->a({-href
=>href
(action
=>"blobdiff",
2142 hash_parent
=>$diffinfo->{'from_id'}[$i],
2143 hash_parent_base
=>$parents[$i],
2144 file_parent
=>$from->{'file'}[$i],
2145 hash
=>$diffinfo->{'to_id'},
2147 file_name
=>$to->{'file'}),
2149 -title
=>"diff" . ($i+1)},
2152 $cgi->a({-href
=>$from->{'href'}[$i], -class=>"path"},
2153 esc_path
($from->{'file'}[$i]));
2155 $line = '--- /dev/null';
2157 $result .= qq
!<div
class="diff from_file">$line</div
>\n!;
2162 #assert($line =~ m/^\+\+\+/) if DEBUG;
2163 # no extra formatting for "^+++ /dev/null"
2164 if ($line =~ m!^\+\+\+ "?b/!) {
2165 if ($to->{'href'}) {
2167 $cgi->a({-href
=>$to->{'href'}, -class=>"path"},
2168 esc_path
($to->{'file'}));
2171 esc_path
($to->{'file'});
2174 $result .= qq
!<div
class="diff to_file">$line</div
>\n!;
2179 # create note for patch simplified by combined diff
2180 sub format_diff_cc_simplified
{
2181 my ($diffinfo, @parents) = @_;
2184 $result .= "<div class=\"diff header\">" .
2186 if (!is_deleted
($diffinfo)) {
2187 $result .= $cgi->a({-href
=> href
(action
=>"blob",
2189 hash
=>$diffinfo->{'to_id'},
2190 file_name
=>$diffinfo->{'to_file'}),
2192 esc_path
($diffinfo->{'to_file'}));
2194 $result .= esc_path
($diffinfo->{'to_file'});
2196 $result .= "</div>\n" . # class="diff header"
2197 "<div class=\"diff nodifferences\">" .
2199 "</div>\n"; # class="diff nodifferences"
2204 # format patch (diff) line (not to be used for diff headers)
2205 sub format_diff_line
{
2207 my ($from, $to) = @_;
2208 my $diff_class = "";
2212 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
2214 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
2215 if ($line =~ m/^\@{3}/) {
2216 $diff_class = " chunk_header";
2217 } elsif ($line =~ m/^\\/) {
2218 $diff_class = " incomplete";
2219 } elsif ($prefix =~ tr/+/+/) {
2220 $diff_class = " add";
2221 } elsif ($prefix =~ tr/-/-/) {
2222 $diff_class = " rem";
2225 # assume ordinary diff
2226 my $char = substr($line, 0, 1);
2228 $diff_class = " add";
2229 } elsif ($char eq '-') {
2230 $diff_class = " rem";
2231 } elsif ($char eq '@') {
2232 $diff_class = " chunk_header";
2233 } elsif ($char eq "\\") {
2234 $diff_class = " incomplete";
2237 $line = untabify
($line);
2238 if ($from && $to && $line =~ m/^\@{2} /) {
2239 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
2240 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
2242 $from_lines = 0 unless defined $from_lines;
2243 $to_lines = 0 unless defined $to_lines;
2245 if ($from->{'href'}) {
2246 $from_text = $cgi->a({-href
=>"$from->{'href'}#l$from_start",
2247 -class=>"list"}, $from_text);
2249 if ($to->{'href'}) {
2250 $to_text = $cgi->a({-href
=>"$to->{'href'}#l$to_start",
2251 -class=>"list"}, $to_text);
2253 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
2254 "<span class=\"section\">" . esc_html
($section, -nbsp
=>1) . "</span>";
2255 return "<div class=\"diff$diff_class\">$line</div>\n";
2256 } elsif ($from && $to && $line =~ m/^\@{3}/) {
2257 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
2258 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
2260 @from_text = split(' ', $ranges);
2261 for (my $i = 0; $i < @from_text; ++$i) {
2262 ($from_start[$i], $from_nlines[$i]) =
2263 (split(',', substr($from_text[$i], 1)), 0);
2266 $to_text = pop @from_text;
2267 $to_start = pop @from_start;
2268 $to_nlines = pop @from_nlines;
2270 $line = "<span class=\"chunk_info\">$prefix ";
2271 for (my $i = 0; $i < @from_text; ++$i) {
2272 if ($from->{'href'}[$i]) {
2273 $line .= $cgi->a({-href
=>"$from->{'href'}[$i]#l$from_start[$i]",
2274 -class=>"list"}, $from_text[$i]);
2276 $line .= $from_text[$i];
2280 if ($to->{'href'}) {
2281 $line .= $cgi->a({-href
=>"$to->{'href'}#l$to_start",
2282 -class=>"list"}, $to_text);
2286 $line .= " $prefix</span>" .
2287 "<span class=\"section\">" . esc_html
($section, -nbsp
=>1) . "</span>";
2288 return "<div class=\"diff$diff_class\">$line</div>\n";
2290 return "<div class=\"diff$diff_class\">" . esc_html
($line, -nbsp
=>1) . "</div>\n";
2293 # Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
2294 # linked. Pass the hash of the tree/commit to snapshot.
2295 sub format_snapshot_links
{
2297 my $num_fmts = @snapshot_fmts;
2298 if ($num_fmts > 1) {
2299 # A parenthesized list of links bearing format names.
2300 # e.g. "snapshot (_tar.gz_ _zip_)"
2301 return "snapshot (" . join(' ', map
2308 }, $known_snapshot_formats{$_}{'display'})
2309 , @snapshot_fmts) . ")";
2310 } elsif ($num_fmts == 1) {
2311 # A single "snapshot" link whose tooltip bears the format name.
2313 my ($fmt) = @snapshot_fmts;
2319 snapshot_format
=>$fmt
2321 -title
=> "in format: $known_snapshot_formats{$fmt}{'display'}"
2323 } else { # $num_fmts == 0
2328 ## ......................................................................
2329 ## functions returning values to be passed, perhaps after some
2330 ## transformation, to other functions; e.g. returning arguments to href()
2332 # returns hash to be passed to href to generate gitweb URL
2333 # in -title key it returns description of link
2335 my $format = shift || 'Atom';
2336 my %res = (action
=> lc($format));
2338 # feed links are possible only for project views
2339 return unless (defined $project);
2340 # some views should link to OPML, or to generic project feed,
2341 # or don't have specific feed yet (so they should use generic)
2342 return if ($action =~ /^(?:tags|heads|forks|tag|search)$/x);
2345 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
2346 # from tag links; this also makes possible to detect branch links
2347 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
2348 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
2351 # find log type for feed description (title)
2353 if (defined $file_name) {
2354 $type = "history of $file_name";
2355 $type .= "/" if ($action eq 'tree');
2356 $type .= " on '$branch'" if (defined $branch);
2358 $type = "log of $branch" if (defined $branch);
2361 $res{-title
} = $type;
2362 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
2363 $res{'file_name'} = $file_name;
2368 ## ----------------------------------------------------------------------
2369 ## git utility subroutines, invoking git commands
2371 # returns path to the core git executable and the --git-dir parameter as list
2373 $number_of_git_cmds++;
2374 return $GIT, '--git-dir='.$git_dir;
2377 # quote the given arguments for passing them to the shell
2378 # quote_command("command", "arg 1", "arg with ' and ! characters")
2379 # => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
2380 # Try to avoid using this function wherever possible.
2383 map { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ );
2386 # get HEAD ref of given project as hash
2387 sub git_get_head_hash
{
2388 return git_get_full_hash
(shift, 'HEAD');
2391 sub git_get_full_hash
{
2392 return git_get_hash
(@_);
2395 sub git_get_short_hash
{
2396 return git_get_hash
(@_, '--short=7');
2400 my ($project, $hash, @options) = @_;
2401 my $o_git_dir = $git_dir;
2403 $git_dir = "$projectroot/$project";
2404 if (open my $fd, '-|', git_cmd
(), 'rev-parse',
2405 '--verify', '-q', @options, $hash) {
2407 chomp $retval if defined $retval;
2410 if (defined $o_git_dir) {
2411 $git_dir = $o_git_dir;
2416 # get type of given object
2420 open my $fd, "-|", git_cmd
(), "cat-file", '-t', $hash or return;
2422 close $fd or return;
2427 # repository configuration
2428 our $config_file = '';
2431 # store multiple values for single key as anonymous array reference
2432 # single values stored directly in the hash, not as [ <value> ]
2433 sub hash_set_multi
{
2434 my ($hash, $key, $value) = @_;
2436 if (!exists $hash->{$key}) {
2437 $hash->{$key} = $value;
2438 } elsif (!ref $hash->{$key}) {
2439 $hash->{$key} = [ $hash->{$key}, $value ];
2441 push @{$hash->{$key}}, $value;
2445 # return hash of git project configuration
2446 # optionally limited to some section, e.g. 'gitweb'
2447 sub git_parse_project_config
{
2448 my $section_regexp = shift;
2453 open my $fh, "-|", git_cmd
(), "config", '-z', '-l',
2456 while (my $keyval = <$fh>) {
2458 my ($key, $value) = split(/\n/, $keyval, 2);
2460 hash_set_multi
(\
%config, $key, $value)
2461 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
2468 # convert config value to boolean: 'true' or 'false'
2469 # no value, number > 0, 'true' and 'yes' values are true
2470 # rest of values are treated as false (never as error)
2471 sub config_to_bool
{
2474 return 1 if !defined $val; # section.key
2476 # strip leading and trailing whitespace
2480 return (($val =~ /^\d+$/ && $val) || # section.key = 1
2481 ($val =~ /^(?:true|yes)$/i)); # section.key = true
2484 # convert config value to simple decimal number
2485 # an optional value suffix of 'k', 'm', or 'g' will cause the value
2486 # to be multiplied by 1024, 1048576, or 1073741824
2490 # strip leading and trailing whitespace
2494 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
2496 # unknown unit is treated as 1
2497 return $num * ($unit eq 'g' ? 1073741824 :
2498 $unit eq 'm' ? 1048576 :
2499 $unit eq 'k' ? 1024 : 1);
2504 # convert config value to array reference, if needed
2505 sub config_to_multi
{
2508 return ref($val) ? $val : (defined($val) ? [ $val ] : []);
2511 sub git_get_project_config
{
2512 my ($key, $type) = @_;
2514 return unless defined $git_dir;
2517 return unless ($key);
2518 $key =~ s/^gitweb\.//;
2519 return if ($key =~ m/\W/);
2522 if (defined $type) {
2525 unless ($type eq 'bool' || $type eq 'int');
2529 if (!defined $config_file ||
2530 $config_file ne "$git_dir/config") {
2531 %config = git_parse_project_config
('gitweb');
2532 $config_file = "$git_dir/config";
2535 # check if config variable (key) exists
2536 return unless exists $config{"gitweb.$key"};
2539 if (!defined $type) {
2540 return $config{"gitweb.$key"};
2541 } elsif ($type eq 'bool') {
2542 # backward compatibility: 'git config --bool' returns true/false
2543 return config_to_bool
($config{"gitweb.$key"}) ? 'true' : 'false';
2544 } elsif ($type eq 'int') {
2545 return config_to_int
($config{"gitweb.$key"});
2547 return $config{"gitweb.$key"};
2550 # get hash of given path at given ref
2551 sub git_get_hash_by_path
{
2553 my $path = shift || return undef;
2558 open my $fd, "-|", git_cmd
(), "ls-tree", $base, "--", $path
2559 or die_error
(500, "Open git-ls-tree failed");
2561 close $fd or return undef;
2563 if (!defined $line) {
2564 # there is no tree or hash given by $path at $base
2568 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2569 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
2570 if (defined $type && $type ne $2) {
2571 # type doesn't match
2577 # get path of entry with given hash at given tree-ish (ref)
2578 # used to get 'from' filename for combined diff (merge commit) for renames
2579 sub git_get_path_by_hash
{
2580 my $base = shift || return;
2581 my $hash = shift || return;
2585 open my $fd, "-|", git_cmd
(), "ls-tree", '-r', '-t', '-z', $base
2587 while (my $line = <$fd>) {
2590 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
2591 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
2592 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
2601 ## ......................................................................
2602 ## git utility functions, directly accessing git repository
2604 # get the value of config variable either from file named as the variable
2605 # itself in the repository ($GIT_DIR/$name file), or from gitweb.$name
2606 # configuration variable in the repository config file.
2607 sub git_get_file_or_project_config
{
2608 my ($path, $name) = @_;
2610 $git_dir = "$projectroot/$path";
2611 open my $fd, '<', "$git_dir/$name"
2612 or return git_get_project_config
($name);
2615 if (defined $conf) {
2621 sub git_get_project_description
{
2623 return git_get_file_or_project_config
($path, 'description');
2626 sub git_get_project_category
{
2628 return git_get_file_or_project_config
($path, 'category');
2632 # supported formats:
2633 # * $GIT_DIR/ctags/<tagname> file (in 'ctags' subdirectory)
2634 # - if its contents is a number, use it as tag weight,
2635 # - otherwise add a tag with weight 1
2636 # * $GIT_DIR/ctags file, each line is a tag (with weight 1)
2637 # the same value multiple times increases tag weight
2638 # * `gitweb.ctag' multi-valued repo config variable
2639 sub git_get_project_ctags
{
2640 my $project = shift;
2643 $git_dir = "$projectroot/$project";
2644 if (opendir my $dh, "$git_dir/ctags") {
2645 my @files = grep { -f
$_ } map { "$git_dir/ctags/$_" } readdir($dh);
2646 foreach my $tagfile (@files) {
2647 open my $ct, '<', $tagfile
2653 (my $ctag = $tagfile) =~ s
#.*/##;
2654 if ($val =~ /^\d+$/) {
2655 $ctags->{$ctag} = $val;
2657 $ctags->{$ctag} = 1;
2662 } elsif (open my $fh, '<', "$git_dir/ctags") {
2663 while (my $line = <$fh>) {
2665 $ctags->{$line}++ if $line;
2670 my $taglist = config_to_multi
(git_get_project_config
('ctag'));
2671 foreach my $tag (@$taglist) {
2679 # return hash, where keys are content tags ('ctags'),
2680 # and values are sum of weights of given tag in every project
2681 sub git_gather_all_ctags
{
2682 my $projects = shift;
2685 foreach my $p (@$projects) {
2686 foreach my $ct (keys %{$p->{'ctags'}}) {
2687 $ctags->{$ct} += $p->{'ctags'}->{$ct};
2694 sub git_populate_project_tagcloud
{
2697 # First, merge different-cased tags; tags vote on casing
2699 foreach (keys %$ctags) {
2700 $ctags_lc{lc $_}->{count
} += $ctags->{$_};
2701 if (not $ctags_lc{lc $_}->{topcount
}
2702 or $ctags_lc{lc $_}->{topcount
} < $ctags->{$_}) {
2703 $ctags_lc{lc $_}->{topcount
} = $ctags->{$_};
2704 $ctags_lc{lc $_}->{topname
} = $_;
2709 my $matched = $cgi->param('by_tag');
2710 if (eval { require HTML
::TagCloud
; 1; }) {
2711 $cloud = HTML
::TagCloud-
>new;
2712 foreach my $ctag (sort keys %ctags_lc) {
2713 # Pad the title with spaces so that the cloud looks
2715 my $title = esc_html
($ctags_lc{$ctag}->{topname
});
2716 $title =~ s/ / /g;
2717 $title =~ s/^/ /g;
2718 $title =~ s/$/ /g;
2719 if (defined $matched && $matched eq $ctag) {
2720 $title = qq(<span class="match">$title</span>);
2722 $cloud->add($title, href
(project
=>undef, ctag
=>$ctag),
2723 $ctags_lc{$ctag}->{count
});
2727 foreach my $ctag (keys %ctags_lc) {
2728 my $title = esc_html
($ctags_lc{$ctag}->{topname
}, -nbsp
=>1);
2729 if (defined $matched && $matched eq $ctag) {
2730 $title = qq(<span class="match">$title</span>);
2732 $cloud->{$ctag}{count
} = $ctags_lc{$ctag}->{count
};
2733 $cloud->{$ctag}{ctag
} =
2734 $cgi->a({-href
=>href
(project
=>undef, ctag
=>$ctag)}, $title);
2740 sub git_show_project_tagcloud
{
2741 my ($cloud, $count) = @_;
2742 if (ref $cloud eq 'HTML::TagCloud') {
2743 return $cloud->html_and_css($count);
2745 my @tags = sort { $cloud->{$a}->{'count'} <=> $cloud->{$b}->{'count'} } keys %$cloud;
2747 '<div id="htmltagcloud"'.($project ? '' : ' align="center"').'>' .
2749 $cloud->{$_}->{'ctag'}
2750 } splice(@tags, 0, $count)) .
2755 sub git_get_project_url_list
{
2758 $git_dir = "$projectroot/$path";
2759 open my $fd, '<', "$git_dir/cloneurl"
2760 or return wantarray ?
2761 @{ config_to_multi
(git_get_project_config
('url')) } :
2762 config_to_multi
(git_get_project_config
('url'));
2763 my @git_project_url_list = map { chomp; $_ } <$fd>;
2766 return wantarray ? @git_project_url_list : \
@git_project_url_list;
2769 sub git_get_projects_list
{
2770 my $filter = shift || '';
2773 $filter =~ s/\.git$//;
2775 if (-d
$projects_list) {
2776 # search in directory
2777 my $dir = $projects_list;
2778 # remove the trailing "/"
2780 my $pfxlen = length("$projects_list");
2781 my $pfxdepth = ($projects_list =~ tr!/!!);
2782 # when filtering, search only given subdirectory
2789 follow_fast
=> 1, # follow symbolic links
2790 follow_skip
=> 2, # ignore duplicates
2791 dangling_symlinks
=> 0, # ignore dangling symlinks, silently
2794 our $project_maxdepth;
2796 # skip project-list toplevel, if we get it.
2797 return if (m!^[/.]$!);
2798 # only directories can be git repositories
2799 return unless (-d
$_);
2800 # don't traverse too deep (Find is super slow on os x)
2801 # $project_maxdepth excludes depth of $projectroot
2802 if (($File::Find
::name
=~ tr!/!!) - $pfxdepth > $project_maxdepth) {
2803 $File::Find
::prune
= 1;
2807 my $path = substr($File::Find
::name
, $pfxlen + 1);
2808 # we check related file in $projectroot
2809 if (check_export_ok
("$projectroot/$path")) {
2810 push @list, { path
=> $path };
2811 $File::Find
::prune
= 1;
2816 } elsif (-f
$projects_list) {
2817 # read from file(url-encoded):
2818 # 'git%2Fgit.git Linus+Torvalds'
2819 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2820 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2821 open my $fd, '<', $projects_list or return;
2823 while (my $line = <$fd>) {
2825 my ($path, $owner) = split ' ', $line;
2826 $path = unescape
($path);
2827 $owner = unescape
($owner);
2828 if (!defined $path) {
2831 # if $filter is rpovided, check if $path begins with $filter
2832 if ($filter && $path !~ m!^\Q$filter\E/!) {
2835 if (check_export_ok
("$projectroot/$path")) {
2838 owner
=> to_utf8
($owner),
2848 # written with help of Tree::Trie module (Perl Artistic License, GPL compatibile)
2849 # as side effects it sets 'forks' field to list of forks for forked projects
2850 sub filter_forks_from_projects_list
{
2851 my $projects = shift;
2853 my %trie; # prefix tree of directories (path components)
2854 # generate trie out of those directories that might contain forks
2855 foreach my $pr (@$projects) {
2856 my $path = $pr->{'path'};
2857 $path =~ s/\.git$//; # forks of 'repo.git' are in 'repo/' directory
2858 next if ($path =~ m!/$!); # skip non-bare repositories, e.g. 'repo/.git'
2859 next unless ($path); # skip '.git' repository: tests, git-instaweb
2860 next unless (-d
$path); # containing directory exists
2861 $pr->{'forks'} = []; # there can be 0 or more forks of project
2864 my @dirs = split('/', $path);
2865 # walk the trie, until either runs out of components or out of trie
2867 while (scalar @dirs &&
2868 exists($ref->{$dirs[0]})) {
2869 $ref = $ref->{shift @dirs};
2871 # create rest of trie structure from rest of components
2872 foreach my $dir (@dirs) {
2873 $ref = $ref->{$dir} = {};
2875 # create end marker, store $pr as a data
2876 $ref->{''} = $pr if (!exists $ref->{''});
2879 # filter out forks, by finding shortest prefix match for paths
2882 foreach my $pr (@$projects) {
2886 foreach my $dir (split('/', $pr->{'path'})) {
2887 if (exists $ref->{''}) {
2888 # found [shortest] prefix, is a fork - skip it
2889 push @{$ref->{''}{'forks'}}, $pr;
2892 if (!exists $ref->{$dir}) {
2893 # not in trie, cannot have prefix, not a fork
2894 push @filtered, $pr;
2897 # If the dir is there, we just walk one step down the trie.
2898 $ref = $ref->{$dir};
2900 # we ran out of trie
2901 # (shouldn't happen: it's either no match, or end marker)
2902 push @filtered, $pr;
2908 # note: fill_project_list_info must be run first,
2909 # for 'descr_long' and 'ctags' to be filled
2910 sub search_projects_list
{
2911 my ($projlist, %opts) = @_;
2912 my $tagfilter = $opts{'tagfilter'};
2913 my $searchtext = $opts{'searchtext'};
2916 unless ($tagfilter || $searchtext);
2920 foreach my $pr (@$projlist) {
2923 next unless ref($pr->{'ctags'}) eq 'HASH';
2925 grep { lc($_) eq lc($tagfilter) } keys %{$pr->{'ctags'}};
2930 $pr->{'path'} =~ /$searchtext/ ||
2931 $pr->{'descr_long'} =~ /$searchtext/;
2934 push @projects, $pr;
2940 our $gitweb_project_owner = undef;
2941 sub git_get_project_list_from_file
{
2943 return if (defined $gitweb_project_owner);
2945 $gitweb_project_owner = {};
2946 # read from file (url-encoded):
2947 # 'git%2Fgit.git Linus+Torvalds'
2948 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2949 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2950 if (-f
$projects_list) {
2951 open(my $fd, '<', $projects_list);
2952 while (my $line = <$fd>) {
2954 my ($pr, $ow) = split ' ', $line;
2955 $pr = unescape
($pr);
2956 $ow = unescape
($ow);
2957 $gitweb_project_owner->{$pr} = to_utf8
($ow);
2963 sub git_get_project_owner
{
2964 my $project = shift;
2967 return undef unless $project;
2968 $git_dir = "$projectroot/$project";
2970 if (!defined $gitweb_project_owner) {
2971 git_get_project_list_from_file
();
2974 if (exists $gitweb_project_owner->{$project}) {
2975 $owner = $gitweb_project_owner->{$project};
2977 if (!defined $owner){
2978 $owner = git_get_project_config
('owner');
2980 if (!defined $owner) {
2981 $owner = get_file_owner
("$git_dir");
2987 sub git_get_last_activity
{
2991 $git_dir = "$projectroot/$path";
2992 open($fd, "-|", git_cmd
(), 'for-each-ref',
2993 '--format=%(committer)',
2994 '--sort=-committerdate',
2996 'refs/heads') or return;
2997 my $most_recent = <$fd>;
2998 close $fd or return;
2999 if (defined $most_recent &&
3000 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
3002 my $age = time - $timestamp;
3003 return ($age, age_string
($age));
3005 return (undef, undef);
3008 # Implementation note: when a single remote is wanted, we cannot use 'git
3009 # remote show -n' because that command always work (assuming it's a remote URL
3010 # if it's not defined), and we cannot use 'git remote show' because that would
3011 # try to make a network roundtrip. So the only way to find if that particular
3012 # remote is defined is to walk the list provided by 'git remote -v' and stop if
3013 # and when we find what we want.
3014 sub git_get_remotes_list
{
3018 open my $fd, '-|' , git_cmd
(), 'remote', '-v';
3020 while (my $remote = <$fd>) {
3022 $remote =~ s!\t(.*?)\s+\((\w+)\)$!!;
3023 next if $wanted and not $remote eq $wanted;
3024 my ($url, $key) = ($1, $2);
3026 $remotes{$remote} ||= { 'heads' => () };
3027 $remotes{$remote}{$key} = $url;
3029 close $fd or return;
3030 return wantarray ? %remotes : \
%remotes;
3033 # Takes a hash of remotes as first parameter and fills it by adding the
3034 # available remote heads for each of the indicated remotes.
3035 sub fill_remote_heads
{
3036 my $remotes = shift;
3037 my @heads = map { "remotes/$_" } keys %$remotes;
3038 my @remoteheads = git_get_heads_list
(undef, @heads);
3039 foreach my $remote (keys %$remotes) {
3040 $remotes->{$remote}{'heads'} = [ grep {
3041 $_->{'name'} =~ s!^$remote/!!
3046 sub git_get_references
{
3047 my $type = shift || "";
3049 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
3050 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
3051 open my $fd, "-|", git_cmd
(), "show-ref", "--dereference",
3052 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
3055 while (my $line = <$fd>) {
3057 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {
3058 if (defined $refs{$1}) {
3059 push @{$refs{$1}}, $2;
3065 close $fd or return;
3069 sub git_get_rev_name_tags
{
3070 my $hash = shift || return undef;
3072 open my $fd, "-|", git_cmd
(), "name-rev", "--tags", $hash
3074 my $name_rev = <$fd>;
3077 if ($name_rev =~ m
|^$hash tags
/(.*)$|) {
3080 # catches also '$hash undefined' output
3085 ## ----------------------------------------------------------------------
3086 ## parse to hash functions
3090 my $tz = shift || "-0000";
3093 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
3094 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
3095 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
3096 $date{'hour'} = $hour;
3097 $date{'minute'} = $min;
3098 $date{'mday'} = $mday;
3099 $date{'day'} = $days[$wday];
3100 $date{'month'} = $months[$mon];
3101 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
3102 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
3103 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
3104 $mday, $months[$mon], $hour ,$min;
3105 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
3106 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
3108 my ($tz_sign, $tz_hour, $tz_min) =
3109 ($tz =~ m/^([-+])(\d\d)(\d\d)$/);
3110 $tz_sign = ($tz_sign eq '-' ? -1 : +1);
3111 my $local = $epoch + $tz_sign*((($tz_hour*60) + $tz_min)*60);
3112 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
3113 $date{'hour_local'} = $hour;
3114 $date{'minute_local'} = $min;
3115 $date{'tz_local'} = $tz;
3116 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
3117 1900+$year, $mon+1, $mday,
3118 $hour, $min, $sec, $tz);
3127 open my $fd, "-|", git_cmd
(), "cat-file", "tag", $tag_id or return;
3128 $tag{'id'} = $tag_id;
3129 while (my $line = <$fd>) {
3131 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
3132 $tag{'object'} = $1;
3133 } elsif ($line =~ m/^type (.+)$/) {
3135 } elsif ($line =~ m/^tag (.+)$/) {
3137 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
3138 $tag{'author'} = $1;
3139 $tag{'author_epoch'} = $2;
3140 $tag{'author_tz'} = $3;
3141 if ($tag{'author'} =~ m/^([^<]+) <([^>]*)>/) {
3142 $tag{'author_name'} = $1;
3143 $tag{'author_email'} = $2;
3145 $tag{'author_name'} = $tag{'author'};
3147 } elsif ($line =~ m/--BEGIN/) {
3148 push @comment, $line;
3150 } elsif ($line eq "") {
3154 push @comment, <$fd>;
3155 $tag{'comment'} = \
@comment;
3156 close $fd or return;
3157 if (!defined $tag{'name'}) {
3163 sub parse_commit_text
{
3164 my ($commit_text, $withparents) = @_;
3165 my @commit_lines = split '\n', $commit_text;
3168 pop @commit_lines; # Remove '\0'
3170 if (! @commit_lines) {
3174 my $header = shift @commit_lines;
3175 if ($header !~ m/^[0-9a-fA-F]{40}/) {
3178 ($co{'id'}, my @parents) = split ' ', $header;
3179 while (my $line = shift @commit_lines) {
3180 last if $line eq "\n";
3181 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
3183 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
3185 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
3186 $co{'author'} = to_utf8
($1);
3187 $co{'author_epoch'} = $2;
3188 $co{'author_tz'} = $3;
3189 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
3190 $co{'author_name'} = $1;
3191 $co{'author_email'} = $2;
3193 $co{'author_name'} = $co{'author'};
3195 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
3196 $co{'committer'} = to_utf8
($1);
3197 $co{'committer_epoch'} = $2;
3198 $co{'committer_tz'} = $3;
3199 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
3200 $co{'committer_name'} = $1;
3201 $co{'committer_email'} = $2;
3203 $co{'committer_name'} = $co{'committer'};
3207 if (!defined $co{'tree'}) {
3210 $co{'parents'} = \
@parents;
3211 $co{'parent'} = $parents[0];
3213 foreach my $title (@commit_lines) {
3216 $co{'title'} = chop_str
($title, 80, 5);
3217 # remove leading stuff of merges to make the interesting part visible
3218 if (length($title) > 50) {
3219 $title =~ s/^Automatic //;
3220 $title =~ s/^merge (of|with) /Merge ... /i;
3221 if (length($title) > 50) {
3222 $title =~ s/(http|rsync):\/\///;
3224 if (length($title) > 50) {
3225 $title =~ s/(master|www|rsync)\.//;
3227 if (length($title) > 50) {
3228 $title =~ s/kernel.org:?//;
3230 if (length($title) > 50) {
3231 $title =~ s/\/pub\/scm//;
3234 $co{'title_short'} = chop_str
($title, 50, 5);
3238 if (! defined $co{'title'} || $co{'title'} eq "") {
3239 $co{'title'} = $co{'title_short'} = '(no commit message)';
3241 # remove added spaces
3242 foreach my $line (@commit_lines) {
3245 $co{'comment'} = \
@commit_lines;
3247 my $age = time - $co{'committer_epoch'};
3249 $co{'age_string'} = age_string
($age);
3250 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
3251 if ($age > 60*60*24*7*2) {
3252 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
3253 $co{'age_string_age'} = $co{'age_string'};
3255 $co{'age_string_date'} = $co{'age_string'};
3256 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
3262 my ($commit_id) = @_;
3267 open my $fd, "-|", git_cmd
(), "rev-list",
3273 or die_error
(500, "Open git-rev-list failed");
3274 %co = parse_commit_text
(<$fd>, 1);
3281 my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
3289 open my $fd, "-|", git_cmd
(), "rev-list",
3292 ("--max-count=" . $maxcount),
3293 ("--skip=" . $skip),
3297 ($filename ? ($filename) : ())
3298 or die_error
(500, "Open git-rev-list failed");
3299 while (my $line = <$fd>) {
3300 my %co = parse_commit_text
($line);
3305 return wantarray ? @cos : \
@cos;
3308 # parse line of git-diff-tree "raw" output
3309 sub parse_difftree_raw_line
{
3313 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
3314 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
3315 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
3316 $res{'from_mode'} = $1;
3317 $res{'to_mode'} = $2;
3318 $res{'from_id'} = $3;
3320 $res{'status'} = $5;
3321 $res{'similarity'} = $6;
3322 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
3323 ($res{'from_file'}, $res{'to_file'}) = map { unquote
($_) } split("\t", $7);
3325 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote
($7);
3328 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
3329 # combined diff (for merge commit)
3330 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
3331 $res{'nparents'} = length($1);
3332 $res{'from_mode'} = [ split(' ', $2) ];
3333 $res{'to_mode'} = pop @{$res{'from_mode'}};
3334 $res{'from_id'} = [ split(' ', $3) ];
3335 $res{'to_id'} = pop @{$res{'from_id'}};
3336 $res{'status'} = [ split('', $4) ];
3337 $res{'to_file'} = unquote
($5);
3339 # 'c512b523472485aef4fff9e57b229d9d243c967f'
3340 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
3341 $res{'commit'} = $1;
3344 return wantarray ? %res : \
%res;
3347 # wrapper: return parsed line of git-diff-tree "raw" output
3348 # (the argument might be raw line, or parsed info)
3349 sub parsed_difftree_line
{
3350 my $line_or_ref = shift;
3352 if (ref($line_or_ref) eq "HASH") {
3353 # pre-parsed (or generated by hand)
3354 return $line_or_ref;
3356 return parse_difftree_raw_line
($line_or_ref);
3360 # parse line of git-ls-tree output
3361 sub parse_ls_tree_line
{
3367 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'
3368 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;
3377 $res{'name'} = unquote
($5);
3380 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
3381 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
3389 $res{'name'} = unquote
($4);
3393 return wantarray ? %res : \
%res;
3396 # generates _two_ hashes, references to which are passed as 2 and 3 argument
3397 sub parse_from_to_diffinfo
{
3398 my ($diffinfo, $from, $to, @parents) = @_;
3400 if ($diffinfo->{'nparents'}) {
3402 $from->{'file'} = [];
3403 $from->{'href'} = [];
3404 fill_from_file_info
($diffinfo, @parents)
3405 unless exists $diffinfo->{'from_file'};
3406 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
3407 $from->{'file'}[$i] =
3408 defined $diffinfo->{'from_file'}[$i] ?
3409 $diffinfo->{'from_file'}[$i] :
3410 $diffinfo->{'to_file'};
3411 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
3412 $from->{'href'}[$i] = href
(action
=>"blob",
3413 hash_base
=>$parents[$i],
3414 hash
=>$diffinfo->{'from_id'}[$i],
3415 file_name
=>$from->{'file'}[$i]);
3417 $from->{'href'}[$i] = undef;
3421 # ordinary (not combined) diff
3422 $from->{'file'} = $diffinfo->{'from_file'};
3423 if ($diffinfo->{'status'} ne "A") { # not new (added) file
3424 $from->{'href'} = href
(action
=>"blob", hash_base
=>$hash_parent,
3425 hash
=>$diffinfo->{'from_id'},
3426 file_name
=>$from->{'file'});
3428 delete $from->{'href'};
3432 $to->{'file'} = $diffinfo->{'to_file'};
3433 if (!is_deleted
($diffinfo)) { # file exists in result
3434 $to->{'href'} = href
(action
=>"blob", hash_base
=>$hash,
3435 hash
=>$diffinfo->{'to_id'},
3436 file_name
=>$to->{'file'});
3438 delete $to->{'href'};
3442 ## ......................................................................
3443 ## parse to array of hashes functions
3445 sub git_get_heads_list
{
3446 my ($limit, @classes) = @_;
3447 @classes = ('heads') unless @classes;
3448 my @patterns = map { "refs/$_" } @classes;
3451 open my $fd, '-|', git_cmd
(), 'for-each-ref',
3452 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
3453 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
3456 while (my $line = <$fd>) {
3460 my ($refinfo, $committerinfo) = split(/\0/, $line);
3461 my ($hash, $name, $title) = split(' ', $refinfo, 3);
3462 my ($committer, $epoch, $tz) =
3463 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
3464 $ref_item{'fullname'} = $name;
3465 $name =~ s!^refs/(?:head|remote)s/!!;
3467 $ref_item{'name'} = $name;
3468 $ref_item{'id'} = $hash;
3469 $ref_item{'title'} = $title || '(no commit message)';
3470 $ref_item{'epoch'} = $epoch;
3472 $ref_item{'age'} = age_string
(time - $ref_item{'epoch'});
3474 $ref_item{'age'} = "unknown";
3477 push @headslist, \
%ref_item;
3481 return wantarray ? @headslist : \
@headslist;
3484 sub git_get_tags_list
{
3488 open my $fd, '-|', git_cmd
(), 'for-each-ref',
3489 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
3490 '--format=%(objectname) %(objecttype) %(refname) '.
3491 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
3494 while (my $line = <$fd>) {
3498 my ($refinfo, $creatorinfo) = split(/\0/, $line);
3499 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
3500 my ($creator, $epoch, $tz) =
3501 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
3502 $ref_item{'fullname'} = $name;
3503 $name =~ s!^refs/tags/!!;
3505 $ref_item{'type'} = $type;
3506 $ref_item{'id'} = $id;
3507 $ref_item{'name'} = $name;
3508 if ($type eq "tag") {
3509 $ref_item{'subject'} = $title;
3510 $ref_item{'reftype'} = $reftype;
3511 $ref_item{'refid'} = $refid;
3513 $ref_item{'reftype'} = $type;
3514 $ref_item{'refid'} = $id;
3517 if ($type eq "tag" || $type eq "commit") {
3518 $ref_item{'epoch'} = $epoch;
3520 $ref_item{'age'} = age_string
(time - $ref_item{'epoch'});
3522 $ref_item{'age'} = "unknown";
3526 push @tagslist, \
%ref_item;
3530 return wantarray ? @tagslist : \
@tagslist;
3533 ## ----------------------------------------------------------------------
3534 ## filesystem-related functions
3536 sub get_file_owner
{
3539 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
3540 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
3541 if (!defined $gcos) {
3545 $owner =~ s/[,;].*$//;
3546 return to_utf8
($owner);
3549 # assume that file exists
3551 my $filename = shift;
3553 open my $fd, '<', $filename;
3554 print map { to_utf8
($_) } <$fd>;
3558 ## ......................................................................
3559 ## mimetype related functions
3561 sub mimetype_guess_file
{
3562 my $filename = shift;
3563 my $mimemap = shift;
3564 -r
$mimemap or return undef;
3567 open(my $mh, '<', $mimemap) or return undef;
3569 next if m/^#/; # skip comments
3570 my ($mimetype, @exts) = split(/\s+/);
3571 foreach my $ext (@exts) {
3572 $mimemap{$ext} = $mimetype;
3577 $filename =~ /\.([^.]*)$/;
3578 return $mimemap{$1};
3581 sub mimetype_guess
{
3582 my $filename = shift;
3584 $filename =~ /\./ or return undef;
3586 if ($mimetypes_file) {
3587 my $file = $mimetypes_file;
3588 if ($file !~ m!^/!) { # if it is relative path
3589 # it is relative to project
3590 $file = "$projectroot/$project/$file";
3592 $mime = mimetype_guess_file
($filename, $file);
3594 $mime ||= mimetype_guess_file
($filename, '/etc/mime.types');
3600 my $filename = shift;
3603 my $mime = mimetype_guess
($filename);
3604 $mime and return $mime;
3608 return $default_blob_plain_mimetype unless $fd;
3611 return 'text/plain';
3612 } elsif (! $filename) {
3613 return 'application/octet-stream';
3614 } elsif ($filename =~ m/\.png$/i) {
3616 } elsif ($filename =~ m/\.gif$/i) {
3618 } elsif ($filename =~ m/\.jpe?g$/i) {
3619 return 'image/jpeg';
3621 return 'application/octet-stream';
3625 sub blob_contenttype
{
3626 my ($fd, $file_name, $type) = @_;
3628 $type ||= blob_mimetype
($fd, $file_name);
3629 if ($type eq 'text/plain' && defined $default_text_plain_charset) {
3630 $type .= "; charset=$default_text_plain_charset";
3636 # guess file syntax for syntax highlighting; return undef if no highlighting
3637 # the name of syntax can (in the future) depend on syntax highlighter used
3638 sub guess_file_syntax
{
3639 my ($highlight, $mimetype, $file_name) = @_;
3640 return undef unless ($highlight && defined $file_name);
3641 my $basename = basename
($file_name, '.in');
3642 return $highlight_basename{$basename}
3643 if exists $highlight_basename{$basename};
3645 $basename =~ /\.([^.]*)$/;
3646 my $ext = $1 or return undef;
3647 return $highlight_ext{$ext}
3648 if exists $highlight_ext{$ext};
3653 # run highlighter and return FD of its output,
3654 # or return original FD if no highlighting
3655 sub run_highlighter
{
3656 my ($fd, $highlight, $syntax) = @_;
3657 return $fd unless ($highlight && defined $syntax);
3660 open $fd, quote_command
(git_cmd
(), "cat-file", "blob", $hash)." | ".
3661 quote_command
($highlight_bin).
3662 " --replace-tabs=8 --fragment --syntax $syntax |"
3663 or die_error
(500, "Couldn't open file or run syntax highlighter");
3667 ## ======================================================================
3668 ## functions printing HTML: header, footer, error page
3670 sub get_page_title
{
3671 my $title = to_utf8
($site_name);
3673 return $title unless (defined $project);
3674 $title .= " - " . to_utf8
($project);
3676 return $title unless (defined $action);
3677 $title .= "/$action"; # $action is US-ASCII (7bit ASCII)
3679 return $title unless (defined $file_name);
3680 $title .= " - " . esc_path
($file_name);
3681 if ($action eq "tree" && $file_name !~ m
|/$|) {
3688 sub print_feed_meta
{
3689 if (defined $project) {
3690 my %href_params = get_feed_info
();
3691 if (!exists $href_params{'-title'}) {
3692 $href_params{'-title'} = 'log';
3695 foreach my $format (qw(RSS Atom)) {
3696 my $type = lc($format);
3698 '-rel' => 'alternate',
3699 '-title' => esc_attr
("$project - $href_params{'-title'} - $format feed"),
3700 '-type' => "application/$type+xml"
3703 $href_params{'action'} = $type;
3704 $link_attr{'-href'} = href
(%href_params);
3706 "rel=\"$link_attr{'-rel'}\" ".
3707 "title=\"$link_attr{'-title'}\" ".
3708 "href=\"$link_attr{'-href'}\" ".
3709 "type=\"$link_attr{'-type'}\" ".
3712 $href_params{'extra_options'} = '--no-merges';
3713 $link_attr{'-href'} = href
(%href_params);
3714 $link_attr{'-title'} .= ' (no merges)';
3716 "rel=\"$link_attr{'-rel'}\" ".
3717 "title=\"$link_attr{'-title'}\" ".
3718 "href=\"$link_attr{'-href'}\" ".
3719 "type=\"$link_attr{'-type'}\" ".
3724 printf('<link rel="alternate" title="%s projects list" '.
3725 'href="%s" type="text/plain; charset=utf-8" />'."\n",
3726 esc_attr
($site_name), href
(project
=>undef, action
=>"project_index"));
3727 printf('<link rel="alternate" title="%s projects feeds" '.
3728 'href="%s" type="text/x-opml" />'."\n",
3729 esc_attr
($site_name), href
(project
=>undef, action
=>"opml"));
3733 sub git_header_html
{
3734 my $status = shift || "200 OK";
3735 my $expires = shift;
3738 my $title = get_page_title
();
3740 # require explicit support from the UA if we are to send the page as
3741 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
3742 # we have to do this because MSIE sometimes globs '*/*', pretending to
3743 # support xhtml+xml but choking when it gets what it asked for.
3744 if (defined $cgi->http('HTTP_ACCEPT') &&
3745 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\
+xml
(,|;|\s
|$)/ &&
3746 $cgi->Accept('application/xhtml+xml') != 0) {
3747 $content_type = 'application/xhtml+xml';
3749 $content_type = 'text/html';
3751 print $cgi->header(-type
=>$content_type, -charset
=> 'utf-8',
3752 -status
=> $status, -expires
=> $expires)
3753 unless ($opts{'-no_http_header'});
3754 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
3756 <?xml version="1.0" encoding="utf-8"?>
3757 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3758 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
3759 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
3760 <!-- git core binaries version $git_version -->
3762 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
3763 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
3764 <meta name="robots" content="index, nofollow"/>
3765 <title>$title</title>
3767 # the stylesheet, favicon etc urls won't work correctly with path_info
3768 # unless we set the appropriate base URL
3769 if ($ENV{'PATH_INFO'}) {
3770 print "<base href=\"".esc_url
($base_url)."\" />\n";
3772 # print out each stylesheet that exist, providing backwards capability
3773 # for those people who defined $stylesheet in a config file
3774 if (defined $stylesheet) {
3775 print '<link rel="stylesheet" type="text/css" href="'.esc_url
($stylesheet).'"/>'."\n";
3777 foreach my $stylesheet (@stylesheets) {
3778 next unless $stylesheet;
3779 print '<link rel="stylesheet" type="text/css" href="'.esc_url
($stylesheet).'"/>'."\n";
3783 if ($status eq '200 OK');
3784 if (defined $favicon) {
3785 print qq(<link rel="shortcut icon" href=").esc_url
($favicon).qq(" type="image/png" />\n);
3791 if (defined $site_header && -f
$site_header) {
3792 insert_file
($site_header);
3795 print "<div class=\"page_header\">\n";
3796 if (defined $logo) {
3797 print $cgi->a({-href
=> esc_url
($logo_url),
3798 -title
=> $logo_label},
3799 $cgi->img({-src
=> esc_url
($logo),
3800 -width
=> 72, -height
=> 27,
3802 -class => "logo"}));
3804 print $cgi->a({-href
=> esc_url
($home_link)}, $home_link_str) . " / ";
3805 if (defined $project) {
3806 print $cgi->a({-href
=> href
(action
=>"summary")}, esc_html
($project));
3807 if (defined $action) {
3808 my $action_print = $action ;
3809 if (defined $opts{-action_extra
}) {
3810 $action_print = $cgi->a({-href
=> href
(action
=>$action)},
3813 print " / $action_print";
3815 if (defined $opts{-action_extra
}) {
3816 print " / $opts{-action_extra}";
3822 my $have_search = gitweb_check_feature
('search');
3823 if (defined $project && $have_search) {
3824 if (!defined $searchtext) {
3828 if (defined $hash_base) {
3829 $search_hash = $hash_base;
3830 } elsif (defined $hash) {
3831 $search_hash = $hash;
3833 $search_hash = "HEAD";
3835 my $action = $my_uri;
3836 my $use_pathinfo = gitweb_check_feature
('pathinfo');
3837 if ($use_pathinfo) {
3838 $action .= "/".esc_url
($project);
3840 print $cgi->startform(-method => "get", -action
=> $action) .
3841 "<div class=\"search\">\n" .
3843 $cgi->input({-name
=>"p", -value
=>$project, -type
=>"hidden"}) . "\n") .
3844 $cgi->input({-name
=>"a", -value
=>"search", -type
=>"hidden"}) . "\n" .
3845 $cgi->input({-name
=>"h", -value
=>$search_hash, -type
=>"hidden"}) . "\n" .
3846 $cgi->popup_menu(-name
=> 'st', -default => 'commit',
3847 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
3848 $cgi->sup($cgi->a({-href
=> href
(action
=>"search_help")}, "?")) .
3850 $cgi->textfield(-name
=> "s", -value
=> $searchtext) . "\n" .
3851 "<span title=\"Extended regular expression\">" .
3852 $cgi->checkbox(-name
=> 'sr', -value
=> 1, -label
=> 're',
3853 -checked
=> $search_use_regexp) .
3856 $cgi->end_form() . "\n";
3860 sub git_footer_html
{
3861 my $feed_class = 'rss_logo';
3863 print "<div class=\"page_footer\">\n";
3864 if (defined $project) {
3865 my $descr = git_get_project_description
($project);
3866 if (defined $descr) {
3867 print "<div class=\"page_footer_text\">" . esc_html
($descr) . "</div>\n";
3870 my %href_params = get_feed_info
();
3871 if (!%href_params) {
3872 $feed_class .= ' generic';
3874 $href_params{'-title'} ||= 'log';
3876 foreach my $format (qw(RSS Atom)) {
3877 $href_params{'action'} = lc($format);
3878 print $cgi->a({-href
=> href
(%href_params),
3879 -title
=> "$href_params{'-title'} $format feed",
3880 -class => $feed_class}, $format)."\n";
3884 print $cgi->a({-href
=> href
(project
=>undef, action
=>"opml"),
3885 -class => $feed_class}, "OPML") . " ";
3886 print $cgi->a({-href
=> href
(project
=>undef, action
=>"project_index"),
3887 -class => $feed_class}, "TXT") . "\n";
3889 print "</div>\n"; # class="page_footer"
3891 if (defined $t0 && gitweb_check_feature
('timed')) {
3892 print "<div id=\"generating_info\">\n";
3893 print 'This page took '.
3894 '<span id="generating_time" class="time_span">'.
3895 tv_interval
($t0, [ gettimeofday
() ]).
3898 '<span id="generating_cmd">'.
3899 $number_of_git_cmds.
3900 '</span> git commands '.
3902 print "</div>\n"; # class="page_footer"
3905 if (defined $site_footer && -f
$site_footer) {
3906 insert_file
($site_footer);
3909 print qq
!<script type
="text/javascript" src
="!.esc_url($javascript).qq!"></script
>\n!;
3910 if (defined $action &&
3911 $action eq 'blame_incremental') {
3912 print qq
!<script type
="text/javascript">\n!.
3913 qq
!startBlame
("!. href(action=>"blame_data
", -replay=>1) .qq!",\n!.
3914 qq
! "!. href() .qq!");\n!.
3917 my ($jstimezone, $tz_cookie, $datetime_class) =
3918 gitweb_get_feature
('javascript-timezone');
3920 print qq
!<script type
="text/javascript">\n!.
3921 qq
!window
.onload
= function
() {\n!;
3922 if (gitweb_check_feature
('javascript-actions')) {
3923 print qq
! fixLinks
();\n!;
3925 if ($jstimezone && $tz_cookie && $datetime_class) {
3926 print qq
! var tz_cookie
= { name
: '$tz_cookie', expires
: 14, path
: '/' };\n!. # in days
3927 qq
! onloadTZSetup
('$jstimezone', tz_cookie
, '$datetime_class');\n!;
3937 # die_error(<http_status_code>, <error_message>[, <detailed_html_description>])
3938 # Example: die_error(404, 'Hash not found')
3939 # By convention, use the following status codes (as defined in RFC 2616):
3940 # 400: Invalid or missing CGI parameters, or
3941 # requested object exists but has wrong type.
3942 # 403: Requested feature (like "pickaxe" or "snapshot") not enabled on
3943 # this server or project.
3944 # 404: Requested object/revision/project doesn't exist.
3945 # 500: The server isn't configured properly, or
3946 # an internal error occurred (e.g. failed assertions caused by bugs), or
3947 # an unknown error occurred (e.g. the git binary died unexpectedly).
3948 # 503: The server is currently unavailable (because it is overloaded,
3949 # or down for maintenance). Generally, this is a temporary state.
3951 my $status = shift || 500;
3952 my $error = esc_html
(shift) || "Internal Server Error";
3956 my %http_responses = (
3957 400 => '400 Bad Request',
3958 403 => '403 Forbidden',
3959 404 => '404 Not Found',
3960 500 => '500 Internal Server Error',
3961 503 => '503 Service Unavailable',
3963 git_header_html
($http_responses{$status}, undef, %opts);
3965 <div class="page_body">
3970 if (defined $extra) {
3978 unless ($opts{'-error_handler'});
3981 ## ----------------------------------------------------------------------
3982 ## functions printing or outputting HTML: navigation
3984 sub git_print_page_nav
{
3985 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
3986 $extra = '' if !defined $extra; # pager or formats
3988 my @navs = qw(summary shortlog log commit commitdiff tree);
3990 @navs = grep { $_ ne $suppress } @navs;
3993 my %arg = map { $_ => {action
=>$_} } @navs;
3994 if (defined $head) {
3995 for (qw(commit commitdiff)) {
3996 $arg{$_}{'hash'} = $head;
3998 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
3999 for (qw(shortlog log)) {
4000 $arg{$_}{'hash'} = $head;
4005 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
4006 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
4008 my @actions = gitweb_get_feature
('actions');
4011 'n' => $project, # project name
4012 'f' => $git_dir, # project path within filesystem
4013 'h' => $treehead || '', # current hash ('h' parameter)
4014 'b' => $treebase || '', # hash base ('hb' parameter)
4017 my ($label, $link, $pos) = splice(@actions,0,3);
4019 @navs = map { $_ eq $pos ? ($_, $label) : $_ } @navs;
4021 $link =~ s/%([%nfhb])/$repl{$1}/g;
4022 $arg{$label}{'_href'} = $link;
4025 print "<div class=\"page_nav\">\n" .
4027 map { $_ eq $current ?
4028 $_ : $cgi->a({-href
=> ($arg{$_}{_href
} ? $arg{$_}{_href
} : href
(%{$arg{$_}}))}, "$_")
4030 print "<br/>\n$extra<br/>\n" .
4034 # returns a submenu for the nagivation of the refs views (tags, heads,
4035 # remotes) with the current view disabled and the remotes view only
4036 # available if the feature is enabled
4037 sub format_ref_views
{
4039 my @ref_views = qw{tags heads};
4040 push @ref_views, 'remotes' if gitweb_check_feature
('remote_heads');
4041 return join " | ", map {
4042 $_ eq $current ? $_ :
4043 $cgi->a({-href
=> href
(action
=>$_)}, $_)
4047 sub format_paging_nav
{
4048 my ($action, $page, $has_next_link) = @_;
4054 $cgi->a({-href
=> href
(-replay
=>1, page
=>undef)}, "first") .
4056 $cgi->a({-href
=> href
(-replay
=>1, page
=>$page-1),
4057 -accesskey
=> "p", -title
=> "Alt-p"}, "prev");
4059 $paging_nav .= "first ⋅ prev";
4062 if ($has_next_link) {
4063 $paging_nav .= " ⋅ " .
4064 $cgi->a({-href
=> href
(-replay
=>1, page
=>$page+1),
4065 -accesskey
=> "n", -title
=> "Alt-n"}, "next");
4067 $paging_nav .= " ⋅ next";
4073 ## ......................................................................
4074 ## functions printing or outputting HTML: div
4076 sub git_print_header_div
{
4077 my ($action, $title, $hash, $hash_base) = @_;
4080 $args{'action'} = $action;
4081 $args{'hash'} = $hash if $hash;
4082 $args{'hash_base'} = $hash_base if $hash_base;
4084 print "<div class=\"header\">\n" .
4085 $cgi->a({-href
=> href
(%args), -class => "title"},
4086 $title ? $title : $action) .
4090 sub format_repo_url
{
4091 my ($name, $url) = @_;
4092 return "<tr class=\"metadata_url\"><td>$name</td><td>$url</td></tr>\n";
4095 # Group output by placing it in a DIV element and adding a header.
4096 # Options for start_div() can be provided by passing a hash reference as the
4097 # first parameter to the function.
4098 # Options to git_print_header_div() can be provided by passing an array
4099 # reference. This must follow the options to start_div if they are present.
4100 # The content can be a scalar, which is output as-is, a scalar reference, which
4101 # is output after html escaping, an IO handle passed either as *handle or
4102 # *handle{IO}, or a function reference. In the latter case all following
4103 # parameters will be taken as argument to the content function call.
4104 sub git_print_section
{
4105 my ($div_args, $header_args, $content);
4107 if (ref($arg) eq 'HASH') {
4111 if (ref($arg) eq 'ARRAY') {
4112 $header_args = $arg;
4117 print $cgi->start_div($div_args);
4118 git_print_header_div
(@$header_args);
4120 if (ref($content) eq 'CODE') {
4122 } elsif (ref($content) eq 'SCALAR') {
4123 print esc_html
($$content);
4124 } elsif (ref($content) eq 'GLOB' or ref($content) eq 'IO::Handle') {
4126 } elsif (!ref($content) && defined($content)) {
4130 print $cgi->end_div;
4133 sub format_timestamp_html
{
4135 my $strtime = $date->{'rfc2822'};
4137 my (undef, undef, $datetime_class) =
4138 gitweb_get_feature
('javascript-timezone');
4139 if ($datetime_class) {
4140 $strtime = qq
!<span
class="$datetime_class">$strtime</span
>!;
4143 my $localtime_format = '(%02d:%02d %s)';
4144 if ($date->{'hour_local'} < 6) {
4145 $localtime_format = '(<span class="atnight">%02d:%02d</span> %s)';
4148 sprintf($localtime_format,
4149 $date->{'hour_local'}, $date->{'minute_local'}, $date->{'tz_local'});
4154 # Outputs the author name and date in long form
4155 sub git_print_authorship
{
4158 my $tag = $opts{-tag
} || 'div';
4159 my $author = $co->{'author_name'};
4161 my %ad = parse_date
($co->{'author_epoch'}, $co->{'author_tz'});
4162 print "<$tag class=\"author_date\">" .
4163 format_search_author
($author, "author", esc_html
($author)) .
4164 " [".format_timestamp_html
(\
%ad)."]".
4165 git_get_avatar
($co->{'author_email'}, -pad_before
=> 1) .
4169 # Outputs table rows containing the full author or committer information,
4170 # in the format expected for 'commit' view (& similar).
4171 # Parameters are a commit hash reference, followed by the list of people
4172 # to output information for. If the list is empty it defaults to both
4173 # author and committer.
4174 sub git_print_authorship_rows
{
4176 # too bad we can't use @people = @_ || ('author', 'committer')
4178 @people = ('author', 'committer') unless @people;
4179 foreach my $who (@people) {
4180 my %wd = parse_date
($co->{"${who}_epoch"}, $co->{"${who}_tz"});
4181 print "<tr><td>$who</td><td>" .
4182 format_search_author
($co->{"${who}_name"}, $who,
4183 esc_html
($co->{"${who}_name"})) . " " .
4184 format_search_author
($co->{"${who}_email"}, $who,
4185 esc_html
("<" . $co->{"${who}_email"} . ">")) .
4186 "</td><td rowspan=\"2\">" .
4187 git_get_avatar
($co->{"${who}_email"}, -size
=> 'double') .
4191 format_timestamp_html
(\
%wd) .
4197 sub git_print_page_path
{
4203 print "<div class=\"page_path\">";
4204 print $cgi->a({-href
=> href
(action
=>"tree", hash_base
=>$hb),
4205 -title
=> 'tree root'}, to_utf8
("[$project]"));
4207 if (defined $name) {
4208 my @dirname = split '/', $name;
4209 my $basename = pop @dirname;
4212 foreach my $dir (@dirname) {
4213 $fullname .= ($fullname ? '/' : '') . $dir;
4214 print $cgi->a({-href
=> href
(action
=>"tree", file_name
=>$fullname,
4216 -title
=> $fullname}, esc_path
($dir));
4219 if (defined $type && $type eq 'blob') {
4220 print $cgi->a({-href
=> href
(action
=>"blob_plain", file_name
=>$file_name,
4222 -title
=> $name}, esc_path
($basename));
4223 } elsif (defined $type && $type eq 'tree') {
4224 print $cgi->a({-href
=> href
(action
=>"tree", file_name
=>$file_name,
4226 -title
=> $name}, esc_path
($basename));
4229 print esc_path
($basename);
4232 print "<br/></div>\n";
4239 if ($opts{'-remove_title'}) {
4240 # remove title, i.e. first line of log
4243 # remove leading empty lines
4244 while (defined $log->[0] && $log->[0] eq "") {
4251 foreach my $line (@$log) {
4252 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
4255 if (! $opts{'-remove_signoff'}) {
4256 print "<span class=\"signoff\">" . esc_html
($line) . "</span><br/>\n";
4259 # remove signoff lines
4266 # print only one empty line
4267 # do not print empty line after signoff
4269 next if ($empty || $signoff);
4275 print format_log_line_html
($line) . "<br/>\n";
4278 if ($opts{'-final_empty_line'}) {
4279 # end with single empty line
4280 print "<br/>\n" unless $empty;
4284 # return link target (what link points to)
4285 sub git_get_link_target
{
4290 open my $fd, "-|", git_cmd
(), "cat-file", "blob", $hash
4294 $link_target = <$fd>;
4299 return $link_target;
4302 # given link target, and the directory (basedir) the link is in,
4303 # return target of link relative to top directory (top tree);
4304 # return undef if it is not possible (including absolute links).
4305 sub normalize_link_target
{
4306 my ($link_target, $basedir) = @_;
4308 # absolute symlinks (beginning with '/') cannot be normalized
4309 return if (substr($link_target, 0, 1) eq '/');
4311 # normalize link target to path from top (root) tree (dir)
4314 $path = $basedir . '/' . $link_target;
4316 # we are in top (root) tree (dir)
4317 $path = $link_target;
4320 # remove //, /./, and /../
4322 foreach my $part (split('/', $path)) {
4323 # discard '.' and ''
4324 next if (!$part || $part eq '.');
4326 if ($part eq '..') {
4330 # link leads outside repository (outside top dir)
4334 push @path_parts, $part;
4337 $path = join('/', @path_parts);
4342 # print tree entry (row of git_tree), but without encompassing <tr> element
4343 sub git_print_tree_entry
{
4344 my ($t, $basedir, $hash_base, $have_blame) = @_;
4347 $base_key{'hash_base'} = $hash_base if defined $hash_base;
4349 # The format of a table row is: mode list link. Where mode is
4350 # the mode of the entry, list is the name of the entry, an href,
4351 # and link is the action links of the entry.
4353 print "<td class=\"mode\">" . mode_str
($t->{'mode'}) . "</td>\n";
4354 if (exists $t->{'size'}) {
4355 print "<td class=\"size\">$t->{'size'}</td>\n";
4357 if ($t->{'type'} eq "blob") {
4358 print "<td class=\"list\">" .
4359 $cgi->a({-href
=> href
(action
=>"blob", hash
=>$t->{'hash'},
4360 file_name
=>"$basedir$t->{'name'}", %base_key),
4361 -class => "list"}, esc_path
($t->{'name'}));
4362 if (S_ISLNK
(oct $t->{'mode'})) {
4363 my $link_target = git_get_link_target
($t->{'hash'});
4365 my $norm_target = normalize_link_target
($link_target, $basedir);
4366 if (defined $norm_target) {
4368 $cgi->a({-href
=> href
(action
=>"object", hash_base
=>$hash_base,
4369 file_name
=>$norm_target),
4370 -title
=> $norm_target}, esc_path
($link_target));
4372 print " -> " . esc_path
($link_target);
4377 print "<td class=\"link\">";
4378 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$t->{'hash'},
4379 file_name
=>"$basedir$t->{'name'}", %base_key)},
4383 $cgi->a({-href
=> href
(action
=>"blame", hash
=>$t->{'hash'},
4384 file_name
=>"$basedir$t->{'name'}", %base_key)},
4387 if (defined $hash_base) {
4389 $cgi->a({-href
=> href
(action
=>"history", hash_base
=>$hash_base,
4390 hash
=>$t->{'hash'}, file_name
=>"$basedir$t->{'name'}")},
4394 $cgi->a({-href
=> href
(action
=>"blob_plain", hash_base
=>$hash_base,
4395 file_name
=>"$basedir$t->{'name'}")},
4399 } elsif ($t->{'type'} eq "tree") {
4400 print "<td class=\"list\">";
4401 print $cgi->a({-href
=> href
(action
=>"tree", hash
=>$t->{'hash'},
4402 file_name
=>"$basedir$t->{'name'}",
4404 esc_path
($t->{'name'}));
4406 print "<td class=\"link\">";
4407 print $cgi->a({-href
=> href
(action
=>"tree", hash
=>$t->{'hash'},
4408 file_name
=>"$basedir$t->{'name'}",
4411 if (defined $hash_base) {
4413 $cgi->a({-href
=> href
(action
=>"history", hash_base
=>$hash_base,
4414 file_name
=>"$basedir$t->{'name'}")},
4419 # unknown object: we can only present history for it
4420 # (this includes 'commit' object, i.e. submodule support)
4421 print "<td class=\"list\">" .
4422 esc_path
($t->{'name'}) .
4424 print "<td class=\"link\">";
4425 if (defined $hash_base) {
4426 print $cgi->a({-href
=> href
(action
=>"history",
4427 hash_base
=>$hash_base,
4428 file_name
=>"$basedir$t->{'name'}")},
4435 ## ......................................................................
4436 ## functions printing large fragments of HTML
4438 # get pre-image filenames for merge (combined) diff
4439 sub fill_from_file_info
{
4440 my ($diff, @parents) = @_;
4442 $diff->{'from_file'} = [ ];
4443 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
4444 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
4445 if ($diff->{'status'}[$i] eq 'R' ||
4446 $diff->{'status'}[$i] eq 'C') {
4447 $diff->{'from_file'}[$i] =
4448 git_get_path_by_hash
($parents[$i], $diff->{'from_id'}[$i]);
4455 # is current raw difftree line of file deletion
4457 my $diffinfo = shift;
4459 return $diffinfo->{'to_id'} eq ('0' x
40);
4462 # does patch correspond to [previous] difftree raw line
4463 # $diffinfo - hashref of parsed raw diff format
4464 # $patchinfo - hashref of parsed patch diff format
4465 # (the same keys as in $diffinfo)
4466 sub is_patch_split
{
4467 my ($diffinfo, $patchinfo) = @_;
4469 return defined $diffinfo && defined $patchinfo
4470 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
4474 sub git_difftree_body
{
4475 my ($difftree, $hash, @parents) = @_;
4476 my ($parent) = $parents[0];
4477 my $have_blame = gitweb_check_feature
('blame');
4478 print "<div class=\"list_head\">\n";
4479 if ($#{$difftree} > 10) {
4480 print(($#{$difftree} + 1) . " files changed:\n");
4484 print "<table class=\"" .
4485 (@parents > 1 ? "combined " : "") .
4488 # header only for combined diff in 'commitdiff' view
4489 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
4492 print "<thead><tr>\n" .
4493 "<th></th><th></th>\n"; # filename, patchN link
4494 for (my $i = 0; $i < @parents; $i++) {
4495 my $par = $parents[$i];
4497 $cgi->a({-href
=> href
(action
=>"commitdiff",
4498 hash
=>$hash, hash_parent
=>$par),
4499 -title
=> 'commitdiff to parent number ' .
4500 ($i+1) . ': ' . substr($par,0,7)},
4504 print "</tr></thead>\n<tbody>\n";
4509 foreach my $line (@{$difftree}) {
4510 my $diff = parsed_difftree_line
($line);
4513 print "<tr class=\"dark\">\n";
4515 print "<tr class=\"light\">\n";
4519 if (exists $diff->{'nparents'}) { # combined diff
4521 fill_from_file_info
($diff, @parents)
4522 unless exists $diff->{'from_file'};
4524 if (!is_deleted
($diff)) {
4525 # file exists in the result (child) commit
4527 $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'to_id'},
4528 file_name
=>$diff->{'to_file'},
4530 -class => "list"}, esc_path
($diff->{'to_file'})) .
4534 esc_path
($diff->{'to_file'}) .
4538 if ($action eq 'commitdiff') {
4541 print "<td class=\"link\">" .
4542 $cgi->a({-href
=> href
(-anchor
=>"patch$patchno")},
4548 my $has_history = 0;
4549 my $not_deleted = 0;
4550 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
4551 my $hash_parent = $parents[$i];
4552 my $from_hash = $diff->{'from_id'}[$i];
4553 my $from_path = $diff->{'from_file'}[$i];
4554 my $status = $diff->{'status'}[$i];
4556 $has_history ||= ($status ne 'A');
4557 $not_deleted ||= ($status ne 'D');
4559 if ($status eq 'A') {
4560 print "<td class=\"link\" align=\"right\"> | </td>\n";
4561 } elsif ($status eq 'D') {
4562 print "<td class=\"link\">" .
4563 $cgi->a({-href
=> href
(action
=>"blob",
4566 file_name
=>$from_path)},
4570 if ($diff->{'to_id'} eq $from_hash) {
4571 print "<td class=\"link nochange\">";
4573 print "<td class=\"link\">";
4575 print $cgi->a({-href
=> href
(action
=>"blobdiff",
4576 hash
=>$diff->{'to_id'},
4577 hash_parent
=>$from_hash,
4579 hash_parent_base
=>$hash_parent,
4580 file_name
=>$diff->{'to_file'},
4581 file_parent
=>$from_path)},
4587 print "<td class=\"link\">";
4589 print $cgi->a({-href
=> href
(action
=>"blob",
4590 hash
=>$diff->{'to_id'},
4591 file_name
=>$diff->{'to_file'},
4594 print " | " if ($has_history);
4597 print $cgi->a({-href
=> href
(action
=>"history",
4598 file_name
=>$diff->{'to_file'},
4605 next; # instead of 'else' clause, to avoid extra indent
4607 # else ordinary diff
4609 my ($to_mode_oct, $to_mode_str, $to_file_type);
4610 my ($from_mode_oct, $from_mode_str, $from_file_type);
4611 if ($diff->{'to_mode'} ne ('0' x
6)) {
4612 $to_mode_oct = oct $diff->{'to_mode'};
4613 if (S_ISREG
($to_mode_oct)) { # only for regular file
4614 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
4616 $to_file_type = file_type
($diff->{'to_mode'});
4618 if ($diff->{'from_mode'} ne ('0' x
6)) {
4619 $from_mode_oct = oct $diff->{'from_mode'};
4620 if (S_ISREG
($from_mode_oct)) { # only for regular file
4621 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
4623 $from_file_type = file_type
($diff->{'from_mode'});
4626 if ($diff->{'status'} eq "A") { # created
4627 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
4628 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
4629 $mode_chng .= "]</span>";
4631 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'to_id'},
4632 hash_base
=>$hash, file_name
=>$diff->{'file'}),
4633 -class => "list"}, esc_path
($diff->{'file'}));
4635 print "<td>$mode_chng</td>\n";
4636 print "<td class=\"link\">";
4637 if ($action eq 'commitdiff') {
4640 print $cgi->a({-href
=> href
(-anchor
=>"patch$patchno")},
4644 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'to_id'},
4645 hash_base
=>$hash, file_name
=>$diff->{'file'})},
4649 } elsif ($diff->{'status'} eq "D") { # deleted
4650 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
4652 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'from_id'},
4653 hash_base
=>$parent, file_name
=>$diff->{'file'}),
4654 -class => "list"}, esc_path
($diff->{'file'}));
4656 print "<td>$mode_chng</td>\n";
4657 print "<td class=\"link\">";
4658 if ($action eq 'commitdiff') {
4661 print $cgi->a({-href
=> href
(-anchor
=>"patch$patchno")},
4665 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'from_id'},
4666 hash_base
=>$parent, file_name
=>$diff->{'file'})},
4669 print $cgi->a({-href
=> href
(action
=>"blame", hash_base
=>$parent,
4670 file_name
=>$diff->{'file'})},
4673 print $cgi->a({-href
=> href
(action
=>"history", hash_base
=>$parent,
4674 file_name
=>$diff->{'file'})},
4678 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
4679 my $mode_chnge = "";
4680 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
4681 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
4682 if ($from_file_type ne $to_file_type) {
4683 $mode_chnge .= " from $from_file_type to $to_file_type";
4685 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
4686 if ($from_mode_str && $to_mode_str) {
4687 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
4688 } elsif ($to_mode_str) {
4689 $mode_chnge .= " mode: $to_mode_str";
4692 $mode_chnge .= "]</span>\n";
4695 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'to_id'},
4696 hash_base
=>$hash, file_name
=>$diff->{'file'}),
4697 -class => "list"}, esc_path
($diff->{'file'}));
4699 print "<td>$mode_chnge</td>\n";
4700 print "<td class=\"link\">";
4701 if ($action eq 'commitdiff') {
4704 print $cgi->a({-href
=> href
(-anchor
=>"patch$patchno")},
4707 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
4708 # "commit" view and modified file (not onlu mode changed)
4709 print $cgi->a({-href
=> href
(action
=>"blobdiff",
4710 hash
=>$diff->{'to_id'}, hash_parent
=>$diff->{'from_id'},
4711 hash_base
=>$hash, hash_parent_base
=>$parent,
4712 file_name
=>$diff->{'file'})},
4716 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'to_id'},
4717 hash_base
=>$hash, file_name
=>$diff->{'file'})},
4720 print $cgi->a({-href
=> href
(action
=>"blame", hash_base
=>$hash,
4721 file_name
=>$diff->{'file'})},
4724 print $cgi->a({-href
=> href
(action
=>"history", hash_base
=>$hash,
4725 file_name
=>$diff->{'file'})},
4729 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
4730 my %status_name = ('R' => 'moved', 'C' => 'copied');
4731 my $nstatus = $status_name{$diff->{'status'}};
4733 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
4734 # mode also for directories, so we cannot use $to_mode_str
4735 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
4738 $cgi->a({-href
=> href
(action
=>"blob", hash_base
=>$hash,
4739 hash
=>$diff->{'to_id'}, file_name
=>$diff->{'to_file'}),
4740 -class => "list"}, esc_path
($diff->{'to_file'})) . "</td>\n" .
4741 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
4742 $cgi->a({-href
=> href
(action
=>"blob", hash_base
=>$parent,
4743 hash
=>$diff->{'from_id'}, file_name
=>$diff->{'from_file'}),
4744 -class => "list"}, esc_path
($diff->{'from_file'})) .
4745 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
4746 "<td class=\"link\">";
4747 if ($action eq 'commitdiff') {
4750 print $cgi->a({-href
=> href
(-anchor
=>"patch$patchno")},
4753 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
4754 # "commit" view and modified file (not only pure rename or copy)
4755 print $cgi->a({-href
=> href
(action
=>"blobdiff",
4756 hash
=>$diff->{'to_id'}, hash_parent
=>$diff->{'from_id'},
4757 hash_base
=>$hash, hash_parent_base
=>$parent,
4758 file_name
=>$diff->{'to_file'}, file_parent
=>$diff->{'from_file'})},
4762 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'to_id'},
4763 hash_base
=>$parent, file_name
=>$diff->{'to_file'})},
4766 print $cgi->a({-href
=> href
(action
=>"blame", hash_base
=>$hash,
4767 file_name
=>$diff->{'to_file'})},
4770 print $cgi->a({-href
=> href
(action
=>"history", hash_base
=>$hash,
4771 file_name
=>$diff->{'to_file'})},
4775 } # we should not encounter Unmerged (U) or Unknown (X) status
4778 print "</tbody>" if $has_header;
4782 sub git_patchset_body
{
4783 my ($fd, $difftree, $hash, @hash_parents) = @_;
4784 my ($hash_parent) = $hash_parents[0];
4786 my $is_combined = (@hash_parents > 1);
4788 my $patch_number = 0;
4794 print "<div class=\"patchset\">\n";
4796 # skip to first patch
4797 while ($patch_line = <$fd>) {
4800 last if ($patch_line =~ m/^diff /);
4804 while ($patch_line) {
4806 # parse "git diff" header line
4807 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
4808 # $1 is from_name, which we do not use
4809 $to_name = unquote
($2);
4810 $to_name =~ s!^b/!!;
4811 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
4812 # $1 is 'cc' or 'combined', which we do not use
4813 $to_name = unquote
($2);
4818 # check if current patch belong to current raw line
4819 # and parse raw git-diff line if needed
4820 if (is_patch_split
($diffinfo, { 'to_file' => $to_name })) {
4821 # this is continuation of a split patch
4822 print "<div class=\"patch cont\">\n";
4824 # advance raw git-diff output if needed
4825 $patch_idx++ if defined $diffinfo;
4827 # read and prepare patch information
4828 $diffinfo = parsed_difftree_line
($difftree->[$patch_idx]);
4830 # compact combined diff output can have some patches skipped
4831 # find which patch (using pathname of result) we are at now;
4833 while ($to_name ne $diffinfo->{'to_file'}) {
4834 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
4835 format_diff_cc_simplified
($diffinfo, @hash_parents) .
4836 "</div>\n"; # class="patch"
4841 last if $patch_idx > $#$difftree;
4842 $diffinfo = parsed_difftree_line
($difftree->[$patch_idx]);
4846 # modifies %from, %to hashes
4847 parse_from_to_diffinfo
($diffinfo, \
%from, \
%to, @hash_parents);
4849 # this is first patch for raw difftree line with $patch_idx index
4850 # we index @$difftree array from 0, but number patches from 1
4851 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
4855 #assert($patch_line =~ m/^diff /) if DEBUG;
4856 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
4858 # print "git diff" header
4859 print format_git_diff_header_line
($patch_line, $diffinfo,
4862 # print extended diff header
4863 print "<div class=\"diff extended_header\">\n";
4865 while ($patch_line = <$fd>) {
4868 last EXTENDED_HEADER
if ($patch_line =~ m/^--- |^diff /);
4870 print format_extended_diff_header_line
($patch_line, $diffinfo,
4873 print "</div>\n"; # class="diff extended_header"
4875 # from-file/to-file diff header
4876 if (! $patch_line) {
4877 print "</div>\n"; # class="patch"
4880 next PATCH
if ($patch_line =~ m/^diff /);
4881 #assert($patch_line =~ m/^---/) if DEBUG;
4883 my $last_patch_line = $patch_line;
4884 $patch_line = <$fd>;
4886 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
4888 print format_diff_from_to_header
($last_patch_line, $patch_line,
4889 $diffinfo, \
%from, \
%to,
4894 while ($patch_line = <$fd>) {
4897 next PATCH
if ($patch_line =~ m/^diff /);
4899 print format_diff_line
($patch_line, \
%from, \
%to);
4903 print "</div>\n"; # class="patch"
4906 # for compact combined (--cc) format, with chunk and patch simplification
4907 # the patchset might be empty, but there might be unprocessed raw lines
4908 for (++$patch_idx if $patch_number > 0;
4909 $patch_idx < @$difftree;
4911 # read and prepare patch information
4912 $diffinfo = parsed_difftree_line
($difftree->[$patch_idx]);
4914 # generate anchor for "patch" links in difftree / whatchanged part
4915 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
4916 format_diff_cc_simplified
($diffinfo, @hash_parents) .
4917 "</div>\n"; # class="patch"
4922 if ($patch_number == 0) {
4923 if (@hash_parents > 1) {
4924 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
4926 print "<div class=\"diff nodifferences\">No differences found</div>\n";
4930 print "</div>\n"; # class="patchset"
4933 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4935 # fills project list info (age, description, owner, category, forks)
4936 # for each project in the list, removing invalid projects from
4938 # NOTE: modifies $projlist, but does not remove entries from it
4939 sub fill_project_list_info
{
4940 my $projlist = shift;
4943 my $show_ctags = gitweb_check_feature
('ctags');
4945 foreach my $pr (@$projlist) {
4946 my (@activity) = git_get_last_activity
($pr->{'path'});
4947 unless (@activity) {
4950 ($pr->{'age'}, $pr->{'age_string'}) = @activity;
4951 if (!defined $pr->{'descr'}) {
4952 my $descr = git_get_project_description
($pr->{'path'}) || "";
4953 $descr = to_utf8
($descr);
4954 $pr->{'descr_long'} = $descr;
4955 $pr->{'descr'} = chop_str
($descr, $projects_list_description_width, 5);
4957 if (!defined $pr->{'owner'}) {
4958 $pr->{'owner'} = git_get_project_owner
("$pr->{'path'}") || "";
4961 $pr->{'ctags'} = git_get_project_ctags
($pr->{'path'});
4963 if ($projects_list_group_categories && !defined $pr->{'category'}) {
4964 my $cat = git_get_project_category
($pr->{'path'}) ||
4965 $project_list_default_category;
4966 $pr->{'category'} = to_utf8
($cat);
4969 push @projects, $pr;
4975 sub sort_projects_list
{
4976 my ($projlist, $order) = @_;
4980 project
=> { key
=> 'path', type
=> 'str' },
4981 descr
=> { key
=> 'descr_long', type
=> 'str' },
4982 owner
=> { key
=> 'owner', type
=> 'str' },
4983 age
=> { key
=> 'age', type
=> 'num' }
4985 my $oi = $order_info{$order};
4986 return @$projlist unless defined $oi;
4987 if ($oi->{'type'} eq 'str') {
4988 @projects = sort {$a->{$oi->{'key'}} cmp $b->{$oi->{'key'}}} @$projlist;
4990 @projects = sort {$a->{$oi->{'key'}} <=> $b->{$oi->{'key'}}} @$projlist;
4996 # returns a hash of categories, containing the list of project
4997 # belonging to each category
4998 sub build_projlist_by_category
{
4999 my ($projlist, $from, $to) = @_;
5002 $from = 0 unless defined $from;
5003 $to = $#$projlist if (!defined $to || $#$projlist < $to);
5005 for (my $i = $from; $i <= $to; $i++) {
5006 my $pr = $projlist->[$i];
5007 push @{$categories{ $pr->{'category'} }}, $pr;
5010 return wantarray ? %categories : \
%categories;
5013 # print 'sort by' <th> element, generating 'sort by $name' replay link
5014 # if that order is not selected
5016 print format_sort_th
(@_);
5019 sub format_sort_th
{
5020 my ($name, $order, $header) = @_;
5022 $header ||= ucfirst($name);
5024 if ($order eq $name) {
5025 $sort_th .= "<th>$header</th>\n";
5027 $sort_th .= "<th>" .
5028 $cgi->a({-href
=> href
(-replay
=>1, order
=>$name),
5029 -class => "header"}, $header) .
5036 sub git_project_list_rows
{
5037 my ($projlist, $from, $to, $check_forks) = @_;
5039 $from = 0 unless defined $from;
5040 $to = $#$projlist if (!defined $to || $#$projlist < $to);
5043 for (my $i = $from; $i <= $to; $i++) {
5044 my $pr = $projlist->[$i];
5047 print "<tr class=\"dark\">\n";
5049 print "<tr class=\"light\">\n";
5055 if ($pr->{'forks'}) {
5056 my $nforks = scalar @{$pr->{'forks'}};
5058 print $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"forks"),
5059 -title
=> "$nforks forks"}, "+");
5061 print $cgi->span({-title
=> "$nforks forks"}, "+");
5066 print "<td>" . $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"summary"),
5067 -class => "list"}, esc_html
($pr->{'path'})) . "</td>\n" .
5068 "<td>" . $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"summary"),
5069 -class => "list", -title
=> $pr->{'descr_long'}},
5070 esc_html
($pr->{'descr'})) . "</td>\n" .
5071 "<td><i>" . chop_and_escape_str
($pr->{'owner'}, 15) . "</i></td>\n";
5072 print "<td class=\"". age_class
($pr->{'age'}) . "\">" .
5073 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
5074 "<td class=\"link\">" .
5075 $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"summary")}, "summary") . " | " .
5076 $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"shortlog")}, "shortlog") . " | " .
5077 $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"log")}, "log") . " | " .
5078 $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"tree")}, "tree") .
5079 ($pr->{'forks'} ? " | " . $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"forks")}, "forks") : '') .
5085 sub git_project_list_body
{
5086 # actually uses global variable $project
5087 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
5088 my @projects = @$projlist;
5090 my $check_forks = gitweb_check_feature
('forks');
5091 my $show_ctags = gitweb_check_feature
('ctags');
5092 my $tagfilter = $show_ctags ? $cgi->param('by_tag') : undef;
5093 $check_forks = undef
5094 if ($tagfilter || $searchtext);
5096 # filtering out forks before filling info allows to do less work
5097 @projects = filter_forks_from_projects_list
(\
@projects)
5099 @projects = fill_project_list_info
(\
@projects);
5100 # searching projects require filling to be run before it
5101 @projects = search_projects_list
(\
@projects,
5102 'searchtext' => $searchtext,
5103 'tagfilter' => $tagfilter)
5104 if ($tagfilter || $searchtext);
5106 $order ||= $default_projects_order;
5107 $from = 0 unless defined $from;
5108 $to = $#projects if (!defined $to || $#projects < $to);
5113 "<b>No such projects found</b><br />\n".
5114 "Click ".$cgi->a({-href
=>href
(project
=>undef)},"here")." to view all projects<br />\n".
5115 "</center>\n<br />\n";
5119 @projects = sort_projects_list
(\
@projects, $order);
5122 my $ctags = git_gather_all_ctags
(\
@projects);
5123 my $cloud = git_populate_project_tagcloud
($ctags);
5124 print git_show_project_tagcloud
($cloud, 64);
5127 print "<table class=\"project_list\">\n";
5128 unless ($no_header) {
5131 print "<th></th>\n";
5133 print_sort_th
('project', $order, 'Project');
5134 print_sort_th
('descr', $order, 'Description');
5135 print_sort_th
('owner', $order, 'Owner');
5136 print_sort_th
('age', $order, 'Last Change');
5137 print "<th></th>\n" . # for links
5141 if ($projects_list_group_categories) {
5142 # only display categories with projects in the $from-$to window
5143 @projects = sort {$a->{'category'} cmp $b->{'category'}} @projects[$from..$to];
5144 my %categories = build_projlist_by_category
(\
@projects, $from, $to);
5145 foreach my $cat (sort keys %categories) {
5146 unless ($cat eq "") {
5149 print "<td></td>\n";
5151 print "<td class=\"category\" colspan=\"5\">".esc_html
($cat)."</td>\n";
5155 git_project_list_rows
($categories{$cat}, undef, undef, $check_forks);
5158 git_project_list_rows
(\
@projects, $from, $to, $check_forks);
5161 if (defined $extra) {
5164 print "<td></td>\n";
5166 print "<td colspan=\"5\">$extra</td>\n" .
5173 # uses global variable $project
5174 my ($commitlist, $from, $to, $refs, $extra) = @_;
5176 $from = 0 unless defined $from;
5177 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
5179 for (my $i = 0; $i <= $to; $i++) {
5180 my %co = %{$commitlist->[$i]};
5182 my $commit = $co{'id'};
5183 my $ref = format_ref_marker
($refs, $commit);
5184 git_print_header_div
('commit',
5185 "<span class=\"age\">$co{'age_string'}</span>" .
5186 esc_html
($co{'title'}) . $ref,
5188 print "<div class=\"title_text\">\n" .
5189 "<div class=\"log_link\">\n" .
5190 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$commit)}, "commit") .
5192 $cgi->a({-href
=> href
(action
=>"commitdiff", hash
=>$commit)}, "commitdiff") .
5194 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$commit, hash_base
=>$commit)}, "tree") .
5197 git_print_authorship
(\
%co, -tag
=> 'span');
5198 print "<br/>\n</div>\n";
5200 print "<div class=\"log_body\">\n";
5201 git_print_log
($co{'comment'}, -final_empty_line
=> 1);
5205 print "<div class=\"page_nav\">\n";
5211 sub git_shortlog_body
{
5212 # uses global variable $project
5213 my ($commitlist, $from, $to, $refs, $extra) = @_;
5215 $from = 0 unless defined $from;
5216 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
5218 print "<table class=\"shortlog\">\n";
5220 for (my $i = $from; $i <= $to; $i++) {
5221 my %co = %{$commitlist->[$i]};
5222 my $commit = $co{'id'};
5223 my $ref = format_ref_marker
($refs, $commit);
5225 print "<tr class=\"dark\">\n";
5227 print "<tr class=\"light\">\n";
5230 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
5231 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5232 format_author_html
('td', \
%co, 10) . "<td>";
5233 print format_subject_html
($co{'title'}, $co{'title_short'},
5234 href
(action
=>"commit", hash
=>$commit), $ref);
5236 "<td class=\"link\">" .
5237 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$commit)}, "commit") . " | " .
5238 $cgi->a({-href
=> href
(action
=>"commitdiff", hash
=>$commit)}, "commitdiff") . " | " .
5239 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$commit, hash_base
=>$commit)}, "tree");
5240 my $snapshot_links = format_snapshot_links
($commit);
5241 if (defined $snapshot_links) {
5242 print " | " . $snapshot_links;
5247 if (defined $extra) {
5249 "<td colspan=\"4\">$extra</td>\n" .
5255 sub git_history_body
{
5256 # Warning: assumes constant type (blob or tree) during history
5257 my ($commitlist, $from, $to, $refs, $extra,
5258 $file_name, $file_hash, $ftype) = @_;
5260 $from = 0 unless defined $from;
5261 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
5263 print "<table class=\"history\">\n";
5265 for (my $i = $from; $i <= $to; $i++) {
5266 my %co = %{$commitlist->[$i]};
5270 my $commit = $co{'id'};
5272 my $ref = format_ref_marker
($refs, $commit);
5275 print "<tr class=\"dark\">\n";
5277 print "<tr class=\"light\">\n";
5280 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5281 # shortlog: format_author_html('td', \%co, 10)
5282 format_author_html
('td', \
%co, 15, 3) . "<td>";
5283 # originally git_history used chop_str($co{'title'}, 50)
5284 print format_subject_html
($co{'title'}, $co{'title_short'},
5285 href
(action
=>"commit", hash
=>$commit), $ref);
5287 "<td class=\"link\">" .
5288 $cgi->a({-href
=> href
(action
=>$ftype, hash_base
=>$commit, file_name
=>$file_name)}, $ftype) . " | " .
5289 $cgi->a({-href
=> href
(action
=>"commitdiff", hash
=>$commit)}, "commitdiff");
5291 if ($ftype eq 'blob') {
5292 my $blob_current = $file_hash;
5293 my $blob_parent = git_get_hash_by_path
($commit, $file_name);
5294 if (defined $blob_current && defined $blob_parent &&
5295 $blob_current ne $blob_parent) {
5297 $cgi->a({-href
=> href
(action
=>"blobdiff",
5298 hash
=>$blob_current, hash_parent
=>$blob_parent,
5299 hash_base
=>$hash_base, hash_parent_base
=>$commit,
5300 file_name
=>$file_name)},
5307 if (defined $extra) {
5309 "<td colspan=\"4\">$extra</td>\n" .
5316 # uses global variable $project
5317 my ($taglist, $from, $to, $extra) = @_;
5318 $from = 0 unless defined $from;
5319 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
5321 print "<table class=\"tags\">\n";
5323 for (my $i = $from; $i <= $to; $i++) {
5324 my $entry = $taglist->[$i];
5326 my $comment = $tag{'subject'};
5328 if (defined $comment) {
5329 $comment_short = chop_str
($comment, 30, 5);
5332 print "<tr class=\"dark\">\n";
5334 print "<tr class=\"light\">\n";
5337 if (defined $tag{'age'}) {
5338 print "<td><i>$tag{'age'}</i></td>\n";
5340 print "<td></td>\n";
5343 $cgi->a({-href
=> href
(action
=>$tag{'reftype'}, hash
=>$tag{'refid'}),
5344 -class => "list name"}, esc_html
($tag{'name'})) .
5347 if (defined $comment) {
5348 print format_subject_html
($comment, $comment_short,
5349 href
(action
=>"tag", hash
=>$tag{'id'}));
5352 "<td class=\"selflink\">";
5353 if ($tag{'type'} eq "tag") {
5354 print $cgi->a({-href
=> href
(action
=>"tag", hash
=>$tag{'id'})}, "tag");
5359 "<td class=\"link\">" . " | " .
5360 $cgi->a({-href
=> href
(action
=>$tag{'reftype'}, hash
=>$tag{'refid'})}, $tag{'reftype'});
5361 if ($tag{'reftype'} eq "commit") {
5362 print " | " . $cgi->a({-href
=> href
(action
=>"shortlog", hash
=>$tag{'fullname'})}, "shortlog") .
5363 " | " . $cgi->a({-href
=> href
(action
=>"log", hash
=>$tag{'fullname'})}, "log");
5364 } elsif ($tag{'reftype'} eq "blob") {
5365 print " | " . $cgi->a({-href
=> href
(action
=>"blob_plain", hash
=>$tag{'refid'})}, "raw");
5370 if (defined $extra) {
5372 "<td colspan=\"5\">$extra</td>\n" .
5378 sub git_heads_body
{
5379 # uses global variable $project
5380 my ($headlist, $head, $from, $to, $extra) = @_;
5381 $from = 0 unless defined $from;
5382 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
5384 print "<table class=\"heads\">\n";
5386 for (my $i = $from; $i <= $to; $i++) {
5387 my $entry = $headlist->[$i];
5389 my $curr = $ref{'id'} eq $head;
5391 print "<tr class=\"dark\">\n";
5393 print "<tr class=\"light\">\n";
5396 print "<td><i>$ref{'age'}</i></td>\n" .
5397 ($curr ? "<td class=\"current_head\">" : "<td>") .
5398 $cgi->a({-href
=> href
(action
=>"shortlog", hash
=>$ref{'fullname'}),
5399 -class => "list name"},esc_html
($ref{'name'})) .
5401 "<td class=\"link\">" .
5402 $cgi->a({-href
=> href
(action
=>"shortlog", hash
=>$ref{'fullname'})}, "shortlog") . " | " .
5403 $cgi->a({-href
=> href
(action
=>"log", hash
=>$ref{'fullname'})}, "log") . " | " .
5404 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$ref{'fullname'}, hash_base
=>$ref{'fullname'})}, "tree") .
5408 if (defined $extra) {
5410 "<td colspan=\"3\">$extra</td>\n" .
5416 # Display a single remote block
5417 sub git_remote_block
{
5418 my ($remote, $rdata, $limit, $head) = @_;
5420 my $heads = $rdata->{'heads'};
5421 my $fetch = $rdata->{'fetch'};
5422 my $push = $rdata->{'push'};
5424 my $urls_table = "<table class=\"projects_list\">\n" ;
5426 if (defined $fetch) {
5427 if ($fetch eq $push) {
5428 $urls_table .= format_repo_url
("URL", $fetch);
5430 $urls_table .= format_repo_url
("Fetch URL", $fetch);
5431 $urls_table .= format_repo_url
("Push URL", $push) if defined $push;
5433 } elsif (defined $push) {
5434 $urls_table .= format_repo_url
("Push URL", $push);
5436 $urls_table .= format_repo_url
("", "No remote URL");
5439 $urls_table .= "</table>\n";
5442 if (defined $limit && $limit < @$heads) {
5443 $dots = $cgi->a({-href
=> href
(action
=>"remotes", hash
=>$remote)}, "...");
5447 git_heads_body
($heads, $head, 0, $limit, $dots);
5450 # Display a list of remote names with the respective fetch and push URLs
5451 sub git_remotes_list
{
5452 my ($remotedata, $limit) = @_;
5453 print "<table class=\"heads\">\n";
5455 my @remotes = sort keys %$remotedata;
5457 my $limited = $limit && $limit < @remotes;
5459 $#remotes = $limit - 1 if $limited;
5461 while (my $remote = shift @remotes) {
5462 my $rdata = $remotedata->{$remote};
5463 my $fetch = $rdata->{'fetch'};
5464 my $push = $rdata->{'push'};
5466 print "<tr class=\"dark\">\n";
5468 print "<tr class=\"light\">\n";
5472 $cgi->a({-href
=> href
(action
=>'remotes', hash
=>$remote),
5473 -class=> "list name"},esc_html
($remote)) .
5475 print "<td class=\"link\">" .
5476 (defined $fetch ? $cgi->a({-href
=> $fetch}, "fetch") : "fetch") .
5478 (defined $push ? $cgi->a({-href
=> $push}, "push") : "push") .
5486 "<td colspan=\"3\">" .
5487 $cgi->a({-href
=> href
(action
=>"remotes")}, "...") .
5488 "</td>\n" . "</tr>\n";
5494 # Display remote heads grouped by remote, unless there are too many
5495 # remotes, in which case we only display the remote names
5496 sub git_remotes_body
{
5497 my ($remotedata, $limit, $head) = @_;
5498 if ($limit and $limit < keys %$remotedata) {
5499 git_remotes_list
($remotedata, $limit);
5501 fill_remote_heads
($remotedata);
5502 while (my ($remote, $rdata) = each %$remotedata) {
5503 git_print_section
({-class=>"remote", -id
=>$remote},
5504 ["remotes", $remote, $remote], sub {
5505 git_remote_block
($remote, $rdata, $limit, $head);
5511 sub git_search_message
{
5515 if ($searchtype eq 'commit') {
5516 $greptype = "--grep=";
5517 } elsif ($searchtype eq 'author') {
5518 $greptype = "--author=";
5519 } elsif ($searchtype eq 'committer') {
5520 $greptype = "--committer=";
5522 $greptype .= $searchtext;
5523 my @commitlist = parse_commits
($hash, 101, (100 * $page), undef,
5524 $greptype, '--regexp-ignore-case',
5525 $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
5527 my $paging_nav = '';
5530 $cgi->a({-href
=> href
(action
=>"search", hash
=>$hash,
5531 searchtext
=>$searchtext,
5532 searchtype
=>$searchtype)},
5534 $paging_nav .= " ⋅ " .
5535 $cgi->a({-href
=> href
(-replay
=>1, page
=>$page-1),
5536 -accesskey
=> "p", -title
=> "Alt-p"}, "prev");
5538 $paging_nav .= "first";
5539 $paging_nav .= " ⋅ prev";
5542 if ($#commitlist >= 100) {
5544 $cgi->a({-href
=> href
(-replay
=>1, page
=>$page+1),
5545 -accesskey
=> "n", -title
=> "Alt-n"}, "next");
5546 $paging_nav .= " ⋅ $next_link";
5548 $paging_nav .= " ⋅ next";
5551 git_print_page_nav
('','', $hash,$co{'tree'},$hash, $paging_nav);
5552 git_print_header_div
('commit', esc_html
($co{'title'}), $hash);
5553 if ($page == 0 && !@commitlist) {
5554 print "<p>No match.</p>\n";
5556 git_search_grep_body
(\
@commitlist, 0, 99, $next_link);
5560 sub git_search_changes
{
5563 git_print_page_nav
('','', $hash,$co{'tree'},$hash);
5564 git_print_header_div
('commit', esc_html
($co{'title'}), $hash);
5566 print "<table class=\"pickaxe search\">\n";
5569 open my $fd, '-|', git_cmd
(), '--no-pager', 'log', @diff_opts,
5570 '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
5571 ($search_use_regexp ? '--pickaxe-regex' : ());
5574 while (my $line = <$fd>) {
5578 my %set = parse_difftree_raw_line
($line);
5579 if (defined $set{'commit'}) {
5580 # finish previous commit
5583 "<td class=\"link\">" .
5584 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$co{'id'})}, "commit") .
5586 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$co{'tree'}, hash_base
=>$co{'id'})}, "tree");
5592 print "<tr class=\"dark\">\n";
5594 print "<tr class=\"light\">\n";
5597 %co = parse_commit
($set{'commit'});
5598 my $author = chop_and_escape_str
($co{'author_name'}, 15, 5);
5599 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5600 "<td><i>$author</i></td>\n" .
5602 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$co{'id'}),
5603 -class => "list subject"},
5604 chop_and_escape_str
($co{'title'}, 50) . "<br/>");
5605 } elsif (defined $set{'to_id'}) {
5606 next if ($set{'to_id'} =~ m/^0{40}$/);
5608 print $cgi->a({-href
=> href
(action
=>"blob", hash_base
=>$co{'id'},
5609 hash
=>$set{'to_id'}, file_name
=>$set{'to_file'}),
5611 "<span class=\"match\">" . esc_path
($set{'file'}) . "</span>") .
5617 # finish last commit (warning: repetition!)
5620 "<td class=\"link\">" .
5621 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$co{'id'})}, "commit") .
5623 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$co{'tree'}, hash_base
=>$co{'id'})}, "tree");
5631 sub git_search_files
{
5634 git_print_page_nav
('','', $hash,$co{'tree'},$hash);
5635 git_print_header_div
('commit', esc_html
($co{'title'}), $hash);
5637 print "<table class=\"grep_search\">\n";
5641 open my $fd, "-|", git_cmd
(), 'grep', '-n',
5642 $search_use_regexp ? ('-E', '-i') : '-F',
5643 $searchtext, $co{'tree'};
5645 while (my $line = <$fd>) {
5647 my ($file, $lno, $ltext, $binary);
5648 last if ($matches++ > 1000);
5649 if ($line =~ /^Binary file (.+) matches$/) {
5653 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
5655 if ($file ne $lastfile) {
5656 $lastfile and print "</td></tr>\n";
5658 print "<tr class=\"dark\">\n";
5660 print "<tr class=\"light\">\n";
5662 print "<td class=\"list\">".
5663 $cgi->a({-href
=> href
(action
=>"blob", hash
=>$co{'hash'},
5664 file_name
=>"$file"),
5665 -class => "list"}, esc_path
($file));
5666 print "</td><td>\n";
5670 print "<div class=\"binary\">Binary file</div>\n";
5672 $ltext = untabify
($ltext);
5673 if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
5674 $ltext = esc_html
($1, -nbsp
=>1);
5675 $ltext .= '<span class="match">';
5676 $ltext .= esc_html
($2, -nbsp
=>1);
5677 $ltext .= '</span>';
5678 $ltext .= esc_html
($3, -nbsp
=>1);
5680 $ltext = esc_html
($ltext, -nbsp
=>1);
5682 print "<div class=\"pre\">" .
5683 $cgi->a({-href
=> href
(action
=>"blob", hash
=>$co{'hash'},
5684 file_name
=>"$file").'#l'.$lno,
5685 -class => "linenr"}, sprintf('%4i', $lno))
5686 . ' ' . $ltext . "</div>\n";
5690 print "</td></tr>\n";
5691 if ($matches > 1000) {
5692 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
5695 print "<div class=\"diff nodifferences\">No matches found</div>\n";
5702 sub git_search_grep_body
{
5703 my ($commitlist, $from, $to, $extra) = @_;
5704 $from = 0 unless defined $from;
5705 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
5707 print "<table class=\"commit_search\">\n";
5709 for (my $i = $from; $i <= $to; $i++) {
5710 my %co = %{$commitlist->[$i]};
5714 my $commit = $co{'id'};
5716 print "<tr class=\"dark\">\n";
5718 print "<tr class=\"light\">\n";
5721 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5722 format_author_html
('td', \
%co, 15, 5) .
5724 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$co{'id'}),
5725 -class => "list subject"},
5726 chop_and_escape_str
($co{'title'}, 50) . "<br/>");
5727 my $comment = $co{'comment'};
5728 foreach my $line (@$comment) {
5729 if ($line =~ m/^(.*?)($search_regexp)(.*)$/i) {
5730 my ($lead, $match, $trail) = ($1, $2, $3);
5731 $match = chop_str
($match, 70, 5, 'center');
5732 my $contextlen = int((80 - length($match))/2);
5733 $contextlen = 30 if ($contextlen > 30);
5734 $lead = chop_str
($lead, $contextlen, 10, 'left');
5735 $trail = chop_str
($trail, $contextlen, 10, 'right');
5737 $lead = esc_html
($lead);
5738 $match = esc_html
($match);
5739 $trail = esc_html
($trail);
5741 print "$lead<span class=\"match\">$match</span>$trail<br />";
5745 "<td class=\"link\">" .
5746 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$co{'id'})}, "commit") .
5748 $cgi->a({-href
=> href
(action
=>"commitdiff", hash
=>$co{'id'})}, "commitdiff") .
5750 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$co{'tree'}, hash_base
=>$co{'id'})}, "tree");
5754 if (defined $extra) {
5756 "<td colspan=\"3\">$extra</td>\n" .
5762 ## ======================================================================
5763 ## ======================================================================
5766 sub git_project_list
{
5767 my $order = $input_params{'order'};
5768 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
5769 die_error
(400, "Unknown order parameter");
5772 my @list = git_get_projects_list
();
5774 die_error
(404, "No projects found");
5778 if (defined $home_text && -f
$home_text) {
5779 print "<div class=\"index_include\">\n";
5780 insert_file
($home_text);
5783 print $cgi->startform(-method => "get") .
5784 "<p class=\"projsearch\">Search:\n" .
5785 $cgi->textfield(-name
=> "s", -value
=> $searchtext) . "\n" .
5787 $cgi->end_form() . "\n";
5788 git_project_list_body
(\
@list, $order);
5793 my $order = $input_params{'order'};
5794 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
5795 die_error
(400, "Unknown order parameter");
5798 my @list = git_get_projects_list
($project);
5800 die_error
(404, "No forks found");
5804 git_print_page_nav
('','');
5805 git_print_header_div
('summary', "$project forks");
5806 git_project_list_body
(\
@list, $order);
5810 sub git_project_index
{
5811 my @projects = git_get_projects_list
();
5813 die_error
(404, "No projects found");
5817 -type
=> 'text/plain',
5818 -charset
=> 'utf-8',
5819 -content_disposition
=> 'inline; filename="index.aux"');
5821 foreach my $pr (@projects) {
5822 if (!exists $pr->{'owner'}) {
5823 $pr->{'owner'} = git_get_project_owner
("$pr->{'path'}");
5826 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
5827 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
5828 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf
("%%%02X", ord($1))/eg
;
5829 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf
("%%%02X", ord($1))/eg
;
5833 print "$path $owner\n";
5838 my $descr = git_get_project_description
($project) || "none";
5839 my %co = parse_commit
("HEAD");
5840 my %cd = %co ? parse_date
($co{'committer_epoch'}, $co{'committer_tz'}) : ();
5841 my $head = $co{'id'};
5842 my $remote_heads = gitweb_check_feature
('remote_heads');
5844 my $owner = git_get_project_owner
($project);
5846 my $refs = git_get_references
();
5847 # These get_*_list functions return one more to allow us to see if
5848 # there are more ...
5849 my @taglist = git_get_tags_list
(16);
5850 my @headlist = git_get_heads_list
(16);
5851 my %remotedata = $remote_heads ? git_get_remotes_list
() : ();
5853 my $check_forks = gitweb_check_feature
('forks');
5856 # find forks of a project
5857 @forklist = git_get_projects_list
($project);
5858 # filter out forks of forks
5859 @forklist = filter_forks_from_projects_list
(\
@forklist)
5864 git_print_page_nav
('summary','', $head);
5866 print "<div class=\"title\"> </div>\n";
5867 print "<table class=\"projects_list\">\n" .
5868 "<tr id=\"metadata_desc\"><td>description</td><td>" . esc_html
($descr) . "</td></tr>\n" .
5869 "<tr id=\"metadata_owner\"><td>owner</td><td>" . esc_html
($owner) . "</td></tr>\n";
5870 if (defined $cd{'rfc2822'}) {
5871 print "<tr id=\"metadata_lchange\"><td>last change</td>" .
5872 "<td>".format_timestamp_html
(\
%cd)."</td></tr>\n";
5875 # use per project git URL list in $projectroot/$project/cloneurl
5876 # or make project git URL from git base URL and project name
5877 my $url_tag = "URL";
5878 my @url_list = git_get_project_url_list
($project);
5879 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
5880 foreach my $git_url (@url_list) {
5881 next unless $git_url;
5882 print format_repo_url
($url_tag, $git_url);
5887 my $show_ctags = gitweb_check_feature
('ctags');
5889 my $ctags = git_get_project_ctags
($project);
5891 # without ability to add tags, don't show if there are none
5892 my $cloud = git_populate_project_tagcloud
($ctags);
5893 print "<tr id=\"metadata_ctags\">" .
5894 "<td>content tags</td>" .
5895 "<td>".git_show_project_tagcloud
($cloud, 48)."</td>" .
5902 # If XSS prevention is on, we don't include README.html.
5903 # TODO: Allow a readme in some safe format.
5904 if (!$prevent_xss && -s
"$projectroot/$project/README.html") {
5905 print "<div class=\"title\">readme</div>\n" .
5906 "<div class=\"readme\">\n";
5907 insert_file
("$projectroot/$project/README.html");
5908 print "\n</div>\n"; # class="readme"
5911 # we need to request one more than 16 (0..15) to check if
5913 my @commitlist = $head ? parse_commits
($head, 17) : ();
5915 git_print_header_div
('shortlog');
5916 git_shortlog_body
(\
@commitlist, 0, 15, $refs,
5917 $#commitlist <= 15 ? undef :
5918 $cgi->a({-href
=> href
(action
=>"shortlog")}, "..."));
5922 git_print_header_div
('tags');
5923 git_tags_body
(\
@taglist, 0, 15,
5924 $#taglist <= 15 ? undef :
5925 $cgi->a({-href
=> href
(action
=>"tags")}, "..."));
5929 git_print_header_div
('heads');
5930 git_heads_body
(\
@headlist, $head, 0, 15,
5931 $#headlist <= 15 ? undef :
5932 $cgi->a({-href
=> href
(action
=>"heads")}, "..."));
5936 git_print_header_div
('remotes');
5937 git_remotes_body
(\
%remotedata, 15, $head);
5941 git_print_header_div
('forks');
5942 git_project_list_body
(\
@forklist, 'age', 0, 15,
5943 $#forklist <= 15 ? undef :
5944 $cgi->a({-href
=> href
(action
=>"forks")}, "..."),
5952 my %tag = parse_tag
($hash);
5955 die_error
(404, "Unknown tag object");
5958 my $head = git_get_head_hash
($project);
5960 git_print_page_nav
('','', $head,undef,$head);
5961 git_print_header_div
('commit', esc_html
($tag{'name'}), $hash);
5962 print "<div class=\"title_text\">\n" .
5963 "<table class=\"object_header\">\n" .
5965 "<td>object</td>\n" .
5966 "<td>" . $cgi->a({-class => "list", -href
=> href
(action
=>$tag{'type'}, hash
=>$tag{'object'})},
5967 $tag{'object'}) . "</td>\n" .
5968 "<td class=\"link\">" . $cgi->a({-href
=> href
(action
=>$tag{'type'}, hash
=>$tag{'object'})},
5969 $tag{'type'}) . "</td>\n" .
5971 if (defined($tag{'author'})) {
5972 git_print_authorship_rows
(\
%tag, 'author');
5974 print "</table>\n\n" .
5976 print "<div class=\"page_body\">";
5977 my $comment = $tag{'comment'};
5978 foreach my $line (@$comment) {
5980 print esc_html
($line, -nbsp
=>1) . "<br/>\n";
5986 sub git_blame_common
{
5987 my $format = shift || 'porcelain';
5988 if ($format eq 'porcelain' && $cgi->param('js')) {
5989 $format = 'incremental';
5990 $action = 'blame_incremental'; # for page title etc
5994 gitweb_check_feature
('blame')
5995 or die_error
(403, "Blame view not allowed");
5998 die_error
(400, "No file name given") unless $file_name;
5999 $hash_base ||= git_get_head_hash
($project);
6000 die_error
(404, "Couldn't find base commit") unless $hash_base;
6001 my %co = parse_commit
($hash_base)
6002 or die_error
(404, "Commit not found");
6004 if (!defined $hash) {
6005 $hash = git_get_hash_by_path
($hash_base, $file_name, "blob")
6006 or die_error
(404, "Error looking up file");
6008 $ftype = git_get_type
($hash);
6009 if ($ftype !~ "blob") {
6010 die_error
(400, "Object is not a blob");
6015 if ($format eq 'incremental') {
6016 # get file contents (as base)
6017 open $fd, "-|", git_cmd
(), 'cat-file', 'blob', $hash
6018 or die_error
(500, "Open git-cat-file failed");
6019 } elsif ($format eq 'data') {
6020 # run git-blame --incremental
6021 open $fd, "-|", git_cmd
(), "blame", "--incremental",
6022 $hash_base, "--", $file_name
6023 or die_error
(500, "Open git-blame --incremental failed");
6025 # run git-blame --porcelain
6026 open $fd, "-|", git_cmd
(), "blame", '-p',
6027 $hash_base, '--', $file_name
6028 or die_error
(500, "Open git-blame --porcelain failed");
6031 # incremental blame data returns early
6032 if ($format eq 'data') {
6034 -type
=>"text/plain", -charset
=> "utf-8",
6035 -status
=> "200 OK");
6036 local $| = 1; # output autoflush
6039 or print "ERROR $!\n";
6042 if (defined $t0 && gitweb_check_feature
('timed')) {
6044 tv_interval
($t0, [ gettimeofday
() ]).
6045 ' '.$number_of_git_cmds;
6055 $cgi->a({-href
=> href
(action
=>"blob", -replay
=>1)},
6058 if ($format eq 'incremental') {
6060 $cgi->a({-href
=> href
(action
=>"blame", javascript
=>0, -replay
=>1)},
6061 "blame") . " (non-incremental)";
6064 $cgi->a({-href
=> href
(action
=>"blame_incremental", -replay
=>1)},
6065 "blame") . " (incremental)";
6069 $cgi->a({-href
=> href
(action
=>"history", -replay
=>1)},
6072 $cgi->a({-href
=> href
(action
=>$action, file_name
=>$file_name)},
6074 git_print_page_nav
('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
6075 git_print_header_div
('commit', esc_html
($co{'title'}), $hash_base);
6076 git_print_page_path
($file_name, $ftype, $hash_base);
6079 if ($format eq 'incremental') {
6080 print "<noscript>\n<div class=\"error\"><center><b>\n".
6081 "This page requires JavaScript to run.\n Use ".
6082 $cgi->a({-href
=> href
(action
=>'blame',javascript
=>0,-replay
=>1)},
6085 "</b></center></div>\n</noscript>\n";
6087 print qq
!<div id
="progress_bar" style
="width: 100%; background-color: yellow"></div
>\n!;
6090 print qq
!<div
class="page_body">\n!;
6091 print qq
!<div id
="progress_info">... / ...</div
>\n!
6092 if ($format eq 'incremental');
6093 print qq
!<table id
="blame_table" class="blame" width
="100%">\n!.
6094 #qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.
6096 qq
!<tr
><th
>Commit
</th><th>Line</th
><th
>Data
</th></tr
>\n!.
6100 my @rev_color = qw(light dark);
6101 my $num_colors = scalar(@rev_color);
6102 my $current_color = 0;
6104 if ($format eq 'incremental') {
6105 my $color_class = $rev_color[$current_color];
6110 while (my $line = <$fd>) {
6114 print qq
!<tr id
="l$linenr" class="$color_class">!.
6115 qq
!<td
class="sha1"><a href
=""> </a></td
>!.
6116 qq
!<td
class="linenr">!.
6117 qq
!<a
class="linenr" href
="">$linenr</a></td
>!;
6118 print qq
!<td
class="pre">! . esc_html
($line) . "</td>\n";
6122 } else { # porcelain, i.e. ordinary blame
6123 my %metainfo = (); # saves information about commits
6127 while (my $line = <$fd>) {
6129 # the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]
6130 # no <lines in group> for subsequent lines in group of lines
6131 my ($full_rev, $orig_lineno, $lineno, $group_size) =
6132 ($line =~ /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);
6133 if (!exists $metainfo{$full_rev}) {
6134 $metainfo{$full_rev} = { 'nprevious' => 0 };
6136 my $meta = $metainfo{$full_rev};
6138 while ($data = <$fd>) {
6140 last if ($data =~ s/^\t//); # contents of line
6141 if ($data =~ /^(\S+)(?: (.*))?$/) {
6142 $meta->{$1} = $2 unless exists $meta->{$1};
6144 if ($data =~ /^previous /) {
6145 $meta->{'nprevious'}++;
6148 my $short_rev = substr($full_rev, 0, 8);
6149 my $author = $meta->{'author'};
6151 parse_date
($meta->{'author-time'}, $meta->{'author-tz'});
6152 my $date = $date{'iso-tz'};
6154 $current_color = ($current_color + 1) % $num_colors;
6156 my $tr_class = $rev_color[$current_color];
6157 $tr_class .= ' boundary' if (exists $meta->{'boundary'});
6158 $tr_class .= ' no-previous' if ($meta->{'nprevious'} == 0);
6159 $tr_class .= ' multiple-previous' if ($meta->{'nprevious'} > 1);
6160 print "<tr id=\"l$lineno\" class=\"$tr_class\">\n";
6162 print "<td class=\"sha1\"";
6163 print " title=\"". esc_html
($author) . ", $date\"";
6164 print " rowspan=\"$group_size\"" if ($group_size > 1);
6166 print $cgi->a({-href
=> href
(action
=>"commit",
6168 file_name
=>$file_name)},
6169 esc_html
($short_rev));
6170 if ($group_size >= 2) {
6171 my @author_initials = ($author =~ /\b([[:upper:]])\B/g);
6172 if (@author_initials) {
6174 esc_html
(join('', @author_initials));
6180 # 'previous' <sha1 of parent commit> <filename at commit>
6181 if (exists $meta->{'previous'} &&
6182 $meta->{'previous'} =~ /^([a-fA-F0-9]{40}) (.*)$/) {
6183 $meta->{'parent'} = $1;
6184 $meta->{'file_parent'} = unquote
($2);
6187 exists($meta->{'parent'}) ?
6188 $meta->{'parent'} : $full_rev;
6189 my $linenr_filename =
6190 exists($meta->{'file_parent'}) ?
6191 $meta->{'file_parent'} : unquote
($meta->{'filename'});
6192 my $blamed = href
(action
=> 'blame',
6193 file_name
=> $linenr_filename,
6194 hash_base
=> $linenr_commit);
6195 print "<td class=\"linenr\">";
6196 print $cgi->a({ -href
=> "$blamed#l$orig_lineno",
6197 -class => "linenr" },
6200 print "<td class=\"pre\">" . esc_html
($data) . "</td>\n";
6208 "</table>\n"; # class="blame"
6209 print "</div>\n"; # class="blame_body"
6211 or print "Reading blob failed\n";
6220 sub git_blame_incremental
{
6221 git_blame_common
('incremental');
6224 sub git_blame_data
{
6225 git_blame_common
('data');
6229 my $head = git_get_head_hash
($project);
6231 git_print_page_nav
('','', $head,undef,$head,format_ref_views
('tags'));
6232 git_print_header_div
('summary', $project);
6234 my @tagslist = git_get_tags_list
();
6236 git_tags_body
(\
@tagslist);
6242 my $head = git_get_head_hash
($project);
6244 git_print_page_nav
('','', $head,undef,$head,format_ref_views
('heads'));
6245 git_print_header_div
('summary', $project);
6247 my @headslist = git_get_heads_list
();
6249 git_heads_body
(\
@headslist, $head);
6254 # used both for single remote view and for list of all the remotes
6256 gitweb_check_feature
('remote_heads')
6257 or die_error
(403, "Remote heads view is disabled");
6259 my $head = git_get_head_hash
($project);
6260 my $remote = $input_params{'hash'};
6262 my $remotedata = git_get_remotes_list
($remote);
6263 die_error
(500, "Unable to get remote information") unless defined $remotedata;
6265 unless (%$remotedata) {
6266 die_error
(404, defined $remote ?
6267 "Remote $remote not found" :
6268 "No remotes found");
6271 git_header_html
(undef, undef, -action_extra
=> $remote);
6272 git_print_page_nav
('', '', $head, undef, $head,
6273 format_ref_views
($remote ? '' : 'remotes'));
6275 fill_remote_heads
($remotedata);
6276 if (defined $remote) {
6277 git_print_header_div
('remotes', "$remote remote for $project");
6278 git_remote_block
($remote, $remotedata->{$remote}, undef, $head);
6280 git_print_header_div
('summary', "$project remotes");
6281 git_remotes_body
($remotedata, undef, $head);
6287 sub git_blob_plain
{
6291 if (!defined $hash) {
6292 if (defined $file_name) {
6293 my $base = $hash_base || git_get_head_hash
($project);
6294 $hash = git_get_hash_by_path
($base, $file_name, "blob")
6295 or die_error
(404, "Cannot find file");
6297 die_error
(400, "No file name defined");
6299 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6300 # blobs defined by non-textual hash id's can be cached
6304 open my $fd, "-|", git_cmd
(), "cat-file", "blob", $hash
6305 or die_error
(500, "Open git-cat-file blob '$hash' failed");
6307 # content-type (can include charset)
6308 $type = blob_contenttype
($fd, $file_name, $type);
6310 # "save as" filename, even when no $file_name is given
6311 my $save_as = "$hash";
6312 if (defined $file_name) {
6313 $save_as = $file_name;
6314 } elsif ($type =~ m/^text\//) {
6318 # With XSS prevention on, blobs of all types except a few known safe
6319 # ones are served with "Content-Disposition: attachment" to make sure
6320 # they don't run in our security domain. For certain image types,
6321 # blob view writes an <img> tag referring to blob_plain view, and we
6322 # want to be sure not to break that by serving the image as an
6323 # attachment (though Firefox 3 doesn't seem to care).
6324 my $sandbox = $prevent_xss &&
6325 $type !~ m!^(?:text/plain|image/(?:gif|png|jpeg))(?:[ ;]|$)!;
6329 -expires
=> $expires,
6330 -content_disposition
=>
6331 ($sandbox ? 'attachment' : 'inline')
6332 . '; filename="' . $save_as . '"');
6334 binmode STDOUT
, ':raw';
6336 binmode STDOUT
, ':utf8'; # as set at the beginning of gitweb.cgi
6343 if (!defined $hash) {
6344 if (defined $file_name) {
6345 my $base = $hash_base || git_get_head_hash
($project);
6346 $hash = git_get_hash_by_path
($base, $file_name, "blob")
6347 or die_error
(404, "Cannot find file");
6349 die_error
(400, "No file name defined");
6351 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6352 # blobs defined by non-textual hash id's can be cached
6356 my $have_blame = gitweb_check_feature
('blame');
6357 open my $fd, "-|", git_cmd
(), "cat-file", "blob", $hash
6358 or die_error
(500, "Couldn't cat $file_name, $hash");
6359 my $mimetype = blob_mimetype
($fd, $file_name);
6360 # use 'blob_plain' (aka 'raw') view for files that cannot be displayed
6361 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B
$fd) {
6363 return git_blob_plain
($mimetype);
6365 # we can have blame only for text/* mimetype
6366 $have_blame &&= ($mimetype =~ m!^text/!);
6368 my $highlight = gitweb_check_feature
('highlight');
6369 my $syntax = guess_file_syntax
($highlight, $mimetype, $file_name);
6370 $fd = run_highlighter
($fd, $highlight, $syntax)
6373 git_header_html
(undef, $expires);
6374 my $formats_nav = '';
6375 if (defined $hash_base && (my %co = parse_commit
($hash_base))) {
6376 if (defined $file_name) {
6379 $cgi->a({-href
=> href
(action
=>"blame", -replay
=>1)},
6384 $cgi->a({-href
=> href
(action
=>"history", -replay
=>1)},
6387 $cgi->a({-href
=> href
(action
=>"blob_plain", -replay
=>1)},
6390 $cgi->a({-href
=> href
(action
=>"blob",
6391 hash_base
=>"HEAD", file_name
=>$file_name)},
6395 $cgi->a({-href
=> href
(action
=>"blob_plain", -replay
=>1)},
6398 git_print_page_nav
('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
6399 git_print_header_div
('commit', esc_html
($co{'title'}), $hash_base);
6401 print "<div class=\"page_nav\">\n" .
6402 "<br/><br/></div>\n" .
6403 "<div class=\"title\">".esc_html
($hash)."</div>\n";
6405 git_print_page_path
($file_name, "blob", $hash_base);
6406 print "<div class=\"page_body\">\n";
6407 if ($mimetype =~ m!^image/!) {
6408 print qq
!<img type
="!.esc_attr($mimetype).qq!"!;
6410 print qq
! alt
="!.esc_attr($file_name).qq!" title
="!.esc_attr($file_name).qq!"!;
6413 href(action=>"blob_plain
", hash=>$hash,
6414 hash_base=>$hash_base, file_name=>$file_name) .
6418 while (my $line = <$fd>) {
6421 $line = untabify
($line);
6422 printf qq
!<div
class="pre"><a id
="l%i" href
="%s#l%i" class="linenr">%4i</a> %s</div
>\n!,
6423 $nr, href
(-replay
=> 1), $nr, $nr, $syntax ? $line : esc_html
($line, -nbsp
=>1);
6427 or print "Reading blob failed.\n";
6433 if (!defined $hash_base) {
6434 $hash_base = "HEAD";
6436 if (!defined $hash) {
6437 if (defined $file_name) {
6438 $hash = git_get_hash_by_path
($hash_base, $file_name, "tree");
6443 die_error
(404, "No such tree") unless defined($hash);
6445 my $show_sizes = gitweb_check_feature
('show-sizes');
6446 my $have_blame = gitweb_check_feature
('blame');
6451 open my $fd, "-|", git_cmd
(), "ls-tree", '-z',
6452 ($show_sizes ? '-l' : ()), @extra_options, $hash
6453 or die_error
(500, "Open git-ls-tree failed");
6454 @entries = map { chomp; $_ } <$fd>;
6456 or die_error
(404, "Reading tree failed");
6459 my $refs = git_get_references
();
6460 my $ref = format_ref_marker
($refs, $hash_base);
6463 if (defined $hash_base && (my %co = parse_commit
($hash_base))) {
6465 if (defined $file_name) {
6467 $cgi->a({-href
=> href
(action
=>"history", -replay
=>1)},
6469 $cgi->a({-href
=> href
(action
=>"tree",
6470 hash_base
=>"HEAD", file_name
=>$file_name)},
6473 my $snapshot_links = format_snapshot_links
($hash);
6474 if (defined $snapshot_links) {
6475 # FIXME: Should be available when we have no hash base as well.
6476 push @views_nav, $snapshot_links;
6478 git_print_page_nav
('tree','', $hash_base, undef, undef,
6479 join(' | ', @views_nav));
6480 git_print_header_div
('commit', esc_html
($co{'title'}) . $ref, $hash_base);
6483 print "<div class=\"page_nav\">\n";
6484 print "<br/><br/></div>\n";
6485 print "<div class=\"title\">".esc_html
($hash)."</div>\n";
6487 if (defined $file_name) {
6488 $basedir = $file_name;
6489 if ($basedir ne '' && substr($basedir, -1) ne '/') {
6492 git_print_page_path
($file_name, 'tree', $hash_base);
6494 print "<div class=\"page_body\">\n";
6495 print "<table class=\"tree\">\n";
6497 # '..' (top directory) link if possible
6498 if (defined $hash_base &&
6499 defined $file_name && $file_name =~ m![^/]+$!) {
6501 print "<tr class=\"dark\">\n";
6503 print "<tr class=\"light\">\n";
6507 my $up = $file_name;
6508 $up =~ s!/?[^/]+$!!;
6509 undef $up unless $up;
6510 # based on git_print_tree_entry
6511 print '<td class="mode">' . mode_str
('040000') . "</td>\n";
6512 print '<td class="size"> </td>'."\n" if $show_sizes;
6513 print '<td class="list">';
6514 print $cgi->a({-href
=> href
(action
=>"tree",
6515 hash_base
=>$hash_base,
6519 print "<td class=\"link\"></td>\n";
6523 foreach my $line (@entries) {
6524 my %t = parse_ls_tree_line
($line, -z
=> 1, -l
=> $show_sizes);
6527 print "<tr class=\"dark\">\n";
6529 print "<tr class=\"light\">\n";
6533 git_print_tree_entry
(\
%t, $basedir, $hash_base, $have_blame);
6537 print "</table>\n" .
6543 my ($project, $hash) = @_;
6545 # path/to/project.git -> project
6546 # path/to/project/.git -> project
6547 my $name = to_utf8
($project);
6548 $name =~ s
,([^/])/*\
.git
$,$1,;
6549 $name = basename
($name);
6551 $name =~ s/[[:cntrl:]]/?/g;
6554 if ($hash =~ /^[0-9a-fA-F]+$/) {
6555 # shorten SHA-1 hash
6556 my $full_hash = git_get_full_hash
($project, $hash);
6557 if ($full_hash =~ /^$hash/ && length($hash) > 7) {
6558 $ver = git_get_short_hash
($project, $hash);
6560 } elsif ($hash =~ m!^refs/tags/(.*)$!) {
6561 # tags don't need shortened SHA-1 hash
6564 # branches and other need shortened SHA-1 hash
6565 if ($hash =~ m!^refs/(?:heads|remotes)/(.*)$!) {
6568 $ver .= '-' . git_get_short_hash
($project, $hash);
6570 # in case of hierarchical branch names
6573 # name = project-version_string
6574 $name = "$name-$ver";
6576 return wantarray ? ($name, $name) : $name;
6580 my $format = $input_params{'snapshot_format'};
6581 if (!@snapshot_fmts) {
6582 die_error
(403, "Snapshots not allowed");
6584 # default to first supported snapshot format
6585 $format ||= $snapshot_fmts[0];
6586 if ($format !~ m/^[a-z0-9]+$/) {
6587 die_error
(400, "Invalid snapshot format parameter");
6588 } elsif (!exists($known_snapshot_formats{$format})) {
6589 die_error
(400, "Unknown snapshot format");
6590 } elsif ($known_snapshot_formats{$format}{'disabled'}) {
6591 die_error
(403, "Snapshot format not allowed");
6592 } elsif (!grep($_ eq $format, @snapshot_fmts)) {
6593 die_error
(403, "Unsupported snapshot format");
6596 my $type = git_get_type
("$hash^{}");
6598 die_error
(404, 'Object does not exist');
6599 } elsif ($type eq 'blob') {
6600 die_error
(400, 'Object is not a tree-ish');
6603 my ($name, $prefix) = snapshot_name
($project, $hash);
6604 my $filename = "$name$known_snapshot_formats{$format}{'suffix'}";
6605 my $cmd = quote_command
(
6606 git_cmd
(), 'archive',
6607 "--format=$known_snapshot_formats{$format}{'format'}",
6608 "--prefix=$prefix/", $hash);
6609 if (exists $known_snapshot_formats{$format}{'compressor'}) {
6610 $cmd .= ' | ' . quote_command
(@{$known_snapshot_formats{$format}{'compressor'}});
6613 $filename =~ s/(["\\])/\\$1/g;
6615 -type
=> $known_snapshot_formats{$format}{'type'},
6616 -content_disposition
=> 'inline; filename="' . $filename . '"',
6617 -status
=> '200 OK');
6619 open my $fd, "-|", $cmd
6620 or die_error
(500, "Execute git-archive failed");
6621 binmode STDOUT
, ':raw';
6623 binmode STDOUT
, ':utf8'; # as set at the beginning of gitweb.cgi
6627 sub git_log_generic
{
6628 my ($fmt_name, $body_subr, $base, $parent, $file_name, $file_hash) = @_;
6630 my $head = git_get_head_hash
($project);
6631 if (!defined $base) {
6634 if (!defined $page) {
6637 my $refs = git_get_references
();
6639 my $commit_hash = $base;
6640 if (defined $parent) {
6641 $commit_hash = "$parent..$base";
6644 parse_commits
($commit_hash, 101, (100 * $page),
6645 defined $file_name ? ($file_name, "--full-history") : ());
6648 if (!defined $file_hash && defined $file_name) {
6649 # some commits could have deleted file in question,
6650 # and not have it in tree, but one of them has to have it
6651 for (my $i = 0; $i < @commitlist; $i++) {
6652 $file_hash = git_get_hash_by_path
($commitlist[$i]{'id'}, $file_name);
6653 last if defined $file_hash;
6656 if (defined $file_hash) {
6657 $ftype = git_get_type
($file_hash);
6659 if (defined $file_name && !defined $ftype) {
6660 die_error
(500, "Unknown type of object");
6663 if (defined $file_name) {
6664 %co = parse_commit
($base)
6665 or die_error
(404, "Unknown commit object");
6669 my $paging_nav = format_paging_nav
($fmt_name, $page, $#commitlist >= 100);
6671 if ($#commitlist >= 100) {
6673 $cgi->a({-href
=> href
(-replay
=>1, page
=>$page+1),
6674 -accesskey
=> "n", -title
=> "Alt-n"}, "next");
6676 my $patch_max = gitweb_get_feature
('patches');
6677 if ($patch_max && !defined $file_name) {
6678 if ($patch_max < 0 || @commitlist <= $patch_max) {
6679 $paging_nav .= " ⋅ " .
6680 $cgi->a({-href
=> href
(action
=>"patches", -replay
=>1)},
6686 git_print_page_nav
($fmt_name,'', $hash,$hash,$hash, $paging_nav);
6687 if (defined $file_name) {
6688 git_print_header_div
('commit', esc_html
($co{'title'}), $base);
6690 git_print_header_div
('summary', $project)
6692 git_print_page_path
($file_name, $ftype, $hash_base)
6693 if (defined $file_name);
6695 $body_subr->(\
@commitlist, 0, 99, $refs, $next_link,
6696 $file_name, $file_hash, $ftype);
6702 git_log_generic
('log', \
&git_log_body
,
6703 $hash, $hash_parent);
6707 $hash ||= $hash_base || "HEAD";
6708 my %co = parse_commit
($hash)
6709 or die_error
(404, "Unknown commit object");
6711 my $parent = $co{'parent'};
6712 my $parents = $co{'parents'}; # listref
6714 # we need to prepare $formats_nav before any parameter munging
6716 if (!defined $parent) {
6718 $formats_nav .= '(initial)';
6719 } elsif (@$parents == 1) {
6720 # single parent commit
6723 $cgi->a({-href
=> href
(action
=>"commit",
6725 esc_html
(substr($parent, 0, 7))) .
6732 $cgi->a({-href
=> href
(action
=>"commit",
6734 esc_html
(substr($_, 0, 7)));
6738 if (gitweb_check_feature
('patches') && @$parents <= 1) {
6739 $formats_nav .= " | " .
6740 $cgi->a({-href
=> href
(action
=>"patch", -replay
=>1)},
6744 if (!defined $parent) {
6748 open my $fd, "-|", git_cmd
(), "diff-tree", '-r', "--no-commit-id",
6750 (@$parents <= 1 ? $parent : '-c'),
6752 or die_error
(500, "Open git-diff-tree failed");
6753 @difftree = map { chomp; $_ } <$fd>;
6754 close $fd or die_error
(404, "Reading git-diff-tree failed");
6756 # non-textual hash id's can be cached
6758 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6761 my $refs = git_get_references
();
6762 my $ref = format_ref_marker
($refs, $co{'id'});
6764 git_header_html
(undef, $expires);
6765 git_print_page_nav
('commit', '',
6766 $hash, $co{'tree'}, $hash,
6769 if (defined $co{'parent'}) {
6770 git_print_header_div
('commitdiff', esc_html
($co{'title'}) . $ref, $hash);
6772 git_print_header_div
('tree', esc_html
($co{'title'}) . $ref, $co{'tree'}, $hash);
6774 print "<div class=\"title_text\">\n" .
6775 "<table class=\"object_header\">\n";
6776 git_print_authorship_rows
(\
%co);
6777 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
6780 "<td class=\"sha1\">" .
6781 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$co{'tree'}, hash_base
=>$hash),
6782 class => "list"}, $co{'tree'}) .
6784 "<td class=\"link\">" .
6785 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$co{'tree'}, hash_base
=>$hash)},
6787 my $snapshot_links = format_snapshot_links
($hash);
6788 if (defined $snapshot_links) {
6789 print " | " . $snapshot_links;
6794 foreach my $par (@$parents) {
6797 "<td class=\"sha1\">" .
6798 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$par),
6799 class => "list"}, $par) .
6801 "<td class=\"link\">" .
6802 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$par)}, "commit") .
6804 $cgi->a({-href
=> href
(action
=>"commitdiff", hash
=>$hash, hash_parent
=>$par)}, "diff") .
6811 print "<div class=\"page_body\">\n";
6812 git_print_log
($co{'comment'});
6815 git_difftree_body
(\
@difftree, $hash, @$parents);
6821 # object is defined by:
6822 # - hash or hash_base alone
6823 # - hash_base and file_name
6826 # - hash or hash_base alone
6827 if ($hash || ($hash_base && !defined $file_name)) {
6828 my $object_id = $hash || $hash_base;
6830 open my $fd, "-|", quote_command
(
6831 git_cmd
(), 'cat-file', '-t', $object_id) . ' 2> /dev/null'
6832 or die_error
(404, "Object does not exist");
6836 or die_error
(404, "Object does not exist");
6838 # - hash_base and file_name
6839 } elsif ($hash_base && defined $file_name) {
6840 $file_name =~ s
,/+$,,;
6842 system(git_cmd
(), "cat-file", '-e', $hash_base) == 0
6843 or die_error
(404, "Base object does not exist");
6845 # here errors should not hapen
6846 open my $fd, "-|", git_cmd
(), "ls-tree", $hash_base, "--", $file_name
6847 or die_error
(500, "Open git-ls-tree failed");
6851 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
6852 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
6853 die_error
(404, "File or directory for given base does not exist");
6858 die_error
(400, "Not enough information to find object");
6861 print $cgi->redirect(-uri
=> href
(action
=>$type, -full
=>1,
6862 hash
=>$hash, hash_base
=>$hash_base,
6863 file_name
=>$file_name),
6864 -status
=> '302 Found');
6868 my $format = shift || 'html';
6875 # preparing $fd and %diffinfo for git_patchset_body
6877 if (defined $hash_base && defined $hash_parent_base) {
6878 if (defined $file_name) {
6880 open $fd, "-|", git_cmd
(), "diff-tree", '-r', @diff_opts,
6881 $hash_parent_base, $hash_base,
6882 "--", (defined $file_parent ? $file_parent : ()), $file_name
6883 or die_error
(500, "Open git-diff-tree failed");
6884 @difftree = map { chomp; $_ } <$fd>;
6886 or die_error
(404, "Reading git-diff-tree failed");
6888 or die_error
(404, "Blob diff not found");
6890 } elsif (defined $hash &&
6891 $hash =~ /[0-9a-fA-F]{40}/) {
6892 # try to find filename from $hash
6894 # read filtered raw output
6895 open $fd, "-|", git_cmd
(), "diff-tree", '-r', @diff_opts,
6896 $hash_parent_base, $hash_base, "--"
6897 or die_error
(500, "Open git-diff-tree failed");
6899 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
6901 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
6902 map { chomp; $_ } <$fd>;
6904 or die_error
(404, "Reading git-diff-tree failed");
6906 or die_error
(404, "Blob diff not found");
6909 die_error
(400, "Missing one of the blob diff parameters");
6912 if (@difftree > 1) {
6913 die_error
(400, "Ambiguous blob diff specification");
6916 %diffinfo = parse_difftree_raw_line
($difftree[0]);
6917 $file_parent ||= $diffinfo{'from_file'} || $file_name;
6918 $file_name ||= $diffinfo{'to_file'};
6920 $hash_parent ||= $diffinfo{'from_id'};
6921 $hash ||= $diffinfo{'to_id'};
6923 # non-textual hash id's can be cached
6924 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
6925 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
6930 open $fd, "-|", git_cmd
(), "diff-tree", '-r', @diff_opts,
6931 '-p', ($format eq 'html' ? "--full-index" : ()),
6932 $hash_parent_base, $hash_base,
6933 "--", (defined $file_parent ? $file_parent : ()), $file_name
6934 or die_error
(500, "Open git-diff-tree failed");
6937 # old/legacy style URI -- not generated anymore since 1.4.3.
6939 die_error
('404 Not Found', "Missing one of the blob diff parameters")
6943 if ($format eq 'html') {
6945 $cgi->a({-href
=> href
(action
=>"blobdiff_plain", -replay
=>1)},
6947 git_header_html
(undef, $expires);
6948 if (defined $hash_base && (my %co = parse_commit
($hash_base))) {
6949 git_print_page_nav
('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
6950 git_print_header_div
('commit', esc_html
($co{'title'}), $hash_base);
6952 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
6953 print "<div class=\"title\">".esc_html
("$hash vs $hash_parent")."</div>\n";
6955 if (defined $file_name) {
6956 git_print_page_path
($file_name, "blob", $hash_base);
6958 print "<div class=\"page_path\"></div>\n";
6961 } elsif ($format eq 'plain') {
6963 -type
=> 'text/plain',
6964 -charset
=> 'utf-8',
6965 -expires
=> $expires,
6966 -content_disposition
=> 'inline; filename="' . "$file_name" . '.patch"');
6968 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
6971 die_error
(400, "Unknown blobdiff format");
6975 if ($format eq 'html') {
6976 print "<div class=\"page_body\">\n";
6978 git_patchset_body
($fd, [ \
%diffinfo ], $hash_base, $hash_parent_base);
6981 print "</div>\n"; # class="page_body"
6985 while (my $line = <$fd>) {
6986 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
6987 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
6991 last if $line =~ m!^\+\+\+!;
6999 sub git_blobdiff_plain
{
7000 git_blobdiff
('plain');
7003 sub git_commitdiff
{
7005 my $format = $params{-format
} || 'html';
7007 my ($patch_max) = gitweb_get_feature
('patches');
7008 if ($format eq 'patch') {
7009 die_error
(403, "Patch view not allowed") unless $patch_max;
7012 $hash ||= $hash_base || "HEAD";
7013 my %co = parse_commit
($hash)
7014 or die_error
(404, "Unknown commit object");
7016 # choose format for commitdiff for merge
7017 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
7018 $hash_parent = '--cc';
7020 # we need to prepare $formats_nav before almost any parameter munging
7022 if ($format eq 'html') {
7024 $cgi->a({-href
=> href
(action
=>"commitdiff_plain", -replay
=>1)},
7026 if ($patch_max && @{$co{'parents'}} <= 1) {
7027 $formats_nav .= " | " .
7028 $cgi->a({-href
=> href
(action
=>"patch", -replay
=>1)},
7032 if (defined $hash_parent &&
7033 $hash_parent ne '-c' && $hash_parent ne '--cc') {
7034 # commitdiff with two commits given
7035 my $hash_parent_short = $hash_parent;
7036 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
7037 $hash_parent_short = substr($hash_parent, 0, 7);
7041 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
7042 if ($co{'parents'}[$i] eq $hash_parent) {
7043 $formats_nav .= ' parent ' . ($i+1);
7047 $formats_nav .= ': ' .
7048 $cgi->a({-href
=> href
(action
=>"commitdiff",
7049 hash
=>$hash_parent)},
7050 esc_html
($hash_parent_short)) .
7052 } elsif (!$co{'parent'}) {
7054 $formats_nav .= ' (initial)';
7055 } elsif (scalar @{$co{'parents'}} == 1) {
7056 # single parent commit
7059 $cgi->a({-href
=> href
(action
=>"commitdiff",
7060 hash
=>$co{'parent'})},
7061 esc_html
(substr($co{'parent'}, 0, 7))) .
7065 if ($hash_parent eq '--cc') {
7066 $formats_nav .= ' | ' .
7067 $cgi->a({-href
=> href
(action
=>"commitdiff",
7068 hash
=>$hash, hash_parent
=>'-c')},
7070 } else { # $hash_parent eq '-c'
7071 $formats_nav .= ' | ' .
7072 $cgi->a({-href
=> href
(action
=>"commitdiff",
7073 hash
=>$hash, hash_parent
=>'--cc')},
7079 $cgi->a({-href
=> href
(action
=>"commitdiff",
7081 esc_html
(substr($_, 0, 7)));
7082 } @{$co{'parents'}} ) .
7087 my $hash_parent_param = $hash_parent;
7088 if (!defined $hash_parent_param) {
7089 # --cc for multiple parents, --root for parentless
7090 $hash_parent_param =
7091 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
7097 if ($format eq 'html') {
7098 open $fd, "-|", git_cmd
(), "diff-tree", '-r', @diff_opts,
7099 "--no-commit-id", "--patch-with-raw", "--full-index",
7100 $hash_parent_param, $hash, "--"
7101 or die_error
(500, "Open git-diff-tree failed");
7103 while (my $line = <$fd>) {
7105 # empty line ends raw part of diff-tree output
7107 push @difftree, scalar parse_difftree_raw_line
($line);
7110 } elsif ($format eq 'plain') {
7111 open $fd, "-|", git_cmd
(), "diff-tree", '-r', @diff_opts,
7112 '-p', $hash_parent_param, $hash, "--"
7113 or die_error
(500, "Open git-diff-tree failed");
7114 } elsif ($format eq 'patch') {
7115 # For commit ranges, we limit the output to the number of
7116 # patches specified in the 'patches' feature.
7117 # For single commits, we limit the output to a single patch,
7118 # diverging from the git-format-patch default.
7119 my @commit_spec = ();
7121 if ($patch_max > 0) {
7122 push @commit_spec, "-$patch_max";
7124 push @commit_spec, '-n', "$hash_parent..$hash";
7126 if ($params{-single
}) {
7127 push @commit_spec, '-1';
7129 if ($patch_max > 0) {
7130 push @commit_spec, "-$patch_max";
7132 push @commit_spec, "-n";
7134 push @commit_spec, '--root', $hash;
7136 open $fd, "-|", git_cmd
(), "format-patch", @diff_opts,
7137 '--encoding=utf8', '--stdout', @commit_spec
7138 or die_error
(500, "Open git-format-patch failed");
7140 die_error
(400, "Unknown commitdiff format");
7143 # non-textual hash id's can be cached
7145 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
7149 # write commit message
7150 if ($format eq 'html') {
7151 my $refs = git_get_references
();
7152 my $ref = format_ref_marker
($refs, $co{'id'});
7154 git_header_html
(undef, $expires);
7155 git_print_page_nav
('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
7156 git_print_header_div
('commit', esc_html
($co{'title'}) . $ref, $hash);
7157 print "<div class=\"title_text\">\n" .
7158 "<table class=\"object_header\">\n";
7159 git_print_authorship_rows
(\
%co);
7162 print "<div class=\"page_body\">\n";
7163 if (@{$co{'comment'}} > 1) {
7164 print "<div class=\"log\">\n";
7165 git_print_log
($co{'comment'}, -final_empty_line
=> 1, -remove_title
=> 1);
7166 print "</div>\n"; # class="log"
7169 } elsif ($format eq 'plain') {
7170 my $refs = git_get_references
("tags");
7171 my $tagname = git_get_rev_name_tags
($hash);
7172 my $filename = basename
($project) . "-$hash.patch";
7175 -type
=> 'text/plain',
7176 -charset
=> 'utf-8',
7177 -expires
=> $expires,
7178 -content_disposition
=> 'inline; filename="' . "$filename" . '"');
7179 my %ad = parse_date
($co{'author_epoch'}, $co{'author_tz'});
7180 print "From: " . to_utf8
($co{'author'}) . "\n";
7181 print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
7182 print "Subject: " . to_utf8
($co{'title'}) . "\n";
7184 print "X-Git-Tag: $tagname\n" if $tagname;
7185 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
7187 foreach my $line (@{$co{'comment'}}) {
7188 print to_utf8
($line) . "\n";
7191 } elsif ($format eq 'patch') {
7192 my $filename = basename
($project) . "-$hash.patch";
7195 -type
=> 'text/plain',
7196 -charset
=> 'utf-8',
7197 -expires
=> $expires,
7198 -content_disposition
=> 'inline; filename="' . "$filename" . '"');
7202 if ($format eq 'html') {
7203 my $use_parents = !defined $hash_parent ||
7204 $hash_parent eq '-c' || $hash_parent eq '--cc';
7205 git_difftree_body
(\
@difftree, $hash,
7206 $use_parents ? @{$co{'parents'}} : $hash_parent);
7209 git_patchset_body
($fd, \
@difftree, $hash,
7210 $use_parents ? @{$co{'parents'}} : $hash_parent);
7212 print "</div>\n"; # class="page_body"
7215 } elsif ($format eq 'plain') {
7219 or print "Reading git-diff-tree failed\n";
7220 } elsif ($format eq 'patch') {
7224 or print "Reading git-format-patch failed\n";
7228 sub git_commitdiff_plain
{
7229 git_commitdiff
(-format
=> 'plain');
7232 # format-patch-style patches
7234 git_commitdiff
(-format
=> 'patch', -single
=> 1);
7238 git_commitdiff
(-format
=> 'patch');
7242 git_log_generic
('history', \
&git_history_body
,
7243 $hash_base, $hash_parent_base,
7248 $searchtype ||= 'commit';
7250 # check if appropriate features are enabled
7251 gitweb_check_feature
('search')
7252 or die_error
(403, "Search is disabled");
7253 if ($searchtype eq 'pickaxe') {
7254 # pickaxe may take all resources of your box and run for several minutes
7255 # with every query - so decide by yourself how public you make this feature
7256 gitweb_check_feature
('pickaxe')
7257 or die_error
(403, "Pickaxe search is disabled");
7259 if ($searchtype eq 'grep') {
7260 # grep search might be potentially CPU-intensive, too
7261 gitweb_check_feature
('grep')
7262 or die_error
(403, "Grep search is disabled");
7265 if (!defined $searchtext) {
7266 die_error
(400, "Text field is empty");
7268 if (!defined $hash) {
7269 $hash = git_get_head_hash
($project);
7271 my %co = parse_commit
($hash);
7273 die_error
(404, "Unknown commit object");
7275 if (!defined $page) {
7281 if ($searchtype eq 'commit' ||
7282 $searchtype eq 'author' ||
7283 $searchtype eq 'committer') {
7284 git_search_message
(%co);
7285 } elsif ($searchtype eq 'pickaxe') {
7286 git_search_changes
(%co);
7287 } elsif ($searchtype eq 'grep') {
7288 git_search_files
(%co);
7294 sub git_search_help
{
7296 git_print_page_nav
('','', $hash,$hash,$hash);
7298 <p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without
7299 regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,
7300 the pattern entered is recognized as the POSIX extended
7301 <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case
7304 <dt><b>commit</b></dt>
7305 <dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
7307 my $have_grep = gitweb_check_feature
('grep');
7310 <dt><b>grep</b></dt>
7311 <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
7312 a different one) are searched for the given pattern. On large trees, this search can take
7313 a while and put some strain on the server, so please use it with some consideration. Note that
7314 due to git-grep peculiarity, currently if regexp mode is turned off, the matches are
7315 case-sensitive.</dd>
7319 <dt><b>author</b></dt>
7320 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>
7321 <dt><b>committer</b></dt>
7322 <dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>
7324 my $have_pickaxe = gitweb_check_feature
('pickaxe');
7325 if ($have_pickaxe) {
7327 <dt><b>pickaxe</b></dt>
7328 <dd>All commits that caused the string to appear or disappear from any file (changes that
7329 added, removed or "modified" the string) will be listed. This search can take a while and
7330 takes a lot of strain on the server, so please use it wisely. Note that since you may be
7331 interested even in changes just changing the case as well, this search is case sensitive.</dd>
7339 git_log_generic
('shortlog', \
&git_shortlog_body
,
7340 $hash, $hash_parent);
7343 ## ......................................................................
7344 ## feeds (RSS, Atom; OPML)
7347 my $format = shift || 'atom';
7348 my $have_blame = gitweb_check_feature
('blame');
7350 # Atom: http://www.atomenabled.org/developers/syndication/
7351 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
7352 if ($format ne 'rss' && $format ne 'atom') {
7353 die_error
(400, "Unknown web feed format");
7356 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
7357 my $head = $hash || 'HEAD';
7358 my @commitlist = parse_commits
($head, 150, 0, $file_name);
7362 my $content_type = "application/$format+xml";
7363 if (defined $cgi->http('HTTP_ACCEPT') &&
7364 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
7365 # browser (feed reader) prefers text/xml
7366 $content_type = 'text/xml';
7368 if (defined($commitlist[0])) {
7369 %latest_commit = %{$commitlist[0]};
7370 my $latest_epoch = $latest_commit{'committer_epoch'};
7371 %latest_date = parse_date
($latest_epoch, $latest_commit{'comitter_tz'});
7372 my $if_modified = $cgi->http('IF_MODIFIED_SINCE');
7373 if (defined $if_modified) {
7375 if (eval { require HTTP
::Date
; 1; }) {
7376 $since = HTTP
::Date
::str2time
($if_modified);
7377 } elsif (eval { require Time
::ParseDate
; 1; }) {
7378 $since = Time
::ParseDate
::parsedate
($if_modified, GMT
=> 1);
7380 if (defined $since && $latest_epoch <= $since) {
7382 -type
=> $content_type,
7383 -charset
=> 'utf-8',
7384 -last_modified
=> $latest_date{'rfc2822'},
7385 -status
=> '304 Not Modified');
7390 -type
=> $content_type,
7391 -charset
=> 'utf-8',
7392 -last_modified
=> $latest_date{'rfc2822'});
7395 -type
=> $content_type,
7396 -charset
=> 'utf-8');
7399 # Optimization: skip generating the body if client asks only
7400 # for Last-Modified date.
7401 return if ($cgi->request_method() eq 'HEAD');
7404 my $title = "$site_name - $project/$action";
7405 my $feed_type = 'log';
7406 if (defined $hash) {
7407 $title .= " - '$hash'";
7408 $feed_type = 'branch log';
7409 if (defined $file_name) {
7410 $title .= " :: $file_name";
7411 $feed_type = 'history';
7413 } elsif (defined $file_name) {
7414 $title .= " - $file_name";
7415 $feed_type = 'history';
7417 $title .= " $feed_type";
7418 my $descr = git_get_project_description
($project);
7419 if (defined $descr) {
7420 $descr = esc_html
($descr);
7422 $descr = "$project " .
7423 ($format eq 'rss' ? 'RSS' : 'Atom') .
7426 my $owner = git_get_project_owner
($project);
7427 $owner = esc_html
($owner);
7431 if (defined $file_name) {
7432 $alt_url = href
(-full
=>1, action
=>"history", hash
=>$hash, file_name
=>$file_name);
7433 } elsif (defined $hash) {
7434 $alt_url = href
(-full
=>1, action
=>"log", hash
=>$hash);
7436 $alt_url = href
(-full
=>1, action
=>"summary");
7438 print qq
!<?xml version
="1.0" encoding
="utf-8"?>\n!;
7439 if ($format eq 'rss') {
7441 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
7444 print "<title>$title</title>\n" .
7445 "<link>$alt_url</link>\n" .
7446 "<description>$descr</description>\n" .
7447 "<language>en</language>\n" .
7448 # project owner is responsible for 'editorial' content
7449 "<managingEditor>$owner</managingEditor>\n";
7450 if (defined $logo || defined $favicon) {
7451 # prefer the logo to the favicon, since RSS
7452 # doesn't allow both
7453 my $img = esc_url
($logo || $favicon);
7455 "<url>$img</url>\n" .
7456 "<title>$title</title>\n" .
7457 "<link>$alt_url</link>\n" .
7461 print "<pubDate>$latest_date{'rfc2822'}</pubDate>\n";
7462 print "<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";
7464 print "<generator>gitweb v.$version/$git_version</generator>\n";
7465 } elsif ($format eq 'atom') {
7467 <feed xmlns="http://www.w3.org/2005/Atom">
7469 print "<title>$title</title>\n" .
7470 "<subtitle>$descr</subtitle>\n" .
7471 '<link rel="alternate" type="text/html" href="' .
7472 $alt_url . '" />' . "\n" .
7473 '<link rel="self" type="' . $content_type . '" href="' .
7474 $cgi->self_url() . '" />' . "\n" .
7475 "<id>" . href
(-full
=>1) . "</id>\n" .
7476 # use project owner for feed author
7477 "<author><name>$owner</name></author>\n";
7478 if (defined $favicon) {
7479 print "<icon>" . esc_url
($favicon) . "</icon>\n";
7481 if (defined $logo) {
7482 # not twice as wide as tall: 72 x 27 pixels
7483 print "<logo>" . esc_url
($logo) . "</logo>\n";
7485 if (! %latest_date) {
7486 # dummy date to keep the feed valid until commits trickle in:
7487 print "<updated>1970-01-01T00:00:00Z</updated>\n";
7489 print "<updated>$latest_date{'iso-8601'}</updated>\n";
7491 print "<generator version='$version/$git_version'>gitweb</generator>\n";
7495 for (my $i = 0; $i <= $#commitlist; $i++) {
7496 my %co = %{$commitlist[$i]};
7497 my $commit = $co{'id'};
7498 # we read 150, we always show 30 and the ones more recent than 48 hours
7499 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
7502 my %cd = parse_date
($co{'author_epoch'}, $co{'author_tz'});
7504 # get list of changed files
7505 open my $fd, "-|", git_cmd
(), "diff-tree", '-r', @diff_opts,
7506 $co{'parent'} || "--root",
7507 $co{'id'}, "--", (defined $file_name ? $file_name : ())
7509 my @difftree = map { chomp; $_ } <$fd>;
7513 # print element (entry, item)
7514 my $co_url = href
(-full
=>1, action
=>"commitdiff", hash
=>$commit);
7515 if ($format eq 'rss') {
7517 "<title>" . esc_html
($co{'title'}) . "</title>\n" .
7518 "<author>" . esc_html
($co{'author'}) . "</author>\n" .
7519 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
7520 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
7521 "<link>$co_url</link>\n" .
7522 "<description>" . esc_html
($co{'title'}) . "</description>\n" .
7523 "<content:encoded>" .
7525 } elsif ($format eq 'atom') {
7527 "<title type=\"html\">" . esc_html
($co{'title'}) . "</title>\n" .
7528 "<updated>$cd{'iso-8601'}</updated>\n" .
7530 " <name>" . esc_html
($co{'author_name'}) . "</name>\n";
7531 if ($co{'author_email'}) {
7532 print " <email>" . esc_html
($co{'author_email'}) . "</email>\n";
7534 print "</author>\n" .
7535 # use committer for contributor
7537 " <name>" . esc_html
($co{'committer_name'}) . "</name>\n";
7538 if ($co{'committer_email'}) {
7539 print " <email>" . esc_html
($co{'committer_email'}) . "</email>\n";
7541 print "</contributor>\n" .
7542 "<published>$cd{'iso-8601'}</published>\n" .
7543 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
7544 "<id>$co_url</id>\n" .
7545 "<content type=\"xhtml\" xml:base=\"" . esc_url
($my_url) . "\">\n" .
7546 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
7548 my $comment = $co{'comment'};
7550 foreach my $line (@$comment) {
7551 $line = esc_html
($line);
7554 print "</pre><ul>\n";
7555 foreach my $difftree_line (@difftree) {
7556 my %difftree = parse_difftree_raw_line
($difftree_line);
7557 next if !$difftree{'from_id'};
7559 my $file = $difftree{'file'} || $difftree{'to_file'};
7563 $cgi->a({-href
=> href
(-full
=>1, action
=>"blobdiff",
7564 hash
=>$difftree{'to_id'}, hash_parent
=>$difftree{'from_id'},
7565 hash_base
=>$co{'id'}, hash_parent_base
=>$co{'parent'},
7566 file_name
=>$file, file_parent
=>$difftree{'from_file'}),
7567 -title
=> "diff"}, 'D');
7569 print $cgi->a({-href
=> href
(-full
=>1, action
=>"blame",
7570 file_name
=>$file, hash_base
=>$commit),
7571 -title
=> "blame"}, 'B');
7573 # if this is not a feed of a file history
7574 if (!defined $file_name || $file_name ne $file) {
7575 print $cgi->a({-href
=> href
(-full
=>1, action
=>"history",
7576 file_name
=>$file, hash
=>$commit),
7577 -title
=> "history"}, 'H');
7579 $file = esc_path
($file);
7583 if ($format eq 'rss') {
7584 print "</ul>]]>\n" .
7585 "</content:encoded>\n" .
7587 } elsif ($format eq 'atom') {
7588 print "</ul>\n</div>\n" .
7595 if ($format eq 'rss') {
7596 print "</channel>\n</rss>\n";
7597 } elsif ($format eq 'atom') {
7611 my @list = git_get_projects_list
();
7613 die_error
(404, "No projects found");
7617 -type
=> 'text/xml',
7618 -charset
=> 'utf-8',
7619 -content_disposition
=> 'inline; filename="opml.xml"');
7622 <?xml version="1.0" encoding="utf-8"?>
7623 <opml version="1.0">
7625 <title>$site_name OPML Export</title>
7628 <outline text="git RSS feeds">
7631 foreach my $pr (@list) {
7633 my $head = git_get_head_hash
($proj{'path'});
7634 if (!defined $head) {
7637 $git_dir = "$projectroot/$proj{'path'}";
7638 my %co = parse_commit
($head);
7643 my $path = esc_html
(chop_str
($proj{'path'}, 25, 5));
7644 my $rss = href
('project' => $proj{'path'}, 'action' => 'rss', -full
=> 1);
7645 my $html = href
('project' => $proj{'path'}, 'action' => 'summary', -full
=> 1);
7646 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";