Merge branch 't/misc/no-stale-vars' into refs/top-bases/t/projlist-cache/caching
[git/gitweb.git] / gitweb / gitweb.perl
blobaa8bd3230a5cf5319bb82f4ef4205e6322ba2a3f
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 use Time::Local;
22 binmode STDOUT, ':utf8';
24 if (!defined($CGI::VERSION) || $CGI::VERSION < 4.08) {
25 eval 'sub CGI::multi_param { CGI::param(@_) }'
28 our $t0 = [ gettimeofday() ];
29 our $number_of_git_cmds = 0;
31 BEGIN {
32 CGI->compile() if $ENV{'MOD_PERL'};
35 our $version = "++GIT_VERSION++";
37 our ($my_url, $my_uri, $base_url, $path_info, $home_link);
38 sub evaluate_uri {
39 our $cgi;
41 our $my_url = $cgi->url();
42 our $my_uri = $cgi->url(-absolute => 1);
44 # Base URL for relative URLs in gitweb ($logo, $favicon, ...),
45 # needed and used only for URLs with nonempty PATH_INFO
46 our $base_url = $my_url;
48 # When the script is used as DirectoryIndex, the URL does not contain the name
49 # of the script file itself, and $cgi->url() fails to strip PATH_INFO, so we
50 # have to do it ourselves. We make $path_info global because it's also used
51 # later on.
53 # Another issue with the script being the DirectoryIndex is that the resulting
54 # $my_url data is not the full script URL: this is good, because we want
55 # generated links to keep implying the script name if it wasn't explicitly
56 # indicated in the URL we're handling, but it means that $my_url cannot be used
57 # as base URL.
58 # Therefore, if we needed to strip PATH_INFO, then we know that we have
59 # to build the base URL ourselves:
60 our $path_info = decode_utf8($ENV{"PATH_INFO"});
61 if ($path_info) {
62 # $path_info has already been URL-decoded by the web server, but
63 # $my_url and $my_uri have not. URL-decode them so we can properly
64 # strip $path_info.
65 $my_url = unescape($my_url);
66 $my_uri = unescape($my_uri);
67 if ($my_url =~ s,\Q$path_info\E$,, &&
68 $my_uri =~ s,\Q$path_info\E$,, &&
69 defined $ENV{'SCRIPT_NAME'}) {
70 $base_url = $cgi->url(-base => 1) . $ENV{'SCRIPT_NAME'};
74 # target of the home link on top of all pages
75 our $home_link = $my_uri || "/";
78 # core git executable to use
79 # this can just be "git" if your webserver has a sensible PATH
80 our $GIT = "++GIT_BINDIR++/git";
82 # absolute fs-path which will be prepended to the project path
83 #our $projectroot = "/pub/scm";
84 our $projectroot = "++GITWEB_PROJECTROOT++";
86 # fs traversing limit for getting project list
87 # the number is relative to the projectroot
88 our $project_maxdepth = "++GITWEB_PROJECT_MAXDEPTH++";
90 # string of the home link on top of all pages
91 our $home_link_str = "++GITWEB_HOME_LINK_STR++";
93 # extra breadcrumbs preceding the home link
94 our @extra_breadcrumbs = ();
96 # name of your site or organization to appear in page titles
97 # replace this with something more descriptive for clearer bookmarks
98 our $site_name = "++GITWEB_SITENAME++"
99 || ($ENV{'SERVER_NAME'} || "Untitled") . " Git";
101 # html snippet to include in the <head> section of each page
102 our $site_html_head_string = "++GITWEB_SITE_HTML_HEAD_STRING++";
103 # filename of html text to include at top of each page
104 our $site_header = "++GITWEB_SITE_HEADER++";
105 # html text to include at home page
106 our $home_text = "++GITWEB_HOMETEXT++";
107 # filename of html text to include at bottom of each page
108 our $site_footer = "++GITWEB_SITE_FOOTER++";
110 # URI of stylesheets
111 our @stylesheets = ("++GITWEB_CSS++");
112 # URI of a single stylesheet, which can be overridden in GITWEB_CONFIG.
113 our $stylesheet = undef;
114 # URI of GIT logo (72x27 size)
115 our $logo = "++GITWEB_LOGO++";
116 # URI of GIT favicon, assumed to be image/png type
117 our $favicon = "++GITWEB_FAVICON++";
118 # URI of gitweb.js (JavaScript code for gitweb)
119 our $javascript = "++GITWEB_JS++";
121 # URI and label (title) of GIT logo link
122 #our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
123 #our $logo_label = "git documentation";
124 our $logo_url = "http://git-scm.com/";
125 our $logo_label = "git homepage";
127 # source of projects list
128 our $projects_list = "++GITWEB_LIST++";
130 # the width (in characters) of the projects list "Description" column
131 our $projects_list_description_width = 25;
133 # group projects by category on the projects list
134 # (enabled if this variable evaluates to true)
135 our $projects_list_group_categories = 0;
137 # default category if none specified
138 # (leave the empty string for no category)
139 our $project_list_default_category = "";
141 # default order of projects list
142 # valid values are none, project, descr, owner, and age
143 our $default_projects_order = "project";
145 # show repository only if this file exists
146 # (only effective if this variable evaluates to true)
147 our $export_ok = "++GITWEB_EXPORT_OK++";
149 # don't generate age column on the projects list page
150 our $omit_age_column = 0;
152 # use contents of this file (in iso, iso-strict or raw format) as
153 # the last activity data if it exists and is a valid date
154 our $lastactivity_file = undef;
156 # don't generate information about owners of repositories
157 our $omit_owner=0;
159 # show repository only if this subroutine returns true
160 # when given the path to the project, for example:
161 # sub { return -e "$_[0]/git-daemon-export-ok"; }
162 our $export_auth_hook = undef;
164 # only allow viewing of repositories also shown on the overview page
165 our $strict_export = "++GITWEB_STRICT_EXPORT++";
167 # list of git base URLs used for URL to where fetch project from,
168 # i.e. full URL is "$git_base_url/$project"
169 our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
171 # default blob_plain mimetype and default charset for text/plain blob
172 our $default_blob_plain_mimetype = 'text/plain';
173 our $default_text_plain_charset = undef;
175 # file to use for guessing MIME types before trying /etc/mime.types
176 # (relative to the current git repository)
177 our $mimetypes_file = undef;
179 # assume this charset if line contains non-UTF-8 characters;
180 # it should be valid encoding (see Encoding::Supported(3pm) for list),
181 # for which encoding all byte sequences are valid, for example
182 # 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
183 # could be even 'utf-8' for the old behavior)
184 our $fallback_encoding = 'latin1';
186 # rename detection options for git-diff and git-diff-tree
187 # - default is '-M', with the cost proportional to
188 # (number of removed files) * (number of new files).
189 # - more costly is '-C' (which implies '-M'), with the cost proportional to
190 # (number of changed files + number of removed files) * (number of new files)
191 # - even more costly is '-C', '--find-copies-harder' with cost
192 # (number of files in the original tree) * (number of new files)
193 # - one might want to include '-B' option, e.g. '-B', '-M'
194 our @diff_opts = ('-M'); # taken from git_commit
196 # Disables features that would allow repository owners to inject script into
197 # the gitweb domain.
198 our $prevent_xss = 0;
200 # Path to the highlight executable to use (must be the one from
201 # http://www.andre-simon.de due to assumptions about parameters and output).
202 # Useful if highlight is not installed on your webserver's PATH.
203 # [Default: highlight]
204 our $highlight_bin = "++HIGHLIGHT_BIN++";
206 # Whether to include project list on the gitweb front page; 0 means yes,
207 # 1 means no list but show tag cloud if enabled (all projects still need
208 # to be scanned), 2 means no list and no tag cloud (very fast)
209 our $frontpage_no_project_list = 0;
211 # information about snapshot formats that gitweb is capable of serving
212 our %known_snapshot_formats = (
213 # name => {
214 # 'display' => display name,
215 # 'type' => mime type,
216 # 'suffix' => filename suffix,
217 # 'format' => --format for git-archive,
218 # 'compressor' => [compressor command and arguments]
219 # (array reference, optional)
220 # 'disabled' => boolean (optional)}
222 'tgz' => {
223 'display' => 'tar.gz',
224 'type' => 'application/x-gzip',
225 'suffix' => '.tar.gz',
226 'format' => 'tar',
227 'compressor' => ['gzip', '-n']},
229 'tbz2' => {
230 'display' => 'tar.bz2',
231 'type' => 'application/x-bzip2',
232 'suffix' => '.tar.bz2',
233 'format' => 'tar',
234 'compressor' => ['bzip2']},
236 'txz' => {
237 'display' => 'tar.xz',
238 'type' => 'application/x-xz',
239 'suffix' => '.tar.xz',
240 'format' => 'tar',
241 'compressor' => ['xz'],
242 'disabled' => 1},
244 'zip' => {
245 'display' => 'zip',
246 'type' => 'application/x-zip',
247 'suffix' => '.zip',
248 'format' => 'zip'},
251 # Aliases so we understand old gitweb.snapshot values in repository
252 # configuration.
253 our %known_snapshot_format_aliases = (
254 'gzip' => 'tgz',
255 'bzip2' => 'tbz2',
256 'xz' => 'txz',
258 # backward compatibility: legacy gitweb config support
259 'x-gzip' => undef, 'gz' => undef,
260 'x-bzip2' => undef, 'bz2' => undef,
261 'x-zip' => undef, '' => undef,
264 # Pixel sizes for icons and avatars. If the default font sizes or lineheights
265 # are changed, it may be appropriate to change these values too via
266 # $GITWEB_CONFIG.
267 our %avatar_size = (
268 'default' => 16,
269 'double' => 32
272 # Used to set the maximum load that we will still respond to gitweb queries.
273 # If server load exceed this value then return "503 server busy" error.
274 # If gitweb cannot determined server load, it is taken to be 0.
275 # Leave it undefined (or set to 'undef') to turn off load checking.
276 our $maxload = 300;
278 # configuration for 'highlight' (http://www.andre-simon.de/)
279 # match by basename
280 our %highlight_basename = (
281 #'Program' => 'py',
282 #'Library' => 'py',
283 'SConstruct' => 'py', # SCons equivalent of Makefile
284 'Makefile' => 'make',
286 # match by extension
287 our %highlight_ext = (
288 # main extensions, defining name of syntax;
289 # see files in /usr/share/highlight/langDefs/ directory
290 (map { $_ => $_ } qw(py rb java css js tex bib xml awk bat ini spec tcl sql)),
291 # alternate extensions, see /etc/highlight/filetypes.conf
292 (map { $_ => 'c' } qw(c h)),
293 (map { $_ => 'sh' } qw(sh bash zsh ksh)),
294 (map { $_ => 'cpp' } qw(cpp cxx c++ cc)),
295 (map { $_ => 'php' } qw(php php3 php4 php5 phps)),
296 (map { $_ => 'pl' } qw(pl perl pm)), # perhaps also 'cgi'
297 (map { $_ => 'make'} qw(make mak mk)),
298 (map { $_ => 'xml' } qw(xml xhtml html htm)),
301 # You define site-wide feature defaults here; override them with
302 # $GITWEB_CONFIG as necessary.
303 our %feature = (
304 # feature => {
305 # 'sub' => feature-sub (subroutine),
306 # 'override' => allow-override (boolean),
307 # 'default' => [ default options...] (array reference)}
309 # if feature is overridable (it means that allow-override has true value),
310 # then feature-sub will be called with default options as parameters;
311 # return value of feature-sub indicates if to enable specified feature
313 # if there is no 'sub' key (no feature-sub), then feature cannot be
314 # overridden
316 # use gitweb_get_feature(<feature>) to retrieve the <feature> value
317 # (an array) or gitweb_check_feature(<feature>) to check if <feature>
318 # is enabled
320 # Enable the 'blame' blob view, showing the last commit that modified
321 # each line in the file. This can be very CPU-intensive.
323 # To enable system wide have in $GITWEB_CONFIG
324 # $feature{'blame'}{'default'} = [1];
325 # To have project specific config enable override in $GITWEB_CONFIG
326 # $feature{'blame'}{'override'} = 1;
327 # and in project config gitweb.blame = 0|1;
328 'blame' => {
329 'sub' => sub { feature_bool('blame', @_) },
330 'override' => 0,
331 'default' => [0]},
333 # Enable the 'snapshot' link, providing a compressed archive of any
334 # tree. This can potentially generate high traffic if you have large
335 # project.
337 # Value is a list of formats defined in %known_snapshot_formats that
338 # you wish to offer.
339 # To disable system wide have in $GITWEB_CONFIG
340 # $feature{'snapshot'}{'default'} = [];
341 # To have project specific config enable override in $GITWEB_CONFIG
342 # $feature{'snapshot'}{'override'} = 1;
343 # and in project config, a comma-separated list of formats or "none"
344 # to disable. Example: gitweb.snapshot = tbz2,zip;
345 'snapshot' => {
346 'sub' => \&feature_snapshot,
347 'override' => 0,
348 'default' => ['tgz']},
350 # Enable text search, which will list the commits which match author,
351 # committer or commit text to a given string. Enabled by default.
352 # Project specific override is not supported.
354 # Note that this controls all search features, which means that if
355 # it is disabled, then 'grep' and 'pickaxe' search would also be
356 # disabled.
357 'search' => {
358 'override' => 0,
359 'default' => [1]},
361 # Enable grep search, which will list the files in currently selected
362 # tree containing the given string. Enabled by default. This can be
363 # potentially CPU-intensive, of course.
364 # Note that you need to have 'search' feature enabled too.
366 # To enable system wide have in $GITWEB_CONFIG
367 # $feature{'grep'}{'default'} = [1];
368 # To have project specific config enable override in $GITWEB_CONFIG
369 # $feature{'grep'}{'override'} = 1;
370 # and in project config gitweb.grep = 0|1;
371 'grep' => {
372 'sub' => sub { feature_bool('grep', @_) },
373 'override' => 0,
374 'default' => [1]},
376 # Enable the pickaxe search, which will list the commits that modified
377 # a given string in a file. This can be practical and quite faster
378 # alternative to 'blame', but still potentially CPU-intensive.
379 # Note that you need to have 'search' feature enabled too.
381 # To enable system wide have in $GITWEB_CONFIG
382 # $feature{'pickaxe'}{'default'} = [1];
383 # To have project specific config enable override in $GITWEB_CONFIG
384 # $feature{'pickaxe'}{'override'} = 1;
385 # and in project config gitweb.pickaxe = 0|1;
386 'pickaxe' => {
387 'sub' => sub { feature_bool('pickaxe', @_) },
388 'override' => 0,
389 'default' => [1]},
391 # Enable showing size of blobs in a 'tree' view, in a separate
392 # column, similar to what 'ls -l' does. This cost a bit of IO.
394 # To disable system wide have in $GITWEB_CONFIG
395 # $feature{'show-sizes'}{'default'} = [0];
396 # To have project specific config enable override in $GITWEB_CONFIG
397 # $feature{'show-sizes'}{'override'} = 1;
398 # and in project config gitweb.showsizes = 0|1;
399 'show-sizes' => {
400 'sub' => sub { feature_bool('showsizes', @_) },
401 'override' => 0,
402 'default' => [1]},
404 # Make gitweb use an alternative format of the URLs which can be
405 # more readable and natural-looking: project name is embedded
406 # directly in the path and the query string contains other
407 # auxiliary information. All gitweb installations recognize
408 # URL in either format; this configures in which formats gitweb
409 # generates links.
411 # To enable system wide have in $GITWEB_CONFIG
412 # $feature{'pathinfo'}{'default'} = [1];
413 # Project specific override is not supported.
415 # Note that you will need to change the default location of CSS,
416 # favicon, logo and possibly other files to an absolute URL. Also,
417 # if gitweb.cgi serves as your indexfile, you will need to force
418 # $my_uri to contain the script name in your $GITWEB_CONFIG.
419 'pathinfo' => {
420 'override' => 0,
421 'default' => [0]},
423 # Make gitweb consider projects in project root subdirectories
424 # to be forks of existing projects. Given project $projname.git,
425 # projects matching $projname/*.git will not be shown in the main
426 # projects list, instead a '+' mark will be added to $projname
427 # there and a 'forks' view will be enabled for the project, listing
428 # all the forks. If project list is taken from a file, forks have
429 # to be listed after the main project.
431 # To enable system wide have in $GITWEB_CONFIG
432 # $feature{'forks'}{'default'} = [1];
433 # Project specific override is not supported.
434 'forks' => {
435 'override' => 0,
436 'default' => [0]},
438 # Insert custom links to the action bar of all project pages.
439 # This enables you mainly to link to third-party scripts integrating
440 # into gitweb; e.g. git-browser for graphical history representation
441 # or custom web-based repository administration interface.
443 # The 'default' value consists of a list of triplets in the form
444 # (label, link, position) where position is the label after which
445 # to insert the link and link is a format string where %n expands
446 # to the project name, %f to the project path within the filesystem,
447 # %h to the current hash (h gitweb parameter) and %b to the current
448 # hash base (hb gitweb parameter); %% expands to %.
450 # To enable system wide have in $GITWEB_CONFIG e.g.
451 # $feature{'actions'}{'default'} = [('graphiclog',
452 # '/git-browser/by-commit.html?r=%n', 'summary')];
453 # Project specific override is not supported.
454 'actions' => {
455 'override' => 0,
456 'default' => []},
458 # Allow gitweb scan project content tags of project repository,
459 # and display the popular Web 2.0-ish "tag cloud" near the projects
460 # list. Note that this is something COMPLETELY different from the
461 # normal Git tags.
463 # gitweb by itself can show existing tags, but it does not handle
464 # tagging itself; you need to do it externally, outside gitweb.
465 # The format is described in git_get_project_ctags() subroutine.
466 # You may want to install the HTML::TagCloud Perl module to get
467 # a pretty tag cloud instead of just a list of tags.
469 # To enable system wide have in $GITWEB_CONFIG
470 # $feature{'ctags'}{'default'} = [1];
471 # Project specific override is not supported.
473 # A value of 0 means no ctags display or editing. A value of
474 # 1 enables ctags display but never editing. A non-empty value
475 # that is not a string of digits enables ctags display AND the
476 # ability to add tags using a form that uses method POST and
477 # an action value set to the configured 'ctags' value.
478 'ctags' => {
479 'override' => 0,
480 'default' => [0]},
482 # The maximum number of patches in a patchset generated in patch
483 # view. Set this to 0 or undef to disable patch view, or to a
484 # negative number to remove any limit.
486 # To disable system wide have in $GITWEB_CONFIG
487 # $feature{'patches'}{'default'} = [0];
488 # To have project specific config enable override in $GITWEB_CONFIG
489 # $feature{'patches'}{'override'} = 1;
490 # and in project config gitweb.patches = 0|n;
491 # where n is the maximum number of patches allowed in a patchset.
492 'patches' => {
493 'sub' => \&feature_patches,
494 'override' => 0,
495 'default' => [16]},
497 # Avatar support. When this feature is enabled, views such as
498 # shortlog or commit will display an avatar associated with
499 # the email of the committer(s) and/or author(s).
501 # Currently available providers are gravatar and picon.
502 # If an unknown provider is specified, the feature is disabled.
504 # Gravatar depends on Digest::MD5.
505 # Picon currently relies on the indiana.edu database.
507 # To enable system wide have in $GITWEB_CONFIG
508 # $feature{'avatar'}{'default'} = ['<provider>'];
509 # where <provider> is either gravatar or picon.
510 # To have project specific config enable override in $GITWEB_CONFIG
511 # $feature{'avatar'}{'override'} = 1;
512 # and in project config gitweb.avatar = <provider>;
513 'avatar' => {
514 'sub' => \&feature_avatar,
515 'override' => 0,
516 'default' => ['']},
518 # Enable displaying how much time and how many git commands
519 # it took to generate and display page. Disabled by default.
520 # Project specific override is not supported.
521 'timed' => {
522 'override' => 0,
523 'default' => [0]},
525 # Enable turning some links into links to actions which require
526 # JavaScript to run (like 'blame_incremental'). Not enabled by
527 # default. Project specific override is currently not supported.
528 'javascript-actions' => {
529 'override' => 0,
530 'default' => [0]},
532 # Enable and configure ability to change common timezone for dates
533 # in gitweb output via JavaScript. Enabled by default.
534 # Project specific override is not supported.
535 'javascript-timezone' => {
536 'override' => 0,
537 'default' => [
538 'local', # default timezone: 'utc', 'local', or '(-|+)HHMM' format,
539 # or undef to turn off this feature
540 'gitweb_tz', # name of cookie where to store selected timezone
541 'datetime', # CSS class used to mark up dates for manipulation
544 # Syntax highlighting support. This is based on Daniel Svensson's
545 # and Sham Chukoury's work in gitweb-xmms2.git.
546 # It requires the 'highlight' program present in $PATH,
547 # and therefore is disabled by default.
549 # To enable system wide have in $GITWEB_CONFIG
550 # $feature{'highlight'}{'default'} = [1];
552 'highlight' => {
553 'sub' => sub { feature_bool('highlight', @_) },
554 'override' => 0,
555 'default' => [0]},
557 # Enable displaying of remote heads in the heads list
559 # To enable system wide have in $GITWEB_CONFIG
560 # $feature{'remote_heads'}{'default'} = [1];
561 # To have project specific config enable override in $GITWEB_CONFIG
562 # $feature{'remote_heads'}{'override'} = 1;
563 # and in project config gitweb.remoteheads = 0|1;
564 'remote_heads' => {
565 'sub' => sub { feature_bool('remote_heads', @_) },
566 'override' => 0,
567 'default' => [0]},
569 # Enable showing branches under other refs in addition to heads
571 # To set system wide extra branch refs have in $GITWEB_CONFIG
572 # $feature{'extra-branch-refs'}{'default'} = ['dirs', 'of', 'choice'];
573 # To have project specific config enable override in $GITWEB_CONFIG
574 # $feature{'extra-branch-refs'}{'override'} = 1;
575 # and in project config gitweb.extrabranchrefs = dirs of choice
576 # Every directory is separated with whitespace.
578 'extra-branch-refs' => {
579 'sub' => \&feature_extra_branch_refs,
580 'override' => 0,
581 'default' => []},
584 sub gitweb_get_feature {
585 my ($name) = @_;
586 return unless exists $feature{$name};
587 my ($sub, $override, @defaults) = (
588 $feature{$name}{'sub'},
589 $feature{$name}{'override'},
590 @{$feature{$name}{'default'}});
591 # project specific override is possible only if we have project
592 our $git_dir; # global variable, declared later
593 if (!$override || !defined $git_dir) {
594 return @defaults;
596 if (!defined $sub) {
597 warn "feature $name is not overridable";
598 return @defaults;
600 return $sub->(@defaults);
603 # A wrapper to check if a given feature is enabled.
604 # With this, you can say
606 # my $bool_feat = gitweb_check_feature('bool_feat');
607 # gitweb_check_feature('bool_feat') or somecode;
609 # instead of
611 # my ($bool_feat) = gitweb_get_feature('bool_feat');
612 # (gitweb_get_feature('bool_feat'))[0] or somecode;
614 sub gitweb_check_feature {
615 return (gitweb_get_feature(@_))[0];
619 sub feature_bool {
620 my $key = shift;
621 my ($val) = git_get_project_config($key, '--bool');
623 if (!defined $val) {
624 return ($_[0]);
625 } elsif ($val eq 'true') {
626 return (1);
627 } elsif ($val eq 'false') {
628 return (0);
632 sub feature_snapshot {
633 my (@fmts) = @_;
635 my ($val) = git_get_project_config('snapshot');
637 if ($val) {
638 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
641 return @fmts;
644 sub feature_patches {
645 my @val = (git_get_project_config('patches', '--int'));
647 if (@val) {
648 return @val;
651 return ($_[0]);
654 sub feature_avatar {
655 my @val = (git_get_project_config('avatar'));
657 return @val ? @val : @_;
660 sub feature_extra_branch_refs {
661 my (@branch_refs) = @_;
662 my $values = git_get_project_config('extrabranchrefs');
664 if ($values) {
665 $values = config_to_multi ($values);
666 @branch_refs = ();
667 foreach my $value (@{$values}) {
668 push @branch_refs, split /\s+/, $value;
672 return @branch_refs;
675 # checking HEAD file with -e is fragile if the repository was
676 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
677 # and then pruned.
678 sub check_head_link {
679 my ($dir) = @_;
680 my $headfile = "$dir/HEAD";
681 return ((-e $headfile) ||
682 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
685 sub check_export_ok {
686 my ($dir) = @_;
687 return (check_head_link($dir) &&
688 (!$export_ok || -e "$dir/$export_ok") &&
689 (!$export_auth_hook || $export_auth_hook->($dir)));
692 # process alternate names for backward compatibility
693 # filter out unsupported (unknown) snapshot formats
694 sub filter_snapshot_fmts {
695 my @fmts = @_;
697 @fmts = map {
698 exists $known_snapshot_format_aliases{$_} ?
699 $known_snapshot_format_aliases{$_} : $_} @fmts;
700 @fmts = grep {
701 exists $known_snapshot_formats{$_} &&
702 !$known_snapshot_formats{$_}{'disabled'}} @fmts;
705 sub filter_and_validate_refs {
706 my @refs = @_;
707 my %unique_refs = ();
709 foreach my $ref (@refs) {
710 die_error(500, "Invalid ref '$ref' in 'extra-branch-refs' feature") unless (is_valid_ref_format($ref));
711 # 'heads' are added implicitly in get_branch_refs().
712 $unique_refs{$ref} = 1 if ($ref ne 'heads');
714 return sort keys %unique_refs;
717 # If it is set to code reference, it is code that it is to be run once per
718 # request, allowing updating configurations that change with each request,
719 # while running other code in config file only once.
721 # Otherwise, if it is false then gitweb would process config file only once;
722 # if it is true then gitweb config would be run for each request.
723 our $per_request_config = 1;
725 # read and parse gitweb config file given by its parameter.
726 # returns true on success, false on recoverable error, allowing
727 # to chain this subroutine, using first file that exists.
728 # dies on errors during parsing config file, as it is unrecoverable.
729 sub read_config_file {
730 my $filename = shift;
731 return unless defined $filename;
732 # die if there are errors parsing config file
733 if (-e $filename) {
734 do $filename;
735 die $@ if $@;
736 return 1;
738 return;
741 our ($GITWEB_CONFIG, $GITWEB_CONFIG_SYSTEM, $GITWEB_CONFIG_COMMON);
742 sub evaluate_gitweb_config {
743 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
744 our $GITWEB_CONFIG_SYSTEM = $ENV{'GITWEB_CONFIG_SYSTEM'} || "++GITWEB_CONFIG_SYSTEM++";
745 our $GITWEB_CONFIG_COMMON = $ENV{'GITWEB_CONFIG_COMMON'} || "++GITWEB_CONFIG_COMMON++";
747 # Protect against duplications of file names, to not read config twice.
748 # Only one of $GITWEB_CONFIG and $GITWEB_CONFIG_SYSTEM is used, so
749 # there possibility of duplication of filename there doesn't matter.
750 $GITWEB_CONFIG = "" if ($GITWEB_CONFIG eq $GITWEB_CONFIG_COMMON);
751 $GITWEB_CONFIG_SYSTEM = "" if ($GITWEB_CONFIG_SYSTEM eq $GITWEB_CONFIG_COMMON);
753 # Common system-wide settings for convenience.
754 # Those settings can be ovverriden by GITWEB_CONFIG or GITWEB_CONFIG_SYSTEM.
755 read_config_file($GITWEB_CONFIG_COMMON);
757 # Use first config file that exists. This means use the per-instance
758 # GITWEB_CONFIG if exists, otherwise use GITWEB_SYSTEM_CONFIG.
759 read_config_file($GITWEB_CONFIG) and return;
760 read_config_file($GITWEB_CONFIG_SYSTEM);
763 # Get loadavg of system, to compare against $maxload.
764 # Currently it requires '/proc/loadavg' present to get loadavg;
765 # if it is not present it returns 0, which means no load checking.
766 sub get_loadavg {
767 if( -e '/proc/loadavg' ){
768 open my $fd, '<', '/proc/loadavg'
769 or return 0;
770 my @load = split(/\s+/, scalar <$fd>);
771 close $fd;
773 # The first three columns measure CPU and IO utilization of the last one,
774 # five, and 10 minute periods. The fourth column shows the number of
775 # currently running processes and the total number of processes in the m/n
776 # format. The last column displays the last process ID used.
777 return $load[0] || 0;
779 # additional checks for load average should go here for things that don't export
780 # /proc/loadavg
782 return 0;
785 # version of the core git binary
786 our $git_version;
787 sub evaluate_git_version {
788 our $git_version = qx("$GIT" --version) =~ m/git version (.*)$/ ? $1 : "unknown";
789 $number_of_git_cmds++;
792 sub check_loadavg {
793 if (defined $maxload && get_loadavg() > $maxload) {
794 die_error(503, "The load average on the server is too high");
798 # ======================================================================
799 # input validation and dispatch
801 # input parameters can be collected from a variety of sources (presently, CGI
802 # and PATH_INFO), so we define an %input_params hash that collects them all
803 # together during validation: this allows subsequent uses (e.g. href()) to be
804 # agnostic of the parameter origin
806 our %input_params = ();
808 # input parameters are stored with the long parameter name as key. This will
809 # also be used in the href subroutine to convert parameters to their CGI
810 # equivalent, and since the href() usage is the most frequent one, we store
811 # the name -> CGI key mapping here, instead of the reverse.
813 # XXX: Warning: If you touch this, check the search form for updating,
814 # too.
816 our @cgi_param_mapping = (
817 project => "p",
818 action => "a",
819 file_name => "f",
820 file_parent => "fp",
821 hash => "h",
822 hash_parent => "hp",
823 hash_base => "hb",
824 hash_parent_base => "hpb",
825 page => "pg",
826 order => "o",
827 searchtext => "s",
828 searchtype => "st",
829 snapshot_format => "sf",
830 ctag_filter => 't',
831 extra_options => "opt",
832 search_use_regexp => "sr",
833 ctag => "by_tag",
834 diff_style => "ds",
835 project_filter => "pf",
836 # this must be last entry (for manipulation from JavaScript)
837 javascript => "js"
839 our %cgi_param_mapping = @cgi_param_mapping;
841 # we will also need to know the possible actions, for validation
842 our %actions = (
843 "blame" => \&git_blame,
844 "blame_incremental" => \&git_blame_incremental,
845 "blame_data" => \&git_blame_data,
846 "blobdiff" => \&git_blobdiff,
847 "blobdiff_plain" => \&git_blobdiff_plain,
848 "blob" => \&git_blob,
849 "blob_plain" => \&git_blob_plain,
850 "commitdiff" => \&git_commitdiff,
851 "commitdiff_plain" => \&git_commitdiff_plain,
852 "commit" => \&git_commit,
853 "forks" => \&git_forks,
854 "heads" => \&git_heads,
855 "history" => \&git_history,
856 "log" => \&git_log,
857 "patch" => \&git_patch,
858 "patches" => \&git_patches,
859 "remotes" => \&git_remotes,
860 "rss" => \&git_rss,
861 "atom" => \&git_atom,
862 "search" => \&git_search,
863 "search_help" => \&git_search_help,
864 "shortlog" => \&git_shortlog,
865 "summary" => \&git_summary,
866 "tag" => \&git_tag,
867 "tags" => \&git_tags,
868 "tree" => \&git_tree,
869 "snapshot" => \&git_snapshot,
870 "object" => \&git_object,
871 # those below don't need $project
872 "opml" => \&git_opml,
873 "frontpage" => \&git_frontpage,
874 "project_list" => \&git_project_list,
875 "project_index" => \&git_project_index,
878 # finally, we have the hash of allowed extra_options for the commands that
879 # allow them
880 our %allowed_options = (
881 "--no-merges" => [ qw(rss atom log shortlog history) ],
884 # fill %input_params with the CGI parameters. All values except for 'opt'
885 # should be single values, but opt can be an array. We should probably
886 # build an array of parameters that can be multi-valued, but since for the time
887 # being it's only this one, we just single it out
888 sub evaluate_query_params {
889 our $cgi;
891 while (my ($name, $symbol) = each %cgi_param_mapping) {
892 if ($symbol eq 'opt') {
893 $input_params{$name} = [ map { decode_utf8($_) } $cgi->multi_param($symbol) ];
894 } else {
895 $input_params{$name} = decode_utf8($cgi->param($symbol));
899 # Backwards compatibility - by_tag= <=> t=
900 if ($input_params{'ctag'}) {
901 $input_params{'ctag_filter'} = $input_params{'ctag'};
905 # now read PATH_INFO and update the parameter list for missing parameters
906 sub evaluate_path_info {
907 return if defined $input_params{'project'};
908 return if !$path_info;
909 $path_info =~ s,^/+,,;
910 return if !$path_info;
912 # find which part of PATH_INFO is project
913 my $project = $path_info;
914 $project =~ s,/+$,,;
915 while ($project && !check_head_link("$projectroot/$project")) {
916 $project =~ s,/*[^/]*$,,;
918 return unless $project;
919 $input_params{'project'} = $project;
921 # do not change any parameters if an action is given using the query string
922 return if $input_params{'action'};
923 $path_info =~ s,^\Q$project\E/*,,;
925 # next, check if we have an action
926 my $action = $path_info;
927 $action =~ s,/.*$,,;
928 if (exists $actions{$action}) {
929 $path_info =~ s,^$action/*,,;
930 $input_params{'action'} = $action;
933 # list of actions that want hash_base instead of hash, but can have no
934 # pathname (f) parameter
935 my @wants_base = (
936 'tree',
937 'history',
940 # we want to catch, among others
941 # [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name]
942 my ($parentrefname, $parentpathname, $refname, $pathname) =
943 ($path_info =~ /^(?:(.+?)(?::(.+))?\.\.)?([^:]+?)?(?::(.+))?$/);
945 # first, analyze the 'current' part
946 if (defined $pathname) {
947 # we got "branch:filename" or "branch:dir/"
948 # we could use git_get_type(branch:pathname), but:
949 # - it needs $git_dir
950 # - it does a git() call
951 # - the convention of terminating directories with a slash
952 # makes it superfluous
953 # - embedding the action in the PATH_INFO would make it even
954 # more superfluous
955 $pathname =~ s,^/+,,;
956 if (!$pathname || substr($pathname, -1) eq "/") {
957 $input_params{'action'} ||= "tree";
958 $pathname =~ s,/$,,;
959 } else {
960 # the default action depends on whether we had parent info
961 # or not
962 if ($parentrefname) {
963 $input_params{'action'} ||= "blobdiff_plain";
964 } else {
965 $input_params{'action'} ||= "blob_plain";
968 $input_params{'hash_base'} ||= $refname;
969 $input_params{'file_name'} ||= $pathname;
970 } elsif (defined $refname) {
971 # we got "branch". In this case we have to choose if we have to
972 # set hash or hash_base.
974 # Most of the actions without a pathname only want hash to be
975 # set, except for the ones specified in @wants_base that want
976 # hash_base instead. It should also be noted that hand-crafted
977 # links having 'history' as an action and no pathname or hash
978 # set will fail, but that happens regardless of PATH_INFO.
979 if (defined $parentrefname) {
980 # if there is parent let the default be 'shortlog' action
981 # (for http://git.example.com/repo.git/A..B links); if there
982 # is no parent, dispatch will detect type of object and set
983 # action appropriately if required (if action is not set)
984 $input_params{'action'} ||= "shortlog";
986 if ($input_params{'action'} &&
987 grep { $_ eq $input_params{'action'} } @wants_base) {
988 $input_params{'hash_base'} ||= $refname;
989 } else {
990 $input_params{'hash'} ||= $refname;
994 # next, handle the 'parent' part, if present
995 if (defined $parentrefname) {
996 # a missing pathspec defaults to the 'current' filename, allowing e.g.
997 # someproject/blobdiff/oldrev..newrev:/filename
998 if ($parentpathname) {
999 $parentpathname =~ s,^/+,,;
1000 $parentpathname =~ s,/$,,;
1001 $input_params{'file_parent'} ||= $parentpathname;
1002 } else {
1003 $input_params{'file_parent'} ||= $input_params{'file_name'};
1005 # we assume that hash_parent_base is wanted if a path was specified,
1006 # or if the action wants hash_base instead of hash
1007 if (defined $input_params{'file_parent'} ||
1008 grep { $_ eq $input_params{'action'} } @wants_base) {
1009 $input_params{'hash_parent_base'} ||= $parentrefname;
1010 } else {
1011 $input_params{'hash_parent'} ||= $parentrefname;
1015 # for the snapshot action, we allow URLs in the form
1016 # $project/snapshot/$hash.ext
1017 # where .ext determines the snapshot and gets removed from the
1018 # passed $refname to provide the $hash.
1020 # To be able to tell that $refname includes the format extension, we
1021 # require the following two conditions to be satisfied:
1022 # - the hash input parameter MUST have been set from the $refname part
1023 # of the URL (i.e. they must be equal)
1024 # - the snapshot format MUST NOT have been defined already (e.g. from
1025 # CGI parameter sf)
1026 # It's also useless to try any matching unless $refname has a dot,
1027 # so we check for that too
1028 if (defined $input_params{'action'} &&
1029 $input_params{'action'} eq 'snapshot' &&
1030 defined $refname && index($refname, '.') != -1 &&
1031 $refname eq $input_params{'hash'} &&
1032 !defined $input_params{'snapshot_format'}) {
1033 # We loop over the known snapshot formats, checking for
1034 # extensions. Allowed extensions are both the defined suffix
1035 # (which includes the initial dot already) and the snapshot
1036 # format key itself, with a prepended dot
1037 while (my ($fmt, $opt) = each %known_snapshot_formats) {
1038 my $hash = $refname;
1039 unless ($hash =~ s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) {
1040 next;
1042 my $sfx = $1;
1043 # a valid suffix was found, so set the snapshot format
1044 # and reset the hash parameter
1045 $input_params{'snapshot_format'} = $fmt;
1046 $input_params{'hash'} = $hash;
1047 # we also set the format suffix to the one requested
1048 # in the URL: this way a request for e.g. .tgz returns
1049 # a .tgz instead of a .tar.gz
1050 $known_snapshot_formats{$fmt}{'suffix'} = $sfx;
1051 last;
1056 our ($action, $project, $file_name, $file_parent, $hash, $hash_parent, $hash_base,
1057 $hash_parent_base, @extra_options, $page, $searchtype, $search_use_regexp,
1058 $searchtext, $search_regexp, $project_filter);
1059 sub evaluate_and_validate_params {
1060 our $action = $input_params{'action'};
1061 if (defined $action) {
1062 if (!is_valid_action($action)) {
1063 die_error(400, "Invalid action parameter");
1067 # parameters which are pathnames
1068 our $project = $input_params{'project'};
1069 if (defined $project) {
1070 if (!is_valid_project($project)) {
1071 undef $project;
1072 die_error(404, "No such project");
1076 our $project_filter = $input_params{'project_filter'};
1077 if (defined $project_filter) {
1078 if (!is_valid_pathname($project_filter)) {
1079 die_error(404, "Invalid project_filter parameter");
1083 our $file_name = $input_params{'file_name'};
1084 if (defined $file_name) {
1085 if (!is_valid_pathname($file_name)) {
1086 die_error(400, "Invalid file parameter");
1090 our $file_parent = $input_params{'file_parent'};
1091 if (defined $file_parent) {
1092 if (!is_valid_pathname($file_parent)) {
1093 die_error(400, "Invalid file parent parameter");
1097 # parameters which are refnames
1098 our $hash = $input_params{'hash'};
1099 if (defined $hash) {
1100 if (!is_valid_refname($hash)) {
1101 die_error(400, "Invalid hash parameter");
1105 our $hash_parent = $input_params{'hash_parent'};
1106 if (defined $hash_parent) {
1107 if (!is_valid_refname($hash_parent)) {
1108 die_error(400, "Invalid hash parent parameter");
1112 our $hash_base = $input_params{'hash_base'};
1113 if (defined $hash_base) {
1114 if (!is_valid_refname($hash_base)) {
1115 die_error(400, "Invalid hash base parameter");
1119 our @extra_options = @{$input_params{'extra_options'}};
1120 # @extra_options is always defined, since it can only be (currently) set from
1121 # CGI, and $cgi->param() returns the empty array in array context if the param
1122 # is not set
1123 foreach my $opt (@extra_options) {
1124 if (not exists $allowed_options{$opt}) {
1125 die_error(400, "Invalid option parameter");
1127 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
1128 die_error(400, "Invalid option parameter for this action");
1132 our $hash_parent_base = $input_params{'hash_parent_base'};
1133 if (defined $hash_parent_base) {
1134 if (!is_valid_refname($hash_parent_base)) {
1135 die_error(400, "Invalid hash parent base parameter");
1139 # other parameters
1140 our $page = $input_params{'page'};
1141 if (defined $page) {
1142 if ($page =~ m/[^0-9]/) {
1143 die_error(400, "Invalid page parameter");
1147 our $searchtype = $input_params{'searchtype'};
1148 if (defined $searchtype) {
1149 if ($searchtype =~ m/[^a-z]/) {
1150 die_error(400, "Invalid searchtype parameter");
1154 our $search_use_regexp = $input_params{'search_use_regexp'};
1156 our $searchtext = $input_params{'searchtext'};
1157 our $search_regexp = undef;
1158 if (defined $searchtext) {
1159 if (length($searchtext) < 2) {
1160 die_error(403, "At least two characters are required for search parameter");
1162 if ($search_use_regexp) {
1163 $search_regexp = $searchtext;
1164 if (!eval { qr/$search_regexp/; 1; }) {
1165 (my $error = $@) =~ s/ at \S+ line \d+.*\n?//;
1166 die_error(400, "Invalid search regexp '$search_regexp'",
1167 esc_html($error));
1169 } else {
1170 $search_regexp = quotemeta $searchtext;
1175 # path to the current git repository
1176 our $git_dir;
1177 sub evaluate_git_dir {
1178 our $git_dir = "$projectroot/$project" if $project;
1181 our (@snapshot_fmts, $git_avatar, @extra_branch_refs);
1182 sub configure_gitweb_features {
1183 # list of supported snapshot formats
1184 our @snapshot_fmts = gitweb_get_feature('snapshot');
1185 @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
1187 # check that the avatar feature is set to a known provider name,
1188 # and for each provider check if the dependencies are satisfied.
1189 # if the provider name is invalid or the dependencies are not met,
1190 # reset $git_avatar to the empty string.
1191 our ($git_avatar) = gitweb_get_feature('avatar');
1192 if ($git_avatar eq 'gravatar') {
1193 $git_avatar = '' unless (eval { require Digest::MD5; 1; });
1194 } elsif ($git_avatar eq 'picon') {
1195 # no dependencies
1196 } else {
1197 $git_avatar = '';
1200 our @extra_branch_refs = gitweb_get_feature('extra-branch-refs');
1201 @extra_branch_refs = filter_and_validate_refs (@extra_branch_refs);
1204 sub get_branch_refs {
1205 return ('heads', @extra_branch_refs);
1208 # custom error handler: 'die <message>' is Internal Server Error
1209 sub handle_errors_html {
1210 my $msg = shift; # it is already HTML escaped
1212 # to avoid infinite loop where error occurs in die_error,
1213 # change handler to default handler, disabling handle_errors_html
1214 set_message("Error occurred when inside die_error:\n$msg");
1216 # you cannot jump out of die_error when called as error handler;
1217 # the subroutine set via CGI::Carp::set_message is called _after_
1218 # HTTP headers are already written, so it cannot write them itself
1219 die_error(undef, undef, $msg, -error_handler => 1, -no_http_header => 1);
1221 set_message(\&handle_errors_html);
1223 # dispatch
1224 sub dispatch {
1225 if (!defined $action) {
1226 if (defined $hash) {
1227 $action = git_get_type($hash);
1228 $action or die_error(404, "Object does not exist");
1229 } elsif (defined $hash_base && defined $file_name) {
1230 $action = git_get_type("$hash_base:$file_name");
1231 $action or die_error(404, "File or directory does not exist");
1232 } elsif (defined $project) {
1233 $action = 'summary';
1234 } else {
1235 $action = 'frontpage';
1238 if (!defined($actions{$action})) {
1239 die_error(400, "Unknown action");
1241 if ($action !~ m/^(?:opml|frontpage|project_list|project_index)$/ &&
1242 !$project) {
1243 die_error(400, "Project needed");
1245 $actions{$action}->();
1248 sub reset_timer {
1249 our $t0 = [ gettimeofday() ]
1250 if defined $t0;
1251 our $number_of_git_cmds = 0;
1254 our $first_request = 1;
1255 sub run_request {
1256 reset_timer();
1258 evaluate_uri();
1259 if ($first_request) {
1260 evaluate_gitweb_config();
1261 evaluate_git_version();
1263 if ($per_request_config) {
1264 if (ref($per_request_config) eq 'CODE') {
1265 $per_request_config->();
1266 } elsif (!$first_request) {
1267 evaluate_gitweb_config();
1270 check_loadavg();
1272 # $projectroot and $projects_list might be set in gitweb config file
1273 $projects_list ||= $projectroot;
1275 evaluate_query_params();
1276 evaluate_path_info();
1277 evaluate_and_validate_params();
1278 evaluate_git_dir();
1280 configure_gitweb_features();
1282 dispatch();
1285 our $is_last_request = sub { 1 };
1286 our ($pre_dispatch_hook, $post_dispatch_hook, $pre_listen_hook);
1287 our $CGI = 'CGI';
1288 our $cgi;
1289 sub configure_as_fcgi {
1290 require CGI::Fast;
1291 our $CGI = 'CGI::Fast';
1293 my $request_number = 0;
1294 # let each child service 100 requests
1295 our $is_last_request = sub { ++$request_number > 100 };
1297 sub evaluate_argv {
1298 my $script_name = $ENV{'SCRIPT_NAME'} || $ENV{'SCRIPT_FILENAME'} || __FILE__;
1299 configure_as_fcgi()
1300 if $script_name =~ /\.fcgi$/;
1302 return unless (@ARGV);
1304 require Getopt::Long;
1305 Getopt::Long::GetOptions(
1306 'fastcgi|fcgi|f' => \&configure_as_fcgi,
1307 'nproc|n=i' => sub {
1308 my ($arg, $val) = @_;
1309 return unless eval { require FCGI::ProcManager; 1; };
1310 my $proc_manager = FCGI::ProcManager->new({
1311 n_processes => $val,
1313 our $pre_listen_hook = sub { $proc_manager->pm_manage() };
1314 our $pre_dispatch_hook = sub { $proc_manager->pm_pre_dispatch() };
1315 our $post_dispatch_hook = sub { $proc_manager->pm_post_dispatch() };
1320 # Any "our" variable that could possibly influence correct handling of
1321 # a CGI request MUST be reset in this subroutine
1322 sub _reset_globals {
1323 # Note that $t0 and $number_of_git_commands are handled by reset_timer
1324 our %input_params = ();
1325 our ($action, $project, $file_name, $file_parent, $hash, $hash_parent, $hash_base,
1326 $hash_parent_base, @extra_options, $page, $searchtype, $search_use_regexp,
1327 $searchtext, $search_regexp, $project_filter) = ();
1328 our $git_dir = undef;
1329 our (@snapshot_fmts, $git_avatar, @extra_branch_refs) = ();
1330 our %avatar_cache = ();
1331 our $config_file = '';
1332 our %config = ();
1333 our $gitweb_project_owner = undef;
1334 keys %known_snapshot_formats; # reset 'each' iterator
1337 sub run {
1338 evaluate_argv();
1340 $first_request = 1;
1341 $pre_listen_hook->()
1342 if $pre_listen_hook;
1344 REQUEST:
1345 while ($cgi = $CGI->new()) {
1346 $pre_dispatch_hook->()
1347 if $pre_dispatch_hook;
1349 # most globals can simply be reset
1350 _reset_globals;
1352 # evaluate_path_info corrupts %known_snapshot_formats
1353 # so we need a deepish copy of it -- note that
1354 # _reset_globals already took care of resetting its
1355 # hash iterator that evaluate_path_info also leaves
1356 # in an indeterminate state
1357 my %formats = ();
1358 while (my ($k,$v) = each(%known_snapshot_formats)) {
1359 $formats{$k} = {%{$known_snapshot_formats{$k}}};
1361 local *known_snapshot_formats = \%formats;
1363 eval {run_request()};
1365 $post_dispatch_hook->()
1366 if $post_dispatch_hook;
1367 $first_request = 0;
1369 last REQUEST if ($is_last_request->());
1375 run();
1377 if (defined caller) {
1378 # wrapped in a subroutine processing requests,
1379 # e.g. mod_perl with ModPerl::Registry, or PSGI with Plack::App::WrapCGI
1380 return;
1381 } else {
1382 # pure CGI script, serving single request
1383 exit;
1386 ## ======================================================================
1387 ## action links
1389 # possible values of extra options
1390 # -full => 0|1 - use absolute/full URL ($my_uri/$my_url as base)
1391 # -replay => 1 - start from a current view (replay with modifications)
1392 # -path_info => 0|1 - don't use/use path_info URL (if possible)
1393 # -anchor => ANCHOR - add #ANCHOR to end of URL, implies -replay if used alone
1394 sub href {
1395 my %params = @_;
1396 # default is to use -absolute url() i.e. $my_uri
1397 my $href = $params{-full} ? $my_url : $my_uri;
1399 # implicit -replay, must be first of implicit params
1400 $params{-replay} = 1 if (keys %params == 1 && $params{-anchor});
1402 $params{'project'} = $project unless exists $params{'project'};
1404 if ($params{-replay}) {
1405 while (my ($name, $symbol) = each %cgi_param_mapping) {
1406 if (!exists $params{$name}) {
1407 $params{$name} = $input_params{$name};
1412 my $use_pathinfo = gitweb_check_feature('pathinfo');
1413 if (defined $params{'project'} &&
1414 (exists $params{-path_info} ? $params{-path_info} : $use_pathinfo)) {
1415 # try to put as many parameters as possible in PATH_INFO:
1416 # - project name
1417 # - action
1418 # - hash_parent or hash_parent_base:/file_parent
1419 # - hash or hash_base:/filename
1420 # - the snapshot_format as an appropriate suffix
1422 # When the script is the root DirectoryIndex for the domain,
1423 # $href here would be something like http://gitweb.example.com/
1424 # Thus, we strip any trailing / from $href, to spare us double
1425 # slashes in the final URL
1426 $href =~ s,/$,,;
1428 # Then add the project name, if present
1429 $href .= "/".esc_path_info($params{'project'});
1430 delete $params{'project'};
1432 # since we destructively absorb parameters, we keep this
1433 # boolean that remembers if we're handling a snapshot
1434 my $is_snapshot = $params{'action'} eq 'snapshot';
1436 # Summary just uses the project path URL, any other action is
1437 # added to the URL
1438 if (defined $params{'action'}) {
1439 $href .= "/".esc_path_info($params{'action'})
1440 unless $params{'action'} eq 'summary';
1441 delete $params{'action'};
1444 # Next, we put hash_parent_base:/file_parent..hash_base:/file_name,
1445 # stripping nonexistent or useless pieces
1446 $href .= "/" if ($params{'hash_base'} || $params{'hash_parent_base'}
1447 || $params{'hash_parent'} || $params{'hash'});
1448 if (defined $params{'hash_base'}) {
1449 if (defined $params{'hash_parent_base'}) {
1450 $href .= esc_path_info($params{'hash_parent_base'});
1451 # skip the file_parent if it's the same as the file_name
1452 if (defined $params{'file_parent'}) {
1453 if (defined $params{'file_name'} && $params{'file_parent'} eq $params{'file_name'}) {
1454 delete $params{'file_parent'};
1455 } elsif ($params{'file_parent'} !~ /\.\./) {
1456 $href .= ":/".esc_path_info($params{'file_parent'});
1457 delete $params{'file_parent'};
1460 $href .= "..";
1461 delete $params{'hash_parent'};
1462 delete $params{'hash_parent_base'};
1463 } elsif (defined $params{'hash_parent'}) {
1464 $href .= esc_path_info($params{'hash_parent'}). "..";
1465 delete $params{'hash_parent'};
1468 $href .= esc_path_info($params{'hash_base'});
1469 if (defined $params{'file_name'} && $params{'file_name'} !~ /\.\./) {
1470 $href .= ":/".esc_path_info($params{'file_name'});
1471 delete $params{'file_name'};
1473 delete $params{'hash'};
1474 delete $params{'hash_base'};
1475 } elsif (defined $params{'hash'}) {
1476 $href .= esc_path_info($params{'hash'});
1477 delete $params{'hash'};
1480 # If the action was a snapshot, we can absorb the
1481 # snapshot_format parameter too
1482 if ($is_snapshot) {
1483 my $fmt = $params{'snapshot_format'};
1484 # snapshot_format should always be defined when href()
1485 # is called, but just in case some code forgets, we
1486 # fall back to the default
1487 $fmt ||= $snapshot_fmts[0];
1488 $href .= $known_snapshot_formats{$fmt}{'suffix'};
1489 delete $params{'snapshot_format'};
1493 # now encode the parameters explicitly
1494 my @result = ();
1495 for (my $i = 0; $i < @cgi_param_mapping; $i += 2) {
1496 my ($name, $symbol) = ($cgi_param_mapping[$i], $cgi_param_mapping[$i+1]);
1497 if (defined $params{$name}) {
1498 if (ref($params{$name}) eq "ARRAY") {
1499 foreach my $par (@{$params{$name}}) {
1500 push @result, $symbol . "=" . esc_param($par);
1502 } else {
1503 push @result, $symbol . "=" . esc_param($params{$name});
1507 $href .= "?" . join(';', @result) if scalar @result;
1509 # final transformation: trailing spaces must be escaped (URI-encoded)
1510 $href =~ s/(\s+)$/CGI::escape($1)/e;
1512 if ($params{-anchor}) {
1513 $href .= "#".esc_param($params{-anchor});
1516 return $href;
1520 ## ======================================================================
1521 ## validation, quoting/unquoting and escaping
1523 sub is_valid_action {
1524 my $input = shift;
1525 return undef unless exists $actions{$input};
1526 return 1;
1529 sub is_valid_project {
1530 my $input = shift;
1532 return unless defined $input;
1533 if (!is_valid_pathname($input) ||
1534 !(-d "$projectroot/$input") ||
1535 !check_export_ok("$projectroot/$input") ||
1536 ($strict_export && !project_in_list($input))) {
1537 return undef;
1538 } else {
1539 return 1;
1543 sub is_valid_pathname {
1544 my $input = shift;
1546 return undef unless defined $input;
1547 # no '.' or '..' as elements of path, i.e. no '.' or '..'
1548 # at the beginning, at the end, and between slashes.
1549 # also this catches doubled slashes
1550 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
1551 return undef;
1553 # no null characters
1554 if ($input =~ m!\0!) {
1555 return undef;
1557 return 1;
1560 sub is_valid_ref_format {
1561 my $input = shift;
1563 return undef unless defined $input;
1564 # restrictions on ref name according to git-check-ref-format
1565 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
1566 return undef;
1568 return 1;
1571 sub is_valid_refname {
1572 my $input = shift;
1574 return undef unless defined $input;
1575 # textual hashes are O.K.
1576 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
1577 return 1;
1579 # it must be correct pathname
1580 is_valid_pathname($input) or return undef;
1581 # check git-check-ref-format restrictions
1582 is_valid_ref_format($input) or return undef;
1583 return 1;
1586 # decode sequences of octets in utf8 into Perl's internal form,
1587 # which is utf-8 with utf8 flag set if needed. gitweb writes out
1588 # in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
1589 sub to_utf8 {
1590 my $str = shift;
1591 return undef unless defined $str;
1593 if (utf8::is_utf8($str) || utf8::decode($str)) {
1594 return $str;
1595 } else {
1596 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
1600 # quote unsafe chars, but keep the slash, even when it's not
1601 # correct, but quoted slashes look too horrible in bookmarks
1602 sub esc_param {
1603 my $str = shift;
1604 return undef unless defined $str;
1605 $str =~ s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;
1606 $str =~ s/ /\+/g;
1607 return $str;
1610 # the quoting rules for path_info fragment are slightly different
1611 sub esc_path_info {
1612 my $str = shift;
1613 return undef unless defined $str;
1615 # path_info doesn't treat '+' as space (specially), but '?' must be escaped
1616 $str =~ s/([^A-Za-z0-9\-_.~();\/;:@&= +]+)/CGI::escape($1)/eg;
1618 return $str;
1621 # quote unsafe chars in whole URL, so some characters cannot be quoted
1622 sub esc_url {
1623 my $str = shift;
1624 return undef unless defined $str;
1625 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&= ]+)/CGI::escape($1)/eg;
1626 $str =~ s/ /\+/g;
1627 return $str;
1630 # quote unsafe characters in HTML attributes
1631 sub esc_attr {
1633 # for XHTML conformance escaping '"' to '&quot;' is not enough
1634 return esc_html(@_);
1637 # replace invalid utf8 character with SUBSTITUTION sequence
1638 sub esc_html {
1639 my $str = shift;
1640 my %opts = @_;
1642 return undef unless defined $str;
1644 $str = to_utf8($str);
1645 $str = $cgi->escapeHTML($str);
1646 if ($opts{'-nbsp'}) {
1647 $str =~ s/ /&nbsp;/g;
1649 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
1650 return $str;
1653 # quote control characters and escape filename to HTML
1654 sub esc_path {
1655 my $str = shift;
1656 my %opts = @_;
1658 return undef unless defined $str;
1660 $str = to_utf8($str);
1661 $str = $cgi->escapeHTML($str);
1662 if ($opts{'-nbsp'}) {
1663 $str =~ s/ /&nbsp;/g;
1665 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
1666 return $str;
1669 # Sanitize for use in XHTML + application/xml+xhtm (valid XML 1.0)
1670 sub sanitize {
1671 my $str = shift;
1673 return undef unless defined $str;
1675 $str = to_utf8($str);
1676 $str =~ s|([[:cntrl:]])|(index("\t\n\r", $1) != -1 ? $1 : quot_cec($1))|eg;
1677 return $str;
1680 # Make control characters "printable", using character escape codes (CEC)
1681 sub quot_cec {
1682 my $cntrl = shift;
1683 my %opts = @_;
1684 my %es = ( # character escape codes, aka escape sequences
1685 "\t" => '\t', # tab (HT)
1686 "\n" => '\n', # line feed (LF)
1687 "\r" => '\r', # carrige return (CR)
1688 "\f" => '\f', # form feed (FF)
1689 "\b" => '\b', # backspace (BS)
1690 "\a" => '\a', # alarm (bell) (BEL)
1691 "\e" => '\e', # escape (ESC)
1692 "\013" => '\v', # vertical tab (VT)
1693 "\000" => '\0', # nul character (NUL)
1695 my $chr = ( (exists $es{$cntrl})
1696 ? $es{$cntrl}
1697 : sprintf('\%2x', ord($cntrl)) );
1698 if ($opts{-nohtml}) {
1699 return $chr;
1700 } else {
1701 return "<span class=\"cntrl\">$chr</span>";
1705 # Alternatively use unicode control pictures codepoints,
1706 # Unicode "printable representation" (PR)
1707 sub quot_upr {
1708 my $cntrl = shift;
1709 my %opts = @_;
1711 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
1712 if ($opts{-nohtml}) {
1713 return $chr;
1714 } else {
1715 return "<span class=\"cntrl\">$chr</span>";
1719 # git may return quoted and escaped filenames
1720 sub unquote {
1721 my $str = shift;
1723 sub unq {
1724 my $seq = shift;
1725 my %es = ( # character escape codes, aka escape sequences
1726 't' => "\t", # tab (HT, TAB)
1727 'n' => "\n", # newline (NL)
1728 'r' => "\r", # return (CR)
1729 'f' => "\f", # form feed (FF)
1730 'b' => "\b", # backspace (BS)
1731 'a' => "\a", # alarm (bell) (BEL)
1732 'e' => "\e", # escape (ESC)
1733 'v' => "\013", # vertical tab (VT)
1736 if ($seq =~ m/^[0-7]{1,3}$/) {
1737 # octal char sequence
1738 return chr(oct($seq));
1739 } elsif (exists $es{$seq}) {
1740 # C escape sequence, aka character escape code
1741 return $es{$seq};
1743 # quoted ordinary character
1744 return $seq;
1747 if ($str =~ m/^"(.*)"$/) {
1748 # needs unquoting
1749 $str = $1;
1750 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
1752 return $str;
1755 # escape tabs (convert tabs to spaces)
1756 sub untabify {
1757 my $line = shift;
1759 while ((my $pos = index($line, "\t")) != -1) {
1760 if (my $count = (8 - ($pos % 8))) {
1761 my $spaces = ' ' x $count;
1762 $line =~ s/\t/$spaces/;
1766 return $line;
1769 sub project_in_list {
1770 my $project = shift;
1771 my @list = git_get_projects_list();
1772 return @list && scalar(grep { $_->{'path'} eq $project } @list);
1775 ## ----------------------------------------------------------------------
1776 ## HTML aware string manipulation
1778 # Try to chop given string on a word boundary between position
1779 # $len and $len+$add_len. If there is no word boundary there,
1780 # chop at $len+$add_len. Do not chop if chopped part plus ellipsis
1781 # (marking chopped part) would be longer than given string.
1782 sub chop_str {
1783 my $str = shift;
1784 my $len = shift;
1785 my $add_len = shift || 10;
1786 my $where = shift || 'right'; # 'left' | 'center' | 'right'
1788 # Make sure perl knows it is utf8 encoded so we don't
1789 # cut in the middle of a utf8 multibyte char.
1790 $str = to_utf8($str);
1792 # allow only $len chars, but don't cut a word if it would fit in $add_len
1793 # if it doesn't fit, cut it if it's still longer than the dots we would add
1794 # remove chopped character entities entirely
1796 # when chopping in the middle, distribute $len into left and right part
1797 # return early if chopping wouldn't make string shorter
1798 if ($where eq 'center') {
1799 return $str if ($len + 5 >= length($str)); # filler is length 5
1800 $len = int($len/2);
1801 } else {
1802 return $str if ($len + 4 >= length($str)); # filler is length 4
1805 # regexps: ending and beginning with word part up to $add_len
1806 my $endre = qr/.{$len}\w{0,$add_len}/;
1807 my $begre = qr/\w{0,$add_len}.{$len}/;
1809 if ($where eq 'left') {
1810 $str =~ m/^(.*?)($begre)$/;
1811 my ($lead, $body) = ($1, $2);
1812 if (length($lead) > 4) {
1813 $lead = " ...";
1815 return "$lead$body";
1817 } elsif ($where eq 'center') {
1818 $str =~ m/^($endre)(.*)$/;
1819 my ($left, $str) = ($1, $2);
1820 $str =~ m/^(.*?)($begre)$/;
1821 my ($mid, $right) = ($1, $2);
1822 if (length($mid) > 5) {
1823 $mid = " ... ";
1825 return "$left$mid$right";
1827 } else {
1828 $str =~ m/^($endre)(.*)$/;
1829 my $body = $1;
1830 my $tail = $2;
1831 if (length($tail) > 4) {
1832 $tail = "... ";
1834 return "$body$tail";
1838 # takes the same arguments as chop_str, but also wraps a <span> around the
1839 # result with a title attribute if it does get chopped. Additionally, the
1840 # string is HTML-escaped.
1841 sub chop_and_escape_str {
1842 my ($str) = @_;
1844 my $chopped = chop_str(@_);
1845 $str = to_utf8($str);
1846 if ($chopped eq $str) {
1847 return esc_html($chopped);
1848 } else {
1849 $str =~ s/[[:cntrl:]]/?/g;
1850 return $cgi->span({-title=>$str}, esc_html($chopped));
1854 # Highlight selected fragments of string, using given CSS class,
1855 # and escape HTML. It is assumed that fragments do not overlap.
1856 # Regions are passed as list of pairs (array references).
1858 # Example: esc_html_hl_regions("foobar", "mark", [ 0, 3 ]) returns
1859 # '<span class="mark">foo</span>bar'
1860 sub esc_html_hl_regions {
1861 my ($str, $css_class, @sel) = @_;
1862 my %opts = grep { ref($_) ne 'ARRAY' } @sel;
1863 @sel = grep { ref($_) eq 'ARRAY' } @sel;
1864 return esc_html($str, %opts) unless @sel;
1866 my $out = '';
1867 my $pos = 0;
1869 for my $s (@sel) {
1870 my ($begin, $end) = @$s;
1872 # Don't create empty <span> elements.
1873 next if $end <= $begin;
1875 my $escaped = esc_html(substr($str, $begin, $end - $begin),
1876 %opts);
1878 $out .= esc_html(substr($str, $pos, $begin - $pos), %opts)
1879 if ($begin - $pos > 0);
1880 $out .= $cgi->span({-class => $css_class}, $escaped);
1882 $pos = $end;
1884 $out .= esc_html(substr($str, $pos), %opts)
1885 if ($pos < length($str));
1887 return $out;
1890 # return positions of beginning and end of each match
1891 sub matchpos_list {
1892 my ($str, $regexp) = @_;
1893 return unless (defined $str && defined $regexp);
1895 my @matches;
1896 while ($str =~ /$regexp/g) {
1897 push @matches, [$-[0], $+[0]];
1899 return @matches;
1902 # highlight match (if any), and escape HTML
1903 sub esc_html_match_hl {
1904 my ($str, $regexp) = @_;
1905 return esc_html($str) unless defined $regexp;
1907 my @matches = matchpos_list($str, $regexp);
1908 return esc_html($str) unless @matches;
1910 return esc_html_hl_regions($str, 'match', @matches);
1914 # highlight match (if any) of shortened string, and escape HTML
1915 sub esc_html_match_hl_chopped {
1916 my ($str, $chopped, $regexp) = @_;
1917 return esc_html_match_hl($str, $regexp) unless defined $chopped;
1919 my @matches = matchpos_list($str, $regexp);
1920 return esc_html($chopped) unless @matches;
1922 # filter matches so that we mark chopped string
1923 my $tail = "... "; # see chop_str
1924 unless ($chopped =~ s/\Q$tail\E$//) {
1925 $tail = '';
1927 my $chop_len = length($chopped);
1928 my $tail_len = length($tail);
1929 my @filtered;
1931 for my $m (@matches) {
1932 if ($m->[0] > $chop_len) {
1933 push @filtered, [ $chop_len, $chop_len + $tail_len ] if ($tail_len > 0);
1934 last;
1935 } elsif ($m->[1] > $chop_len) {
1936 push @filtered, [ $m->[0], $chop_len + $tail_len ];
1937 last;
1939 push @filtered, $m;
1942 return esc_html_hl_regions($chopped . $tail, 'match', @filtered);
1945 ## ----------------------------------------------------------------------
1946 ## functions returning short strings
1948 # CSS class for given age value (in seconds)
1949 sub age_class {
1950 my $age = shift;
1952 if (!defined $age) {
1953 return "noage";
1954 } elsif ($age < 60*60*2) {
1955 return "age0";
1956 } elsif ($age < 60*60*24*2) {
1957 return "age1";
1958 } else {
1959 return "age2";
1963 # convert age in seconds to "nn units ago" string
1964 sub age_string {
1965 my $age = shift;
1966 my $age_str;
1968 if ($age > 60*60*24*365*2) {
1969 $age_str = (int $age/60/60/24/365);
1970 $age_str .= " years ago";
1971 } elsif ($age > 60*60*24*(365/12)*2) {
1972 $age_str = int $age/60/60/24/(365/12);
1973 $age_str .= " months ago";
1974 } elsif ($age > 60*60*24*7*2) {
1975 $age_str = int $age/60/60/24/7;
1976 $age_str .= " weeks ago";
1977 } elsif ($age > 60*60*24*2) {
1978 $age_str = int $age/60/60/24;
1979 $age_str .= " days ago";
1980 } elsif ($age > 60*60*2) {
1981 $age_str = int $age/60/60;
1982 $age_str .= " hours ago";
1983 } elsif ($age > 60*2) {
1984 $age_str = int $age/60;
1985 $age_str .= " min ago";
1986 } elsif ($age > 2) {
1987 $age_str = int $age;
1988 $age_str .= " sec ago";
1989 } else {
1990 $age_str .= " right now";
1992 return $age_str;
1995 use constant {
1996 S_IFINVALID => 0030000,
1997 S_IFGITLINK => 0160000,
2000 # submodule/subproject, a commit object reference
2001 sub S_ISGITLINK {
2002 my $mode = shift;
2004 return (($mode & S_IFMT) == S_IFGITLINK)
2007 # convert file mode in octal to symbolic file mode string
2008 sub mode_str {
2009 my $mode = oct shift;
2011 if (S_ISGITLINK($mode)) {
2012 return 'm---------';
2013 } elsif (S_ISDIR($mode & S_IFMT)) {
2014 return 'drwxr-xr-x';
2015 } elsif (S_ISLNK($mode)) {
2016 return 'lrwxrwxrwx';
2017 } elsif (S_ISREG($mode)) {
2018 # git cares only about the executable bit
2019 if ($mode & S_IXUSR) {
2020 return '-rwxr-xr-x';
2021 } else {
2022 return '-rw-r--r--';
2024 } else {
2025 return '----------';
2029 # convert file mode in octal to file type string
2030 sub file_type {
2031 my $mode = shift;
2033 if ($mode !~ m/^[0-7]+$/) {
2034 return $mode;
2035 } else {
2036 $mode = oct $mode;
2039 if (S_ISGITLINK($mode)) {
2040 return "submodule";
2041 } elsif (S_ISDIR($mode & S_IFMT)) {
2042 return "directory";
2043 } elsif (S_ISLNK($mode)) {
2044 return "symlink";
2045 } elsif (S_ISREG($mode)) {
2046 return "file";
2047 } else {
2048 return "unknown";
2052 # convert file mode in octal to file type description string
2053 sub file_type_long {
2054 my $mode = shift;
2056 if ($mode !~ m/^[0-7]+$/) {
2057 return $mode;
2058 } else {
2059 $mode = oct $mode;
2062 if (S_ISGITLINK($mode)) {
2063 return "submodule";
2064 } elsif (S_ISDIR($mode & S_IFMT)) {
2065 return "directory";
2066 } elsif (S_ISLNK($mode)) {
2067 return "symlink";
2068 } elsif (S_ISREG($mode)) {
2069 if ($mode & S_IXUSR) {
2070 return "executable";
2071 } else {
2072 return "file";
2074 } else {
2075 return "unknown";
2080 ## ----------------------------------------------------------------------
2081 ## functions returning short HTML fragments, or transforming HTML fragments
2082 ## which don't belong to other sections
2084 # format line of commit message.
2085 sub format_log_line_html {
2086 my $line = shift;
2088 $line = esc_html($line, -nbsp=>1);
2089 $line =~ s{\b([0-9a-fA-F]{8,40})\b}{
2090 $cgi->a({-href => href(action=>"object", hash=>$1),
2091 -class => "text"}, $1);
2092 }eg;
2094 return $line;
2097 # format marker of refs pointing to given object
2099 # the destination action is chosen based on object type and current context:
2100 # - for annotated tags, we choose the tag view unless it's the current view
2101 # already, in which case we go to shortlog view
2102 # - for other refs, we keep the current view if we're in history, shortlog or
2103 # log view, and select shortlog otherwise
2104 sub format_ref_marker {
2105 my ($refs, $id) = @_;
2106 my $markers = '';
2108 if (defined $refs->{$id}) {
2109 foreach my $ref (@{$refs->{$id}}) {
2110 # this code exploits the fact that non-lightweight tags are the
2111 # only indirect objects, and that they are the only objects for which
2112 # we want to use tag instead of shortlog as action
2113 my ($type, $name) = qw();
2114 my $indirect = ($ref =~ s/\^\{\}$//);
2115 # e.g. tags/v2.6.11 or heads/next
2116 if ($ref =~ m!^(.*?)s?/(.*)$!) {
2117 $type = $1;
2118 $name = $2;
2119 } else {
2120 $type = "ref";
2121 $name = $ref;
2124 my $class = $type;
2125 $class .= " indirect" if $indirect;
2127 my $dest_action = "shortlog";
2129 if ($indirect) {
2130 $dest_action = "tag" unless $action eq "tag";
2131 } elsif ($action =~ /^(history|(short)?log)$/) {
2132 $dest_action = $action;
2135 my $dest = "";
2136 $dest .= "refs/" unless $ref =~ m!^refs/!;
2137 $dest .= $ref;
2139 my $link = $cgi->a({
2140 -href => href(
2141 action=>$dest_action,
2142 hash=>$dest
2143 )}, $name);
2145 $markers .= " <span class=\"".esc_attr($class)."\" title=\"".esc_attr($ref)."\">" .
2146 $link . "</span>";
2150 if ($markers) {
2151 return ' <span class="refs">'. $markers . '</span>';
2152 } else {
2153 return "";
2157 # format, perhaps shortened and with markers, title line
2158 sub format_subject_html {
2159 my ($long, $short, $href, $extra) = @_;
2160 $extra = '' unless defined($extra);
2162 if (length($short) < length($long)) {
2163 $long =~ s/[[:cntrl:]]/?/g;
2164 return $cgi->a({-href => $href, -class => "list subject",
2165 -title => to_utf8($long)},
2166 esc_html($short)) . $extra;
2167 } else {
2168 return $cgi->a({-href => $href, -class => "list subject"},
2169 esc_html($long)) . $extra;
2173 # Rather than recomputing the url for an email multiple times, we cache it
2174 # after the first hit. This gives a visible benefit in views where the avatar
2175 # for the same email is used repeatedly (e.g. shortlog).
2176 # The cache is shared by all avatar engines (currently gravatar only), which
2177 # are free to use it as preferred. Since only one avatar engine is used for any
2178 # given page, there's no risk for cache conflicts.
2179 our %avatar_cache = ();
2181 # Compute the picon url for a given email, by using the picon search service over at
2182 # http://www.cs.indiana.edu/picons/search.html
2183 sub picon_url {
2184 my $email = lc shift;
2185 if (!$avatar_cache{$email}) {
2186 my ($user, $domain) = split('@', $email);
2187 $avatar_cache{$email} =
2188 "//www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/" .
2189 "$domain/$user/" .
2190 "users+domains+unknown/up/single";
2192 return $avatar_cache{$email};
2195 # Compute the gravatar url for a given email, if it's not in the cache already.
2196 # Gravatar stores only the part of the URL before the size, since that's the
2197 # one computationally more expensive. This also allows reuse of the cache for
2198 # different sizes (for this particular engine).
2199 sub gravatar_url {
2200 my $email = lc shift;
2201 my $size = shift;
2202 $avatar_cache{$email} ||=
2203 "//www.gravatar.com/avatar/" .
2204 Digest::MD5::md5_hex($email) . "?s=";
2205 return $avatar_cache{$email} . $size;
2208 # Insert an avatar for the given $email at the given $size if the feature
2209 # is enabled.
2210 sub git_get_avatar {
2211 my ($email, %opts) = @_;
2212 my $pre_white = ($opts{-pad_before} ? "&nbsp;" : "");
2213 my $post_white = ($opts{-pad_after} ? "&nbsp;" : "");
2214 $opts{-size} ||= 'default';
2215 my $size = $avatar_size{$opts{-size}} || $avatar_size{'default'};
2216 my $url = "";
2217 if ($git_avatar eq 'gravatar') {
2218 $url = gravatar_url($email, $size);
2219 } elsif ($git_avatar eq 'picon') {
2220 $url = picon_url($email);
2222 # Other providers can be added by extending the if chain, defining $url
2223 # as needed. If no variant puts something in $url, we assume avatars
2224 # are completely disabled/unavailable.
2225 if ($url) {
2226 return $pre_white .
2227 "<img width=\"$size\" " .
2228 "class=\"avatar\" " .
2229 "src=\"".esc_url($url)."\" " .
2230 "alt=\"\" " .
2231 "/>" . $post_white;
2232 } else {
2233 return "";
2237 sub format_search_author {
2238 my ($author, $searchtype, $displaytext) = @_;
2239 my $have_search = gitweb_check_feature('search');
2241 if ($have_search) {
2242 my $performed = "";
2243 if ($searchtype eq 'author') {
2244 $performed = "authored";
2245 } elsif ($searchtype eq 'committer') {
2246 $performed = "committed";
2249 return $cgi->a({-href => href(action=>"search", hash=>$hash,
2250 searchtext=>$author,
2251 searchtype=>$searchtype), class=>"list",
2252 title=>"Search for commits $performed by $author"},
2253 $displaytext);
2255 } else {
2256 return $displaytext;
2260 # format the author name of the given commit with the given tag
2261 # the author name is chopped and escaped according to the other
2262 # optional parameters (see chop_str).
2263 sub format_author_html {
2264 my $tag = shift;
2265 my $co = shift;
2266 my $author = chop_and_escape_str($co->{'author_name'}, @_);
2267 return "<$tag class=\"author\">" .
2268 format_search_author($co->{'author_name'}, "author",
2269 git_get_avatar($co->{'author_email'}, -pad_after => 1) .
2270 $author) .
2271 "</$tag>";
2274 # format git diff header line, i.e. "diff --(git|combined|cc) ..."
2275 sub format_git_diff_header_line {
2276 my $line = shift;
2277 my $diffinfo = shift;
2278 my ($from, $to) = @_;
2280 if ($diffinfo->{'nparents'}) {
2281 # combined diff
2282 $line =~ s!^(diff (.*?) )"?.*$!$1!;
2283 if ($to->{'href'}) {
2284 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
2285 esc_path($to->{'file'}));
2286 } else { # file was deleted (no href)
2287 $line .= esc_path($to->{'file'});
2289 } else {
2290 # "ordinary" diff
2291 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
2292 if ($from->{'href'}) {
2293 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
2294 'a/' . esc_path($from->{'file'}));
2295 } else { # file was added (no href)
2296 $line .= 'a/' . esc_path($from->{'file'});
2298 $line .= ' ';
2299 if ($to->{'href'}) {
2300 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
2301 'b/' . esc_path($to->{'file'}));
2302 } else { # file was deleted
2303 $line .= 'b/' . esc_path($to->{'file'});
2307 return "<div class=\"diff header\">$line</div>\n";
2310 # format extended diff header line, before patch itself
2311 sub format_extended_diff_header_line {
2312 my $line = shift;
2313 my $diffinfo = shift;
2314 my ($from, $to) = @_;
2316 # match <path>
2317 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
2318 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
2319 esc_path($from->{'file'}));
2321 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
2322 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
2323 esc_path($to->{'file'}));
2325 # match single <mode>
2326 if ($line =~ m/\s(\d{6})$/) {
2327 $line .= '<span class="info"> (' .
2328 file_type_long($1) .
2329 ')</span>';
2331 # match <hash>
2332 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
2333 # can match only for combined diff
2334 $line = 'index ';
2335 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2336 if ($from->{'href'}[$i]) {
2337 $line .= $cgi->a({-href=>$from->{'href'}[$i],
2338 -class=>"hash"},
2339 substr($diffinfo->{'from_id'}[$i],0,7));
2340 } else {
2341 $line .= '0' x 7;
2343 # separator
2344 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
2346 $line .= '..';
2347 if ($to->{'href'}) {
2348 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
2349 substr($diffinfo->{'to_id'},0,7));
2350 } else {
2351 $line .= '0' x 7;
2354 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
2355 # can match only for ordinary diff
2356 my ($from_link, $to_link);
2357 if ($from->{'href'}) {
2358 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
2359 substr($diffinfo->{'from_id'},0,7));
2360 } else {
2361 $from_link = '0' x 7;
2363 if ($to->{'href'}) {
2364 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
2365 substr($diffinfo->{'to_id'},0,7));
2366 } else {
2367 $to_link = '0' x 7;
2369 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
2370 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
2373 return $line . "<br/>\n";
2376 # format from-file/to-file diff header
2377 sub format_diff_from_to_header {
2378 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
2379 my $line;
2380 my $result = '';
2382 $line = $from_line;
2383 #assert($line =~ m/^---/) if DEBUG;
2384 # no extra formatting for "^--- /dev/null"
2385 if (! $diffinfo->{'nparents'}) {
2386 # ordinary (single parent) diff
2387 if ($line =~ m!^--- "?a/!) {
2388 if ($from->{'href'}) {
2389 $line = '--- a/' .
2390 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
2391 esc_path($from->{'file'}));
2392 } else {
2393 $line = '--- a/' .
2394 esc_path($from->{'file'});
2397 $result .= qq!<div class="diff from_file">$line</div>\n!;
2399 } else {
2400 # combined diff (merge commit)
2401 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2402 if ($from->{'href'}[$i]) {
2403 $line = '--- ' .
2404 $cgi->a({-href=>href(action=>"blobdiff",
2405 hash_parent=>$diffinfo->{'from_id'}[$i],
2406 hash_parent_base=>$parents[$i],
2407 file_parent=>$from->{'file'}[$i],
2408 hash=>$diffinfo->{'to_id'},
2409 hash_base=>$hash,
2410 file_name=>$to->{'file'}),
2411 -class=>"path",
2412 -title=>"diff" . ($i+1)},
2413 $i+1) .
2414 '/' .
2415 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
2416 esc_path($from->{'file'}[$i]));
2417 } else {
2418 $line = '--- /dev/null';
2420 $result .= qq!<div class="diff from_file">$line</div>\n!;
2424 $line = $to_line;
2425 #assert($line =~ m/^\+\+\+/) if DEBUG;
2426 # no extra formatting for "^+++ /dev/null"
2427 if ($line =~ m!^\+\+\+ "?b/!) {
2428 if ($to->{'href'}) {
2429 $line = '+++ b/' .
2430 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
2431 esc_path($to->{'file'}));
2432 } else {
2433 $line = '+++ b/' .
2434 esc_path($to->{'file'});
2437 $result .= qq!<div class="diff to_file">$line</div>\n!;
2439 return $result;
2442 # create note for patch simplified by combined diff
2443 sub format_diff_cc_simplified {
2444 my ($diffinfo, @parents) = @_;
2445 my $result = '';
2447 $result .= "<div class=\"diff header\">" .
2448 "diff --cc ";
2449 if (!is_deleted($diffinfo)) {
2450 $result .= $cgi->a({-href => href(action=>"blob",
2451 hash_base=>$hash,
2452 hash=>$diffinfo->{'to_id'},
2453 file_name=>$diffinfo->{'to_file'}),
2454 -class => "path"},
2455 esc_path($diffinfo->{'to_file'}));
2456 } else {
2457 $result .= esc_path($diffinfo->{'to_file'});
2459 $result .= "</div>\n" . # class="diff header"
2460 "<div class=\"diff nodifferences\">" .
2461 "Simple merge" .
2462 "</div>\n"; # class="diff nodifferences"
2464 return $result;
2467 sub diff_line_class {
2468 my ($line, $from, $to) = @_;
2470 # ordinary diff
2471 my $num_sign = 1;
2472 # combined diff
2473 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
2474 $num_sign = scalar @{$from->{'href'}};
2477 my @diff_line_classifier = (
2478 { regexp => qr/^\@\@{$num_sign} /, class => "chunk_header"},
2479 { regexp => qr/^\\/, class => "incomplete" },
2480 { regexp => qr/^ {$num_sign}/, class => "ctx" },
2481 # classifier for context must come before classifier add/rem,
2482 # or we would have to use more complicated regexp, for example
2483 # qr/(?= {0,$m}\+)[+ ]{$num_sign}/, where $m = $num_sign - 1;
2484 { regexp => qr/^[+ ]{$num_sign}/, class => "add" },
2485 { regexp => qr/^[- ]{$num_sign}/, class => "rem" },
2487 for my $clsfy (@diff_line_classifier) {
2488 return $clsfy->{'class'}
2489 if ($line =~ $clsfy->{'regexp'});
2492 # fallback
2493 return "";
2496 # assumes that $from and $to are defined and correctly filled,
2497 # and that $line holds a line of chunk header for unified diff
2498 sub format_unidiff_chunk_header {
2499 my ($line, $from, $to) = @_;
2501 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
2502 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
2504 $from_lines = 0 unless defined $from_lines;
2505 $to_lines = 0 unless defined $to_lines;
2507 if ($from->{'href'}) {
2508 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
2509 -class=>"list"}, $from_text);
2511 if ($to->{'href'}) {
2512 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
2513 -class=>"list"}, $to_text);
2515 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
2516 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
2517 return $line;
2520 # assumes that $from and $to are defined and correctly filled,
2521 # and that $line holds a line of chunk header for combined diff
2522 sub format_cc_diff_chunk_header {
2523 my ($line, $from, $to) = @_;
2525 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
2526 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
2528 @from_text = split(' ', $ranges);
2529 for (my $i = 0; $i < @from_text; ++$i) {
2530 ($from_start[$i], $from_nlines[$i]) =
2531 (split(',', substr($from_text[$i], 1)), 0);
2534 $to_text = pop @from_text;
2535 $to_start = pop @from_start;
2536 $to_nlines = pop @from_nlines;
2538 $line = "<span class=\"chunk_info\">$prefix ";
2539 for (my $i = 0; $i < @from_text; ++$i) {
2540 if ($from->{'href'}[$i]) {
2541 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
2542 -class=>"list"}, $from_text[$i]);
2543 } else {
2544 $line .= $from_text[$i];
2546 $line .= " ";
2548 if ($to->{'href'}) {
2549 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
2550 -class=>"list"}, $to_text);
2551 } else {
2552 $line .= $to_text;
2554 $line .= " $prefix</span>" .
2555 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
2556 return $line;
2559 # process patch (diff) line (not to be used for diff headers),
2560 # returning HTML-formatted (but not wrapped) line.
2561 # If the line is passed as a reference, it is treated as HTML and not
2562 # esc_html()'ed.
2563 sub format_diff_line {
2564 my ($line, $diff_class, $from, $to) = @_;
2566 if (ref($line)) {
2567 $line = $$line;
2568 } else {
2569 chomp $line;
2570 $line = untabify($line);
2572 if ($from && $to && $line =~ m/^\@{2} /) {
2573 $line = format_unidiff_chunk_header($line, $from, $to);
2574 } elsif ($from && $to && $line =~ m/^\@{3}/) {
2575 $line = format_cc_diff_chunk_header($line, $from, $to);
2576 } else {
2577 $line = esc_html($line, -nbsp=>1);
2581 my $diff_classes = "diff";
2582 $diff_classes .= " $diff_class" if ($diff_class);
2583 $line = "<div class=\"$diff_classes\">$line</div>\n";
2585 return $line;
2588 # Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
2589 # linked. Pass the hash of the tree/commit to snapshot.
2590 sub format_snapshot_links {
2591 my ($hash) = @_;
2592 my $num_fmts = @snapshot_fmts;
2593 if ($num_fmts > 1) {
2594 # A parenthesized list of links bearing format names.
2595 # e.g. "snapshot (_tar.gz_ _zip_)"
2596 return "snapshot (" . join(' ', map
2597 $cgi->a({
2598 -href => href(
2599 action=>"snapshot",
2600 hash=>$hash,
2601 snapshot_format=>$_
2603 }, $known_snapshot_formats{$_}{'display'})
2604 , @snapshot_fmts) . ")";
2605 } elsif ($num_fmts == 1) {
2606 # A single "snapshot" link whose tooltip bears the format name.
2607 # i.e. "_snapshot_"
2608 my ($fmt) = @snapshot_fmts;
2609 return
2610 $cgi->a({
2611 -href => href(
2612 action=>"snapshot",
2613 hash=>$hash,
2614 snapshot_format=>$fmt
2616 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
2617 }, "snapshot");
2618 } else { # $num_fmts == 0
2619 return undef;
2623 ## ......................................................................
2624 ## functions returning values to be passed, perhaps after some
2625 ## transformation, to other functions; e.g. returning arguments to href()
2627 # returns hash to be passed to href to generate gitweb URL
2628 # in -title key it returns description of link
2629 sub get_feed_info {
2630 my $format = shift || 'Atom';
2631 my %res = (action => lc($format));
2632 my $matched_ref = 0;
2634 # feed links are possible only for project views
2635 return unless (defined $project);
2636 # some views should link to OPML, or to generic project feed,
2637 # or don't have specific feed yet (so they should use generic)
2638 return if (!$action || $action =~ /^(?:tags|heads|forks|tag|search)$/x);
2640 my $branch = undef;
2641 # branches refs uses 'refs/' + $get_branch_refs()[x] + '/' prefix
2642 # (fullname) to differentiate from tag links; this also makes
2643 # possible to detect branch links
2644 for my $ref (get_branch_refs()) {
2645 if ((defined $hash_base && $hash_base =~ m!^refs/\Q$ref\E/(.*)$!) ||
2646 (defined $hash && $hash =~ m!^refs/\Q$ref\E/(.*)$!)) {
2647 $branch = $1;
2648 $matched_ref = $ref;
2649 last;
2652 # find log type for feed description (title)
2653 my $type = 'log';
2654 if (defined $file_name) {
2655 $type = "history of $file_name";
2656 $type .= "/" if ($action eq 'tree');
2657 $type .= " on '$branch'" if (defined $branch);
2658 } else {
2659 $type = "log of $branch" if (defined $branch);
2662 $res{-title} = $type;
2663 $res{'hash'} = (defined $branch ? "refs/$matched_ref/$branch" : undef);
2664 $res{'file_name'} = $file_name;
2666 return %res;
2669 ## ----------------------------------------------------------------------
2670 ## git utility subroutines, invoking git commands
2672 # returns path to the core git executable and the --git-dir parameter as list
2673 sub git_cmd {
2674 $number_of_git_cmds++;
2675 return $GIT, '--git-dir='.$git_dir;
2678 # quote the given arguments for passing them to the shell
2679 # quote_command("command", "arg 1", "arg with ' and ! characters")
2680 # => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
2681 # Try to avoid using this function wherever possible.
2682 sub quote_command {
2683 return join(' ',
2684 map { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ );
2687 # get HEAD ref of given project as hash
2688 sub git_get_head_hash {
2689 return git_get_full_hash(shift, 'HEAD');
2692 sub git_get_full_hash {
2693 return git_get_hash(@_);
2696 sub git_get_short_hash {
2697 return git_get_hash(@_, '--short=7');
2700 sub git_get_hash {
2701 my ($project, $hash, @options) = @_;
2702 my $o_git_dir = $git_dir;
2703 my $retval = undef;
2704 $git_dir = "$projectroot/$project";
2705 if (open my $fd, '-|', git_cmd(), 'rev-parse',
2706 '--verify', '-q', @options, $hash) {
2707 $retval = <$fd>;
2708 chomp $retval if defined $retval;
2709 close $fd;
2711 if (defined $o_git_dir) {
2712 $git_dir = $o_git_dir;
2714 return $retval;
2717 # get type of given object
2718 sub git_get_type {
2719 my $hash = shift;
2721 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
2722 my $type = <$fd>;
2723 close $fd or return;
2724 chomp $type;
2725 return $type;
2728 # repository configuration
2729 our $config_file = '';
2730 our %config;
2732 # store multiple values for single key as anonymous array reference
2733 # single values stored directly in the hash, not as [ <value> ]
2734 sub hash_set_multi {
2735 my ($hash, $key, $value) = @_;
2737 if (!exists $hash->{$key}) {
2738 $hash->{$key} = $value;
2739 } elsif (!ref $hash->{$key}) {
2740 $hash->{$key} = [ $hash->{$key}, $value ];
2741 } else {
2742 push @{$hash->{$key}}, $value;
2746 # return hash of git project configuration
2747 # optionally limited to some section, e.g. 'gitweb'
2748 sub git_parse_project_config {
2749 my $section_regexp = shift;
2750 my %config;
2752 local $/ = "\0";
2754 open my $fh, "-|", git_cmd(), "config", '-z', '-l',
2755 or return;
2757 while (my $keyval = <$fh>) {
2758 chomp $keyval;
2759 my ($key, $value) = split(/\n/, $keyval, 2);
2761 hash_set_multi(\%config, $key, $value)
2762 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
2764 close $fh;
2766 return %config;
2769 # convert config value to boolean: 'true' or 'false'
2770 # no value, number > 0, 'true' and 'yes' values are true
2771 # rest of values are treated as false (never as error)
2772 sub config_to_bool {
2773 my $val = shift;
2775 return 1 if !defined $val; # section.key
2777 # strip leading and trailing whitespace
2778 $val =~ s/^\s+//;
2779 $val =~ s/\s+$//;
2781 return (($val =~ /^\d+$/ && $val) || # section.key = 1
2782 ($val =~ /^(?:true|yes)$/i)); # section.key = true
2785 # convert config value to simple decimal number
2786 # an optional value suffix of 'k', 'm', or 'g' will cause the value
2787 # to be multiplied by 1024, 1048576, or 1073741824
2788 sub config_to_int {
2789 my $val = shift;
2791 # strip leading and trailing whitespace
2792 $val =~ s/^\s+//;
2793 $val =~ s/\s+$//;
2795 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
2796 $unit = lc($unit);
2797 # unknown unit is treated as 1
2798 return $num * ($unit eq 'g' ? 1073741824 :
2799 $unit eq 'm' ? 1048576 :
2800 $unit eq 'k' ? 1024 : 1);
2802 return $val;
2805 # convert config value to array reference, if needed
2806 sub config_to_multi {
2807 my $val = shift;
2809 return ref($val) ? $val : (defined($val) ? [ $val ] : []);
2812 sub git_get_project_config {
2813 my ($key, $type) = @_;
2815 return unless defined $git_dir;
2817 # key sanity check
2818 return unless ($key);
2819 # only subsection, if exists, is case sensitive,
2820 # and not lowercased by 'git config -z -l'
2821 if (my ($hi, $mi, $lo) = ($key =~ /^([^.]*)\.(.*)\.([^.]*)$/)) {
2822 $lo =~ s/_//g;
2823 $key = join(".", lc($hi), $mi, lc($lo));
2824 return if ($lo =~ /\W/ || $hi =~ /\W/);
2825 } else {
2826 $key = lc($key);
2827 $key =~ s/_//g;
2828 return if ($key =~ /\W/);
2830 $key =~ s/^gitweb\.//;
2832 # type sanity check
2833 if (defined $type) {
2834 $type =~ s/^--//;
2835 $type = undef
2836 unless ($type eq 'bool' || $type eq 'int');
2839 # get config
2840 if (!defined $config_file ||
2841 $config_file ne "$git_dir/config") {
2842 %config = git_parse_project_config('gitweb');
2843 $config_file = "$git_dir/config";
2846 # check if config variable (key) exists
2847 return unless exists $config{"gitweb.$key"};
2849 # ensure given type
2850 if (!defined $type) {
2851 return $config{"gitweb.$key"};
2852 } elsif ($type eq 'bool') {
2853 # backward compatibility: 'git config --bool' returns true/false
2854 return config_to_bool($config{"gitweb.$key"}) ? 'true' : 'false';
2855 } elsif ($type eq 'int') {
2856 return config_to_int($config{"gitweb.$key"});
2858 return $config{"gitweb.$key"};
2861 # get hash of given path at given ref
2862 sub git_get_hash_by_path {
2863 my $base = shift;
2864 my $path = shift || return undef;
2865 my $type = shift;
2867 $path =~ s,/+$,,;
2869 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
2870 or die_error(500, "Open git-ls-tree failed");
2871 my $line = <$fd>;
2872 close $fd or return undef;
2874 if (!defined $line) {
2875 # there is no tree or hash given by $path at $base
2876 return undef;
2879 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2880 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
2881 if (defined $type && $type ne $2) {
2882 # type doesn't match
2883 return undef;
2885 return $3;
2888 # get path of entry with given hash at given tree-ish (ref)
2889 # used to get 'from' filename for combined diff (merge commit) for renames
2890 sub git_get_path_by_hash {
2891 my $base = shift || return;
2892 my $hash = shift || return;
2894 local $/ = "\0";
2896 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
2897 or return undef;
2898 while (my $line = <$fd>) {
2899 chomp $line;
2901 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
2902 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
2903 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
2904 close $fd;
2905 return $1;
2908 close $fd;
2909 return undef;
2912 ## ......................................................................
2913 ## git utility functions, directly accessing git repository
2915 # get the value of config variable either from file named as the variable
2916 # itself in the repository ($GIT_DIR/$name file), or from gitweb.$name
2917 # configuration variable in the repository config file.
2918 sub git_get_file_or_project_config {
2919 my ($path, $name) = @_;
2921 $git_dir = "$projectroot/$path";
2922 open my $fd, '<', "$git_dir/$name"
2923 or return git_get_project_config($name);
2924 my $conf = <$fd>;
2925 close $fd;
2926 if (defined $conf) {
2927 chomp $conf;
2929 return $conf;
2932 sub git_get_project_description {
2933 my $path = shift;
2934 return git_get_file_or_project_config($path, 'description');
2937 sub git_get_project_category {
2938 my $path = shift;
2939 return git_get_file_or_project_config($path, 'category');
2943 # supported formats:
2944 # * $GIT_DIR/ctags/<tagname> file (in 'ctags' subdirectory)
2945 # - if its contents is a number, use it as tag weight,
2946 # - otherwise add a tag with weight 1
2947 # * $GIT_DIR/ctags file, each line is a tag (with weight 1)
2948 # the same value multiple times increases tag weight
2949 # * `gitweb.ctag' multi-valued repo config variable
2950 sub git_get_project_ctags {
2951 my $project = shift;
2952 my $ctags = {};
2954 $git_dir = "$projectroot/$project";
2955 if (opendir my $dh, "$git_dir/ctags") {
2956 my @files = grep { -f $_ } map { "$git_dir/ctags/$_" } readdir($dh);
2957 foreach my $tagfile (@files) {
2958 open my $ct, '<', $tagfile
2959 or next;
2960 my $val = <$ct>;
2961 chomp $val if $val;
2962 close $ct;
2964 (my $ctag = $tagfile) =~ s#.*/##;
2965 if ($val =~ /^\d+$/) {
2966 $ctags->{$ctag} = $val;
2967 } else {
2968 $ctags->{$ctag} = 1;
2971 closedir $dh;
2973 } elsif (open my $fh, '<', "$git_dir/ctags") {
2974 while (my $line = <$fh>) {
2975 chomp $line;
2976 $ctags->{$line}++ if $line;
2978 close $fh;
2980 } else {
2981 my $taglist = config_to_multi(git_get_project_config('ctag'));
2982 foreach my $tag (@$taglist) {
2983 $ctags->{$tag}++;
2987 return $ctags;
2990 # return hash, where keys are content tags ('ctags'),
2991 # and values are sum of weights of given tag in every project
2992 sub git_gather_all_ctags {
2993 my $projects = shift;
2994 my $ctags = {};
2996 foreach my $p (@$projects) {
2997 foreach my $ct (keys %{$p->{'ctags'}}) {
2998 $ctags->{$ct} += $p->{'ctags'}->{$ct};
3002 return $ctags;
3005 sub git_populate_project_tagcloud {
3006 my ($ctags, $action) = @_;
3008 # First, merge different-cased tags; tags vote on casing
3009 my %ctags_lc;
3010 foreach (keys %$ctags) {
3011 $ctags_lc{lc $_}->{count} += $ctags->{$_};
3012 if (not $ctags_lc{lc $_}->{topcount}
3013 or $ctags_lc{lc $_}->{topcount} < $ctags->{$_}) {
3014 $ctags_lc{lc $_}->{topcount} = $ctags->{$_};
3015 $ctags_lc{lc $_}->{topname} = $_;
3019 my $cloud;
3020 my $matched = $input_params{'ctag_filter'};
3021 if (eval { require HTML::TagCloud; 1; }) {
3022 $cloud = HTML::TagCloud->new;
3023 foreach my $ctag (sort keys %ctags_lc) {
3024 # Pad the title with spaces so that the cloud looks
3025 # less crammed.
3026 my $title = esc_html($ctags_lc{$ctag}->{topname});
3027 $title =~ s/ /&nbsp;/g;
3028 $title =~ s/^/&nbsp;/g;
3029 $title =~ s/$/&nbsp;/g;
3030 if (defined $matched && $matched eq $ctag) {
3031 $title = qq(<span class="match">$title</span>);
3033 $cloud->add($title, href(-replay=>1, action=>$action, ctag_filter=>$ctag),
3034 $ctags_lc{$ctag}->{count});
3036 } else {
3037 $cloud = {};
3038 foreach my $ctag (keys %ctags_lc) {
3039 my $title = esc_html($ctags_lc{$ctag}->{topname}, -nbsp=>1);
3040 if (defined $matched && $matched eq $ctag) {
3041 $title = qq(<span class="match">$title</span>);
3043 $cloud->{$ctag}{count} = $ctags_lc{$ctag}->{count};
3044 $cloud->{$ctag}{ctag} =
3045 $cgi->a({-href=>href(-replay=>1, action=>$action, ctag_filter=>$ctag)}, $title);
3048 return $cloud;
3051 sub git_show_project_tagcloud {
3052 my ($cloud, $count) = @_;
3053 if (ref $cloud eq 'HTML::TagCloud') {
3054 return $cloud->html_and_css($count);
3055 } else {
3056 my @tags = sort { $cloud->{$a}->{'count'} <=> $cloud->{$b}->{'count'} } keys %$cloud;
3057 return
3058 '<div id="htmltagcloud"'.($project ? '' : ' align="center"').'>' .
3059 join (', ', map {
3060 $cloud->{$_}->{'ctag'}
3061 } splice(@tags, 0, $count)) .
3062 '</div>';
3066 sub git_get_project_url_list {
3067 my $path = shift;
3069 $git_dir = "$projectroot/$path";
3070 open my $fd, '<', "$git_dir/cloneurl"
3071 or return wantarray ?
3072 @{ config_to_multi(git_get_project_config('url')) } :
3073 config_to_multi(git_get_project_config('url'));
3074 my @git_project_url_list = map { chomp; $_ } <$fd>;
3075 close $fd;
3077 return wantarray ? @git_project_url_list : \@git_project_url_list;
3080 sub git_get_projects_list {
3081 my $filter = shift || '';
3082 my $paranoid = shift;
3083 my @list;
3085 if (-d $projects_list) {
3086 # search in directory
3087 my $dir = $projects_list;
3088 # remove the trailing "/"
3089 $dir =~ s!/+$!!;
3090 my $pfxlen = length("$dir");
3091 my $pfxdepth = ($dir =~ tr!/!!);
3092 # when filtering, search only given subdirectory
3093 if ($filter && !$paranoid) {
3094 $dir .= "/$filter";
3095 $dir =~ s!/+$!!;
3098 File::Find::find({
3099 follow_fast => 1, # follow symbolic links
3100 follow_skip => 2, # ignore duplicates
3101 dangling_symlinks => 0, # ignore dangling symlinks, silently
3102 wanted => sub {
3103 # global variables
3104 our $project_maxdepth;
3105 our $projectroot;
3106 # skip project-list toplevel, if we get it.
3107 return if (m!^[/.]$!);
3108 # only directories can be git repositories
3109 return unless (-d $_);
3110 # don't traverse too deep (Find is super slow on os x)
3111 # $project_maxdepth excludes depth of $projectroot
3112 if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
3113 $File::Find::prune = 1;
3114 return;
3117 my $path = substr($File::Find::name, $pfxlen + 1);
3118 # paranoidly only filter here
3119 if ($paranoid && $filter && $path !~ m!^\Q$filter\E/!) {
3120 next;
3122 # we check related file in $projectroot
3123 if (check_export_ok("$projectroot/$path")) {
3124 push @list, { path => $path };
3125 $File::Find::prune = 1;
3128 }, "$dir");
3130 } elsif (-f $projects_list) {
3131 # read from file(url-encoded):
3132 # 'git%2Fgit.git Linus+Torvalds'
3133 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
3134 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
3135 open my $fd, '<', $projects_list or return;
3136 PROJECT:
3137 while (my $line = <$fd>) {
3138 chomp $line;
3139 my ($path, $owner) = split ' ', $line;
3140 $path = unescape($path);
3141 $owner = unescape($owner);
3142 if (!defined $path) {
3143 next;
3145 # if $filter is rpovided, check if $path begins with $filter
3146 if ($filter && $path !~ m!^\Q$filter\E/!) {
3147 next;
3149 if (check_export_ok("$projectroot/$path")) {
3150 my $pr = {
3151 path => $path
3153 if ($owner) {
3154 $pr->{'owner'} = to_utf8($owner);
3156 push @list, $pr;
3159 close $fd;
3161 return @list;
3164 # written with help of Tree::Trie module (Perl Artistic License, GPL compatibile)
3165 # as side effects it sets 'forks' field to list of forks for forked projects
3166 sub filter_forks_from_projects_list {
3167 my $projects = shift;
3169 my %trie; # prefix tree of directories (path components)
3170 # generate trie out of those directories that might contain forks
3171 foreach my $pr (@$projects) {
3172 my $path = $pr->{'path'};
3173 $path =~ s/\.git$//; # forks of 'repo.git' are in 'repo/' directory
3174 next if ($path =~ m!/$!); # skip non-bare repositories, e.g. 'repo/.git'
3175 next unless ($path); # skip '.git' repository: tests, git-instaweb
3176 next unless (-d "$projectroot/$path"); # containing directory exists
3177 $pr->{'forks'} = []; # there can be 0 or more forks of project
3179 # add to trie
3180 my @dirs = split('/', $path);
3181 # walk the trie, until either runs out of components or out of trie
3182 my $ref = \%trie;
3183 while (scalar @dirs &&
3184 exists($ref->{$dirs[0]})) {
3185 $ref = $ref->{shift @dirs};
3187 # create rest of trie structure from rest of components
3188 foreach my $dir (@dirs) {
3189 $ref = $ref->{$dir} = {};
3191 # create end marker, store $pr as a data
3192 $ref->{''} = $pr if (!exists $ref->{''});
3195 # filter out forks, by finding shortest prefix match for paths
3196 my @filtered;
3197 PROJECT:
3198 foreach my $pr (@$projects) {
3199 # trie lookup
3200 my $ref = \%trie;
3201 DIR:
3202 foreach my $dir (split('/', $pr->{'path'})) {
3203 if (exists $ref->{''}) {
3204 # found [shortest] prefix, is a fork - skip it
3205 push @{$ref->{''}{'forks'}}, $pr;
3206 next PROJECT;
3208 if (!exists $ref->{$dir}) {
3209 # not in trie, cannot have prefix, not a fork
3210 push @filtered, $pr;
3211 next PROJECT;
3213 # If the dir is there, we just walk one step down the trie.
3214 $ref = $ref->{$dir};
3216 # we ran out of trie
3217 # (shouldn't happen: it's either no match, or end marker)
3218 push @filtered, $pr;
3221 return @filtered;
3224 # note: fill_project_list_info must be run first,
3225 # for 'descr_long' and 'ctags' to be filled
3226 sub search_projects_list {
3227 my ($projlist, %opts) = @_;
3228 my $tagfilter = $opts{'tagfilter'};
3229 my $search_re = $opts{'search_regexp'};
3231 return @$projlist
3232 unless ($tagfilter || $search_re);
3234 # searching projects require filling to be run before it;
3235 fill_project_list_info($projlist,
3236 $tagfilter ? 'ctags' : (),
3237 $search_re ? ('path', 'descr') : ());
3238 my @projects;
3239 PROJECT:
3240 foreach my $pr (@$projlist) {
3242 if ($tagfilter) {
3243 next unless ref($pr->{'ctags'}) eq 'HASH';
3244 next unless
3245 grep { lc($_) eq lc($tagfilter) } keys %{$pr->{'ctags'}};
3248 if ($search_re) {
3249 next unless
3250 $pr->{'path'} =~ /$search_re/ ||
3251 $pr->{'descr_long'} =~ /$search_re/;
3254 push @projects, $pr;
3257 return @projects;
3260 our $gitweb_project_owner = undef;
3261 sub git_get_project_list_from_file {
3263 return if (defined $gitweb_project_owner);
3265 $gitweb_project_owner = {};
3266 # read from file (url-encoded):
3267 # 'git%2Fgit.git Linus+Torvalds'
3268 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
3269 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
3270 if (-f $projects_list) {
3271 open(my $fd, '<', $projects_list);
3272 while (my $line = <$fd>) {
3273 chomp $line;
3274 my ($pr, $ow) = split ' ', $line;
3275 $pr = unescape($pr);
3276 $ow = unescape($ow);
3277 $gitweb_project_owner->{$pr} = to_utf8($ow);
3279 close $fd;
3283 sub git_get_project_owner {
3284 my $project = shift;
3285 my $owner;
3287 return undef unless $project;
3288 $git_dir = "$projectroot/$project";
3290 if (!defined $gitweb_project_owner) {
3291 git_get_project_list_from_file();
3294 if (exists $gitweb_project_owner->{$project}) {
3295 $owner = $gitweb_project_owner->{$project};
3297 if (!defined $owner){
3298 $owner = git_get_project_config('owner');
3300 if (!defined $owner) {
3301 $owner = get_file_owner("$git_dir");
3304 return $owner;
3307 sub parse_activity_date {
3308 my $dstr = shift;
3310 if ($dstr =~ /^\s*([-+]?\d+)(?:\s+([-+]\d{4}))?\s*$/) {
3311 # Unix timestamp
3312 return 0 + $1;
3314 if ($dstr =~ /^\s*(\d{4})-(\d{2})-(\d{2})[Tt _](\d{1,2}):(\d{2}):(\d{2})(?:[ _]?([Zz]|(?:[-+]\d{1,2}:?\d{2})))?\s*$/) {
3315 my ($Y,$m,$d,$H,$M,$S,$z) = ($1,$2,$3,$4,$5,$6,$7||'');
3316 my $seconds = timegm(0+$S, 0+$M, 0+$H, 0+$d, $m-1, $Y-1900);
3317 defined($z) && $z ne '' or $z = 'Z';
3318 $z =~ s/://;
3319 substr($z,1,0) = '0' if length($z) == 4;
3320 my $off = 0;
3321 if (uc($z) ne 'Z') {
3322 $off = 60 * (60 * (0+substr($z,1,2)) + (0+substr($z,3,2)));
3323 $off = -$off if substr($z,0,1) eq '-';
3325 return $seconds - $off;
3327 return undef;
3330 # If $quick is true only look at $lastactivity_file
3331 sub git_get_last_activity {
3332 my ($path, $quick) = @_;
3333 my $fd;
3335 $git_dir = "$projectroot/$path";
3336 if ($lastactivity_file && open($fd, "<", "$git_dir/$lastactivity_file")) {
3337 my $activity = <$fd>;
3338 close $fd;
3339 return (undef, undef) unless defined $activity;
3340 chomp $activity;
3341 return (undef, undef) if $activity eq '';
3342 if (my $timestamp = parse_activity_date($activity)) {
3343 my $age = time - $timestamp;
3344 return ($age, age_string($age));
3347 return (undef, undef) if $quick;
3348 open($fd, "-|", git_cmd(), 'for-each-ref',
3349 '--format=%(committer)',
3350 '--sort=-committerdate',
3351 '--count=1',
3352 map { "refs/$_" } get_branch_refs ()) or return;
3353 my $most_recent = <$fd>;
3354 close $fd or return;
3355 if (defined $most_recent &&
3356 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
3357 my $timestamp = $1;
3358 my $age = time - $timestamp;
3359 return ($age, age_string($age));
3361 return (undef, undef);
3364 # Implementation note: when a single remote is wanted, we cannot use 'git
3365 # remote show -n' because that command always work (assuming it's a remote URL
3366 # if it's not defined), and we cannot use 'git remote show' because that would
3367 # try to make a network roundtrip. So the only way to find if that particular
3368 # remote is defined is to walk the list provided by 'git remote -v' and stop if
3369 # and when we find what we want.
3370 sub git_get_remotes_list {
3371 my $wanted = shift;
3372 my %remotes = ();
3374 open my $fd, '-|' , git_cmd(), 'remote', '-v';
3375 return unless $fd;
3376 while (my $remote = <$fd>) {
3377 chomp $remote;
3378 $remote =~ s!\t(.*?)\s+\((\w+)\)$!!;
3379 next if $wanted and not $remote eq $wanted;
3380 my ($url, $key) = ($1, $2);
3382 $remotes{$remote} ||= { 'heads' => () };
3383 $remotes{$remote}{$key} = $url;
3385 close $fd or return;
3386 return wantarray ? %remotes : \%remotes;
3389 # Takes a hash of remotes as first parameter and fills it by adding the
3390 # available remote heads for each of the indicated remotes.
3391 sub fill_remote_heads {
3392 my $remotes = shift;
3393 my @heads = map { "remotes/$_" } keys %$remotes;
3394 my @remoteheads = git_get_heads_list(undef, @heads);
3395 foreach my $remote (keys %$remotes) {
3396 $remotes->{$remote}{'heads'} = [ grep {
3397 $_->{'name'} =~ s!^$remote/!!
3398 } @remoteheads ];
3402 sub git_get_references {
3403 my $type = shift || "";
3404 my %refs;
3405 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
3406 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
3407 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
3408 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
3409 or return;
3411 while (my $line = <$fd>) {
3412 chomp $line;
3413 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {
3414 if (defined $refs{$1}) {
3415 push @{$refs{$1}}, $2;
3416 } else {
3417 $refs{$1} = [ $2 ];
3421 close $fd or return;
3422 return \%refs;
3425 sub git_get_rev_name_tags {
3426 my $hash = shift || return undef;
3428 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
3429 or return;
3430 my $name_rev = <$fd>;
3431 close $fd;
3433 if ($name_rev =~ m|^$hash tags/(.*)$|) {
3434 return $1;
3435 } else {
3436 # catches also '$hash undefined' output
3437 return undef;
3441 ## ----------------------------------------------------------------------
3442 ## parse to hash functions
3444 sub parse_date {
3445 my $epoch = shift;
3446 my $tz = shift || "-0000";
3448 my %date;
3449 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
3450 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
3451 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
3452 $date{'hour'} = $hour;
3453 $date{'minute'} = $min;
3454 $date{'mday'} = $mday;
3455 $date{'day'} = $days[$wday];
3456 $date{'month'} = $months[$mon];
3457 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
3458 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
3459 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
3460 $mday, $months[$mon], $hour ,$min;
3461 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
3462 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
3464 my ($tz_sign, $tz_hour, $tz_min) =
3465 ($tz =~ m/^([-+])(\d\d)(\d\d)$/);
3466 $tz_sign = ($tz_sign eq '-' ? -1 : +1);
3467 my $local = $epoch + $tz_sign*((($tz_hour*60) + $tz_min)*60);
3468 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
3469 $date{'hour_local'} = $hour;
3470 $date{'minute_local'} = $min;
3471 $date{'tz_local'} = $tz;
3472 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
3473 1900+$year, $mon+1, $mday,
3474 $hour, $min, $sec, $tz);
3475 return %date;
3478 sub parse_tag {
3479 my $tag_id = shift;
3480 my %tag;
3481 my @comment;
3483 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
3484 $tag{'id'} = $tag_id;
3485 while (my $line = <$fd>) {
3486 chomp $line;
3487 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
3488 $tag{'object'} = $1;
3489 } elsif ($line =~ m/^type (.+)$/) {
3490 $tag{'type'} = $1;
3491 } elsif ($line =~ m/^tag (.+)$/) {
3492 $tag{'name'} = $1;
3493 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
3494 $tag{'author'} = $1;
3495 $tag{'author_epoch'} = $2;
3496 $tag{'author_tz'} = $3;
3497 if ($tag{'author'} =~ m/^([^<]+) <([^>]*)>/) {
3498 $tag{'author_name'} = $1;
3499 $tag{'author_email'} = $2;
3500 } else {
3501 $tag{'author_name'} = $tag{'author'};
3503 } elsif ($line =~ m/--BEGIN/) {
3504 push @comment, $line;
3505 last;
3506 } elsif ($line eq "") {
3507 last;
3510 push @comment, <$fd>;
3511 $tag{'comment'} = \@comment;
3512 close $fd or return;
3513 if (!defined $tag{'name'}) {
3514 return
3516 return %tag
3519 sub parse_commit_text {
3520 my ($commit_text, $withparents) = @_;
3521 my @commit_lines = split '\n', $commit_text;
3522 my %co;
3524 pop @commit_lines; # Remove '\0'
3526 if (! @commit_lines) {
3527 return;
3530 my $header = shift @commit_lines;
3531 if ($header !~ m/^[0-9a-fA-F]{40}/) {
3532 return;
3534 ($co{'id'}, my @parents) = split ' ', $header;
3535 while (my $line = shift @commit_lines) {
3536 last if $line eq "\n";
3537 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
3538 $co{'tree'} = $1;
3539 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
3540 push @parents, $1;
3541 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
3542 $co{'author'} = to_utf8($1);
3543 $co{'author_epoch'} = $2;
3544 $co{'author_tz'} = $3;
3545 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
3546 $co{'author_name'} = $1;
3547 $co{'author_email'} = $2;
3548 } else {
3549 $co{'author_name'} = $co{'author'};
3551 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
3552 $co{'committer'} = to_utf8($1);
3553 $co{'committer_epoch'} = $2;
3554 $co{'committer_tz'} = $3;
3555 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
3556 $co{'committer_name'} = $1;
3557 $co{'committer_email'} = $2;
3558 } else {
3559 $co{'committer_name'} = $co{'committer'};
3563 if (!defined $co{'tree'}) {
3564 return;
3566 $co{'parents'} = \@parents;
3567 $co{'parent'} = $parents[0];
3569 foreach my $title (@commit_lines) {
3570 $title =~ s/^ //;
3571 if ($title ne "") {
3572 $co{'title'} = chop_str($title, 80, 5);
3573 # remove leading stuff of merges to make the interesting part visible
3574 if (length($title) > 50) {
3575 $title =~ s/^Automatic //;
3576 $title =~ s/^merge (of|with) /Merge ... /i;
3577 if (length($title) > 50) {
3578 $title =~ s/(http|rsync):\/\///;
3580 if (length($title) > 50) {
3581 $title =~ s/(master|www|rsync)\.//;
3583 if (length($title) > 50) {
3584 $title =~ s/kernel.org:?//;
3586 if (length($title) > 50) {
3587 $title =~ s/\/pub\/scm//;
3590 $co{'title_short'} = chop_str($title, 50, 5);
3591 last;
3594 if (! defined $co{'title'} || $co{'title'} eq "") {
3595 $co{'title'} = $co{'title_short'} = '(no commit message)';
3597 # remove added spaces
3598 foreach my $line (@commit_lines) {
3599 $line =~ s/^ //;
3601 $co{'comment'} = \@commit_lines;
3603 my $age = time - $co{'committer_epoch'};
3604 $co{'age'} = $age;
3605 $co{'age_string'} = age_string($age);
3606 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
3607 if ($age > 60*60*24*7*2) {
3608 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
3609 $co{'age_string_age'} = $co{'age_string'};
3610 } else {
3611 $co{'age_string_date'} = $co{'age_string'};
3612 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
3614 return %co;
3617 sub parse_commit {
3618 my ($commit_id) = @_;
3619 my %co;
3621 local $/ = "\0";
3623 open my $fd, "-|", git_cmd(), "rev-list",
3624 "--parents",
3625 "--header",
3626 "--max-count=1",
3627 $commit_id,
3628 "--",
3629 or die_error(500, "Open git-rev-list failed");
3630 %co = parse_commit_text(<$fd>, 1);
3631 close $fd;
3633 return %co;
3636 sub parse_commits {
3637 my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
3638 my @cos;
3640 $maxcount ||= 1;
3641 $skip ||= 0;
3643 local $/ = "\0";
3645 open my $fd, "-|", git_cmd(), "rev-list",
3646 "--header",
3647 @args,
3648 ("--max-count=" . $maxcount),
3649 ("--skip=" . $skip),
3650 @extra_options,
3651 $commit_id,
3652 "--",
3653 ($filename ? ($filename) : ())
3654 or die_error(500, "Open git-rev-list failed");
3655 while (my $line = <$fd>) {
3656 my %co = parse_commit_text($line);
3657 push @cos, \%co;
3659 close $fd;
3661 return wantarray ? @cos : \@cos;
3664 # parse line of git-diff-tree "raw" output
3665 sub parse_difftree_raw_line {
3666 my $line = shift;
3667 my %res;
3669 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
3670 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
3671 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
3672 $res{'from_mode'} = $1;
3673 $res{'to_mode'} = $2;
3674 $res{'from_id'} = $3;
3675 $res{'to_id'} = $4;
3676 $res{'status'} = $5;
3677 $res{'similarity'} = $6;
3678 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
3679 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
3680 } else {
3681 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote($7);
3684 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
3685 # combined diff (for merge commit)
3686 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
3687 $res{'nparents'} = length($1);
3688 $res{'from_mode'} = [ split(' ', $2) ];
3689 $res{'to_mode'} = pop @{$res{'from_mode'}};
3690 $res{'from_id'} = [ split(' ', $3) ];
3691 $res{'to_id'} = pop @{$res{'from_id'}};
3692 $res{'status'} = [ split('', $4) ];
3693 $res{'to_file'} = unquote($5);
3695 # 'c512b523472485aef4fff9e57b229d9d243c967f'
3696 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
3697 $res{'commit'} = $1;
3700 return wantarray ? %res : \%res;
3703 # wrapper: return parsed line of git-diff-tree "raw" output
3704 # (the argument might be raw line, or parsed info)
3705 sub parsed_difftree_line {
3706 my $line_or_ref = shift;
3708 if (ref($line_or_ref) eq "HASH") {
3709 # pre-parsed (or generated by hand)
3710 return $line_or_ref;
3711 } else {
3712 return parse_difftree_raw_line($line_or_ref);
3716 # parse line of git-ls-tree output
3717 sub parse_ls_tree_line {
3718 my $line = shift;
3719 my %opts = @_;
3720 my %res;
3722 if ($opts{'-l'}) {
3723 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'
3724 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;
3726 $res{'mode'} = $1;
3727 $res{'type'} = $2;
3728 $res{'hash'} = $3;
3729 $res{'size'} = $4;
3730 if ($opts{'-z'}) {
3731 $res{'name'} = $5;
3732 } else {
3733 $res{'name'} = unquote($5);
3735 } else {
3736 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
3737 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
3739 $res{'mode'} = $1;
3740 $res{'type'} = $2;
3741 $res{'hash'} = $3;
3742 if ($opts{'-z'}) {
3743 $res{'name'} = $4;
3744 } else {
3745 $res{'name'} = unquote($4);
3749 return wantarray ? %res : \%res;
3752 # generates _two_ hashes, references to which are passed as 2 and 3 argument
3753 sub parse_from_to_diffinfo {
3754 my ($diffinfo, $from, $to, @parents) = @_;
3756 if ($diffinfo->{'nparents'}) {
3757 # combined diff
3758 $from->{'file'} = [];
3759 $from->{'href'} = [];
3760 fill_from_file_info($diffinfo, @parents)
3761 unless exists $diffinfo->{'from_file'};
3762 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
3763 $from->{'file'}[$i] =
3764 defined $diffinfo->{'from_file'}[$i] ?
3765 $diffinfo->{'from_file'}[$i] :
3766 $diffinfo->{'to_file'};
3767 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
3768 $from->{'href'}[$i] = href(action=>"blob",
3769 hash_base=>$parents[$i],
3770 hash=>$diffinfo->{'from_id'}[$i],
3771 file_name=>$from->{'file'}[$i]);
3772 } else {
3773 $from->{'href'}[$i] = undef;
3776 } else {
3777 # ordinary (not combined) diff
3778 $from->{'file'} = $diffinfo->{'from_file'};
3779 if ($diffinfo->{'status'} ne "A") { # not new (added) file
3780 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
3781 hash=>$diffinfo->{'from_id'},
3782 file_name=>$from->{'file'});
3783 } else {
3784 delete $from->{'href'};
3788 $to->{'file'} = $diffinfo->{'to_file'};
3789 if (!is_deleted($diffinfo)) { # file exists in result
3790 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
3791 hash=>$diffinfo->{'to_id'},
3792 file_name=>$to->{'file'});
3793 } else {
3794 delete $to->{'href'};
3798 ## ......................................................................
3799 ## parse to array of hashes functions
3801 sub git_get_heads_list {
3802 my ($limit, @classes) = @_;
3803 @classes = get_branch_refs() unless @classes;
3804 my @patterns = map { "refs/$_" } @classes;
3805 my @headslist;
3807 open my $fd, '-|', git_cmd(), 'for-each-ref',
3808 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
3809 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
3810 @patterns
3811 or return;
3812 while (my $line = <$fd>) {
3813 my %ref_item;
3815 chomp $line;
3816 my ($refinfo, $committerinfo) = split(/\0/, $line);
3817 my ($hash, $name, $title) = split(' ', $refinfo, 3);
3818 my ($committer, $epoch, $tz) =
3819 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
3820 $ref_item{'fullname'} = $name;
3821 my $strip_refs = join '|', map { quotemeta } get_branch_refs();
3822 $name =~ s!^refs/($strip_refs|remotes)/!!;
3823 $ref_item{'name'} = $name;
3824 # for refs neither in 'heads' nor 'remotes' we want to
3825 # show their ref dir
3826 my $ref_dir = (defined $1) ? $1 : '';
3827 if ($ref_dir ne '' and $ref_dir ne 'heads' and $ref_dir ne 'remotes') {
3828 $ref_item{'name'} .= ' (' . $ref_dir . ')';
3831 $ref_item{'id'} = $hash;
3832 $ref_item{'title'} = $title || '(no commit message)';
3833 $ref_item{'epoch'} = $epoch;
3834 if ($epoch) {
3835 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
3836 } else {
3837 $ref_item{'age'} = "unknown";
3840 push @headslist, \%ref_item;
3842 close $fd;
3844 return wantarray ? @headslist : \@headslist;
3847 sub git_get_tags_list {
3848 my $limit = shift;
3849 my @tagslist;
3851 open my $fd, '-|', git_cmd(), 'for-each-ref',
3852 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
3853 '--format=%(objectname) %(objecttype) %(refname) '.
3854 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
3855 'refs/tags'
3856 or return;
3857 while (my $line = <$fd>) {
3858 my %ref_item;
3860 chomp $line;
3861 my ($refinfo, $creatorinfo) = split(/\0/, $line);
3862 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
3863 my ($creator, $epoch, $tz) =
3864 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
3865 $ref_item{'fullname'} = $name;
3866 $name =~ s!^refs/tags/!!;
3868 $ref_item{'type'} = $type;
3869 $ref_item{'id'} = $id;
3870 $ref_item{'name'} = $name;
3871 if ($type eq "tag") {
3872 $ref_item{'subject'} = $title;
3873 $ref_item{'reftype'} = $reftype;
3874 $ref_item{'refid'} = $refid;
3875 } else {
3876 $ref_item{'reftype'} = $type;
3877 $ref_item{'refid'} = $id;
3880 if ($type eq "tag" || $type eq "commit") {
3881 $ref_item{'epoch'} = $epoch;
3882 if ($epoch) {
3883 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
3884 } else {
3885 $ref_item{'age'} = "unknown";
3889 push @tagslist, \%ref_item;
3891 close $fd;
3893 return wantarray ? @tagslist : \@tagslist;
3896 ## ----------------------------------------------------------------------
3897 ## filesystem-related functions
3899 sub get_file_owner {
3900 my $path = shift;
3902 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
3903 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
3904 if (!defined $gcos) {
3905 return undef;
3907 my $owner = $gcos;
3908 $owner =~ s/[,;].*$//;
3909 return to_utf8($owner);
3912 # assume that file exists
3913 sub insert_file {
3914 my $filename = shift;
3916 open my $fd, '<', $filename;
3917 print map { to_utf8($_) } <$fd>;
3918 close $fd;
3921 ## ......................................................................
3922 ## mimetype related functions
3924 sub mimetype_guess_file {
3925 my $filename = shift;
3926 my $mimemap = shift;
3927 -r $mimemap or return undef;
3929 my %mimemap;
3930 open(my $mh, '<', $mimemap) or return undef;
3931 while (<$mh>) {
3932 next if m/^#/; # skip comments
3933 my ($mimetype, @exts) = split(/\s+/);
3934 foreach my $ext (@exts) {
3935 $mimemap{$ext} = $mimetype;
3938 close($mh);
3940 $filename =~ /\.([^.]*)$/;
3941 return $mimemap{$1};
3944 sub mimetype_guess {
3945 my $filename = shift;
3946 my $mime;
3947 $filename =~ /\./ or return undef;
3949 if ($mimetypes_file) {
3950 my $file = $mimetypes_file;
3951 if ($file !~ m!^/!) { # if it is relative path
3952 # it is relative to project
3953 $file = "$projectroot/$project/$file";
3955 $mime = mimetype_guess_file($filename, $file);
3957 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
3958 return $mime;
3961 sub blob_mimetype {
3962 my $fd = shift;
3963 my $filename = shift;
3965 if ($filename) {
3966 my $mime = mimetype_guess($filename);
3967 $mime and return $mime;
3970 # just in case
3971 return $default_blob_plain_mimetype unless $fd;
3973 if (-T $fd) {
3974 return 'text/plain';
3975 } elsif (! $filename) {
3976 return 'application/octet-stream';
3977 } elsif ($filename =~ m/\.png$/i) {
3978 return 'image/png';
3979 } elsif ($filename =~ m/\.gif$/i) {
3980 return 'image/gif';
3981 } elsif ($filename =~ m/\.jpe?g$/i) {
3982 return 'image/jpeg';
3983 } else {
3984 return 'application/octet-stream';
3988 sub blob_contenttype {
3989 my ($fd, $file_name, $type) = @_;
3991 $type ||= blob_mimetype($fd, $file_name);
3992 if ($type eq 'text/plain' && defined $default_text_plain_charset) {
3993 $type .= "; charset=$default_text_plain_charset";
3996 return $type;
3999 # guess file syntax for syntax highlighting; return undef if no highlighting
4000 # the name of syntax can (in the future) depend on syntax highlighter used
4001 sub guess_file_syntax {
4002 my ($highlight, $mimetype, $file_name) = @_;
4003 return undef unless ($highlight && defined $file_name);
4004 my $basename = basename($file_name, '.in');
4005 return $highlight_basename{$basename}
4006 if exists $highlight_basename{$basename};
4008 $basename =~ /\.([^.]*)$/;
4009 my $ext = $1 or return undef;
4010 return $highlight_ext{$ext}
4011 if exists $highlight_ext{$ext};
4013 return undef;
4016 # run highlighter and return FD of its output,
4017 # or return original FD if no highlighting
4018 sub run_highlighter {
4019 my ($fd, $highlight, $syntax) = @_;
4020 return $fd unless ($highlight && defined $syntax);
4022 close $fd;
4023 open $fd, quote_command(git_cmd(), "cat-file", "blob", $hash)." | ".
4024 quote_command($highlight_bin).
4025 " --replace-tabs=8 --fragment --syntax $syntax |"
4026 or die_error(500, "Couldn't open file or run syntax highlighter");
4027 return $fd;
4030 ## ======================================================================
4031 ## functions printing HTML: header, footer, error page
4033 sub get_page_title {
4034 my $title = to_utf8($site_name);
4036 unless (defined $project) {
4037 if (defined $project_filter) {
4038 $title .= " - projects in '" . esc_path($project_filter) . "'";
4040 return $title;
4042 $title .= " - " . to_utf8($project);
4044 return $title unless (defined $action);
4045 $title .= "/$action"; # $action is US-ASCII (7bit ASCII)
4047 return $title unless (defined $file_name);
4048 $title .= " - " . esc_path($file_name);
4049 if ($action eq "tree" && $file_name !~ m|/$|) {
4050 $title .= "/";
4053 return $title;
4056 sub get_content_type_html {
4057 # require explicit support from the UA if we are to send the page as
4058 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
4059 # we have to do this because MSIE sometimes globs '*/*', pretending to
4060 # support xhtml+xml but choking when it gets what it asked for.
4061 if (defined $cgi->http('HTTP_ACCEPT') &&
4062 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
4063 $cgi->Accept('application/xhtml+xml') != 0) {
4064 return 'application/xhtml+xml';
4065 } else {
4066 return 'text/html';
4070 sub print_feed_meta {
4071 if (defined $project) {
4072 my %href_params = get_feed_info();
4073 if (!exists $href_params{'-title'}) {
4074 $href_params{'-title'} = 'log';
4077 foreach my $format (qw(RSS Atom)) {
4078 my $type = lc($format);
4079 my %link_attr = (
4080 '-rel' => 'alternate',
4081 '-title' => esc_attr("$project - $href_params{'-title'} - $format feed"),
4082 '-type' => "application/$type+xml"
4085 $href_params{'extra_options'} = undef;
4086 $href_params{'action'} = $type;
4087 $link_attr{'-href'} = href(%href_params);
4088 print "<link ".
4089 "rel=\"$link_attr{'-rel'}\" ".
4090 "title=\"$link_attr{'-title'}\" ".
4091 "href=\"$link_attr{'-href'}\" ".
4092 "type=\"$link_attr{'-type'}\" ".
4093 "/>\n";
4095 $href_params{'extra_options'} = '--no-merges';
4096 $link_attr{'-href'} = href(%href_params);
4097 $link_attr{'-title'} .= ' (no merges)';
4098 print "<link ".
4099 "rel=\"$link_attr{'-rel'}\" ".
4100 "title=\"$link_attr{'-title'}\" ".
4101 "href=\"$link_attr{'-href'}\" ".
4102 "type=\"$link_attr{'-type'}\" ".
4103 "/>\n";
4106 } else {
4107 printf('<link rel="alternate" title="%s projects list" '.
4108 'href="%s" type="text/plain; charset=utf-8" />'."\n",
4109 esc_attr($site_name), href(project=>undef, action=>"project_index"));
4110 printf('<link rel="alternate" title="%s projects feeds" '.
4111 'href="%s" type="text/x-opml" />'."\n",
4112 esc_attr($site_name), href(project=>undef, action=>"opml"));
4116 sub print_header_links {
4117 my $status = shift;
4119 # print out each stylesheet that exist, providing backwards capability
4120 # for those people who defined $stylesheet in a config file
4121 if (defined $stylesheet) {
4122 print '<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";
4123 } else {
4124 foreach my $stylesheet (@stylesheets) {
4125 next unless $stylesheet;
4126 print '<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";
4129 print_feed_meta()
4130 if ($status eq '200 OK');
4131 if (defined $favicon) {
4132 print qq(<link rel="shortcut icon" href=").esc_url($favicon).qq(" type="image/png" />\n);
4136 sub print_nav_breadcrumbs_path {
4137 my $dirprefix = undef;
4138 while (my $part = shift) {
4139 $dirprefix .= "/" if defined $dirprefix;
4140 $dirprefix .= $part;
4141 print $cgi->a({-href => href(project => undef,
4142 project_filter => $dirprefix,
4143 action => "project_list")},
4144 esc_html($part)) . " / ";
4148 sub print_nav_breadcrumbs {
4149 my %opts = @_;
4151 for my $crumb (@extra_breadcrumbs, [ $home_link_str => $home_link ]) {
4152 print $cgi->a({-href => esc_url($crumb->[1])}, $crumb->[0]) . " / ";
4154 if (defined $project) {
4155 my @dirname = split '/', $project;
4156 my $projectbasename = pop @dirname;
4157 print_nav_breadcrumbs_path(@dirname);
4158 print $cgi->a({-href => href(action=>"summary")}, esc_html($projectbasename));
4159 if (defined $action) {
4160 my $action_print = $action ;
4161 if (defined $opts{-action_extra}) {
4162 $action_print = $cgi->a({-href => href(action=>$action)},
4163 $action);
4165 print " / $action_print";
4167 if (defined $opts{-action_extra}) {
4168 print " / $opts{-action_extra}";
4170 print "\n";
4171 } elsif (defined $project_filter) {
4172 print_nav_breadcrumbs_path(split '/', $project_filter);
4176 sub print_search_form {
4177 if (!defined $searchtext) {
4178 $searchtext = "";
4180 my $search_hash;
4181 if (defined $hash_base) {
4182 $search_hash = $hash_base;
4183 } elsif (defined $hash) {
4184 $search_hash = $hash;
4185 } else {
4186 $search_hash = "HEAD";
4188 my $action = $my_uri;
4189 my $use_pathinfo = gitweb_check_feature('pathinfo');
4190 if ($use_pathinfo) {
4191 $action .= "/".esc_url($project);
4193 print $cgi->start_form(-method => "get", -action => $action) .
4194 "<div class=\"search\">\n" .
4195 (!$use_pathinfo &&
4196 $cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) . "\n") .
4197 $cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) . "\n" .
4198 $cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) . "\n" .
4199 $cgi->popup_menu(-name => 'st', -default => 'commit',
4200 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
4201 " " . $cgi->a({-href => href(action=>"search_help"),
4202 -title => "search help" }, "?") . " search:\n",
4203 $cgi->textfield(-name => "s", -value => $searchtext, -override => 1) . "\n" .
4204 "<span title=\"Extended regular expression\">" .
4205 $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
4206 -checked => $search_use_regexp) .
4207 "</span>" .
4208 "</div>" .
4209 $cgi->end_form() . "\n";
4212 sub git_header_html {
4213 my $status = shift || "200 OK";
4214 my $expires = shift;
4215 my %opts = @_;
4217 my $title = get_page_title();
4218 my $content_type = get_content_type_html();
4219 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
4220 -status=> $status, -expires => $expires)
4221 unless ($opts{'-no_http_header'});
4222 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
4223 print <<EOF;
4224 <?xml version="1.0" encoding="utf-8"?>
4225 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
4226 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
4227 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
4228 <!-- git core binaries version $git_version -->
4229 <head>
4230 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
4231 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
4232 <meta name="robots" content="index, nofollow"/>
4233 <title>$title</title>
4235 # the stylesheet, favicon etc urls won't work correctly with path_info
4236 # unless we set the appropriate base URL
4237 if ($ENV{'PATH_INFO'}) {
4238 print "<base href=\"".esc_url($base_url)."\" />\n";
4240 print_header_links($status);
4242 if (defined $site_html_head_string) {
4243 print to_utf8($site_html_head_string);
4246 print "</head>\n" .
4247 "<body>\n";
4249 if (defined $site_header && -f $site_header) {
4250 insert_file($site_header);
4253 print "<div class=\"page_header\">\n";
4254 if (defined $logo) {
4255 print $cgi->a({-href => esc_url($logo_url),
4256 -title => $logo_label},
4257 $cgi->img({-src => esc_url($logo),
4258 -width => 72, -height => 27,
4259 -alt => "git",
4260 -class => "logo"}));
4262 print_nav_breadcrumbs(%opts);
4263 print "</div>\n";
4265 my $have_search = gitweb_check_feature('search');
4266 if (defined $project && $have_search) {
4267 print_search_form();
4271 sub git_footer_html {
4272 my $feed_class = 'rss_logo';
4274 print "<div class=\"page_footer\">\n";
4275 if (defined $project) {
4276 my $descr = git_get_project_description($project);
4277 if (defined $descr) {
4278 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
4281 my %href_params = get_feed_info();
4282 if (!%href_params) {
4283 $feed_class .= ' generic';
4285 $href_params{'-title'} ||= 'log';
4287 foreach my $format (qw(RSS Atom)) {
4288 $href_params{'action'} = lc($format);
4289 print $cgi->a({-href => href(%href_params),
4290 -title => "$href_params{'-title'} $format feed",
4291 -class => $feed_class}, $format)."\n";
4294 } else {
4295 print $cgi->a({-href => href(project=>undef, action=>"opml",
4296 project_filter => $project_filter),
4297 -class => $feed_class}, "OPML") . " ";
4298 print $cgi->a({-href => href(project=>undef, action=>"project_index",
4299 project_filter => $project_filter),
4300 -class => $feed_class}, "TXT") . "\n";
4302 print "</div>\n"; # class="page_footer"
4304 if (defined $t0 && gitweb_check_feature('timed')) {
4305 print "<div id=\"generating_info\">\n";
4306 print 'This page took '.
4307 '<span id="generating_time" class="time_span">'.
4308 tv_interval($t0, [ gettimeofday() ]).
4309 ' seconds </span>'.
4310 ' and '.
4311 '<span id="generating_cmd">'.
4312 $number_of_git_cmds.
4313 '</span> git commands '.
4314 " to generate.\n";
4315 print "</div>\n"; # class="page_footer"
4318 if (defined $site_footer && -f $site_footer) {
4319 insert_file($site_footer);
4322 print qq!<script type="text/javascript" src="!.esc_url($javascript).qq!"></script>\n!;
4323 if (defined $action &&
4324 $action eq 'blame_incremental') {
4325 print qq!<script type="text/javascript">\n!.
4326 qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.
4327 qq! "!. href() .qq!");\n!.
4328 qq!</script>\n!;
4329 } else {
4330 my ($jstimezone, $tz_cookie, $datetime_class) =
4331 gitweb_get_feature('javascript-timezone');
4333 print qq!<script type="text/javascript">\n!.
4334 qq!window.onload = function () {\n!;
4335 if (gitweb_check_feature('javascript-actions')) {
4336 print qq! fixLinks();\n!;
4338 if ($jstimezone && $tz_cookie && $datetime_class) {
4339 print qq! var tz_cookie = { name: '$tz_cookie', expires: 14, path: '/' };\n!. # in days
4340 qq! onloadTZSetup('$jstimezone', tz_cookie, '$datetime_class');\n!;
4342 print qq!};\n!.
4343 qq!</script>\n!;
4346 print "</body>\n" .
4347 "</html>";
4350 # die_error(<http_status_code>, <error_message>[, <detailed_html_description>])
4351 # Example: die_error(404, 'Hash not found')
4352 # By convention, use the following status codes (as defined in RFC 2616):
4353 # 400: Invalid or missing CGI parameters, or
4354 # requested object exists but has wrong type.
4355 # 403: Requested feature (like "pickaxe" or "snapshot") not enabled on
4356 # this server or project.
4357 # 404: Requested object/revision/project doesn't exist.
4358 # 500: The server isn't configured properly, or
4359 # an internal error occurred (e.g. failed assertions caused by bugs), or
4360 # an unknown error occurred (e.g. the git binary died unexpectedly).
4361 # 503: The server is currently unavailable (because it is overloaded,
4362 # or down for maintenance). Generally, this is a temporary state.
4363 sub die_error {
4364 my $status = shift || 500;
4365 my $error = esc_html(shift) || "Internal Server Error";
4366 my $extra = shift;
4367 my %opts = @_;
4369 my %http_responses = (
4370 400 => '400 Bad Request',
4371 403 => '403 Forbidden',
4372 404 => '404 Not Found',
4373 500 => '500 Internal Server Error',
4374 503 => '503 Service Unavailable',
4376 git_header_html($http_responses{$status}, undef, %opts);
4377 print <<EOF;
4378 <div class="page_body">
4379 <br /><br />
4380 $status - $error
4381 <br />
4383 if (defined $extra) {
4384 print "<hr />\n" .
4385 "$extra\n";
4387 print "</div>\n";
4389 git_footer_html();
4390 CORE::die
4391 unless ($opts{'-error_handler'});
4394 ## ----------------------------------------------------------------------
4395 ## functions printing or outputting HTML: navigation
4397 sub git_print_page_nav {
4398 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
4399 $extra = '' if !defined $extra; # pager or formats
4401 my @navs = qw(summary shortlog log commit commitdiff tree);
4402 if ($suppress) {
4403 @navs = grep { $_ ne $suppress } @navs;
4406 my %arg = map { $_ => {action=>$_} } @navs;
4407 if (defined $head) {
4408 for (qw(commit commitdiff)) {
4409 $arg{$_}{'hash'} = $head;
4411 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
4412 for (qw(shortlog log)) {
4413 $arg{$_}{'hash'} = $head;
4418 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
4419 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
4421 my @actions = gitweb_get_feature('actions');
4422 my %repl = (
4423 '%' => '%',
4424 'n' => $project, # project name
4425 'f' => $git_dir, # project path within filesystem
4426 'h' => $treehead || '', # current hash ('h' parameter)
4427 'b' => $treebase || '', # hash base ('hb' parameter)
4429 while (@actions) {
4430 my ($label, $link, $pos) = splice(@actions,0,3);
4431 # insert
4432 @navs = map { $_ eq $pos ? ($_, $label) : $_ } @navs;
4433 # munch munch
4434 $link =~ s/%([%nfhb])/$repl{$1}/g;
4435 $arg{$label}{'_href'} = $link;
4438 print "<div class=\"page_nav\">\n" .
4439 (join " | ",
4440 map { $_ eq $current ?
4441 $_ : $cgi->a({-href => ($arg{$_}{_href} ? $arg{$_}{_href} : href(%{$arg{$_}}))}, "$_")
4442 } @navs);
4443 print "<br/>\n$extra<br/>\n" .
4444 "</div>\n";
4447 # returns a submenu for the nagivation of the refs views (tags, heads,
4448 # remotes) with the current view disabled and the remotes view only
4449 # available if the feature is enabled
4450 sub format_ref_views {
4451 my ($current) = @_;
4452 my @ref_views = qw{tags heads};
4453 push @ref_views, 'remotes' if gitweb_check_feature('remote_heads');
4454 return join " | ", map {
4455 $_ eq $current ? $_ :
4456 $cgi->a({-href => href(action=>$_)}, $_)
4457 } @ref_views
4460 sub format_paging_nav {
4461 my ($action, $page, $has_next_link) = @_;
4462 my $paging_nav;
4465 if ($page > 0) {
4466 $paging_nav .=
4467 $cgi->a({-href => href(-replay=>1, page=>undef)}, "first") .
4468 " &sdot; " .
4469 $cgi->a({-href => href(-replay=>1, page=>$page-1),
4470 -accesskey => "p", -title => "Alt-p"}, "prev");
4471 } else {
4472 $paging_nav .= "first &sdot; prev";
4475 if ($has_next_link) {
4476 $paging_nav .= " &sdot; " .
4477 $cgi->a({-href => href(-replay=>1, page=>$page+1),
4478 -accesskey => "n", -title => "Alt-n"}, "next");
4479 } else {
4480 $paging_nav .= " &sdot; next";
4483 return $paging_nav;
4486 ## ......................................................................
4487 ## functions printing or outputting HTML: div
4489 sub git_print_header_div {
4490 my ($action, $title, $hash, $hash_base) = @_;
4491 my %args = ();
4493 $args{'action'} = $action;
4494 $args{'hash'} = $hash if $hash;
4495 $args{'hash_base'} = $hash_base if $hash_base;
4497 print "<div class=\"header\">\n" .
4498 $cgi->a({-href => href(%args), -class => "title"},
4499 $title ? $title : $action) .
4500 "\n</div>\n";
4503 sub format_repo_url {
4504 my ($name, $url) = @_;
4505 return "<tr class=\"metadata_url\"><td>$name</td><td>$url</td></tr>\n";
4508 # Group output by placing it in a DIV element and adding a header.
4509 # Options for start_div() can be provided by passing a hash reference as the
4510 # first parameter to the function.
4511 # Options to git_print_header_div() can be provided by passing an array
4512 # reference. This must follow the options to start_div if they are present.
4513 # The content can be a scalar, which is output as-is, a scalar reference, which
4514 # is output after html escaping, an IO handle passed either as *handle or
4515 # *handle{IO}, or a function reference. In the latter case all following
4516 # parameters will be taken as argument to the content function call.
4517 sub git_print_section {
4518 my ($div_args, $header_args, $content);
4519 my $arg = shift;
4520 if (ref($arg) eq 'HASH') {
4521 $div_args = $arg;
4522 $arg = shift;
4524 if (ref($arg) eq 'ARRAY') {
4525 $header_args = $arg;
4526 $arg = shift;
4528 $content = $arg;
4530 print $cgi->start_div($div_args);
4531 git_print_header_div(@$header_args);
4533 if (ref($content) eq 'CODE') {
4534 $content->(@_);
4535 } elsif (ref($content) eq 'SCALAR') {
4536 print esc_html($$content);
4537 } elsif (ref($content) eq 'GLOB' or ref($content) eq 'IO::Handle') {
4538 print <$content>;
4539 } elsif (!ref($content) && defined($content)) {
4540 print $content;
4543 print $cgi->end_div;
4546 sub format_timestamp_html {
4547 my $date = shift;
4548 my $strtime = $date->{'rfc2822'};
4550 my (undef, undef, $datetime_class) =
4551 gitweb_get_feature('javascript-timezone');
4552 if ($datetime_class) {
4553 $strtime = qq!<span class="$datetime_class">$strtime</span>!;
4556 my $localtime_format = '(%02d:%02d %s)';
4557 if ($date->{'hour_local'} < 6) {
4558 $localtime_format = '(<span class="atnight">%02d:%02d</span> %s)';
4560 $strtime .= ' ' .
4561 sprintf($localtime_format,
4562 $date->{'hour_local'}, $date->{'minute_local'}, $date->{'tz_local'});
4564 return $strtime;
4567 # Outputs the author name and date in long form
4568 sub git_print_authorship {
4569 my $co = shift;
4570 my %opts = @_;
4571 my $tag = $opts{-tag} || 'div';
4572 my $author = $co->{'author_name'};
4574 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
4575 print "<$tag class=\"author_date\">" .
4576 format_search_author($author, "author", esc_html($author)) .
4577 " [".format_timestamp_html(\%ad)."]".
4578 git_get_avatar($co->{'author_email'}, -pad_before => 1) .
4579 "</$tag>\n";
4582 # Outputs table rows containing the full author or committer information,
4583 # in the format expected for 'commit' view (& similar).
4584 # Parameters are a commit hash reference, followed by the list of people
4585 # to output information for. If the list is empty it defaults to both
4586 # author and committer.
4587 sub git_print_authorship_rows {
4588 my $co = shift;
4589 # too bad we can't use @people = @_ || ('author', 'committer')
4590 my @people = @_;
4591 @people = ('author', 'committer') unless @people;
4592 foreach my $who (@people) {
4593 my %wd = parse_date($co->{"${who}_epoch"}, $co->{"${who}_tz"});
4594 print "<tr><td>$who</td><td>" .
4595 format_search_author($co->{"${who}_name"}, $who,
4596 esc_html($co->{"${who}_name"})) . " " .
4597 format_search_author($co->{"${who}_email"}, $who,
4598 esc_html("<" . $co->{"${who}_email"} . ">")) .
4599 "</td><td rowspan=\"2\">" .
4600 git_get_avatar($co->{"${who}_email"}, -size => 'double') .
4601 "</td></tr>\n" .
4602 "<tr>" .
4603 "<td></td><td>" .
4604 format_timestamp_html(\%wd) .
4605 "</td>" .
4606 "</tr>\n";
4610 sub git_print_page_path {
4611 my $name = shift;
4612 my $type = shift;
4613 my $hb = shift;
4616 print "<div class=\"page_path\">";
4617 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
4618 -title => 'tree root'}, to_utf8("[$project]"));
4619 print " / ";
4620 if (defined $name) {
4621 my @dirname = split '/', $name;
4622 my $basename = pop @dirname;
4623 my $fullname = '';
4625 foreach my $dir (@dirname) {
4626 $fullname .= ($fullname ? '/' : '') . $dir;
4627 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
4628 hash_base=>$hb),
4629 -title => $fullname}, esc_path($dir));
4630 print " / ";
4632 if (defined $type && $type eq 'blob') {
4633 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
4634 hash_base=>$hb),
4635 -title => $name}, esc_path($basename));
4636 } elsif (defined $type && $type eq 'tree') {
4637 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
4638 hash_base=>$hb),
4639 -title => $name}, esc_path($basename));
4640 print " / ";
4641 } else {
4642 print esc_path($basename);
4645 print "<br/></div>\n";
4648 sub git_print_log {
4649 my $log = shift;
4650 my %opts = @_;
4652 if ($opts{'-remove_title'}) {
4653 # remove title, i.e. first line of log
4654 shift @$log;
4656 # remove leading empty lines
4657 while (defined $log->[0] && $log->[0] eq "") {
4658 shift @$log;
4661 # print log
4662 my $skip_blank_line = 0;
4663 foreach my $line (@$log) {
4664 if ($line =~ m/^\s*([A-Z][-A-Za-z]*-[Bb]y|C[Cc]): /) {
4665 if (! $opts{'-remove_signoff'}) {
4666 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
4667 $skip_blank_line = 1;
4669 next;
4672 if ($line =~ m,\s*([a-z]*link): (https?://\S+),i) {
4673 if (! $opts{'-remove_signoff'}) {
4674 print "<span class=\"signoff\">" . esc_html($1) . ": " .
4675 "<a href=\"" . esc_html($2) . "\">" . esc_html($2) . "</a>" .
4676 "</span><br/>\n";
4677 $skip_blank_line = 1;
4679 next;
4682 # print only one empty line
4683 # do not print empty line after signoff
4684 if ($line eq "") {
4685 next if ($skip_blank_line);
4686 $skip_blank_line = 1;
4687 } else {
4688 $skip_blank_line = 0;
4691 print format_log_line_html($line) . "<br/>\n";
4694 if ($opts{'-final_empty_line'}) {
4695 # end with single empty line
4696 print "<br/>\n" unless $skip_blank_line;
4700 # return link target (what link points to)
4701 sub git_get_link_target {
4702 my $hash = shift;
4703 my $link_target;
4705 # read link
4706 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4707 or return;
4709 local $/ = undef;
4710 $link_target = <$fd>;
4712 close $fd
4713 or return;
4715 return $link_target;
4718 # given link target, and the directory (basedir) the link is in,
4719 # return target of link relative to top directory (top tree);
4720 # return undef if it is not possible (including absolute links).
4721 sub normalize_link_target {
4722 my ($link_target, $basedir) = @_;
4724 # absolute symlinks (beginning with '/') cannot be normalized
4725 return if (substr($link_target, 0, 1) eq '/');
4727 # normalize link target to path from top (root) tree (dir)
4728 my $path;
4729 if ($basedir) {
4730 $path = $basedir . '/' . $link_target;
4731 } else {
4732 # we are in top (root) tree (dir)
4733 $path = $link_target;
4736 # remove //, /./, and /../
4737 my @path_parts;
4738 foreach my $part (split('/', $path)) {
4739 # discard '.' and ''
4740 next if (!$part || $part eq '.');
4741 # handle '..'
4742 if ($part eq '..') {
4743 if (@path_parts) {
4744 pop @path_parts;
4745 } else {
4746 # link leads outside repository (outside top dir)
4747 return;
4749 } else {
4750 push @path_parts, $part;
4753 $path = join('/', @path_parts);
4755 return $path;
4758 # print tree entry (row of git_tree), but without encompassing <tr> element
4759 sub git_print_tree_entry {
4760 my ($t, $basedir, $hash_base, $have_blame) = @_;
4762 my %base_key = ();
4763 $base_key{'hash_base'} = $hash_base if defined $hash_base;
4765 # The format of a table row is: mode list link. Where mode is
4766 # the mode of the entry, list is the name of the entry, an href,
4767 # and link is the action links of the entry.
4769 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
4770 if (exists $t->{'size'}) {
4771 print "<td class=\"size\">$t->{'size'}</td>\n";
4773 if ($t->{'type'} eq "blob") {
4774 print "<td class=\"list\">" .
4775 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
4776 file_name=>"$basedir$t->{'name'}", %base_key),
4777 -class => "list"}, esc_path($t->{'name'}));
4778 if (S_ISLNK(oct $t->{'mode'})) {
4779 my $link_target = git_get_link_target($t->{'hash'});
4780 if ($link_target) {
4781 my $norm_target = normalize_link_target($link_target, $basedir);
4782 if (defined $norm_target) {
4783 print " -> " .
4784 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
4785 file_name=>$norm_target),
4786 -title => $norm_target}, esc_path($link_target));
4787 } else {
4788 print " -> " . esc_path($link_target);
4792 print "</td>\n";
4793 print "<td class=\"link\">";
4794 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
4795 file_name=>"$basedir$t->{'name'}", %base_key)},
4796 "blob");
4797 if ($have_blame) {
4798 print " | " .
4799 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
4800 file_name=>"$basedir$t->{'name'}", %base_key)},
4801 "blame");
4803 if (defined $hash_base) {
4804 print " | " .
4805 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
4806 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
4807 "history");
4809 print " | " .
4810 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
4811 file_name=>"$basedir$t->{'name'}")},
4812 "raw");
4813 print "</td>\n";
4815 } elsif ($t->{'type'} eq "tree") {
4816 print "<td class=\"list\">";
4817 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
4818 file_name=>"$basedir$t->{'name'}",
4819 %base_key)},
4820 esc_path($t->{'name'}));
4821 print "</td>\n";
4822 print "<td class=\"link\">";
4823 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
4824 file_name=>"$basedir$t->{'name'}",
4825 %base_key)},
4826 "tree");
4827 if (defined $hash_base) {
4828 print " | " .
4829 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
4830 file_name=>"$basedir$t->{'name'}")},
4831 "history");
4833 print "</td>\n";
4834 } else {
4835 # unknown object: we can only present history for it
4836 # (this includes 'commit' object, i.e. submodule support)
4837 print "<td class=\"list\">" .
4838 esc_path($t->{'name'}) .
4839 "</td>\n";
4840 print "<td class=\"link\">";
4841 if (defined $hash_base) {
4842 print $cgi->a({-href => href(action=>"history",
4843 hash_base=>$hash_base,
4844 file_name=>"$basedir$t->{'name'}")},
4845 "history");
4847 print "</td>\n";
4851 ## ......................................................................
4852 ## functions printing large fragments of HTML
4854 # get pre-image filenames for merge (combined) diff
4855 sub fill_from_file_info {
4856 my ($diff, @parents) = @_;
4858 $diff->{'from_file'} = [ ];
4859 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
4860 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
4861 if ($diff->{'status'}[$i] eq 'R' ||
4862 $diff->{'status'}[$i] eq 'C') {
4863 $diff->{'from_file'}[$i] =
4864 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
4868 return $diff;
4871 # is current raw difftree line of file deletion
4872 sub is_deleted {
4873 my $diffinfo = shift;
4875 return $diffinfo->{'to_id'} eq ('0' x 40);
4878 # does patch correspond to [previous] difftree raw line
4879 # $diffinfo - hashref of parsed raw diff format
4880 # $patchinfo - hashref of parsed patch diff format
4881 # (the same keys as in $diffinfo)
4882 sub is_patch_split {
4883 my ($diffinfo, $patchinfo) = @_;
4885 return defined $diffinfo && defined $patchinfo
4886 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
4890 sub git_difftree_body {
4891 my ($difftree, $hash, @parents) = @_;
4892 my ($parent) = $parents[0];
4893 my $have_blame = gitweb_check_feature('blame');
4894 print "<div class=\"list_head\">\n";
4895 if ($#{$difftree} > 10) {
4896 print(($#{$difftree} + 1) . " files changed:\n");
4898 print "</div>\n";
4900 print "<table class=\"" .
4901 (@parents > 1 ? "combined " : "") .
4902 "diff_tree\">\n";
4904 # header only for combined diff in 'commitdiff' view
4905 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
4906 if ($has_header) {
4907 # table header
4908 print "<thead><tr>\n" .
4909 "<th></th><th></th>\n"; # filename, patchN link
4910 for (my $i = 0; $i < @parents; $i++) {
4911 my $par = $parents[$i];
4912 print "<th>" .
4913 $cgi->a({-href => href(action=>"commitdiff",
4914 hash=>$hash, hash_parent=>$par),
4915 -title => 'commitdiff to parent number ' .
4916 ($i+1) . ': ' . substr($par,0,7)},
4917 $i+1) .
4918 "&nbsp;</th>\n";
4920 print "</tr></thead>\n<tbody>\n";
4923 my $alternate = 1;
4924 my $patchno = 0;
4925 foreach my $line (@{$difftree}) {
4926 my $diff = parsed_difftree_line($line);
4928 if ($alternate) {
4929 print "<tr class=\"dark\">\n";
4930 } else {
4931 print "<tr class=\"light\">\n";
4933 $alternate ^= 1;
4935 if (exists $diff->{'nparents'}) { # combined diff
4937 fill_from_file_info($diff, @parents)
4938 unless exists $diff->{'from_file'};
4940 if (!is_deleted($diff)) {
4941 # file exists in the result (child) commit
4942 print "<td>" .
4943 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4944 file_name=>$diff->{'to_file'},
4945 hash_base=>$hash),
4946 -class => "list"}, esc_path($diff->{'to_file'})) .
4947 "</td>\n";
4948 } else {
4949 print "<td>" .
4950 esc_path($diff->{'to_file'}) .
4951 "</td>\n";
4954 if ($action eq 'commitdiff') {
4955 # link to patch
4956 $patchno++;
4957 print "<td class=\"link\">" .
4958 $cgi->a({-href => href(-anchor=>"patch$patchno")},
4959 "patch") .
4960 " | " .
4961 "</td>\n";
4964 my $has_history = 0;
4965 my $not_deleted = 0;
4966 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
4967 my $hash_parent = $parents[$i];
4968 my $from_hash = $diff->{'from_id'}[$i];
4969 my $from_path = $diff->{'from_file'}[$i];
4970 my $status = $diff->{'status'}[$i];
4972 $has_history ||= ($status ne 'A');
4973 $not_deleted ||= ($status ne 'D');
4975 if ($status eq 'A') {
4976 print "<td class=\"link\" align=\"right\"> | </td>\n";
4977 } elsif ($status eq 'D') {
4978 print "<td class=\"link\">" .
4979 $cgi->a({-href => href(action=>"blob",
4980 hash_base=>$hash,
4981 hash=>$from_hash,
4982 file_name=>$from_path)},
4983 "blob" . ($i+1)) .
4984 " | </td>\n";
4985 } else {
4986 if ($diff->{'to_id'} eq $from_hash) {
4987 print "<td class=\"link nochange\">";
4988 } else {
4989 print "<td class=\"link\">";
4991 print $cgi->a({-href => href(action=>"blobdiff",
4992 hash=>$diff->{'to_id'},
4993 hash_parent=>$from_hash,
4994 hash_base=>$hash,
4995 hash_parent_base=>$hash_parent,
4996 file_name=>$diff->{'to_file'},
4997 file_parent=>$from_path)},
4998 "diff" . ($i+1)) .
4999 " | </td>\n";
5003 print "<td class=\"link\">";
5004 if ($not_deleted) {
5005 print $cgi->a({-href => href(action=>"blob",
5006 hash=>$diff->{'to_id'},
5007 file_name=>$diff->{'to_file'},
5008 hash_base=>$hash)},
5009 "blob");
5010 print " | " if ($has_history);
5012 if ($has_history) {
5013 print $cgi->a({-href => href(action=>"history",
5014 file_name=>$diff->{'to_file'},
5015 hash_base=>$hash)},
5016 "history");
5018 print "</td>\n";
5020 print "</tr>\n";
5021 next; # instead of 'else' clause, to avoid extra indent
5023 # else ordinary diff
5025 my ($to_mode_oct, $to_mode_str, $to_file_type);
5026 my ($from_mode_oct, $from_mode_str, $from_file_type);
5027 if ($diff->{'to_mode'} ne ('0' x 6)) {
5028 $to_mode_oct = oct $diff->{'to_mode'};
5029 if (S_ISREG($to_mode_oct)) { # only for regular file
5030 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
5032 $to_file_type = file_type($diff->{'to_mode'});
5034 if ($diff->{'from_mode'} ne ('0' x 6)) {
5035 $from_mode_oct = oct $diff->{'from_mode'};
5036 if (S_ISREG($from_mode_oct)) { # only for regular file
5037 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
5039 $from_file_type = file_type($diff->{'from_mode'});
5042 if ($diff->{'status'} eq "A") { # created
5043 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
5044 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
5045 $mode_chng .= "]</span>";
5046 print "<td>";
5047 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
5048 hash_base=>$hash, file_name=>$diff->{'file'}),
5049 -class => "list"}, esc_path($diff->{'file'}));
5050 print "</td>\n";
5051 print "<td>$mode_chng</td>\n";
5052 print "<td class=\"link\">";
5053 if ($action eq 'commitdiff') {
5054 # link to patch
5055 $patchno++;
5056 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
5057 "patch") .
5058 " | ";
5060 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
5061 hash_base=>$hash, file_name=>$diff->{'file'})},
5062 "blob");
5063 print "</td>\n";
5065 } elsif ($diff->{'status'} eq "D") { # deleted
5066 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
5067 print "<td>";
5068 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
5069 hash_base=>$parent, file_name=>$diff->{'file'}),
5070 -class => "list"}, esc_path($diff->{'file'}));
5071 print "</td>\n";
5072 print "<td>$mode_chng</td>\n";
5073 print "<td class=\"link\">";
5074 if ($action eq 'commitdiff') {
5075 # link to patch
5076 $patchno++;
5077 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
5078 "patch") .
5079 " | ";
5081 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
5082 hash_base=>$parent, file_name=>$diff->{'file'})},
5083 "blob") . " | ";
5084 if ($have_blame) {
5085 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
5086 file_name=>$diff->{'file'})},
5087 "blame") . " | ";
5089 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
5090 file_name=>$diff->{'file'})},
5091 "history");
5092 print "</td>\n";
5094 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
5095 my $mode_chnge = "";
5096 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
5097 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
5098 if ($from_file_type ne $to_file_type) {
5099 $mode_chnge .= " from $from_file_type to $to_file_type";
5101 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
5102 if ($from_mode_str && $to_mode_str) {
5103 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
5104 } elsif ($to_mode_str) {
5105 $mode_chnge .= " mode: $to_mode_str";
5108 $mode_chnge .= "]</span>\n";
5110 print "<td>";
5111 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
5112 hash_base=>$hash, file_name=>$diff->{'file'}),
5113 -class => "list"}, esc_path($diff->{'file'}));
5114 print "</td>\n";
5115 print "<td>$mode_chnge</td>\n";
5116 print "<td class=\"link\">";
5117 if ($action eq 'commitdiff') {
5118 # link to patch
5119 $patchno++;
5120 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
5121 "patch") .
5122 " | ";
5123 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
5124 # "commit" view and modified file (not onlu mode changed)
5125 print $cgi->a({-href => href(action=>"blobdiff",
5126 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
5127 hash_base=>$hash, hash_parent_base=>$parent,
5128 file_name=>$diff->{'file'})},
5129 "diff") .
5130 " | ";
5132 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
5133 hash_base=>$hash, file_name=>$diff->{'file'})},
5134 "blob") . " | ";
5135 if ($have_blame) {
5136 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
5137 file_name=>$diff->{'file'})},
5138 "blame") . " | ";
5140 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
5141 file_name=>$diff->{'file'})},
5142 "history");
5143 print "</td>\n";
5145 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
5146 my %status_name = ('R' => 'moved', 'C' => 'copied');
5147 my $nstatus = $status_name{$diff->{'status'}};
5148 my $mode_chng = "";
5149 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
5150 # mode also for directories, so we cannot use $to_mode_str
5151 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
5153 print "<td>" .
5154 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
5155 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
5156 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
5157 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
5158 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
5159 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
5160 -class => "list"}, esc_path($diff->{'from_file'})) .
5161 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
5162 "<td class=\"link\">";
5163 if ($action eq 'commitdiff') {
5164 # link to patch
5165 $patchno++;
5166 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
5167 "patch") .
5168 " | ";
5169 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
5170 # "commit" view and modified file (not only pure rename or copy)
5171 print $cgi->a({-href => href(action=>"blobdiff",
5172 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
5173 hash_base=>$hash, hash_parent_base=>$parent,
5174 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
5175 "diff") .
5176 " | ";
5178 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
5179 hash_base=>$parent, file_name=>$diff->{'to_file'})},
5180 "blob") . " | ";
5181 if ($have_blame) {
5182 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
5183 file_name=>$diff->{'to_file'})},
5184 "blame") . " | ";
5186 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
5187 file_name=>$diff->{'to_file'})},
5188 "history");
5189 print "</td>\n";
5191 } # we should not encounter Unmerged (U) or Unknown (X) status
5192 print "</tr>\n";
5194 print "</tbody>" if $has_header;
5195 print "</table>\n";
5198 # Print context lines and then rem/add lines in a side-by-side manner.
5199 sub print_sidebyside_diff_lines {
5200 my ($ctx, $rem, $add) = @_;
5202 # print context block before add/rem block
5203 if (@$ctx) {
5204 print join '',
5205 '<div class="chunk_block ctx">',
5206 '<div class="old">',
5207 @$ctx,
5208 '</div>',
5209 '<div class="new">',
5210 @$ctx,
5211 '</div>',
5212 '</div>';
5215 if (!@$add) {
5216 # pure removal
5217 print join '',
5218 '<div class="chunk_block rem">',
5219 '<div class="old">',
5220 @$rem,
5221 '</div>',
5222 '</div>';
5223 } elsif (!@$rem) {
5224 # pure addition
5225 print join '',
5226 '<div class="chunk_block add">',
5227 '<div class="new">',
5228 @$add,
5229 '</div>',
5230 '</div>';
5231 } else {
5232 print join '',
5233 '<div class="chunk_block chg">',
5234 '<div class="old">',
5235 @$rem,
5236 '</div>',
5237 '<div class="new">',
5238 @$add,
5239 '</div>',
5240 '</div>';
5244 # Print context lines and then rem/add lines in inline manner.
5245 sub print_inline_diff_lines {
5246 my ($ctx, $rem, $add) = @_;
5248 print @$ctx, @$rem, @$add;
5251 # Format removed and added line, mark changed part and HTML-format them.
5252 # Implementation is based on contrib/diff-highlight
5253 sub format_rem_add_lines_pair {
5254 my ($rem, $add, $num_parents) = @_;
5256 # We need to untabify lines before split()'ing them;
5257 # otherwise offsets would be invalid.
5258 chomp $rem;
5259 chomp $add;
5260 $rem = untabify($rem);
5261 $add = untabify($add);
5263 my @rem = split(//, $rem);
5264 my @add = split(//, $add);
5265 my ($esc_rem, $esc_add);
5266 # Ignore leading +/- characters for each parent.
5267 my ($prefix_len, $suffix_len) = ($num_parents, 0);
5268 my ($prefix_has_nonspace, $suffix_has_nonspace);
5270 my $shorter = (@rem < @add) ? @rem : @add;
5271 while ($prefix_len < $shorter) {
5272 last if ($rem[$prefix_len] ne $add[$prefix_len]);
5274 $prefix_has_nonspace = 1 if ($rem[$prefix_len] !~ /\s/);
5275 $prefix_len++;
5278 while ($prefix_len + $suffix_len < $shorter) {
5279 last if ($rem[-1 - $suffix_len] ne $add[-1 - $suffix_len]);
5281 $suffix_has_nonspace = 1 if ($rem[-1 - $suffix_len] !~ /\s/);
5282 $suffix_len++;
5285 # Mark lines that are different from each other, but have some common
5286 # part that isn't whitespace. If lines are completely different, don't
5287 # mark them because that would make output unreadable, especially if
5288 # diff consists of multiple lines.
5289 if ($prefix_has_nonspace || $suffix_has_nonspace) {
5290 $esc_rem = esc_html_hl_regions($rem, 'marked',
5291 [$prefix_len, @rem - $suffix_len], -nbsp=>1);
5292 $esc_add = esc_html_hl_regions($add, 'marked',
5293 [$prefix_len, @add - $suffix_len], -nbsp=>1);
5294 } else {
5295 $esc_rem = esc_html($rem, -nbsp=>1);
5296 $esc_add = esc_html($add, -nbsp=>1);
5299 return format_diff_line(\$esc_rem, 'rem'),
5300 format_diff_line(\$esc_add, 'add');
5303 # HTML-format diff context, removed and added lines.
5304 sub format_ctx_rem_add_lines {
5305 my ($ctx, $rem, $add, $num_parents) = @_;
5306 my (@new_ctx, @new_rem, @new_add);
5307 my $can_highlight = 0;
5308 my $is_combined = ($num_parents > 1);
5310 # Highlight if every removed line has a corresponding added line.
5311 if (@$add > 0 && @$add == @$rem) {
5312 $can_highlight = 1;
5314 # Highlight lines in combined diff only if the chunk contains
5315 # diff between the same version, e.g.
5317 # - a
5318 # - b
5319 # + c
5320 # + d
5322 # Otherwise the highlightling would be confusing.
5323 if ($is_combined) {
5324 for (my $i = 0; $i < @$add; $i++) {
5325 my $prefix_rem = substr($rem->[$i], 0, $num_parents);
5326 my $prefix_add = substr($add->[$i], 0, $num_parents);
5328 $prefix_rem =~ s/-/+/g;
5330 if ($prefix_rem ne $prefix_add) {
5331 $can_highlight = 0;
5332 last;
5338 if ($can_highlight) {
5339 for (my $i = 0; $i < @$add; $i++) {
5340 my ($line_rem, $line_add) = format_rem_add_lines_pair(
5341 $rem->[$i], $add->[$i], $num_parents);
5342 push @new_rem, $line_rem;
5343 push @new_add, $line_add;
5345 } else {
5346 @new_rem = map { format_diff_line($_, 'rem') } @$rem;
5347 @new_add = map { format_diff_line($_, 'add') } @$add;
5350 @new_ctx = map { format_diff_line($_, 'ctx') } @$ctx;
5352 return (\@new_ctx, \@new_rem, \@new_add);
5355 # Print context lines and then rem/add lines.
5356 sub print_diff_lines {
5357 my ($ctx, $rem, $add, $diff_style, $num_parents) = @_;
5358 my $is_combined = $num_parents > 1;
5360 ($ctx, $rem, $add) = format_ctx_rem_add_lines($ctx, $rem, $add,
5361 $num_parents);
5363 if ($diff_style eq 'sidebyside' && !$is_combined) {
5364 print_sidebyside_diff_lines($ctx, $rem, $add);
5365 } else {
5366 # default 'inline' style and unknown styles
5367 print_inline_diff_lines($ctx, $rem, $add);
5371 sub print_diff_chunk {
5372 my ($diff_style, $num_parents, $from, $to, @chunk) = @_;
5373 my (@ctx, @rem, @add);
5375 # The class of the previous line.
5376 my $prev_class = '';
5378 return unless @chunk;
5380 # incomplete last line might be among removed or added lines,
5381 # or both, or among context lines: find which
5382 for (my $i = 1; $i < @chunk; $i++) {
5383 if ($chunk[$i][0] eq 'incomplete') {
5384 $chunk[$i][0] = $chunk[$i-1][0];
5388 # guardian
5389 push @chunk, ["", ""];
5391 foreach my $line_info (@chunk) {
5392 my ($class, $line) = @$line_info;
5394 # print chunk headers
5395 if ($class && $class eq 'chunk_header') {
5396 print format_diff_line($line, $class, $from, $to);
5397 next;
5400 ## print from accumulator when have some add/rem lines or end
5401 # of chunk (flush context lines), or when have add and rem
5402 # lines and new block is reached (otherwise add/rem lines could
5403 # be reordered)
5404 if (!$class || ((@rem || @add) && $class eq 'ctx') ||
5405 (@rem && @add && $class ne $prev_class)) {
5406 print_diff_lines(\@ctx, \@rem, \@add,
5407 $diff_style, $num_parents);
5408 @ctx = @rem = @add = ();
5411 ## adding lines to accumulator
5412 # guardian value
5413 last unless $line;
5414 # rem, add or change
5415 if ($class eq 'rem') {
5416 push @rem, $line;
5417 } elsif ($class eq 'add') {
5418 push @add, $line;
5420 # context line
5421 if ($class eq 'ctx') {
5422 push @ctx, $line;
5425 $prev_class = $class;
5429 sub git_patchset_body {
5430 my ($fd, $diff_style, $difftree, $hash, @hash_parents) = @_;
5431 my ($hash_parent) = $hash_parents[0];
5433 my $is_combined = (@hash_parents > 1);
5434 my $patch_idx = 0;
5435 my $patch_number = 0;
5436 my $patch_line;
5437 my $diffinfo;
5438 my $to_name;
5439 my (%from, %to);
5440 my @chunk; # for side-by-side diff
5442 print "<div class=\"patchset\">\n";
5444 # skip to first patch
5445 while ($patch_line = <$fd>) {
5446 chomp $patch_line;
5448 last if ($patch_line =~ m/^diff /);
5451 PATCH:
5452 while ($patch_line) {
5454 # parse "git diff" header line
5455 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
5456 # $1 is from_name, which we do not use
5457 $to_name = unquote($2);
5458 $to_name =~ s!^b/!!;
5459 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
5460 # $1 is 'cc' or 'combined', which we do not use
5461 $to_name = unquote($2);
5462 } else {
5463 $to_name = undef;
5466 # check if current patch belong to current raw line
5467 # and parse raw git-diff line if needed
5468 if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
5469 # this is continuation of a split patch
5470 print "<div class=\"patch cont\">\n";
5471 } else {
5472 # advance raw git-diff output if needed
5473 $patch_idx++ if defined $diffinfo;
5475 # read and prepare patch information
5476 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
5478 # compact combined diff output can have some patches skipped
5479 # find which patch (using pathname of result) we are at now;
5480 if ($is_combined) {
5481 while ($to_name ne $diffinfo->{'to_file'}) {
5482 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
5483 format_diff_cc_simplified($diffinfo, @hash_parents) .
5484 "</div>\n"; # class="patch"
5486 $patch_idx++;
5487 $patch_number++;
5489 last if $patch_idx > $#$difftree;
5490 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
5494 # modifies %from, %to hashes
5495 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
5497 # this is first patch for raw difftree line with $patch_idx index
5498 # we index @$difftree array from 0, but number patches from 1
5499 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
5502 # git diff header
5503 #assert($patch_line =~ m/^diff /) if DEBUG;
5504 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
5505 $patch_number++;
5506 # print "git diff" header
5507 print format_git_diff_header_line($patch_line, $diffinfo,
5508 \%from, \%to);
5510 # print extended diff header
5511 print "<div class=\"diff extended_header\">\n";
5512 EXTENDED_HEADER:
5513 while ($patch_line = <$fd>) {
5514 chomp $patch_line;
5516 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
5518 print format_extended_diff_header_line($patch_line, $diffinfo,
5519 \%from, \%to);
5521 print "</div>\n"; # class="diff extended_header"
5523 # from-file/to-file diff header
5524 if (! $patch_line) {
5525 print "</div>\n"; # class="patch"
5526 last PATCH;
5528 next PATCH if ($patch_line =~ m/^diff /);
5529 #assert($patch_line =~ m/^---/) if DEBUG;
5531 my $last_patch_line = $patch_line;
5532 $patch_line = <$fd>;
5533 chomp $patch_line;
5534 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
5536 print format_diff_from_to_header($last_patch_line, $patch_line,
5537 $diffinfo, \%from, \%to,
5538 @hash_parents);
5540 # the patch itself
5541 LINE:
5542 while ($patch_line = <$fd>) {
5543 chomp $patch_line;
5545 next PATCH if ($patch_line =~ m/^diff /);
5547 my $class = diff_line_class($patch_line, \%from, \%to);
5549 if ($class eq 'chunk_header') {
5550 print_diff_chunk($diff_style, scalar @hash_parents, \%from, \%to, @chunk);
5551 @chunk = ();
5554 push @chunk, [ $class, $patch_line ];
5557 } continue {
5558 if (@chunk) {
5559 print_diff_chunk($diff_style, scalar @hash_parents, \%from, \%to, @chunk);
5560 @chunk = ();
5562 print "</div>\n"; # class="patch"
5565 # for compact combined (--cc) format, with chunk and patch simplification
5566 # the patchset might be empty, but there might be unprocessed raw lines
5567 for (++$patch_idx if $patch_number > 0;
5568 $patch_idx < @$difftree;
5569 ++$patch_idx) {
5570 # read and prepare patch information
5571 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
5573 # generate anchor for "patch" links in difftree / whatchanged part
5574 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
5575 format_diff_cc_simplified($diffinfo, @hash_parents) .
5576 "</div>\n"; # class="patch"
5578 $patch_number++;
5581 if ($patch_number == 0) {
5582 if (@hash_parents > 1) {
5583 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
5584 } else {
5585 print "<div class=\"diff nodifferences\">No differences found</div>\n";
5589 print "</div>\n"; # class="patchset"
5592 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5594 sub git_project_search_form {
5595 my ($searchtext, $search_use_regexp) = @_;
5597 my $limit = '';
5598 if ($project_filter) {
5599 $limit = " in '$project_filter'";
5602 print "<div class=\"projsearch\">\n";
5603 print $cgi->start_form(-method => 'get', -action => $my_uri) .
5604 $cgi->hidden(-name => 'a', -value => 'project_list') . "\n";
5605 print $cgi->hidden(-name => 'pf', -value => $project_filter). "\n"
5606 if (defined $project_filter);
5607 print $cgi->textfield(-name => 's', -value => $searchtext,
5608 -title => "Search project by name and description$limit",
5609 -size => 60) . "\n" .
5610 "<span title=\"Extended regular expression\">" .
5611 $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
5612 -checked => $search_use_regexp) .
5613 "</span>\n" .
5614 $cgi->submit(-name => 'btnS', -value => 'Search') .
5615 $cgi->end_form() . "\n" .
5616 "<span class=\"projectlist_link\">" .
5617 $cgi->a({-href => href(project => undef, searchtext => undef,
5618 action => 'project_list',
5619 project_filter => $project_filter)},
5620 esc_html("List all projects$limit")) . "</span><br />\n";
5621 print "<span class=\"projectlist_link\">" .
5622 $cgi->a({-href => href(project => undef, searchtext => undef,
5623 action => 'project_list',
5624 project_filter => undef)},
5625 esc_html("List all projects")) . "</span>\n" if $project_filter;
5626 print "</div>\n";
5629 # entry for given @keys needs filling if at least one of keys in list
5630 # is not present in %$project_info
5631 sub project_info_needs_filling {
5632 my ($project_info, @keys) = @_;
5634 # return List::MoreUtils::any { !exists $project_info->{$_} } @keys;
5635 foreach my $key (@keys) {
5636 if (!exists $project_info->{$key}) {
5637 return 1;
5640 return;
5643 # fills project list info (age, description, owner, category, forks, etc.)
5644 # for each project in the list, removing invalid projects from
5645 # returned list, or fill only specified info.
5647 # Invalid projects are removed from the returned list if and only if you
5648 # ask 'age' or 'age_string' to be filled, because they are the only fields
5649 # that run unconditionally git command that requires repository, and
5650 # therefore do always check if project repository is invalid.
5652 # USAGE:
5653 # * fill_project_list_info(\@project_list, 'descr_long', 'ctags')
5654 # ensures that 'descr_long' and 'ctags' fields are filled
5655 # * @project_list = fill_project_list_info(\@project_list)
5656 # ensures that all fields are filled (and invalid projects removed)
5658 # NOTE: modifies $projlist, but does not remove entries from it
5659 sub fill_project_list_info {
5660 my ($projlist, @wanted_keys) = @_;
5661 my @projects;
5662 my $filter_set = sub { return @_; };
5663 if (@wanted_keys) {
5664 my %wanted_keys = map { $_ => 1 } @wanted_keys;
5665 $filter_set = sub { return grep { $wanted_keys{$_} } @_; };
5668 my $show_ctags = gitweb_check_feature('ctags');
5669 PROJECT:
5670 foreach my $pr (@$projlist) {
5671 if (project_info_needs_filling($pr, $filter_set->('age', 'age_string'))) {
5672 my (@activity) = git_get_last_activity($pr->{'path'});
5673 unless (@activity) {
5674 next PROJECT;
5676 ($pr->{'age'}, $pr->{'age_string'}) = @activity;
5678 if (project_info_needs_filling($pr, $filter_set->('descr', 'descr_long'))) {
5679 my $descr = git_get_project_description($pr->{'path'}) || "";
5680 $descr = to_utf8($descr);
5681 $pr->{'descr_long'} = $descr;
5682 $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
5684 if (project_info_needs_filling($pr, $filter_set->('owner'))) {
5685 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
5687 if ($show_ctags &&
5688 project_info_needs_filling($pr, $filter_set->('ctags'))) {
5689 $pr->{'ctags'} = git_get_project_ctags($pr->{'path'});
5691 if ($projects_list_group_categories &&
5692 project_info_needs_filling($pr, $filter_set->('category'))) {
5693 my $cat = git_get_project_category($pr->{'path'}) ||
5694 $project_list_default_category;
5695 $pr->{'category'} = to_utf8($cat);
5698 push @projects, $pr;
5701 return @projects;
5704 sub sort_projects_list {
5705 my ($projlist, $order) = @_;
5707 sub order_str {
5708 my $key = shift;
5709 return sub { $a->{$key} cmp $b->{$key} };
5712 sub order_num_then_undef {
5713 my $key = shift;
5714 return sub {
5715 defined $a->{$key} ?
5716 (defined $b->{$key} ? $a->{$key} <=> $b->{$key} : -1) :
5717 (defined $b->{$key} ? 1 : 0)
5721 my %orderings = (
5722 project => order_str('path'),
5723 descr => order_str('descr_long'),
5724 owner => order_str('owner'),
5725 age => order_num_then_undef('age'),
5728 my $ordering = $orderings{$order};
5729 return defined $ordering ? sort $ordering @$projlist : @$projlist;
5732 # returns a hash of categories, containing the list of project
5733 # belonging to each category
5734 sub build_projlist_by_category {
5735 my ($projlist, $from, $to) = @_;
5736 my %categories;
5738 $from = 0 unless defined $from;
5739 $to = $#$projlist if (!defined $to || $#$projlist < $to);
5741 for (my $i = $from; $i <= $to; $i++) {
5742 my $pr = $projlist->[$i];
5743 push @{$categories{ $pr->{'category'} }}, $pr;
5746 return wantarray ? %categories : \%categories;
5749 # print 'sort by' <th> element, generating 'sort by $name' replay link
5750 # if that order is not selected
5751 sub print_sort_th {
5752 print format_sort_th(@_);
5755 sub format_sort_th {
5756 my ($name, $order, $header) = @_;
5757 my $sort_th = "";
5758 $header ||= ucfirst($name);
5760 if ($order eq $name) {
5761 $sort_th .= "<th>$header</th>\n";
5762 } else {
5763 $sort_th .= "<th>" .
5764 $cgi->a({-href => href(-replay=>1, order=>$name),
5765 -class => "header"}, $header) .
5766 "</th>\n";
5769 return $sort_th;
5772 sub git_project_list_rows {
5773 my ($projlist, $from, $to, $check_forks) = @_;
5775 $from = 0 unless defined $from;
5776 $to = $#$projlist if (!defined $to || $#$projlist < $to);
5778 my $alternate = 1;
5779 for (my $i = $from; $i <= $to; $i++) {
5780 my $pr = $projlist->[$i];
5782 if ($alternate) {
5783 print "<tr class=\"dark\">\n";
5784 } else {
5785 print "<tr class=\"light\">\n";
5787 $alternate ^= 1;
5789 if ($check_forks) {
5790 print "<td>";
5791 if ($pr->{'forks'}) {
5792 my $nforks = scalar @{$pr->{'forks'}};
5793 if ($nforks > 0) {
5794 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks"),
5795 -title => "$nforks forks"}, "+");
5796 } else {
5797 print $cgi->span({-title => "$nforks forks"}, "+");
5800 print "</td>\n";
5802 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
5803 -class => "list"},
5804 esc_html_match_hl($pr->{'path'}, $search_regexp)) .
5805 "</td>\n" .
5806 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
5807 -class => "list",
5808 -title => $pr->{'descr_long'}},
5809 $search_regexp
5810 ? esc_html_match_hl_chopped($pr->{'descr_long'},
5811 $pr->{'descr'}, $search_regexp)
5812 : esc_html($pr->{'descr'})) .
5813 "</td>\n";
5814 unless ($omit_owner) {
5815 print "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n";
5817 unless ($omit_age_column) {
5818 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
5819 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n";
5821 print"<td class=\"link\">" .
5822 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
5823 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
5824 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
5825 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
5826 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
5827 "</td>\n" .
5828 "</tr>\n";
5832 sub git_project_list_body {
5833 # actually uses global variable $project
5834 my ($projlist, $order, $from, $to, $extra, $no_header, $ctags_action) = @_;
5835 my @projects = @$projlist;
5837 my $check_forks = gitweb_check_feature('forks');
5838 my $show_ctags = gitweb_check_feature('ctags');
5839 my $tagfilter = $show_ctags ? $input_params{'ctag_filter'} : undef;
5840 $check_forks = undef
5841 if ($tagfilter || $search_regexp);
5843 # filtering out forks before filling info allows to do less work
5844 @projects = filter_forks_from_projects_list(\@projects)
5845 if ($check_forks);
5846 # search_projects_list pre-fills required info
5847 @projects = search_projects_list(\@projects,
5848 'search_regexp' => $search_regexp,
5849 'tagfilter' => $tagfilter)
5850 if ($tagfilter || $search_regexp);
5851 # fill the rest
5852 my @all_fields = ('descr', 'descr_long', 'ctags', 'category');
5853 push @all_fields, ('age', 'age_string') unless($omit_age_column);
5854 push @all_fields, 'owner' unless($omit_owner);
5855 @projects = fill_project_list_info(\@projects, @all_fields);
5857 $order ||= $default_projects_order;
5858 $from = 0 unless defined $from;
5859 $to = $#projects if (!defined $to || $#projects < $to);
5861 # short circuit
5862 if ($from > $to) {
5863 print "<center>\n".
5864 "<b>No such projects found</b><br />\n".
5865 "Click ".$cgi->a({-href=>href(project=>undef,action=>'project_list')},"here")." to view all projects<br />\n".
5866 "</center>\n<br />\n";
5867 return;
5870 @projects = sort_projects_list(\@projects, $order);
5872 if ($show_ctags) {
5873 my $ctags = git_gather_all_ctags(\@projects);
5874 my $cloud = git_populate_project_tagcloud($ctags, $ctags_action||'project_list');
5875 print git_show_project_tagcloud($cloud, 64);
5878 print "<table class=\"project_list\">\n";
5879 unless ($no_header) {
5880 print "<tr>\n";
5881 if ($check_forks) {
5882 print "<th></th>\n";
5884 print_sort_th('project', $order, 'Project');
5885 print_sort_th('descr', $order, 'Description');
5886 print_sort_th('owner', $order, 'Owner') unless $omit_owner;
5887 print_sort_th('age', $order, 'Last Change') unless $omit_age_column;
5888 print "<th></th>\n" . # for links
5889 "</tr>\n";
5892 if ($projects_list_group_categories) {
5893 # only display categories with projects in the $from-$to window
5894 @projects = sort {$a->{'category'} cmp $b->{'category'}} @projects[$from..$to];
5895 my %categories = build_projlist_by_category(\@projects, $from, $to);
5896 foreach my $cat (sort keys %categories) {
5897 unless ($cat eq "") {
5898 print "<tr>\n";
5899 if ($check_forks) {
5900 print "<td></td>\n";
5902 print "<td class=\"category\" colspan=\"5\">".esc_html($cat)."</td>\n";
5903 print "</tr>\n";
5906 git_project_list_rows($categories{$cat}, undef, undef, $check_forks);
5908 } else {
5909 git_project_list_rows(\@projects, $from, $to, $check_forks);
5912 if (defined $extra) {
5913 print "<tr>\n";
5914 if ($check_forks) {
5915 print "<td></td>\n";
5917 print "<td colspan=\"5\">$extra</td>\n" .
5918 "</tr>\n";
5920 print "</table>\n";
5923 sub git_log_body {
5924 # uses global variable $project
5925 my ($commitlist, $from, $to, $refs, $extra) = @_;
5927 $from = 0 unless defined $from;
5928 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
5930 for (my $i = 0; $i <= $to; $i++) {
5931 my %co = %{$commitlist->[$i]};
5932 next if !%co;
5933 my $commit = $co{'id'};
5934 my $ref = format_ref_marker($refs, $commit);
5935 git_print_header_div('commit',
5936 "<span class=\"age\">$co{'age_string'}</span>" .
5937 esc_html($co{'title'}) . $ref,
5938 $commit);
5939 print "<div class=\"title_text\">\n" .
5940 "<div class=\"log_link\">\n" .
5941 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
5942 " | " .
5943 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
5944 " | " .
5945 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
5946 "<br/>\n" .
5947 "</div>\n";
5948 git_print_authorship(\%co, -tag => 'span');
5949 print "<br/>\n</div>\n";
5951 print "<div class=\"log_body\">\n";
5952 git_print_log($co{'comment'}, -final_empty_line=> 1);
5953 print "</div>\n";
5955 if ($extra) {
5956 print "<div class=\"page_nav\">\n";
5957 print "$extra\n";
5958 print "</div>\n";
5962 sub git_shortlog_body {
5963 # uses global variable $project
5964 my ($commitlist, $from, $to, $refs, $extra) = @_;
5966 $from = 0 unless defined $from;
5967 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
5969 print "<table class=\"shortlog\">\n";
5970 my $alternate = 1;
5971 for (my $i = $from; $i <= $to; $i++) {
5972 my %co = %{$commitlist->[$i]};
5973 my $commit = $co{'id'};
5974 my $ref = format_ref_marker($refs, $commit);
5975 if ($alternate) {
5976 print "<tr class=\"dark\">\n";
5977 } else {
5978 print "<tr class=\"light\">\n";
5980 $alternate ^= 1;
5981 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
5982 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5983 format_author_html('td', \%co, 10) . "<td>";
5984 print format_subject_html($co{'title'}, $co{'title_short'},
5985 href(action=>"commit", hash=>$commit), $ref);
5986 print "</td>\n" .
5987 "<td class=\"link\">" .
5988 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
5989 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
5990 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
5991 my $snapshot_links = format_snapshot_links($commit);
5992 if (defined $snapshot_links) {
5993 print " | " . $snapshot_links;
5995 print "</td>\n" .
5996 "</tr>\n";
5998 if (defined $extra) {
5999 print "<tr>\n" .
6000 "<td colspan=\"4\">$extra</td>\n" .
6001 "</tr>\n";
6003 print "</table>\n";
6006 sub git_history_body {
6007 # Warning: assumes constant type (blob or tree) during history
6008 my ($commitlist, $from, $to, $refs, $extra,
6009 $file_name, $file_hash, $ftype) = @_;
6011 $from = 0 unless defined $from;
6012 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
6014 print "<table class=\"history\">\n";
6015 my $alternate = 1;
6016 for (my $i = $from; $i <= $to; $i++) {
6017 my %co = %{$commitlist->[$i]};
6018 if (!%co) {
6019 next;
6021 my $commit = $co{'id'};
6023 my $ref = format_ref_marker($refs, $commit);
6025 if ($alternate) {
6026 print "<tr class=\"dark\">\n";
6027 } else {
6028 print "<tr class=\"light\">\n";
6030 $alternate ^= 1;
6031 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
6032 # shortlog: format_author_html('td', \%co, 10)
6033 format_author_html('td', \%co, 15, 3) . "<td>";
6034 # originally git_history used chop_str($co{'title'}, 50)
6035 print format_subject_html($co{'title'}, $co{'title_short'},
6036 href(action=>"commit", hash=>$commit), $ref);
6037 print "</td>\n" .
6038 "<td class=\"link\">" .
6039 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
6040 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
6042 if ($ftype eq 'blob') {
6043 my $blob_current = $file_hash;
6044 my $blob_parent = git_get_hash_by_path($commit, $file_name);
6045 if (defined $blob_current && defined $blob_parent &&
6046 $blob_current ne $blob_parent) {
6047 print " | " .
6048 $cgi->a({-href => href(action=>"blobdiff",
6049 hash=>$blob_current, hash_parent=>$blob_parent,
6050 hash_base=>$hash_base, hash_parent_base=>$commit,
6051 file_name=>$file_name)},
6052 "diff to current");
6055 print "</td>\n" .
6056 "</tr>\n";
6058 if (defined $extra) {
6059 print "<tr>\n" .
6060 "<td colspan=\"4\">$extra</td>\n" .
6061 "</tr>\n";
6063 print "</table>\n";
6066 sub git_tags_body {
6067 # uses global variable $project
6068 my ($taglist, $from, $to, $extra) = @_;
6069 $from = 0 unless defined $from;
6070 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
6072 print "<table class=\"tags\">\n";
6073 my $alternate = 1;
6074 for (my $i = $from; $i <= $to; $i++) {
6075 my $entry = $taglist->[$i];
6076 my %tag = %$entry;
6077 my $comment = $tag{'subject'};
6078 my $comment_short;
6079 if (defined $comment) {
6080 $comment_short = chop_str($comment, 30, 5);
6082 if ($alternate) {
6083 print "<tr class=\"dark\">\n";
6084 } else {
6085 print "<tr class=\"light\">\n";
6087 $alternate ^= 1;
6088 if (defined $tag{'age'}) {
6089 print "<td><i>$tag{'age'}</i></td>\n";
6090 } else {
6091 print "<td></td>\n";
6093 print "<td>" .
6094 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
6095 -class => "list name"}, esc_html($tag{'name'})) .
6096 "</td>\n" .
6097 "<td>";
6098 if (defined $comment) {
6099 print format_subject_html($comment, $comment_short,
6100 href(action=>"tag", hash=>$tag{'id'}));
6102 print "</td>\n" .
6103 "<td class=\"selflink\">";
6104 if ($tag{'type'} eq "tag") {
6105 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
6106 } else {
6107 print "&nbsp;";
6109 print "</td>\n" .
6110 "<td class=\"link\">" . " | " .
6111 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
6112 if ($tag{'reftype'} eq "commit") {
6113 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})}, "shortlog") .
6114 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})}, "log");
6115 } elsif ($tag{'reftype'} eq "blob") {
6116 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
6118 print "</td>\n" .
6119 "</tr>";
6121 if (defined $extra) {
6122 print "<tr>\n" .
6123 "<td colspan=\"5\">$extra</td>\n" .
6124 "</tr>\n";
6126 print "</table>\n";
6129 sub git_heads_body {
6130 # uses global variable $project
6131 my ($headlist, $head_at, $from, $to, $extra) = @_;
6132 $from = 0 unless defined $from;
6133 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
6135 print "<table class=\"heads\">\n";
6136 my $alternate = 1;
6137 for (my $i = $from; $i <= $to; $i++) {
6138 my $entry = $headlist->[$i];
6139 my %ref = %$entry;
6140 my $curr = defined $head_at && $ref{'id'} eq $head_at;
6141 if ($alternate) {
6142 print "<tr class=\"dark\">\n";
6143 } else {
6144 print "<tr class=\"light\">\n";
6146 $alternate ^= 1;
6147 print "<td><i>$ref{'age'}</i></td>\n" .
6148 ($curr ? "<td class=\"current_head\">" : "<td>") .
6149 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),
6150 -class => "list name"},esc_html($ref{'name'})) .
6151 "</td>\n" .
6152 "<td class=\"link\">" .
6153 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})}, "shortlog") . " | " .
6154 $cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})}, "log") . " | " .
6155 $cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'fullname'})}, "tree") .
6156 "</td>\n" .
6157 "</tr>";
6159 if (defined $extra) {
6160 print "<tr>\n" .
6161 "<td colspan=\"3\">$extra</td>\n" .
6162 "</tr>\n";
6164 print "</table>\n";
6167 # Display a single remote block
6168 sub git_remote_block {
6169 my ($remote, $rdata, $limit, $head) = @_;
6171 my $heads = $rdata->{'heads'};
6172 my $fetch = $rdata->{'fetch'};
6173 my $push = $rdata->{'push'};
6175 my $urls_table = "<table class=\"projects_list\">\n" ;
6177 if (defined $fetch) {
6178 if ($fetch eq $push) {
6179 $urls_table .= format_repo_url("URL", $fetch);
6180 } else {
6181 $urls_table .= format_repo_url("Fetch URL", $fetch);
6182 $urls_table .= format_repo_url("Push URL", $push) if defined $push;
6184 } elsif (defined $push) {
6185 $urls_table .= format_repo_url("Push URL", $push);
6186 } else {
6187 $urls_table .= format_repo_url("", "No remote URL");
6190 $urls_table .= "</table>\n";
6192 my $dots;
6193 if (defined $limit && $limit < @$heads) {
6194 $dots = $cgi->a({-href => href(action=>"remotes", hash=>$remote)}, "...");
6197 print $urls_table;
6198 git_heads_body($heads, $head, 0, $limit, $dots);
6201 # Display a list of remote names with the respective fetch and push URLs
6202 sub git_remotes_list {
6203 my ($remotedata, $limit) = @_;
6204 print "<table class=\"heads\">\n";
6205 my $alternate = 1;
6206 my @remotes = sort keys %$remotedata;
6208 my $limited = $limit && $limit < @remotes;
6210 $#remotes = $limit - 1 if $limited;
6212 while (my $remote = shift @remotes) {
6213 my $rdata = $remotedata->{$remote};
6214 my $fetch = $rdata->{'fetch'};
6215 my $push = $rdata->{'push'};
6216 if ($alternate) {
6217 print "<tr class=\"dark\">\n";
6218 } else {
6219 print "<tr class=\"light\">\n";
6221 $alternate ^= 1;
6222 print "<td>" .
6223 $cgi->a({-href=> href(action=>'remotes', hash=>$remote),
6224 -class=> "list name"},esc_html($remote)) .
6225 "</td>";
6226 print "<td class=\"link\">" .
6227 (defined $fetch ? $cgi->a({-href=> $fetch}, "fetch") : "fetch") .
6228 " | " .
6229 (defined $push ? $cgi->a({-href=> $push}, "push") : "push") .
6230 "</td>";
6232 print "</tr>\n";
6235 if ($limited) {
6236 print "<tr>\n" .
6237 "<td colspan=\"3\">" .
6238 $cgi->a({-href => href(action=>"remotes")}, "...") .
6239 "</td>\n" . "</tr>\n";
6242 print "</table>";
6245 # Display remote heads grouped by remote, unless there are too many
6246 # remotes, in which case we only display the remote names
6247 sub git_remotes_body {
6248 my ($remotedata, $limit, $head) = @_;
6249 if ($limit and $limit < keys %$remotedata) {
6250 git_remotes_list($remotedata, $limit);
6251 } else {
6252 fill_remote_heads($remotedata);
6253 while (my ($remote, $rdata) = each %$remotedata) {
6254 git_print_section({-class=>"remote", -id=>$remote},
6255 ["remotes", $remote, $remote], sub {
6256 git_remote_block($remote, $rdata, $limit, $head);
6262 sub git_search_message {
6263 my %co = @_;
6265 my $greptype;
6266 if ($searchtype eq 'commit') {
6267 $greptype = "--grep=";
6268 } elsif ($searchtype eq 'author') {
6269 $greptype = "--author=";
6270 } elsif ($searchtype eq 'committer') {
6271 $greptype = "--committer=";
6273 $greptype .= $searchtext;
6274 my @commitlist = parse_commits($hash, 101, (100 * $page), undef,
6275 $greptype, '--regexp-ignore-case',
6276 $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
6278 my $paging_nav = '';
6279 if ($page > 0) {
6280 $paging_nav .=
6281 $cgi->a({-href => href(-replay=>1, page=>undef)},
6282 "first") .
6283 " &sdot; " .
6284 $cgi->a({-href => href(-replay=>1, page=>$page-1),
6285 -accesskey => "p", -title => "Alt-p"}, "prev");
6286 } else {
6287 $paging_nav .= "first &sdot; prev";
6289 my $next_link = '';
6290 if ($#commitlist >= 100) {
6291 $next_link =
6292 $cgi->a({-href => href(-replay=>1, page=>$page+1),
6293 -accesskey => "n", -title => "Alt-n"}, "next");
6294 $paging_nav .= " &sdot; $next_link";
6295 } else {
6296 $paging_nav .= " &sdot; next";
6299 git_header_html();
6301 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
6302 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6303 if ($page == 0 && !@commitlist) {
6304 print "<p>No match.</p>\n";
6305 } else {
6306 git_search_grep_body(\@commitlist, 0, 99, $next_link);
6309 git_footer_html();
6312 sub git_search_changes {
6313 my %co = @_;
6315 local $/ = "\n";
6316 open my $fd, '-|', git_cmd(), '--no-pager', 'log', @diff_opts,
6317 '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
6318 ($search_use_regexp ? '--pickaxe-regex' : ())
6319 or die_error(500, "Open git-log failed");
6321 git_header_html();
6323 git_print_page_nav('','', $hash,$co{'tree'},$hash);
6324 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6326 print "<table class=\"pickaxe search\">\n";
6327 my $alternate = 1;
6328 undef %co;
6329 my @files;
6330 while (my $line = <$fd>) {
6331 chomp $line;
6332 next unless $line;
6334 my %set = parse_difftree_raw_line($line);
6335 if (defined $set{'commit'}) {
6336 # finish previous commit
6337 if (%co) {
6338 print "</td>\n" .
6339 "<td class=\"link\">" .
6340 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},
6341 "commit") .
6342 " | " .
6343 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'},
6344 hash_base=>$co{'id'})},
6345 "tree") .
6346 "</td>\n" .
6347 "</tr>\n";
6350 if ($alternate) {
6351 print "<tr class=\"dark\">\n";
6352 } else {
6353 print "<tr class=\"light\">\n";
6355 $alternate ^= 1;
6356 %co = parse_commit($set{'commit'});
6357 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
6358 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
6359 "<td><i>$author</i></td>\n" .
6360 "<td>" .
6361 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
6362 -class => "list subject"},
6363 chop_and_escape_str($co{'title'}, 50) . "<br/>");
6364 } elsif (defined $set{'to_id'}) {
6365 next if ($set{'to_id'} =~ m/^0{40}$/);
6367 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
6368 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),
6369 -class => "list"},
6370 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
6371 "<br/>\n";
6374 close $fd;
6376 # finish last commit (warning: repetition!)
6377 if (%co) {
6378 print "</td>\n" .
6379 "<td class=\"link\">" .
6380 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},
6381 "commit") .
6382 " | " .
6383 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'},
6384 hash_base=>$co{'id'})},
6385 "tree") .
6386 "</td>\n" .
6387 "</tr>\n";
6390 print "</table>\n";
6392 git_footer_html();
6395 sub git_search_files {
6396 my %co = @_;
6398 local $/ = "\n";
6399 open my $fd, "-|", git_cmd(), 'grep', '-n', '-z',
6400 $search_use_regexp ? ('-E', '-i') : '-F',
6401 $searchtext, $co{'tree'}
6402 or die_error(500, "Open git-grep failed");
6404 git_header_html();
6406 git_print_page_nav('','', $hash,$co{'tree'},$hash);
6407 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6409 print "<table class=\"grep_search\">\n";
6410 my $alternate = 1;
6411 my $matches = 0;
6412 my $lastfile = '';
6413 my $file_href;
6414 while (my $line = <$fd>) {
6415 chomp $line;
6416 my ($file, $lno, $ltext, $binary);
6417 last if ($matches++ > 1000);
6418 if ($line =~ /^Binary file (.+) matches$/) {
6419 $file = $1;
6420 $binary = 1;
6421 } else {
6422 ($file, $lno, $ltext) = split(/\0/, $line, 3);
6423 $file =~ s/^$co{'tree'}://;
6425 if ($file ne $lastfile) {
6426 $lastfile and print "</td></tr>\n";
6427 if ($alternate++) {
6428 print "<tr class=\"dark\">\n";
6429 } else {
6430 print "<tr class=\"light\">\n";
6432 $file_href = href(action=>"blob", hash_base=>$co{'id'},
6433 file_name=>$file);
6434 print "<td class=\"list\">".
6435 $cgi->a({-href => $file_href, -class => "list"}, esc_path($file));
6436 print "</td><td>\n";
6437 $lastfile = $file;
6439 if ($binary) {
6440 print "<div class=\"binary\">Binary file</div>\n";
6441 } else {
6442 $ltext = untabify($ltext);
6443 if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
6444 $ltext = esc_html($1, -nbsp=>1);
6445 $ltext .= '<span class="match">';
6446 $ltext .= esc_html($2, -nbsp=>1);
6447 $ltext .= '</span>';
6448 $ltext .= esc_html($3, -nbsp=>1);
6449 } else {
6450 $ltext = esc_html($ltext, -nbsp=>1);
6452 print "<div class=\"pre\">" .
6453 $cgi->a({-href => $file_href.'#l'.$lno,
6454 -class => "linenr"}, sprintf('%4i', $lno)) .
6455 ' ' . $ltext . "</div>\n";
6458 if ($lastfile) {
6459 print "</td></tr>\n";
6460 if ($matches > 1000) {
6461 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
6463 } else {
6464 print "<div class=\"diff nodifferences\">No matches found</div>\n";
6466 close $fd;
6468 print "</table>\n";
6470 git_footer_html();
6473 sub git_search_grep_body {
6474 my ($commitlist, $from, $to, $extra) = @_;
6475 $from = 0 unless defined $from;
6476 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
6478 print "<table class=\"commit_search\">\n";
6479 my $alternate = 1;
6480 for (my $i = $from; $i <= $to; $i++) {
6481 my %co = %{$commitlist->[$i]};
6482 if (!%co) {
6483 next;
6485 my $commit = $co{'id'};
6486 if ($alternate) {
6487 print "<tr class=\"dark\">\n";
6488 } else {
6489 print "<tr class=\"light\">\n";
6491 $alternate ^= 1;
6492 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
6493 format_author_html('td', \%co, 15, 5) .
6494 "<td>" .
6495 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
6496 -class => "list subject"},
6497 chop_and_escape_str($co{'title'}, 50) . "<br/>");
6498 my $comment = $co{'comment'};
6499 foreach my $line (@$comment) {
6500 if ($line =~ m/^(.*?)($search_regexp)(.*)$/i) {
6501 my ($lead, $match, $trail) = ($1, $2, $3);
6502 $match = chop_str($match, 70, 5, 'center');
6503 my $contextlen = int((80 - length($match))/2);
6504 $contextlen = 30 if ($contextlen > 30);
6505 $lead = chop_str($lead, $contextlen, 10, 'left');
6506 $trail = chop_str($trail, $contextlen, 10, 'right');
6508 $lead = esc_html($lead);
6509 $match = esc_html($match);
6510 $trail = esc_html($trail);
6512 print "$lead<span class=\"match\">$match</span>$trail<br />";
6515 print "</td>\n" .
6516 "<td class=\"link\">" .
6517 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
6518 " | " .
6519 $cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})}, "commitdiff") .
6520 " | " .
6521 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
6522 print "</td>\n" .
6523 "</tr>\n";
6525 if (defined $extra) {
6526 print "<tr>\n" .
6527 "<td colspan=\"3\">$extra</td>\n" .
6528 "</tr>\n";
6530 print "</table>\n";
6533 ## ======================================================================
6534 ## ======================================================================
6535 ## actions
6537 sub git_project_list_load {
6538 my $empty_list_ok = shift;
6539 my $order = $input_params{'order'};
6540 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
6541 die_error(400, "Unknown order parameter");
6544 my @list = git_get_projects_list($project_filter, $strict_export);
6545 if (!@list) {
6546 die_error(404, "No projects found") unless $empty_list_ok;
6549 return (\@list, $order);
6552 sub git_frontpage {
6553 my ($projlist, $order);
6555 if ($frontpage_no_project_list) {
6556 $project = undef;
6557 $project_filter = undef;
6558 } else {
6559 ($projlist, $order) = git_project_list_load(1);
6561 git_header_html();
6562 if (defined $home_text && -f $home_text) {
6563 print "<div class=\"index_include\">\n";
6564 insert_file($home_text);
6565 print "</div>\n";
6567 git_project_search_form($searchtext, $search_use_regexp);
6568 if ($frontpage_no_project_list) {
6569 my $show_ctags = gitweb_check_feature('ctags');
6570 if ($frontpage_no_project_list == 1 and $show_ctags) {
6571 my @projects = git_get_projects_list($project_filter, $strict_export);
6572 @projects = filter_forks_from_projects_list(\@projects) if gitweb_check_feature('forks');
6573 @projects = fill_project_list_info(\@projects, 'ctags');
6574 my $ctags = git_gather_all_ctags(\@projects);
6575 my $cloud = git_populate_project_tagcloud($ctags, 'project_list');
6576 print git_show_project_tagcloud($cloud, 64);
6578 } else {
6579 git_project_list_body($projlist, $order);
6581 git_footer_html();
6584 sub git_project_list {
6585 my ($projlist, $order) = git_project_list_load();
6586 git_header_html();
6587 if (!$frontpage_no_project_list && defined $home_text && -f $home_text) {
6588 print "<div class=\"index_include\">\n";
6589 insert_file($home_text);
6590 print "</div>\n";
6592 git_project_search_form();
6593 git_project_list_body($projlist, $order);
6594 git_footer_html();
6597 sub git_forks {
6598 my $order = $input_params{'order'};
6599 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
6600 die_error(400, "Unknown order parameter");
6603 my $filter = $project;
6604 $filter =~ s/\.git$//;
6605 my @list = git_get_projects_list($filter);
6606 if (!@list) {
6607 die_error(404, "No forks found");
6610 git_header_html();
6611 git_print_page_nav('','');
6612 git_print_header_div('summary', "$project forks");
6613 git_project_list_body(\@list, $order, undef, undef, undef, undef, 'forks');
6614 git_footer_html();
6617 sub git_project_index {
6618 my @projects = git_get_projects_list($project_filter, $strict_export);
6619 if (!@projects) {
6620 die_error(404, "No projects found");
6623 print $cgi->header(
6624 -type => 'text/plain',
6625 -charset => 'utf-8',
6626 -content_disposition => 'inline; filename="index.aux"');
6628 foreach my $pr (@projects) {
6629 if (!exists $pr->{'owner'}) {
6630 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
6633 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
6634 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
6635 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
6636 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
6637 $path =~ s/ /\+/g;
6638 $owner =~ s/ /\+/g;
6640 print "$path $owner\n";
6644 sub git_summary {
6645 my $descr = git_get_project_description($project) || "none";
6646 my %co = parse_commit("HEAD");
6647 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
6648 my $head = $co{'id'};
6649 my $remote_heads = gitweb_check_feature('remote_heads');
6651 my $owner = git_get_project_owner($project);
6653 my $refs = git_get_references();
6654 # These get_*_list functions return one more to allow us to see if
6655 # there are more ...
6656 my @taglist = git_get_tags_list(16);
6657 my @headlist = git_get_heads_list(16);
6658 my %remotedata = $remote_heads ? git_get_remotes_list() : ();
6659 my @forklist;
6660 my $check_forks = gitweb_check_feature('forks');
6662 if ($check_forks) {
6663 # find forks of a project
6664 my $filter = $project;
6665 $filter =~ s/\.git$//;
6666 @forklist = git_get_projects_list($filter);
6667 # filter out forks of forks
6668 @forklist = filter_forks_from_projects_list(\@forklist)
6669 if (@forklist);
6672 git_header_html();
6673 git_print_page_nav('summary','', $head);
6675 print "<div class=\"title\">&nbsp;</div>\n";
6676 print "<table class=\"projects_list\">\n" .
6677 "<tr id=\"metadata_desc\"><td>description</td><td>" . esc_html($descr) . "</td></tr>\n";
6678 if ($owner and not $omit_owner) {
6679 print "<tr id=\"metadata_owner\"><td>owner</td><td>" . esc_html($owner) . "</td></tr>\n";
6681 if (defined $cd{'rfc2822'}) {
6682 print "<tr id=\"metadata_lchange\"><td>last change</td>" .
6683 "<td>".format_timestamp_html(\%cd)."</td></tr>\n";
6686 # use per project git URL list in $projectroot/$project/cloneurl
6687 # or make project git URL from git base URL and project name
6688 my $url_tag = "URL";
6689 my @url_list = git_get_project_url_list($project);
6690 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
6691 foreach my $git_url (@url_list) {
6692 next unless $git_url;
6693 print format_repo_url($url_tag, $git_url);
6694 $url_tag = "";
6697 # Tag cloud
6698 my $show_ctags = gitweb_check_feature('ctags');
6699 if ($show_ctags) {
6700 my $ctags = git_get_project_ctags($project);
6701 if (%$ctags || $show_ctags !~ /^\d+$/) {
6702 # without ability to add tags, don't show if there are none
6703 my $cloud = git_populate_project_tagcloud($ctags, 'project_list');
6704 print "<tr id=\"metadata_ctags\">" .
6705 "<td style=\"vertical-align:middle\">content&#160;tags<br />";
6706 print "</td>\n<td>" unless %$ctags;
6707 print "<form action=\"$show_ctags\" method=\"post\" style=\"white-space:nowrap\">" .
6708 "<input type=\"hidden\" name=\"p\" value=\"$project\"/>" .
6709 "add: <input type=\"text\" name=\"t\" size=\"8\" /></form>"
6710 unless $show_ctags =~ /^\d+$/;
6711 print "</td>\n<td>" if %$ctags;
6712 print git_show_project_tagcloud($cloud, 48)."</td>" .
6713 "</tr>\n";
6717 print "</table>\n";
6719 # If XSS prevention is on, we don't include README.html.
6720 # TODO: Allow a readme in some safe format.
6721 if (!$prevent_xss && -s "$projectroot/$project/README.html") {
6722 print "<div class=\"title\">readme</div>\n" .
6723 "<div class=\"readme\">\n";
6724 insert_file("$projectroot/$project/README.html");
6725 print "\n</div>\n"; # class="readme"
6728 # we need to request one more than 16 (0..15) to check if
6729 # those 16 are all
6730 my @commitlist = $head ? parse_commits($head, 17) : ();
6731 if (@commitlist) {
6732 git_print_header_div('shortlog');
6733 git_shortlog_body(\@commitlist, 0, 15, $refs,
6734 $#commitlist <= 15 ? undef :
6735 $cgi->a({-href => href(action=>"shortlog")}, "..."));
6738 if (@taglist) {
6739 git_print_header_div('tags');
6740 git_tags_body(\@taglist, 0, 15,
6741 $#taglist <= 15 ? undef :
6742 $cgi->a({-href => href(action=>"tags")}, "..."));
6745 if (@headlist) {
6746 git_print_header_div('heads');
6747 git_heads_body(\@headlist, $head, 0, 15,
6748 $#headlist <= 15 ? undef :
6749 $cgi->a({-href => href(action=>"heads")}, "..."));
6752 if (%remotedata) {
6753 git_print_header_div('remotes');
6754 git_remotes_body(\%remotedata, 15, $head);
6757 if (@forklist) {
6758 git_print_header_div('forks');
6759 git_project_list_body(\@forklist, 'age', 0, 15,
6760 $#forklist <= 15 ? undef :
6761 $cgi->a({-href => href(action=>"forks")}, "..."),
6762 'no_header', 'forks');
6765 git_footer_html();
6768 sub git_tag {
6769 my %tag = parse_tag($hash);
6771 if (! %tag) {
6772 die_error(404, "Unknown tag object");
6775 my $head = git_get_head_hash($project);
6776 git_header_html();
6777 git_print_page_nav('','', $head,undef,$head);
6778 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
6779 print "<div class=\"title_text\">\n" .
6780 "<table class=\"object_header\">\n" .
6781 "<tr>\n" .
6782 "<td>object</td>\n" .
6783 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
6784 $tag{'object'}) . "</td>\n" .
6785 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
6786 $tag{'type'}) . "</td>\n" .
6787 "</tr>\n";
6788 if (defined($tag{'author'})) {
6789 git_print_authorship_rows(\%tag, 'author');
6791 print "</table>\n\n" .
6792 "</div>\n";
6793 print "<div class=\"page_body\">";
6794 my $comment = $tag{'comment'};
6795 foreach my $line (@$comment) {
6796 chomp $line;
6797 print esc_html($line, -nbsp=>1) . "<br/>\n";
6799 print "</div>\n";
6800 git_footer_html();
6803 sub git_blame_common {
6804 my $format = shift || 'porcelain';
6805 if ($format eq 'porcelain' && $input_params{'javascript'}) {
6806 $format = 'incremental';
6807 $action = 'blame_incremental'; # for page title etc
6810 # permissions
6811 gitweb_check_feature('blame')
6812 or die_error(403, "Blame view not allowed");
6814 # error checking
6815 die_error(400, "No file name given") unless $file_name;
6816 $hash_base ||= git_get_head_hash($project);
6817 die_error(404, "Couldn't find base commit") unless $hash_base;
6818 my %co = parse_commit($hash_base)
6819 or die_error(404, "Commit not found");
6820 my $ftype = "blob";
6821 if (!defined $hash) {
6822 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
6823 or die_error(404, "Error looking up file");
6824 } else {
6825 $ftype = git_get_type($hash);
6826 if ($ftype !~ "blob") {
6827 die_error(400, "Object is not a blob");
6831 my $fd;
6832 if ($format eq 'incremental') {
6833 # get file contents (as base)
6834 open $fd, "-|", git_cmd(), 'cat-file', 'blob', $hash
6835 or die_error(500, "Open git-cat-file failed");
6836 } elsif ($format eq 'data') {
6837 # run git-blame --incremental
6838 open $fd, "-|", git_cmd(), "blame", "--incremental",
6839 $hash_base, "--", $file_name
6840 or die_error(500, "Open git-blame --incremental failed");
6841 } else {
6842 # run git-blame --porcelain
6843 open $fd, "-|", git_cmd(), "blame", '-p',
6844 $hash_base, '--', $file_name
6845 or die_error(500, "Open git-blame --porcelain failed");
6847 binmode $fd, ':utf8';
6849 # incremental blame data returns early
6850 if ($format eq 'data') {
6851 print $cgi->header(
6852 -type=>"text/plain", -charset => "utf-8",
6853 -status=> "200 OK");
6854 local $| = 1; # output autoflush
6855 while (my $line = <$fd>) {
6856 print to_utf8($line);
6858 close $fd
6859 or print "ERROR $!\n";
6861 print 'END';
6862 if (defined $t0 && gitweb_check_feature('timed')) {
6863 print ' '.
6864 tv_interval($t0, [ gettimeofday() ]).
6865 ' '.$number_of_git_cmds;
6867 print "\n";
6869 return;
6872 # page header
6873 git_header_html();
6874 my $formats_nav =
6875 $cgi->a({-href => href(action=>"blob", -replay=>1)},
6876 "blob") .
6877 " | ";
6878 if ($format eq 'incremental') {
6879 $formats_nav .=
6880 $cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},
6881 "blame") . " (non-incremental)";
6882 } else {
6883 $formats_nav .=
6884 $cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},
6885 "blame") . " (incremental)";
6887 $formats_nav .=
6888 " | " .
6889 $cgi->a({-href => href(action=>"history", -replay=>1)},
6890 "history") .
6891 " | " .
6892 $cgi->a({-href => href(action=>$action, file_name=>$file_name)},
6893 "HEAD");
6894 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
6895 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
6896 git_print_page_path($file_name, $ftype, $hash_base);
6898 # page body
6899 if ($format eq 'incremental') {
6900 print "<noscript>\n<div class=\"error\"><center><b>\n".
6901 "This page requires JavaScript to run.\n Use ".
6902 $cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},
6903 'this page').
6904 " instead.\n".
6905 "</b></center></div>\n</noscript>\n";
6907 print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;
6910 print qq!<div class="page_body">\n!;
6911 print qq!<div id="progress_info">... / ...</div>\n!
6912 if ($format eq 'incremental');
6913 print qq!<table id="blame_table" class="blame" width="100%">\n!.
6914 #qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.
6915 qq!<thead>\n!.
6916 qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.
6917 qq!</thead>\n!.
6918 qq!<tbody>\n!;
6920 my @rev_color = qw(light dark);
6921 my $num_colors = scalar(@rev_color);
6922 my $current_color = 0;
6924 if ($format eq 'incremental') {
6925 my $color_class = $rev_color[$current_color];
6927 #contents of a file
6928 my $linenr = 0;
6929 LINE:
6930 while (my $line = <$fd>) {
6931 chomp $line;
6932 $linenr++;
6934 print qq!<tr id="l$linenr" class="$color_class">!.
6935 qq!<td class="sha1"><a href=""> </a></td>!.
6936 qq!<td class="linenr">!.
6937 qq!<a class="linenr" href="">$linenr</a></td>!;
6938 print qq!<td class="pre">! . esc_html($line) . "</td>\n";
6939 print qq!</tr>\n!;
6942 } else { # porcelain, i.e. ordinary blame
6943 my %metainfo = (); # saves information about commits
6945 # blame data
6946 LINE:
6947 while (my $line = <$fd>) {
6948 chomp $line;
6949 # the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]
6950 # no <lines in group> for subsequent lines in group of lines
6951 my ($full_rev, $orig_lineno, $lineno, $group_size) =
6952 ($line =~ /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);
6953 if (!exists $metainfo{$full_rev}) {
6954 $metainfo{$full_rev} = { 'nprevious' => 0 };
6956 my $meta = $metainfo{$full_rev};
6957 my $data;
6958 while ($data = <$fd>) {
6959 chomp $data;
6960 last if ($data =~ s/^\t//); # contents of line
6961 if ($data =~ /^(\S+)(?: (.*))?$/) {
6962 $meta->{$1} = $2 unless exists $meta->{$1};
6964 if ($data =~ /^previous /) {
6965 $meta->{'nprevious'}++;
6968 my $short_rev = substr($full_rev, 0, 8);
6969 my $author = $meta->{'author'};
6970 my %date =
6971 parse_date($meta->{'author-time'}, $meta->{'author-tz'});
6972 my $date = $date{'iso-tz'};
6973 if ($group_size) {
6974 $current_color = ($current_color + 1) % $num_colors;
6976 my $tr_class = $rev_color[$current_color];
6977 $tr_class .= ' boundary' if (exists $meta->{'boundary'});
6978 $tr_class .= ' no-previous' if ($meta->{'nprevious'} == 0);
6979 $tr_class .= ' multiple-previous' if ($meta->{'nprevious'} > 1);
6980 print "<tr id=\"l$lineno\" class=\"$tr_class\">\n";
6981 if ($group_size) {
6982 print "<td class=\"sha1\"";
6983 print " title=\"". esc_html($author) . ", $date\"";
6984 print " rowspan=\"$group_size\"" if ($group_size > 1);
6985 print ">";
6986 print $cgi->a({-href => href(action=>"commit",
6987 hash=>$full_rev,
6988 file_name=>$file_name)},
6989 esc_html($short_rev));
6990 if ($group_size >= 2) {
6991 my @author_initials = ($author =~ /\b([[:upper:]])\B/g);
6992 if (@author_initials) {
6993 print "<br />" .
6994 esc_html(join('', @author_initials));
6995 # or join('.', ...)
6998 print "</td>\n";
7000 # 'previous' <sha1 of parent commit> <filename at commit>
7001 if (exists $meta->{'previous'} &&
7002 $meta->{'previous'} =~ /^([a-fA-F0-9]{40}) (.*)$/) {
7003 $meta->{'parent'} = $1;
7004 $meta->{'file_parent'} = unquote($2);
7006 my $linenr_commit =
7007 exists($meta->{'parent'}) ?
7008 $meta->{'parent'} : $full_rev;
7009 my $linenr_filename =
7010 exists($meta->{'file_parent'}) ?
7011 $meta->{'file_parent'} : unquote($meta->{'filename'});
7012 my $blamed = href(action => 'blame',
7013 file_name => $linenr_filename,
7014 hash_base => $linenr_commit);
7015 print "<td class=\"linenr\">";
7016 print $cgi->a({ -href => "$blamed#l$orig_lineno",
7017 -class => "linenr" },
7018 esc_html($lineno));
7019 print "</td>";
7020 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
7021 print "</tr>\n";
7022 } # end while
7026 # footer
7027 print "</tbody>\n".
7028 "</table>\n"; # class="blame"
7029 print "</div>\n"; # class="blame_body"
7030 close $fd
7031 or print "Reading blob failed\n";
7033 git_footer_html();
7036 sub git_blame {
7037 git_blame_common();
7040 sub git_blame_incremental {
7041 git_blame_common('incremental');
7044 sub git_blame_data {
7045 git_blame_common('data');
7048 sub git_tags {
7049 my $head = git_get_head_hash($project);
7050 git_header_html();
7051 git_print_page_nav('','', $head,undef,$head,format_ref_views('tags'));
7052 git_print_header_div('summary', $project);
7054 my @tagslist = git_get_tags_list();
7055 if (@tagslist) {
7056 git_tags_body(\@tagslist);
7058 git_footer_html();
7061 sub git_heads {
7062 my $head = git_get_head_hash($project);
7063 git_header_html();
7064 git_print_page_nav('','', $head,undef,$head,format_ref_views('heads'));
7065 git_print_header_div('summary', $project);
7067 my @headslist = git_get_heads_list();
7068 if (@headslist) {
7069 git_heads_body(\@headslist, $head);
7071 git_footer_html();
7074 # used both for single remote view and for list of all the remotes
7075 sub git_remotes {
7076 gitweb_check_feature('remote_heads')
7077 or die_error(403, "Remote heads view is disabled");
7079 my $head = git_get_head_hash($project);
7080 my $remote = $input_params{'hash'};
7082 my $remotedata = git_get_remotes_list($remote);
7083 die_error(500, "Unable to get remote information") unless defined $remotedata;
7085 unless (%$remotedata) {
7086 die_error(404, defined $remote ?
7087 "Remote $remote not found" :
7088 "No remotes found");
7091 git_header_html(undef, undef, -action_extra => $remote);
7092 git_print_page_nav('', '', $head, undef, $head,
7093 format_ref_views($remote ? '' : 'remotes'));
7095 fill_remote_heads($remotedata);
7096 if (defined $remote) {
7097 git_print_header_div('remotes', "$remote remote for $project");
7098 git_remote_block($remote, $remotedata->{$remote}, undef, $head);
7099 } else {
7100 git_print_header_div('summary', "$project remotes");
7101 git_remotes_body($remotedata, undef, $head);
7104 git_footer_html();
7107 sub git_blob_plain {
7108 my $type = shift;
7109 my $expires;
7111 if (!defined $hash) {
7112 if (defined $file_name) {
7113 my $base = $hash_base || git_get_head_hash($project);
7114 $hash = git_get_hash_by_path($base, $file_name, "blob")
7115 or die_error(404, "Cannot find file");
7116 } else {
7117 die_error(400, "No file name defined");
7119 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
7120 # blobs defined by non-textual hash id's can be cached
7121 $expires = "+1d";
7124 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
7125 or die_error(500, "Open git-cat-file blob '$hash' failed");
7127 # content-type (can include charset)
7128 $type = blob_contenttype($fd, $file_name, $type);
7130 # "save as" filename, even when no $file_name is given
7131 my $save_as = "$hash";
7132 if (defined $file_name) {
7133 $save_as = $file_name;
7134 } elsif ($type =~ m/^text\//) {
7135 $save_as .= '.txt';
7138 # With XSS prevention on, blobs of all types except a few known safe
7139 # ones are served with "Content-Disposition: attachment" to make sure
7140 # they don't run in our security domain. For certain image types,
7141 # blob view writes an <img> tag referring to blob_plain view, and we
7142 # want to be sure not to break that by serving the image as an
7143 # attachment (though Firefox 3 doesn't seem to care).
7144 my $sandbox = $prevent_xss &&
7145 $type !~ m!^(?:text/[a-z]+|image/(?:gif|png|jpeg))(?:[ ;]|$)!;
7147 # serve text/* as text/plain
7148 if ($prevent_xss &&
7149 ($type =~ m!^text/[a-z]+\b(.*)$! ||
7150 ($type =~ m!^[a-z]+/[a-z]\+xml\b(.*)$! && -T $fd))) {
7151 my $rest = $1;
7152 $rest = defined $rest ? $rest : '';
7153 $type = "text/plain$rest";
7156 print $cgi->header(
7157 -type => $type,
7158 -expires => $expires,
7159 -content_disposition =>
7160 ($sandbox ? 'attachment' : 'inline')
7161 . '; filename="' . $save_as . '"');
7162 local $/ = undef;
7163 binmode STDOUT, ':raw';
7164 print <$fd>;
7165 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
7166 close $fd;
7169 sub git_blob {
7170 my $expires;
7172 if (!defined $hash) {
7173 if (defined $file_name) {
7174 my $base = $hash_base || git_get_head_hash($project);
7175 $hash = git_get_hash_by_path($base, $file_name, "blob")
7176 or die_error(404, "Cannot find file");
7177 } else {
7178 die_error(400, "No file name defined");
7180 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
7181 # blobs defined by non-textual hash id's can be cached
7182 $expires = "+1d";
7185 my $have_blame = gitweb_check_feature('blame');
7186 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
7187 or die_error(500, "Couldn't cat $file_name, $hash");
7188 my $mimetype = blob_mimetype($fd, $file_name);
7189 # use 'blob_plain' (aka 'raw') view for files that cannot be displayed
7190 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {
7191 close $fd;
7192 return git_blob_plain($mimetype);
7194 # we can have blame only for text/* mimetype
7195 $have_blame &&= ($mimetype =~ m!^text/!);
7197 my $highlight = gitweb_check_feature('highlight');
7198 my $syntax = guess_file_syntax($highlight, $mimetype, $file_name);
7199 $fd = run_highlighter($fd, $highlight, $syntax)
7200 if $syntax;
7202 git_header_html(undef, $expires);
7203 my $formats_nav = '';
7204 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
7205 if (defined $file_name) {
7206 if ($have_blame) {
7207 $formats_nav .=
7208 $cgi->a({-href => href(action=>"blame", -replay=>1)},
7209 "blame") .
7210 " | ";
7212 $formats_nav .=
7213 $cgi->a({-href => href(action=>"history", -replay=>1)},
7214 "history") .
7215 " | " .
7216 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
7217 "raw") .
7218 " | " .
7219 $cgi->a({-href => href(action=>"blob",
7220 hash_base=>"HEAD", file_name=>$file_name)},
7221 "HEAD");
7222 } else {
7223 $formats_nav .=
7224 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
7225 "raw");
7227 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
7228 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
7229 } else {
7230 print "<div class=\"page_nav\">\n" .
7231 "<br/><br/></div>\n" .
7232 "<div class=\"title\">".esc_html($hash)."</div>\n";
7234 git_print_page_path($file_name, "blob", $hash_base);
7235 print "<div class=\"page_body\">\n";
7236 if ($mimetype =~ m!^image/!) {
7237 print qq!<img class="blob" type="!.esc_attr($mimetype).qq!"!;
7238 if ($file_name) {
7239 print qq! alt="!.esc_attr($file_name).qq!" title="!.esc_attr($file_name).qq!"!;
7241 print qq! src="! .
7242 href(action=>"blob_plain", hash=>$hash,
7243 hash_base=>$hash_base, file_name=>$file_name) .
7244 qq!" />\n!;
7245 } else {
7246 my $nr;
7247 while (my $line = <$fd>) {
7248 chomp $line;
7249 $nr++;
7250 $line = untabify($line);
7251 printf qq!<div class="pre"><a id="l%i" href="%s#l%i" class="linenr">%4i</a> %s</div>\n!,
7252 $nr, esc_attr(href(-replay => 1)), $nr, $nr,
7253 $syntax ? sanitize($line) : esc_html($line, -nbsp=>1);
7256 close $fd
7257 or print "Reading blob failed.\n";
7258 print "</div>";
7259 git_footer_html();
7262 sub git_tree {
7263 if (!defined $hash_base) {
7264 $hash_base = "HEAD";
7266 if (!defined $hash) {
7267 if (defined $file_name) {
7268 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
7269 } else {
7270 $hash = $hash_base;
7273 die_error(404, "No such tree") unless defined($hash);
7275 my $show_sizes = gitweb_check_feature('show-sizes');
7276 my $have_blame = gitweb_check_feature('blame');
7278 my @entries = ();
7280 local $/ = "\0";
7281 open my $fd, "-|", git_cmd(), "ls-tree", '-z',
7282 ($show_sizes ? '-l' : ()), @extra_options, $hash
7283 or die_error(500, "Open git-ls-tree failed");
7284 @entries = map { chomp; $_ } <$fd>;
7285 close $fd
7286 or die_error(404, "Reading tree failed");
7289 my $refs = git_get_references();
7290 my $ref = format_ref_marker($refs, $hash_base);
7291 git_header_html();
7292 my $basedir = '';
7293 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
7294 my @views_nav = ();
7295 if (defined $file_name) {
7296 push @views_nav,
7297 $cgi->a({-href => href(action=>"history", -replay=>1)},
7298 "history"),
7299 $cgi->a({-href => href(action=>"tree",
7300 hash_base=>"HEAD", file_name=>$file_name)},
7301 "HEAD"),
7303 my $snapshot_links = format_snapshot_links($hash);
7304 if (defined $snapshot_links) {
7305 # FIXME: Should be available when we have no hash base as well.
7306 push @views_nav, $snapshot_links;
7308 git_print_page_nav('tree','', $hash_base, undef, undef,
7309 join(' | ', @views_nav));
7310 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
7311 } else {
7312 undef $hash_base;
7313 print "<div class=\"page_nav\">\n";
7314 print "<br/><br/></div>\n";
7315 print "<div class=\"title\">".esc_html($hash)."</div>\n";
7317 if (defined $file_name) {
7318 $basedir = $file_name;
7319 if ($basedir ne '' && substr($basedir, -1) ne '/') {
7320 $basedir .= '/';
7322 git_print_page_path($file_name, 'tree', $hash_base);
7324 print "<div class=\"page_body\">\n";
7325 print "<table class=\"tree\">\n";
7326 my $alternate = 1;
7327 # '..' (top directory) link if possible
7328 if (defined $hash_base &&
7329 defined $file_name && $file_name =~ m![^/]+$!) {
7330 if ($alternate) {
7331 print "<tr class=\"dark\">\n";
7332 } else {
7333 print "<tr class=\"light\">\n";
7335 $alternate ^= 1;
7337 my $up = $file_name;
7338 $up =~ s!/?[^/]+$!!;
7339 undef $up unless $up;
7340 # based on git_print_tree_entry
7341 print '<td class="mode">' . mode_str('040000') . "</td>\n";
7342 print '<td class="size">&nbsp;</td>'."\n" if $show_sizes;
7343 print '<td class="list">';
7344 print $cgi->a({-href => href(action=>"tree",
7345 hash_base=>$hash_base,
7346 file_name=>$up)},
7347 "..");
7348 print "</td>\n";
7349 print "<td class=\"link\"></td>\n";
7351 print "</tr>\n";
7353 foreach my $line (@entries) {
7354 my %t = parse_ls_tree_line($line, -z => 1, -l => $show_sizes);
7356 if ($alternate) {
7357 print "<tr class=\"dark\">\n";
7358 } else {
7359 print "<tr class=\"light\">\n";
7361 $alternate ^= 1;
7363 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
7365 print "</tr>\n";
7367 print "</table>\n" .
7368 "</div>";
7369 git_footer_html();
7372 sub sanitize_for_filename {
7373 my $name = shift;
7375 $name =~ s!/!-!g;
7376 $name =~ s/[^[:alnum:]_.-]//g;
7378 return $name;
7381 sub snapshot_name {
7382 my ($project, $hash) = @_;
7384 # path/to/project.git -> project
7385 # path/to/project/.git -> project
7386 my $name = to_utf8($project);
7387 $name =~ s,([^/])/*\.git$,$1,;
7388 $name = sanitize_for_filename(basename($name));
7390 my $ver = $hash;
7391 if ($hash =~ /^[0-9a-fA-F]+$/) {
7392 # shorten SHA-1 hash
7393 my $full_hash = git_get_full_hash($project, $hash);
7394 if ($full_hash =~ /^$hash/ && length($hash) > 7) {
7395 $ver = git_get_short_hash($project, $hash);
7397 } elsif ($hash =~ m!^refs/tags/(.*)$!) {
7398 # tags don't need shortened SHA-1 hash
7399 $ver = $1;
7400 } else {
7401 # branches and other need shortened SHA-1 hash
7402 my $strip_refs = join '|', map { quotemeta } get_branch_refs();
7403 if ($hash =~ m!^refs/($strip_refs|remotes)/(.*)$!) {
7404 my $ref_dir = (defined $1) ? $1 : '';
7405 $ver = $2;
7407 $ref_dir = sanitize_for_filename($ref_dir);
7408 # for refs neither in heads nor remotes we want to
7409 # add a ref dir to archive name
7410 if ($ref_dir ne '' and $ref_dir ne 'heads' and $ref_dir ne 'remotes') {
7411 $ver = $ref_dir . '-' . $ver;
7414 $ver .= '-' . git_get_short_hash($project, $hash);
7416 # special case of sanitization for filename - we change
7417 # slashes to dots instead of dashes
7418 # in case of hierarchical branch names
7419 $ver =~ s!/!.!g;
7420 $ver =~ s/[^[:alnum:]_.-]//g;
7422 # name = project-version_string
7423 $name = "$name-$ver";
7425 return wantarray ? ($name, $name) : $name;
7428 sub exit_if_unmodified_since {
7429 my ($latest_epoch) = @_;
7430 our $cgi;
7432 my $if_modified = $cgi->http('IF_MODIFIED_SINCE');
7433 if (defined $if_modified) {
7434 my $since;
7435 if (eval { require HTTP::Date; 1; }) {
7436 $since = HTTP::Date::str2time($if_modified);
7437 } elsif (eval { require Time::ParseDate; 1; }) {
7438 $since = Time::ParseDate::parsedate($if_modified, GMT => 1);
7440 if (defined $since && $latest_epoch <= $since) {
7441 my %latest_date = parse_date($latest_epoch);
7442 print $cgi->header(
7443 -last_modified => $latest_date{'rfc2822'},
7444 -status => '304 Not Modified');
7445 CORE::die;
7450 sub git_snapshot {
7451 my $format = $input_params{'snapshot_format'};
7452 if (!@snapshot_fmts) {
7453 die_error(403, "Snapshots not allowed");
7455 # default to first supported snapshot format
7456 $format ||= $snapshot_fmts[0];
7457 if ($format !~ m/^[a-z0-9]+$/) {
7458 die_error(400, "Invalid snapshot format parameter");
7459 } elsif (!exists($known_snapshot_formats{$format})) {
7460 die_error(400, "Unknown snapshot format");
7461 } elsif ($known_snapshot_formats{$format}{'disabled'}) {
7462 die_error(403, "Snapshot format not allowed");
7463 } elsif (!grep($_ eq $format, @snapshot_fmts)) {
7464 die_error(403, "Unsupported snapshot format");
7467 my $type = git_get_type("$hash^{}");
7468 if (!$type) {
7469 die_error(404, 'Object does not exist');
7470 } elsif ($type eq 'blob') {
7471 die_error(400, 'Object is not a tree-ish');
7474 my ($name, $prefix) = snapshot_name($project, $hash);
7475 my $filename = "$name$known_snapshot_formats{$format}{'suffix'}";
7477 my %co = parse_commit($hash);
7478 exit_if_unmodified_since($co{'committer_epoch'}) if %co;
7480 my $cmd = quote_command(
7481 git_cmd(), 'archive',
7482 "--format=$known_snapshot_formats{$format}{'format'}",
7483 "--prefix=$prefix/", $hash);
7484 if (exists $known_snapshot_formats{$format}{'compressor'}) {
7485 $cmd .= ' | ' . quote_command(@{$known_snapshot_formats{$format}{'compressor'}});
7488 $filename =~ s/(["\\])/\\$1/g;
7489 my %latest_date;
7490 if (%co) {
7491 %latest_date = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
7494 print $cgi->header(
7495 -type => $known_snapshot_formats{$format}{'type'},
7496 -content_disposition => 'inline; filename="' . $filename . '"',
7497 %co ? (-last_modified => $latest_date{'rfc2822'}) : (),
7498 -status => '200 OK');
7500 open my $fd, "-|", $cmd
7501 or die_error(500, "Execute git-archive failed");
7502 binmode STDOUT, ':raw';
7503 print <$fd>;
7504 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
7505 close $fd;
7508 sub git_log_generic {
7509 my ($fmt_name, $body_subr, $base, $parent, $file_name, $file_hash) = @_;
7511 my $head = git_get_head_hash($project);
7512 if (!defined $base) {
7513 $base = $head;
7515 if (!defined $page) {
7516 $page = 0;
7518 my $refs = git_get_references();
7520 my $commit_hash = $base;
7521 if (defined $parent) {
7522 $commit_hash = "$parent..$base";
7524 my @commitlist =
7525 parse_commits($commit_hash, 101, (100 * $page),
7526 defined $file_name ? ($file_name, "--full-history") : ());
7528 my $ftype;
7529 if (!defined $file_hash && defined $file_name) {
7530 # some commits could have deleted file in question,
7531 # and not have it in tree, but one of them has to have it
7532 for (my $i = 0; $i < @commitlist; $i++) {
7533 $file_hash = git_get_hash_by_path($commitlist[$i]{'id'}, $file_name);
7534 last if defined $file_hash;
7537 if (defined $file_hash) {
7538 $ftype = git_get_type($file_hash);
7540 if (defined $file_name && !defined $ftype) {
7541 die_error(500, "Unknown type of object");
7543 my %co;
7544 if (defined $file_name) {
7545 %co = parse_commit($base)
7546 or die_error(404, "Unknown commit object");
7550 my $paging_nav = format_paging_nav($fmt_name, $page, $#commitlist >= 100);
7551 my $next_link = '';
7552 if ($#commitlist >= 100) {
7553 $next_link =
7554 $cgi->a({-href => href(-replay=>1, page=>$page+1),
7555 -accesskey => "n", -title => "Alt-n"}, "next");
7557 my $patch_max = gitweb_get_feature('patches');
7558 if ($patch_max && !defined $file_name) {
7559 if ($patch_max < 0 || @commitlist <= $patch_max) {
7560 $paging_nav .= " &sdot; " .
7561 $cgi->a({-href => href(action=>"patches", -replay=>1)},
7562 "patches");
7566 git_header_html();
7567 git_print_page_nav($fmt_name,'', $hash,$hash,$hash, $paging_nav);
7568 if (defined $file_name) {
7569 git_print_header_div('commit', esc_html($co{'title'}), $base);
7570 } else {
7571 git_print_header_div('summary', $project)
7573 git_print_page_path($file_name, $ftype, $hash_base)
7574 if (defined $file_name);
7576 $body_subr->(\@commitlist, 0, 99, $refs, $next_link,
7577 $file_name, $file_hash, $ftype);
7579 git_footer_html();
7582 sub git_log {
7583 git_log_generic('log', \&git_log_body,
7584 $hash, $hash_parent);
7587 sub git_commit {
7588 $hash ||= $hash_base || "HEAD";
7589 my %co = parse_commit($hash)
7590 or die_error(404, "Unknown commit object");
7592 my $parent = $co{'parent'};
7593 my $parents = $co{'parents'}; # listref
7595 # we need to prepare $formats_nav before any parameter munging
7596 my $formats_nav;
7597 if (!defined $parent) {
7598 # --root commitdiff
7599 $formats_nav .= '(initial)';
7600 } elsif (@$parents == 1) {
7601 # single parent commit
7602 $formats_nav .=
7603 '(parent: ' .
7604 $cgi->a({-href => href(action=>"commit",
7605 hash=>$parent)},
7606 esc_html(substr($parent, 0, 7))) .
7607 ')';
7608 } else {
7609 # merge commit
7610 $formats_nav .=
7611 '(merge: ' .
7612 join(' ', map {
7613 $cgi->a({-href => href(action=>"commit",
7614 hash=>$_)},
7615 esc_html(substr($_, 0, 7)));
7616 } @$parents ) .
7617 ')';
7619 if (gitweb_check_feature('patches') && @$parents <= 1) {
7620 $formats_nav .= " | " .
7621 $cgi->a({-href => href(action=>"patch", -replay=>1)},
7622 "patch");
7625 if (!defined $parent) {
7626 $parent = "--root";
7628 my @difftree;
7629 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
7630 @diff_opts,
7631 (@$parents <= 1 ? $parent : '-c'),
7632 $hash, "--"
7633 or die_error(500, "Open git-diff-tree failed");
7634 @difftree = map { chomp; $_ } <$fd>;
7635 close $fd or die_error(404, "Reading git-diff-tree failed");
7637 # non-textual hash id's can be cached
7638 my $expires;
7639 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
7640 $expires = "+1d";
7642 my $refs = git_get_references();
7643 my $ref = format_ref_marker($refs, $co{'id'});
7645 git_header_html(undef, $expires);
7646 git_print_page_nav('commit', '',
7647 $hash, $co{'tree'}, $hash,
7648 $formats_nav);
7650 if (defined $co{'parent'}) {
7651 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
7652 } else {
7653 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
7655 print "<div class=\"title_text\">\n" .
7656 "<table class=\"object_header\">\n";
7657 git_print_authorship_rows(\%co);
7658 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
7659 print "<tr>" .
7660 "<td>tree</td>" .
7661 "<td class=\"sha1\">" .
7662 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
7663 class => "list"}, $co{'tree'}) .
7664 "</td>" .
7665 "<td class=\"link\">" .
7666 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
7667 "tree");
7668 my $snapshot_links = format_snapshot_links($hash);
7669 if (defined $snapshot_links) {
7670 print " | " . $snapshot_links;
7672 print "</td>" .
7673 "</tr>\n";
7675 foreach my $par (@$parents) {
7676 print "<tr>" .
7677 "<td>parent</td>" .
7678 "<td class=\"sha1\">" .
7679 $cgi->a({-href => href(action=>"commit", hash=>$par),
7680 class => "list"}, $par) .
7681 "</td>" .
7682 "<td class=\"link\">" .
7683 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
7684 " | " .
7685 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
7686 "</td>" .
7687 "</tr>\n";
7689 print "</table>".
7690 "</div>\n";
7692 print "<div class=\"page_body\">\n";
7693 git_print_log($co{'comment'});
7694 print "</div>\n";
7696 git_difftree_body(\@difftree, $hash, @$parents);
7698 git_footer_html();
7701 sub git_object {
7702 # object is defined by:
7703 # - hash or hash_base alone
7704 # - hash_base and file_name
7705 my $type;
7707 # - hash or hash_base alone
7708 if ($hash || ($hash_base && !defined $file_name)) {
7709 my $object_id = $hash || $hash_base;
7711 open my $fd, "-|", quote_command(
7712 git_cmd(), 'cat-file', '-t', $object_id) . ' 2> /dev/null'
7713 or die_error(404, "Object does not exist");
7714 $type = <$fd>;
7715 chomp $type;
7716 close $fd
7717 or die_error(404, "Object does not exist");
7719 # - hash_base and file_name
7720 } elsif ($hash_base && defined $file_name) {
7721 $file_name =~ s,/+$,,;
7723 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
7724 or die_error(404, "Base object does not exist");
7726 # here errors should not happen
7727 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
7728 or die_error(500, "Open git-ls-tree failed");
7729 my $line = <$fd>;
7730 close $fd;
7732 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
7733 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
7734 die_error(404, "File or directory for given base does not exist");
7736 $type = $2;
7737 $hash = $3;
7738 } else {
7739 die_error(400, "Not enough information to find object");
7742 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
7743 hash=>$hash, hash_base=>$hash_base,
7744 file_name=>$file_name),
7745 -status => '302 Found');
7748 sub git_blobdiff {
7749 my $format = shift || 'html';
7750 my $diff_style = $input_params{'diff_style'} || 'inline';
7752 my $fd;
7753 my @difftree;
7754 my %diffinfo;
7755 my $expires;
7757 # preparing $fd and %diffinfo for git_patchset_body
7758 # new style URI
7759 if (defined $hash_base && defined $hash_parent_base) {
7760 if (defined $file_name) {
7761 # read raw output
7762 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
7763 $hash_parent_base, $hash_base,
7764 "--", (defined $file_parent ? $file_parent : ()), $file_name
7765 or die_error(500, "Open git-diff-tree failed");
7766 @difftree = map { chomp; $_ } <$fd>;
7767 close $fd
7768 or die_error(404, "Reading git-diff-tree failed");
7769 @difftree
7770 or die_error(404, "Blob diff not found");
7772 } elsif (defined $hash &&
7773 $hash =~ /[0-9a-fA-F]{40}/) {
7774 # try to find filename from $hash
7776 # read filtered raw output
7777 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
7778 $hash_parent_base, $hash_base, "--"
7779 or die_error(500, "Open git-diff-tree failed");
7780 @difftree =
7781 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
7782 # $hash == to_id
7783 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
7784 map { chomp; $_ } <$fd>;
7785 close $fd
7786 or die_error(404, "Reading git-diff-tree failed");
7787 @difftree
7788 or die_error(404, "Blob diff not found");
7790 } else {
7791 die_error(400, "Missing one of the blob diff parameters");
7794 if (@difftree > 1) {
7795 die_error(400, "Ambiguous blob diff specification");
7798 %diffinfo = parse_difftree_raw_line($difftree[0]);
7799 $file_parent ||= $diffinfo{'from_file'} || $file_name;
7800 $file_name ||= $diffinfo{'to_file'};
7802 $hash_parent ||= $diffinfo{'from_id'};
7803 $hash ||= $diffinfo{'to_id'};
7805 # non-textual hash id's can be cached
7806 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
7807 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
7808 $expires = '+1d';
7811 # open patch output
7812 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
7813 '-p', ($format eq 'html' ? "--full-index" : ()),
7814 $hash_parent_base, $hash_base,
7815 "--", (defined $file_parent ? $file_parent : ()), $file_name
7816 or die_error(500, "Open git-diff-tree failed");
7819 # old/legacy style URI -- not generated anymore since 1.4.3.
7820 if (!%diffinfo) {
7821 die_error('404 Not Found', "Missing one of the blob diff parameters")
7824 # header
7825 if ($format eq 'html') {
7826 my $formats_nav =
7827 $cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},
7828 "raw");
7829 $formats_nav .= diff_style_nav($diff_style);
7830 git_header_html(undef, $expires);
7831 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
7832 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
7833 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
7834 } else {
7835 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
7836 print "<div class=\"title\">".esc_html("$hash vs $hash_parent")."</div>\n";
7838 if (defined $file_name) {
7839 git_print_page_path($file_name, "blob", $hash_base);
7840 } else {
7841 print "<div class=\"page_path\"></div>\n";
7844 } elsif ($format eq 'plain') {
7845 print $cgi->header(
7846 -type => 'text/plain',
7847 -charset => 'utf-8',
7848 -expires => $expires,
7849 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
7851 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
7853 } else {
7854 die_error(400, "Unknown blobdiff format");
7857 # patch
7858 if ($format eq 'html') {
7859 print "<div class=\"page_body\">\n";
7861 git_patchset_body($fd, $diff_style,
7862 [ \%diffinfo ], $hash_base, $hash_parent_base);
7863 close $fd;
7865 print "</div>\n"; # class="page_body"
7866 git_footer_html();
7868 } else {
7869 while (my $line = <$fd>) {
7870 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
7871 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
7873 print $line;
7875 last if $line =~ m!^\+\+\+!;
7877 local $/ = undef;
7878 print <$fd>;
7879 close $fd;
7883 sub git_blobdiff_plain {
7884 git_blobdiff('plain');
7887 # assumes that it is added as later part of already existing navigation,
7888 # so it returns "| foo | bar" rather than just "foo | bar"
7889 sub diff_style_nav {
7890 my ($diff_style, $is_combined) = @_;
7891 $diff_style ||= 'inline';
7893 return "" if ($is_combined);
7895 my @styles = (inline => 'inline', 'sidebyside' => 'side by side');
7896 my %styles = @styles;
7897 @styles =
7898 @styles[ map { $_ * 2 } 0..$#styles/2 ];
7900 return join '',
7901 map { " | ".$_ }
7902 map {
7903 $_ eq $diff_style ? $styles{$_} :
7904 $cgi->a({-href => href(-replay=>1, diff_style => $_)}, $styles{$_})
7905 } @styles;
7908 sub git_commitdiff {
7909 my %params = @_;
7910 my $format = $params{-format} || 'html';
7911 my $diff_style = $input_params{'diff_style'} || 'inline';
7913 my ($patch_max) = gitweb_get_feature('patches');
7914 if ($format eq 'patch') {
7915 die_error(403, "Patch view not allowed") unless $patch_max;
7918 $hash ||= $hash_base || "HEAD";
7919 my %co = parse_commit($hash)
7920 or die_error(404, "Unknown commit object");
7922 # choose format for commitdiff for merge
7923 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
7924 $hash_parent = '--cc';
7926 # we need to prepare $formats_nav before almost any parameter munging
7927 my $formats_nav;
7928 if ($format eq 'html') {
7929 $formats_nav =
7930 $cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
7931 "raw");
7932 if ($patch_max && @{$co{'parents'}} <= 1) {
7933 $formats_nav .= " | " .
7934 $cgi->a({-href => href(action=>"patch", -replay=>1)},
7935 "patch");
7937 $formats_nav .= diff_style_nav($diff_style, @{$co{'parents'}} > 1);
7939 if (defined $hash_parent &&
7940 $hash_parent ne '-c' && $hash_parent ne '--cc') {
7941 # commitdiff with two commits given
7942 my $hash_parent_short = $hash_parent;
7943 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
7944 $hash_parent_short = substr($hash_parent, 0, 7);
7946 $formats_nav .=
7947 ' (from';
7948 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
7949 if ($co{'parents'}[$i] eq $hash_parent) {
7950 $formats_nav .= ' parent ' . ($i+1);
7951 last;
7954 $formats_nav .= ': ' .
7955 $cgi->a({-href => href(-replay=>1,
7956 hash=>$hash_parent, hash_base=>undef)},
7957 esc_html($hash_parent_short)) .
7958 ')';
7959 } elsif (!$co{'parent'}) {
7960 # --root commitdiff
7961 $formats_nav .= ' (initial)';
7962 } elsif (scalar @{$co{'parents'}} == 1) {
7963 # single parent commit
7964 $formats_nav .=
7965 ' (parent: ' .
7966 $cgi->a({-href => href(-replay=>1,
7967 hash=>$co{'parent'}, hash_base=>undef)},
7968 esc_html(substr($co{'parent'}, 0, 7))) .
7969 ')';
7970 } else {
7971 # merge commit
7972 if ($hash_parent eq '--cc') {
7973 $formats_nav .= ' | ' .
7974 $cgi->a({-href => href(-replay=>1,
7975 hash=>$hash, hash_parent=>'-c')},
7976 'combined');
7977 } else { # $hash_parent eq '-c'
7978 $formats_nav .= ' | ' .
7979 $cgi->a({-href => href(-replay=>1,
7980 hash=>$hash, hash_parent=>'--cc')},
7981 'compact');
7983 $formats_nav .=
7984 ' (merge: ' .
7985 join(' ', map {
7986 $cgi->a({-href => href(-replay=>1,
7987 hash=>$_, hash_base=>undef)},
7988 esc_html(substr($_, 0, 7)));
7989 } @{$co{'parents'}} ) .
7990 ')';
7994 my $hash_parent_param = $hash_parent;
7995 if (!defined $hash_parent_param) {
7996 # --cc for multiple parents, --root for parentless
7997 $hash_parent_param =
7998 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
8001 # read commitdiff
8002 my $fd;
8003 my @difftree;
8004 if ($format eq 'html') {
8005 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
8006 "--no-commit-id", "--patch-with-raw", "--full-index",
8007 $hash_parent_param, $hash, "--"
8008 or die_error(500, "Open git-diff-tree failed");
8010 while (my $line = <$fd>) {
8011 chomp $line;
8012 # empty line ends raw part of diff-tree output
8013 last unless $line;
8014 push @difftree, scalar parse_difftree_raw_line($line);
8017 } elsif ($format eq 'plain') {
8018 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
8019 '-p', $hash_parent_param, $hash, "--"
8020 or die_error(500, "Open git-diff-tree failed");
8021 } elsif ($format eq 'patch') {
8022 # For commit ranges, we limit the output to the number of
8023 # patches specified in the 'patches' feature.
8024 # For single commits, we limit the output to a single patch,
8025 # diverging from the git-format-patch default.
8026 my @commit_spec = ();
8027 if ($hash_parent) {
8028 if ($patch_max > 0) {
8029 push @commit_spec, "-$patch_max";
8031 push @commit_spec, '-n', "$hash_parent..$hash";
8032 } else {
8033 if ($params{-single}) {
8034 push @commit_spec, '-1';
8035 } else {
8036 if ($patch_max > 0) {
8037 push @commit_spec, "-$patch_max";
8039 push @commit_spec, "-n";
8041 push @commit_spec, '--root', $hash;
8043 open $fd, "-|", git_cmd(), "format-patch", @diff_opts,
8044 '--encoding=utf8', '--stdout', @commit_spec
8045 or die_error(500, "Open git-format-patch failed");
8046 } else {
8047 die_error(400, "Unknown commitdiff format");
8050 # non-textual hash id's can be cached
8051 my $expires;
8052 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
8053 $expires = "+1d";
8056 # write commit message
8057 if ($format eq 'html') {
8058 my $refs = git_get_references();
8059 my $ref = format_ref_marker($refs, $co{'id'});
8061 git_header_html(undef, $expires);
8062 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
8063 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
8064 print "<div class=\"title_text\">\n" .
8065 "<table class=\"object_header\">\n";
8066 git_print_authorship_rows(\%co);
8067 print "</table>".
8068 "</div>\n";
8069 print "<div class=\"page_body\">\n";
8070 if (@{$co{'comment'}} > 1) {
8071 print "<div class=\"log\">\n";
8072 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
8073 print "</div>\n"; # class="log"
8076 } elsif ($format eq 'plain') {
8077 my $refs = git_get_references("tags");
8078 my $tagname = git_get_rev_name_tags($hash);
8079 my $filename = basename($project) . "-$hash.patch";
8081 print $cgi->header(
8082 -type => 'text/plain',
8083 -charset => 'utf-8',
8084 -expires => $expires,
8085 -content_disposition => 'inline; filename="' . "$filename" . '"');
8086 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
8087 print "From: " . to_utf8($co{'author'}) . "\n";
8088 print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
8089 print "Subject: " . to_utf8($co{'title'}) . "\n";
8091 print "X-Git-Tag: $tagname\n" if $tagname;
8092 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
8094 foreach my $line (@{$co{'comment'}}) {
8095 print to_utf8($line) . "\n";
8097 print "---\n\n";
8098 } elsif ($format eq 'patch') {
8099 my $filename = basename($project) . "-$hash.patch";
8101 print $cgi->header(
8102 -type => 'text/plain',
8103 -charset => 'utf-8',
8104 -expires => $expires,
8105 -content_disposition => 'inline; filename="' . "$filename" . '"');
8108 # write patch
8109 if ($format eq 'html') {
8110 my $use_parents = !defined $hash_parent ||
8111 $hash_parent eq '-c' || $hash_parent eq '--cc';
8112 git_difftree_body(\@difftree, $hash,
8113 $use_parents ? @{$co{'parents'}} : $hash_parent);
8114 print "<br/>\n";
8116 git_patchset_body($fd, $diff_style,
8117 \@difftree, $hash,
8118 $use_parents ? @{$co{'parents'}} : $hash_parent);
8119 close $fd;
8120 print "</div>\n"; # class="page_body"
8121 git_footer_html();
8123 } elsif ($format eq 'plain') {
8124 local $/ = undef;
8125 print <$fd>;
8126 close $fd
8127 or print "Reading git-diff-tree failed\n";
8128 } elsif ($format eq 'patch') {
8129 local $/ = undef;
8130 print <$fd>;
8131 close $fd
8132 or print "Reading git-format-patch failed\n";
8136 sub git_commitdiff_plain {
8137 git_commitdiff(-format => 'plain');
8140 # format-patch-style patches
8141 sub git_patch {
8142 git_commitdiff(-format => 'patch', -single => 1);
8145 sub git_patches {
8146 git_commitdiff(-format => 'patch');
8149 sub git_history {
8150 git_log_generic('history', \&git_history_body,
8151 $hash_base, $hash_parent_base,
8152 $file_name, $hash);
8155 sub git_search {
8156 $searchtype ||= 'commit';
8158 # check if appropriate features are enabled
8159 gitweb_check_feature('search')
8160 or die_error(403, "Search is disabled");
8161 if ($searchtype eq 'pickaxe') {
8162 # pickaxe may take all resources of your box and run for several minutes
8163 # with every query - so decide by yourself how public you make this feature
8164 gitweb_check_feature('pickaxe')
8165 or die_error(403, "Pickaxe search is disabled");
8167 if ($searchtype eq 'grep') {
8168 # grep search might be potentially CPU-intensive, too
8169 gitweb_check_feature('grep')
8170 or die_error(403, "Grep search is disabled");
8173 if (!defined $searchtext) {
8174 die_error(400, "Text field is empty");
8176 if (!defined $hash) {
8177 $hash = git_get_head_hash($project);
8179 my %co = parse_commit($hash);
8180 if (!%co) {
8181 die_error(404, "Unknown commit object");
8183 if (!defined $page) {
8184 $page = 0;
8187 if ($searchtype eq 'commit' ||
8188 $searchtype eq 'author' ||
8189 $searchtype eq 'committer') {
8190 git_search_message(%co);
8191 } elsif ($searchtype eq 'pickaxe') {
8192 git_search_changes(%co);
8193 } elsif ($searchtype eq 'grep') {
8194 git_search_files(%co);
8195 } else {
8196 die_error(400, "Unknown search type");
8200 sub git_search_help {
8201 git_header_html();
8202 git_print_page_nav('','', $hash,$hash,$hash);
8203 print <<EOT;
8204 <p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without
8205 regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,
8206 the pattern entered is recognized as the POSIX extended
8207 <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case
8208 insensitive).</p>
8209 <dl>
8210 <dt><b>commit</b></dt>
8211 <dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
8213 my $have_grep = gitweb_check_feature('grep');
8214 if ($have_grep) {
8215 print <<EOT;
8216 <dt><b>grep</b></dt>
8217 <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
8218 a different one) are searched for the given pattern. On large trees, this search can take
8219 a while and put some strain on the server, so please use it with some consideration. Note that
8220 due to git-grep peculiarity, currently if regexp mode is turned off, the matches are
8221 case-sensitive.</dd>
8224 print <<EOT;
8225 <dt><b>author</b></dt>
8226 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>
8227 <dt><b>committer</b></dt>
8228 <dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>
8230 my $have_pickaxe = gitweb_check_feature('pickaxe');
8231 if ($have_pickaxe) {
8232 print <<EOT;
8233 <dt><b>pickaxe</b></dt>
8234 <dd>All commits that caused the string to appear or disappear from any file (changes that
8235 added, removed or "modified" the string) will be listed. This search can take a while and
8236 takes a lot of strain on the server, so please use it wisely. Note that since you may be
8237 interested even in changes just changing the case as well, this search is case sensitive.</dd>
8240 print "</dl>\n";
8241 git_footer_html();
8244 sub git_shortlog {
8245 git_log_generic('shortlog', \&git_shortlog_body,
8246 $hash, $hash_parent);
8249 ## ......................................................................
8250 ## feeds (RSS, Atom; OPML)
8252 sub git_feed {
8253 my $format = shift || 'atom';
8254 my $have_blame = gitweb_check_feature('blame');
8256 # Atom: http://www.atomenabled.org/developers/syndication/
8257 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
8258 if ($format ne 'rss' && $format ne 'atom') {
8259 die_error(400, "Unknown web feed format");
8262 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
8263 my $head = $hash || 'HEAD';
8264 my @commitlist = parse_commits($head, 150, 0, $file_name);
8266 my %latest_commit;
8267 my %latest_date;
8268 my $content_type = "application/$format+xml";
8269 if (defined $cgi->http('HTTP_ACCEPT') &&
8270 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
8271 # browser (feed reader) prefers text/xml
8272 $content_type = 'text/xml';
8274 if (defined($commitlist[0])) {
8275 %latest_commit = %{$commitlist[0]};
8276 my $latest_epoch = $latest_commit{'committer_epoch'};
8277 exit_if_unmodified_since($latest_epoch);
8278 %latest_date = parse_date($latest_epoch, $latest_commit{'committer_tz'});
8280 print $cgi->header(
8281 -type => $content_type,
8282 -charset => 'utf-8',
8283 %latest_date ? (-last_modified => $latest_date{'rfc2822'}) : (),
8284 -status => '200 OK');
8286 # Optimization: skip generating the body if client asks only
8287 # for Last-Modified date.
8288 return if ($cgi->request_method() eq 'HEAD');
8290 # header variables
8291 my $title = "$site_name - $project/$action";
8292 my $feed_type = 'log';
8293 if (defined $hash) {
8294 $title .= " - '$hash'";
8295 $feed_type = 'branch log';
8296 if (defined $file_name) {
8297 $title .= " :: $file_name";
8298 $feed_type = 'history';
8300 } elsif (defined $file_name) {
8301 $title .= " - $file_name";
8302 $feed_type = 'history';
8304 $title .= " $feed_type";
8305 $title = esc_html($title);
8306 my $descr = git_get_project_description($project);
8307 if (defined $descr) {
8308 $descr = esc_html($descr);
8309 } else {
8310 $descr = "$project " .
8311 ($format eq 'rss' ? 'RSS' : 'Atom') .
8312 " feed";
8314 my $owner = git_get_project_owner($project);
8315 $owner = esc_html($owner);
8317 #header
8318 my $alt_url;
8319 if (defined $file_name) {
8320 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
8321 } elsif (defined $hash) {
8322 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
8323 } else {
8324 $alt_url = href(-full=>1, action=>"summary");
8326 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
8327 if ($format eq 'rss') {
8328 print <<XML;
8329 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
8330 <channel>
8332 print "<title>$title</title>\n" .
8333 "<link>$alt_url</link>\n" .
8334 "<description>$descr</description>\n" .
8335 "<language>en</language>\n" .
8336 # project owner is responsible for 'editorial' content
8337 "<managingEditor>$owner</managingEditor>\n";
8338 if (defined $logo || defined $favicon) {
8339 # prefer the logo to the favicon, since RSS
8340 # doesn't allow both
8341 my $img = esc_url($logo || $favicon);
8342 print "<image>\n" .
8343 "<url>$img</url>\n" .
8344 "<title>$title</title>\n" .
8345 "<link>$alt_url</link>\n" .
8346 "</image>\n";
8348 if (%latest_date) {
8349 print "<pubDate>$latest_date{'rfc2822'}</pubDate>\n";
8350 print "<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";
8352 print "<generator>gitweb v.$version/$git_version</generator>\n";
8353 } elsif ($format eq 'atom') {
8354 print <<XML;
8355 <feed xmlns="http://www.w3.org/2005/Atom">
8357 print "<title>$title</title>\n" .
8358 "<subtitle>$descr</subtitle>\n" .
8359 '<link rel="alternate" type="text/html" href="' .
8360 $alt_url . '" />' . "\n" .
8361 '<link rel="self" type="' . $content_type . '" href="' .
8362 $cgi->self_url() . '" />' . "\n" .
8363 "<id>" . href(-full=>1) . "</id>\n" .
8364 # use project owner for feed author
8365 "<author><name>$owner</name></author>\n";
8366 if (defined $favicon) {
8367 print "<icon>" . esc_url($favicon) . "</icon>\n";
8369 if (defined $logo) {
8370 # not twice as wide as tall: 72 x 27 pixels
8371 print "<logo>" . esc_url($logo) . "</logo>\n";
8373 if (! %latest_date) {
8374 # dummy date to keep the feed valid until commits trickle in:
8375 print "<updated>1970-01-01T00:00:00Z</updated>\n";
8376 } else {
8377 print "<updated>$latest_date{'iso-8601'}</updated>\n";
8379 print "<generator version='$version/$git_version'>gitweb</generator>\n";
8382 # contents
8383 for (my $i = 0; $i <= $#commitlist; $i++) {
8384 my %co = %{$commitlist[$i]};
8385 my $commit = $co{'id'};
8386 # we read 150, we always show 30 and the ones more recent than 48 hours
8387 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
8388 last;
8390 my %cd = parse_date($co{'author_epoch'}, $co{'author_tz'});
8392 # get list of changed files
8393 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
8394 $co{'parent'} || "--root",
8395 $co{'id'}, "--", (defined $file_name ? $file_name : ())
8396 or next;
8397 my @difftree = map { chomp; $_ } <$fd>;
8398 close $fd
8399 or next;
8401 # print element (entry, item)
8402 my $co_url = href(-full=>1, action=>"commitdiff", hash=>$commit);
8403 if ($format eq 'rss') {
8404 print "<item>\n" .
8405 "<title>" . esc_html($co{'title'}) . "</title>\n" .
8406 "<author>" . esc_html($co{'author'}) . "</author>\n" .
8407 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
8408 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
8409 "<link>$co_url</link>\n" .
8410 "<description>" . esc_html($co{'title'}) . "</description>\n" .
8411 "<content:encoded>" .
8412 "<![CDATA[\n";
8413 } elsif ($format eq 'atom') {
8414 print "<entry>\n" .
8415 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
8416 "<updated>$cd{'iso-8601'}</updated>\n" .
8417 "<author>\n" .
8418 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
8419 if ($co{'author_email'}) {
8420 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
8422 print "</author>\n" .
8423 # use committer for contributor
8424 "<contributor>\n" .
8425 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
8426 if ($co{'committer_email'}) {
8427 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
8429 print "</contributor>\n" .
8430 "<published>$cd{'iso-8601'}</published>\n" .
8431 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
8432 "<id>$co_url</id>\n" .
8433 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
8434 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
8436 my $comment = $co{'comment'};
8437 print "<pre>\n";
8438 foreach my $line (@$comment) {
8439 $line = esc_html($line);
8440 print "$line\n";
8442 print "</pre><ul>\n";
8443 foreach my $difftree_line (@difftree) {
8444 my %difftree = parse_difftree_raw_line($difftree_line);
8445 next if !$difftree{'from_id'};
8447 my $file = $difftree{'file'} || $difftree{'to_file'};
8449 print "<li>" .
8450 "[" .
8451 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
8452 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
8453 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
8454 file_name=>$file, file_parent=>$difftree{'from_file'}),
8455 -title => "diff"}, 'D');
8456 if ($have_blame) {
8457 print $cgi->a({-href => href(-full=>1, action=>"blame",
8458 file_name=>$file, hash_base=>$commit),
8459 -title => "blame"}, 'B');
8461 # if this is not a feed of a file history
8462 if (!defined $file_name || $file_name ne $file) {
8463 print $cgi->a({-href => href(-full=>1, action=>"history",
8464 file_name=>$file, hash=>$commit),
8465 -title => "history"}, 'H');
8467 $file = esc_path($file);
8468 print "] ".
8469 "$file</li>\n";
8471 if ($format eq 'rss') {
8472 print "</ul>]]>\n" .
8473 "</content:encoded>\n" .
8474 "</item>\n";
8475 } elsif ($format eq 'atom') {
8476 print "</ul>\n</div>\n" .
8477 "</content>\n" .
8478 "</entry>\n";
8482 # end of feed
8483 if ($format eq 'rss') {
8484 print "</channel>\n</rss>\n";
8485 } elsif ($format eq 'atom') {
8486 print "</feed>\n";
8490 sub git_rss {
8491 git_feed('rss');
8494 sub git_atom {
8495 git_feed('atom');
8498 sub git_opml {
8499 my @list = git_get_projects_list($project_filter, $strict_export);
8500 if (!@list) {
8501 die_error(404, "No projects found");
8504 print $cgi->header(
8505 -type => 'text/xml',
8506 -charset => 'utf-8',
8507 -content_disposition => 'inline; filename="opml.xml"');
8509 my $title = esc_html($site_name);
8510 my $filter = " within subdirectory ";
8511 if (defined $project_filter) {
8512 $filter .= esc_html($project_filter);
8513 } else {
8514 $filter = "";
8516 print <<XML;
8517 <?xml version="1.0" encoding="utf-8"?>
8518 <opml version="1.0">
8519 <head>
8520 <title>$title OPML Export$filter</title>
8521 </head>
8522 <body>
8523 <outline text="git RSS feeds">
8526 foreach my $pr (@list) {
8527 my %proj = %$pr;
8528 my $head = git_get_head_hash($proj{'path'});
8529 if (!defined $head) {
8530 next;
8532 $git_dir = "$projectroot/$proj{'path'}";
8533 my %co = parse_commit($head);
8534 if (!%co) {
8535 next;
8538 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
8539 my $rss = href('project' => $proj{'path'}, 'action' => 'rss', -full => 1);
8540 my $html = href('project' => $proj{'path'}, 'action' => 'summary', -full => 1);
8541 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
8543 print <<XML;
8544 </outline>
8545 </body>
8546 </opml>