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;
99 # URI of GIT logo (72x27 size)
100 our $logo = "++GITWEB_LOGO++";
101 # URI of GIT favicon, assumed to be image/png type
102 our $favicon = "++GITWEB_FAVICON++";
103 # URI of gitweb.js (JavaScript code for gitweb)
104 our $javascript = "++GITWEB_JS++";
106 # URI and label (title) of GIT logo link
107 #our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
108 #our $logo_label = "git documentation";
109 our $logo_url = "http://git-scm.com/";
110 our $logo_label = "git homepage";
112 # source of projects list
113 our $projects_list = "++GITWEB_LIST++";
115 # the width (in characters) of the projects list "Description" column
116 our $projects_list_description_width = 25;
118 # group projects by category on the projects list
119 # (enabled if this variable evaluates to true)
120 our $projects_list_group_categories = 0;
122 # default category if none specified
123 # (leave the empty string for no category)
124 our $project_list_default_category = "";
126 # default order of projects list
127 # valid values are none, project, descr, owner, and age
128 our $default_projects_order = "project";
130 # show repository only if this file exists
131 # (only effective if this variable evaluates to true)
132 our $export_ok = "++GITWEB_EXPORT_OK++";
134 # show repository only if this subroutine returns true
135 # when given the path to the project, for example:
136 # sub { return -e "$_[0]/git-daemon-export-ok"; }
137 our $export_auth_hook = undef;
139 # only allow viewing of repositories also shown on the overview page
140 our $strict_export = "++GITWEB_STRICT_EXPORT++";
142 # list of git base URLs used for URL to where fetch project from,
143 # i.e. full URL is "$git_base_url/$project"
144 our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
146 # default blob_plain mimetype and default charset for text/plain blob
147 our $default_blob_plain_mimetype = 'text/plain';
148 our $default_text_plain_charset = undef;
150 # file to use for guessing MIME types before trying /etc/mime.types
151 # (relative to the current git repository)
152 our $mimetypes_file = undef;
154 # assume this charset if line contains non-UTF-8 characters;
155 # it should be valid encoding (see Encoding::Supported(3pm) for list),
156 # for which encoding all byte sequences are valid, for example
157 # 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
158 # could be even 'utf-8' for the old behavior)
159 our $fallback_encoding = 'latin1';
161 # rename detection options for git-diff and git-diff-tree
162 # - default is '-M', with the cost proportional to
163 # (number of removed files) * (number of new files).
164 # - more costly is '-C' (which implies '-M'), with the cost proportional to
165 # (number of changed files + number of removed files) * (number of new files)
166 # - even more costly is '-C', '--find-copies-harder' with cost
167 # (number of files in the original tree) * (number of new files)
168 # - one might want to include '-B' option, e.g. '-B', '-M'
169 our @diff_opts = ('-M'); # taken from git_commit
171 # Disables features that would allow repository owners to inject script into
173 our $prevent_xss = 0;
175 # Path to the highlight executable to use (must be the one from
176 # http://www.andre-simon.de due to assumptions about parameters and output).
177 # Useful if highlight is not installed on your webserver's PATH.
178 # [Default: highlight]
179 our $highlight_bin = "++HIGHLIGHT_BIN++";
181 # information about snapshot formats that gitweb is capable of serving
182 our %known_snapshot_formats = (
184 # 'display' => display name,
185 # 'type' => mime type,
186 # 'suffix' => filename suffix,
187 # 'format' => --format for git-archive,
188 # 'compressor' => [compressor command and arguments]
189 # (array reference, optional)
190 # 'disabled' => boolean (optional)}
193 'display' => 'tar.gz',
194 'type' => 'application/x-gzip',
195 'suffix' => '.tar.gz',
197 'compressor' => ['gzip', '-n']},
200 'display' => 'tar.bz2',
201 'type' => 'application/x-bzip2',
202 'suffix' => '.tar.bz2',
204 'compressor' => ['bzip2']},
207 'display' => 'tar.xz',
208 'type' => 'application/x-xz',
209 'suffix' => '.tar.xz',
211 'compressor' => ['xz'],
216 'type' => 'application/x-zip',
221 # Aliases so we understand old gitweb.snapshot values in repository
223 our %known_snapshot_format_aliases = (
228 # backward compatibility: legacy gitweb config support
229 'x-gzip' => undef, 'gz' => undef,
230 'x-bzip2' => undef, 'bz2' => undef,
231 'x-zip' => undef, '' => undef,
234 # Pixel sizes for icons and avatars. If the default font sizes or lineheights
235 # are changed, it may be appropriate to change these values too via
242 # Used to set the maximum load that we will still respond to gitweb queries.
243 # If server load exceed this value then return "503 server busy" error.
244 # If gitweb cannot determined server load, it is taken to be 0.
245 # Leave it undefined (or set to 'undef') to turn off load checking.
248 # configuration for 'highlight' (http://www.andre-simon.de/)
250 our %highlight_basename = (
253 'SConstruct' => 'py', # SCons equivalent of Makefile
254 'Makefile' => 'make',
257 our %highlight_ext = (
258 # main extensions, defining name of syntax;
259 # see files in /usr/share/highlight/langDefs/ directory
261 qw(py c cpp rb java css php sh pl js tex bib xml awk bat ini spec tcl sql make),
262 # alternate extensions, see /etc/highlight/filetypes.conf
264 map { $_ => 'sh' } qw(bash zsh ksh),
265 map { $_ => 'cpp' } qw(cxx c++ cc),
266 map { $_ => 'php' } qw(php3 php4 php5 phps),
267 map { $_ => 'pl' } qw(perl pm), # perhaps also 'cgi'
268 map { $_ => 'make'} qw(mak mk),
269 map { $_ => 'xml' } qw(xhtml html htm),
272 # You define site-wide feature defaults here; override them with
273 # $GITWEB_CONFIG as necessary.
276 # 'sub' => feature-sub (subroutine),
277 # 'override' => allow-override (boolean),
278 # 'default' => [ default options...] (array reference)}
280 # if feature is overridable (it means that allow-override has true value),
281 # then feature-sub will be called with default options as parameters;
282 # return value of feature-sub indicates if to enable specified feature
284 # if there is no 'sub' key (no feature-sub), then feature cannot be
287 # use gitweb_get_feature(<feature>) to retrieve the <feature> value
288 # (an array) or gitweb_check_feature(<feature>) to check if <feature>
291 # Enable the 'blame' blob view, showing the last commit that modified
292 # each line in the file. This can be very CPU-intensive.
294 # To enable system wide have in $GITWEB_CONFIG
295 # $feature{'blame'}{'default'} = [1];
296 # To have project specific config enable override in $GITWEB_CONFIG
297 # $feature{'blame'}{'override'} = 1;
298 # and in project config gitweb.blame = 0|1;
300 'sub' => sub { feature_bool
('blame', @_) },
304 # Enable the 'snapshot' link, providing a compressed archive of any
305 # tree. This can potentially generate high traffic if you have large
308 # Value is a list of formats defined in %known_snapshot_formats that
310 # To disable system wide have in $GITWEB_CONFIG
311 # $feature{'snapshot'}{'default'} = [];
312 # To have project specific config enable override in $GITWEB_CONFIG
313 # $feature{'snapshot'}{'override'} = 1;
314 # and in project config, a comma-separated list of formats or "none"
315 # to disable. Example: gitweb.snapshot = tbz2,zip;
317 'sub' => \
&feature_snapshot
,
319 'default' => ['tgz']},
321 # Enable text search, which will list the commits which match author,
322 # committer or commit text to a given string. Enabled by default.
323 # Project specific override is not supported.
328 # Enable grep search, which will list the files in currently selected
329 # tree containing the given string. Enabled by default. This can be
330 # potentially CPU-intensive, of course.
332 # To enable system wide have in $GITWEB_CONFIG
333 # $feature{'grep'}{'default'} = [1];
334 # To have project specific config enable override in $GITWEB_CONFIG
335 # $feature{'grep'}{'override'} = 1;
336 # and in project config gitweb.grep = 0|1;
338 'sub' => sub { feature_bool
('grep', @_) },
342 # Enable the pickaxe search, which will list the commits that modified
343 # a given string in a file. This can be practical and quite faster
344 # alternative to 'blame', but still potentially CPU-intensive.
346 # To enable system wide have in $GITWEB_CONFIG
347 # $feature{'pickaxe'}{'default'} = [1];
348 # To have project specific config enable override in $GITWEB_CONFIG
349 # $feature{'pickaxe'}{'override'} = 1;
350 # and in project config gitweb.pickaxe = 0|1;
352 'sub' => sub { feature_bool
('pickaxe', @_) },
356 # Enable showing size of blobs in a 'tree' view, in a separate
357 # column, similar to what 'ls -l' does. This cost a bit of IO.
359 # To disable system wide have in $GITWEB_CONFIG
360 # $feature{'show-sizes'}{'default'} = [0];
361 # To have project specific config enable override in $GITWEB_CONFIG
362 # $feature{'show-sizes'}{'override'} = 1;
363 # and in project config gitweb.showsizes = 0|1;
365 'sub' => sub { feature_bool
('showsizes', @_) },
369 # Make gitweb use an alternative format of the URLs which can be
370 # more readable and natural-looking: project name is embedded
371 # directly in the path and the query string contains other
372 # auxiliary information. All gitweb installations recognize
373 # URL in either format; this configures in which formats gitweb
376 # To enable system wide have in $GITWEB_CONFIG
377 # $feature{'pathinfo'}{'default'} = [1];
378 # Project specific override is not supported.
380 # Note that you will need to change the default location of CSS,
381 # favicon, logo and possibly other files to an absolute URL. Also,
382 # if gitweb.cgi serves as your indexfile, you will need to force
383 # $my_uri to contain the script name in your $GITWEB_CONFIG.
388 # Make gitweb consider projects in project root subdirectories
389 # to be forks of existing projects. Given project $projname.git,
390 # projects matching $projname/*.git will not be shown in the main
391 # projects list, instead a '+' mark will be added to $projname
392 # there and a 'forks' view will be enabled for the project, listing
393 # all the forks. If project list is taken from a file, forks have
394 # to be listed after the main project.
396 # To enable system wide have in $GITWEB_CONFIG
397 # $feature{'forks'}{'default'} = [1];
398 # Project specific override is not supported.
403 # Insert custom links to the action bar of all project pages.
404 # This enables you mainly to link to third-party scripts integrating
405 # into gitweb; e.g. git-browser for graphical history representation
406 # or custom web-based repository administration interface.
408 # The 'default' value consists of a list of triplets in the form
409 # (label, link, position) where position is the label after which
410 # to insert the link and link is a format string where %n expands
411 # to the project name, %f to the project path within the filesystem,
412 # %h to the current hash (h gitweb parameter) and %b to the current
413 # hash base (hb gitweb parameter); %% expands to %.
415 # To enable system wide have in $GITWEB_CONFIG e.g.
416 # $feature{'actions'}{'default'} = [('graphiclog',
417 # '/git-browser/by-commit.html?r=%n', 'summary')];
418 # Project specific override is not supported.
423 # Allow gitweb scan project content tags of project repository,
424 # and display the popular Web 2.0-ish "tag cloud" near the projects
425 # list. Note that this is something COMPLETELY different from the
428 # gitweb by itself can show existing tags, but it does not handle
429 # tagging itself; you need to do it externally, outside gitweb.
430 # The format is described in git_get_project_ctags() subroutine.
431 # You may want to install the HTML::TagCloud Perl module to get
432 # a pretty tag cloud instead of just a list of tags.
434 # To enable system wide have in $GITWEB_CONFIG
435 # $feature{'ctags'}{'default'} = [1];
436 # Project specific override is not supported.
438 # In the future whether ctags editing is enabled might depend
439 # on the value, but using 1 should always mean no editing of ctags.
444 # The maximum number of patches in a patchset generated in patch
445 # view. Set this to 0 or undef to disable patch view, or to a
446 # negative number to remove any limit.
448 # To disable system wide have in $GITWEB_CONFIG
449 # $feature{'patches'}{'default'} = [0];
450 # To have project specific config enable override in $GITWEB_CONFIG
451 # $feature{'patches'}{'override'} = 1;
452 # and in project config gitweb.patches = 0|n;
453 # where n is the maximum number of patches allowed in a patchset.
455 'sub' => \
&feature_patches
,
459 # Avatar support. When this feature is enabled, views such as
460 # shortlog or commit will display an avatar associated with
461 # the email of the committer(s) and/or author(s).
463 # Currently available providers are gravatar and picon.
464 # If an unknown provider is specified, the feature is disabled.
466 # Gravatar depends on Digest::MD5.
467 # Picon currently relies on the indiana.edu database.
469 # To enable system wide have in $GITWEB_CONFIG
470 # $feature{'avatar'}{'default'} = ['<provider>'];
471 # where <provider> is either gravatar or picon.
472 # To have project specific config enable override in $GITWEB_CONFIG
473 # $feature{'avatar'}{'override'} = 1;
474 # and in project config gitweb.avatar = <provider>;
476 'sub' => \
&feature_avatar
,
480 # Enable displaying how much time and how many git commands
481 # it took to generate and display page. Disabled by default.
482 # Project specific override is not supported.
487 # Enable turning some links into links to actions which require
488 # JavaScript to run (like 'blame_incremental'). Not enabled by
489 # default. Project specific override is currently not supported.
490 'javascript-actions' => {
494 # Enable and configure ability to change common timezone for dates
495 # in gitweb output via JavaScript. Enabled by default.
496 # Project specific override is not supported.
497 'javascript-timezone' => {
500 'local', # default timezone: 'utc', 'local', or '(-|+)HHMM' format,
501 # or undef to turn off this feature
502 'gitweb_tz', # name of cookie where to store selected timezone
503 'datetime', # CSS class used to mark up dates for manipulation
506 # Syntax highlighting support. This is based on Daniel Svensson's
507 # and Sham Chukoury's work in gitweb-xmms2.git.
508 # It requires the 'highlight' program present in $PATH,
509 # and therefore is disabled by default.
511 # To enable system wide have in $GITWEB_CONFIG
512 # $feature{'highlight'}{'default'} = [1];
515 'sub' => sub { feature_bool
('highlight', @_) },
519 # Enable displaying of remote heads in the heads list
521 # To enable system wide have in $GITWEB_CONFIG
522 # $feature{'remote_heads'}{'default'} = [1];
523 # To have project specific config enable override in $GITWEB_CONFIG
524 # $feature{'remote_heads'}{'override'} = 1;
525 # and in project config gitweb.remote_heads = 0|1;
527 'sub' => sub { feature_bool
('remote_heads', @_) },
532 sub gitweb_get_feature
{
534 return unless exists $feature{$name};
535 my ($sub, $override, @defaults) = (
536 $feature{$name}{'sub'},
537 $feature{$name}{'override'},
538 @
{$feature{$name}{'default'}});
539 # project specific override is possible only if we have project
540 our $git_dir; # global variable, declared later
541 if (!$override || !defined $git_dir) {
545 warn "feature $name is not overridable";
548 return $sub->(@defaults);
551 # A wrapper to check if a given feature is enabled.
552 # With this, you can say
554 # my $bool_feat = gitweb_check_feature('bool_feat');
555 # gitweb_check_feature('bool_feat') or somecode;
559 # my ($bool_feat) = gitweb_get_feature('bool_feat');
560 # (gitweb_get_feature('bool_feat'))[0] or somecode;
562 sub gitweb_check_feature
{
563 return (gitweb_get_feature
(@_))[0];
569 my ($val) = git_get_project_config
($key, '--bool');
573 } elsif ($val eq 'true') {
575 } elsif ($val eq 'false') {
580 sub feature_snapshot
{
583 my ($val) = git_get_project_config
('snapshot');
586 @fmts = ($val eq 'none' ?
() : split /\s*[,\s]\s*/, $val);
592 sub feature_patches
{
593 my @val = (git_get_project_config
('patches', '--int'));
603 my @val = (git_get_project_config
('avatar'));
605 return @val ?
@val : @_;
608 # checking HEAD file with -e is fragile if the repository was
609 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
611 sub check_head_link
{
613 my $headfile = "$dir/HEAD";
614 return ((-e
$headfile) ||
615 (-l
$headfile && readlink($headfile) =~ /^refs\/heads\
//));
618 sub check_export_ok
{
620 return (check_head_link
($dir) &&
621 (!$export_ok || -e
"$dir/$export_ok") &&
622 (!$export_auth_hook || $export_auth_hook->($dir)));
625 # process alternate names for backward compatibility
626 # filter out unsupported (unknown) snapshot formats
627 sub filter_snapshot_fmts
{
631 exists $known_snapshot_format_aliases{$_} ?
632 $known_snapshot_format_aliases{$_} : $_} @fmts;
634 exists $known_snapshot_formats{$_} &&
635 !$known_snapshot_formats{$_}{'disabled'}} @fmts;
638 # If it is set to code reference, it is code that it is to be run once per
639 # request, allowing updating configurations that change with each request,
640 # while running other code in config file only once.
642 # Otherwise, if it is false then gitweb would process config file only once;
643 # if it is true then gitweb config would be run for each request.
644 our $per_request_config = 1;
646 # read and parse gitweb config file given by its parameter.
647 # returns true on success, false on recoverable error, allowing
648 # to chain this subroutine, using first file that exists.
649 # dies on errors during parsing config file, as it is unrecoverable.
650 sub read_config_file
{
651 my $filename = shift;
652 return unless defined $filename;
653 # die if there are errors parsing config file
662 our ($GITWEB_CONFIG, $GITWEB_CONFIG_SYSTEM);
663 sub evaluate_gitweb_config
{
664 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
665 our $GITWEB_CONFIG_SYSTEM = $ENV{'GITWEB_CONFIG_SYSTEM'} || "++GITWEB_CONFIG_SYSTEM++";
667 # use first config file that exists
668 read_config_file
($GITWEB_CONFIG) or
669 read_config_file
($GITWEB_CONFIG_SYSTEM);
672 # Get loadavg of system, to compare against $maxload.
673 # Currently it requires '/proc/loadavg' present to get loadavg;
674 # if it is not present it returns 0, which means no load checking.
676 if( -e
'/proc/loadavg' ){
677 open my $fd, '<', '/proc/loadavg'
679 my @load = split(/\s+/, scalar <$fd>);
682 # The first three columns measure CPU and IO utilization of the last one,
683 # five, and 10 minute periods. The fourth column shows the number of
684 # currently running processes and the total number of processes in the m/n
685 # format. The last column displays the last process ID used.
686 return $load[0] || 0;
688 # additional checks for load average should go here for things that don't export
694 # version of the core git binary
696 sub evaluate_git_version
{
697 our $git_version = qx("$GIT" --version
) =~ m/git version (.*)$/ ?
$1 : "unknown";
698 $number_of_git_cmds++;
702 if (defined $maxload && get_loadavg
() > $maxload) {
703 die_error
(503, "The load average on the server is too high");
707 # ======================================================================
708 # input validation and dispatch
710 # input parameters can be collected from a variety of sources (presently, CGI
711 # and PATH_INFO), so we define an %input_params hash that collects them all
712 # together during validation: this allows subsequent uses (e.g. href()) to be
713 # agnostic of the parameter origin
715 our %input_params = ();
717 # input parameters are stored with the long parameter name as key. This will
718 # also be used in the href subroutine to convert parameters to their CGI
719 # equivalent, and since the href() usage is the most frequent one, we store
720 # the name -> CGI key mapping here, instead of the reverse.
722 # XXX: Warning: If you touch this, check the search form for updating,
725 our @cgi_param_mapping = (
733 hash_parent_base
=> "hpb",
738 snapshot_format
=> "sf",
739 extra_options
=> "opt",
740 search_use_regexp
=> "sr",
742 # this must be last entry (for manipulation from JavaScript)
745 our %cgi_param_mapping = @cgi_param_mapping;
747 # we will also need to know the possible actions, for validation
749 "blame" => \
&git_blame
,
750 "blame_incremental" => \
&git_blame_incremental
,
751 "blame_data" => \
&git_blame_data
,
752 "blobdiff" => \
&git_blobdiff
,
753 "blobdiff_plain" => \
&git_blobdiff_plain
,
754 "blob" => \
&git_blob
,
755 "blob_plain" => \
&git_blob_plain
,
756 "commitdiff" => \
&git_commitdiff
,
757 "commitdiff_plain" => \
&git_commitdiff_plain
,
758 "commit" => \
&git_commit
,
759 "forks" => \
&git_forks
,
760 "heads" => \
&git_heads
,
761 "history" => \
&git_history
,
763 "patch" => \
&git_patch
,
764 "patches" => \
&git_patches
,
765 "remotes" => \
&git_remotes
,
767 "atom" => \
&git_atom
,
768 "search" => \
&git_search
,
769 "search_help" => \
&git_search_help
,
770 "shortlog" => \
&git_shortlog
,
771 "summary" => \
&git_summary
,
773 "tags" => \
&git_tags
,
774 "tree" => \
&git_tree
,
775 "snapshot" => \
&git_snapshot
,
776 "object" => \
&git_object
,
777 # those below don't need $project
778 "opml" => \
&git_opml
,
779 "project_list" => \
&git_project_list
,
780 "project_index" => \
&git_project_index
,
783 # finally, we have the hash of allowed extra_options for the commands that
785 our %allowed_options = (
786 "--no-merges" => [ qw(rss atom log shortlog history) ],
789 # fill %input_params with the CGI parameters. All values except for 'opt'
790 # should be single values, but opt can be an array. We should probably
791 # build an array of parameters that can be multi-valued, but since for the time
792 # being it's only this one, we just single it out
793 sub evaluate_query_params
{
796 while (my ($name, $symbol) = each %cgi_param_mapping) {
797 if ($symbol eq 'opt') {
798 $input_params{$name} = [ $cgi->param($symbol) ];
800 $input_params{$name} = $cgi->param($symbol);
805 # now read PATH_INFO and update the parameter list for missing parameters
806 sub evaluate_path_info
{
807 return if defined $input_params{'project'};
808 return if !$path_info;
809 $path_info =~ s
,^/+,,;
810 return if !$path_info;
812 # find which part of PATH_INFO is project
813 my $project = $path_info;
815 while ($project && !check_head_link
("$projectroot/$project")) {
816 $project =~ s
,/*[^/]*$,,;
818 return unless $project;
819 $input_params{'project'} = $project;
821 # do not change any parameters if an action is given using the query string
822 return if $input_params{'action'};
823 $path_info =~ s
,^\Q
$project\E
/*,,;
825 # next, check if we have an action
826 my $action = $path_info;
828 if (exists $actions{$action}) {
829 $path_info =~ s
,^$action/*,,;
830 $input_params{'action'} = $action;
833 # list of actions that want hash_base instead of hash, but can have no
834 # pathname (f) parameter
840 # we want to catch, among others
841 # [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name]
842 my ($parentrefname, $parentpathname, $refname, $pathname) =
843 ($path_info =~ /^(?:(.+?)(?::(.+))?\.\.)?([^:]+?)?(?::(.+))?$/);
845 # first, analyze the 'current' part
846 if (defined $pathname) {
847 # we got "branch:filename" or "branch:dir/"
848 # we could use git_get_type(branch:pathname), but:
849 # - it needs $git_dir
850 # - it does a git() call
851 # - the convention of terminating directories with a slash
852 # makes it superfluous
853 # - embedding the action in the PATH_INFO would make it even
855 $pathname =~ s
,^/+,,;
856 if (!$pathname || substr($pathname, -1) eq "/") {
857 $input_params{'action'} ||= "tree";
860 # the default action depends on whether we had parent info
862 if ($parentrefname) {
863 $input_params{'action'} ||= "blobdiff_plain";
865 $input_params{'action'} ||= "blob_plain";
868 $input_params{'hash_base'} ||= $refname;
869 $input_params{'file_name'} ||= $pathname;
870 } elsif (defined $refname) {
871 # we got "branch". In this case we have to choose if we have to
872 # set hash or hash_base.
874 # Most of the actions without a pathname only want hash to be
875 # set, except for the ones specified in @wants_base that want
876 # hash_base instead. It should also be noted that hand-crafted
877 # links having 'history' as an action and no pathname or hash
878 # set will fail, but that happens regardless of PATH_INFO.
879 if (defined $parentrefname) {
880 # if there is parent let the default be 'shortlog' action
881 # (for http://git.example.com/repo.git/A..B links); if there
882 # is no parent, dispatch will detect type of object and set
883 # action appropriately if required (if action is not set)
884 $input_params{'action'} ||= "shortlog";
886 if ($input_params{'action'} &&
887 grep { $_ eq $input_params{'action'} } @wants_base) {
888 $input_params{'hash_base'} ||= $refname;
890 $input_params{'hash'} ||= $refname;
894 # next, handle the 'parent' part, if present
895 if (defined $parentrefname) {
896 # a missing pathspec defaults to the 'current' filename, allowing e.g.
897 # someproject/blobdiff/oldrev..newrev:/filename
898 if ($parentpathname) {
899 $parentpathname =~ s
,^/+,,;
900 $parentpathname =~ s
,/$,,;
901 $input_params{'file_parent'} ||= $parentpathname;
903 $input_params{'file_parent'} ||= $input_params{'file_name'};
905 # we assume that hash_parent_base is wanted if a path was specified,
906 # or if the action wants hash_base instead of hash
907 if (defined $input_params{'file_parent'} ||
908 grep { $_ eq $input_params{'action'} } @wants_base) {
909 $input_params{'hash_parent_base'} ||= $parentrefname;
911 $input_params{'hash_parent'} ||= $parentrefname;
915 # for the snapshot action, we allow URLs in the form
916 # $project/snapshot/$hash.ext
917 # where .ext determines the snapshot and gets removed from the
918 # passed $refname to provide the $hash.
920 # To be able to tell that $refname includes the format extension, we
921 # require the following two conditions to be satisfied:
922 # - the hash input parameter MUST have been set from the $refname part
923 # of the URL (i.e. they must be equal)
924 # - the snapshot format MUST NOT have been defined already (e.g. from
926 # It's also useless to try any matching unless $refname has a dot,
927 # so we check for that too
928 if (defined $input_params{'action'} &&
929 $input_params{'action'} eq 'snapshot' &&
930 defined $refname && index($refname, '.') != -1 &&
931 $refname eq $input_params{'hash'} &&
932 !defined $input_params{'snapshot_format'}) {
933 # We loop over the known snapshot formats, checking for
934 # extensions. Allowed extensions are both the defined suffix
935 # (which includes the initial dot already) and the snapshot
936 # format key itself, with a prepended dot
937 while (my ($fmt, $opt) = each %known_snapshot_formats) {
939 unless ($hash =~ s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) {
943 # a valid suffix was found, so set the snapshot format
944 # and reset the hash parameter
945 $input_params{'snapshot_format'} = $fmt;
946 $input_params{'hash'} = $hash;
947 # we also set the format suffix to the one requested
948 # in the URL: this way a request for e.g. .tgz returns
949 # a .tgz instead of a .tar.gz
950 $known_snapshot_formats{$fmt}{'suffix'} = $sfx;
956 our ($action, $project, $file_name, $file_parent, $hash, $hash_parent, $hash_base,
957 $hash_parent_base, @extra_options, $page, $searchtype, $search_use_regexp,
958 $searchtext, $search_regexp);
959 sub evaluate_and_validate_params
{
960 our $action = $input_params{'action'};
961 if (defined $action) {
962 if (!validate_action
($action)) {
963 die_error
(400, "Invalid action parameter");
967 # parameters which are pathnames
968 our $project = $input_params{'project'};
969 if (defined $project) {
970 if (!validate_project
($project)) {
972 die_error
(404, "No such project");
976 our $file_name = $input_params{'file_name'};
977 if (defined $file_name) {
978 if (!validate_pathname
($file_name)) {
979 die_error
(400, "Invalid file parameter");
983 our $file_parent = $input_params{'file_parent'};
984 if (defined $file_parent) {
985 if (!validate_pathname
($file_parent)) {
986 die_error
(400, "Invalid file parent parameter");
990 # parameters which are refnames
991 our $hash = $input_params{'hash'};
993 if (!validate_refname
($hash)) {
994 die_error
(400, "Invalid hash parameter");
998 our $hash_parent = $input_params{'hash_parent'};
999 if (defined $hash_parent) {
1000 if (!validate_refname
($hash_parent)) {
1001 die_error
(400, "Invalid hash parent parameter");
1005 our $hash_base = $input_params{'hash_base'};
1006 if (defined $hash_base) {
1007 if (!validate_refname
($hash_base)) {
1008 die_error
(400, "Invalid hash base parameter");
1012 our @extra_options = @
{$input_params{'extra_options'}};
1013 # @extra_options is always defined, since it can only be (currently) set from
1014 # CGI, and $cgi->param() returns the empty array in array context if the param
1016 foreach my $opt (@extra_options) {
1017 if (not exists $allowed_options{$opt}) {
1018 die_error
(400, "Invalid option parameter");
1020 if (not grep(/^$action$/, @
{$allowed_options{$opt}})) {
1021 die_error
(400, "Invalid option parameter for this action");
1025 our $hash_parent_base = $input_params{'hash_parent_base'};
1026 if (defined $hash_parent_base) {
1027 if (!validate_refname
($hash_parent_base)) {
1028 die_error
(400, "Invalid hash parent base parameter");
1033 our $page = $input_params{'page'};
1034 if (defined $page) {
1035 if ($page =~ m/[^0-9]/) {
1036 die_error
(400, "Invalid page parameter");
1040 our $searchtype = $input_params{'searchtype'};
1041 if (defined $searchtype) {
1042 if ($searchtype =~ m/[^a-z]/) {
1043 die_error
(400, "Invalid searchtype parameter");
1047 our $search_use_regexp = $input_params{'search_use_regexp'};
1049 our $searchtext = $input_params{'searchtext'};
1051 if (defined $searchtext) {
1052 if (length($searchtext) < 2) {
1053 die_error
(403, "At least two characters are required for search parameter");
1055 $search_regexp = $search_use_regexp ?
$searchtext : quotemeta $searchtext;
1059 # path to the current git repository
1061 sub evaluate_git_dir
{
1062 our $git_dir = "$projectroot/$project" if $project;
1065 our (@snapshot_fmts, $git_avatar);
1066 sub configure_gitweb_features
{
1067 # list of supported snapshot formats
1068 our @snapshot_fmts = gitweb_get_feature
('snapshot');
1069 @snapshot_fmts = filter_snapshot_fmts
(@snapshot_fmts);
1071 # check that the avatar feature is set to a known provider name,
1072 # and for each provider check if the dependencies are satisfied.
1073 # if the provider name is invalid or the dependencies are not met,
1074 # reset $git_avatar to the empty string.
1075 our ($git_avatar) = gitweb_get_feature
('avatar');
1076 if ($git_avatar eq 'gravatar') {
1077 $git_avatar = '' unless (eval { require Digest
::MD5
; 1; });
1078 } elsif ($git_avatar eq 'picon') {
1085 # custom error handler: 'die <message>' is Internal Server Error
1086 sub handle_errors_html
{
1087 my $msg = shift; # it is already HTML escaped
1089 # to avoid infinite loop where error occurs in die_error,
1090 # change handler to default handler, disabling handle_errors_html
1091 set_message
("Error occured when inside die_error:\n$msg");
1093 # you cannot jump out of die_error when called as error handler;
1094 # the subroutine set via CGI::Carp::set_message is called _after_
1095 # HTTP headers are already written, so it cannot write them itself
1096 die_error
(undef, undef, $msg, -error_handler
=> 1, -no_http_header
=> 1);
1098 set_message
(\
&handle_errors_html
);
1102 if (!defined $action) {
1103 if (defined $hash) {
1104 $action = git_get_type
($hash);
1105 } elsif (defined $hash_base && defined $file_name) {
1106 $action = git_get_type
("$hash_base:$file_name");
1107 } elsif (defined $project) {
1108 $action = 'summary';
1110 $action = 'project_list';
1113 if (!defined($actions{$action})) {
1114 die_error
(400, "Unknown action");
1116 if ($action !~ m/^(?:opml|project_list|project_index)$/ &&
1118 die_error
(400, "Project needed");
1120 $actions{$action}->();
1124 our $t0 = [ gettimeofday
() ]
1126 our $number_of_git_cmds = 0;
1129 our $first_request = 1;
1134 if ($first_request) {
1135 evaluate_gitweb_config
();
1136 evaluate_git_version
();
1138 if ($per_request_config) {
1139 if (ref($per_request_config) eq 'CODE') {
1140 $per_request_config->();
1141 } elsif (!$first_request) {
1142 evaluate_gitweb_config
();
1147 # $projectroot and $projects_list might be set in gitweb config file
1148 $projects_list ||= $projectroot;
1150 evaluate_query_params
();
1151 evaluate_path_info
();
1152 evaluate_and_validate_params
();
1155 configure_gitweb_features
();
1160 our $is_last_request = sub { 1 };
1161 our ($pre_dispatch_hook, $post_dispatch_hook, $pre_listen_hook);
1164 sub configure_as_fcgi
{
1166 our $CGI = 'CGI::Fast';
1168 my $request_number = 0;
1169 # let each child service 100 requests
1170 our $is_last_request = sub { ++$request_number > 100 };
1173 my $script_name = $ENV{'SCRIPT_NAME'} || $ENV{'SCRIPT_FILENAME'} || __FILE__
;
1175 if $script_name =~ /\.fcgi$/;
1177 return unless (@ARGV);
1179 require Getopt
::Long
;
1180 Getopt
::Long
::GetOptions
(
1181 'fastcgi|fcgi|f' => \
&configure_as_fcgi
,
1182 'nproc|n=i' => sub {
1183 my ($arg, $val) = @_;
1184 return unless eval { require FCGI
::ProcManager
; 1; };
1185 my $proc_manager = FCGI
::ProcManager
->new({
1186 n_processes
=> $val,
1188 our $pre_listen_hook = sub { $proc_manager->pm_manage() };
1189 our $pre_dispatch_hook = sub { $proc_manager->pm_pre_dispatch() };
1190 our $post_dispatch_hook = sub { $proc_manager->pm_post_dispatch() };
1199 $pre_listen_hook->()
1200 if $pre_listen_hook;
1203 while ($cgi = $CGI->new()) {
1204 $pre_dispatch_hook->()
1205 if $pre_dispatch_hook;
1209 $post_dispatch_hook->()
1210 if $post_dispatch_hook;
1213 last REQUEST
if ($is_last_request->());
1222 if (defined caller) {
1223 # wrapped in a subroutine processing requests,
1224 # e.g. mod_perl with ModPerl::Registry, or PSGI with Plack::App::WrapCGI
1227 # pure CGI script, serving single request
1231 ## ======================================================================
1234 # possible values of extra options
1235 # -full => 0|1 - use absolute/full URL ($my_uri/$my_url as base)
1236 # -replay => 1 - start from a current view (replay with modifications)
1237 # -path_info => 0|1 - don't use/use path_info URL (if possible)
1238 # -anchor => ANCHOR - add #ANCHOR to end of URL, implies -replay if used alone
1241 # default is to use -absolute url() i.e. $my_uri
1242 my $href = $params{-full
} ?
$my_url : $my_uri;
1244 # implicit -replay, must be first of implicit params
1245 $params{-replay
} = 1 if (keys %params == 1 && $params{-anchor
});
1247 $params{'project'} = $project unless exists $params{'project'};
1249 if ($params{-replay
}) {
1250 while (my ($name, $symbol) = each %cgi_param_mapping) {
1251 if (!exists $params{$name}) {
1252 $params{$name} = $input_params{$name};
1257 my $use_pathinfo = gitweb_check_feature
('pathinfo');
1258 if (defined $params{'project'} &&
1259 (exists $params{-path_info
} ?
$params{-path_info
} : $use_pathinfo)) {
1260 # try to put as many parameters as possible in PATH_INFO:
1263 # - hash_parent or hash_parent_base:/file_parent
1264 # - hash or hash_base:/filename
1265 # - the snapshot_format as an appropriate suffix
1267 # When the script is the root DirectoryIndex for the domain,
1268 # $href here would be something like http://gitweb.example.com/
1269 # Thus, we strip any trailing / from $href, to spare us double
1270 # slashes in the final URL
1273 # Then add the project name, if present
1274 $href .= "/".esc_path_info
($params{'project'});
1275 delete $params{'project'};
1277 # since we destructively absorb parameters, we keep this
1278 # boolean that remembers if we're handling a snapshot
1279 my $is_snapshot = $params{'action'} eq 'snapshot';
1281 # Summary just uses the project path URL, any other action is
1283 if (defined $params{'action'}) {
1284 $href .= "/".esc_path_info
($params{'action'})
1285 unless $params{'action'} eq 'summary';
1286 delete $params{'action'};
1289 # Next, we put hash_parent_base:/file_parent..hash_base:/file_name,
1290 # stripping nonexistent or useless pieces
1291 $href .= "/" if ($params{'hash_base'} || $params{'hash_parent_base'}
1292 || $params{'hash_parent'} || $params{'hash'});
1293 if (defined $params{'hash_base'}) {
1294 if (defined $params{'hash_parent_base'}) {
1295 $href .= esc_path_info
($params{'hash_parent_base'});
1296 # skip the file_parent if it's the same as the file_name
1297 if (defined $params{'file_parent'}) {
1298 if (defined $params{'file_name'} && $params{'file_parent'} eq $params{'file_name'}) {
1299 delete $params{'file_parent'};
1300 } elsif ($params{'file_parent'} !~ /\.\./) {
1301 $href .= ":/".esc_path_info
($params{'file_parent'});
1302 delete $params{'file_parent'};
1306 delete $params{'hash_parent'};
1307 delete $params{'hash_parent_base'};
1308 } elsif (defined $params{'hash_parent'}) {
1309 $href .= esc_path_info
($params{'hash_parent'}). "..";
1310 delete $params{'hash_parent'};
1313 $href .= esc_path_info
($params{'hash_base'});
1314 if (defined $params{'file_name'} && $params{'file_name'} !~ /\.\./) {
1315 $href .= ":/".esc_path_info
($params{'file_name'});
1316 delete $params{'file_name'};
1318 delete $params{'hash'};
1319 delete $params{'hash_base'};
1320 } elsif (defined $params{'hash'}) {
1321 $href .= esc_path_info
($params{'hash'});
1322 delete $params{'hash'};
1325 # If the action was a snapshot, we can absorb the
1326 # snapshot_format parameter too
1328 my $fmt = $params{'snapshot_format'};
1329 # snapshot_format should always be defined when href()
1330 # is called, but just in case some code forgets, we
1331 # fall back to the default
1332 $fmt ||= $snapshot_fmts[0];
1333 $href .= $known_snapshot_formats{$fmt}{'suffix'};
1334 delete $params{'snapshot_format'};
1338 # now encode the parameters explicitly
1340 for (my $i = 0; $i < @cgi_param_mapping; $i += 2) {
1341 my ($name, $symbol) = ($cgi_param_mapping[$i], $cgi_param_mapping[$i+1]);
1342 if (defined $params{$name}) {
1343 if (ref($params{$name}) eq "ARRAY") {
1344 foreach my $par (@
{$params{$name}}) {
1345 push @result, $symbol . "=" . esc_param
($par);
1348 push @result, $symbol . "=" . esc_param
($params{$name});
1352 $href .= "?" . join(';', @result) if scalar @result;
1354 # final transformation: trailing spaces must be escaped (URI-encoded)
1355 $href =~ s/(\s+)$/CGI::escape($1)/e;
1357 if ($params{-anchor
}) {
1358 $href .= "#".esc_param
($params{-anchor
});
1365 ## ======================================================================
1366 ## validation, quoting/unquoting and escaping
1368 sub validate_action
{
1369 my $input = shift || return undef;
1370 return undef unless exists $actions{$input};
1374 sub validate_project
{
1375 my $input = shift || return undef;
1376 if (!validate_pathname
($input) ||
1377 !(-d
"$projectroot/$input") ||
1378 !check_export_ok
("$projectroot/$input") ||
1379 ($strict_export && !project_in_list
($input))) {
1386 sub validate_pathname
{
1387 my $input = shift || return undef;
1389 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
1390 # at the beginning, at the end, and between slashes.
1391 # also this catches doubled slashes
1392 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
1395 # no null characters
1396 if ($input =~ m!\0!) {
1402 sub validate_refname
{
1403 my $input = shift || return undef;
1405 # textual hashes are O.K.
1406 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
1409 # it must be correct pathname
1410 $input = validate_pathname
($input)
1412 # restrictions on ref name according to git-check-ref-format
1413 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
1419 # decode sequences of octets in utf8 into Perl's internal form,
1420 # which is utf-8 with utf8 flag set if needed. gitweb writes out
1421 # in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
1424 return undef unless defined $str;
1425 if (utf8
::valid
($str)) {
1429 return decode
($fallback_encoding, $str, Encode
::FB_DEFAULT
);
1433 # quote unsafe chars, but keep the slash, even when it's not
1434 # correct, but quoted slashes look too horrible in bookmarks
1437 return undef unless defined $str;
1438 $str =~ s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI
::escape
($1)/eg
;
1443 # the quoting rules for path_info fragment are slightly different
1446 return undef unless defined $str;
1448 # path_info doesn't treat '+' as space (specially), but '?' must be escaped
1449 $str =~ s/([^A-Za-z0-9\-_.~();\/;:@&= +]+)/CGI
::escape
($1)/eg
;
1454 # quote unsafe chars in whole URL, so some characters cannot be quoted
1457 return undef unless defined $str;
1458 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&= ]+)/CGI
::escape
($1)/eg
;
1463 # quote unsafe characters in HTML attributes
1466 # for XHTML conformance escaping '"' to '"' is not enough
1467 return esc_html
(@_);
1470 # replace invalid utf8 character with SUBSTITUTION sequence
1475 return undef unless defined $str;
1477 $str = to_utf8
($str);
1478 $str = $cgi->escapeHTML($str);
1479 if ($opts{'-nbsp'}) {
1480 $str =~ s/ / /g;
1482 $str =~ s
|([[:cntrl
:]])|(($1 ne "\t") ? quot_cec
($1) : $1)|eg
;
1486 # quote control characters and escape filename to HTML
1491 return undef unless defined $str;
1493 $str = to_utf8
($str);
1494 $str = $cgi->escapeHTML($str);
1495 if ($opts{'-nbsp'}) {
1496 $str =~ s/ / /g;
1498 $str =~ s
|([[:cntrl
:]])|quot_cec
($1)|eg
;
1502 # Make control characters "printable", using character escape codes (CEC)
1506 my %es = ( # character escape codes, aka escape sequences
1507 "\t" => '\t', # tab (HT)
1508 "\n" => '\n', # line feed (LF)
1509 "\r" => '\r', # carrige return (CR)
1510 "\f" => '\f', # form feed (FF)
1511 "\b" => '\b', # backspace (BS)
1512 "\a" => '\a', # alarm (bell) (BEL)
1513 "\e" => '\e', # escape (ESC)
1514 "\013" => '\v', # vertical tab (VT)
1515 "\000" => '\0', # nul character (NUL)
1517 my $chr = ( (exists $es{$cntrl})
1519 : sprintf('\%2x', ord($cntrl)) );
1520 if ($opts{-nohtml
}) {
1523 return "<span class=\"cntrl\">$chr</span>";
1527 # Alternatively use unicode control pictures codepoints,
1528 # Unicode "printable representation" (PR)
1533 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
1534 if ($opts{-nohtml
}) {
1537 return "<span class=\"cntrl\">$chr</span>";
1541 # git may return quoted and escaped filenames
1547 my %es = ( # character escape codes, aka escape sequences
1548 't' => "\t", # tab (HT, TAB)
1549 'n' => "\n", # newline (NL)
1550 'r' => "\r", # return (CR)
1551 'f' => "\f", # form feed (FF)
1552 'b' => "\b", # backspace (BS)
1553 'a' => "\a", # alarm (bell) (BEL)
1554 'e' => "\e", # escape (ESC)
1555 'v' => "\013", # vertical tab (VT)
1558 if ($seq =~ m/^[0-7]{1,3}$/) {
1559 # octal char sequence
1560 return chr(oct($seq));
1561 } elsif (exists $es{$seq}) {
1562 # C escape sequence, aka character escape code
1565 # quoted ordinary character
1569 if ($str =~ m/^"(.*)"$/) {
1572 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
1577 # escape tabs (convert tabs to spaces)
1581 while ((my $pos = index($line, "\t")) != -1) {
1582 if (my $count = (8 - ($pos % 8))) {
1583 my $spaces = ' ' x
$count;
1584 $line =~ s/\t/$spaces/;
1591 sub project_in_list
{
1592 my $project = shift;
1593 my @list = git_get_projects_list
();
1594 return @list && scalar(grep { $_->{'path'} eq $project } @list);
1597 ## ----------------------------------------------------------------------
1598 ## HTML aware string manipulation
1600 # Try to chop given string on a word boundary between position
1601 # $len and $len+$add_len. If there is no word boundary there,
1602 # chop at $len+$add_len. Do not chop if chopped part plus ellipsis
1603 # (marking chopped part) would be longer than given string.
1607 my $add_len = shift || 10;
1608 my $where = shift || 'right'; # 'left' | 'center' | 'right'
1610 # Make sure perl knows it is utf8 encoded so we don't
1611 # cut in the middle of a utf8 multibyte char.
1612 $str = to_utf8
($str);
1614 # allow only $len chars, but don't cut a word if it would fit in $add_len
1615 # if it doesn't fit, cut it if it's still longer than the dots we would add
1616 # remove chopped character entities entirely
1618 # when chopping in the middle, distribute $len into left and right part
1619 # return early if chopping wouldn't make string shorter
1620 if ($where eq 'center') {
1621 return $str if ($len + 5 >= length($str)); # filler is length 5
1624 return $str if ($len + 4 >= length($str)); # filler is length 4
1627 # regexps: ending and beginning with word part up to $add_len
1628 my $endre = qr/.{$len}\w{0,$add_len}/;
1629 my $begre = qr/\w{0,$add_len}.{$len}/;
1631 if ($where eq 'left') {
1632 $str =~ m/^(.*?)($begre)$/;
1633 my ($lead, $body) = ($1, $2);
1634 if (length($lead) > 4) {
1637 return "$lead$body";
1639 } elsif ($where eq 'center') {
1640 $str =~ m/^($endre)(.*)$/;
1641 my ($left, $str) = ($1, $2);
1642 $str =~ m/^(.*?)($begre)$/;
1643 my ($mid, $right) = ($1, $2);
1644 if (length($mid) > 5) {
1647 return "$left$mid$right";
1650 $str =~ m/^($endre)(.*)$/;
1653 if (length($tail) > 4) {
1656 return "$body$tail";
1660 # takes the same arguments as chop_str, but also wraps a <span> around the
1661 # result with a title attribute if it does get chopped. Additionally, the
1662 # string is HTML-escaped.
1663 sub chop_and_escape_str
{
1666 my $chopped = chop_str
(@_);
1667 if ($chopped eq $str) {
1668 return esc_html
($chopped);
1670 $str =~ s/[[:cntrl:]]/?/g;
1671 return $cgi->span({-title
=>$str}, esc_html
($chopped));
1675 ## ----------------------------------------------------------------------
1676 ## functions returning short strings
1678 # CSS class for given age value (in seconds)
1682 if (!defined $age) {
1684 } elsif ($age < 60*60*2) {
1686 } elsif ($age < 60*60*24*2) {
1693 # convert age in seconds to "nn units ago" string
1698 if ($age > 60*60*24*365*2) {
1699 $age_str = (int $age/60/60/24/365);
1700 $age_str .= " years ago";
1701 } elsif ($age > 60*60*24*(365/12)*2) {
1702 $age_str = int $age/60/60/24/(365/12);
1703 $age_str .= " months ago";
1704 } elsif ($age > 60*60*24*7*2) {
1705 $age_str = int $age/60/60/24/7;
1706 $age_str .= " weeks ago";
1707 } elsif ($age > 60*60*24*2) {
1708 $age_str = int $age/60/60/24;
1709 $age_str .= " days ago";
1710 } elsif ($age > 60*60*2) {
1711 $age_str = int $age/60/60;
1712 $age_str .= " hours ago";
1713 } elsif ($age > 60*2) {
1714 $age_str = int $age/60;
1715 $age_str .= " min ago";
1716 } elsif ($age > 2) {
1717 $age_str = int $age;
1718 $age_str .= " sec ago";
1720 $age_str .= " right now";
1726 S_IFINVALID
=> 0030000,
1727 S_IFGITLINK
=> 0160000,
1730 # submodule/subproject, a commit object reference
1734 return (($mode & S_IFMT
) == S_IFGITLINK
)
1737 # convert file mode in octal to symbolic file mode string
1739 my $mode = oct shift;
1741 if (S_ISGITLINK
($mode)) {
1742 return 'm---------';
1743 } elsif (S_ISDIR
($mode & S_IFMT
)) {
1744 return 'drwxr-xr-x';
1745 } elsif (S_ISLNK
($mode)) {
1746 return 'lrwxrwxrwx';
1747 } elsif (S_ISREG
($mode)) {
1748 # git cares only about the executable bit
1749 if ($mode & S_IXUSR
) {
1750 return '-rwxr-xr-x';
1752 return '-rw-r--r--';
1755 return '----------';
1759 # convert file mode in octal to file type string
1763 if ($mode !~ m/^[0-7]+$/) {
1769 if (S_ISGITLINK
($mode)) {
1771 } elsif (S_ISDIR
($mode & S_IFMT
)) {
1773 } elsif (S_ISLNK
($mode)) {
1775 } elsif (S_ISREG
($mode)) {
1782 # convert file mode in octal to file type description string
1783 sub file_type_long
{
1786 if ($mode !~ m/^[0-7]+$/) {
1792 if (S_ISGITLINK
($mode)) {
1794 } elsif (S_ISDIR
($mode & S_IFMT
)) {
1796 } elsif (S_ISLNK
($mode)) {
1798 } elsif (S_ISREG
($mode)) {
1799 if ($mode & S_IXUSR
) {
1800 return "executable";
1810 ## ----------------------------------------------------------------------
1811 ## functions returning short HTML fragments, or transforming HTML fragments
1812 ## which don't belong to other sections
1814 # format line of commit message.
1815 sub format_log_line_html
{
1818 $line = esc_html
($line, -nbsp
=>1);
1819 $line =~ s
{\b([0-9a
-fA
-F
]{8,40})\b}{
1820 $cgi->a({-href
=> href
(action
=>"object", hash
=>$1),
1821 -class => "text"}, $1);
1827 # format marker of refs pointing to given object
1829 # the destination action is chosen based on object type and current context:
1830 # - for annotated tags, we choose the tag view unless it's the current view
1831 # already, in which case we go to shortlog view
1832 # - for other refs, we keep the current view if we're in history, shortlog or
1833 # log view, and select shortlog otherwise
1834 sub format_ref_marker
{
1835 my ($refs, $id) = @_;
1838 if (defined $refs->{$id}) {
1839 foreach my $ref (@
{$refs->{$id}}) {
1840 # this code exploits the fact that non-lightweight tags are the
1841 # only indirect objects, and that they are the only objects for which
1842 # we want to use tag instead of shortlog as action
1843 my ($type, $name) = qw();
1844 my $indirect = ($ref =~ s/\^\{\}$//);
1845 # e.g. tags/v2.6.11 or heads/next
1846 if ($ref =~ m!^(.*?)s?/(.*)$!) {
1855 $class .= " indirect" if $indirect;
1857 my $dest_action = "shortlog";
1860 $dest_action = "tag" unless $action eq "tag";
1861 } elsif ($action =~ /^(history|(short)?log)$/) {
1862 $dest_action = $action;
1866 $dest .= "refs/" unless $ref =~ m
!^refs
/!;
1869 my $link = $cgi->a({
1871 action
=>$dest_action,
1875 $markers .= " <span class=\"".esc_attr
($class)."\" title=\"".esc_attr
($ref)."\">" .
1881 return ' <span class="refs">'. $markers . '</span>';
1887 # format, perhaps shortened and with markers, title line
1888 sub format_subject_html
{
1889 my ($long, $short, $href, $extra) = @_;
1890 $extra = '' unless defined($extra);
1892 if (length($short) < length($long)) {
1893 $long =~ s/[[:cntrl:]]/?/g;
1894 return $cgi->a({-href
=> $href, -class => "list subject",
1895 -title
=> to_utf8
($long)},
1896 esc_html
($short)) . $extra;
1898 return $cgi->a({-href
=> $href, -class => "list subject"},
1899 esc_html
($long)) . $extra;
1903 # Rather than recomputing the url for an email multiple times, we cache it
1904 # after the first hit. This gives a visible benefit in views where the avatar
1905 # for the same email is used repeatedly (e.g. shortlog).
1906 # The cache is shared by all avatar engines (currently gravatar only), which
1907 # are free to use it as preferred. Since only one avatar engine is used for any
1908 # given page, there's no risk for cache conflicts.
1909 our %avatar_cache = ();
1911 # Compute the picon url for a given email, by using the picon search service over at
1912 # http://www.cs.indiana.edu/picons/search.html
1914 my $email = lc shift;
1915 if (!$avatar_cache{$email}) {
1916 my ($user, $domain) = split('@', $email);
1917 $avatar_cache{$email} =
1918 "http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/" .
1920 "users+domains+unknown/up/single";
1922 return $avatar_cache{$email};
1925 # Compute the gravatar url for a given email, if it's not in the cache already.
1926 # Gravatar stores only the part of the URL before the size, since that's the
1927 # one computationally more expensive. This also allows reuse of the cache for
1928 # different sizes (for this particular engine).
1930 my $email = lc shift;
1932 $avatar_cache{$email} ||=
1933 "http://www.gravatar.com/avatar/" .
1934 Digest
::MD5
::md5_hex
($email) . "?s=";
1935 return $avatar_cache{$email} . $size;
1938 # Insert an avatar for the given $email at the given $size if the feature
1940 sub git_get_avatar
{
1941 my ($email, %opts) = @_;
1942 my $pre_white = ($opts{-pad_before
} ?
" " : "");
1943 my $post_white = ($opts{-pad_after
} ?
" " : "");
1944 $opts{-size
} ||= 'default';
1945 my $size = $avatar_size{$opts{-size
}} || $avatar_size{'default'};
1947 if ($git_avatar eq 'gravatar') {
1948 $url = gravatar_url
($email, $size);
1949 } elsif ($git_avatar eq 'picon') {
1950 $url = picon_url
($email);
1952 # Other providers can be added by extending the if chain, defining $url
1953 # as needed. If no variant puts something in $url, we assume avatars
1954 # are completely disabled/unavailable.
1957 "<img width=\"$size\" " .
1958 "class=\"avatar\" " .
1959 "src=\"".esc_url
($url)."\" " .
1967 sub format_search_author
{
1968 my ($author, $searchtype, $displaytext) = @_;
1969 my $have_search = gitweb_check_feature
('search');
1973 if ($searchtype eq 'author') {
1974 $performed = "authored";
1975 } elsif ($searchtype eq 'committer') {
1976 $performed = "committed";
1979 return $cgi->a({-href
=> href
(action
=>"search", hash
=>$hash,
1980 searchtext
=>$author,
1981 searchtype
=>$searchtype), class=>"list",
1982 title
=>"Search for commits $performed by $author"},
1986 return $displaytext;
1990 # format the author name of the given commit with the given tag
1991 # the author name is chopped and escaped according to the other
1992 # optional parameters (see chop_str).
1993 sub format_author_html
{
1996 my $author = chop_and_escape_str
($co->{'author_name'}, @_);
1997 return "<$tag class=\"author\">" .
1998 format_search_author
($co->{'author_name'}, "author",
1999 git_get_avatar
($co->{'author_email'}, -pad_after
=> 1) .
2004 # format git diff header line, i.e. "diff --(git|combined|cc) ..."
2005 sub format_git_diff_header_line
{
2007 my $diffinfo = shift;
2008 my ($from, $to) = @_;
2010 if ($diffinfo->{'nparents'}) {
2012 $line =~ s!^(diff (.*?) )"?.*$!$1!;
2013 if ($to->{'href'}) {
2014 $line .= $cgi->a({-href
=> $to->{'href'}, -class => "path"},
2015 esc_path
($to->{'file'}));
2016 } else { # file was deleted (no href)
2017 $line .= esc_path
($to->{'file'});
2021 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
2022 if ($from->{'href'}) {
2023 $line .= $cgi->a({-href
=> $from->{'href'}, -class => "path"},
2024 'a/' . esc_path
($from->{'file'}));
2025 } else { # file was added (no href)
2026 $line .= 'a/' . esc_path
($from->{'file'});
2029 if ($to->{'href'}) {
2030 $line .= $cgi->a({-href
=> $to->{'href'}, -class => "path"},
2031 'b/' . esc_path
($to->{'file'}));
2032 } else { # file was deleted
2033 $line .= 'b/' . esc_path
($to->{'file'});
2037 return "<div class=\"diff header\">$line</div>\n";
2040 # format extended diff header line, before patch itself
2041 sub format_extended_diff_header_line
{
2043 my $diffinfo = shift;
2044 my ($from, $to) = @_;
2047 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
2048 $line .= $cgi->a({-href
=>$from->{'href'}, -class=>"path"},
2049 esc_path
($from->{'file'}));
2051 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
2052 $line .= $cgi->a({-href
=>$to->{'href'}, -class=>"path"},
2053 esc_path
($to->{'file'}));
2055 # match single <mode>
2056 if ($line =~ m/\s(\d{6})$/) {
2057 $line .= '<span class="info"> (' .
2058 file_type_long
($1) .
2062 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
2063 # can match only for combined diff
2065 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2066 if ($from->{'href'}[$i]) {
2067 $line .= $cgi->a({-href
=>$from->{'href'}[$i],
2069 substr($diffinfo->{'from_id'}[$i],0,7));
2074 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
2077 if ($to->{'href'}) {
2078 $line .= $cgi->a({-href
=>$to->{'href'}, -class=>"hash"},
2079 substr($diffinfo->{'to_id'},0,7));
2084 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
2085 # can match only for ordinary diff
2086 my ($from_link, $to_link);
2087 if ($from->{'href'}) {
2088 $from_link = $cgi->a({-href
=>$from->{'href'}, -class=>"hash"},
2089 substr($diffinfo->{'from_id'},0,7));
2091 $from_link = '0' x
7;
2093 if ($to->{'href'}) {
2094 $to_link = $cgi->a({-href
=>$to->{'href'}, -class=>"hash"},
2095 substr($diffinfo->{'to_id'},0,7));
2099 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
2100 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
2103 return $line . "<br/>\n";
2106 # format from-file/to-file diff header
2107 sub format_diff_from_to_header
{
2108 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
2113 #assert($line =~ m/^---/) if DEBUG;
2114 # no extra formatting for "^--- /dev/null"
2115 if (! $diffinfo->{'nparents'}) {
2116 # ordinary (single parent) diff
2117 if ($line =~ m!^--- "?a/!) {
2118 if ($from->{'href'}) {
2120 $cgi->a({-href
=>$from->{'href'}, -class=>"path"},
2121 esc_path
($from->{'file'}));
2124 esc_path
($from->{'file'});
2127 $result .= qq!<div
class="diff from_file">$line</div
>\n!;
2130 # combined diff (merge commit)
2131 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2132 if ($from->{'href'}[$i]) {
2134 $cgi->a({-href
=>href
(action
=>"blobdiff",
2135 hash_parent
=>$diffinfo->{'from_id'}[$i],
2136 hash_parent_base
=>$parents[$i],
2137 file_parent
=>$from->{'file'}[$i],
2138 hash
=>$diffinfo->{'to_id'},
2140 file_name
=>$to->{'file'}),
2142 -title
=>"diff" . ($i+1)},
2145 $cgi->a({-href
=>$from->{'href'}[$i], -class=>"path"},
2146 esc_path
($from->{'file'}[$i]));
2148 $line = '--- /dev/null';
2150 $result .= qq!<div
class="diff from_file">$line</div
>\n!;
2155 #assert($line =~ m/^\+\+\+/) if DEBUG;
2156 # no extra formatting for "^+++ /dev/null"
2157 if ($line =~ m!^\+\+\+ "?b/!) {
2158 if ($to->{'href'}) {
2160 $cgi->a({-href
=>$to->{'href'}, -class=>"path"},
2161 esc_path
($to->{'file'}));
2164 esc_path
($to->{'file'});
2167 $result .= qq!<div
class="diff to_file">$line</div
>\n!;
2172 # create note for patch simplified by combined diff
2173 sub format_diff_cc_simplified
{
2174 my ($diffinfo, @parents) = @_;
2177 $result .= "<div class=\"diff header\">" .
2179 if (!is_deleted
($diffinfo)) {
2180 $result .= $cgi->a({-href
=> href
(action
=>"blob",
2182 hash
=>$diffinfo->{'to_id'},
2183 file_name
=>$diffinfo->{'to_file'}),
2185 esc_path
($diffinfo->{'to_file'}));
2187 $result .= esc_path
($diffinfo->{'to_file'});
2189 $result .= "</div>\n" . # class="diff header"
2190 "<div class=\"diff nodifferences\">" .
2192 "</div>\n"; # class="diff nodifferences"
2197 # format patch (diff) line (not to be used for diff headers)
2198 sub format_diff_line
{
2200 my ($from, $to) = @_;
2201 my $diff_class = "";
2205 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
2207 my $prefix = substr($line, 0, scalar @
{$from->{'href'}});
2208 if ($line =~ m/^\@{3}/) {
2209 $diff_class = " chunk_header";
2210 } elsif ($line =~ m/^\\/) {
2211 $diff_class = " incomplete";
2212 } elsif ($prefix =~ tr/+/+/) {
2213 $diff_class = " add";
2214 } elsif ($prefix =~ tr/-/-/) {
2215 $diff_class = " rem";
2218 # assume ordinary diff
2219 my $char = substr($line, 0, 1);
2221 $diff_class = " add";
2222 } elsif ($char eq '-') {
2223 $diff_class = " rem";
2224 } elsif ($char eq '@') {
2225 $diff_class = " chunk_header";
2226 } elsif ($char eq "\\") {
2227 $diff_class = " incomplete";
2230 $line = untabify
($line);
2231 if ($from && $to && $line =~ m/^\@{2} /) {
2232 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
2233 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
2235 $from_lines = 0 unless defined $from_lines;
2236 $to_lines = 0 unless defined $to_lines;
2238 if ($from->{'href'}) {
2239 $from_text = $cgi->a({-href
=>"$from->{'href'}#l$from_start",
2240 -class=>"list"}, $from_text);
2242 if ($to->{'href'}) {
2243 $to_text = $cgi->a({-href
=>"$to->{'href'}#l$to_start",
2244 -class=>"list"}, $to_text);
2246 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
2247 "<span class=\"section\">" . esc_html
($section, -nbsp
=>1) . "</span>";
2248 return "<div class=\"diff$diff_class\">$line</div>\n";
2249 } elsif ($from && $to && $line =~ m/^\@{3}/) {
2250 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
2251 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
2253 @from_text = split(' ', $ranges);
2254 for (my $i = 0; $i < @from_text; ++$i) {
2255 ($from_start[$i], $from_nlines[$i]) =
2256 (split(',', substr($from_text[$i], 1)), 0);
2259 $to_text = pop @from_text;
2260 $to_start = pop @from_start;
2261 $to_nlines = pop @from_nlines;
2263 $line = "<span class=\"chunk_info\">$prefix ";
2264 for (my $i = 0; $i < @from_text; ++$i) {
2265 if ($from->{'href'}[$i]) {
2266 $line .= $cgi->a({-href
=>"$from->{'href'}[$i]#l$from_start[$i]",
2267 -class=>"list"}, $from_text[$i]);
2269 $line .= $from_text[$i];
2273 if ($to->{'href'}) {
2274 $line .= $cgi->a({-href
=>"$to->{'href'}#l$to_start",
2275 -class=>"list"}, $to_text);
2279 $line .= " $prefix</span>" .
2280 "<span class=\"section\">" . esc_html
($section, -nbsp
=>1) . "</span>";
2281 return "<div class=\"diff$diff_class\">$line</div>\n";
2283 return "<div class=\"diff$diff_class\">" . esc_html
($line, -nbsp
=>1) . "</div>\n";
2286 # Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
2287 # linked. Pass the hash of the tree/commit to snapshot.
2288 sub format_snapshot_links
{
2290 my $num_fmts = @snapshot_fmts;
2291 if ($num_fmts > 1) {
2292 # A parenthesized list of links bearing format names.
2293 # e.g. "snapshot (_tar.gz_ _zip_)"
2294 return "snapshot (" . join(' ', map
2301 }, $known_snapshot_formats{$_}{'display'})
2302 , @snapshot_fmts) . ")";
2303 } elsif ($num_fmts == 1) {
2304 # A single "snapshot" link whose tooltip bears the format name.
2306 my ($fmt) = @snapshot_fmts;
2312 snapshot_format
=>$fmt
2314 -title
=> "in format: $known_snapshot_formats{$fmt}{'display'}"
2316 } else { # $num_fmts == 0
2321 ## ......................................................................
2322 ## functions returning values to be passed, perhaps after some
2323 ## transformation, to other functions; e.g. returning arguments to href()
2325 # returns hash to be passed to href to generate gitweb URL
2326 # in -title key it returns description of link
2328 my $format = shift || 'Atom';
2329 my %res = (action
=> lc($format));
2331 # feed links are possible only for project views
2332 return unless (defined $project);
2333 # some views should link to OPML, or to generic project feed,
2334 # or don't have specific feed yet (so they should use generic)
2335 return if ($action =~ /^(?:tags|heads|forks|tag|search)$/x);
2338 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
2339 # from tag links; this also makes possible to detect branch links
2340 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
2341 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
2344 # find log type for feed description (title)
2346 if (defined $file_name) {
2347 $type = "history of $file_name";
2348 $type .= "/" if ($action eq 'tree');
2349 $type .= " on '$branch'" if (defined $branch);
2351 $type = "log of $branch" if (defined $branch);
2354 $res{-title
} = $type;
2355 $res{'hash'} = (defined $branch ?
"refs/heads/$branch" : undef);
2356 $res{'file_name'} = $file_name;
2361 ## ----------------------------------------------------------------------
2362 ## git utility subroutines, invoking git commands
2364 # returns path to the core git executable and the --git-dir parameter as list
2366 $number_of_git_cmds++;
2367 return $GIT, '--git-dir='.$git_dir;
2370 # quote the given arguments for passing them to the shell
2371 # quote_command("command", "arg 1", "arg with ' and ! characters")
2372 # => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
2373 # Try to avoid using this function wherever possible.
2376 map { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ );
2379 # get HEAD ref of given project as hash
2380 sub git_get_head_hash
{
2381 return git_get_full_hash
(shift, 'HEAD');
2384 sub git_get_full_hash
{
2385 return git_get_hash
(@_);
2388 sub git_get_short_hash
{
2389 return git_get_hash
(@_, '--short=7');
2393 my ($project, $hash, @options) = @_;
2394 my $o_git_dir = $git_dir;
2396 $git_dir = "$projectroot/$project";
2397 if (open my $fd, '-|', git_cmd
(), 'rev-parse',
2398 '--verify', '-q', @options, $hash) {
2400 chomp $retval if defined $retval;
2403 if (defined $o_git_dir) {
2404 $git_dir = $o_git_dir;
2409 # get type of given object
2413 open my $fd, "-|", git_cmd
(), "cat-file", '-t', $hash or return;
2415 close $fd or return;
2420 # repository configuration
2421 our $config_file = '';
2424 # store multiple values for single key as anonymous array reference
2425 # single values stored directly in the hash, not as [ <value> ]
2426 sub hash_set_multi
{
2427 my ($hash, $key, $value) = @_;
2429 if (!exists $hash->{$key}) {
2430 $hash->{$key} = $value;
2431 } elsif (!ref $hash->{$key}) {
2432 $hash->{$key} = [ $hash->{$key}, $value ];
2434 push @
{$hash->{$key}}, $value;
2438 # return hash of git project configuration
2439 # optionally limited to some section, e.g. 'gitweb'
2440 sub git_parse_project_config
{
2441 my $section_regexp = shift;
2446 open my $fh, "-|", git_cmd
(), "config", '-z', '-l',
2449 while (my $keyval = <$fh>) {
2451 my ($key, $value) = split(/\n/, $keyval, 2);
2453 hash_set_multi
(\
%config, $key, $value)
2454 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
2461 # convert config value to boolean: 'true' or 'false'
2462 # no value, number > 0, 'true' and 'yes' values are true
2463 # rest of values are treated as false (never as error)
2464 sub config_to_bool
{
2467 return 1 if !defined $val; # section.key
2469 # strip leading and trailing whitespace
2473 return (($val =~ /^\d+$/ && $val) || # section.key = 1
2474 ($val =~ /^(?:true|yes)$/i)); # section.key = true
2477 # convert config value to simple decimal number
2478 # an optional value suffix of 'k', 'm', or 'g' will cause the value
2479 # to be multiplied by 1024, 1048576, or 1073741824
2483 # strip leading and trailing whitespace
2487 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
2489 # unknown unit is treated as 1
2490 return $num * ($unit eq 'g' ?
1073741824 :
2491 $unit eq 'm' ?
1048576 :
2492 $unit eq 'k' ?
1024 : 1);
2497 # convert config value to array reference, if needed
2498 sub config_to_multi
{
2501 return ref($val) ?
$val : (defined($val) ?
[ $val ] : []);
2504 sub git_get_project_config
{
2505 my ($key, $type) = @_;
2507 return unless defined $git_dir;
2510 return unless ($key);
2511 $key =~ s/^gitweb\.//;
2512 return if ($key =~ m/\W/);
2515 if (defined $type) {
2518 unless ($type eq 'bool' || $type eq 'int');
2522 if (!defined $config_file ||
2523 $config_file ne "$git_dir/config") {
2524 %config = git_parse_project_config
('gitweb');
2525 $config_file = "$git_dir/config";
2528 # check if config variable (key) exists
2529 return unless exists $config{"gitweb.$key"};
2532 if (!defined $type) {
2533 return $config{"gitweb.$key"};
2534 } elsif ($type eq 'bool') {
2535 # backward compatibility: 'git config --bool' returns true/false
2536 return config_to_bool
($config{"gitweb.$key"}) ?
'true' : 'false';
2537 } elsif ($type eq 'int') {
2538 return config_to_int
($config{"gitweb.$key"});
2540 return $config{"gitweb.$key"};
2543 # get hash of given path at given ref
2544 sub git_get_hash_by_path
{
2546 my $path = shift || return undef;
2551 open my $fd, "-|", git_cmd
(), "ls-tree", $base, "--", $path
2552 or die_error
(500, "Open git-ls-tree failed");
2554 close $fd or return undef;
2556 if (!defined $line) {
2557 # there is no tree or hash given by $path at $base
2561 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2562 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
2563 if (defined $type && $type ne $2) {
2564 # type doesn't match
2570 # get path of entry with given hash at given tree-ish (ref)
2571 # used to get 'from' filename for combined diff (merge commit) for renames
2572 sub git_get_path_by_hash
{
2573 my $base = shift || return;
2574 my $hash = shift || return;
2578 open my $fd, "-|", git_cmd
(), "ls-tree", '-r', '-t', '-z', $base
2580 while (my $line = <$fd>) {
2583 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
2584 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
2585 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
2594 ## ......................................................................
2595 ## git utility functions, directly accessing git repository
2597 # get the value of config variable either from file named as the variable
2598 # itself in the repository ($GIT_DIR/$name file), or from gitweb.$name
2599 # configuration variable in the repository config file.
2600 sub git_get_file_or_project_config
{
2601 my ($path, $name) = @_;
2603 $git_dir = "$projectroot/$path";
2604 open my $fd, '<', "$git_dir/$name"
2605 or return git_get_project_config
($name);
2608 if (defined $conf) {
2614 sub git_get_project_description
{
2616 return git_get_file_or_project_config
($path, 'description');
2619 sub git_get_project_category
{
2621 return git_get_file_or_project_config
($path, 'category');
2625 # supported formats:
2626 # * $GIT_DIR/ctags/<tagname> file (in 'ctags' subdirectory)
2627 # - if its contents is a number, use it as tag weight,
2628 # - otherwise add a tag with weight 1
2629 # * $GIT_DIR/ctags file, each line is a tag (with weight 1)
2630 # the same value multiple times increases tag weight
2631 # * `gitweb.ctag' multi-valued repo config variable
2632 sub git_get_project_ctags
{
2633 my $project = shift;
2636 $git_dir = "$projectroot/$project";
2637 if (opendir my $dh, "$git_dir/ctags") {
2638 my @files = grep { -f
$_ } map { "$git_dir/ctags/$_" } readdir($dh);
2639 foreach my $tagfile (@files) {
2640 open my $ct, '<', $tagfile
2646 (my $ctag = $tagfile) =~ s
#.*/##;
2647 if ($val =~ /\d+/) {
2648 $ctags->{$ctag} = $val;
2650 $ctags->{$ctag} = 1;
2655 } elsif (open my $fh, '<', "$git_dir/ctags") {
2656 while (my $line = <$fh>) {
2658 $ctags->{$line}++ if $line;
2663 my $taglist = config_to_multi
(git_get_project_config
('ctag'));
2664 foreach my $tag (@
$taglist) {
2672 # return hash, where keys are content tags ('ctags'),
2673 # and values are sum of weights of given tag in every project
2674 sub git_gather_all_ctags
{
2675 my $projects = shift;
2678 foreach my $p (@
$projects) {
2679 foreach my $ct (keys %{$p->{'ctags'}}) {
2680 $ctags->{$ct} += $p->{'ctags'}->{$ct};
2687 sub git_populate_project_tagcloud
{
2690 # First, merge different-cased tags; tags vote on casing
2692 foreach (keys %$ctags) {
2693 $ctags_lc{lc $_}->{count
} += $ctags->{$_};
2694 if (not $ctags_lc{lc $_}->{topcount
}
2695 or $ctags_lc{lc $_}->{topcount
} < $ctags->{$_}) {
2696 $ctags_lc{lc $_}->{topcount
} = $ctags->{$_};
2697 $ctags_lc{lc $_}->{topname
} = $_;
2702 my $matched = $cgi->param('by_tag');
2703 if (eval { require HTML
::TagCloud
; 1; }) {
2704 $cloud = HTML
::TagCloud
->new;
2705 foreach my $ctag (sort keys %ctags_lc) {
2706 # Pad the title with spaces so that the cloud looks
2708 my $title = esc_html
($ctags_lc{$ctag}->{topname
});
2709 $title =~ s/ / /g;
2710 $title =~ s/^/ /g;
2711 $title =~ s/$/ /g;
2712 if (defined $matched && $matched eq $ctag) {
2713 $title = qq(<span
class="match">$title</span
>);
2715 $cloud->add($title, href
(project
=>undef, ctag
=>$ctag),
2716 $ctags_lc{$ctag}->{count
});
2720 foreach my $ctag (keys %ctags_lc) {
2721 my $title = esc_html
($ctags_lc{$ctag}->{topname
}, -nbsp
=>1);
2722 if (defined $matched && $matched eq $ctag) {
2723 $title = qq(<span
class="match">$title</span
>);
2725 $cloud->{$ctag}{count
} = $ctags_lc{$ctag}->{count
};
2726 $cloud->{$ctag}{ctag
} =
2727 $cgi->a({-href
=>href
(project
=>undef, ctag
=>$ctag)}, $title);
2733 sub git_show_project_tagcloud
{
2734 my ($cloud, $count) = @_;
2735 if (ref $cloud eq 'HTML::TagCloud') {
2736 return $cloud->html_and_css($count);
2738 my @tags = sort { $cloud->{$a}->{'count'} <=> $cloud->{$b}->{'count'} } keys %$cloud;
2740 '<div id="htmltagcloud"'.($project ?
'' : ' align="center"').'>' .
2742 $cloud->{$_}->{'ctag'}
2743 } splice(@tags, 0, $count)) .
2748 sub git_get_project_url_list
{
2751 $git_dir = "$projectroot/$path";
2752 open my $fd, '<', "$git_dir/cloneurl"
2753 or return wantarray ?
2754 @
{ config_to_multi
(git_get_project_config
('url')) } :
2755 config_to_multi
(git_get_project_config
('url'));
2756 my @git_project_url_list = map { chomp; $_ } <$fd>;
2759 return wantarray ?
@git_project_url_list : \
@git_project_url_list;
2762 sub git_get_projects_list
{
2763 my $filter = shift || '';
2766 $filter =~ s/\.git$//;
2768 if (-d
$projects_list) {
2769 # search in directory
2770 my $dir = $projects_list;
2771 # remove the trailing "/"
2773 my $pfxlen = length("$projects_list");
2774 my $pfxdepth = ($projects_list =~ tr!/!!);
2775 # when filtering, search only given subdirectory
2782 follow_fast
=> 1, # follow symbolic links
2783 follow_skip
=> 2, # ignore duplicates
2784 dangling_symlinks
=> 0, # ignore dangling symlinks, silently
2787 our $project_maxdepth;
2789 # skip project-list toplevel, if we get it.
2790 return if (m!^[/.]$!);
2791 # only directories can be git repositories
2792 return unless (-d
$_);
2793 # don't traverse too deep (Find is super slow on os x)
2794 # $project_maxdepth excludes depth of $projectroot
2795 if (($File::Find
::name
=~ tr!/!!) - $pfxdepth > $project_maxdepth) {
2796 $File::Find
::prune
= 1;
2800 my $path = substr($File::Find
::name
, $pfxlen + 1);
2801 # we check related file in $projectroot
2802 if (check_export_ok
("$projectroot/$path")) {
2803 push @list, { path
=> $path };
2804 $File::Find
::prune
= 1;
2809 } elsif (-f
$projects_list) {
2810 # read from file(url-encoded):
2811 # 'git%2Fgit.git Linus+Torvalds'
2812 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2813 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2814 open my $fd, '<', $projects_list or return;
2816 while (my $line = <$fd>) {
2818 my ($path, $owner) = split ' ', $line;
2819 $path = unescape
($path);
2820 $owner = unescape
($owner);
2821 if (!defined $path) {
2824 # if $filter is rpovided, check if $path begins with $filter
2825 if ($filter && $path !~ m!^\Q$filter\E/!) {
2828 if (check_export_ok
("$projectroot/$path")) {
2831 owner
=> to_utf8
($owner),
2841 # written with help of Tree::Trie module (Perl Artistic License, GPL compatibile)
2842 # as side effects it sets 'forks' field to list of forks for forked projects
2843 sub filter_forks_from_projects_list
{
2844 my $projects = shift;
2846 my %trie; # prefix tree of directories (path components)
2847 # generate trie out of those directories that might contain forks
2848 foreach my $pr (@
$projects) {
2849 my $path = $pr->{'path'};
2850 $path =~ s/\.git$//; # forks of 'repo.git' are in 'repo/' directory
2851 next if ($path =~ m!/$!); # skip non-bare repositories, e.g. 'repo/.git'
2852 next unless ($path); # skip '.git' repository: tests, git-instaweb
2853 next unless (-d
$path); # containing directory exists
2854 $pr->{'forks'} = []; # there can be 0 or more forks of project
2857 my @dirs = split('/', $path);
2858 # walk the trie, until either runs out of components or out of trie
2860 while (scalar @dirs &&
2861 exists($ref->{$dirs[0]})) {
2862 $ref = $ref->{shift @dirs};
2864 # create rest of trie structure from rest of components
2865 foreach my $dir (@dirs) {
2866 $ref = $ref->{$dir} = {};
2868 # create end marker, store $pr as a data
2869 $ref->{''} = $pr if (!exists $ref->{''});
2872 # filter out forks, by finding shortest prefix match for paths
2875 foreach my $pr (@
$projects) {
2879 foreach my $dir (split('/', $pr->{'path'})) {
2880 if (exists $ref->{''}) {
2881 # found [shortest] prefix, is a fork - skip it
2882 push @
{$ref->{''}{'forks'}}, $pr;
2885 if (!exists $ref->{$dir}) {
2886 # not in trie, cannot have prefix, not a fork
2887 push @filtered, $pr;
2890 # If the dir is there, we just walk one step down the trie.
2891 $ref = $ref->{$dir};
2893 # we ran out of trie
2894 # (shouldn't happen: it's either no match, or end marker)
2895 push @filtered, $pr;
2901 # note: fill_project_list_info must be run first,
2902 # for 'descr_long' and 'ctags' to be filled
2903 sub search_projects_list
{
2904 my ($projlist, %opts) = @_;
2905 my $tagfilter = $opts{'tagfilter'};
2906 my $searchtext = $opts{'searchtext'};
2909 unless ($tagfilter || $searchtext);
2913 foreach my $pr (@
$projlist) {
2916 next unless ref($pr->{'ctags'}) eq 'HASH';
2918 grep { lc($_) eq lc($tagfilter) } keys %{$pr->{'ctags'}};
2923 $pr->{'path'} =~ /$searchtext/ ||
2924 $pr->{'descr_long'} =~ /$searchtext/;
2927 push @projects, $pr;
2933 our $gitweb_project_owner = undef;
2934 sub git_get_project_list_from_file
{
2936 return if (defined $gitweb_project_owner);
2938 $gitweb_project_owner = {};
2939 # read from file (url-encoded):
2940 # 'git%2Fgit.git Linus+Torvalds'
2941 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2942 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2943 if (-f
$projects_list) {
2944 open(my $fd, '<', $projects_list);
2945 while (my $line = <$fd>) {
2947 my ($pr, $ow) = split ' ', $line;
2948 $pr = unescape
($pr);
2949 $ow = unescape
($ow);
2950 $gitweb_project_owner->{$pr} = to_utf8
($ow);
2956 sub git_get_project_owner
{
2957 my $project = shift;
2960 return undef unless $project;
2961 $git_dir = "$projectroot/$project";
2963 if (!defined $gitweb_project_owner) {
2964 git_get_project_list_from_file
();
2967 if (exists $gitweb_project_owner->{$project}) {
2968 $owner = $gitweb_project_owner->{$project};
2970 if (!defined $owner){
2971 $owner = git_get_project_config
('owner');
2973 if (!defined $owner) {
2974 $owner = get_file_owner
("$git_dir");
2980 sub git_get_last_activity
{
2984 $git_dir = "$projectroot/$path";
2985 open($fd, "-|", git_cmd
(), 'for-each-ref',
2986 '--format=%(committer)',
2987 '--sort=-committerdate',
2989 'refs/heads') or return;
2990 my $most_recent = <$fd>;
2991 close $fd or return;
2992 if (defined $most_recent &&
2993 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
2995 my $age = time - $timestamp;
2996 return ($age, age_string
($age));
2998 return (undef, undef);
3001 # Implementation note: when a single remote is wanted, we cannot use 'git
3002 # remote show -n' because that command always work (assuming it's a remote URL
3003 # if it's not defined), and we cannot use 'git remote show' because that would
3004 # try to make a network roundtrip. So the only way to find if that particular
3005 # remote is defined is to walk the list provided by 'git remote -v' and stop if
3006 # and when we find what we want.
3007 sub git_get_remotes_list
{
3011 open my $fd, '-|' , git_cmd
(), 'remote', '-v';
3013 while (my $remote = <$fd>) {
3015 $remote =~ s!\t(.*?)\s+\((\w+)\)$!!;
3016 next if $wanted and not $remote eq $wanted;
3017 my ($url, $key) = ($1, $2);
3019 $remotes{$remote} ||= { 'heads' => () };
3020 $remotes{$remote}{$key} = $url;
3022 close $fd or return;
3023 return wantarray ?
%remotes : \
%remotes;
3026 # Takes a hash of remotes as first parameter and fills it by adding the
3027 # available remote heads for each of the indicated remotes.
3028 sub fill_remote_heads
{
3029 my $remotes = shift;
3030 my @heads = map { "remotes/$_" } keys %$remotes;
3031 my @remoteheads = git_get_heads_list
(undef, @heads);
3032 foreach my $remote (keys %$remotes) {
3033 $remotes->{$remote}{'heads'} = [ grep {
3034 $_->{'name'} =~ s!^$remote/!!
3039 sub git_get_references
{
3040 my $type = shift || "";
3042 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
3043 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
3044 open my $fd, "-|", git_cmd
(), "show-ref", "--dereference",
3045 ($type ?
("--", "refs/$type") : ()) # use -- <pattern> if $type
3048 while (my $line = <$fd>) {
3050 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {
3051 if (defined $refs{$1}) {
3052 push @
{$refs{$1}}, $2;
3058 close $fd or return;
3062 sub git_get_rev_name_tags
{
3063 my $hash = shift || return undef;
3065 open my $fd, "-|", git_cmd
(), "name-rev", "--tags", $hash
3067 my $name_rev = <$fd>;
3070 if ($name_rev =~ m
|^$hash tags
/(.*)$|) {
3073 # catches also '$hash undefined' output
3078 ## ----------------------------------------------------------------------
3079 ## parse to hash functions
3083 my $tz = shift || "-0000";
3086 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
3087 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
3088 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
3089 $date{'hour'} = $hour;
3090 $date{'minute'} = $min;
3091 $date{'mday'} = $mday;
3092 $date{'day'} = $days[$wday];
3093 $date{'month'} = $months[$mon];
3094 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
3095 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
3096 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
3097 $mday, $months[$mon], $hour ,$min;
3098 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
3099 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
3101 my ($tz_sign, $tz_hour, $tz_min) =
3102 ($tz =~ m/^([-+])(\d\d)(\d\d)$/);
3103 $tz_sign = ($tz_sign eq '-' ?
-1 : +1);
3104 my $local = $epoch + $tz_sign*((($tz_hour*60) + $tz_min)*60);
3105 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
3106 $date{'hour_local'} = $hour;
3107 $date{'minute_local'} = $min;
3108 $date{'tz_local'} = $tz;
3109 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
3110 1900+$year, $mon+1, $mday,
3111 $hour, $min, $sec, $tz);
3120 open my $fd, "-|", git_cmd
(), "cat-file", "tag", $tag_id or return;
3121 $tag{'id'} = $tag_id;
3122 while (my $line = <$fd>) {
3124 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
3125 $tag{'object'} = $1;
3126 } elsif ($line =~ m/^type (.+)$/) {
3128 } elsif ($line =~ m/^tag (.+)$/) {
3130 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
3131 $tag{'author'} = $1;
3132 $tag{'author_epoch'} = $2;
3133 $tag{'author_tz'} = $3;
3134 if ($tag{'author'} =~ m/^([^<]+) <([^>]*)>/) {
3135 $tag{'author_name'} = $1;
3136 $tag{'author_email'} = $2;
3138 $tag{'author_name'} = $tag{'author'};
3140 } elsif ($line =~ m/--BEGIN/) {
3141 push @comment, $line;
3143 } elsif ($line eq "") {
3147 push @comment, <$fd>;
3148 $tag{'comment'} = \
@comment;
3149 close $fd or return;
3150 if (!defined $tag{'name'}) {
3156 sub parse_commit_text
{
3157 my ($commit_text, $withparents) = @_;
3158 my @commit_lines = split '\n', $commit_text;
3161 pop @commit_lines; # Remove '\0'
3163 if (! @commit_lines) {
3167 my $header = shift @commit_lines;
3168 if ($header !~ m/^[0-9a-fA-F]{40}/) {
3171 ($co{'id'}, my @parents) = split ' ', $header;
3172 while (my $line = shift @commit_lines) {
3173 last if $line eq "\n";
3174 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
3176 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
3178 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
3179 $co{'author'} = to_utf8
($1);
3180 $co{'author_epoch'} = $2;
3181 $co{'author_tz'} = $3;
3182 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
3183 $co{'author_name'} = $1;
3184 $co{'author_email'} = $2;
3186 $co{'author_name'} = $co{'author'};
3188 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
3189 $co{'committer'} = to_utf8
($1);
3190 $co{'committer_epoch'} = $2;
3191 $co{'committer_tz'} = $3;
3192 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
3193 $co{'committer_name'} = $1;
3194 $co{'committer_email'} = $2;
3196 $co{'committer_name'} = $co{'committer'};
3200 if (!defined $co{'tree'}) {
3203 $co{'parents'} = \
@parents;
3204 $co{'parent'} = $parents[0];
3206 foreach my $title (@commit_lines) {
3209 $co{'title'} = chop_str
($title, 80, 5);
3210 # remove leading stuff of merges to make the interesting part visible
3211 if (length($title) > 50) {
3212 $title =~ s/^Automatic //;
3213 $title =~ s/^merge (of|with) /Merge ... /i;
3214 if (length($title) > 50) {
3215 $title =~ s/(http|rsync):\/\///;
3217 if (length($title) > 50) {
3218 $title =~ s/(master|www|rsync)\.//;
3220 if (length($title) > 50) {
3221 $title =~ s/kernel.org:?//;
3223 if (length($title) > 50) {
3224 $title =~ s/\/pub\/scm//;
3227 $co{'title_short'} = chop_str
($title, 50, 5);
3231 if (! defined $co{'title'} || $co{'title'} eq "") {
3232 $co{'title'} = $co{'title_short'} = '(no commit message)';
3234 # remove added spaces
3235 foreach my $line (@commit_lines) {
3238 $co{'comment'} = \
@commit_lines;
3240 my $age = time - $co{'committer_epoch'};
3242 $co{'age_string'} = age_string
($age);
3243 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
3244 if ($age > 60*60*24*7*2) {
3245 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
3246 $co{'age_string_age'} = $co{'age_string'};
3248 $co{'age_string_date'} = $co{'age_string'};
3249 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
3255 my ($commit_id) = @_;
3260 open my $fd, "-|", git_cmd
(), "rev-list",
3266 or die_error
(500, "Open git-rev-list failed");
3267 %co = parse_commit_text
(<$fd>, 1);
3274 my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
3282 open my $fd, "-|", git_cmd
(), "rev-list",
3285 ("--max-count=" . $maxcount),
3286 ("--skip=" . $skip),
3290 ($filename ?
($filename) : ())
3291 or die_error
(500, "Open git-rev-list failed");
3292 while (my $line = <$fd>) {
3293 my %co = parse_commit_text
($line);
3298 return wantarray ?
@cos : \
@cos;
3301 # parse line of git-diff-tree "raw" output
3302 sub parse_difftree_raw_line
{
3306 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
3307 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
3308 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
3309 $res{'from_mode'} = $1;
3310 $res{'to_mode'} = $2;
3311 $res{'from_id'} = $3;
3313 $res{'status'} = $5;
3314 $res{'similarity'} = $6;
3315 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
3316 ($res{'from_file'}, $res{'to_file'}) = map { unquote
($_) } split("\t", $7);
3318 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote
($7);
3321 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
3322 # combined diff (for merge commit)
3323 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
3324 $res{'nparents'} = length($1);
3325 $res{'from_mode'} = [ split(' ', $2) ];
3326 $res{'to_mode'} = pop @
{$res{'from_mode'}};
3327 $res{'from_id'} = [ split(' ', $3) ];
3328 $res{'to_id'} = pop @
{$res{'from_id'}};
3329 $res{'status'} = [ split('', $4) ];
3330 $res{'to_file'} = unquote
($5);
3332 # 'c512b523472485aef4fff9e57b229d9d243c967f'
3333 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
3334 $res{'commit'} = $1;
3337 return wantarray ?
%res : \
%res;
3340 # wrapper: return parsed line of git-diff-tree "raw" output
3341 # (the argument might be raw line, or parsed info)
3342 sub parsed_difftree_line
{
3343 my $line_or_ref = shift;
3345 if (ref($line_or_ref) eq "HASH") {
3346 # pre-parsed (or generated by hand)
3347 return $line_or_ref;
3349 return parse_difftree_raw_line
($line_or_ref);
3353 # parse line of git-ls-tree output
3354 sub parse_ls_tree_line
{
3360 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'
3361 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;
3370 $res{'name'} = unquote
($5);
3373 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
3374 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
3382 $res{'name'} = unquote
($4);
3386 return wantarray ?
%res : \
%res;
3389 # generates _two_ hashes, references to which are passed as 2 and 3 argument
3390 sub parse_from_to_diffinfo
{
3391 my ($diffinfo, $from, $to, @parents) = @_;
3393 if ($diffinfo->{'nparents'}) {
3395 $from->{'file'} = [];
3396 $from->{'href'} = [];
3397 fill_from_file_info
($diffinfo, @parents)
3398 unless exists $diffinfo->{'from_file'};
3399 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
3400 $from->{'file'}[$i] =
3401 defined $diffinfo->{'from_file'}[$i] ?
3402 $diffinfo->{'from_file'}[$i] :
3403 $diffinfo->{'to_file'};
3404 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
3405 $from->{'href'}[$i] = href
(action
=>"blob",
3406 hash_base
=>$parents[$i],
3407 hash
=>$diffinfo->{'from_id'}[$i],
3408 file_name
=>$from->{'file'}[$i]);
3410 $from->{'href'}[$i] = undef;
3414 # ordinary (not combined) diff
3415 $from->{'file'} = $diffinfo->{'from_file'};
3416 if ($diffinfo->{'status'} ne "A") { # not new (added) file
3417 $from->{'href'} = href
(action
=>"blob", hash_base
=>$hash_parent,
3418 hash
=>$diffinfo->{'from_id'},
3419 file_name
=>$from->{'file'});
3421 delete $from->{'href'};
3425 $to->{'file'} = $diffinfo->{'to_file'};
3426 if (!is_deleted
($diffinfo)) { # file exists in result
3427 $to->{'href'} = href
(action
=>"blob", hash_base
=>$hash,
3428 hash
=>$diffinfo->{'to_id'},
3429 file_name
=>$to->{'file'});
3431 delete $to->{'href'};
3435 ## ......................................................................
3436 ## parse to array of hashes functions
3438 sub git_get_heads_list
{
3439 my ($limit, @classes) = @_;
3440 @classes = ('heads') unless @classes;
3441 my @patterns = map { "refs/$_" } @classes;
3444 open my $fd, '-|', git_cmd
(), 'for-each-ref',
3445 ($limit ?
'--count='.($limit+1) : ()), '--sort=-committerdate',
3446 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
3449 while (my $line = <$fd>) {
3453 my ($refinfo, $committerinfo) = split(/\0/, $line);
3454 my ($hash, $name, $title) = split(' ', $refinfo, 3);
3455 my ($committer, $epoch, $tz) =
3456 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
3457 $ref_item{'fullname'} = $name;
3458 $name =~ s!^refs/(?:head|remote)s/!!;
3460 $ref_item{'name'} = $name;
3461 $ref_item{'id'} = $hash;
3462 $ref_item{'title'} = $title || '(no commit message)';
3463 $ref_item{'epoch'} = $epoch;
3465 $ref_item{'age'} = age_string
(time - $ref_item{'epoch'});
3467 $ref_item{'age'} = "unknown";
3470 push @headslist, \
%ref_item;
3474 return wantarray ?
@headslist : \
@headslist;
3477 sub git_get_tags_list
{
3481 open my $fd, '-|', git_cmd
(), 'for-each-ref',
3482 ($limit ?
'--count='.($limit+1) : ()), '--sort=-creatordate',
3483 '--format=%(objectname) %(objecttype) %(refname) '.
3484 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
3487 while (my $line = <$fd>) {
3491 my ($refinfo, $creatorinfo) = split(/\0/, $line);
3492 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
3493 my ($creator, $epoch, $tz) =
3494 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
3495 $ref_item{'fullname'} = $name;
3496 $name =~ s!^refs/tags/!!;
3498 $ref_item{'type'} = $type;
3499 $ref_item{'id'} = $id;
3500 $ref_item{'name'} = $name;
3501 if ($type eq "tag") {
3502 $ref_item{'subject'} = $title;
3503 $ref_item{'reftype'} = $reftype;
3504 $ref_item{'refid'} = $refid;
3506 $ref_item{'reftype'} = $type;
3507 $ref_item{'refid'} = $id;
3510 if ($type eq "tag" || $type eq "commit") {
3511 $ref_item{'epoch'} = $epoch;
3513 $ref_item{'age'} = age_string
(time - $ref_item{'epoch'});
3515 $ref_item{'age'} = "unknown";
3519 push @tagslist, \
%ref_item;
3523 return wantarray ?
@tagslist : \
@tagslist;
3526 ## ----------------------------------------------------------------------
3527 ## filesystem-related functions
3529 sub get_file_owner
{
3532 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
3533 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
3534 if (!defined $gcos) {
3538 $owner =~ s/[,;].*$//;
3539 return to_utf8
($owner);
3542 # assume that file exists
3544 my $filename = shift;
3546 open my $fd, '<', $filename;
3547 print map { to_utf8
($_) } <$fd>;
3551 ## ......................................................................
3552 ## mimetype related functions
3554 sub mimetype_guess_file
{
3555 my $filename = shift;
3556 my $mimemap = shift;
3557 -r
$mimemap or return undef;
3560 open(my $mh, '<', $mimemap) or return undef;
3562 next if m/^#/; # skip comments
3563 my ($mimetype, $exts) = split(/\t+/);
3564 if (defined $exts) {
3565 my @exts = split(/\s+/, $exts);
3566 foreach my $ext (@exts) {
3567 $mimemap{$ext} = $mimetype;
3573 $filename =~ /\.([^.]*)$/;
3574 return $mimemap{$1};
3577 sub mimetype_guess
{
3578 my $filename = shift;
3580 $filename =~ /\./ or return undef;
3582 if ($mimetypes_file) {
3583 my $file = $mimetypes_file;
3584 if ($file !~ m!^/!) { # if it is relative path
3585 # it is relative to project
3586 $file = "$projectroot/$project/$file";
3588 $mime = mimetype_guess_file
($filename, $file);
3590 $mime ||= mimetype_guess_file
($filename, '/etc/mime.types');
3596 my $filename = shift;
3599 my $mime = mimetype_guess
($filename);
3600 $mime and return $mime;
3604 return $default_blob_plain_mimetype unless $fd;
3607 return 'text/plain';
3608 } elsif (! $filename) {
3609 return 'application/octet-stream';
3610 } elsif ($filename =~ m/\.png$/i) {
3612 } elsif ($filename =~ m/\.gif$/i) {
3614 } elsif ($filename =~ m/\.jpe?g$/i) {
3615 return 'image/jpeg';
3617 return 'application/octet-stream';
3621 sub blob_contenttype
{
3622 my ($fd, $file_name, $type) = @_;
3624 $type ||= blob_mimetype
($fd, $file_name);
3625 if ($type eq 'text/plain' && defined $default_text_plain_charset) {
3626 $type .= "; charset=$default_text_plain_charset";
3632 # guess file syntax for syntax highlighting; return undef if no highlighting
3633 # the name of syntax can (in the future) depend on syntax highlighter used
3634 sub guess_file_syntax
{
3635 my ($highlight, $mimetype, $file_name) = @_;
3636 return undef unless ($highlight && defined $file_name);
3637 my $basename = basename
($file_name, '.in');
3638 return $highlight_basename{$basename}
3639 if exists $highlight_basename{$basename};
3641 $basename =~ /\.([^.]*)$/;
3642 my $ext = $1 or return undef;
3643 return $highlight_ext{$ext}
3644 if exists $highlight_ext{$ext};
3649 # run highlighter and return FD of its output,
3650 # or return original FD if no highlighting
3651 sub run_highlighter
{
3652 my ($fd, $highlight, $syntax) = @_;
3653 return $fd unless ($highlight && defined $syntax);
3656 open $fd, quote_command
(git_cmd
(), "cat-file", "blob", $hash)." | ".
3657 quote_command
($highlight_bin).
3658 " --replace-tabs=8 --fragment --syntax $syntax |"
3659 or die_error
(500, "Couldn't open file or run syntax highlighter");
3663 ## ======================================================================
3664 ## functions printing HTML: header, footer, error page
3666 sub get_page_title
{
3667 my $title = to_utf8
($site_name);
3669 return $title unless (defined $project);
3670 $title .= " - " . to_utf8
($project);
3672 return $title unless (defined $action);
3673 $title .= "/$action"; # $action is US-ASCII (7bit ASCII)
3675 return $title unless (defined $file_name);
3676 $title .= " - " . esc_path
($file_name);
3677 if ($action eq "tree" && $file_name !~ m
|/$|) {
3684 sub print_feed_meta
{
3685 if (defined $project) {
3686 my %href_params = get_feed_info
();
3687 if (!exists $href_params{'-title'}) {
3688 $href_params{'-title'} = 'log';
3691 foreach my $format (qw(RSS Atom)) {
3692 my $type = lc($format);
3694 '-rel' => 'alternate',
3695 '-title' => esc_attr
("$project - $href_params{'-title'} - $format feed"),
3696 '-type' => "application/$type+xml"
3699 $href_params{'action'} = $type;
3700 $link_attr{'-href'} = href
(%href_params);
3702 "rel=\"$link_attr{'-rel'}\" ".
3703 "title=\"$link_attr{'-title'}\" ".
3704 "href=\"$link_attr{'-href'}\" ".
3705 "type=\"$link_attr{'-type'}\" ".
3708 $href_params{'extra_options'} = '--no-merges';
3709 $link_attr{'-href'} = href
(%href_params);
3710 $link_attr{'-title'} .= ' (no merges)';
3712 "rel=\"$link_attr{'-rel'}\" ".
3713 "title=\"$link_attr{'-title'}\" ".
3714 "href=\"$link_attr{'-href'}\" ".
3715 "type=\"$link_attr{'-type'}\" ".
3720 printf('<link rel="alternate" title="%s projects list" '.
3721 'href="%s" type="text/plain; charset=utf-8" />'."\n",
3722 esc_attr
($site_name), href
(project
=>undef, action
=>"project_index"));
3723 printf('<link rel="alternate" title="%s projects feeds" '.
3724 'href="%s" type="text/x-opml" />'."\n",
3725 esc_attr
($site_name), href
(project
=>undef, action
=>"opml"));
3729 sub git_header_html
{
3730 my $status = shift || "200 OK";
3731 my $expires = shift;
3734 my $title = get_page_title
();
3736 # require explicit support from the UA if we are to send the page as
3737 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
3738 # we have to do this because MSIE sometimes globs '*/*', pretending to
3739 # support xhtml+xml but choking when it gets what it asked for.
3740 if (defined $cgi->http('HTTP_ACCEPT') &&
3741 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\
+xml
(,|;|\s
|$)/ &&
3742 $cgi->Accept('application/xhtml+xml') != 0) {
3743 $content_type = 'application/xhtml+xml';
3745 $content_type = 'text/html';
3747 print $cgi->header(-type
=>$content_type, -charset
=> 'utf-8',
3748 -status
=> $status, -expires
=> $expires)
3749 unless ($opts{'-no_http_header'});
3750 my $mod_perl_version = $ENV{'MOD_PERL'} ?
" $ENV{'MOD_PERL'}" : '';
3752 <?xml version="1.0" encoding="utf-8"?>
3753 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3754 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
3755 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
3756 <!-- git core binaries version $git_version -->
3758 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
3759 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
3760 <meta name="robots" content="index, nofollow"/>
3761 <title>$title</title>
3763 # the stylesheet, favicon etc urls won't work correctly with path_info
3764 # unless we set the appropriate base URL
3765 if ($ENV{'PATH_INFO'}) {
3766 print "<base href=\"".esc_url
($base_url)."\" />\n";
3768 # print out each stylesheet that exist, providing backwards capability
3769 # for those people who defined $stylesheet in a config file
3770 if (defined $stylesheet) {
3771 print '<link rel="stylesheet" type="text/css" href="'.esc_url
($stylesheet).'"/>'."\n";
3773 foreach my $stylesheet (@stylesheets) {
3774 next unless $stylesheet;
3775 print '<link rel="stylesheet" type="text/css" href="'.esc_url
($stylesheet).'"/>'."\n";
3779 if ($status eq '200 OK');
3780 if (defined $favicon) {
3781 print qq(<link rel
="shortcut icon" href
=").esc_url($favicon).qq(" type
="image/png" />\n);
3787 if (defined $site_header && -f
$site_header) {
3788 insert_file
($site_header);
3791 print "<div class=\"page_header\">\n";
3792 if (defined $logo) {
3793 print $cgi->a({-href
=> esc_url
($logo_url),
3794 -title
=> $logo_label},
3795 $cgi->img({-src
=> esc_url
($logo),
3796 -width
=> 72, -height
=> 27,
3798 -class => "logo"}));
3800 print $cgi->a({-href
=> esc_url
($home_link)}, $home_link_str) . " / ";
3801 if (defined $project) {
3802 print $cgi->a({-href
=> href
(action
=>"summary")}, esc_html
($project));
3803 if (defined $action) {
3804 my $action_print = $action ;
3805 if (defined $opts{-action_extra
}) {
3806 $action_print = $cgi->a({-href
=> href
(action
=>$action)},
3809 print " / $action_print";
3811 if (defined $opts{-action_extra
}) {
3812 print " / $opts{-action_extra}";
3818 my $have_search = gitweb_check_feature
('search');
3819 if (defined $project && $have_search) {
3820 if (!defined $searchtext) {
3824 if (defined $hash_base) {
3825 $search_hash = $hash_base;
3826 } elsif (defined $hash) {
3827 $search_hash = $hash;
3829 $search_hash = "HEAD";
3831 my $action = $my_uri;
3832 my $use_pathinfo = gitweb_check_feature
('pathinfo');
3833 if ($use_pathinfo) {
3834 $action .= "/".esc_url
($project);
3836 print $cgi->startform(-method
=> "get", -action
=> $action) .
3837 "<div class=\"search\">\n" .
3839 $cgi->input({-name
=>"p", -value
=>$project, -type
=>"hidden"}) . "\n") .
3840 $cgi->input({-name
=>"a", -value
=>"search", -type
=>"hidden"}) . "\n" .
3841 $cgi->input({-name
=>"h", -value
=>$search_hash, -type
=>"hidden"}) . "\n" .
3842 $cgi->popup_menu(-name
=> 'st', -default => 'commit',
3843 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
3844 $cgi->sup($cgi->a({-href
=> href
(action
=>"search_help")}, "?")) .
3846 $cgi->textfield(-name
=> "s", -value
=> $searchtext) . "\n" .
3847 "<span title=\"Extended regular expression\">" .
3848 $cgi->checkbox(-name
=> 'sr', -value
=> 1, -label
=> 're',
3849 -checked
=> $search_use_regexp) .
3852 $cgi->end_form() . "\n";
3856 sub git_footer_html
{
3857 my $feed_class = 'rss_logo';
3859 print "<div class=\"page_footer\">\n";
3860 if (defined $project) {
3861 my $descr = git_get_project_description
($project);
3862 if (defined $descr) {
3863 print "<div class=\"page_footer_text\">" . esc_html
($descr) . "</div>\n";
3866 my %href_params = get_feed_info
();
3867 if (!%href_params) {
3868 $feed_class .= ' generic';
3870 $href_params{'-title'} ||= 'log';
3872 foreach my $format (qw(RSS Atom)) {
3873 $href_params{'action'} = lc($format);
3874 print $cgi->a({-href
=> href
(%href_params),
3875 -title
=> "$href_params{'-title'} $format feed",
3876 -class => $feed_class}, $format)."\n";
3880 print $cgi->a({-href
=> href
(project
=>undef, action
=>"opml"),
3881 -class => $feed_class}, "OPML") . " ";
3882 print $cgi->a({-href
=> href
(project
=>undef, action
=>"project_index"),
3883 -class => $feed_class}, "TXT") . "\n";
3885 print "</div>\n"; # class="page_footer"
3887 if (defined $t0 && gitweb_check_feature
('timed')) {
3888 print "<div id=\"generating_info\">\n";
3889 print 'This page took '.
3890 '<span id="generating_time" class="time_span">'.
3891 tv_interval
($t0, [ gettimeofday
() ]).
3894 '<span id="generating_cmd">'.
3895 $number_of_git_cmds.
3896 '</span> git commands '.
3898 print "</div>\n"; # class="page_footer"
3901 if (defined $site_footer && -f
$site_footer) {
3902 insert_file
($site_footer);
3905 print qq!<script type
="text/javascript" src
="!.esc_url($javascript).qq!"></script
>\n!;
3906 if (defined $action &&
3907 $action eq 'blame_incremental') {
3908 print qq!<script type
="text/javascript">\n!.
3909 qq!startBlame
("!. href(action=>"blame_data
", -replay=>1) .qq!",\n!.
3910 qq! "!. href() .qq!");\n!.
3913 my ($jstimezone, $tz_cookie, $datetime_class) =
3914 gitweb_get_feature
('javascript-timezone');
3916 print qq!<script type
="text/javascript">\n!.
3917 qq!window
.onload
= function
() {\n!;
3918 if (gitweb_check_feature
('javascript-actions')) {
3919 print qq! fixLinks
();\n!;
3921 if ($jstimezone && $tz_cookie && $datetime_class) {
3922 print qq! var tz_cookie
= { name
: '$tz_cookie', expires
: 14, path
: '/' };\n!. # in days
3923 qq! onloadTZSetup
('$jstimezone', tz_cookie
, '$datetime_class');\n!;
3933 # die_error(<http_status_code>, <error_message>[, <detailed_html_description>])
3934 # Example: die_error(404, 'Hash not found')
3935 # By convention, use the following status codes (as defined in RFC 2616):
3936 # 400: Invalid or missing CGI parameters, or
3937 # requested object exists but has wrong type.
3938 # 403: Requested feature (like "pickaxe" or "snapshot") not enabled on
3939 # this server or project.
3940 # 404: Requested object/revision/project doesn't exist.
3941 # 500: The server isn't configured properly, or
3942 # an internal error occurred (e.g. failed assertions caused by bugs), or
3943 # an unknown error occurred (e.g. the git binary died unexpectedly).
3944 # 503: The server is currently unavailable (because it is overloaded,
3945 # or down for maintenance). Generally, this is a temporary state.
3947 my $status = shift || 500;
3948 my $error = esc_html
(shift) || "Internal Server Error";
3952 my %http_responses = (
3953 400 => '400 Bad Request',
3954 403 => '403 Forbidden',
3955 404 => '404 Not Found',
3956 500 => '500 Internal Server Error',
3957 503 => '503 Service Unavailable',
3959 git_header_html
($http_responses{$status}, undef, %opts);
3961 <div class="page_body">
3966 if (defined $extra) {
3974 unless ($opts{'-error_handler'});
3977 ## ----------------------------------------------------------------------
3978 ## functions printing or outputting HTML: navigation
3980 sub git_print_page_nav
{
3981 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
3982 $extra = '' if !defined $extra; # pager or formats
3984 my @navs = qw(summary shortlog log commit commitdiff tree);
3986 @navs = grep { $_ ne $suppress } @navs;
3989 my %arg = map { $_ => {action
=>$_} } @navs;
3990 if (defined $head) {
3991 for (qw(commit commitdiff)) {
3992 $arg{$_}{'hash'} = $head;
3994 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
3995 for (qw(shortlog log)) {
3996 $arg{$_}{'hash'} = $head;
4001 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
4002 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
4004 my @actions = gitweb_get_feature
('actions');
4007 'n' => $project, # project name
4008 'f' => $git_dir, # project path within filesystem
4009 'h' => $treehead || '', # current hash ('h' parameter)
4010 'b' => $treebase || '', # hash base ('hb' parameter)
4013 my ($label, $link, $pos) = splice(@actions,0,3);
4015 @navs = map { $_ eq $pos ?
($_, $label) : $_ } @navs;
4017 $link =~ s/%([%nfhb])/$repl{$1}/g;
4018 $arg{$label}{'_href'} = $link;
4021 print "<div class=\"page_nav\">\n" .
4023 map { $_ eq $current ?
4024 $_ : $cgi->a({-href
=> ($arg{$_}{_href
} ?
$arg{$_}{_href
} : href
(%{$arg{$_}}))}, "$_")
4026 print "<br/>\n$extra<br/>\n" .
4030 # returns a submenu for the nagivation of the refs views (tags, heads,
4031 # remotes) with the current view disabled and the remotes view only
4032 # available if the feature is enabled
4033 sub format_ref_views
{
4035 my @ref_views = qw{tags heads
};
4036 push @ref_views, 'remotes' if gitweb_check_feature
('remote_heads');
4037 return join " | ", map {
4038 $_ eq $current ?
$_ :
4039 $cgi->a({-href
=> href
(action
=>$_)}, $_)
4043 sub format_paging_nav
{
4044 my ($action, $page, $has_next_link) = @_;
4050 $cgi->a({-href
=> href
(-replay
=>1, page
=>undef)}, "first") .
4052 $cgi->a({-href
=> href
(-replay
=>1, page
=>$page-1),
4053 -accesskey
=> "p", -title
=> "Alt-p"}, "prev");
4055 $paging_nav .= "first ⋅ prev";
4058 if ($has_next_link) {
4059 $paging_nav .= " ⋅ " .
4060 $cgi->a({-href
=> href
(-replay
=>1, page
=>$page+1),
4061 -accesskey
=> "n", -title
=> "Alt-n"}, "next");
4063 $paging_nav .= " ⋅ next";
4069 ## ......................................................................
4070 ## functions printing or outputting HTML: div
4072 sub git_print_header_div
{
4073 my ($action, $title, $hash, $hash_base) = @_;
4076 $args{'action'} = $action;
4077 $args{'hash'} = $hash if $hash;
4078 $args{'hash_base'} = $hash_base if $hash_base;
4080 print "<div class=\"header\">\n" .
4081 $cgi->a({-href
=> href
(%args), -class => "title"},
4082 $title ?
$title : $action) .
4086 sub format_repo_url
{
4087 my ($name, $url) = @_;
4088 return "<tr class=\"metadata_url\"><td>$name</td><td>$url</td></tr>\n";
4091 # Group output by placing it in a DIV element and adding a header.
4092 # Options for start_div() can be provided by passing a hash reference as the
4093 # first parameter to the function.
4094 # Options to git_print_header_div() can be provided by passing an array
4095 # reference. This must follow the options to start_div if they are present.
4096 # The content can be a scalar, which is output as-is, a scalar reference, which
4097 # is output after html escaping, an IO handle passed either as *handle or
4098 # *handle{IO}, or a function reference. In the latter case all following
4099 # parameters will be taken as argument to the content function call.
4100 sub git_print_section
{
4101 my ($div_args, $header_args, $content);
4103 if (ref($arg) eq 'HASH') {
4107 if (ref($arg) eq 'ARRAY') {
4108 $header_args = $arg;
4113 print $cgi->start_div($div_args);
4114 git_print_header_div
(@
$header_args);
4116 if (ref($content) eq 'CODE') {
4118 } elsif (ref($content) eq 'SCALAR') {
4119 print esc_html
($$content);
4120 } elsif (ref($content) eq 'GLOB' or ref($content) eq 'IO::Handle') {
4122 } elsif (!ref($content) && defined($content)) {
4126 print $cgi->end_div;
4129 sub format_timestamp_html
{
4131 my $strtime = $date->{'rfc2822'};
4133 my (undef, undef, $datetime_class) =
4134 gitweb_get_feature
('javascript-timezone');
4135 if ($datetime_class) {
4136 $strtime = qq!<span
class="$datetime_class">$strtime</span
>!;
4139 my $localtime_format = '(%02d:%02d %s)';
4140 if ($date->{'hour_local'} < 6) {
4141 $localtime_format = '(<span class="atnight">%02d:%02d</span> %s)';
4144 sprintf($localtime_format,
4145 $date->{'hour_local'}, $date->{'minute_local'}, $date->{'tz_local'});
4150 # Outputs the author name and date in long form
4151 sub git_print_authorship
{
4154 my $tag = $opts{-tag
} || 'div';
4155 my $author = $co->{'author_name'};
4157 my %ad = parse_date
($co->{'author_epoch'}, $co->{'author_tz'});
4158 print "<$tag class=\"author_date\">" .
4159 format_search_author
($author, "author", esc_html
($author)) .
4160 " [".format_timestamp_html
(\
%ad)."]".
4161 git_get_avatar
($co->{'author_email'}, -pad_before
=> 1) .
4165 # Outputs table rows containing the full author or committer information,
4166 # in the format expected for 'commit' view (& similar).
4167 # Parameters are a commit hash reference, followed by the list of people
4168 # to output information for. If the list is empty it defaults to both
4169 # author and committer.
4170 sub git_print_authorship_rows
{
4172 # too bad we can't use @people = @_ || ('author', 'committer')
4174 @people = ('author', 'committer') unless @people;
4175 foreach my $who (@people) {
4176 my %wd = parse_date
($co->{"${who}_epoch"}, $co->{"${who}_tz"});
4177 print "<tr><td>$who</td><td>" .
4178 format_search_author
($co->{"${who}_name"}, $who,
4179 esc_html
($co->{"${who}_name"})) . " " .
4180 format_search_author
($co->{"${who}_email"}, $who,
4181 esc_html
("<" . $co->{"${who}_email"} . ">")) .
4182 "</td><td rowspan=\"2\">" .
4183 git_get_avatar
($co->{"${who}_email"}, -size
=> 'double') .
4187 format_timestamp_html
(\
%wd) .
4193 sub git_print_page_path
{
4199 print "<div class=\"page_path\">";
4200 print $cgi->a({-href
=> href
(action
=>"tree", hash_base
=>$hb),
4201 -title
=> 'tree root'}, to_utf8
("[$project]"));
4203 if (defined $name) {
4204 my @dirname = split '/', $name;
4205 my $basename = pop @dirname;
4208 foreach my $dir (@dirname) {
4209 $fullname .= ($fullname ?
'/' : '') . $dir;
4210 print $cgi->a({-href
=> href
(action
=>"tree", file_name
=>$fullname,
4212 -title
=> $fullname}, esc_path
($dir));
4215 if (defined $type && $type eq 'blob') {
4216 print $cgi->a({-href
=> href
(action
=>"blob_plain", file_name
=>$file_name,
4218 -title
=> $name}, esc_path
($basename));
4219 print '
4220 <a id="lineNoToggle" href="#" onclick="toggleLineNumbers();"></a>
4222 function toggleLineNumbers() {
4223 e = document.getElementById("lineNoStyle");
4224 e2 = document.getElementById("lineNoToggle");
4225 if (e2.innerHTML == "[Hide line numbers]") {
4226 e.innerHTML = ".linenr { display:none; }";
4227 e2.innerHTML = "[Show line numbers]";
4231 e2.innerHTML = "[Hide line numbers]";
4234 var style = document.createElement("style");
4235 style.setAttribute("id", "lineNoStyle");
4236 document.getElementsByTagName("head")[0].appendChild(style);
4237 toggleLineNumbers();
4240 } elsif (defined $type && $type eq 'tree') {
4241 print $cgi->a({-href
=> href
(action
=>"tree", file_name
=>$file_name,
4243 -title
=> $name}, esc_path
($basename));
4246 print esc_path
($basename);
4249 print "<br/></div>\n";
4256 if ($opts{'-remove_title'}) {
4257 # remove title, i.e. first line of log
4260 # remove leading empty lines
4261 while (defined $log->[0] && $log->[0] eq "") {
4268 foreach my $line (@
$log) {
4269 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
4272 if (! $opts{'-remove_signoff'}) {
4273 print "<span class=\"signoff\">" . esc_html
($line) . "</span><br/>\n";
4276 # remove signoff lines
4283 # print only one empty line
4284 # do not print empty line after signoff
4286 next if ($empty || $signoff);
4292 print format_log_line_html
($line) . "<br/>\n";
4295 if ($opts{'-final_empty_line'}) {
4296 # end with single empty line
4297 print "<br/>\n" unless $empty;
4301 # return link target (what link points to)
4302 sub git_get_link_target
{
4307 open my $fd, "-|", git_cmd
(), "cat-file", "blob", $hash
4311 $link_target = <$fd>;
4316 return $link_target;
4319 # given link target, and the directory (basedir) the link is in,
4320 # return target of link relative to top directory (top tree);
4321 # return undef if it is not possible (including absolute links).
4322 sub normalize_link_target
{
4323 my ($link_target, $basedir) = @_;
4325 # absolute symlinks (beginning with '/') cannot be normalized
4326 return if (substr($link_target, 0, 1) eq '/');
4328 # normalize link target to path from top (root) tree (dir)
4331 $path = $basedir . '/' . $link_target;
4333 # we are in top (root) tree (dir)
4334 $path = $link_target;
4337 # remove //, /./, and /../
4339 foreach my $part (split('/', $path)) {
4340 # discard '.' and ''
4341 next if (!$part || $part eq '.');
4343 if ($part eq '..') {
4347 # link leads outside repository (outside top dir)
4351 push @path_parts, $part;
4354 $path = join('/', @path_parts);
4359 # print tree entry (row of git_tree), but without encompassing <tr> element
4360 sub git_print_tree_entry
{
4361 my ($t, $basedir, $hash_base, $have_blame) = @_;
4364 $base_key{'hash_base'} = $hash_base if defined $hash_base;
4366 # The format of a table row is: mode list link. Where mode is
4367 # the mode of the entry, list is the name of the entry, an href,
4368 # and link is the action links of the entry.
4370 print "<td class=\"mode\">" . mode_str
($t->{'mode'}) . "</td>\n";
4371 if (exists $t->{'size'}) {
4372 print "<td class=\"size\">$t->{'size'}</td>\n";
4374 if ($t->{'type'} eq "blob") {
4375 print "<td class=\"list\">" .
4376 $cgi->a({-href
=> href
(action
=>"blob", hash
=>$t->{'hash'},
4377 file_name
=>"$basedir$t->{'name'}", %base_key),
4378 -class => "list"}, esc_path
($t->{'name'}));
4379 if (S_ISLNK
(oct $t->{'mode'})) {
4380 my $link_target = git_get_link_target
($t->{'hash'});
4382 my $norm_target = normalize_link_target
($link_target, $basedir);
4383 if (defined $norm_target) {
4385 $cgi->a({-href
=> href
(action
=>"object", hash_base
=>$hash_base,
4386 file_name
=>$norm_target),
4387 -title
=> $norm_target}, esc_path
($link_target));
4389 print " -> " . esc_path
($link_target);
4394 print "<td class=\"link\">";
4395 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$t->{'hash'},
4396 file_name
=>"$basedir$t->{'name'}", %base_key)},
4400 $cgi->a({-href
=> href
(action
=>"blame", hash
=>$t->{'hash'},
4401 file_name
=>"$basedir$t->{'name'}", %base_key)},
4404 if (defined $hash_base) {
4406 $cgi->a({-href
=> href
(action
=>"history", hash_base
=>$hash_base,
4407 hash
=>$t->{'hash'}, file_name
=>"$basedir$t->{'name'}")},
4411 $cgi->a({-href
=> href
(action
=>"blob_plain", hash_base
=>$hash_base,
4412 file_name
=>"$basedir$t->{'name'}")},
4416 } elsif ($t->{'type'} eq "tree") {
4417 print "<td class=\"list\">";
4418 print $cgi->a({-href
=> href
(action
=>"tree", hash
=>$t->{'hash'},
4419 file_name
=>"$basedir$t->{'name'}",
4421 esc_path
($t->{'name'}));
4423 print "<td class=\"link\">";
4424 print $cgi->a({-href
=> href
(action
=>"tree", hash
=>$t->{'hash'},
4425 file_name
=>"$basedir$t->{'name'}",
4428 if (defined $hash_base) {
4430 $cgi->a({-href
=> href
(action
=>"history", hash_base
=>$hash_base,
4431 file_name
=>"$basedir$t->{'name'}")},
4436 # unknown object: we can only present history for it
4437 # (this includes 'commit' object, i.e. submodule support)
4438 print "<td class=\"list\">" .
4439 esc_path
($t->{'name'}) .
4441 print "<td class=\"link\">";
4442 if (defined $hash_base) {
4443 print $cgi->a({-href
=> href
(action
=>"history",
4444 hash_base
=>$hash_base,
4445 file_name
=>"$basedir$t->{'name'}")},
4452 ## ......................................................................
4453 ## functions printing large fragments of HTML
4455 # get pre-image filenames for merge (combined) diff
4456 sub fill_from_file_info
{
4457 my ($diff, @parents) = @_;
4459 $diff->{'from_file'} = [ ];
4460 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
4461 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
4462 if ($diff->{'status'}[$i] eq 'R' ||
4463 $diff->{'status'}[$i] eq 'C') {
4464 $diff->{'from_file'}[$i] =
4465 git_get_path_by_hash
($parents[$i], $diff->{'from_id'}[$i]);
4472 # is current raw difftree line of file deletion
4474 my $diffinfo = shift;
4476 return $diffinfo->{'to_id'} eq ('0' x
40);
4479 # does patch correspond to [previous] difftree raw line
4480 # $diffinfo - hashref of parsed raw diff format
4481 # $patchinfo - hashref of parsed patch diff format
4482 # (the same keys as in $diffinfo)
4483 sub is_patch_split
{
4484 my ($diffinfo, $patchinfo) = @_;
4486 return defined $diffinfo && defined $patchinfo
4487 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
4491 sub git_difftree_body
{
4492 my ($difftree, $hash, @parents) = @_;
4493 my ($parent) = $parents[0];
4494 my $have_blame = gitweb_check_feature
('blame');
4495 print "<div class=\"list_head\">\n";
4496 if ($#{$difftree} > 10) {
4497 print(($#{$difftree} + 1) . " files changed:\n");
4501 print "<table class=\"" .
4502 (@parents > 1 ?
"combined " : "") .
4505 # header only for combined diff in 'commitdiff' view
4506 my $has_header = @
$difftree && @parents > 1 && $action eq 'commitdiff';
4509 print "<thead><tr>\n" .
4510 "<th></th><th></th>\n"; # filename, patchN link
4511 for (my $i = 0; $i < @parents; $i++) {
4512 my $par = $parents[$i];
4514 $cgi->a({-href
=> href
(action
=>"commitdiff",
4515 hash
=>$hash, hash_parent
=>$par),
4516 -title
=> 'commitdiff to parent number ' .
4517 ($i+1) . ': ' . substr($par,0,7)},
4521 print "</tr></thead>\n<tbody>\n";
4526 foreach my $line (@
{$difftree}) {
4527 my $diff = parsed_difftree_line
($line);
4530 print "<tr class=\"dark\">\n";
4532 print "<tr class=\"light\">\n";
4536 if (exists $diff->{'nparents'}) { # combined diff
4538 fill_from_file_info
($diff, @parents)
4539 unless exists $diff->{'from_file'};
4541 if (!is_deleted
($diff)) {
4542 # file exists in the result (child) commit
4544 $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'to_id'},
4545 file_name
=>$diff->{'to_file'},
4547 -class => "list"}, esc_path
($diff->{'to_file'})) .
4551 esc_path
($diff->{'to_file'}) .
4555 if ($action eq 'commitdiff') {
4558 print "<td class=\"link\">" .
4559 $cgi->a({-href
=> href
(-anchor
=>"patch$patchno")},
4565 my $has_history = 0;
4566 my $not_deleted = 0;
4567 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
4568 my $hash_parent = $parents[$i];
4569 my $from_hash = $diff->{'from_id'}[$i];
4570 my $from_path = $diff->{'from_file'}[$i];
4571 my $status = $diff->{'status'}[$i];
4573 $has_history ||= ($status ne 'A');
4574 $not_deleted ||= ($status ne 'D');
4576 if ($status eq 'A') {
4577 print "<td class=\"link\" align=\"right\"> | </td>\n";
4578 } elsif ($status eq 'D') {
4579 print "<td class=\"link\">" .
4580 $cgi->a({-href
=> href
(action
=>"blob",
4583 file_name
=>$from_path)},
4587 if ($diff->{'to_id'} eq $from_hash) {
4588 print "<td class=\"link nochange\">";
4590 print "<td class=\"link\">";
4592 print $cgi->a({-href
=> href
(action
=>"blobdiff",
4593 hash
=>$diff->{'to_id'},
4594 hash_parent
=>$from_hash,
4596 hash_parent_base
=>$hash_parent,
4597 file_name
=>$diff->{'to_file'},
4598 file_parent
=>$from_path)},
4604 print "<td class=\"link\">";
4606 print $cgi->a({-href
=> href
(action
=>"blob",
4607 hash
=>$diff->{'to_id'},
4608 file_name
=>$diff->{'to_file'},
4611 print " | " if ($has_history);
4614 print $cgi->a({-href
=> href
(action
=>"history",
4615 file_name
=>$diff->{'to_file'},
4622 next; # instead of 'else' clause, to avoid extra indent
4624 # else ordinary diff
4626 my ($to_mode_oct, $to_mode_str, $to_file_type);
4627 my ($from_mode_oct, $from_mode_str, $from_file_type);
4628 if ($diff->{'to_mode'} ne ('0' x
6)) {
4629 $to_mode_oct = oct $diff->{'to_mode'};
4630 if (S_ISREG
($to_mode_oct)) { # only for regular file
4631 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
4633 $to_file_type = file_type
($diff->{'to_mode'});
4635 if ($diff->{'from_mode'} ne ('0' x
6)) {
4636 $from_mode_oct = oct $diff->{'from_mode'};
4637 if (S_ISREG
($from_mode_oct)) { # only for regular file
4638 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
4640 $from_file_type = file_type
($diff->{'from_mode'});
4643 if ($diff->{'status'} eq "A") { # created
4644 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
4645 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
4646 $mode_chng .= "]</span>";
4648 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'to_id'},
4649 hash_base
=>$hash, file_name
=>$diff->{'file'}),
4650 -class => "list"}, esc_path
($diff->{'file'}));
4652 print "<td>$mode_chng</td>\n";
4653 print "<td class=\"link\">";
4654 if ($action eq 'commitdiff') {
4657 print $cgi->a({-href
=> href
(-anchor
=>"patch$patchno")},
4661 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'to_id'},
4662 hash_base
=>$hash, file_name
=>$diff->{'file'})},
4666 } elsif ($diff->{'status'} eq "D") { # deleted
4667 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
4669 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'from_id'},
4670 hash_base
=>$parent, file_name
=>$diff->{'file'}),
4671 -class => "list"}, esc_path
($diff->{'file'}));
4673 print "<td>$mode_chng</td>\n";
4674 print "<td class=\"link\">";
4675 if ($action eq 'commitdiff') {
4678 print $cgi->a({-href
=> href
(-anchor
=>"patch$patchno")},
4682 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'from_id'},
4683 hash_base
=>$parent, file_name
=>$diff->{'file'})},
4686 print $cgi->a({-href
=> href
(action
=>"blame", hash_base
=>$parent,
4687 file_name
=>$diff->{'file'})},
4690 print $cgi->a({-href
=> href
(action
=>"history", hash_base
=>$parent,
4691 file_name
=>$diff->{'file'})},
4695 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
4696 my $mode_chnge = "";
4697 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
4698 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
4699 if ($from_file_type ne $to_file_type) {
4700 $mode_chnge .= " from $from_file_type to $to_file_type";
4702 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
4703 if ($from_mode_str && $to_mode_str) {
4704 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
4705 } elsif ($to_mode_str) {
4706 $mode_chnge .= " mode: $to_mode_str";
4709 $mode_chnge .= "]</span>\n";
4712 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'to_id'},
4713 hash_base
=>$hash, file_name
=>$diff->{'file'}),
4714 -class => "list"}, esc_path
($diff->{'file'}));
4716 print "<td>$mode_chnge</td>\n";
4717 print "<td class=\"link\">";
4718 if ($action eq 'commitdiff') {
4721 print $cgi->a({-href
=> href
(-anchor
=>"patch$patchno")},
4724 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
4725 # "commit" view and modified file (not onlu mode changed)
4726 print $cgi->a({-href
=> href
(action
=>"blobdiff",
4727 hash
=>$diff->{'to_id'}, hash_parent
=>$diff->{'from_id'},
4728 hash_base
=>$hash, hash_parent_base
=>$parent,
4729 file_name
=>$diff->{'file'})},
4733 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'to_id'},
4734 hash_base
=>$hash, file_name
=>$diff->{'file'})},
4737 print $cgi->a({-href
=> href
(action
=>"blame", hash_base
=>$hash,
4738 file_name
=>$diff->{'file'})},
4741 print $cgi->a({-href
=> href
(action
=>"history", hash_base
=>$hash,
4742 file_name
=>$diff->{'file'})},
4746 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
4747 my %status_name = ('R' => 'moved', 'C' => 'copied');
4748 my $nstatus = $status_name{$diff->{'status'}};
4750 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
4751 # mode also for directories, so we cannot use $to_mode_str
4752 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
4755 $cgi->a({-href
=> href
(action
=>"blob", hash_base
=>$hash,
4756 hash
=>$diff->{'to_id'}, file_name
=>$diff->{'to_file'}),
4757 -class => "list"}, esc_path
($diff->{'to_file'})) . "</td>\n" .
4758 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
4759 $cgi->a({-href
=> href
(action
=>"blob", hash_base
=>$parent,
4760 hash
=>$diff->{'from_id'}, file_name
=>$diff->{'from_file'}),
4761 -class => "list"}, esc_path
($diff->{'from_file'})) .
4762 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
4763 "<td class=\"link\">";
4764 if ($action eq 'commitdiff') {
4767 print $cgi->a({-href
=> href
(-anchor
=>"patch$patchno")},
4770 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
4771 # "commit" view and modified file (not only pure rename or copy)
4772 print $cgi->a({-href
=> href
(action
=>"blobdiff",
4773 hash
=>$diff->{'to_id'}, hash_parent
=>$diff->{'from_id'},
4774 hash_base
=>$hash, hash_parent_base
=>$parent,
4775 file_name
=>$diff->{'to_file'}, file_parent
=>$diff->{'from_file'})},
4779 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'to_id'},
4780 hash_base
=>$parent, file_name
=>$diff->{'to_file'})},
4783 print $cgi->a({-href
=> href
(action
=>"blame", hash_base
=>$hash,
4784 file_name
=>$diff->{'to_file'})},
4787 print $cgi->a({-href
=> href
(action
=>"history", hash_base
=>$hash,
4788 file_name
=>$diff->{'to_file'})},
4792 } # we should not encounter Unmerged (U) or Unknown (X) status
4795 print "</tbody>" if $has_header;
4799 sub git_patchset_body
{
4800 my ($fd, $difftree, $hash, @hash_parents) = @_;
4801 my ($hash_parent) = $hash_parents[0];
4803 my $is_combined = (@hash_parents > 1);
4805 my $patch_number = 0;
4811 print "<div class=\"patchset\">\n";
4813 # skip to first patch
4814 while ($patch_line = <$fd>) {
4817 last if ($patch_line =~ m/^diff /);
4821 while ($patch_line) {
4823 # parse "git diff" header line
4824 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
4825 # $1 is from_name, which we do not use
4826 $to_name = unquote
($2);
4827 $to_name =~ s!^b/!!;
4828 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
4829 # $1 is 'cc' or 'combined', which we do not use
4830 $to_name = unquote
($2);
4835 # check if current patch belong to current raw line
4836 # and parse raw git-diff line if needed
4837 if (is_patch_split
($diffinfo, { 'to_file' => $to_name })) {
4838 # this is continuation of a split patch
4839 print "<div class=\"patch cont\">\n";
4841 # advance raw git-diff output if needed
4842 $patch_idx++ if defined $diffinfo;
4844 # read and prepare patch information
4845 $diffinfo = parsed_difftree_line
($difftree->[$patch_idx]);
4847 # compact combined diff output can have some patches skipped
4848 # find which patch (using pathname of result) we are at now;
4850 while ($to_name ne $diffinfo->{'to_file'}) {
4851 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
4852 format_diff_cc_simplified
($diffinfo, @hash_parents) .
4853 "</div>\n"; # class="patch"
4858 last if $patch_idx > $#$difftree;
4859 $diffinfo = parsed_difftree_line
($difftree->[$patch_idx]);
4863 # modifies %from, %to hashes
4864 parse_from_to_diffinfo
($diffinfo, \
%from, \
%to, @hash_parents);
4866 # this is first patch for raw difftree line with $patch_idx index
4867 # we index @$difftree array from 0, but number patches from 1
4868 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
4872 #assert($patch_line =~ m/^diff /) if DEBUG;
4873 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
4875 # print "git diff" header
4876 print format_git_diff_header_line
($patch_line, $diffinfo,
4879 # print extended diff header
4880 print "<div class=\"diff extended_header\">\n";
4882 while ($patch_line = <$fd>) {
4885 last EXTENDED_HEADER
if ($patch_line =~ m/^--- |^diff /);
4887 print format_extended_diff_header_line
($patch_line, $diffinfo,
4890 print "</div>\n"; # class="diff extended_header"
4892 # from-file/to-file diff header
4893 if (! $patch_line) {
4894 print "</div>\n"; # class="patch"
4897 next PATCH
if ($patch_line =~ m/^diff /);
4898 #assert($patch_line =~ m/^---/) if DEBUG;
4900 my $last_patch_line = $patch_line;
4901 $patch_line = <$fd>;
4903 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
4905 print format_diff_from_to_header
($last_patch_line, $patch_line,
4906 $diffinfo, \
%from, \
%to,
4911 while ($patch_line = <$fd>) {
4914 next PATCH
if ($patch_line =~ m/^diff /);
4916 print format_diff_line
($patch_line, \
%from, \
%to);
4920 print "</div>\n"; # class="patch"
4923 # for compact combined (--cc) format, with chunk and patch simplification
4924 # the patchset might be empty, but there might be unprocessed raw lines
4925 for (++$patch_idx if $patch_number > 0;
4926 $patch_idx < @
$difftree;
4928 # read and prepare patch information
4929 $diffinfo = parsed_difftree_line
($difftree->[$patch_idx]);
4931 # generate anchor for "patch" links in difftree / whatchanged part
4932 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
4933 format_diff_cc_simplified
($diffinfo, @hash_parents) .
4934 "</div>\n"; # class="patch"
4939 if ($patch_number == 0) {
4940 if (@hash_parents > 1) {
4941 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
4943 print "<div class=\"diff nodifferences\">No differences found</div>\n";
4947 print "</div>\n"; # class="patchset"
4950 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4952 # fills project list info (age, description, owner, category, forks)
4953 # for each project in the list, removing invalid projects from
4955 # NOTE: modifies $projlist, but does not remove entries from it
4956 sub fill_project_list_info
{
4957 my $projlist = shift;
4960 my $show_ctags = gitweb_check_feature
('ctags');
4962 foreach my $pr (@
$projlist) {
4963 my (@activity) = git_get_last_activity
($pr->{'path'});
4964 unless (@activity) {
4967 ($pr->{'age'}, $pr->{'age_string'}) = @activity;
4968 if (!defined $pr->{'descr'}) {
4969 my $descr = git_get_project_description
($pr->{'path'}) || "";
4970 $descr = to_utf8
($descr);
4971 $pr->{'descr_long'} = $descr;
4972 $pr->{'descr'} = chop_str
($descr, $projects_list_description_width, 5);
4974 if (!defined $pr->{'owner'}) {
4975 $pr->{'owner'} = git_get_project_owner
("$pr->{'path'}") || "";
4978 $pr->{'ctags'} = git_get_project_ctags
($pr->{'path'});
4980 if ($projects_list_group_categories && !defined $pr->{'category'}) {
4981 my $cat = git_get_project_category
($pr->{'path'}) ||
4982 $project_list_default_category;
4983 $pr->{'category'} = to_utf8
($cat);
4986 push @projects, $pr;
4992 sub sort_projects_list
{
4993 my ($projlist, $order) = @_;
4997 project
=> { key
=> 'path', type
=> 'str' },
4998 descr
=> { key
=> 'descr_long', type
=> 'str' },
4999 owner
=> { key
=> 'owner', type
=> 'str' },
5000 age
=> { key
=> 'age', type
=> 'num' }
5002 my $oi = $order_info{$order};
5003 return @
$projlist unless defined $oi;
5004 if ($oi->{'type'} eq 'str') {
5005 @projects = sort {$a->{$oi->{'key'}} cmp $b->{$oi->{'key'}}} @
$projlist;
5007 @projects = sort {$a->{$oi->{'key'}} <=> $b->{$oi->{'key'}}} @
$projlist;
5013 # returns a hash of categories, containing the list of project
5014 # belonging to each category
5015 sub build_projlist_by_category
{
5016 my ($projlist, $from, $to) = @_;
5019 $from = 0 unless defined $from;
5020 $to = $#$projlist if (!defined $to || $#$projlist < $to);
5022 for (my $i = $from; $i <= $to; $i++) {
5023 my $pr = $projlist->[$i];
5024 push @
{$categories{ $pr->{'category'} }}, $pr;
5027 return wantarray ?
%categories : \
%categories;
5030 # print 'sort by' <th> element, generating 'sort by $name' replay link
5031 # if that order is not selected
5033 print format_sort_th
(@_);
5036 sub format_sort_th
{
5037 my ($name, $order, $header) = @_;
5039 $header ||= ucfirst($name);
5041 if ($order eq $name) {
5042 $sort_th .= "<th>$header</th>\n";
5044 $sort_th .= "<th>" .
5045 $cgi->a({-href
=> href
(-replay
=>1, order
=>$name),
5046 -class => "header"}, $header) .
5053 sub git_project_list_rows
{
5054 my ($projlist, $from, $to, $check_forks) = @_;
5056 $from = 0 unless defined $from;
5057 $to = $#$projlist if (!defined $to || $#$projlist < $to);
5060 for (my $i = $from; $i <= $to; $i++) {
5061 my $pr = $projlist->[$i];
5064 print "<tr class=\"dark\">\n";
5066 print "<tr class=\"light\">\n";
5072 if ($pr->{'forks'}) {
5073 my $nforks = scalar @
{$pr->{'forks'}};
5075 print $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"forks"),
5076 -title
=> "$nforks forks"}, "+");
5078 print $cgi->span({-title
=> "$nforks forks"}, "+");
5083 print "<td>" . $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"summary"),
5084 -class => "list"}, esc_html
($pr->{'path'})) . "</td>\n" .
5085 "<td>" . $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"summary"),
5086 -class => "list", -title
=> $pr->{'descr_long'}},
5087 esc_html
($pr->{'descr'})) . "</td>\n" .
5088 "<td><i>" . chop_and_escape_str
($pr->{'owner'}, 15) . "</i></td>\n";
5089 print "<td class=\"". age_class
($pr->{'age'}) . "\">" .
5090 (defined $pr->{'age_string'} ?
$pr->{'age_string'} : "No commits") . "</td>\n" .
5091 "<td class=\"link\">" .
5092 $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"summary")}, "summary") . " | " .
5093 $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"shortlog")}, "shortlog") . " | " .
5094 $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"log")}, "log") . " | " .
5095 $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"tree")}, "tree") .
5096 ($pr->{'forks'} ?
" | " . $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"forks")}, "forks") : '') .
5102 sub git_project_list_body
{
5103 # actually uses global variable $project
5104 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
5105 my @projects = @
$projlist;
5107 my $check_forks = gitweb_check_feature
('forks');
5108 my $show_ctags = gitweb_check_feature
('ctags');
5109 my $tagfilter = $show_ctags ?
$cgi->param('by_tag') : undef;
5110 $check_forks = undef
5111 if ($tagfilter || $searchtext);
5113 # filtering out forks before filling info allows to do less work
5114 @projects = filter_forks_from_projects_list
(\
@projects)
5116 @projects = fill_project_list_info
(\
@projects);
5117 # searching projects require filling to be run before it
5118 @projects = search_projects_list
(\
@projects,
5119 'searchtext' => $searchtext,
5120 'tagfilter' => $tagfilter)
5121 if ($tagfilter || $searchtext);
5123 $order ||= $default_projects_order;
5124 $from = 0 unless defined $from;
5125 $to = $#projects if (!defined $to || $#projects < $to);
5130 "<b>No such projects found</b><br />\n".
5131 "Click ".$cgi->a({-href
=>href
(project
=>undef)},"here")." to view all projects<br />\n".
5132 "</center>\n<br />\n";
5136 @projects = sort_projects_list
(\
@projects, $order);
5139 my $ctags = git_gather_all_ctags
(\
@projects);
5140 my $cloud = git_populate_project_tagcloud
($ctags);
5141 print git_show_project_tagcloud
($cloud, 64);
5144 print "<table class=\"project_list\">\n";
5145 unless ($no_header) {
5148 print "<th></th>\n";
5150 print_sort_th
('project', $order, 'Project');
5151 print_sort_th
('descr', $order, 'Description');
5152 print_sort_th
('owner', $order, 'Owner');
5153 print_sort_th
('age', $order, 'Last Change');
5154 print "<th></th>\n" . # for links
5158 if ($projects_list_group_categories) {
5159 # only display categories with projects in the $from-$to window
5160 @projects = sort {$a->{'category'} cmp $b->{'category'}} @projects[$from..$to];
5161 my %categories = build_projlist_by_category
(\
@projects, $from, $to);
5162 foreach my $cat (sort keys %categories) {
5163 unless ($cat eq "") {
5166 print "<td></td>\n";
5168 print "<td class=\"category\" colspan=\"5\">".esc_html
($cat)."</td>\n";
5172 git_project_list_rows
($categories{$cat}, undef, undef, $check_forks);
5175 git_project_list_rows
(\
@projects, $from, $to, $check_forks);
5178 if (defined $extra) {
5181 print "<td></td>\n";
5183 print "<td colspan=\"5\">$extra</td>\n" .
5190 # uses global variable $project
5191 my ($commitlist, $from, $to, $refs, $extra) = @_;
5193 $from = 0 unless defined $from;
5194 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
5196 for (my $i = 0; $i <= $to; $i++) {
5197 my %co = %{$commitlist->[$i]};
5199 my $commit = $co{'id'};
5200 my $ref = format_ref_marker
($refs, $commit);
5201 git_print_header_div
('commit',
5202 "<span class=\"age\">$co{'age_string'}</span>" .
5203 esc_html
($co{'title'}) . $ref,
5205 print "<div class=\"title_text\">\n" .
5206 "<div class=\"log_link\">\n" .
5207 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$commit)}, "commit") .
5209 $cgi->a({-href
=> href
(action
=>"commitdiff", hash
=>$commit)}, "commitdiff") .
5211 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$commit, hash_base
=>$commit)}, "tree") .
5214 git_print_authorship
(\
%co, -tag
=> 'span');
5215 print "<br/>\n</div>\n";
5217 print "<div class=\"log_body\">\n";
5218 git_print_log
($co{'comment'}, -final_empty_line
=> 1);
5222 print "<div class=\"page_nav\">\n";
5228 sub git_shortlog_body
{
5229 # uses global variable $project
5230 my ($commitlist, $from, $to, $refs, $extra) = @_;
5232 $from = 0 unless defined $from;
5233 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
5235 print "<table class=\"shortlog\">\n";
5237 for (my $i = $from; $i <= $to; $i++) {
5238 my %co = %{$commitlist->[$i]};
5239 my $commit = $co{'id'};
5240 my $ref = format_ref_marker
($refs, $commit);
5242 print "<tr class=\"dark\">\n";
5244 print "<tr class=\"light\">\n";
5247 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
5248 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5249 format_author_html
('td', \
%co, 10) . "<td>";
5250 print format_subject_html
($co{'title'}, $co{'title_short'},
5251 href
(action
=>"commit", hash
=>$commit), $ref);
5253 "<td class=\"link\">" .
5254 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$commit)}, "commit") . " | " .
5255 $cgi->a({-href
=> href
(action
=>"commitdiff", hash
=>$commit)}, "commitdiff") . " | " .
5256 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$commit, hash_base
=>$commit)}, "tree");
5257 my $snapshot_links = format_snapshot_links
($commit);
5258 if (defined $snapshot_links) {
5259 print " | " . $snapshot_links;
5264 if (defined $extra) {
5266 "<td colspan=\"4\">$extra</td>\n" .
5272 sub git_history_body
{
5273 # Warning: assumes constant type (blob or tree) during history
5274 my ($commitlist, $from, $to, $refs, $extra,
5275 $file_name, $file_hash, $ftype) = @_;
5277 $from = 0 unless defined $from;
5278 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
5280 print "<table class=\"history\">\n";
5282 for (my $i = $from; $i <= $to; $i++) {
5283 my %co = %{$commitlist->[$i]};
5287 my $commit = $co{'id'};
5289 my $ref = format_ref_marker
($refs, $commit);
5292 print "<tr class=\"dark\">\n";
5294 print "<tr class=\"light\">\n";
5297 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5298 # shortlog: format_author_html('td', \%co, 10)
5299 format_author_html
('td', \
%co, 15, 3) . "<td>";
5300 # originally git_history used chop_str($co{'title'}, 50)
5301 print format_subject_html
($co{'title'}, $co{'title_short'},
5302 href
(action
=>"commit", hash
=>$commit), $ref);
5304 "<td class=\"link\">" .
5305 $cgi->a({-href
=> href
(action
=>$ftype, hash_base
=>$commit, file_name
=>$file_name)}, $ftype) . " | " .
5306 $cgi->a({-href
=> href
(action
=>"commitdiff", hash
=>$commit)}, "commitdiff");
5308 if ($ftype eq 'blob') {
5309 my $blob_current = $file_hash;
5310 my $blob_parent = git_get_hash_by_path
($commit, $file_name);
5311 if (defined $blob_current && defined $blob_parent &&
5312 $blob_current ne $blob_parent) {
5314 $cgi->a({-href
=> href
(action
=>"blobdiff",
5315 hash
=>$blob_current, hash_parent
=>$blob_parent,
5316 hash_base
=>$hash_base, hash_parent_base
=>$commit,
5317 file_name
=>$file_name)},
5324 if (defined $extra) {
5326 "<td colspan=\"4\">$extra</td>\n" .
5333 # uses global variable $project
5334 my ($taglist, $from, $to, $extra) = @_;
5335 $from = 0 unless defined $from;
5336 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
5338 print "<table class=\"tags\">\n";
5340 for (my $i = $from; $i <= $to; $i++) {
5341 my $entry = $taglist->[$i];
5343 my $comment = $tag{'subject'};
5345 if (defined $comment) {
5346 $comment_short = chop_str
($comment, 30, 5);
5349 print "<tr class=\"dark\">\n";
5351 print "<tr class=\"light\">\n";
5354 if (defined $tag{'age'}) {
5355 print "<td><i>$tag{'age'}</i></td>\n";
5357 print "<td></td>\n";
5360 $cgi->a({-href
=> href
(action
=>$tag{'reftype'}, hash
=>$tag{'refid'}),
5361 -class => "list name"}, esc_html
($tag{'name'})) .
5364 if (defined $comment) {
5365 print format_subject_html
($comment, $comment_short,
5366 href
(action
=>"tag", hash
=>$tag{'id'}));
5369 "<td class=\"selflink\">";
5370 if ($tag{'type'} eq "tag") {
5371 print $cgi->a({-href
=> href
(action
=>"tag", hash
=>$tag{'id'})}, "tag");
5376 "<td class=\"link\">" . " | " .
5377 $cgi->a({-href
=> href
(action
=>$tag{'reftype'}, hash
=>$tag{'refid'})}, $tag{'reftype'});
5378 if ($tag{'reftype'} eq "commit") {
5379 print " | " . $cgi->a({-href
=> href
(action
=>"shortlog", hash
=>$tag{'fullname'})}, "shortlog") .
5380 " | " . $cgi->a({-href
=> href
(action
=>"log", hash
=>$tag{'fullname'})}, "log");
5381 } elsif ($tag{'reftype'} eq "blob") {
5382 print " | " . $cgi->a({-href
=> href
(action
=>"blob_plain", hash
=>$tag{'refid'})}, "raw");
5387 if (defined $extra) {
5389 "<td colspan=\"5\">$extra</td>\n" .
5395 sub git_heads_body
{
5396 # uses global variable $project
5397 my ($headlist, $head, $from, $to, $extra) = @_;
5398 $from = 0 unless defined $from;
5399 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
5401 print "<table class=\"heads\">\n";
5403 for (my $i = $from; $i <= $to; $i++) {
5404 my $entry = $headlist->[$i];
5406 my $curr = $ref{'id'} eq $head;
5408 print "<tr class=\"dark\">\n";
5410 print "<tr class=\"light\">\n";
5413 print "<td><i>$ref{'age'}</i></td>\n" .
5414 ($curr ?
"<td class=\"current_head\">" : "<td>") .
5415 $cgi->a({-href
=> href
(action
=>"shortlog", hash
=>$ref{'fullname'}),
5416 -class => "list name"},esc_html
($ref{'name'})) .
5418 "<td class=\"link\">" .
5419 $cgi->a({-href
=> href
(action
=>"shortlog", hash
=>$ref{'fullname'})}, "shortlog") . " | " .
5420 $cgi->a({-href
=> href
(action
=>"log", hash
=>$ref{'fullname'})}, "log") . " | " .
5421 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$ref{'fullname'}, hash_base
=>$ref{'fullname'})}, "tree") .
5425 if (defined $extra) {
5427 "<td colspan=\"3\">$extra</td>\n" .
5433 # Display a single remote block
5434 sub git_remote_block
{
5435 my ($remote, $rdata, $limit, $head) = @_;
5437 my $heads = $rdata->{'heads'};
5438 my $fetch = $rdata->{'fetch'};
5439 my $push = $rdata->{'push'};
5441 my $urls_table = "<table class=\"projects_list\">\n" ;
5443 if (defined $fetch) {
5444 if ($fetch eq $push) {
5445 $urls_table .= format_repo_url
("URL", $fetch);
5447 $urls_table .= format_repo_url
("Fetch URL", $fetch);
5448 $urls_table .= format_repo_url
("Push URL", $push) if defined $push;
5450 } elsif (defined $push) {
5451 $urls_table .= format_repo_url
("Push URL", $push);
5453 $urls_table .= format_repo_url
("", "No remote URL");
5456 $urls_table .= "</table>\n";
5459 if (defined $limit && $limit < @
$heads) {
5460 $dots = $cgi->a({-href
=> href
(action
=>"remotes", hash
=>$remote)}, "...");
5464 git_heads_body
($heads, $head, 0, $limit, $dots);
5467 # Display a list of remote names with the respective fetch and push URLs
5468 sub git_remotes_list
{
5469 my ($remotedata, $limit) = @_;
5470 print "<table class=\"heads\">\n";
5472 my @remotes = sort keys %$remotedata;
5474 my $limited = $limit && $limit < @remotes;
5476 $#remotes = $limit - 1 if $limited;
5478 while (my $remote = shift @remotes) {
5479 my $rdata = $remotedata->{$remote};
5480 my $fetch = $rdata->{'fetch'};
5481 my $push = $rdata->{'push'};
5483 print "<tr class=\"dark\">\n";
5485 print "<tr class=\"light\">\n";
5489 $cgi->a({-href
=> href
(action
=>'remotes', hash
=>$remote),
5490 -class=> "list name"},esc_html
($remote)) .
5492 print "<td class=\"link\">" .
5493 (defined $fetch ?
$cgi->a({-href
=> $fetch}, "fetch") : "fetch") .
5495 (defined $push ?
$cgi->a({-href
=> $push}, "push") : "push") .
5503 "<td colspan=\"3\">" .
5504 $cgi->a({-href
=> href
(action
=>"remotes")}, "...") .
5505 "</td>\n" . "</tr>\n";
5511 # Display remote heads grouped by remote, unless there are too many
5512 # remotes, in which case we only display the remote names
5513 sub git_remotes_body
{
5514 my ($remotedata, $limit, $head) = @_;
5515 if ($limit and $limit < keys %$remotedata) {
5516 git_remotes_list
($remotedata, $limit);
5518 fill_remote_heads
($remotedata);
5519 while (my ($remote, $rdata) = each %$remotedata) {
5520 git_print_section
({-class=>"remote", -id
=>$remote},
5521 ["remotes", $remote, $remote], sub {
5522 git_remote_block
($remote, $rdata, $limit, $head);
5528 sub git_search_grep_body
{
5529 my ($commitlist, $from, $to, $extra) = @_;
5530 $from = 0 unless defined $from;
5531 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
5533 print "<table class=\"commit_search\">\n";
5535 for (my $i = $from; $i <= $to; $i++) {
5536 my %co = %{$commitlist->[$i]};
5540 my $commit = $co{'id'};
5542 print "<tr class=\"dark\">\n";
5544 print "<tr class=\"light\">\n";
5547 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5548 format_author_html
('td', \
%co, 15, 5) .
5550 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$co{'id'}),
5551 -class => "list subject"},
5552 chop_and_escape_str
($co{'title'}, 50) . "<br/>");
5553 my $comment = $co{'comment'};
5554 foreach my $line (@
$comment) {
5555 if ($line =~ m/^(.*?)($search_regexp)(.*)$/i) {
5556 my ($lead, $match, $trail) = ($1, $2, $3);
5557 $match = chop_str
($match, 70, 5, 'center');
5558 my $contextlen = int((80 - length($match))/2);
5559 $contextlen = 30 if ($contextlen > 30);
5560 $lead = chop_str
($lead, $contextlen, 10, 'left');
5561 $trail = chop_str
($trail, $contextlen, 10, 'right');
5563 $lead = esc_html
($lead);
5564 $match = esc_html
($match);
5565 $trail = esc_html
($trail);
5567 print "$lead<span class=\"match\">$match</span>$trail<br />";
5571 "<td class=\"link\">" .
5572 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$co{'id'})}, "commit") .
5574 $cgi->a({-href
=> href
(action
=>"commitdiff", hash
=>$co{'id'})}, "commitdiff") .
5576 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$co{'tree'}, hash_base
=>$co{'id'})}, "tree");
5580 if (defined $extra) {
5582 "<td colspan=\"3\">$extra</td>\n" .
5588 ## ======================================================================
5589 ## ======================================================================
5592 sub git_project_list
{
5593 my $order = $input_params{'order'};
5594 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
5595 die_error
(400, "Unknown order parameter");
5598 my @list = git_get_projects_list
();
5600 die_error
(404, "No projects found");
5604 if (defined $home_text && -f
$home_text) {
5605 print "<div class=\"index_include\">\n";
5606 insert_file
($home_text);
5609 print $cgi->startform(-method
=> "get") .
5610 "<p class=\"projsearch\">Search:\n" .
5611 $cgi->textfield(-name
=> "s", -value
=> $searchtext) . "\n" .
5613 $cgi->end_form() . "\n";
5614 git_project_list_body
(\
@list, $order);
5619 my $order = $input_params{'order'};
5620 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
5621 die_error
(400, "Unknown order parameter");
5624 my @list = git_get_projects_list
($project);
5626 die_error
(404, "No forks found");
5630 git_print_page_nav
('','');
5631 git_print_header_div
('summary', "$project forks");
5632 git_project_list_body
(\
@list, $order);
5636 sub git_project_index
{
5637 my @projects = git_get_projects_list
();
5639 die_error
(404, "No projects found");
5643 -type
=> 'text/plain',
5644 -charset
=> 'utf-8',
5645 -content_disposition
=> 'inline; filename="index.aux"');
5647 foreach my $pr (@projects) {
5648 if (!exists $pr->{'owner'}) {
5649 $pr->{'owner'} = git_get_project_owner
("$pr->{'path'}");
5652 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
5653 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
5654 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf
("%%%02X", ord($1))/eg
;
5655 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf
("%%%02X", ord($1))/eg
;
5659 print "$path $owner\n";
5664 my $descr = git_get_project_description
($project) || "none";
5665 my %co = parse_commit
("HEAD");
5666 my %cd = %co ? parse_date
($co{'committer_epoch'}, $co{'committer_tz'}) : ();
5667 my $head = $co{'id'};
5668 my $remote_heads = gitweb_check_feature
('remote_heads');
5670 my $owner = git_get_project_owner
($project);
5672 my $refs = git_get_references
();
5673 # These get_*_list functions return one more to allow us to see if
5674 # there are more ...
5675 my @taglist = git_get_tags_list
(16);
5676 my @headlist = git_get_heads_list
(16);
5677 my %remotedata = $remote_heads ? git_get_remotes_list
() : ();
5679 my $check_forks = gitweb_check_feature
('forks');
5682 # find forks of a project
5683 @forklist = git_get_projects_list
($project);
5684 # filter out forks of forks
5685 @forklist = filter_forks_from_projects_list
(\
@forklist)
5690 git_print_page_nav
('summary','', $head);
5692 print "<div class=\"title\"> </div>\n";
5693 print "<table class=\"projects_list\">\n" .
5694 "<tr id=\"metadata_desc\"><td>description</td><td>" . esc_html
($descr) . "</td></tr>\n" .
5695 "<tr id=\"metadata_owner\"><td>owner</td><td>" . esc_html
($owner) . "</td></tr>\n";
5696 if (defined $cd{'rfc2822'}) {
5697 print "<tr id=\"metadata_lchange\"><td>last change</td>" .
5698 "<td>".format_timestamp_html
(\
%cd)."</td></tr>\n";
5701 # use per project git URL list in $projectroot/$project/cloneurl
5702 # or make project git URL from git base URL and project name
5703 my $url_tag = "URL";
5704 my @url_list = git_get_project_url_list
($project);
5705 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
5706 foreach my $git_url (@url_list) {
5707 next unless $git_url;
5708 print format_repo_url
($url_tag, $git_url);
5713 my $show_ctags = gitweb_check_feature
('ctags');
5715 my $ctags = git_get_project_ctags
($project);
5717 # without ability to add tags, don't show if there are none
5718 my $cloud = git_populate_project_tagcloud
($ctags);
5719 print "<tr id=\"metadata_ctags\">" .
5720 "<td>content tags</td>" .
5721 "<td>".git_show_project_tagcloud
($cloud, 48)."</td>" .
5728 # If XSS prevention is on, we don't include README.html.
5729 # TODO: Allow a readme in some safe format.
5730 if (!$prevent_xss && -s
"$projectroot/$project/README.html") {
5731 print "<div class=\"title\">readme</div>\n" .
5732 "<div class=\"readme\">\n";
5733 insert_file
("$projectroot/$project/README.html");
5734 print "\n</div>\n"; # class="readme"
5737 # we need to request one more than 16 (0..15) to check if
5739 my @commitlist = $head ? parse_commits
($head, 17) : ();
5741 git_print_header_div
('shortlog');
5742 git_shortlog_body
(\
@commitlist, 0, 15, $refs,
5743 $#commitlist <= 15 ?
undef :
5744 $cgi->a({-href
=> href
(action
=>"shortlog")}, "..."));
5748 git_print_header_div
('tags');
5749 git_tags_body
(\
@taglist, 0, 15,
5750 $#taglist <= 15 ?
undef :
5751 $cgi->a({-href
=> href
(action
=>"tags")}, "..."));
5755 git_print_header_div
('heads');
5756 git_heads_body
(\
@headlist, $head, 0, 15,
5757 $#headlist <= 15 ?
undef :
5758 $cgi->a({-href
=> href
(action
=>"heads")}, "..."));
5762 git_print_header_div
('remotes');
5763 git_remotes_body
(\
%remotedata, 15, $head);
5767 git_print_header_div
('forks');
5768 git_project_list_body
(\
@forklist, 'age', 0, 15,
5769 $#forklist <= 15 ?
undef :
5770 $cgi->a({-href
=> href
(action
=>"forks")}, "..."),
5778 my %tag = parse_tag
($hash);
5781 die_error
(404, "Unknown tag object");
5784 my $head = git_get_head_hash
($project);
5786 git_print_page_nav
('','', $head,undef,$head);
5787 git_print_header_div
('commit', esc_html
($tag{'name'}), $hash);
5788 print "<div class=\"title_text\">\n" .
5789 "<table class=\"object_header\">\n" .
5791 "<td>object</td>\n" .
5792 "<td>" . $cgi->a({-class => "list", -href
=> href
(action
=>$tag{'type'}, hash
=>$tag{'object'})},
5793 $tag{'object'}) . "</td>\n" .
5794 "<td class=\"link\">" . $cgi->a({-href
=> href
(action
=>$tag{'type'}, hash
=>$tag{'object'})},
5795 $tag{'type'}) . "</td>\n" .
5797 if (defined($tag{'author'})) {
5798 git_print_authorship_rows
(\
%tag, 'author');
5800 print "</table>\n\n" .
5802 print "<div class=\"page_body\">";
5803 my $comment = $tag{'comment'};
5804 foreach my $line (@
$comment) {
5806 print esc_html
($line, -nbsp
=>1) . "<br/>\n";
5812 sub git_blame_common
{
5813 my $format = shift || 'porcelain';
5814 if ($format eq 'porcelain' && $cgi->param('js')) {
5815 $format = 'incremental';
5816 $action = 'blame_incremental'; # for page title etc
5820 gitweb_check_feature
('blame')
5821 or die_error
(403, "Blame view not allowed");
5824 die_error
(400, "No file name given") unless $file_name;
5825 $hash_base ||= git_get_head_hash
($project);
5826 die_error
(404, "Couldn't find base commit") unless $hash_base;
5827 my %co = parse_commit
($hash_base)
5828 or die_error
(404, "Commit not found");
5830 if (!defined $hash) {
5831 $hash = git_get_hash_by_path
($hash_base, $file_name, "blob")
5832 or die_error
(404, "Error looking up file");
5834 $ftype = git_get_type
($hash);
5835 if ($ftype !~ "blob") {
5836 die_error
(400, "Object is not a blob");
5841 if ($format eq 'incremental') {
5842 # get file contents (as base)
5843 open $fd, "-|", git_cmd
(), 'cat-file', 'blob', $hash
5844 or die_error
(500, "Open git-cat-file failed");
5845 } elsif ($format eq 'data') {
5846 # run git-blame --incremental
5847 open $fd, "-|", git_cmd
(), "blame", "--incremental",
5848 $hash_base, "--", $file_name
5849 or die_error
(500, "Open git-blame --incremental failed");
5851 # run git-blame --porcelain
5852 open $fd, "-|", git_cmd
(), "blame", '-p',
5853 $hash_base, '--', $file_name
5854 or die_error
(500, "Open git-blame --porcelain failed");
5857 # incremental blame data returns early
5858 if ($format eq 'data') {
5860 -type
=>"text/plain", -charset
=> "utf-8",
5861 -status
=> "200 OK");
5862 local $| = 1; # output autoflush
5865 or print "ERROR $!\n";
5868 if (defined $t0 && gitweb_check_feature
('timed')) {
5870 tv_interval
($t0, [ gettimeofday
() ]).
5871 ' '.$number_of_git_cmds;
5881 $cgi->a({-href
=> href
(action
=>"blob", -replay
=>1)},
5884 if ($format eq 'incremental') {
5886 $cgi->a({-href
=> href
(action
=>"blame", javascript
=>0, -replay
=>1)},
5887 "blame") . " (non-incremental)";
5890 $cgi->a({-href
=> href
(action
=>"blame_incremental", -replay
=>1)},
5891 "blame") . " (incremental)";
5895 $cgi->a({-href
=> href
(action
=>"history", -replay
=>1)},
5898 $cgi->a({-href
=> href
(action
=>$action, file_name
=>$file_name)},
5900 git_print_page_nav
('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
5901 git_print_header_div
('commit', esc_html
($co{'title'}), $hash_base);
5902 git_print_page_path
($file_name, $ftype, $hash_base);
5905 if ($format eq 'incremental') {
5906 print "<noscript>\n<div class=\"error\"><center><b>\n".
5907 "This page requires JavaScript to run.\n Use ".
5908 $cgi->a({-href
=> href
(action
=>'blame',javascript
=>0,-replay
=>1)},
5911 "</b></center></div>\n</noscript>\n";
5913 print qq!<div id
="progress_bar" style
="width: 100%; background-color: yellow"></div
>\n!;
5916 print qq!<div
class="page_body">\n!;
5917 print qq!<div id
="progress_info">... / ...</div
>\n!
5918 if ($format eq 'incremental');
5919 print qq!<table id
="blame_table" class="blame" width
="100%">\n!.
5920 #qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.
5922 qq!<tr
><th
>Commit
</th><th>Line</th
><th
>Data
</th></tr
>\n!.
5926 my @rev_color = qw(light dark);
5927 my $num_colors = scalar(@rev_color);
5928 my $current_color = 0;
5930 if ($format eq 'incremental') {
5931 my $color_class = $rev_color[$current_color];
5936 while (my $line = <$fd>) {
5940 print qq!<tr id
="l$linenr" class="$color_class">!.
5941 qq!<td
class="sha1"><a href
=""> </a></td
>!.
5942 qq!<td
class="linenr">!.
5943 qq!<a
class="linenr" href
="">$linenr</a></td
>!;
5944 print qq!<td
class="pre">! . esc_html
($line) . "</td>\n";
5948 } else { # porcelain, i.e. ordinary blame
5949 my %metainfo = (); # saves information about commits
5953 while (my $line = <$fd>) {
5955 # the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]
5956 # no <lines in group> for subsequent lines in group of lines
5957 my ($full_rev, $orig_lineno, $lineno, $group_size) =
5958 ($line =~ /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);
5959 if (!exists $metainfo{$full_rev}) {
5960 $metainfo{$full_rev} = { 'nprevious' => 0 };
5962 my $meta = $metainfo{$full_rev};
5964 while ($data = <$fd>) {
5966 last if ($data =~ s/^\t//); # contents of line
5967 if ($data =~ /^(\S+)(?: (.*))?$/) {
5968 $meta->{$1} = $2 unless exists $meta->{$1};
5970 if ($data =~ /^previous /) {
5971 $meta->{'nprevious'}++;
5974 my $short_rev = substr($full_rev, 0, 8);
5975 my $author = $meta->{'author'};
5977 parse_date
($meta->{'author-time'}, $meta->{'author-tz'});
5978 my $date = $date{'iso-tz'};
5980 $current_color = ($current_color + 1) % $num_colors;
5982 my $tr_class = $rev_color[$current_color];
5983 $tr_class .= ' boundary' if (exists $meta->{'boundary'});
5984 $tr_class .= ' no-previous' if ($meta->{'nprevious'} == 0);
5985 $tr_class .= ' multiple-previous' if ($meta->{'nprevious'} > 1);
5986 print "<tr id=\"l$lineno\" class=\"$tr_class\">\n";
5988 print "<td class=\"sha1\"";
5989 print " title=\"". esc_html
($author) . ", $date\"";
5990 print " rowspan=\"$group_size\"" if ($group_size > 1);
5992 print $cgi->a({-href
=> href
(action
=>"commit",
5994 file_name
=>$file_name)},
5995 esc_html
($short_rev));
5996 if ($group_size >= 2) {
5997 my @author_initials = ($author =~ /\b([[:upper:]])\B/g);
5998 if (@author_initials) {
6000 esc_html
(join('', @author_initials));
6006 # 'previous' <sha1 of parent commit> <filename at commit>
6007 if (exists $meta->{'previous'} &&
6008 $meta->{'previous'} =~ /^([a-fA-F0-9]{40}) (.*)$/) {
6009 $meta->{'parent'} = $1;
6010 $meta->{'file_parent'} = unquote
($2);
6013 exists($meta->{'parent'}) ?
6014 $meta->{'parent'} : $full_rev;
6015 my $linenr_filename =
6016 exists($meta->{'file_parent'}) ?
6017 $meta->{'file_parent'} : unquote
($meta->{'filename'});
6018 my $blamed = href
(action
=> 'blame',
6019 file_name
=> $linenr_filename,
6020 hash_base
=> $linenr_commit);
6021 print "<td class=\"linenr\">";
6022 print $cgi->a({ -href
=> "$blamed#l$orig_lineno",
6023 -class => "linenr" },
6026 print "<td class=\"pre\">" . esc_html
($data) . "</td>\n";
6034 "</table>\n"; # class="blame"
6035 print "</div>\n"; # class="blame_body"
6037 or print "Reading blob failed\n";
6046 sub git_blame_incremental
{
6047 git_blame_common
('incremental');
6050 sub git_blame_data
{
6051 git_blame_common
('data');
6055 my $head = git_get_head_hash
($project);
6057 git_print_page_nav
('','', $head,undef,$head,format_ref_views
('tags'));
6058 git_print_header_div
('summary', $project);
6060 my @tagslist = git_get_tags_list
();
6062 git_tags_body
(\
@tagslist);
6068 my $head = git_get_head_hash
($project);
6070 git_print_page_nav
('','', $head,undef,$head,format_ref_views
('heads'));
6071 git_print_header_div
('summary', $project);
6073 my @headslist = git_get_heads_list
();
6075 git_heads_body
(\
@headslist, $head);
6080 # used both for single remote view and for list of all the remotes
6082 gitweb_check_feature
('remote_heads')
6083 or die_error
(403, "Remote heads view is disabled");
6085 my $head = git_get_head_hash
($project);
6086 my $remote = $input_params{'hash'};
6088 my $remotedata = git_get_remotes_list
($remote);
6089 die_error
(500, "Unable to get remote information") unless defined $remotedata;
6091 unless (%$remotedata) {
6092 die_error
(404, defined $remote ?
6093 "Remote $remote not found" :
6094 "No remotes found");
6097 git_header_html
(undef, undef, -action_extra
=> $remote);
6098 git_print_page_nav
('', '', $head, undef, $head,
6099 format_ref_views
($remote ?
'' : 'remotes'));
6101 fill_remote_heads
($remotedata);
6102 if (defined $remote) {
6103 git_print_header_div
('remotes', "$remote remote for $project");
6104 git_remote_block
($remote, $remotedata->{$remote}, undef, $head);
6106 git_print_header_div
('summary', "$project remotes");
6107 git_remotes_body
($remotedata, undef, $head);
6113 sub git_blob_plain
{
6117 if (!defined $hash) {
6118 if (defined $file_name) {
6119 my $base = $hash_base || git_get_head_hash
($project);
6120 $hash = git_get_hash_by_path
($base, $file_name, "blob")
6121 or die_error
(404, "Cannot find file");
6123 die_error
(400, "No file name defined");
6125 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6126 # blobs defined by non-textual hash id's can be cached
6130 open my $fd, "-|", git_cmd
(), "cat-file", "blob", $hash
6131 or die_error
(500, "Open git-cat-file blob '$hash' failed");
6133 # content-type (can include charset)
6134 $type = blob_contenttype
($fd, $file_name, $type);
6136 # "save as" filename, even when no $file_name is given
6137 my $save_as = "$hash";
6138 if (defined $file_name) {
6139 $save_as = $file_name;
6140 } elsif ($type =~ m/^text\//) {
6144 # With XSS prevention on, blobs of all types except a few known safe
6145 # ones are served with "Content-Disposition: attachment" to make sure
6146 # they don't run in our security domain. For certain image types,
6147 # blob view writes an <img> tag referring to blob_plain view, and we
6148 # want to be sure not to break that by serving the image as an
6149 # attachment (though Firefox 3 doesn't seem to care).
6150 my $sandbox = $prevent_xss &&
6151 $type !~ m!^(?:text/plain|image/(?:gif|png|jpeg))$!;
6155 -expires
=> $expires,
6156 -content_disposition
=>
6157 ($sandbox ?
'attachment' : 'inline')
6158 . '; filename="' . $save_as . '"');
6160 binmode STDOUT
, ':raw';
6162 binmode STDOUT
, ':utf8'; # as set at the beginning of gitweb.cgi
6169 if (!defined $hash) {
6170 if (defined $file_name) {
6171 my $base = $hash_base || git_get_head_hash
($project);
6172 $hash = git_get_hash_by_path
($base, $file_name, "blob")
6173 or die_error
(404, "Cannot find file");
6175 die_error
(400, "No file name defined");
6177 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6178 # blobs defined by non-textual hash id's can be cached
6182 my $have_blame = gitweb_check_feature
('blame');
6183 open my $fd, "-|", git_cmd
(), "cat-file", "blob", $hash
6184 or die_error
(500, "Couldn't cat $file_name, $hash");
6185 my $mimetype = blob_mimetype
($fd, $file_name);
6186 # use 'blob_plain' (aka 'raw') view for files that cannot be displayed
6187 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B
$fd) {
6189 return git_blob_plain
($mimetype);
6191 # we can have blame only for text/* mimetype
6192 $have_blame &&= ($mimetype =~ m!^text/!);
6194 my $highlight = gitweb_check_feature
('highlight');
6195 my $syntax = guess_file_syntax
($highlight, $mimetype, $file_name);
6196 $fd = run_highlighter
($fd, $highlight, $syntax)
6199 git_header_html
(undef, $expires);
6200 my $formats_nav = '';
6201 if (defined $hash_base && (my %co = parse_commit
($hash_base))) {
6202 if (defined $file_name) {
6205 $cgi->a({-href
=> href
(action
=>"blame", -replay
=>1)},
6210 $cgi->a({-href
=> href
(action
=>"history", -replay
=>1)},
6213 $cgi->a({-href
=> href
(action
=>"blob_plain", -replay
=>1)},
6216 $cgi->a({-href
=> href
(action
=>"blob",
6217 hash_base
=>"HEAD", file_name
=>$file_name)},
6221 $cgi->a({-href
=> href
(action
=>"blob_plain", -replay
=>1)},
6224 git_print_page_nav
('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
6225 git_print_header_div
('commit', esc_html
($co{'title'}), $hash_base);
6227 print "<div class=\"page_nav\">\n" .
6228 "<br/><br/></div>\n" .
6229 "<div class=\"title\">".esc_html
($hash)."</div>\n";
6231 git_print_page_path
($file_name, "blob", $hash_base);
6232 print "<div class=\"page_body\">\n";
6233 if ($mimetype =~ m!^image/!) {
6234 print qq!<img type
="!.esc_attr($mimetype).qq!"!;
6236 print qq! alt
="!.esc_attr($file_name).qq!" title
="!.esc_attr($file_name).qq!"!;
6239 href(action=>"blob_plain
", hash=>$hash,
6240 hash_base=>$hash_base, file_name=>$file_name) .
6244 while (my $line = <$fd>) {
6247 $line = untabify
($line);
6248 printf qq!<div
class="pre"><a id
="l%i" href
="%s#l%i" class="linenr">%4i</a> %s</div
>\n!,
6249 $nr, esc_attr
(href
(-replay
=> 1)), $nr, $nr, $syntax ?
$line : esc_html
($line, -nbsp
=>1);
6253 or print "Reading blob failed.\n";
6259 if (!defined $hash_base) {
6260 $hash_base = "HEAD";
6262 if (!defined $hash) {
6263 if (defined $file_name) {
6264 $hash = git_get_hash_by_path
($hash_base, $file_name, "tree");
6269 die_error
(404, "No such tree") unless defined($hash);
6271 my $show_sizes = gitweb_check_feature
('show-sizes');
6272 my $have_blame = gitweb_check_feature
('blame');
6277 open my $fd, "-|", git_cmd
(), "ls-tree", '-z',
6278 ($show_sizes ?
'-l' : ()), @extra_options, $hash
6279 or die_error
(500, "Open git-ls-tree failed");
6280 @entries = map { chomp; $_ } <$fd>;
6282 or die_error
(404, "Reading tree failed");
6285 my $refs = git_get_references
();
6286 my $ref = format_ref_marker
($refs, $hash_base);
6289 if (defined $hash_base && (my %co = parse_commit
($hash_base))) {
6291 if (defined $file_name) {
6293 $cgi->a({-href
=> href
(action
=>"history", -replay
=>1)},
6295 $cgi->a({-href
=> href
(action
=>"tree",
6296 hash_base
=>"HEAD", file_name
=>$file_name)},
6299 my $snapshot_links = format_snapshot_links
($hash);
6300 if (defined $snapshot_links) {
6301 # FIXME: Should be available when we have no hash base as well.
6302 push @views_nav, $snapshot_links;
6304 git_print_page_nav
('tree','', $hash_base, undef, undef,
6305 join(' | ', @views_nav));
6306 git_print_header_div
('commit', esc_html
($co{'title'}) . $ref, $hash_base);
6309 print "<div class=\"page_nav\">\n";
6310 print "<br/><br/></div>\n";
6311 print "<div class=\"title\">".esc_html
($hash)."</div>\n";
6313 if (defined $file_name) {
6314 $basedir = $file_name;
6315 if ($basedir ne '' && substr($basedir, -1) ne '/') {
6318 git_print_page_path
($file_name, 'tree', $hash_base);
6320 print "<div class=\"page_body\">\n";
6321 print "<table class=\"tree\">\n";
6323 # '..' (top directory) link if possible
6324 if (defined $hash_base &&
6325 defined $file_name && $file_name =~ m![^/]+$!) {
6327 print "<tr class=\"dark\">\n";
6329 print "<tr class=\"light\">\n";
6333 my $up = $file_name;
6334 $up =~ s!/?[^/]+$!!;
6335 undef $up unless $up;
6336 # based on git_print_tree_entry
6337 print '<td class="mode">' . mode_str
('040000') . "</td>\n";
6338 print '<td class="size"> </td>'."\n" if $show_sizes;
6339 print '<td class="list">';
6340 print $cgi->a({-href
=> href
(action
=>"tree",
6341 hash_base
=>$hash_base,
6345 print "<td class=\"link\"></td>\n";
6349 foreach my $line (@entries) {
6350 my %t = parse_ls_tree_line
($line, -z
=> 1, -l
=> $show_sizes);
6353 print "<tr class=\"dark\">\n";
6355 print "<tr class=\"light\">\n";
6359 git_print_tree_entry
(\
%t, $basedir, $hash_base, $have_blame);
6363 print "</table>\n" .
6369 my ($project, $hash) = @_;
6371 # path/to/project.git -> project
6372 # path/to/project/.git -> project
6373 my $name = to_utf8
($project);
6374 $name =~ s
,([^/])/*\
.git
$,$1,;
6375 $name = basename
($name);
6377 $name =~ s/[[:cntrl:]]/?/g;
6380 if ($hash =~ /^[0-9a-fA-F]+$/) {
6381 # shorten SHA-1 hash
6382 my $full_hash = git_get_full_hash
($project, $hash);
6383 if ($full_hash =~ /^$hash/ && length($hash) > 7) {
6384 $ver = git_get_short_hash
($project, $hash);
6386 } elsif ($hash =~ m!^refs/tags/(.*)$!) {
6387 # tags don't need shortened SHA-1 hash
6390 # branches and other need shortened SHA-1 hash
6391 if ($hash =~ m!^refs/(?:heads|remotes)/(.*)$!) {
6394 $ver .= '-' . git_get_short_hash
($project, $hash);
6396 # in case of hierarchical branch names
6399 # name = project-version_string
6400 $name = "$name-$ver";
6402 return wantarray ?
($name, $name) : $name;
6406 my $format = $input_params{'snapshot_format'};
6407 if (!@snapshot_fmts) {
6408 die_error
(403, "Snapshots not allowed");
6410 # default to first supported snapshot format
6411 $format ||= $snapshot_fmts[0];
6412 if ($format !~ m/^[a-z0-9]+$/) {
6413 die_error
(400, "Invalid snapshot format parameter");
6414 } elsif (!exists($known_snapshot_formats{$format})) {
6415 die_error
(400, "Unknown snapshot format");
6416 } elsif ($known_snapshot_formats{$format}{'disabled'}) {
6417 die_error
(403, "Snapshot format not allowed");
6418 } elsif (!grep($_ eq $format, @snapshot_fmts)) {
6419 die_error
(403, "Unsupported snapshot format");
6422 my $type = git_get_type
("$hash^{}");
6424 die_error
(404, 'Object does not exist');
6425 } elsif ($type eq 'blob') {
6426 die_error
(400, 'Object is not a tree-ish');
6429 my ($name, $prefix) = snapshot_name
($project, $hash);
6430 my $filename = "$name$known_snapshot_formats{$format}{'suffix'}";
6431 my $cmd = quote_command
(
6432 git_cmd
(), 'archive',
6433 "--format=$known_snapshot_formats{$format}{'format'}",
6434 "--prefix=$prefix/", $hash);
6435 if (exists $known_snapshot_formats{$format}{'compressor'}) {
6436 $cmd .= ' | ' . quote_command
(@
{$known_snapshot_formats{$format}{'compressor'}});
6439 $filename =~ s/(["\\])/\\$1/g;
6441 -type
=> $known_snapshot_formats{$format}{'type'},
6442 -content_disposition
=> 'inline; filename="' . $filename . '"',
6443 -status
=> '200 OK');
6445 open my $fd, "-|", $cmd
6446 or die_error
(500, "Execute git-archive failed");
6447 binmode STDOUT
, ':raw';
6449 binmode STDOUT
, ':utf8'; # as set at the beginning of gitweb.cgi
6453 sub git_log_generic
{
6454 my ($fmt_name, $body_subr, $base, $parent, $file_name, $file_hash) = @_;
6456 my $head = git_get_head_hash
($project);
6457 if (!defined $base) {
6460 if (!defined $page) {
6463 my $refs = git_get_references
();
6465 my $commit_hash = $base;
6466 if (defined $parent) {
6467 $commit_hash = "$parent..$base";
6470 parse_commits
($commit_hash, 101, (100 * $page),
6471 defined $file_name ?
($file_name, "--full-history") : ());
6474 if (!defined $file_hash && defined $file_name) {
6475 # some commits could have deleted file in question,
6476 # and not have it in tree, but one of them has to have it
6477 for (my $i = 0; $i < @commitlist; $i++) {
6478 $file_hash = git_get_hash_by_path
($commitlist[$i]{'id'}, $file_name);
6479 last if defined $file_hash;
6482 if (defined $file_hash) {
6483 $ftype = git_get_type
($file_hash);
6485 if (defined $file_name && !defined $ftype) {
6486 die_error
(500, "Unknown type of object");
6489 if (defined $file_name) {
6490 %co = parse_commit
($base)
6491 or die_error
(404, "Unknown commit object");
6495 my $paging_nav = format_paging_nav
($fmt_name, $page, $#commitlist >= 100);
6497 if ($#commitlist >= 100) {
6499 $cgi->a({-href
=> href
(-replay
=>1, page
=>$page+1),
6500 -accesskey
=> "n", -title
=> "Alt-n"}, "next");
6502 my $patch_max = gitweb_get_feature
('patches');
6503 if ($patch_max && !defined $file_name) {
6504 if ($patch_max < 0 || @commitlist <= $patch_max) {
6505 $paging_nav .= " ⋅ " .
6506 $cgi->a({-href
=> href
(action
=>"patches", -replay
=>1)},
6512 git_print_page_nav
($fmt_name,'', $hash,$hash,$hash, $paging_nav);
6513 if (defined $file_name) {
6514 git_print_header_div
('commit', esc_html
($co{'title'}), $base);
6516 git_print_header_div
('summary', $project)
6518 git_print_page_path
($file_name, $ftype, $hash_base)
6519 if (defined $file_name);
6521 $body_subr->(\
@commitlist, 0, 99, $refs, $next_link,
6522 $file_name, $file_hash, $ftype);
6528 git_log_generic
('log', \
&git_log_body
,
6529 $hash, $hash_parent);
6533 $hash ||= $hash_base || "HEAD";
6534 my %co = parse_commit
($hash)
6535 or die_error
(404, "Unknown commit object");
6537 my $parent = $co{'parent'};
6538 my $parents = $co{'parents'}; # listref
6540 # we need to prepare $formats_nav before any parameter munging
6542 if (!defined $parent) {
6544 $formats_nav .= '(initial)';
6545 } elsif (@
$parents == 1) {
6546 # single parent commit
6549 $cgi->a({-href
=> href
(action
=>"commit",
6551 esc_html
(substr($parent, 0, 7))) .
6558 $cgi->a({-href
=> href
(action
=>"commit",
6560 esc_html
(substr($_, 0, 7)));
6564 if (gitweb_check_feature
('patches') && @
$parents <= 1) {
6565 $formats_nav .= " | " .
6566 $cgi->a({-href
=> href
(action
=>"patch", -replay
=>1)},
6570 if (!defined $parent) {
6574 open my $fd, "-|", git_cmd
(), "diff-tree", '-r', "--no-commit-id",
6576 (@
$parents <= 1 ?
$parent : '-c'),
6578 or die_error
(500, "Open git-diff-tree failed");
6579 @difftree = map { chomp; $_ } <$fd>;
6580 close $fd or die_error
(404, "Reading git-diff-tree failed");
6582 # non-textual hash id's can be cached
6584 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6587 my $refs = git_get_references
();
6588 my $ref = format_ref_marker
($refs, $co{'id'});
6590 git_header_html
(undef, $expires);
6591 git_print_page_nav
('commit', '',
6592 $hash, $co{'tree'}, $hash,
6595 if (defined $co{'parent'}) {
6596 git_print_header_div
('commitdiff', esc_html
($co{'title'}) . $ref, $hash);
6598 git_print_header_div
('tree', esc_html
($co{'title'}) . $ref, $co{'tree'}, $hash);
6600 print "<div class=\"title_text\">\n" .
6601 "<table class=\"object_header\">\n";
6602 git_print_authorship_rows
(\
%co);
6603 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
6606 "<td class=\"sha1\">" .
6607 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$co{'tree'}, hash_base
=>$hash),
6608 class => "list"}, $co{'tree'}) .
6610 "<td class=\"link\">" .
6611 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$co{'tree'}, hash_base
=>$hash)},
6613 my $snapshot_links = format_snapshot_links
($hash);
6614 if (defined $snapshot_links) {
6615 print " | " . $snapshot_links;
6620 foreach my $par (@
$parents) {
6623 "<td class=\"sha1\">" .
6624 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$par),
6625 class => "list"}, $par) .
6627 "<td class=\"link\">" .
6628 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$par)}, "commit") .
6630 $cgi->a({-href
=> href
(action
=>"commitdiff", hash
=>$hash, hash_parent
=>$par)}, "diff") .
6637 print "<div class=\"page_body\">\n";
6638 git_print_log
($co{'comment'});
6641 git_difftree_body
(\
@difftree, $hash, @
$parents);
6647 # object is defined by:
6648 # - hash or hash_base alone
6649 # - hash_base and file_name
6652 # - hash or hash_base alone
6653 if ($hash || ($hash_base && !defined $file_name)) {
6654 my $object_id = $hash || $hash_base;
6656 open my $fd, "-|", quote_command
(
6657 git_cmd
(), 'cat-file', '-t', $object_id) . ' 2> /dev/null'
6658 or die_error
(404, "Object does not exist");
6662 or die_error
(404, "Object does not exist");
6664 # - hash_base and file_name
6665 } elsif ($hash_base && defined $file_name) {
6666 $file_name =~ s
,/+$,,;
6668 system(git_cmd
(), "cat-file", '-e', $hash_base) == 0
6669 or die_error
(404, "Base object does not exist");
6671 # here errors should not hapen
6672 open my $fd, "-|", git_cmd
(), "ls-tree", $hash_base, "--", $file_name
6673 or die_error
(500, "Open git-ls-tree failed");
6677 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
6678 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
6679 die_error
(404, "File or directory for given base does not exist");
6684 die_error
(400, "Not enough information to find object");
6687 print $cgi->redirect(-uri
=> href
(action
=>$type, -full
=>1,
6688 hash
=>$hash, hash_base
=>$hash_base,
6689 file_name
=>$file_name),
6690 -status
=> '302 Found');
6694 my $format = shift || 'html';
6701 # preparing $fd and %diffinfo for git_patchset_body
6703 if (defined $hash_base && defined $hash_parent_base) {
6704 if (defined $file_name) {
6706 open $fd, "-|", git_cmd
(), "diff-tree", '-r', @diff_opts,
6707 $hash_parent_base, $hash_base,
6708 "--", (defined $file_parent ?
$file_parent : ()), $file_name
6709 or die_error
(500, "Open git-diff-tree failed");
6710 @difftree = map { chomp; $_ } <$fd>;
6712 or die_error
(404, "Reading git-diff-tree failed");
6714 or die_error
(404, "Blob diff not found");
6716 } elsif (defined $hash &&
6717 $hash =~ /[0-9a-fA-F]{40}/) {
6718 # try to find filename from $hash
6720 # read filtered raw output
6721 open $fd, "-|", git_cmd
(), "diff-tree", '-r', @diff_opts,
6722 $hash_parent_base, $hash_base, "--"
6723 or die_error
(500, "Open git-diff-tree failed");
6725 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
6727 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
6728 map { chomp; $_ } <$fd>;
6730 or die_error
(404, "Reading git-diff-tree failed");
6732 or die_error
(404, "Blob diff not found");
6735 die_error
(400, "Missing one of the blob diff parameters");
6738 if (@difftree > 1) {
6739 die_error
(400, "Ambiguous blob diff specification");
6742 %diffinfo = parse_difftree_raw_line
($difftree[0]);
6743 $file_parent ||= $diffinfo{'from_file'} || $file_name;
6744 $file_name ||= $diffinfo{'to_file'};
6746 $hash_parent ||= $diffinfo{'from_id'};
6747 $hash ||= $diffinfo{'to_id'};
6749 # non-textual hash id's can be cached
6750 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
6751 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
6756 open $fd, "-|", git_cmd
(), "diff-tree", '-r', @diff_opts,
6757 '-p', ($format eq 'html' ?
"--full-index" : ()),
6758 $hash_parent_base, $hash_base,
6759 "--", (defined $file_parent ?
$file_parent : ()), $file_name
6760 or die_error
(500, "Open git-diff-tree failed");
6763 # old/legacy style URI -- not generated anymore since 1.4.3.
6765 die_error
('404 Not Found', "Missing one of the blob diff parameters")
6769 if ($format eq 'html') {
6771 $cgi->a({-href
=> href
(action
=>"blobdiff_plain", -replay
=>1)},
6773 git_header_html
(undef, $expires);
6774 if (defined $hash_base && (my %co = parse_commit
($hash_base))) {
6775 git_print_page_nav
('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
6776 git_print_header_div
('commit', esc_html
($co{'title'}), $hash_base);
6778 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
6779 print "<div class=\"title\">".esc_html
("$hash vs $hash_parent")."</div>\n";
6781 if (defined $file_name) {
6782 git_print_page_path
($file_name, "blob", $hash_base);
6784 print "<div class=\"page_path\"></div>\n";
6787 } elsif ($format eq 'plain') {
6789 -type
=> 'text/plain',
6790 -charset
=> 'utf-8',
6791 -expires
=> $expires,
6792 -content_disposition
=> 'inline; filename="' . "$file_name" . '.patch"');
6794 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
6797 die_error
(400, "Unknown blobdiff format");
6801 if ($format eq 'html') {
6802 print "<div class=\"page_body\">\n";
6804 git_patchset_body
($fd, [ \
%diffinfo ], $hash_base, $hash_parent_base);
6807 print "</div>\n"; # class="page_body"
6811 while (my $line = <$fd>) {
6812 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
6813 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
6817 last if $line =~ m!^\+\+\+!;
6825 sub git_blobdiff_plain
{
6826 git_blobdiff
('plain');
6829 sub git_commitdiff
{
6831 my $format = $params{-format
} || 'html';
6833 my ($patch_max) = gitweb_get_feature
('patches');
6834 if ($format eq 'patch') {
6835 die_error
(403, "Patch view not allowed") unless $patch_max;
6838 $hash ||= $hash_base || "HEAD";
6839 my %co = parse_commit
($hash)
6840 or die_error
(404, "Unknown commit object");
6842 # choose format for commitdiff for merge
6843 if (! defined $hash_parent && @
{$co{'parents'}} > 1) {
6844 $hash_parent = '--cc';
6846 # we need to prepare $formats_nav before almost any parameter munging
6848 if ($format eq 'html') {
6850 $cgi->a({-href
=> href
(action
=>"commitdiff_plain", -replay
=>1)},
6852 if ($patch_max && @
{$co{'parents'}} <= 1) {
6853 $formats_nav .= " | " .
6854 $cgi->a({-href
=> href
(action
=>"patch", -replay
=>1)},
6858 if (defined $hash_parent &&
6859 $hash_parent ne '-c' && $hash_parent ne '--cc') {
6860 # commitdiff with two commits given
6861 my $hash_parent_short = $hash_parent;
6862 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
6863 $hash_parent_short = substr($hash_parent, 0, 7);
6867 for (my $i = 0; $i < @
{$co{'parents'}}; $i++) {
6868 if ($co{'parents'}[$i] eq $hash_parent) {
6869 $formats_nav .= ' parent ' . ($i+1);
6873 $formats_nav .= ': ' .
6874 $cgi->a({-href
=> href
(action
=>"commitdiff",
6875 hash
=>$hash_parent)},
6876 esc_html
($hash_parent_short)) .
6878 } elsif (!$co{'parent'}) {
6880 $formats_nav .= ' (initial)';
6881 } elsif (scalar @
{$co{'parents'}} == 1) {
6882 # single parent commit
6885 $cgi->a({-href
=> href
(action
=>"commitdiff",
6886 hash
=>$co{'parent'})},
6887 esc_html
(substr($co{'parent'}, 0, 7))) .
6891 if ($hash_parent eq '--cc') {
6892 $formats_nav .= ' | ' .
6893 $cgi->a({-href
=> href
(action
=>"commitdiff",
6894 hash
=>$hash, hash_parent
=>'-c')},
6896 } else { # $hash_parent eq '-c'
6897 $formats_nav .= ' | ' .
6898 $cgi->a({-href
=> href
(action
=>"commitdiff",
6899 hash
=>$hash, hash_parent
=>'--cc')},
6905 $cgi->a({-href
=> href
(action
=>"commitdiff",
6907 esc_html
(substr($_, 0, 7)));
6908 } @
{$co{'parents'}} ) .
6913 my $hash_parent_param = $hash_parent;
6914 if (!defined $hash_parent_param) {
6915 # --cc for multiple parents, --root for parentless
6916 $hash_parent_param =
6917 @
{$co{'parents'}} > 1 ?
'--cc' : $co{'parent'} || '--root';
6923 if ($format eq 'html') {
6924 open $fd, "-|", git_cmd
(), "diff-tree", '-r', @diff_opts,
6925 "--no-commit-id", "--patch-with-raw", "--full-index",
6926 $hash_parent_param, $hash, "--"
6927 or die_error
(500, "Open git-diff-tree failed");
6929 while (my $line = <$fd>) {
6931 # empty line ends raw part of diff-tree output
6933 push @difftree, scalar parse_difftree_raw_line
($line);
6936 } elsif ($format eq 'plain') {
6937 open $fd, "-|", git_cmd
(), "diff-tree", '-r', @diff_opts,
6938 '-p', $hash_parent_param, $hash, "--"
6939 or die_error
(500, "Open git-diff-tree failed");
6940 } elsif ($format eq 'patch') {
6941 # For commit ranges, we limit the output to the number of
6942 # patches specified in the 'patches' feature.
6943 # For single commits, we limit the output to a single patch,
6944 # diverging from the git-format-patch default.
6945 my @commit_spec = ();
6947 if ($patch_max > 0) {
6948 push @commit_spec, "-$patch_max";
6950 push @commit_spec, '-n', "$hash_parent..$hash";
6952 if ($params{-single
}) {
6953 push @commit_spec, '-1';
6955 if ($patch_max > 0) {
6956 push @commit_spec, "-$patch_max";
6958 push @commit_spec, "-n";
6960 push @commit_spec, '--root', $hash;
6962 open $fd, "-|", git_cmd
(), "format-patch", @diff_opts,
6963 '--encoding=utf8', '--stdout', @commit_spec
6964 or die_error
(500, "Open git-format-patch failed");
6966 die_error
(400, "Unknown commitdiff format");
6969 # non-textual hash id's can be cached
6971 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6975 # write commit message
6976 if ($format eq 'html') {
6977 my $refs = git_get_references
();
6978 my $ref = format_ref_marker
($refs, $co{'id'});
6980 git_header_html
(undef, $expires);
6981 git_print_page_nav
('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
6982 git_print_header_div
('commit', esc_html
($co{'title'}) . $ref, $hash);
6983 print "<div class=\"title_text\">\n" .
6984 "<table class=\"object_header\">\n";
6985 git_print_authorship_rows
(\
%co);
6988 print "<div class=\"page_body\">\n";
6989 if (@
{$co{'comment'}} > 1) {
6990 print "<div class=\"log\">\n";
6991 git_print_log
($co{'comment'}, -final_empty_line
=> 1, -remove_title
=> 1);
6992 print "</div>\n"; # class="log"
6995 } elsif ($format eq 'plain') {
6996 my $refs = git_get_references
("tags");
6997 my $tagname = git_get_rev_name_tags
($hash);
6998 my $filename = basename
($project) . "-$hash.patch";
7001 -type
=> 'text/plain',
7002 -charset
=> 'utf-8',
7003 -expires
=> $expires,
7004 -content_disposition
=> 'inline; filename="' . "$filename" . '"');
7005 my %ad = parse_date
($co{'author_epoch'}, $co{'author_tz'});
7006 print "From: " . to_utf8
($co{'author'}) . "\n";
7007 print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
7008 print "Subject: " . to_utf8
($co{'title'}) . "\n";
7010 print "X-Git-Tag: $tagname\n" if $tagname;
7011 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
7013 foreach my $line (@
{$co{'comment'}}) {
7014 print to_utf8
($line) . "\n";
7017 } elsif ($format eq 'patch') {
7018 my $filename = basename
($project) . "-$hash.patch";
7021 -type
=> 'text/plain',
7022 -charset
=> 'utf-8',
7023 -expires
=> $expires,
7024 -content_disposition
=> 'inline; filename="' . "$filename" . '"');
7028 if ($format eq 'html') {
7029 my $use_parents = !defined $hash_parent ||
7030 $hash_parent eq '-c' || $hash_parent eq '--cc';
7031 git_difftree_body
(\
@difftree, $hash,
7032 $use_parents ? @
{$co{'parents'}} : $hash_parent);
7035 git_patchset_body
($fd, \
@difftree, $hash,
7036 $use_parents ? @
{$co{'parents'}} : $hash_parent);
7038 print "</div>\n"; # class="page_body"
7041 } elsif ($format eq 'plain') {
7045 or print "Reading git-diff-tree failed\n";
7046 } elsif ($format eq 'patch') {
7050 or print "Reading git-format-patch failed\n";
7054 sub git_commitdiff_plain
{
7055 git_commitdiff
(-format
=> 'plain');
7058 # format-patch-style patches
7060 git_commitdiff
(-format
=> 'patch', -single
=> 1);
7064 git_commitdiff
(-format
=> 'patch');
7068 git_log_generic
('history', \
&git_history_body
,
7069 $hash_base, $hash_parent_base,
7074 gitweb_check_feature
('search') or die_error
(403, "Search is disabled");
7075 if (!defined $searchtext) {
7076 die_error
(400, "Text field is empty");
7078 if (!defined $hash) {
7079 $hash = git_get_head_hash
($project);
7081 my %co = parse_commit
($hash);
7083 die_error
(404, "Unknown commit object");
7085 if (!defined $page) {
7089 $searchtype ||= 'commit';
7090 if ($searchtype eq 'pickaxe') {
7091 # pickaxe may take all resources of your box and run for several minutes
7092 # with every query - so decide by yourself how public you make this feature
7093 gitweb_check_feature
('pickaxe')
7094 or die_error
(403, "Pickaxe is disabled");
7096 if ($searchtype eq 'grep') {
7097 gitweb_check_feature
('grep')
7098 or die_error
(403, "Grep is disabled");
7103 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
7105 if ($searchtype eq 'commit') {
7106 $greptype = "--grep=";
7107 } elsif ($searchtype eq 'author') {
7108 $greptype = "--author=";
7109 } elsif ($searchtype eq 'committer') {
7110 $greptype = "--committer=";
7112 $greptype .= $searchtext;
7113 my @commitlist = parse_commits
($hash, 101, (100 * $page), undef,
7114 $greptype, '--regexp-ignore-case',
7115 $search_use_regexp ?
'--extended-regexp' : '--fixed-strings');
7117 my $paging_nav = '';
7120 $cgi->a({-href
=> href
(action
=>"search", hash
=>$hash,
7121 searchtext
=>$searchtext,
7122 searchtype
=>$searchtype)},
7124 $paging_nav .= " ⋅ " .
7125 $cgi->a({-href
=> href
(-replay
=>1, page
=>$page-1),
7126 -accesskey
=> "p", -title
=> "Alt-p"}, "prev");
7128 $paging_nav .= "first";
7129 $paging_nav .= " ⋅ prev";
7132 if ($#commitlist >= 100) {
7134 $cgi->a({-href
=> href
(-replay
=>1, page
=>$page+1),
7135 -accesskey
=> "n", -title
=> "Alt-n"}, "next");
7136 $paging_nav .= " ⋅ $next_link";
7138 $paging_nav .= " ⋅ next";
7141 git_print_page_nav
('','', $hash,$co{'tree'},$hash, $paging_nav);
7142 git_print_header_div
('commit', esc_html
($co{'title'}), $hash);
7143 if ($page == 0 && !@commitlist) {
7144 print "<p>No match.</p>\n";
7146 git_search_grep_body
(\
@commitlist, 0, 99, $next_link);
7150 if ($searchtype eq 'pickaxe') {
7151 git_print_page_nav
('','', $hash,$co{'tree'},$hash);
7152 git_print_header_div
('commit', esc_html
($co{'title'}), $hash);
7154 print "<table class=\"pickaxe search\">\n";
7157 open my $fd, '-|', git_cmd
(), '--no-pager', 'log', @diff_opts,
7158 '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
7159 ($search_use_regexp ?
'--pickaxe-regex' : ());
7162 while (my $line = <$fd>) {
7166 my %set = parse_difftree_raw_line
($line);
7167 if (defined $set{'commit'}) {
7168 # finish previous commit
7171 "<td class=\"link\">" .
7172 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$co{'id'})}, "commit") .
7174 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$co{'tree'}, hash_base
=>$co{'id'})}, "tree");
7180 print "<tr class=\"dark\">\n";
7182 print "<tr class=\"light\">\n";
7185 %co = parse_commit
($set{'commit'});
7186 my $author = chop_and_escape_str
($co{'author_name'}, 15, 5);
7187 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
7188 "<td><i>$author</i></td>\n" .
7190 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$co{'id'}),
7191 -class => "list subject"},
7192 chop_and_escape_str
($co{'title'}, 50) . "<br/>");
7193 } elsif (defined $set{'to_id'}) {
7194 next if ($set{'to_id'} =~ m/^0{40}$/);
7196 print $cgi->a({-href
=> href
(action
=>"blob", hash_base
=>$co{'id'},
7197 hash
=>$set{'to_id'}, file_name
=>$set{'to_file'}),
7199 "<span class=\"match\">" . esc_path
($set{'file'}) . "</span>") .
7205 # finish last commit (warning: repetition!)
7208 "<td class=\"link\">" .
7209 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$co{'id'})}, "commit") .
7211 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$co{'tree'}, hash_base
=>$co{'id'})}, "tree");
7219 if ($searchtype eq 'grep') {
7220 git_print_page_nav
('','', $hash,$co{'tree'},$hash);
7221 git_print_header_div
('commit', esc_html
($co{'title'}), $hash);
7223 print "<table class=\"grep_search\">\n";
7227 open my $fd, "-|", git_cmd
(), 'grep', '-n',
7228 $search_use_regexp ?
('-E', '-i') : '-F',
7229 $searchtext, $co{'tree'};
7231 while (my $line = <$fd>) {
7233 my ($file, $lno, $ltext, $binary);
7234 last if ($matches++ > 1000);
7235 if ($line =~ /^Binary file (.+) matches$/) {
7239 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
7241 if ($file ne $lastfile) {
7242 $lastfile and print "</td></tr>\n";
7244 print "<tr class=\"dark\">\n";
7246 print "<tr class=\"light\">\n";
7248 print "<td class=\"list\">".
7249 $cgi->a({-href
=> href
(action
=>"blob", hash
=>$co{'hash'},
7250 file_name
=>"$file"),
7251 -class => "list"}, esc_path
($file));
7252 print "</td><td>\n";
7256 print "<div class=\"binary\">Binary file</div>\n";
7258 $ltext = untabify
($ltext);
7259 if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
7260 $ltext = esc_html
($1, -nbsp
=>1);
7261 $ltext .= '<span class="match">';
7262 $ltext .= esc_html
($2, -nbsp
=>1);
7263 $ltext .= '</span>';
7264 $ltext .= esc_html
($3, -nbsp
=>1);
7266 $ltext = esc_html
($ltext, -nbsp
=>1);
7268 print "<div class=\"pre\">" .
7269 $cgi->a({-href
=> href
(action
=>"blob", hash
=>$co{'hash'},
7270 file_name
=>"$file").'#l'.$lno,
7271 -class => "linenr"}, sprintf('%4i', $lno))
7272 . ' ' . $ltext . "</div>\n";
7276 print "</td></tr>\n";
7277 if ($matches > 1000) {
7278 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
7281 print "<div class=\"diff nodifferences\">No matches found</div>\n";
7290 sub git_search_help
{
7292 git_print_page_nav
('','', $hash,$hash,$hash);
7294 <p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without
7295 regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,
7296 the pattern entered is recognized as the POSIX extended
7297 <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case
7300 <dt><b>commit</b></dt>
7301 <dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
7303 my $have_grep = gitweb_check_feature
('grep');
7306 <dt><b>grep</b></dt>
7307 <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
7308 a different one) are searched for the given pattern. On large trees, this search can take
7309 a while and put some strain on the server, so please use it with some consideration. Note that
7310 due to git-grep peculiarity, currently if regexp mode is turned off, the matches are
7311 case-sensitive.</dd>
7315 <dt><b>author</b></dt>
7316 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>
7317 <dt><b>committer</b></dt>
7318 <dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>
7320 my $have_pickaxe = gitweb_check_feature
('pickaxe');
7321 if ($have_pickaxe) {
7323 <dt><b>pickaxe</b></dt>
7324 <dd>All commits that caused the string to appear or disappear from any file (changes that
7325 added, removed or "modified" the string) will be listed. This search can take a while and
7326 takes a lot of strain on the server, so please use it wisely. Note that since you may be
7327 interested even in changes just changing the case as well, this search is case sensitive.</dd>
7335 git_log_generic
('shortlog', \
&git_shortlog_body
,
7336 $hash, $hash_parent);
7339 ## ......................................................................
7340 ## feeds (RSS, Atom; OPML)
7343 my $format = shift || 'atom';
7344 my $have_blame = gitweb_check_feature
('blame');
7346 # Atom: http://www.atomenabled.org/developers/syndication/
7347 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
7348 if ($format ne 'rss' && $format ne 'atom') {
7349 die_error
(400, "Unknown web feed format");
7352 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
7353 my $head = $hash || 'HEAD';
7354 my @commitlist = parse_commits
($head, 150, 0, $file_name);
7358 my $content_type = "application/$format+xml";
7359 if (defined $cgi->http('HTTP_ACCEPT') &&
7360 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
7361 # browser (feed reader) prefers text/xml
7362 $content_type = 'text/xml';
7364 if (defined($commitlist[0])) {
7365 %latest_commit = %{$commitlist[0]};
7366 my $latest_epoch = $latest_commit{'committer_epoch'};
7367 %latest_date = parse_date
($latest_epoch, $latest_commit{'comitter_tz'});
7368 my $if_modified = $cgi->http('IF_MODIFIED_SINCE');
7369 if (defined $if_modified) {
7371 if (eval { require HTTP
::Date
; 1; }) {
7372 $since = HTTP
::Date
::str2time
($if_modified);
7373 } elsif (eval { require Time
::ParseDate
; 1; }) {
7374 $since = Time
::ParseDate
::parsedate
($if_modified, GMT
=> 1);
7376 if (defined $since && $latest_epoch <= $since) {
7378 -type
=> $content_type,
7379 -charset
=> 'utf-8',
7380 -last_modified
=> $latest_date{'rfc2822'},
7381 -status
=> '304 Not Modified');
7386 -type
=> $content_type,
7387 -charset
=> 'utf-8',
7388 -last_modified
=> $latest_date{'rfc2822'});
7391 -type
=> $content_type,
7392 -charset
=> 'utf-8');
7395 # Optimization: skip generating the body if client asks only
7396 # for Last-Modified date.
7397 return if ($cgi->request_method() eq 'HEAD');
7400 my $title = "$site_name - $project/$action";
7401 my $feed_type = 'log';
7402 if (defined $hash) {
7403 $title .= " - '$hash'";
7404 $feed_type = 'branch log';
7405 if (defined $file_name) {
7406 $title .= " :: $file_name";
7407 $feed_type = 'history';
7409 } elsif (defined $file_name) {
7410 $title .= " - $file_name";
7411 $feed_type = 'history';
7413 $title .= " $feed_type";
7414 my $descr = git_get_project_description
($project);
7415 if (defined $descr) {
7416 $descr = esc_html
($descr);
7418 $descr = "$project " .
7419 ($format eq 'rss' ?
'RSS' : 'Atom') .
7422 my $owner = git_get_project_owner
($project);
7423 $owner = esc_html
($owner);
7427 if (defined $file_name) {
7428 $alt_url = href
(-full
=>1, action
=>"history", hash
=>$hash, file_name
=>$file_name);
7429 } elsif (defined $hash) {
7430 $alt_url = href
(-full
=>1, action
=>"log", hash
=>$hash);
7432 $alt_url = href
(-full
=>1, action
=>"summary");
7434 print qq!<?xml version
="1.0" encoding
="utf-8"?
>\n!;
7435 if ($format eq 'rss') {
7437 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
7440 print "<title>$title</title>\n" .
7441 "<link>$alt_url</link>\n" .
7442 "<description>$descr</description>\n" .
7443 "<language>en</language>\n" .
7444 # project owner is responsible for 'editorial' content
7445 "<managingEditor>$owner</managingEditor>\n";
7446 if (defined $logo || defined $favicon) {
7447 # prefer the logo to the favicon, since RSS
7448 # doesn't allow both
7449 my $img = esc_url
($logo || $favicon);
7451 "<url>$img</url>\n" .
7452 "<title>$title</title>\n" .
7453 "<link>$alt_url</link>\n" .
7457 print "<pubDate>$latest_date{'rfc2822'}</pubDate>\n";
7458 print "<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";
7460 print "<generator>gitweb v.$version/$git_version</generator>\n";
7461 } elsif ($format eq 'atom') {
7463 <feed xmlns="http://www.w3.org/2005/Atom">
7465 print "<title>$title</title>\n" .
7466 "<subtitle>$descr</subtitle>\n" .
7467 '<link rel="alternate" type="text/html" href="' .
7468 $alt_url . '" />' . "\n" .
7469 '<link rel="self" type="' . $content_type . '" href="' .
7470 $cgi->self_url() . '" />' . "\n" .
7471 "<id>" . href
(-full
=>1) . "</id>\n" .
7472 # use project owner for feed author
7473 "<author><name>$owner</name></author>\n";
7474 if (defined $favicon) {
7475 print "<icon>" . esc_url
($favicon) . "</icon>\n";
7477 if (defined $logo) {
7478 # not twice as wide as tall: 72 x 27 pixels
7479 print "<logo>" . esc_url
($logo) . "</logo>\n";
7481 if (! %latest_date) {
7482 # dummy date to keep the feed valid until commits trickle in:
7483 print "<updated>1970-01-01T00:00:00Z</updated>\n";
7485 print "<updated>$latest_date{'iso-8601'}</updated>\n";
7487 print "<generator version='$version/$git_version'>gitweb</generator>\n";
7491 for (my $i = 0; $i <= $#commitlist; $i++) {
7492 my %co = %{$commitlist[$i]};
7493 my $commit = $co{'id'};
7494 # we read 150, we always show 30 and the ones more recent than 48 hours
7495 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
7498 my %cd = parse_date
($co{'author_epoch'}, $co{'author_tz'});
7500 # get list of changed files
7501 open my $fd, "-|", git_cmd
(), "diff-tree", '-r', @diff_opts,
7502 $co{'parent'} || "--root",
7503 $co{'id'}, "--", (defined $file_name ?
$file_name : ())
7505 my @difftree = map { chomp; $_ } <$fd>;
7509 # print element (entry, item)
7510 my $co_url = href
(-full
=>1, action
=>"commitdiff", hash
=>$commit);
7511 if ($format eq 'rss') {
7513 "<title>" . esc_html
($co{'title'}) . "</title>\n" .
7514 "<author>" . esc_html
($co{'author'}) . "</author>\n" .
7515 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
7516 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
7517 "<link>$co_url</link>\n" .
7518 "<description>" . esc_html
($co{'title'}) . "</description>\n" .
7519 "<content:encoded>" .
7521 } elsif ($format eq 'atom') {
7523 "<title type=\"html\">" . esc_html
($co{'title'}) . "</title>\n" .
7524 "<updated>$cd{'iso-8601'}</updated>\n" .
7526 " <name>" . esc_html
($co{'author_name'}) . "</name>\n";
7527 if ($co{'author_email'}) {
7528 print " <email>" . esc_html
($co{'author_email'}) . "</email>\n";
7530 print "</author>\n" .
7531 # use committer for contributor
7533 " <name>" . esc_html
($co{'committer_name'}) . "</name>\n";
7534 if ($co{'committer_email'}) {
7535 print " <email>" . esc_html
($co{'committer_email'}) . "</email>\n";
7537 print "</contributor>\n" .
7538 "<published>$cd{'iso-8601'}</published>\n" .
7539 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
7540 "<id>$co_url</id>\n" .
7541 "<content type=\"xhtml\" xml:base=\"" . esc_url
($my_url) . "\">\n" .
7542 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
7544 my $comment = $co{'comment'};
7546 foreach my $line (@
$comment) {
7547 $line = esc_html
($line);
7550 print "</pre><ul>\n";
7551 foreach my $difftree_line (@difftree) {
7552 my %difftree = parse_difftree_raw_line
($difftree_line);
7553 next if !$difftree{'from_id'};
7555 my $file = $difftree{'file'} || $difftree{'to_file'};
7559 $cgi->a({-href
=> href
(-full
=>1, action
=>"blobdiff",
7560 hash
=>$difftree{'to_id'}, hash_parent
=>$difftree{'from_id'},
7561 hash_base
=>$co{'id'}, hash_parent_base
=>$co{'parent'},
7562 file_name
=>$file, file_parent
=>$difftree{'from_file'}),
7563 -title
=> "diff"}, 'D');
7565 print $cgi->a({-href
=> href
(-full
=>1, action
=>"blame",
7566 file_name
=>$file, hash_base
=>$commit),
7567 -title
=> "blame"}, 'B');
7569 # if this is not a feed of a file history
7570 if (!defined $file_name || $file_name ne $file) {
7571 print $cgi->a({-href
=> href
(-full
=>1, action
=>"history",
7572 file_name
=>$file, hash
=>$commit),
7573 -title
=> "history"}, 'H');
7575 $file = esc_path
($file);
7579 if ($format eq 'rss') {
7580 print "</ul>]]>\n" .
7581 "</content:encoded>\n" .
7583 } elsif ($format eq 'atom') {
7584 print "</ul>\n</div>\n" .
7591 if ($format eq 'rss') {
7592 print "</channel>\n</rss>\n";
7593 } elsif ($format eq 'atom') {
7607 my @list = git_get_projects_list
();
7609 die_error
(404, "No projects found");
7613 -type
=> 'text/xml',
7614 -charset
=> 'utf-8',
7615 -content_disposition
=> 'inline; filename="opml.xml"');
7618 <?xml version="1.0" encoding="utf-8"?>
7619 <opml version="1.0">
7621 <title>$site_name OPML Export</title>
7624 <outline text="git RSS feeds">
7627 foreach my $pr (@list) {
7629 my $head = git_get_head_hash
($proj{'path'});
7630 if (!defined $head) {
7633 $git_dir = "$projectroot/$proj{'path'}";
7634 my %co = parse_commit
($head);
7639 my $path = esc_html
(chop_str
($proj{'path'}, 25, 5));
7640 my $rss = href
('project' => $proj{'path'}, 'action' => 'rss', -full
=> 1);
7641 my $html = href
('project' => $proj{'path'}, 'action' => 'summary', -full
=> 1);
7642 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";