3 # gitweb - simple web interface to track changes in git repositories
5 # (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org>
6 # (C) 2005, Christian Gierke
8 # This program is licensed under the GPLv2
13 use CGI qw(:standard :escapeHTML -nosticky);
14 use CGI::Util qw(unescape);
15 use CGI::Carp qw(fatalsToBrowser set_message);
19 use File::Basename qw(basename);
20 use Time::HiRes qw(gettimeofday tv_interval);
21 binmode STDOUT, ':utf8';
23 our $t0 = [ gettimeofday() ];
24 our $number_of_git_cmds = 0;
27 CGI->compile() if $ENV{'MOD_PERL'};
30 our $version = "++GIT_VERSION++";
32 our ($my_url, $my_uri, $base_url, $path_info, $home_link);
36 our $my_url = $cgi->url();
37 our $my_uri = $cgi->url(-absolute => 1);
39 # Base URL for relative URLs in gitweb ($logo, $favicon, ...),
40 # needed and used only for URLs with nonempty PATH_INFO
41 our $base_url = $my_url;
43 # When the script is used as DirectoryIndex, the URL does not contain the name
44 # of the script file itself, and $cgi->url() fails to strip PATH_INFO, so we
45 # have to do it ourselves. We make $path_info global because it's also used
48 # Another issue with the script being the DirectoryIndex is that the resulting
49 # $my_url data is not the full script URL: this is good, because we want
50 # generated links to keep implying the script name if it wasn't explicitly
51 # indicated in the URL we're handling, but it means that $my_url cannot be used
53 # Therefore, if we needed to strip PATH_INFO, then we know that we have
54 # to build the base URL ourselves:
55 our $path_info = decode_utf8($ENV{"PATH_INFO"});
57 # $path_info has already been URL-decoded by the web server, but
58 # $my_url and $my_uri have not. URL-decode them so we can properly
60 $my_url = unescape($my_url);
61 $my_uri = unescape($my_uri);
62 if ($my_url =~ s,\Q$path_info\E$,, &&
63 $my_uri =~ s,\Q$path_info\E$,, &&
64 defined $ENV{'SCRIPT_NAME'}) {
65 $base_url = $cgi->url(-base => 1) . $ENV{'SCRIPT_NAME'};
69 # target of the home link on top of all pages
70 our $home_link = $my_uri || "/";
73 # core git executable to use
74 # this can just be "git" if your webserver has a sensible PATH
75 our $GIT = "++GIT_BINDIR++/git";
77 # absolute fs-path which will be prepended to the project path
78 #our $projectroot = "/pub/scm";
79 our $projectroot = "++GITWEB_PROJECTROOT++";
81 # fs traversing limit for getting project list
82 # the number is relative to the projectroot
83 our $project_maxdepth = "++GITWEB_PROJECT_MAXDEPTH++";
85 # string of the home link on top of all pages
86 our $home_link_str = "++GITWEB_HOME_LINK_STR++";
88 # extra breadcrumbs preceding the home link
89 our @extra_breadcrumbs = ();
91 # name of your site or organization to appear in page titles
92 # replace this with something more descriptive for clearer bookmarks
93 our $site_name = "++GITWEB_SITENAME++"
94 || ($ENV{'SERVER_NAME'} || "Untitled") . " Git";
96 # html snippet to include in the <head> section of each page
97 our $site_html_head_string = "++GITWEB_SITE_HTML_HEAD_STRING++";
98 # filename of html text to include at top of each page
99 our $site_header = "++GITWEB_SITE_HEADER++";
100 # html text to include at home page
101 our $home_text = "++GITWEB_HOMETEXT++";
102 # filename of html text to include at bottom of each page
103 our $site_footer = "++GITWEB_SITE_FOOTER++";
106 our @stylesheets = ("++GITWEB_CSS++");
107 # URI of a single stylesheet, which can be overridden in GITWEB_CONFIG.
108 our $stylesheet = undef;
109 # URI of GIT logo (72x27 size)
110 our $logo = "++GITWEB_LOGO++";
111 # URI of GIT favicon, assumed to be image/png type
112 our $favicon = "++GITWEB_FAVICON++";
113 # URI of gitweb.js (JavaScript code for gitweb)
114 our $javascript = "++GITWEB_JS++";
116 # URI and label (title) of GIT logo link
117 #our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
118 #our $logo_label = "git documentation";
119 our $logo_url = "http://git-scm.com/";
120 our $logo_label = "git homepage";
122 # source of projects list
123 our $projects_list = "++GITWEB_LIST++";
125 # the width (in characters) of the projects list "Description" column
126 our $projects_list_description_width = 25;
128 # group projects by category on the projects list
129 # (enabled if this variable evaluates to true)
130 our $projects_list_group_categories = 0;
132 # default category if none specified
133 # (leave the empty string for no category)
134 our $project_list_default_category = "";
136 # default order of projects list
137 # valid values are none, project, descr, owner, and age
138 our $default_projects_order = "project";
140 # show repository only if this file exists
141 # (only effective if this variable evaluates to true)
142 our $export_ok = "++GITWEB_EXPORT_OK++";
144 # don't generate age column on the projects list page
145 our $omit_age_column = 0;
147 # don't generate information about owners of repositories
150 # show repository only if this subroutine returns true
151 # when given the path to the project, for example:
152 # sub { return -e "$_[0]/git-daemon-export-ok"; }
153 our $export_auth_hook = undef;
155 # only allow viewing of repositories also shown on the overview page
156 our $strict_export = "++GITWEB_STRICT_EXPORT++";
158 # list of git base URLs used for URL to where fetch project from,
159 # i.e. full URL is "$git_base_url/$project"
160 our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
162 # default blob_plain mimetype and default charset for text/plain blob
163 our $default_blob_plain_mimetype = 'text/plain';
164 our $default_text_plain_charset = undef;
166 # file to use for guessing MIME types before trying /etc/mime.types
167 # (relative to the current git repository)
168 our $mimetypes_file = undef;
170 # assume this charset if line contains non-UTF-8 characters;
171 # it should be valid encoding (see Encoding::Supported(3pm) for list),
172 # for which encoding all byte sequences are valid, for example
173 # 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
174 # could be even 'utf-8' for the old behavior)
175 our $fallback_encoding = 'latin1';
177 # rename detection options for git-diff and git-diff-tree
178 # - default is '-M', with the cost proportional to
179 # (number of removed files) * (number of new files).
180 # - more costly is '-C' (which implies '-M'), with the cost proportional to
181 # (number of changed files + number of removed files) * (number of new files)
182 # - even more costly is '-C', '--find-copies-harder' with cost
183 # (number of files in the original tree) * (number of new files)
184 # - one might want to include '-B' option, e.g. '-B', '-M'
185 our @diff_opts = ('-M'); # taken from git_commit
187 # Disables features that would allow repository owners to inject script into
189 our $prevent_xss = 0;
191 # Path to the highlight executable to use (must be the one from
192 # http://www.andre-simon.de due to assumptions about parameters and output).
193 # Useful if highlight is not installed on your webserver's PATH.
194 # [Default: highlight]
195 our $highlight_bin = "++HIGHLIGHT_BIN++";
197 # information about snapshot formats that gitweb is capable of serving
198 our %known_snapshot_formats = (
200 # 'display' => display name,
201 # 'type' => mime type,
202 # 'suffix' => filename suffix,
203 # 'format' => --format for git-archive,
204 # 'compressor' => [compressor command and arguments]
205 # (array reference, optional)
206 # 'disabled' => boolean (optional)}
209 'display' => 'tar.gz',
210 'type' => 'application/x-gzip',
211 'suffix' => '.tar.gz',
213 'compressor' => ['gzip', '-n']},
216 'display' => 'tar.bz2',
217 'type' => 'application/x-bzip2',
218 'suffix' => '.tar.bz2',
220 'compressor' => ['bzip2']},
223 'display' => 'tar.xz',
224 'type' => 'application/x-xz',
225 'suffix' => '.tar.xz',
227 'compressor' => ['xz'],
232 'type' => 'application/x-zip',
237 # Aliases so we understand old gitweb.snapshot values in repository
239 our %known_snapshot_format_aliases = (
244 # backward compatibility: legacy gitweb config support
245 'x-gzip' => undef, 'gz' => undef,
246 'x-bzip2' => undef, 'bz2' => undef,
247 'x-zip' => undef, '' => undef,
250 # Pixel sizes for icons and avatars. If the default font sizes or lineheights
251 # are changed, it may be appropriate to change these values too via
258 # Used to set the maximum load that we will still respond to gitweb queries.
259 # If server load exceed this value then return "503 server busy" error.
260 # If gitweb cannot determined server load, it is taken to be 0.
261 # Leave it undefined (or set to 'undef') to turn off load checking.
264 # configuration for 'highlight' (http://www.andre-simon.de/)
266 our %highlight_basename = (
269 'SConstruct' => 'py', # SCons equivalent of Makefile
270 'Makefile' => 'make',
273 our %highlight_ext = (
274 # main extensions, defining name of syntax;
275 # see files in /usr/share/highlight/langDefs/ directory
276 (map { $_ => $_ } qw(py rb java css js tex bib xml awk bat ini spec tcl sql)),
277 # alternate extensions, see /etc/highlight/filetypes.conf
278 (map { $_ => 'c' } qw(c h)),
279 (map { $_ => 'sh' } qw(sh bash zsh ksh)),
280 (map { $_ => 'cpp' } qw(cpp cxx c++ cc)),
281 (map { $_ => 'php' } qw(php php3 php4 php5 phps)),
282 (map { $_ => 'pl' } qw(pl perl pm)), # perhaps also 'cgi'
283 (map { $_ => 'make'} qw(make mak mk)),
284 (map { $_ => 'xml' } qw(xml xhtml html htm)),
287 # You define site-wide feature defaults here; override them with
288 # $GITWEB_CONFIG as necessary.
291 # 'sub' => feature-sub (subroutine),
292 # 'override' => allow-override (boolean),
293 # 'default' => [ default options...] (array reference)}
295 # if feature is overridable (it means that allow-override has true value),
296 # then feature-sub will be called with default options as parameters;
297 # return value of feature-sub indicates if to enable specified feature
299 # if there is no 'sub' key (no feature-sub), then feature cannot be
302 # use gitweb_get_feature(<feature>) to retrieve the <feature> value
303 # (an array) or gitweb_check_feature(<feature>) to check if <feature>
306 # Enable the 'blame' blob view, showing the last commit that modified
307 # each line in the file. This can be very CPU-intensive.
309 # To enable system wide have in $GITWEB_CONFIG
310 # $feature{'blame'}{'default'} = [1];
311 # To have project specific config enable override in $GITWEB_CONFIG
312 # $feature{'blame'}{'override'} = 1;
313 # and in project config gitweb.blame = 0|1;
315 'sub' => sub { feature_bool('blame', @_) },
319 # Enable the 'snapshot' link, providing a compressed archive of any
320 # tree. This can potentially generate high traffic if you have large
323 # Value is a list of formats defined in %known_snapshot_formats that
325 # To disable system wide have in $GITWEB_CONFIG
326 # $feature{'snapshot'}{'default'} = [];
327 # To have project specific config enable override in $GITWEB_CONFIG
328 # $feature{'snapshot'}{'override'} = 1;
329 # and in project config, a comma-separated list of formats or "none"
330 # to disable. Example: gitweb.snapshot = tbz2,zip;
332 'sub' => \&feature_snapshot,
334 'default' => ['tgz']},
336 # Enable text search, which will list the commits which match author,
337 # committer or commit text to a given string. Enabled by default.
338 # Project specific override is not supported.
340 # Note that this controls all search features, which means that if
341 # it is disabled, then 'grep' and 'pickaxe' search would also be
347 # Enable grep search, which will list the files in currently selected
348 # tree containing the given string. Enabled by default. This can be
349 # potentially CPU-intensive, of course.
350 # Note that you need to have 'search' feature enabled too.
352 # To enable system wide have in $GITWEB_CONFIG
353 # $feature{'grep'}{'default'} = [1];
354 # To have project specific config enable override in $GITWEB_CONFIG
355 # $feature{'grep'}{'override'} = 1;
356 # and in project config gitweb.grep = 0|1;
358 'sub' => sub { feature_bool('grep', @_) },
362 # Enable the pickaxe search, which will list the commits that modified
363 # a given string in a file. This can be practical and quite faster
364 # alternative to 'blame', but still potentially CPU-intensive.
365 # Note that you need to have 'search' feature enabled too.
367 # To enable system wide have in $GITWEB_CONFIG
368 # $feature{'pickaxe'}{'default'} = [1];
369 # To have project specific config enable override in $GITWEB_CONFIG
370 # $feature{'pickaxe'}{'override'} = 1;
371 # and in project config gitweb.pickaxe = 0|1;
373 'sub' => sub { feature_bool('pickaxe', @_) },
377 # Enable showing size of blobs in a 'tree' view, in a separate
378 # column, similar to what 'ls -l' does. This cost a bit of IO.
380 # To disable system wide have in $GITWEB_CONFIG
381 # $feature{'show-sizes'}{'default'} = [0];
382 # To have project specific config enable override in $GITWEB_CONFIG
383 # $feature{'show-sizes'}{'override'} = 1;
384 # and in project config gitweb.showsizes = 0|1;
386 'sub' => sub { feature_bool('showsizes', @_) },
390 # Make gitweb use an alternative format of the URLs which can be
391 # more readable and natural-looking: project name is embedded
392 # directly in the path and the query string contains other
393 # auxiliary information. All gitweb installations recognize
394 # URL in either format; this configures in which formats gitweb
397 # To enable system wide have in $GITWEB_CONFIG
398 # $feature{'pathinfo'}{'default'} = [1];
399 # Project specific override is not supported.
401 # Note that you will need to change the default location of CSS,
402 # favicon, logo and possibly other files to an absolute URL. Also,
403 # if gitweb.cgi serves as your indexfile, you will need to force
404 # $my_uri to contain the script name in your $GITWEB_CONFIG.
409 # Make gitweb consider projects in project root subdirectories
410 # to be forks of existing projects. Given project $projname.git,
411 # projects matching $projname/*.git will not be shown in the main
412 # projects list, instead a '+' mark will be added to $projname
413 # there and a 'forks' view will be enabled for the project, listing
414 # all the forks. If project list is taken from a file, forks have
415 # to be listed after the main project.
417 # To enable system wide have in $GITWEB_CONFIG
418 # $feature{'forks'}{'default'} = [1];
419 # Project specific override is not supported.
424 # Insert custom links to the action bar of all project pages.
425 # This enables you mainly to link to third-party scripts integrating
426 # into gitweb; e.g. git-browser for graphical history representation
427 # or custom web-based repository administration interface.
429 # The 'default' value consists of a list of triplets in the form
430 # (label, link, position) where position is the label after which
431 # to insert the link and link is a format string where %n expands
432 # to the project name, %f to the project path within the filesystem,
433 # %h to the current hash (h gitweb parameter) and %b to the current
434 # hash base (hb gitweb parameter); %% expands to %.
436 # To enable system wide have in $GITWEB_CONFIG e.g.
437 # $feature{'actions'}{'default'} = [('graphiclog',
438 # '/git-browser/by-commit.html?r=%n', 'summary')];
439 # Project specific override is not supported.
444 # Allow gitweb scan project content tags of project repository,
445 # and display the popular Web 2.0-ish "tag cloud" near the projects
446 # list. Note that this is something COMPLETELY different from the
449 # gitweb by itself can show existing tags, but it does not handle
450 # tagging itself; you need to do it externally, outside gitweb.
451 # The format is described in git_get_project_ctags() subroutine.
452 # You may want to install the HTML::TagCloud Perl module to get
453 # a pretty tag cloud instead of just a list of tags.
455 # To enable system wide have in $GITWEB_CONFIG
456 # $feature{'ctags'}{'default'} = [1];
457 # Project specific override is not supported.
459 # In the future whether ctags editing is enabled might depend
460 # on the value, but using 1 should always mean no editing of ctags.
465 # The maximum number of patches in a patchset generated in patch
466 # view. Set this to 0 or undef to disable patch view, or to a
467 # negative number to remove any limit.
469 # To disable system wide have in $GITWEB_CONFIG
470 # $feature{'patches'}{'default'} = [0];
471 # To have project specific config enable override in $GITWEB_CONFIG
472 # $feature{'patches'}{'override'} = 1;
473 # and in project config gitweb.patches = 0|n;
474 # where n is the maximum number of patches allowed in a patchset.
476 'sub' => \&feature_patches,
480 # Avatar support. When this feature is enabled, views such as
481 # shortlog or commit will display an avatar associated with
482 # the email of the committer(s) and/or author(s).
484 # Currently available providers are gravatar and picon.
485 # If an unknown provider is specified, the feature is disabled.
487 # Gravatar depends on Digest::MD5.
488 # Picon currently relies on the indiana.edu database.
490 # To enable system wide have in $GITWEB_CONFIG
491 # $feature{'avatar'}{'default'} = ['<provider>'];
492 # where <provider> is either gravatar or picon.
493 # To have project specific config enable override in $GITWEB_CONFIG
494 # $feature{'avatar'}{'override'} = 1;
495 # and in project config gitweb.avatar = <provider>;
497 'sub' => \&feature_avatar,
501 # Enable displaying how much time and how many git commands
502 # it took to generate and display page. Disabled by default.
503 # Project specific override is not supported.
508 # Enable turning some links into links to actions which require
509 # JavaScript to run (like 'blame_incremental'). Not enabled by
510 # default. Project specific override is currently not supported.
511 'javascript-actions' => {
515 # Enable and configure ability to change common timezone for dates
516 # in gitweb output via JavaScript. Enabled by default.
517 # Project specific override is not supported.
518 'javascript-timezone' => {
521 'local', # default timezone: 'utc', 'local', or '(-|+)HHMM' format,
522 # or undef to turn off this feature
523 'gitweb_tz', # name of cookie where to store selected timezone
524 'datetime', # CSS class used to mark up dates for manipulation
527 # Syntax highlighting support. This is based on Daniel Svensson's
528 # and Sham Chukoury's work in gitweb-xmms2.git.
529 # It requires the 'highlight' program present in $PATH,
530 # and therefore is disabled by default.
532 # To enable system wide have in $GITWEB_CONFIG
533 # $feature{'highlight'}{'default'} = [1];
536 'sub' => sub { feature_bool('highlight', @_) },
540 # Enable displaying of remote heads in the heads list
542 # To enable system wide have in $GITWEB_CONFIG
543 # $feature{'remote_heads'}{'default'} = [1];
544 # To have project specific config enable override in $GITWEB_CONFIG
545 # $feature{'remote_heads'}{'override'} = 1;
546 # and in project config gitweb.remoteheads = 0|1;
548 'sub' => sub { feature_bool('remote_heads', @_) },
553 sub gitweb_get_feature {
555 return unless exists $feature{$name};
556 my ($sub, $override, @defaults) = (
557 $feature{$name}{'sub'},
558 $feature{$name}{'override'},
559 @{$feature{$name}{'default'}});
560 # project specific override is possible only if we have project
561 our $git_dir; # global variable, declared later
562 if (!$override || !defined $git_dir) {
566 warn "feature $name is not overridable";
569 return $sub->(@defaults);
572 # A wrapper to check if a given feature is enabled.
573 # With this, you can say
575 # my $bool_feat = gitweb_check_feature('bool_feat');
576 # gitweb_check_feature('bool_feat') or somecode;
580 # my ($bool_feat) = gitweb_get_feature('bool_feat');
581 # (gitweb_get_feature('bool_feat'))[0] or somecode;
583 sub gitweb_check_feature {
584 return (gitweb_get_feature(@_))[0];
590 my ($val) = git_get_project_config($key, '--bool');
594 } elsif ($val eq 'true') {
596 } elsif ($val eq 'false') {
601 sub feature_snapshot {
604 my ($val) = git_get_project_config('snapshot');
607 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
613 sub feature_patches {
614 my @val = (git_get_project_config('patches', '--int'));
624 my @val = (git_get_project_config('avatar'));
626 return @val ? @val : @_;
629 # checking HEAD file with -e is fragile if the repository was
630 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
632 sub check_head_link {
634 my $headfile = "$dir/HEAD";
635 return ((-e $headfile) ||
636 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
639 sub check_export_ok {
641 return (check_head_link($dir) &&
642 (!$export_ok || -e "$dir/$export_ok") &&
643 (!$export_auth_hook || $export_auth_hook->($dir)));
646 # process alternate names for backward compatibility
647 # filter out unsupported (unknown) snapshot formats
648 sub filter_snapshot_fmts {
652 exists $known_snapshot_format_aliases{$_} ?
653 $known_snapshot_format_aliases{$_} : $_} @fmts;
655 exists $known_snapshot_formats{$_} &&
656 !$known_snapshot_formats{$_}{'disabled'}} @fmts;
659 # If it is set to code reference, it is code that it is to be run once per
660 # request, allowing updating configurations that change with each request,
661 # while running other code in config file only once.
663 # Otherwise, if it is false then gitweb would process config file only once;
664 # if it is true then gitweb config would be run for each request.
665 our $per_request_config = 1;
667 # read and parse gitweb config file given by its parameter.
668 # returns true on success, false on recoverable error, allowing
669 # to chain this subroutine, using first file that exists.
670 # dies on errors during parsing config file, as it is unrecoverable.
671 sub read_config_file {
672 my $filename = shift;
673 return unless defined $filename;
674 # die if there are errors parsing config file
683 our ($GITWEB_CONFIG, $GITWEB_CONFIG_SYSTEM, $GITWEB_CONFIG_COMMON);
684 sub evaluate_gitweb_config {
685 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
686 our $GITWEB_CONFIG_SYSTEM = $ENV{'GITWEB_CONFIG_SYSTEM'} || "++GITWEB_CONFIG_SYSTEM++";
687 our $GITWEB_CONFIG_COMMON = $ENV{'GITWEB_CONFIG_COMMON'} || "++GITWEB_CONFIG_COMMON++";
689 # Protect against duplications of file names, to not read config twice.
690 # Only one of $GITWEB_CONFIG and $GITWEB_CONFIG_SYSTEM is used, so
691 # there possibility of duplication of filename there doesn't matter.
692 $GITWEB_CONFIG = "" if ($GITWEB_CONFIG eq $GITWEB_CONFIG_COMMON);
693 $GITWEB_CONFIG_SYSTEM = "" if ($GITWEB_CONFIG_SYSTEM eq $GITWEB_CONFIG_COMMON);
695 # Common system-wide settings for convenience.
696 # Those settings can be ovverriden by GITWEB_CONFIG or GITWEB_CONFIG_SYSTEM.
697 read_config_file($GITWEB_CONFIG_COMMON);
699 # Use first config file that exists. This means use the per-instance
700 # GITWEB_CONFIG if exists, otherwise use GITWEB_SYSTEM_CONFIG.
701 read_config_file($GITWEB_CONFIG) and return;
702 read_config_file($GITWEB_CONFIG_SYSTEM);
705 # Get loadavg of system, to compare against $maxload.
706 # Currently it requires '/proc/loadavg' present to get loadavg;
707 # if it is not present it returns 0, which means no load checking.
709 if( -e '/proc/loadavg' ){
710 open my $fd, '<', '/proc/loadavg'
712 my @load = split(/\s+/, scalar <$fd>);
715 # The first three columns measure CPU and IO utilization of the last one,
716 # five, and 10 minute periods. The fourth column shows the number of
717 # currently running processes and the total number of processes in the m/n
718 # format. The last column displays the last process ID used.
719 return $load[0] || 0;
721 # additional checks for load average should go here for things that don't export
727 # version of the core git binary
729 sub evaluate_git_version {
730 our $git_version = qx("$GIT" --version) =~ m/git version (.*)$/ ? $1 : "unknown";
731 $number_of_git_cmds++;
735 if (defined $maxload && get_loadavg() > $maxload) {
736 die_error(503, "The load average on the server is too high");
740 # ======================================================================
741 # input validation and dispatch
743 # input parameters can be collected from a variety of sources (presently, CGI
744 # and PATH_INFO), so we define an %input_params hash that collects them all
745 # together during validation: this allows subsequent uses (e.g. href()) to be
746 # agnostic of the parameter origin
748 our %input_params = ();
750 # input parameters are stored with the long parameter name as key. This will
751 # also be used in the href subroutine to convert parameters to their CGI
752 # equivalent, and since the href() usage is the most frequent one, we store
753 # the name -> CGI key mapping here, instead of the reverse.
755 # XXX: Warning: If you touch this, check the search form for updating,
758 our @cgi_param_mapping = (
766 hash_parent_base => "hpb",
771 snapshot_format => "sf",
772 extra_options => "opt",
773 search_use_regexp => "sr",
776 project_filter => "pf",
777 # this must be last entry (for manipulation from JavaScript)
780 our %cgi_param_mapping = @cgi_param_mapping;
782 # we will also need to know the possible actions, for validation
784 "blame" => \&git_blame,
785 "blame_incremental" => \&git_blame_incremental,
786 "blame_data" => \&git_blame_data,
787 "blobdiff" => \&git_blobdiff,
788 "blobdiff_plain" => \&git_blobdiff_plain,
789 "blob" => \&git_blob,
790 "blob_plain" => \&git_blob_plain,
791 "commitdiff" => \&git_commitdiff,
792 "commitdiff_plain" => \&git_commitdiff_plain,
793 "commit" => \&git_commit,
794 "forks" => \&git_forks,
795 "heads" => \&git_heads,
796 "history" => \&git_history,
798 "patch" => \&git_patch,
799 "patches" => \&git_patches,
800 "remotes" => \&git_remotes,
802 "atom" => \&git_atom,
803 "search" => \&git_search,
804 "search_help" => \&git_search_help,
805 "shortlog" => \&git_shortlog,
806 "summary" => \&git_summary,
808 "tags" => \&git_tags,
809 "tree" => \&git_tree,
810 "snapshot" => \&git_snapshot,
811 "object" => \&git_object,
812 # those below don't need $project
813 "opml" => \&git_opml,
814 "project_list" => \&git_project_list,
815 "project_index" => \&git_project_index,
818 # finally, we have the hash of allowed extra_options for the commands that
820 our %allowed_options = (
821 "--no-merges" => [ qw(rss atom log shortlog history) ],
824 # fill %input_params with the CGI parameters. All values except for 'opt'
825 # should be single values, but opt can be an array. We should probably
826 # build an array of parameters that can be multi-valued, but since for the time
827 # being it's only this one, we just single it out
828 sub evaluate_query_params {
831 while (my ($name, $symbol) = each %cgi_param_mapping) {
832 if ($symbol eq 'opt') {
833 $input_params{$name} = [ map { decode_utf8($_) } $cgi->param($symbol) ];
835 $input_params{$name} = decode_utf8($cgi->param($symbol));
840 # now read PATH_INFO and update the parameter list for missing parameters
841 sub evaluate_path_info {
842 return if defined $input_params{'project'};
843 return if !$path_info;
844 $path_info =~ s,^/+,,;
845 return if !$path_info;
847 # find which part of PATH_INFO is project
848 my $project = $path_info;
850 while ($project && !check_head_link("$projectroot/$project")) {
851 $project =~ s,/*[^/]*$,,;
853 return unless $project;
854 $input_params{'project'} = $project;
856 # do not change any parameters if an action is given using the query string
857 return if $input_params{'action'};
858 $path_info =~ s,^\Q$project\E/*,,;
860 # next, check if we have an action
861 my $action = $path_info;
863 if (exists $actions{$action}) {
864 $path_info =~ s,^$action/*,,;
865 $input_params{'action'} = $action;
868 # list of actions that want hash_base instead of hash, but can have no
869 # pathname (f) parameter
875 # we want to catch, among others
876 # [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name]
877 my ($parentrefname, $parentpathname, $refname, $pathname) =
878 ($path_info =~ /^(?:(.+?)(?::(.+))?\.\.)?([^:]+?)?(?::(.+))?$/);
880 # first, analyze the 'current' part
881 if (defined $pathname) {
882 # we got "branch:filename" or "branch:dir/"
883 # we could use git_get_type(branch:pathname), but:
884 # - it needs $git_dir
885 # - it does a git() call
886 # - the convention of terminating directories with a slash
887 # makes it superfluous
888 # - embedding the action in the PATH_INFO would make it even
890 $pathname =~ s,^/+,,;
891 if (!$pathname || substr($pathname, -1) eq "/") {
892 $input_params{'action'} ||= "tree";
895 # the default action depends on whether we had parent info
897 if ($parentrefname) {
898 $input_params{'action'} ||= "blobdiff_plain";
900 $input_params{'action'} ||= "blob_plain";
903 $input_params{'hash_base'} ||= $refname;
904 $input_params{'file_name'} ||= $pathname;
905 } elsif (defined $refname) {
906 # we got "branch". In this case we have to choose if we have to
907 # set hash or hash_base.
909 # Most of the actions without a pathname only want hash to be
910 # set, except for the ones specified in @wants_base that want
911 # hash_base instead. It should also be noted that hand-crafted
912 # links having 'history' as an action and no pathname or hash
913 # set will fail, but that happens regardless of PATH_INFO.
914 if (defined $parentrefname) {
915 # if there is parent let the default be 'shortlog' action
916 # (for http://git.example.com/repo.git/A..B links); if there
917 # is no parent, dispatch will detect type of object and set
918 # action appropriately if required (if action is not set)
919 $input_params{'action'} ||= "shortlog";
921 if ($input_params{'action'} &&
922 grep { $_ eq $input_params{'action'} } @wants_base) {
923 $input_params{'hash_base'} ||= $refname;
925 $input_params{'hash'} ||= $refname;
929 # next, handle the 'parent' part, if present
930 if (defined $parentrefname) {
931 # a missing pathspec defaults to the 'current' filename, allowing e.g.
932 # someproject/blobdiff/oldrev..newrev:/filename
933 if ($parentpathname) {
934 $parentpathname =~ s,^/+,,;
935 $parentpathname =~ s,/$,,;
936 $input_params{'file_parent'} ||= $parentpathname;
938 $input_params{'file_parent'} ||= $input_params{'file_name'};
940 # we assume that hash_parent_base is wanted if a path was specified,
941 # or if the action wants hash_base instead of hash
942 if (defined $input_params{'file_parent'} ||
943 grep { $_ eq $input_params{'action'} } @wants_base) {
944 $input_params{'hash_parent_base'} ||= $parentrefname;
946 $input_params{'hash_parent'} ||= $parentrefname;
950 # for the snapshot action, we allow URLs in the form
951 # $project/snapshot/$hash.ext
952 # where .ext determines the snapshot and gets removed from the
953 # passed $refname to provide the $hash.
955 # To be able to tell that $refname includes the format extension, we
956 # require the following two conditions to be satisfied:
957 # - the hash input parameter MUST have been set from the $refname part
958 # of the URL (i.e. they must be equal)
959 # - the snapshot format MUST NOT have been defined already (e.g. from
961 # It's also useless to try any matching unless $refname has a dot,
962 # so we check for that too
963 if (defined $input_params{'action'} &&
964 $input_params{'action'} eq 'snapshot' &&
965 defined $refname && index($refname, '.') != -1 &&
966 $refname eq $input_params{'hash'} &&
967 !defined $input_params{'snapshot_format'}) {
968 # We loop over the known snapshot formats, checking for
969 # extensions. Allowed extensions are both the defined suffix
970 # (which includes the initial dot already) and the snapshot
971 # format key itself, with a prepended dot
972 while (my ($fmt, $opt) = each %known_snapshot_formats) {
974 unless ($hash =~ s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) {
978 # a valid suffix was found, so set the snapshot format
979 # and reset the hash parameter
980 $input_params{'snapshot_format'} = $fmt;
981 $input_params{'hash'} = $hash;
982 # we also set the format suffix to the one requested
983 # in the URL: this way a request for e.g. .tgz returns
984 # a .tgz instead of a .tar.gz
985 $known_snapshot_formats{$fmt}{'suffix'} = $sfx;
991 our ($action, $project, $file_name, $file_parent, $hash, $hash_parent, $hash_base,
992 $hash_parent_base, @extra_options, $page, $searchtype, $search_use_regexp,
993 $searchtext, $search_regexp, $project_filter);
994 sub evaluate_and_validate_params {
995 our $action = $input_params{'action'};
996 if (defined $action) {
997 if (!validate_action($action)) {
998 die_error(400, "Invalid action parameter");
1002 # parameters which are pathnames
1003 our $project = $input_params{'project'};
1004 if (defined $project) {
1005 if (!validate_project($project)) {
1007 die_error(404, "No such project");
1011 our $project_filter = $input_params{'project_filter'};
1012 if (defined $project_filter) {
1013 if (!validate_pathname($project_filter)) {
1014 die_error(404, "Invalid project_filter parameter");
1018 our $file_name = $input_params{'file_name'};
1019 if (defined $file_name) {
1020 if (!validate_pathname($file_name)) {
1021 die_error(400, "Invalid file parameter");
1025 our $file_parent = $input_params{'file_parent'};
1026 if (defined $file_parent) {
1027 if (!validate_pathname($file_parent)) {
1028 die_error(400, "Invalid file parent parameter");
1032 # parameters which are refnames
1033 our $hash = $input_params{'hash'};
1034 if (defined $hash) {
1035 if (!validate_refname($hash)) {
1036 die_error(400, "Invalid hash parameter");
1040 our $hash_parent = $input_params{'hash_parent'};
1041 if (defined $hash_parent) {
1042 if (!validate_refname($hash_parent)) {
1043 die_error(400, "Invalid hash parent parameter");
1047 our $hash_base = $input_params{'hash_base'};
1048 if (defined $hash_base) {
1049 if (!validate_refname($hash_base)) {
1050 die_error(400, "Invalid hash base parameter");
1054 our @extra_options = @{$input_params{'extra_options'}};
1055 # @extra_options is always defined, since it can only be (currently) set from
1056 # CGI, and $cgi->param() returns the empty array in array context if the param
1058 foreach my $opt (@extra_options) {
1059 if (not exists $allowed_options{$opt}) {
1060 die_error(400, "Invalid option parameter");
1062 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
1063 die_error(400, "Invalid option parameter for this action");
1067 our $hash_parent_base = $input_params{'hash_parent_base'};
1068 if (defined $hash_parent_base) {
1069 if (!validate_refname($hash_parent_base)) {
1070 die_error(400, "Invalid hash parent base parameter");
1075 our $page = $input_params{'page'};
1076 if (defined $page) {
1077 if ($page =~ m/[^0-9]/) {
1078 die_error(400, "Invalid page parameter");
1082 our $searchtype = $input_params{'searchtype'};
1083 if (defined $searchtype) {
1084 if ($searchtype =~ m/[^a-z]/) {
1085 die_error(400, "Invalid searchtype parameter");
1089 our $search_use_regexp = $input_params{'search_use_regexp'};
1091 our $searchtext = $input_params{'searchtext'};
1092 our $search_regexp = undef;
1093 if (defined $searchtext) {
1094 if (length($searchtext) < 2) {
1095 die_error(403, "At least two characters are required for search parameter");
1097 if ($search_use_regexp) {
1098 $search_regexp = $searchtext;
1099 if (!eval { qr/$search_regexp/; 1; }) {
1100 (my $error = $@) =~ s/ at \S+ line \d+.*\n?//;
1101 die_error(400, "Invalid search regexp '$search_regexp'",
1105 $search_regexp = quotemeta $searchtext;
1110 # path to the current git repository
1112 sub evaluate_git_dir {
1113 our $git_dir = "$projectroot/$project" if $project;
1116 our (@snapshot_fmts, $git_avatar);
1117 sub configure_gitweb_features {
1118 # list of supported snapshot formats
1119 our @snapshot_fmts = gitweb_get_feature('snapshot');
1120 @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
1122 # check that the avatar feature is set to a known provider name,
1123 # and for each provider check if the dependencies are satisfied.
1124 # if the provider name is invalid or the dependencies are not met,
1125 # reset $git_avatar to the empty string.
1126 our ($git_avatar) = gitweb_get_feature('avatar');
1127 if ($git_avatar eq 'gravatar') {
1128 $git_avatar = '' unless (eval { require Digest::MD5; 1; });
1129 } elsif ($git_avatar eq 'picon') {
1136 # custom error handler: 'die <message>' is Internal Server Error
1137 sub handle_errors_html {
1138 my $msg = shift; # it is already HTML escaped
1140 # to avoid infinite loop where error occurs in die_error,
1141 # change handler to default handler, disabling handle_errors_html
1142 set_message("Error occurred when inside die_error:\n$msg");
1144 # you cannot jump out of die_error when called as error handler;
1145 # the subroutine set via CGI::Carp::set_message is called _after_
1146 # HTTP headers are already written, so it cannot write them itself
1147 die_error(undef, undef, $msg, -error_handler => 1, -no_http_header => 1);
1149 set_message(\&handle_errors_html);
1153 if (!defined $action) {
1154 if (defined $hash) {
1155 $action = git_get_type($hash);
1156 $action or die_error(404, "Object does not exist");
1157 } elsif (defined $hash_base && defined $file_name) {
1158 $action = git_get_type("$hash_base:$file_name");
1159 $action or die_error(404, "File or directory does not exist");
1160 } elsif (defined $project) {
1161 $action = 'summary';
1163 $action = 'project_list';
1166 if (!defined($actions{$action})) {
1167 die_error(400, "Unknown action");
1169 if ($action !~ m/^(?:opml|project_list|project_index)$/ &&
1171 die_error(400, "Project needed");
1173 $actions{$action}->();
1177 our $t0 = [ gettimeofday() ]
1179 our $number_of_git_cmds = 0;
1182 our $first_request = 1;
1187 if ($first_request) {
1188 evaluate_gitweb_config();
1189 evaluate_git_version();
1191 if ($per_request_config) {
1192 if (ref($per_request_config) eq 'CODE') {
1193 $per_request_config->();
1194 } elsif (!$first_request) {
1195 evaluate_gitweb_config();
1200 # $projectroot and $projects_list might be set in gitweb config file
1201 $projects_list ||= $projectroot;
1203 evaluate_query_params();
1204 evaluate_path_info();
1205 evaluate_and_validate_params();
1208 configure_gitweb_features();
1213 our $is_last_request = sub { 1 };
1214 our ($pre_dispatch_hook, $post_dispatch_hook, $pre_listen_hook);
1217 sub configure_as_fcgi {
1219 our $CGI = 'CGI::Fast';
1221 my $request_number = 0;
1222 # let each child service 100 requests
1223 our $is_last_request = sub { ++$request_number > 100 };
1226 my $script_name = $ENV{'SCRIPT_NAME'} || $ENV{'SCRIPT_FILENAME'} || __FILE__;
1228 if $script_name =~ /\.fcgi$/;
1230 return unless (@ARGV);
1232 require Getopt::Long;
1233 Getopt::Long::GetOptions(
1234 'fastcgi|fcgi|f' => \&configure_as_fcgi,
1235 'nproc|n=i' => sub {
1236 my ($arg, $val) = @_;
1237 return unless eval { require FCGI::ProcManager; 1; };
1238 my $proc_manager = FCGI::ProcManager->new({
1239 n_processes => $val,
1241 our $pre_listen_hook = sub { $proc_manager->pm_manage() };
1242 our $pre_dispatch_hook = sub { $proc_manager->pm_pre_dispatch() };
1243 our $post_dispatch_hook = sub { $proc_manager->pm_post_dispatch() };
1252 $pre_listen_hook->()
1253 if $pre_listen_hook;
1256 while ($cgi = $CGI->new()) {
1257 $pre_dispatch_hook->()
1258 if $pre_dispatch_hook;
1262 $post_dispatch_hook->()
1263 if $post_dispatch_hook;
1266 last REQUEST if ($is_last_request->());
1275 if (defined caller) {
1276 # wrapped in a subroutine processing requests,
1277 # e.g. mod_perl with ModPerl::Registry, or PSGI with Plack::App::WrapCGI
1280 # pure CGI script, serving single request
1284 ## ======================================================================
1287 # possible values of extra options
1288 # -full => 0|1 - use absolute/full URL ($my_uri/$my_url as base)
1289 # -replay => 1 - start from a current view (replay with modifications)
1290 # -path_info => 0|1 - don't use/use path_info URL (if possible)
1291 # -anchor => ANCHOR - add #ANCHOR to end of URL, implies -replay if used alone
1294 # default is to use -absolute url() i.e. $my_uri
1295 my $href = $params{-full} ? $my_url : $my_uri;
1297 # implicit -replay, must be first of implicit params
1298 $params{-replay} = 1 if (keys %params == 1 && $params{-anchor});
1300 $params{'project'} = $project unless exists $params{'project'};
1302 if ($params{-replay}) {
1303 while (my ($name, $symbol) = each %cgi_param_mapping) {
1304 if (!exists $params{$name}) {
1305 $params{$name} = $input_params{$name};
1310 my $use_pathinfo = gitweb_check_feature('pathinfo');
1311 if (defined $params{'project'} &&
1312 (exists $params{-path_info} ? $params{-path_info} : $use_pathinfo)) {
1313 # try to put as many parameters as possible in PATH_INFO:
1316 # - hash_parent or hash_parent_base:/file_parent
1317 # - hash or hash_base:/filename
1318 # - the snapshot_format as an appropriate suffix
1320 # When the script is the root DirectoryIndex for the domain,
1321 # $href here would be something like http://gitweb.example.com/
1322 # Thus, we strip any trailing / from $href, to spare us double
1323 # slashes in the final URL
1326 # Then add the project name, if present
1327 $href .= "/".esc_path_info($params{'project'});
1328 delete $params{'project'};
1330 # since we destructively absorb parameters, we keep this
1331 # boolean that remembers if we're handling a snapshot
1332 my $is_snapshot = $params{'action'} eq 'snapshot';
1334 # Summary just uses the project path URL, any other action is
1336 if (defined $params{'action'}) {
1337 $href .= "/".esc_path_info($params{'action'})
1338 unless $params{'action'} eq 'summary';
1339 delete $params{'action'};
1342 # Next, we put hash_parent_base:/file_parent..hash_base:/file_name,
1343 # stripping nonexistent or useless pieces
1344 $href .= "/" if ($params{'hash_base'} || $params{'hash_parent_base'}
1345 || $params{'hash_parent'} || $params{'hash'});
1346 if (defined $params{'hash_base'}) {
1347 if (defined $params{'hash_parent_base'}) {
1348 $href .= esc_path_info($params{'hash_parent_base'});
1349 # skip the file_parent if it's the same as the file_name
1350 if (defined $params{'file_parent'}) {
1351 if (defined $params{'file_name'} && $params{'file_parent'} eq $params{'file_name'}) {
1352 delete $params{'file_parent'};
1353 } elsif ($params{'file_parent'} !~ /\.\./) {
1354 $href .= ":/".esc_path_info($params{'file_parent'});
1355 delete $params{'file_parent'};
1359 delete $params{'hash_parent'};
1360 delete $params{'hash_parent_base'};
1361 } elsif (defined $params{'hash_parent'}) {
1362 $href .= esc_path_info($params{'hash_parent'}). "..";
1363 delete $params{'hash_parent'};
1366 $href .= esc_path_info($params{'hash_base'});
1367 if (defined $params{'file_name'} && $params{'file_name'} !~ /\.\./) {
1368 $href .= ":/".esc_path_info($params{'file_name'});
1369 delete $params{'file_name'};
1371 delete $params{'hash'};
1372 delete $params{'hash_base'};
1373 } elsif (defined $params{'hash'}) {
1374 $href .= esc_path_info($params{'hash'});
1375 delete $params{'hash'};
1378 # If the action was a snapshot, we can absorb the
1379 # snapshot_format parameter too
1381 my $fmt = $params{'snapshot_format'};
1382 # snapshot_format should always be defined when href()
1383 # is called, but just in case some code forgets, we
1384 # fall back to the default
1385 $fmt ||= $snapshot_fmts[0];
1386 $href .= $known_snapshot_formats{$fmt}{'suffix'};
1387 delete $params{'snapshot_format'};
1391 # now encode the parameters explicitly
1393 for (my $i = 0; $i < @cgi_param_mapping; $i += 2) {
1394 my ($name, $symbol) = ($cgi_param_mapping[$i], $cgi_param_mapping[$i+1]);
1395 if (defined $params{$name}) {
1396 if (ref($params{$name}) eq "ARRAY") {
1397 foreach my $par (@{$params{$name}}) {
1398 push @result, $symbol . "=" . esc_param($par);
1401 push @result, $symbol . "=" . esc_param($params{$name});
1405 $href .= "?" . join(';', @result) if scalar @result;
1407 # final transformation: trailing spaces must be escaped (URI-encoded)
1408 $href =~ s/(\s+)$/CGI::escape($1)/e;
1410 if ($params{-anchor}) {
1411 $href .= "#".esc_param($params{-anchor});
1418 ## ======================================================================
1419 ## validation, quoting/unquoting and escaping
1421 sub validate_action {
1422 my $input = shift || return undef;
1423 return undef unless exists $actions{$input};
1427 sub validate_project {
1428 my $input = shift || return undef;
1429 if (!validate_pathname($input) ||
1430 !(-d "$projectroot/$input") ||
1431 !check_export_ok("$projectroot/$input") ||
1432 ($strict_export && !project_in_list($input))) {
1439 sub validate_pathname {
1440 my $input = shift || return undef;
1442 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
1443 # at the beginning, at the end, and between slashes.
1444 # also this catches doubled slashes
1445 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
1448 # no null characters
1449 if ($input =~ m!\0!) {
1455 sub validate_refname {
1456 my $input = shift || return undef;
1458 # textual hashes are O.K.
1459 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
1462 # it must be correct pathname
1463 $input = validate_pathname($input)
1465 # restrictions on ref name according to git-check-ref-format
1466 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
1472 # decode sequences of octets in utf8 into Perl's internal form,
1473 # which is utf-8 with utf8 flag set if needed. gitweb writes out
1474 # in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
1477 return undef unless defined $str;
1479 if (utf8::is_utf8($str) || utf8::decode($str)) {
1482 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
1486 # quote unsafe chars, but keep the slash, even when it's not
1487 # correct, but quoted slashes look too horrible in bookmarks
1490 return undef unless defined $str;
1491 $str =~ s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;
1496 # the quoting rules for path_info fragment are slightly different
1499 return undef unless defined $str;
1501 # path_info doesn't treat '+' as space (specially), but '?' must be escaped
1502 $str =~ s/([^A-Za-z0-9\-_.~();\/;:@&= +]+)/CGI::escape($1)/eg;
1507 # quote unsafe chars in whole URL, so some characters cannot be quoted
1510 return undef unless defined $str;
1511 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&= ]+)/CGI::escape($1)/eg;
1516 # quote unsafe characters in HTML attributes
1519 # for XHTML conformance escaping '"' to '"' is not enough
1520 return esc_html(@_);
1523 # replace invalid utf8 character with SUBSTITUTION sequence
1528 return undef unless defined $str;
1530 $str = to_utf8($str);
1531 $str = $cgi->escapeHTML($str);
1532 if ($opts{'-nbsp'}) {
1533 $str =~ s/ / /g;
1535 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
1539 # quote control characters and escape filename to HTML
1544 return undef unless defined $str;
1546 $str = to_utf8($str);
1547 $str = $cgi->escapeHTML($str);
1548 if ($opts{'-nbsp'}) {
1549 $str =~ s/ / /g;
1551 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
1555 # Sanitize for use in XHTML + application/xml+xhtm (valid XML 1.0)
1559 return undef unless defined $str;
1561 $str = to_utf8($str);
1562 $str =~ s|([[:cntrl:]])|(index("\t\n\r", $1) != -1 ? $1 : quot_cec($1))|eg;
1566 # Make control characters "printable", using character escape codes (CEC)
1570 my %es = ( # character escape codes, aka escape sequences
1571 "\t" => '\t', # tab (HT)
1572 "\n" => '\n', # line feed (LF)
1573 "\r" => '\r', # carrige return (CR)
1574 "\f" => '\f', # form feed (FF)
1575 "\b" => '\b', # backspace (BS)
1576 "\a" => '\a', # alarm (bell) (BEL)
1577 "\e" => '\e', # escape (ESC)
1578 "\013" => '\v', # vertical tab (VT)
1579 "\000" => '\0', # nul character (NUL)
1581 my $chr = ( (exists $es{$cntrl})
1583 : sprintf('\%2x', ord($cntrl)) );
1584 if ($opts{-nohtml}) {
1587 return "<span class=\"cntrl\">$chr</span>";
1591 # Alternatively use unicode control pictures codepoints,
1592 # Unicode "printable representation" (PR)
1597 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
1598 if ($opts{-nohtml}) {
1601 return "<span class=\"cntrl\">$chr</span>";
1605 # git may return quoted and escaped filenames
1611 my %es = ( # character escape codes, aka escape sequences
1612 't' => "\t", # tab (HT, TAB)
1613 'n' => "\n", # newline (NL)
1614 'r' => "\r", # return (CR)
1615 'f' => "\f", # form feed (FF)
1616 'b' => "\b", # backspace (BS)
1617 'a' => "\a", # alarm (bell) (BEL)
1618 'e' => "\e", # escape (ESC)
1619 'v' => "\013", # vertical tab (VT)
1622 if ($seq =~ m/^[0-7]{1,3}$/) {
1623 # octal char sequence
1624 return chr(oct($seq));
1625 } elsif (exists $es{$seq}) {
1626 # C escape sequence, aka character escape code
1629 # quoted ordinary character
1633 if ($str =~ m/^"(.*)"$/) {
1636 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
1641 # escape tabs (convert tabs to spaces)
1645 while ((my $pos = index($line, "\t")) != -1) {
1646 if (my $count = (8 - ($pos % 8))) {
1647 my $spaces = ' ' x $count;
1648 $line =~ s/\t/$spaces/;
1655 sub project_in_list {
1656 my $project = shift;
1657 my @list = git_get_projects_list();
1658 return @list && scalar(grep { $_->{'path'} eq $project } @list);
1661 ## ----------------------------------------------------------------------
1662 ## HTML aware string manipulation
1664 # Try to chop given string on a word boundary between position
1665 # $len and $len+$add_len. If there is no word boundary there,
1666 # chop at $len+$add_len. Do not chop if chopped part plus ellipsis
1667 # (marking chopped part) would be longer than given string.
1671 my $add_len = shift || 10;
1672 my $where = shift || 'right'; # 'left' | 'center' | 'right'
1674 # Make sure perl knows it is utf8 encoded so we don't
1675 # cut in the middle of a utf8 multibyte char.
1676 $str = to_utf8($str);
1678 # allow only $len chars, but don't cut a word if it would fit in $add_len
1679 # if it doesn't fit, cut it if it's still longer than the dots we would add
1680 # remove chopped character entities entirely
1682 # when chopping in the middle, distribute $len into left and right part
1683 # return early if chopping wouldn't make string shorter
1684 if ($where eq 'center') {
1685 return $str if ($len + 5 >= length($str)); # filler is length 5
1688 return $str if ($len + 4 >= length($str)); # filler is length 4
1691 # regexps: ending and beginning with word part up to $add_len
1692 my $endre = qr/.{$len}\w{0,$add_len}/;
1693 my $begre = qr/\w{0,$add_len}.{$len}/;
1695 if ($where eq 'left') {
1696 $str =~ m/^(.*?)($begre)$/;
1697 my ($lead, $body) = ($1, $2);
1698 if (length($lead) > 4) {
1701 return "$lead$body";
1703 } elsif ($where eq 'center') {
1704 $str =~ m/^($endre)(.*)$/;
1705 my ($left, $str) = ($1, $2);
1706 $str =~ m/^(.*?)($begre)$/;
1707 my ($mid, $right) = ($1, $2);
1708 if (length($mid) > 5) {
1711 return "$left$mid$right";
1714 $str =~ m/^($endre)(.*)$/;
1717 if (length($tail) > 4) {
1720 return "$body$tail";
1724 # takes the same arguments as chop_str, but also wraps a <span> around the
1725 # result with a title attribute if it does get chopped. Additionally, the
1726 # string is HTML-escaped.
1727 sub chop_and_escape_str {
1730 my $chopped = chop_str(@_);
1731 $str = to_utf8($str);
1732 if ($chopped eq $str) {
1733 return esc_html($chopped);
1735 $str =~ s/[[:cntrl:]]/?/g;
1736 return $cgi->span({-title=>$str}, esc_html($chopped));
1740 # Highlight selected fragments of string, using given CSS class,
1741 # and escape HTML. It is assumed that fragments do not overlap.
1742 # Regions are passed as list of pairs (array references).
1744 # Example: esc_html_hl_regions("foobar", "mark", [ 0, 3 ]) returns
1745 # '<span class="mark">foo</span>bar'
1746 sub esc_html_hl_regions {
1747 my ($str, $css_class, @sel) = @_;
1748 my %opts = grep { ref($_) ne 'ARRAY' } @sel;
1749 @sel = grep { ref($_) eq 'ARRAY' } @sel;
1750 return esc_html($str, %opts) unless @sel;
1756 my ($begin, $end) = @$s;
1758 # Don't create empty <span> elements.
1759 next if $end <= $begin;
1761 my $escaped = esc_html(substr($str, $begin, $end - $begin),
1764 $out .= esc_html(substr($str, $pos, $begin - $pos), %opts)
1765 if ($begin - $pos > 0);
1766 $out .= $cgi->span({-class => $css_class}, $escaped);
1770 $out .= esc_html(substr($str, $pos), %opts)
1771 if ($pos < length($str));
1776 # return positions of beginning and end of each match
1778 my ($str, $regexp) = @_;
1779 return unless (defined $str && defined $regexp);
1782 while ($str =~ /$regexp/g) {
1783 push @matches, [$-[0], $+[0]];
1788 # highlight match (if any), and escape HTML
1789 sub esc_html_match_hl {
1790 my ($str, $regexp) = @_;
1791 return esc_html($str) unless defined $regexp;
1793 my @matches = matchpos_list($str, $regexp);
1794 return esc_html($str) unless @matches;
1796 return esc_html_hl_regions($str, 'match', @matches);
1800 # highlight match (if any) of shortened string, and escape HTML
1801 sub esc_html_match_hl_chopped {
1802 my ($str, $chopped, $regexp) = @_;
1803 return esc_html_match_hl($str, $regexp) unless defined $chopped;
1805 my @matches = matchpos_list($str, $regexp);
1806 return esc_html($chopped) unless @matches;
1808 # filter matches so that we mark chopped string
1809 my $tail = "... "; # see chop_str
1810 unless ($chopped =~ s/\Q$tail\E$//) {
1813 my $chop_len = length($chopped);
1814 my $tail_len = length($tail);
1817 for my $m (@matches) {
1818 if ($m->[0] > $chop_len) {
1819 push @filtered, [ $chop_len, $chop_len + $tail_len ] if ($tail_len > 0);
1821 } elsif ($m->[1] > $chop_len) {
1822 push @filtered, [ $m->[0], $chop_len + $tail_len ];
1828 return esc_html_hl_regions($chopped . $tail, 'match', @filtered);
1831 ## ----------------------------------------------------------------------
1832 ## functions returning short strings
1834 # CSS class for given age value (in seconds)
1838 if (!defined $age) {
1840 } elsif ($age < 60*60*2) {
1842 } elsif ($age < 60*60*24*2) {
1849 # convert age in seconds to "nn units ago" string
1854 if ($age > 60*60*24*365*2) {
1855 $age_str = (int $age/60/60/24/365);
1856 $age_str .= " years ago";
1857 } elsif ($age > 60*60*24*(365/12)*2) {
1858 $age_str = int $age/60/60/24/(365/12);
1859 $age_str .= " months ago";
1860 } elsif ($age > 60*60*24*7*2) {
1861 $age_str = int $age/60/60/24/7;
1862 $age_str .= " weeks ago";
1863 } elsif ($age > 60*60*24*2) {
1864 $age_str = int $age/60/60/24;
1865 $age_str .= " days ago";
1866 } elsif ($age > 60*60*2) {
1867 $age_str = int $age/60/60;
1868 $age_str .= " hours ago";
1869 } elsif ($age > 60*2) {
1870 $age_str = int $age/60;
1871 $age_str .= " min ago";
1872 } elsif ($age > 2) {
1873 $age_str = int $age;
1874 $age_str .= " sec ago";
1876 $age_str .= " right now";
1882 S_IFINVALID => 0030000,
1883 S_IFGITLINK => 0160000,
1886 # submodule/subproject, a commit object reference
1890 return (($mode & S_IFMT) == S_IFGITLINK)
1893 # convert file mode in octal to symbolic file mode string
1895 my $mode = oct shift;
1897 if (S_ISGITLINK($mode)) {
1898 return 'm---------';
1899 } elsif (S_ISDIR($mode & S_IFMT)) {
1900 return 'drwxr-xr-x';
1901 } elsif (S_ISLNK($mode)) {
1902 return 'lrwxrwxrwx';
1903 } elsif (S_ISREG($mode)) {
1904 # git cares only about the executable bit
1905 if ($mode & S_IXUSR) {
1906 return '-rwxr-xr-x';
1908 return '-rw-r--r--';
1911 return '----------';
1915 # convert file mode in octal to file type string
1919 if ($mode !~ m/^[0-7]+$/) {
1925 if (S_ISGITLINK($mode)) {
1927 } elsif (S_ISDIR($mode & S_IFMT)) {
1929 } elsif (S_ISLNK($mode)) {
1931 } elsif (S_ISREG($mode)) {
1938 # convert file mode in octal to file type description string
1939 sub file_type_long {
1942 if ($mode !~ m/^[0-7]+$/) {
1948 if (S_ISGITLINK($mode)) {
1950 } elsif (S_ISDIR($mode & S_IFMT)) {
1952 } elsif (S_ISLNK($mode)) {
1954 } elsif (S_ISREG($mode)) {
1955 if ($mode & S_IXUSR) {
1956 return "executable";
1966 ## ----------------------------------------------------------------------
1967 ## functions returning short HTML fragments, or transforming HTML fragments
1968 ## which don't belong to other sections
1970 # format line of commit message.
1971 sub format_log_line_html {
1974 $line = esc_html($line, -nbsp=>1);
1975 $line =~ s{\b([0-9a-fA-F]{8,40})\b}{
1976 $cgi->a({-href => href(action=>"object", hash=>$1),
1977 -class => "text"}, $1);
1983 # format marker of refs pointing to given object
1985 # the destination action is chosen based on object type and current context:
1986 # - for annotated tags, we choose the tag view unless it's the current view
1987 # already, in which case we go to shortlog view
1988 # - for other refs, we keep the current view if we're in history, shortlog or
1989 # log view, and select shortlog otherwise
1990 sub format_ref_marker {
1991 my ($refs, $id) = @_;
1994 if (defined $refs->{$id}) {
1995 foreach my $ref (@{$refs->{$id}}) {
1996 # this code exploits the fact that non-lightweight tags are the
1997 # only indirect objects, and that they are the only objects for which
1998 # we want to use tag instead of shortlog as action
1999 my ($type, $name) = qw();
2000 my $indirect = ($ref =~ s/\^\{\}$//);
2001 # e.g. tags/v2.6.11 or heads/next
2002 if ($ref =~ m!^(.*?)s?/(.*)$!) {
2011 $class .= " indirect" if $indirect;
2013 my $dest_action = "shortlog";
2016 $dest_action = "tag" unless $action eq "tag";
2017 } elsif ($action =~ /^(history|(short)?log)$/) {
2018 $dest_action = $action;
2022 $dest .= "refs/" unless $ref =~ m!^refs/!;
2025 my $link = $cgi->a({
2027 action=>$dest_action,
2031 $markers .= " <span class=\"".esc_attr($class)."\" title=\"".esc_attr($ref)."\">" .
2037 return ' <span class="refs">'. $markers . '</span>';
2043 # format, perhaps shortened and with markers, title line
2044 sub format_subject_html {
2045 my ($long, $short, $href, $extra) = @_;
2046 $extra = '' unless defined($extra);
2048 if (length($short) < length($long)) {
2049 $long =~ s/[[:cntrl:]]/?/g;
2050 return $cgi->a({-href => $href, -class => "list subject",
2051 -title => to_utf8($long)},
2052 esc_html($short)) . $extra;
2054 return $cgi->a({-href => $href, -class => "list subject"},
2055 esc_html($long)) . $extra;
2059 # Rather than recomputing the url for an email multiple times, we cache it
2060 # after the first hit. This gives a visible benefit in views where the avatar
2061 # for the same email is used repeatedly (e.g. shortlog).
2062 # The cache is shared by all avatar engines (currently gravatar only), which
2063 # are free to use it as preferred. Since only one avatar engine is used for any
2064 # given page, there's no risk for cache conflicts.
2065 our %avatar_cache = ();
2067 # Compute the picon url for a given email, by using the picon search service over at
2068 # http://www.cs.indiana.edu/picons/search.html
2070 my $email = lc shift;
2071 if (!$avatar_cache{$email}) {
2072 my ($user, $domain) = split('@', $email);
2073 $avatar_cache{$email} =
2074 "//www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/" .
2076 "users+domains+unknown/up/single";
2078 return $avatar_cache{$email};
2081 # Compute the gravatar url for a given email, if it's not in the cache already.
2082 # Gravatar stores only the part of the URL before the size, since that's the
2083 # one computationally more expensive. This also allows reuse of the cache for
2084 # different sizes (for this particular engine).
2086 my $email = lc shift;
2088 $avatar_cache{$email} ||=
2089 "//www.gravatar.com/avatar/" .
2090 Digest::MD5::md5_hex($email) . "?s=";
2091 return $avatar_cache{$email} . $size;
2094 # Insert an avatar for the given $email at the given $size if the feature
2096 sub git_get_avatar {
2097 my ($email, %opts) = @_;
2098 my $pre_white = ($opts{-pad_before} ? " " : "");
2099 my $post_white = ($opts{-pad_after} ? " " : "");
2100 $opts{-size} ||= 'default';
2101 my $size = $avatar_size{$opts{-size}} || $avatar_size{'default'};
2103 if ($git_avatar eq 'gravatar') {
2104 $url = gravatar_url($email, $size);
2105 } elsif ($git_avatar eq 'picon') {
2106 $url = picon_url($email);
2108 # Other providers can be added by extending the if chain, defining $url
2109 # as needed. If no variant puts something in $url, we assume avatars
2110 # are completely disabled/unavailable.
2113 "<img width=\"$size\" " .
2114 "class=\"avatar\" " .
2115 "src=\"".esc_url($url)."\" " .
2123 sub format_search_author {
2124 my ($author, $searchtype, $displaytext) = @_;
2125 my $have_search = gitweb_check_feature('search');
2129 if ($searchtype eq 'author') {
2130 $performed = "authored";
2131 } elsif ($searchtype eq 'committer') {
2132 $performed = "committed";
2135 return $cgi->a({-href => href(action=>"search", hash=>$hash,
2136 searchtext=>$author,
2137 searchtype=>$searchtype), class=>"list",
2138 title=>"Search for commits $performed by $author"},
2142 return $displaytext;
2146 # format the author name of the given commit with the given tag
2147 # the author name is chopped and escaped according to the other
2148 # optional parameters (see chop_str).
2149 sub format_author_html {
2152 my $author = chop_and_escape_str($co->{'author_name'}, @_);
2153 return "<$tag class=\"author\">" .
2154 format_search_author($co->{'author_name'}, "author",
2155 git_get_avatar($co->{'author_email'}, -pad_after => 1) .
2160 # format git diff header line, i.e. "diff --(git|combined|cc) ..."
2161 sub format_git_diff_header_line {
2163 my $diffinfo = shift;
2164 my ($from, $to) = @_;
2166 if ($diffinfo->{'nparents'}) {
2168 $line =~ s!^(diff (.*?) )"?.*$!$1!;
2169 if ($to->{'href'}) {
2170 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
2171 esc_path($to->{'file'}));
2172 } else { # file was deleted (no href)
2173 $line .= esc_path($to->{'file'});
2177 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
2178 if ($from->{'href'}) {
2179 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
2180 'a/' . esc_path($from->{'file'}));
2181 } else { # file was added (no href)
2182 $line .= 'a/' . esc_path($from->{'file'});
2185 if ($to->{'href'}) {
2186 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
2187 'b/' . esc_path($to->{'file'}));
2188 } else { # file was deleted
2189 $line .= 'b/' . esc_path($to->{'file'});
2193 return "<div class=\"diff header\">$line</div>\n";
2196 # format extended diff header line, before patch itself
2197 sub format_extended_diff_header_line {
2199 my $diffinfo = shift;
2200 my ($from, $to) = @_;
2203 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
2204 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
2205 esc_path($from->{'file'}));
2207 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
2208 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
2209 esc_path($to->{'file'}));
2211 # match single <mode>
2212 if ($line =~ m/\s(\d{6})$/) {
2213 $line .= '<span class="info"> (' .
2214 file_type_long($1) .
2218 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
2219 # can match only for combined diff
2221 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2222 if ($from->{'href'}[$i]) {
2223 $line .= $cgi->a({-href=>$from->{'href'}[$i],
2225 substr($diffinfo->{'from_id'}[$i],0,7));
2230 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
2233 if ($to->{'href'}) {
2234 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
2235 substr($diffinfo->{'to_id'},0,7));
2240 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
2241 # can match only for ordinary diff
2242 my ($from_link, $to_link);
2243 if ($from->{'href'}) {
2244 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
2245 substr($diffinfo->{'from_id'},0,7));
2247 $from_link = '0' x 7;
2249 if ($to->{'href'}) {
2250 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
2251 substr($diffinfo->{'to_id'},0,7));
2255 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
2256 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
2259 return $line . "<br/>\n";
2262 # format from-file/to-file diff header
2263 sub format_diff_from_to_header {
2264 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
2269 #assert($line =~ m/^---/) if DEBUG;
2270 # no extra formatting for "^--- /dev/null"
2271 if (! $diffinfo->{'nparents'}) {
2272 # ordinary (single parent) diff
2273 if ($line =~ m!^--- "?a/!) {
2274 if ($from->{'href'}) {
2276 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
2277 esc_path($from->{'file'}));
2280 esc_path($from->{'file'});
2283 $result .= qq!<div class="diff from_file">$line</div>\n!;
2286 # combined diff (merge commit)
2287 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2288 if ($from->{'href'}[$i]) {
2290 $cgi->a({-href=>href(action=>"blobdiff",
2291 hash_parent=>$diffinfo->{'from_id'}[$i],
2292 hash_parent_base=>$parents[$i],
2293 file_parent=>$from->{'file'}[$i],
2294 hash=>$diffinfo->{'to_id'},
2296 file_name=>$to->{'file'}),
2298 -title=>"diff" . ($i+1)},
2301 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
2302 esc_path($from->{'file'}[$i]));
2304 $line = '--- /dev/null';
2306 $result .= qq!<div class="diff from_file">$line</div>\n!;
2311 #assert($line =~ m/^\+\+\+/) if DEBUG;
2312 # no extra formatting for "^+++ /dev/null"
2313 if ($line =~ m!^\+\+\+ "?b/!) {
2314 if ($to->{'href'}) {
2316 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
2317 esc_path($to->{'file'}));
2320 esc_path($to->{'file'});
2323 $result .= qq!<div class="diff to_file">$line</div>\n!;
2328 # create note for patch simplified by combined diff
2329 sub format_diff_cc_simplified {
2330 my ($diffinfo, @parents) = @_;
2333 $result .= "<div class=\"diff header\">" .
2335 if (!is_deleted($diffinfo)) {
2336 $result .= $cgi->a({-href => href(action=>"blob",
2338 hash=>$diffinfo->{'to_id'},
2339 file_name=>$diffinfo->{'to_file'}),
2341 esc_path($diffinfo->{'to_file'}));
2343 $result .= esc_path($diffinfo->{'to_file'});
2345 $result .= "</div>\n" . # class="diff header"
2346 "<div class=\"diff nodifferences\">" .
2348 "</div>\n"; # class="diff nodifferences"
2353 sub diff_line_class {
2354 my ($line, $from, $to) = @_;
2359 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
2360 $num_sign = scalar @{$from->{'href'}};
2363 my @diff_line_classifier = (
2364 { regexp => qr/^\@\@{$num_sign} /, class => "chunk_header"},
2365 { regexp => qr/^\\/, class => "incomplete" },
2366 { regexp => qr/^ {$num_sign}/, class => "ctx" },
2367 # classifier for context must come before classifier add/rem,
2368 # or we would have to use more complicated regexp, for example
2369 # qr/(?= {0,$m}\+)[+ ]{$num_sign}/, where $m = $num_sign - 1;
2370 { regexp => qr/^[+ ]{$num_sign}/, class => "add" },
2371 { regexp => qr/^[- ]{$num_sign}/, class => "rem" },
2373 for my $clsfy (@diff_line_classifier) {
2374 return $clsfy->{'class'}
2375 if ($line =~ $clsfy->{'regexp'});
2382 # assumes that $from and $to are defined and correctly filled,
2383 # and that $line holds a line of chunk header for unified diff
2384 sub format_unidiff_chunk_header {
2385 my ($line, $from, $to) = @_;
2387 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
2388 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
2390 $from_lines = 0 unless defined $from_lines;
2391 $to_lines = 0 unless defined $to_lines;
2393 if ($from->{'href'}) {
2394 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
2395 -class=>"list"}, $from_text);
2397 if ($to->{'href'}) {
2398 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
2399 -class=>"list"}, $to_text);
2401 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
2402 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
2406 # assumes that $from and $to are defined and correctly filled,
2407 # and that $line holds a line of chunk header for combined diff
2408 sub format_cc_diff_chunk_header {
2409 my ($line, $from, $to) = @_;
2411 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
2412 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
2414 @from_text = split(' ', $ranges);
2415 for (my $i = 0; $i < @from_text; ++$i) {
2416 ($from_start[$i], $from_nlines[$i]) =
2417 (split(',', substr($from_text[$i], 1)), 0);
2420 $to_text = pop @from_text;
2421 $to_start = pop @from_start;
2422 $to_nlines = pop @from_nlines;
2424 $line = "<span class=\"chunk_info\">$prefix ";
2425 for (my $i = 0; $i < @from_text; ++$i) {
2426 if ($from->{'href'}[$i]) {
2427 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
2428 -class=>"list"}, $from_text[$i]);
2430 $line .= $from_text[$i];
2434 if ($to->{'href'}) {
2435 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
2436 -class=>"list"}, $to_text);
2440 $line .= " $prefix</span>" .
2441 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
2445 # process patch (diff) line (not to be used for diff headers),
2446 # returning HTML-formatted (but not wrapped) line.
2447 # If the line is passed as a reference, it is treated as HTML and not
2449 sub format_diff_line {
2450 my ($line, $diff_class, $from, $to) = @_;
2456 $line = untabify($line);
2458 if ($from && $to && $line =~ m/^\@{2} /) {
2459 $line = format_unidiff_chunk_header($line, $from, $to);
2460 } elsif ($from && $to && $line =~ m/^\@{3}/) {
2461 $line = format_cc_diff_chunk_header($line, $from, $to);
2463 $line = esc_html($line, -nbsp=>1);
2467 my $diff_classes = "diff";
2468 $diff_classes .= " $diff_class" if ($diff_class);
2469 $line = "<div class=\"$diff_classes\">$line</div>\n";
2474 # Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
2475 # linked. Pass the hash of the tree/commit to snapshot.
2476 sub format_snapshot_links {
2478 my $num_fmts = @snapshot_fmts;
2479 if ($num_fmts > 1) {
2480 # A parenthesized list of links bearing format names.
2481 # e.g. "snapshot (_tar.gz_ _zip_)"
2482 return "snapshot (" . join(' ', map
2489 }, $known_snapshot_formats{$_}{'display'})
2490 , @snapshot_fmts) . ")";
2491 } elsif ($num_fmts == 1) {
2492 # A single "snapshot" link whose tooltip bears the format name.
2494 my ($fmt) = @snapshot_fmts;
2500 snapshot_format=>$fmt
2502 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
2504 } else { # $num_fmts == 0
2509 ## ......................................................................
2510 ## functions returning values to be passed, perhaps after some
2511 ## transformation, to other functions; e.g. returning arguments to href()
2513 # returns hash to be passed to href to generate gitweb URL
2514 # in -title key it returns description of link
2516 my $format = shift || 'Atom';
2517 my %res = (action => lc($format));
2519 # feed links are possible only for project views
2520 return unless (defined $project);
2521 # some views should link to OPML, or to generic project feed,
2522 # or don't have specific feed yet (so they should use generic)
2523 return if (!$action || $action =~ /^(?:tags|heads|forks|tag|search)$/x);
2526 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
2527 # from tag links; this also makes possible to detect branch links
2528 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
2529 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
2532 # find log type for feed description (title)
2534 if (defined $file_name) {
2535 $type = "history of $file_name";
2536 $type .= "/" if ($action eq 'tree');
2537 $type .= " on '$branch'" if (defined $branch);
2539 $type = "log of $branch" if (defined $branch);
2542 $res{-title} = $type;
2543 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
2544 $res{'file_name'} = $file_name;
2549 ## ----------------------------------------------------------------------
2550 ## git utility subroutines, invoking git commands
2552 # returns path to the core git executable and the --git-dir parameter as list
2554 $number_of_git_cmds++;
2555 return $GIT, '--git-dir='.$git_dir;
2558 # quote the given arguments for passing them to the shell
2559 # quote_command("command", "arg 1", "arg with ' and ! characters")
2560 # => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
2561 # Try to avoid using this function wherever possible.
2564 map { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ );
2567 # get HEAD ref of given project as hash
2568 sub git_get_head_hash {
2569 return git_get_full_hash(shift, 'HEAD');
2572 sub git_get_full_hash {
2573 return git_get_hash(@_);
2576 sub git_get_short_hash {
2577 return git_get_hash(@_, '--short=7');
2581 my ($project, $hash, @options) = @_;
2582 my $o_git_dir = $git_dir;
2584 $git_dir = "$projectroot/$project";
2585 if (open my $fd, '-|', git_cmd(), 'rev-parse',
2586 '--verify', '-q', @options, $hash) {
2588 chomp $retval if defined $retval;
2591 if (defined $o_git_dir) {
2592 $git_dir = $o_git_dir;
2597 # get type of given object
2601 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
2603 close $fd or return;
2608 # repository configuration
2609 our $config_file = '';
2612 # store multiple values for single key as anonymous array reference
2613 # single values stored directly in the hash, not as [ <value> ]
2614 sub hash_set_multi {
2615 my ($hash, $key, $value) = @_;
2617 if (!exists $hash->{$key}) {
2618 $hash->{$key} = $value;
2619 } elsif (!ref $hash->{$key}) {
2620 $hash->{$key} = [ $hash->{$key}, $value ];
2622 push @{$hash->{$key}}, $value;
2626 # return hash of git project configuration
2627 # optionally limited to some section, e.g. 'gitweb'
2628 sub git_parse_project_config {
2629 my $section_regexp = shift;
2634 open my $fh, "-|", git_cmd(), "config", '-z', '-l',
2637 while (my $keyval = <$fh>) {
2639 my ($key, $value) = split(/\n/, $keyval, 2);
2641 hash_set_multi(\%config, $key, $value)
2642 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
2649 # convert config value to boolean: 'true' or 'false'
2650 # no value, number > 0, 'true' and 'yes' values are true
2651 # rest of values are treated as false (never as error)
2652 sub config_to_bool {
2655 return 1 if !defined $val; # section.key
2657 # strip leading and trailing whitespace
2661 return (($val =~ /^\d+$/ && $val) || # section.key = 1
2662 ($val =~ /^(?:true|yes)$/i)); # section.key = true
2665 # convert config value to simple decimal number
2666 # an optional value suffix of 'k', 'm', or 'g' will cause the value
2667 # to be multiplied by 1024, 1048576, or 1073741824
2671 # strip leading and trailing whitespace
2675 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
2677 # unknown unit is treated as 1
2678 return $num * ($unit eq 'g' ? 1073741824 :
2679 $unit eq 'm' ? 1048576 :
2680 $unit eq 'k' ? 1024 : 1);
2685 # convert config value to array reference, if needed
2686 sub config_to_multi {
2689 return ref($val) ? $val : (defined($val) ? [ $val ] : []);
2692 sub git_get_project_config {
2693 my ($key, $type) = @_;
2695 return unless defined $git_dir;
2698 return unless ($key);
2699 # only subsection, if exists, is case sensitive,
2700 # and not lowercased by 'git config -z -l'
2701 if (my ($hi, $mi, $lo) = ($key =~ /^([^.]*)\.(.*)\.([^.]*)$/)) {
2703 $key = join(".", lc($hi), $mi, lc($lo));
2704 return if ($lo =~ /\W/ || $hi =~ /\W/);
2708 return if ($key =~ /\W/);
2710 $key =~ s/^gitweb\.//;
2713 if (defined $type) {
2716 unless ($type eq 'bool' || $type eq 'int');
2720 if (!defined $config_file ||
2721 $config_file ne "$git_dir/config") {
2722 %config = git_parse_project_config('gitweb');
2723 $config_file = "$git_dir/config";
2726 # check if config variable (key) exists
2727 return unless exists $config{"gitweb.$key"};
2730 if (!defined $type) {
2731 return $config{"gitweb.$key"};
2732 } elsif ($type eq 'bool') {
2733 # backward compatibility: 'git config --bool' returns true/false
2734 return config_to_bool($config{"gitweb.$key"}) ? 'true' : 'false';
2735 } elsif ($type eq 'int') {
2736 return config_to_int($config{"gitweb.$key"});
2738 return $config{"gitweb.$key"};
2741 # get hash of given path at given ref
2742 sub git_get_hash_by_path {
2744 my $path = shift || return undef;
2749 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
2750 or die_error(500, "Open git-ls-tree failed");
2752 close $fd or return undef;
2754 if (!defined $line) {
2755 # there is no tree or hash given by $path at $base
2759 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2760 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
2761 if (defined $type && $type ne $2) {
2762 # type doesn't match
2768 # get path of entry with given hash at given tree-ish (ref)
2769 # used to get 'from' filename for combined diff (merge commit) for renames
2770 sub git_get_path_by_hash {
2771 my $base = shift || return;
2772 my $hash = shift || return;
2776 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
2778 while (my $line = <$fd>) {
2781 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
2782 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
2783 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
2792 ## ......................................................................
2793 ## git utility functions, directly accessing git repository
2795 # get the value of config variable either from file named as the variable
2796 # itself in the repository ($GIT_DIR/$name file), or from gitweb.$name
2797 # configuration variable in the repository config file.
2798 sub git_get_file_or_project_config {
2799 my ($path, $name) = @_;
2801 $git_dir = "$projectroot/$path";
2802 open my $fd, '<', "$git_dir/$name"
2803 or return git_get_project_config($name);
2806 if (defined $conf) {
2812 sub git_get_project_description {
2814 return git_get_file_or_project_config($path, 'description');
2817 sub git_get_project_category {
2819 return git_get_file_or_project_config($path, 'category');
2823 # supported formats:
2824 # * $GIT_DIR/ctags/<tagname> file (in 'ctags' subdirectory)
2825 # - if its contents is a number, use it as tag weight,
2826 # - otherwise add a tag with weight 1
2827 # * $GIT_DIR/ctags file, each line is a tag (with weight 1)
2828 # the same value multiple times increases tag weight
2829 # * `gitweb.ctag' multi-valued repo config variable
2830 sub git_get_project_ctags {
2831 my $project = shift;
2834 $git_dir = "$projectroot/$project";
2835 if (opendir my $dh, "$git_dir/ctags") {
2836 my @files = grep { -f $_ } map { "$git_dir/ctags/$_" } readdir($dh);
2837 foreach my $tagfile (@files) {
2838 open my $ct, '<', $tagfile
2844 (my $ctag = $tagfile) =~ s#.*/##;
2845 if ($val =~ /^\d+$/) {
2846 $ctags->{$ctag} = $val;
2848 $ctags->{$ctag} = 1;
2853 } elsif (open my $fh, '<', "$git_dir/ctags") {
2854 while (my $line = <$fh>) {
2856 $ctags->{$line}++ if $line;
2861 my $taglist = config_to_multi(git_get_project_config('ctag'));
2862 foreach my $tag (@$taglist) {
2870 # return hash, where keys are content tags ('ctags'),
2871 # and values are sum of weights of given tag in every project
2872 sub git_gather_all_ctags {
2873 my $projects = shift;
2876 foreach my $p (@$projects) {
2877 foreach my $ct (keys %{$p->{'ctags'}}) {
2878 $ctags->{$ct} += $p->{'ctags'}->{$ct};
2885 sub git_populate_project_tagcloud {
2888 # First, merge different-cased tags; tags vote on casing
2890 foreach (keys %$ctags) {
2891 $ctags_lc{lc $_}->{count} += $ctags->{$_};
2892 if (not $ctags_lc{lc $_}->{topcount}
2893 or $ctags_lc{lc $_}->{topcount} < $ctags->{$_}) {
2894 $ctags_lc{lc $_}->{topcount} = $ctags->{$_};
2895 $ctags_lc{lc $_}->{topname} = $_;
2900 my $matched = $input_params{'ctag'};
2901 if (eval { require HTML::TagCloud; 1; }) {
2902 $cloud = HTML::TagCloud->new;
2903 foreach my $ctag (sort keys %ctags_lc) {
2904 # Pad the title with spaces so that the cloud looks
2906 my $title = esc_html($ctags_lc{$ctag}->{topname});
2907 $title =~ s/ / /g;
2908 $title =~ s/^/ /g;
2909 $title =~ s/$/ /g;
2910 if (defined $matched && $matched eq $ctag) {
2911 $title = qq(<span class="match">$title</span>);
2913 $cloud->add($title, href(project=>undef, ctag=>$ctag),
2914 $ctags_lc{$ctag}->{count});
2918 foreach my $ctag (keys %ctags_lc) {
2919 my $title = esc_html($ctags_lc{$ctag}->{topname}, -nbsp=>1);
2920 if (defined $matched && $matched eq $ctag) {
2921 $title = qq(<span class="match">$title</span>);
2923 $cloud->{$ctag}{count} = $ctags_lc{$ctag}->{count};
2924 $cloud->{$ctag}{ctag} =
2925 $cgi->a({-href=>href(project=>undef, ctag=>$ctag)}, $title);
2931 sub git_show_project_tagcloud {
2932 my ($cloud, $count) = @_;
2933 if (ref $cloud eq 'HTML::TagCloud') {
2934 return $cloud->html_and_css($count);
2936 my @tags = sort { $cloud->{$a}->{'count'} <=> $cloud->{$b}->{'count'} } keys %$cloud;
2938 '<div id="htmltagcloud"'.($project ? '' : ' align="center"').'>' .
2940 $cloud->{$_}->{'ctag'}
2941 } splice(@tags, 0, $count)) .
2946 sub git_get_project_url_list {
2949 $git_dir = "$projectroot/$path";
2950 open my $fd, '<', "$git_dir/cloneurl"
2951 or return wantarray ?
2952 @{ config_to_multi(git_get_project_config('url')) } :
2953 config_to_multi(git_get_project_config('url'));
2954 my @git_project_url_list = map { chomp; $_ } <$fd>;
2957 return wantarray ? @git_project_url_list : \@git_project_url_list;
2960 sub git_get_projects_list {
2961 my $filter = shift || '';
2962 my $paranoid = shift;
2965 if (-d $projects_list) {
2966 # search in directory
2967 my $dir = $projects_list;
2968 # remove the trailing "/"
2970 my $pfxlen = length("$dir");
2971 my $pfxdepth = ($dir =~ tr!/!!);
2972 # when filtering, search only given subdirectory
2973 if ($filter && !$paranoid) {
2979 follow_fast => 1, # follow symbolic links
2980 follow_skip => 2, # ignore duplicates
2981 dangling_symlinks => 0, # ignore dangling symlinks, silently
2984 our $project_maxdepth;
2986 # skip project-list toplevel, if we get it.
2987 return if (m!^[/.]$!);
2988 # only directories can be git repositories
2989 return unless (-d $_);
2990 # don't traverse too deep (Find is super slow on os x)
2991 # $project_maxdepth excludes depth of $projectroot
2992 if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
2993 $File::Find::prune = 1;
2997 my $path = substr($File::Find::name, $pfxlen + 1);
2998 # paranoidly only filter here
2999 if ($paranoid && $filter && $path !~ m!^\Q$filter\E/!) {
3002 # we check related file in $projectroot
3003 if (check_export_ok("$projectroot/$path")) {
3004 push @list, { path => $path };
3005 $File::Find::prune = 1;
3010 } elsif (-f $projects_list) {
3011 # read from file(url-encoded):
3012 # 'git%2Fgit.git Linus+Torvalds'
3013 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
3014 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
3015 open my $fd, '<', $projects_list or return;
3017 while (my $line = <$fd>) {
3019 my ($path, $owner) = split ' ', $line;
3020 $path = unescape($path);
3021 $owner = unescape($owner);
3022 if (!defined $path) {
3025 # if $filter is rpovided, check if $path begins with $filter
3026 if ($filter && $path !~ m!^\Q$filter\E/!) {
3029 if (check_export_ok("$projectroot/$path")) {
3034 $pr->{'owner'} = to_utf8($owner);
3044 # written with help of Tree::Trie module (Perl Artistic License, GPL compatibile)
3045 # as side effects it sets 'forks' field to list of forks for forked projects
3046 sub filter_forks_from_projects_list {
3047 my $projects = shift;
3049 my %trie; # prefix tree of directories (path components)
3050 # generate trie out of those directories that might contain forks
3051 foreach my $pr (@$projects) {
3052 my $path = $pr->{'path'};
3053 $path =~ s/\.git$//; # forks of 'repo.git' are in 'repo/' directory
3054 next if ($path =~ m!/$!); # skip non-bare repositories, e.g. 'repo/.git'
3055 next unless ($path); # skip '.git' repository: tests, git-instaweb
3056 next unless (-d "$projectroot/$path"); # containing directory exists
3057 $pr->{'forks'} = []; # there can be 0 or more forks of project
3060 my @dirs = split('/', $path);
3061 # walk the trie, until either runs out of components or out of trie
3063 while (scalar @dirs &&
3064 exists($ref->{$dirs[0]})) {
3065 $ref = $ref->{shift @dirs};
3067 # create rest of trie structure from rest of components
3068 foreach my $dir (@dirs) {
3069 $ref = $ref->{$dir} = {};
3071 # create end marker, store $pr as a data
3072 $ref->{''} = $pr if (!exists $ref->{''});
3075 # filter out forks, by finding shortest prefix match for paths
3078 foreach my $pr (@$projects) {
3082 foreach my $dir (split('/', $pr->{'path'})) {
3083 if (exists $ref->{''}) {
3084 # found [shortest] prefix, is a fork - skip it
3085 push @{$ref->{''}{'forks'}}, $pr;
3088 if (!exists $ref->{$dir}) {
3089 # not in trie, cannot have prefix, not a fork
3090 push @filtered, $pr;
3093 # If the dir is there, we just walk one step down the trie.
3094 $ref = $ref->{$dir};
3096 # we ran out of trie
3097 # (shouldn't happen: it's either no match, or end marker)
3098 push @filtered, $pr;
3104 # note: fill_project_list_info must be run first,
3105 # for 'descr_long' and 'ctags' to be filled
3106 sub search_projects_list {
3107 my ($projlist, %opts) = @_;
3108 my $tagfilter = $opts{'tagfilter'};
3109 my $search_re = $opts{'search_regexp'};
3112 unless ($tagfilter || $search_re);
3114 # searching projects require filling to be run before it;
3115 fill_project_list_info($projlist,
3116 $tagfilter ? 'ctags' : (),
3117 $search_re ? ('path', 'descr') : ());
3120 foreach my $pr (@$projlist) {
3123 next unless ref($pr->{'ctags'}) eq 'HASH';
3125 grep { lc($_) eq lc($tagfilter) } keys %{$pr->{'ctags'}};
3130 $pr->{'path'} =~ /$search_re/ ||
3131 $pr->{'descr_long'} =~ /$search_re/;
3134 push @projects, $pr;
3140 our $gitweb_project_owner = undef;
3141 sub git_get_project_list_from_file {
3143 return if (defined $gitweb_project_owner);
3145 $gitweb_project_owner = {};
3146 # read from file (url-encoded):
3147 # 'git%2Fgit.git Linus+Torvalds'
3148 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
3149 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
3150 if (-f $projects_list) {
3151 open(my $fd, '<', $projects_list);
3152 while (my $line = <$fd>) {
3154 my ($pr, $ow) = split ' ', $line;
3155 $pr = unescape($pr);
3156 $ow = unescape($ow);
3157 $gitweb_project_owner->{$pr} = to_utf8($ow);
3163 sub git_get_project_owner {
3164 my $project = shift;
3167 return undef unless $project;
3168 $git_dir = "$projectroot/$project";
3170 if (!defined $gitweb_project_owner) {
3171 git_get_project_list_from_file();
3174 if (exists $gitweb_project_owner->{$project}) {
3175 $owner = $gitweb_project_owner->{$project};
3177 if (!defined $owner){
3178 $owner = git_get_project_config('owner');
3180 if (!defined $owner) {
3181 $owner = get_file_owner("$git_dir");
3187 sub git_get_last_activity {
3191 $git_dir = "$projectroot/$path";
3192 open($fd, "-|", git_cmd(), 'for-each-ref',
3193 '--format=%(committer)',
3194 '--sort=-committerdate',
3196 'refs/heads') or return;
3197 my $most_recent = <$fd>;
3198 close $fd or return;
3199 if (defined $most_recent &&
3200 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
3202 my $age = time - $timestamp;
3203 return ($age, age_string($age));
3205 return (undef, undef);
3208 # Implementation note: when a single remote is wanted, we cannot use 'git
3209 # remote show -n' because that command always work (assuming it's a remote URL
3210 # if it's not defined), and we cannot use 'git remote show' because that would
3211 # try to make a network roundtrip. So the only way to find if that particular
3212 # remote is defined is to walk the list provided by 'git remote -v' and stop if
3213 # and when we find what we want.
3214 sub git_get_remotes_list {
3218 open my $fd, '-|' , git_cmd(), 'remote', '-v';
3220 while (my $remote = <$fd>) {
3222 $remote =~ s!\t(.*?)\s+\((\w+)\)$!!;
3223 next if $wanted and not $remote eq $wanted;
3224 my ($url, $key) = ($1, $2);
3226 $remotes{$remote} ||= { 'heads' => () };
3227 $remotes{$remote}{$key} = $url;
3229 close $fd or return;
3230 return wantarray ? %remotes : \%remotes;
3233 # Takes a hash of remotes as first parameter and fills it by adding the
3234 # available remote heads for each of the indicated remotes.
3235 sub fill_remote_heads {
3236 my $remotes = shift;
3237 my @heads = map { "remotes/$_" } keys %$remotes;
3238 my @remoteheads = git_get_heads_list(undef, @heads);
3239 foreach my $remote (keys %$remotes) {
3240 $remotes->{$remote}{'heads'} = [ grep {
3241 $_->{'name'} =~ s!^$remote/!!
3246 sub git_get_references {
3247 my $type = shift || "";
3249 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
3250 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
3251 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
3252 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
3255 while (my $line = <$fd>) {
3257 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {
3258 if (defined $refs{$1}) {
3259 push @{$refs{$1}}, $2;
3265 close $fd or return;
3269 sub git_get_rev_name_tags {
3270 my $hash = shift || return undef;
3272 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
3274 my $name_rev = <$fd>;
3277 if ($name_rev =~ m|^$hash tags/(.*)$|) {
3280 # catches also '$hash undefined' output
3285 ## ----------------------------------------------------------------------
3286 ## parse to hash functions
3290 my $tz = shift || "-0000";
3293 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
3294 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
3295 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
3296 $date{'hour'} = $hour;
3297 $date{'minute'} = $min;
3298 $date{'mday'} = $mday;
3299 $date{'day'} = $days[$wday];
3300 $date{'month'} = $months[$mon];
3301 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
3302 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
3303 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
3304 $mday, $months[$mon], $hour ,$min;
3305 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
3306 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
3308 my ($tz_sign, $tz_hour, $tz_min) =
3309 ($tz =~ m/^([-+])(\d\d)(\d\d)$/);
3310 $tz_sign = ($tz_sign eq '-' ? -1 : +1);
3311 my $local = $epoch + $tz_sign*((($tz_hour*60) + $tz_min)*60);
3312 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
3313 $date{'hour_local'} = $hour;
3314 $date{'minute_local'} = $min;
3315 $date{'tz_local'} = $tz;
3316 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
3317 1900+$year, $mon+1, $mday,
3318 $hour, $min, $sec, $tz);
3327 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
3328 $tag{'id'} = $tag_id;
3329 while (my $line = <$fd>) {
3331 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
3332 $tag{'object'} = $1;
3333 } elsif ($line =~ m/^type (.+)$/) {
3335 } elsif ($line =~ m/^tag (.+)$/) {
3337 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
3338 $tag{'author'} = $1;
3339 $tag{'author_epoch'} = $2;
3340 $tag{'author_tz'} = $3;
3341 if ($tag{'author'} =~ m/^([^<]+) <([^>]*)>/) {
3342 $tag{'author_name'} = $1;
3343 $tag{'author_email'} = $2;
3345 $tag{'author_name'} = $tag{'author'};
3347 } elsif ($line =~ m/--BEGIN/) {
3348 push @comment, $line;
3350 } elsif ($line eq "") {
3354 push @comment, <$fd>;
3355 $tag{'comment'} = \@comment;
3356 close $fd or return;
3357 if (!defined $tag{'name'}) {
3363 sub parse_commit_text {
3364 my ($commit_text, $withparents) = @_;
3365 my @commit_lines = split '\n', $commit_text;
3368 pop @commit_lines; # Remove '\0'
3370 if (! @commit_lines) {
3374 my $header = shift @commit_lines;
3375 if ($header !~ m/^[0-9a-fA-F]{40}/) {
3378 ($co{'id'}, my @parents) = split ' ', $header;
3379 while (my $line = shift @commit_lines) {
3380 last if $line eq "\n";
3381 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
3383 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
3385 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
3386 $co{'author'} = to_utf8($1);
3387 $co{'author_epoch'} = $2;
3388 $co{'author_tz'} = $3;
3389 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
3390 $co{'author_name'} = $1;
3391 $co{'author_email'} = $2;
3393 $co{'author_name'} = $co{'author'};
3395 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
3396 $co{'committer'} = to_utf8($1);
3397 $co{'committer_epoch'} = $2;
3398 $co{'committer_tz'} = $3;
3399 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
3400 $co{'committer_name'} = $1;
3401 $co{'committer_email'} = $2;
3403 $co{'committer_name'} = $co{'committer'};
3407 if (!defined $co{'tree'}) {
3410 $co{'parents'} = \@parents;
3411 $co{'parent'} = $parents[0];
3413 foreach my $title (@commit_lines) {
3416 $co{'title'} = chop_str($title, 80, 5);
3417 # remove leading stuff of merges to make the interesting part visible
3418 if (length($title) > 50) {
3419 $title =~ s/^Automatic //;
3420 $title =~ s/^merge (of|with) /Merge ... /i;
3421 if (length($title) > 50) {
3422 $title =~ s/(http|rsync):\/\///;
3424 if (length($title) > 50) {
3425 $title =~ s/(master|www|rsync)\.//;
3427 if (length($title) > 50) {
3428 $title =~ s/kernel.org:?//;
3430 if (length($title) > 50) {
3431 $title =~ s/\/pub\/scm//;
3434 $co{'title_short'} = chop_str($title, 50, 5);
3438 if (! defined $co{'title'} || $co{'title'} eq "") {
3439 $co{'title'} = $co{'title_short'} = '(no commit message)';
3441 # remove added spaces
3442 foreach my $line (@commit_lines) {
3445 $co{'comment'} = \@commit_lines;
3447 my $age = time - $co{'committer_epoch'};
3449 $co{'age_string'} = age_string($age);
3450 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
3451 if ($age > 60*60*24*7*2) {
3452 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
3453 $co{'age_string_age'} = $co{'age_string'};
3455 $co{'age_string_date'} = $co{'age_string'};
3456 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
3462 my ($commit_id) = @_;
3467 open my $fd, "-|", git_cmd(), "rev-list",
3473 or die_error(500, "Open git-rev-list failed");
3474 %co = parse_commit_text(<$fd>, 1);
3481 my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
3489 open my $fd, "-|", git_cmd(), "rev-list",
3492 ("--max-count=" . $maxcount),
3493 ("--skip=" . $skip),
3497 ($filename ? ($filename) : ())
3498 or die_error(500, "Open git-rev-list failed");
3499 while (my $line = <$fd>) {
3500 my %co = parse_commit_text($line);
3505 return wantarray ? @cos : \@cos;
3508 # parse line of git-diff-tree "raw" output
3509 sub parse_difftree_raw_line {
3513 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
3514 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
3515 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
3516 $res{'from_mode'} = $1;
3517 $res{'to_mode'} = $2;
3518 $res{'from_id'} = $3;
3520 $res{'status'} = $5;
3521 $res{'similarity'} = $6;
3522 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
3523 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
3525 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote($7);
3528 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
3529 # combined diff (for merge commit)
3530 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
3531 $res{'nparents'} = length($1);
3532 $res{'from_mode'} = [ split(' ', $2) ];
3533 $res{'to_mode'} = pop @{$res{'from_mode'}};
3534 $res{'from_id'} = [ split(' ', $3) ];
3535 $res{'to_id'} = pop @{$res{'from_id'}};
3536 $res{'status'} = [ split('', $4) ];
3537 $res{'to_file'} = unquote($5);
3539 # 'c512b523472485aef4fff9e57b229d9d243c967f'
3540 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
3541 $res{'commit'} = $1;
3544 return wantarray ? %res : \%res;
3547 # wrapper: return parsed line of git-diff-tree "raw" output
3548 # (the argument might be raw line, or parsed info)
3549 sub parsed_difftree_line {
3550 my $line_or_ref = shift;
3552 if (ref($line_or_ref) eq "HASH") {
3553 # pre-parsed (or generated by hand)
3554 return $line_or_ref;
3556 return parse_difftree_raw_line($line_or_ref);
3560 # parse line of git-ls-tree output
3561 sub parse_ls_tree_line {
3567 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'
3568 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;
3577 $res{'name'} = unquote($5);
3580 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
3581 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
3589 $res{'name'} = unquote($4);
3593 return wantarray ? %res : \%res;
3596 # generates _two_ hashes, references to which are passed as 2 and 3 argument
3597 sub parse_from_to_diffinfo {
3598 my ($diffinfo, $from, $to, @parents) = @_;
3600 if ($diffinfo->{'nparents'}) {
3602 $from->{'file'} = [];
3603 $from->{'href'} = [];
3604 fill_from_file_info($diffinfo, @parents)
3605 unless exists $diffinfo->{'from_file'};
3606 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
3607 $from->{'file'}[$i] =
3608 defined $diffinfo->{'from_file'}[$i] ?
3609 $diffinfo->{'from_file'}[$i] :
3610 $diffinfo->{'to_file'};
3611 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
3612 $from->{'href'}[$i] = href(action=>"blob",
3613 hash_base=>$parents[$i],
3614 hash=>$diffinfo->{'from_id'}[$i],
3615 file_name=>$from->{'file'}[$i]);
3617 $from->{'href'}[$i] = undef;
3621 # ordinary (not combined) diff
3622 $from->{'file'} = $diffinfo->{'from_file'};
3623 if ($diffinfo->{'status'} ne "A") { # not new (added) file
3624 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
3625 hash=>$diffinfo->{'from_id'},
3626 file_name=>$from->{'file'});
3628 delete $from->{'href'};
3632 $to->{'file'} = $diffinfo->{'to_file'};
3633 if (!is_deleted($diffinfo)) { # file exists in result
3634 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
3635 hash=>$diffinfo->{'to_id'},
3636 file_name=>$to->{'file'});
3638 delete $to->{'href'};
3642 ## ......................................................................
3643 ## parse to array of hashes functions
3645 sub git_get_heads_list {
3646 my ($limit, @classes) = @_;
3647 @classes = ('heads') unless @classes;
3648 my @patterns = map { "refs/$_" } @classes;
3651 open my $fd, '-|', git_cmd(), 'for-each-ref',
3652 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
3653 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
3656 while (my $line = <$fd>) {
3660 my ($refinfo, $committerinfo) = split(/\0/, $line);
3661 my ($hash, $name, $title) = split(' ', $refinfo, 3);
3662 my ($committer, $epoch, $tz) =
3663 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
3664 $ref_item{'fullname'} = $name;
3665 $name =~ s!^refs/(?:head|remote)s/!!;
3667 $ref_item{'name'} = $name;
3668 $ref_item{'id'} = $hash;
3669 $ref_item{'title'} = $title || '(no commit message)';
3670 $ref_item{'epoch'} = $epoch;
3672 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
3674 $ref_item{'age'} = "unknown";
3677 push @headslist, \%ref_item;
3681 return wantarray ? @headslist : \@headslist;
3684 sub git_get_tags_list {
3688 open my $fd, '-|', git_cmd(), 'for-each-ref',
3689 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
3690 '--format=%(objectname) %(objecttype) %(refname) '.
3691 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
3694 while (my $line = <$fd>) {
3698 my ($refinfo, $creatorinfo) = split(/\0/, $line);
3699 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
3700 my ($creator, $epoch, $tz) =
3701 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
3702 $ref_item{'fullname'} = $name;
3703 $name =~ s!^refs/tags/!!;
3705 $ref_item{'type'} = $type;
3706 $ref_item{'id'} = $id;
3707 $ref_item{'name'} = $name;
3708 if ($type eq "tag") {
3709 $ref_item{'subject'} = $title;
3710 $ref_item{'reftype'} = $reftype;
3711 $ref_item{'refid'} = $refid;
3713 $ref_item{'reftype'} = $type;
3714 $ref_item{'refid'} = $id;
3717 if ($type eq "tag" || $type eq "commit") {
3718 $ref_item{'epoch'} = $epoch;
3720 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
3722 $ref_item{'age'} = "unknown";
3726 push @tagslist, \%ref_item;
3730 return wantarray ? @tagslist : \@tagslist;
3733 ## ----------------------------------------------------------------------
3734 ## filesystem-related functions
3736 sub get_file_owner {
3739 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
3740 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
3741 if (!defined $gcos) {
3745 $owner =~ s/[,;].*$//;
3746 return to_utf8($owner);
3749 # assume that file exists
3751 my $filename = shift;
3753 open my $fd, '<', $filename;
3754 print map { to_utf8($_) } <$fd>;
3758 ## ......................................................................
3759 ## mimetype related functions
3761 sub mimetype_guess_file {
3762 my $filename = shift;
3763 my $mimemap = shift;
3764 -r $mimemap or return undef;
3767 open(my $mh, '<', $mimemap) or return undef;
3769 next if m/^#/; # skip comments
3770 my ($mimetype, @exts) = split(/\s+/);
3771 foreach my $ext (@exts) {
3772 $mimemap{$ext} = $mimetype;
3777 $filename =~ /\.([^.]*)$/;
3778 return $mimemap{$1};
3781 sub mimetype_guess {
3782 my $filename = shift;
3784 $filename =~ /\./ or return undef;
3786 if ($mimetypes_file) {
3787 my $file = $mimetypes_file;
3788 if ($file !~ m!^/!) { # if it is relative path
3789 # it is relative to project
3790 $file = "$projectroot/$project/$file";
3792 $mime = mimetype_guess_file($filename, $file);
3794 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
3800 my $filename = shift;
3803 my $mime = mimetype_guess($filename);
3804 $mime and return $mime;
3808 return $default_blob_plain_mimetype unless $fd;
3811 return 'text/plain';
3812 } elsif (! $filename) {
3813 return 'application/octet-stream';
3814 } elsif ($filename =~ m/\.png$/i) {
3816 } elsif ($filename =~ m/\.gif$/i) {
3818 } elsif ($filename =~ m/\.jpe?g$/i) {
3819 return 'image/jpeg';
3821 return 'application/octet-stream';
3825 sub blob_contenttype {
3826 my ($fd, $file_name, $type) = @_;
3828 $type ||= blob_mimetype($fd, $file_name);
3829 if ($type eq 'text/plain' && defined $default_text_plain_charset) {
3830 $type .= "; charset=$default_text_plain_charset";
3836 # guess file syntax for syntax highlighting; return undef if no highlighting
3837 # the name of syntax can (in the future) depend on syntax highlighter used
3838 sub guess_file_syntax {
3839 my ($highlight, $mimetype, $file_name) = @_;
3840 return undef unless ($highlight && defined $file_name);
3841 my $basename = basename($file_name, '.in');
3842 return $highlight_basename{$basename}
3843 if exists $highlight_basename{$basename};
3845 $basename =~ /\.([^.]*)$/;
3846 my $ext = $1 or return undef;
3847 return $highlight_ext{$ext}
3848 if exists $highlight_ext{$ext};
3853 # run highlighter and return FD of its output,
3854 # or return original FD if no highlighting
3855 sub run_highlighter {
3856 my ($fd, $highlight, $syntax) = @_;
3857 return $fd unless ($highlight && defined $syntax);
3860 open $fd, quote_command(git_cmd(), "cat-file", "blob", $hash)." | ".
3861 quote_command($highlight_bin).
3862 " --replace-tabs=8 --fragment --syntax $syntax |"
3863 or die_error(500, "Couldn't open file or run syntax highlighter");
3867 ## ======================================================================
3868 ## functions printing HTML: header, footer, error page
3870 sub get_page_title {
3871 my $title = to_utf8($site_name);
3873 unless (defined $project) {
3874 if (defined $project_filter) {
3875 $title .= " - projects in '" . esc_path($project_filter) . "'";
3879 $title .= " - " . to_utf8($project);
3881 return $title unless (defined $action);
3882 $title .= "/$action"; # $action is US-ASCII (7bit ASCII)
3884 return $title unless (defined $file_name);
3885 $title .= " - " . esc_path($file_name);
3886 if ($action eq "tree" && $file_name !~ m|/$|) {
3893 sub get_content_type_html {
3894 # require explicit support from the UA if we are to send the page as
3895 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
3896 # we have to do this because MSIE sometimes globs '*/*', pretending to
3897 # support xhtml+xml but choking when it gets what it asked for.
3898 if (defined $cgi->http('HTTP_ACCEPT') &&
3899 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
3900 $cgi->Accept('application/xhtml+xml') != 0) {
3901 return 'application/xhtml+xml';
3907 sub print_feed_meta {
3908 if (defined $project) {
3909 my %href_params = get_feed_info();
3910 if (!exists $href_params{'-title'}) {
3911 $href_params{'-title'} = 'log';
3914 foreach my $format (qw(RSS Atom)) {
3915 my $type = lc($format);
3917 '-rel' => 'alternate',
3918 '-title' => esc_attr("$project - $href_params{'-title'} - $format feed"),
3919 '-type' => "application/$type+xml"
3922 $href_params{'extra_options'} = undef;
3923 $href_params{'action'} = $type;
3924 $link_attr{'-href'} = href(%href_params);
3926 "rel=\"$link_attr{'-rel'}\" ".
3927 "title=\"$link_attr{'-title'}\" ".
3928 "href=\"$link_attr{'-href'}\" ".
3929 "type=\"$link_attr{'-type'}\" ".
3932 $href_params{'extra_options'} = '--no-merges';
3933 $link_attr{'-href'} = href(%href_params);
3934 $link_attr{'-title'} .= ' (no merges)';
3936 "rel=\"$link_attr{'-rel'}\" ".
3937 "title=\"$link_attr{'-title'}\" ".
3938 "href=\"$link_attr{'-href'}\" ".
3939 "type=\"$link_attr{'-type'}\" ".
3944 printf('<link rel="alternate" title="%s projects list" '.
3945 'href="%s" type="text/plain; charset=utf-8" />'."\n",
3946 esc_attr($site_name), href(project=>undef, action=>"project_index"));
3947 printf('<link rel="alternate" title="%s projects feeds" '.
3948 'href="%s" type="text/x-opml" />'."\n",
3949 esc_attr($site_name), href(project=>undef, action=>"opml"));
3953 sub print_header_links {
3956 # print out each stylesheet that exist, providing backwards capability
3957 # for those people who defined $stylesheet in a config file
3958 if (defined $stylesheet) {
3959 print '<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";
3961 foreach my $stylesheet (@stylesheets) {
3962 next unless $stylesheet;
3963 print '<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";
3967 if ($status eq '200 OK');
3968 if (defined $favicon) {
3969 print qq(<link rel="shortcut icon" href=").esc_url($favicon).qq(" type="image/png" />\n);
3973 sub print_nav_breadcrumbs_path {
3974 my $dirprefix = undef;
3975 while (my $part = shift) {
3976 $dirprefix .= "/" if defined $dirprefix;
3977 $dirprefix .= $part;
3978 print $cgi->a({-href => href(project => undef,
3979 project_filter => $dirprefix,
3980 action => "project_list")},
3981 esc_html($part)) . " / ";
3985 sub print_nav_breadcrumbs {
3988 for my $crumb (@extra_breadcrumbs, [ $home_link_str => $home_link ]) {
3989 print $cgi->a({-href => esc_url($crumb->[1])}, $crumb->[0]) . " / ";
3991 if (defined $project) {
3992 my @dirname = split '/', $project;
3993 my $projectbasename = pop @dirname;
3994 print_nav_breadcrumbs_path(@dirname);
3995 print $cgi->a({-href => href(action=>"summary")}, esc_html($projectbasename));
3996 if (defined $action) {
3997 my $action_print = $action ;
3998 if (defined $opts{-action_extra}) {
3999 $action_print = $cgi->a({-href => href(action=>$action)},
4002 print " / $action_print";
4004 if (defined $opts{-action_extra}) {
4005 print " / $opts{-action_extra}";
4008 } elsif (defined $project_filter) {
4009 print_nav_breadcrumbs_path(split '/', $project_filter);
4013 sub print_search_form {
4014 if (!defined $searchtext) {
4018 if (defined $hash_base) {
4019 $search_hash = $hash_base;
4020 } elsif (defined $hash) {
4021 $search_hash = $hash;
4023 $search_hash = "HEAD";
4025 my $action = $my_uri;
4026 my $use_pathinfo = gitweb_check_feature('pathinfo');
4027 if ($use_pathinfo) {
4028 $action .= "/".esc_url($project);
4030 print $cgi->startform(-method => "get", -action => $action) .
4031 "<div class=\"search\">\n" .
4033 $cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) . "\n") .
4034 $cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) . "\n" .
4035 $cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) . "\n" .
4036 $cgi->popup_menu(-name => 'st', -default => 'commit',
4037 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
4038 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
4040 $cgi->textfield(-name => "s", -value => $searchtext, -override => 1) . "\n" .
4041 "<span title=\"Extended regular expression\">" .
4042 $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
4043 -checked => $search_use_regexp) .
4046 $cgi->end_form() . "\n";
4049 sub git_header_html {
4050 my $status = shift || "200 OK";
4051 my $expires = shift;
4054 my $title = get_page_title();
4055 my $content_type = get_content_type_html();
4056 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
4057 -status=> $status, -expires => $expires)
4058 unless ($opts{'-no_http_header'});
4059 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
4061 <?xml version="1.0" encoding="utf-8"?>
4062 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
4063 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
4064 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
4065 <!-- git core binaries version $git_version -->
4067 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
4068 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
4069 <meta name="robots" content="index, nofollow"/>
4070 <title>$title</title>
4072 # the stylesheet, favicon etc urls won't work correctly with path_info
4073 # unless we set the appropriate base URL
4074 if ($ENV{'PATH_INFO'}) {
4075 print "<base href=\"".esc_url($base_url)."\" />\n";
4077 print_header_links($status);
4079 if (defined $site_html_head_string) {
4080 print to_utf8($site_html_head_string);
4086 if (defined $site_header && -f $site_header) {
4087 insert_file($site_header);
4090 print "<div class=\"page_header\">\n";
4091 if (defined $logo) {
4092 print $cgi->a({-href => esc_url($logo_url),
4093 -title => $logo_label},
4094 $cgi->img({-src => esc_url($logo),
4095 -width => 72, -height => 27,
4097 -class => "logo"}));
4099 print_nav_breadcrumbs(%opts);
4102 my $have_search = gitweb_check_feature('search');
4103 if (defined $project && $have_search) {
4104 print_search_form();
4108 sub git_footer_html {
4109 my $feed_class = 'rss_logo';
4111 print "<div class=\"page_footer\">\n";
4112 if (defined $project) {
4113 my $descr = git_get_project_description($project);
4114 if (defined $descr) {
4115 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
4118 my %href_params = get_feed_info();
4119 if (!%href_params) {
4120 $feed_class .= ' generic';
4122 $href_params{'-title'} ||= 'log';
4124 foreach my $format (qw(RSS Atom)) {
4125 $href_params{'action'} = lc($format);
4126 print $cgi->a({-href => href(%href_params),
4127 -title => "$href_params{'-title'} $format feed",
4128 -class => $feed_class}, $format)."\n";
4132 print $cgi->a({-href => href(project=>undef, action=>"opml",
4133 project_filter => $project_filter),
4134 -class => $feed_class}, "OPML") . " ";
4135 print $cgi->a({-href => href(project=>undef, action=>"project_index",
4136 project_filter => $project_filter),
4137 -class => $feed_class}, "TXT") . "\n";
4139 print "</div>\n"; # class="page_footer"
4141 if (defined $t0 && gitweb_check_feature('timed')) {
4142 print "<div id=\"generating_info\">\n";
4143 print 'This page took '.
4144 '<span id="generating_time" class="time_span">'.
4145 tv_interval($t0, [ gettimeofday() ]).
4148 '<span id="generating_cmd">'.
4149 $number_of_git_cmds.
4150 '</span> git commands '.
4152 print "</div>\n"; # class="page_footer"
4155 if (defined $site_footer && -f $site_footer) {
4156 insert_file($site_footer);
4159 print qq!<script type="text/javascript" src="!.esc_url($javascript).qq!"></script>\n!;
4160 if (defined $action &&
4161 $action eq 'blame_incremental') {
4162 print qq!<script type="text/javascript">\n!.
4163 qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.
4164 qq! "!. href() .qq!");\n!.
4167 my ($jstimezone, $tz_cookie, $datetime_class) =
4168 gitweb_get_feature('javascript-timezone');
4170 print qq!<script type="text/javascript">\n!.
4171 qq!window.onload = function () {\n!;
4172 if (gitweb_check_feature('javascript-actions')) {
4173 print qq! fixLinks();\n!;
4175 if ($jstimezone && $tz_cookie && $datetime_class) {
4176 print qq! var tz_cookie = { name: '$tz_cookie', expires: 14, path: '/' };\n!. # in days
4177 qq! onloadTZSetup('$jstimezone', tz_cookie, '$datetime_class');\n!;
4187 # die_error(<http_status_code>, <error_message>[, <detailed_html_description>])
4188 # Example: die_error(404, 'Hash not found')
4189 # By convention, use the following status codes (as defined in RFC 2616):
4190 # 400: Invalid or missing CGI parameters, or
4191 # requested object exists but has wrong type.
4192 # 403: Requested feature (like "pickaxe" or "snapshot") not enabled on
4193 # this server or project.
4194 # 404: Requested object/revision/project doesn't exist.
4195 # 500: The server isn't configured properly, or
4196 # an internal error occurred (e.g. failed assertions caused by bugs), or
4197 # an unknown error occurred (e.g. the git binary died unexpectedly).
4198 # 503: The server is currently unavailable (because it is overloaded,
4199 # or down for maintenance). Generally, this is a temporary state.
4201 my $status = shift || 500;
4202 my $error = esc_html(shift) || "Internal Server Error";
4206 my %http_responses = (
4207 400 => '400 Bad Request',
4208 403 => '403 Forbidden',
4209 404 => '404 Not Found',
4210 500 => '500 Internal Server Error',
4211 503 => '503 Service Unavailable',
4213 git_header_html($http_responses{$status}, undef, %opts);
4215 <div class="page_body">
4220 if (defined $extra) {
4228 unless ($opts{'-error_handler'});
4231 ## ----------------------------------------------------------------------
4232 ## functions printing or outputting HTML: navigation
4234 sub git_print_page_nav {
4235 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
4236 $extra = '' if !defined $extra; # pager or formats
4238 my @navs = qw(summary shortlog log commit commitdiff tree);
4240 @navs = grep { $_ ne $suppress } @navs;
4243 my %arg = map { $_ => {action=>$_} } @navs;
4244 if (defined $head) {
4245 for (qw(commit commitdiff)) {
4246 $arg{$_}{'hash'} = $head;
4248 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
4249 for (qw(shortlog log)) {
4250 $arg{$_}{'hash'} = $head;
4255 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
4256 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
4258 my @actions = gitweb_get_feature('actions');
4261 'n' => $project, # project name
4262 'f' => $git_dir, # project path within filesystem
4263 'h' => $treehead || '', # current hash ('h' parameter)
4264 'b' => $treebase || '', # hash base ('hb' parameter)
4267 my ($label, $link, $pos) = splice(@actions,0,3);
4269 @navs = map { $_ eq $pos ? ($_, $label) : $_ } @navs;
4271 $link =~ s/%([%nfhb])/$repl{$1}/g;
4272 $arg{$label}{'_href'} = $link;
4275 print "<div class=\"page_nav\">\n" .
4277 map { $_ eq $current ?
4278 $_ : $cgi->a({-href => ($arg{$_}{_href} ? $arg{$_}{_href} : href(%{$arg{$_}}))}, "$_")
4280 print "<br/>\n$extra<br/>\n" .
4284 # returns a submenu for the nagivation of the refs views (tags, heads,
4285 # remotes) with the current view disabled and the remotes view only
4286 # available if the feature is enabled
4287 sub format_ref_views {
4289 my @ref_views = qw{tags heads};
4290 push @ref_views, 'remotes' if gitweb_check_feature('remote_heads');
4291 return join " | ", map {
4292 $_ eq $current ? $_ :
4293 $cgi->a({-href => href(action=>$_)}, $_)
4297 sub format_paging_nav {
4298 my ($action, $page, $has_next_link) = @_;
4304 $cgi->a({-href => href(-replay=>1, page=>undef)}, "first") .
4306 $cgi->a({-href => href(-replay=>1, page=>$page-1),
4307 -accesskey => "p", -title => "Alt-p"}, "prev");
4309 $paging_nav .= "first ⋅ prev";
4312 if ($has_next_link) {
4313 $paging_nav .= " ⋅ " .
4314 $cgi->a({-href => href(-replay=>1, page=>$page+1),
4315 -accesskey => "n", -title => "Alt-n"}, "next");
4317 $paging_nav .= " ⋅ next";
4323 ## ......................................................................
4324 ## functions printing or outputting HTML: div
4326 sub git_print_header_div {
4327 my ($action, $title, $hash, $hash_base) = @_;
4330 $args{'action'} = $action;
4331 $args{'hash'} = $hash if $hash;
4332 $args{'hash_base'} = $hash_base if $hash_base;
4334 print "<div class=\"header\">\n" .
4335 $cgi->a({-href => href(%args), -class => "title"},
4336 $title ? $title : $action) .
4340 sub format_repo_url {
4341 my ($name, $url) = @_;
4342 return "<tr class=\"metadata_url\"><td>$name</td><td>$url</td></tr>\n";
4345 # Group output by placing it in a DIV element and adding a header.
4346 # Options for start_div() can be provided by passing a hash reference as the
4347 # first parameter to the function.
4348 # Options to git_print_header_div() can be provided by passing an array
4349 # reference. This must follow the options to start_div if they are present.
4350 # The content can be a scalar, which is output as-is, a scalar reference, which
4351 # is output after html escaping, an IO handle passed either as *handle or
4352 # *handle{IO}, or a function reference. In the latter case all following
4353 # parameters will be taken as argument to the content function call.
4354 sub git_print_section {
4355 my ($div_args, $header_args, $content);
4357 if (ref($arg) eq 'HASH') {
4361 if (ref($arg) eq 'ARRAY') {
4362 $header_args = $arg;
4367 print $cgi->start_div($div_args);
4368 git_print_header_div(@$header_args);
4370 if (ref($content) eq 'CODE') {
4372 } elsif (ref($content) eq 'SCALAR') {
4373 print esc_html($$content);
4374 } elsif (ref($content) eq 'GLOB' or ref($content) eq 'IO::Handle') {
4376 } elsif (!ref($content) && defined($content)) {
4380 print $cgi->end_div;
4383 sub format_timestamp_html {
4385 my $strtime = $date->{'rfc2822'};
4387 my (undef, undef, $datetime_class) =
4388 gitweb_get_feature('javascript-timezone');
4389 if ($datetime_class) {
4390 $strtime = qq!<span class="$datetime_class">$strtime</span>!;
4393 my $localtime_format = '(%02d:%02d %s)';
4394 if ($date->{'hour_local'} < 6) {
4395 $localtime_format = '(<span class="atnight">%02d:%02d</span> %s)';
4398 sprintf($localtime_format,
4399 $date->{'hour_local'}, $date->{'minute_local'}, $date->{'tz_local'});
4404 # Outputs the author name and date in long form
4405 sub git_print_authorship {
4408 my $tag = $opts{-tag} || 'div';
4409 my $author = $co->{'author_name'};
4411 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
4412 print "<$tag class=\"author_date\">" .
4413 format_search_author($author, "author", esc_html($author)) .
4414 " [".format_timestamp_html(\%ad)."]".
4415 git_get_avatar($co->{'author_email'}, -pad_before => 1) .
4419 # Outputs table rows containing the full author or committer information,
4420 # in the format expected for 'commit' view (& similar).
4421 # Parameters are a commit hash reference, followed by the list of people
4422 # to output information for. If the list is empty it defaults to both
4423 # author and committer.
4424 sub git_print_authorship_rows {
4426 # too bad we can't use @people = @_ || ('author', 'committer')
4428 @people = ('author', 'committer') unless @people;
4429 foreach my $who (@people) {
4430 my %wd = parse_date($co->{"${who}_epoch"}, $co->{"${who}_tz"});
4431 print "<tr><td>$who</td><td>" .
4432 format_search_author($co->{"${who}_name"}, $who,
4433 esc_html($co->{"${who}_name"})) . " " .
4434 format_search_author($co->{"${who}_email"}, $who,
4435 esc_html("<" . $co->{"${who}_email"} . ">")) .
4436 "</td><td rowspan=\"2\">" .
4437 git_get_avatar($co->{"${who}_email"}, -size => 'double') .
4441 format_timestamp_html(\%wd) .
4447 sub git_print_page_path {
4453 print "<div class=\"page_path\">";
4454 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
4455 -title => 'tree root'}, to_utf8("[$project]"));
4457 if (defined $name) {
4458 my @dirname = split '/', $name;
4459 my $basename = pop @dirname;
4462 foreach my $dir (@dirname) {
4463 $fullname .= ($fullname ? '/' : '') . $dir;
4464 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
4466 -title => $fullname}, esc_path($dir));
4469 if (defined $type && $type eq 'blob') {
4470 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
4472 -title => $name}, esc_path($basename));
4473 } elsif (defined $type && $type eq 'tree') {
4474 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
4476 -title => $name}, esc_path($basename));
4479 print esc_path($basename);
4482 print "<br/></div>\n";
4489 if ($opts{'-remove_title'}) {
4490 # remove title, i.e. first line of log
4493 # remove leading empty lines
4494 while (defined $log->[0] && $log->[0] eq "") {
4499 my $skip_blank_line = 0;
4500 foreach my $line (@$log) {
4501 if ($line =~ m/^\s*([A-Z][-A-Za-z]*-[Bb]y|C[Cc]): /) {
4502 if (! $opts{'-remove_signoff'}) {
4503 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
4504 $skip_blank_line = 1;
4509 if ($line =~ m,\s*([a-z]*link): (https?://\S+),i) {
4510 if (! $opts{'-remove_signoff'}) {
4511 print "<span class=\"signoff\">" . esc_html($1) . ": " .
4512 "<a href=\"" . esc_html($2) . "\">" . esc_html($2) . "</a>" .
4514 $skip_blank_line = 1;
4519 # print only one empty line
4520 # do not print empty line after signoff
4522 next if ($skip_blank_line);
4523 $skip_blank_line = 1;
4525 $skip_blank_line = 0;
4528 print format_log_line_html($line) . "<br/>\n";
4531 if ($opts{'-final_empty_line'}) {
4532 # end with single empty line
4533 print "<br/>\n" unless $skip_blank_line;
4537 # return link target (what link points to)
4538 sub git_get_link_target {
4543 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4547 $link_target = <$fd>;
4552 return $link_target;
4555 # given link target, and the directory (basedir) the link is in,
4556 # return target of link relative to top directory (top tree);
4557 # return undef if it is not possible (including absolute links).
4558 sub normalize_link_target {
4559 my ($link_target, $basedir) = @_;
4561 # absolute symlinks (beginning with '/') cannot be normalized
4562 return if (substr($link_target, 0, 1) eq '/');
4564 # normalize link target to path from top (root) tree (dir)
4567 $path = $basedir . '/' . $link_target;
4569 # we are in top (root) tree (dir)
4570 $path = $link_target;
4573 # remove //, /./, and /../
4575 foreach my $part (split('/', $path)) {
4576 # discard '.' and ''
4577 next if (!$part || $part eq '.');
4579 if ($part eq '..') {
4583 # link leads outside repository (outside top dir)
4587 push @path_parts, $part;
4590 $path = join('/', @path_parts);
4595 # print tree entry (row of git_tree), but without encompassing <tr> element
4596 sub git_print_tree_entry {
4597 my ($t, $basedir, $hash_base, $have_blame) = @_;
4600 $base_key{'hash_base'} = $hash_base if defined $hash_base;
4602 # The format of a table row is: mode list link. Where mode is
4603 # the mode of the entry, list is the name of the entry, an href,
4604 # and link is the action links of the entry.
4606 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
4607 if (exists $t->{'size'}) {
4608 print "<td class=\"size\">$t->{'size'}</td>\n";
4610 if ($t->{'type'} eq "blob") {
4611 print "<td class=\"list\">" .
4612 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
4613 file_name=>"$basedir$t->{'name'}", %base_key),
4614 -class => "list"}, esc_path($t->{'name'}));
4615 if (S_ISLNK(oct $t->{'mode'})) {
4616 my $link_target = git_get_link_target($t->{'hash'});
4618 my $norm_target = normalize_link_target($link_target, $basedir);
4619 if (defined $norm_target) {
4621 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
4622 file_name=>$norm_target),
4623 -title => $norm_target}, esc_path($link_target));
4625 print " -> " . esc_path($link_target);
4630 print "<td class=\"link\">";
4631 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
4632 file_name=>"$basedir$t->{'name'}", %base_key)},
4636 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
4637 file_name=>"$basedir$t->{'name'}", %base_key)},
4640 if (defined $hash_base) {
4642 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
4643 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
4647 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
4648 file_name=>"$basedir$t->{'name'}")},
4652 } elsif ($t->{'type'} eq "tree") {
4653 print "<td class=\"list\">";
4654 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
4655 file_name=>"$basedir$t->{'name'}",
4657 esc_path($t->{'name'}));
4659 print "<td class=\"link\">";
4660 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
4661 file_name=>"$basedir$t->{'name'}",
4664 if (defined $hash_base) {
4666 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
4667 file_name=>"$basedir$t->{'name'}")},
4672 # unknown object: we can only present history for it
4673 # (this includes 'commit' object, i.e. submodule support)
4674 print "<td class=\"list\">" .
4675 esc_path($t->{'name'}) .
4677 print "<td class=\"link\">";
4678 if (defined $hash_base) {
4679 print $cgi->a({-href => href(action=>"history",
4680 hash_base=>$hash_base,
4681 file_name=>"$basedir$t->{'name'}")},
4688 ## ......................................................................
4689 ## functions printing large fragments of HTML
4691 # get pre-image filenames for merge (combined) diff
4692 sub fill_from_file_info {
4693 my ($diff, @parents) = @_;
4695 $diff->{'from_file'} = [ ];
4696 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
4697 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
4698 if ($diff->{'status'}[$i] eq 'R' ||
4699 $diff->{'status'}[$i] eq 'C') {
4700 $diff->{'from_file'}[$i] =
4701 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
4708 # is current raw difftree line of file deletion
4710 my $diffinfo = shift;
4712 return $diffinfo->{'to_id'} eq ('0' x 40);
4715 # does patch correspond to [previous] difftree raw line
4716 # $diffinfo - hashref of parsed raw diff format
4717 # $patchinfo - hashref of parsed patch diff format
4718 # (the same keys as in $diffinfo)
4719 sub is_patch_split {
4720 my ($diffinfo, $patchinfo) = @_;
4722 return defined $diffinfo && defined $patchinfo
4723 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
4727 sub git_difftree_body {
4728 my ($difftree, $hash, @parents) = @_;
4729 my ($parent) = $parents[0];
4730 my $have_blame = gitweb_check_feature('blame');
4731 print "<div class=\"list_head\">\n";
4732 if ($#{$difftree} > 10) {
4733 print(($#{$difftree} + 1) . " files changed:\n");
4737 print "<table class=\"" .
4738 (@parents > 1 ? "combined " : "") .
4741 # header only for combined diff in 'commitdiff' view
4742 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
4745 print "<thead><tr>\n" .
4746 "<th></th><th></th>\n"; # filename, patchN link
4747 for (my $i = 0; $i < @parents; $i++) {
4748 my $par = $parents[$i];
4750 $cgi->a({-href => href(action=>"commitdiff",
4751 hash=>$hash, hash_parent=>$par),
4752 -title => 'commitdiff to parent number ' .
4753 ($i+1) . ': ' . substr($par,0,7)},
4757 print "</tr></thead>\n<tbody>\n";
4762 foreach my $line (@{$difftree}) {
4763 my $diff = parsed_difftree_line($line);
4766 print "<tr class=\"dark\">\n";
4768 print "<tr class=\"light\">\n";
4772 if (exists $diff->{'nparents'}) { # combined diff
4774 fill_from_file_info($diff, @parents)
4775 unless exists $diff->{'from_file'};
4777 if (!is_deleted($diff)) {
4778 # file exists in the result (child) commit
4780 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4781 file_name=>$diff->{'to_file'},
4783 -class => "list"}, esc_path($diff->{'to_file'})) .
4787 esc_path($diff->{'to_file'}) .
4791 if ($action eq 'commitdiff') {
4794 print "<td class=\"link\">" .
4795 $cgi->a({-href => href(-anchor=>"patch$patchno")},
4801 my $has_history = 0;
4802 my $not_deleted = 0;
4803 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
4804 my $hash_parent = $parents[$i];
4805 my $from_hash = $diff->{'from_id'}[$i];
4806 my $from_path = $diff->{'from_file'}[$i];
4807 my $status = $diff->{'status'}[$i];
4809 $has_history ||= ($status ne 'A');
4810 $not_deleted ||= ($status ne 'D');
4812 if ($status eq 'A') {
4813 print "<td class=\"link\" align=\"right\"> | </td>\n";
4814 } elsif ($status eq 'D') {
4815 print "<td class=\"link\">" .
4816 $cgi->a({-href => href(action=>"blob",
4819 file_name=>$from_path)},
4823 if ($diff->{'to_id'} eq $from_hash) {
4824 print "<td class=\"link nochange\">";
4826 print "<td class=\"link\">";
4828 print $cgi->a({-href => href(action=>"blobdiff",
4829 hash=>$diff->{'to_id'},
4830 hash_parent=>$from_hash,
4832 hash_parent_base=>$hash_parent,
4833 file_name=>$diff->{'to_file'},
4834 file_parent=>$from_path)},
4840 print "<td class=\"link\">";
4842 print $cgi->a({-href => href(action=>"blob",
4843 hash=>$diff->{'to_id'},
4844 file_name=>$diff->{'to_file'},
4847 print " | " if ($has_history);
4850 print $cgi->a({-href => href(action=>"history",
4851 file_name=>$diff->{'to_file'},
4858 next; # instead of 'else' clause, to avoid extra indent
4860 # else ordinary diff
4862 my ($to_mode_oct, $to_mode_str, $to_file_type);
4863 my ($from_mode_oct, $from_mode_str, $from_file_type);
4864 if ($diff->{'to_mode'} ne ('0' x 6)) {
4865 $to_mode_oct = oct $diff->{'to_mode'};
4866 if (S_ISREG($to_mode_oct)) { # only for regular file
4867 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
4869 $to_file_type = file_type($diff->{'to_mode'});
4871 if ($diff->{'from_mode'} ne ('0' x 6)) {
4872 $from_mode_oct = oct $diff->{'from_mode'};
4873 if (S_ISREG($from_mode_oct)) { # only for regular file
4874 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
4876 $from_file_type = file_type($diff->{'from_mode'});
4879 if ($diff->{'status'} eq "A") { # created
4880 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
4881 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
4882 $mode_chng .= "]</span>";
4884 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4885 hash_base=>$hash, file_name=>$diff->{'file'}),
4886 -class => "list"}, esc_path($diff->{'file'}));
4888 print "<td>$mode_chng</td>\n";
4889 print "<td class=\"link\">";
4890 if ($action eq 'commitdiff') {
4893 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
4897 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4898 hash_base=>$hash, file_name=>$diff->{'file'})},
4902 } elsif ($diff->{'status'} eq "D") { # deleted
4903 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
4905 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
4906 hash_base=>$parent, file_name=>$diff->{'file'}),
4907 -class => "list"}, esc_path($diff->{'file'}));
4909 print "<td>$mode_chng</td>\n";
4910 print "<td class=\"link\">";
4911 if ($action eq 'commitdiff') {
4914 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
4918 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
4919 hash_base=>$parent, file_name=>$diff->{'file'})},
4922 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
4923 file_name=>$diff->{'file'})},
4926 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
4927 file_name=>$diff->{'file'})},
4931 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
4932 my $mode_chnge = "";
4933 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
4934 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
4935 if ($from_file_type ne $to_file_type) {
4936 $mode_chnge .= " from $from_file_type to $to_file_type";
4938 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
4939 if ($from_mode_str && $to_mode_str) {
4940 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
4941 } elsif ($to_mode_str) {
4942 $mode_chnge .= " mode: $to_mode_str";
4945 $mode_chnge .= "]</span>\n";
4948 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4949 hash_base=>$hash, file_name=>$diff->{'file'}),
4950 -class => "list"}, esc_path($diff->{'file'}));
4952 print "<td>$mode_chnge</td>\n";
4953 print "<td class=\"link\">";
4954 if ($action eq 'commitdiff') {
4957 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
4960 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
4961 # "commit" view and modified file (not onlu mode changed)
4962 print $cgi->a({-href => href(action=>"blobdiff",
4963 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
4964 hash_base=>$hash, hash_parent_base=>$parent,
4965 file_name=>$diff->{'file'})},
4969 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4970 hash_base=>$hash, file_name=>$diff->{'file'})},
4973 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
4974 file_name=>$diff->{'file'})},
4977 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
4978 file_name=>$diff->{'file'})},
4982 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
4983 my %status_name = ('R' => 'moved', 'C' => 'copied');
4984 my $nstatus = $status_name{$diff->{'status'}};
4986 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
4987 # mode also for directories, so we cannot use $to_mode_str
4988 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
4991 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
4992 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
4993 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
4994 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
4995 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
4996 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
4997 -class => "list"}, esc_path($diff->{'from_file'})) .
4998 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
4999 "<td class=\"link\">";
5000 if ($action eq 'commitdiff') {
5003 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
5006 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
5007 # "commit" view and modified file (not only pure rename or copy)
5008 print $cgi->a({-href => href(action=>"blobdiff",
5009 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
5010 hash_base=>$hash, hash_parent_base=>$parent,
5011 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
5015 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
5016 hash_base=>$parent, file_name=>$diff->{'to_file'})},
5019 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
5020 file_name=>$diff->{'to_file'})},
5023 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
5024 file_name=>$diff->{'to_file'})},
5028 } # we should not encounter Unmerged (U) or Unknown (X) status
5031 print "</tbody>" if $has_header;
5035 # Print context lines and then rem/add lines in a side-by-side manner.
5036 sub print_sidebyside_diff_lines {
5037 my ($ctx, $rem, $add) = @_;
5039 # print context block before add/rem block
5042 '<div class="chunk_block ctx">',
5043 '<div class="old">',
5046 '<div class="new">',
5055 '<div class="chunk_block rem">',
5056 '<div class="old">',
5063 '<div class="chunk_block add">',
5064 '<div class="new">',
5070 '<div class="chunk_block chg">',
5071 '<div class="old">',
5074 '<div class="new">',
5081 # Print context lines and then rem/add lines in inline manner.
5082 sub print_inline_diff_lines {
5083 my ($ctx, $rem, $add) = @_;
5085 print @$ctx, @$rem, @$add;
5088 # Format removed and added line, mark changed part and HTML-format them.
5089 # Implementation is based on contrib/diff-highlight
5090 sub format_rem_add_lines_pair {
5091 my ($rem, $add, $num_parents) = @_;
5093 # We need to untabify lines before split()'ing them;
5094 # otherwise offsets would be invalid.
5097 $rem = untabify($rem);
5098 $add = untabify($add);
5100 my @rem = split(//, $rem);
5101 my @add = split(//, $add);
5102 my ($esc_rem, $esc_add);
5103 # Ignore leading +/- characters for each parent.
5104 my ($prefix_len, $suffix_len) = ($num_parents, 0);
5105 my ($prefix_has_nonspace, $suffix_has_nonspace);
5107 my $shorter = (@rem < @add) ? @rem : @add;
5108 while ($prefix_len < $shorter) {
5109 last if ($rem[$prefix_len] ne $add[$prefix_len]);
5111 $prefix_has_nonspace = 1 if ($rem[$prefix_len] !~ /\s/);
5115 while ($prefix_len + $suffix_len < $shorter) {
5116 last if ($rem[-1 - $suffix_len] ne $add[-1 - $suffix_len]);
5118 $suffix_has_nonspace = 1 if ($rem[-1 - $suffix_len] !~ /\s/);
5122 # Mark lines that are different from each other, but have some common
5123 # part that isn't whitespace. If lines are completely different, don't
5124 # mark them because that would make output unreadable, especially if
5125 # diff consists of multiple lines.
5126 if ($prefix_has_nonspace || $suffix_has_nonspace) {
5127 $esc_rem = esc_html_hl_regions($rem, 'marked',
5128 [$prefix_len, @rem - $suffix_len], -nbsp=>1);
5129 $esc_add = esc_html_hl_regions($add, 'marked',
5130 [$prefix_len, @add - $suffix_len], -nbsp=>1);
5132 $esc_rem = esc_html($rem, -nbsp=>1);
5133 $esc_add = esc_html($add, -nbsp=>1);
5136 return format_diff_line(\$esc_rem, 'rem'),
5137 format_diff_line(\$esc_add, 'add');
5140 # HTML-format diff context, removed and added lines.
5141 sub format_ctx_rem_add_lines {
5142 my ($ctx, $rem, $add, $num_parents) = @_;
5143 my (@new_ctx, @new_rem, @new_add);
5144 my $can_highlight = 0;
5145 my $is_combined = ($num_parents > 1);
5147 # Highlight if every removed line has a corresponding added line.
5148 if (@$add > 0 && @$add == @$rem) {
5151 # Highlight lines in combined diff only if the chunk contains
5152 # diff between the same version, e.g.
5159 # Otherwise the highlightling would be confusing.
5161 for (my $i = 0; $i < @$add; $i++) {
5162 my $prefix_rem = substr($rem->[$i], 0, $num_parents);
5163 my $prefix_add = substr($add->[$i], 0, $num_parents);
5165 $prefix_rem =~ s/-/+/g;
5167 if ($prefix_rem ne $prefix_add) {
5175 if ($can_highlight) {
5176 for (my $i = 0; $i < @$add; $i++) {
5177 my ($line_rem, $line_add) = format_rem_add_lines_pair(
5178 $rem->[$i], $add->[$i], $num_parents);
5179 push @new_rem, $line_rem;
5180 push @new_add, $line_add;
5183 @new_rem = map { format_diff_line($_, 'rem') } @$rem;
5184 @new_add = map { format_diff_line($_, 'add') } @$add;
5187 @new_ctx = map { format_diff_line($_, 'ctx') } @$ctx;
5189 return (\@new_ctx, \@new_rem, \@new_add);
5192 # Print context lines and then rem/add lines.
5193 sub print_diff_lines {
5194 my ($ctx, $rem, $add, $diff_style, $num_parents) = @_;
5195 my $is_combined = $num_parents > 1;
5197 ($ctx, $rem, $add) = format_ctx_rem_add_lines($ctx, $rem, $add,
5200 if ($diff_style eq 'sidebyside' && !$is_combined) {
5201 print_sidebyside_diff_lines($ctx, $rem, $add);
5203 # default 'inline' style and unknown styles
5204 print_inline_diff_lines($ctx, $rem, $add);
5208 sub print_diff_chunk {
5209 my ($diff_style, $num_parents, $from, $to, @chunk) = @_;
5210 my (@ctx, @rem, @add);
5212 # The class of the previous line.
5213 my $prev_class = '';
5215 return unless @chunk;
5217 # incomplete last line might be among removed or added lines,
5218 # or both, or among context lines: find which
5219 for (my $i = 1; $i < @chunk; $i++) {
5220 if ($chunk[$i][0] eq 'incomplete') {
5221 $chunk[$i][0] = $chunk[$i-1][0];
5226 push @chunk, ["", ""];
5228 foreach my $line_info (@chunk) {
5229 my ($class, $line) = @$line_info;
5231 # print chunk headers
5232 if ($class && $class eq 'chunk_header') {
5233 print format_diff_line($line, $class, $from, $to);
5237 ## print from accumulator when have some add/rem lines or end
5238 # of chunk (flush context lines), or when have add and rem
5239 # lines and new block is reached (otherwise add/rem lines could
5241 if (!$class || ((@rem || @add) && $class eq 'ctx') ||
5242 (@rem && @add && $class ne $prev_class)) {
5243 print_diff_lines(\@ctx, \@rem, \@add,
5244 $diff_style, $num_parents);
5245 @ctx = @rem = @add = ();
5248 ## adding lines to accumulator
5251 # rem, add or change
5252 if ($class eq 'rem') {
5254 } elsif ($class eq 'add') {
5258 if ($class eq 'ctx') {
5262 $prev_class = $class;
5266 sub git_patchset_body {
5267 my ($fd, $diff_style, $difftree, $hash, @hash_parents) = @_;
5268 my ($hash_parent) = $hash_parents[0];
5270 my $is_combined = (@hash_parents > 1);
5272 my $patch_number = 0;
5277 my @chunk; # for side-by-side diff
5279 print "<div class=\"patchset\">\n";
5281 # skip to first patch
5282 while ($patch_line = <$fd>) {
5285 last if ($patch_line =~ m/^diff /);
5289 while ($patch_line) {
5291 # parse "git diff" header line
5292 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
5293 # $1 is from_name, which we do not use
5294 $to_name = unquote($2);
5295 $to_name =~ s!^b/!!;
5296 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
5297 # $1 is 'cc' or 'combined', which we do not use
5298 $to_name = unquote($2);
5303 # check if current patch belong to current raw line
5304 # and parse raw git-diff line if needed
5305 if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
5306 # this is continuation of a split patch
5307 print "<div class=\"patch cont\">\n";
5309 # advance raw git-diff output if needed
5310 $patch_idx++ if defined $diffinfo;
5312 # read and prepare patch information
5313 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
5315 # compact combined diff output can have some patches skipped
5316 # find which patch (using pathname of result) we are at now;
5318 while ($to_name ne $diffinfo->{'to_file'}) {
5319 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
5320 format_diff_cc_simplified($diffinfo, @hash_parents) .
5321 "</div>\n"; # class="patch"
5326 last if $patch_idx > $#$difftree;
5327 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
5331 # modifies %from, %to hashes
5332 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
5334 # this is first patch for raw difftree line with $patch_idx index
5335 # we index @$difftree array from 0, but number patches from 1
5336 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
5340 #assert($patch_line =~ m/^diff /) if DEBUG;
5341 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
5343 # print "git diff" header
5344 print format_git_diff_header_line($patch_line, $diffinfo,
5347 # print extended diff header
5348 print "<div class=\"diff extended_header\">\n";
5350 while ($patch_line = <$fd>) {
5353 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
5355 print format_extended_diff_header_line($patch_line, $diffinfo,
5358 print "</div>\n"; # class="diff extended_header"
5360 # from-file/to-file diff header
5361 if (! $patch_line) {
5362 print "</div>\n"; # class="patch"
5365 next PATCH if ($patch_line =~ m/^diff /);
5366 #assert($patch_line =~ m/^---/) if DEBUG;
5368 my $last_patch_line = $patch_line;
5369 $patch_line = <$fd>;
5371 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
5373 print format_diff_from_to_header($last_patch_line, $patch_line,
5374 $diffinfo, \%from, \%to,
5379 while ($patch_line = <$fd>) {
5382 next PATCH if ($patch_line =~ m/^diff /);
5384 my $class = diff_line_class($patch_line, \%from, \%to);
5386 if ($class eq 'chunk_header') {
5387 print_diff_chunk($diff_style, scalar @hash_parents, \%from, \%to, @chunk);
5391 push @chunk, [ $class, $patch_line ];
5396 print_diff_chunk($diff_style, scalar @hash_parents, \%from, \%to, @chunk);
5399 print "</div>\n"; # class="patch"
5402 # for compact combined (--cc) format, with chunk and patch simplification
5403 # the patchset might be empty, but there might be unprocessed raw lines
5404 for (++$patch_idx if $patch_number > 0;
5405 $patch_idx < @$difftree;
5407 # read and prepare patch information
5408 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
5410 # generate anchor for "patch" links in difftree / whatchanged part
5411 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
5412 format_diff_cc_simplified($diffinfo, @hash_parents) .
5413 "</div>\n"; # class="patch"
5418 if ($patch_number == 0) {
5419 if (@hash_parents > 1) {
5420 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
5422 print "<div class=\"diff nodifferences\">No differences found</div>\n";
5426 print "</div>\n"; # class="patchset"
5429 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5431 sub git_project_search_form {
5432 my ($searchtext, $search_use_regexp) = @_;
5435 if ($project_filter) {
5436 $limit = " in '$project_filter/'";
5439 print "<div class=\"projsearch\">\n";
5440 print $cgi->startform(-method => 'get', -action => $my_uri) .
5441 $cgi->hidden(-name => 'a', -value => 'project_list') . "\n";
5442 print $cgi->hidden(-name => 'pf', -value => $project_filter). "\n"
5443 if (defined $project_filter);
5444 print $cgi->textfield(-name => 's', -value => $searchtext,
5445 -title => "Search project by name and description$limit",
5446 -size => 60) . "\n" .
5447 "<span title=\"Extended regular expression\">" .
5448 $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
5449 -checked => $search_use_regexp) .
5451 $cgi->submit(-name => 'btnS', -value => 'Search') .
5452 $cgi->end_form() . "\n" .
5453 $cgi->a({-href => href(project => undef, searchtext => undef,
5454 project_filter => $project_filter)},
5455 esc_html("List all projects$limit")) . "<br />\n";
5459 # entry for given @keys needs filling if at least one of keys in list
5460 # is not present in %$project_info
5461 sub project_info_needs_filling {
5462 my ($project_info, @keys) = @_;
5464 # return List::MoreUtils::any { !exists $project_info->{$_} } @keys;
5465 foreach my $key (@keys) {
5466 if (!exists $project_info->{$key}) {
5473 # fills project list info (age, description, owner, category, forks, etc.)
5474 # for each project in the list, removing invalid projects from
5475 # returned list, or fill only specified info.
5477 # Invalid projects are removed from the returned list if and only if you
5478 # ask 'age' or 'age_string' to be filled, because they are the only fields
5479 # that run unconditionally git command that requires repository, and
5480 # therefore do always check if project repository is invalid.
5483 # * fill_project_list_info(\@project_list, 'descr_long', 'ctags')
5484 # ensures that 'descr_long' and 'ctags' fields are filled
5485 # * @project_list = fill_project_list_info(\@project_list)
5486 # ensures that all fields are filled (and invalid projects removed)
5488 # NOTE: modifies $projlist, but does not remove entries from it
5489 sub fill_project_list_info {
5490 my ($projlist, @wanted_keys) = @_;
5492 my $filter_set = sub { return @_; };
5494 my %wanted_keys = map { $_ => 1 } @wanted_keys;
5495 $filter_set = sub { return grep { $wanted_keys{$_} } @_; };
5498 my $show_ctags = gitweb_check_feature('ctags');
5500 foreach my $pr (@$projlist) {
5501 if (project_info_needs_filling($pr, $filter_set->('age', 'age_string'))) {
5502 my (@activity) = git_get_last_activity($pr->{'path'});
5503 unless (@activity) {
5506 ($pr->{'age'}, $pr->{'age_string'}) = @activity;
5508 if (project_info_needs_filling($pr, $filter_set->('descr', 'descr_long'))) {
5509 my $descr = git_get_project_description($pr->{'path'}) || "";
5510 $descr = to_utf8($descr);
5511 $pr->{'descr_long'} = $descr;
5512 $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
5514 if (project_info_needs_filling($pr, $filter_set->('owner'))) {
5515 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
5518 project_info_needs_filling($pr, $filter_set->('ctags'))) {
5519 $pr->{'ctags'} = git_get_project_ctags($pr->{'path'});
5521 if ($projects_list_group_categories &&
5522 project_info_needs_filling($pr, $filter_set->('category'))) {
5523 my $cat = git_get_project_category($pr->{'path'}) ||
5524 $project_list_default_category;
5525 $pr->{'category'} = to_utf8($cat);
5528 push @projects, $pr;
5534 sub sort_projects_list {
5535 my ($projlist, $order) = @_;
5539 return sub { $a->{$key} cmp $b->{$key} };
5542 sub order_num_then_undef {
5545 defined $a->{$key} ?
5546 (defined $b->{$key} ? $a->{$key} <=> $b->{$key} : -1) :
5547 (defined $b->{$key} ? 1 : 0)
5552 project => order_str('path'),
5553 descr => order_str('descr_long'),
5554 owner => order_str('owner'),
5555 age => order_num_then_undef('age'),
5558 my $ordering = $orderings{$order};
5559 return defined $ordering ? sort $ordering @$projlist : @$projlist;
5562 # returns a hash of categories, containing the list of project
5563 # belonging to each category
5564 sub build_projlist_by_category {
5565 my ($projlist, $from, $to) = @_;
5568 $from = 0 unless defined $from;
5569 $to = $#$projlist if (!defined $to || $#$projlist < $to);
5571 for (my $i = $from; $i <= $to; $i++) {
5572 my $pr = $projlist->[$i];
5573 push @{$categories{ $pr->{'category'} }}, $pr;
5576 return wantarray ? %categories : \%categories;
5579 # print 'sort by' <th> element, generating 'sort by $name' replay link
5580 # if that order is not selected
5582 print format_sort_th(@_);
5585 sub format_sort_th {
5586 my ($name, $order, $header) = @_;
5588 $header ||= ucfirst($name);
5590 if ($order eq $name) {
5591 $sort_th .= "<th>$header</th>\n";
5593 $sort_th .= "<th>" .
5594 $cgi->a({-href => href(-replay=>1, order=>$name),
5595 -class => "header"}, $header) .
5602 sub git_project_list_rows {
5603 my ($projlist, $from, $to, $check_forks) = @_;
5605 $from = 0 unless defined $from;
5606 $to = $#$projlist if (!defined $to || $#$projlist < $to);
5609 for (my $i = $from; $i <= $to; $i++) {
5610 my $pr = $projlist->[$i];
5613 print "<tr class=\"dark\">\n";
5615 print "<tr class=\"light\">\n";
5621 if ($pr->{'forks'}) {
5622 my $nforks = scalar @{$pr->{'forks'}};
5624 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks"),
5625 -title => "$nforks forks"}, "+");
5627 print $cgi->span({-title => "$nforks forks"}, "+");
5632 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
5634 esc_html_match_hl($pr->{'path'}, $search_regexp)) .
5636 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
5638 -title => $pr->{'descr_long'}},
5640 ? esc_html_match_hl_chopped($pr->{'descr_long'},
5641 $pr->{'descr'}, $search_regexp)
5642 : esc_html($pr->{'descr'})) .
5644 unless ($omit_owner) {
5645 print "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n";
5647 unless ($omit_age_column) {
5648 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
5649 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n";
5651 print"<td class=\"link\">" .
5652 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
5653 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
5654 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
5655 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
5656 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
5662 sub git_project_list_body {
5663 # actually uses global variable $project
5664 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
5665 my @projects = @$projlist;
5667 my $check_forks = gitweb_check_feature('forks');
5668 my $show_ctags = gitweb_check_feature('ctags');
5669 my $tagfilter = $show_ctags ? $input_params{'ctag'} : undef;
5670 $check_forks = undef
5671 if ($tagfilter || $search_regexp);
5673 # filtering out forks before filling info allows to do less work
5674 @projects = filter_forks_from_projects_list(\@projects)
5676 # search_projects_list pre-fills required info
5677 @projects = search_projects_list(\@projects,
5678 'search_regexp' => $search_regexp,
5679 'tagfilter' => $tagfilter)
5680 if ($tagfilter || $search_regexp);
5682 my @all_fields = ('descr', 'descr_long', 'ctags', 'category');
5683 push @all_fields, ('age', 'age_string') unless($omit_age_column);
5684 push @all_fields, 'owner' unless($omit_owner);
5685 @projects = fill_project_list_info(\@projects, @all_fields);
5687 $order ||= $default_projects_order;
5688 $from = 0 unless defined $from;
5689 $to = $#projects if (!defined $to || $#projects < $to);
5694 "<b>No such projects found</b><br />\n".
5695 "Click ".$cgi->a({-href=>href(project=>undef)},"here")." to view all projects<br />\n".
5696 "</center>\n<br />\n";
5700 @projects = sort_projects_list(\@projects, $order);
5703 my $ctags = git_gather_all_ctags(\@projects);
5704 my $cloud = git_populate_project_tagcloud($ctags);
5705 print git_show_project_tagcloud($cloud, 64);
5708 print "<table class=\"project_list\">\n";
5709 unless ($no_header) {
5712 print "<th></th>\n";
5714 print_sort_th('project', $order, 'Project');
5715 print_sort_th('descr', $order, 'Description');
5716 print_sort_th('owner', $order, 'Owner') unless $omit_owner;
5717 print_sort_th('age', $order, 'Last Change') unless $omit_age_column;
5718 print "<th></th>\n" . # for links
5722 if ($projects_list_group_categories) {
5723 # only display categories with projects in the $from-$to window
5724 @projects = sort {$a->{'category'} cmp $b->{'category'}} @projects[$from..$to];
5725 my %categories = build_projlist_by_category(\@projects, $from, $to);
5726 foreach my $cat (sort keys %categories) {
5727 unless ($cat eq "") {
5730 print "<td></td>\n";
5732 print "<td class=\"category\" colspan=\"5\">".esc_html($cat)."</td>\n";
5736 git_project_list_rows($categories{$cat}, undef, undef, $check_forks);
5739 git_project_list_rows(\@projects, $from, $to, $check_forks);
5742 if (defined $extra) {
5745 print "<td></td>\n";
5747 print "<td colspan=\"5\">$extra</td>\n" .
5754 # uses global variable $project
5755 my ($commitlist, $from, $to, $refs, $extra) = @_;
5757 $from = 0 unless defined $from;
5758 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
5760 for (my $i = 0; $i <= $to; $i++) {
5761 my %co = %{$commitlist->[$i]};
5763 my $commit = $co{'id'};
5764 my $ref = format_ref_marker($refs, $commit);
5765 git_print_header_div('commit',
5766 "<span class=\"age\">$co{'age_string'}</span>" .
5767 esc_html($co{'title'}) . $ref,
5769 print "<div class=\"title_text\">\n" .
5770 "<div class=\"log_link\">\n" .
5771 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
5773 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
5775 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
5778 git_print_authorship(\%co, -tag => 'span');
5779 print "<br/>\n</div>\n";
5781 print "<div class=\"log_body\">\n";
5782 git_print_log($co{'comment'}, -final_empty_line=> 1);
5786 print "<div class=\"page_nav\">\n";
5792 sub git_shortlog_body {
5793 # uses global variable $project
5794 my ($commitlist, $from, $to, $refs, $extra) = @_;
5796 $from = 0 unless defined $from;
5797 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
5799 print "<table class=\"shortlog\">\n";
5801 for (my $i = $from; $i <= $to; $i++) {
5802 my %co = %{$commitlist->[$i]};
5803 my $commit = $co{'id'};
5804 my $ref = format_ref_marker($refs, $commit);
5806 print "<tr class=\"dark\">\n";
5808 print "<tr class=\"light\">\n";
5811 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
5812 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5813 format_author_html('td', \%co, 10) . "<td>";
5814 print format_subject_html($co{'title'}, $co{'title_short'},
5815 href(action=>"commit", hash=>$commit), $ref);
5817 "<td class=\"link\">" .
5818 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
5819 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
5820 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
5821 my $snapshot_links = format_snapshot_links($commit);
5822 if (defined $snapshot_links) {
5823 print " | " . $snapshot_links;
5828 if (defined $extra) {
5830 "<td colspan=\"4\">$extra</td>\n" .
5836 sub git_history_body {
5837 # Warning: assumes constant type (blob or tree) during history
5838 my ($commitlist, $from, $to, $refs, $extra,
5839 $file_name, $file_hash, $ftype) = @_;
5841 $from = 0 unless defined $from;
5842 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
5844 print "<table class=\"history\">\n";
5846 for (my $i = $from; $i <= $to; $i++) {
5847 my %co = %{$commitlist->[$i]};
5851 my $commit = $co{'id'};
5853 my $ref = format_ref_marker($refs, $commit);
5856 print "<tr class=\"dark\">\n";
5858 print "<tr class=\"light\">\n";
5861 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5862 # shortlog: format_author_html('td', \%co, 10)
5863 format_author_html('td', \%co, 15, 3) . "<td>";
5864 # originally git_history used chop_str($co{'title'}, 50)
5865 print format_subject_html($co{'title'}, $co{'title_short'},
5866 href(action=>"commit", hash=>$commit), $ref);
5868 "<td class=\"link\">" .
5869 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
5870 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
5872 if ($ftype eq 'blob') {
5873 my $blob_current = $file_hash;
5874 my $blob_parent = git_get_hash_by_path($commit, $file_name);
5875 if (defined $blob_current && defined $blob_parent &&
5876 $blob_current ne $blob_parent) {
5878 $cgi->a({-href => href(action=>"blobdiff",
5879 hash=>$blob_current, hash_parent=>$blob_parent,
5880 hash_base=>$hash_base, hash_parent_base=>$commit,
5881 file_name=>$file_name)},
5888 if (defined $extra) {
5890 "<td colspan=\"4\">$extra</td>\n" .
5897 # uses global variable $project
5898 my ($taglist, $from, $to, $extra) = @_;
5899 $from = 0 unless defined $from;
5900 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
5902 print "<table class=\"tags\">\n";
5904 for (my $i = $from; $i <= $to; $i++) {
5905 my $entry = $taglist->[$i];
5907 my $comment = $tag{'subject'};
5909 if (defined $comment) {
5910 $comment_short = chop_str($comment, 30, 5);
5913 print "<tr class=\"dark\">\n";
5915 print "<tr class=\"light\">\n";
5918 if (defined $tag{'age'}) {
5919 print "<td><i>$tag{'age'}</i></td>\n";
5921 print "<td></td>\n";
5924 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
5925 -class => "list name"}, esc_html($tag{'name'})) .
5928 if (defined $comment) {
5929 print format_subject_html($comment, $comment_short,
5930 href(action=>"tag", hash=>$tag{'id'}));
5933 "<td class=\"selflink\">";
5934 if ($tag{'type'} eq "tag") {
5935 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
5940 "<td class=\"link\">" . " | " .
5941 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
5942 if ($tag{'reftype'} eq "commit") {
5943 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})}, "shortlog") .
5944 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})}, "log");
5945 } elsif ($tag{'reftype'} eq "blob") {
5946 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
5951 if (defined $extra) {
5953 "<td colspan=\"5\">$extra</td>\n" .
5959 sub git_heads_body {
5960 # uses global variable $project
5961 my ($headlist, $head_at, $from, $to, $extra) = @_;
5962 $from = 0 unless defined $from;
5963 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
5965 print "<table class=\"heads\">\n";
5967 for (my $i = $from; $i <= $to; $i++) {
5968 my $entry = $headlist->[$i];
5970 my $curr = defined $head_at && $ref{'id'} eq $head_at;
5972 print "<tr class=\"dark\">\n";
5974 print "<tr class=\"light\">\n";
5977 print "<td><i>$ref{'age'}</i></td>\n" .
5978 ($curr ? "<td class=\"current_head\">" : "<td>") .
5979 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),
5980 -class => "list name"},esc_html($ref{'name'})) .
5982 "<td class=\"link\">" .
5983 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})}, "shortlog") . " | " .
5984 $cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})}, "log") . " | " .
5985 $cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'fullname'})}, "tree") .
5989 if (defined $extra) {
5991 "<td colspan=\"3\">$extra</td>\n" .
5997 # Display a single remote block
5998 sub git_remote_block {
5999 my ($remote, $rdata, $limit, $head) = @_;
6001 my $heads = $rdata->{'heads'};
6002 my $fetch = $rdata->{'fetch'};
6003 my $push = $rdata->{'push'};
6005 my $urls_table = "<table class=\"projects_list\">\n" ;
6007 if (defined $fetch) {
6008 if ($fetch eq $push) {
6009 $urls_table .= format_repo_url("URL", $fetch);
6011 $urls_table .= format_repo_url("Fetch URL", $fetch);
6012 $urls_table .= format_repo_url("Push URL", $push) if defined $push;
6014 } elsif (defined $push) {
6015 $urls_table .= format_repo_url("Push URL", $push);
6017 $urls_table .= format_repo_url("", "No remote URL");
6020 $urls_table .= "</table>\n";
6023 if (defined $limit && $limit < @$heads) {
6024 $dots = $cgi->a({-href => href(action=>"remotes", hash=>$remote)}, "...");
6028 git_heads_body($heads, $head, 0, $limit, $dots);
6031 # Display a list of remote names with the respective fetch and push URLs
6032 sub git_remotes_list {
6033 my ($remotedata, $limit) = @_;
6034 print "<table class=\"heads\">\n";
6036 my @remotes = sort keys %$remotedata;
6038 my $limited = $limit && $limit < @remotes;
6040 $#remotes = $limit - 1 if $limited;
6042 while (my $remote = shift @remotes) {
6043 my $rdata = $remotedata->{$remote};
6044 my $fetch = $rdata->{'fetch'};
6045 my $push = $rdata->{'push'};
6047 print "<tr class=\"dark\">\n";
6049 print "<tr class=\"light\">\n";
6053 $cgi->a({-href=> href(action=>'remotes', hash=>$remote),
6054 -class=> "list name"},esc_html($remote)) .
6056 print "<td class=\"link\">" .
6057 (defined $fetch ? $cgi->a({-href=> $fetch}, "fetch") : "fetch") .
6059 (defined $push ? $cgi->a({-href=> $push}, "push") : "push") .
6067 "<td colspan=\"3\">" .
6068 $cgi->a({-href => href(action=>"remotes")}, "...") .
6069 "</td>\n" . "</tr>\n";
6075 # Display remote heads grouped by remote, unless there are too many
6076 # remotes, in which case we only display the remote names
6077 sub git_remotes_body {
6078 my ($remotedata, $limit, $head) = @_;
6079 if ($limit and $limit < keys %$remotedata) {
6080 git_remotes_list($remotedata, $limit);
6082 fill_remote_heads($remotedata);
6083 while (my ($remote, $rdata) = each %$remotedata) {
6084 git_print_section({-class=>"remote", -id=>$remote},
6085 ["remotes", $remote, $remote], sub {
6086 git_remote_block($remote, $rdata, $limit, $head);
6092 sub git_search_message {
6096 if ($searchtype eq 'commit') {
6097 $greptype = "--grep=";
6098 } elsif ($searchtype eq 'author') {
6099 $greptype = "--author=";
6100 } elsif ($searchtype eq 'committer') {
6101 $greptype = "--committer=";
6103 $greptype .= $searchtext;
6104 my @commitlist = parse_commits($hash, 101, (100 * $page), undef,
6105 $greptype, '--regexp-ignore-case',
6106 $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
6108 my $paging_nav = '';
6111 $cgi->a({-href => href(-replay=>1, page=>undef)},
6114 $cgi->a({-href => href(-replay=>1, page=>$page-1),
6115 -accesskey => "p", -title => "Alt-p"}, "prev");
6117 $paging_nav .= "first ⋅ prev";
6120 if ($#commitlist >= 100) {
6122 $cgi->a({-href => href(-replay=>1, page=>$page+1),
6123 -accesskey => "n", -title => "Alt-n"}, "next");
6124 $paging_nav .= " ⋅ $next_link";
6126 $paging_nav .= " ⋅ next";
6131 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
6132 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6133 if ($page == 0 && !@commitlist) {
6134 print "<p>No match.</p>\n";
6136 git_search_grep_body(\@commitlist, 0, 99, $next_link);
6142 sub git_search_changes {
6146 open my $fd, '-|', git_cmd(), '--no-pager', 'log', @diff_opts,
6147 '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
6148 ($search_use_regexp ? '--pickaxe-regex' : ())
6149 or die_error(500, "Open git-log failed");
6153 git_print_page_nav('','', $hash,$co{'tree'},$hash);
6154 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6156 print "<table class=\"pickaxe search\">\n";
6160 while (my $line = <$fd>) {
6164 my %set = parse_difftree_raw_line($line);
6165 if (defined $set{'commit'}) {
6166 # finish previous commit
6169 "<td class=\"link\">" .
6170 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},
6173 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'},
6174 hash_base=>$co{'id'})},
6181 print "<tr class=\"dark\">\n";
6183 print "<tr class=\"light\">\n";
6186 %co = parse_commit($set{'commit'});
6187 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
6188 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
6189 "<td><i>$author</i></td>\n" .
6191 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
6192 -class => "list subject"},
6193 chop_and_escape_str($co{'title'}, 50) . "<br/>");
6194 } elsif (defined $set{'to_id'}) {
6195 next if ($set{'to_id'} =~ m/^0{40}$/);
6197 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
6198 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),
6200 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
6206 # finish last commit (warning: repetition!)
6209 "<td class=\"link\">" .
6210 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},
6213 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'},
6214 hash_base=>$co{'id'})},
6225 sub git_search_files {
6229 open my $fd, "-|", git_cmd(), 'grep', '-n', '-z',
6230 $search_use_regexp ? ('-E', '-i') : '-F',
6231 $searchtext, $co{'tree'}
6232 or die_error(500, "Open git-grep failed");
6236 git_print_page_nav('','', $hash,$co{'tree'},$hash);
6237 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6239 print "<table class=\"grep_search\">\n";
6244 while (my $line = <$fd>) {
6246 my ($file, $lno, $ltext, $binary);
6247 last if ($matches++ > 1000);
6248 if ($line =~ /^Binary file (.+) matches$/) {
6252 ($file, $lno, $ltext) = split(/\0/, $line, 3);
6253 $file =~ s/^$co{'tree'}://;
6255 if ($file ne $lastfile) {
6256 $lastfile and print "</td></tr>\n";
6258 print "<tr class=\"dark\">\n";
6260 print "<tr class=\"light\">\n";
6262 $file_href = href(action=>"blob", hash_base=>$co{'id'},
6264 print "<td class=\"list\">".
6265 $cgi->a({-href => $file_href, -class => "list"}, esc_path($file));
6266 print "</td><td>\n";
6270 print "<div class=\"binary\">Binary file</div>\n";
6272 $ltext = untabify($ltext);
6273 if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
6274 $ltext = esc_html($1, -nbsp=>1);
6275 $ltext .= '<span class="match">';
6276 $ltext .= esc_html($2, -nbsp=>1);
6277 $ltext .= '</span>';
6278 $ltext .= esc_html($3, -nbsp=>1);
6280 $ltext = esc_html($ltext, -nbsp=>1);
6282 print "<div class=\"pre\">" .
6283 $cgi->a({-href => $file_href.'#l'.$lno,
6284 -class => "linenr"}, sprintf('%4i', $lno)) .
6285 ' ' . $ltext . "</div>\n";
6289 print "</td></tr>\n";
6290 if ($matches > 1000) {
6291 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
6294 print "<div class=\"diff nodifferences\">No matches found</div>\n";
6303 sub git_search_grep_body {
6304 my ($commitlist, $from, $to, $extra) = @_;
6305 $from = 0 unless defined $from;
6306 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
6308 print "<table class=\"commit_search\">\n";
6310 for (my $i = $from; $i <= $to; $i++) {
6311 my %co = %{$commitlist->[$i]};
6315 my $commit = $co{'id'};
6317 print "<tr class=\"dark\">\n";
6319 print "<tr class=\"light\">\n";
6322 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
6323 format_author_html('td', \%co, 15, 5) .
6325 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
6326 -class => "list subject"},
6327 chop_and_escape_str($co{'title'}, 50) . "<br/>");
6328 my $comment = $co{'comment'};
6329 foreach my $line (@$comment) {
6330 if ($line =~ m/^(.*?)($search_regexp)(.*)$/i) {
6331 my ($lead, $match, $trail) = ($1, $2, $3);
6332 $match = chop_str($match, 70, 5, 'center');
6333 my $contextlen = int((80 - length($match))/2);
6334 $contextlen = 30 if ($contextlen > 30);
6335 $lead = chop_str($lead, $contextlen, 10, 'left');
6336 $trail = chop_str($trail, $contextlen, 10, 'right');
6338 $lead = esc_html($lead);
6339 $match = esc_html($match);
6340 $trail = esc_html($trail);
6342 print "$lead<span class=\"match\">$match</span>$trail<br />";
6346 "<td class=\"link\">" .
6347 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
6349 $cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})}, "commitdiff") .
6351 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
6355 if (defined $extra) {
6357 "<td colspan=\"3\">$extra</td>\n" .
6363 ## ======================================================================
6364 ## ======================================================================
6367 sub git_project_list {
6368 my $order = $input_params{'order'};
6369 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
6370 die_error(400, "Unknown order parameter");
6373 my @list = git_get_projects_list($project_filter, $strict_export);
6375 die_error(404, "No projects found");
6379 if (defined $home_text && -f $home_text) {
6380 print "<div class=\"index_include\">\n";
6381 insert_file($home_text);
6385 git_project_search_form($searchtext, $search_use_regexp);
6386 git_project_list_body(\@list, $order);
6391 my $order = $input_params{'order'};
6392 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
6393 die_error(400, "Unknown order parameter");
6396 my $filter = $project;
6397 $filter =~ s/\.git$//;
6398 my @list = git_get_projects_list($filter);
6400 die_error(404, "No forks found");
6404 git_print_page_nav('','');
6405 git_print_header_div('summary', "$project forks");
6406 git_project_list_body(\@list, $order);
6410 sub git_project_index {
6411 my @projects = git_get_projects_list($project_filter, $strict_export);
6413 die_error(404, "No projects found");
6417 -type => 'text/plain',
6418 -charset => 'utf-8',
6419 -content_disposition => 'inline; filename="index.aux"');
6421 foreach my $pr (@projects) {
6422 if (!exists $pr->{'owner'}) {
6423 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
6426 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
6427 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
6428 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
6429 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
6433 print "$path $owner\n";
6438 my $descr = git_get_project_description($project) || "none";
6439 my %co = parse_commit("HEAD");
6440 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
6441 my $head = $co{'id'};
6442 my $remote_heads = gitweb_check_feature('remote_heads');
6444 my $owner = git_get_project_owner($project);
6446 my $refs = git_get_references();
6447 # These get_*_list functions return one more to allow us to see if
6448 # there are more ...
6449 my @taglist = git_get_tags_list(16);
6450 my @headlist = git_get_heads_list(16);
6451 my %remotedata = $remote_heads ? git_get_remotes_list() : ();
6453 my $check_forks = gitweb_check_feature('forks');
6456 # find forks of a project
6457 my $filter = $project;
6458 $filter =~ s/\.git$//;
6459 @forklist = git_get_projects_list($filter);
6460 # filter out forks of forks
6461 @forklist = filter_forks_from_projects_list(\@forklist)
6466 git_print_page_nav('summary','', $head);
6468 print "<div class=\"title\"> </div>\n";
6469 print "<table class=\"projects_list\">\n" .
6470 "<tr id=\"metadata_desc\"><td>description</td><td>" . esc_html($descr) . "</td></tr>\n";
6471 unless ($omit_owner) {
6472 print "<tr id=\"metadata_owner\"><td>owner</td><td>" . esc_html($owner) . "</td></tr>\n";
6474 if (defined $cd{'rfc2822'}) {
6475 print "<tr id=\"metadata_lchange\"><td>last change</td>" .
6476 "<td>".format_timestamp_html(\%cd)."</td></tr>\n";
6479 # use per project git URL list in $projectroot/$project/cloneurl
6480 # or make project git URL from git base URL and project name
6481 my $url_tag = "URL";
6482 my @url_list = git_get_project_url_list($project);
6483 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
6484 foreach my $git_url (@url_list) {
6485 next unless $git_url;
6486 print format_repo_url($url_tag, $git_url);
6491 my $show_ctags = gitweb_check_feature('ctags');
6493 my $ctags = git_get_project_ctags($project);
6495 # without ability to add tags, don't show if there are none
6496 my $cloud = git_populate_project_tagcloud($ctags);
6497 print "<tr id=\"metadata_ctags\">" .
6498 "<td>content tags</td>" .
6499 "<td>".git_show_project_tagcloud($cloud, 48)."</td>" .
6506 # If XSS prevention is on, we don't include README.html.
6507 # TODO: Allow a readme in some safe format.
6508 if (!$prevent_xss && -s "$projectroot/$project/README.html") {
6509 print "<div class=\"title\">readme</div>\n" .
6510 "<div class=\"readme\">\n";
6511 insert_file("$projectroot/$project/README.html");
6512 print "\n</div>\n"; # class="readme"
6515 # we need to request one more than 16 (0..15) to check if
6517 my @commitlist = $head ? parse_commits($head, 17) : ();
6519 git_print_header_div('shortlog');
6520 git_shortlog_body(\@commitlist, 0, 15, $refs,
6521 $#commitlist <= 15 ? undef :
6522 $cgi->a({-href => href(action=>"shortlog")}, "..."));
6526 git_print_header_div('tags');
6527 git_tags_body(\@taglist, 0, 15,
6528 $#taglist <= 15 ? undef :
6529 $cgi->a({-href => href(action=>"tags")}, "..."));
6533 git_print_header_div('heads');
6534 git_heads_body(\@headlist, $head, 0, 15,
6535 $#headlist <= 15 ? undef :
6536 $cgi->a({-href => href(action=>"heads")}, "..."));
6540 git_print_header_div('remotes');
6541 git_remotes_body(\%remotedata, 15, $head);
6545 git_print_header_div('forks');
6546 git_project_list_body(\@forklist, 'age', 0, 15,
6547 $#forklist <= 15 ? undef :
6548 $cgi->a({-href => href(action=>"forks")}, "..."),
6556 my %tag = parse_tag($hash);
6559 die_error(404, "Unknown tag object");
6562 my $head = git_get_head_hash($project);
6564 git_print_page_nav('','', $head,undef,$head);
6565 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
6566 print "<div class=\"title_text\">\n" .
6567 "<table class=\"object_header\">\n" .
6569 "<td>object</td>\n" .
6570 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
6571 $tag{'object'}) . "</td>\n" .
6572 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
6573 $tag{'type'}) . "</td>\n" .
6575 if (defined($tag{'author'})) {
6576 git_print_authorship_rows(\%tag, 'author');
6578 print "</table>\n\n" .
6580 print "<div class=\"page_body\">";
6581 my $comment = $tag{'comment'};
6582 foreach my $line (@$comment) {
6584 print esc_html($line, -nbsp=>1) . "<br/>\n";
6590 sub git_blame_common {
6591 my $format = shift || 'porcelain';
6592 if ($format eq 'porcelain' && $input_params{'javascript'}) {
6593 $format = 'incremental';
6594 $action = 'blame_incremental'; # for page title etc
6598 gitweb_check_feature('blame')
6599 or die_error(403, "Blame view not allowed");
6602 die_error(400, "No file name given") unless $file_name;
6603 $hash_base ||= git_get_head_hash($project);
6604 die_error(404, "Couldn't find base commit") unless $hash_base;
6605 my %co = parse_commit($hash_base)
6606 or die_error(404, "Commit not found");
6608 if (!defined $hash) {
6609 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
6610 or die_error(404, "Error looking up file");
6612 $ftype = git_get_type($hash);
6613 if ($ftype !~ "blob") {
6614 die_error(400, "Object is not a blob");
6619 if ($format eq 'incremental') {
6620 # get file contents (as base)
6621 open $fd, "-|", git_cmd(), 'cat-file', 'blob', $hash
6622 or die_error(500, "Open git-cat-file failed");
6623 } elsif ($format eq 'data') {
6624 # run git-blame --incremental
6625 open $fd, "-|", git_cmd(), "blame", "--incremental",
6626 $hash_base, "--", $file_name
6627 or die_error(500, "Open git-blame --incremental failed");
6629 # run git-blame --porcelain
6630 open $fd, "-|", git_cmd(), "blame", '-p',
6631 $hash_base, '--', $file_name
6632 or die_error(500, "Open git-blame --porcelain failed");
6635 # incremental blame data returns early
6636 if ($format eq 'data') {
6638 -type=>"text/plain", -charset => "utf-8",
6639 -status=> "200 OK");
6640 local $| = 1; # output autoflush
6641 while (my $line = <$fd>) {
6642 print to_utf8($line);
6645 or print "ERROR $!\n";
6648 if (defined $t0 && gitweb_check_feature('timed')) {
6650 tv_interval($t0, [ gettimeofday() ]).
6651 ' '.$number_of_git_cmds;
6661 $cgi->a({-href => href(action=>"blob", -replay=>1)},
6664 if ($format eq 'incremental') {
6666 $cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},
6667 "blame") . " (non-incremental)";
6670 $cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},
6671 "blame") . " (incremental)";
6675 $cgi->a({-href => href(action=>"history", -replay=>1)},
6678 $cgi->a({-href => href(action=>$action, file_name=>$file_name)},
6680 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
6681 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
6682 git_print_page_path($file_name, $ftype, $hash_base);
6685 if ($format eq 'incremental') {
6686 print "<noscript>\n<div class=\"error\"><center><b>\n".
6687 "This page requires JavaScript to run.\n Use ".
6688 $cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},
6691 "</b></center></div>\n</noscript>\n";
6693 print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;
6696 print qq!<div class="page_body">\n!;
6697 print qq!<div id="progress_info">... / ...</div>\n!
6698 if ($format eq 'incremental');
6699 print qq!<table id="blame_table" class="blame" width="100%">\n!.
6700 #qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.
6702 qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.
6706 my @rev_color = qw(light dark);
6707 my $num_colors = scalar(@rev_color);
6708 my $current_color = 0;
6710 if ($format eq 'incremental') {
6711 my $color_class = $rev_color[$current_color];
6716 while (my $line = <$fd>) {
6720 print qq!<tr id="l$linenr" class="$color_class">!.
6721 qq!<td class="sha1"><a href=""> </a></td>!.
6722 qq!<td class="linenr">!.
6723 qq!<a class="linenr" href="">$linenr</a></td>!;
6724 print qq!<td class="pre">! . esc_html($line) . "</td>\n";
6728 } else { # porcelain, i.e. ordinary blame
6729 my %metainfo = (); # saves information about commits
6733 while (my $line = <$fd>) {
6735 # the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]
6736 # no <lines in group> for subsequent lines in group of lines
6737 my ($full_rev, $orig_lineno, $lineno, $group_size) =
6738 ($line =~ /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);
6739 if (!exists $metainfo{$full_rev}) {
6740 $metainfo{$full_rev} = { 'nprevious' => 0 };
6742 my $meta = $metainfo{$full_rev};
6744 while ($data = <$fd>) {
6746 last if ($data =~ s/^\t//); # contents of line
6747 if ($data =~ /^(\S+)(?: (.*))?$/) {
6748 $meta->{$1} = $2 unless exists $meta->{$1};
6750 if ($data =~ /^previous /) {
6751 $meta->{'nprevious'}++;
6754 my $short_rev = substr($full_rev, 0, 8);
6755 my $author = $meta->{'author'};
6757 parse_date($meta->{'author-time'}, $meta->{'author-tz'});
6758 my $date = $date{'iso-tz'};
6760 $current_color = ($current_color + 1) % $num_colors;
6762 my $tr_class = $rev_color[$current_color];
6763 $tr_class .= ' boundary' if (exists $meta->{'boundary'});
6764 $tr_class .= ' no-previous' if ($meta->{'nprevious'} == 0);
6765 $tr_class .= ' multiple-previous' if ($meta->{'nprevious'} > 1);
6766 print "<tr id=\"l$lineno\" class=\"$tr_class\">\n";
6768 print "<td class=\"sha1\"";
6769 print " title=\"". esc_html($author) . ", $date\"";
6770 print " rowspan=\"$group_size\"" if ($group_size > 1);
6772 print $cgi->a({-href => href(action=>"commit",
6774 file_name=>$file_name)},
6775 esc_html($short_rev));
6776 if ($group_size >= 2) {
6777 my @author_initials = ($author =~ /\b([[:upper:]])\B/g);
6778 if (@author_initials) {
6780 esc_html(join('', @author_initials));
6786 # 'previous' <sha1 of parent commit> <filename at commit>
6787 if (exists $meta->{'previous'} &&
6788 $meta->{'previous'} =~ /^([a-fA-F0-9]{40}) (.*)$/) {
6789 $meta->{'parent'} = $1;
6790 $meta->{'file_parent'} = unquote($2);
6793 exists($meta->{'parent'}) ?
6794 $meta->{'parent'} : $full_rev;
6795 my $linenr_filename =
6796 exists($meta->{'file_parent'}) ?
6797 $meta->{'file_parent'} : unquote($meta->{'filename'});
6798 my $blamed = href(action => 'blame',
6799 file_name => $linenr_filename,
6800 hash_base => $linenr_commit);
6801 print "<td class=\"linenr\">";
6802 print $cgi->a({ -href => "$blamed#l$orig_lineno",
6803 -class => "linenr" },
6806 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
6814 "</table>\n"; # class="blame"
6815 print "</div>\n"; # class="blame_body"
6817 or print "Reading blob failed\n";
6826 sub git_blame_incremental {
6827 git_blame_common('incremental');
6830 sub git_blame_data {
6831 git_blame_common('data');
6835 my $head = git_get_head_hash($project);
6837 git_print_page_nav('','', $head,undef,$head,format_ref_views('tags'));
6838 git_print_header_div('summary', $project);
6840 my @tagslist = git_get_tags_list();
6842 git_tags_body(\@tagslist);
6848 my $head = git_get_head_hash($project);
6850 git_print_page_nav('','', $head,undef,$head,format_ref_views('heads'));
6851 git_print_header_div('summary', $project);
6853 my @headslist = git_get_heads_list();
6855 git_heads_body(\@headslist, $head);
6860 # used both for single remote view and for list of all the remotes
6862 gitweb_check_feature('remote_heads')
6863 or die_error(403, "Remote heads view is disabled");
6865 my $head = git_get_head_hash($project);
6866 my $remote = $input_params{'hash'};
6868 my $remotedata = git_get_remotes_list($remote);
6869 die_error(500, "Unable to get remote information") unless defined $remotedata;
6871 unless (%$remotedata) {
6872 die_error(404, defined $remote ?
6873 "Remote $remote not found" :
6874 "No remotes found");
6877 git_header_html(undef, undef, -action_extra => $remote);
6878 git_print_page_nav('', '', $head, undef, $head,
6879 format_ref_views($remote ? '' : 'remotes'));
6881 fill_remote_heads($remotedata);
6882 if (defined $remote) {
6883 git_print_header_div('remotes', "$remote remote for $project");
6884 git_remote_block($remote, $remotedata->{$remote}, undef, $head);
6886 git_print_header_div('summary', "$project remotes");
6887 git_remotes_body($remotedata, undef, $head);
6893 sub git_blob_plain {
6897 if (!defined $hash) {
6898 if (defined $file_name) {
6899 my $base = $hash_base || git_get_head_hash($project);
6900 $hash = git_get_hash_by_path($base, $file_name, "blob")
6901 or die_error(404, "Cannot find file");
6903 die_error(400, "No file name defined");
6905 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6906 # blobs defined by non-textual hash id's can be cached
6910 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
6911 or die_error(500, "Open git-cat-file blob '$hash' failed");
6913 # content-type (can include charset)
6914 $type = blob_contenttype($fd, $file_name, $type);
6916 # "save as" filename, even when no $file_name is given
6917 my $save_as = "$hash";
6918 if (defined $file_name) {
6919 $save_as = $file_name;
6920 } elsif ($type =~ m/^text\//) {
6924 # With XSS prevention on, blobs of all types except a few known safe
6925 # ones are served with "Content-Disposition: attachment" to make sure
6926 # they don't run in our security domain. For certain image types,
6927 # blob view writes an <img> tag referring to blob_plain view, and we
6928 # want to be sure not to break that by serving the image as an
6929 # attachment (though Firefox 3 doesn't seem to care).
6930 my $sandbox = $prevent_xss &&
6931 $type !~ m!^(?:text/[a-z]+|image/(?:gif|png|jpeg))(?:[ ;]|$)!;
6933 # serve text/* as text/plain
6935 ($type =~ m!^text/[a-z]+\b(.*)$! ||
6936 ($type =~ m!^[a-z]+/[a-z]\+xml\b(.*)$! && -T $fd))) {
6938 $rest = defined $rest ? $rest : '';
6939 $type = "text/plain$rest";
6944 -expires => $expires,
6945 -content_disposition =>
6946 ($sandbox ? 'attachment' : 'inline')
6947 . '; filename="' . $save_as . '"');
6949 binmode STDOUT, ':raw';
6951 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
6958 if (!defined $hash) {
6959 if (defined $file_name) {
6960 my $base = $hash_base || git_get_head_hash($project);
6961 $hash = git_get_hash_by_path($base, $file_name, "blob")
6962 or die_error(404, "Cannot find file");
6964 die_error(400, "No file name defined");
6966 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6967 # blobs defined by non-textual hash id's can be cached
6971 my $have_blame = gitweb_check_feature('blame');
6972 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
6973 or die_error(500, "Couldn't cat $file_name, $hash");
6974 my $mimetype = blob_mimetype($fd, $file_name);
6975 # use 'blob_plain' (aka 'raw') view for files that cannot be displayed
6976 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {
6978 return git_blob_plain($mimetype);
6980 # we can have blame only for text/* mimetype
6981 $have_blame &&= ($mimetype =~ m!^text/!);
6983 my $highlight = gitweb_check_feature('highlight');
6984 my $syntax = guess_file_syntax($highlight, $mimetype, $file_name);
6985 $fd = run_highlighter($fd, $highlight, $syntax)
6988 git_header_html(undef, $expires);
6989 my $formats_nav = '';
6990 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
6991 if (defined $file_name) {
6994 $cgi->a({-href => href(action=>"blame", -replay=>1)},
6999 $cgi->a({-href => href(action=>"history", -replay=>1)},
7002 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
7005 $cgi->a({-href => href(action=>"blob",
7006 hash_base=>"HEAD", file_name=>$file_name)},
7010 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
7013 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
7014 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
7016 print "<div class=\"page_nav\">\n" .
7017 "<br/><br/></div>\n" .
7018 "<div class=\"title\">".esc_html($hash)."</div>\n";
7020 git_print_page_path($file_name, "blob", $hash_base);
7021 print "<div class=\"page_body\">\n";
7022 if ($mimetype =~ m!^image/!) {
7023 print qq!<img type="!.esc_attr($mimetype).qq!"!;
7025 print qq! alt="!.esc_attr($file_name).qq!" title="!.esc_attr($file_name).qq!"!;
7028 href(action=>"blob_plain", hash=>$hash,
7029 hash_base=>$hash_base, file_name=>$file_name) .
7033 while (my $line = <$fd>) {
7036 $line = untabify($line);
7037 printf qq!<div class="pre"><a id="l%i" href="%s#l%i" class="linenr">%4i</a> %s</div>\n!,
7038 $nr, esc_attr(href(-replay => 1)), $nr, $nr,
7039 $syntax ? sanitize($line) : esc_html($line, -nbsp=>1);
7043 or print "Reading blob failed.\n";
7049 if (!defined $hash_base) {
7050 $hash_base = "HEAD";
7052 if (!defined $hash) {
7053 if (defined $file_name) {
7054 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
7059 die_error(404, "No such tree") unless defined($hash);
7061 my $show_sizes = gitweb_check_feature('show-sizes');
7062 my $have_blame = gitweb_check_feature('blame');
7067 open my $fd, "-|", git_cmd(), "ls-tree", '-z',
7068 ($show_sizes ? '-l' : ()), @extra_options, $hash
7069 or die_error(500, "Open git-ls-tree failed");
7070 @entries = map { chomp; $_ } <$fd>;
7072 or die_error(404, "Reading tree failed");
7075 my $refs = git_get_references();
7076 my $ref = format_ref_marker($refs, $hash_base);
7079 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
7081 if (defined $file_name) {
7083 $cgi->a({-href => href(action=>"history", -replay=>1)},
7085 $cgi->a({-href => href(action=>"tree",
7086 hash_base=>"HEAD", file_name=>$file_name)},
7089 my $snapshot_links = format_snapshot_links($hash);
7090 if (defined $snapshot_links) {
7091 # FIXME: Should be available when we have no hash base as well.
7092 push @views_nav, $snapshot_links;
7094 git_print_page_nav('tree','', $hash_base, undef, undef,
7095 join(' | ', @views_nav));
7096 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
7099 print "<div class=\"page_nav\">\n";
7100 print "<br/><br/></div>\n";
7101 print "<div class=\"title\">".esc_html($hash)."</div>\n";
7103 if (defined $file_name) {
7104 $basedir = $file_name;
7105 if ($basedir ne '' && substr($basedir, -1) ne '/') {
7108 git_print_page_path($file_name, 'tree', $hash_base);
7110 print "<div class=\"page_body\">\n";
7111 print "<table class=\"tree\">\n";
7113 # '..' (top directory) link if possible
7114 if (defined $hash_base &&
7115 defined $file_name && $file_name =~ m![^/]+$!) {
7117 print "<tr class=\"dark\">\n";
7119 print "<tr class=\"light\">\n";
7123 my $up = $file_name;
7124 $up =~ s!/?[^/]+$!!;
7125 undef $up unless $up;
7126 # based on git_print_tree_entry
7127 print '<td class="mode">' . mode_str('040000') . "</td>\n";
7128 print '<td class="size"> </td>'."\n" if $show_sizes;
7129 print '<td class="list">';
7130 print $cgi->a({-href => href(action=>"tree",
7131 hash_base=>$hash_base,
7135 print "<td class=\"link\"></td>\n";
7139 foreach my $line (@entries) {
7140 my %t = parse_ls_tree_line($line, -z => 1, -l => $show_sizes);
7143 print "<tr class=\"dark\">\n";
7145 print "<tr class=\"light\">\n";
7149 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
7153 print "</table>\n" .
7159 my ($project, $hash) = @_;
7161 # path/to/project.git -> project
7162 # path/to/project/.git -> project
7163 my $name = to_utf8($project);
7164 $name =~ s,([^/])/*\.git$,$1,;
7165 $name = basename($name);
7167 $name =~ s/[[:cntrl:]]/?/g;
7170 if ($hash =~ /^[0-9a-fA-F]+$/) {
7171 # shorten SHA-1 hash
7172 my $full_hash = git_get_full_hash($project, $hash);
7173 if ($full_hash =~ /^$hash/ && length($hash) > 7) {
7174 $ver = git_get_short_hash($project, $hash);
7176 } elsif ($hash =~ m!^refs/tags/(.*)$!) {
7177 # tags don't need shortened SHA-1 hash
7180 # branches and other need shortened SHA-1 hash
7181 if ($hash =~ m!^refs/(?:heads|remotes)/(.*)$!) {
7184 $ver .= '-' . git_get_short_hash($project, $hash);
7186 # in case of hierarchical branch names
7189 # name = project-version_string
7190 $name = "$name-$ver";
7192 return wantarray ? ($name, $name) : $name;
7195 sub exit_if_unmodified_since {
7196 my ($latest_epoch) = @_;
7199 my $if_modified = $cgi->http('IF_MODIFIED_SINCE');
7200 if (defined $if_modified) {
7202 if (eval { require HTTP::Date; 1; }) {
7203 $since = HTTP::Date::str2time($if_modified);
7204 } elsif (eval { require Time::ParseDate; 1; }) {
7205 $since = Time::ParseDate::parsedate($if_modified, GMT => 1);
7207 if (defined $since && $latest_epoch <= $since) {
7208 my %latest_date = parse_date($latest_epoch);
7210 -last_modified => $latest_date{'rfc2822'},
7211 -status => '304 Not Modified');
7218 my $format = $input_params{'snapshot_format'};
7219 if (!@snapshot_fmts) {
7220 die_error(403, "Snapshots not allowed");
7222 # default to first supported snapshot format
7223 $format ||= $snapshot_fmts[0];
7224 if ($format !~ m/^[a-z0-9]+$/) {
7225 die_error(400, "Invalid snapshot format parameter");
7226 } elsif (!exists($known_snapshot_formats{$format})) {
7227 die_error(400, "Unknown snapshot format");
7228 } elsif ($known_snapshot_formats{$format}{'disabled'}) {
7229 die_error(403, "Snapshot format not allowed");
7230 } elsif (!grep($_ eq $format, @snapshot_fmts)) {
7231 die_error(403, "Unsupported snapshot format");
7234 my $type = git_get_type("$hash^{}");
7236 die_error(404, 'Object does not exist');
7237 } elsif ($type eq 'blob') {
7238 die_error(400, 'Object is not a tree-ish');
7241 my ($name, $prefix) = snapshot_name($project, $hash);
7242 my $filename = "$name$known_snapshot_formats{$format}{'suffix'}";
7244 my %co = parse_commit($hash);
7245 exit_if_unmodified_since($co{'committer_epoch'}) if %co;
7247 my $cmd = quote_command(
7248 git_cmd(), 'archive',
7249 "--format=$known_snapshot_formats{$format}{'format'}",
7250 "--prefix=$prefix/", $hash);
7251 if (exists $known_snapshot_formats{$format}{'compressor'}) {
7252 $cmd .= ' | ' . quote_command(@{$known_snapshot_formats{$format}{'compressor'}});
7255 $filename =~ s/(["\\])/\\$1/g;
7258 %latest_date = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
7262 -type => $known_snapshot_formats{$format}{'type'},
7263 -content_disposition => 'inline; filename="' . $filename . '"',
7264 %co ? (-last_modified => $latest_date{'rfc2822'}) : (),
7265 -status => '200 OK');
7267 open my $fd, "-|", $cmd
7268 or die_error(500, "Execute git-archive failed");
7269 binmode STDOUT, ':raw';
7271 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
7275 sub git_log_generic {
7276 my ($fmt_name, $body_subr, $base, $parent, $file_name, $file_hash) = @_;
7278 my $head = git_get_head_hash($project);
7279 if (!defined $base) {
7282 if (!defined $page) {
7285 my $refs = git_get_references();
7287 my $commit_hash = $base;
7288 if (defined $parent) {
7289 $commit_hash = "$parent..$base";
7292 parse_commits($commit_hash, 101, (100 * $page),
7293 defined $file_name ? ($file_name, "--full-history") : ());
7296 if (!defined $file_hash && defined $file_name) {
7297 # some commits could have deleted file in question,
7298 # and not have it in tree, but one of them has to have it
7299 for (my $i = 0; $i < @commitlist; $i++) {
7300 $file_hash = git_get_hash_by_path($commitlist[$i]{'id'}, $file_name);
7301 last if defined $file_hash;
7304 if (defined $file_hash) {
7305 $ftype = git_get_type($file_hash);
7307 if (defined $file_name && !defined $ftype) {
7308 die_error(500, "Unknown type of object");
7311 if (defined $file_name) {
7312 %co = parse_commit($base)
7313 or die_error(404, "Unknown commit object");
7317 my $paging_nav = format_paging_nav($fmt_name, $page, $#commitlist >= 100);
7319 if ($#commitlist >= 100) {
7321 $cgi->a({-href => href(-replay=>1, page=>$page+1),
7322 -accesskey => "n", -title => "Alt-n"}, "next");
7324 my $patch_max = gitweb_get_feature('patches');
7325 if ($patch_max && !defined $file_name) {
7326 if ($patch_max < 0 || @commitlist <= $patch_max) {
7327 $paging_nav .= " ⋅ " .
7328 $cgi->a({-href => href(action=>"patches", -replay=>1)},
7334 git_print_page_nav($fmt_name,'', $hash,$hash,$hash, $paging_nav);
7335 if (defined $file_name) {
7336 git_print_header_div('commit', esc_html($co{'title'}), $base);
7338 git_print_header_div('summary', $project)
7340 git_print_page_path($file_name, $ftype, $hash_base)
7341 if (defined $file_name);
7343 $body_subr->(\@commitlist, 0, 99, $refs, $next_link,
7344 $file_name, $file_hash, $ftype);
7350 git_log_generic('log', \&git_log_body,
7351 $hash, $hash_parent);
7355 $hash ||= $hash_base || "HEAD";
7356 my %co = parse_commit($hash)
7357 or die_error(404, "Unknown commit object");
7359 my $parent = $co{'parent'};
7360 my $parents = $co{'parents'}; # listref
7362 # we need to prepare $formats_nav before any parameter munging
7364 if (!defined $parent) {
7366 $formats_nav .= '(initial)';
7367 } elsif (@$parents == 1) {
7368 # single parent commit
7371 $cgi->a({-href => href(action=>"commit",
7373 esc_html(substr($parent, 0, 7))) .
7380 $cgi->a({-href => href(action=>"commit",
7382 esc_html(substr($_, 0, 7)));
7386 if (gitweb_check_feature('patches') && @$parents <= 1) {
7387 $formats_nav .= " | " .
7388 $cgi->a({-href => href(action=>"patch", -replay=>1)},
7392 if (!defined $parent) {
7396 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
7398 (@$parents <= 1 ? $parent : '-c'),
7400 or die_error(500, "Open git-diff-tree failed");
7401 @difftree = map { chomp; $_ } <$fd>;
7402 close $fd or die_error(404, "Reading git-diff-tree failed");
7404 # non-textual hash id's can be cached
7406 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
7409 my $refs = git_get_references();
7410 my $ref = format_ref_marker($refs, $co{'id'});
7412 git_header_html(undef, $expires);
7413 git_print_page_nav('commit', '',
7414 $hash, $co{'tree'}, $hash,
7417 if (defined $co{'parent'}) {
7418 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
7420 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
7422 print "<div class=\"title_text\">\n" .
7423 "<table class=\"object_header\">\n";
7424 git_print_authorship_rows(\%co);
7425 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
7428 "<td class=\"sha1\">" .
7429 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
7430 class => "list"}, $co{'tree'}) .
7432 "<td class=\"link\">" .
7433 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
7435 my $snapshot_links = format_snapshot_links($hash);
7436 if (defined $snapshot_links) {
7437 print " | " . $snapshot_links;
7442 foreach my $par (@$parents) {
7445 "<td class=\"sha1\">" .
7446 $cgi->a({-href => href(action=>"commit", hash=>$par),
7447 class => "list"}, $par) .
7449 "<td class=\"link\">" .
7450 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
7452 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
7459 print "<div class=\"page_body\">\n";
7460 git_print_log($co{'comment'});
7463 git_difftree_body(\@difftree, $hash, @$parents);
7469 # object is defined by:
7470 # - hash or hash_base alone
7471 # - hash_base and file_name
7474 # - hash or hash_base alone
7475 if ($hash || ($hash_base && !defined $file_name)) {
7476 my $object_id = $hash || $hash_base;
7478 open my $fd, "-|", quote_command(
7479 git_cmd(), 'cat-file', '-t', $object_id) . ' 2> /dev/null'
7480 or die_error(404, "Object does not exist");
7484 or die_error(404, "Object does not exist");
7486 # - hash_base and file_name
7487 } elsif ($hash_base && defined $file_name) {
7488 $file_name =~ s,/+$,,;
7490 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
7491 or die_error(404, "Base object does not exist");
7493 # here errors should not happen
7494 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
7495 or die_error(500, "Open git-ls-tree failed");
7499 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
7500 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
7501 die_error(404, "File or directory for given base does not exist");
7506 die_error(400, "Not enough information to find object");
7509 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
7510 hash=>$hash, hash_base=>$hash_base,
7511 file_name=>$file_name),
7512 -status => '302 Found');
7516 my $format = shift || 'html';
7517 my $diff_style = $input_params{'diff_style'} || 'inline';
7524 # preparing $fd and %diffinfo for git_patchset_body
7526 if (defined $hash_base && defined $hash_parent_base) {
7527 if (defined $file_name) {
7529 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
7530 $hash_parent_base, $hash_base,
7531 "--", (defined $file_parent ? $file_parent : ()), $file_name
7532 or die_error(500, "Open git-diff-tree failed");
7533 @difftree = map { chomp; $_ } <$fd>;
7535 or die_error(404, "Reading git-diff-tree failed");
7537 or die_error(404, "Blob diff not found");
7539 } elsif (defined $hash &&
7540 $hash =~ /[0-9a-fA-F]{40}/) {
7541 # try to find filename from $hash
7543 # read filtered raw output
7544 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
7545 $hash_parent_base, $hash_base, "--"
7546 or die_error(500, "Open git-diff-tree failed");
7548 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
7550 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
7551 map { chomp; $_ } <$fd>;
7553 or die_error(404, "Reading git-diff-tree failed");
7555 or die_error(404, "Blob diff not found");
7558 die_error(400, "Missing one of the blob diff parameters");
7561 if (@difftree > 1) {
7562 die_error(400, "Ambiguous blob diff specification");
7565 %diffinfo = parse_difftree_raw_line($difftree[0]);
7566 $file_parent ||= $diffinfo{'from_file'} || $file_name;
7567 $file_name ||= $diffinfo{'to_file'};
7569 $hash_parent ||= $diffinfo{'from_id'};
7570 $hash ||= $diffinfo{'to_id'};
7572 # non-textual hash id's can be cached
7573 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
7574 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
7579 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
7580 '-p', ($format eq 'html' ? "--full-index" : ()),
7581 $hash_parent_base, $hash_base,
7582 "--", (defined $file_parent ? $file_parent : ()), $file_name
7583 or die_error(500, "Open git-diff-tree failed");
7586 # old/legacy style URI -- not generated anymore since 1.4.3.
7588 die_error('404 Not Found', "Missing one of the blob diff parameters")
7592 if ($format eq 'html') {
7594 $cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},
7596 $formats_nav .= diff_style_nav($diff_style);
7597 git_header_html(undef, $expires);
7598 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
7599 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
7600 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
7602 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
7603 print "<div class=\"title\">".esc_html("$hash vs $hash_parent")."</div>\n";
7605 if (defined $file_name) {
7606 git_print_page_path($file_name, "blob", $hash_base);
7608 print "<div class=\"page_path\"></div>\n";
7611 } elsif ($format eq 'plain') {
7613 -type => 'text/plain',
7614 -charset => 'utf-8',
7615 -expires => $expires,
7616 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
7618 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
7621 die_error(400, "Unknown blobdiff format");
7625 if ($format eq 'html') {
7626 print "<div class=\"page_body\">\n";
7628 git_patchset_body($fd, $diff_style,
7629 [ \%diffinfo ], $hash_base, $hash_parent_base);
7632 print "</div>\n"; # class="page_body"
7636 while (my $line = <$fd>) {
7637 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
7638 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
7642 last if $line =~ m!^\+\+\+!;
7650 sub git_blobdiff_plain {
7651 git_blobdiff('plain');
7654 # assumes that it is added as later part of already existing navigation,
7655 # so it returns "| foo | bar" rather than just "foo | bar"
7656 sub diff_style_nav {
7657 my ($diff_style, $is_combined) = @_;
7658 $diff_style ||= 'inline';
7660 return "" if ($is_combined);
7662 my @styles = (inline => 'inline', 'sidebyside' => 'side by side');
7663 my %styles = @styles;
7665 @styles[ map { $_ * 2 } 0..$#styles/2 ];
7670 $_ eq $diff_style ? $styles{$_} :
7671 $cgi->a({-href => href(-replay=>1, diff_style => $_)}, $styles{$_})
7675 sub git_commitdiff {
7677 my $format = $params{-format} || 'html';
7678 my $diff_style = $input_params{'diff_style'} || 'inline';
7680 my ($patch_max) = gitweb_get_feature('patches');
7681 if ($format eq 'patch') {
7682 die_error(403, "Patch view not allowed") unless $patch_max;
7685 $hash ||= $hash_base || "HEAD";
7686 my %co = parse_commit($hash)
7687 or die_error(404, "Unknown commit object");
7689 # choose format for commitdiff for merge
7690 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
7691 $hash_parent = '--cc';
7693 # we need to prepare $formats_nav before almost any parameter munging
7695 if ($format eq 'html') {
7697 $cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
7699 if ($patch_max && @{$co{'parents'}} <= 1) {
7700 $formats_nav .= " | " .
7701 $cgi->a({-href => href(action=>"patch", -replay=>1)},
7704 $formats_nav .= diff_style_nav($diff_style, @{$co{'parents'}} > 1);
7706 if (defined $hash_parent &&
7707 $hash_parent ne '-c' && $hash_parent ne '--cc') {
7708 # commitdiff with two commits given
7709 my $hash_parent_short = $hash_parent;
7710 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
7711 $hash_parent_short = substr($hash_parent, 0, 7);
7715 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
7716 if ($co{'parents'}[$i] eq $hash_parent) {
7717 $formats_nav .= ' parent ' . ($i+1);
7721 $formats_nav .= ': ' .
7722 $cgi->a({-href => href(-replay=>1,
7723 hash=>$hash_parent, hash_base=>undef)},
7724 esc_html($hash_parent_short)) .
7726 } elsif (!$co{'parent'}) {
7728 $formats_nav .= ' (initial)';
7729 } elsif (scalar @{$co{'parents'}} == 1) {
7730 # single parent commit
7733 $cgi->a({-href => href(-replay=>1,
7734 hash=>$co{'parent'}, hash_base=>undef)},
7735 esc_html(substr($co{'parent'}, 0, 7))) .
7739 if ($hash_parent eq '--cc') {
7740 $formats_nav .= ' | ' .
7741 $cgi->a({-href => href(-replay=>1,
7742 hash=>$hash, hash_parent=>'-c')},
7744 } else { # $hash_parent eq '-c'
7745 $formats_nav .= ' | ' .
7746 $cgi->a({-href => href(-replay=>1,
7747 hash=>$hash, hash_parent=>'--cc')},
7753 $cgi->a({-href => href(-replay=>1,
7754 hash=>$_, hash_base=>undef)},
7755 esc_html(substr($_, 0, 7)));
7756 } @{$co{'parents'}} ) .
7761 my $hash_parent_param = $hash_parent;
7762 if (!defined $hash_parent_param) {
7763 # --cc for multiple parents, --root for parentless
7764 $hash_parent_param =
7765 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
7771 if ($format eq 'html') {
7772 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
7773 "--no-commit-id", "--patch-with-raw", "--full-index",
7774 $hash_parent_param, $hash, "--"
7775 or die_error(500, "Open git-diff-tree failed");
7777 while (my $line = <$fd>) {
7779 # empty line ends raw part of diff-tree output
7781 push @difftree, scalar parse_difftree_raw_line($line);
7784 } elsif ($format eq 'plain') {
7785 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
7786 '-p', $hash_parent_param, $hash, "--"
7787 or die_error(500, "Open git-diff-tree failed");
7788 } elsif ($format eq 'patch') {
7789 # For commit ranges, we limit the output to the number of
7790 # patches specified in the 'patches' feature.
7791 # For single commits, we limit the output to a single patch,
7792 # diverging from the git-format-patch default.
7793 my @commit_spec = ();
7795 if ($patch_max > 0) {
7796 push @commit_spec, "-$patch_max";
7798 push @commit_spec, '-n', "$hash_parent..$hash";
7800 if ($params{-single}) {
7801 push @commit_spec, '-1';
7803 if ($patch_max > 0) {
7804 push @commit_spec, "-$patch_max";
7806 push @commit_spec, "-n";
7808 push @commit_spec, '--root', $hash;
7810 open $fd, "-|", git_cmd(), "format-patch", @diff_opts,
7811 '--encoding=utf8', '--stdout', @commit_spec
7812 or die_error(500, "Open git-format-patch failed");
7814 die_error(400, "Unknown commitdiff format");
7817 # non-textual hash id's can be cached
7819 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
7823 # write commit message
7824 if ($format eq 'html') {
7825 my $refs = git_get_references();
7826 my $ref = format_ref_marker($refs, $co{'id'});
7828 git_header_html(undef, $expires);
7829 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
7830 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
7831 print "<div class=\"title_text\">\n" .
7832 "<table class=\"object_header\">\n";
7833 git_print_authorship_rows(\%co);
7836 print "<div class=\"page_body\">\n";
7837 if (@{$co{'comment'}} > 1) {
7838 print "<div class=\"log\">\n";
7839 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
7840 print "</div>\n"; # class="log"
7843 } elsif ($format eq 'plain') {
7844 my $refs = git_get_references("tags");
7845 my $tagname = git_get_rev_name_tags($hash);
7846 my $filename = basename($project) . "-$hash.patch";
7849 -type => 'text/plain',
7850 -charset => 'utf-8',
7851 -expires => $expires,
7852 -content_disposition => 'inline; filename="' . "$filename" . '"');
7853 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
7854 print "From: " . to_utf8($co{'author'}) . "\n";
7855 print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
7856 print "Subject: " . to_utf8($co{'title'}) . "\n";
7858 print "X-Git-Tag: $tagname\n" if $tagname;
7859 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
7861 foreach my $line (@{$co{'comment'}}) {
7862 print to_utf8($line) . "\n";
7865 } elsif ($format eq 'patch') {
7866 my $filename = basename($project) . "-$hash.patch";
7869 -type => 'text/plain',
7870 -charset => 'utf-8',
7871 -expires => $expires,
7872 -content_disposition => 'inline; filename="' . "$filename" . '"');
7876 if ($format eq 'html') {
7877 my $use_parents = !defined $hash_parent ||
7878 $hash_parent eq '-c' || $hash_parent eq '--cc';
7879 git_difftree_body(\@difftree, $hash,
7880 $use_parents ? @{$co{'parents'}} : $hash_parent);
7883 git_patchset_body($fd, $diff_style,
7885 $use_parents ? @{$co{'parents'}} : $hash_parent);
7887 print "</div>\n"; # class="page_body"
7890 } elsif ($format eq 'plain') {
7894 or print "Reading git-diff-tree failed\n";
7895 } elsif ($format eq 'patch') {
7899 or print "Reading git-format-patch failed\n";
7903 sub git_commitdiff_plain {
7904 git_commitdiff(-format => 'plain');
7907 # format-patch-style patches
7909 git_commitdiff(-format => 'patch', -single => 1);
7913 git_commitdiff(-format => 'patch');
7917 git_log_generic('history', \&git_history_body,
7918 $hash_base, $hash_parent_base,
7923 $searchtype ||= 'commit';
7925 # check if appropriate features are enabled
7926 gitweb_check_feature('search')
7927 or die_error(403, "Search is disabled");
7928 if ($searchtype eq 'pickaxe') {
7929 # pickaxe may take all resources of your box and run for several minutes
7930 # with every query - so decide by yourself how public you make this feature
7931 gitweb_check_feature('pickaxe')
7932 or die_error(403, "Pickaxe search is disabled");
7934 if ($searchtype eq 'grep') {
7935 # grep search might be potentially CPU-intensive, too
7936 gitweb_check_feature('grep')
7937 or die_error(403, "Grep search is disabled");
7940 if (!defined $searchtext) {
7941 die_error(400, "Text field is empty");
7943 if (!defined $hash) {
7944 $hash = git_get_head_hash($project);
7946 my %co = parse_commit($hash);
7948 die_error(404, "Unknown commit object");
7950 if (!defined $page) {
7954 if ($searchtype eq 'commit' ||
7955 $searchtype eq 'author' ||
7956 $searchtype eq 'committer') {
7957 git_search_message(%co);
7958 } elsif ($searchtype eq 'pickaxe') {
7959 git_search_changes(%co);
7960 } elsif ($searchtype eq 'grep') {
7961 git_search_files(%co);
7963 die_error(400, "Unknown search type");
7967 sub git_search_help {
7969 git_print_page_nav('','', $hash,$hash,$hash);
7971 <p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without
7972 regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,
7973 the pattern entered is recognized as the POSIX extended
7974 <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case
7977 <dt><b>commit</b></dt>
7978 <dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
7980 my $have_grep = gitweb_check_feature('grep');
7983 <dt><b>grep</b></dt>
7984 <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
7985 a different one) are searched for the given pattern. On large trees, this search can take
7986 a while and put some strain on the server, so please use it with some consideration. Note that
7987 due to git-grep peculiarity, currently if regexp mode is turned off, the matches are
7988 case-sensitive.</dd>
7992 <dt><b>author</b></dt>
7993 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>
7994 <dt><b>committer</b></dt>
7995 <dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>
7997 my $have_pickaxe = gitweb_check_feature('pickaxe');
7998 if ($have_pickaxe) {
8000 <dt><b>pickaxe</b></dt>
8001 <dd>All commits that caused the string to appear or disappear from any file (changes that
8002 added, removed or "modified" the string) will be listed. This search can take a while and
8003 takes a lot of strain on the server, so please use it wisely. Note that since you may be
8004 interested even in changes just changing the case as well, this search is case sensitive.</dd>
8012 git_log_generic('shortlog', \&git_shortlog_body,
8013 $hash, $hash_parent);
8016 ## ......................................................................
8017 ## feeds (RSS, Atom; OPML)
8020 my $format = shift || 'atom';
8021 my $have_blame = gitweb_check_feature('blame');
8023 # Atom: http://www.atomenabled.org/developers/syndication/
8024 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
8025 if ($format ne 'rss' && $format ne 'atom') {
8026 die_error(400, "Unknown web feed format");
8029 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
8030 my $head = $hash || 'HEAD';
8031 my @commitlist = parse_commits($head, 150, 0, $file_name);
8035 my $content_type = "application/$format+xml";
8036 if (defined $cgi->http('HTTP_ACCEPT') &&
8037 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
8038 # browser (feed reader) prefers text/xml
8039 $content_type = 'text/xml';
8041 if (defined($commitlist[0])) {
8042 %latest_commit = %{$commitlist[0]};
8043 my $latest_epoch = $latest_commit{'committer_epoch'};
8044 exit_if_unmodified_since($latest_epoch);
8045 %latest_date = parse_date($latest_epoch, $latest_commit{'committer_tz'});
8048 -type => $content_type,
8049 -charset => 'utf-8',
8050 %latest_date ? (-last_modified => $latest_date{'rfc2822'}) : (),
8051 -status => '200 OK');
8053 # Optimization: skip generating the body if client asks only
8054 # for Last-Modified date.
8055 return if ($cgi->request_method() eq 'HEAD');
8058 my $title = "$site_name - $project/$action";
8059 my $feed_type = 'log';
8060 if (defined $hash) {
8061 $title .= " - '$hash'";
8062 $feed_type = 'branch log';
8063 if (defined $file_name) {
8064 $title .= " :: $file_name";
8065 $feed_type = 'history';
8067 } elsif (defined $file_name) {
8068 $title .= " - $file_name";
8069 $feed_type = 'history';
8071 $title .= " $feed_type";
8072 $title = esc_html($title);
8073 my $descr = git_get_project_description($project);
8074 if (defined $descr) {
8075 $descr = esc_html($descr);
8077 $descr = "$project " .
8078 ($format eq 'rss' ? 'RSS' : 'Atom') .
8081 my $owner = git_get_project_owner($project);
8082 $owner = esc_html($owner);
8086 if (defined $file_name) {
8087 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
8088 } elsif (defined $hash) {
8089 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
8091 $alt_url = href(-full=>1, action=>"summary");
8093 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
8094 if ($format eq 'rss') {
8096 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
8099 print "<title>$title</title>\n" .
8100 "<link>$alt_url</link>\n" .
8101 "<description>$descr</description>\n" .
8102 "<language>en</language>\n" .
8103 # project owner is responsible for 'editorial' content
8104 "<managingEditor>$owner</managingEditor>\n";
8105 if (defined $logo || defined $favicon) {
8106 # prefer the logo to the favicon, since RSS
8107 # doesn't allow both
8108 my $img = esc_url($logo || $favicon);
8110 "<url>$img</url>\n" .
8111 "<title>$title</title>\n" .
8112 "<link>$alt_url</link>\n" .
8116 print "<pubDate>$latest_date{'rfc2822'}</pubDate>\n";
8117 print "<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";
8119 print "<generator>gitweb v.$version/$git_version</generator>\n";
8120 } elsif ($format eq 'atom') {
8122 <feed xmlns="http://www.w3.org/2005/Atom">
8124 print "<title>$title</title>\n" .
8125 "<subtitle>$descr</subtitle>\n" .
8126 '<link rel="alternate" type="text/html" href="' .
8127 $alt_url . '" />' . "\n" .
8128 '<link rel="self" type="' . $content_type . '" href="' .
8129 $cgi->self_url() . '" />' . "\n" .
8130 "<id>" . href(-full=>1) . "</id>\n" .
8131 # use project owner for feed author
8132 "<author><name>$owner</name></author>\n";
8133 if (defined $favicon) {
8134 print "<icon>" . esc_url($favicon) . "</icon>\n";
8136 if (defined $logo) {
8137 # not twice as wide as tall: 72 x 27 pixels
8138 print "<logo>" . esc_url($logo) . "</logo>\n";
8140 if (! %latest_date) {
8141 # dummy date to keep the feed valid until commits trickle in:
8142 print "<updated>1970-01-01T00:00:00Z</updated>\n";
8144 print "<updated>$latest_date{'iso-8601'}</updated>\n";
8146 print "<generator version='$version/$git_version'>gitweb</generator>\n";
8150 for (my $i = 0; $i <= $#commitlist; $i++) {
8151 my %co = %{$commitlist[$i]};
8152 my $commit = $co{'id'};
8153 # we read 150, we always show 30 and the ones more recent than 48 hours
8154 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
8157 my %cd = parse_date($co{'author_epoch'}, $co{'author_tz'});
8159 # get list of changed files
8160 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
8161 $co{'parent'} || "--root",
8162 $co{'id'}, "--", (defined $file_name ? $file_name : ())
8164 my @difftree = map { chomp; $_ } <$fd>;
8168 # print element (entry, item)
8169 my $co_url = href(-full=>1, action=>"commitdiff", hash=>$commit);
8170 if ($format eq 'rss') {
8172 "<title>" . esc_html($co{'title'}) . "</title>\n" .
8173 "<author>" . esc_html($co{'author'}) . "</author>\n" .
8174 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
8175 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
8176 "<link>$co_url</link>\n" .
8177 "<description>" . esc_html($co{'title'}) . "</description>\n" .
8178 "<content:encoded>" .
8180 } elsif ($format eq 'atom') {
8182 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
8183 "<updated>$cd{'iso-8601'}</updated>\n" .
8185 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
8186 if ($co{'author_email'}) {
8187 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
8189 print "</author>\n" .
8190 # use committer for contributor
8192 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
8193 if ($co{'committer_email'}) {
8194 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
8196 print "</contributor>\n" .
8197 "<published>$cd{'iso-8601'}</published>\n" .
8198 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
8199 "<id>$co_url</id>\n" .
8200 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
8201 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
8203 my $comment = $co{'comment'};
8205 foreach my $line (@$comment) {
8206 $line = esc_html($line);
8209 print "</pre><ul>\n";
8210 foreach my $difftree_line (@difftree) {
8211 my %difftree = parse_difftree_raw_line($difftree_line);
8212 next if !$difftree{'from_id'};
8214 my $file = $difftree{'file'} || $difftree{'to_file'};
8218 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
8219 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
8220 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
8221 file_name=>$file, file_parent=>$difftree{'from_file'}),
8222 -title => "diff"}, 'D');
8224 print $cgi->a({-href => href(-full=>1, action=>"blame",
8225 file_name=>$file, hash_base=>$commit),
8226 -title => "blame"}, 'B');
8228 # if this is not a feed of a file history
8229 if (!defined $file_name || $file_name ne $file) {
8230 print $cgi->a({-href => href(-full=>1, action=>"history",
8231 file_name=>$file, hash=>$commit),
8232 -title => "history"}, 'H');
8234 $file = esc_path($file);
8238 if ($format eq 'rss') {
8239 print "</ul>]]>\n" .
8240 "</content:encoded>\n" .
8242 } elsif ($format eq 'atom') {
8243 print "</ul>\n</div>\n" .
8250 if ($format eq 'rss') {
8251 print "</channel>\n</rss>\n";
8252 } elsif ($format eq 'atom') {
8266 my @list = git_get_projects_list($project_filter, $strict_export);
8268 die_error(404, "No projects found");
8272 -type => 'text/xml',
8273 -charset => 'utf-8',
8274 -content_disposition => 'inline; filename="opml.xml"');
8276 my $title = esc_html($site_name);
8277 my $filter = " within subdirectory ";
8278 if (defined $project_filter) {
8279 $filter .= esc_html($project_filter);
8284 <?xml version="1.0" encoding="utf-8"?>
8285 <opml version="1.0">
8287 <title>$title OPML Export$filter</title>
8290 <outline text="git RSS feeds">
8293 foreach my $pr (@list) {
8295 my $head = git_get_head_hash($proj{'path'});
8296 if (!defined $head) {
8299 $git_dir = "$projectroot/$proj{'path'}";
8300 my %co = parse_commit($head);
8305 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
8306 my $rss = href('project' => $proj{'path'}, 'action' => 'rss', -full => 1);
8307 my $html = href('project' => $proj{'path'}, 'action' => 'summary', -full => 1);
8308 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";