Git 1.7.9.3
[git/jnareb-git.git] / gitweb / gitweb.perl
blob20ace61b6d158934068bb1e25408336f2058781f
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 # show repository only if this subroutine returns true
137 # when given the path to the project, for example:
138 # sub { return -e "$_[0]/git-daemon-export-ok"; }
139 our $export_auth_hook = undef;
141 # only allow viewing of repositories also shown on the overview page
142 our $strict_export = "++GITWEB_STRICT_EXPORT++";
144 # list of git base URLs used for URL to where fetch project from,
145 # i.e. full URL is "$git_base_url/$project"
146 our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
148 # default blob_plain mimetype and default charset for text/plain blob
149 our $default_blob_plain_mimetype = 'text/plain';
150 our $default_text_plain_charset = undef;
152 # file to use for guessing MIME types before trying /etc/mime.types
153 # (relative to the current git repository)
154 our $mimetypes_file = undef;
156 # assume this charset if line contains non-UTF-8 characters;
157 # it should be valid encoding (see Encoding::Supported(3pm) for list),
158 # for which encoding all byte sequences are valid, for example
159 # 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
160 # could be even 'utf-8' for the old behavior)
161 our $fallback_encoding = 'latin1';
163 # rename detection options for git-diff and git-diff-tree
164 # - default is '-M', with the cost proportional to
165 # (number of removed files) * (number of new files).
166 # - more costly is '-C' (which implies '-M'), with the cost proportional to
167 # (number of changed files + number of removed files) * (number of new files)
168 # - even more costly is '-C', '--find-copies-harder' with cost
169 # (number of files in the original tree) * (number of new files)
170 # - one might want to include '-B' option, e.g. '-B', '-M'
171 our @diff_opts = ('-M'); # taken from git_commit
173 # Disables features that would allow repository owners to inject script into
174 # the gitweb domain.
175 our $prevent_xss = 0;
177 # Path to the highlight executable to use (must be the one from
178 # http://www.andre-simon.de due to assumptions about parameters and output).
179 # Useful if highlight is not installed on your webserver's PATH.
180 # [Default: highlight]
181 our $highlight_bin = "++HIGHLIGHT_BIN++";
183 # information about snapshot formats that gitweb is capable of serving
184 our %known_snapshot_formats = (
185 # name => {
186 # 'display' => display name,
187 # 'type' => mime type,
188 # 'suffix' => filename suffix,
189 # 'format' => --format for git-archive,
190 # 'compressor' => [compressor command and arguments]
191 # (array reference, optional)
192 # 'disabled' => boolean (optional)}
194 'tgz' => {
195 'display' => 'tar.gz',
196 'type' => 'application/x-gzip',
197 'suffix' => '.tar.gz',
198 'format' => 'tar',
199 'compressor' => ['gzip', '-n']},
201 'tbz2' => {
202 'display' => 'tar.bz2',
203 'type' => 'application/x-bzip2',
204 'suffix' => '.tar.bz2',
205 'format' => 'tar',
206 'compressor' => ['bzip2']},
208 'txz' => {
209 'display' => 'tar.xz',
210 'type' => 'application/x-xz',
211 'suffix' => '.tar.xz',
212 'format' => 'tar',
213 'compressor' => ['xz'],
214 'disabled' => 1},
216 'zip' => {
217 'display' => 'zip',
218 'type' => 'application/x-zip',
219 'suffix' => '.zip',
220 'format' => 'zip'},
223 # Aliases so we understand old gitweb.snapshot values in repository
224 # configuration.
225 our %known_snapshot_format_aliases = (
226 'gzip' => 'tgz',
227 'bzip2' => 'tbz2',
228 'xz' => 'txz',
230 # backward compatibility: legacy gitweb config support
231 'x-gzip' => undef, 'gz' => undef,
232 'x-bzip2' => undef, 'bz2' => undef,
233 'x-zip' => undef, '' => undef,
236 # Pixel sizes for icons and avatars. If the default font sizes or lineheights
237 # are changed, it may be appropriate to change these values too via
238 # $GITWEB_CONFIG.
239 our %avatar_size = (
240 'default' => 16,
241 'double' => 32
244 # Used to set the maximum load that we will still respond to gitweb queries.
245 # If server load exceed this value then return "503 server busy" error.
246 # If gitweb cannot determined server load, it is taken to be 0.
247 # Leave it undefined (or set to 'undef') to turn off load checking.
248 our $maxload = 300;
250 # configuration for 'highlight' (http://www.andre-simon.de/)
251 # match by basename
252 our %highlight_basename = (
253 #'Program' => 'py',
254 #'Library' => 'py',
255 'SConstruct' => 'py', # SCons equivalent of Makefile
256 'Makefile' => 'make',
258 # match by extension
259 our %highlight_ext = (
260 # main extensions, defining name of syntax;
261 # see files in /usr/share/highlight/langDefs/ directory
262 map { $_ => $_ }
263 qw(py c cpp rb java css php sh pl js tex bib xml awk bat ini spec tcl sql make),
264 # alternate extensions, see /etc/highlight/filetypes.conf
265 'h' => 'c',
266 map { $_ => 'sh' } qw(bash zsh ksh),
267 map { $_ => 'cpp' } qw(cxx c++ cc),
268 map { $_ => 'php' } qw(php3 php4 php5 phps),
269 map { $_ => 'pl' } qw(perl pm), # perhaps also 'cgi'
270 map { $_ => 'make'} qw(mak mk),
271 map { $_ => 'xml' } qw(xhtml html htm),
274 # You define site-wide feature defaults here; override them with
275 # $GITWEB_CONFIG as necessary.
276 our %feature = (
277 # feature => {
278 # 'sub' => feature-sub (subroutine),
279 # 'override' => allow-override (boolean),
280 # 'default' => [ default options...] (array reference)}
282 # if feature is overridable (it means that allow-override has true value),
283 # then feature-sub will be called with default options as parameters;
284 # return value of feature-sub indicates if to enable specified feature
286 # if there is no 'sub' key (no feature-sub), then feature cannot be
287 # overridden
289 # use gitweb_get_feature(<feature>) to retrieve the <feature> value
290 # (an array) or gitweb_check_feature(<feature>) to check if <feature>
291 # is enabled
293 # Enable the 'blame' blob view, showing the last commit that modified
294 # each line in the file. This can be very CPU-intensive.
296 # To enable system wide have in $GITWEB_CONFIG
297 # $feature{'blame'}{'default'} = [1];
298 # To have project specific config enable override in $GITWEB_CONFIG
299 # $feature{'blame'}{'override'} = 1;
300 # and in project config gitweb.blame = 0|1;
301 'blame' => {
302 'sub' => sub { feature_bool('blame', @_) },
303 'override' => 0,
304 'default' => [0]},
306 # Enable the 'snapshot' link, providing a compressed archive of any
307 # tree. This can potentially generate high traffic if you have large
308 # project.
310 # Value is a list of formats defined in %known_snapshot_formats that
311 # you wish to offer.
312 # To disable system wide have in $GITWEB_CONFIG
313 # $feature{'snapshot'}{'default'} = [];
314 # To have project specific config enable override in $GITWEB_CONFIG
315 # $feature{'snapshot'}{'override'} = 1;
316 # and in project config, a comma-separated list of formats or "none"
317 # to disable. Example: gitweb.snapshot = tbz2,zip;
318 'snapshot' => {
319 'sub' => \&feature_snapshot,
320 'override' => 0,
321 'default' => ['tgz']},
323 # Enable text search, which will list the commits which match author,
324 # committer or commit text to a given string. Enabled by default.
325 # Project specific override is not supported.
327 # Note that this controls all search features, which means that if
328 # it is disabled, then 'grep' and 'pickaxe' search would also be
329 # disabled.
330 'search' => {
331 'override' => 0,
332 'default' => [1]},
334 # Enable grep search, which will list the files in currently selected
335 # tree containing the given string. Enabled by default. This can be
336 # potentially CPU-intensive, of course.
337 # Note that you need to have 'search' feature enabled too.
339 # To enable system wide have in $GITWEB_CONFIG
340 # $feature{'grep'}{'default'} = [1];
341 # To have project specific config enable override in $GITWEB_CONFIG
342 # $feature{'grep'}{'override'} = 1;
343 # and in project config gitweb.grep = 0|1;
344 'grep' => {
345 'sub' => sub { feature_bool('grep', @_) },
346 'override' => 0,
347 'default' => [1]},
349 # Enable the pickaxe search, which will list the commits that modified
350 # a given string in a file. This can be practical and quite faster
351 # alternative to 'blame', but still potentially CPU-intensive.
352 # Note that you need to have 'search' feature enabled too.
354 # To enable system wide have in $GITWEB_CONFIG
355 # $feature{'pickaxe'}{'default'} = [1];
356 # To have project specific config enable override in $GITWEB_CONFIG
357 # $feature{'pickaxe'}{'override'} = 1;
358 # and in project config gitweb.pickaxe = 0|1;
359 'pickaxe' => {
360 'sub' => sub { feature_bool('pickaxe', @_) },
361 'override' => 0,
362 'default' => [1]},
364 # Enable showing size of blobs in a 'tree' view, in a separate
365 # column, similar to what 'ls -l' does. This cost a bit of IO.
367 # To disable system wide have in $GITWEB_CONFIG
368 # $feature{'show-sizes'}{'default'} = [0];
369 # To have project specific config enable override in $GITWEB_CONFIG
370 # $feature{'show-sizes'}{'override'} = 1;
371 # and in project config gitweb.showsizes = 0|1;
372 'show-sizes' => {
373 'sub' => sub { feature_bool('showsizes', @_) },
374 'override' => 0,
375 'default' => [1]},
377 # Make gitweb use an alternative format of the URLs which can be
378 # more readable and natural-looking: project name is embedded
379 # directly in the path and the query string contains other
380 # auxiliary information. All gitweb installations recognize
381 # URL in either format; this configures in which formats gitweb
382 # generates links.
384 # To enable system wide have in $GITWEB_CONFIG
385 # $feature{'pathinfo'}{'default'} = [1];
386 # Project specific override is not supported.
388 # Note that you will need to change the default location of CSS,
389 # favicon, logo and possibly other files to an absolute URL. Also,
390 # if gitweb.cgi serves as your indexfile, you will need to force
391 # $my_uri to contain the script name in your $GITWEB_CONFIG.
392 'pathinfo' => {
393 'override' => 0,
394 'default' => [0]},
396 # Make gitweb consider projects in project root subdirectories
397 # to be forks of existing projects. Given project $projname.git,
398 # projects matching $projname/*.git will not be shown in the main
399 # projects list, instead a '+' mark will be added to $projname
400 # there and a 'forks' view will be enabled for the project, listing
401 # all the forks. If project list is taken from a file, forks have
402 # to be listed after the main project.
404 # To enable system wide have in $GITWEB_CONFIG
405 # $feature{'forks'}{'default'} = [1];
406 # Project specific override is not supported.
407 'forks' => {
408 'override' => 0,
409 'default' => [0]},
411 # Insert custom links to the action bar of all project pages.
412 # This enables you mainly to link to third-party scripts integrating
413 # into gitweb; e.g. git-browser for graphical history representation
414 # or custom web-based repository administration interface.
416 # The 'default' value consists of a list of triplets in the form
417 # (label, link, position) where position is the label after which
418 # to insert the link and link is a format string where %n expands
419 # to the project name, %f to the project path within the filesystem,
420 # %h to the current hash (h gitweb parameter) and %b to the current
421 # hash base (hb gitweb parameter); %% expands to %.
423 # To enable system wide have in $GITWEB_CONFIG e.g.
424 # $feature{'actions'}{'default'} = [('graphiclog',
425 # '/git-browser/by-commit.html?r=%n', 'summary')];
426 # Project specific override is not supported.
427 'actions' => {
428 'override' => 0,
429 'default' => []},
431 # Allow gitweb scan project content tags of project repository,
432 # and display the popular Web 2.0-ish "tag cloud" near the projects
433 # list. Note that this is something COMPLETELY different from the
434 # normal Git tags.
436 # gitweb by itself can show existing tags, but it does not handle
437 # tagging itself; you need to do it externally, outside gitweb.
438 # The format is described in git_get_project_ctags() subroutine.
439 # You may want to install the HTML::TagCloud Perl module to get
440 # a pretty tag cloud instead of just a list of tags.
442 # To enable system wide have in $GITWEB_CONFIG
443 # $feature{'ctags'}{'default'} = [1];
444 # Project specific override is not supported.
446 # In the future whether ctags editing is enabled might depend
447 # on the value, but using 1 should always mean no editing of ctags.
448 'ctags' => {
449 'override' => 0,
450 'default' => [0]},
452 # The maximum number of patches in a patchset generated in patch
453 # view. Set this to 0 or undef to disable patch view, or to a
454 # negative number to remove any limit.
456 # To disable system wide have in $GITWEB_CONFIG
457 # $feature{'patches'}{'default'} = [0];
458 # To have project specific config enable override in $GITWEB_CONFIG
459 # $feature{'patches'}{'override'} = 1;
460 # and in project config gitweb.patches = 0|n;
461 # where n is the maximum number of patches allowed in a patchset.
462 'patches' => {
463 'sub' => \&feature_patches,
464 'override' => 0,
465 'default' => [16]},
467 # Avatar support. When this feature is enabled, views such as
468 # shortlog or commit will display an avatar associated with
469 # the email of the committer(s) and/or author(s).
471 # Currently available providers are gravatar and picon.
472 # If an unknown provider is specified, the feature is disabled.
474 # Gravatar depends on Digest::MD5.
475 # Picon currently relies on the indiana.edu database.
477 # To enable system wide have in $GITWEB_CONFIG
478 # $feature{'avatar'}{'default'} = ['<provider>'];
479 # where <provider> is either gravatar or picon.
480 # To have project specific config enable override in $GITWEB_CONFIG
481 # $feature{'avatar'}{'override'} = 1;
482 # and in project config gitweb.avatar = <provider>;
483 'avatar' => {
484 'sub' => \&feature_avatar,
485 'override' => 0,
486 'default' => ['']},
488 # Enable displaying how much time and how many git commands
489 # it took to generate and display page. Disabled by default.
490 # Project specific override is not supported.
491 'timed' => {
492 'override' => 0,
493 'default' => [0]},
495 # Enable turning some links into links to actions which require
496 # JavaScript to run (like 'blame_incremental'). Not enabled by
497 # default. Project specific override is currently not supported.
498 'javascript-actions' => {
499 'override' => 0,
500 'default' => [0]},
502 # Enable and configure ability to change common timezone for dates
503 # in gitweb output via JavaScript. Enabled by default.
504 # Project specific override is not supported.
505 'javascript-timezone' => {
506 'override' => 0,
507 'default' => [
508 'local', # default timezone: 'utc', 'local', or '(-|+)HHMM' format,
509 # or undef to turn off this feature
510 'gitweb_tz', # name of cookie where to store selected timezone
511 'datetime', # CSS class used to mark up dates for manipulation
514 # Syntax highlighting support. This is based on Daniel Svensson's
515 # and Sham Chukoury's work in gitweb-xmms2.git.
516 # It requires the 'highlight' program present in $PATH,
517 # and therefore is disabled by default.
519 # To enable system wide have in $GITWEB_CONFIG
520 # $feature{'highlight'}{'default'} = [1];
522 'highlight' => {
523 'sub' => sub { feature_bool('highlight', @_) },
524 'override' => 0,
525 'default' => [0]},
527 # Enable displaying of remote heads in the heads list
529 # To enable system wide have in $GITWEB_CONFIG
530 # $feature{'remote_heads'}{'default'} = [1];
531 # To have project specific config enable override in $GITWEB_CONFIG
532 # $feature{'remote_heads'}{'override'} = 1;
533 # and in project config gitweb.remote_heads = 0|1;
534 'remote_heads' => {
535 'sub' => sub { feature_bool('remote_heads', @_) },
536 'override' => 0,
537 'default' => [0]},
540 sub gitweb_get_feature {
541 my ($name) = @_;
542 return unless exists $feature{$name};
543 my ($sub, $override, @defaults) = (
544 $feature{$name}{'sub'},
545 $feature{$name}{'override'},
546 @{$feature{$name}{'default'}});
547 # project specific override is possible only if we have project
548 our $git_dir; # global variable, declared later
549 if (!$override || !defined $git_dir) {
550 return @defaults;
552 if (!defined $sub) {
553 warn "feature $name is not overridable";
554 return @defaults;
556 return $sub->(@defaults);
559 # A wrapper to check if a given feature is enabled.
560 # With this, you can say
562 # my $bool_feat = gitweb_check_feature('bool_feat');
563 # gitweb_check_feature('bool_feat') or somecode;
565 # instead of
567 # my ($bool_feat) = gitweb_get_feature('bool_feat');
568 # (gitweb_get_feature('bool_feat'))[0] or somecode;
570 sub gitweb_check_feature {
571 return (gitweb_get_feature(@_))[0];
575 sub feature_bool {
576 my $key = shift;
577 my ($val) = git_get_project_config($key, '--bool');
579 if (!defined $val) {
580 return ($_[0]);
581 } elsif ($val eq 'true') {
582 return (1);
583 } elsif ($val eq 'false') {
584 return (0);
588 sub feature_snapshot {
589 my (@fmts) = @_;
591 my ($val) = git_get_project_config('snapshot');
593 if ($val) {
594 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
597 return @fmts;
600 sub feature_patches {
601 my @val = (git_get_project_config('patches', '--int'));
603 if (@val) {
604 return @val;
607 return ($_[0]);
610 sub feature_avatar {
611 my @val = (git_get_project_config('avatar'));
613 return @val ? @val : @_;
616 # checking HEAD file with -e is fragile if the repository was
617 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
618 # and then pruned.
619 sub check_head_link {
620 my ($dir) = @_;
621 my $headfile = "$dir/HEAD";
622 return ((-e $headfile) ||
623 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
626 sub check_export_ok {
627 my ($dir) = @_;
628 return (check_head_link($dir) &&
629 (!$export_ok || -e "$dir/$export_ok") &&
630 (!$export_auth_hook || $export_auth_hook->($dir)));
633 # process alternate names for backward compatibility
634 # filter out unsupported (unknown) snapshot formats
635 sub filter_snapshot_fmts {
636 my @fmts = @_;
638 @fmts = map {
639 exists $known_snapshot_format_aliases{$_} ?
640 $known_snapshot_format_aliases{$_} : $_} @fmts;
641 @fmts = grep {
642 exists $known_snapshot_formats{$_} &&
643 !$known_snapshot_formats{$_}{'disabled'}} @fmts;
646 # If it is set to code reference, it is code that it is to be run once per
647 # request, allowing updating configurations that change with each request,
648 # while running other code in config file only once.
650 # Otherwise, if it is false then gitweb would process config file only once;
651 # if it is true then gitweb config would be run for each request.
652 our $per_request_config = 1;
654 # read and parse gitweb config file given by its parameter.
655 # returns true on success, false on recoverable error, allowing
656 # to chain this subroutine, using first file that exists.
657 # dies on errors during parsing config file, as it is unrecoverable.
658 sub read_config_file {
659 my $filename = shift;
660 return unless defined $filename;
661 # die if there are errors parsing config file
662 if (-e $filename) {
663 do $filename;
664 die $@ if $@;
665 return 1;
667 return;
670 our ($GITWEB_CONFIG, $GITWEB_CONFIG_SYSTEM, $GITWEB_CONFIG_COMMON);
671 sub evaluate_gitweb_config {
672 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
673 our $GITWEB_CONFIG_SYSTEM = $ENV{'GITWEB_CONFIG_SYSTEM'} || "++GITWEB_CONFIG_SYSTEM++";
674 our $GITWEB_CONFIG_COMMON = $ENV{'GITWEB_CONFIG_COMMON'} || "++GITWEB_CONFIG_COMMON++";
676 # Protect agains duplications of file names, to not read config twice.
677 # Only one of $GITWEB_CONFIG and $GITWEB_CONFIG_SYSTEM is used, so
678 # there possibility of duplication of filename there doesn't matter.
679 $GITWEB_CONFIG = "" if ($GITWEB_CONFIG eq $GITWEB_CONFIG_COMMON);
680 $GITWEB_CONFIG_SYSTEM = "" if ($GITWEB_CONFIG_SYSTEM eq $GITWEB_CONFIG_COMMON);
682 # Common system-wide settings for convenience.
683 # Those settings can be ovverriden by GITWEB_CONFIG or GITWEB_CONFIG_SYSTEM.
684 read_config_file($GITWEB_CONFIG_COMMON);
686 # Use first config file that exists. This means use the per-instance
687 # GITWEB_CONFIG if exists, otherwise use GITWEB_SYSTEM_CONFIG.
688 read_config_file($GITWEB_CONFIG) and return;
689 read_config_file($GITWEB_CONFIG_SYSTEM);
692 # Get loadavg of system, to compare against $maxload.
693 # Currently it requires '/proc/loadavg' present to get loadavg;
694 # if it is not present it returns 0, which means no load checking.
695 sub get_loadavg {
696 if( -e '/proc/loadavg' ){
697 open my $fd, '<', '/proc/loadavg'
698 or return 0;
699 my @load = split(/\s+/, scalar <$fd>);
700 close $fd;
702 # The first three columns measure CPU and IO utilization of the last one,
703 # five, and 10 minute periods. The fourth column shows the number of
704 # currently running processes and the total number of processes in the m/n
705 # format. The last column displays the last process ID used.
706 return $load[0] || 0;
708 # additional checks for load average should go here for things that don't export
709 # /proc/loadavg
711 return 0;
714 # version of the core git binary
715 our $git_version;
716 sub evaluate_git_version {
717 our $git_version = qx("$GIT" --version) =~ m/git version (.*)$/ ? $1 : "unknown";
718 $number_of_git_cmds++;
721 sub check_loadavg {
722 if (defined $maxload && get_loadavg() > $maxload) {
723 die_error(503, "The load average on the server is too high");
727 # ======================================================================
728 # input validation and dispatch
730 # input parameters can be collected from a variety of sources (presently, CGI
731 # and PATH_INFO), so we define an %input_params hash that collects them all
732 # together during validation: this allows subsequent uses (e.g. href()) to be
733 # agnostic of the parameter origin
735 our %input_params = ();
737 # input parameters are stored with the long parameter name as key. This will
738 # also be used in the href subroutine to convert parameters to their CGI
739 # equivalent, and since the href() usage is the most frequent one, we store
740 # the name -> CGI key mapping here, instead of the reverse.
742 # XXX: Warning: If you touch this, check the search form for updating,
743 # too.
745 our @cgi_param_mapping = (
746 project => "p",
747 action => "a",
748 file_name => "f",
749 file_parent => "fp",
750 hash => "h",
751 hash_parent => "hp",
752 hash_base => "hb",
753 hash_parent_base => "hpb",
754 page => "pg",
755 order => "o",
756 searchtext => "s",
757 searchtype => "st",
758 snapshot_format => "sf",
759 extra_options => "opt",
760 search_use_regexp => "sr",
761 ctag => "by_tag",
762 diff_style => "ds",
763 # this must be last entry (for manipulation from JavaScript)
764 javascript => "js"
766 our %cgi_param_mapping = @cgi_param_mapping;
768 # we will also need to know the possible actions, for validation
769 our %actions = (
770 "blame" => \&git_blame,
771 "blame_incremental" => \&git_blame_incremental,
772 "blame_data" => \&git_blame_data,
773 "blobdiff" => \&git_blobdiff,
774 "blobdiff_plain" => \&git_blobdiff_plain,
775 "blob" => \&git_blob,
776 "blob_plain" => \&git_blob_plain,
777 "commitdiff" => \&git_commitdiff,
778 "commitdiff_plain" => \&git_commitdiff_plain,
779 "commit" => \&git_commit,
780 "forks" => \&git_forks,
781 "heads" => \&git_heads,
782 "history" => \&git_history,
783 "log" => \&git_log,
784 "patch" => \&git_patch,
785 "patches" => \&git_patches,
786 "remotes" => \&git_remotes,
787 "rss" => \&git_rss,
788 "atom" => \&git_atom,
789 "search" => \&git_search,
790 "search_help" => \&git_search_help,
791 "shortlog" => \&git_shortlog,
792 "summary" => \&git_summary,
793 "tag" => \&git_tag,
794 "tags" => \&git_tags,
795 "tree" => \&git_tree,
796 "snapshot" => \&git_snapshot,
797 "object" => \&git_object,
798 # those below don't need $project
799 "opml" => \&git_opml,
800 "project_list" => \&git_project_list,
801 "project_index" => \&git_project_index,
804 # finally, we have the hash of allowed extra_options for the commands that
805 # allow them
806 our %allowed_options = (
807 "--no-merges" => [ qw(rss atom log shortlog history) ],
810 # fill %input_params with the CGI parameters. All values except for 'opt'
811 # should be single values, but opt can be an array. We should probably
812 # build an array of parameters that can be multi-valued, but since for the time
813 # being it's only this one, we just single it out
814 sub evaluate_query_params {
815 our $cgi;
817 while (my ($name, $symbol) = each %cgi_param_mapping) {
818 if ($symbol eq 'opt') {
819 $input_params{$name} = [ map { decode_utf8($_) } $cgi->param($symbol) ];
820 } else {
821 $input_params{$name} = decode_utf8($cgi->param($symbol));
826 # now read PATH_INFO and update the parameter list for missing parameters
827 sub evaluate_path_info {
828 return if defined $input_params{'project'};
829 return if !$path_info;
830 $path_info =~ s,^/+,,;
831 return if !$path_info;
833 # find which part of PATH_INFO is project
834 my $project = $path_info;
835 $project =~ s,/+$,,;
836 while ($project && !check_head_link("$projectroot/$project")) {
837 $project =~ s,/*[^/]*$,,;
839 return unless $project;
840 $input_params{'project'} = $project;
842 # do not change any parameters if an action is given using the query string
843 return if $input_params{'action'};
844 $path_info =~ s,^\Q$project\E/*,,;
846 # next, check if we have an action
847 my $action = $path_info;
848 $action =~ s,/.*$,,;
849 if (exists $actions{$action}) {
850 $path_info =~ s,^$action/*,,;
851 $input_params{'action'} = $action;
854 # list of actions that want hash_base instead of hash, but can have no
855 # pathname (f) parameter
856 my @wants_base = (
857 'tree',
858 'history',
861 # we want to catch, among others
862 # [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name]
863 my ($parentrefname, $parentpathname, $refname, $pathname) =
864 ($path_info =~ /^(?:(.+?)(?::(.+))?\.\.)?([^:]+?)?(?::(.+))?$/);
866 # first, analyze the 'current' part
867 if (defined $pathname) {
868 # we got "branch:filename" or "branch:dir/"
869 # we could use git_get_type(branch:pathname), but:
870 # - it needs $git_dir
871 # - it does a git() call
872 # - the convention of terminating directories with a slash
873 # makes it superfluous
874 # - embedding the action in the PATH_INFO would make it even
875 # more superfluous
876 $pathname =~ s,^/+,,;
877 if (!$pathname || substr($pathname, -1) eq "/") {
878 $input_params{'action'} ||= "tree";
879 $pathname =~ s,/$,,;
880 } else {
881 # the default action depends on whether we had parent info
882 # or not
883 if ($parentrefname) {
884 $input_params{'action'} ||= "blobdiff_plain";
885 } else {
886 $input_params{'action'} ||= "blob_plain";
889 $input_params{'hash_base'} ||= $refname;
890 $input_params{'file_name'} ||= $pathname;
891 } elsif (defined $refname) {
892 # we got "branch". In this case we have to choose if we have to
893 # set hash or hash_base.
895 # Most of the actions without a pathname only want hash to be
896 # set, except for the ones specified in @wants_base that want
897 # hash_base instead. It should also be noted that hand-crafted
898 # links having 'history' as an action and no pathname or hash
899 # set will fail, but that happens regardless of PATH_INFO.
900 if (defined $parentrefname) {
901 # if there is parent let the default be 'shortlog' action
902 # (for http://git.example.com/repo.git/A..B links); if there
903 # is no parent, dispatch will detect type of object and set
904 # action appropriately if required (if action is not set)
905 $input_params{'action'} ||= "shortlog";
907 if ($input_params{'action'} &&
908 grep { $_ eq $input_params{'action'} } @wants_base) {
909 $input_params{'hash_base'} ||= $refname;
910 } else {
911 $input_params{'hash'} ||= $refname;
915 # next, handle the 'parent' part, if present
916 if (defined $parentrefname) {
917 # a missing pathspec defaults to the 'current' filename, allowing e.g.
918 # someproject/blobdiff/oldrev..newrev:/filename
919 if ($parentpathname) {
920 $parentpathname =~ s,^/+,,;
921 $parentpathname =~ s,/$,,;
922 $input_params{'file_parent'} ||= $parentpathname;
923 } else {
924 $input_params{'file_parent'} ||= $input_params{'file_name'};
926 # we assume that hash_parent_base is wanted if a path was specified,
927 # or if the action wants hash_base instead of hash
928 if (defined $input_params{'file_parent'} ||
929 grep { $_ eq $input_params{'action'} } @wants_base) {
930 $input_params{'hash_parent_base'} ||= $parentrefname;
931 } else {
932 $input_params{'hash_parent'} ||= $parentrefname;
936 # for the snapshot action, we allow URLs in the form
937 # $project/snapshot/$hash.ext
938 # where .ext determines the snapshot and gets removed from the
939 # passed $refname to provide the $hash.
941 # To be able to tell that $refname includes the format extension, we
942 # require the following two conditions to be satisfied:
943 # - the hash input parameter MUST have been set from the $refname part
944 # of the URL (i.e. they must be equal)
945 # - the snapshot format MUST NOT have been defined already (e.g. from
946 # CGI parameter sf)
947 # It's also useless to try any matching unless $refname has a dot,
948 # so we check for that too
949 if (defined $input_params{'action'} &&
950 $input_params{'action'} eq 'snapshot' &&
951 defined $refname && index($refname, '.') != -1 &&
952 $refname eq $input_params{'hash'} &&
953 !defined $input_params{'snapshot_format'}) {
954 # We loop over the known snapshot formats, checking for
955 # extensions. Allowed extensions are both the defined suffix
956 # (which includes the initial dot already) and the snapshot
957 # format key itself, with a prepended dot
958 while (my ($fmt, $opt) = each %known_snapshot_formats) {
959 my $hash = $refname;
960 unless ($hash =~ s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) {
961 next;
963 my $sfx = $1;
964 # a valid suffix was found, so set the snapshot format
965 # and reset the hash parameter
966 $input_params{'snapshot_format'} = $fmt;
967 $input_params{'hash'} = $hash;
968 # we also set the format suffix to the one requested
969 # in the URL: this way a request for e.g. .tgz returns
970 # a .tgz instead of a .tar.gz
971 $known_snapshot_formats{$fmt}{'suffix'} = $sfx;
972 last;
977 our ($action, $project, $file_name, $file_parent, $hash, $hash_parent, $hash_base,
978 $hash_parent_base, @extra_options, $page, $searchtype, $search_use_regexp,
979 $searchtext, $search_regexp);
980 sub evaluate_and_validate_params {
981 our $action = $input_params{'action'};
982 if (defined $action) {
983 if (!validate_action($action)) {
984 die_error(400, "Invalid action parameter");
988 # parameters which are pathnames
989 our $project = $input_params{'project'};
990 if (defined $project) {
991 if (!validate_project($project)) {
992 undef $project;
993 die_error(404, "No such project");
997 our $file_name = $input_params{'file_name'};
998 if (defined $file_name) {
999 if (!validate_pathname($file_name)) {
1000 die_error(400, "Invalid file parameter");
1004 our $file_parent = $input_params{'file_parent'};
1005 if (defined $file_parent) {
1006 if (!validate_pathname($file_parent)) {
1007 die_error(400, "Invalid file parent parameter");
1011 # parameters which are refnames
1012 our $hash = $input_params{'hash'};
1013 if (defined $hash) {
1014 if (!validate_refname($hash)) {
1015 die_error(400, "Invalid hash parameter");
1019 our $hash_parent = $input_params{'hash_parent'};
1020 if (defined $hash_parent) {
1021 if (!validate_refname($hash_parent)) {
1022 die_error(400, "Invalid hash parent parameter");
1026 our $hash_base = $input_params{'hash_base'};
1027 if (defined $hash_base) {
1028 if (!validate_refname($hash_base)) {
1029 die_error(400, "Invalid hash base parameter");
1033 our @extra_options = @{$input_params{'extra_options'}};
1034 # @extra_options is always defined, since it can only be (currently) set from
1035 # CGI, and $cgi->param() returns the empty array in array context if the param
1036 # is not set
1037 foreach my $opt (@extra_options) {
1038 if (not exists $allowed_options{$opt}) {
1039 die_error(400, "Invalid option parameter");
1041 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
1042 die_error(400, "Invalid option parameter for this action");
1046 our $hash_parent_base = $input_params{'hash_parent_base'};
1047 if (defined $hash_parent_base) {
1048 if (!validate_refname($hash_parent_base)) {
1049 die_error(400, "Invalid hash parent base parameter");
1053 # other parameters
1054 our $page = $input_params{'page'};
1055 if (defined $page) {
1056 if ($page =~ m/[^0-9]/) {
1057 die_error(400, "Invalid page parameter");
1061 our $searchtype = $input_params{'searchtype'};
1062 if (defined $searchtype) {
1063 if ($searchtype =~ m/[^a-z]/) {
1064 die_error(400, "Invalid searchtype parameter");
1068 our $search_use_regexp = $input_params{'search_use_regexp'};
1070 our $searchtext = $input_params{'searchtext'};
1071 our $search_regexp;
1072 if (defined $searchtext) {
1073 if (length($searchtext) < 2) {
1074 die_error(403, "At least two characters are required for search parameter");
1076 if ($search_use_regexp) {
1077 $search_regexp = $searchtext;
1078 if (!eval { qr/$search_regexp/; 1; }) {
1079 (my $error = $@) =~ s/ at \S+ line \d+.*\n?//;
1080 die_error(400, "Invalid search regexp '$search_regexp'",
1081 esc_html($error));
1083 } else {
1084 $search_regexp = quotemeta $searchtext;
1089 # path to the current git repository
1090 our $git_dir;
1091 sub evaluate_git_dir {
1092 our $git_dir = "$projectroot/$project" if $project;
1095 our (@snapshot_fmts, $git_avatar);
1096 sub configure_gitweb_features {
1097 # list of supported snapshot formats
1098 our @snapshot_fmts = gitweb_get_feature('snapshot');
1099 @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
1101 # check that the avatar feature is set to a known provider name,
1102 # and for each provider check if the dependencies are satisfied.
1103 # if the provider name is invalid or the dependencies are not met,
1104 # reset $git_avatar to the empty string.
1105 our ($git_avatar) = gitweb_get_feature('avatar');
1106 if ($git_avatar eq 'gravatar') {
1107 $git_avatar = '' unless (eval { require Digest::MD5; 1; });
1108 } elsif ($git_avatar eq 'picon') {
1109 # no dependencies
1110 } else {
1111 $git_avatar = '';
1115 # custom error handler: 'die <message>' is Internal Server Error
1116 sub handle_errors_html {
1117 my $msg = shift; # it is already HTML escaped
1119 # to avoid infinite loop where error occurs in die_error,
1120 # change handler to default handler, disabling handle_errors_html
1121 set_message("Error occured when inside die_error:\n$msg");
1123 # you cannot jump out of die_error when called as error handler;
1124 # the subroutine set via CGI::Carp::set_message is called _after_
1125 # HTTP headers are already written, so it cannot write them itself
1126 die_error(undef, undef, $msg, -error_handler => 1, -no_http_header => 1);
1128 set_message(\&handle_errors_html);
1130 # dispatch
1131 sub dispatch {
1132 if (!defined $action) {
1133 if (defined $hash) {
1134 $action = git_get_type($hash);
1135 } elsif (defined $hash_base && defined $file_name) {
1136 $action = git_get_type("$hash_base:$file_name");
1137 } elsif (defined $project) {
1138 $action = 'summary';
1139 } else {
1140 $action = 'project_list';
1143 if (!defined($actions{$action})) {
1144 die_error(400, "Unknown action");
1146 if ($action !~ m/^(?:opml|project_list|project_index)$/ &&
1147 !$project) {
1148 die_error(400, "Project needed");
1150 $actions{$action}->();
1153 sub reset_timer {
1154 our $t0 = [ gettimeofday() ]
1155 if defined $t0;
1156 our $number_of_git_cmds = 0;
1159 our $first_request = 1;
1160 sub run_request {
1161 reset_timer();
1163 evaluate_uri();
1164 if ($first_request) {
1165 evaluate_gitweb_config();
1166 evaluate_git_version();
1168 if ($per_request_config) {
1169 if (ref($per_request_config) eq 'CODE') {
1170 $per_request_config->();
1171 } elsif (!$first_request) {
1172 evaluate_gitweb_config();
1175 check_loadavg();
1177 # $projectroot and $projects_list might be set in gitweb config file
1178 $projects_list ||= $projectroot;
1180 evaluate_query_params();
1181 evaluate_path_info();
1182 evaluate_and_validate_params();
1183 evaluate_git_dir();
1185 configure_gitweb_features();
1187 dispatch();
1190 our $is_last_request = sub { 1 };
1191 our ($pre_dispatch_hook, $post_dispatch_hook, $pre_listen_hook);
1192 our $CGI = 'CGI';
1193 our $cgi;
1194 sub configure_as_fcgi {
1195 require CGI::Fast;
1196 our $CGI = 'CGI::Fast';
1198 my $request_number = 0;
1199 # let each child service 100 requests
1200 our $is_last_request = sub { ++$request_number > 100 };
1202 sub evaluate_argv {
1203 my $script_name = $ENV{'SCRIPT_NAME'} || $ENV{'SCRIPT_FILENAME'} || __FILE__;
1204 configure_as_fcgi()
1205 if $script_name =~ /\.fcgi$/;
1207 return unless (@ARGV);
1209 require Getopt::Long;
1210 Getopt::Long::GetOptions(
1211 'fastcgi|fcgi|f' => \&configure_as_fcgi,
1212 'nproc|n=i' => sub {
1213 my ($arg, $val) = @_;
1214 return unless eval { require FCGI::ProcManager; 1; };
1215 my $proc_manager = FCGI::ProcManager->new({
1216 n_processes => $val,
1218 our $pre_listen_hook = sub { $proc_manager->pm_manage() };
1219 our $pre_dispatch_hook = sub { $proc_manager->pm_pre_dispatch() };
1220 our $post_dispatch_hook = sub { $proc_manager->pm_post_dispatch() };
1225 sub run {
1226 evaluate_argv();
1228 $first_request = 1;
1229 $pre_listen_hook->()
1230 if $pre_listen_hook;
1232 REQUEST:
1233 while ($cgi = $CGI->new()) {
1234 $pre_dispatch_hook->()
1235 if $pre_dispatch_hook;
1237 run_request();
1239 $post_dispatch_hook->()
1240 if $post_dispatch_hook;
1241 $first_request = 0;
1243 last REQUEST if ($is_last_request->());
1246 DONE_GITWEB:
1250 run();
1252 if (defined caller) {
1253 # wrapped in a subroutine processing requests,
1254 # e.g. mod_perl with ModPerl::Registry, or PSGI with Plack::App::WrapCGI
1255 return;
1256 } else {
1257 # pure CGI script, serving single request
1258 exit;
1261 ## ======================================================================
1262 ## action links
1264 # possible values of extra options
1265 # -full => 0|1 - use absolute/full URL ($my_uri/$my_url as base)
1266 # -replay => 1 - start from a current view (replay with modifications)
1267 # -path_info => 0|1 - don't use/use path_info URL (if possible)
1268 # -anchor => ANCHOR - add #ANCHOR to end of URL, implies -replay if used alone
1269 sub href {
1270 my %params = @_;
1271 # default is to use -absolute url() i.e. $my_uri
1272 my $href = $params{-full} ? $my_url : $my_uri;
1274 # implicit -replay, must be first of implicit params
1275 $params{-replay} = 1 if (keys %params == 1 && $params{-anchor});
1277 $params{'project'} = $project unless exists $params{'project'};
1279 if ($params{-replay}) {
1280 while (my ($name, $symbol) = each %cgi_param_mapping) {
1281 if (!exists $params{$name}) {
1282 $params{$name} = $input_params{$name};
1287 my $use_pathinfo = gitweb_check_feature('pathinfo');
1288 if (defined $params{'project'} &&
1289 (exists $params{-path_info} ? $params{-path_info} : $use_pathinfo)) {
1290 # try to put as many parameters as possible in PATH_INFO:
1291 # - project name
1292 # - action
1293 # - hash_parent or hash_parent_base:/file_parent
1294 # - hash or hash_base:/filename
1295 # - the snapshot_format as an appropriate suffix
1297 # When the script is the root DirectoryIndex for the domain,
1298 # $href here would be something like http://gitweb.example.com/
1299 # Thus, we strip any trailing / from $href, to spare us double
1300 # slashes in the final URL
1301 $href =~ s,/$,,;
1303 # Then add the project name, if present
1304 $href .= "/".esc_path_info($params{'project'});
1305 delete $params{'project'};
1307 # since we destructively absorb parameters, we keep this
1308 # boolean that remembers if we're handling a snapshot
1309 my $is_snapshot = $params{'action'} eq 'snapshot';
1311 # Summary just uses the project path URL, any other action is
1312 # added to the URL
1313 if (defined $params{'action'}) {
1314 $href .= "/".esc_path_info($params{'action'})
1315 unless $params{'action'} eq 'summary';
1316 delete $params{'action'};
1319 # Next, we put hash_parent_base:/file_parent..hash_base:/file_name,
1320 # stripping nonexistent or useless pieces
1321 $href .= "/" if ($params{'hash_base'} || $params{'hash_parent_base'}
1322 || $params{'hash_parent'} || $params{'hash'});
1323 if (defined $params{'hash_base'}) {
1324 if (defined $params{'hash_parent_base'}) {
1325 $href .= esc_path_info($params{'hash_parent_base'});
1326 # skip the file_parent if it's the same as the file_name
1327 if (defined $params{'file_parent'}) {
1328 if (defined $params{'file_name'} && $params{'file_parent'} eq $params{'file_name'}) {
1329 delete $params{'file_parent'};
1330 } elsif ($params{'file_parent'} !~ /\.\./) {
1331 $href .= ":/".esc_path_info($params{'file_parent'});
1332 delete $params{'file_parent'};
1335 $href .= "..";
1336 delete $params{'hash_parent'};
1337 delete $params{'hash_parent_base'};
1338 } elsif (defined $params{'hash_parent'}) {
1339 $href .= esc_path_info($params{'hash_parent'}). "..";
1340 delete $params{'hash_parent'};
1343 $href .= esc_path_info($params{'hash_base'});
1344 if (defined $params{'file_name'} && $params{'file_name'} !~ /\.\./) {
1345 $href .= ":/".esc_path_info($params{'file_name'});
1346 delete $params{'file_name'};
1348 delete $params{'hash'};
1349 delete $params{'hash_base'};
1350 } elsif (defined $params{'hash'}) {
1351 $href .= esc_path_info($params{'hash'});
1352 delete $params{'hash'};
1355 # If the action was a snapshot, we can absorb the
1356 # snapshot_format parameter too
1357 if ($is_snapshot) {
1358 my $fmt = $params{'snapshot_format'};
1359 # snapshot_format should always be defined when href()
1360 # is called, but just in case some code forgets, we
1361 # fall back to the default
1362 $fmt ||= $snapshot_fmts[0];
1363 $href .= $known_snapshot_formats{$fmt}{'suffix'};
1364 delete $params{'snapshot_format'};
1368 # now encode the parameters explicitly
1369 my @result = ();
1370 for (my $i = 0; $i < @cgi_param_mapping; $i += 2) {
1371 my ($name, $symbol) = ($cgi_param_mapping[$i], $cgi_param_mapping[$i+1]);
1372 if (defined $params{$name}) {
1373 if (ref($params{$name}) eq "ARRAY") {
1374 foreach my $par (@{$params{$name}}) {
1375 push @result, $symbol . "=" . esc_param($par);
1377 } else {
1378 push @result, $symbol . "=" . esc_param($params{$name});
1382 $href .= "?" . join(';', @result) if scalar @result;
1384 # final transformation: trailing spaces must be escaped (URI-encoded)
1385 $href =~ s/(\s+)$/CGI::escape($1)/e;
1387 if ($params{-anchor}) {
1388 $href .= "#".esc_param($params{-anchor});
1391 return $href;
1395 ## ======================================================================
1396 ## validation, quoting/unquoting and escaping
1398 sub validate_action {
1399 my $input = shift || return undef;
1400 return undef unless exists $actions{$input};
1401 return $input;
1404 sub validate_project {
1405 my $input = shift || return undef;
1406 if (!validate_pathname($input) ||
1407 !(-d "$projectroot/$input") ||
1408 !check_export_ok("$projectroot/$input") ||
1409 ($strict_export && !project_in_list($input))) {
1410 return undef;
1411 } else {
1412 return $input;
1416 sub validate_pathname {
1417 my $input = shift || return undef;
1419 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
1420 # at the beginning, at the end, and between slashes.
1421 # also this catches doubled slashes
1422 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
1423 return undef;
1425 # no null characters
1426 if ($input =~ m!\0!) {
1427 return undef;
1429 return $input;
1432 sub validate_refname {
1433 my $input = shift || return undef;
1435 # textual hashes are O.K.
1436 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
1437 return $input;
1439 # it must be correct pathname
1440 $input = validate_pathname($input)
1441 or return undef;
1442 # restrictions on ref name according to git-check-ref-format
1443 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
1444 return undef;
1446 return $input;
1449 # decode sequences of octets in utf8 into Perl's internal form,
1450 # which is utf-8 with utf8 flag set if needed. gitweb writes out
1451 # in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
1452 sub to_utf8 {
1453 my $str = shift;
1454 return undef unless defined $str;
1456 if (utf8::is_utf8($str) || utf8::decode($str)) {
1457 return $str;
1458 } else {
1459 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
1463 # quote unsafe chars, but keep the slash, even when it's not
1464 # correct, but quoted slashes look too horrible in bookmarks
1465 sub esc_param {
1466 my $str = shift;
1467 return undef unless defined $str;
1468 $str =~ s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;
1469 $str =~ s/ /\+/g;
1470 return $str;
1473 # the quoting rules for path_info fragment are slightly different
1474 sub esc_path_info {
1475 my $str = shift;
1476 return undef unless defined $str;
1478 # path_info doesn't treat '+' as space (specially), but '?' must be escaped
1479 $str =~ s/([^A-Za-z0-9\-_.~();\/;:@&= +]+)/CGI::escape($1)/eg;
1481 return $str;
1484 # quote unsafe chars in whole URL, so some characters cannot be quoted
1485 sub esc_url {
1486 my $str = shift;
1487 return undef unless defined $str;
1488 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&= ]+)/CGI::escape($1)/eg;
1489 $str =~ s/ /\+/g;
1490 return $str;
1493 # quote unsafe characters in HTML attributes
1494 sub esc_attr {
1496 # for XHTML conformance escaping '"' to '&quot;' is not enough
1497 return esc_html(@_);
1500 # replace invalid utf8 character with SUBSTITUTION sequence
1501 sub esc_html {
1502 my $str = shift;
1503 my %opts = @_;
1505 return undef unless defined $str;
1507 $str = to_utf8($str);
1508 $str = $cgi->escapeHTML($str);
1509 if ($opts{'-nbsp'}) {
1510 $str =~ s/ /&nbsp;/g;
1512 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
1513 return $str;
1516 # quote control characters and escape filename to HTML
1517 sub esc_path {
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:]])|quot_cec($1)|eg;
1529 return $str;
1532 # Sanitize for use in XHTML + application/xml+xhtm (valid XML 1.0)
1533 sub sanitize {
1534 my $str = shift;
1536 return undef unless defined $str;
1538 $str = to_utf8($str);
1539 $str =~ s|([[:cntrl:]])|($1 =~ /[\t\n\r]/ ? $1 : quot_cec($1))|eg;
1540 return $str;
1543 # Make control characters "printable", using character escape codes (CEC)
1544 sub quot_cec {
1545 my $cntrl = shift;
1546 my %opts = @_;
1547 my %es = ( # character escape codes, aka escape sequences
1548 "\t" => '\t', # tab (HT)
1549 "\n" => '\n', # line feed (LF)
1550 "\r" => '\r', # carrige return (CR)
1551 "\f" => '\f', # form feed (FF)
1552 "\b" => '\b', # backspace (BS)
1553 "\a" => '\a', # alarm (bell) (BEL)
1554 "\e" => '\e', # escape (ESC)
1555 "\013" => '\v', # vertical tab (VT)
1556 "\000" => '\0', # nul character (NUL)
1558 my $chr = ( (exists $es{$cntrl})
1559 ? $es{$cntrl}
1560 : sprintf('\%2x', ord($cntrl)) );
1561 if ($opts{-nohtml}) {
1562 return $chr;
1563 } else {
1564 return "<span class=\"cntrl\">$chr</span>";
1568 # Alternatively use unicode control pictures codepoints,
1569 # Unicode "printable representation" (PR)
1570 sub quot_upr {
1571 my $cntrl = shift;
1572 my %opts = @_;
1574 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
1575 if ($opts{-nohtml}) {
1576 return $chr;
1577 } else {
1578 return "<span class=\"cntrl\">$chr</span>";
1582 # git may return quoted and escaped filenames
1583 sub unquote {
1584 my $str = shift;
1586 sub unq {
1587 my $seq = shift;
1588 my %es = ( # character escape codes, aka escape sequences
1589 't' => "\t", # tab (HT, TAB)
1590 'n' => "\n", # newline (NL)
1591 'r' => "\r", # return (CR)
1592 'f' => "\f", # form feed (FF)
1593 'b' => "\b", # backspace (BS)
1594 'a' => "\a", # alarm (bell) (BEL)
1595 'e' => "\e", # escape (ESC)
1596 'v' => "\013", # vertical tab (VT)
1599 if ($seq =~ m/^[0-7]{1,3}$/) {
1600 # octal char sequence
1601 return chr(oct($seq));
1602 } elsif (exists $es{$seq}) {
1603 # C escape sequence, aka character escape code
1604 return $es{$seq};
1606 # quoted ordinary character
1607 return $seq;
1610 if ($str =~ m/^"(.*)"$/) {
1611 # needs unquoting
1612 $str = $1;
1613 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
1615 return $str;
1618 # escape tabs (convert tabs to spaces)
1619 sub untabify {
1620 my $line = shift;
1622 while ((my $pos = index($line, "\t")) != -1) {
1623 if (my $count = (8 - ($pos % 8))) {
1624 my $spaces = ' ' x $count;
1625 $line =~ s/\t/$spaces/;
1629 return $line;
1632 sub project_in_list {
1633 my $project = shift;
1634 my @list = git_get_projects_list();
1635 return @list && scalar(grep { $_->{'path'} eq $project } @list);
1638 ## ----------------------------------------------------------------------
1639 ## HTML aware string manipulation
1641 # Try to chop given string on a word boundary between position
1642 # $len and $len+$add_len. If there is no word boundary there,
1643 # chop at $len+$add_len. Do not chop if chopped part plus ellipsis
1644 # (marking chopped part) would be longer than given string.
1645 sub chop_str {
1646 my $str = shift;
1647 my $len = shift;
1648 my $add_len = shift || 10;
1649 my $where = shift || 'right'; # 'left' | 'center' | 'right'
1651 # Make sure perl knows it is utf8 encoded so we don't
1652 # cut in the middle of a utf8 multibyte char.
1653 $str = to_utf8($str);
1655 # allow only $len chars, but don't cut a word if it would fit in $add_len
1656 # if it doesn't fit, cut it if it's still longer than the dots we would add
1657 # remove chopped character entities entirely
1659 # when chopping in the middle, distribute $len into left and right part
1660 # return early if chopping wouldn't make string shorter
1661 if ($where eq 'center') {
1662 return $str if ($len + 5 >= length($str)); # filler is length 5
1663 $len = int($len/2);
1664 } else {
1665 return $str if ($len + 4 >= length($str)); # filler is length 4
1668 # regexps: ending and beginning with word part up to $add_len
1669 my $endre = qr/.{$len}\w{0,$add_len}/;
1670 my $begre = qr/\w{0,$add_len}.{$len}/;
1672 if ($where eq 'left') {
1673 $str =~ m/^(.*?)($begre)$/;
1674 my ($lead, $body) = ($1, $2);
1675 if (length($lead) > 4) {
1676 $lead = " ...";
1678 return "$lead$body";
1680 } elsif ($where eq 'center') {
1681 $str =~ m/^($endre)(.*)$/;
1682 my ($left, $str) = ($1, $2);
1683 $str =~ m/^(.*?)($begre)$/;
1684 my ($mid, $right) = ($1, $2);
1685 if (length($mid) > 5) {
1686 $mid = " ... ";
1688 return "$left$mid$right";
1690 } else {
1691 $str =~ m/^($endre)(.*)$/;
1692 my $body = $1;
1693 my $tail = $2;
1694 if (length($tail) > 4) {
1695 $tail = "... ";
1697 return "$body$tail";
1701 # takes the same arguments as chop_str, but also wraps a <span> around the
1702 # result with a title attribute if it does get chopped. Additionally, the
1703 # string is HTML-escaped.
1704 sub chop_and_escape_str {
1705 my ($str) = @_;
1707 my $chopped = chop_str(@_);
1708 $str = to_utf8($str);
1709 if ($chopped eq $str) {
1710 return esc_html($chopped);
1711 } else {
1712 $str =~ s/[[:cntrl:]]/?/g;
1713 return $cgi->span({-title=>$str}, esc_html($chopped));
1717 ## ----------------------------------------------------------------------
1718 ## functions returning short strings
1720 # CSS class for given age value (in seconds)
1721 sub age_class {
1722 my $age = shift;
1724 if (!defined $age) {
1725 return "noage";
1726 } elsif ($age < 60*60*2) {
1727 return "age0";
1728 } elsif ($age < 60*60*24*2) {
1729 return "age1";
1730 } else {
1731 return "age2";
1735 # convert age in seconds to "nn units ago" string
1736 sub age_string {
1737 my $age = shift;
1738 my $age_str;
1740 if ($age > 60*60*24*365*2) {
1741 $age_str = (int $age/60/60/24/365);
1742 $age_str .= " years ago";
1743 } elsif ($age > 60*60*24*(365/12)*2) {
1744 $age_str = int $age/60/60/24/(365/12);
1745 $age_str .= " months ago";
1746 } elsif ($age > 60*60*24*7*2) {
1747 $age_str = int $age/60/60/24/7;
1748 $age_str .= " weeks ago";
1749 } elsif ($age > 60*60*24*2) {
1750 $age_str = int $age/60/60/24;
1751 $age_str .= " days ago";
1752 } elsif ($age > 60*60*2) {
1753 $age_str = int $age/60/60;
1754 $age_str .= " hours ago";
1755 } elsif ($age > 60*2) {
1756 $age_str = int $age/60;
1757 $age_str .= " min ago";
1758 } elsif ($age > 2) {
1759 $age_str = int $age;
1760 $age_str .= " sec ago";
1761 } else {
1762 $age_str .= " right now";
1764 return $age_str;
1767 use constant {
1768 S_IFINVALID => 0030000,
1769 S_IFGITLINK => 0160000,
1772 # submodule/subproject, a commit object reference
1773 sub S_ISGITLINK {
1774 my $mode = shift;
1776 return (($mode & S_IFMT) == S_IFGITLINK)
1779 # convert file mode in octal to symbolic file mode string
1780 sub mode_str {
1781 my $mode = oct shift;
1783 if (S_ISGITLINK($mode)) {
1784 return 'm---------';
1785 } elsif (S_ISDIR($mode & S_IFMT)) {
1786 return 'drwxr-xr-x';
1787 } elsif (S_ISLNK($mode)) {
1788 return 'lrwxrwxrwx';
1789 } elsif (S_ISREG($mode)) {
1790 # git cares only about the executable bit
1791 if ($mode & S_IXUSR) {
1792 return '-rwxr-xr-x';
1793 } else {
1794 return '-rw-r--r--';
1796 } else {
1797 return '----------';
1801 # convert file mode in octal to file type string
1802 sub file_type {
1803 my $mode = shift;
1805 if ($mode !~ m/^[0-7]+$/) {
1806 return $mode;
1807 } else {
1808 $mode = oct $mode;
1811 if (S_ISGITLINK($mode)) {
1812 return "submodule";
1813 } elsif (S_ISDIR($mode & S_IFMT)) {
1814 return "directory";
1815 } elsif (S_ISLNK($mode)) {
1816 return "symlink";
1817 } elsif (S_ISREG($mode)) {
1818 return "file";
1819 } else {
1820 return "unknown";
1824 # convert file mode in octal to file type description string
1825 sub file_type_long {
1826 my $mode = shift;
1828 if ($mode !~ m/^[0-7]+$/) {
1829 return $mode;
1830 } else {
1831 $mode = oct $mode;
1834 if (S_ISGITLINK($mode)) {
1835 return "submodule";
1836 } elsif (S_ISDIR($mode & S_IFMT)) {
1837 return "directory";
1838 } elsif (S_ISLNK($mode)) {
1839 return "symlink";
1840 } elsif (S_ISREG($mode)) {
1841 if ($mode & S_IXUSR) {
1842 return "executable";
1843 } else {
1844 return "file";
1846 } else {
1847 return "unknown";
1852 ## ----------------------------------------------------------------------
1853 ## functions returning short HTML fragments, or transforming HTML fragments
1854 ## which don't belong to other sections
1856 # format line of commit message.
1857 sub format_log_line_html {
1858 my $line = shift;
1860 $line = esc_html($line, -nbsp=>1);
1861 $line =~ s{\b([0-9a-fA-F]{8,40})\b}{
1862 $cgi->a({-href => href(action=>"object", hash=>$1),
1863 -class => "text"}, $1);
1864 }eg;
1866 return $line;
1869 # format marker of refs pointing to given object
1871 # the destination action is chosen based on object type and current context:
1872 # - for annotated tags, we choose the tag view unless it's the current view
1873 # already, in which case we go to shortlog view
1874 # - for other refs, we keep the current view if we're in history, shortlog or
1875 # log view, and select shortlog otherwise
1876 sub format_ref_marker {
1877 my ($refs, $id) = @_;
1878 my $markers = '';
1880 if (defined $refs->{$id}) {
1881 foreach my $ref (@{$refs->{$id}}) {
1882 # this code exploits the fact that non-lightweight tags are the
1883 # only indirect objects, and that they are the only objects for which
1884 # we want to use tag instead of shortlog as action
1885 my ($type, $name) = qw();
1886 my $indirect = ($ref =~ s/\^\{\}$//);
1887 # e.g. tags/v2.6.11 or heads/next
1888 if ($ref =~ m!^(.*?)s?/(.*)$!) {
1889 $type = $1;
1890 $name = $2;
1891 } else {
1892 $type = "ref";
1893 $name = $ref;
1896 my $class = $type;
1897 $class .= " indirect" if $indirect;
1899 my $dest_action = "shortlog";
1901 if ($indirect) {
1902 $dest_action = "tag" unless $action eq "tag";
1903 } elsif ($action =~ /^(history|(short)?log)$/) {
1904 $dest_action = $action;
1907 my $dest = "";
1908 $dest .= "refs/" unless $ref =~ m!^refs/!;
1909 $dest .= $ref;
1911 my $link = $cgi->a({
1912 -href => href(
1913 action=>$dest_action,
1914 hash=>$dest
1915 )}, $name);
1917 $markers .= " <span class=\"".esc_attr($class)."\" title=\"".esc_attr($ref)."\">" .
1918 $link . "</span>";
1922 if ($markers) {
1923 return ' <span class="refs">'. $markers . '</span>';
1924 } else {
1925 return "";
1929 # format, perhaps shortened and with markers, title line
1930 sub format_subject_html {
1931 my ($long, $short, $href, $extra) = @_;
1932 $extra = '' unless defined($extra);
1934 if (length($short) < length($long)) {
1935 $long =~ s/[[:cntrl:]]/?/g;
1936 return $cgi->a({-href => $href, -class => "list subject",
1937 -title => to_utf8($long)},
1938 esc_html($short)) . $extra;
1939 } else {
1940 return $cgi->a({-href => $href, -class => "list subject"},
1941 esc_html($long)) . $extra;
1945 # Rather than recomputing the url for an email multiple times, we cache it
1946 # after the first hit. This gives a visible benefit in views where the avatar
1947 # for the same email is used repeatedly (e.g. shortlog).
1948 # The cache is shared by all avatar engines (currently gravatar only), which
1949 # are free to use it as preferred. Since only one avatar engine is used for any
1950 # given page, there's no risk for cache conflicts.
1951 our %avatar_cache = ();
1953 # Compute the picon url for a given email, by using the picon search service over at
1954 # http://www.cs.indiana.edu/picons/search.html
1955 sub picon_url {
1956 my $email = lc shift;
1957 if (!$avatar_cache{$email}) {
1958 my ($user, $domain) = split('@', $email);
1959 $avatar_cache{$email} =
1960 "http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/" .
1961 "$domain/$user/" .
1962 "users+domains+unknown/up/single";
1964 return $avatar_cache{$email};
1967 # Compute the gravatar url for a given email, if it's not in the cache already.
1968 # Gravatar stores only the part of the URL before the size, since that's the
1969 # one computationally more expensive. This also allows reuse of the cache for
1970 # different sizes (for this particular engine).
1971 sub gravatar_url {
1972 my $email = lc shift;
1973 my $size = shift;
1974 $avatar_cache{$email} ||=
1975 "http://www.gravatar.com/avatar/" .
1976 Digest::MD5::md5_hex($email) . "?s=";
1977 return $avatar_cache{$email} . $size;
1980 # Insert an avatar for the given $email at the given $size if the feature
1981 # is enabled.
1982 sub git_get_avatar {
1983 my ($email, %opts) = @_;
1984 my $pre_white = ($opts{-pad_before} ? "&nbsp;" : "");
1985 my $post_white = ($opts{-pad_after} ? "&nbsp;" : "");
1986 $opts{-size} ||= 'default';
1987 my $size = $avatar_size{$opts{-size}} || $avatar_size{'default'};
1988 my $url = "";
1989 if ($git_avatar eq 'gravatar') {
1990 $url = gravatar_url($email, $size);
1991 } elsif ($git_avatar eq 'picon') {
1992 $url = picon_url($email);
1994 # Other providers can be added by extending the if chain, defining $url
1995 # as needed. If no variant puts something in $url, we assume avatars
1996 # are completely disabled/unavailable.
1997 if ($url) {
1998 return $pre_white .
1999 "<img width=\"$size\" " .
2000 "class=\"avatar\" " .
2001 "src=\"".esc_url($url)."\" " .
2002 "alt=\"\" " .
2003 "/>" . $post_white;
2004 } else {
2005 return "";
2009 sub format_search_author {
2010 my ($author, $searchtype, $displaytext) = @_;
2011 my $have_search = gitweb_check_feature('search');
2013 if ($have_search) {
2014 my $performed = "";
2015 if ($searchtype eq 'author') {
2016 $performed = "authored";
2017 } elsif ($searchtype eq 'committer') {
2018 $performed = "committed";
2021 return $cgi->a({-href => href(action=>"search", hash=>$hash,
2022 searchtext=>$author,
2023 searchtype=>$searchtype), class=>"list",
2024 title=>"Search for commits $performed by $author"},
2025 $displaytext);
2027 } else {
2028 return $displaytext;
2032 # format the author name of the given commit with the given tag
2033 # the author name is chopped and escaped according to the other
2034 # optional parameters (see chop_str).
2035 sub format_author_html {
2036 my $tag = shift;
2037 my $co = shift;
2038 my $author = chop_and_escape_str($co->{'author_name'}, @_);
2039 return "<$tag class=\"author\">" .
2040 format_search_author($co->{'author_name'}, "author",
2041 git_get_avatar($co->{'author_email'}, -pad_after => 1) .
2042 $author) .
2043 "</$tag>";
2046 # format git diff header line, i.e. "diff --(git|combined|cc) ..."
2047 sub format_git_diff_header_line {
2048 my $line = shift;
2049 my $diffinfo = shift;
2050 my ($from, $to) = @_;
2052 if ($diffinfo->{'nparents'}) {
2053 # combined diff
2054 $line =~ s!^(diff (.*?) )"?.*$!$1!;
2055 if ($to->{'href'}) {
2056 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
2057 esc_path($to->{'file'}));
2058 } else { # file was deleted (no href)
2059 $line .= esc_path($to->{'file'});
2061 } else {
2062 # "ordinary" diff
2063 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
2064 if ($from->{'href'}) {
2065 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
2066 'a/' . esc_path($from->{'file'}));
2067 } else { # file was added (no href)
2068 $line .= 'a/' . esc_path($from->{'file'});
2070 $line .= ' ';
2071 if ($to->{'href'}) {
2072 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
2073 'b/' . esc_path($to->{'file'}));
2074 } else { # file was deleted
2075 $line .= 'b/' . esc_path($to->{'file'});
2079 return "<div class=\"diff header\">$line</div>\n";
2082 # format extended diff header line, before patch itself
2083 sub format_extended_diff_header_line {
2084 my $line = shift;
2085 my $diffinfo = shift;
2086 my ($from, $to) = @_;
2088 # match <path>
2089 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
2090 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
2091 esc_path($from->{'file'}));
2093 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
2094 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
2095 esc_path($to->{'file'}));
2097 # match single <mode>
2098 if ($line =~ m/\s(\d{6})$/) {
2099 $line .= '<span class="info"> (' .
2100 file_type_long($1) .
2101 ')</span>';
2103 # match <hash>
2104 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
2105 # can match only for combined diff
2106 $line = 'index ';
2107 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2108 if ($from->{'href'}[$i]) {
2109 $line .= $cgi->a({-href=>$from->{'href'}[$i],
2110 -class=>"hash"},
2111 substr($diffinfo->{'from_id'}[$i],0,7));
2112 } else {
2113 $line .= '0' x 7;
2115 # separator
2116 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
2118 $line .= '..';
2119 if ($to->{'href'}) {
2120 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
2121 substr($diffinfo->{'to_id'},0,7));
2122 } else {
2123 $line .= '0' x 7;
2126 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
2127 # can match only for ordinary diff
2128 my ($from_link, $to_link);
2129 if ($from->{'href'}) {
2130 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
2131 substr($diffinfo->{'from_id'},0,7));
2132 } else {
2133 $from_link = '0' x 7;
2135 if ($to->{'href'}) {
2136 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
2137 substr($diffinfo->{'to_id'},0,7));
2138 } else {
2139 $to_link = '0' x 7;
2141 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
2142 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
2145 return $line . "<br/>\n";
2148 # format from-file/to-file diff header
2149 sub format_diff_from_to_header {
2150 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
2151 my $line;
2152 my $result = '';
2154 $line = $from_line;
2155 #assert($line =~ m/^---/) if DEBUG;
2156 # no extra formatting for "^--- /dev/null"
2157 if (! $diffinfo->{'nparents'}) {
2158 # ordinary (single parent) diff
2159 if ($line =~ m!^--- "?a/!) {
2160 if ($from->{'href'}) {
2161 $line = '--- a/' .
2162 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
2163 esc_path($from->{'file'}));
2164 } else {
2165 $line = '--- a/' .
2166 esc_path($from->{'file'});
2169 $result .= qq!<div class="diff from_file">$line</div>\n!;
2171 } else {
2172 # combined diff (merge commit)
2173 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2174 if ($from->{'href'}[$i]) {
2175 $line = '--- ' .
2176 $cgi->a({-href=>href(action=>"blobdiff",
2177 hash_parent=>$diffinfo->{'from_id'}[$i],
2178 hash_parent_base=>$parents[$i],
2179 file_parent=>$from->{'file'}[$i],
2180 hash=>$diffinfo->{'to_id'},
2181 hash_base=>$hash,
2182 file_name=>$to->{'file'}),
2183 -class=>"path",
2184 -title=>"diff" . ($i+1)},
2185 $i+1) .
2186 '/' .
2187 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
2188 esc_path($from->{'file'}[$i]));
2189 } else {
2190 $line = '--- /dev/null';
2192 $result .= qq!<div class="diff from_file">$line</div>\n!;
2196 $line = $to_line;
2197 #assert($line =~ m/^\+\+\+/) if DEBUG;
2198 # no extra formatting for "^+++ /dev/null"
2199 if ($line =~ m!^\+\+\+ "?b/!) {
2200 if ($to->{'href'}) {
2201 $line = '+++ b/' .
2202 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
2203 esc_path($to->{'file'}));
2204 } else {
2205 $line = '+++ b/' .
2206 esc_path($to->{'file'});
2209 $result .= qq!<div class="diff to_file">$line</div>\n!;
2211 return $result;
2214 # create note for patch simplified by combined diff
2215 sub format_diff_cc_simplified {
2216 my ($diffinfo, @parents) = @_;
2217 my $result = '';
2219 $result .= "<div class=\"diff header\">" .
2220 "diff --cc ";
2221 if (!is_deleted($diffinfo)) {
2222 $result .= $cgi->a({-href => href(action=>"blob",
2223 hash_base=>$hash,
2224 hash=>$diffinfo->{'to_id'},
2225 file_name=>$diffinfo->{'to_file'}),
2226 -class => "path"},
2227 esc_path($diffinfo->{'to_file'}));
2228 } else {
2229 $result .= esc_path($diffinfo->{'to_file'});
2231 $result .= "</div>\n" . # class="diff header"
2232 "<div class=\"diff nodifferences\">" .
2233 "Simple merge" .
2234 "</div>\n"; # class="diff nodifferences"
2236 return $result;
2239 sub diff_line_class {
2240 my ($line, $from, $to) = @_;
2242 # ordinary diff
2243 my $num_sign = 1;
2244 # combined diff
2245 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
2246 $num_sign = scalar @{$from->{'href'}};
2249 my @diff_line_classifier = (
2250 { regexp => qr/^\@\@{$num_sign} /, class => "chunk_header"},
2251 { regexp => qr/^\\/, class => "incomplete" },
2252 { regexp => qr/^ {$num_sign}/, class => "ctx" },
2253 # classifier for context must come before classifier add/rem,
2254 # or we would have to use more complicated regexp, for example
2255 # qr/(?= {0,$m}\+)[+ ]{$num_sign}/, where $m = $num_sign - 1;
2256 { regexp => qr/^[+ ]{$num_sign}/, class => "add" },
2257 { regexp => qr/^[- ]{$num_sign}/, class => "rem" },
2259 for my $clsfy (@diff_line_classifier) {
2260 return $clsfy->{'class'}
2261 if ($line =~ $clsfy->{'regexp'});
2264 # fallback
2265 return "";
2268 # assumes that $from and $to are defined and correctly filled,
2269 # and that $line holds a line of chunk header for unified diff
2270 sub format_unidiff_chunk_header {
2271 my ($line, $from, $to) = @_;
2273 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
2274 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
2276 $from_lines = 0 unless defined $from_lines;
2277 $to_lines = 0 unless defined $to_lines;
2279 if ($from->{'href'}) {
2280 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
2281 -class=>"list"}, $from_text);
2283 if ($to->{'href'}) {
2284 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
2285 -class=>"list"}, $to_text);
2287 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
2288 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
2289 return $line;
2292 # assumes that $from and $to are defined and correctly filled,
2293 # and that $line holds a line of chunk header for combined diff
2294 sub format_cc_diff_chunk_header {
2295 my ($line, $from, $to) = @_;
2297 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
2298 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
2300 @from_text = split(' ', $ranges);
2301 for (my $i = 0; $i < @from_text; ++$i) {
2302 ($from_start[$i], $from_nlines[$i]) =
2303 (split(',', substr($from_text[$i], 1)), 0);
2306 $to_text = pop @from_text;
2307 $to_start = pop @from_start;
2308 $to_nlines = pop @from_nlines;
2310 $line = "<span class=\"chunk_info\">$prefix ";
2311 for (my $i = 0; $i < @from_text; ++$i) {
2312 if ($from->{'href'}[$i]) {
2313 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
2314 -class=>"list"}, $from_text[$i]);
2315 } else {
2316 $line .= $from_text[$i];
2318 $line .= " ";
2320 if ($to->{'href'}) {
2321 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
2322 -class=>"list"}, $to_text);
2323 } else {
2324 $line .= $to_text;
2326 $line .= " $prefix</span>" .
2327 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
2328 return $line;
2331 # process patch (diff) line (not to be used for diff headers),
2332 # returning class and HTML-formatted (but not wrapped) line
2333 sub process_diff_line {
2334 my $line = shift;
2335 my ($from, $to) = @_;
2337 my $diff_class = diff_line_class($line, $from, $to);
2339 chomp $line;
2340 $line = untabify($line);
2342 if ($from && $to && $line =~ m/^\@{2} /) {
2343 $line = format_unidiff_chunk_header($line, $from, $to);
2344 return $diff_class, $line;
2346 } elsif ($from && $to && $line =~ m/^\@{3}/) {
2347 $line = format_cc_diff_chunk_header($line, $from, $to);
2348 return $diff_class, $line;
2351 return $diff_class, esc_html($line, -nbsp=>1);
2354 # Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
2355 # linked. Pass the hash of the tree/commit to snapshot.
2356 sub format_snapshot_links {
2357 my ($hash) = @_;
2358 my $num_fmts = @snapshot_fmts;
2359 if ($num_fmts > 1) {
2360 # A parenthesized list of links bearing format names.
2361 # e.g. "snapshot (_tar.gz_ _zip_)"
2362 return "snapshot (" . join(' ', map
2363 $cgi->a({
2364 -href => href(
2365 action=>"snapshot",
2366 hash=>$hash,
2367 snapshot_format=>$_
2369 }, $known_snapshot_formats{$_}{'display'})
2370 , @snapshot_fmts) . ")";
2371 } elsif ($num_fmts == 1) {
2372 # A single "snapshot" link whose tooltip bears the format name.
2373 # i.e. "_snapshot_"
2374 my ($fmt) = @snapshot_fmts;
2375 return
2376 $cgi->a({
2377 -href => href(
2378 action=>"snapshot",
2379 hash=>$hash,
2380 snapshot_format=>$fmt
2382 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
2383 }, "snapshot");
2384 } else { # $num_fmts == 0
2385 return undef;
2389 ## ......................................................................
2390 ## functions returning values to be passed, perhaps after some
2391 ## transformation, to other functions; e.g. returning arguments to href()
2393 # returns hash to be passed to href to generate gitweb URL
2394 # in -title key it returns description of link
2395 sub get_feed_info {
2396 my $format = shift || 'Atom';
2397 my %res = (action => lc($format));
2399 # feed links are possible only for project views
2400 return unless (defined $project);
2401 # some views should link to OPML, or to generic project feed,
2402 # or don't have specific feed yet (so they should use generic)
2403 return if ($action =~ /^(?:tags|heads|forks|tag|search)$/x);
2405 my $branch;
2406 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
2407 # from tag links; this also makes possible to detect branch links
2408 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
2409 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
2410 $branch = $1;
2412 # find log type for feed description (title)
2413 my $type = 'log';
2414 if (defined $file_name) {
2415 $type = "history of $file_name";
2416 $type .= "/" if ($action eq 'tree');
2417 $type .= " on '$branch'" if (defined $branch);
2418 } else {
2419 $type = "log of $branch" if (defined $branch);
2422 $res{-title} = $type;
2423 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
2424 $res{'file_name'} = $file_name;
2426 return %res;
2429 ## ----------------------------------------------------------------------
2430 ## git utility subroutines, invoking git commands
2432 # returns path to the core git executable and the --git-dir parameter as list
2433 sub git_cmd {
2434 $number_of_git_cmds++;
2435 return $GIT, '--git-dir='.$git_dir;
2438 # quote the given arguments for passing them to the shell
2439 # quote_command("command", "arg 1", "arg with ' and ! characters")
2440 # => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
2441 # Try to avoid using this function wherever possible.
2442 sub quote_command {
2443 return join(' ',
2444 map { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ );
2447 # get HEAD ref of given project as hash
2448 sub git_get_head_hash {
2449 return git_get_full_hash(shift, 'HEAD');
2452 sub git_get_full_hash {
2453 return git_get_hash(@_);
2456 sub git_get_short_hash {
2457 return git_get_hash(@_, '--short=7');
2460 sub git_get_hash {
2461 my ($project, $hash, @options) = @_;
2462 my $o_git_dir = $git_dir;
2463 my $retval = undef;
2464 $git_dir = "$projectroot/$project";
2465 if (open my $fd, '-|', git_cmd(), 'rev-parse',
2466 '--verify', '-q', @options, $hash) {
2467 $retval = <$fd>;
2468 chomp $retval if defined $retval;
2469 close $fd;
2471 if (defined $o_git_dir) {
2472 $git_dir = $o_git_dir;
2474 return $retval;
2477 # get type of given object
2478 sub git_get_type {
2479 my $hash = shift;
2481 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
2482 my $type = <$fd>;
2483 close $fd or return;
2484 chomp $type;
2485 return $type;
2488 # repository configuration
2489 our $config_file = '';
2490 our %config;
2492 # store multiple values for single key as anonymous array reference
2493 # single values stored directly in the hash, not as [ <value> ]
2494 sub hash_set_multi {
2495 my ($hash, $key, $value) = @_;
2497 if (!exists $hash->{$key}) {
2498 $hash->{$key} = $value;
2499 } elsif (!ref $hash->{$key}) {
2500 $hash->{$key} = [ $hash->{$key}, $value ];
2501 } else {
2502 push @{$hash->{$key}}, $value;
2506 # return hash of git project configuration
2507 # optionally limited to some section, e.g. 'gitweb'
2508 sub git_parse_project_config {
2509 my $section_regexp = shift;
2510 my %config;
2512 local $/ = "\0";
2514 open my $fh, "-|", git_cmd(), "config", '-z', '-l',
2515 or return;
2517 while (my $keyval = <$fh>) {
2518 chomp $keyval;
2519 my ($key, $value) = split(/\n/, $keyval, 2);
2521 hash_set_multi(\%config, $key, $value)
2522 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
2524 close $fh;
2526 return %config;
2529 # convert config value to boolean: 'true' or 'false'
2530 # no value, number > 0, 'true' and 'yes' values are true
2531 # rest of values are treated as false (never as error)
2532 sub config_to_bool {
2533 my $val = shift;
2535 return 1 if !defined $val; # section.key
2537 # strip leading and trailing whitespace
2538 $val =~ s/^\s+//;
2539 $val =~ s/\s+$//;
2541 return (($val =~ /^\d+$/ && $val) || # section.key = 1
2542 ($val =~ /^(?:true|yes)$/i)); # section.key = true
2545 # convert config value to simple decimal number
2546 # an optional value suffix of 'k', 'm', or 'g' will cause the value
2547 # to be multiplied by 1024, 1048576, or 1073741824
2548 sub config_to_int {
2549 my $val = shift;
2551 # strip leading and trailing whitespace
2552 $val =~ s/^\s+//;
2553 $val =~ s/\s+$//;
2555 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
2556 $unit = lc($unit);
2557 # unknown unit is treated as 1
2558 return $num * ($unit eq 'g' ? 1073741824 :
2559 $unit eq 'm' ? 1048576 :
2560 $unit eq 'k' ? 1024 : 1);
2562 return $val;
2565 # convert config value to array reference, if needed
2566 sub config_to_multi {
2567 my $val = shift;
2569 return ref($val) ? $val : (defined($val) ? [ $val ] : []);
2572 sub git_get_project_config {
2573 my ($key, $type) = @_;
2575 return unless defined $git_dir;
2577 # key sanity check
2578 return unless ($key);
2579 # only subsection, if exists, is case sensitive,
2580 # and not lowercased by 'git config -z -l'
2581 if (my ($hi, $mi, $lo) = ($key =~ /^([^.]*)\.(.*)\.([^.]*)$/)) {
2582 $key = join(".", lc($hi), $mi, lc($lo));
2583 } else {
2584 $key = lc($key);
2586 $key =~ s/^gitweb\.//;
2587 return if ($key =~ m/\W/);
2589 # type sanity check
2590 if (defined $type) {
2591 $type =~ s/^--//;
2592 $type = undef
2593 unless ($type eq 'bool' || $type eq 'int');
2596 # get config
2597 if (!defined $config_file ||
2598 $config_file ne "$git_dir/config") {
2599 %config = git_parse_project_config('gitweb');
2600 $config_file = "$git_dir/config";
2603 # check if config variable (key) exists
2604 return unless exists $config{"gitweb.$key"};
2606 # ensure given type
2607 if (!defined $type) {
2608 return $config{"gitweb.$key"};
2609 } elsif ($type eq 'bool') {
2610 # backward compatibility: 'git config --bool' returns true/false
2611 return config_to_bool($config{"gitweb.$key"}) ? 'true' : 'false';
2612 } elsif ($type eq 'int') {
2613 return config_to_int($config{"gitweb.$key"});
2615 return $config{"gitweb.$key"};
2618 # get hash of given path at given ref
2619 sub git_get_hash_by_path {
2620 my $base = shift;
2621 my $path = shift || return undef;
2622 my $type = shift;
2624 $path =~ s,/+$,,;
2626 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
2627 or die_error(500, "Open git-ls-tree failed");
2628 my $line = <$fd>;
2629 close $fd or return undef;
2631 if (!defined $line) {
2632 # there is no tree or hash given by $path at $base
2633 return undef;
2636 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2637 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
2638 if (defined $type && $type ne $2) {
2639 # type doesn't match
2640 return undef;
2642 return $3;
2645 # get path of entry with given hash at given tree-ish (ref)
2646 # used to get 'from' filename for combined diff (merge commit) for renames
2647 sub git_get_path_by_hash {
2648 my $base = shift || return;
2649 my $hash = shift || return;
2651 local $/ = "\0";
2653 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
2654 or return undef;
2655 while (my $line = <$fd>) {
2656 chomp $line;
2658 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
2659 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
2660 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
2661 close $fd;
2662 return $1;
2665 close $fd;
2666 return undef;
2669 ## ......................................................................
2670 ## git utility functions, directly accessing git repository
2672 # get the value of config variable either from file named as the variable
2673 # itself in the repository ($GIT_DIR/$name file), or from gitweb.$name
2674 # configuration variable in the repository config file.
2675 sub git_get_file_or_project_config {
2676 my ($path, $name) = @_;
2678 $git_dir = "$projectroot/$path";
2679 open my $fd, '<', "$git_dir/$name"
2680 or return git_get_project_config($name);
2681 my $conf = <$fd>;
2682 close $fd;
2683 if (defined $conf) {
2684 chomp $conf;
2686 return $conf;
2689 sub git_get_project_description {
2690 my $path = shift;
2691 return git_get_file_or_project_config($path, 'description');
2694 sub git_get_project_category {
2695 my $path = shift;
2696 return git_get_file_or_project_config($path, 'category');
2700 # supported formats:
2701 # * $GIT_DIR/ctags/<tagname> file (in 'ctags' subdirectory)
2702 # - if its contents is a number, use it as tag weight,
2703 # - otherwise add a tag with weight 1
2704 # * $GIT_DIR/ctags file, each line is a tag (with weight 1)
2705 # the same value multiple times increases tag weight
2706 # * `gitweb.ctag' multi-valued repo config variable
2707 sub git_get_project_ctags {
2708 my $project = shift;
2709 my $ctags = {};
2711 $git_dir = "$projectroot/$project";
2712 if (opendir my $dh, "$git_dir/ctags") {
2713 my @files = grep { -f $_ } map { "$git_dir/ctags/$_" } readdir($dh);
2714 foreach my $tagfile (@files) {
2715 open my $ct, '<', $tagfile
2716 or next;
2717 my $val = <$ct>;
2718 chomp $val if $val;
2719 close $ct;
2721 (my $ctag = $tagfile) =~ s#.*/##;
2722 if ($val =~ /^\d+$/) {
2723 $ctags->{$ctag} = $val;
2724 } else {
2725 $ctags->{$ctag} = 1;
2728 closedir $dh;
2730 } elsif (open my $fh, '<', "$git_dir/ctags") {
2731 while (my $line = <$fh>) {
2732 chomp $line;
2733 $ctags->{$line}++ if $line;
2735 close $fh;
2737 } else {
2738 my $taglist = config_to_multi(git_get_project_config('ctag'));
2739 foreach my $tag (@$taglist) {
2740 $ctags->{$tag}++;
2744 return $ctags;
2747 # return hash, where keys are content tags ('ctags'),
2748 # and values are sum of weights of given tag in every project
2749 sub git_gather_all_ctags {
2750 my $projects = shift;
2751 my $ctags = {};
2753 foreach my $p (@$projects) {
2754 foreach my $ct (keys %{$p->{'ctags'}}) {
2755 $ctags->{$ct} += $p->{'ctags'}->{$ct};
2759 return $ctags;
2762 sub git_populate_project_tagcloud {
2763 my $ctags = shift;
2765 # First, merge different-cased tags; tags vote on casing
2766 my %ctags_lc;
2767 foreach (keys %$ctags) {
2768 $ctags_lc{lc $_}->{count} += $ctags->{$_};
2769 if (not $ctags_lc{lc $_}->{topcount}
2770 or $ctags_lc{lc $_}->{topcount} < $ctags->{$_}) {
2771 $ctags_lc{lc $_}->{topcount} = $ctags->{$_};
2772 $ctags_lc{lc $_}->{topname} = $_;
2776 my $cloud;
2777 my $matched = $input_params{'ctag'};
2778 if (eval { require HTML::TagCloud; 1; }) {
2779 $cloud = HTML::TagCloud->new;
2780 foreach my $ctag (sort keys %ctags_lc) {
2781 # Pad the title with spaces so that the cloud looks
2782 # less crammed.
2783 my $title = esc_html($ctags_lc{$ctag}->{topname});
2784 $title =~ s/ /&nbsp;/g;
2785 $title =~ s/^/&nbsp;/g;
2786 $title =~ s/$/&nbsp;/g;
2787 if (defined $matched && $matched eq $ctag) {
2788 $title = qq(<span class="match">$title</span>);
2790 $cloud->add($title, href(project=>undef, ctag=>$ctag),
2791 $ctags_lc{$ctag}->{count});
2793 } else {
2794 $cloud = {};
2795 foreach my $ctag (keys %ctags_lc) {
2796 my $title = esc_html($ctags_lc{$ctag}->{topname}, -nbsp=>1);
2797 if (defined $matched && $matched eq $ctag) {
2798 $title = qq(<span class="match">$title</span>);
2800 $cloud->{$ctag}{count} = $ctags_lc{$ctag}->{count};
2801 $cloud->{$ctag}{ctag} =
2802 $cgi->a({-href=>href(project=>undef, ctag=>$ctag)}, $title);
2805 return $cloud;
2808 sub git_show_project_tagcloud {
2809 my ($cloud, $count) = @_;
2810 if (ref $cloud eq 'HTML::TagCloud') {
2811 return $cloud->html_and_css($count);
2812 } else {
2813 my @tags = sort { $cloud->{$a}->{'count'} <=> $cloud->{$b}->{'count'} } keys %$cloud;
2814 return
2815 '<div id="htmltagcloud"'.($project ? '' : ' align="center"').'>' .
2816 join (', ', map {
2817 $cloud->{$_}->{'ctag'}
2818 } splice(@tags, 0, $count)) .
2819 '</div>';
2823 sub git_get_project_url_list {
2824 my $path = shift;
2826 $git_dir = "$projectroot/$path";
2827 open my $fd, '<', "$git_dir/cloneurl"
2828 or return wantarray ?
2829 @{ config_to_multi(git_get_project_config('url')) } :
2830 config_to_multi(git_get_project_config('url'));
2831 my @git_project_url_list = map { chomp; $_ } <$fd>;
2832 close $fd;
2834 return wantarray ? @git_project_url_list : \@git_project_url_list;
2837 sub git_get_projects_list {
2838 my $filter = shift || '';
2839 my @list;
2841 $filter =~ s/\.git$//;
2843 if (-d $projects_list) {
2844 # search in directory
2845 my $dir = $projects_list;
2846 # remove the trailing "/"
2847 $dir =~ s!/+$!!;
2848 my $pfxlen = length("$dir");
2849 my $pfxdepth = ($dir =~ tr!/!!);
2850 # when filtering, search only given subdirectory
2851 if ($filter) {
2852 $dir .= "/$filter";
2853 $dir =~ s!/+$!!;
2856 File::Find::find({
2857 follow_fast => 1, # follow symbolic links
2858 follow_skip => 2, # ignore duplicates
2859 dangling_symlinks => 0, # ignore dangling symlinks, silently
2860 wanted => sub {
2861 # global variables
2862 our $project_maxdepth;
2863 our $projectroot;
2864 # skip project-list toplevel, if we get it.
2865 return if (m!^[/.]$!);
2866 # only directories can be git repositories
2867 return unless (-d $_);
2868 # don't traverse too deep (Find is super slow on os x)
2869 # $project_maxdepth excludes depth of $projectroot
2870 if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
2871 $File::Find::prune = 1;
2872 return;
2875 my $path = substr($File::Find::name, $pfxlen + 1);
2876 # we check related file in $projectroot
2877 if (check_export_ok("$projectroot/$path")) {
2878 push @list, { path => $path };
2879 $File::Find::prune = 1;
2882 }, "$dir");
2884 } elsif (-f $projects_list) {
2885 # read from file(url-encoded):
2886 # 'git%2Fgit.git Linus+Torvalds'
2887 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2888 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2889 open my $fd, '<', $projects_list or return;
2890 PROJECT:
2891 while (my $line = <$fd>) {
2892 chomp $line;
2893 my ($path, $owner) = split ' ', $line;
2894 $path = unescape($path);
2895 $owner = unescape($owner);
2896 if (!defined $path) {
2897 next;
2899 # if $filter is rpovided, check if $path begins with $filter
2900 if ($filter && $path !~ m!^\Q$filter\E/!) {
2901 next;
2903 if (check_export_ok("$projectroot/$path")) {
2904 my $pr = {
2905 path => $path,
2906 owner => to_utf8($owner),
2908 push @list, $pr;
2911 close $fd;
2913 return @list;
2916 # written with help of Tree::Trie module (Perl Artistic License, GPL compatibile)
2917 # as side effects it sets 'forks' field to list of forks for forked projects
2918 sub filter_forks_from_projects_list {
2919 my $projects = shift;
2921 my %trie; # prefix tree of directories (path components)
2922 # generate trie out of those directories that might contain forks
2923 foreach my $pr (@$projects) {
2924 my $path = $pr->{'path'};
2925 $path =~ s/\.git$//; # forks of 'repo.git' are in 'repo/' directory
2926 next if ($path =~ m!/$!); # skip non-bare repositories, e.g. 'repo/.git'
2927 next unless ($path); # skip '.git' repository: tests, git-instaweb
2928 next unless (-d "$projectroot/$path"); # containing directory exists
2929 $pr->{'forks'} = []; # there can be 0 or more forks of project
2931 # add to trie
2932 my @dirs = split('/', $path);
2933 # walk the trie, until either runs out of components or out of trie
2934 my $ref = \%trie;
2935 while (scalar @dirs &&
2936 exists($ref->{$dirs[0]})) {
2937 $ref = $ref->{shift @dirs};
2939 # create rest of trie structure from rest of components
2940 foreach my $dir (@dirs) {
2941 $ref = $ref->{$dir} = {};
2943 # create end marker, store $pr as a data
2944 $ref->{''} = $pr if (!exists $ref->{''});
2947 # filter out forks, by finding shortest prefix match for paths
2948 my @filtered;
2949 PROJECT:
2950 foreach my $pr (@$projects) {
2951 # trie lookup
2952 my $ref = \%trie;
2953 DIR:
2954 foreach my $dir (split('/', $pr->{'path'})) {
2955 if (exists $ref->{''}) {
2956 # found [shortest] prefix, is a fork - skip it
2957 push @{$ref->{''}{'forks'}}, $pr;
2958 next PROJECT;
2960 if (!exists $ref->{$dir}) {
2961 # not in trie, cannot have prefix, not a fork
2962 push @filtered, $pr;
2963 next PROJECT;
2965 # If the dir is there, we just walk one step down the trie.
2966 $ref = $ref->{$dir};
2968 # we ran out of trie
2969 # (shouldn't happen: it's either no match, or end marker)
2970 push @filtered, $pr;
2973 return @filtered;
2976 # note: fill_project_list_info must be run first,
2977 # for 'descr_long' and 'ctags' to be filled
2978 sub search_projects_list {
2979 my ($projlist, %opts) = @_;
2980 my $tagfilter = $opts{'tagfilter'};
2981 my $searchtext = $opts{'searchtext'};
2983 return @$projlist
2984 unless ($tagfilter || $searchtext);
2986 my @projects;
2987 PROJECT:
2988 foreach my $pr (@$projlist) {
2990 if ($tagfilter) {
2991 next unless ref($pr->{'ctags'}) eq 'HASH';
2992 next unless
2993 grep { lc($_) eq lc($tagfilter) } keys %{$pr->{'ctags'}};
2996 if ($searchtext) {
2997 next unless
2998 $pr->{'path'} =~ /$searchtext/ ||
2999 $pr->{'descr_long'} =~ /$searchtext/;
3002 push @projects, $pr;
3005 return @projects;
3008 our $gitweb_project_owner = undef;
3009 sub git_get_project_list_from_file {
3011 return if (defined $gitweb_project_owner);
3013 $gitweb_project_owner = {};
3014 # read from file (url-encoded):
3015 # 'git%2Fgit.git Linus+Torvalds'
3016 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
3017 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
3018 if (-f $projects_list) {
3019 open(my $fd, '<', $projects_list);
3020 while (my $line = <$fd>) {
3021 chomp $line;
3022 my ($pr, $ow) = split ' ', $line;
3023 $pr = unescape($pr);
3024 $ow = unescape($ow);
3025 $gitweb_project_owner->{$pr} = to_utf8($ow);
3027 close $fd;
3031 sub git_get_project_owner {
3032 my $project = shift;
3033 my $owner;
3035 return undef unless $project;
3036 $git_dir = "$projectroot/$project";
3038 if (!defined $gitweb_project_owner) {
3039 git_get_project_list_from_file();
3042 if (exists $gitweb_project_owner->{$project}) {
3043 $owner = $gitweb_project_owner->{$project};
3045 if (!defined $owner){
3046 $owner = git_get_project_config('owner');
3048 if (!defined $owner) {
3049 $owner = get_file_owner("$git_dir");
3052 return $owner;
3055 sub git_get_last_activity {
3056 my ($path) = @_;
3057 my $fd;
3059 $git_dir = "$projectroot/$path";
3060 open($fd, "-|", git_cmd(), 'for-each-ref',
3061 '--format=%(committer)',
3062 '--sort=-committerdate',
3063 '--count=1',
3064 'refs/heads') or return;
3065 my $most_recent = <$fd>;
3066 close $fd or return;
3067 if (defined $most_recent &&
3068 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
3069 my $timestamp = $1;
3070 my $age = time - $timestamp;
3071 return ($age, age_string($age));
3073 return (undef, undef);
3076 # Implementation note: when a single remote is wanted, we cannot use 'git
3077 # remote show -n' because that command always work (assuming it's a remote URL
3078 # if it's not defined), and we cannot use 'git remote show' because that would
3079 # try to make a network roundtrip. So the only way to find if that particular
3080 # remote is defined is to walk the list provided by 'git remote -v' and stop if
3081 # and when we find what we want.
3082 sub git_get_remotes_list {
3083 my $wanted = shift;
3084 my %remotes = ();
3086 open my $fd, '-|' , git_cmd(), 'remote', '-v';
3087 return unless $fd;
3088 while (my $remote = <$fd>) {
3089 chomp $remote;
3090 $remote =~ s!\t(.*?)\s+\((\w+)\)$!!;
3091 next if $wanted and not $remote eq $wanted;
3092 my ($url, $key) = ($1, $2);
3094 $remotes{$remote} ||= { 'heads' => () };
3095 $remotes{$remote}{$key} = $url;
3097 close $fd or return;
3098 return wantarray ? %remotes : \%remotes;
3101 # Takes a hash of remotes as first parameter and fills it by adding the
3102 # available remote heads for each of the indicated remotes.
3103 sub fill_remote_heads {
3104 my $remotes = shift;
3105 my @heads = map { "remotes/$_" } keys %$remotes;
3106 my @remoteheads = git_get_heads_list(undef, @heads);
3107 foreach my $remote (keys %$remotes) {
3108 $remotes->{$remote}{'heads'} = [ grep {
3109 $_->{'name'} =~ s!^$remote/!!
3110 } @remoteheads ];
3114 sub git_get_references {
3115 my $type = shift || "";
3116 my %refs;
3117 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
3118 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
3119 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
3120 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
3121 or return;
3123 while (my $line = <$fd>) {
3124 chomp $line;
3125 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {
3126 if (defined $refs{$1}) {
3127 push @{$refs{$1}}, $2;
3128 } else {
3129 $refs{$1} = [ $2 ];
3133 close $fd or return;
3134 return \%refs;
3137 sub git_get_rev_name_tags {
3138 my $hash = shift || return undef;
3140 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
3141 or return;
3142 my $name_rev = <$fd>;
3143 close $fd;
3145 if ($name_rev =~ m|^$hash tags/(.*)$|) {
3146 return $1;
3147 } else {
3148 # catches also '$hash undefined' output
3149 return undef;
3153 ## ----------------------------------------------------------------------
3154 ## parse to hash functions
3156 sub parse_date {
3157 my $epoch = shift;
3158 my $tz = shift || "-0000";
3160 my %date;
3161 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
3162 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
3163 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
3164 $date{'hour'} = $hour;
3165 $date{'minute'} = $min;
3166 $date{'mday'} = $mday;
3167 $date{'day'} = $days[$wday];
3168 $date{'month'} = $months[$mon];
3169 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
3170 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
3171 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
3172 $mday, $months[$mon], $hour ,$min;
3173 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
3174 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
3176 my ($tz_sign, $tz_hour, $tz_min) =
3177 ($tz =~ m/^([-+])(\d\d)(\d\d)$/);
3178 $tz_sign = ($tz_sign eq '-' ? -1 : +1);
3179 my $local = $epoch + $tz_sign*((($tz_hour*60) + $tz_min)*60);
3180 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
3181 $date{'hour_local'} = $hour;
3182 $date{'minute_local'} = $min;
3183 $date{'tz_local'} = $tz;
3184 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
3185 1900+$year, $mon+1, $mday,
3186 $hour, $min, $sec, $tz);
3187 return %date;
3190 sub parse_tag {
3191 my $tag_id = shift;
3192 my %tag;
3193 my @comment;
3195 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
3196 $tag{'id'} = $tag_id;
3197 while (my $line = <$fd>) {
3198 chomp $line;
3199 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
3200 $tag{'object'} = $1;
3201 } elsif ($line =~ m/^type (.+)$/) {
3202 $tag{'type'} = $1;
3203 } elsif ($line =~ m/^tag (.+)$/) {
3204 $tag{'name'} = $1;
3205 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
3206 $tag{'author'} = $1;
3207 $tag{'author_epoch'} = $2;
3208 $tag{'author_tz'} = $3;
3209 if ($tag{'author'} =~ m/^([^<]+) <([^>]*)>/) {
3210 $tag{'author_name'} = $1;
3211 $tag{'author_email'} = $2;
3212 } else {
3213 $tag{'author_name'} = $tag{'author'};
3215 } elsif ($line =~ m/--BEGIN/) {
3216 push @comment, $line;
3217 last;
3218 } elsif ($line eq "") {
3219 last;
3222 push @comment, <$fd>;
3223 $tag{'comment'} = \@comment;
3224 close $fd or return;
3225 if (!defined $tag{'name'}) {
3226 return
3228 return %tag
3231 sub parse_commit_text {
3232 my ($commit_text, $withparents) = @_;
3233 my @commit_lines = split '\n', $commit_text;
3234 my %co;
3236 pop @commit_lines; # Remove '\0'
3238 if (! @commit_lines) {
3239 return;
3242 my $header = shift @commit_lines;
3243 if ($header !~ m/^[0-9a-fA-F]{40}/) {
3244 return;
3246 ($co{'id'}, my @parents) = split ' ', $header;
3247 while (my $line = shift @commit_lines) {
3248 last if $line eq "\n";
3249 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
3250 $co{'tree'} = $1;
3251 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
3252 push @parents, $1;
3253 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
3254 $co{'author'} = to_utf8($1);
3255 $co{'author_epoch'} = $2;
3256 $co{'author_tz'} = $3;
3257 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
3258 $co{'author_name'} = $1;
3259 $co{'author_email'} = $2;
3260 } else {
3261 $co{'author_name'} = $co{'author'};
3263 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
3264 $co{'committer'} = to_utf8($1);
3265 $co{'committer_epoch'} = $2;
3266 $co{'committer_tz'} = $3;
3267 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
3268 $co{'committer_name'} = $1;
3269 $co{'committer_email'} = $2;
3270 } else {
3271 $co{'committer_name'} = $co{'committer'};
3275 if (!defined $co{'tree'}) {
3276 return;
3278 $co{'parents'} = \@parents;
3279 $co{'parent'} = $parents[0];
3281 foreach my $title (@commit_lines) {
3282 $title =~ s/^ //;
3283 if ($title ne "") {
3284 $co{'title'} = chop_str($title, 80, 5);
3285 # remove leading stuff of merges to make the interesting part visible
3286 if (length($title) > 50) {
3287 $title =~ s/^Automatic //;
3288 $title =~ s/^merge (of|with) /Merge ... /i;
3289 if (length($title) > 50) {
3290 $title =~ s/(http|rsync):\/\///;
3292 if (length($title) > 50) {
3293 $title =~ s/(master|www|rsync)\.//;
3295 if (length($title) > 50) {
3296 $title =~ s/kernel.org:?//;
3298 if (length($title) > 50) {
3299 $title =~ s/\/pub\/scm//;
3302 $co{'title_short'} = chop_str($title, 50, 5);
3303 last;
3306 if (! defined $co{'title'} || $co{'title'} eq "") {
3307 $co{'title'} = $co{'title_short'} = '(no commit message)';
3309 # remove added spaces
3310 foreach my $line (@commit_lines) {
3311 $line =~ s/^ //;
3313 $co{'comment'} = \@commit_lines;
3315 my $age = time - $co{'committer_epoch'};
3316 $co{'age'} = $age;
3317 $co{'age_string'} = age_string($age);
3318 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
3319 if ($age > 60*60*24*7*2) {
3320 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
3321 $co{'age_string_age'} = $co{'age_string'};
3322 } else {
3323 $co{'age_string_date'} = $co{'age_string'};
3324 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
3326 return %co;
3329 sub parse_commit {
3330 my ($commit_id) = @_;
3331 my %co;
3333 local $/ = "\0";
3335 open my $fd, "-|", git_cmd(), "rev-list",
3336 "--parents",
3337 "--header",
3338 "--max-count=1",
3339 $commit_id,
3340 "--",
3341 or die_error(500, "Open git-rev-list failed");
3342 %co = parse_commit_text(<$fd>, 1);
3343 close $fd;
3345 return %co;
3348 sub parse_commits {
3349 my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
3350 my @cos;
3352 $maxcount ||= 1;
3353 $skip ||= 0;
3355 local $/ = "\0";
3357 open my $fd, "-|", git_cmd(), "rev-list",
3358 "--header",
3359 @args,
3360 ("--max-count=" . $maxcount),
3361 ("--skip=" . $skip),
3362 @extra_options,
3363 $commit_id,
3364 "--",
3365 ($filename ? ($filename) : ())
3366 or die_error(500, "Open git-rev-list failed");
3367 while (my $line = <$fd>) {
3368 my %co = parse_commit_text($line);
3369 push @cos, \%co;
3371 close $fd;
3373 return wantarray ? @cos : \@cos;
3376 # parse line of git-diff-tree "raw" output
3377 sub parse_difftree_raw_line {
3378 my $line = shift;
3379 my %res;
3381 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
3382 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
3383 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
3384 $res{'from_mode'} = $1;
3385 $res{'to_mode'} = $2;
3386 $res{'from_id'} = $3;
3387 $res{'to_id'} = $4;
3388 $res{'status'} = $5;
3389 $res{'similarity'} = $6;
3390 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
3391 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
3392 } else {
3393 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote($7);
3396 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
3397 # combined diff (for merge commit)
3398 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
3399 $res{'nparents'} = length($1);
3400 $res{'from_mode'} = [ split(' ', $2) ];
3401 $res{'to_mode'} = pop @{$res{'from_mode'}};
3402 $res{'from_id'} = [ split(' ', $3) ];
3403 $res{'to_id'} = pop @{$res{'from_id'}};
3404 $res{'status'} = [ split('', $4) ];
3405 $res{'to_file'} = unquote($5);
3407 # 'c512b523472485aef4fff9e57b229d9d243c967f'
3408 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
3409 $res{'commit'} = $1;
3412 return wantarray ? %res : \%res;
3415 # wrapper: return parsed line of git-diff-tree "raw" output
3416 # (the argument might be raw line, or parsed info)
3417 sub parsed_difftree_line {
3418 my $line_or_ref = shift;
3420 if (ref($line_or_ref) eq "HASH") {
3421 # pre-parsed (or generated by hand)
3422 return $line_or_ref;
3423 } else {
3424 return parse_difftree_raw_line($line_or_ref);
3428 # parse line of git-ls-tree output
3429 sub parse_ls_tree_line {
3430 my $line = shift;
3431 my %opts = @_;
3432 my %res;
3434 if ($opts{'-l'}) {
3435 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'
3436 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;
3438 $res{'mode'} = $1;
3439 $res{'type'} = $2;
3440 $res{'hash'} = $3;
3441 $res{'size'} = $4;
3442 if ($opts{'-z'}) {
3443 $res{'name'} = $5;
3444 } else {
3445 $res{'name'} = unquote($5);
3447 } else {
3448 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
3449 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
3451 $res{'mode'} = $1;
3452 $res{'type'} = $2;
3453 $res{'hash'} = $3;
3454 if ($opts{'-z'}) {
3455 $res{'name'} = $4;
3456 } else {
3457 $res{'name'} = unquote($4);
3461 return wantarray ? %res : \%res;
3464 # generates _two_ hashes, references to which are passed as 2 and 3 argument
3465 sub parse_from_to_diffinfo {
3466 my ($diffinfo, $from, $to, @parents) = @_;
3468 if ($diffinfo->{'nparents'}) {
3469 # combined diff
3470 $from->{'file'} = [];
3471 $from->{'href'} = [];
3472 fill_from_file_info($diffinfo, @parents)
3473 unless exists $diffinfo->{'from_file'};
3474 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
3475 $from->{'file'}[$i] =
3476 defined $diffinfo->{'from_file'}[$i] ?
3477 $diffinfo->{'from_file'}[$i] :
3478 $diffinfo->{'to_file'};
3479 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
3480 $from->{'href'}[$i] = href(action=>"blob",
3481 hash_base=>$parents[$i],
3482 hash=>$diffinfo->{'from_id'}[$i],
3483 file_name=>$from->{'file'}[$i]);
3484 } else {
3485 $from->{'href'}[$i] = undef;
3488 } else {
3489 # ordinary (not combined) diff
3490 $from->{'file'} = $diffinfo->{'from_file'};
3491 if ($diffinfo->{'status'} ne "A") { # not new (added) file
3492 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
3493 hash=>$diffinfo->{'from_id'},
3494 file_name=>$from->{'file'});
3495 } else {
3496 delete $from->{'href'};
3500 $to->{'file'} = $diffinfo->{'to_file'};
3501 if (!is_deleted($diffinfo)) { # file exists in result
3502 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
3503 hash=>$diffinfo->{'to_id'},
3504 file_name=>$to->{'file'});
3505 } else {
3506 delete $to->{'href'};
3510 ## ......................................................................
3511 ## parse to array of hashes functions
3513 sub git_get_heads_list {
3514 my ($limit, @classes) = @_;
3515 @classes = ('heads') unless @classes;
3516 my @patterns = map { "refs/$_" } @classes;
3517 my @headslist;
3519 open my $fd, '-|', git_cmd(), 'for-each-ref',
3520 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
3521 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
3522 @patterns
3523 or return;
3524 while (my $line = <$fd>) {
3525 my %ref_item;
3527 chomp $line;
3528 my ($refinfo, $committerinfo) = split(/\0/, $line);
3529 my ($hash, $name, $title) = split(' ', $refinfo, 3);
3530 my ($committer, $epoch, $tz) =
3531 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
3532 $ref_item{'fullname'} = $name;
3533 $name =~ s!^refs/(?:head|remote)s/!!;
3535 $ref_item{'name'} = $name;
3536 $ref_item{'id'} = $hash;
3537 $ref_item{'title'} = $title || '(no commit message)';
3538 $ref_item{'epoch'} = $epoch;
3539 if ($epoch) {
3540 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
3541 } else {
3542 $ref_item{'age'} = "unknown";
3545 push @headslist, \%ref_item;
3547 close $fd;
3549 return wantarray ? @headslist : \@headslist;
3552 sub git_get_tags_list {
3553 my $limit = shift;
3554 my @tagslist;
3556 open my $fd, '-|', git_cmd(), 'for-each-ref',
3557 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
3558 '--format=%(objectname) %(objecttype) %(refname) '.
3559 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
3560 'refs/tags'
3561 or return;
3562 while (my $line = <$fd>) {
3563 my %ref_item;
3565 chomp $line;
3566 my ($refinfo, $creatorinfo) = split(/\0/, $line);
3567 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
3568 my ($creator, $epoch, $tz) =
3569 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
3570 $ref_item{'fullname'} = $name;
3571 $name =~ s!^refs/tags/!!;
3573 $ref_item{'type'} = $type;
3574 $ref_item{'id'} = $id;
3575 $ref_item{'name'} = $name;
3576 if ($type eq "tag") {
3577 $ref_item{'subject'} = $title;
3578 $ref_item{'reftype'} = $reftype;
3579 $ref_item{'refid'} = $refid;
3580 } else {
3581 $ref_item{'reftype'} = $type;
3582 $ref_item{'refid'} = $id;
3585 if ($type eq "tag" || $type eq "commit") {
3586 $ref_item{'epoch'} = $epoch;
3587 if ($epoch) {
3588 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
3589 } else {
3590 $ref_item{'age'} = "unknown";
3594 push @tagslist, \%ref_item;
3596 close $fd;
3598 return wantarray ? @tagslist : \@tagslist;
3601 ## ----------------------------------------------------------------------
3602 ## filesystem-related functions
3604 sub get_file_owner {
3605 my $path = shift;
3607 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
3608 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
3609 if (!defined $gcos) {
3610 return undef;
3612 my $owner = $gcos;
3613 $owner =~ s/[,;].*$//;
3614 return to_utf8($owner);
3617 # assume that file exists
3618 sub insert_file {
3619 my $filename = shift;
3621 open my $fd, '<', $filename;
3622 print map { to_utf8($_) } <$fd>;
3623 close $fd;
3626 ## ......................................................................
3627 ## mimetype related functions
3629 sub mimetype_guess_file {
3630 my $filename = shift;
3631 my $mimemap = shift;
3632 -r $mimemap or return undef;
3634 my %mimemap;
3635 open(my $mh, '<', $mimemap) or return undef;
3636 while (<$mh>) {
3637 next if m/^#/; # skip comments
3638 my ($mimetype, @exts) = split(/\s+/);
3639 foreach my $ext (@exts) {
3640 $mimemap{$ext} = $mimetype;
3643 close($mh);
3645 $filename =~ /\.([^.]*)$/;
3646 return $mimemap{$1};
3649 sub mimetype_guess {
3650 my $filename = shift;
3651 my $mime;
3652 $filename =~ /\./ or return undef;
3654 if ($mimetypes_file) {
3655 my $file = $mimetypes_file;
3656 if ($file !~ m!^/!) { # if it is relative path
3657 # it is relative to project
3658 $file = "$projectroot/$project/$file";
3660 $mime = mimetype_guess_file($filename, $file);
3662 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
3663 return $mime;
3666 sub blob_mimetype {
3667 my $fd = shift;
3668 my $filename = shift;
3670 if ($filename) {
3671 my $mime = mimetype_guess($filename);
3672 $mime and return $mime;
3675 # just in case
3676 return $default_blob_plain_mimetype unless $fd;
3678 if (-T $fd) {
3679 return 'text/plain';
3680 } elsif (! $filename) {
3681 return 'application/octet-stream';
3682 } elsif ($filename =~ m/\.png$/i) {
3683 return 'image/png';
3684 } elsif ($filename =~ m/\.gif$/i) {
3685 return 'image/gif';
3686 } elsif ($filename =~ m/\.jpe?g$/i) {
3687 return 'image/jpeg';
3688 } else {
3689 return 'application/octet-stream';
3693 sub blob_contenttype {
3694 my ($fd, $file_name, $type) = @_;
3696 $type ||= blob_mimetype($fd, $file_name);
3697 if ($type eq 'text/plain' && defined $default_text_plain_charset) {
3698 $type .= "; charset=$default_text_plain_charset";
3701 return $type;
3704 # guess file syntax for syntax highlighting; return undef if no highlighting
3705 # the name of syntax can (in the future) depend on syntax highlighter used
3706 sub guess_file_syntax {
3707 my ($highlight, $mimetype, $file_name) = @_;
3708 return undef unless ($highlight && defined $file_name);
3709 my $basename = basename($file_name, '.in');
3710 return $highlight_basename{$basename}
3711 if exists $highlight_basename{$basename};
3713 $basename =~ /\.([^.]*)$/;
3714 my $ext = $1 or return undef;
3715 return $highlight_ext{$ext}
3716 if exists $highlight_ext{$ext};
3718 return undef;
3721 # run highlighter and return FD of its output,
3722 # or return original FD if no highlighting
3723 sub run_highlighter {
3724 my ($fd, $highlight, $syntax) = @_;
3725 return $fd unless ($highlight && defined $syntax);
3727 close $fd;
3728 open $fd, quote_command(git_cmd(), "cat-file", "blob", $hash)." | ".
3729 quote_command($highlight_bin).
3730 " --replace-tabs=8 --fragment --syntax $syntax |"
3731 or die_error(500, "Couldn't open file or run syntax highlighter");
3732 return $fd;
3735 ## ======================================================================
3736 ## functions printing HTML: header, footer, error page
3738 sub get_page_title {
3739 my $title = to_utf8($site_name);
3741 return $title unless (defined $project);
3742 $title .= " - " . to_utf8($project);
3744 return $title unless (defined $action);
3745 $title .= "/$action"; # $action is US-ASCII (7bit ASCII)
3747 return $title unless (defined $file_name);
3748 $title .= " - " . esc_path($file_name);
3749 if ($action eq "tree" && $file_name !~ m|/$|) {
3750 $title .= "/";
3753 return $title;
3756 sub get_content_type_html {
3757 # require explicit support from the UA if we are to send the page as
3758 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
3759 # we have to do this because MSIE sometimes globs '*/*', pretending to
3760 # support xhtml+xml but choking when it gets what it asked for.
3761 if (defined $cgi->http('HTTP_ACCEPT') &&
3762 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
3763 $cgi->Accept('application/xhtml+xml') != 0) {
3764 return 'application/xhtml+xml';
3765 } else {
3766 return 'text/html';
3770 sub print_feed_meta {
3771 if (defined $project) {
3772 my %href_params = get_feed_info();
3773 if (!exists $href_params{'-title'}) {
3774 $href_params{'-title'} = 'log';
3777 foreach my $format (qw(RSS Atom)) {
3778 my $type = lc($format);
3779 my %link_attr = (
3780 '-rel' => 'alternate',
3781 '-title' => esc_attr("$project - $href_params{'-title'} - $format feed"),
3782 '-type' => "application/$type+xml"
3785 $href_params{'action'} = $type;
3786 $link_attr{'-href'} = href(%href_params);
3787 print "<link ".
3788 "rel=\"$link_attr{'-rel'}\" ".
3789 "title=\"$link_attr{'-title'}\" ".
3790 "href=\"$link_attr{'-href'}\" ".
3791 "type=\"$link_attr{'-type'}\" ".
3792 "/>\n";
3794 $href_params{'extra_options'} = '--no-merges';
3795 $link_attr{'-href'} = href(%href_params);
3796 $link_attr{'-title'} .= ' (no merges)';
3797 print "<link ".
3798 "rel=\"$link_attr{'-rel'}\" ".
3799 "title=\"$link_attr{'-title'}\" ".
3800 "href=\"$link_attr{'-href'}\" ".
3801 "type=\"$link_attr{'-type'}\" ".
3802 "/>\n";
3805 } else {
3806 printf('<link rel="alternate" title="%s projects list" '.
3807 'href="%s" type="text/plain; charset=utf-8" />'."\n",
3808 esc_attr($site_name), href(project=>undef, action=>"project_index"));
3809 printf('<link rel="alternate" title="%s projects feeds" '.
3810 'href="%s" type="text/x-opml" />'."\n",
3811 esc_attr($site_name), href(project=>undef, action=>"opml"));
3815 sub print_header_links {
3816 my $status = shift;
3818 # print out each stylesheet that exist, providing backwards capability
3819 # for those people who defined $stylesheet in a config file
3820 if (defined $stylesheet) {
3821 print '<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";
3822 } else {
3823 foreach my $stylesheet (@stylesheets) {
3824 next unless $stylesheet;
3825 print '<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";
3828 print_feed_meta()
3829 if ($status eq '200 OK');
3830 if (defined $favicon) {
3831 print qq(<link rel="shortcut icon" href=").esc_url($favicon).qq(" type="image/png" />\n);
3835 sub print_nav_breadcrumbs {
3836 my %opts = @_;
3838 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
3839 if (defined $project) {
3840 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
3841 if (defined $action) {
3842 my $action_print = $action ;
3843 if (defined $opts{-action_extra}) {
3844 $action_print = $cgi->a({-href => href(action=>$action)},
3845 $action);
3847 print " / $action_print";
3849 if (defined $opts{-action_extra}) {
3850 print " / $opts{-action_extra}";
3852 print "\n";
3856 sub print_search_form {
3857 if (!defined $searchtext) {
3858 $searchtext = "";
3860 my $search_hash;
3861 if (defined $hash_base) {
3862 $search_hash = $hash_base;
3863 } elsif (defined $hash) {
3864 $search_hash = $hash;
3865 } else {
3866 $search_hash = "HEAD";
3868 my $action = $my_uri;
3869 my $use_pathinfo = gitweb_check_feature('pathinfo');
3870 if ($use_pathinfo) {
3871 $action .= "/".esc_url($project);
3873 print $cgi->startform(-method => "get", -action => $action) .
3874 "<div class=\"search\">\n" .
3875 (!$use_pathinfo &&
3876 $cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) . "\n") .
3877 $cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) . "\n" .
3878 $cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) . "\n" .
3879 $cgi->popup_menu(-name => 'st', -default => 'commit',
3880 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
3881 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
3882 " search:\n",
3883 $cgi->textfield(-name => "s", -value => $searchtext, -override => 1) . "\n" .
3884 "<span title=\"Extended regular expression\">" .
3885 $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
3886 -checked => $search_use_regexp) .
3887 "</span>" .
3888 "</div>" .
3889 $cgi->end_form() . "\n";
3892 sub git_header_html {
3893 my $status = shift || "200 OK";
3894 my $expires = shift;
3895 my %opts = @_;
3897 my $title = get_page_title();
3898 my $content_type = get_content_type_html();
3899 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
3900 -status=> $status, -expires => $expires)
3901 unless ($opts{'-no_http_header'});
3902 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
3903 print <<EOF;
3904 <?xml version="1.0" encoding="utf-8"?>
3905 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3906 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
3907 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
3908 <!-- git core binaries version $git_version -->
3909 <head>
3910 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
3911 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
3912 <meta name="robots" content="index, nofollow"/>
3913 <title>$title</title>
3915 # the stylesheet, favicon etc urls won't work correctly with path_info
3916 # unless we set the appropriate base URL
3917 if ($ENV{'PATH_INFO'}) {
3918 print "<base href=\"".esc_url($base_url)."\" />\n";
3920 print_header_links($status);
3922 if (defined $site_html_head_string) {
3923 print to_utf8($site_html_head_string);
3926 print "</head>\n" .
3927 "<body>\n";
3929 if (defined $site_header && -f $site_header) {
3930 insert_file($site_header);
3933 print "<div class=\"page_header\">\n";
3934 if (defined $logo) {
3935 print $cgi->a({-href => esc_url($logo_url),
3936 -title => $logo_label},
3937 $cgi->img({-src => esc_url($logo),
3938 -width => 72, -height => 27,
3939 -alt => "git",
3940 -class => "logo"}));
3942 print_nav_breadcrumbs(%opts);
3943 print "</div>\n";
3945 my $have_search = gitweb_check_feature('search');
3946 if (defined $project && $have_search) {
3947 print_search_form();
3951 sub git_footer_html {
3952 my $feed_class = 'rss_logo';
3954 print "<div class=\"page_footer\">\n";
3955 if (defined $project) {
3956 my $descr = git_get_project_description($project);
3957 if (defined $descr) {
3958 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
3961 my %href_params = get_feed_info();
3962 if (!%href_params) {
3963 $feed_class .= ' generic';
3965 $href_params{'-title'} ||= 'log';
3967 foreach my $format (qw(RSS Atom)) {
3968 $href_params{'action'} = lc($format);
3969 print $cgi->a({-href => href(%href_params),
3970 -title => "$href_params{'-title'} $format feed",
3971 -class => $feed_class}, $format)."\n";
3974 } else {
3975 print $cgi->a({-href => href(project=>undef, action=>"opml"),
3976 -class => $feed_class}, "OPML") . " ";
3977 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
3978 -class => $feed_class}, "TXT") . "\n";
3980 print "</div>\n"; # class="page_footer"
3982 if (defined $t0 && gitweb_check_feature('timed')) {
3983 print "<div id=\"generating_info\">\n";
3984 print 'This page took '.
3985 '<span id="generating_time" class="time_span">'.
3986 tv_interval($t0, [ gettimeofday() ]).
3987 ' seconds </span>'.
3988 ' and '.
3989 '<span id="generating_cmd">'.
3990 $number_of_git_cmds.
3991 '</span> git commands '.
3992 " to generate.\n";
3993 print "</div>\n"; # class="page_footer"
3996 if (defined $site_footer && -f $site_footer) {
3997 insert_file($site_footer);
4000 print qq!<script type="text/javascript" src="!.esc_url($javascript).qq!"></script>\n!;
4001 if (defined $action &&
4002 $action eq 'blame_incremental') {
4003 print qq!<script type="text/javascript">\n!.
4004 qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.
4005 qq! "!. href() .qq!");\n!.
4006 qq!</script>\n!;
4007 } else {
4008 my ($jstimezone, $tz_cookie, $datetime_class) =
4009 gitweb_get_feature('javascript-timezone');
4011 print qq!<script type="text/javascript">\n!.
4012 qq!window.onload = function () {\n!;
4013 if (gitweb_check_feature('javascript-actions')) {
4014 print qq! fixLinks();\n!;
4016 if ($jstimezone && $tz_cookie && $datetime_class) {
4017 print qq! var tz_cookie = { name: '$tz_cookie', expires: 14, path: '/' };\n!. # in days
4018 qq! onloadTZSetup('$jstimezone', tz_cookie, '$datetime_class');\n!;
4020 print qq!};\n!.
4021 qq!</script>\n!;
4024 print "</body>\n" .
4025 "</html>";
4028 # die_error(<http_status_code>, <error_message>[, <detailed_html_description>])
4029 # Example: die_error(404, 'Hash not found')
4030 # By convention, use the following status codes (as defined in RFC 2616):
4031 # 400: Invalid or missing CGI parameters, or
4032 # requested object exists but has wrong type.
4033 # 403: Requested feature (like "pickaxe" or "snapshot") not enabled on
4034 # this server or project.
4035 # 404: Requested object/revision/project doesn't exist.
4036 # 500: The server isn't configured properly, or
4037 # an internal error occurred (e.g. failed assertions caused by bugs), or
4038 # an unknown error occurred (e.g. the git binary died unexpectedly).
4039 # 503: The server is currently unavailable (because it is overloaded,
4040 # or down for maintenance). Generally, this is a temporary state.
4041 sub die_error {
4042 my $status = shift || 500;
4043 my $error = esc_html(shift) || "Internal Server Error";
4044 my $extra = shift;
4045 my %opts = @_;
4047 my %http_responses = (
4048 400 => '400 Bad Request',
4049 403 => '403 Forbidden',
4050 404 => '404 Not Found',
4051 500 => '500 Internal Server Error',
4052 503 => '503 Service Unavailable',
4054 git_header_html($http_responses{$status}, undef, %opts);
4055 print <<EOF;
4056 <div class="page_body">
4057 <br /><br />
4058 $status - $error
4059 <br />
4061 if (defined $extra) {
4062 print "<hr />\n" .
4063 "$extra\n";
4065 print "</div>\n";
4067 git_footer_html();
4068 goto DONE_GITWEB
4069 unless ($opts{'-error_handler'});
4072 ## ----------------------------------------------------------------------
4073 ## functions printing or outputting HTML: navigation
4075 sub git_print_page_nav {
4076 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
4077 $extra = '' if !defined $extra; # pager or formats
4079 my @navs = qw(summary shortlog log commit commitdiff tree);
4080 if ($suppress) {
4081 @navs = grep { $_ ne $suppress } @navs;
4084 my %arg = map { $_ => {action=>$_} } @navs;
4085 if (defined $head) {
4086 for (qw(commit commitdiff)) {
4087 $arg{$_}{'hash'} = $head;
4089 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
4090 for (qw(shortlog log)) {
4091 $arg{$_}{'hash'} = $head;
4096 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
4097 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
4099 my @actions = gitweb_get_feature('actions');
4100 my %repl = (
4101 '%' => '%',
4102 'n' => $project, # project name
4103 'f' => $git_dir, # project path within filesystem
4104 'h' => $treehead || '', # current hash ('h' parameter)
4105 'b' => $treebase || '', # hash base ('hb' parameter)
4107 while (@actions) {
4108 my ($label, $link, $pos) = splice(@actions,0,3);
4109 # insert
4110 @navs = map { $_ eq $pos ? ($_, $label) : $_ } @navs;
4111 # munch munch
4112 $link =~ s/%([%nfhb])/$repl{$1}/g;
4113 $arg{$label}{'_href'} = $link;
4116 print "<div class=\"page_nav\">\n" .
4117 (join " | ",
4118 map { $_ eq $current ?
4119 $_ : $cgi->a({-href => ($arg{$_}{_href} ? $arg{$_}{_href} : href(%{$arg{$_}}))}, "$_")
4120 } @navs);
4121 print "<br/>\n$extra<br/>\n" .
4122 "</div>\n";
4125 # returns a submenu for the nagivation of the refs views (tags, heads,
4126 # remotes) with the current view disabled and the remotes view only
4127 # available if the feature is enabled
4128 sub format_ref_views {
4129 my ($current) = @_;
4130 my @ref_views = qw{tags heads};
4131 push @ref_views, 'remotes' if gitweb_check_feature('remote_heads');
4132 return join " | ", map {
4133 $_ eq $current ? $_ :
4134 $cgi->a({-href => href(action=>$_)}, $_)
4135 } @ref_views
4138 sub format_paging_nav {
4139 my ($action, $page, $has_next_link) = @_;
4140 my $paging_nav;
4143 if ($page > 0) {
4144 $paging_nav .=
4145 $cgi->a({-href => href(-replay=>1, page=>undef)}, "first") .
4146 " &sdot; " .
4147 $cgi->a({-href => href(-replay=>1, page=>$page-1),
4148 -accesskey => "p", -title => "Alt-p"}, "prev");
4149 } else {
4150 $paging_nav .= "first &sdot; prev";
4153 if ($has_next_link) {
4154 $paging_nav .= " &sdot; " .
4155 $cgi->a({-href => href(-replay=>1, page=>$page+1),
4156 -accesskey => "n", -title => "Alt-n"}, "next");
4157 } else {
4158 $paging_nav .= " &sdot; next";
4161 return $paging_nav;
4164 ## ......................................................................
4165 ## functions printing or outputting HTML: div
4167 sub git_print_header_div {
4168 my ($action, $title, $hash, $hash_base) = @_;
4169 my %args = ();
4171 $args{'action'} = $action;
4172 $args{'hash'} = $hash if $hash;
4173 $args{'hash_base'} = $hash_base if $hash_base;
4175 print "<div class=\"header\">\n" .
4176 $cgi->a({-href => href(%args), -class => "title"},
4177 $title ? $title : $action) .
4178 "\n</div>\n";
4181 sub format_repo_url {
4182 my ($name, $url) = @_;
4183 return "<tr class=\"metadata_url\"><td>$name</td><td>$url</td></tr>\n";
4186 # Group output by placing it in a DIV element and adding a header.
4187 # Options for start_div() can be provided by passing a hash reference as the
4188 # first parameter to the function.
4189 # Options to git_print_header_div() can be provided by passing an array
4190 # reference. This must follow the options to start_div if they are present.
4191 # The content can be a scalar, which is output as-is, a scalar reference, which
4192 # is output after html escaping, an IO handle passed either as *handle or
4193 # *handle{IO}, or a function reference. In the latter case all following
4194 # parameters will be taken as argument to the content function call.
4195 sub git_print_section {
4196 my ($div_args, $header_args, $content);
4197 my $arg = shift;
4198 if (ref($arg) eq 'HASH') {
4199 $div_args = $arg;
4200 $arg = shift;
4202 if (ref($arg) eq 'ARRAY') {
4203 $header_args = $arg;
4204 $arg = shift;
4206 $content = $arg;
4208 print $cgi->start_div($div_args);
4209 git_print_header_div(@$header_args);
4211 if (ref($content) eq 'CODE') {
4212 $content->(@_);
4213 } elsif (ref($content) eq 'SCALAR') {
4214 print esc_html($$content);
4215 } elsif (ref($content) eq 'GLOB' or ref($content) eq 'IO::Handle') {
4216 print <$content>;
4217 } elsif (!ref($content) && defined($content)) {
4218 print $content;
4221 print $cgi->end_div;
4224 sub format_timestamp_html {
4225 my $date = shift;
4226 my $strtime = $date->{'rfc2822'};
4228 my (undef, undef, $datetime_class) =
4229 gitweb_get_feature('javascript-timezone');
4230 if ($datetime_class) {
4231 $strtime = qq!<span class="$datetime_class">$strtime</span>!;
4234 my $localtime_format = '(%02d:%02d %s)';
4235 if ($date->{'hour_local'} < 6) {
4236 $localtime_format = '(<span class="atnight">%02d:%02d</span> %s)';
4238 $strtime .= ' ' .
4239 sprintf($localtime_format,
4240 $date->{'hour_local'}, $date->{'minute_local'}, $date->{'tz_local'});
4242 return $strtime;
4245 # Outputs the author name and date in long form
4246 sub git_print_authorship {
4247 my $co = shift;
4248 my %opts = @_;
4249 my $tag = $opts{-tag} || 'div';
4250 my $author = $co->{'author_name'};
4252 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
4253 print "<$tag class=\"author_date\">" .
4254 format_search_author($author, "author", esc_html($author)) .
4255 " [".format_timestamp_html(\%ad)."]".
4256 git_get_avatar($co->{'author_email'}, -pad_before => 1) .
4257 "</$tag>\n";
4260 # Outputs table rows containing the full author or committer information,
4261 # in the format expected for 'commit' view (& similar).
4262 # Parameters are a commit hash reference, followed by the list of people
4263 # to output information for. If the list is empty it defaults to both
4264 # author and committer.
4265 sub git_print_authorship_rows {
4266 my $co = shift;
4267 # too bad we can't use @people = @_ || ('author', 'committer')
4268 my @people = @_;
4269 @people = ('author', 'committer') unless @people;
4270 foreach my $who (@people) {
4271 my %wd = parse_date($co->{"${who}_epoch"}, $co->{"${who}_tz"});
4272 print "<tr><td>$who</td><td>" .
4273 format_search_author($co->{"${who}_name"}, $who,
4274 esc_html($co->{"${who}_name"})) . " " .
4275 format_search_author($co->{"${who}_email"}, $who,
4276 esc_html("<" . $co->{"${who}_email"} . ">")) .
4277 "</td><td rowspan=\"2\">" .
4278 git_get_avatar($co->{"${who}_email"}, -size => 'double') .
4279 "</td></tr>\n" .
4280 "<tr>" .
4281 "<td></td><td>" .
4282 format_timestamp_html(\%wd) .
4283 "</td>" .
4284 "</tr>\n";
4288 sub git_print_page_path {
4289 my $name = shift;
4290 my $type = shift;
4291 my $hb = shift;
4294 print "<div class=\"page_path\">";
4295 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
4296 -title => 'tree root'}, to_utf8("[$project]"));
4297 print " / ";
4298 if (defined $name) {
4299 my @dirname = split '/', $name;
4300 my $basename = pop @dirname;
4301 my $fullname = '';
4303 foreach my $dir (@dirname) {
4304 $fullname .= ($fullname ? '/' : '') . $dir;
4305 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
4306 hash_base=>$hb),
4307 -title => $fullname}, esc_path($dir));
4308 print " / ";
4310 if (defined $type && $type eq 'blob') {
4311 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
4312 hash_base=>$hb),
4313 -title => $name}, esc_path($basename));
4314 } elsif (defined $type && $type eq 'tree') {
4315 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
4316 hash_base=>$hb),
4317 -title => $name}, esc_path($basename));
4318 print " / ";
4319 } else {
4320 print esc_path($basename);
4323 print "<br/></div>\n";
4326 sub git_print_log {
4327 my $log = shift;
4328 my %opts = @_;
4330 if ($opts{'-remove_title'}) {
4331 # remove title, i.e. first line of log
4332 shift @$log;
4334 # remove leading empty lines
4335 while (defined $log->[0] && $log->[0] eq "") {
4336 shift @$log;
4339 # print log
4340 my $signoff = 0;
4341 my $empty = 0;
4342 foreach my $line (@$log) {
4343 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
4344 $signoff = 1;
4345 $empty = 0;
4346 if (! $opts{'-remove_signoff'}) {
4347 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
4348 next;
4349 } else {
4350 # remove signoff lines
4351 next;
4353 } else {
4354 $signoff = 0;
4357 # print only one empty line
4358 # do not print empty line after signoff
4359 if ($line eq "") {
4360 next if ($empty || $signoff);
4361 $empty = 1;
4362 } else {
4363 $empty = 0;
4366 print format_log_line_html($line) . "<br/>\n";
4369 if ($opts{'-final_empty_line'}) {
4370 # end with single empty line
4371 print "<br/>\n" unless $empty;
4375 # return link target (what link points to)
4376 sub git_get_link_target {
4377 my $hash = shift;
4378 my $link_target;
4380 # read link
4381 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4382 or return;
4384 local $/ = undef;
4385 $link_target = <$fd>;
4387 close $fd
4388 or return;
4390 return $link_target;
4393 # given link target, and the directory (basedir) the link is in,
4394 # return target of link relative to top directory (top tree);
4395 # return undef if it is not possible (including absolute links).
4396 sub normalize_link_target {
4397 my ($link_target, $basedir) = @_;
4399 # absolute symlinks (beginning with '/') cannot be normalized
4400 return if (substr($link_target, 0, 1) eq '/');
4402 # normalize link target to path from top (root) tree (dir)
4403 my $path;
4404 if ($basedir) {
4405 $path = $basedir . '/' . $link_target;
4406 } else {
4407 # we are in top (root) tree (dir)
4408 $path = $link_target;
4411 # remove //, /./, and /../
4412 my @path_parts;
4413 foreach my $part (split('/', $path)) {
4414 # discard '.' and ''
4415 next if (!$part || $part eq '.');
4416 # handle '..'
4417 if ($part eq '..') {
4418 if (@path_parts) {
4419 pop @path_parts;
4420 } else {
4421 # link leads outside repository (outside top dir)
4422 return;
4424 } else {
4425 push @path_parts, $part;
4428 $path = join('/', @path_parts);
4430 return $path;
4433 # print tree entry (row of git_tree), but without encompassing <tr> element
4434 sub git_print_tree_entry {
4435 my ($t, $basedir, $hash_base, $have_blame) = @_;
4437 my %base_key = ();
4438 $base_key{'hash_base'} = $hash_base if defined $hash_base;
4440 # The format of a table row is: mode list link. Where mode is
4441 # the mode of the entry, list is the name of the entry, an href,
4442 # and link is the action links of the entry.
4444 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
4445 if (exists $t->{'size'}) {
4446 print "<td class=\"size\">$t->{'size'}</td>\n";
4448 if ($t->{'type'} eq "blob") {
4449 print "<td class=\"list\">" .
4450 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
4451 file_name=>"$basedir$t->{'name'}", %base_key),
4452 -class => "list"}, esc_path($t->{'name'}));
4453 if (S_ISLNK(oct $t->{'mode'})) {
4454 my $link_target = git_get_link_target($t->{'hash'});
4455 if ($link_target) {
4456 my $norm_target = normalize_link_target($link_target, $basedir);
4457 if (defined $norm_target) {
4458 print " -> " .
4459 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
4460 file_name=>$norm_target),
4461 -title => $norm_target}, esc_path($link_target));
4462 } else {
4463 print " -> " . esc_path($link_target);
4467 print "</td>\n";
4468 print "<td class=\"link\">";
4469 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
4470 file_name=>"$basedir$t->{'name'}", %base_key)},
4471 "blob");
4472 if ($have_blame) {
4473 print " | " .
4474 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
4475 file_name=>"$basedir$t->{'name'}", %base_key)},
4476 "blame");
4478 if (defined $hash_base) {
4479 print " | " .
4480 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
4481 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
4482 "history");
4484 print " | " .
4485 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
4486 file_name=>"$basedir$t->{'name'}")},
4487 "raw");
4488 print "</td>\n";
4490 } elsif ($t->{'type'} eq "tree") {
4491 print "<td class=\"list\">";
4492 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
4493 file_name=>"$basedir$t->{'name'}",
4494 %base_key)},
4495 esc_path($t->{'name'}));
4496 print "</td>\n";
4497 print "<td class=\"link\">";
4498 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
4499 file_name=>"$basedir$t->{'name'}",
4500 %base_key)},
4501 "tree");
4502 if (defined $hash_base) {
4503 print " | " .
4504 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
4505 file_name=>"$basedir$t->{'name'}")},
4506 "history");
4508 print "</td>\n";
4509 } else {
4510 # unknown object: we can only present history for it
4511 # (this includes 'commit' object, i.e. submodule support)
4512 print "<td class=\"list\">" .
4513 esc_path($t->{'name'}) .
4514 "</td>\n";
4515 print "<td class=\"link\">";
4516 if (defined $hash_base) {
4517 print $cgi->a({-href => href(action=>"history",
4518 hash_base=>$hash_base,
4519 file_name=>"$basedir$t->{'name'}")},
4520 "history");
4522 print "</td>\n";
4526 ## ......................................................................
4527 ## functions printing large fragments of HTML
4529 # get pre-image filenames for merge (combined) diff
4530 sub fill_from_file_info {
4531 my ($diff, @parents) = @_;
4533 $diff->{'from_file'} = [ ];
4534 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
4535 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
4536 if ($diff->{'status'}[$i] eq 'R' ||
4537 $diff->{'status'}[$i] eq 'C') {
4538 $diff->{'from_file'}[$i] =
4539 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
4543 return $diff;
4546 # is current raw difftree line of file deletion
4547 sub is_deleted {
4548 my $diffinfo = shift;
4550 return $diffinfo->{'to_id'} eq ('0' x 40);
4553 # does patch correspond to [previous] difftree raw line
4554 # $diffinfo - hashref of parsed raw diff format
4555 # $patchinfo - hashref of parsed patch diff format
4556 # (the same keys as in $diffinfo)
4557 sub is_patch_split {
4558 my ($diffinfo, $patchinfo) = @_;
4560 return defined $diffinfo && defined $patchinfo
4561 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
4565 sub git_difftree_body {
4566 my ($difftree, $hash, @parents) = @_;
4567 my ($parent) = $parents[0];
4568 my $have_blame = gitweb_check_feature('blame');
4569 print "<div class=\"list_head\">\n";
4570 if ($#{$difftree} > 10) {
4571 print(($#{$difftree} + 1) . " files changed:\n");
4573 print "</div>\n";
4575 print "<table class=\"" .
4576 (@parents > 1 ? "combined " : "") .
4577 "diff_tree\">\n";
4579 # header only for combined diff in 'commitdiff' view
4580 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
4581 if ($has_header) {
4582 # table header
4583 print "<thead><tr>\n" .
4584 "<th></th><th></th>\n"; # filename, patchN link
4585 for (my $i = 0; $i < @parents; $i++) {
4586 my $par = $parents[$i];
4587 print "<th>" .
4588 $cgi->a({-href => href(action=>"commitdiff",
4589 hash=>$hash, hash_parent=>$par),
4590 -title => 'commitdiff to parent number ' .
4591 ($i+1) . ': ' . substr($par,0,7)},
4592 $i+1) .
4593 "&nbsp;</th>\n";
4595 print "</tr></thead>\n<tbody>\n";
4598 my $alternate = 1;
4599 my $patchno = 0;
4600 foreach my $line (@{$difftree}) {
4601 my $diff = parsed_difftree_line($line);
4603 if ($alternate) {
4604 print "<tr class=\"dark\">\n";
4605 } else {
4606 print "<tr class=\"light\">\n";
4608 $alternate ^= 1;
4610 if (exists $diff->{'nparents'}) { # combined diff
4612 fill_from_file_info($diff, @parents)
4613 unless exists $diff->{'from_file'};
4615 if (!is_deleted($diff)) {
4616 # file exists in the result (child) commit
4617 print "<td>" .
4618 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4619 file_name=>$diff->{'to_file'},
4620 hash_base=>$hash),
4621 -class => "list"}, esc_path($diff->{'to_file'})) .
4622 "</td>\n";
4623 } else {
4624 print "<td>" .
4625 esc_path($diff->{'to_file'}) .
4626 "</td>\n";
4629 if ($action eq 'commitdiff') {
4630 # link to patch
4631 $patchno++;
4632 print "<td class=\"link\">" .
4633 $cgi->a({-href => href(-anchor=>"patch$patchno")},
4634 "patch") .
4635 " | " .
4636 "</td>\n";
4639 my $has_history = 0;
4640 my $not_deleted = 0;
4641 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
4642 my $hash_parent = $parents[$i];
4643 my $from_hash = $diff->{'from_id'}[$i];
4644 my $from_path = $diff->{'from_file'}[$i];
4645 my $status = $diff->{'status'}[$i];
4647 $has_history ||= ($status ne 'A');
4648 $not_deleted ||= ($status ne 'D');
4650 if ($status eq 'A') {
4651 print "<td class=\"link\" align=\"right\"> | </td>\n";
4652 } elsif ($status eq 'D') {
4653 print "<td class=\"link\">" .
4654 $cgi->a({-href => href(action=>"blob",
4655 hash_base=>$hash,
4656 hash=>$from_hash,
4657 file_name=>$from_path)},
4658 "blob" . ($i+1)) .
4659 " | </td>\n";
4660 } else {
4661 if ($diff->{'to_id'} eq $from_hash) {
4662 print "<td class=\"link nochange\">";
4663 } else {
4664 print "<td class=\"link\">";
4666 print $cgi->a({-href => href(action=>"blobdiff",
4667 hash=>$diff->{'to_id'},
4668 hash_parent=>$from_hash,
4669 hash_base=>$hash,
4670 hash_parent_base=>$hash_parent,
4671 file_name=>$diff->{'to_file'},
4672 file_parent=>$from_path)},
4673 "diff" . ($i+1)) .
4674 " | </td>\n";
4678 print "<td class=\"link\">";
4679 if ($not_deleted) {
4680 print $cgi->a({-href => href(action=>"blob",
4681 hash=>$diff->{'to_id'},
4682 file_name=>$diff->{'to_file'},
4683 hash_base=>$hash)},
4684 "blob");
4685 print " | " if ($has_history);
4687 if ($has_history) {
4688 print $cgi->a({-href => href(action=>"history",
4689 file_name=>$diff->{'to_file'},
4690 hash_base=>$hash)},
4691 "history");
4693 print "</td>\n";
4695 print "</tr>\n";
4696 next; # instead of 'else' clause, to avoid extra indent
4698 # else ordinary diff
4700 my ($to_mode_oct, $to_mode_str, $to_file_type);
4701 my ($from_mode_oct, $from_mode_str, $from_file_type);
4702 if ($diff->{'to_mode'} ne ('0' x 6)) {
4703 $to_mode_oct = oct $diff->{'to_mode'};
4704 if (S_ISREG($to_mode_oct)) { # only for regular file
4705 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
4707 $to_file_type = file_type($diff->{'to_mode'});
4709 if ($diff->{'from_mode'} ne ('0' x 6)) {
4710 $from_mode_oct = oct $diff->{'from_mode'};
4711 if (S_ISREG($from_mode_oct)) { # only for regular file
4712 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
4714 $from_file_type = file_type($diff->{'from_mode'});
4717 if ($diff->{'status'} eq "A") { # created
4718 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
4719 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
4720 $mode_chng .= "]</span>";
4721 print "<td>";
4722 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4723 hash_base=>$hash, file_name=>$diff->{'file'}),
4724 -class => "list"}, esc_path($diff->{'file'}));
4725 print "</td>\n";
4726 print "<td>$mode_chng</td>\n";
4727 print "<td class=\"link\">";
4728 if ($action eq 'commitdiff') {
4729 # link to patch
4730 $patchno++;
4731 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
4732 "patch") .
4733 " | ";
4735 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4736 hash_base=>$hash, file_name=>$diff->{'file'})},
4737 "blob");
4738 print "</td>\n";
4740 } elsif ($diff->{'status'} eq "D") { # deleted
4741 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
4742 print "<td>";
4743 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
4744 hash_base=>$parent, file_name=>$diff->{'file'}),
4745 -class => "list"}, esc_path($diff->{'file'}));
4746 print "</td>\n";
4747 print "<td>$mode_chng</td>\n";
4748 print "<td class=\"link\">";
4749 if ($action eq 'commitdiff') {
4750 # link to patch
4751 $patchno++;
4752 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
4753 "patch") .
4754 " | ";
4756 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
4757 hash_base=>$parent, file_name=>$diff->{'file'})},
4758 "blob") . " | ";
4759 if ($have_blame) {
4760 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
4761 file_name=>$diff->{'file'})},
4762 "blame") . " | ";
4764 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
4765 file_name=>$diff->{'file'})},
4766 "history");
4767 print "</td>\n";
4769 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
4770 my $mode_chnge = "";
4771 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
4772 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
4773 if ($from_file_type ne $to_file_type) {
4774 $mode_chnge .= " from $from_file_type to $to_file_type";
4776 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
4777 if ($from_mode_str && $to_mode_str) {
4778 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
4779 } elsif ($to_mode_str) {
4780 $mode_chnge .= " mode: $to_mode_str";
4783 $mode_chnge .= "]</span>\n";
4785 print "<td>";
4786 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4787 hash_base=>$hash, file_name=>$diff->{'file'}),
4788 -class => "list"}, esc_path($diff->{'file'}));
4789 print "</td>\n";
4790 print "<td>$mode_chnge</td>\n";
4791 print "<td class=\"link\">";
4792 if ($action eq 'commitdiff') {
4793 # link to patch
4794 $patchno++;
4795 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
4796 "patch") .
4797 " | ";
4798 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
4799 # "commit" view and modified file (not onlu mode changed)
4800 print $cgi->a({-href => href(action=>"blobdiff",
4801 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
4802 hash_base=>$hash, hash_parent_base=>$parent,
4803 file_name=>$diff->{'file'})},
4804 "diff") .
4805 " | ";
4807 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4808 hash_base=>$hash, file_name=>$diff->{'file'})},
4809 "blob") . " | ";
4810 if ($have_blame) {
4811 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
4812 file_name=>$diff->{'file'})},
4813 "blame") . " | ";
4815 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
4816 file_name=>$diff->{'file'})},
4817 "history");
4818 print "</td>\n";
4820 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
4821 my %status_name = ('R' => 'moved', 'C' => 'copied');
4822 my $nstatus = $status_name{$diff->{'status'}};
4823 my $mode_chng = "";
4824 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
4825 # mode also for directories, so we cannot use $to_mode_str
4826 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
4828 print "<td>" .
4829 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
4830 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
4831 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
4832 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
4833 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
4834 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
4835 -class => "list"}, esc_path($diff->{'from_file'})) .
4836 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
4837 "<td class=\"link\">";
4838 if ($action eq 'commitdiff') {
4839 # link to patch
4840 $patchno++;
4841 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
4842 "patch") .
4843 " | ";
4844 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
4845 # "commit" view and modified file (not only pure rename or copy)
4846 print $cgi->a({-href => href(action=>"blobdiff",
4847 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
4848 hash_base=>$hash, hash_parent_base=>$parent,
4849 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
4850 "diff") .
4851 " | ";
4853 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4854 hash_base=>$parent, file_name=>$diff->{'to_file'})},
4855 "blob") . " | ";
4856 if ($have_blame) {
4857 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
4858 file_name=>$diff->{'to_file'})},
4859 "blame") . " | ";
4861 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
4862 file_name=>$diff->{'to_file'})},
4863 "history");
4864 print "</td>\n";
4866 } # we should not encounter Unmerged (U) or Unknown (X) status
4867 print "</tr>\n";
4869 print "</tbody>" if $has_header;
4870 print "</table>\n";
4873 sub print_sidebyside_diff_chunk {
4874 my @chunk = @_;
4875 my (@ctx, @rem, @add);
4877 return unless @chunk;
4879 # incomplete last line might be among removed or added lines,
4880 # or both, or among context lines: find which
4881 for (my $i = 1; $i < @chunk; $i++) {
4882 if ($chunk[$i][0] eq 'incomplete') {
4883 $chunk[$i][0] = $chunk[$i-1][0];
4887 # guardian
4888 push @chunk, ["", ""];
4890 foreach my $line_info (@chunk) {
4891 my ($class, $line) = @$line_info;
4893 # print chunk headers
4894 if ($class && $class eq 'chunk_header') {
4895 print $line;
4896 next;
4899 ## print from accumulator when type of class of lines change
4900 # empty contents block on start rem/add block, or end of chunk
4901 if (@ctx && (!$class || $class eq 'rem' || $class eq 'add')) {
4902 print join '',
4903 '<div class="chunk_block ctx">',
4904 '<div class="old">',
4905 @ctx,
4906 '</div>',
4907 '<div class="new">',
4908 @ctx,
4909 '</div>',
4910 '</div>';
4911 @ctx = ();
4913 # empty add/rem block on start context block, or end of chunk
4914 if ((@rem || @add) && (!$class || $class eq 'ctx')) {
4915 if (!@add) {
4916 # pure removal
4917 print join '',
4918 '<div class="chunk_block rem">',
4919 '<div class="old">',
4920 @rem,
4921 '</div>',
4922 '</div>';
4923 } elsif (!@rem) {
4924 # pure addition
4925 print join '',
4926 '<div class="chunk_block add">',
4927 '<div class="new">',
4928 @add,
4929 '</div>',
4930 '</div>';
4931 } else {
4932 # assume that it is change
4933 print join '',
4934 '<div class="chunk_block chg">',
4935 '<div class="old">',
4936 @rem,
4937 '</div>',
4938 '<div class="new">',
4939 @add,
4940 '</div>',
4941 '</div>';
4943 @rem = @add = ();
4946 ## adding lines to accumulator
4947 # guardian value
4948 last unless $line;
4949 # rem, add or change
4950 if ($class eq 'rem') {
4951 push @rem, $line;
4952 } elsif ($class eq 'add') {
4953 push @add, $line;
4955 # context line
4956 if ($class eq 'ctx') {
4957 push @ctx, $line;
4962 sub git_patchset_body {
4963 my ($fd, $diff_style, $difftree, $hash, @hash_parents) = @_;
4964 my ($hash_parent) = $hash_parents[0];
4966 my $is_combined = (@hash_parents > 1);
4967 my $patch_idx = 0;
4968 my $patch_number = 0;
4969 my $patch_line;
4970 my $diffinfo;
4971 my $to_name;
4972 my (%from, %to);
4973 my @chunk; # for side-by-side diff
4975 print "<div class=\"patchset\">\n";
4977 # skip to first patch
4978 while ($patch_line = <$fd>) {
4979 chomp $patch_line;
4981 last if ($patch_line =~ m/^diff /);
4984 PATCH:
4985 while ($patch_line) {
4987 # parse "git diff" header line
4988 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
4989 # $1 is from_name, which we do not use
4990 $to_name = unquote($2);
4991 $to_name =~ s!^b/!!;
4992 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
4993 # $1 is 'cc' or 'combined', which we do not use
4994 $to_name = unquote($2);
4995 } else {
4996 $to_name = undef;
4999 # check if current patch belong to current raw line
5000 # and parse raw git-diff line if needed
5001 if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
5002 # this is continuation of a split patch
5003 print "<div class=\"patch cont\">\n";
5004 } else {
5005 # advance raw git-diff output if needed
5006 $patch_idx++ if defined $diffinfo;
5008 # read and prepare patch information
5009 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
5011 # compact combined diff output can have some patches skipped
5012 # find which patch (using pathname of result) we are at now;
5013 if ($is_combined) {
5014 while ($to_name ne $diffinfo->{'to_file'}) {
5015 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
5016 format_diff_cc_simplified($diffinfo, @hash_parents) .
5017 "</div>\n"; # class="patch"
5019 $patch_idx++;
5020 $patch_number++;
5022 last if $patch_idx > $#$difftree;
5023 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
5027 # modifies %from, %to hashes
5028 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
5030 # this is first patch for raw difftree line with $patch_idx index
5031 # we index @$difftree array from 0, but number patches from 1
5032 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
5035 # git diff header
5036 #assert($patch_line =~ m/^diff /) if DEBUG;
5037 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
5038 $patch_number++;
5039 # print "git diff" header
5040 print format_git_diff_header_line($patch_line, $diffinfo,
5041 \%from, \%to);
5043 # print extended diff header
5044 print "<div class=\"diff extended_header\">\n";
5045 EXTENDED_HEADER:
5046 while ($patch_line = <$fd>) {
5047 chomp $patch_line;
5049 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
5051 print format_extended_diff_header_line($patch_line, $diffinfo,
5052 \%from, \%to);
5054 print "</div>\n"; # class="diff extended_header"
5056 # from-file/to-file diff header
5057 if (! $patch_line) {
5058 print "</div>\n"; # class="patch"
5059 last PATCH;
5061 next PATCH if ($patch_line =~ m/^diff /);
5062 #assert($patch_line =~ m/^---/) if DEBUG;
5064 my $last_patch_line = $patch_line;
5065 $patch_line = <$fd>;
5066 chomp $patch_line;
5067 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
5069 print format_diff_from_to_header($last_patch_line, $patch_line,
5070 $diffinfo, \%from, \%to,
5071 @hash_parents);
5073 # the patch itself
5074 LINE:
5075 while ($patch_line = <$fd>) {
5076 chomp $patch_line;
5078 next PATCH if ($patch_line =~ m/^diff /);
5080 my ($class, $line) = process_diff_line($patch_line, \%from, \%to);
5081 my $diff_classes = "diff";
5082 $diff_classes .= " $class" if ($class);
5083 $line = "<div class=\"$diff_classes\">$line</div>\n";
5085 if ($diff_style eq 'sidebyside' && !$is_combined) {
5086 if ($class eq 'chunk_header') {
5087 print_sidebyside_diff_chunk(@chunk);
5088 @chunk = ( [ $class, $line ] );
5089 } else {
5090 push @chunk, [ $class, $line ];
5092 } else {
5093 # default 'inline' style and unknown styles
5094 print $line;
5098 } continue {
5099 if (@chunk) {
5100 print_sidebyside_diff_chunk(@chunk);
5101 @chunk = ();
5103 print "</div>\n"; # class="patch"
5106 # for compact combined (--cc) format, with chunk and patch simplification
5107 # the patchset might be empty, but there might be unprocessed raw lines
5108 for (++$patch_idx if $patch_number > 0;
5109 $patch_idx < @$difftree;
5110 ++$patch_idx) {
5111 # read and prepare patch information
5112 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
5114 # generate anchor for "patch" links in difftree / whatchanged part
5115 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
5116 format_diff_cc_simplified($diffinfo, @hash_parents) .
5117 "</div>\n"; # class="patch"
5119 $patch_number++;
5122 if ($patch_number == 0) {
5123 if (@hash_parents > 1) {
5124 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
5125 } else {
5126 print "<div class=\"diff nodifferences\">No differences found</div>\n";
5130 print "</div>\n"; # class="patchset"
5133 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5135 # fills project list info (age, description, owner, category, forks)
5136 # for each project in the list, removing invalid projects from
5137 # returned list
5138 # NOTE: modifies $projlist, but does not remove entries from it
5139 sub fill_project_list_info {
5140 my $projlist = shift;
5141 my @projects;
5143 my $show_ctags = gitweb_check_feature('ctags');
5144 PROJECT:
5145 foreach my $pr (@$projlist) {
5146 my (@activity) = git_get_last_activity($pr->{'path'});
5147 unless (@activity) {
5148 next PROJECT;
5150 ($pr->{'age'}, $pr->{'age_string'}) = @activity;
5151 if (!defined $pr->{'descr'}) {
5152 my $descr = git_get_project_description($pr->{'path'}) || "";
5153 $descr = to_utf8($descr);
5154 $pr->{'descr_long'} = $descr;
5155 $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
5157 if (!defined $pr->{'owner'}) {
5158 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
5160 if ($show_ctags) {
5161 $pr->{'ctags'} = git_get_project_ctags($pr->{'path'});
5163 if ($projects_list_group_categories && !defined $pr->{'category'}) {
5164 my $cat = git_get_project_category($pr->{'path'}) ||
5165 $project_list_default_category;
5166 $pr->{'category'} = to_utf8($cat);
5169 push @projects, $pr;
5172 return @projects;
5175 sub sort_projects_list {
5176 my ($projlist, $order) = @_;
5177 my @projects;
5179 my %order_info = (
5180 project => { key => 'path', type => 'str' },
5181 descr => { key => 'descr_long', type => 'str' },
5182 owner => { key => 'owner', type => 'str' },
5183 age => { key => 'age', type => 'num' }
5185 my $oi = $order_info{$order};
5186 return @$projlist unless defined $oi;
5187 if ($oi->{'type'} eq 'str') {
5188 @projects = sort {$a->{$oi->{'key'}} cmp $b->{$oi->{'key'}}} @$projlist;
5189 } else {
5190 @projects = sort {$a->{$oi->{'key'}} <=> $b->{$oi->{'key'}}} @$projlist;
5193 return @projects;
5196 # returns a hash of categories, containing the list of project
5197 # belonging to each category
5198 sub build_projlist_by_category {
5199 my ($projlist, $from, $to) = @_;
5200 my %categories;
5202 $from = 0 unless defined $from;
5203 $to = $#$projlist if (!defined $to || $#$projlist < $to);
5205 for (my $i = $from; $i <= $to; $i++) {
5206 my $pr = $projlist->[$i];
5207 push @{$categories{ $pr->{'category'} }}, $pr;
5210 return wantarray ? %categories : \%categories;
5213 # print 'sort by' <th> element, generating 'sort by $name' replay link
5214 # if that order is not selected
5215 sub print_sort_th {
5216 print format_sort_th(@_);
5219 sub format_sort_th {
5220 my ($name, $order, $header) = @_;
5221 my $sort_th = "";
5222 $header ||= ucfirst($name);
5224 if ($order eq $name) {
5225 $sort_th .= "<th>$header</th>\n";
5226 } else {
5227 $sort_th .= "<th>" .
5228 $cgi->a({-href => href(-replay=>1, order=>$name),
5229 -class => "header"}, $header) .
5230 "</th>\n";
5233 return $sort_th;
5236 sub git_project_list_rows {
5237 my ($projlist, $from, $to, $check_forks) = @_;
5239 $from = 0 unless defined $from;
5240 $to = $#$projlist if (!defined $to || $#$projlist < $to);
5242 my $alternate = 1;
5243 for (my $i = $from; $i <= $to; $i++) {
5244 my $pr = $projlist->[$i];
5246 if ($alternate) {
5247 print "<tr class=\"dark\">\n";
5248 } else {
5249 print "<tr class=\"light\">\n";
5251 $alternate ^= 1;
5253 if ($check_forks) {
5254 print "<td>";
5255 if ($pr->{'forks'}) {
5256 my $nforks = scalar @{$pr->{'forks'}};
5257 if ($nforks > 0) {
5258 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks"),
5259 -title => "$nforks forks"}, "+");
5260 } else {
5261 print $cgi->span({-title => "$nforks forks"}, "+");
5264 print "</td>\n";
5266 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
5267 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
5268 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
5269 -class => "list", -title => $pr->{'descr_long'}},
5270 esc_html($pr->{'descr'})) . "</td>\n" .
5271 "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n";
5272 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
5273 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
5274 "<td class=\"link\">" .
5275 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
5276 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
5277 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
5278 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
5279 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
5280 "</td>\n" .
5281 "</tr>\n";
5285 sub git_project_list_body {
5286 # actually uses global variable $project
5287 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
5288 my @projects = @$projlist;
5290 my $check_forks = gitweb_check_feature('forks');
5291 my $show_ctags = gitweb_check_feature('ctags');
5292 my $tagfilter = $show_ctags ? $input_params{'ctag'} : undef;
5293 $check_forks = undef
5294 if ($tagfilter || $searchtext);
5296 # filtering out forks before filling info allows to do less work
5297 @projects = filter_forks_from_projects_list(\@projects)
5298 if ($check_forks);
5299 @projects = fill_project_list_info(\@projects);
5300 # searching projects require filling to be run before it
5301 @projects = search_projects_list(\@projects,
5302 'searchtext' => $searchtext,
5303 'tagfilter' => $tagfilter)
5304 if ($tagfilter || $searchtext);
5306 $order ||= $default_projects_order;
5307 $from = 0 unless defined $from;
5308 $to = $#projects if (!defined $to || $#projects < $to);
5310 # short circuit
5311 if ($from > $to) {
5312 print "<center>\n".
5313 "<b>No such projects found</b><br />\n".
5314 "Click ".$cgi->a({-href=>href(project=>undef)},"here")." to view all projects<br />\n".
5315 "</center>\n<br />\n";
5316 return;
5319 @projects = sort_projects_list(\@projects, $order);
5321 if ($show_ctags) {
5322 my $ctags = git_gather_all_ctags(\@projects);
5323 my $cloud = git_populate_project_tagcloud($ctags);
5324 print git_show_project_tagcloud($cloud, 64);
5327 print "<table class=\"project_list\">\n";
5328 unless ($no_header) {
5329 print "<tr>\n";
5330 if ($check_forks) {
5331 print "<th></th>\n";
5333 print_sort_th('project', $order, 'Project');
5334 print_sort_th('descr', $order, 'Description');
5335 print_sort_th('owner', $order, 'Owner');
5336 print_sort_th('age', $order, 'Last Change');
5337 print "<th></th>\n" . # for links
5338 "</tr>\n";
5341 if ($projects_list_group_categories) {
5342 # only display categories with projects in the $from-$to window
5343 @projects = sort {$a->{'category'} cmp $b->{'category'}} @projects[$from..$to];
5344 my %categories = build_projlist_by_category(\@projects, $from, $to);
5345 foreach my $cat (sort keys %categories) {
5346 unless ($cat eq "") {
5347 print "<tr>\n";
5348 if ($check_forks) {
5349 print "<td></td>\n";
5351 print "<td class=\"category\" colspan=\"5\">".esc_html($cat)."</td>\n";
5352 print "</tr>\n";
5355 git_project_list_rows($categories{$cat}, undef, undef, $check_forks);
5357 } else {
5358 git_project_list_rows(\@projects, $from, $to, $check_forks);
5361 if (defined $extra) {
5362 print "<tr>\n";
5363 if ($check_forks) {
5364 print "<td></td>\n";
5366 print "<td colspan=\"5\">$extra</td>\n" .
5367 "</tr>\n";
5369 print "</table>\n";
5372 sub git_log_body {
5373 # uses global variable $project
5374 my ($commitlist, $from, $to, $refs, $extra) = @_;
5376 $from = 0 unless defined $from;
5377 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
5379 for (my $i = 0; $i <= $to; $i++) {
5380 my %co = %{$commitlist->[$i]};
5381 next if !%co;
5382 my $commit = $co{'id'};
5383 my $ref = format_ref_marker($refs, $commit);
5384 git_print_header_div('commit',
5385 "<span class=\"age\">$co{'age_string'}</span>" .
5386 esc_html($co{'title'}) . $ref,
5387 $commit);
5388 print "<div class=\"title_text\">\n" .
5389 "<div class=\"log_link\">\n" .
5390 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
5391 " | " .
5392 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
5393 " | " .
5394 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
5395 "<br/>\n" .
5396 "</div>\n";
5397 git_print_authorship(\%co, -tag => 'span');
5398 print "<br/>\n</div>\n";
5400 print "<div class=\"log_body\">\n";
5401 git_print_log($co{'comment'}, -final_empty_line=> 1);
5402 print "</div>\n";
5404 if ($extra) {
5405 print "<div class=\"page_nav\">\n";
5406 print "$extra\n";
5407 print "</div>\n";
5411 sub git_shortlog_body {
5412 # uses global variable $project
5413 my ($commitlist, $from, $to, $refs, $extra) = @_;
5415 $from = 0 unless defined $from;
5416 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
5418 print "<table class=\"shortlog\">\n";
5419 my $alternate = 1;
5420 for (my $i = $from; $i <= $to; $i++) {
5421 my %co = %{$commitlist->[$i]};
5422 my $commit = $co{'id'};
5423 my $ref = format_ref_marker($refs, $commit);
5424 if ($alternate) {
5425 print "<tr class=\"dark\">\n";
5426 } else {
5427 print "<tr class=\"light\">\n";
5429 $alternate ^= 1;
5430 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
5431 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5432 format_author_html('td', \%co, 10) . "<td>";
5433 print format_subject_html($co{'title'}, $co{'title_short'},
5434 href(action=>"commit", hash=>$commit), $ref);
5435 print "</td>\n" .
5436 "<td class=\"link\">" .
5437 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
5438 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
5439 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
5440 my $snapshot_links = format_snapshot_links($commit);
5441 if (defined $snapshot_links) {
5442 print " | " . $snapshot_links;
5444 print "</td>\n" .
5445 "</tr>\n";
5447 if (defined $extra) {
5448 print "<tr>\n" .
5449 "<td colspan=\"4\">$extra</td>\n" .
5450 "</tr>\n";
5452 print "</table>\n";
5455 sub git_history_body {
5456 # Warning: assumes constant type (blob or tree) during history
5457 my ($commitlist, $from, $to, $refs, $extra,
5458 $file_name, $file_hash, $ftype) = @_;
5460 $from = 0 unless defined $from;
5461 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
5463 print "<table class=\"history\">\n";
5464 my $alternate = 1;
5465 for (my $i = $from; $i <= $to; $i++) {
5466 my %co = %{$commitlist->[$i]};
5467 if (!%co) {
5468 next;
5470 my $commit = $co{'id'};
5472 my $ref = format_ref_marker($refs, $commit);
5474 if ($alternate) {
5475 print "<tr class=\"dark\">\n";
5476 } else {
5477 print "<tr class=\"light\">\n";
5479 $alternate ^= 1;
5480 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5481 # shortlog: format_author_html('td', \%co, 10)
5482 format_author_html('td', \%co, 15, 3) . "<td>";
5483 # originally git_history used chop_str($co{'title'}, 50)
5484 print format_subject_html($co{'title'}, $co{'title_short'},
5485 href(action=>"commit", hash=>$commit), $ref);
5486 print "</td>\n" .
5487 "<td class=\"link\">" .
5488 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
5489 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
5491 if ($ftype eq 'blob') {
5492 my $blob_current = $file_hash;
5493 my $blob_parent = git_get_hash_by_path($commit, $file_name);
5494 if (defined $blob_current && defined $blob_parent &&
5495 $blob_current ne $blob_parent) {
5496 print " | " .
5497 $cgi->a({-href => href(action=>"blobdiff",
5498 hash=>$blob_current, hash_parent=>$blob_parent,
5499 hash_base=>$hash_base, hash_parent_base=>$commit,
5500 file_name=>$file_name)},
5501 "diff to current");
5504 print "</td>\n" .
5505 "</tr>\n";
5507 if (defined $extra) {
5508 print "<tr>\n" .
5509 "<td colspan=\"4\">$extra</td>\n" .
5510 "</tr>\n";
5512 print "</table>\n";
5515 sub git_tags_body {
5516 # uses global variable $project
5517 my ($taglist, $from, $to, $extra) = @_;
5518 $from = 0 unless defined $from;
5519 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
5521 print "<table class=\"tags\">\n";
5522 my $alternate = 1;
5523 for (my $i = $from; $i <= $to; $i++) {
5524 my $entry = $taglist->[$i];
5525 my %tag = %$entry;
5526 my $comment = $tag{'subject'};
5527 my $comment_short;
5528 if (defined $comment) {
5529 $comment_short = chop_str($comment, 30, 5);
5531 if ($alternate) {
5532 print "<tr class=\"dark\">\n";
5533 } else {
5534 print "<tr class=\"light\">\n";
5536 $alternate ^= 1;
5537 if (defined $tag{'age'}) {
5538 print "<td><i>$tag{'age'}</i></td>\n";
5539 } else {
5540 print "<td></td>\n";
5542 print "<td>" .
5543 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
5544 -class => "list name"}, esc_html($tag{'name'})) .
5545 "</td>\n" .
5546 "<td>";
5547 if (defined $comment) {
5548 print format_subject_html($comment, $comment_short,
5549 href(action=>"tag", hash=>$tag{'id'}));
5551 print "</td>\n" .
5552 "<td class=\"selflink\">";
5553 if ($tag{'type'} eq "tag") {
5554 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
5555 } else {
5556 print "&nbsp;";
5558 print "</td>\n" .
5559 "<td class=\"link\">" . " | " .
5560 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
5561 if ($tag{'reftype'} eq "commit") {
5562 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})}, "shortlog") .
5563 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})}, "log");
5564 } elsif ($tag{'reftype'} eq "blob") {
5565 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
5567 print "</td>\n" .
5568 "</tr>";
5570 if (defined $extra) {
5571 print "<tr>\n" .
5572 "<td colspan=\"5\">$extra</td>\n" .
5573 "</tr>\n";
5575 print "</table>\n";
5578 sub git_heads_body {
5579 # uses global variable $project
5580 my ($headlist, $head_at, $from, $to, $extra) = @_;
5581 $from = 0 unless defined $from;
5582 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
5584 print "<table class=\"heads\">\n";
5585 my $alternate = 1;
5586 for (my $i = $from; $i <= $to; $i++) {
5587 my $entry = $headlist->[$i];
5588 my %ref = %$entry;
5589 my $curr = defined $head_at && $ref{'id'} eq $head_at;
5590 if ($alternate) {
5591 print "<tr class=\"dark\">\n";
5592 } else {
5593 print "<tr class=\"light\">\n";
5595 $alternate ^= 1;
5596 print "<td><i>$ref{'age'}</i></td>\n" .
5597 ($curr ? "<td class=\"current_head\">" : "<td>") .
5598 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),
5599 -class => "list name"},esc_html($ref{'name'})) .
5600 "</td>\n" .
5601 "<td class=\"link\">" .
5602 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})}, "shortlog") . " | " .
5603 $cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})}, "log") . " | " .
5604 $cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'fullname'})}, "tree") .
5605 "</td>\n" .
5606 "</tr>";
5608 if (defined $extra) {
5609 print "<tr>\n" .
5610 "<td colspan=\"3\">$extra</td>\n" .
5611 "</tr>\n";
5613 print "</table>\n";
5616 # Display a single remote block
5617 sub git_remote_block {
5618 my ($remote, $rdata, $limit, $head) = @_;
5620 my $heads = $rdata->{'heads'};
5621 my $fetch = $rdata->{'fetch'};
5622 my $push = $rdata->{'push'};
5624 my $urls_table = "<table class=\"projects_list\">\n" ;
5626 if (defined $fetch) {
5627 if ($fetch eq $push) {
5628 $urls_table .= format_repo_url("URL", $fetch);
5629 } else {
5630 $urls_table .= format_repo_url("Fetch URL", $fetch);
5631 $urls_table .= format_repo_url("Push URL", $push) if defined $push;
5633 } elsif (defined $push) {
5634 $urls_table .= format_repo_url("Push URL", $push);
5635 } else {
5636 $urls_table .= format_repo_url("", "No remote URL");
5639 $urls_table .= "</table>\n";
5641 my $dots;
5642 if (defined $limit && $limit < @$heads) {
5643 $dots = $cgi->a({-href => href(action=>"remotes", hash=>$remote)}, "...");
5646 print $urls_table;
5647 git_heads_body($heads, $head, 0, $limit, $dots);
5650 # Display a list of remote names with the respective fetch and push URLs
5651 sub git_remotes_list {
5652 my ($remotedata, $limit) = @_;
5653 print "<table class=\"heads\">\n";
5654 my $alternate = 1;
5655 my @remotes = sort keys %$remotedata;
5657 my $limited = $limit && $limit < @remotes;
5659 $#remotes = $limit - 1 if $limited;
5661 while (my $remote = shift @remotes) {
5662 my $rdata = $remotedata->{$remote};
5663 my $fetch = $rdata->{'fetch'};
5664 my $push = $rdata->{'push'};
5665 if ($alternate) {
5666 print "<tr class=\"dark\">\n";
5667 } else {
5668 print "<tr class=\"light\">\n";
5670 $alternate ^= 1;
5671 print "<td>" .
5672 $cgi->a({-href=> href(action=>'remotes', hash=>$remote),
5673 -class=> "list name"},esc_html($remote)) .
5674 "</td>";
5675 print "<td class=\"link\">" .
5676 (defined $fetch ? $cgi->a({-href=> $fetch}, "fetch") : "fetch") .
5677 " | " .
5678 (defined $push ? $cgi->a({-href=> $push}, "push") : "push") .
5679 "</td>";
5681 print "</tr>\n";
5684 if ($limited) {
5685 print "<tr>\n" .
5686 "<td colspan=\"3\">" .
5687 $cgi->a({-href => href(action=>"remotes")}, "...") .
5688 "</td>\n" . "</tr>\n";
5691 print "</table>";
5694 # Display remote heads grouped by remote, unless there are too many
5695 # remotes, in which case we only display the remote names
5696 sub git_remotes_body {
5697 my ($remotedata, $limit, $head) = @_;
5698 if ($limit and $limit < keys %$remotedata) {
5699 git_remotes_list($remotedata, $limit);
5700 } else {
5701 fill_remote_heads($remotedata);
5702 while (my ($remote, $rdata) = each %$remotedata) {
5703 git_print_section({-class=>"remote", -id=>$remote},
5704 ["remotes", $remote, $remote], sub {
5705 git_remote_block($remote, $rdata, $limit, $head);
5711 sub git_search_message {
5712 my %co = @_;
5714 my $greptype;
5715 if ($searchtype eq 'commit') {
5716 $greptype = "--grep=";
5717 } elsif ($searchtype eq 'author') {
5718 $greptype = "--author=";
5719 } elsif ($searchtype eq 'committer') {
5720 $greptype = "--committer=";
5722 $greptype .= $searchtext;
5723 my @commitlist = parse_commits($hash, 101, (100 * $page), undef,
5724 $greptype, '--regexp-ignore-case',
5725 $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
5727 my $paging_nav = '';
5728 if ($page > 0) {
5729 $paging_nav .=
5730 $cgi->a({-href => href(-replay=>1, page=>undef)},
5731 "first") .
5732 " &sdot; " .
5733 $cgi->a({-href => href(-replay=>1, page=>$page-1),
5734 -accesskey => "p", -title => "Alt-p"}, "prev");
5735 } else {
5736 $paging_nav .= "first &sdot; prev";
5738 my $next_link = '';
5739 if ($#commitlist >= 100) {
5740 $next_link =
5741 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5742 -accesskey => "n", -title => "Alt-n"}, "next");
5743 $paging_nav .= " &sdot; $next_link";
5744 } else {
5745 $paging_nav .= " &sdot; next";
5748 git_header_html();
5750 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
5751 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5752 if ($page == 0 && !@commitlist) {
5753 print "<p>No match.</p>\n";
5754 } else {
5755 git_search_grep_body(\@commitlist, 0, 99, $next_link);
5758 git_footer_html();
5761 sub git_search_changes {
5762 my %co = @_;
5764 local $/ = "\n";
5765 open my $fd, '-|', git_cmd(), '--no-pager', 'log', @diff_opts,
5766 '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
5767 ($search_use_regexp ? '--pickaxe-regex' : ())
5768 or die_error(500, "Open git-log failed");
5770 git_header_html();
5772 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5773 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5775 print "<table class=\"pickaxe search\">\n";
5776 my $alternate = 1;
5777 undef %co;
5778 my @files;
5779 while (my $line = <$fd>) {
5780 chomp $line;
5781 next unless $line;
5783 my %set = parse_difftree_raw_line($line);
5784 if (defined $set{'commit'}) {
5785 # finish previous commit
5786 if (%co) {
5787 print "</td>\n" .
5788 "<td class=\"link\">" .
5789 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},
5790 "commit") .
5791 " | " .
5792 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'},
5793 hash_base=>$co{'id'})},
5794 "tree") .
5795 "</td>\n" .
5796 "</tr>\n";
5799 if ($alternate) {
5800 print "<tr class=\"dark\">\n";
5801 } else {
5802 print "<tr class=\"light\">\n";
5804 $alternate ^= 1;
5805 %co = parse_commit($set{'commit'});
5806 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
5807 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5808 "<td><i>$author</i></td>\n" .
5809 "<td>" .
5810 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
5811 -class => "list subject"},
5812 chop_and_escape_str($co{'title'}, 50) . "<br/>");
5813 } elsif (defined $set{'to_id'}) {
5814 next if ($set{'to_id'} =~ m/^0{40}$/);
5816 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
5817 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),
5818 -class => "list"},
5819 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
5820 "<br/>\n";
5823 close $fd;
5825 # finish last commit (warning: repetition!)
5826 if (%co) {
5827 print "</td>\n" .
5828 "<td class=\"link\">" .
5829 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},
5830 "commit") .
5831 " | " .
5832 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'},
5833 hash_base=>$co{'id'})},
5834 "tree") .
5835 "</td>\n" .
5836 "</tr>\n";
5839 print "</table>\n";
5841 git_footer_html();
5844 sub git_search_files {
5845 my %co = @_;
5847 local $/ = "\n";
5848 open my $fd, "-|", git_cmd(), 'grep', '-n', '-z',
5849 $search_use_regexp ? ('-E', '-i') : '-F',
5850 $searchtext, $co{'tree'}
5851 or die_error(500, "Open git-grep failed");
5853 git_header_html();
5855 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5856 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5858 print "<table class=\"grep_search\">\n";
5859 my $alternate = 1;
5860 my $matches = 0;
5861 my $lastfile = '';
5862 my $file_href;
5863 while (my $line = <$fd>) {
5864 chomp $line;
5865 my ($file, $lno, $ltext, $binary);
5866 last if ($matches++ > 1000);
5867 if ($line =~ /^Binary file (.+) matches$/) {
5868 $file = $1;
5869 $binary = 1;
5870 } else {
5871 ($file, $lno, $ltext) = split(/\0/, $line, 3);
5872 $file =~ s/^$co{'tree'}://;
5874 if ($file ne $lastfile) {
5875 $lastfile and print "</td></tr>\n";
5876 if ($alternate++) {
5877 print "<tr class=\"dark\">\n";
5878 } else {
5879 print "<tr class=\"light\">\n";
5881 $file_href = href(action=>"blob", hash_base=>$co{'id'},
5882 file_name=>$file);
5883 print "<td class=\"list\">".
5884 $cgi->a({-href => $file_href, -class => "list"}, esc_path($file));
5885 print "</td><td>\n";
5886 $lastfile = $file;
5888 if ($binary) {
5889 print "<div class=\"binary\">Binary file</div>\n";
5890 } else {
5891 $ltext = untabify($ltext);
5892 if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
5893 $ltext = esc_html($1, -nbsp=>1);
5894 $ltext .= '<span class="match">';
5895 $ltext .= esc_html($2, -nbsp=>1);
5896 $ltext .= '</span>';
5897 $ltext .= esc_html($3, -nbsp=>1);
5898 } else {
5899 $ltext = esc_html($ltext, -nbsp=>1);
5901 print "<div class=\"pre\">" .
5902 $cgi->a({-href => $file_href.'#l'.$lno,
5903 -class => "linenr"}, sprintf('%4i', $lno)) .
5904 ' ' . $ltext . "</div>\n";
5907 if ($lastfile) {
5908 print "</td></tr>\n";
5909 if ($matches > 1000) {
5910 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
5912 } else {
5913 print "<div class=\"diff nodifferences\">No matches found</div>\n";
5915 close $fd;
5917 print "</table>\n";
5919 git_footer_html();
5922 sub git_search_grep_body {
5923 my ($commitlist, $from, $to, $extra) = @_;
5924 $from = 0 unless defined $from;
5925 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
5927 print "<table class=\"commit_search\">\n";
5928 my $alternate = 1;
5929 for (my $i = $from; $i <= $to; $i++) {
5930 my %co = %{$commitlist->[$i]};
5931 if (!%co) {
5932 next;
5934 my $commit = $co{'id'};
5935 if ($alternate) {
5936 print "<tr class=\"dark\">\n";
5937 } else {
5938 print "<tr class=\"light\">\n";
5940 $alternate ^= 1;
5941 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5942 format_author_html('td', \%co, 15, 5) .
5943 "<td>" .
5944 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
5945 -class => "list subject"},
5946 chop_and_escape_str($co{'title'}, 50) . "<br/>");
5947 my $comment = $co{'comment'};
5948 foreach my $line (@$comment) {
5949 if ($line =~ m/^(.*?)($search_regexp)(.*)$/i) {
5950 my ($lead, $match, $trail) = ($1, $2, $3);
5951 $match = chop_str($match, 70, 5, 'center');
5952 my $contextlen = int((80 - length($match))/2);
5953 $contextlen = 30 if ($contextlen > 30);
5954 $lead = chop_str($lead, $contextlen, 10, 'left');
5955 $trail = chop_str($trail, $contextlen, 10, 'right');
5957 $lead = esc_html($lead);
5958 $match = esc_html($match);
5959 $trail = esc_html($trail);
5961 print "$lead<span class=\"match\">$match</span>$trail<br />";
5964 print "</td>\n" .
5965 "<td class=\"link\">" .
5966 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5967 " | " .
5968 $cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})}, "commitdiff") .
5969 " | " .
5970 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5971 print "</td>\n" .
5972 "</tr>\n";
5974 if (defined $extra) {
5975 print "<tr>\n" .
5976 "<td colspan=\"3\">$extra</td>\n" .
5977 "</tr>\n";
5979 print "</table>\n";
5982 ## ======================================================================
5983 ## ======================================================================
5984 ## actions
5986 sub git_project_list {
5987 my $order = $input_params{'order'};
5988 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
5989 die_error(400, "Unknown order parameter");
5992 my @list = git_get_projects_list();
5993 if (!@list) {
5994 die_error(404, "No projects found");
5997 git_header_html();
5998 if (defined $home_text && -f $home_text) {
5999 print "<div class=\"index_include\">\n";
6000 insert_file($home_text);
6001 print "</div>\n";
6003 print $cgi->startform(-method => "get") .
6004 "<p class=\"projsearch\">Search:\n" .
6005 $cgi->textfield(-name => "s", -value => $searchtext, -override => 1) . "\n" .
6006 "</p>" .
6007 $cgi->end_form() . "\n";
6008 git_project_list_body(\@list, $order);
6009 git_footer_html();
6012 sub git_forks {
6013 my $order = $input_params{'order'};
6014 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
6015 die_error(400, "Unknown order parameter");
6018 my @list = git_get_projects_list($project);
6019 if (!@list) {
6020 die_error(404, "No forks found");
6023 git_header_html();
6024 git_print_page_nav('','');
6025 git_print_header_div('summary', "$project forks");
6026 git_project_list_body(\@list, $order);
6027 git_footer_html();
6030 sub git_project_index {
6031 my @projects = git_get_projects_list();
6032 if (!@projects) {
6033 die_error(404, "No projects found");
6036 print $cgi->header(
6037 -type => 'text/plain',
6038 -charset => 'utf-8',
6039 -content_disposition => 'inline; filename="index.aux"');
6041 foreach my $pr (@projects) {
6042 if (!exists $pr->{'owner'}) {
6043 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
6046 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
6047 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
6048 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
6049 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
6050 $path =~ s/ /\+/g;
6051 $owner =~ s/ /\+/g;
6053 print "$path $owner\n";
6057 sub git_summary {
6058 my $descr = git_get_project_description($project) || "none";
6059 my %co = parse_commit("HEAD");
6060 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
6061 my $head = $co{'id'};
6062 my $remote_heads = gitweb_check_feature('remote_heads');
6064 my $owner = git_get_project_owner($project);
6066 my $refs = git_get_references();
6067 # These get_*_list functions return one more to allow us to see if
6068 # there are more ...
6069 my @taglist = git_get_tags_list(16);
6070 my @headlist = git_get_heads_list(16);
6071 my %remotedata = $remote_heads ? git_get_remotes_list() : ();
6072 my @forklist;
6073 my $check_forks = gitweb_check_feature('forks');
6075 if ($check_forks) {
6076 # find forks of a project
6077 @forklist = git_get_projects_list($project);
6078 # filter out forks of forks
6079 @forklist = filter_forks_from_projects_list(\@forklist)
6080 if (@forklist);
6083 git_header_html();
6084 git_print_page_nav('summary','', $head);
6086 print "<div class=\"title\">&nbsp;</div>\n";
6087 print "<table class=\"projects_list\">\n" .
6088 "<tr id=\"metadata_desc\"><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
6089 "<tr id=\"metadata_owner\"><td>owner</td><td>" . esc_html($owner) . "</td></tr>\n";
6090 if (defined $cd{'rfc2822'}) {
6091 print "<tr id=\"metadata_lchange\"><td>last change</td>" .
6092 "<td>".format_timestamp_html(\%cd)."</td></tr>\n";
6095 # use per project git URL list in $projectroot/$project/cloneurl
6096 # or make project git URL from git base URL and project name
6097 my $url_tag = "URL";
6098 my @url_list = git_get_project_url_list($project);
6099 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
6100 foreach my $git_url (@url_list) {
6101 next unless $git_url;
6102 print format_repo_url($url_tag, $git_url);
6103 $url_tag = "";
6106 # Tag cloud
6107 my $show_ctags = gitweb_check_feature('ctags');
6108 if ($show_ctags) {
6109 my $ctags = git_get_project_ctags($project);
6110 if (%$ctags) {
6111 # without ability to add tags, don't show if there are none
6112 my $cloud = git_populate_project_tagcloud($ctags);
6113 print "<tr id=\"metadata_ctags\">" .
6114 "<td>content tags</td>" .
6115 "<td>".git_show_project_tagcloud($cloud, 48)."</td>" .
6116 "</tr>\n";
6120 print "</table>\n";
6122 # If XSS prevention is on, we don't include README.html.
6123 # TODO: Allow a readme in some safe format.
6124 if (!$prevent_xss && -s "$projectroot/$project/README.html") {
6125 print "<div class=\"title\">readme</div>\n" .
6126 "<div class=\"readme\">\n";
6127 insert_file("$projectroot/$project/README.html");
6128 print "\n</div>\n"; # class="readme"
6131 # we need to request one more than 16 (0..15) to check if
6132 # those 16 are all
6133 my @commitlist = $head ? parse_commits($head, 17) : ();
6134 if (@commitlist) {
6135 git_print_header_div('shortlog');
6136 git_shortlog_body(\@commitlist, 0, 15, $refs,
6137 $#commitlist <= 15 ? undef :
6138 $cgi->a({-href => href(action=>"shortlog")}, "..."));
6141 if (@taglist) {
6142 git_print_header_div('tags');
6143 git_tags_body(\@taglist, 0, 15,
6144 $#taglist <= 15 ? undef :
6145 $cgi->a({-href => href(action=>"tags")}, "..."));
6148 if (@headlist) {
6149 git_print_header_div('heads');
6150 git_heads_body(\@headlist, $head, 0, 15,
6151 $#headlist <= 15 ? undef :
6152 $cgi->a({-href => href(action=>"heads")}, "..."));
6155 if (%remotedata) {
6156 git_print_header_div('remotes');
6157 git_remotes_body(\%remotedata, 15, $head);
6160 if (@forklist) {
6161 git_print_header_div('forks');
6162 git_project_list_body(\@forklist, 'age', 0, 15,
6163 $#forklist <= 15 ? undef :
6164 $cgi->a({-href => href(action=>"forks")}, "..."),
6165 'no_header');
6168 git_footer_html();
6171 sub git_tag {
6172 my %tag = parse_tag($hash);
6174 if (! %tag) {
6175 die_error(404, "Unknown tag object");
6178 my $head = git_get_head_hash($project);
6179 git_header_html();
6180 git_print_page_nav('','', $head,undef,$head);
6181 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
6182 print "<div class=\"title_text\">\n" .
6183 "<table class=\"object_header\">\n" .
6184 "<tr>\n" .
6185 "<td>object</td>\n" .
6186 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
6187 $tag{'object'}) . "</td>\n" .
6188 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
6189 $tag{'type'}) . "</td>\n" .
6190 "</tr>\n";
6191 if (defined($tag{'author'})) {
6192 git_print_authorship_rows(\%tag, 'author');
6194 print "</table>\n\n" .
6195 "</div>\n";
6196 print "<div class=\"page_body\">";
6197 my $comment = $tag{'comment'};
6198 foreach my $line (@$comment) {
6199 chomp $line;
6200 print esc_html($line, -nbsp=>1) . "<br/>\n";
6202 print "</div>\n";
6203 git_footer_html();
6206 sub git_blame_common {
6207 my $format = shift || 'porcelain';
6208 if ($format eq 'porcelain' && $input_params{'javascript'}) {
6209 $format = 'incremental';
6210 $action = 'blame_incremental'; # for page title etc
6213 # permissions
6214 gitweb_check_feature('blame')
6215 or die_error(403, "Blame view not allowed");
6217 # error checking
6218 die_error(400, "No file name given") unless $file_name;
6219 $hash_base ||= git_get_head_hash($project);
6220 die_error(404, "Couldn't find base commit") unless $hash_base;
6221 my %co = parse_commit($hash_base)
6222 or die_error(404, "Commit not found");
6223 my $ftype = "blob";
6224 if (!defined $hash) {
6225 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
6226 or die_error(404, "Error looking up file");
6227 } else {
6228 $ftype = git_get_type($hash);
6229 if ($ftype !~ "blob") {
6230 die_error(400, "Object is not a blob");
6234 my $fd;
6235 if ($format eq 'incremental') {
6236 # get file contents (as base)
6237 open $fd, "-|", git_cmd(), 'cat-file', 'blob', $hash
6238 or die_error(500, "Open git-cat-file failed");
6239 } elsif ($format eq 'data') {
6240 # run git-blame --incremental
6241 open $fd, "-|", git_cmd(), "blame", "--incremental",
6242 $hash_base, "--", $file_name
6243 or die_error(500, "Open git-blame --incremental failed");
6244 } else {
6245 # run git-blame --porcelain
6246 open $fd, "-|", git_cmd(), "blame", '-p',
6247 $hash_base, '--', $file_name
6248 or die_error(500, "Open git-blame --porcelain failed");
6251 # incremental blame data returns early
6252 if ($format eq 'data') {
6253 print $cgi->header(
6254 -type=>"text/plain", -charset => "utf-8",
6255 -status=> "200 OK");
6256 local $| = 1; # output autoflush
6257 while (my $line = <$fd>) {
6258 print to_utf8($line);
6260 close $fd
6261 or print "ERROR $!\n";
6263 print 'END';
6264 if (defined $t0 && gitweb_check_feature('timed')) {
6265 print ' '.
6266 tv_interval($t0, [ gettimeofday() ]).
6267 ' '.$number_of_git_cmds;
6269 print "\n";
6271 return;
6274 # page header
6275 git_header_html();
6276 my $formats_nav =
6277 $cgi->a({-href => href(action=>"blob", -replay=>1)},
6278 "blob") .
6279 " | ";
6280 if ($format eq 'incremental') {
6281 $formats_nav .=
6282 $cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},
6283 "blame") . " (non-incremental)";
6284 } else {
6285 $formats_nav .=
6286 $cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},
6287 "blame") . " (incremental)";
6289 $formats_nav .=
6290 " | " .
6291 $cgi->a({-href => href(action=>"history", -replay=>1)},
6292 "history") .
6293 " | " .
6294 $cgi->a({-href => href(action=>$action, file_name=>$file_name)},
6295 "HEAD");
6296 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
6297 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
6298 git_print_page_path($file_name, $ftype, $hash_base);
6300 # page body
6301 if ($format eq 'incremental') {
6302 print "<noscript>\n<div class=\"error\"><center><b>\n".
6303 "This page requires JavaScript to run.\n Use ".
6304 $cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},
6305 'this page').
6306 " instead.\n".
6307 "</b></center></div>\n</noscript>\n";
6309 print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;
6312 print qq!<div class="page_body">\n!;
6313 print qq!<div id="progress_info">... / ...</div>\n!
6314 if ($format eq 'incremental');
6315 print qq!<table id="blame_table" class="blame" width="100%">\n!.
6316 #qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.
6317 qq!<thead>\n!.
6318 qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.
6319 qq!</thead>\n!.
6320 qq!<tbody>\n!;
6322 my @rev_color = qw(light dark);
6323 my $num_colors = scalar(@rev_color);
6324 my $current_color = 0;
6326 if ($format eq 'incremental') {
6327 my $color_class = $rev_color[$current_color];
6329 #contents of a file
6330 my $linenr = 0;
6331 LINE:
6332 while (my $line = <$fd>) {
6333 chomp $line;
6334 $linenr++;
6336 print qq!<tr id="l$linenr" class="$color_class">!.
6337 qq!<td class="sha1"><a href=""> </a></td>!.
6338 qq!<td class="linenr">!.
6339 qq!<a class="linenr" href="">$linenr</a></td>!;
6340 print qq!<td class="pre">! . esc_html($line) . "</td>\n";
6341 print qq!</tr>\n!;
6344 } else { # porcelain, i.e. ordinary blame
6345 my %metainfo = (); # saves information about commits
6347 # blame data
6348 LINE:
6349 while (my $line = <$fd>) {
6350 chomp $line;
6351 # the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]
6352 # no <lines in group> for subsequent lines in group of lines
6353 my ($full_rev, $orig_lineno, $lineno, $group_size) =
6354 ($line =~ /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);
6355 if (!exists $metainfo{$full_rev}) {
6356 $metainfo{$full_rev} = { 'nprevious' => 0 };
6358 my $meta = $metainfo{$full_rev};
6359 my $data;
6360 while ($data = <$fd>) {
6361 chomp $data;
6362 last if ($data =~ s/^\t//); # contents of line
6363 if ($data =~ /^(\S+)(?: (.*))?$/) {
6364 $meta->{$1} = $2 unless exists $meta->{$1};
6366 if ($data =~ /^previous /) {
6367 $meta->{'nprevious'}++;
6370 my $short_rev = substr($full_rev, 0, 8);
6371 my $author = $meta->{'author'};
6372 my %date =
6373 parse_date($meta->{'author-time'}, $meta->{'author-tz'});
6374 my $date = $date{'iso-tz'};
6375 if ($group_size) {
6376 $current_color = ($current_color + 1) % $num_colors;
6378 my $tr_class = $rev_color[$current_color];
6379 $tr_class .= ' boundary' if (exists $meta->{'boundary'});
6380 $tr_class .= ' no-previous' if ($meta->{'nprevious'} == 0);
6381 $tr_class .= ' multiple-previous' if ($meta->{'nprevious'} > 1);
6382 print "<tr id=\"l$lineno\" class=\"$tr_class\">\n";
6383 if ($group_size) {
6384 print "<td class=\"sha1\"";
6385 print " title=\"". esc_html($author) . ", $date\"";
6386 print " rowspan=\"$group_size\"" if ($group_size > 1);
6387 print ">";
6388 print $cgi->a({-href => href(action=>"commit",
6389 hash=>$full_rev,
6390 file_name=>$file_name)},
6391 esc_html($short_rev));
6392 if ($group_size >= 2) {
6393 my @author_initials = ($author =~ /\b([[:upper:]])\B/g);
6394 if (@author_initials) {
6395 print "<br />" .
6396 esc_html(join('', @author_initials));
6397 # or join('.', ...)
6400 print "</td>\n";
6402 # 'previous' <sha1 of parent commit> <filename at commit>
6403 if (exists $meta->{'previous'} &&
6404 $meta->{'previous'} =~ /^([a-fA-F0-9]{40}) (.*)$/) {
6405 $meta->{'parent'} = $1;
6406 $meta->{'file_parent'} = unquote($2);
6408 my $linenr_commit =
6409 exists($meta->{'parent'}) ?
6410 $meta->{'parent'} : $full_rev;
6411 my $linenr_filename =
6412 exists($meta->{'file_parent'}) ?
6413 $meta->{'file_parent'} : unquote($meta->{'filename'});
6414 my $blamed = href(action => 'blame',
6415 file_name => $linenr_filename,
6416 hash_base => $linenr_commit);
6417 print "<td class=\"linenr\">";
6418 print $cgi->a({ -href => "$blamed#l$orig_lineno",
6419 -class => "linenr" },
6420 esc_html($lineno));
6421 print "</td>";
6422 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
6423 print "</tr>\n";
6424 } # end while
6428 # footer
6429 print "</tbody>\n".
6430 "</table>\n"; # class="blame"
6431 print "</div>\n"; # class="blame_body"
6432 close $fd
6433 or print "Reading blob failed\n";
6435 git_footer_html();
6438 sub git_blame {
6439 git_blame_common();
6442 sub git_blame_incremental {
6443 git_blame_common('incremental');
6446 sub git_blame_data {
6447 git_blame_common('data');
6450 sub git_tags {
6451 my $head = git_get_head_hash($project);
6452 git_header_html();
6453 git_print_page_nav('','', $head,undef,$head,format_ref_views('tags'));
6454 git_print_header_div('summary', $project);
6456 my @tagslist = git_get_tags_list();
6457 if (@tagslist) {
6458 git_tags_body(\@tagslist);
6460 git_footer_html();
6463 sub git_heads {
6464 my $head = git_get_head_hash($project);
6465 git_header_html();
6466 git_print_page_nav('','', $head,undef,$head,format_ref_views('heads'));
6467 git_print_header_div('summary', $project);
6469 my @headslist = git_get_heads_list();
6470 if (@headslist) {
6471 git_heads_body(\@headslist, $head);
6473 git_footer_html();
6476 # used both for single remote view and for list of all the remotes
6477 sub git_remotes {
6478 gitweb_check_feature('remote_heads')
6479 or die_error(403, "Remote heads view is disabled");
6481 my $head = git_get_head_hash($project);
6482 my $remote = $input_params{'hash'};
6484 my $remotedata = git_get_remotes_list($remote);
6485 die_error(500, "Unable to get remote information") unless defined $remotedata;
6487 unless (%$remotedata) {
6488 die_error(404, defined $remote ?
6489 "Remote $remote not found" :
6490 "No remotes found");
6493 git_header_html(undef, undef, -action_extra => $remote);
6494 git_print_page_nav('', '', $head, undef, $head,
6495 format_ref_views($remote ? '' : 'remotes'));
6497 fill_remote_heads($remotedata);
6498 if (defined $remote) {
6499 git_print_header_div('remotes', "$remote remote for $project");
6500 git_remote_block($remote, $remotedata->{$remote}, undef, $head);
6501 } else {
6502 git_print_header_div('summary', "$project remotes");
6503 git_remotes_body($remotedata, undef, $head);
6506 git_footer_html();
6509 sub git_blob_plain {
6510 my $type = shift;
6511 my $expires;
6513 if (!defined $hash) {
6514 if (defined $file_name) {
6515 my $base = $hash_base || git_get_head_hash($project);
6516 $hash = git_get_hash_by_path($base, $file_name, "blob")
6517 or die_error(404, "Cannot find file");
6518 } else {
6519 die_error(400, "No file name defined");
6521 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6522 # blobs defined by non-textual hash id's can be cached
6523 $expires = "+1d";
6526 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
6527 or die_error(500, "Open git-cat-file blob '$hash' failed");
6529 # content-type (can include charset)
6530 $type = blob_contenttype($fd, $file_name, $type);
6532 # "save as" filename, even when no $file_name is given
6533 my $save_as = "$hash";
6534 if (defined $file_name) {
6535 $save_as = $file_name;
6536 } elsif ($type =~ m/^text\//) {
6537 $save_as .= '.txt';
6540 # With XSS prevention on, blobs of all types except a few known safe
6541 # ones are served with "Content-Disposition: attachment" to make sure
6542 # they don't run in our security domain. For certain image types,
6543 # blob view writes an <img> tag referring to blob_plain view, and we
6544 # want to be sure not to break that by serving the image as an
6545 # attachment (though Firefox 3 doesn't seem to care).
6546 my $sandbox = $prevent_xss &&
6547 $type !~ m!^(?:text/[a-z]+|image/(?:gif|png|jpeg))(?:[ ;]|$)!;
6549 # serve text/* as text/plain
6550 if ($prevent_xss &&
6551 ($type =~ m!^text/[a-z]+\b(.*)$! ||
6552 ($type =~ m!^[a-z]+/[a-z]\+xml\b(.*)$! && -T $fd))) {
6553 my $rest = $1;
6554 $rest = defined $rest ? $rest : '';
6555 $type = "text/plain$rest";
6558 print $cgi->header(
6559 -type => $type,
6560 -expires => $expires,
6561 -content_disposition =>
6562 ($sandbox ? 'attachment' : 'inline')
6563 . '; filename="' . $save_as . '"');
6564 local $/ = undef;
6565 binmode STDOUT, ':raw';
6566 print <$fd>;
6567 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
6568 close $fd;
6571 sub git_blob {
6572 my $expires;
6574 if (!defined $hash) {
6575 if (defined $file_name) {
6576 my $base = $hash_base || git_get_head_hash($project);
6577 $hash = git_get_hash_by_path($base, $file_name, "blob")
6578 or die_error(404, "Cannot find file");
6579 } else {
6580 die_error(400, "No file name defined");
6582 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6583 # blobs defined by non-textual hash id's can be cached
6584 $expires = "+1d";
6587 my $have_blame = gitweb_check_feature('blame');
6588 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
6589 or die_error(500, "Couldn't cat $file_name, $hash");
6590 my $mimetype = blob_mimetype($fd, $file_name);
6591 # use 'blob_plain' (aka 'raw') view for files that cannot be displayed
6592 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {
6593 close $fd;
6594 return git_blob_plain($mimetype);
6596 # we can have blame only for text/* mimetype
6597 $have_blame &&= ($mimetype =~ m!^text/!);
6599 my $highlight = gitweb_check_feature('highlight');
6600 my $syntax = guess_file_syntax($highlight, $mimetype, $file_name);
6601 $fd = run_highlighter($fd, $highlight, $syntax)
6602 if $syntax;
6604 git_header_html(undef, $expires);
6605 my $formats_nav = '';
6606 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
6607 if (defined $file_name) {
6608 if ($have_blame) {
6609 $formats_nav .=
6610 $cgi->a({-href => href(action=>"blame", -replay=>1)},
6611 "blame") .
6612 " | ";
6614 $formats_nav .=
6615 $cgi->a({-href => href(action=>"history", -replay=>1)},
6616 "history") .
6617 " | " .
6618 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
6619 "raw") .
6620 " | " .
6621 $cgi->a({-href => href(action=>"blob",
6622 hash_base=>"HEAD", file_name=>$file_name)},
6623 "HEAD");
6624 } else {
6625 $formats_nav .=
6626 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
6627 "raw");
6629 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
6630 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
6631 } else {
6632 print "<div class=\"page_nav\">\n" .
6633 "<br/><br/></div>\n" .
6634 "<div class=\"title\">".esc_html($hash)."</div>\n";
6636 git_print_page_path($file_name, "blob", $hash_base);
6637 print "<div class=\"page_body\">\n";
6638 if ($mimetype =~ m!^image/!) {
6639 print qq!<img type="!.esc_attr($mimetype).qq!"!;
6640 if ($file_name) {
6641 print qq! alt="!.esc_attr($file_name).qq!" title="!.esc_attr($file_name).qq!"!;
6643 print qq! src="! .
6644 href(action=>"blob_plain", hash=>$hash,
6645 hash_base=>$hash_base, file_name=>$file_name) .
6646 qq!" />\n!;
6647 } else {
6648 my $nr;
6649 while (my $line = <$fd>) {
6650 chomp $line;
6651 $nr++;
6652 $line = untabify($line);
6653 printf qq!<div class="pre"><a id="l%i" href="%s#l%i" class="linenr">%4i</a> %s</div>\n!,
6654 $nr, esc_attr(href(-replay => 1)), $nr, $nr,
6655 $syntax ? sanitize($line) : esc_html($line, -nbsp=>1);
6658 close $fd
6659 or print "Reading blob failed.\n";
6660 print "</div>";
6661 git_footer_html();
6664 sub git_tree {
6665 if (!defined $hash_base) {
6666 $hash_base = "HEAD";
6668 if (!defined $hash) {
6669 if (defined $file_name) {
6670 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
6671 } else {
6672 $hash = $hash_base;
6675 die_error(404, "No such tree") unless defined($hash);
6677 my $show_sizes = gitweb_check_feature('show-sizes');
6678 my $have_blame = gitweb_check_feature('blame');
6680 my @entries = ();
6682 local $/ = "\0";
6683 open my $fd, "-|", git_cmd(), "ls-tree", '-z',
6684 ($show_sizes ? '-l' : ()), @extra_options, $hash
6685 or die_error(500, "Open git-ls-tree failed");
6686 @entries = map { chomp; $_ } <$fd>;
6687 close $fd
6688 or die_error(404, "Reading tree failed");
6691 my $refs = git_get_references();
6692 my $ref = format_ref_marker($refs, $hash_base);
6693 git_header_html();
6694 my $basedir = '';
6695 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
6696 my @views_nav = ();
6697 if (defined $file_name) {
6698 push @views_nav,
6699 $cgi->a({-href => href(action=>"history", -replay=>1)},
6700 "history"),
6701 $cgi->a({-href => href(action=>"tree",
6702 hash_base=>"HEAD", file_name=>$file_name)},
6703 "HEAD"),
6705 my $snapshot_links = format_snapshot_links($hash);
6706 if (defined $snapshot_links) {
6707 # FIXME: Should be available when we have no hash base as well.
6708 push @views_nav, $snapshot_links;
6710 git_print_page_nav('tree','', $hash_base, undef, undef,
6711 join(' | ', @views_nav));
6712 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
6713 } else {
6714 undef $hash_base;
6715 print "<div class=\"page_nav\">\n";
6716 print "<br/><br/></div>\n";
6717 print "<div class=\"title\">".esc_html($hash)."</div>\n";
6719 if (defined $file_name) {
6720 $basedir = $file_name;
6721 if ($basedir ne '' && substr($basedir, -1) ne '/') {
6722 $basedir .= '/';
6724 git_print_page_path($file_name, 'tree', $hash_base);
6726 print "<div class=\"page_body\">\n";
6727 print "<table class=\"tree\">\n";
6728 my $alternate = 1;
6729 # '..' (top directory) link if possible
6730 if (defined $hash_base &&
6731 defined $file_name && $file_name =~ m![^/]+$!) {
6732 if ($alternate) {
6733 print "<tr class=\"dark\">\n";
6734 } else {
6735 print "<tr class=\"light\">\n";
6737 $alternate ^= 1;
6739 my $up = $file_name;
6740 $up =~ s!/?[^/]+$!!;
6741 undef $up unless $up;
6742 # based on git_print_tree_entry
6743 print '<td class="mode">' . mode_str('040000') . "</td>\n";
6744 print '<td class="size">&nbsp;</td>'."\n" if $show_sizes;
6745 print '<td class="list">';
6746 print $cgi->a({-href => href(action=>"tree",
6747 hash_base=>$hash_base,
6748 file_name=>$up)},
6749 "..");
6750 print "</td>\n";
6751 print "<td class=\"link\"></td>\n";
6753 print "</tr>\n";
6755 foreach my $line (@entries) {
6756 my %t = parse_ls_tree_line($line, -z => 1, -l => $show_sizes);
6758 if ($alternate) {
6759 print "<tr class=\"dark\">\n";
6760 } else {
6761 print "<tr class=\"light\">\n";
6763 $alternate ^= 1;
6765 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
6767 print "</tr>\n";
6769 print "</table>\n" .
6770 "</div>";
6771 git_footer_html();
6774 sub snapshot_name {
6775 my ($project, $hash) = @_;
6777 # path/to/project.git -> project
6778 # path/to/project/.git -> project
6779 my $name = to_utf8($project);
6780 $name =~ s,([^/])/*\.git$,$1,;
6781 $name = basename($name);
6782 # sanitize name
6783 $name =~ s/[[:cntrl:]]/?/g;
6785 my $ver = $hash;
6786 if ($hash =~ /^[0-9a-fA-F]+$/) {
6787 # shorten SHA-1 hash
6788 my $full_hash = git_get_full_hash($project, $hash);
6789 if ($full_hash =~ /^$hash/ && length($hash) > 7) {
6790 $ver = git_get_short_hash($project, $hash);
6792 } elsif ($hash =~ m!^refs/tags/(.*)$!) {
6793 # tags don't need shortened SHA-1 hash
6794 $ver = $1;
6795 } else {
6796 # branches and other need shortened SHA-1 hash
6797 if ($hash =~ m!^refs/(?:heads|remotes)/(.*)$!) {
6798 $ver = $1;
6800 $ver .= '-' . git_get_short_hash($project, $hash);
6802 # in case of hierarchical branch names
6803 $ver =~ s!/!.!g;
6805 # name = project-version_string
6806 $name = "$name-$ver";
6808 return wantarray ? ($name, $name) : $name;
6811 sub git_snapshot {
6812 my $format = $input_params{'snapshot_format'};
6813 if (!@snapshot_fmts) {
6814 die_error(403, "Snapshots not allowed");
6816 # default to first supported snapshot format
6817 $format ||= $snapshot_fmts[0];
6818 if ($format !~ m/^[a-z0-9]+$/) {
6819 die_error(400, "Invalid snapshot format parameter");
6820 } elsif (!exists($known_snapshot_formats{$format})) {
6821 die_error(400, "Unknown snapshot format");
6822 } elsif ($known_snapshot_formats{$format}{'disabled'}) {
6823 die_error(403, "Snapshot format not allowed");
6824 } elsif (!grep($_ eq $format, @snapshot_fmts)) {
6825 die_error(403, "Unsupported snapshot format");
6828 my $type = git_get_type("$hash^{}");
6829 if (!$type) {
6830 die_error(404, 'Object does not exist');
6831 } elsif ($type eq 'blob') {
6832 die_error(400, 'Object is not a tree-ish');
6835 my ($name, $prefix) = snapshot_name($project, $hash);
6836 my $filename = "$name$known_snapshot_formats{$format}{'suffix'}";
6837 my $cmd = quote_command(
6838 git_cmd(), 'archive',
6839 "--format=$known_snapshot_formats{$format}{'format'}",
6840 "--prefix=$prefix/", $hash);
6841 if (exists $known_snapshot_formats{$format}{'compressor'}) {
6842 $cmd .= ' | ' . quote_command(@{$known_snapshot_formats{$format}{'compressor'}});
6845 $filename =~ s/(["\\])/\\$1/g;
6846 print $cgi->header(
6847 -type => $known_snapshot_formats{$format}{'type'},
6848 -content_disposition => 'inline; filename="' . $filename . '"',
6849 -status => '200 OK');
6851 open my $fd, "-|", $cmd
6852 or die_error(500, "Execute git-archive failed");
6853 binmode STDOUT, ':raw';
6854 print <$fd>;
6855 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
6856 close $fd;
6859 sub git_log_generic {
6860 my ($fmt_name, $body_subr, $base, $parent, $file_name, $file_hash) = @_;
6862 my $head = git_get_head_hash($project);
6863 if (!defined $base) {
6864 $base = $head;
6866 if (!defined $page) {
6867 $page = 0;
6869 my $refs = git_get_references();
6871 my $commit_hash = $base;
6872 if (defined $parent) {
6873 $commit_hash = "$parent..$base";
6875 my @commitlist =
6876 parse_commits($commit_hash, 101, (100 * $page),
6877 defined $file_name ? ($file_name, "--full-history") : ());
6879 my $ftype;
6880 if (!defined $file_hash && defined $file_name) {
6881 # some commits could have deleted file in question,
6882 # and not have it in tree, but one of them has to have it
6883 for (my $i = 0; $i < @commitlist; $i++) {
6884 $file_hash = git_get_hash_by_path($commitlist[$i]{'id'}, $file_name);
6885 last if defined $file_hash;
6888 if (defined $file_hash) {
6889 $ftype = git_get_type($file_hash);
6891 if (defined $file_name && !defined $ftype) {
6892 die_error(500, "Unknown type of object");
6894 my %co;
6895 if (defined $file_name) {
6896 %co = parse_commit($base)
6897 or die_error(404, "Unknown commit object");
6901 my $paging_nav = format_paging_nav($fmt_name, $page, $#commitlist >= 100);
6902 my $next_link = '';
6903 if ($#commitlist >= 100) {
6904 $next_link =
6905 $cgi->a({-href => href(-replay=>1, page=>$page+1),
6906 -accesskey => "n", -title => "Alt-n"}, "next");
6908 my $patch_max = gitweb_get_feature('patches');
6909 if ($patch_max && !defined $file_name) {
6910 if ($patch_max < 0 || @commitlist <= $patch_max) {
6911 $paging_nav .= " &sdot; " .
6912 $cgi->a({-href => href(action=>"patches", -replay=>1)},
6913 "patches");
6917 git_header_html();
6918 git_print_page_nav($fmt_name,'', $hash,$hash,$hash, $paging_nav);
6919 if (defined $file_name) {
6920 git_print_header_div('commit', esc_html($co{'title'}), $base);
6921 } else {
6922 git_print_header_div('summary', $project)
6924 git_print_page_path($file_name, $ftype, $hash_base)
6925 if (defined $file_name);
6927 $body_subr->(\@commitlist, 0, 99, $refs, $next_link,
6928 $file_name, $file_hash, $ftype);
6930 git_footer_html();
6933 sub git_log {
6934 git_log_generic('log', \&git_log_body,
6935 $hash, $hash_parent);
6938 sub git_commit {
6939 $hash ||= $hash_base || "HEAD";
6940 my %co = parse_commit($hash)
6941 or die_error(404, "Unknown commit object");
6943 my $parent = $co{'parent'};
6944 my $parents = $co{'parents'}; # listref
6946 # we need to prepare $formats_nav before any parameter munging
6947 my $formats_nav;
6948 if (!defined $parent) {
6949 # --root commitdiff
6950 $formats_nav .= '(initial)';
6951 } elsif (@$parents == 1) {
6952 # single parent commit
6953 $formats_nav .=
6954 '(parent: ' .
6955 $cgi->a({-href => href(action=>"commit",
6956 hash=>$parent)},
6957 esc_html(substr($parent, 0, 7))) .
6958 ')';
6959 } else {
6960 # merge commit
6961 $formats_nav .=
6962 '(merge: ' .
6963 join(' ', map {
6964 $cgi->a({-href => href(action=>"commit",
6965 hash=>$_)},
6966 esc_html(substr($_, 0, 7)));
6967 } @$parents ) .
6968 ')';
6970 if (gitweb_check_feature('patches') && @$parents <= 1) {
6971 $formats_nav .= " | " .
6972 $cgi->a({-href => href(action=>"patch", -replay=>1)},
6973 "patch");
6976 if (!defined $parent) {
6977 $parent = "--root";
6979 my @difftree;
6980 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
6981 @diff_opts,
6982 (@$parents <= 1 ? $parent : '-c'),
6983 $hash, "--"
6984 or die_error(500, "Open git-diff-tree failed");
6985 @difftree = map { chomp; $_ } <$fd>;
6986 close $fd or die_error(404, "Reading git-diff-tree failed");
6988 # non-textual hash id's can be cached
6989 my $expires;
6990 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6991 $expires = "+1d";
6993 my $refs = git_get_references();
6994 my $ref = format_ref_marker($refs, $co{'id'});
6996 git_header_html(undef, $expires);
6997 git_print_page_nav('commit', '',
6998 $hash, $co{'tree'}, $hash,
6999 $formats_nav);
7001 if (defined $co{'parent'}) {
7002 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
7003 } else {
7004 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
7006 print "<div class=\"title_text\">\n" .
7007 "<table class=\"object_header\">\n";
7008 git_print_authorship_rows(\%co);
7009 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
7010 print "<tr>" .
7011 "<td>tree</td>" .
7012 "<td class=\"sha1\">" .
7013 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
7014 class => "list"}, $co{'tree'}) .
7015 "</td>" .
7016 "<td class=\"link\">" .
7017 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
7018 "tree");
7019 my $snapshot_links = format_snapshot_links($hash);
7020 if (defined $snapshot_links) {
7021 print " | " . $snapshot_links;
7023 print "</td>" .
7024 "</tr>\n";
7026 foreach my $par (@$parents) {
7027 print "<tr>" .
7028 "<td>parent</td>" .
7029 "<td class=\"sha1\">" .
7030 $cgi->a({-href => href(action=>"commit", hash=>$par),
7031 class => "list"}, $par) .
7032 "</td>" .
7033 "<td class=\"link\">" .
7034 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
7035 " | " .
7036 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
7037 "</td>" .
7038 "</tr>\n";
7040 print "</table>".
7041 "</div>\n";
7043 print "<div class=\"page_body\">\n";
7044 git_print_log($co{'comment'});
7045 print "</div>\n";
7047 git_difftree_body(\@difftree, $hash, @$parents);
7049 git_footer_html();
7052 sub git_object {
7053 # object is defined by:
7054 # - hash or hash_base alone
7055 # - hash_base and file_name
7056 my $type;
7058 # - hash or hash_base alone
7059 if ($hash || ($hash_base && !defined $file_name)) {
7060 my $object_id = $hash || $hash_base;
7062 open my $fd, "-|", quote_command(
7063 git_cmd(), 'cat-file', '-t', $object_id) . ' 2> /dev/null'
7064 or die_error(404, "Object does not exist");
7065 $type = <$fd>;
7066 chomp $type;
7067 close $fd
7068 or die_error(404, "Object does not exist");
7070 # - hash_base and file_name
7071 } elsif ($hash_base && defined $file_name) {
7072 $file_name =~ s,/+$,,;
7074 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
7075 or die_error(404, "Base object does not exist");
7077 # here errors should not hapen
7078 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
7079 or die_error(500, "Open git-ls-tree failed");
7080 my $line = <$fd>;
7081 close $fd;
7083 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
7084 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
7085 die_error(404, "File or directory for given base does not exist");
7087 $type = $2;
7088 $hash = $3;
7089 } else {
7090 die_error(400, "Not enough information to find object");
7093 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
7094 hash=>$hash, hash_base=>$hash_base,
7095 file_name=>$file_name),
7096 -status => '302 Found');
7099 sub git_blobdiff {
7100 my $format = shift || 'html';
7101 my $diff_style = $input_params{'diff_style'} || 'inline';
7103 my $fd;
7104 my @difftree;
7105 my %diffinfo;
7106 my $expires;
7108 # preparing $fd and %diffinfo for git_patchset_body
7109 # new style URI
7110 if (defined $hash_base && defined $hash_parent_base) {
7111 if (defined $file_name) {
7112 # read raw output
7113 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
7114 $hash_parent_base, $hash_base,
7115 "--", (defined $file_parent ? $file_parent : ()), $file_name
7116 or die_error(500, "Open git-diff-tree failed");
7117 @difftree = map { chomp; $_ } <$fd>;
7118 close $fd
7119 or die_error(404, "Reading git-diff-tree failed");
7120 @difftree
7121 or die_error(404, "Blob diff not found");
7123 } elsif (defined $hash &&
7124 $hash =~ /[0-9a-fA-F]{40}/) {
7125 # try to find filename from $hash
7127 # read filtered raw output
7128 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
7129 $hash_parent_base, $hash_base, "--"
7130 or die_error(500, "Open git-diff-tree failed");
7131 @difftree =
7132 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
7133 # $hash == to_id
7134 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
7135 map { chomp; $_ } <$fd>;
7136 close $fd
7137 or die_error(404, "Reading git-diff-tree failed");
7138 @difftree
7139 or die_error(404, "Blob diff not found");
7141 } else {
7142 die_error(400, "Missing one of the blob diff parameters");
7145 if (@difftree > 1) {
7146 die_error(400, "Ambiguous blob diff specification");
7149 %diffinfo = parse_difftree_raw_line($difftree[0]);
7150 $file_parent ||= $diffinfo{'from_file'} || $file_name;
7151 $file_name ||= $diffinfo{'to_file'};
7153 $hash_parent ||= $diffinfo{'from_id'};
7154 $hash ||= $diffinfo{'to_id'};
7156 # non-textual hash id's can be cached
7157 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
7158 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
7159 $expires = '+1d';
7162 # open patch output
7163 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
7164 '-p', ($format eq 'html' ? "--full-index" : ()),
7165 $hash_parent_base, $hash_base,
7166 "--", (defined $file_parent ? $file_parent : ()), $file_name
7167 or die_error(500, "Open git-diff-tree failed");
7170 # old/legacy style URI -- not generated anymore since 1.4.3.
7171 if (!%diffinfo) {
7172 die_error('404 Not Found', "Missing one of the blob diff parameters")
7175 # header
7176 if ($format eq 'html') {
7177 my $formats_nav =
7178 $cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},
7179 "raw");
7180 $formats_nav .= diff_style_nav($diff_style);
7181 git_header_html(undef, $expires);
7182 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
7183 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
7184 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
7185 } else {
7186 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
7187 print "<div class=\"title\">".esc_html("$hash vs $hash_parent")."</div>\n";
7189 if (defined $file_name) {
7190 git_print_page_path($file_name, "blob", $hash_base);
7191 } else {
7192 print "<div class=\"page_path\"></div>\n";
7195 } elsif ($format eq 'plain') {
7196 print $cgi->header(
7197 -type => 'text/plain',
7198 -charset => 'utf-8',
7199 -expires => $expires,
7200 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
7202 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
7204 } else {
7205 die_error(400, "Unknown blobdiff format");
7208 # patch
7209 if ($format eq 'html') {
7210 print "<div class=\"page_body\">\n";
7212 git_patchset_body($fd, $diff_style,
7213 [ \%diffinfo ], $hash_base, $hash_parent_base);
7214 close $fd;
7216 print "</div>\n"; # class="page_body"
7217 git_footer_html();
7219 } else {
7220 while (my $line = <$fd>) {
7221 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
7222 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
7224 print $line;
7226 last if $line =~ m!^\+\+\+!;
7228 local $/ = undef;
7229 print <$fd>;
7230 close $fd;
7234 sub git_blobdiff_plain {
7235 git_blobdiff('plain');
7238 # assumes that it is added as later part of already existing navigation,
7239 # so it returns "| foo | bar" rather than just "foo | bar"
7240 sub diff_style_nav {
7241 my ($diff_style, $is_combined) = @_;
7242 $diff_style ||= 'inline';
7244 return "" if ($is_combined);
7246 my @styles = (inline => 'inline', 'sidebyside' => 'side by side');
7247 my %styles = @styles;
7248 @styles =
7249 @styles[ map { $_ * 2 } 0..$#styles/2 ];
7251 return join '',
7252 map { " | ".$_ }
7253 map {
7254 $_ eq $diff_style ? $styles{$_} :
7255 $cgi->a({-href => href(-replay=>1, diff_style => $_)}, $styles{$_})
7256 } @styles;
7259 sub git_commitdiff {
7260 my %params = @_;
7261 my $format = $params{-format} || 'html';
7262 my $diff_style = $input_params{'diff_style'} || 'inline';
7264 my ($patch_max) = gitweb_get_feature('patches');
7265 if ($format eq 'patch') {
7266 die_error(403, "Patch view not allowed") unless $patch_max;
7269 $hash ||= $hash_base || "HEAD";
7270 my %co = parse_commit($hash)
7271 or die_error(404, "Unknown commit object");
7273 # choose format for commitdiff for merge
7274 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
7275 $hash_parent = '--cc';
7277 # we need to prepare $formats_nav before almost any parameter munging
7278 my $formats_nav;
7279 if ($format eq 'html') {
7280 $formats_nav =
7281 $cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
7282 "raw");
7283 if ($patch_max && @{$co{'parents'}} <= 1) {
7284 $formats_nav .= " | " .
7285 $cgi->a({-href => href(action=>"patch", -replay=>1)},
7286 "patch");
7288 $formats_nav .= diff_style_nav($diff_style, @{$co{'parents'}} > 1);
7290 if (defined $hash_parent &&
7291 $hash_parent ne '-c' && $hash_parent ne '--cc') {
7292 # commitdiff with two commits given
7293 my $hash_parent_short = $hash_parent;
7294 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
7295 $hash_parent_short = substr($hash_parent, 0, 7);
7297 $formats_nav .=
7298 ' (from';
7299 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
7300 if ($co{'parents'}[$i] eq $hash_parent) {
7301 $formats_nav .= ' parent ' . ($i+1);
7302 last;
7305 $formats_nav .= ': ' .
7306 $cgi->a({-href => href(-replay=>1,
7307 hash=>$hash_parent, hash_base=>undef)},
7308 esc_html($hash_parent_short)) .
7309 ')';
7310 } elsif (!$co{'parent'}) {
7311 # --root commitdiff
7312 $formats_nav .= ' (initial)';
7313 } elsif (scalar @{$co{'parents'}} == 1) {
7314 # single parent commit
7315 $formats_nav .=
7316 ' (parent: ' .
7317 $cgi->a({-href => href(-replay=>1,
7318 hash=>$co{'parent'}, hash_base=>undef)},
7319 esc_html(substr($co{'parent'}, 0, 7))) .
7320 ')';
7321 } else {
7322 # merge commit
7323 if ($hash_parent eq '--cc') {
7324 $formats_nav .= ' | ' .
7325 $cgi->a({-href => href(-replay=>1,
7326 hash=>$hash, hash_parent=>'-c')},
7327 'combined');
7328 } else { # $hash_parent eq '-c'
7329 $formats_nav .= ' | ' .
7330 $cgi->a({-href => href(-replay=>1,
7331 hash=>$hash, hash_parent=>'--cc')},
7332 'compact');
7334 $formats_nav .=
7335 ' (merge: ' .
7336 join(' ', map {
7337 $cgi->a({-href => href(-replay=>1,
7338 hash=>$_, hash_base=>undef)},
7339 esc_html(substr($_, 0, 7)));
7340 } @{$co{'parents'}} ) .
7341 ')';
7345 my $hash_parent_param = $hash_parent;
7346 if (!defined $hash_parent_param) {
7347 # --cc for multiple parents, --root for parentless
7348 $hash_parent_param =
7349 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
7352 # read commitdiff
7353 my $fd;
7354 my @difftree;
7355 if ($format eq 'html') {
7356 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
7357 "--no-commit-id", "--patch-with-raw", "--full-index",
7358 $hash_parent_param, $hash, "--"
7359 or die_error(500, "Open git-diff-tree failed");
7361 while (my $line = <$fd>) {
7362 chomp $line;
7363 # empty line ends raw part of diff-tree output
7364 last unless $line;
7365 push @difftree, scalar parse_difftree_raw_line($line);
7368 } elsif ($format eq 'plain') {
7369 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
7370 '-p', $hash_parent_param, $hash, "--"
7371 or die_error(500, "Open git-diff-tree failed");
7372 } elsif ($format eq 'patch') {
7373 # For commit ranges, we limit the output to the number of
7374 # patches specified in the 'patches' feature.
7375 # For single commits, we limit the output to a single patch,
7376 # diverging from the git-format-patch default.
7377 my @commit_spec = ();
7378 if ($hash_parent) {
7379 if ($patch_max > 0) {
7380 push @commit_spec, "-$patch_max";
7382 push @commit_spec, '-n', "$hash_parent..$hash";
7383 } else {
7384 if ($params{-single}) {
7385 push @commit_spec, '-1';
7386 } else {
7387 if ($patch_max > 0) {
7388 push @commit_spec, "-$patch_max";
7390 push @commit_spec, "-n";
7392 push @commit_spec, '--root', $hash;
7394 open $fd, "-|", git_cmd(), "format-patch", @diff_opts,
7395 '--encoding=utf8', '--stdout', @commit_spec
7396 or die_error(500, "Open git-format-patch failed");
7397 } else {
7398 die_error(400, "Unknown commitdiff format");
7401 # non-textual hash id's can be cached
7402 my $expires;
7403 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
7404 $expires = "+1d";
7407 # write commit message
7408 if ($format eq 'html') {
7409 my $refs = git_get_references();
7410 my $ref = format_ref_marker($refs, $co{'id'});
7412 git_header_html(undef, $expires);
7413 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
7414 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
7415 print "<div class=\"title_text\">\n" .
7416 "<table class=\"object_header\">\n";
7417 git_print_authorship_rows(\%co);
7418 print "</table>".
7419 "</div>\n";
7420 print "<div class=\"page_body\">\n";
7421 if (@{$co{'comment'}} > 1) {
7422 print "<div class=\"log\">\n";
7423 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
7424 print "</div>\n"; # class="log"
7427 } elsif ($format eq 'plain') {
7428 my $refs = git_get_references("tags");
7429 my $tagname = git_get_rev_name_tags($hash);
7430 my $filename = basename($project) . "-$hash.patch";
7432 print $cgi->header(
7433 -type => 'text/plain',
7434 -charset => 'utf-8',
7435 -expires => $expires,
7436 -content_disposition => 'inline; filename="' . "$filename" . '"');
7437 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
7438 print "From: " . to_utf8($co{'author'}) . "\n";
7439 print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
7440 print "Subject: " . to_utf8($co{'title'}) . "\n";
7442 print "X-Git-Tag: $tagname\n" if $tagname;
7443 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
7445 foreach my $line (@{$co{'comment'}}) {
7446 print to_utf8($line) . "\n";
7448 print "---\n\n";
7449 } elsif ($format eq 'patch') {
7450 my $filename = basename($project) . "-$hash.patch";
7452 print $cgi->header(
7453 -type => 'text/plain',
7454 -charset => 'utf-8',
7455 -expires => $expires,
7456 -content_disposition => 'inline; filename="' . "$filename" . '"');
7459 # write patch
7460 if ($format eq 'html') {
7461 my $use_parents = !defined $hash_parent ||
7462 $hash_parent eq '-c' || $hash_parent eq '--cc';
7463 git_difftree_body(\@difftree, $hash,
7464 $use_parents ? @{$co{'parents'}} : $hash_parent);
7465 print "<br/>\n";
7467 git_patchset_body($fd, $diff_style,
7468 \@difftree, $hash,
7469 $use_parents ? @{$co{'parents'}} : $hash_parent);
7470 close $fd;
7471 print "</div>\n"; # class="page_body"
7472 git_footer_html();
7474 } elsif ($format eq 'plain') {
7475 local $/ = undef;
7476 print <$fd>;
7477 close $fd
7478 or print "Reading git-diff-tree failed\n";
7479 } elsif ($format eq 'patch') {
7480 local $/ = undef;
7481 print <$fd>;
7482 close $fd
7483 or print "Reading git-format-patch failed\n";
7487 sub git_commitdiff_plain {
7488 git_commitdiff(-format => 'plain');
7491 # format-patch-style patches
7492 sub git_patch {
7493 git_commitdiff(-format => 'patch', -single => 1);
7496 sub git_patches {
7497 git_commitdiff(-format => 'patch');
7500 sub git_history {
7501 git_log_generic('history', \&git_history_body,
7502 $hash_base, $hash_parent_base,
7503 $file_name, $hash);
7506 sub git_search {
7507 $searchtype ||= 'commit';
7509 # check if appropriate features are enabled
7510 gitweb_check_feature('search')
7511 or die_error(403, "Search is disabled");
7512 if ($searchtype eq 'pickaxe') {
7513 # pickaxe may take all resources of your box and run for several minutes
7514 # with every query - so decide by yourself how public you make this feature
7515 gitweb_check_feature('pickaxe')
7516 or die_error(403, "Pickaxe search is disabled");
7518 if ($searchtype eq 'grep') {
7519 # grep search might be potentially CPU-intensive, too
7520 gitweb_check_feature('grep')
7521 or die_error(403, "Grep search is disabled");
7524 if (!defined $searchtext) {
7525 die_error(400, "Text field is empty");
7527 if (!defined $hash) {
7528 $hash = git_get_head_hash($project);
7530 my %co = parse_commit($hash);
7531 if (!%co) {
7532 die_error(404, "Unknown commit object");
7534 if (!defined $page) {
7535 $page = 0;
7538 if ($searchtype eq 'commit' ||
7539 $searchtype eq 'author' ||
7540 $searchtype eq 'committer') {
7541 git_search_message(%co);
7542 } elsif ($searchtype eq 'pickaxe') {
7543 git_search_changes(%co);
7544 } elsif ($searchtype eq 'grep') {
7545 git_search_files(%co);
7546 } else {
7547 die_error(400, "Unknown search type");
7551 sub git_search_help {
7552 git_header_html();
7553 git_print_page_nav('','', $hash,$hash,$hash);
7554 print <<EOT;
7555 <p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without
7556 regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,
7557 the pattern entered is recognized as the POSIX extended
7558 <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case
7559 insensitive).</p>
7560 <dl>
7561 <dt><b>commit</b></dt>
7562 <dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
7564 my $have_grep = gitweb_check_feature('grep');
7565 if ($have_grep) {
7566 print <<EOT;
7567 <dt><b>grep</b></dt>
7568 <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
7569 a different one) are searched for the given pattern. On large trees, this search can take
7570 a while and put some strain on the server, so please use it with some consideration. Note that
7571 due to git-grep peculiarity, currently if regexp mode is turned off, the matches are
7572 case-sensitive.</dd>
7575 print <<EOT;
7576 <dt><b>author</b></dt>
7577 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>
7578 <dt><b>committer</b></dt>
7579 <dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>
7581 my $have_pickaxe = gitweb_check_feature('pickaxe');
7582 if ($have_pickaxe) {
7583 print <<EOT;
7584 <dt><b>pickaxe</b></dt>
7585 <dd>All commits that caused the string to appear or disappear from any file (changes that
7586 added, removed or "modified" the string) will be listed. This search can take a while and
7587 takes a lot of strain on the server, so please use it wisely. Note that since you may be
7588 interested even in changes just changing the case as well, this search is case sensitive.</dd>
7591 print "</dl>\n";
7592 git_footer_html();
7595 sub git_shortlog {
7596 git_log_generic('shortlog', \&git_shortlog_body,
7597 $hash, $hash_parent);
7600 ## ......................................................................
7601 ## feeds (RSS, Atom; OPML)
7603 sub git_feed {
7604 my $format = shift || 'atom';
7605 my $have_blame = gitweb_check_feature('blame');
7607 # Atom: http://www.atomenabled.org/developers/syndication/
7608 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
7609 if ($format ne 'rss' && $format ne 'atom') {
7610 die_error(400, "Unknown web feed format");
7613 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
7614 my $head = $hash || 'HEAD';
7615 my @commitlist = parse_commits($head, 150, 0, $file_name);
7617 my %latest_commit;
7618 my %latest_date;
7619 my $content_type = "application/$format+xml";
7620 if (defined $cgi->http('HTTP_ACCEPT') &&
7621 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
7622 # browser (feed reader) prefers text/xml
7623 $content_type = 'text/xml';
7625 if (defined($commitlist[0])) {
7626 %latest_commit = %{$commitlist[0]};
7627 my $latest_epoch = $latest_commit{'committer_epoch'};
7628 %latest_date = parse_date($latest_epoch, $latest_commit{'comitter_tz'});
7629 my $if_modified = $cgi->http('IF_MODIFIED_SINCE');
7630 if (defined $if_modified) {
7631 my $since;
7632 if (eval { require HTTP::Date; 1; }) {
7633 $since = HTTP::Date::str2time($if_modified);
7634 } elsif (eval { require Time::ParseDate; 1; }) {
7635 $since = Time::ParseDate::parsedate($if_modified, GMT => 1);
7637 if (defined $since && $latest_epoch <= $since) {
7638 print $cgi->header(
7639 -type => $content_type,
7640 -charset => 'utf-8',
7641 -last_modified => $latest_date{'rfc2822'},
7642 -status => '304 Not Modified');
7643 return;
7646 print $cgi->header(
7647 -type => $content_type,
7648 -charset => 'utf-8',
7649 -last_modified => $latest_date{'rfc2822'});
7650 } else {
7651 print $cgi->header(
7652 -type => $content_type,
7653 -charset => 'utf-8');
7656 # Optimization: skip generating the body if client asks only
7657 # for Last-Modified date.
7658 return if ($cgi->request_method() eq 'HEAD');
7660 # header variables
7661 my $title = "$site_name - $project/$action";
7662 my $feed_type = 'log';
7663 if (defined $hash) {
7664 $title .= " - '$hash'";
7665 $feed_type = 'branch log';
7666 if (defined $file_name) {
7667 $title .= " :: $file_name";
7668 $feed_type = 'history';
7670 } elsif (defined $file_name) {
7671 $title .= " - $file_name";
7672 $feed_type = 'history';
7674 $title .= " $feed_type";
7675 my $descr = git_get_project_description($project);
7676 if (defined $descr) {
7677 $descr = esc_html($descr);
7678 } else {
7679 $descr = "$project " .
7680 ($format eq 'rss' ? 'RSS' : 'Atom') .
7681 " feed";
7683 my $owner = git_get_project_owner($project);
7684 $owner = esc_html($owner);
7686 #header
7687 my $alt_url;
7688 if (defined $file_name) {
7689 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
7690 } elsif (defined $hash) {
7691 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
7692 } else {
7693 $alt_url = href(-full=>1, action=>"summary");
7695 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
7696 if ($format eq 'rss') {
7697 print <<XML;
7698 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
7699 <channel>
7701 print "<title>$title</title>\n" .
7702 "<link>$alt_url</link>\n" .
7703 "<description>$descr</description>\n" .
7704 "<language>en</language>\n" .
7705 # project owner is responsible for 'editorial' content
7706 "<managingEditor>$owner</managingEditor>\n";
7707 if (defined $logo || defined $favicon) {
7708 # prefer the logo to the favicon, since RSS
7709 # doesn't allow both
7710 my $img = esc_url($logo || $favicon);
7711 print "<image>\n" .
7712 "<url>$img</url>\n" .
7713 "<title>$title</title>\n" .
7714 "<link>$alt_url</link>\n" .
7715 "</image>\n";
7717 if (%latest_date) {
7718 print "<pubDate>$latest_date{'rfc2822'}</pubDate>\n";
7719 print "<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";
7721 print "<generator>gitweb v.$version/$git_version</generator>\n";
7722 } elsif ($format eq 'atom') {
7723 print <<XML;
7724 <feed xmlns="http://www.w3.org/2005/Atom">
7726 print "<title>$title</title>\n" .
7727 "<subtitle>$descr</subtitle>\n" .
7728 '<link rel="alternate" type="text/html" href="' .
7729 $alt_url . '" />' . "\n" .
7730 '<link rel="self" type="' . $content_type . '" href="' .
7731 $cgi->self_url() . '" />' . "\n" .
7732 "<id>" . href(-full=>1) . "</id>\n" .
7733 # use project owner for feed author
7734 "<author><name>$owner</name></author>\n";
7735 if (defined $favicon) {
7736 print "<icon>" . esc_url($favicon) . "</icon>\n";
7738 if (defined $logo) {
7739 # not twice as wide as tall: 72 x 27 pixels
7740 print "<logo>" . esc_url($logo) . "</logo>\n";
7742 if (! %latest_date) {
7743 # dummy date to keep the feed valid until commits trickle in:
7744 print "<updated>1970-01-01T00:00:00Z</updated>\n";
7745 } else {
7746 print "<updated>$latest_date{'iso-8601'}</updated>\n";
7748 print "<generator version='$version/$git_version'>gitweb</generator>\n";
7751 # contents
7752 for (my $i = 0; $i <= $#commitlist; $i++) {
7753 my %co = %{$commitlist[$i]};
7754 my $commit = $co{'id'};
7755 # we read 150, we always show 30 and the ones more recent than 48 hours
7756 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
7757 last;
7759 my %cd = parse_date($co{'author_epoch'}, $co{'author_tz'});
7761 # get list of changed files
7762 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
7763 $co{'parent'} || "--root",
7764 $co{'id'}, "--", (defined $file_name ? $file_name : ())
7765 or next;
7766 my @difftree = map { chomp; $_ } <$fd>;
7767 close $fd
7768 or next;
7770 # print element (entry, item)
7771 my $co_url = href(-full=>1, action=>"commitdiff", hash=>$commit);
7772 if ($format eq 'rss') {
7773 print "<item>\n" .
7774 "<title>" . esc_html($co{'title'}) . "</title>\n" .
7775 "<author>" . esc_html($co{'author'}) . "</author>\n" .
7776 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
7777 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
7778 "<link>$co_url</link>\n" .
7779 "<description>" . esc_html($co{'title'}) . "</description>\n" .
7780 "<content:encoded>" .
7781 "<![CDATA[\n";
7782 } elsif ($format eq 'atom') {
7783 print "<entry>\n" .
7784 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
7785 "<updated>$cd{'iso-8601'}</updated>\n" .
7786 "<author>\n" .
7787 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
7788 if ($co{'author_email'}) {
7789 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
7791 print "</author>\n" .
7792 # use committer for contributor
7793 "<contributor>\n" .
7794 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
7795 if ($co{'committer_email'}) {
7796 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
7798 print "</contributor>\n" .
7799 "<published>$cd{'iso-8601'}</published>\n" .
7800 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
7801 "<id>$co_url</id>\n" .
7802 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
7803 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
7805 my $comment = $co{'comment'};
7806 print "<pre>\n";
7807 foreach my $line (@$comment) {
7808 $line = esc_html($line);
7809 print "$line\n";
7811 print "</pre><ul>\n";
7812 foreach my $difftree_line (@difftree) {
7813 my %difftree = parse_difftree_raw_line($difftree_line);
7814 next if !$difftree{'from_id'};
7816 my $file = $difftree{'file'} || $difftree{'to_file'};
7818 print "<li>" .
7819 "[" .
7820 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
7821 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
7822 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
7823 file_name=>$file, file_parent=>$difftree{'from_file'}),
7824 -title => "diff"}, 'D');
7825 if ($have_blame) {
7826 print $cgi->a({-href => href(-full=>1, action=>"blame",
7827 file_name=>$file, hash_base=>$commit),
7828 -title => "blame"}, 'B');
7830 # if this is not a feed of a file history
7831 if (!defined $file_name || $file_name ne $file) {
7832 print $cgi->a({-href => href(-full=>1, action=>"history",
7833 file_name=>$file, hash=>$commit),
7834 -title => "history"}, 'H');
7836 $file = esc_path($file);
7837 print "] ".
7838 "$file</li>\n";
7840 if ($format eq 'rss') {
7841 print "</ul>]]>\n" .
7842 "</content:encoded>\n" .
7843 "</item>\n";
7844 } elsif ($format eq 'atom') {
7845 print "</ul>\n</div>\n" .
7846 "</content>\n" .
7847 "</entry>\n";
7851 # end of feed
7852 if ($format eq 'rss') {
7853 print "</channel>\n</rss>\n";
7854 } elsif ($format eq 'atom') {
7855 print "</feed>\n";
7859 sub git_rss {
7860 git_feed('rss');
7863 sub git_atom {
7864 git_feed('atom');
7867 sub git_opml {
7868 my @list = git_get_projects_list();
7869 if (!@list) {
7870 die_error(404, "No projects found");
7873 print $cgi->header(
7874 -type => 'text/xml',
7875 -charset => 'utf-8',
7876 -content_disposition => 'inline; filename="opml.xml"');
7878 my $title = esc_html($site_name);
7879 print <<XML;
7880 <?xml version="1.0" encoding="utf-8"?>
7881 <opml version="1.0">
7882 <head>
7883 <title>$title OPML Export</title>
7884 </head>
7885 <body>
7886 <outline text="git RSS feeds">
7889 foreach my $pr (@list) {
7890 my %proj = %$pr;
7891 my $head = git_get_head_hash($proj{'path'});
7892 if (!defined $head) {
7893 next;
7895 $git_dir = "$projectroot/$proj{'path'}";
7896 my %co = parse_commit($head);
7897 if (!%co) {
7898 next;
7901 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
7902 my $rss = href('project' => $proj{'path'}, 'action' => 'rss', -full => 1);
7903 my $html = href('project' => $proj{'path'}, 'action' => 'summary', -full => 1);
7904 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
7906 print <<XML;
7907 </outline>
7908 </body>
7909 </opml>