gitweb: Option to not display information about owner
[git/raj.git] / gitweb / gitweb.perl
blobcf160a1b5765761aefbe210124d7c61505e05951
1 #!/usr/bin/perl
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
10 use 5.008;
11 use strict;
12 use warnings;
13 use CGI qw(:standard :escapeHTML -nosticky);
14 use CGI::Util qw(unescape);
15 use CGI::Carp qw(fatalsToBrowser set_message);
16 use Encode;
17 use Fcntl ':mode';
18 use File::Find qw();
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;
26 BEGIN {
27 CGI->compile() if $ENV{'MOD_PERL'};
30 our $version = "++GIT_VERSION++";
32 our ($my_url, $my_uri, $base_url, $path_info, $home_link);
33 sub evaluate_uri {
34 our $cgi;
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
46 # later on.
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
52 # as base URL.
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 = decode_utf8($ENV{"PATH_INFO"});
56 if ($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 # html snippet to include in the <head> section of each page
89 our $site_html_head_string = "++GITWEB_SITE_HTML_HEAD_STRING++";
90 # filename of html text to include at top of each page
91 our $site_header = "++GITWEB_SITE_HEADER++";
92 # html text to include at home page
93 our $home_text = "++GITWEB_HOMETEXT++";
94 # filename of html text to include at bottom of each page
95 our $site_footer = "++GITWEB_SITE_FOOTER++";
97 # URI of stylesheets
98 our @stylesheets = ("++GITWEB_CSS++");
99 # URI of a single stylesheet, which can be overridden in GITWEB_CONFIG.
100 our $stylesheet = undef;
101 # URI of GIT logo (72x27 size)
102 our $logo = "++GITWEB_LOGO++";
103 # URI of GIT favicon, assumed to be image/png type
104 our $favicon = "++GITWEB_FAVICON++";
105 # URI of gitweb.js (JavaScript code for gitweb)
106 our $javascript = "++GITWEB_JS++";
108 # URI and label (title) of GIT logo link
109 #our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
110 #our $logo_label = "git documentation";
111 our $logo_url = "http://git-scm.com/";
112 our $logo_label = "git homepage";
114 # source of projects list
115 our $projects_list = "++GITWEB_LIST++";
117 # the width (in characters) of the projects list "Description" column
118 our $projects_list_description_width = 25;
120 # group projects by category on the projects list
121 # (enabled if this variable evaluates to true)
122 our $projects_list_group_categories = 0;
124 # default category if none specified
125 # (leave the empty string for no category)
126 our $project_list_default_category = "";
128 # default order of projects list
129 # valid values are none, project, descr, owner, and age
130 our $default_projects_order = "project";
132 # show repository only if this file exists
133 # (only effective if this variable evaluates to true)
134 our $export_ok = "++GITWEB_EXPORT_OK++";
136 # don't generate age column on the projects list page
137 our $omit_age_column = 0;
139 # don't generate information about owners of repositories
140 our $omit_owner=0;
142 # show repository only if this subroutine returns true
143 # when given the path to the project, for example:
144 # sub { return -e "$_[0]/git-daemon-export-ok"; }
145 our $export_auth_hook = undef;
147 # only allow viewing of repositories also shown on the overview page
148 our $strict_export = "++GITWEB_STRICT_EXPORT++";
150 # list of git base URLs used for URL to where fetch project from,
151 # i.e. full URL is "$git_base_url/$project"
152 our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
154 # default blob_plain mimetype and default charset for text/plain blob
155 our $default_blob_plain_mimetype = 'text/plain';
156 our $default_text_plain_charset = undef;
158 # file to use for guessing MIME types before trying /etc/mime.types
159 # (relative to the current git repository)
160 our $mimetypes_file = undef;
162 # assume this charset if line contains non-UTF-8 characters;
163 # it should be valid encoding (see Encoding::Supported(3pm) for list),
164 # for which encoding all byte sequences are valid, for example
165 # 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
166 # could be even 'utf-8' for the old behavior)
167 our $fallback_encoding = 'latin1';
169 # rename detection options for git-diff and git-diff-tree
170 # - default is '-M', with the cost proportional to
171 # (number of removed files) * (number of new files).
172 # - more costly is '-C' (which implies '-M'), with the cost proportional to
173 # (number of changed files + number of removed files) * (number of new files)
174 # - even more costly is '-C', '--find-copies-harder' with cost
175 # (number of files in the original tree) * (number of new files)
176 # - one might want to include '-B' option, e.g. '-B', '-M'
177 our @diff_opts = ('-M'); # taken from git_commit
179 # Disables features that would allow repository owners to inject script into
180 # the gitweb domain.
181 our $prevent_xss = 0;
183 # Path to the highlight executable to use (must be the one from
184 # http://www.andre-simon.de due to assumptions about parameters and output).
185 # Useful if highlight is not installed on your webserver's PATH.
186 # [Default: highlight]
187 our $highlight_bin = "++HIGHLIGHT_BIN++";
189 # information about snapshot formats that gitweb is capable of serving
190 our %known_snapshot_formats = (
191 # name => {
192 # 'display' => display name,
193 # 'type' => mime type,
194 # 'suffix' => filename suffix,
195 # 'format' => --format for git-archive,
196 # 'compressor' => [compressor command and arguments]
197 # (array reference, optional)
198 # 'disabled' => boolean (optional)}
200 'tgz' => {
201 'display' => 'tar.gz',
202 'type' => 'application/x-gzip',
203 'suffix' => '.tar.gz',
204 'format' => 'tar',
205 'compressor' => ['gzip', '-n']},
207 'tbz2' => {
208 'display' => 'tar.bz2',
209 'type' => 'application/x-bzip2',
210 'suffix' => '.tar.bz2',
211 'format' => 'tar',
212 'compressor' => ['bzip2']},
214 'txz' => {
215 'display' => 'tar.xz',
216 'type' => 'application/x-xz',
217 'suffix' => '.tar.xz',
218 'format' => 'tar',
219 'compressor' => ['xz'],
220 'disabled' => 1},
222 'zip' => {
223 'display' => 'zip',
224 'type' => 'application/x-zip',
225 'suffix' => '.zip',
226 'format' => 'zip'},
229 # Aliases so we understand old gitweb.snapshot values in repository
230 # configuration.
231 our %known_snapshot_format_aliases = (
232 'gzip' => 'tgz',
233 'bzip2' => 'tbz2',
234 'xz' => 'txz',
236 # backward compatibility: legacy gitweb config support
237 'x-gzip' => undef, 'gz' => undef,
238 'x-bzip2' => undef, 'bz2' => undef,
239 'x-zip' => undef, '' => undef,
242 # Pixel sizes for icons and avatars. If the default font sizes or lineheights
243 # are changed, it may be appropriate to change these values too via
244 # $GITWEB_CONFIG.
245 our %avatar_size = (
246 'default' => 16,
247 'double' => 32
250 # Used to set the maximum load that we will still respond to gitweb queries.
251 # If server load exceed this value then return "503 server busy" error.
252 # If gitweb cannot determined server load, it is taken to be 0.
253 # Leave it undefined (or set to 'undef') to turn off load checking.
254 our $maxload = 300;
256 # configuration for 'highlight' (http://www.andre-simon.de/)
257 # match by basename
258 our %highlight_basename = (
259 #'Program' => 'py',
260 #'Library' => 'py',
261 'SConstruct' => 'py', # SCons equivalent of Makefile
262 'Makefile' => 'make',
264 # match by extension
265 our %highlight_ext = (
266 # main extensions, defining name of syntax;
267 # see files in /usr/share/highlight/langDefs/ directory
268 map { $_ => $_ }
269 qw(py c cpp rb java css php sh pl js tex bib xml awk bat ini spec tcl sql make),
270 # alternate extensions, see /etc/highlight/filetypes.conf
271 'h' => 'c',
272 map { $_ => 'sh' } qw(bash zsh ksh),
273 map { $_ => 'cpp' } qw(cxx c++ cc),
274 map { $_ => 'php' } qw(php3 php4 php5 phps),
275 map { $_ => 'pl' } qw(perl pm), # perhaps also 'cgi'
276 map { $_ => 'make'} qw(mak mk),
277 map { $_ => 'xml' } qw(xhtml html htm),
280 # You define site-wide feature defaults here; override them with
281 # $GITWEB_CONFIG as necessary.
282 our %feature = (
283 # feature => {
284 # 'sub' => feature-sub (subroutine),
285 # 'override' => allow-override (boolean),
286 # 'default' => [ default options...] (array reference)}
288 # if feature is overridable (it means that allow-override has true value),
289 # then feature-sub will be called with default options as parameters;
290 # return value of feature-sub indicates if to enable specified feature
292 # if there is no 'sub' key (no feature-sub), then feature cannot be
293 # overridden
295 # use gitweb_get_feature(<feature>) to retrieve the <feature> value
296 # (an array) or gitweb_check_feature(<feature>) to check if <feature>
297 # is enabled
299 # Enable the 'blame' blob view, showing the last commit that modified
300 # each line in the file. This can be very CPU-intensive.
302 # To enable system wide have in $GITWEB_CONFIG
303 # $feature{'blame'}{'default'} = [1];
304 # To have project specific config enable override in $GITWEB_CONFIG
305 # $feature{'blame'}{'override'} = 1;
306 # and in project config gitweb.blame = 0|1;
307 'blame' => {
308 'sub' => sub { feature_bool('blame', @_) },
309 'override' => 0,
310 'default' => [0]},
312 # Enable the 'snapshot' link, providing a compressed archive of any
313 # tree. This can potentially generate high traffic if you have large
314 # project.
316 # Value is a list of formats defined in %known_snapshot_formats that
317 # you wish to offer.
318 # To disable system wide have in $GITWEB_CONFIG
319 # $feature{'snapshot'}{'default'} = [];
320 # To have project specific config enable override in $GITWEB_CONFIG
321 # $feature{'snapshot'}{'override'} = 1;
322 # and in project config, a comma-separated list of formats or "none"
323 # to disable. Example: gitweb.snapshot = tbz2,zip;
324 'snapshot' => {
325 'sub' => \&feature_snapshot,
326 'override' => 0,
327 'default' => ['tgz']},
329 # Enable text search, which will list the commits which match author,
330 # committer or commit text to a given string. Enabled by default.
331 # Project specific override is not supported.
333 # Note that this controls all search features, which means that if
334 # it is disabled, then 'grep' and 'pickaxe' search would also be
335 # disabled.
336 'search' => {
337 'override' => 0,
338 'default' => [1]},
340 # Enable grep search, which will list the files in currently selected
341 # tree containing the given string. Enabled by default. This can be
342 # potentially CPU-intensive, of course.
343 # Note that you need to have 'search' feature enabled too.
345 # To enable system wide have in $GITWEB_CONFIG
346 # $feature{'grep'}{'default'} = [1];
347 # To have project specific config enable override in $GITWEB_CONFIG
348 # $feature{'grep'}{'override'} = 1;
349 # and in project config gitweb.grep = 0|1;
350 'grep' => {
351 'sub' => sub { feature_bool('grep', @_) },
352 'override' => 0,
353 'default' => [1]},
355 # Enable the pickaxe search, which will list the commits that modified
356 # a given string in a file. This can be practical and quite faster
357 # alternative to 'blame', but still potentially CPU-intensive.
358 # Note that you need to have 'search' feature enabled too.
360 # To enable system wide have in $GITWEB_CONFIG
361 # $feature{'pickaxe'}{'default'} = [1];
362 # To have project specific config enable override in $GITWEB_CONFIG
363 # $feature{'pickaxe'}{'override'} = 1;
364 # and in project config gitweb.pickaxe = 0|1;
365 'pickaxe' => {
366 'sub' => sub { feature_bool('pickaxe', @_) },
367 'override' => 0,
368 'default' => [1]},
370 # Enable showing size of blobs in a 'tree' view, in a separate
371 # column, similar to what 'ls -l' does. This cost a bit of IO.
373 # To disable system wide have in $GITWEB_CONFIG
374 # $feature{'show-sizes'}{'default'} = [0];
375 # To have project specific config enable override in $GITWEB_CONFIG
376 # $feature{'show-sizes'}{'override'} = 1;
377 # and in project config gitweb.showsizes = 0|1;
378 'show-sizes' => {
379 'sub' => sub { feature_bool('showsizes', @_) },
380 'override' => 0,
381 'default' => [1]},
383 # Make gitweb use an alternative format of the URLs which can be
384 # more readable and natural-looking: project name is embedded
385 # directly in the path and the query string contains other
386 # auxiliary information. All gitweb installations recognize
387 # URL in either format; this configures in which formats gitweb
388 # generates links.
390 # To enable system wide have in $GITWEB_CONFIG
391 # $feature{'pathinfo'}{'default'} = [1];
392 # Project specific override is not supported.
394 # Note that you will need to change the default location of CSS,
395 # favicon, logo and possibly other files to an absolute URL. Also,
396 # if gitweb.cgi serves as your indexfile, you will need to force
397 # $my_uri to contain the script name in your $GITWEB_CONFIG.
398 'pathinfo' => {
399 'override' => 0,
400 'default' => [0]},
402 # Make gitweb consider projects in project root subdirectories
403 # to be forks of existing projects. Given project $projname.git,
404 # projects matching $projname/*.git will not be shown in the main
405 # projects list, instead a '+' mark will be added to $projname
406 # there and a 'forks' view will be enabled for the project, listing
407 # all the forks. If project list is taken from a file, forks have
408 # to be listed after the main project.
410 # To enable system wide have in $GITWEB_CONFIG
411 # $feature{'forks'}{'default'} = [1];
412 # Project specific override is not supported.
413 'forks' => {
414 'override' => 0,
415 'default' => [0]},
417 # Insert custom links to the action bar of all project pages.
418 # This enables you mainly to link to third-party scripts integrating
419 # into gitweb; e.g. git-browser for graphical history representation
420 # or custom web-based repository administration interface.
422 # The 'default' value consists of a list of triplets in the form
423 # (label, link, position) where position is the label after which
424 # to insert the link and link is a format string where %n expands
425 # to the project name, %f to the project path within the filesystem,
426 # %h to the current hash (h gitweb parameter) and %b to the current
427 # hash base (hb gitweb parameter); %% expands to %.
429 # To enable system wide have in $GITWEB_CONFIG e.g.
430 # $feature{'actions'}{'default'} = [('graphiclog',
431 # '/git-browser/by-commit.html?r=%n', 'summary')];
432 # Project specific override is not supported.
433 'actions' => {
434 'override' => 0,
435 'default' => []},
437 # Allow gitweb scan project content tags of project repository,
438 # and display the popular Web 2.0-ish "tag cloud" near the projects
439 # list. Note that this is something COMPLETELY different from the
440 # normal Git tags.
442 # gitweb by itself can show existing tags, but it does not handle
443 # tagging itself; you need to do it externally, outside gitweb.
444 # The format is described in git_get_project_ctags() subroutine.
445 # You may want to install the HTML::TagCloud Perl module to get
446 # a pretty tag cloud instead of just a list of tags.
448 # To enable system wide have in $GITWEB_CONFIG
449 # $feature{'ctags'}{'default'} = [1];
450 # Project specific override is not supported.
452 # In the future whether ctags editing is enabled might depend
453 # on the value, but using 1 should always mean no editing of ctags.
454 'ctags' => {
455 'override' => 0,
456 'default' => [0]},
458 # The maximum number of patches in a patchset generated in patch
459 # view. Set this to 0 or undef to disable patch view, or to a
460 # negative number to remove any limit.
462 # To disable system wide have in $GITWEB_CONFIG
463 # $feature{'patches'}{'default'} = [0];
464 # To have project specific config enable override in $GITWEB_CONFIG
465 # $feature{'patches'}{'override'} = 1;
466 # and in project config gitweb.patches = 0|n;
467 # where n is the maximum number of patches allowed in a patchset.
468 'patches' => {
469 'sub' => \&feature_patches,
470 'override' => 0,
471 'default' => [16]},
473 # Avatar support. When this feature is enabled, views such as
474 # shortlog or commit will display an avatar associated with
475 # the email of the committer(s) and/or author(s).
477 # Currently available providers are gravatar and picon.
478 # If an unknown provider is specified, the feature is disabled.
480 # Gravatar depends on Digest::MD5.
481 # Picon currently relies on the indiana.edu database.
483 # To enable system wide have in $GITWEB_CONFIG
484 # $feature{'avatar'}{'default'} = ['<provider>'];
485 # where <provider> is either gravatar or picon.
486 # To have project specific config enable override in $GITWEB_CONFIG
487 # $feature{'avatar'}{'override'} = 1;
488 # and in project config gitweb.avatar = <provider>;
489 'avatar' => {
490 'sub' => \&feature_avatar,
491 'override' => 0,
492 'default' => ['']},
494 # Enable displaying how much time and how many git commands
495 # it took to generate and display page. Disabled by default.
496 # Project specific override is not supported.
497 'timed' => {
498 'override' => 0,
499 'default' => [0]},
501 # Enable turning some links into links to actions which require
502 # JavaScript to run (like 'blame_incremental'). Not enabled by
503 # default. Project specific override is currently not supported.
504 'javascript-actions' => {
505 'override' => 0,
506 'default' => [0]},
508 # Enable and configure ability to change common timezone for dates
509 # in gitweb output via JavaScript. Enabled by default.
510 # Project specific override is not supported.
511 'javascript-timezone' => {
512 'override' => 0,
513 'default' => [
514 'local', # default timezone: 'utc', 'local', or '(-|+)HHMM' format,
515 # or undef to turn off this feature
516 'gitweb_tz', # name of cookie where to store selected timezone
517 'datetime', # CSS class used to mark up dates for manipulation
520 # Syntax highlighting support. This is based on Daniel Svensson's
521 # and Sham Chukoury's work in gitweb-xmms2.git.
522 # It requires the 'highlight' program present in $PATH,
523 # and therefore is disabled by default.
525 # To enable system wide have in $GITWEB_CONFIG
526 # $feature{'highlight'}{'default'} = [1];
528 'highlight' => {
529 'sub' => sub { feature_bool('highlight', @_) },
530 'override' => 0,
531 'default' => [0]},
533 # Enable displaying of remote heads in the heads list
535 # To enable system wide have in $GITWEB_CONFIG
536 # $feature{'remote_heads'}{'default'} = [1];
537 # To have project specific config enable override in $GITWEB_CONFIG
538 # $feature{'remote_heads'}{'override'} = 1;
539 # and in project config gitweb.remote_heads = 0|1;
540 'remote_heads' => {
541 'sub' => sub { feature_bool('remote_heads', @_) },
542 'override' => 0,
543 'default' => [0]},
546 sub gitweb_get_feature {
547 my ($name) = @_;
548 return unless exists $feature{$name};
549 my ($sub, $override, @defaults) = (
550 $feature{$name}{'sub'},
551 $feature{$name}{'override'},
552 @{$feature{$name}{'default'}});
553 # project specific override is possible only if we have project
554 our $git_dir; # global variable, declared later
555 if (!$override || !defined $git_dir) {
556 return @defaults;
558 if (!defined $sub) {
559 warn "feature $name is not overridable";
560 return @defaults;
562 return $sub->(@defaults);
565 # A wrapper to check if a given feature is enabled.
566 # With this, you can say
568 # my $bool_feat = gitweb_check_feature('bool_feat');
569 # gitweb_check_feature('bool_feat') or somecode;
571 # instead of
573 # my ($bool_feat) = gitweb_get_feature('bool_feat');
574 # (gitweb_get_feature('bool_feat'))[0] or somecode;
576 sub gitweb_check_feature {
577 return (gitweb_get_feature(@_))[0];
581 sub feature_bool {
582 my $key = shift;
583 my ($val) = git_get_project_config($key, '--bool');
585 if (!defined $val) {
586 return ($_[0]);
587 } elsif ($val eq 'true') {
588 return (1);
589 } elsif ($val eq 'false') {
590 return (0);
594 sub feature_snapshot {
595 my (@fmts) = @_;
597 my ($val) = git_get_project_config('snapshot');
599 if ($val) {
600 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
603 return @fmts;
606 sub feature_patches {
607 my @val = (git_get_project_config('patches', '--int'));
609 if (@val) {
610 return @val;
613 return ($_[0]);
616 sub feature_avatar {
617 my @val = (git_get_project_config('avatar'));
619 return @val ? @val : @_;
622 # checking HEAD file with -e is fragile if the repository was
623 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
624 # and then pruned.
625 sub check_head_link {
626 my ($dir) = @_;
627 my $headfile = "$dir/HEAD";
628 return ((-e $headfile) ||
629 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
632 sub check_export_ok {
633 my ($dir) = @_;
634 return (check_head_link($dir) &&
635 (!$export_ok || -e "$dir/$export_ok") &&
636 (!$export_auth_hook || $export_auth_hook->($dir)));
639 # process alternate names for backward compatibility
640 # filter out unsupported (unknown) snapshot formats
641 sub filter_snapshot_fmts {
642 my @fmts = @_;
644 @fmts = map {
645 exists $known_snapshot_format_aliases{$_} ?
646 $known_snapshot_format_aliases{$_} : $_} @fmts;
647 @fmts = grep {
648 exists $known_snapshot_formats{$_} &&
649 !$known_snapshot_formats{$_}{'disabled'}} @fmts;
652 # If it is set to code reference, it is code that it is to be run once per
653 # request, allowing updating configurations that change with each request,
654 # while running other code in config file only once.
656 # Otherwise, if it is false then gitweb would process config file only once;
657 # if it is true then gitweb config would be run for each request.
658 our $per_request_config = 1;
660 # read and parse gitweb config file given by its parameter.
661 # returns true on success, false on recoverable error, allowing
662 # to chain this subroutine, using first file that exists.
663 # dies on errors during parsing config file, as it is unrecoverable.
664 sub read_config_file {
665 my $filename = shift;
666 return unless defined $filename;
667 # die if there are errors parsing config file
668 if (-e $filename) {
669 do $filename;
670 die $@ if $@;
671 return 1;
673 return;
676 our ($GITWEB_CONFIG, $GITWEB_CONFIG_SYSTEM, $GITWEB_CONFIG_COMMON);
677 sub evaluate_gitweb_config {
678 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
679 our $GITWEB_CONFIG_SYSTEM = $ENV{'GITWEB_CONFIG_SYSTEM'} || "++GITWEB_CONFIG_SYSTEM++";
680 our $GITWEB_CONFIG_COMMON = $ENV{'GITWEB_CONFIG_COMMON'} || "++GITWEB_CONFIG_COMMON++";
682 # Protect agains duplications of file names, to not read config twice.
683 # Only one of $GITWEB_CONFIG and $GITWEB_CONFIG_SYSTEM is used, so
684 # there possibility of duplication of filename there doesn't matter.
685 $GITWEB_CONFIG = "" if ($GITWEB_CONFIG eq $GITWEB_CONFIG_COMMON);
686 $GITWEB_CONFIG_SYSTEM = "" if ($GITWEB_CONFIG_SYSTEM eq $GITWEB_CONFIG_COMMON);
688 # Common system-wide settings for convenience.
689 # Those settings can be ovverriden by GITWEB_CONFIG or GITWEB_CONFIG_SYSTEM.
690 read_config_file($GITWEB_CONFIG_COMMON);
692 # Use first config file that exists. This means use the per-instance
693 # GITWEB_CONFIG if exists, otherwise use GITWEB_SYSTEM_CONFIG.
694 read_config_file($GITWEB_CONFIG) and return;
695 read_config_file($GITWEB_CONFIG_SYSTEM);
698 # Get loadavg of system, to compare against $maxload.
699 # Currently it requires '/proc/loadavg' present to get loadavg;
700 # if it is not present it returns 0, which means no load checking.
701 sub get_loadavg {
702 if( -e '/proc/loadavg' ){
703 open my $fd, '<', '/proc/loadavg'
704 or return 0;
705 my @load = split(/\s+/, scalar <$fd>);
706 close $fd;
708 # The first three columns measure CPU and IO utilization of the last one,
709 # five, and 10 minute periods. The fourth column shows the number of
710 # currently running processes and the total number of processes in the m/n
711 # format. The last column displays the last process ID used.
712 return $load[0] || 0;
714 # additional checks for load average should go here for things that don't export
715 # /proc/loadavg
717 return 0;
720 # version of the core git binary
721 our $git_version;
722 sub evaluate_git_version {
723 our $git_version = qx("$GIT" --version) =~ m/git version (.*)$/ ? $1 : "unknown";
724 $number_of_git_cmds++;
727 sub check_loadavg {
728 if (defined $maxload && get_loadavg() > $maxload) {
729 die_error(503, "The load average on the server is too high");
733 # ======================================================================
734 # input validation and dispatch
736 # input parameters can be collected from a variety of sources (presently, CGI
737 # and PATH_INFO), so we define an %input_params hash that collects them all
738 # together during validation: this allows subsequent uses (e.g. href()) to be
739 # agnostic of the parameter origin
741 our %input_params = ();
743 # input parameters are stored with the long parameter name as key. This will
744 # also be used in the href subroutine to convert parameters to their CGI
745 # equivalent, and since the href() usage is the most frequent one, we store
746 # the name -> CGI key mapping here, instead of the reverse.
748 # XXX: Warning: If you touch this, check the search form for updating,
749 # too.
751 our @cgi_param_mapping = (
752 project => "p",
753 action => "a",
754 file_name => "f",
755 file_parent => "fp",
756 hash => "h",
757 hash_parent => "hp",
758 hash_base => "hb",
759 hash_parent_base => "hpb",
760 page => "pg",
761 order => "o",
762 searchtext => "s",
763 searchtype => "st",
764 snapshot_format => "sf",
765 extra_options => "opt",
766 search_use_regexp => "sr",
767 ctag => "by_tag",
768 diff_style => "ds",
769 project_filter => "pf",
770 # this must be last entry (for manipulation from JavaScript)
771 javascript => "js"
773 our %cgi_param_mapping = @cgi_param_mapping;
775 # we will also need to know the possible actions, for validation
776 our %actions = (
777 "blame" => \&git_blame,
778 "blame_incremental" => \&git_blame_incremental,
779 "blame_data" => \&git_blame_data,
780 "blobdiff" => \&git_blobdiff,
781 "blobdiff_plain" => \&git_blobdiff_plain,
782 "blob" => \&git_blob,
783 "blob_plain" => \&git_blob_plain,
784 "commitdiff" => \&git_commitdiff,
785 "commitdiff_plain" => \&git_commitdiff_plain,
786 "commit" => \&git_commit,
787 "forks" => \&git_forks,
788 "heads" => \&git_heads,
789 "history" => \&git_history,
790 "log" => \&git_log,
791 "patch" => \&git_patch,
792 "patches" => \&git_patches,
793 "remotes" => \&git_remotes,
794 "rss" => \&git_rss,
795 "atom" => \&git_atom,
796 "search" => \&git_search,
797 "search_help" => \&git_search_help,
798 "shortlog" => \&git_shortlog,
799 "summary" => \&git_summary,
800 "tag" => \&git_tag,
801 "tags" => \&git_tags,
802 "tree" => \&git_tree,
803 "snapshot" => \&git_snapshot,
804 "object" => \&git_object,
805 # those below don't need $project
806 "opml" => \&git_opml,
807 "project_list" => \&git_project_list,
808 "project_index" => \&git_project_index,
811 # finally, we have the hash of allowed extra_options for the commands that
812 # allow them
813 our %allowed_options = (
814 "--no-merges" => [ qw(rss atom log shortlog history) ],
817 # fill %input_params with the CGI parameters. All values except for 'opt'
818 # should be single values, but opt can be an array. We should probably
819 # build an array of parameters that can be multi-valued, but since for the time
820 # being it's only this one, we just single it out
821 sub evaluate_query_params {
822 our $cgi;
824 while (my ($name, $symbol) = each %cgi_param_mapping) {
825 if ($symbol eq 'opt') {
826 $input_params{$name} = [ map { decode_utf8($_) } $cgi->param($symbol) ];
827 } else {
828 $input_params{$name} = decode_utf8($cgi->param($symbol));
833 # now read PATH_INFO and update the parameter list for missing parameters
834 sub evaluate_path_info {
835 return if defined $input_params{'project'};
836 return if !$path_info;
837 $path_info =~ s,^/+,,;
838 return if !$path_info;
840 # find which part of PATH_INFO is project
841 my $project = $path_info;
842 $project =~ s,/+$,,;
843 while ($project && !check_head_link("$projectroot/$project")) {
844 $project =~ s,/*[^/]*$,,;
846 return unless $project;
847 $input_params{'project'} = $project;
849 # do not change any parameters if an action is given using the query string
850 return if $input_params{'action'};
851 $path_info =~ s,^\Q$project\E/*,,;
853 # next, check if we have an action
854 my $action = $path_info;
855 $action =~ s,/.*$,,;
856 if (exists $actions{$action}) {
857 $path_info =~ s,^$action/*,,;
858 $input_params{'action'} = $action;
861 # list of actions that want hash_base instead of hash, but can have no
862 # pathname (f) parameter
863 my @wants_base = (
864 'tree',
865 'history',
868 # we want to catch, among others
869 # [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name]
870 my ($parentrefname, $parentpathname, $refname, $pathname) =
871 ($path_info =~ /^(?:(.+?)(?::(.+))?\.\.)?([^:]+?)?(?::(.+))?$/);
873 # first, analyze the 'current' part
874 if (defined $pathname) {
875 # we got "branch:filename" or "branch:dir/"
876 # we could use git_get_type(branch:pathname), but:
877 # - it needs $git_dir
878 # - it does a git() call
879 # - the convention of terminating directories with a slash
880 # makes it superfluous
881 # - embedding the action in the PATH_INFO would make it even
882 # more superfluous
883 $pathname =~ s,^/+,,;
884 if (!$pathname || substr($pathname, -1) eq "/") {
885 $input_params{'action'} ||= "tree";
886 $pathname =~ s,/$,,;
887 } else {
888 # the default action depends on whether we had parent info
889 # or not
890 if ($parentrefname) {
891 $input_params{'action'} ||= "blobdiff_plain";
892 } else {
893 $input_params{'action'} ||= "blob_plain";
896 $input_params{'hash_base'} ||= $refname;
897 $input_params{'file_name'} ||= $pathname;
898 } elsif (defined $refname) {
899 # we got "branch". In this case we have to choose if we have to
900 # set hash or hash_base.
902 # Most of the actions without a pathname only want hash to be
903 # set, except for the ones specified in @wants_base that want
904 # hash_base instead. It should also be noted that hand-crafted
905 # links having 'history' as an action and no pathname or hash
906 # set will fail, but that happens regardless of PATH_INFO.
907 if (defined $parentrefname) {
908 # if there is parent let the default be 'shortlog' action
909 # (for http://git.example.com/repo.git/A..B links); if there
910 # is no parent, dispatch will detect type of object and set
911 # action appropriately if required (if action is not set)
912 $input_params{'action'} ||= "shortlog";
914 if ($input_params{'action'} &&
915 grep { $_ eq $input_params{'action'} } @wants_base) {
916 $input_params{'hash_base'} ||= $refname;
917 } else {
918 $input_params{'hash'} ||= $refname;
922 # next, handle the 'parent' part, if present
923 if (defined $parentrefname) {
924 # a missing pathspec defaults to the 'current' filename, allowing e.g.
925 # someproject/blobdiff/oldrev..newrev:/filename
926 if ($parentpathname) {
927 $parentpathname =~ s,^/+,,;
928 $parentpathname =~ s,/$,,;
929 $input_params{'file_parent'} ||= $parentpathname;
930 } else {
931 $input_params{'file_parent'} ||= $input_params{'file_name'};
933 # we assume that hash_parent_base is wanted if a path was specified,
934 # or if the action wants hash_base instead of hash
935 if (defined $input_params{'file_parent'} ||
936 grep { $_ eq $input_params{'action'} } @wants_base) {
937 $input_params{'hash_parent_base'} ||= $parentrefname;
938 } else {
939 $input_params{'hash_parent'} ||= $parentrefname;
943 # for the snapshot action, we allow URLs in the form
944 # $project/snapshot/$hash.ext
945 # where .ext determines the snapshot and gets removed from the
946 # passed $refname to provide the $hash.
948 # To be able to tell that $refname includes the format extension, we
949 # require the following two conditions to be satisfied:
950 # - the hash input parameter MUST have been set from the $refname part
951 # of the URL (i.e. they must be equal)
952 # - the snapshot format MUST NOT have been defined already (e.g. from
953 # CGI parameter sf)
954 # It's also useless to try any matching unless $refname has a dot,
955 # so we check for that too
956 if (defined $input_params{'action'} &&
957 $input_params{'action'} eq 'snapshot' &&
958 defined $refname && index($refname, '.') != -1 &&
959 $refname eq $input_params{'hash'} &&
960 !defined $input_params{'snapshot_format'}) {
961 # We loop over the known snapshot formats, checking for
962 # extensions. Allowed extensions are both the defined suffix
963 # (which includes the initial dot already) and the snapshot
964 # format key itself, with a prepended dot
965 while (my ($fmt, $opt) = each %known_snapshot_formats) {
966 my $hash = $refname;
967 unless ($hash =~ s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) {
968 next;
970 my $sfx = $1;
971 # a valid suffix was found, so set the snapshot format
972 # and reset the hash parameter
973 $input_params{'snapshot_format'} = $fmt;
974 $input_params{'hash'} = $hash;
975 # we also set the format suffix to the one requested
976 # in the URL: this way a request for e.g. .tgz returns
977 # a .tgz instead of a .tar.gz
978 $known_snapshot_formats{$fmt}{'suffix'} = $sfx;
979 last;
984 our ($action, $project, $file_name, $file_parent, $hash, $hash_parent, $hash_base,
985 $hash_parent_base, @extra_options, $page, $searchtype, $search_use_regexp,
986 $searchtext, $search_regexp, $project_filter);
987 sub evaluate_and_validate_params {
988 our $action = $input_params{'action'};
989 if (defined $action) {
990 if (!validate_action($action)) {
991 die_error(400, "Invalid action parameter");
995 # parameters which are pathnames
996 our $project = $input_params{'project'};
997 if (defined $project) {
998 if (!validate_project($project)) {
999 undef $project;
1000 die_error(404, "No such project");
1004 our $project_filter = $input_params{'project_filter'};
1005 if (defined $project_filter) {
1006 if (!validate_pathname($project_filter)) {
1007 die_error(404, "Invalid project_filter parameter");
1011 our $file_name = $input_params{'file_name'};
1012 if (defined $file_name) {
1013 if (!validate_pathname($file_name)) {
1014 die_error(400, "Invalid file parameter");
1018 our $file_parent = $input_params{'file_parent'};
1019 if (defined $file_parent) {
1020 if (!validate_pathname($file_parent)) {
1021 die_error(400, "Invalid file parent parameter");
1025 # parameters which are refnames
1026 our $hash = $input_params{'hash'};
1027 if (defined $hash) {
1028 if (!validate_refname($hash)) {
1029 die_error(400, "Invalid hash parameter");
1033 our $hash_parent = $input_params{'hash_parent'};
1034 if (defined $hash_parent) {
1035 if (!validate_refname($hash_parent)) {
1036 die_error(400, "Invalid hash parent parameter");
1040 our $hash_base = $input_params{'hash_base'};
1041 if (defined $hash_base) {
1042 if (!validate_refname($hash_base)) {
1043 die_error(400, "Invalid hash base parameter");
1047 our @extra_options = @{$input_params{'extra_options'}};
1048 # @extra_options is always defined, since it can only be (currently) set from
1049 # CGI, and $cgi->param() returns the empty array in array context if the param
1050 # is not set
1051 foreach my $opt (@extra_options) {
1052 if (not exists $allowed_options{$opt}) {
1053 die_error(400, "Invalid option parameter");
1055 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
1056 die_error(400, "Invalid option parameter for this action");
1060 our $hash_parent_base = $input_params{'hash_parent_base'};
1061 if (defined $hash_parent_base) {
1062 if (!validate_refname($hash_parent_base)) {
1063 die_error(400, "Invalid hash parent base parameter");
1067 # other parameters
1068 our $page = $input_params{'page'};
1069 if (defined $page) {
1070 if ($page =~ m/[^0-9]/) {
1071 die_error(400, "Invalid page parameter");
1075 our $searchtype = $input_params{'searchtype'};
1076 if (defined $searchtype) {
1077 if ($searchtype =~ m/[^a-z]/) {
1078 die_error(400, "Invalid searchtype parameter");
1082 our $search_use_regexp = $input_params{'search_use_regexp'};
1084 our $searchtext = $input_params{'searchtext'};
1085 our $search_regexp;
1086 if (defined $searchtext) {
1087 if (length($searchtext) < 2) {
1088 die_error(403, "At least two characters are required for search parameter");
1090 if ($search_use_regexp) {
1091 $search_regexp = $searchtext;
1092 if (!eval { qr/$search_regexp/; 1; }) {
1093 (my $error = $@) =~ s/ at \S+ line \d+.*\n?//;
1094 die_error(400, "Invalid search regexp '$search_regexp'",
1095 esc_html($error));
1097 } else {
1098 $search_regexp = quotemeta $searchtext;
1103 # path to the current git repository
1104 our $git_dir;
1105 sub evaluate_git_dir {
1106 our $git_dir = "$projectroot/$project" if $project;
1109 our (@snapshot_fmts, $git_avatar);
1110 sub configure_gitweb_features {
1111 # list of supported snapshot formats
1112 our @snapshot_fmts = gitweb_get_feature('snapshot');
1113 @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
1115 # check that the avatar feature is set to a known provider name,
1116 # and for each provider check if the dependencies are satisfied.
1117 # if the provider name is invalid or the dependencies are not met,
1118 # reset $git_avatar to the empty string.
1119 our ($git_avatar) = gitweb_get_feature('avatar');
1120 if ($git_avatar eq 'gravatar') {
1121 $git_avatar = '' unless (eval { require Digest::MD5; 1; });
1122 } elsif ($git_avatar eq 'picon') {
1123 # no dependencies
1124 } else {
1125 $git_avatar = '';
1129 # custom error handler: 'die <message>' is Internal Server Error
1130 sub handle_errors_html {
1131 my $msg = shift; # it is already HTML escaped
1133 # to avoid infinite loop where error occurs in die_error,
1134 # change handler to default handler, disabling handle_errors_html
1135 set_message("Error occured when inside die_error:\n$msg");
1137 # you cannot jump out of die_error when called as error handler;
1138 # the subroutine set via CGI::Carp::set_message is called _after_
1139 # HTTP headers are already written, so it cannot write them itself
1140 die_error(undef, undef, $msg, -error_handler => 1, -no_http_header => 1);
1142 set_message(\&handle_errors_html);
1144 # dispatch
1145 sub dispatch {
1146 if (!defined $action) {
1147 if (defined $hash) {
1148 $action = git_get_type($hash);
1149 $action or die_error(404, "Object does not exist");
1150 } elsif (defined $hash_base && defined $file_name) {
1151 $action = git_get_type("$hash_base:$file_name");
1152 $action or die_error(404, "File or directory does not exist");
1153 } elsif (defined $project) {
1154 $action = 'summary';
1155 } else {
1156 $action = 'project_list';
1159 if (!defined($actions{$action})) {
1160 die_error(400, "Unknown action");
1162 if ($action !~ m/^(?:opml|project_list|project_index)$/ &&
1163 !$project) {
1164 die_error(400, "Project needed");
1166 $actions{$action}->();
1169 sub reset_timer {
1170 our $t0 = [ gettimeofday() ]
1171 if defined $t0;
1172 our $number_of_git_cmds = 0;
1175 our $first_request = 1;
1176 sub run_request {
1177 reset_timer();
1179 evaluate_uri();
1180 if ($first_request) {
1181 evaluate_gitweb_config();
1182 evaluate_git_version();
1184 if ($per_request_config) {
1185 if (ref($per_request_config) eq 'CODE') {
1186 $per_request_config->();
1187 } elsif (!$first_request) {
1188 evaluate_gitweb_config();
1191 check_loadavg();
1193 # $projectroot and $projects_list might be set in gitweb config file
1194 $projects_list ||= $projectroot;
1196 evaluate_query_params();
1197 evaluate_path_info();
1198 evaluate_and_validate_params();
1199 evaluate_git_dir();
1201 configure_gitweb_features();
1203 dispatch();
1206 our $is_last_request = sub { 1 };
1207 our ($pre_dispatch_hook, $post_dispatch_hook, $pre_listen_hook);
1208 our $CGI = 'CGI';
1209 our $cgi;
1210 sub configure_as_fcgi {
1211 require CGI::Fast;
1212 our $CGI = 'CGI::Fast';
1214 my $request_number = 0;
1215 # let each child service 100 requests
1216 our $is_last_request = sub { ++$request_number > 100 };
1218 sub evaluate_argv {
1219 my $script_name = $ENV{'SCRIPT_NAME'} || $ENV{'SCRIPT_FILENAME'} || __FILE__;
1220 configure_as_fcgi()
1221 if $script_name =~ /\.fcgi$/;
1223 return unless (@ARGV);
1225 require Getopt::Long;
1226 Getopt::Long::GetOptions(
1227 'fastcgi|fcgi|f' => \&configure_as_fcgi,
1228 'nproc|n=i' => sub {
1229 my ($arg, $val) = @_;
1230 return unless eval { require FCGI::ProcManager; 1; };
1231 my $proc_manager = FCGI::ProcManager->new({
1232 n_processes => $val,
1234 our $pre_listen_hook = sub { $proc_manager->pm_manage() };
1235 our $pre_dispatch_hook = sub { $proc_manager->pm_pre_dispatch() };
1236 our $post_dispatch_hook = sub { $proc_manager->pm_post_dispatch() };
1241 sub run {
1242 evaluate_argv();
1244 $first_request = 1;
1245 $pre_listen_hook->()
1246 if $pre_listen_hook;
1248 REQUEST:
1249 while ($cgi = $CGI->new()) {
1250 $pre_dispatch_hook->()
1251 if $pre_dispatch_hook;
1253 run_request();
1255 $post_dispatch_hook->()
1256 if $post_dispatch_hook;
1257 $first_request = 0;
1259 last REQUEST if ($is_last_request->());
1262 DONE_GITWEB:
1266 run();
1268 if (defined caller) {
1269 # wrapped in a subroutine processing requests,
1270 # e.g. mod_perl with ModPerl::Registry, or PSGI with Plack::App::WrapCGI
1271 return;
1272 } else {
1273 # pure CGI script, serving single request
1274 exit;
1277 ## ======================================================================
1278 ## action links
1280 # possible values of extra options
1281 # -full => 0|1 - use absolute/full URL ($my_uri/$my_url as base)
1282 # -replay => 1 - start from a current view (replay with modifications)
1283 # -path_info => 0|1 - don't use/use path_info URL (if possible)
1284 # -anchor => ANCHOR - add #ANCHOR to end of URL, implies -replay if used alone
1285 sub href {
1286 my %params = @_;
1287 # default is to use -absolute url() i.e. $my_uri
1288 my $href = $params{-full} ? $my_url : $my_uri;
1290 # implicit -replay, must be first of implicit params
1291 $params{-replay} = 1 if (keys %params == 1 && $params{-anchor});
1293 $params{'project'} = $project unless exists $params{'project'};
1295 if ($params{-replay}) {
1296 while (my ($name, $symbol) = each %cgi_param_mapping) {
1297 if (!exists $params{$name}) {
1298 $params{$name} = $input_params{$name};
1303 my $use_pathinfo = gitweb_check_feature('pathinfo');
1304 if (defined $params{'project'} &&
1305 (exists $params{-path_info} ? $params{-path_info} : $use_pathinfo)) {
1306 # try to put as many parameters as possible in PATH_INFO:
1307 # - project name
1308 # - action
1309 # - hash_parent or hash_parent_base:/file_parent
1310 # - hash or hash_base:/filename
1311 # - the snapshot_format as an appropriate suffix
1313 # When the script is the root DirectoryIndex for the domain,
1314 # $href here would be something like http://gitweb.example.com/
1315 # Thus, we strip any trailing / from $href, to spare us double
1316 # slashes in the final URL
1317 $href =~ s,/$,,;
1319 # Then add the project name, if present
1320 $href .= "/".esc_path_info($params{'project'});
1321 delete $params{'project'};
1323 # since we destructively absorb parameters, we keep this
1324 # boolean that remembers if we're handling a snapshot
1325 my $is_snapshot = $params{'action'} eq 'snapshot';
1327 # Summary just uses the project path URL, any other action is
1328 # added to the URL
1329 if (defined $params{'action'}) {
1330 $href .= "/".esc_path_info($params{'action'})
1331 unless $params{'action'} eq 'summary';
1332 delete $params{'action'};
1335 # Next, we put hash_parent_base:/file_parent..hash_base:/file_name,
1336 # stripping nonexistent or useless pieces
1337 $href .= "/" if ($params{'hash_base'} || $params{'hash_parent_base'}
1338 || $params{'hash_parent'} || $params{'hash'});
1339 if (defined $params{'hash_base'}) {
1340 if (defined $params{'hash_parent_base'}) {
1341 $href .= esc_path_info($params{'hash_parent_base'});
1342 # skip the file_parent if it's the same as the file_name
1343 if (defined $params{'file_parent'}) {
1344 if (defined $params{'file_name'} && $params{'file_parent'} eq $params{'file_name'}) {
1345 delete $params{'file_parent'};
1346 } elsif ($params{'file_parent'} !~ /\.\./) {
1347 $href .= ":/".esc_path_info($params{'file_parent'});
1348 delete $params{'file_parent'};
1351 $href .= "..";
1352 delete $params{'hash_parent'};
1353 delete $params{'hash_parent_base'};
1354 } elsif (defined $params{'hash_parent'}) {
1355 $href .= esc_path_info($params{'hash_parent'}). "..";
1356 delete $params{'hash_parent'};
1359 $href .= esc_path_info($params{'hash_base'});
1360 if (defined $params{'file_name'} && $params{'file_name'} !~ /\.\./) {
1361 $href .= ":/".esc_path_info($params{'file_name'});
1362 delete $params{'file_name'};
1364 delete $params{'hash'};
1365 delete $params{'hash_base'};
1366 } elsif (defined $params{'hash'}) {
1367 $href .= esc_path_info($params{'hash'});
1368 delete $params{'hash'};
1371 # If the action was a snapshot, we can absorb the
1372 # snapshot_format parameter too
1373 if ($is_snapshot) {
1374 my $fmt = $params{'snapshot_format'};
1375 # snapshot_format should always be defined when href()
1376 # is called, but just in case some code forgets, we
1377 # fall back to the default
1378 $fmt ||= $snapshot_fmts[0];
1379 $href .= $known_snapshot_formats{$fmt}{'suffix'};
1380 delete $params{'snapshot_format'};
1384 # now encode the parameters explicitly
1385 my @result = ();
1386 for (my $i = 0; $i < @cgi_param_mapping; $i += 2) {
1387 my ($name, $symbol) = ($cgi_param_mapping[$i], $cgi_param_mapping[$i+1]);
1388 if (defined $params{$name}) {
1389 if (ref($params{$name}) eq "ARRAY") {
1390 foreach my $par (@{$params{$name}}) {
1391 push @result, $symbol . "=" . esc_param($par);
1393 } else {
1394 push @result, $symbol . "=" . esc_param($params{$name});
1398 $href .= "?" . join(';', @result) if scalar @result;
1400 # final transformation: trailing spaces must be escaped (URI-encoded)
1401 $href =~ s/(\s+)$/CGI::escape($1)/e;
1403 if ($params{-anchor}) {
1404 $href .= "#".esc_param($params{-anchor});
1407 return $href;
1411 ## ======================================================================
1412 ## validation, quoting/unquoting and escaping
1414 sub validate_action {
1415 my $input = shift || return undef;
1416 return undef unless exists $actions{$input};
1417 return $input;
1420 sub validate_project {
1421 my $input = shift || return undef;
1422 if (!validate_pathname($input) ||
1423 !(-d "$projectroot/$input") ||
1424 !check_export_ok("$projectroot/$input") ||
1425 ($strict_export && !project_in_list($input))) {
1426 return undef;
1427 } else {
1428 return $input;
1432 sub validate_pathname {
1433 my $input = shift || return undef;
1435 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
1436 # at the beginning, at the end, and between slashes.
1437 # also this catches doubled slashes
1438 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
1439 return undef;
1441 # no null characters
1442 if ($input =~ m!\0!) {
1443 return undef;
1445 return $input;
1448 sub validate_refname {
1449 my $input = shift || return undef;
1451 # textual hashes are O.K.
1452 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
1453 return $input;
1455 # it must be correct pathname
1456 $input = validate_pathname($input)
1457 or return undef;
1458 # restrictions on ref name according to git-check-ref-format
1459 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
1460 return undef;
1462 return $input;
1465 # decode sequences of octets in utf8 into Perl's internal form,
1466 # which is utf-8 with utf8 flag set if needed. gitweb writes out
1467 # in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
1468 sub to_utf8 {
1469 my $str = shift;
1470 return undef unless defined $str;
1472 if (utf8::is_utf8($str) || utf8::decode($str)) {
1473 return $str;
1474 } else {
1475 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
1479 # quote unsafe chars, but keep the slash, even when it's not
1480 # correct, but quoted slashes look too horrible in bookmarks
1481 sub esc_param {
1482 my $str = shift;
1483 return undef unless defined $str;
1484 $str =~ s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;
1485 $str =~ s/ /\+/g;
1486 return $str;
1489 # the quoting rules for path_info fragment are slightly different
1490 sub esc_path_info {
1491 my $str = shift;
1492 return undef unless defined $str;
1494 # path_info doesn't treat '+' as space (specially), but '?' must be escaped
1495 $str =~ s/([^A-Za-z0-9\-_.~();\/;:@&= +]+)/CGI::escape($1)/eg;
1497 return $str;
1500 # quote unsafe chars in whole URL, so some characters cannot be quoted
1501 sub esc_url {
1502 my $str = shift;
1503 return undef unless defined $str;
1504 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&= ]+)/CGI::escape($1)/eg;
1505 $str =~ s/ /\+/g;
1506 return $str;
1509 # quote unsafe characters in HTML attributes
1510 sub esc_attr {
1512 # for XHTML conformance escaping '"' to '&quot;' is not enough
1513 return esc_html(@_);
1516 # replace invalid utf8 character with SUBSTITUTION sequence
1517 sub esc_html {
1518 my $str = shift;
1519 my %opts = @_;
1521 return undef unless defined $str;
1523 $str = to_utf8($str);
1524 $str = $cgi->escapeHTML($str);
1525 if ($opts{'-nbsp'}) {
1526 $str =~ s/ /&nbsp;/g;
1528 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
1529 return $str;
1532 # quote control characters and escape filename to HTML
1533 sub esc_path {
1534 my $str = shift;
1535 my %opts = @_;
1537 return undef unless defined $str;
1539 $str = to_utf8($str);
1540 $str = $cgi->escapeHTML($str);
1541 if ($opts{'-nbsp'}) {
1542 $str =~ s/ /&nbsp;/g;
1544 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
1545 return $str;
1548 # Sanitize for use in XHTML + application/xml+xhtm (valid XML 1.0)
1549 sub sanitize {
1550 my $str = shift;
1552 return undef unless defined $str;
1554 $str = to_utf8($str);
1555 $str =~ s|([[:cntrl:]])|($1 =~ /[\t\n\r]/ ? $1 : quot_cec($1))|eg;
1556 return $str;
1559 # Make control characters "printable", using character escape codes (CEC)
1560 sub quot_cec {
1561 my $cntrl = shift;
1562 my %opts = @_;
1563 my %es = ( # character escape codes, aka escape sequences
1564 "\t" => '\t', # tab (HT)
1565 "\n" => '\n', # line feed (LF)
1566 "\r" => '\r', # carrige return (CR)
1567 "\f" => '\f', # form feed (FF)
1568 "\b" => '\b', # backspace (BS)
1569 "\a" => '\a', # alarm (bell) (BEL)
1570 "\e" => '\e', # escape (ESC)
1571 "\013" => '\v', # vertical tab (VT)
1572 "\000" => '\0', # nul character (NUL)
1574 my $chr = ( (exists $es{$cntrl})
1575 ? $es{$cntrl}
1576 : sprintf('\%2x', ord($cntrl)) );
1577 if ($opts{-nohtml}) {
1578 return $chr;
1579 } else {
1580 return "<span class=\"cntrl\">$chr</span>";
1584 # Alternatively use unicode control pictures codepoints,
1585 # Unicode "printable representation" (PR)
1586 sub quot_upr {
1587 my $cntrl = shift;
1588 my %opts = @_;
1590 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
1591 if ($opts{-nohtml}) {
1592 return $chr;
1593 } else {
1594 return "<span class=\"cntrl\">$chr</span>";
1598 # git may return quoted and escaped filenames
1599 sub unquote {
1600 my $str = shift;
1602 sub unq {
1603 my $seq = shift;
1604 my %es = ( # character escape codes, aka escape sequences
1605 't' => "\t", # tab (HT, TAB)
1606 'n' => "\n", # newline (NL)
1607 'r' => "\r", # return (CR)
1608 'f' => "\f", # form feed (FF)
1609 'b' => "\b", # backspace (BS)
1610 'a' => "\a", # alarm (bell) (BEL)
1611 'e' => "\e", # escape (ESC)
1612 'v' => "\013", # vertical tab (VT)
1615 if ($seq =~ m/^[0-7]{1,3}$/) {
1616 # octal char sequence
1617 return chr(oct($seq));
1618 } elsif (exists $es{$seq}) {
1619 # C escape sequence, aka character escape code
1620 return $es{$seq};
1622 # quoted ordinary character
1623 return $seq;
1626 if ($str =~ m/^"(.*)"$/) {
1627 # needs unquoting
1628 $str = $1;
1629 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
1631 return $str;
1634 # escape tabs (convert tabs to spaces)
1635 sub untabify {
1636 my $line = shift;
1638 while ((my $pos = index($line, "\t")) != -1) {
1639 if (my $count = (8 - ($pos % 8))) {
1640 my $spaces = ' ' x $count;
1641 $line =~ s/\t/$spaces/;
1645 return $line;
1648 sub project_in_list {
1649 my $project = shift;
1650 my @list = git_get_projects_list();
1651 return @list && scalar(grep { $_->{'path'} eq $project } @list);
1654 ## ----------------------------------------------------------------------
1655 ## HTML aware string manipulation
1657 # Try to chop given string on a word boundary between position
1658 # $len and $len+$add_len. If there is no word boundary there,
1659 # chop at $len+$add_len. Do not chop if chopped part plus ellipsis
1660 # (marking chopped part) would be longer than given string.
1661 sub chop_str {
1662 my $str = shift;
1663 my $len = shift;
1664 my $add_len = shift || 10;
1665 my $where = shift || 'right'; # 'left' | 'center' | 'right'
1667 # Make sure perl knows it is utf8 encoded so we don't
1668 # cut in the middle of a utf8 multibyte char.
1669 $str = to_utf8($str);
1671 # allow only $len chars, but don't cut a word if it would fit in $add_len
1672 # if it doesn't fit, cut it if it's still longer than the dots we would add
1673 # remove chopped character entities entirely
1675 # when chopping in the middle, distribute $len into left and right part
1676 # return early if chopping wouldn't make string shorter
1677 if ($where eq 'center') {
1678 return $str if ($len + 5 >= length($str)); # filler is length 5
1679 $len = int($len/2);
1680 } else {
1681 return $str if ($len + 4 >= length($str)); # filler is length 4
1684 # regexps: ending and beginning with word part up to $add_len
1685 my $endre = qr/.{$len}\w{0,$add_len}/;
1686 my $begre = qr/\w{0,$add_len}.{$len}/;
1688 if ($where eq 'left') {
1689 $str =~ m/^(.*?)($begre)$/;
1690 my ($lead, $body) = ($1, $2);
1691 if (length($lead) > 4) {
1692 $lead = " ...";
1694 return "$lead$body";
1696 } elsif ($where eq 'center') {
1697 $str =~ m/^($endre)(.*)$/;
1698 my ($left, $str) = ($1, $2);
1699 $str =~ m/^(.*?)($begre)$/;
1700 my ($mid, $right) = ($1, $2);
1701 if (length($mid) > 5) {
1702 $mid = " ... ";
1704 return "$left$mid$right";
1706 } else {
1707 $str =~ m/^($endre)(.*)$/;
1708 my $body = $1;
1709 my $tail = $2;
1710 if (length($tail) > 4) {
1711 $tail = "... ";
1713 return "$body$tail";
1717 # takes the same arguments as chop_str, but also wraps a <span> around the
1718 # result with a title attribute if it does get chopped. Additionally, the
1719 # string is HTML-escaped.
1720 sub chop_and_escape_str {
1721 my ($str) = @_;
1723 my $chopped = chop_str(@_);
1724 $str = to_utf8($str);
1725 if ($chopped eq $str) {
1726 return esc_html($chopped);
1727 } else {
1728 $str =~ s/[[:cntrl:]]/?/g;
1729 return $cgi->span({-title=>$str}, esc_html($chopped));
1733 # Highlight selected fragments of string, using given CSS class,
1734 # and escape HTML. It is assumed that fragments do not overlap.
1735 # Regions are passed as list of pairs (array references).
1737 # Example: esc_html_hl_regions("foobar", "mark", [ 0, 3 ]) returns
1738 # '<span class="mark">foo</span>bar'
1739 sub esc_html_hl_regions {
1740 my ($str, $css_class, @sel) = @_;
1741 return esc_html($str) unless @sel;
1743 my $out = '';
1744 my $pos = 0;
1746 for my $s (@sel) {
1747 $out .= esc_html(substr($str, $pos, $s->[0] - $pos))
1748 if ($s->[0] - $pos > 0);
1749 $out .= $cgi->span({-class => $css_class},
1750 esc_html(substr($str, $s->[0], $s->[1] - $s->[0])));
1752 $pos = $s->[1];
1754 $out .= esc_html(substr($str, $pos))
1755 if ($pos < length($str));
1757 return $out;
1760 # return positions of beginning and end of each match
1761 sub matchpos_list {
1762 my ($str, $regexp) = @_;
1763 return unless (defined $str && defined $regexp);
1765 my @matches;
1766 while ($str =~ /$regexp/g) {
1767 push @matches, [$-[0], $+[0]];
1769 return @matches;
1772 # highlight match (if any), and escape HTML
1773 sub esc_html_match_hl {
1774 my ($str, $regexp) = @_;
1775 return esc_html($str) unless defined $regexp;
1777 my @matches = matchpos_list($str, $regexp);
1778 return esc_html($str) unless @matches;
1780 return esc_html_hl_regions($str, 'match', @matches);
1784 # highlight match (if any) of shortened string, and escape HTML
1785 sub esc_html_match_hl_chopped {
1786 my ($str, $chopped, $regexp) = @_;
1787 return esc_html_match_hl($str, $regexp) unless defined $chopped;
1789 my @matches = matchpos_list($str, $regexp);
1790 return esc_html($chopped) unless @matches;
1792 # filter matches so that we mark chopped string
1793 my $tail = "... "; # see chop_str
1794 unless ($chopped =~ s/\Q$tail\E$//) {
1795 $tail = '';
1797 my $chop_len = length($chopped);
1798 my $tail_len = length($tail);
1799 my @filtered;
1801 for my $m (@matches) {
1802 if ($m->[0] > $chop_len) {
1803 push @filtered, [ $chop_len, $chop_len + $tail_len ] if ($tail_len > 0);
1804 last;
1805 } elsif ($m->[1] > $chop_len) {
1806 push @filtered, [ $m->[0], $chop_len + $tail_len ];
1807 last;
1809 push @filtered, $m;
1812 return esc_html_hl_regions($chopped . $tail, 'match', @filtered);
1815 ## ----------------------------------------------------------------------
1816 ## functions returning short strings
1818 # CSS class for given age value (in seconds)
1819 sub age_class {
1820 my $age = shift;
1822 if (!defined $age) {
1823 return "noage";
1824 } elsif ($age < 60*60*2) {
1825 return "age0";
1826 } elsif ($age < 60*60*24*2) {
1827 return "age1";
1828 } else {
1829 return "age2";
1833 # convert age in seconds to "nn units ago" string
1834 sub age_string {
1835 my $age = shift;
1836 my $age_str;
1838 if ($age > 60*60*24*365*2) {
1839 $age_str = (int $age/60/60/24/365);
1840 $age_str .= " years ago";
1841 } elsif ($age > 60*60*24*(365/12)*2) {
1842 $age_str = int $age/60/60/24/(365/12);
1843 $age_str .= " months ago";
1844 } elsif ($age > 60*60*24*7*2) {
1845 $age_str = int $age/60/60/24/7;
1846 $age_str .= " weeks ago";
1847 } elsif ($age > 60*60*24*2) {
1848 $age_str = int $age/60/60/24;
1849 $age_str .= " days ago";
1850 } elsif ($age > 60*60*2) {
1851 $age_str = int $age/60/60;
1852 $age_str .= " hours ago";
1853 } elsif ($age > 60*2) {
1854 $age_str = int $age/60;
1855 $age_str .= " min ago";
1856 } elsif ($age > 2) {
1857 $age_str = int $age;
1858 $age_str .= " sec ago";
1859 } else {
1860 $age_str .= " right now";
1862 return $age_str;
1865 use constant {
1866 S_IFINVALID => 0030000,
1867 S_IFGITLINK => 0160000,
1870 # submodule/subproject, a commit object reference
1871 sub S_ISGITLINK {
1872 my $mode = shift;
1874 return (($mode & S_IFMT) == S_IFGITLINK)
1877 # convert file mode in octal to symbolic file mode string
1878 sub mode_str {
1879 my $mode = oct shift;
1881 if (S_ISGITLINK($mode)) {
1882 return 'm---------';
1883 } elsif (S_ISDIR($mode & S_IFMT)) {
1884 return 'drwxr-xr-x';
1885 } elsif (S_ISLNK($mode)) {
1886 return 'lrwxrwxrwx';
1887 } elsif (S_ISREG($mode)) {
1888 # git cares only about the executable bit
1889 if ($mode & S_IXUSR) {
1890 return '-rwxr-xr-x';
1891 } else {
1892 return '-rw-r--r--';
1894 } else {
1895 return '----------';
1899 # convert file mode in octal to file type string
1900 sub file_type {
1901 my $mode = shift;
1903 if ($mode !~ m/^[0-7]+$/) {
1904 return $mode;
1905 } else {
1906 $mode = oct $mode;
1909 if (S_ISGITLINK($mode)) {
1910 return "submodule";
1911 } elsif (S_ISDIR($mode & S_IFMT)) {
1912 return "directory";
1913 } elsif (S_ISLNK($mode)) {
1914 return "symlink";
1915 } elsif (S_ISREG($mode)) {
1916 return "file";
1917 } else {
1918 return "unknown";
1922 # convert file mode in octal to file type description string
1923 sub file_type_long {
1924 my $mode = shift;
1926 if ($mode !~ m/^[0-7]+$/) {
1927 return $mode;
1928 } else {
1929 $mode = oct $mode;
1932 if (S_ISGITLINK($mode)) {
1933 return "submodule";
1934 } elsif (S_ISDIR($mode & S_IFMT)) {
1935 return "directory";
1936 } elsif (S_ISLNK($mode)) {
1937 return "symlink";
1938 } elsif (S_ISREG($mode)) {
1939 if ($mode & S_IXUSR) {
1940 return "executable";
1941 } else {
1942 return "file";
1944 } else {
1945 return "unknown";
1950 ## ----------------------------------------------------------------------
1951 ## functions returning short HTML fragments, or transforming HTML fragments
1952 ## which don't belong to other sections
1954 # format line of commit message.
1955 sub format_log_line_html {
1956 my $line = shift;
1958 $line = esc_html($line, -nbsp=>1);
1959 $line =~ s{\b([0-9a-fA-F]{8,40})\b}{
1960 $cgi->a({-href => href(action=>"object", hash=>$1),
1961 -class => "text"}, $1);
1962 }eg;
1964 return $line;
1967 # format marker of refs pointing to given object
1969 # the destination action is chosen based on object type and current context:
1970 # - for annotated tags, we choose the tag view unless it's the current view
1971 # already, in which case we go to shortlog view
1972 # - for other refs, we keep the current view if we're in history, shortlog or
1973 # log view, and select shortlog otherwise
1974 sub format_ref_marker {
1975 my ($refs, $id) = @_;
1976 my $markers = '';
1978 if (defined $refs->{$id}) {
1979 foreach my $ref (@{$refs->{$id}}) {
1980 # this code exploits the fact that non-lightweight tags are the
1981 # only indirect objects, and that they are the only objects for which
1982 # we want to use tag instead of shortlog as action
1983 my ($type, $name) = qw();
1984 my $indirect = ($ref =~ s/\^\{\}$//);
1985 # e.g. tags/v2.6.11 or heads/next
1986 if ($ref =~ m!^(.*?)s?/(.*)$!) {
1987 $type = $1;
1988 $name = $2;
1989 } else {
1990 $type = "ref";
1991 $name = $ref;
1994 my $class = $type;
1995 $class .= " indirect" if $indirect;
1997 my $dest_action = "shortlog";
1999 if ($indirect) {
2000 $dest_action = "tag" unless $action eq "tag";
2001 } elsif ($action =~ /^(history|(short)?log)$/) {
2002 $dest_action = $action;
2005 my $dest = "";
2006 $dest .= "refs/" unless $ref =~ m!^refs/!;
2007 $dest .= $ref;
2009 my $link = $cgi->a({
2010 -href => href(
2011 action=>$dest_action,
2012 hash=>$dest
2013 )}, $name);
2015 $markers .= " <span class=\"".esc_attr($class)."\" title=\"".esc_attr($ref)."\">" .
2016 $link . "</span>";
2020 if ($markers) {
2021 return ' <span class="refs">'. $markers . '</span>';
2022 } else {
2023 return "";
2027 # format, perhaps shortened and with markers, title line
2028 sub format_subject_html {
2029 my ($long, $short, $href, $extra) = @_;
2030 $extra = '' unless defined($extra);
2032 if (length($short) < length($long)) {
2033 $long =~ s/[[:cntrl:]]/?/g;
2034 return $cgi->a({-href => $href, -class => "list subject",
2035 -title => to_utf8($long)},
2036 esc_html($short)) . $extra;
2037 } else {
2038 return $cgi->a({-href => $href, -class => "list subject"},
2039 esc_html($long)) . $extra;
2043 # Rather than recomputing the url for an email multiple times, we cache it
2044 # after the first hit. This gives a visible benefit in views where the avatar
2045 # for the same email is used repeatedly (e.g. shortlog).
2046 # The cache is shared by all avatar engines (currently gravatar only), which
2047 # are free to use it as preferred. Since only one avatar engine is used for any
2048 # given page, there's no risk for cache conflicts.
2049 our %avatar_cache = ();
2051 # Compute the picon url for a given email, by using the picon search service over at
2052 # http://www.cs.indiana.edu/picons/search.html
2053 sub picon_url {
2054 my $email = lc shift;
2055 if (!$avatar_cache{$email}) {
2056 my ($user, $domain) = split('@', $email);
2057 $avatar_cache{$email} =
2058 "http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/" .
2059 "$domain/$user/" .
2060 "users+domains+unknown/up/single";
2062 return $avatar_cache{$email};
2065 # Compute the gravatar url for a given email, if it's not in the cache already.
2066 # Gravatar stores only the part of the URL before the size, since that's the
2067 # one computationally more expensive. This also allows reuse of the cache for
2068 # different sizes (for this particular engine).
2069 sub gravatar_url {
2070 my $email = lc shift;
2071 my $size = shift;
2072 $avatar_cache{$email} ||=
2073 "http://www.gravatar.com/avatar/" .
2074 Digest::MD5::md5_hex($email) . "?s=";
2075 return $avatar_cache{$email} . $size;
2078 # Insert an avatar for the given $email at the given $size if the feature
2079 # is enabled.
2080 sub git_get_avatar {
2081 my ($email, %opts) = @_;
2082 my $pre_white = ($opts{-pad_before} ? "&nbsp;" : "");
2083 my $post_white = ($opts{-pad_after} ? "&nbsp;" : "");
2084 $opts{-size} ||= 'default';
2085 my $size = $avatar_size{$opts{-size}} || $avatar_size{'default'};
2086 my $url = "";
2087 if ($git_avatar eq 'gravatar') {
2088 $url = gravatar_url($email, $size);
2089 } elsif ($git_avatar eq 'picon') {
2090 $url = picon_url($email);
2092 # Other providers can be added by extending the if chain, defining $url
2093 # as needed. If no variant puts something in $url, we assume avatars
2094 # are completely disabled/unavailable.
2095 if ($url) {
2096 return $pre_white .
2097 "<img width=\"$size\" " .
2098 "class=\"avatar\" " .
2099 "src=\"".esc_url($url)."\" " .
2100 "alt=\"\" " .
2101 "/>" . $post_white;
2102 } else {
2103 return "";
2107 sub format_search_author {
2108 my ($author, $searchtype, $displaytext) = @_;
2109 my $have_search = gitweb_check_feature('search');
2111 if ($have_search) {
2112 my $performed = "";
2113 if ($searchtype eq 'author') {
2114 $performed = "authored";
2115 } elsif ($searchtype eq 'committer') {
2116 $performed = "committed";
2119 return $cgi->a({-href => href(action=>"search", hash=>$hash,
2120 searchtext=>$author,
2121 searchtype=>$searchtype), class=>"list",
2122 title=>"Search for commits $performed by $author"},
2123 $displaytext);
2125 } else {
2126 return $displaytext;
2130 # format the author name of the given commit with the given tag
2131 # the author name is chopped and escaped according to the other
2132 # optional parameters (see chop_str).
2133 sub format_author_html {
2134 my $tag = shift;
2135 my $co = shift;
2136 my $author = chop_and_escape_str($co->{'author_name'}, @_);
2137 return "<$tag class=\"author\">" .
2138 format_search_author($co->{'author_name'}, "author",
2139 git_get_avatar($co->{'author_email'}, -pad_after => 1) .
2140 $author) .
2141 "</$tag>";
2144 # format git diff header line, i.e. "diff --(git|combined|cc) ..."
2145 sub format_git_diff_header_line {
2146 my $line = shift;
2147 my $diffinfo = shift;
2148 my ($from, $to) = @_;
2150 if ($diffinfo->{'nparents'}) {
2151 # combined diff
2152 $line =~ s!^(diff (.*?) )"?.*$!$1!;
2153 if ($to->{'href'}) {
2154 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
2155 esc_path($to->{'file'}));
2156 } else { # file was deleted (no href)
2157 $line .= esc_path($to->{'file'});
2159 } else {
2160 # "ordinary" diff
2161 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
2162 if ($from->{'href'}) {
2163 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
2164 'a/' . esc_path($from->{'file'}));
2165 } else { # file was added (no href)
2166 $line .= 'a/' . esc_path($from->{'file'});
2168 $line .= ' ';
2169 if ($to->{'href'}) {
2170 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
2171 'b/' . esc_path($to->{'file'}));
2172 } else { # file was deleted
2173 $line .= 'b/' . esc_path($to->{'file'});
2177 return "<div class=\"diff header\">$line</div>\n";
2180 # format extended diff header line, before patch itself
2181 sub format_extended_diff_header_line {
2182 my $line = shift;
2183 my $diffinfo = shift;
2184 my ($from, $to) = @_;
2186 # match <path>
2187 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
2188 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
2189 esc_path($from->{'file'}));
2191 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
2192 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
2193 esc_path($to->{'file'}));
2195 # match single <mode>
2196 if ($line =~ m/\s(\d{6})$/) {
2197 $line .= '<span class="info"> (' .
2198 file_type_long($1) .
2199 ')</span>';
2201 # match <hash>
2202 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
2203 # can match only for combined diff
2204 $line = 'index ';
2205 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2206 if ($from->{'href'}[$i]) {
2207 $line .= $cgi->a({-href=>$from->{'href'}[$i],
2208 -class=>"hash"},
2209 substr($diffinfo->{'from_id'}[$i],0,7));
2210 } else {
2211 $line .= '0' x 7;
2213 # separator
2214 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
2216 $line .= '..';
2217 if ($to->{'href'}) {
2218 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
2219 substr($diffinfo->{'to_id'},0,7));
2220 } else {
2221 $line .= '0' x 7;
2224 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
2225 # can match only for ordinary diff
2226 my ($from_link, $to_link);
2227 if ($from->{'href'}) {
2228 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
2229 substr($diffinfo->{'from_id'},0,7));
2230 } else {
2231 $from_link = '0' x 7;
2233 if ($to->{'href'}) {
2234 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
2235 substr($diffinfo->{'to_id'},0,7));
2236 } else {
2237 $to_link = '0' x 7;
2239 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
2240 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
2243 return $line . "<br/>\n";
2246 # format from-file/to-file diff header
2247 sub format_diff_from_to_header {
2248 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
2249 my $line;
2250 my $result = '';
2252 $line = $from_line;
2253 #assert($line =~ m/^---/) if DEBUG;
2254 # no extra formatting for "^--- /dev/null"
2255 if (! $diffinfo->{'nparents'}) {
2256 # ordinary (single parent) diff
2257 if ($line =~ m!^--- "?a/!) {
2258 if ($from->{'href'}) {
2259 $line = '--- a/' .
2260 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
2261 esc_path($from->{'file'}));
2262 } else {
2263 $line = '--- a/' .
2264 esc_path($from->{'file'});
2267 $result .= qq!<div class="diff from_file">$line</div>\n!;
2269 } else {
2270 # combined diff (merge commit)
2271 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2272 if ($from->{'href'}[$i]) {
2273 $line = '--- ' .
2274 $cgi->a({-href=>href(action=>"blobdiff",
2275 hash_parent=>$diffinfo->{'from_id'}[$i],
2276 hash_parent_base=>$parents[$i],
2277 file_parent=>$from->{'file'}[$i],
2278 hash=>$diffinfo->{'to_id'},
2279 hash_base=>$hash,
2280 file_name=>$to->{'file'}),
2281 -class=>"path",
2282 -title=>"diff" . ($i+1)},
2283 $i+1) .
2284 '/' .
2285 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
2286 esc_path($from->{'file'}[$i]));
2287 } else {
2288 $line = '--- /dev/null';
2290 $result .= qq!<div class="diff from_file">$line</div>\n!;
2294 $line = $to_line;
2295 #assert($line =~ m/^\+\+\+/) if DEBUG;
2296 # no extra formatting for "^+++ /dev/null"
2297 if ($line =~ m!^\+\+\+ "?b/!) {
2298 if ($to->{'href'}) {
2299 $line = '+++ b/' .
2300 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
2301 esc_path($to->{'file'}));
2302 } else {
2303 $line = '+++ b/' .
2304 esc_path($to->{'file'});
2307 $result .= qq!<div class="diff to_file">$line</div>\n!;
2309 return $result;
2312 # create note for patch simplified by combined diff
2313 sub format_diff_cc_simplified {
2314 my ($diffinfo, @parents) = @_;
2315 my $result = '';
2317 $result .= "<div class=\"diff header\">" .
2318 "diff --cc ";
2319 if (!is_deleted($diffinfo)) {
2320 $result .= $cgi->a({-href => href(action=>"blob",
2321 hash_base=>$hash,
2322 hash=>$diffinfo->{'to_id'},
2323 file_name=>$diffinfo->{'to_file'}),
2324 -class => "path"},
2325 esc_path($diffinfo->{'to_file'}));
2326 } else {
2327 $result .= esc_path($diffinfo->{'to_file'});
2329 $result .= "</div>\n" . # class="diff header"
2330 "<div class=\"diff nodifferences\">" .
2331 "Simple merge" .
2332 "</div>\n"; # class="diff nodifferences"
2334 return $result;
2337 sub diff_line_class {
2338 my ($line, $from, $to) = @_;
2340 # ordinary diff
2341 my $num_sign = 1;
2342 # combined diff
2343 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
2344 $num_sign = scalar @{$from->{'href'}};
2347 my @diff_line_classifier = (
2348 { regexp => qr/^\@\@{$num_sign} /, class => "chunk_header"},
2349 { regexp => qr/^\\/, class => "incomplete" },
2350 { regexp => qr/^ {$num_sign}/, class => "ctx" },
2351 # classifier for context must come before classifier add/rem,
2352 # or we would have to use more complicated regexp, for example
2353 # qr/(?= {0,$m}\+)[+ ]{$num_sign}/, where $m = $num_sign - 1;
2354 { regexp => qr/^[+ ]{$num_sign}/, class => "add" },
2355 { regexp => qr/^[- ]{$num_sign}/, class => "rem" },
2357 for my $clsfy (@diff_line_classifier) {
2358 return $clsfy->{'class'}
2359 if ($line =~ $clsfy->{'regexp'});
2362 # fallback
2363 return "";
2366 # assumes that $from and $to are defined and correctly filled,
2367 # and that $line holds a line of chunk header for unified diff
2368 sub format_unidiff_chunk_header {
2369 my ($line, $from, $to) = @_;
2371 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
2372 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
2374 $from_lines = 0 unless defined $from_lines;
2375 $to_lines = 0 unless defined $to_lines;
2377 if ($from->{'href'}) {
2378 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
2379 -class=>"list"}, $from_text);
2381 if ($to->{'href'}) {
2382 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
2383 -class=>"list"}, $to_text);
2385 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
2386 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
2387 return $line;
2390 # assumes that $from and $to are defined and correctly filled,
2391 # and that $line holds a line of chunk header for combined diff
2392 sub format_cc_diff_chunk_header {
2393 my ($line, $from, $to) = @_;
2395 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
2396 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
2398 @from_text = split(' ', $ranges);
2399 for (my $i = 0; $i < @from_text; ++$i) {
2400 ($from_start[$i], $from_nlines[$i]) =
2401 (split(',', substr($from_text[$i], 1)), 0);
2404 $to_text = pop @from_text;
2405 $to_start = pop @from_start;
2406 $to_nlines = pop @from_nlines;
2408 $line = "<span class=\"chunk_info\">$prefix ";
2409 for (my $i = 0; $i < @from_text; ++$i) {
2410 if ($from->{'href'}[$i]) {
2411 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
2412 -class=>"list"}, $from_text[$i]);
2413 } else {
2414 $line .= $from_text[$i];
2416 $line .= " ";
2418 if ($to->{'href'}) {
2419 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
2420 -class=>"list"}, $to_text);
2421 } else {
2422 $line .= $to_text;
2424 $line .= " $prefix</span>" .
2425 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
2426 return $line;
2429 # process patch (diff) line (not to be used for diff headers),
2430 # returning class and HTML-formatted (but not wrapped) line
2431 sub process_diff_line {
2432 my $line = shift;
2433 my ($from, $to) = @_;
2435 my $diff_class = diff_line_class($line, $from, $to);
2437 chomp $line;
2438 $line = untabify($line);
2440 if ($from && $to && $line =~ m/^\@{2} /) {
2441 $line = format_unidiff_chunk_header($line, $from, $to);
2442 return $diff_class, $line;
2444 } elsif ($from && $to && $line =~ m/^\@{3}/) {
2445 $line = format_cc_diff_chunk_header($line, $from, $to);
2446 return $diff_class, $line;
2449 return $diff_class, esc_html($line, -nbsp=>1);
2452 # Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
2453 # linked. Pass the hash of the tree/commit to snapshot.
2454 sub format_snapshot_links {
2455 my ($hash) = @_;
2456 my $num_fmts = @snapshot_fmts;
2457 if ($num_fmts > 1) {
2458 # A parenthesized list of links bearing format names.
2459 # e.g. "snapshot (_tar.gz_ _zip_)"
2460 return "snapshot (" . join(' ', map
2461 $cgi->a({
2462 -href => href(
2463 action=>"snapshot",
2464 hash=>$hash,
2465 snapshot_format=>$_
2467 }, $known_snapshot_formats{$_}{'display'})
2468 , @snapshot_fmts) . ")";
2469 } elsif ($num_fmts == 1) {
2470 # A single "snapshot" link whose tooltip bears the format name.
2471 # i.e. "_snapshot_"
2472 my ($fmt) = @snapshot_fmts;
2473 return
2474 $cgi->a({
2475 -href => href(
2476 action=>"snapshot",
2477 hash=>$hash,
2478 snapshot_format=>$fmt
2480 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
2481 }, "snapshot");
2482 } else { # $num_fmts == 0
2483 return undef;
2487 ## ......................................................................
2488 ## functions returning values to be passed, perhaps after some
2489 ## transformation, to other functions; e.g. returning arguments to href()
2491 # returns hash to be passed to href to generate gitweb URL
2492 # in -title key it returns description of link
2493 sub get_feed_info {
2494 my $format = shift || 'Atom';
2495 my %res = (action => lc($format));
2497 # feed links are possible only for project views
2498 return unless (defined $project);
2499 # some views should link to OPML, or to generic project feed,
2500 # or don't have specific feed yet (so they should use generic)
2501 return if (!$action || $action =~ /^(?:tags|heads|forks|tag|search)$/x);
2503 my $branch;
2504 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
2505 # from tag links; this also makes possible to detect branch links
2506 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
2507 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
2508 $branch = $1;
2510 # find log type for feed description (title)
2511 my $type = 'log';
2512 if (defined $file_name) {
2513 $type = "history of $file_name";
2514 $type .= "/" if ($action eq 'tree');
2515 $type .= " on '$branch'" if (defined $branch);
2516 } else {
2517 $type = "log of $branch" if (defined $branch);
2520 $res{-title} = $type;
2521 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
2522 $res{'file_name'} = $file_name;
2524 return %res;
2527 ## ----------------------------------------------------------------------
2528 ## git utility subroutines, invoking git commands
2530 # returns path to the core git executable and the --git-dir parameter as list
2531 sub git_cmd {
2532 $number_of_git_cmds++;
2533 return $GIT, '--git-dir='.$git_dir;
2536 # quote the given arguments for passing them to the shell
2537 # quote_command("command", "arg 1", "arg with ' and ! characters")
2538 # => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
2539 # Try to avoid using this function wherever possible.
2540 sub quote_command {
2541 return join(' ',
2542 map { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ );
2545 # get HEAD ref of given project as hash
2546 sub git_get_head_hash {
2547 return git_get_full_hash(shift, 'HEAD');
2550 sub git_get_full_hash {
2551 return git_get_hash(@_);
2554 sub git_get_short_hash {
2555 return git_get_hash(@_, '--short=7');
2558 sub git_get_hash {
2559 my ($project, $hash, @options) = @_;
2560 my $o_git_dir = $git_dir;
2561 my $retval = undef;
2562 $git_dir = "$projectroot/$project";
2563 if (open my $fd, '-|', git_cmd(), 'rev-parse',
2564 '--verify', '-q', @options, $hash) {
2565 $retval = <$fd>;
2566 chomp $retval if defined $retval;
2567 close $fd;
2569 if (defined $o_git_dir) {
2570 $git_dir = $o_git_dir;
2572 return $retval;
2575 # get type of given object
2576 sub git_get_type {
2577 my $hash = shift;
2579 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
2580 my $type = <$fd>;
2581 close $fd or return;
2582 chomp $type;
2583 return $type;
2586 # repository configuration
2587 our $config_file = '';
2588 our %config;
2590 # store multiple values for single key as anonymous array reference
2591 # single values stored directly in the hash, not as [ <value> ]
2592 sub hash_set_multi {
2593 my ($hash, $key, $value) = @_;
2595 if (!exists $hash->{$key}) {
2596 $hash->{$key} = $value;
2597 } elsif (!ref $hash->{$key}) {
2598 $hash->{$key} = [ $hash->{$key}, $value ];
2599 } else {
2600 push @{$hash->{$key}}, $value;
2604 # return hash of git project configuration
2605 # optionally limited to some section, e.g. 'gitweb'
2606 sub git_parse_project_config {
2607 my $section_regexp = shift;
2608 my %config;
2610 local $/ = "\0";
2612 open my $fh, "-|", git_cmd(), "config", '-z', '-l',
2613 or return;
2615 while (my $keyval = <$fh>) {
2616 chomp $keyval;
2617 my ($key, $value) = split(/\n/, $keyval, 2);
2619 hash_set_multi(\%config, $key, $value)
2620 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
2622 close $fh;
2624 return %config;
2627 # convert config value to boolean: 'true' or 'false'
2628 # no value, number > 0, 'true' and 'yes' values are true
2629 # rest of values are treated as false (never as error)
2630 sub config_to_bool {
2631 my $val = shift;
2633 return 1 if !defined $val; # section.key
2635 # strip leading and trailing whitespace
2636 $val =~ s/^\s+//;
2637 $val =~ s/\s+$//;
2639 return (($val =~ /^\d+$/ && $val) || # section.key = 1
2640 ($val =~ /^(?:true|yes)$/i)); # section.key = true
2643 # convert config value to simple decimal number
2644 # an optional value suffix of 'k', 'm', or 'g' will cause the value
2645 # to be multiplied by 1024, 1048576, or 1073741824
2646 sub config_to_int {
2647 my $val = shift;
2649 # strip leading and trailing whitespace
2650 $val =~ s/^\s+//;
2651 $val =~ s/\s+$//;
2653 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
2654 $unit = lc($unit);
2655 # unknown unit is treated as 1
2656 return $num * ($unit eq 'g' ? 1073741824 :
2657 $unit eq 'm' ? 1048576 :
2658 $unit eq 'k' ? 1024 : 1);
2660 return $val;
2663 # convert config value to array reference, if needed
2664 sub config_to_multi {
2665 my $val = shift;
2667 return ref($val) ? $val : (defined($val) ? [ $val ] : []);
2670 sub git_get_project_config {
2671 my ($key, $type) = @_;
2673 return unless defined $git_dir;
2675 # key sanity check
2676 return unless ($key);
2677 # only subsection, if exists, is case sensitive,
2678 # and not lowercased by 'git config -z -l'
2679 if (my ($hi, $mi, $lo) = ($key =~ /^([^.]*)\.(.*)\.([^.]*)$/)) {
2680 $key = join(".", lc($hi), $mi, lc($lo));
2681 } else {
2682 $key = lc($key);
2684 $key =~ s/^gitweb\.//;
2685 return if ($key =~ m/\W/);
2687 # type sanity check
2688 if (defined $type) {
2689 $type =~ s/^--//;
2690 $type = undef
2691 unless ($type eq 'bool' || $type eq 'int');
2694 # get config
2695 if (!defined $config_file ||
2696 $config_file ne "$git_dir/config") {
2697 %config = git_parse_project_config('gitweb');
2698 $config_file = "$git_dir/config";
2701 # check if config variable (key) exists
2702 return unless exists $config{"gitweb.$key"};
2704 # ensure given type
2705 if (!defined $type) {
2706 return $config{"gitweb.$key"};
2707 } elsif ($type eq 'bool') {
2708 # backward compatibility: 'git config --bool' returns true/false
2709 return config_to_bool($config{"gitweb.$key"}) ? 'true' : 'false';
2710 } elsif ($type eq 'int') {
2711 return config_to_int($config{"gitweb.$key"});
2713 return $config{"gitweb.$key"};
2716 # get hash of given path at given ref
2717 sub git_get_hash_by_path {
2718 my $base = shift;
2719 my $path = shift || return undef;
2720 my $type = shift;
2722 $path =~ s,/+$,,;
2724 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
2725 or die_error(500, "Open git-ls-tree failed");
2726 my $line = <$fd>;
2727 close $fd or return undef;
2729 if (!defined $line) {
2730 # there is no tree or hash given by $path at $base
2731 return undef;
2734 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2735 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
2736 if (defined $type && $type ne $2) {
2737 # type doesn't match
2738 return undef;
2740 return $3;
2743 # get path of entry with given hash at given tree-ish (ref)
2744 # used to get 'from' filename for combined diff (merge commit) for renames
2745 sub git_get_path_by_hash {
2746 my $base = shift || return;
2747 my $hash = shift || return;
2749 local $/ = "\0";
2751 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
2752 or return undef;
2753 while (my $line = <$fd>) {
2754 chomp $line;
2756 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
2757 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
2758 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
2759 close $fd;
2760 return $1;
2763 close $fd;
2764 return undef;
2767 ## ......................................................................
2768 ## git utility functions, directly accessing git repository
2770 # get the value of config variable either from file named as the variable
2771 # itself in the repository ($GIT_DIR/$name file), or from gitweb.$name
2772 # configuration variable in the repository config file.
2773 sub git_get_file_or_project_config {
2774 my ($path, $name) = @_;
2776 $git_dir = "$projectroot/$path";
2777 open my $fd, '<', "$git_dir/$name"
2778 or return git_get_project_config($name);
2779 my $conf = <$fd>;
2780 close $fd;
2781 if (defined $conf) {
2782 chomp $conf;
2784 return $conf;
2787 sub git_get_project_description {
2788 my $path = shift;
2789 return git_get_file_or_project_config($path, 'description');
2792 sub git_get_project_category {
2793 my $path = shift;
2794 return git_get_file_or_project_config($path, 'category');
2798 # supported formats:
2799 # * $GIT_DIR/ctags/<tagname> file (in 'ctags' subdirectory)
2800 # - if its contents is a number, use it as tag weight,
2801 # - otherwise add a tag with weight 1
2802 # * $GIT_DIR/ctags file, each line is a tag (with weight 1)
2803 # the same value multiple times increases tag weight
2804 # * `gitweb.ctag' multi-valued repo config variable
2805 sub git_get_project_ctags {
2806 my $project = shift;
2807 my $ctags = {};
2809 $git_dir = "$projectroot/$project";
2810 if (opendir my $dh, "$git_dir/ctags") {
2811 my @files = grep { -f $_ } map { "$git_dir/ctags/$_" } readdir($dh);
2812 foreach my $tagfile (@files) {
2813 open my $ct, '<', $tagfile
2814 or next;
2815 my $val = <$ct>;
2816 chomp $val if $val;
2817 close $ct;
2819 (my $ctag = $tagfile) =~ s#.*/##;
2820 if ($val =~ /^\d+$/) {
2821 $ctags->{$ctag} = $val;
2822 } else {
2823 $ctags->{$ctag} = 1;
2826 closedir $dh;
2828 } elsif (open my $fh, '<', "$git_dir/ctags") {
2829 while (my $line = <$fh>) {
2830 chomp $line;
2831 $ctags->{$line}++ if $line;
2833 close $fh;
2835 } else {
2836 my $taglist = config_to_multi(git_get_project_config('ctag'));
2837 foreach my $tag (@$taglist) {
2838 $ctags->{$tag}++;
2842 return $ctags;
2845 # return hash, where keys are content tags ('ctags'),
2846 # and values are sum of weights of given tag in every project
2847 sub git_gather_all_ctags {
2848 my $projects = shift;
2849 my $ctags = {};
2851 foreach my $p (@$projects) {
2852 foreach my $ct (keys %{$p->{'ctags'}}) {
2853 $ctags->{$ct} += $p->{'ctags'}->{$ct};
2857 return $ctags;
2860 sub git_populate_project_tagcloud {
2861 my $ctags = shift;
2863 # First, merge different-cased tags; tags vote on casing
2864 my %ctags_lc;
2865 foreach (keys %$ctags) {
2866 $ctags_lc{lc $_}->{count} += $ctags->{$_};
2867 if (not $ctags_lc{lc $_}->{topcount}
2868 or $ctags_lc{lc $_}->{topcount} < $ctags->{$_}) {
2869 $ctags_lc{lc $_}->{topcount} = $ctags->{$_};
2870 $ctags_lc{lc $_}->{topname} = $_;
2874 my $cloud;
2875 my $matched = $input_params{'ctag'};
2876 if (eval { require HTML::TagCloud; 1; }) {
2877 $cloud = HTML::TagCloud->new;
2878 foreach my $ctag (sort keys %ctags_lc) {
2879 # Pad the title with spaces so that the cloud looks
2880 # less crammed.
2881 my $title = esc_html($ctags_lc{$ctag}->{topname});
2882 $title =~ s/ /&nbsp;/g;
2883 $title =~ s/^/&nbsp;/g;
2884 $title =~ s/$/&nbsp;/g;
2885 if (defined $matched && $matched eq $ctag) {
2886 $title = qq(<span class="match">$title</span>);
2888 $cloud->add($title, href(project=>undef, ctag=>$ctag),
2889 $ctags_lc{$ctag}->{count});
2891 } else {
2892 $cloud = {};
2893 foreach my $ctag (keys %ctags_lc) {
2894 my $title = esc_html($ctags_lc{$ctag}->{topname}, -nbsp=>1);
2895 if (defined $matched && $matched eq $ctag) {
2896 $title = qq(<span class="match">$title</span>);
2898 $cloud->{$ctag}{count} = $ctags_lc{$ctag}->{count};
2899 $cloud->{$ctag}{ctag} =
2900 $cgi->a({-href=>href(project=>undef, ctag=>$ctag)}, $title);
2903 return $cloud;
2906 sub git_show_project_tagcloud {
2907 my ($cloud, $count) = @_;
2908 if (ref $cloud eq 'HTML::TagCloud') {
2909 return $cloud->html_and_css($count);
2910 } else {
2911 my @tags = sort { $cloud->{$a}->{'count'} <=> $cloud->{$b}->{'count'} } keys %$cloud;
2912 return
2913 '<div id="htmltagcloud"'.($project ? '' : ' align="center"').'>' .
2914 join (', ', map {
2915 $cloud->{$_}->{'ctag'}
2916 } splice(@tags, 0, $count)) .
2917 '</div>';
2921 sub git_get_project_url_list {
2922 my $path = shift;
2924 $git_dir = "$projectroot/$path";
2925 open my $fd, '<', "$git_dir/cloneurl"
2926 or return wantarray ?
2927 @{ config_to_multi(git_get_project_config('url')) } :
2928 config_to_multi(git_get_project_config('url'));
2929 my @git_project_url_list = map { chomp; $_ } <$fd>;
2930 close $fd;
2932 return wantarray ? @git_project_url_list : \@git_project_url_list;
2935 sub git_get_projects_list {
2936 my $filter = shift || '';
2937 my $paranoid = shift;
2938 my @list;
2940 if (-d $projects_list) {
2941 # search in directory
2942 my $dir = $projects_list;
2943 # remove the trailing "/"
2944 $dir =~ s!/+$!!;
2945 my $pfxlen = length("$dir");
2946 my $pfxdepth = ($dir =~ tr!/!!);
2947 # when filtering, search only given subdirectory
2948 if ($filter && !$paranoid) {
2949 $dir .= "/$filter";
2950 $dir =~ s!/+$!!;
2953 File::Find::find({
2954 follow_fast => 1, # follow symbolic links
2955 follow_skip => 2, # ignore duplicates
2956 dangling_symlinks => 0, # ignore dangling symlinks, silently
2957 wanted => sub {
2958 # global variables
2959 our $project_maxdepth;
2960 our $projectroot;
2961 # skip project-list toplevel, if we get it.
2962 return if (m!^[/.]$!);
2963 # only directories can be git repositories
2964 return unless (-d $_);
2965 # don't traverse too deep (Find is super slow on os x)
2966 # $project_maxdepth excludes depth of $projectroot
2967 if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
2968 $File::Find::prune = 1;
2969 return;
2972 my $path = substr($File::Find::name, $pfxlen + 1);
2973 # paranoidly only filter here
2974 if ($paranoid && $filter && $path !~ m!^\Q$filter\E/!) {
2975 next;
2977 # we check related file in $projectroot
2978 if (check_export_ok("$projectroot/$path")) {
2979 push @list, { path => $path };
2980 $File::Find::prune = 1;
2983 }, "$dir");
2985 } elsif (-f $projects_list) {
2986 # read from file(url-encoded):
2987 # 'git%2Fgit.git Linus+Torvalds'
2988 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2989 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2990 open my $fd, '<', $projects_list or return;
2991 PROJECT:
2992 while (my $line = <$fd>) {
2993 chomp $line;
2994 my ($path, $owner) = split ' ', $line;
2995 $path = unescape($path);
2996 $owner = unescape($owner);
2997 if (!defined $path) {
2998 next;
3000 # if $filter is rpovided, check if $path begins with $filter
3001 if ($filter && $path !~ m!^\Q$filter\E/!) {
3002 next;
3004 if (check_export_ok("$projectroot/$path")) {
3005 my $pr = {
3006 path => $path
3008 if ($owner) {
3009 $pr->{'owner'} = to_utf8($owner);
3011 push @list, $pr;
3014 close $fd;
3016 return @list;
3019 # written with help of Tree::Trie module (Perl Artistic License, GPL compatibile)
3020 # as side effects it sets 'forks' field to list of forks for forked projects
3021 sub filter_forks_from_projects_list {
3022 my $projects = shift;
3024 my %trie; # prefix tree of directories (path components)
3025 # generate trie out of those directories that might contain forks
3026 foreach my $pr (@$projects) {
3027 my $path = $pr->{'path'};
3028 $path =~ s/\.git$//; # forks of 'repo.git' are in 'repo/' directory
3029 next if ($path =~ m!/$!); # skip non-bare repositories, e.g. 'repo/.git'
3030 next unless ($path); # skip '.git' repository: tests, git-instaweb
3031 next unless (-d "$projectroot/$path"); # containing directory exists
3032 $pr->{'forks'} = []; # there can be 0 or more forks of project
3034 # add to trie
3035 my @dirs = split('/', $path);
3036 # walk the trie, until either runs out of components or out of trie
3037 my $ref = \%trie;
3038 while (scalar @dirs &&
3039 exists($ref->{$dirs[0]})) {
3040 $ref = $ref->{shift @dirs};
3042 # create rest of trie structure from rest of components
3043 foreach my $dir (@dirs) {
3044 $ref = $ref->{$dir} = {};
3046 # create end marker, store $pr as a data
3047 $ref->{''} = $pr if (!exists $ref->{''});
3050 # filter out forks, by finding shortest prefix match for paths
3051 my @filtered;
3052 PROJECT:
3053 foreach my $pr (@$projects) {
3054 # trie lookup
3055 my $ref = \%trie;
3056 DIR:
3057 foreach my $dir (split('/', $pr->{'path'})) {
3058 if (exists $ref->{''}) {
3059 # found [shortest] prefix, is a fork - skip it
3060 push @{$ref->{''}{'forks'}}, $pr;
3061 next PROJECT;
3063 if (!exists $ref->{$dir}) {
3064 # not in trie, cannot have prefix, not a fork
3065 push @filtered, $pr;
3066 next PROJECT;
3068 # If the dir is there, we just walk one step down the trie.
3069 $ref = $ref->{$dir};
3071 # we ran out of trie
3072 # (shouldn't happen: it's either no match, or end marker)
3073 push @filtered, $pr;
3076 return @filtered;
3079 # note: fill_project_list_info must be run first,
3080 # for 'descr_long' and 'ctags' to be filled
3081 sub search_projects_list {
3082 my ($projlist, %opts) = @_;
3083 my $tagfilter = $opts{'tagfilter'};
3084 my $search_re = $opts{'search_regexp'};
3086 return @$projlist
3087 unless ($tagfilter || $search_re);
3089 # searching projects require filling to be run before it;
3090 fill_project_list_info($projlist,
3091 $tagfilter ? 'ctags' : (),
3092 $search_re ? ('path', 'descr') : ());
3093 my @projects;
3094 PROJECT:
3095 foreach my $pr (@$projlist) {
3097 if ($tagfilter) {
3098 next unless ref($pr->{'ctags'}) eq 'HASH';
3099 next unless
3100 grep { lc($_) eq lc($tagfilter) } keys %{$pr->{'ctags'}};
3103 if ($search_re) {
3104 next unless
3105 $pr->{'path'} =~ /$search_re/ ||
3106 $pr->{'descr_long'} =~ /$search_re/;
3109 push @projects, $pr;
3112 return @projects;
3115 our $gitweb_project_owner = undef;
3116 sub git_get_project_list_from_file {
3118 return if (defined $gitweb_project_owner);
3120 $gitweb_project_owner = {};
3121 # read from file (url-encoded):
3122 # 'git%2Fgit.git Linus+Torvalds'
3123 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
3124 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
3125 if (-f $projects_list) {
3126 open(my $fd, '<', $projects_list);
3127 while (my $line = <$fd>) {
3128 chomp $line;
3129 my ($pr, $ow) = split ' ', $line;
3130 $pr = unescape($pr);
3131 $ow = unescape($ow);
3132 $gitweb_project_owner->{$pr} = to_utf8($ow);
3134 close $fd;
3138 sub git_get_project_owner {
3139 my $project = shift;
3140 my $owner;
3142 return undef unless $project;
3143 $git_dir = "$projectroot/$project";
3145 if (!defined $gitweb_project_owner) {
3146 git_get_project_list_from_file();
3149 if (exists $gitweb_project_owner->{$project}) {
3150 $owner = $gitweb_project_owner->{$project};
3152 if (!defined $owner){
3153 $owner = git_get_project_config('owner');
3155 if (!defined $owner) {
3156 $owner = get_file_owner("$git_dir");
3159 return $owner;
3162 sub git_get_last_activity {
3163 my ($path) = @_;
3164 my $fd;
3166 $git_dir = "$projectroot/$path";
3167 open($fd, "-|", git_cmd(), 'for-each-ref',
3168 '--format=%(committer)',
3169 '--sort=-committerdate',
3170 '--count=1',
3171 'refs/heads') or return;
3172 my $most_recent = <$fd>;
3173 close $fd or return;
3174 if (defined $most_recent &&
3175 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
3176 my $timestamp = $1;
3177 my $age = time - $timestamp;
3178 return ($age, age_string($age));
3180 return (undef, undef);
3183 # Implementation note: when a single remote is wanted, we cannot use 'git
3184 # remote show -n' because that command always work (assuming it's a remote URL
3185 # if it's not defined), and we cannot use 'git remote show' because that would
3186 # try to make a network roundtrip. So the only way to find if that particular
3187 # remote is defined is to walk the list provided by 'git remote -v' and stop if
3188 # and when we find what we want.
3189 sub git_get_remotes_list {
3190 my $wanted = shift;
3191 my %remotes = ();
3193 open my $fd, '-|' , git_cmd(), 'remote', '-v';
3194 return unless $fd;
3195 while (my $remote = <$fd>) {
3196 chomp $remote;
3197 $remote =~ s!\t(.*?)\s+\((\w+)\)$!!;
3198 next if $wanted and not $remote eq $wanted;
3199 my ($url, $key) = ($1, $2);
3201 $remotes{$remote} ||= { 'heads' => () };
3202 $remotes{$remote}{$key} = $url;
3204 close $fd or return;
3205 return wantarray ? %remotes : \%remotes;
3208 # Takes a hash of remotes as first parameter and fills it by adding the
3209 # available remote heads for each of the indicated remotes.
3210 sub fill_remote_heads {
3211 my $remotes = shift;
3212 my @heads = map { "remotes/$_" } keys %$remotes;
3213 my @remoteheads = git_get_heads_list(undef, @heads);
3214 foreach my $remote (keys %$remotes) {
3215 $remotes->{$remote}{'heads'} = [ grep {
3216 $_->{'name'} =~ s!^$remote/!!
3217 } @remoteheads ];
3221 sub git_get_references {
3222 my $type = shift || "";
3223 my %refs;
3224 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
3225 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
3226 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
3227 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
3228 or return;
3230 while (my $line = <$fd>) {
3231 chomp $line;
3232 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {
3233 if (defined $refs{$1}) {
3234 push @{$refs{$1}}, $2;
3235 } else {
3236 $refs{$1} = [ $2 ];
3240 close $fd or return;
3241 return \%refs;
3244 sub git_get_rev_name_tags {
3245 my $hash = shift || return undef;
3247 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
3248 or return;
3249 my $name_rev = <$fd>;
3250 close $fd;
3252 if ($name_rev =~ m|^$hash tags/(.*)$|) {
3253 return $1;
3254 } else {
3255 # catches also '$hash undefined' output
3256 return undef;
3260 ## ----------------------------------------------------------------------
3261 ## parse to hash functions
3263 sub parse_date {
3264 my $epoch = shift;
3265 my $tz = shift || "-0000";
3267 my %date;
3268 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
3269 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
3270 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
3271 $date{'hour'} = $hour;
3272 $date{'minute'} = $min;
3273 $date{'mday'} = $mday;
3274 $date{'day'} = $days[$wday];
3275 $date{'month'} = $months[$mon];
3276 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
3277 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
3278 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
3279 $mday, $months[$mon], $hour ,$min;
3280 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
3281 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
3283 my ($tz_sign, $tz_hour, $tz_min) =
3284 ($tz =~ m/^([-+])(\d\d)(\d\d)$/);
3285 $tz_sign = ($tz_sign eq '-' ? -1 : +1);
3286 my $local = $epoch + $tz_sign*((($tz_hour*60) + $tz_min)*60);
3287 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
3288 $date{'hour_local'} = $hour;
3289 $date{'minute_local'} = $min;
3290 $date{'tz_local'} = $tz;
3291 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
3292 1900+$year, $mon+1, $mday,
3293 $hour, $min, $sec, $tz);
3294 return %date;
3297 sub parse_tag {
3298 my $tag_id = shift;
3299 my %tag;
3300 my @comment;
3302 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
3303 $tag{'id'} = $tag_id;
3304 while (my $line = <$fd>) {
3305 chomp $line;
3306 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
3307 $tag{'object'} = $1;
3308 } elsif ($line =~ m/^type (.+)$/) {
3309 $tag{'type'} = $1;
3310 } elsif ($line =~ m/^tag (.+)$/) {
3311 $tag{'name'} = $1;
3312 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
3313 $tag{'author'} = $1;
3314 $tag{'author_epoch'} = $2;
3315 $tag{'author_tz'} = $3;
3316 if ($tag{'author'} =~ m/^([^<]+) <([^>]*)>/) {
3317 $tag{'author_name'} = $1;
3318 $tag{'author_email'} = $2;
3319 } else {
3320 $tag{'author_name'} = $tag{'author'};
3322 } elsif ($line =~ m/--BEGIN/) {
3323 push @comment, $line;
3324 last;
3325 } elsif ($line eq "") {
3326 last;
3329 push @comment, <$fd>;
3330 $tag{'comment'} = \@comment;
3331 close $fd or return;
3332 if (!defined $tag{'name'}) {
3333 return
3335 return %tag
3338 sub parse_commit_text {
3339 my ($commit_text, $withparents) = @_;
3340 my @commit_lines = split '\n', $commit_text;
3341 my %co;
3343 pop @commit_lines; # Remove '\0'
3345 if (! @commit_lines) {
3346 return;
3349 my $header = shift @commit_lines;
3350 if ($header !~ m/^[0-9a-fA-F]{40}/) {
3351 return;
3353 ($co{'id'}, my @parents) = split ' ', $header;
3354 while (my $line = shift @commit_lines) {
3355 last if $line eq "\n";
3356 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
3357 $co{'tree'} = $1;
3358 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
3359 push @parents, $1;
3360 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
3361 $co{'author'} = to_utf8($1);
3362 $co{'author_epoch'} = $2;
3363 $co{'author_tz'} = $3;
3364 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
3365 $co{'author_name'} = $1;
3366 $co{'author_email'} = $2;
3367 } else {
3368 $co{'author_name'} = $co{'author'};
3370 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
3371 $co{'committer'} = to_utf8($1);
3372 $co{'committer_epoch'} = $2;
3373 $co{'committer_tz'} = $3;
3374 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
3375 $co{'committer_name'} = $1;
3376 $co{'committer_email'} = $2;
3377 } else {
3378 $co{'committer_name'} = $co{'committer'};
3382 if (!defined $co{'tree'}) {
3383 return;
3385 $co{'parents'} = \@parents;
3386 $co{'parent'} = $parents[0];
3388 foreach my $title (@commit_lines) {
3389 $title =~ s/^ //;
3390 if ($title ne "") {
3391 $co{'title'} = chop_str($title, 80, 5);
3392 # remove leading stuff of merges to make the interesting part visible
3393 if (length($title) > 50) {
3394 $title =~ s/^Automatic //;
3395 $title =~ s/^merge (of|with) /Merge ... /i;
3396 if (length($title) > 50) {
3397 $title =~ s/(http|rsync):\/\///;
3399 if (length($title) > 50) {
3400 $title =~ s/(master|www|rsync)\.//;
3402 if (length($title) > 50) {
3403 $title =~ s/kernel.org:?//;
3405 if (length($title) > 50) {
3406 $title =~ s/\/pub\/scm//;
3409 $co{'title_short'} = chop_str($title, 50, 5);
3410 last;
3413 if (! defined $co{'title'} || $co{'title'} eq "") {
3414 $co{'title'} = $co{'title_short'} = '(no commit message)';
3416 # remove added spaces
3417 foreach my $line (@commit_lines) {
3418 $line =~ s/^ //;
3420 $co{'comment'} = \@commit_lines;
3422 my $age = time - $co{'committer_epoch'};
3423 $co{'age'} = $age;
3424 $co{'age_string'} = age_string($age);
3425 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
3426 if ($age > 60*60*24*7*2) {
3427 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
3428 $co{'age_string_age'} = $co{'age_string'};
3429 } else {
3430 $co{'age_string_date'} = $co{'age_string'};
3431 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
3433 return %co;
3436 sub parse_commit {
3437 my ($commit_id) = @_;
3438 my %co;
3440 local $/ = "\0";
3442 open my $fd, "-|", git_cmd(), "rev-list",
3443 "--parents",
3444 "--header",
3445 "--max-count=1",
3446 $commit_id,
3447 "--",
3448 or die_error(500, "Open git-rev-list failed");
3449 %co = parse_commit_text(<$fd>, 1);
3450 close $fd;
3452 return %co;
3455 sub parse_commits {
3456 my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
3457 my @cos;
3459 $maxcount ||= 1;
3460 $skip ||= 0;
3462 local $/ = "\0";
3464 open my $fd, "-|", git_cmd(), "rev-list",
3465 "--header",
3466 @args,
3467 ("--max-count=" . $maxcount),
3468 ("--skip=" . $skip),
3469 @extra_options,
3470 $commit_id,
3471 "--",
3472 ($filename ? ($filename) : ())
3473 or die_error(500, "Open git-rev-list failed");
3474 while (my $line = <$fd>) {
3475 my %co = parse_commit_text($line);
3476 push @cos, \%co;
3478 close $fd;
3480 return wantarray ? @cos : \@cos;
3483 # parse line of git-diff-tree "raw" output
3484 sub parse_difftree_raw_line {
3485 my $line = shift;
3486 my %res;
3488 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
3489 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
3490 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
3491 $res{'from_mode'} = $1;
3492 $res{'to_mode'} = $2;
3493 $res{'from_id'} = $3;
3494 $res{'to_id'} = $4;
3495 $res{'status'} = $5;
3496 $res{'similarity'} = $6;
3497 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
3498 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
3499 } else {
3500 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote($7);
3503 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
3504 # combined diff (for merge commit)
3505 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
3506 $res{'nparents'} = length($1);
3507 $res{'from_mode'} = [ split(' ', $2) ];
3508 $res{'to_mode'} = pop @{$res{'from_mode'}};
3509 $res{'from_id'} = [ split(' ', $3) ];
3510 $res{'to_id'} = pop @{$res{'from_id'}};
3511 $res{'status'} = [ split('', $4) ];
3512 $res{'to_file'} = unquote($5);
3514 # 'c512b523472485aef4fff9e57b229d9d243c967f'
3515 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
3516 $res{'commit'} = $1;
3519 return wantarray ? %res : \%res;
3522 # wrapper: return parsed line of git-diff-tree "raw" output
3523 # (the argument might be raw line, or parsed info)
3524 sub parsed_difftree_line {
3525 my $line_or_ref = shift;
3527 if (ref($line_or_ref) eq "HASH") {
3528 # pre-parsed (or generated by hand)
3529 return $line_or_ref;
3530 } else {
3531 return parse_difftree_raw_line($line_or_ref);
3535 # parse line of git-ls-tree output
3536 sub parse_ls_tree_line {
3537 my $line = shift;
3538 my %opts = @_;
3539 my %res;
3541 if ($opts{'-l'}) {
3542 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'
3543 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;
3545 $res{'mode'} = $1;
3546 $res{'type'} = $2;
3547 $res{'hash'} = $3;
3548 $res{'size'} = $4;
3549 if ($opts{'-z'}) {
3550 $res{'name'} = $5;
3551 } else {
3552 $res{'name'} = unquote($5);
3554 } else {
3555 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
3556 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
3558 $res{'mode'} = $1;
3559 $res{'type'} = $2;
3560 $res{'hash'} = $3;
3561 if ($opts{'-z'}) {
3562 $res{'name'} = $4;
3563 } else {
3564 $res{'name'} = unquote($4);
3568 return wantarray ? %res : \%res;
3571 # generates _two_ hashes, references to which are passed as 2 and 3 argument
3572 sub parse_from_to_diffinfo {
3573 my ($diffinfo, $from, $to, @parents) = @_;
3575 if ($diffinfo->{'nparents'}) {
3576 # combined diff
3577 $from->{'file'} = [];
3578 $from->{'href'} = [];
3579 fill_from_file_info($diffinfo, @parents)
3580 unless exists $diffinfo->{'from_file'};
3581 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
3582 $from->{'file'}[$i] =
3583 defined $diffinfo->{'from_file'}[$i] ?
3584 $diffinfo->{'from_file'}[$i] :
3585 $diffinfo->{'to_file'};
3586 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
3587 $from->{'href'}[$i] = href(action=>"blob",
3588 hash_base=>$parents[$i],
3589 hash=>$diffinfo->{'from_id'}[$i],
3590 file_name=>$from->{'file'}[$i]);
3591 } else {
3592 $from->{'href'}[$i] = undef;
3595 } else {
3596 # ordinary (not combined) diff
3597 $from->{'file'} = $diffinfo->{'from_file'};
3598 if ($diffinfo->{'status'} ne "A") { # not new (added) file
3599 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
3600 hash=>$diffinfo->{'from_id'},
3601 file_name=>$from->{'file'});
3602 } else {
3603 delete $from->{'href'};
3607 $to->{'file'} = $diffinfo->{'to_file'};
3608 if (!is_deleted($diffinfo)) { # file exists in result
3609 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
3610 hash=>$diffinfo->{'to_id'},
3611 file_name=>$to->{'file'});
3612 } else {
3613 delete $to->{'href'};
3617 ## ......................................................................
3618 ## parse to array of hashes functions
3620 sub git_get_heads_list {
3621 my ($limit, @classes) = @_;
3622 @classes = ('heads') unless @classes;
3623 my @patterns = map { "refs/$_" } @classes;
3624 my @headslist;
3626 open my $fd, '-|', git_cmd(), 'for-each-ref',
3627 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
3628 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
3629 @patterns
3630 or return;
3631 while (my $line = <$fd>) {
3632 my %ref_item;
3634 chomp $line;
3635 my ($refinfo, $committerinfo) = split(/\0/, $line);
3636 my ($hash, $name, $title) = split(' ', $refinfo, 3);
3637 my ($committer, $epoch, $tz) =
3638 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
3639 $ref_item{'fullname'} = $name;
3640 $name =~ s!^refs/(?:head|remote)s/!!;
3642 $ref_item{'name'} = $name;
3643 $ref_item{'id'} = $hash;
3644 $ref_item{'title'} = $title || '(no commit message)';
3645 $ref_item{'epoch'} = $epoch;
3646 if ($epoch) {
3647 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
3648 } else {
3649 $ref_item{'age'} = "unknown";
3652 push @headslist, \%ref_item;
3654 close $fd;
3656 return wantarray ? @headslist : \@headslist;
3659 sub git_get_tags_list {
3660 my $limit = shift;
3661 my @tagslist;
3663 open my $fd, '-|', git_cmd(), 'for-each-ref',
3664 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
3665 '--format=%(objectname) %(objecttype) %(refname) '.
3666 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
3667 'refs/tags'
3668 or return;
3669 while (my $line = <$fd>) {
3670 my %ref_item;
3672 chomp $line;
3673 my ($refinfo, $creatorinfo) = split(/\0/, $line);
3674 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
3675 my ($creator, $epoch, $tz) =
3676 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
3677 $ref_item{'fullname'} = $name;
3678 $name =~ s!^refs/tags/!!;
3680 $ref_item{'type'} = $type;
3681 $ref_item{'id'} = $id;
3682 $ref_item{'name'} = $name;
3683 if ($type eq "tag") {
3684 $ref_item{'subject'} = $title;
3685 $ref_item{'reftype'} = $reftype;
3686 $ref_item{'refid'} = $refid;
3687 } else {
3688 $ref_item{'reftype'} = $type;
3689 $ref_item{'refid'} = $id;
3692 if ($type eq "tag" || $type eq "commit") {
3693 $ref_item{'epoch'} = $epoch;
3694 if ($epoch) {
3695 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
3696 } else {
3697 $ref_item{'age'} = "unknown";
3701 push @tagslist, \%ref_item;
3703 close $fd;
3705 return wantarray ? @tagslist : \@tagslist;
3708 ## ----------------------------------------------------------------------
3709 ## filesystem-related functions
3711 sub get_file_owner {
3712 my $path = shift;
3714 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
3715 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
3716 if (!defined $gcos) {
3717 return undef;
3719 my $owner = $gcos;
3720 $owner =~ s/[,;].*$//;
3721 return to_utf8($owner);
3724 # assume that file exists
3725 sub insert_file {
3726 my $filename = shift;
3728 open my $fd, '<', $filename;
3729 print map { to_utf8($_) } <$fd>;
3730 close $fd;
3733 ## ......................................................................
3734 ## mimetype related functions
3736 sub mimetype_guess_file {
3737 my $filename = shift;
3738 my $mimemap = shift;
3739 -r $mimemap or return undef;
3741 my %mimemap;
3742 open(my $mh, '<', $mimemap) or return undef;
3743 while (<$mh>) {
3744 next if m/^#/; # skip comments
3745 my ($mimetype, @exts) = split(/\s+/);
3746 foreach my $ext (@exts) {
3747 $mimemap{$ext} = $mimetype;
3750 close($mh);
3752 $filename =~ /\.([^.]*)$/;
3753 return $mimemap{$1};
3756 sub mimetype_guess {
3757 my $filename = shift;
3758 my $mime;
3759 $filename =~ /\./ or return undef;
3761 if ($mimetypes_file) {
3762 my $file = $mimetypes_file;
3763 if ($file !~ m!^/!) { # if it is relative path
3764 # it is relative to project
3765 $file = "$projectroot/$project/$file";
3767 $mime = mimetype_guess_file($filename, $file);
3769 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
3770 return $mime;
3773 sub blob_mimetype {
3774 my $fd = shift;
3775 my $filename = shift;
3777 if ($filename) {
3778 my $mime = mimetype_guess($filename);
3779 $mime and return $mime;
3782 # just in case
3783 return $default_blob_plain_mimetype unless $fd;
3785 if (-T $fd) {
3786 return 'text/plain';
3787 } elsif (! $filename) {
3788 return 'application/octet-stream';
3789 } elsif ($filename =~ m/\.png$/i) {
3790 return 'image/png';
3791 } elsif ($filename =~ m/\.gif$/i) {
3792 return 'image/gif';
3793 } elsif ($filename =~ m/\.jpe?g$/i) {
3794 return 'image/jpeg';
3795 } else {
3796 return 'application/octet-stream';
3800 sub blob_contenttype {
3801 my ($fd, $file_name, $type) = @_;
3803 $type ||= blob_mimetype($fd, $file_name);
3804 if ($type eq 'text/plain' && defined $default_text_plain_charset) {
3805 $type .= "; charset=$default_text_plain_charset";
3808 return $type;
3811 # guess file syntax for syntax highlighting; return undef if no highlighting
3812 # the name of syntax can (in the future) depend on syntax highlighter used
3813 sub guess_file_syntax {
3814 my ($highlight, $mimetype, $file_name) = @_;
3815 return undef unless ($highlight && defined $file_name);
3816 my $basename = basename($file_name, '.in');
3817 return $highlight_basename{$basename}
3818 if exists $highlight_basename{$basename};
3820 $basename =~ /\.([^.]*)$/;
3821 my $ext = $1 or return undef;
3822 return $highlight_ext{$ext}
3823 if exists $highlight_ext{$ext};
3825 return undef;
3828 # run highlighter and return FD of its output,
3829 # or return original FD if no highlighting
3830 sub run_highlighter {
3831 my ($fd, $highlight, $syntax) = @_;
3832 return $fd unless ($highlight && defined $syntax);
3834 close $fd;
3835 open $fd, quote_command(git_cmd(), "cat-file", "blob", $hash)." | ".
3836 quote_command($highlight_bin).
3837 " --replace-tabs=8 --fragment --syntax $syntax |"
3838 or die_error(500, "Couldn't open file or run syntax highlighter");
3839 return $fd;
3842 ## ======================================================================
3843 ## functions printing HTML: header, footer, error page
3845 sub get_page_title {
3846 my $title = to_utf8($site_name);
3848 unless (defined $project) {
3849 if (defined $project_filter) {
3850 $title .= " - projects in '" . esc_path($project_filter) . "'";
3852 return $title;
3854 $title .= " - " . to_utf8($project);
3856 return $title unless (defined $action);
3857 $title .= "/$action"; # $action is US-ASCII (7bit ASCII)
3859 return $title unless (defined $file_name);
3860 $title .= " - " . esc_path($file_name);
3861 if ($action eq "tree" && $file_name !~ m|/$|) {
3862 $title .= "/";
3865 return $title;
3868 sub get_content_type_html {
3869 # require explicit support from the UA if we are to send the page as
3870 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
3871 # we have to do this because MSIE sometimes globs '*/*', pretending to
3872 # support xhtml+xml but choking when it gets what it asked for.
3873 if (defined $cgi->http('HTTP_ACCEPT') &&
3874 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
3875 $cgi->Accept('application/xhtml+xml') != 0) {
3876 return 'application/xhtml+xml';
3877 } else {
3878 return 'text/html';
3882 sub print_feed_meta {
3883 if (defined $project) {
3884 my %href_params = get_feed_info();
3885 if (!exists $href_params{'-title'}) {
3886 $href_params{'-title'} = 'log';
3889 foreach my $format (qw(RSS Atom)) {
3890 my $type = lc($format);
3891 my %link_attr = (
3892 '-rel' => 'alternate',
3893 '-title' => esc_attr("$project - $href_params{'-title'} - $format feed"),
3894 '-type' => "application/$type+xml"
3897 $href_params{'action'} = $type;
3898 $link_attr{'-href'} = href(%href_params);
3899 print "<link ".
3900 "rel=\"$link_attr{'-rel'}\" ".
3901 "title=\"$link_attr{'-title'}\" ".
3902 "href=\"$link_attr{'-href'}\" ".
3903 "type=\"$link_attr{'-type'}\" ".
3904 "/>\n";
3906 $href_params{'extra_options'} = '--no-merges';
3907 $link_attr{'-href'} = href(%href_params);
3908 $link_attr{'-title'} .= ' (no merges)';
3909 print "<link ".
3910 "rel=\"$link_attr{'-rel'}\" ".
3911 "title=\"$link_attr{'-title'}\" ".
3912 "href=\"$link_attr{'-href'}\" ".
3913 "type=\"$link_attr{'-type'}\" ".
3914 "/>\n";
3917 } else {
3918 printf('<link rel="alternate" title="%s projects list" '.
3919 'href="%s" type="text/plain; charset=utf-8" />'."\n",
3920 esc_attr($site_name), href(project=>undef, action=>"project_index"));
3921 printf('<link rel="alternate" title="%s projects feeds" '.
3922 'href="%s" type="text/x-opml" />'."\n",
3923 esc_attr($site_name), href(project=>undef, action=>"opml"));
3927 sub print_header_links {
3928 my $status = shift;
3930 # print out each stylesheet that exist, providing backwards capability
3931 # for those people who defined $stylesheet in a config file
3932 if (defined $stylesheet) {
3933 print '<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";
3934 } else {
3935 foreach my $stylesheet (@stylesheets) {
3936 next unless $stylesheet;
3937 print '<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";
3940 print_feed_meta()
3941 if ($status eq '200 OK');
3942 if (defined $favicon) {
3943 print qq(<link rel="shortcut icon" href=").esc_url($favicon).qq(" type="image/png" />\n);
3947 sub print_nav_breadcrumbs_path {
3948 my $dirprefix = undef;
3949 while (my $part = shift) {
3950 $dirprefix .= "/" if defined $dirprefix;
3951 $dirprefix .= $part;
3952 print $cgi->a({-href => href(project => undef,
3953 project_filter => $dirprefix,
3954 action => "project_list")},
3955 esc_html($part)) . " / ";
3959 sub print_nav_breadcrumbs {
3960 my %opts = @_;
3962 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
3963 if (defined $project) {
3964 my @dirname = split '/', $project;
3965 my $projectbasename = pop @dirname;
3966 print_nav_breadcrumbs_path(@dirname);
3967 print $cgi->a({-href => href(action=>"summary")}, esc_html($projectbasename));
3968 if (defined $action) {
3969 my $action_print = $action ;
3970 if (defined $opts{-action_extra}) {
3971 $action_print = $cgi->a({-href => href(action=>$action)},
3972 $action);
3974 print " / $action_print";
3976 if (defined $opts{-action_extra}) {
3977 print " / $opts{-action_extra}";
3979 print "\n";
3980 } elsif (defined $project_filter) {
3981 print_nav_breadcrumbs_path(split '/', $project_filter);
3985 sub print_search_form {
3986 if (!defined $searchtext) {
3987 $searchtext = "";
3989 my $search_hash;
3990 if (defined $hash_base) {
3991 $search_hash = $hash_base;
3992 } elsif (defined $hash) {
3993 $search_hash = $hash;
3994 } else {
3995 $search_hash = "HEAD";
3997 my $action = $my_uri;
3998 my $use_pathinfo = gitweb_check_feature('pathinfo');
3999 if ($use_pathinfo) {
4000 $action .= "/".esc_url($project);
4002 print $cgi->startform(-method => "get", -action => $action) .
4003 "<div class=\"search\">\n" .
4004 (!$use_pathinfo &&
4005 $cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) . "\n") .
4006 $cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) . "\n" .
4007 $cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) . "\n" .
4008 $cgi->popup_menu(-name => 'st', -default => 'commit',
4009 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
4010 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
4011 " search:\n",
4012 $cgi->textfield(-name => "s", -value => $searchtext, -override => 1) . "\n" .
4013 "<span title=\"Extended regular expression\">" .
4014 $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
4015 -checked => $search_use_regexp) .
4016 "</span>" .
4017 "</div>" .
4018 $cgi->end_form() . "\n";
4021 sub git_header_html {
4022 my $status = shift || "200 OK";
4023 my $expires = shift;
4024 my %opts = @_;
4026 my $title = get_page_title();
4027 my $content_type = get_content_type_html();
4028 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
4029 -status=> $status, -expires => $expires)
4030 unless ($opts{'-no_http_header'});
4031 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
4032 print <<EOF;
4033 <?xml version="1.0" encoding="utf-8"?>
4034 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
4035 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
4036 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
4037 <!-- git core binaries version $git_version -->
4038 <head>
4039 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
4040 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
4041 <meta name="robots" content="index, nofollow"/>
4042 <title>$title</title>
4044 # the stylesheet, favicon etc urls won't work correctly with path_info
4045 # unless we set the appropriate base URL
4046 if ($ENV{'PATH_INFO'}) {
4047 print "<base href=\"".esc_url($base_url)."\" />\n";
4049 print_header_links($status);
4051 if (defined $site_html_head_string) {
4052 print to_utf8($site_html_head_string);
4055 print "</head>\n" .
4056 "<body>\n";
4058 if (defined $site_header && -f $site_header) {
4059 insert_file($site_header);
4062 print "<div class=\"page_header\">\n";
4063 if (defined $logo) {
4064 print $cgi->a({-href => esc_url($logo_url),
4065 -title => $logo_label},
4066 $cgi->img({-src => esc_url($logo),
4067 -width => 72, -height => 27,
4068 -alt => "git",
4069 -class => "logo"}));
4071 print_nav_breadcrumbs(%opts);
4072 print "</div>\n";
4074 my $have_search = gitweb_check_feature('search');
4075 if (defined $project && $have_search) {
4076 print_search_form();
4080 sub git_footer_html {
4081 my $feed_class = 'rss_logo';
4083 print "<div class=\"page_footer\">\n";
4084 if (defined $project) {
4085 my $descr = git_get_project_description($project);
4086 if (defined $descr) {
4087 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
4090 my %href_params = get_feed_info();
4091 if (!%href_params) {
4092 $feed_class .= ' generic';
4094 $href_params{'-title'} ||= 'log';
4096 foreach my $format (qw(RSS Atom)) {
4097 $href_params{'action'} = lc($format);
4098 print $cgi->a({-href => href(%href_params),
4099 -title => "$href_params{'-title'} $format feed",
4100 -class => $feed_class}, $format)."\n";
4103 } else {
4104 print $cgi->a({-href => href(project=>undef, action=>"opml",
4105 project_filter => $project_filter),
4106 -class => $feed_class}, "OPML") . " ";
4107 print $cgi->a({-href => href(project=>undef, action=>"project_index",
4108 project_filter => $project_filter),
4109 -class => $feed_class}, "TXT") . "\n";
4111 print "</div>\n"; # class="page_footer"
4113 if (defined $t0 && gitweb_check_feature('timed')) {
4114 print "<div id=\"generating_info\">\n";
4115 print 'This page took '.
4116 '<span id="generating_time" class="time_span">'.
4117 tv_interval($t0, [ gettimeofday() ]).
4118 ' seconds </span>'.
4119 ' and '.
4120 '<span id="generating_cmd">'.
4121 $number_of_git_cmds.
4122 '</span> git commands '.
4123 " to generate.\n";
4124 print "</div>\n"; # class="page_footer"
4127 if (defined $site_footer && -f $site_footer) {
4128 insert_file($site_footer);
4131 print qq!<script type="text/javascript" src="!.esc_url($javascript).qq!"></script>\n!;
4132 if (defined $action &&
4133 $action eq 'blame_incremental') {
4134 print qq!<script type="text/javascript">\n!.
4135 qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.
4136 qq! "!. href() .qq!");\n!.
4137 qq!</script>\n!;
4138 } else {
4139 my ($jstimezone, $tz_cookie, $datetime_class) =
4140 gitweb_get_feature('javascript-timezone');
4142 print qq!<script type="text/javascript">\n!.
4143 qq!window.onload = function () {\n!;
4144 if (gitweb_check_feature('javascript-actions')) {
4145 print qq! fixLinks();\n!;
4147 if ($jstimezone && $tz_cookie && $datetime_class) {
4148 print qq! var tz_cookie = { name: '$tz_cookie', expires: 14, path: '/' };\n!. # in days
4149 qq! onloadTZSetup('$jstimezone', tz_cookie, '$datetime_class');\n!;
4151 print qq!};\n!.
4152 qq!</script>\n!;
4155 print "</body>\n" .
4156 "</html>";
4159 # die_error(<http_status_code>, <error_message>[, <detailed_html_description>])
4160 # Example: die_error(404, 'Hash not found')
4161 # By convention, use the following status codes (as defined in RFC 2616):
4162 # 400: Invalid or missing CGI parameters, or
4163 # requested object exists but has wrong type.
4164 # 403: Requested feature (like "pickaxe" or "snapshot") not enabled on
4165 # this server or project.
4166 # 404: Requested object/revision/project doesn't exist.
4167 # 500: The server isn't configured properly, or
4168 # an internal error occurred (e.g. failed assertions caused by bugs), or
4169 # an unknown error occurred (e.g. the git binary died unexpectedly).
4170 # 503: The server is currently unavailable (because it is overloaded,
4171 # or down for maintenance). Generally, this is a temporary state.
4172 sub die_error {
4173 my $status = shift || 500;
4174 my $error = esc_html(shift) || "Internal Server Error";
4175 my $extra = shift;
4176 my %opts = @_;
4178 my %http_responses = (
4179 400 => '400 Bad Request',
4180 403 => '403 Forbidden',
4181 404 => '404 Not Found',
4182 500 => '500 Internal Server Error',
4183 503 => '503 Service Unavailable',
4185 git_header_html($http_responses{$status}, undef, %opts);
4186 print <<EOF;
4187 <div class="page_body">
4188 <br /><br />
4189 $status - $error
4190 <br />
4192 if (defined $extra) {
4193 print "<hr />\n" .
4194 "$extra\n";
4196 print "</div>\n";
4198 git_footer_html();
4199 goto DONE_GITWEB
4200 unless ($opts{'-error_handler'});
4203 ## ----------------------------------------------------------------------
4204 ## functions printing or outputting HTML: navigation
4206 sub git_print_page_nav {
4207 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
4208 $extra = '' if !defined $extra; # pager or formats
4210 my @navs = qw(summary shortlog log commit commitdiff tree);
4211 if ($suppress) {
4212 @navs = grep { $_ ne $suppress } @navs;
4215 my %arg = map { $_ => {action=>$_} } @navs;
4216 if (defined $head) {
4217 for (qw(commit commitdiff)) {
4218 $arg{$_}{'hash'} = $head;
4220 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
4221 for (qw(shortlog log)) {
4222 $arg{$_}{'hash'} = $head;
4227 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
4228 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
4230 my @actions = gitweb_get_feature('actions');
4231 my %repl = (
4232 '%' => '%',
4233 'n' => $project, # project name
4234 'f' => $git_dir, # project path within filesystem
4235 'h' => $treehead || '', # current hash ('h' parameter)
4236 'b' => $treebase || '', # hash base ('hb' parameter)
4238 while (@actions) {
4239 my ($label, $link, $pos) = splice(@actions,0,3);
4240 # insert
4241 @navs = map { $_ eq $pos ? ($_, $label) : $_ } @navs;
4242 # munch munch
4243 $link =~ s/%([%nfhb])/$repl{$1}/g;
4244 $arg{$label}{'_href'} = $link;
4247 print "<div class=\"page_nav\">\n" .
4248 (join " | ",
4249 map { $_ eq $current ?
4250 $_ : $cgi->a({-href => ($arg{$_}{_href} ? $arg{$_}{_href} : href(%{$arg{$_}}))}, "$_")
4251 } @navs);
4252 print "<br/>\n$extra<br/>\n" .
4253 "</div>\n";
4256 # returns a submenu for the nagivation of the refs views (tags, heads,
4257 # remotes) with the current view disabled and the remotes view only
4258 # available if the feature is enabled
4259 sub format_ref_views {
4260 my ($current) = @_;
4261 my @ref_views = qw{tags heads};
4262 push @ref_views, 'remotes' if gitweb_check_feature('remote_heads');
4263 return join " | ", map {
4264 $_ eq $current ? $_ :
4265 $cgi->a({-href => href(action=>$_)}, $_)
4266 } @ref_views
4269 sub format_paging_nav {
4270 my ($action, $page, $has_next_link) = @_;
4271 my $paging_nav;
4274 if ($page > 0) {
4275 $paging_nav .=
4276 $cgi->a({-href => href(-replay=>1, page=>undef)}, "first") .
4277 " &sdot; " .
4278 $cgi->a({-href => href(-replay=>1, page=>$page-1),
4279 -accesskey => "p", -title => "Alt-p"}, "prev");
4280 } else {
4281 $paging_nav .= "first &sdot; prev";
4284 if ($has_next_link) {
4285 $paging_nav .= " &sdot; " .
4286 $cgi->a({-href => href(-replay=>1, page=>$page+1),
4287 -accesskey => "n", -title => "Alt-n"}, "next");
4288 } else {
4289 $paging_nav .= " &sdot; next";
4292 return $paging_nav;
4295 ## ......................................................................
4296 ## functions printing or outputting HTML: div
4298 sub git_print_header_div {
4299 my ($action, $title, $hash, $hash_base) = @_;
4300 my %args = ();
4302 $args{'action'} = $action;
4303 $args{'hash'} = $hash if $hash;
4304 $args{'hash_base'} = $hash_base if $hash_base;
4306 print "<div class=\"header\">\n" .
4307 $cgi->a({-href => href(%args), -class => "title"},
4308 $title ? $title : $action) .
4309 "\n</div>\n";
4312 sub format_repo_url {
4313 my ($name, $url) = @_;
4314 return "<tr class=\"metadata_url\"><td>$name</td><td>$url</td></tr>\n";
4317 # Group output by placing it in a DIV element and adding a header.
4318 # Options for start_div() can be provided by passing a hash reference as the
4319 # first parameter to the function.
4320 # Options to git_print_header_div() can be provided by passing an array
4321 # reference. This must follow the options to start_div if they are present.
4322 # The content can be a scalar, which is output as-is, a scalar reference, which
4323 # is output after html escaping, an IO handle passed either as *handle or
4324 # *handle{IO}, or a function reference. In the latter case all following
4325 # parameters will be taken as argument to the content function call.
4326 sub git_print_section {
4327 my ($div_args, $header_args, $content);
4328 my $arg = shift;
4329 if (ref($arg) eq 'HASH') {
4330 $div_args = $arg;
4331 $arg = shift;
4333 if (ref($arg) eq 'ARRAY') {
4334 $header_args = $arg;
4335 $arg = shift;
4337 $content = $arg;
4339 print $cgi->start_div($div_args);
4340 git_print_header_div(@$header_args);
4342 if (ref($content) eq 'CODE') {
4343 $content->(@_);
4344 } elsif (ref($content) eq 'SCALAR') {
4345 print esc_html($$content);
4346 } elsif (ref($content) eq 'GLOB' or ref($content) eq 'IO::Handle') {
4347 print <$content>;
4348 } elsif (!ref($content) && defined($content)) {
4349 print $content;
4352 print $cgi->end_div;
4355 sub format_timestamp_html {
4356 my $date = shift;
4357 my $strtime = $date->{'rfc2822'};
4359 my (undef, undef, $datetime_class) =
4360 gitweb_get_feature('javascript-timezone');
4361 if ($datetime_class) {
4362 $strtime = qq!<span class="$datetime_class">$strtime</span>!;
4365 my $localtime_format = '(%02d:%02d %s)';
4366 if ($date->{'hour_local'} < 6) {
4367 $localtime_format = '(<span class="atnight">%02d:%02d</span> %s)';
4369 $strtime .= ' ' .
4370 sprintf($localtime_format,
4371 $date->{'hour_local'}, $date->{'minute_local'}, $date->{'tz_local'});
4373 return $strtime;
4376 # Outputs the author name and date in long form
4377 sub git_print_authorship {
4378 my $co = shift;
4379 my %opts = @_;
4380 my $tag = $opts{-tag} || 'div';
4381 my $author = $co->{'author_name'};
4383 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
4384 print "<$tag class=\"author_date\">" .
4385 format_search_author($author, "author", esc_html($author)) .
4386 " [".format_timestamp_html(\%ad)."]".
4387 git_get_avatar($co->{'author_email'}, -pad_before => 1) .
4388 "</$tag>\n";
4391 # Outputs table rows containing the full author or committer information,
4392 # in the format expected for 'commit' view (& similar).
4393 # Parameters are a commit hash reference, followed by the list of people
4394 # to output information for. If the list is empty it defaults to both
4395 # author and committer.
4396 sub git_print_authorship_rows {
4397 my $co = shift;
4398 # too bad we can't use @people = @_ || ('author', 'committer')
4399 my @people = @_;
4400 @people = ('author', 'committer') unless @people;
4401 foreach my $who (@people) {
4402 my %wd = parse_date($co->{"${who}_epoch"}, $co->{"${who}_tz"});
4403 print "<tr><td>$who</td><td>" .
4404 format_search_author($co->{"${who}_name"}, $who,
4405 esc_html($co->{"${who}_name"})) . " " .
4406 format_search_author($co->{"${who}_email"}, $who,
4407 esc_html("<" . $co->{"${who}_email"} . ">")) .
4408 "</td><td rowspan=\"2\">" .
4409 git_get_avatar($co->{"${who}_email"}, -size => 'double') .
4410 "</td></tr>\n" .
4411 "<tr>" .
4412 "<td></td><td>" .
4413 format_timestamp_html(\%wd) .
4414 "</td>" .
4415 "</tr>\n";
4419 sub git_print_page_path {
4420 my $name = shift;
4421 my $type = shift;
4422 my $hb = shift;
4425 print "<div class=\"page_path\">";
4426 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
4427 -title => 'tree root'}, to_utf8("[$project]"));
4428 print " / ";
4429 if (defined $name) {
4430 my @dirname = split '/', $name;
4431 my $basename = pop @dirname;
4432 my $fullname = '';
4434 foreach my $dir (@dirname) {
4435 $fullname .= ($fullname ? '/' : '') . $dir;
4436 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
4437 hash_base=>$hb),
4438 -title => $fullname}, esc_path($dir));
4439 print " / ";
4441 if (defined $type && $type eq 'blob') {
4442 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
4443 hash_base=>$hb),
4444 -title => $name}, esc_path($basename));
4445 } elsif (defined $type && $type eq 'tree') {
4446 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
4447 hash_base=>$hb),
4448 -title => $name}, esc_path($basename));
4449 print " / ";
4450 } else {
4451 print esc_path($basename);
4454 print "<br/></div>\n";
4457 sub git_print_log {
4458 my $log = shift;
4459 my %opts = @_;
4461 if ($opts{'-remove_title'}) {
4462 # remove title, i.e. first line of log
4463 shift @$log;
4465 # remove leading empty lines
4466 while (defined $log->[0] && $log->[0] eq "") {
4467 shift @$log;
4470 # print log
4471 my $signoff = 0;
4472 my $empty = 0;
4473 foreach my $line (@$log) {
4474 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
4475 $signoff = 1;
4476 $empty = 0;
4477 if (! $opts{'-remove_signoff'}) {
4478 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
4479 next;
4480 } else {
4481 # remove signoff lines
4482 next;
4484 } else {
4485 $signoff = 0;
4488 # print only one empty line
4489 # do not print empty line after signoff
4490 if ($line eq "") {
4491 next if ($empty || $signoff);
4492 $empty = 1;
4493 } else {
4494 $empty = 0;
4497 print format_log_line_html($line) . "<br/>\n";
4500 if ($opts{'-final_empty_line'}) {
4501 # end with single empty line
4502 print "<br/>\n" unless $empty;
4506 # return link target (what link points to)
4507 sub git_get_link_target {
4508 my $hash = shift;
4509 my $link_target;
4511 # read link
4512 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4513 or return;
4515 local $/ = undef;
4516 $link_target = <$fd>;
4518 close $fd
4519 or return;
4521 return $link_target;
4524 # given link target, and the directory (basedir) the link is in,
4525 # return target of link relative to top directory (top tree);
4526 # return undef if it is not possible (including absolute links).
4527 sub normalize_link_target {
4528 my ($link_target, $basedir) = @_;
4530 # absolute symlinks (beginning with '/') cannot be normalized
4531 return if (substr($link_target, 0, 1) eq '/');
4533 # normalize link target to path from top (root) tree (dir)
4534 my $path;
4535 if ($basedir) {
4536 $path = $basedir . '/' . $link_target;
4537 } else {
4538 # we are in top (root) tree (dir)
4539 $path = $link_target;
4542 # remove //, /./, and /../
4543 my @path_parts;
4544 foreach my $part (split('/', $path)) {
4545 # discard '.' and ''
4546 next if (!$part || $part eq '.');
4547 # handle '..'
4548 if ($part eq '..') {
4549 if (@path_parts) {
4550 pop @path_parts;
4551 } else {
4552 # link leads outside repository (outside top dir)
4553 return;
4555 } else {
4556 push @path_parts, $part;
4559 $path = join('/', @path_parts);
4561 return $path;
4564 # print tree entry (row of git_tree), but without encompassing <tr> element
4565 sub git_print_tree_entry {
4566 my ($t, $basedir, $hash_base, $have_blame) = @_;
4568 my %base_key = ();
4569 $base_key{'hash_base'} = $hash_base if defined $hash_base;
4571 # The format of a table row is: mode list link. Where mode is
4572 # the mode of the entry, list is the name of the entry, an href,
4573 # and link is the action links of the entry.
4575 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
4576 if (exists $t->{'size'}) {
4577 print "<td class=\"size\">$t->{'size'}</td>\n";
4579 if ($t->{'type'} eq "blob") {
4580 print "<td class=\"list\">" .
4581 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
4582 file_name=>"$basedir$t->{'name'}", %base_key),
4583 -class => "list"}, esc_path($t->{'name'}));
4584 if (S_ISLNK(oct $t->{'mode'})) {
4585 my $link_target = git_get_link_target($t->{'hash'});
4586 if ($link_target) {
4587 my $norm_target = normalize_link_target($link_target, $basedir);
4588 if (defined $norm_target) {
4589 print " -> " .
4590 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
4591 file_name=>$norm_target),
4592 -title => $norm_target}, esc_path($link_target));
4593 } else {
4594 print " -> " . esc_path($link_target);
4598 print "</td>\n";
4599 print "<td class=\"link\">";
4600 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
4601 file_name=>"$basedir$t->{'name'}", %base_key)},
4602 "blob");
4603 if ($have_blame) {
4604 print " | " .
4605 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
4606 file_name=>"$basedir$t->{'name'}", %base_key)},
4607 "blame");
4609 if (defined $hash_base) {
4610 print " | " .
4611 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
4612 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
4613 "history");
4615 print " | " .
4616 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
4617 file_name=>"$basedir$t->{'name'}")},
4618 "raw");
4619 print "</td>\n";
4621 } elsif ($t->{'type'} eq "tree") {
4622 print "<td class=\"list\">";
4623 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
4624 file_name=>"$basedir$t->{'name'}",
4625 %base_key)},
4626 esc_path($t->{'name'}));
4627 print "</td>\n";
4628 print "<td class=\"link\">";
4629 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
4630 file_name=>"$basedir$t->{'name'}",
4631 %base_key)},
4632 "tree");
4633 if (defined $hash_base) {
4634 print " | " .
4635 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
4636 file_name=>"$basedir$t->{'name'}")},
4637 "history");
4639 print "</td>\n";
4640 } else {
4641 # unknown object: we can only present history for it
4642 # (this includes 'commit' object, i.e. submodule support)
4643 print "<td class=\"list\">" .
4644 esc_path($t->{'name'}) .
4645 "</td>\n";
4646 print "<td class=\"link\">";
4647 if (defined $hash_base) {
4648 print $cgi->a({-href => href(action=>"history",
4649 hash_base=>$hash_base,
4650 file_name=>"$basedir$t->{'name'}")},
4651 "history");
4653 print "</td>\n";
4657 ## ......................................................................
4658 ## functions printing large fragments of HTML
4660 # get pre-image filenames for merge (combined) diff
4661 sub fill_from_file_info {
4662 my ($diff, @parents) = @_;
4664 $diff->{'from_file'} = [ ];
4665 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
4666 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
4667 if ($diff->{'status'}[$i] eq 'R' ||
4668 $diff->{'status'}[$i] eq 'C') {
4669 $diff->{'from_file'}[$i] =
4670 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
4674 return $diff;
4677 # is current raw difftree line of file deletion
4678 sub is_deleted {
4679 my $diffinfo = shift;
4681 return $diffinfo->{'to_id'} eq ('0' x 40);
4684 # does patch correspond to [previous] difftree raw line
4685 # $diffinfo - hashref of parsed raw diff format
4686 # $patchinfo - hashref of parsed patch diff format
4687 # (the same keys as in $diffinfo)
4688 sub is_patch_split {
4689 my ($diffinfo, $patchinfo) = @_;
4691 return defined $diffinfo && defined $patchinfo
4692 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
4696 sub git_difftree_body {
4697 my ($difftree, $hash, @parents) = @_;
4698 my ($parent) = $parents[0];
4699 my $have_blame = gitweb_check_feature('blame');
4700 print "<div class=\"list_head\">\n";
4701 if ($#{$difftree} > 10) {
4702 print(($#{$difftree} + 1) . " files changed:\n");
4704 print "</div>\n";
4706 print "<table class=\"" .
4707 (@parents > 1 ? "combined " : "") .
4708 "diff_tree\">\n";
4710 # header only for combined diff in 'commitdiff' view
4711 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
4712 if ($has_header) {
4713 # table header
4714 print "<thead><tr>\n" .
4715 "<th></th><th></th>\n"; # filename, patchN link
4716 for (my $i = 0; $i < @parents; $i++) {
4717 my $par = $parents[$i];
4718 print "<th>" .
4719 $cgi->a({-href => href(action=>"commitdiff",
4720 hash=>$hash, hash_parent=>$par),
4721 -title => 'commitdiff to parent number ' .
4722 ($i+1) . ': ' . substr($par,0,7)},
4723 $i+1) .
4724 "&nbsp;</th>\n";
4726 print "</tr></thead>\n<tbody>\n";
4729 my $alternate = 1;
4730 my $patchno = 0;
4731 foreach my $line (@{$difftree}) {
4732 my $diff = parsed_difftree_line($line);
4734 if ($alternate) {
4735 print "<tr class=\"dark\">\n";
4736 } else {
4737 print "<tr class=\"light\">\n";
4739 $alternate ^= 1;
4741 if (exists $diff->{'nparents'}) { # combined diff
4743 fill_from_file_info($diff, @parents)
4744 unless exists $diff->{'from_file'};
4746 if (!is_deleted($diff)) {
4747 # file exists in the result (child) commit
4748 print "<td>" .
4749 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4750 file_name=>$diff->{'to_file'},
4751 hash_base=>$hash),
4752 -class => "list"}, esc_path($diff->{'to_file'})) .
4753 "</td>\n";
4754 } else {
4755 print "<td>" .
4756 esc_path($diff->{'to_file'}) .
4757 "</td>\n";
4760 if ($action eq 'commitdiff') {
4761 # link to patch
4762 $patchno++;
4763 print "<td class=\"link\">" .
4764 $cgi->a({-href => href(-anchor=>"patch$patchno")},
4765 "patch") .
4766 " | " .
4767 "</td>\n";
4770 my $has_history = 0;
4771 my $not_deleted = 0;
4772 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
4773 my $hash_parent = $parents[$i];
4774 my $from_hash = $diff->{'from_id'}[$i];
4775 my $from_path = $diff->{'from_file'}[$i];
4776 my $status = $diff->{'status'}[$i];
4778 $has_history ||= ($status ne 'A');
4779 $not_deleted ||= ($status ne 'D');
4781 if ($status eq 'A') {
4782 print "<td class=\"link\" align=\"right\"> | </td>\n";
4783 } elsif ($status eq 'D') {
4784 print "<td class=\"link\">" .
4785 $cgi->a({-href => href(action=>"blob",
4786 hash_base=>$hash,
4787 hash=>$from_hash,
4788 file_name=>$from_path)},
4789 "blob" . ($i+1)) .
4790 " | </td>\n";
4791 } else {
4792 if ($diff->{'to_id'} eq $from_hash) {
4793 print "<td class=\"link nochange\">";
4794 } else {
4795 print "<td class=\"link\">";
4797 print $cgi->a({-href => href(action=>"blobdiff",
4798 hash=>$diff->{'to_id'},
4799 hash_parent=>$from_hash,
4800 hash_base=>$hash,
4801 hash_parent_base=>$hash_parent,
4802 file_name=>$diff->{'to_file'},
4803 file_parent=>$from_path)},
4804 "diff" . ($i+1)) .
4805 " | </td>\n";
4809 print "<td class=\"link\">";
4810 if ($not_deleted) {
4811 print $cgi->a({-href => href(action=>"blob",
4812 hash=>$diff->{'to_id'},
4813 file_name=>$diff->{'to_file'},
4814 hash_base=>$hash)},
4815 "blob");
4816 print " | " if ($has_history);
4818 if ($has_history) {
4819 print $cgi->a({-href => href(action=>"history",
4820 file_name=>$diff->{'to_file'},
4821 hash_base=>$hash)},
4822 "history");
4824 print "</td>\n";
4826 print "</tr>\n";
4827 next; # instead of 'else' clause, to avoid extra indent
4829 # else ordinary diff
4831 my ($to_mode_oct, $to_mode_str, $to_file_type);
4832 my ($from_mode_oct, $from_mode_str, $from_file_type);
4833 if ($diff->{'to_mode'} ne ('0' x 6)) {
4834 $to_mode_oct = oct $diff->{'to_mode'};
4835 if (S_ISREG($to_mode_oct)) { # only for regular file
4836 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
4838 $to_file_type = file_type($diff->{'to_mode'});
4840 if ($diff->{'from_mode'} ne ('0' x 6)) {
4841 $from_mode_oct = oct $diff->{'from_mode'};
4842 if (S_ISREG($from_mode_oct)) { # only for regular file
4843 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
4845 $from_file_type = file_type($diff->{'from_mode'});
4848 if ($diff->{'status'} eq "A") { # created
4849 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
4850 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
4851 $mode_chng .= "]</span>";
4852 print "<td>";
4853 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4854 hash_base=>$hash, file_name=>$diff->{'file'}),
4855 -class => "list"}, esc_path($diff->{'file'}));
4856 print "</td>\n";
4857 print "<td>$mode_chng</td>\n";
4858 print "<td class=\"link\">";
4859 if ($action eq 'commitdiff') {
4860 # link to patch
4861 $patchno++;
4862 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
4863 "patch") .
4864 " | ";
4866 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4867 hash_base=>$hash, file_name=>$diff->{'file'})},
4868 "blob");
4869 print "</td>\n";
4871 } elsif ($diff->{'status'} eq "D") { # deleted
4872 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
4873 print "<td>";
4874 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
4875 hash_base=>$parent, file_name=>$diff->{'file'}),
4876 -class => "list"}, esc_path($diff->{'file'}));
4877 print "</td>\n";
4878 print "<td>$mode_chng</td>\n";
4879 print "<td class=\"link\">";
4880 if ($action eq 'commitdiff') {
4881 # link to patch
4882 $patchno++;
4883 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
4884 "patch") .
4885 " | ";
4887 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
4888 hash_base=>$parent, file_name=>$diff->{'file'})},
4889 "blob") . " | ";
4890 if ($have_blame) {
4891 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
4892 file_name=>$diff->{'file'})},
4893 "blame") . " | ";
4895 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
4896 file_name=>$diff->{'file'})},
4897 "history");
4898 print "</td>\n";
4900 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
4901 my $mode_chnge = "";
4902 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
4903 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
4904 if ($from_file_type ne $to_file_type) {
4905 $mode_chnge .= " from $from_file_type to $to_file_type";
4907 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
4908 if ($from_mode_str && $to_mode_str) {
4909 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
4910 } elsif ($to_mode_str) {
4911 $mode_chnge .= " mode: $to_mode_str";
4914 $mode_chnge .= "]</span>\n";
4916 print "<td>";
4917 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4918 hash_base=>$hash, file_name=>$diff->{'file'}),
4919 -class => "list"}, esc_path($diff->{'file'}));
4920 print "</td>\n";
4921 print "<td>$mode_chnge</td>\n";
4922 print "<td class=\"link\">";
4923 if ($action eq 'commitdiff') {
4924 # link to patch
4925 $patchno++;
4926 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
4927 "patch") .
4928 " | ";
4929 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
4930 # "commit" view and modified file (not onlu mode changed)
4931 print $cgi->a({-href => href(action=>"blobdiff",
4932 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
4933 hash_base=>$hash, hash_parent_base=>$parent,
4934 file_name=>$diff->{'file'})},
4935 "diff") .
4936 " | ";
4938 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4939 hash_base=>$hash, file_name=>$diff->{'file'})},
4940 "blob") . " | ";
4941 if ($have_blame) {
4942 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
4943 file_name=>$diff->{'file'})},
4944 "blame") . " | ";
4946 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
4947 file_name=>$diff->{'file'})},
4948 "history");
4949 print "</td>\n";
4951 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
4952 my %status_name = ('R' => 'moved', 'C' => 'copied');
4953 my $nstatus = $status_name{$diff->{'status'}};
4954 my $mode_chng = "";
4955 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
4956 # mode also for directories, so we cannot use $to_mode_str
4957 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
4959 print "<td>" .
4960 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
4961 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
4962 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
4963 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
4964 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
4965 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
4966 -class => "list"}, esc_path($diff->{'from_file'})) .
4967 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
4968 "<td class=\"link\">";
4969 if ($action eq 'commitdiff') {
4970 # link to patch
4971 $patchno++;
4972 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
4973 "patch") .
4974 " | ";
4975 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
4976 # "commit" view and modified file (not only pure rename or copy)
4977 print $cgi->a({-href => href(action=>"blobdiff",
4978 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
4979 hash_base=>$hash, hash_parent_base=>$parent,
4980 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
4981 "diff") .
4982 " | ";
4984 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4985 hash_base=>$parent, file_name=>$diff->{'to_file'})},
4986 "blob") . " | ";
4987 if ($have_blame) {
4988 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
4989 file_name=>$diff->{'to_file'})},
4990 "blame") . " | ";
4992 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
4993 file_name=>$diff->{'to_file'})},
4994 "history");
4995 print "</td>\n";
4997 } # we should not encounter Unmerged (U) or Unknown (X) status
4998 print "</tr>\n";
5000 print "</tbody>" if $has_header;
5001 print "</table>\n";
5004 sub print_sidebyside_diff_chunk {
5005 my @chunk = @_;
5006 my (@ctx, @rem, @add);
5008 return unless @chunk;
5010 # incomplete last line might be among removed or added lines,
5011 # or both, or among context lines: find which
5012 for (my $i = 1; $i < @chunk; $i++) {
5013 if ($chunk[$i][0] eq 'incomplete') {
5014 $chunk[$i][0] = $chunk[$i-1][0];
5018 # guardian
5019 push @chunk, ["", ""];
5021 foreach my $line_info (@chunk) {
5022 my ($class, $line) = @$line_info;
5024 # print chunk headers
5025 if ($class && $class eq 'chunk_header') {
5026 print $line;
5027 next;
5030 ## print from accumulator when type of class of lines change
5031 # empty contents block on start rem/add block, or end of chunk
5032 if (@ctx && (!$class || $class eq 'rem' || $class eq 'add')) {
5033 print join '',
5034 '<div class="chunk_block ctx">',
5035 '<div class="old">',
5036 @ctx,
5037 '</div>',
5038 '<div class="new">',
5039 @ctx,
5040 '</div>',
5041 '</div>';
5042 @ctx = ();
5044 # empty add/rem block on start context block, or end of chunk
5045 if ((@rem || @add) && (!$class || $class eq 'ctx')) {
5046 if (!@add) {
5047 # pure removal
5048 print join '',
5049 '<div class="chunk_block rem">',
5050 '<div class="old">',
5051 @rem,
5052 '</div>',
5053 '</div>';
5054 } elsif (!@rem) {
5055 # pure addition
5056 print join '',
5057 '<div class="chunk_block add">',
5058 '<div class="new">',
5059 @add,
5060 '</div>',
5061 '</div>';
5062 } else {
5063 # assume that it is change
5064 print join '',
5065 '<div class="chunk_block chg">',
5066 '<div class="old">',
5067 @rem,
5068 '</div>',
5069 '<div class="new">',
5070 @add,
5071 '</div>',
5072 '</div>';
5074 @rem = @add = ();
5077 ## adding lines to accumulator
5078 # guardian value
5079 last unless $line;
5080 # rem, add or change
5081 if ($class eq 'rem') {
5082 push @rem, $line;
5083 } elsif ($class eq 'add') {
5084 push @add, $line;
5086 # context line
5087 if ($class eq 'ctx') {
5088 push @ctx, $line;
5093 sub git_patchset_body {
5094 my ($fd, $diff_style, $difftree, $hash, @hash_parents) = @_;
5095 my ($hash_parent) = $hash_parents[0];
5097 my $is_combined = (@hash_parents > 1);
5098 my $patch_idx = 0;
5099 my $patch_number = 0;
5100 my $patch_line;
5101 my $diffinfo;
5102 my $to_name;
5103 my (%from, %to);
5104 my @chunk; # for side-by-side diff
5106 print "<div class=\"patchset\">\n";
5108 # skip to first patch
5109 while ($patch_line = <$fd>) {
5110 chomp $patch_line;
5112 last if ($patch_line =~ m/^diff /);
5115 PATCH:
5116 while ($patch_line) {
5118 # parse "git diff" header line
5119 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
5120 # $1 is from_name, which we do not use
5121 $to_name = unquote($2);
5122 $to_name =~ s!^b/!!;
5123 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
5124 # $1 is 'cc' or 'combined', which we do not use
5125 $to_name = unquote($2);
5126 } else {
5127 $to_name = undef;
5130 # check if current patch belong to current raw line
5131 # and parse raw git-diff line if needed
5132 if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
5133 # this is continuation of a split patch
5134 print "<div class=\"patch cont\">\n";
5135 } else {
5136 # advance raw git-diff output if needed
5137 $patch_idx++ if defined $diffinfo;
5139 # read and prepare patch information
5140 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
5142 # compact combined diff output can have some patches skipped
5143 # find which patch (using pathname of result) we are at now;
5144 if ($is_combined) {
5145 while ($to_name ne $diffinfo->{'to_file'}) {
5146 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
5147 format_diff_cc_simplified($diffinfo, @hash_parents) .
5148 "</div>\n"; # class="patch"
5150 $patch_idx++;
5151 $patch_number++;
5153 last if $patch_idx > $#$difftree;
5154 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
5158 # modifies %from, %to hashes
5159 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
5161 # this is first patch for raw difftree line with $patch_idx index
5162 # we index @$difftree array from 0, but number patches from 1
5163 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
5166 # git diff header
5167 #assert($patch_line =~ m/^diff /) if DEBUG;
5168 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
5169 $patch_number++;
5170 # print "git diff" header
5171 print format_git_diff_header_line($patch_line, $diffinfo,
5172 \%from, \%to);
5174 # print extended diff header
5175 print "<div class=\"diff extended_header\">\n";
5176 EXTENDED_HEADER:
5177 while ($patch_line = <$fd>) {
5178 chomp $patch_line;
5180 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
5182 print format_extended_diff_header_line($patch_line, $diffinfo,
5183 \%from, \%to);
5185 print "</div>\n"; # class="diff extended_header"
5187 # from-file/to-file diff header
5188 if (! $patch_line) {
5189 print "</div>\n"; # class="patch"
5190 last PATCH;
5192 next PATCH if ($patch_line =~ m/^diff /);
5193 #assert($patch_line =~ m/^---/) if DEBUG;
5195 my $last_patch_line = $patch_line;
5196 $patch_line = <$fd>;
5197 chomp $patch_line;
5198 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
5200 print format_diff_from_to_header($last_patch_line, $patch_line,
5201 $diffinfo, \%from, \%to,
5202 @hash_parents);
5204 # the patch itself
5205 LINE:
5206 while ($patch_line = <$fd>) {
5207 chomp $patch_line;
5209 next PATCH if ($patch_line =~ m/^diff /);
5211 my ($class, $line) = process_diff_line($patch_line, \%from, \%to);
5212 my $diff_classes = "diff";
5213 $diff_classes .= " $class" if ($class);
5214 $line = "<div class=\"$diff_classes\">$line</div>\n";
5216 if ($diff_style eq 'sidebyside' && !$is_combined) {
5217 if ($class eq 'chunk_header') {
5218 print_sidebyside_diff_chunk(@chunk);
5219 @chunk = ( [ $class, $line ] );
5220 } else {
5221 push @chunk, [ $class, $line ];
5223 } else {
5224 # default 'inline' style and unknown styles
5225 print $line;
5229 } continue {
5230 if (@chunk) {
5231 print_sidebyside_diff_chunk(@chunk);
5232 @chunk = ();
5234 print "</div>\n"; # class="patch"
5237 # for compact combined (--cc) format, with chunk and patch simplification
5238 # the patchset might be empty, but there might be unprocessed raw lines
5239 for (++$patch_idx if $patch_number > 0;
5240 $patch_idx < @$difftree;
5241 ++$patch_idx) {
5242 # read and prepare patch information
5243 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
5245 # generate anchor for "patch" links in difftree / whatchanged part
5246 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
5247 format_diff_cc_simplified($diffinfo, @hash_parents) .
5248 "</div>\n"; # class="patch"
5250 $patch_number++;
5253 if ($patch_number == 0) {
5254 if (@hash_parents > 1) {
5255 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
5256 } else {
5257 print "<div class=\"diff nodifferences\">No differences found</div>\n";
5261 print "</div>\n"; # class="patchset"
5264 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5266 sub git_project_search_form {
5267 my ($searchtext, $search_use_regexp) = @_;
5269 my $limit = '';
5270 if ($project_filter) {
5271 $limit = " in '$project_filter/'";
5274 print "<div class=\"projsearch\">\n";
5275 print $cgi->startform(-method => 'get', -action => $my_uri) .
5276 $cgi->hidden(-name => 'a', -value => 'project_list') . "\n";
5277 print $cgi->hidden(-name => 'pf', -value => $project_filter). "\n"
5278 if (defined $project_filter);
5279 print $cgi->textfield(-name => 's', -value => $searchtext,
5280 -title => "Search project by name and description$limit",
5281 -size => 60) . "\n" .
5282 "<span title=\"Extended regular expression\">" .
5283 $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
5284 -checked => $search_use_regexp) .
5285 "</span>\n" .
5286 $cgi->submit(-name => 'btnS', -value => 'Search') .
5287 $cgi->end_form() . "\n" .
5288 $cgi->a({-href => href(project => undef, searchtext => undef,
5289 project_filter => $project_filter)},
5290 esc_html("List all projects$limit")) . "<br />\n";
5291 print "</div>\n";
5294 # entry for given @keys needs filling if at least one of keys in list
5295 # is not present in %$project_info
5296 sub project_info_needs_filling {
5297 my ($project_info, @keys) = @_;
5299 # return List::MoreUtils::any { !exists $project_info->{$_} } @keys;
5300 foreach my $key (@keys) {
5301 if (!exists $project_info->{$key}) {
5302 return 1;
5305 return;
5308 # fills project list info (age, description, owner, category, forks, etc.)
5309 # for each project in the list, removing invalid projects from
5310 # returned list, or fill only specified info.
5312 # Invalid projects are removed from the returned list if and only if you
5313 # ask 'age' or 'age_string' to be filled, because they are the only fields
5314 # that run unconditionally git command that requires repository, and
5315 # therefore do always check if project repository is invalid.
5317 # USAGE:
5318 # * fill_project_list_info(\@project_list, 'descr_long', 'ctags')
5319 # ensures that 'descr_long' and 'ctags' fields are filled
5320 # * @project_list = fill_project_list_info(\@project_list)
5321 # ensures that all fields are filled (and invalid projects removed)
5323 # NOTE: modifies $projlist, but does not remove entries from it
5324 sub fill_project_list_info {
5325 my ($projlist, @wanted_keys) = @_;
5326 my @projects;
5327 my $filter_set = sub { return @_; };
5328 if (@wanted_keys) {
5329 my %wanted_keys = map { $_ => 1 } @wanted_keys;
5330 $filter_set = sub { return grep { $wanted_keys{$_} } @_; };
5333 my $show_ctags = gitweb_check_feature('ctags');
5334 PROJECT:
5335 foreach my $pr (@$projlist) {
5336 if (project_info_needs_filling($pr, $filter_set->('age', 'age_string'))) {
5337 my (@activity) = git_get_last_activity($pr->{'path'});
5338 unless (@activity) {
5339 next PROJECT;
5341 ($pr->{'age'}, $pr->{'age_string'}) = @activity;
5343 if (project_info_needs_filling($pr, $filter_set->('descr', 'descr_long'))) {
5344 my $descr = git_get_project_description($pr->{'path'}) || "";
5345 $descr = to_utf8($descr);
5346 $pr->{'descr_long'} = $descr;
5347 $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
5349 if (project_info_needs_filling($pr, $filter_set->('owner'))) {
5350 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
5352 if ($show_ctags &&
5353 project_info_needs_filling($pr, $filter_set->('ctags'))) {
5354 $pr->{'ctags'} = git_get_project_ctags($pr->{'path'});
5356 if ($projects_list_group_categories &&
5357 project_info_needs_filling($pr, $filter_set->('category'))) {
5358 my $cat = git_get_project_category($pr->{'path'}) ||
5359 $project_list_default_category;
5360 $pr->{'category'} = to_utf8($cat);
5363 push @projects, $pr;
5366 return @projects;
5369 sub sort_projects_list {
5370 my ($projlist, $order) = @_;
5371 my @projects;
5373 my %order_info = (
5374 project => { key => 'path', type => 'str' },
5375 descr => { key => 'descr_long', type => 'str' },
5376 owner => { key => 'owner', type => 'str' },
5377 age => { key => 'age', type => 'num' }
5379 my $oi = $order_info{$order};
5380 return @$projlist unless defined $oi;
5381 if ($oi->{'type'} eq 'str') {
5382 @projects = sort {$a->{$oi->{'key'}} cmp $b->{$oi->{'key'}}} @$projlist;
5383 } else {
5384 @projects = sort {$a->{$oi->{'key'}} <=> $b->{$oi->{'key'}}} @$projlist;
5387 return @projects;
5390 # returns a hash of categories, containing the list of project
5391 # belonging to each category
5392 sub build_projlist_by_category {
5393 my ($projlist, $from, $to) = @_;
5394 my %categories;
5396 $from = 0 unless defined $from;
5397 $to = $#$projlist if (!defined $to || $#$projlist < $to);
5399 for (my $i = $from; $i <= $to; $i++) {
5400 my $pr = $projlist->[$i];
5401 push @{$categories{ $pr->{'category'} }}, $pr;
5404 return wantarray ? %categories : \%categories;
5407 # print 'sort by' <th> element, generating 'sort by $name' replay link
5408 # if that order is not selected
5409 sub print_sort_th {
5410 print format_sort_th(@_);
5413 sub format_sort_th {
5414 my ($name, $order, $header) = @_;
5415 my $sort_th = "";
5416 $header ||= ucfirst($name);
5418 if ($order eq $name) {
5419 $sort_th .= "<th>$header</th>\n";
5420 } else {
5421 $sort_th .= "<th>" .
5422 $cgi->a({-href => href(-replay=>1, order=>$name),
5423 -class => "header"}, $header) .
5424 "</th>\n";
5427 return $sort_th;
5430 sub git_project_list_rows {
5431 my ($projlist, $from, $to, $check_forks) = @_;
5433 $from = 0 unless defined $from;
5434 $to = $#$projlist if (!defined $to || $#$projlist < $to);
5436 my $alternate = 1;
5437 for (my $i = $from; $i <= $to; $i++) {
5438 my $pr = $projlist->[$i];
5440 if ($alternate) {
5441 print "<tr class=\"dark\">\n";
5442 } else {
5443 print "<tr class=\"light\">\n";
5445 $alternate ^= 1;
5447 if ($check_forks) {
5448 print "<td>";
5449 if ($pr->{'forks'}) {
5450 my $nforks = scalar @{$pr->{'forks'}};
5451 if ($nforks > 0) {
5452 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks"),
5453 -title => "$nforks forks"}, "+");
5454 } else {
5455 print $cgi->span({-title => "$nforks forks"}, "+");
5458 print "</td>\n";
5460 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
5461 -class => "list"},
5462 esc_html_match_hl($pr->{'path'}, $search_regexp)) .
5463 "</td>\n" .
5464 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
5465 -class => "list",
5466 -title => $pr->{'descr_long'}},
5467 $search_regexp
5468 ? esc_html_match_hl_chopped($pr->{'descr_long'},
5469 $pr->{'descr'}, $search_regexp)
5470 : esc_html($pr->{'descr'})) .
5471 "</td>\n";
5472 unless ($omit_owner) {
5473 print "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n";
5475 unless ($omit_age_column) {
5476 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
5477 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n";
5479 print"<td class=\"link\">" .
5480 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
5481 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
5482 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
5483 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
5484 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
5485 "</td>\n" .
5486 "</tr>\n";
5490 sub git_project_list_body {
5491 # actually uses global variable $project
5492 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
5493 my @projects = @$projlist;
5495 my $check_forks = gitweb_check_feature('forks');
5496 my $show_ctags = gitweb_check_feature('ctags');
5497 my $tagfilter = $show_ctags ? $input_params{'ctag'} : undef;
5498 $check_forks = undef
5499 if ($tagfilter || $search_regexp);
5501 # filtering out forks before filling info allows to do less work
5502 @projects = filter_forks_from_projects_list(\@projects)
5503 if ($check_forks);
5504 # search_projects_list pre-fills required info
5505 @projects = search_projects_list(\@projects,
5506 'search_regexp' => $search_regexp,
5507 'tagfilter' => $tagfilter)
5508 if ($tagfilter || $search_regexp);
5509 # fill the rest
5510 my @all_fields = ('descr', 'descr_long', 'ctags', 'category');
5511 push @all_fields, ('age', 'age_string') unless($omit_age_column);
5512 push @all_fields, 'owner' unless($omit_owner);
5513 @projects = fill_project_list_info(\@projects, @all_fields);
5515 $order ||= $default_projects_order;
5516 $from = 0 unless defined $from;
5517 $to = $#projects if (!defined $to || $#projects < $to);
5519 # short circuit
5520 if ($from > $to) {
5521 print "<center>\n".
5522 "<b>No such projects found</b><br />\n".
5523 "Click ".$cgi->a({-href=>href(project=>undef)},"here")." to view all projects<br />\n".
5524 "</center>\n<br />\n";
5525 return;
5528 @projects = sort_projects_list(\@projects, $order);
5530 if ($show_ctags) {
5531 my $ctags = git_gather_all_ctags(\@projects);
5532 my $cloud = git_populate_project_tagcloud($ctags);
5533 print git_show_project_tagcloud($cloud, 64);
5536 print "<table class=\"project_list\">\n";
5537 unless ($no_header) {
5538 print "<tr>\n";
5539 if ($check_forks) {
5540 print "<th></th>\n";
5542 print_sort_th('project', $order, 'Project');
5543 print_sort_th('descr', $order, 'Description');
5544 print_sort_th('owner', $order, 'Owner') unless $omit_owner;
5545 print_sort_th('age', $order, 'Last Change') unless $omit_age_column;
5546 print "<th></th>\n" . # for links
5547 "</tr>\n";
5550 if ($projects_list_group_categories) {
5551 # only display categories with projects in the $from-$to window
5552 @projects = sort {$a->{'category'} cmp $b->{'category'}} @projects[$from..$to];
5553 my %categories = build_projlist_by_category(\@projects, $from, $to);
5554 foreach my $cat (sort keys %categories) {
5555 unless ($cat eq "") {
5556 print "<tr>\n";
5557 if ($check_forks) {
5558 print "<td></td>\n";
5560 print "<td class=\"category\" colspan=\"5\">".esc_html($cat)."</td>\n";
5561 print "</tr>\n";
5564 git_project_list_rows($categories{$cat}, undef, undef, $check_forks);
5566 } else {
5567 git_project_list_rows(\@projects, $from, $to, $check_forks);
5570 if (defined $extra) {
5571 print "<tr>\n";
5572 if ($check_forks) {
5573 print "<td></td>\n";
5575 print "<td colspan=\"5\">$extra</td>\n" .
5576 "</tr>\n";
5578 print "</table>\n";
5581 sub git_log_body {
5582 # uses global variable $project
5583 my ($commitlist, $from, $to, $refs, $extra) = @_;
5585 $from = 0 unless defined $from;
5586 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
5588 for (my $i = 0; $i <= $to; $i++) {
5589 my %co = %{$commitlist->[$i]};
5590 next if !%co;
5591 my $commit = $co{'id'};
5592 my $ref = format_ref_marker($refs, $commit);
5593 git_print_header_div('commit',
5594 "<span class=\"age\">$co{'age_string'}</span>" .
5595 esc_html($co{'title'}) . $ref,
5596 $commit);
5597 print "<div class=\"title_text\">\n" .
5598 "<div class=\"log_link\">\n" .
5599 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
5600 " | " .
5601 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
5602 " | " .
5603 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
5604 "<br/>\n" .
5605 "</div>\n";
5606 git_print_authorship(\%co, -tag => 'span');
5607 print "<br/>\n</div>\n";
5609 print "<div class=\"log_body\">\n";
5610 git_print_log($co{'comment'}, -final_empty_line=> 1);
5611 print "</div>\n";
5613 if ($extra) {
5614 print "<div class=\"page_nav\">\n";
5615 print "$extra\n";
5616 print "</div>\n";
5620 sub git_shortlog_body {
5621 # uses global variable $project
5622 my ($commitlist, $from, $to, $refs, $extra) = @_;
5624 $from = 0 unless defined $from;
5625 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
5627 print "<table class=\"shortlog\">\n";
5628 my $alternate = 1;
5629 for (my $i = $from; $i <= $to; $i++) {
5630 my %co = %{$commitlist->[$i]};
5631 my $commit = $co{'id'};
5632 my $ref = format_ref_marker($refs, $commit);
5633 if ($alternate) {
5634 print "<tr class=\"dark\">\n";
5635 } else {
5636 print "<tr class=\"light\">\n";
5638 $alternate ^= 1;
5639 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
5640 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5641 format_author_html('td', \%co, 10) . "<td>";
5642 print format_subject_html($co{'title'}, $co{'title_short'},
5643 href(action=>"commit", hash=>$commit), $ref);
5644 print "</td>\n" .
5645 "<td class=\"link\">" .
5646 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
5647 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
5648 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
5649 my $snapshot_links = format_snapshot_links($commit);
5650 if (defined $snapshot_links) {
5651 print " | " . $snapshot_links;
5653 print "</td>\n" .
5654 "</tr>\n";
5656 if (defined $extra) {
5657 print "<tr>\n" .
5658 "<td colspan=\"4\">$extra</td>\n" .
5659 "</tr>\n";
5661 print "</table>\n";
5664 sub git_history_body {
5665 # Warning: assumes constant type (blob or tree) during history
5666 my ($commitlist, $from, $to, $refs, $extra,
5667 $file_name, $file_hash, $ftype) = @_;
5669 $from = 0 unless defined $from;
5670 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
5672 print "<table class=\"history\">\n";
5673 my $alternate = 1;
5674 for (my $i = $from; $i <= $to; $i++) {
5675 my %co = %{$commitlist->[$i]};
5676 if (!%co) {
5677 next;
5679 my $commit = $co{'id'};
5681 my $ref = format_ref_marker($refs, $commit);
5683 if ($alternate) {
5684 print "<tr class=\"dark\">\n";
5685 } else {
5686 print "<tr class=\"light\">\n";
5688 $alternate ^= 1;
5689 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5690 # shortlog: format_author_html('td', \%co, 10)
5691 format_author_html('td', \%co, 15, 3) . "<td>";
5692 # originally git_history used chop_str($co{'title'}, 50)
5693 print format_subject_html($co{'title'}, $co{'title_short'},
5694 href(action=>"commit", hash=>$commit), $ref);
5695 print "</td>\n" .
5696 "<td class=\"link\">" .
5697 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
5698 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
5700 if ($ftype eq 'blob') {
5701 my $blob_current = $file_hash;
5702 my $blob_parent = git_get_hash_by_path($commit, $file_name);
5703 if (defined $blob_current && defined $blob_parent &&
5704 $blob_current ne $blob_parent) {
5705 print " | " .
5706 $cgi->a({-href => href(action=>"blobdiff",
5707 hash=>$blob_current, hash_parent=>$blob_parent,
5708 hash_base=>$hash_base, hash_parent_base=>$commit,
5709 file_name=>$file_name)},
5710 "diff to current");
5713 print "</td>\n" .
5714 "</tr>\n";
5716 if (defined $extra) {
5717 print "<tr>\n" .
5718 "<td colspan=\"4\">$extra</td>\n" .
5719 "</tr>\n";
5721 print "</table>\n";
5724 sub git_tags_body {
5725 # uses global variable $project
5726 my ($taglist, $from, $to, $extra) = @_;
5727 $from = 0 unless defined $from;
5728 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
5730 print "<table class=\"tags\">\n";
5731 my $alternate = 1;
5732 for (my $i = $from; $i <= $to; $i++) {
5733 my $entry = $taglist->[$i];
5734 my %tag = %$entry;
5735 my $comment = $tag{'subject'};
5736 my $comment_short;
5737 if (defined $comment) {
5738 $comment_short = chop_str($comment, 30, 5);
5740 if ($alternate) {
5741 print "<tr class=\"dark\">\n";
5742 } else {
5743 print "<tr class=\"light\">\n";
5745 $alternate ^= 1;
5746 if (defined $tag{'age'}) {
5747 print "<td><i>$tag{'age'}</i></td>\n";
5748 } else {
5749 print "<td></td>\n";
5751 print "<td>" .
5752 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
5753 -class => "list name"}, esc_html($tag{'name'})) .
5754 "</td>\n" .
5755 "<td>";
5756 if (defined $comment) {
5757 print format_subject_html($comment, $comment_short,
5758 href(action=>"tag", hash=>$tag{'id'}));
5760 print "</td>\n" .
5761 "<td class=\"selflink\">";
5762 if ($tag{'type'} eq "tag") {
5763 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
5764 } else {
5765 print "&nbsp;";
5767 print "</td>\n" .
5768 "<td class=\"link\">" . " | " .
5769 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
5770 if ($tag{'reftype'} eq "commit") {
5771 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})}, "shortlog") .
5772 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})}, "log");
5773 } elsif ($tag{'reftype'} eq "blob") {
5774 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
5776 print "</td>\n" .
5777 "</tr>";
5779 if (defined $extra) {
5780 print "<tr>\n" .
5781 "<td colspan=\"5\">$extra</td>\n" .
5782 "</tr>\n";
5784 print "</table>\n";
5787 sub git_heads_body {
5788 # uses global variable $project
5789 my ($headlist, $head_at, $from, $to, $extra) = @_;
5790 $from = 0 unless defined $from;
5791 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
5793 print "<table class=\"heads\">\n";
5794 my $alternate = 1;
5795 for (my $i = $from; $i <= $to; $i++) {
5796 my $entry = $headlist->[$i];
5797 my %ref = %$entry;
5798 my $curr = defined $head_at && $ref{'id'} eq $head_at;
5799 if ($alternate) {
5800 print "<tr class=\"dark\">\n";
5801 } else {
5802 print "<tr class=\"light\">\n";
5804 $alternate ^= 1;
5805 print "<td><i>$ref{'age'}</i></td>\n" .
5806 ($curr ? "<td class=\"current_head\">" : "<td>") .
5807 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),
5808 -class => "list name"},esc_html($ref{'name'})) .
5809 "</td>\n" .
5810 "<td class=\"link\">" .
5811 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})}, "shortlog") . " | " .
5812 $cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})}, "log") . " | " .
5813 $cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'fullname'})}, "tree") .
5814 "</td>\n" .
5815 "</tr>";
5817 if (defined $extra) {
5818 print "<tr>\n" .
5819 "<td colspan=\"3\">$extra</td>\n" .
5820 "</tr>\n";
5822 print "</table>\n";
5825 # Display a single remote block
5826 sub git_remote_block {
5827 my ($remote, $rdata, $limit, $head) = @_;
5829 my $heads = $rdata->{'heads'};
5830 my $fetch = $rdata->{'fetch'};
5831 my $push = $rdata->{'push'};
5833 my $urls_table = "<table class=\"projects_list\">\n" ;
5835 if (defined $fetch) {
5836 if ($fetch eq $push) {
5837 $urls_table .= format_repo_url("URL", $fetch);
5838 } else {
5839 $urls_table .= format_repo_url("Fetch URL", $fetch);
5840 $urls_table .= format_repo_url("Push URL", $push) if defined $push;
5842 } elsif (defined $push) {
5843 $urls_table .= format_repo_url("Push URL", $push);
5844 } else {
5845 $urls_table .= format_repo_url("", "No remote URL");
5848 $urls_table .= "</table>\n";
5850 my $dots;
5851 if (defined $limit && $limit < @$heads) {
5852 $dots = $cgi->a({-href => href(action=>"remotes", hash=>$remote)}, "...");
5855 print $urls_table;
5856 git_heads_body($heads, $head, 0, $limit, $dots);
5859 # Display a list of remote names with the respective fetch and push URLs
5860 sub git_remotes_list {
5861 my ($remotedata, $limit) = @_;
5862 print "<table class=\"heads\">\n";
5863 my $alternate = 1;
5864 my @remotes = sort keys %$remotedata;
5866 my $limited = $limit && $limit < @remotes;
5868 $#remotes = $limit - 1 if $limited;
5870 while (my $remote = shift @remotes) {
5871 my $rdata = $remotedata->{$remote};
5872 my $fetch = $rdata->{'fetch'};
5873 my $push = $rdata->{'push'};
5874 if ($alternate) {
5875 print "<tr class=\"dark\">\n";
5876 } else {
5877 print "<tr class=\"light\">\n";
5879 $alternate ^= 1;
5880 print "<td>" .
5881 $cgi->a({-href=> href(action=>'remotes', hash=>$remote),
5882 -class=> "list name"},esc_html($remote)) .
5883 "</td>";
5884 print "<td class=\"link\">" .
5885 (defined $fetch ? $cgi->a({-href=> $fetch}, "fetch") : "fetch") .
5886 " | " .
5887 (defined $push ? $cgi->a({-href=> $push}, "push") : "push") .
5888 "</td>";
5890 print "</tr>\n";
5893 if ($limited) {
5894 print "<tr>\n" .
5895 "<td colspan=\"3\">" .
5896 $cgi->a({-href => href(action=>"remotes")}, "...") .
5897 "</td>\n" . "</tr>\n";
5900 print "</table>";
5903 # Display remote heads grouped by remote, unless there are too many
5904 # remotes, in which case we only display the remote names
5905 sub git_remotes_body {
5906 my ($remotedata, $limit, $head) = @_;
5907 if ($limit and $limit < keys %$remotedata) {
5908 git_remotes_list($remotedata, $limit);
5909 } else {
5910 fill_remote_heads($remotedata);
5911 while (my ($remote, $rdata) = each %$remotedata) {
5912 git_print_section({-class=>"remote", -id=>$remote},
5913 ["remotes", $remote, $remote], sub {
5914 git_remote_block($remote, $rdata, $limit, $head);
5920 sub git_search_message {
5921 my %co = @_;
5923 my $greptype;
5924 if ($searchtype eq 'commit') {
5925 $greptype = "--grep=";
5926 } elsif ($searchtype eq 'author') {
5927 $greptype = "--author=";
5928 } elsif ($searchtype eq 'committer') {
5929 $greptype = "--committer=";
5931 $greptype .= $searchtext;
5932 my @commitlist = parse_commits($hash, 101, (100 * $page), undef,
5933 $greptype, '--regexp-ignore-case',
5934 $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
5936 my $paging_nav = '';
5937 if ($page > 0) {
5938 $paging_nav .=
5939 $cgi->a({-href => href(-replay=>1, page=>undef)},
5940 "first") .
5941 " &sdot; " .
5942 $cgi->a({-href => href(-replay=>1, page=>$page-1),
5943 -accesskey => "p", -title => "Alt-p"}, "prev");
5944 } else {
5945 $paging_nav .= "first &sdot; prev";
5947 my $next_link = '';
5948 if ($#commitlist >= 100) {
5949 $next_link =
5950 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5951 -accesskey => "n", -title => "Alt-n"}, "next");
5952 $paging_nav .= " &sdot; $next_link";
5953 } else {
5954 $paging_nav .= " &sdot; next";
5957 git_header_html();
5959 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
5960 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5961 if ($page == 0 && !@commitlist) {
5962 print "<p>No match.</p>\n";
5963 } else {
5964 git_search_grep_body(\@commitlist, 0, 99, $next_link);
5967 git_footer_html();
5970 sub git_search_changes {
5971 my %co = @_;
5973 local $/ = "\n";
5974 open my $fd, '-|', git_cmd(), '--no-pager', 'log', @diff_opts,
5975 '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
5976 ($search_use_regexp ? '--pickaxe-regex' : ())
5977 or die_error(500, "Open git-log failed");
5979 git_header_html();
5981 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5982 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5984 print "<table class=\"pickaxe search\">\n";
5985 my $alternate = 1;
5986 undef %co;
5987 my @files;
5988 while (my $line = <$fd>) {
5989 chomp $line;
5990 next unless $line;
5992 my %set = parse_difftree_raw_line($line);
5993 if (defined $set{'commit'}) {
5994 # finish previous commit
5995 if (%co) {
5996 print "</td>\n" .
5997 "<td class=\"link\">" .
5998 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},
5999 "commit") .
6000 " | " .
6001 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'},
6002 hash_base=>$co{'id'})},
6003 "tree") .
6004 "</td>\n" .
6005 "</tr>\n";
6008 if ($alternate) {
6009 print "<tr class=\"dark\">\n";
6010 } else {
6011 print "<tr class=\"light\">\n";
6013 $alternate ^= 1;
6014 %co = parse_commit($set{'commit'});
6015 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
6016 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
6017 "<td><i>$author</i></td>\n" .
6018 "<td>" .
6019 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
6020 -class => "list subject"},
6021 chop_and_escape_str($co{'title'}, 50) . "<br/>");
6022 } elsif (defined $set{'to_id'}) {
6023 next if ($set{'to_id'} =~ m/^0{40}$/);
6025 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
6026 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),
6027 -class => "list"},
6028 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
6029 "<br/>\n";
6032 close $fd;
6034 # finish last commit (warning: repetition!)
6035 if (%co) {
6036 print "</td>\n" .
6037 "<td class=\"link\">" .
6038 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},
6039 "commit") .
6040 " | " .
6041 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'},
6042 hash_base=>$co{'id'})},
6043 "tree") .
6044 "</td>\n" .
6045 "</tr>\n";
6048 print "</table>\n";
6050 git_footer_html();
6053 sub git_search_files {
6054 my %co = @_;
6056 local $/ = "\n";
6057 open my $fd, "-|", git_cmd(), 'grep', '-n', '-z',
6058 $search_use_regexp ? ('-E', '-i') : '-F',
6059 $searchtext, $co{'tree'}
6060 or die_error(500, "Open git-grep failed");
6062 git_header_html();
6064 git_print_page_nav('','', $hash,$co{'tree'},$hash);
6065 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6067 print "<table class=\"grep_search\">\n";
6068 my $alternate = 1;
6069 my $matches = 0;
6070 my $lastfile = '';
6071 my $file_href;
6072 while (my $line = <$fd>) {
6073 chomp $line;
6074 my ($file, $lno, $ltext, $binary);
6075 last if ($matches++ > 1000);
6076 if ($line =~ /^Binary file (.+) matches$/) {
6077 $file = $1;
6078 $binary = 1;
6079 } else {
6080 ($file, $lno, $ltext) = split(/\0/, $line, 3);
6081 $file =~ s/^$co{'tree'}://;
6083 if ($file ne $lastfile) {
6084 $lastfile and print "</td></tr>\n";
6085 if ($alternate++) {
6086 print "<tr class=\"dark\">\n";
6087 } else {
6088 print "<tr class=\"light\">\n";
6090 $file_href = href(action=>"blob", hash_base=>$co{'id'},
6091 file_name=>$file);
6092 print "<td class=\"list\">".
6093 $cgi->a({-href => $file_href, -class => "list"}, esc_path($file));
6094 print "</td><td>\n";
6095 $lastfile = $file;
6097 if ($binary) {
6098 print "<div class=\"binary\">Binary file</div>\n";
6099 } else {
6100 $ltext = untabify($ltext);
6101 if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
6102 $ltext = esc_html($1, -nbsp=>1);
6103 $ltext .= '<span class="match">';
6104 $ltext .= esc_html($2, -nbsp=>1);
6105 $ltext .= '</span>';
6106 $ltext .= esc_html($3, -nbsp=>1);
6107 } else {
6108 $ltext = esc_html($ltext, -nbsp=>1);
6110 print "<div class=\"pre\">" .
6111 $cgi->a({-href => $file_href.'#l'.$lno,
6112 -class => "linenr"}, sprintf('%4i', $lno)) .
6113 ' ' . $ltext . "</div>\n";
6116 if ($lastfile) {
6117 print "</td></tr>\n";
6118 if ($matches > 1000) {
6119 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
6121 } else {
6122 print "<div class=\"diff nodifferences\">No matches found</div>\n";
6124 close $fd;
6126 print "</table>\n";
6128 git_footer_html();
6131 sub git_search_grep_body {
6132 my ($commitlist, $from, $to, $extra) = @_;
6133 $from = 0 unless defined $from;
6134 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
6136 print "<table class=\"commit_search\">\n";
6137 my $alternate = 1;
6138 for (my $i = $from; $i <= $to; $i++) {
6139 my %co = %{$commitlist->[$i]};
6140 if (!%co) {
6141 next;
6143 my $commit = $co{'id'};
6144 if ($alternate) {
6145 print "<tr class=\"dark\">\n";
6146 } else {
6147 print "<tr class=\"light\">\n";
6149 $alternate ^= 1;
6150 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
6151 format_author_html('td', \%co, 15, 5) .
6152 "<td>" .
6153 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
6154 -class => "list subject"},
6155 chop_and_escape_str($co{'title'}, 50) . "<br/>");
6156 my $comment = $co{'comment'};
6157 foreach my $line (@$comment) {
6158 if ($line =~ m/^(.*?)($search_regexp)(.*)$/i) {
6159 my ($lead, $match, $trail) = ($1, $2, $3);
6160 $match = chop_str($match, 70, 5, 'center');
6161 my $contextlen = int((80 - length($match))/2);
6162 $contextlen = 30 if ($contextlen > 30);
6163 $lead = chop_str($lead, $contextlen, 10, 'left');
6164 $trail = chop_str($trail, $contextlen, 10, 'right');
6166 $lead = esc_html($lead);
6167 $match = esc_html($match);
6168 $trail = esc_html($trail);
6170 print "$lead<span class=\"match\">$match</span>$trail<br />";
6173 print "</td>\n" .
6174 "<td class=\"link\">" .
6175 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
6176 " | " .
6177 $cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})}, "commitdiff") .
6178 " | " .
6179 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
6180 print "</td>\n" .
6181 "</tr>\n";
6183 if (defined $extra) {
6184 print "<tr>\n" .
6185 "<td colspan=\"3\">$extra</td>\n" .
6186 "</tr>\n";
6188 print "</table>\n";
6191 ## ======================================================================
6192 ## ======================================================================
6193 ## actions
6195 sub git_project_list {
6196 my $order = $input_params{'order'};
6197 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
6198 die_error(400, "Unknown order parameter");
6201 my @list = git_get_projects_list($project_filter, $strict_export);
6202 if (!@list) {
6203 die_error(404, "No projects found");
6206 git_header_html();
6207 if (defined $home_text && -f $home_text) {
6208 print "<div class=\"index_include\">\n";
6209 insert_file($home_text);
6210 print "</div>\n";
6213 git_project_search_form($searchtext, $search_use_regexp);
6214 git_project_list_body(\@list, $order);
6215 git_footer_html();
6218 sub git_forks {
6219 my $order = $input_params{'order'};
6220 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
6221 die_error(400, "Unknown order parameter");
6224 my $filter = $project;
6225 $filter =~ s/\.git$//;
6226 my @list = git_get_projects_list($filter);
6227 if (!@list) {
6228 die_error(404, "No forks found");
6231 git_header_html();
6232 git_print_page_nav('','');
6233 git_print_header_div('summary', "$project forks");
6234 git_project_list_body(\@list, $order);
6235 git_footer_html();
6238 sub git_project_index {
6239 my @projects = git_get_projects_list($project_filter, $strict_export);
6240 if (!@projects) {
6241 die_error(404, "No projects found");
6244 print $cgi->header(
6245 -type => 'text/plain',
6246 -charset => 'utf-8',
6247 -content_disposition => 'inline; filename="index.aux"');
6249 foreach my $pr (@projects) {
6250 if (!exists $pr->{'owner'}) {
6251 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
6254 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
6255 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
6256 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
6257 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
6258 $path =~ s/ /\+/g;
6259 $owner =~ s/ /\+/g;
6261 print "$path $owner\n";
6265 sub git_summary {
6266 my $descr = git_get_project_description($project) || "none";
6267 my %co = parse_commit("HEAD");
6268 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
6269 my $head = $co{'id'};
6270 my $remote_heads = gitweb_check_feature('remote_heads');
6272 my $owner = git_get_project_owner($project);
6274 my $refs = git_get_references();
6275 # These get_*_list functions return one more to allow us to see if
6276 # there are more ...
6277 my @taglist = git_get_tags_list(16);
6278 my @headlist = git_get_heads_list(16);
6279 my %remotedata = $remote_heads ? git_get_remotes_list() : ();
6280 my @forklist;
6281 my $check_forks = gitweb_check_feature('forks');
6283 if ($check_forks) {
6284 # find forks of a project
6285 my $filter = $project;
6286 $filter =~ s/\.git$//;
6287 @forklist = git_get_projects_list($filter);
6288 # filter out forks of forks
6289 @forklist = filter_forks_from_projects_list(\@forklist)
6290 if (@forklist);
6293 git_header_html();
6294 git_print_page_nav('summary','', $head);
6296 print "<div class=\"title\">&nbsp;</div>\n";
6297 print "<table class=\"projects_list\">\n" .
6298 "<tr id=\"metadata_desc\"><td>description</td><td>" . esc_html($descr) . "</td></tr>\n";
6299 unless ($omit_owner) {
6300 print "<tr id=\"metadata_owner\"><td>owner</td><td>" . esc_html($owner) . "</td></tr>\n";
6302 if (defined $cd{'rfc2822'}) {
6303 print "<tr id=\"metadata_lchange\"><td>last change</td>" .
6304 "<td>".format_timestamp_html(\%cd)."</td></tr>\n";
6307 # use per project git URL list in $projectroot/$project/cloneurl
6308 # or make project git URL from git base URL and project name
6309 my $url_tag = "URL";
6310 my @url_list = git_get_project_url_list($project);
6311 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
6312 foreach my $git_url (@url_list) {
6313 next unless $git_url;
6314 print format_repo_url($url_tag, $git_url);
6315 $url_tag = "";
6318 # Tag cloud
6319 my $show_ctags = gitweb_check_feature('ctags');
6320 if ($show_ctags) {
6321 my $ctags = git_get_project_ctags($project);
6322 if (%$ctags) {
6323 # without ability to add tags, don't show if there are none
6324 my $cloud = git_populate_project_tagcloud($ctags);
6325 print "<tr id=\"metadata_ctags\">" .
6326 "<td>content tags</td>" .
6327 "<td>".git_show_project_tagcloud($cloud, 48)."</td>" .
6328 "</tr>\n";
6332 print "</table>\n";
6334 # If XSS prevention is on, we don't include README.html.
6335 # TODO: Allow a readme in some safe format.
6336 if (!$prevent_xss && -s "$projectroot/$project/README.html") {
6337 print "<div class=\"title\">readme</div>\n" .
6338 "<div class=\"readme\">\n";
6339 insert_file("$projectroot/$project/README.html");
6340 print "\n</div>\n"; # class="readme"
6343 # we need to request one more than 16 (0..15) to check if
6344 # those 16 are all
6345 my @commitlist = $head ? parse_commits($head, 17) : ();
6346 if (@commitlist) {
6347 git_print_header_div('shortlog');
6348 git_shortlog_body(\@commitlist, 0, 15, $refs,
6349 $#commitlist <= 15 ? undef :
6350 $cgi->a({-href => href(action=>"shortlog")}, "..."));
6353 if (@taglist) {
6354 git_print_header_div('tags');
6355 git_tags_body(\@taglist, 0, 15,
6356 $#taglist <= 15 ? undef :
6357 $cgi->a({-href => href(action=>"tags")}, "..."));
6360 if (@headlist) {
6361 git_print_header_div('heads');
6362 git_heads_body(\@headlist, $head, 0, 15,
6363 $#headlist <= 15 ? undef :
6364 $cgi->a({-href => href(action=>"heads")}, "..."));
6367 if (%remotedata) {
6368 git_print_header_div('remotes');
6369 git_remotes_body(\%remotedata, 15, $head);
6372 if (@forklist) {
6373 git_print_header_div('forks');
6374 git_project_list_body(\@forklist, 'age', 0, 15,
6375 $#forklist <= 15 ? undef :
6376 $cgi->a({-href => href(action=>"forks")}, "..."),
6377 'no_header');
6380 git_footer_html();
6383 sub git_tag {
6384 my %tag = parse_tag($hash);
6386 if (! %tag) {
6387 die_error(404, "Unknown tag object");
6390 my $head = git_get_head_hash($project);
6391 git_header_html();
6392 git_print_page_nav('','', $head,undef,$head);
6393 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
6394 print "<div class=\"title_text\">\n" .
6395 "<table class=\"object_header\">\n" .
6396 "<tr>\n" .
6397 "<td>object</td>\n" .
6398 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
6399 $tag{'object'}) . "</td>\n" .
6400 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
6401 $tag{'type'}) . "</td>\n" .
6402 "</tr>\n";
6403 if (defined($tag{'author'})) {
6404 git_print_authorship_rows(\%tag, 'author');
6406 print "</table>\n\n" .
6407 "</div>\n";
6408 print "<div class=\"page_body\">";
6409 my $comment = $tag{'comment'};
6410 foreach my $line (@$comment) {
6411 chomp $line;
6412 print esc_html($line, -nbsp=>1) . "<br/>\n";
6414 print "</div>\n";
6415 git_footer_html();
6418 sub git_blame_common {
6419 my $format = shift || 'porcelain';
6420 if ($format eq 'porcelain' && $input_params{'javascript'}) {
6421 $format = 'incremental';
6422 $action = 'blame_incremental'; # for page title etc
6425 # permissions
6426 gitweb_check_feature('blame')
6427 or die_error(403, "Blame view not allowed");
6429 # error checking
6430 die_error(400, "No file name given") unless $file_name;
6431 $hash_base ||= git_get_head_hash($project);
6432 die_error(404, "Couldn't find base commit") unless $hash_base;
6433 my %co = parse_commit($hash_base)
6434 or die_error(404, "Commit not found");
6435 my $ftype = "blob";
6436 if (!defined $hash) {
6437 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
6438 or die_error(404, "Error looking up file");
6439 } else {
6440 $ftype = git_get_type($hash);
6441 if ($ftype !~ "blob") {
6442 die_error(400, "Object is not a blob");
6446 my $fd;
6447 if ($format eq 'incremental') {
6448 # get file contents (as base)
6449 open $fd, "-|", git_cmd(), 'cat-file', 'blob', $hash
6450 or die_error(500, "Open git-cat-file failed");
6451 } elsif ($format eq 'data') {
6452 # run git-blame --incremental
6453 open $fd, "-|", git_cmd(), "blame", "--incremental",
6454 $hash_base, "--", $file_name
6455 or die_error(500, "Open git-blame --incremental failed");
6456 } else {
6457 # run git-blame --porcelain
6458 open $fd, "-|", git_cmd(), "blame", '-p',
6459 $hash_base, '--', $file_name
6460 or die_error(500, "Open git-blame --porcelain failed");
6463 # incremental blame data returns early
6464 if ($format eq 'data') {
6465 print $cgi->header(
6466 -type=>"text/plain", -charset => "utf-8",
6467 -status=> "200 OK");
6468 local $| = 1; # output autoflush
6469 while (my $line = <$fd>) {
6470 print to_utf8($line);
6472 close $fd
6473 or print "ERROR $!\n";
6475 print 'END';
6476 if (defined $t0 && gitweb_check_feature('timed')) {
6477 print ' '.
6478 tv_interval($t0, [ gettimeofday() ]).
6479 ' '.$number_of_git_cmds;
6481 print "\n";
6483 return;
6486 # page header
6487 git_header_html();
6488 my $formats_nav =
6489 $cgi->a({-href => href(action=>"blob", -replay=>1)},
6490 "blob") .
6491 " | ";
6492 if ($format eq 'incremental') {
6493 $formats_nav .=
6494 $cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},
6495 "blame") . " (non-incremental)";
6496 } else {
6497 $formats_nav .=
6498 $cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},
6499 "blame") . " (incremental)";
6501 $formats_nav .=
6502 " | " .
6503 $cgi->a({-href => href(action=>"history", -replay=>1)},
6504 "history") .
6505 " | " .
6506 $cgi->a({-href => href(action=>$action, file_name=>$file_name)},
6507 "HEAD");
6508 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
6509 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
6510 git_print_page_path($file_name, $ftype, $hash_base);
6512 # page body
6513 if ($format eq 'incremental') {
6514 print "<noscript>\n<div class=\"error\"><center><b>\n".
6515 "This page requires JavaScript to run.\n Use ".
6516 $cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},
6517 'this page').
6518 " instead.\n".
6519 "</b></center></div>\n</noscript>\n";
6521 print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;
6524 print qq!<div class="page_body">\n!;
6525 print qq!<div id="progress_info">... / ...</div>\n!
6526 if ($format eq 'incremental');
6527 print qq!<table id="blame_table" class="blame" width="100%">\n!.
6528 #qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.
6529 qq!<thead>\n!.
6530 qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.
6531 qq!</thead>\n!.
6532 qq!<tbody>\n!;
6534 my @rev_color = qw(light dark);
6535 my $num_colors = scalar(@rev_color);
6536 my $current_color = 0;
6538 if ($format eq 'incremental') {
6539 my $color_class = $rev_color[$current_color];
6541 #contents of a file
6542 my $linenr = 0;
6543 LINE:
6544 while (my $line = <$fd>) {
6545 chomp $line;
6546 $linenr++;
6548 print qq!<tr id="l$linenr" class="$color_class">!.
6549 qq!<td class="sha1"><a href=""> </a></td>!.
6550 qq!<td class="linenr">!.
6551 qq!<a class="linenr" href="">$linenr</a></td>!;
6552 print qq!<td class="pre">! . esc_html($line) . "</td>\n";
6553 print qq!</tr>\n!;
6556 } else { # porcelain, i.e. ordinary blame
6557 my %metainfo = (); # saves information about commits
6559 # blame data
6560 LINE:
6561 while (my $line = <$fd>) {
6562 chomp $line;
6563 # the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]
6564 # no <lines in group> for subsequent lines in group of lines
6565 my ($full_rev, $orig_lineno, $lineno, $group_size) =
6566 ($line =~ /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);
6567 if (!exists $metainfo{$full_rev}) {
6568 $metainfo{$full_rev} = { 'nprevious' => 0 };
6570 my $meta = $metainfo{$full_rev};
6571 my $data;
6572 while ($data = <$fd>) {
6573 chomp $data;
6574 last if ($data =~ s/^\t//); # contents of line
6575 if ($data =~ /^(\S+)(?: (.*))?$/) {
6576 $meta->{$1} = $2 unless exists $meta->{$1};
6578 if ($data =~ /^previous /) {
6579 $meta->{'nprevious'}++;
6582 my $short_rev = substr($full_rev, 0, 8);
6583 my $author = $meta->{'author'};
6584 my %date =
6585 parse_date($meta->{'author-time'}, $meta->{'author-tz'});
6586 my $date = $date{'iso-tz'};
6587 if ($group_size) {
6588 $current_color = ($current_color + 1) % $num_colors;
6590 my $tr_class = $rev_color[$current_color];
6591 $tr_class .= ' boundary' if (exists $meta->{'boundary'});
6592 $tr_class .= ' no-previous' if ($meta->{'nprevious'} == 0);
6593 $tr_class .= ' multiple-previous' if ($meta->{'nprevious'} > 1);
6594 print "<tr id=\"l$lineno\" class=\"$tr_class\">\n";
6595 if ($group_size) {
6596 print "<td class=\"sha1\"";
6597 print " title=\"". esc_html($author) . ", $date\"";
6598 print " rowspan=\"$group_size\"" if ($group_size > 1);
6599 print ">";
6600 print $cgi->a({-href => href(action=>"commit",
6601 hash=>$full_rev,
6602 file_name=>$file_name)},
6603 esc_html($short_rev));
6604 if ($group_size >= 2) {
6605 my @author_initials = ($author =~ /\b([[:upper:]])\B/g);
6606 if (@author_initials) {
6607 print "<br />" .
6608 esc_html(join('', @author_initials));
6609 # or join('.', ...)
6612 print "</td>\n";
6614 # 'previous' <sha1 of parent commit> <filename at commit>
6615 if (exists $meta->{'previous'} &&
6616 $meta->{'previous'} =~ /^([a-fA-F0-9]{40}) (.*)$/) {
6617 $meta->{'parent'} = $1;
6618 $meta->{'file_parent'} = unquote($2);
6620 my $linenr_commit =
6621 exists($meta->{'parent'}) ?
6622 $meta->{'parent'} : $full_rev;
6623 my $linenr_filename =
6624 exists($meta->{'file_parent'}) ?
6625 $meta->{'file_parent'} : unquote($meta->{'filename'});
6626 my $blamed = href(action => 'blame',
6627 file_name => $linenr_filename,
6628 hash_base => $linenr_commit);
6629 print "<td class=\"linenr\">";
6630 print $cgi->a({ -href => "$blamed#l$orig_lineno",
6631 -class => "linenr" },
6632 esc_html($lineno));
6633 print "</td>";
6634 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
6635 print "</tr>\n";
6636 } # end while
6640 # footer
6641 print "</tbody>\n".
6642 "</table>\n"; # class="blame"
6643 print "</div>\n"; # class="blame_body"
6644 close $fd
6645 or print "Reading blob failed\n";
6647 git_footer_html();
6650 sub git_blame {
6651 git_blame_common();
6654 sub git_blame_incremental {
6655 git_blame_common('incremental');
6658 sub git_blame_data {
6659 git_blame_common('data');
6662 sub git_tags {
6663 my $head = git_get_head_hash($project);
6664 git_header_html();
6665 git_print_page_nav('','', $head,undef,$head,format_ref_views('tags'));
6666 git_print_header_div('summary', $project);
6668 my @tagslist = git_get_tags_list();
6669 if (@tagslist) {
6670 git_tags_body(\@tagslist);
6672 git_footer_html();
6675 sub git_heads {
6676 my $head = git_get_head_hash($project);
6677 git_header_html();
6678 git_print_page_nav('','', $head,undef,$head,format_ref_views('heads'));
6679 git_print_header_div('summary', $project);
6681 my @headslist = git_get_heads_list();
6682 if (@headslist) {
6683 git_heads_body(\@headslist, $head);
6685 git_footer_html();
6688 # used both for single remote view and for list of all the remotes
6689 sub git_remotes {
6690 gitweb_check_feature('remote_heads')
6691 or die_error(403, "Remote heads view is disabled");
6693 my $head = git_get_head_hash($project);
6694 my $remote = $input_params{'hash'};
6696 my $remotedata = git_get_remotes_list($remote);
6697 die_error(500, "Unable to get remote information") unless defined $remotedata;
6699 unless (%$remotedata) {
6700 die_error(404, defined $remote ?
6701 "Remote $remote not found" :
6702 "No remotes found");
6705 git_header_html(undef, undef, -action_extra => $remote);
6706 git_print_page_nav('', '', $head, undef, $head,
6707 format_ref_views($remote ? '' : 'remotes'));
6709 fill_remote_heads($remotedata);
6710 if (defined $remote) {
6711 git_print_header_div('remotes', "$remote remote for $project");
6712 git_remote_block($remote, $remotedata->{$remote}, undef, $head);
6713 } else {
6714 git_print_header_div('summary', "$project remotes");
6715 git_remotes_body($remotedata, undef, $head);
6718 git_footer_html();
6721 sub git_blob_plain {
6722 my $type = shift;
6723 my $expires;
6725 if (!defined $hash) {
6726 if (defined $file_name) {
6727 my $base = $hash_base || git_get_head_hash($project);
6728 $hash = git_get_hash_by_path($base, $file_name, "blob")
6729 or die_error(404, "Cannot find file");
6730 } else {
6731 die_error(400, "No file name defined");
6733 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6734 # blobs defined by non-textual hash id's can be cached
6735 $expires = "+1d";
6738 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
6739 or die_error(500, "Open git-cat-file blob '$hash' failed");
6741 # content-type (can include charset)
6742 $type = blob_contenttype($fd, $file_name, $type);
6744 # "save as" filename, even when no $file_name is given
6745 my $save_as = "$hash";
6746 if (defined $file_name) {
6747 $save_as = $file_name;
6748 } elsif ($type =~ m/^text\//) {
6749 $save_as .= '.txt';
6752 # With XSS prevention on, blobs of all types except a few known safe
6753 # ones are served with "Content-Disposition: attachment" to make sure
6754 # they don't run in our security domain. For certain image types,
6755 # blob view writes an <img> tag referring to blob_plain view, and we
6756 # want to be sure not to break that by serving the image as an
6757 # attachment (though Firefox 3 doesn't seem to care).
6758 my $sandbox = $prevent_xss &&
6759 $type !~ m!^(?:text/[a-z]+|image/(?:gif|png|jpeg))(?:[ ;]|$)!;
6761 # serve text/* as text/plain
6762 if ($prevent_xss &&
6763 ($type =~ m!^text/[a-z]+\b(.*)$! ||
6764 ($type =~ m!^[a-z]+/[a-z]\+xml\b(.*)$! && -T $fd))) {
6765 my $rest = $1;
6766 $rest = defined $rest ? $rest : '';
6767 $type = "text/plain$rest";
6770 print $cgi->header(
6771 -type => $type,
6772 -expires => $expires,
6773 -content_disposition =>
6774 ($sandbox ? 'attachment' : 'inline')
6775 . '; filename="' . $save_as . '"');
6776 local $/ = undef;
6777 binmode STDOUT, ':raw';
6778 print <$fd>;
6779 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
6780 close $fd;
6783 sub git_blob {
6784 my $expires;
6786 if (!defined $hash) {
6787 if (defined $file_name) {
6788 my $base = $hash_base || git_get_head_hash($project);
6789 $hash = git_get_hash_by_path($base, $file_name, "blob")
6790 or die_error(404, "Cannot find file");
6791 } else {
6792 die_error(400, "No file name defined");
6794 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6795 # blobs defined by non-textual hash id's can be cached
6796 $expires = "+1d";
6799 my $have_blame = gitweb_check_feature('blame');
6800 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
6801 or die_error(500, "Couldn't cat $file_name, $hash");
6802 my $mimetype = blob_mimetype($fd, $file_name);
6803 # use 'blob_plain' (aka 'raw') view for files that cannot be displayed
6804 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {
6805 close $fd;
6806 return git_blob_plain($mimetype);
6808 # we can have blame only for text/* mimetype
6809 $have_blame &&= ($mimetype =~ m!^text/!);
6811 my $highlight = gitweb_check_feature('highlight');
6812 my $syntax = guess_file_syntax($highlight, $mimetype, $file_name);
6813 $fd = run_highlighter($fd, $highlight, $syntax)
6814 if $syntax;
6816 git_header_html(undef, $expires);
6817 my $formats_nav = '';
6818 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
6819 if (defined $file_name) {
6820 if ($have_blame) {
6821 $formats_nav .=
6822 $cgi->a({-href => href(action=>"blame", -replay=>1)},
6823 "blame") .
6824 " | ";
6826 $formats_nav .=
6827 $cgi->a({-href => href(action=>"history", -replay=>1)},
6828 "history") .
6829 " | " .
6830 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
6831 "raw") .
6832 " | " .
6833 $cgi->a({-href => href(action=>"blob",
6834 hash_base=>"HEAD", file_name=>$file_name)},
6835 "HEAD");
6836 } else {
6837 $formats_nav .=
6838 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
6839 "raw");
6841 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
6842 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
6843 } else {
6844 print "<div class=\"page_nav\">\n" .
6845 "<br/><br/></div>\n" .
6846 "<div class=\"title\">".esc_html($hash)."</div>\n";
6848 git_print_page_path($file_name, "blob", $hash_base);
6849 print "<div class=\"page_body\">\n";
6850 if ($mimetype =~ m!^image/!) {
6851 print qq!<img type="!.esc_attr($mimetype).qq!"!;
6852 if ($file_name) {
6853 print qq! alt="!.esc_attr($file_name).qq!" title="!.esc_attr($file_name).qq!"!;
6855 print qq! src="! .
6856 href(action=>"blob_plain", hash=>$hash,
6857 hash_base=>$hash_base, file_name=>$file_name) .
6858 qq!" />\n!;
6859 } else {
6860 my $nr;
6861 while (my $line = <$fd>) {
6862 chomp $line;
6863 $nr++;
6864 $line = untabify($line);
6865 printf qq!<div class="pre"><a id="l%i" href="%s#l%i" class="linenr">%4i</a> %s</div>\n!,
6866 $nr, esc_attr(href(-replay => 1)), $nr, $nr,
6867 $syntax ? sanitize($line) : esc_html($line, -nbsp=>1);
6870 close $fd
6871 or print "Reading blob failed.\n";
6872 print "</div>";
6873 git_footer_html();
6876 sub git_tree {
6877 if (!defined $hash_base) {
6878 $hash_base = "HEAD";
6880 if (!defined $hash) {
6881 if (defined $file_name) {
6882 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
6883 } else {
6884 $hash = $hash_base;
6887 die_error(404, "No such tree") unless defined($hash);
6889 my $show_sizes = gitweb_check_feature('show-sizes');
6890 my $have_blame = gitweb_check_feature('blame');
6892 my @entries = ();
6894 local $/ = "\0";
6895 open my $fd, "-|", git_cmd(), "ls-tree", '-z',
6896 ($show_sizes ? '-l' : ()), @extra_options, $hash
6897 or die_error(500, "Open git-ls-tree failed");
6898 @entries = map { chomp; $_ } <$fd>;
6899 close $fd
6900 or die_error(404, "Reading tree failed");
6903 my $refs = git_get_references();
6904 my $ref = format_ref_marker($refs, $hash_base);
6905 git_header_html();
6906 my $basedir = '';
6907 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
6908 my @views_nav = ();
6909 if (defined $file_name) {
6910 push @views_nav,
6911 $cgi->a({-href => href(action=>"history", -replay=>1)},
6912 "history"),
6913 $cgi->a({-href => href(action=>"tree",
6914 hash_base=>"HEAD", file_name=>$file_name)},
6915 "HEAD"),
6917 my $snapshot_links = format_snapshot_links($hash);
6918 if (defined $snapshot_links) {
6919 # FIXME: Should be available when we have no hash base as well.
6920 push @views_nav, $snapshot_links;
6922 git_print_page_nav('tree','', $hash_base, undef, undef,
6923 join(' | ', @views_nav));
6924 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
6925 } else {
6926 undef $hash_base;
6927 print "<div class=\"page_nav\">\n";
6928 print "<br/><br/></div>\n";
6929 print "<div class=\"title\">".esc_html($hash)."</div>\n";
6931 if (defined $file_name) {
6932 $basedir = $file_name;
6933 if ($basedir ne '' && substr($basedir, -1) ne '/') {
6934 $basedir .= '/';
6936 git_print_page_path($file_name, 'tree', $hash_base);
6938 print "<div class=\"page_body\">\n";
6939 print "<table class=\"tree\">\n";
6940 my $alternate = 1;
6941 # '..' (top directory) link if possible
6942 if (defined $hash_base &&
6943 defined $file_name && $file_name =~ m![^/]+$!) {
6944 if ($alternate) {
6945 print "<tr class=\"dark\">\n";
6946 } else {
6947 print "<tr class=\"light\">\n";
6949 $alternate ^= 1;
6951 my $up = $file_name;
6952 $up =~ s!/?[^/]+$!!;
6953 undef $up unless $up;
6954 # based on git_print_tree_entry
6955 print '<td class="mode">' . mode_str('040000') . "</td>\n";
6956 print '<td class="size">&nbsp;</td>'."\n" if $show_sizes;
6957 print '<td class="list">';
6958 print $cgi->a({-href => href(action=>"tree",
6959 hash_base=>$hash_base,
6960 file_name=>$up)},
6961 "..");
6962 print "</td>\n";
6963 print "<td class=\"link\"></td>\n";
6965 print "</tr>\n";
6967 foreach my $line (@entries) {
6968 my %t = parse_ls_tree_line($line, -z => 1, -l => $show_sizes);
6970 if ($alternate) {
6971 print "<tr class=\"dark\">\n";
6972 } else {
6973 print "<tr class=\"light\">\n";
6975 $alternate ^= 1;
6977 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
6979 print "</tr>\n";
6981 print "</table>\n" .
6982 "</div>";
6983 git_footer_html();
6986 sub snapshot_name {
6987 my ($project, $hash) = @_;
6989 # path/to/project.git -> project
6990 # path/to/project/.git -> project
6991 my $name = to_utf8($project);
6992 $name =~ s,([^/])/*\.git$,$1,;
6993 $name = basename($name);
6994 # sanitize name
6995 $name =~ s/[[:cntrl:]]/?/g;
6997 my $ver = $hash;
6998 if ($hash =~ /^[0-9a-fA-F]+$/) {
6999 # shorten SHA-1 hash
7000 my $full_hash = git_get_full_hash($project, $hash);
7001 if ($full_hash =~ /^$hash/ && length($hash) > 7) {
7002 $ver = git_get_short_hash($project, $hash);
7004 } elsif ($hash =~ m!^refs/tags/(.*)$!) {
7005 # tags don't need shortened SHA-1 hash
7006 $ver = $1;
7007 } else {
7008 # branches and other need shortened SHA-1 hash
7009 if ($hash =~ m!^refs/(?:heads|remotes)/(.*)$!) {
7010 $ver = $1;
7012 $ver .= '-' . git_get_short_hash($project, $hash);
7014 # in case of hierarchical branch names
7015 $ver =~ s!/!.!g;
7017 # name = project-version_string
7018 $name = "$name-$ver";
7020 return wantarray ? ($name, $name) : $name;
7023 sub git_snapshot {
7024 my $format = $input_params{'snapshot_format'};
7025 if (!@snapshot_fmts) {
7026 die_error(403, "Snapshots not allowed");
7028 # default to first supported snapshot format
7029 $format ||= $snapshot_fmts[0];
7030 if ($format !~ m/^[a-z0-9]+$/) {
7031 die_error(400, "Invalid snapshot format parameter");
7032 } elsif (!exists($known_snapshot_formats{$format})) {
7033 die_error(400, "Unknown snapshot format");
7034 } elsif ($known_snapshot_formats{$format}{'disabled'}) {
7035 die_error(403, "Snapshot format not allowed");
7036 } elsif (!grep($_ eq $format, @snapshot_fmts)) {
7037 die_error(403, "Unsupported snapshot format");
7040 my $type = git_get_type("$hash^{}");
7041 if (!$type) {
7042 die_error(404, 'Object does not exist');
7043 } elsif ($type eq 'blob') {
7044 die_error(400, 'Object is not a tree-ish');
7047 my ($name, $prefix) = snapshot_name($project, $hash);
7048 my $filename = "$name$known_snapshot_formats{$format}{'suffix'}";
7049 my $cmd = quote_command(
7050 git_cmd(), 'archive',
7051 "--format=$known_snapshot_formats{$format}{'format'}",
7052 "--prefix=$prefix/", $hash);
7053 if (exists $known_snapshot_formats{$format}{'compressor'}) {
7054 $cmd .= ' | ' . quote_command(@{$known_snapshot_formats{$format}{'compressor'}});
7057 $filename =~ s/(["\\])/\\$1/g;
7058 print $cgi->header(
7059 -type => $known_snapshot_formats{$format}{'type'},
7060 -content_disposition => 'inline; filename="' . $filename . '"',
7061 -status => '200 OK');
7063 open my $fd, "-|", $cmd
7064 or die_error(500, "Execute git-archive failed");
7065 binmode STDOUT, ':raw';
7066 print <$fd>;
7067 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
7068 close $fd;
7071 sub git_log_generic {
7072 my ($fmt_name, $body_subr, $base, $parent, $file_name, $file_hash) = @_;
7074 my $head = git_get_head_hash($project);
7075 if (!defined $base) {
7076 $base = $head;
7078 if (!defined $page) {
7079 $page = 0;
7081 my $refs = git_get_references();
7083 my $commit_hash = $base;
7084 if (defined $parent) {
7085 $commit_hash = "$parent..$base";
7087 my @commitlist =
7088 parse_commits($commit_hash, 101, (100 * $page),
7089 defined $file_name ? ($file_name, "--full-history") : ());
7091 my $ftype;
7092 if (!defined $file_hash && defined $file_name) {
7093 # some commits could have deleted file in question,
7094 # and not have it in tree, but one of them has to have it
7095 for (my $i = 0; $i < @commitlist; $i++) {
7096 $file_hash = git_get_hash_by_path($commitlist[$i]{'id'}, $file_name);
7097 last if defined $file_hash;
7100 if (defined $file_hash) {
7101 $ftype = git_get_type($file_hash);
7103 if (defined $file_name && !defined $ftype) {
7104 die_error(500, "Unknown type of object");
7106 my %co;
7107 if (defined $file_name) {
7108 %co = parse_commit($base)
7109 or die_error(404, "Unknown commit object");
7113 my $paging_nav = format_paging_nav($fmt_name, $page, $#commitlist >= 100);
7114 my $next_link = '';
7115 if ($#commitlist >= 100) {
7116 $next_link =
7117 $cgi->a({-href => href(-replay=>1, page=>$page+1),
7118 -accesskey => "n", -title => "Alt-n"}, "next");
7120 my $patch_max = gitweb_get_feature('patches');
7121 if ($patch_max && !defined $file_name) {
7122 if ($patch_max < 0 || @commitlist <= $patch_max) {
7123 $paging_nav .= " &sdot; " .
7124 $cgi->a({-href => href(action=>"patches", -replay=>1)},
7125 "patches");
7129 git_header_html();
7130 git_print_page_nav($fmt_name,'', $hash,$hash,$hash, $paging_nav);
7131 if (defined $file_name) {
7132 git_print_header_div('commit', esc_html($co{'title'}), $base);
7133 } else {
7134 git_print_header_div('summary', $project)
7136 git_print_page_path($file_name, $ftype, $hash_base)
7137 if (defined $file_name);
7139 $body_subr->(\@commitlist, 0, 99, $refs, $next_link,
7140 $file_name, $file_hash, $ftype);
7142 git_footer_html();
7145 sub git_log {
7146 git_log_generic('log', \&git_log_body,
7147 $hash, $hash_parent);
7150 sub git_commit {
7151 $hash ||= $hash_base || "HEAD";
7152 my %co = parse_commit($hash)
7153 or die_error(404, "Unknown commit object");
7155 my $parent = $co{'parent'};
7156 my $parents = $co{'parents'}; # listref
7158 # we need to prepare $formats_nav before any parameter munging
7159 my $formats_nav;
7160 if (!defined $parent) {
7161 # --root commitdiff
7162 $formats_nav .= '(initial)';
7163 } elsif (@$parents == 1) {
7164 # single parent commit
7165 $formats_nav .=
7166 '(parent: ' .
7167 $cgi->a({-href => href(action=>"commit",
7168 hash=>$parent)},
7169 esc_html(substr($parent, 0, 7))) .
7170 ')';
7171 } else {
7172 # merge commit
7173 $formats_nav .=
7174 '(merge: ' .
7175 join(' ', map {
7176 $cgi->a({-href => href(action=>"commit",
7177 hash=>$_)},
7178 esc_html(substr($_, 0, 7)));
7179 } @$parents ) .
7180 ')';
7182 if (gitweb_check_feature('patches') && @$parents <= 1) {
7183 $formats_nav .= " | " .
7184 $cgi->a({-href => href(action=>"patch", -replay=>1)},
7185 "patch");
7188 if (!defined $parent) {
7189 $parent = "--root";
7191 my @difftree;
7192 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
7193 @diff_opts,
7194 (@$parents <= 1 ? $parent : '-c'),
7195 $hash, "--"
7196 or die_error(500, "Open git-diff-tree failed");
7197 @difftree = map { chomp; $_ } <$fd>;
7198 close $fd or die_error(404, "Reading git-diff-tree failed");
7200 # non-textual hash id's can be cached
7201 my $expires;
7202 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
7203 $expires = "+1d";
7205 my $refs = git_get_references();
7206 my $ref = format_ref_marker($refs, $co{'id'});
7208 git_header_html(undef, $expires);
7209 git_print_page_nav('commit', '',
7210 $hash, $co{'tree'}, $hash,
7211 $formats_nav);
7213 if (defined $co{'parent'}) {
7214 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
7215 } else {
7216 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
7218 print "<div class=\"title_text\">\n" .
7219 "<table class=\"object_header\">\n";
7220 git_print_authorship_rows(\%co);
7221 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
7222 print "<tr>" .
7223 "<td>tree</td>" .
7224 "<td class=\"sha1\">" .
7225 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
7226 class => "list"}, $co{'tree'}) .
7227 "</td>" .
7228 "<td class=\"link\">" .
7229 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
7230 "tree");
7231 my $snapshot_links = format_snapshot_links($hash);
7232 if (defined $snapshot_links) {
7233 print " | " . $snapshot_links;
7235 print "</td>" .
7236 "</tr>\n";
7238 foreach my $par (@$parents) {
7239 print "<tr>" .
7240 "<td>parent</td>" .
7241 "<td class=\"sha1\">" .
7242 $cgi->a({-href => href(action=>"commit", hash=>$par),
7243 class => "list"}, $par) .
7244 "</td>" .
7245 "<td class=\"link\">" .
7246 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
7247 " | " .
7248 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
7249 "</td>" .
7250 "</tr>\n";
7252 print "</table>".
7253 "</div>\n";
7255 print "<div class=\"page_body\">\n";
7256 git_print_log($co{'comment'});
7257 print "</div>\n";
7259 git_difftree_body(\@difftree, $hash, @$parents);
7261 git_footer_html();
7264 sub git_object {
7265 # object is defined by:
7266 # - hash or hash_base alone
7267 # - hash_base and file_name
7268 my $type;
7270 # - hash or hash_base alone
7271 if ($hash || ($hash_base && !defined $file_name)) {
7272 my $object_id = $hash || $hash_base;
7274 open my $fd, "-|", quote_command(
7275 git_cmd(), 'cat-file', '-t', $object_id) . ' 2> /dev/null'
7276 or die_error(404, "Object does not exist");
7277 $type = <$fd>;
7278 chomp $type;
7279 close $fd
7280 or die_error(404, "Object does not exist");
7282 # - hash_base and file_name
7283 } elsif ($hash_base && defined $file_name) {
7284 $file_name =~ s,/+$,,;
7286 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
7287 or die_error(404, "Base object does not exist");
7289 # here errors should not hapen
7290 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
7291 or die_error(500, "Open git-ls-tree failed");
7292 my $line = <$fd>;
7293 close $fd;
7295 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
7296 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
7297 die_error(404, "File or directory for given base does not exist");
7299 $type = $2;
7300 $hash = $3;
7301 } else {
7302 die_error(400, "Not enough information to find object");
7305 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
7306 hash=>$hash, hash_base=>$hash_base,
7307 file_name=>$file_name),
7308 -status => '302 Found');
7311 sub git_blobdiff {
7312 my $format = shift || 'html';
7313 my $diff_style = $input_params{'diff_style'} || 'inline';
7315 my $fd;
7316 my @difftree;
7317 my %diffinfo;
7318 my $expires;
7320 # preparing $fd and %diffinfo for git_patchset_body
7321 # new style URI
7322 if (defined $hash_base && defined $hash_parent_base) {
7323 if (defined $file_name) {
7324 # read raw output
7325 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
7326 $hash_parent_base, $hash_base,
7327 "--", (defined $file_parent ? $file_parent : ()), $file_name
7328 or die_error(500, "Open git-diff-tree failed");
7329 @difftree = map { chomp; $_ } <$fd>;
7330 close $fd
7331 or die_error(404, "Reading git-diff-tree failed");
7332 @difftree
7333 or die_error(404, "Blob diff not found");
7335 } elsif (defined $hash &&
7336 $hash =~ /[0-9a-fA-F]{40}/) {
7337 # try to find filename from $hash
7339 # read filtered raw output
7340 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
7341 $hash_parent_base, $hash_base, "--"
7342 or die_error(500, "Open git-diff-tree failed");
7343 @difftree =
7344 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
7345 # $hash == to_id
7346 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
7347 map { chomp; $_ } <$fd>;
7348 close $fd
7349 or die_error(404, "Reading git-diff-tree failed");
7350 @difftree
7351 or die_error(404, "Blob diff not found");
7353 } else {
7354 die_error(400, "Missing one of the blob diff parameters");
7357 if (@difftree > 1) {
7358 die_error(400, "Ambiguous blob diff specification");
7361 %diffinfo = parse_difftree_raw_line($difftree[0]);
7362 $file_parent ||= $diffinfo{'from_file'} || $file_name;
7363 $file_name ||= $diffinfo{'to_file'};
7365 $hash_parent ||= $diffinfo{'from_id'};
7366 $hash ||= $diffinfo{'to_id'};
7368 # non-textual hash id's can be cached
7369 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
7370 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
7371 $expires = '+1d';
7374 # open patch output
7375 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
7376 '-p', ($format eq 'html' ? "--full-index" : ()),
7377 $hash_parent_base, $hash_base,
7378 "--", (defined $file_parent ? $file_parent : ()), $file_name
7379 or die_error(500, "Open git-diff-tree failed");
7382 # old/legacy style URI -- not generated anymore since 1.4.3.
7383 if (!%diffinfo) {
7384 die_error('404 Not Found', "Missing one of the blob diff parameters")
7387 # header
7388 if ($format eq 'html') {
7389 my $formats_nav =
7390 $cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},
7391 "raw");
7392 $formats_nav .= diff_style_nav($diff_style);
7393 git_header_html(undef, $expires);
7394 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
7395 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
7396 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
7397 } else {
7398 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
7399 print "<div class=\"title\">".esc_html("$hash vs $hash_parent")."</div>\n";
7401 if (defined $file_name) {
7402 git_print_page_path($file_name, "blob", $hash_base);
7403 } else {
7404 print "<div class=\"page_path\"></div>\n";
7407 } elsif ($format eq 'plain') {
7408 print $cgi->header(
7409 -type => 'text/plain',
7410 -charset => 'utf-8',
7411 -expires => $expires,
7412 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
7414 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
7416 } else {
7417 die_error(400, "Unknown blobdiff format");
7420 # patch
7421 if ($format eq 'html') {
7422 print "<div class=\"page_body\">\n";
7424 git_patchset_body($fd, $diff_style,
7425 [ \%diffinfo ], $hash_base, $hash_parent_base);
7426 close $fd;
7428 print "</div>\n"; # class="page_body"
7429 git_footer_html();
7431 } else {
7432 while (my $line = <$fd>) {
7433 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
7434 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
7436 print $line;
7438 last if $line =~ m!^\+\+\+!;
7440 local $/ = undef;
7441 print <$fd>;
7442 close $fd;
7446 sub git_blobdiff_plain {
7447 git_blobdiff('plain');
7450 # assumes that it is added as later part of already existing navigation,
7451 # so it returns "| foo | bar" rather than just "foo | bar"
7452 sub diff_style_nav {
7453 my ($diff_style, $is_combined) = @_;
7454 $diff_style ||= 'inline';
7456 return "" if ($is_combined);
7458 my @styles = (inline => 'inline', 'sidebyside' => 'side by side');
7459 my %styles = @styles;
7460 @styles =
7461 @styles[ map { $_ * 2 } 0..$#styles/2 ];
7463 return join '',
7464 map { " | ".$_ }
7465 map {
7466 $_ eq $diff_style ? $styles{$_} :
7467 $cgi->a({-href => href(-replay=>1, diff_style => $_)}, $styles{$_})
7468 } @styles;
7471 sub git_commitdiff {
7472 my %params = @_;
7473 my $format = $params{-format} || 'html';
7474 my $diff_style = $input_params{'diff_style'} || 'inline';
7476 my ($patch_max) = gitweb_get_feature('patches');
7477 if ($format eq 'patch') {
7478 die_error(403, "Patch view not allowed") unless $patch_max;
7481 $hash ||= $hash_base || "HEAD";
7482 my %co = parse_commit($hash)
7483 or die_error(404, "Unknown commit object");
7485 # choose format for commitdiff for merge
7486 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
7487 $hash_parent = '--cc';
7489 # we need to prepare $formats_nav before almost any parameter munging
7490 my $formats_nav;
7491 if ($format eq 'html') {
7492 $formats_nav =
7493 $cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
7494 "raw");
7495 if ($patch_max && @{$co{'parents'}} <= 1) {
7496 $formats_nav .= " | " .
7497 $cgi->a({-href => href(action=>"patch", -replay=>1)},
7498 "patch");
7500 $formats_nav .= diff_style_nav($diff_style, @{$co{'parents'}} > 1);
7502 if (defined $hash_parent &&
7503 $hash_parent ne '-c' && $hash_parent ne '--cc') {
7504 # commitdiff with two commits given
7505 my $hash_parent_short = $hash_parent;
7506 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
7507 $hash_parent_short = substr($hash_parent, 0, 7);
7509 $formats_nav .=
7510 ' (from';
7511 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
7512 if ($co{'parents'}[$i] eq $hash_parent) {
7513 $formats_nav .= ' parent ' . ($i+1);
7514 last;
7517 $formats_nav .= ': ' .
7518 $cgi->a({-href => href(-replay=>1,
7519 hash=>$hash_parent, hash_base=>undef)},
7520 esc_html($hash_parent_short)) .
7521 ')';
7522 } elsif (!$co{'parent'}) {
7523 # --root commitdiff
7524 $formats_nav .= ' (initial)';
7525 } elsif (scalar @{$co{'parents'}} == 1) {
7526 # single parent commit
7527 $formats_nav .=
7528 ' (parent: ' .
7529 $cgi->a({-href => href(-replay=>1,
7530 hash=>$co{'parent'}, hash_base=>undef)},
7531 esc_html(substr($co{'parent'}, 0, 7))) .
7532 ')';
7533 } else {
7534 # merge commit
7535 if ($hash_parent eq '--cc') {
7536 $formats_nav .= ' | ' .
7537 $cgi->a({-href => href(-replay=>1,
7538 hash=>$hash, hash_parent=>'-c')},
7539 'combined');
7540 } else { # $hash_parent eq '-c'
7541 $formats_nav .= ' | ' .
7542 $cgi->a({-href => href(-replay=>1,
7543 hash=>$hash, hash_parent=>'--cc')},
7544 'compact');
7546 $formats_nav .=
7547 ' (merge: ' .
7548 join(' ', map {
7549 $cgi->a({-href => href(-replay=>1,
7550 hash=>$_, hash_base=>undef)},
7551 esc_html(substr($_, 0, 7)));
7552 } @{$co{'parents'}} ) .
7553 ')';
7557 my $hash_parent_param = $hash_parent;
7558 if (!defined $hash_parent_param) {
7559 # --cc for multiple parents, --root for parentless
7560 $hash_parent_param =
7561 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
7564 # read commitdiff
7565 my $fd;
7566 my @difftree;
7567 if ($format eq 'html') {
7568 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
7569 "--no-commit-id", "--patch-with-raw", "--full-index",
7570 $hash_parent_param, $hash, "--"
7571 or die_error(500, "Open git-diff-tree failed");
7573 while (my $line = <$fd>) {
7574 chomp $line;
7575 # empty line ends raw part of diff-tree output
7576 last unless $line;
7577 push @difftree, scalar parse_difftree_raw_line($line);
7580 } elsif ($format eq 'plain') {
7581 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
7582 '-p', $hash_parent_param, $hash, "--"
7583 or die_error(500, "Open git-diff-tree failed");
7584 } elsif ($format eq 'patch') {
7585 # For commit ranges, we limit the output to the number of
7586 # patches specified in the 'patches' feature.
7587 # For single commits, we limit the output to a single patch,
7588 # diverging from the git-format-patch default.
7589 my @commit_spec = ();
7590 if ($hash_parent) {
7591 if ($patch_max > 0) {
7592 push @commit_spec, "-$patch_max";
7594 push @commit_spec, '-n', "$hash_parent..$hash";
7595 } else {
7596 if ($params{-single}) {
7597 push @commit_spec, '-1';
7598 } else {
7599 if ($patch_max > 0) {
7600 push @commit_spec, "-$patch_max";
7602 push @commit_spec, "-n";
7604 push @commit_spec, '--root', $hash;
7606 open $fd, "-|", git_cmd(), "format-patch", @diff_opts,
7607 '--encoding=utf8', '--stdout', @commit_spec
7608 or die_error(500, "Open git-format-patch failed");
7609 } else {
7610 die_error(400, "Unknown commitdiff format");
7613 # non-textual hash id's can be cached
7614 my $expires;
7615 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
7616 $expires = "+1d";
7619 # write commit message
7620 if ($format eq 'html') {
7621 my $refs = git_get_references();
7622 my $ref = format_ref_marker($refs, $co{'id'});
7624 git_header_html(undef, $expires);
7625 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
7626 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
7627 print "<div class=\"title_text\">\n" .
7628 "<table class=\"object_header\">\n";
7629 git_print_authorship_rows(\%co);
7630 print "</table>".
7631 "</div>\n";
7632 print "<div class=\"page_body\">\n";
7633 if (@{$co{'comment'}} > 1) {
7634 print "<div class=\"log\">\n";
7635 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
7636 print "</div>\n"; # class="log"
7639 } elsif ($format eq 'plain') {
7640 my $refs = git_get_references("tags");
7641 my $tagname = git_get_rev_name_tags($hash);
7642 my $filename = basename($project) . "-$hash.patch";
7644 print $cgi->header(
7645 -type => 'text/plain',
7646 -charset => 'utf-8',
7647 -expires => $expires,
7648 -content_disposition => 'inline; filename="' . "$filename" . '"');
7649 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
7650 print "From: " . to_utf8($co{'author'}) . "\n";
7651 print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
7652 print "Subject: " . to_utf8($co{'title'}) . "\n";
7654 print "X-Git-Tag: $tagname\n" if $tagname;
7655 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
7657 foreach my $line (@{$co{'comment'}}) {
7658 print to_utf8($line) . "\n";
7660 print "---\n\n";
7661 } elsif ($format eq 'patch') {
7662 my $filename = basename($project) . "-$hash.patch";
7664 print $cgi->header(
7665 -type => 'text/plain',
7666 -charset => 'utf-8',
7667 -expires => $expires,
7668 -content_disposition => 'inline; filename="' . "$filename" . '"');
7671 # write patch
7672 if ($format eq 'html') {
7673 my $use_parents = !defined $hash_parent ||
7674 $hash_parent eq '-c' || $hash_parent eq '--cc';
7675 git_difftree_body(\@difftree, $hash,
7676 $use_parents ? @{$co{'parents'}} : $hash_parent);
7677 print "<br/>\n";
7679 git_patchset_body($fd, $diff_style,
7680 \@difftree, $hash,
7681 $use_parents ? @{$co{'parents'}} : $hash_parent);
7682 close $fd;
7683 print "</div>\n"; # class="page_body"
7684 git_footer_html();
7686 } elsif ($format eq 'plain') {
7687 local $/ = undef;
7688 print <$fd>;
7689 close $fd
7690 or print "Reading git-diff-tree failed\n";
7691 } elsif ($format eq 'patch') {
7692 local $/ = undef;
7693 print <$fd>;
7694 close $fd
7695 or print "Reading git-format-patch failed\n";
7699 sub git_commitdiff_plain {
7700 git_commitdiff(-format => 'plain');
7703 # format-patch-style patches
7704 sub git_patch {
7705 git_commitdiff(-format => 'patch', -single => 1);
7708 sub git_patches {
7709 git_commitdiff(-format => 'patch');
7712 sub git_history {
7713 git_log_generic('history', \&git_history_body,
7714 $hash_base, $hash_parent_base,
7715 $file_name, $hash);
7718 sub git_search {
7719 $searchtype ||= 'commit';
7721 # check if appropriate features are enabled
7722 gitweb_check_feature('search')
7723 or die_error(403, "Search is disabled");
7724 if ($searchtype eq 'pickaxe') {
7725 # pickaxe may take all resources of your box and run for several minutes
7726 # with every query - so decide by yourself how public you make this feature
7727 gitweb_check_feature('pickaxe')
7728 or die_error(403, "Pickaxe search is disabled");
7730 if ($searchtype eq 'grep') {
7731 # grep search might be potentially CPU-intensive, too
7732 gitweb_check_feature('grep')
7733 or die_error(403, "Grep search is disabled");
7736 if (!defined $searchtext) {
7737 die_error(400, "Text field is empty");
7739 if (!defined $hash) {
7740 $hash = git_get_head_hash($project);
7742 my %co = parse_commit($hash);
7743 if (!%co) {
7744 die_error(404, "Unknown commit object");
7746 if (!defined $page) {
7747 $page = 0;
7750 if ($searchtype eq 'commit' ||
7751 $searchtype eq 'author' ||
7752 $searchtype eq 'committer') {
7753 git_search_message(%co);
7754 } elsif ($searchtype eq 'pickaxe') {
7755 git_search_changes(%co);
7756 } elsif ($searchtype eq 'grep') {
7757 git_search_files(%co);
7758 } else {
7759 die_error(400, "Unknown search type");
7763 sub git_search_help {
7764 git_header_html();
7765 git_print_page_nav('','', $hash,$hash,$hash);
7766 print <<EOT;
7767 <p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without
7768 regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,
7769 the pattern entered is recognized as the POSIX extended
7770 <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case
7771 insensitive).</p>
7772 <dl>
7773 <dt><b>commit</b></dt>
7774 <dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
7776 my $have_grep = gitweb_check_feature('grep');
7777 if ($have_grep) {
7778 print <<EOT;
7779 <dt><b>grep</b></dt>
7780 <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
7781 a different one) are searched for the given pattern. On large trees, this search can take
7782 a while and put some strain on the server, so please use it with some consideration. Note that
7783 due to git-grep peculiarity, currently if regexp mode is turned off, the matches are
7784 case-sensitive.</dd>
7787 print <<EOT;
7788 <dt><b>author</b></dt>
7789 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>
7790 <dt><b>committer</b></dt>
7791 <dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>
7793 my $have_pickaxe = gitweb_check_feature('pickaxe');
7794 if ($have_pickaxe) {
7795 print <<EOT;
7796 <dt><b>pickaxe</b></dt>
7797 <dd>All commits that caused the string to appear or disappear from any file (changes that
7798 added, removed or "modified" the string) will be listed. This search can take a while and
7799 takes a lot of strain on the server, so please use it wisely. Note that since you may be
7800 interested even in changes just changing the case as well, this search is case sensitive.</dd>
7803 print "</dl>\n";
7804 git_footer_html();
7807 sub git_shortlog {
7808 git_log_generic('shortlog', \&git_shortlog_body,
7809 $hash, $hash_parent);
7812 ## ......................................................................
7813 ## feeds (RSS, Atom; OPML)
7815 sub git_feed {
7816 my $format = shift || 'atom';
7817 my $have_blame = gitweb_check_feature('blame');
7819 # Atom: http://www.atomenabled.org/developers/syndication/
7820 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
7821 if ($format ne 'rss' && $format ne 'atom') {
7822 die_error(400, "Unknown web feed format");
7825 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
7826 my $head = $hash || 'HEAD';
7827 my @commitlist = parse_commits($head, 150, 0, $file_name);
7829 my %latest_commit;
7830 my %latest_date;
7831 my $content_type = "application/$format+xml";
7832 if (defined $cgi->http('HTTP_ACCEPT') &&
7833 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
7834 # browser (feed reader) prefers text/xml
7835 $content_type = 'text/xml';
7837 if (defined($commitlist[0])) {
7838 %latest_commit = %{$commitlist[0]};
7839 my $latest_epoch = $latest_commit{'committer_epoch'};
7840 %latest_date = parse_date($latest_epoch, $latest_commit{'comitter_tz'});
7841 my $if_modified = $cgi->http('IF_MODIFIED_SINCE');
7842 if (defined $if_modified) {
7843 my $since;
7844 if (eval { require HTTP::Date; 1; }) {
7845 $since = HTTP::Date::str2time($if_modified);
7846 } elsif (eval { require Time::ParseDate; 1; }) {
7847 $since = Time::ParseDate::parsedate($if_modified, GMT => 1);
7849 if (defined $since && $latest_epoch <= $since) {
7850 print $cgi->header(
7851 -type => $content_type,
7852 -charset => 'utf-8',
7853 -last_modified => $latest_date{'rfc2822'},
7854 -status => '304 Not Modified');
7855 return;
7858 print $cgi->header(
7859 -type => $content_type,
7860 -charset => 'utf-8',
7861 -last_modified => $latest_date{'rfc2822'});
7862 } else {
7863 print $cgi->header(
7864 -type => $content_type,
7865 -charset => 'utf-8');
7868 # Optimization: skip generating the body if client asks only
7869 # for Last-Modified date.
7870 return if ($cgi->request_method() eq 'HEAD');
7872 # header variables
7873 my $title = "$site_name - $project/$action";
7874 my $feed_type = 'log';
7875 if (defined $hash) {
7876 $title .= " - '$hash'";
7877 $feed_type = 'branch log';
7878 if (defined $file_name) {
7879 $title .= " :: $file_name";
7880 $feed_type = 'history';
7882 } elsif (defined $file_name) {
7883 $title .= " - $file_name";
7884 $feed_type = 'history';
7886 $title .= " $feed_type";
7887 my $descr = git_get_project_description($project);
7888 if (defined $descr) {
7889 $descr = esc_html($descr);
7890 } else {
7891 $descr = "$project " .
7892 ($format eq 'rss' ? 'RSS' : 'Atom') .
7893 " feed";
7895 my $owner = git_get_project_owner($project);
7896 $owner = esc_html($owner);
7898 #header
7899 my $alt_url;
7900 if (defined $file_name) {
7901 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
7902 } elsif (defined $hash) {
7903 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
7904 } else {
7905 $alt_url = href(-full=>1, action=>"summary");
7907 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
7908 if ($format eq 'rss') {
7909 print <<XML;
7910 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
7911 <channel>
7913 print "<title>$title</title>\n" .
7914 "<link>$alt_url</link>\n" .
7915 "<description>$descr</description>\n" .
7916 "<language>en</language>\n" .
7917 # project owner is responsible for 'editorial' content
7918 "<managingEditor>$owner</managingEditor>\n";
7919 if (defined $logo || defined $favicon) {
7920 # prefer the logo to the favicon, since RSS
7921 # doesn't allow both
7922 my $img = esc_url($logo || $favicon);
7923 print "<image>\n" .
7924 "<url>$img</url>\n" .
7925 "<title>$title</title>\n" .
7926 "<link>$alt_url</link>\n" .
7927 "</image>\n";
7929 if (%latest_date) {
7930 print "<pubDate>$latest_date{'rfc2822'}</pubDate>\n";
7931 print "<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";
7933 print "<generator>gitweb v.$version/$git_version</generator>\n";
7934 } elsif ($format eq 'atom') {
7935 print <<XML;
7936 <feed xmlns="http://www.w3.org/2005/Atom">
7938 print "<title>$title</title>\n" .
7939 "<subtitle>$descr</subtitle>\n" .
7940 '<link rel="alternate" type="text/html" href="' .
7941 $alt_url . '" />' . "\n" .
7942 '<link rel="self" type="' . $content_type . '" href="' .
7943 $cgi->self_url() . '" />' . "\n" .
7944 "<id>" . href(-full=>1) . "</id>\n" .
7945 # use project owner for feed author
7946 "<author><name>$owner</name></author>\n";
7947 if (defined $favicon) {
7948 print "<icon>" . esc_url($favicon) . "</icon>\n";
7950 if (defined $logo) {
7951 # not twice as wide as tall: 72 x 27 pixels
7952 print "<logo>" . esc_url($logo) . "</logo>\n";
7954 if (! %latest_date) {
7955 # dummy date to keep the feed valid until commits trickle in:
7956 print "<updated>1970-01-01T00:00:00Z</updated>\n";
7957 } else {
7958 print "<updated>$latest_date{'iso-8601'}</updated>\n";
7960 print "<generator version='$version/$git_version'>gitweb</generator>\n";
7963 # contents
7964 for (my $i = 0; $i <= $#commitlist; $i++) {
7965 my %co = %{$commitlist[$i]};
7966 my $commit = $co{'id'};
7967 # we read 150, we always show 30 and the ones more recent than 48 hours
7968 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
7969 last;
7971 my %cd = parse_date($co{'author_epoch'}, $co{'author_tz'});
7973 # get list of changed files
7974 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
7975 $co{'parent'} || "--root",
7976 $co{'id'}, "--", (defined $file_name ? $file_name : ())
7977 or next;
7978 my @difftree = map { chomp; $_ } <$fd>;
7979 close $fd
7980 or next;
7982 # print element (entry, item)
7983 my $co_url = href(-full=>1, action=>"commitdiff", hash=>$commit);
7984 if ($format eq 'rss') {
7985 print "<item>\n" .
7986 "<title>" . esc_html($co{'title'}) . "</title>\n" .
7987 "<author>" . esc_html($co{'author'}) . "</author>\n" .
7988 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
7989 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
7990 "<link>$co_url</link>\n" .
7991 "<description>" . esc_html($co{'title'}) . "</description>\n" .
7992 "<content:encoded>" .
7993 "<![CDATA[\n";
7994 } elsif ($format eq 'atom') {
7995 print "<entry>\n" .
7996 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
7997 "<updated>$cd{'iso-8601'}</updated>\n" .
7998 "<author>\n" .
7999 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
8000 if ($co{'author_email'}) {
8001 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
8003 print "</author>\n" .
8004 # use committer for contributor
8005 "<contributor>\n" .
8006 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
8007 if ($co{'committer_email'}) {
8008 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
8010 print "</contributor>\n" .
8011 "<published>$cd{'iso-8601'}</published>\n" .
8012 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
8013 "<id>$co_url</id>\n" .
8014 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
8015 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
8017 my $comment = $co{'comment'};
8018 print "<pre>\n";
8019 foreach my $line (@$comment) {
8020 $line = esc_html($line);
8021 print "$line\n";
8023 print "</pre><ul>\n";
8024 foreach my $difftree_line (@difftree) {
8025 my %difftree = parse_difftree_raw_line($difftree_line);
8026 next if !$difftree{'from_id'};
8028 my $file = $difftree{'file'} || $difftree{'to_file'};
8030 print "<li>" .
8031 "[" .
8032 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
8033 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
8034 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
8035 file_name=>$file, file_parent=>$difftree{'from_file'}),
8036 -title => "diff"}, 'D');
8037 if ($have_blame) {
8038 print $cgi->a({-href => href(-full=>1, action=>"blame",
8039 file_name=>$file, hash_base=>$commit),
8040 -title => "blame"}, 'B');
8042 # if this is not a feed of a file history
8043 if (!defined $file_name || $file_name ne $file) {
8044 print $cgi->a({-href => href(-full=>1, action=>"history",
8045 file_name=>$file, hash=>$commit),
8046 -title => "history"}, 'H');
8048 $file = esc_path($file);
8049 print "] ".
8050 "$file</li>\n";
8052 if ($format eq 'rss') {
8053 print "</ul>]]>\n" .
8054 "</content:encoded>\n" .
8055 "</item>\n";
8056 } elsif ($format eq 'atom') {
8057 print "</ul>\n</div>\n" .
8058 "</content>\n" .
8059 "</entry>\n";
8063 # end of feed
8064 if ($format eq 'rss') {
8065 print "</channel>\n</rss>\n";
8066 } elsif ($format eq 'atom') {
8067 print "</feed>\n";
8071 sub git_rss {
8072 git_feed('rss');
8075 sub git_atom {
8076 git_feed('atom');
8079 sub git_opml {
8080 my @list = git_get_projects_list($project_filter, $strict_export);
8081 if (!@list) {
8082 die_error(404, "No projects found");
8085 print $cgi->header(
8086 -type => 'text/xml',
8087 -charset => 'utf-8',
8088 -content_disposition => 'inline; filename="opml.xml"');
8090 my $title = esc_html($site_name);
8091 my $filter = " within subdirectory ";
8092 if (defined $project_filter) {
8093 $filter .= esc_html($project_filter);
8094 } else {
8095 $filter = "";
8097 print <<XML;
8098 <?xml version="1.0" encoding="utf-8"?>
8099 <opml version="1.0">
8100 <head>
8101 <title>$title OPML Export$filter</title>
8102 </head>
8103 <body>
8104 <outline text="git RSS feeds">
8107 foreach my $pr (@list) {
8108 my %proj = %$pr;
8109 my $head = git_get_head_hash($proj{'path'});
8110 if (!defined $head) {
8111 next;
8113 $git_dir = "$projectroot/$proj{'path'}";
8114 my %co = parse_commit($head);
8115 if (!%co) {
8116 next;
8119 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
8120 my $rss = href('project' => $proj{'path'}, 'action' => 'rss', -full => 1);
8121 my $html = href('project' => $proj{'path'}, 'action' => 'summary', -full => 1);
8122 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
8124 print <<XML;
8125 </outline>
8126 </body>
8127 </opml>