Merge branch 'bc/fix-cherry-pick-root'
[git/dkf.git] / gitweb / gitweb.perl
blobe5910ce8f93cbfb92fdb2ed63c65140d9b8b39b3
1 #!/usr/bin/perl
3 # gitweb - simple web interface to track changes in git repositories
5 # (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org>
6 # (C) 2005, Christian Gierke
8 # This program is licensed under the GPLv2
10 use strict;
11 use warnings;
12 use CGI qw(:standard :escapeHTML -nosticky);
13 use CGI::Util qw(unescape);
14 use CGI::Carp qw(fatalsToBrowser set_message);
15 use Encode;
16 use Fcntl ':mode';
17 use File::Find qw();
18 use File::Basename qw(basename);
19 binmode STDOUT, ':utf8';
21 our $t0;
22 if (eval { require Time::HiRes; 1; }) {
23 $t0 = [Time::HiRes::gettimeofday()];
25 our $number_of_git_cmds = 0;
27 BEGIN {
28 CGI->compile() if $ENV{'MOD_PERL'};
31 our $version = "++GIT_VERSION++";
33 our ($my_url, $my_uri, $base_url, $path_info, $home_link);
34 sub evaluate_uri {
35 our $cgi;
37 our $my_url = $cgi->url();
38 our $my_uri = $cgi->url(-absolute => 1);
40 # Base URL for relative URLs in gitweb ($logo, $favicon, ...),
41 # needed and used only for URLs with nonempty PATH_INFO
42 our $base_url = $my_url;
44 # When the script is used as DirectoryIndex, the URL does not contain the name
45 # of the script file itself, and $cgi->url() fails to strip PATH_INFO, so we
46 # have to do it ourselves. We make $path_info global because it's also used
47 # later on.
49 # Another issue with the script being the DirectoryIndex is that the resulting
50 # $my_url data is not the full script URL: this is good, because we want
51 # generated links to keep implying the script name if it wasn't explicitly
52 # indicated in the URL we're handling, but it means that $my_url cannot be used
53 # as base URL.
54 # Therefore, if we needed to strip PATH_INFO, then we know that we have
55 # to build the base URL ourselves:
56 our $path_info = $ENV{"PATH_INFO"};
57 if ($path_info) {
58 if ($my_url =~ s,\Q$path_info\E$,, &&
59 $my_uri =~ s,\Q$path_info\E$,, &&
60 defined $ENV{'SCRIPT_NAME'}) {
61 $base_url = $cgi->url(-base => 1) . $ENV{'SCRIPT_NAME'};
65 # target of the home link on top of all pages
66 our $home_link = $my_uri || "/";
69 # core git executable to use
70 # this can just be "git" if your webserver has a sensible PATH
71 our $GIT = "++GIT_BINDIR++/git";
73 # absolute fs-path which will be prepended to the project path
74 #our $projectroot = "/pub/scm";
75 our $projectroot = "++GITWEB_PROJECTROOT++";
77 # fs traversing limit for getting project list
78 # the number is relative to the projectroot
79 our $project_maxdepth = "++GITWEB_PROJECT_MAXDEPTH++";
81 # string of the home link on top of all pages
82 our $home_link_str = "++GITWEB_HOME_LINK_STR++";
84 # name of your site or organization to appear in page titles
85 # replace this with something more descriptive for clearer bookmarks
86 our $site_name = "++GITWEB_SITENAME++"
87 || ($ENV{'SERVER_NAME'} || "Untitled") . " Git";
89 # filename of html text to include at top of each page
90 our $site_header = "++GITWEB_SITE_HEADER++";
91 # html text to include at home page
92 our $home_text = "++GITWEB_HOMETEXT++";
93 # filename of html text to include at bottom of each page
94 our $site_footer = "++GITWEB_SITE_FOOTER++";
96 # URI of stylesheets
97 our @stylesheets = ("++GITWEB_CSS++");
98 # URI of a single stylesheet, which can be overridden in GITWEB_CONFIG.
99 our $stylesheet = undef;
100 # URI of GIT logo (72x27 size)
101 our $logo = "++GITWEB_LOGO++";
102 # URI of GIT favicon, assumed to be image/png type
103 our $favicon = "++GITWEB_FAVICON++";
104 # URI of gitweb.js (JavaScript code for gitweb)
105 our $javascript = "++GITWEB_JS++";
107 # URI and label (title) of GIT logo link
108 #our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
109 #our $logo_label = "git documentation";
110 our $logo_url = "http://git-scm.com/";
111 our $logo_label = "git homepage";
113 # source of projects list
114 our $projects_list = "++GITWEB_LIST++";
116 # the width (in characters) of the projects list "Description" column
117 our $projects_list_description_width = 25;
119 # default order of projects list
120 # valid values are none, project, descr, owner, and age
121 our $default_projects_order = "project";
123 # show repository only if this file exists
124 # (only effective if this variable evaluates to true)
125 our $export_ok = "++GITWEB_EXPORT_OK++";
127 # show repository only if this subroutine returns true
128 # when given the path to the project, for example:
129 # sub { return -e "$_[0]/git-daemon-export-ok"; }
130 our $export_auth_hook = undef;
132 # only allow viewing of repositories also shown on the overview page
133 our $strict_export = "++GITWEB_STRICT_EXPORT++";
135 # list of git base URLs used for URL to where fetch project from,
136 # i.e. full URL is "$git_base_url/$project"
137 our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
139 # default blob_plain mimetype and default charset for text/plain blob
140 our $default_blob_plain_mimetype = 'text/plain';
141 our $default_text_plain_charset = undef;
143 # file to use for guessing MIME types before trying /etc/mime.types
144 # (relative to the current git repository)
145 our $mimetypes_file = undef;
147 # assume this charset if line contains non-UTF-8 characters;
148 # it should be valid encoding (see Encoding::Supported(3pm) for list),
149 # for which encoding all byte sequences are valid, for example
150 # 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
151 # could be even 'utf-8' for the old behavior)
152 our $fallback_encoding = 'latin1';
154 # rename detection options for git-diff and git-diff-tree
155 # - default is '-M', with the cost proportional to
156 # (number of removed files) * (number of new files).
157 # - more costly is '-C' (which implies '-M'), with the cost proportional to
158 # (number of changed files + number of removed files) * (number of new files)
159 # - even more costly is '-C', '--find-copies-harder' with cost
160 # (number of files in the original tree) * (number of new files)
161 # - one might want to include '-B' option, e.g. '-B', '-M'
162 our @diff_opts = ('-M'); # taken from git_commit
164 # Disables features that would allow repository owners to inject script into
165 # the gitweb domain.
166 our $prevent_xss = 0;
168 # Path to the highlight executable to use (must be the one from
169 # http://www.andre-simon.de due to assumptions about parameters and output).
170 # Useful if highlight is not installed on your webserver's PATH.
171 # [Default: highlight]
172 our $highlight_bin = "++HIGHLIGHT_BIN++";
174 # information about snapshot formats that gitweb is capable of serving
175 our %known_snapshot_formats = (
176 # name => {
177 # 'display' => display name,
178 # 'type' => mime type,
179 # 'suffix' => filename suffix,
180 # 'format' => --format for git-archive,
181 # 'compressor' => [compressor command and arguments]
182 # (array reference, optional)
183 # 'disabled' => boolean (optional)}
185 'tgz' => {
186 'display' => 'tar.gz',
187 'type' => 'application/x-gzip',
188 'suffix' => '.tar.gz',
189 'format' => 'tar',
190 'compressor' => ['gzip']},
192 'tbz2' => {
193 'display' => 'tar.bz2',
194 'type' => 'application/x-bzip2',
195 'suffix' => '.tar.bz2',
196 'format' => 'tar',
197 'compressor' => ['bzip2']},
199 'txz' => {
200 'display' => 'tar.xz',
201 'type' => 'application/x-xz',
202 'suffix' => '.tar.xz',
203 'format' => 'tar',
204 'compressor' => ['xz'],
205 'disabled' => 1},
207 'zip' => {
208 'display' => 'zip',
209 'type' => 'application/x-zip',
210 'suffix' => '.zip',
211 'format' => 'zip'},
214 # Aliases so we understand old gitweb.snapshot values in repository
215 # configuration.
216 our %known_snapshot_format_aliases = (
217 'gzip' => 'tgz',
218 'bzip2' => 'tbz2',
219 'xz' => 'txz',
221 # backward compatibility: legacy gitweb config support
222 'x-gzip' => undef, 'gz' => undef,
223 'x-bzip2' => undef, 'bz2' => undef,
224 'x-zip' => undef, '' => undef,
227 # Pixel sizes for icons and avatars. If the default font sizes or lineheights
228 # are changed, it may be appropriate to change these values too via
229 # $GITWEB_CONFIG.
230 our %avatar_size = (
231 'default' => 16,
232 'double' => 32
235 # Used to set the maximum load that we will still respond to gitweb queries.
236 # If server load exceed this value then return "503 server busy" error.
237 # If gitweb cannot determined server load, it is taken to be 0.
238 # Leave it undefined (or set to 'undef') to turn off load checking.
239 our $maxload = 300;
241 # configuration for 'highlight' (http://www.andre-simon.de/)
242 # match by basename
243 our %highlight_basename = (
244 #'Program' => 'py',
245 #'Library' => 'py',
246 'SConstruct' => 'py', # SCons equivalent of Makefile
247 'Makefile' => 'make',
249 # match by extension
250 our %highlight_ext = (
251 # main extensions, defining name of syntax;
252 # see files in /usr/share/highlight/langDefs/ directory
253 map { $_ => $_ }
254 qw(py c cpp rb java css php sh pl js tex bib xml awk bat ini spec tcl),
255 # alternate extensions, see /etc/highlight/filetypes.conf
256 'h' => 'c',
257 map { $_ => 'cpp' } qw(cxx c++ cc),
258 map { $_ => 'php' } qw(php3 php4),
259 map { $_ => 'pl' } qw(perl pm), # perhaps also 'cgi'
260 'mak' => 'make',
261 map { $_ => 'xml' } qw(xhtml html htm),
264 # You define site-wide feature defaults here; override them with
265 # $GITWEB_CONFIG as necessary.
266 our %feature = (
267 # feature => {
268 # 'sub' => feature-sub (subroutine),
269 # 'override' => allow-override (boolean),
270 # 'default' => [ default options...] (array reference)}
272 # if feature is overridable (it means that allow-override has true value),
273 # then feature-sub will be called with default options as parameters;
274 # return value of feature-sub indicates if to enable specified feature
276 # if there is no 'sub' key (no feature-sub), then feature cannot be
277 # overridden
279 # use gitweb_get_feature(<feature>) to retrieve the <feature> value
280 # (an array) or gitweb_check_feature(<feature>) to check if <feature>
281 # is enabled
283 # Enable the 'blame' blob view, showing the last commit that modified
284 # each line in the file. This can be very CPU-intensive.
286 # To enable system wide have in $GITWEB_CONFIG
287 # $feature{'blame'}{'default'} = [1];
288 # To have project specific config enable override in $GITWEB_CONFIG
289 # $feature{'blame'}{'override'} = 1;
290 # and in project config gitweb.blame = 0|1;
291 'blame' => {
292 'sub' => sub { feature_bool('blame', @_) },
293 'override' => 0,
294 'default' => [0]},
296 # Enable the 'snapshot' link, providing a compressed archive of any
297 # tree. This can potentially generate high traffic if you have large
298 # project.
300 # Value is a list of formats defined in %known_snapshot_formats that
301 # you wish to offer.
302 # To disable system wide have in $GITWEB_CONFIG
303 # $feature{'snapshot'}{'default'} = [];
304 # To have project specific config enable override in $GITWEB_CONFIG
305 # $feature{'snapshot'}{'override'} = 1;
306 # and in project config, a comma-separated list of formats or "none"
307 # to disable. Example: gitweb.snapshot = tbz2,zip;
308 'snapshot' => {
309 'sub' => \&feature_snapshot,
310 'override' => 0,
311 'default' => ['tgz']},
313 # Enable text search, which will list the commits which match author,
314 # committer or commit text to a given string. Enabled by default.
315 # Project specific override is not supported.
316 'search' => {
317 'override' => 0,
318 'default' => [1]},
320 # Enable grep search, which will list the files in currently selected
321 # tree containing the given string. Enabled by default. This can be
322 # potentially CPU-intensive, of course.
324 # To enable system wide have in $GITWEB_CONFIG
325 # $feature{'grep'}{'default'} = [1];
326 # To have project specific config enable override in $GITWEB_CONFIG
327 # $feature{'grep'}{'override'} = 1;
328 # and in project config gitweb.grep = 0|1;
329 'grep' => {
330 'sub' => sub { feature_bool('grep', @_) },
331 'override' => 0,
332 'default' => [1]},
334 # Enable the pickaxe search, which will list the commits that modified
335 # a given string in a file. This can be practical and quite faster
336 # alternative to 'blame', but still potentially CPU-intensive.
338 # To enable system wide have in $GITWEB_CONFIG
339 # $feature{'pickaxe'}{'default'} = [1];
340 # To have project specific config enable override in $GITWEB_CONFIG
341 # $feature{'pickaxe'}{'override'} = 1;
342 # and in project config gitweb.pickaxe = 0|1;
343 'pickaxe' => {
344 'sub' => sub { feature_bool('pickaxe', @_) },
345 'override' => 0,
346 'default' => [1]},
348 # Enable showing size of blobs in a 'tree' view, in a separate
349 # column, similar to what 'ls -l' does. This cost a bit of IO.
351 # To disable system wide have in $GITWEB_CONFIG
352 # $feature{'show-sizes'}{'default'} = [0];
353 # To have project specific config enable override in $GITWEB_CONFIG
354 # $feature{'show-sizes'}{'override'} = 1;
355 # and in project config gitweb.showsizes = 0|1;
356 'show-sizes' => {
357 'sub' => sub { feature_bool('showsizes', @_) },
358 'override' => 0,
359 'default' => [1]},
361 # Make gitweb use an alternative format of the URLs which can be
362 # more readable and natural-looking: project name is embedded
363 # directly in the path and the query string contains other
364 # auxiliary information. All gitweb installations recognize
365 # URL in either format; this configures in which formats gitweb
366 # generates links.
368 # To enable system wide have in $GITWEB_CONFIG
369 # $feature{'pathinfo'}{'default'} = [1];
370 # Project specific override is not supported.
372 # Note that you will need to change the default location of CSS,
373 # favicon, logo and possibly other files to an absolute URL. Also,
374 # if gitweb.cgi serves as your indexfile, you will need to force
375 # $my_uri to contain the script name in your $GITWEB_CONFIG.
376 'pathinfo' => {
377 'override' => 0,
378 'default' => [0]},
380 # Make gitweb consider projects in project root subdirectories
381 # to be forks of existing projects. Given project $projname.git,
382 # projects matching $projname/*.git will not be shown in the main
383 # projects list, instead a '+' mark will be added to $projname
384 # there and a 'forks' view will be enabled for the project, listing
385 # all the forks. If project list is taken from a file, forks have
386 # to be listed after the main project.
388 # To enable system wide have in $GITWEB_CONFIG
389 # $feature{'forks'}{'default'} = [1];
390 # Project specific override is not supported.
391 'forks' => {
392 'override' => 0,
393 'default' => [0]},
395 # Insert custom links to the action bar of all project pages.
396 # This enables you mainly to link to third-party scripts integrating
397 # into gitweb; e.g. git-browser for graphical history representation
398 # or custom web-based repository administration interface.
400 # The 'default' value consists of a list of triplets in the form
401 # (label, link, position) where position is the label after which
402 # to insert the link and link is a format string where %n expands
403 # to the project name, %f to the project path within the filesystem,
404 # %h to the current hash (h gitweb parameter) and %b to the current
405 # hash base (hb gitweb parameter); %% expands to %.
407 # To enable system wide have in $GITWEB_CONFIG e.g.
408 # $feature{'actions'}{'default'} = [('graphiclog',
409 # '/git-browser/by-commit.html?r=%n', 'summary')];
410 # Project specific override is not supported.
411 'actions' => {
412 'override' => 0,
413 'default' => []},
415 # Allow gitweb scan project content tags described in ctags/
416 # of project repository, and display the popular Web 2.0-ish
417 # "tag cloud" near the project list. Note that this is something
418 # COMPLETELY different from the normal Git tags.
420 # gitweb by itself can show existing tags, but it does not handle
421 # tagging itself; you need an external application for that.
422 # For an example script, check Girocco's cgi/tagproj.cgi.
423 # You may want to install the HTML::TagCloud Perl module to get
424 # a pretty tag cloud instead of just a list of tags.
426 # To enable system wide have in $GITWEB_CONFIG
427 # $feature{'ctags'}{'default'} = ['path_to_tag_script'];
428 # Project specific override is not supported.
429 'ctags' => {
430 'override' => 0,
431 'default' => [0]},
433 # The maximum number of patches in a patchset generated in patch
434 # view. Set this to 0 or undef to disable patch view, or to a
435 # negative number to remove any limit.
437 # To disable system wide have in $GITWEB_CONFIG
438 # $feature{'patches'}{'default'} = [0];
439 # To have project specific config enable override in $GITWEB_CONFIG
440 # $feature{'patches'}{'override'} = 1;
441 # and in project config gitweb.patches = 0|n;
442 # where n is the maximum number of patches allowed in a patchset.
443 'patches' => {
444 'sub' => \&feature_patches,
445 'override' => 0,
446 'default' => [16]},
448 # Avatar support. When this feature is enabled, views such as
449 # shortlog or commit will display an avatar associated with
450 # the email of the committer(s) and/or author(s).
452 # Currently available providers are gravatar and picon.
453 # If an unknown provider is specified, the feature is disabled.
455 # Gravatar depends on Digest::MD5.
456 # Picon currently relies on the indiana.edu database.
458 # To enable system wide have in $GITWEB_CONFIG
459 # $feature{'avatar'}{'default'} = ['<provider>'];
460 # where <provider> is either gravatar or picon.
461 # To have project specific config enable override in $GITWEB_CONFIG
462 # $feature{'avatar'}{'override'} = 1;
463 # and in project config gitweb.avatar = <provider>;
464 'avatar' => {
465 'sub' => \&feature_avatar,
466 'override' => 0,
467 'default' => ['']},
469 # Enable displaying how much time and how many git commands
470 # it took to generate and display page. Disabled by default.
471 # Project specific override is not supported.
472 'timed' => {
473 'override' => 0,
474 'default' => [0]},
476 # Enable turning some links into links to actions which require
477 # JavaScript to run (like 'blame_incremental'). Not enabled by
478 # default. Project specific override is currently not supported.
479 'javascript-actions' => {
480 'override' => 0,
481 'default' => [0]},
483 # Syntax highlighting support. This is based on Daniel Svensson's
484 # and Sham Chukoury's work in gitweb-xmms2.git.
485 # It requires the 'highlight' program present in $PATH,
486 # and therefore is disabled by default.
488 # To enable system wide have in $GITWEB_CONFIG
489 # $feature{'highlight'}{'default'} = [1];
491 'highlight' => {
492 'sub' => sub { feature_bool('highlight', @_) },
493 'override' => 0,
494 'default' => [0]},
497 sub gitweb_get_feature {
498 my ($name) = @_;
499 return unless exists $feature{$name};
500 my ($sub, $override, @defaults) = (
501 $feature{$name}{'sub'},
502 $feature{$name}{'override'},
503 @{$feature{$name}{'default'}});
504 # project specific override is possible only if we have project
505 our $git_dir; # global variable, declared later
506 if (!$override || !defined $git_dir) {
507 return @defaults;
509 if (!defined $sub) {
510 warn "feature $name is not overridable";
511 return @defaults;
513 return $sub->(@defaults);
516 # A wrapper to check if a given feature is enabled.
517 # With this, you can say
519 # my $bool_feat = gitweb_check_feature('bool_feat');
520 # gitweb_check_feature('bool_feat') or somecode;
522 # instead of
524 # my ($bool_feat) = gitweb_get_feature('bool_feat');
525 # (gitweb_get_feature('bool_feat'))[0] or somecode;
527 sub gitweb_check_feature {
528 return (gitweb_get_feature(@_))[0];
532 sub feature_bool {
533 my $key = shift;
534 my ($val) = git_get_project_config($key, '--bool');
536 if (!defined $val) {
537 return ($_[0]);
538 } elsif ($val eq 'true') {
539 return (1);
540 } elsif ($val eq 'false') {
541 return (0);
545 sub feature_snapshot {
546 my (@fmts) = @_;
548 my ($val) = git_get_project_config('snapshot');
550 if ($val) {
551 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
554 return @fmts;
557 sub feature_patches {
558 my @val = (git_get_project_config('patches', '--int'));
560 if (@val) {
561 return @val;
564 return ($_[0]);
567 sub feature_avatar {
568 my @val = (git_get_project_config('avatar'));
570 return @val ? @val : @_;
573 # checking HEAD file with -e is fragile if the repository was
574 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
575 # and then pruned.
576 sub check_head_link {
577 my ($dir) = @_;
578 my $headfile = "$dir/HEAD";
579 return ((-e $headfile) ||
580 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
583 sub check_export_ok {
584 my ($dir) = @_;
585 return (check_head_link($dir) &&
586 (!$export_ok || -e "$dir/$export_ok") &&
587 (!$export_auth_hook || $export_auth_hook->($dir)));
590 # process alternate names for backward compatibility
591 # filter out unsupported (unknown) snapshot formats
592 sub filter_snapshot_fmts {
593 my @fmts = @_;
595 @fmts = map {
596 exists $known_snapshot_format_aliases{$_} ?
597 $known_snapshot_format_aliases{$_} : $_} @fmts;
598 @fmts = grep {
599 exists $known_snapshot_formats{$_} &&
600 !$known_snapshot_formats{$_}{'disabled'}} @fmts;
603 our ($GITWEB_CONFIG, $GITWEB_CONFIG_SYSTEM);
604 sub evaluate_gitweb_config {
605 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
606 our $GITWEB_CONFIG_SYSTEM = $ENV{'GITWEB_CONFIG_SYSTEM'} || "++GITWEB_CONFIG_SYSTEM++";
607 # die if there are errors parsing config file
608 if (-e $GITWEB_CONFIG) {
609 do $GITWEB_CONFIG;
610 die $@ if $@;
611 } elsif (-e $GITWEB_CONFIG_SYSTEM) {
612 do $GITWEB_CONFIG_SYSTEM;
613 die $@ if $@;
617 # Get loadavg of system, to compare against $maxload.
618 # Currently it requires '/proc/loadavg' present to get loadavg;
619 # if it is not present it returns 0, which means no load checking.
620 sub get_loadavg {
621 if( -e '/proc/loadavg' ){
622 open my $fd, '<', '/proc/loadavg'
623 or return 0;
624 my @load = split(/\s+/, scalar <$fd>);
625 close $fd;
627 # The first three columns measure CPU and IO utilization of the last one,
628 # five, and 10 minute periods. The fourth column shows the number of
629 # currently running processes and the total number of processes in the m/n
630 # format. The last column displays the last process ID used.
631 return $load[0] || 0;
633 # additional checks for load average should go here for things that don't export
634 # /proc/loadavg
636 return 0;
639 # version of the core git binary
640 our $git_version;
641 sub evaluate_git_version {
642 our $git_version = qx("$GIT" --version) =~ m/git version (.*)$/ ? $1 : "unknown";
643 $number_of_git_cmds++;
646 sub check_loadavg {
647 if (defined $maxload && get_loadavg() > $maxload) {
648 die_error(503, "The load average on the server is too high");
652 # ======================================================================
653 # input validation and dispatch
655 # input parameters can be collected from a variety of sources (presently, CGI
656 # and PATH_INFO), so we define an %input_params hash that collects them all
657 # together during validation: this allows subsequent uses (e.g. href()) to be
658 # agnostic of the parameter origin
660 our %input_params = ();
662 # input parameters are stored with the long parameter name as key. This will
663 # also be used in the href subroutine to convert parameters to their CGI
664 # equivalent, and since the href() usage is the most frequent one, we store
665 # the name -> CGI key mapping here, instead of the reverse.
667 # XXX: Warning: If you touch this, check the search form for updating,
668 # too.
670 our @cgi_param_mapping = (
671 project => "p",
672 action => "a",
673 file_name => "f",
674 file_parent => "fp",
675 hash => "h",
676 hash_parent => "hp",
677 hash_base => "hb",
678 hash_parent_base => "hpb",
679 page => "pg",
680 order => "o",
681 searchtext => "s",
682 searchtype => "st",
683 snapshot_format => "sf",
684 extra_options => "opt",
685 search_use_regexp => "sr",
686 # this must be last entry (for manipulation from JavaScript)
687 javascript => "js"
689 our %cgi_param_mapping = @cgi_param_mapping;
691 # we will also need to know the possible actions, for validation
692 our %actions = (
693 "blame" => \&git_blame,
694 "blame_incremental" => \&git_blame_incremental,
695 "blame_data" => \&git_blame_data,
696 "blobdiff" => \&git_blobdiff,
697 "blobdiff_plain" => \&git_blobdiff_plain,
698 "blob" => \&git_blob,
699 "blob_plain" => \&git_blob_plain,
700 "commitdiff" => \&git_commitdiff,
701 "commitdiff_plain" => \&git_commitdiff_plain,
702 "commit" => \&git_commit,
703 "forks" => \&git_forks,
704 "heads" => \&git_heads,
705 "history" => \&git_history,
706 "log" => \&git_log,
707 "patch" => \&git_patch,
708 "patches" => \&git_patches,
709 "rss" => \&git_rss,
710 "atom" => \&git_atom,
711 "search" => \&git_search,
712 "search_help" => \&git_search_help,
713 "shortlog" => \&git_shortlog,
714 "summary" => \&git_summary,
715 "tag" => \&git_tag,
716 "tags" => \&git_tags,
717 "tree" => \&git_tree,
718 "snapshot" => \&git_snapshot,
719 "object" => \&git_object,
720 # those below don't need $project
721 "opml" => \&git_opml,
722 "project_list" => \&git_project_list,
723 "project_index" => \&git_project_index,
726 # finally, we have the hash of allowed extra_options for the commands that
727 # allow them
728 our %allowed_options = (
729 "--no-merges" => [ qw(rss atom log shortlog history) ],
732 # fill %input_params with the CGI parameters. All values except for 'opt'
733 # should be single values, but opt can be an array. We should probably
734 # build an array of parameters that can be multi-valued, but since for the time
735 # being it's only this one, we just single it out
736 sub evaluate_query_params {
737 our $cgi;
739 while (my ($name, $symbol) = each %cgi_param_mapping) {
740 if ($symbol eq 'opt') {
741 $input_params{$name} = [ $cgi->param($symbol) ];
742 } else {
743 $input_params{$name} = $cgi->param($symbol);
748 # now read PATH_INFO and update the parameter list for missing parameters
749 sub evaluate_path_info {
750 return if defined $input_params{'project'};
751 return if !$path_info;
752 $path_info =~ s,^/+,,;
753 return if !$path_info;
755 # find which part of PATH_INFO is project
756 my $project = $path_info;
757 $project =~ s,/+$,,;
758 while ($project && !check_head_link("$projectroot/$project")) {
759 $project =~ s,/*[^/]*$,,;
761 return unless $project;
762 $input_params{'project'} = $project;
764 # do not change any parameters if an action is given using the query string
765 return if $input_params{'action'};
766 $path_info =~ s,^\Q$project\E/*,,;
768 # next, check if we have an action
769 my $action = $path_info;
770 $action =~ s,/.*$,,;
771 if (exists $actions{$action}) {
772 $path_info =~ s,^$action/*,,;
773 $input_params{'action'} = $action;
776 # list of actions that want hash_base instead of hash, but can have no
777 # pathname (f) parameter
778 my @wants_base = (
779 'tree',
780 'history',
783 # we want to catch
784 # [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name]
785 my ($parentrefname, $parentpathname, $refname, $pathname) =
786 ($path_info =~ /^(?:(.+?)(?::(.+))?\.\.)?(.+?)(?::(.+))?$/);
788 # first, analyze the 'current' part
789 if (defined $pathname) {
790 # we got "branch:filename" or "branch:dir/"
791 # we could use git_get_type(branch:pathname), but:
792 # - it needs $git_dir
793 # - it does a git() call
794 # - the convention of terminating directories with a slash
795 # makes it superfluous
796 # - embedding the action in the PATH_INFO would make it even
797 # more superfluous
798 $pathname =~ s,^/+,,;
799 if (!$pathname || substr($pathname, -1) eq "/") {
800 $input_params{'action'} ||= "tree";
801 $pathname =~ s,/$,,;
802 } else {
803 # the default action depends on whether we had parent info
804 # or not
805 if ($parentrefname) {
806 $input_params{'action'} ||= "blobdiff_plain";
807 } else {
808 $input_params{'action'} ||= "blob_plain";
811 $input_params{'hash_base'} ||= $refname;
812 $input_params{'file_name'} ||= $pathname;
813 } elsif (defined $refname) {
814 # we got "branch". In this case we have to choose if we have to
815 # set hash or hash_base.
817 # Most of the actions without a pathname only want hash to be
818 # set, except for the ones specified in @wants_base that want
819 # hash_base instead. It should also be noted that hand-crafted
820 # links having 'history' as an action and no pathname or hash
821 # set will fail, but that happens regardless of PATH_INFO.
822 $input_params{'action'} ||= "shortlog";
823 if (grep { $_ eq $input_params{'action'} } @wants_base) {
824 $input_params{'hash_base'} ||= $refname;
825 } else {
826 $input_params{'hash'} ||= $refname;
830 # next, handle the 'parent' part, if present
831 if (defined $parentrefname) {
832 # a missing pathspec defaults to the 'current' filename, allowing e.g.
833 # someproject/blobdiff/oldrev..newrev:/filename
834 if ($parentpathname) {
835 $parentpathname =~ s,^/+,,;
836 $parentpathname =~ s,/$,,;
837 $input_params{'file_parent'} ||= $parentpathname;
838 } else {
839 $input_params{'file_parent'} ||= $input_params{'file_name'};
841 # we assume that hash_parent_base is wanted if a path was specified,
842 # or if the action wants hash_base instead of hash
843 if (defined $input_params{'file_parent'} ||
844 grep { $_ eq $input_params{'action'} } @wants_base) {
845 $input_params{'hash_parent_base'} ||= $parentrefname;
846 } else {
847 $input_params{'hash_parent'} ||= $parentrefname;
851 # for the snapshot action, we allow URLs in the form
852 # $project/snapshot/$hash.ext
853 # where .ext determines the snapshot and gets removed from the
854 # passed $refname to provide the $hash.
856 # To be able to tell that $refname includes the format extension, we
857 # require the following two conditions to be satisfied:
858 # - the hash input parameter MUST have been set from the $refname part
859 # of the URL (i.e. they must be equal)
860 # - the snapshot format MUST NOT have been defined already (e.g. from
861 # CGI parameter sf)
862 # It's also useless to try any matching unless $refname has a dot,
863 # so we check for that too
864 if (defined $input_params{'action'} &&
865 $input_params{'action'} eq 'snapshot' &&
866 defined $refname && index($refname, '.') != -1 &&
867 $refname eq $input_params{'hash'} &&
868 !defined $input_params{'snapshot_format'}) {
869 # We loop over the known snapshot formats, checking for
870 # extensions. Allowed extensions are both the defined suffix
871 # (which includes the initial dot already) and the snapshot
872 # format key itself, with a prepended dot
873 while (my ($fmt, $opt) = each %known_snapshot_formats) {
874 my $hash = $refname;
875 unless ($hash =~ s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) {
876 next;
878 my $sfx = $1;
879 # a valid suffix was found, so set the snapshot format
880 # and reset the hash parameter
881 $input_params{'snapshot_format'} = $fmt;
882 $input_params{'hash'} = $hash;
883 # we also set the format suffix to the one requested
884 # in the URL: this way a request for e.g. .tgz returns
885 # a .tgz instead of a .tar.gz
886 $known_snapshot_formats{$fmt}{'suffix'} = $sfx;
887 last;
892 our ($action, $project, $file_name, $file_parent, $hash, $hash_parent, $hash_base,
893 $hash_parent_base, @extra_options, $page, $searchtype, $search_use_regexp,
894 $searchtext, $search_regexp);
895 sub evaluate_and_validate_params {
896 our $action = $input_params{'action'};
897 if (defined $action) {
898 if (!validate_action($action)) {
899 die_error(400, "Invalid action parameter");
903 # parameters which are pathnames
904 our $project = $input_params{'project'};
905 if (defined $project) {
906 if (!validate_project($project)) {
907 undef $project;
908 die_error(404, "No such project");
912 our $file_name = $input_params{'file_name'};
913 if (defined $file_name) {
914 if (!validate_pathname($file_name)) {
915 die_error(400, "Invalid file parameter");
919 our $file_parent = $input_params{'file_parent'};
920 if (defined $file_parent) {
921 if (!validate_pathname($file_parent)) {
922 die_error(400, "Invalid file parent parameter");
926 # parameters which are refnames
927 our $hash = $input_params{'hash'};
928 if (defined $hash) {
929 if (!validate_refname($hash)) {
930 die_error(400, "Invalid hash parameter");
934 our $hash_parent = $input_params{'hash_parent'};
935 if (defined $hash_parent) {
936 if (!validate_refname($hash_parent)) {
937 die_error(400, "Invalid hash parent parameter");
941 our $hash_base = $input_params{'hash_base'};
942 if (defined $hash_base) {
943 if (!validate_refname($hash_base)) {
944 die_error(400, "Invalid hash base parameter");
948 our @extra_options = @{$input_params{'extra_options'}};
949 # @extra_options is always defined, since it can only be (currently) set from
950 # CGI, and $cgi->param() returns the empty array in array context if the param
951 # is not set
952 foreach my $opt (@extra_options) {
953 if (not exists $allowed_options{$opt}) {
954 die_error(400, "Invalid option parameter");
956 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
957 die_error(400, "Invalid option parameter for this action");
961 our $hash_parent_base = $input_params{'hash_parent_base'};
962 if (defined $hash_parent_base) {
963 if (!validate_refname($hash_parent_base)) {
964 die_error(400, "Invalid hash parent base parameter");
968 # other parameters
969 our $page = $input_params{'page'};
970 if (defined $page) {
971 if ($page =~ m/[^0-9]/) {
972 die_error(400, "Invalid page parameter");
976 our $searchtype = $input_params{'searchtype'};
977 if (defined $searchtype) {
978 if ($searchtype =~ m/[^a-z]/) {
979 die_error(400, "Invalid searchtype parameter");
983 our $search_use_regexp = $input_params{'search_use_regexp'};
985 our $searchtext = $input_params{'searchtext'};
986 our $search_regexp;
987 if (defined $searchtext) {
988 if (length($searchtext) < 2) {
989 die_error(403, "At least two characters are required for search parameter");
991 $search_regexp = $search_use_regexp ? $searchtext : quotemeta $searchtext;
995 # path to the current git repository
996 our $git_dir;
997 sub evaluate_git_dir {
998 our $git_dir = "$projectroot/$project" if $project;
1001 our (@snapshot_fmts, $git_avatar);
1002 sub configure_gitweb_features {
1003 # list of supported snapshot formats
1004 our @snapshot_fmts = gitweb_get_feature('snapshot');
1005 @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
1007 # check that the avatar feature is set to a known provider name,
1008 # and for each provider check if the dependencies are satisfied.
1009 # if the provider name is invalid or the dependencies are not met,
1010 # reset $git_avatar to the empty string.
1011 our ($git_avatar) = gitweb_get_feature('avatar');
1012 if ($git_avatar eq 'gravatar') {
1013 $git_avatar = '' unless (eval { require Digest::MD5; 1; });
1014 } elsif ($git_avatar eq 'picon') {
1015 # no dependencies
1016 } else {
1017 $git_avatar = '';
1021 # custom error handler: 'die <message>' is Internal Server Error
1022 sub handle_errors_html {
1023 my $msg = shift; # it is already HTML escaped
1025 # to avoid infinite loop where error occurs in die_error,
1026 # change handler to default handler, disabling handle_errors_html
1027 set_message("Error occured when inside die_error:\n$msg");
1029 # you cannot jump out of die_error when called as error handler;
1030 # the subroutine set via CGI::Carp::set_message is called _after_
1031 # HTTP headers are already written, so it cannot write them itself
1032 die_error(undef, undef, $msg, -error_handler => 1, -no_http_header => 1);
1034 set_message(\&handle_errors_html);
1036 # dispatch
1037 sub dispatch {
1038 if (!defined $action) {
1039 if (defined $hash) {
1040 $action = git_get_type($hash);
1041 } elsif (defined $hash_base && defined $file_name) {
1042 $action = git_get_type("$hash_base:$file_name");
1043 } elsif (defined $project) {
1044 $action = 'summary';
1045 } else {
1046 $action = 'project_list';
1049 if (!defined($actions{$action})) {
1050 die_error(400, "Unknown action");
1052 if ($action !~ m/^(?:opml|project_list|project_index)$/ &&
1053 !$project) {
1054 die_error(400, "Project needed");
1056 $actions{$action}->();
1059 sub reset_timer {
1060 our $t0 = [Time::HiRes::gettimeofday()]
1061 if defined $t0;
1062 our $number_of_git_cmds = 0;
1065 sub run_request {
1066 reset_timer();
1068 evaluate_uri();
1069 evaluate_gitweb_config();
1070 check_loadavg();
1072 # $projectroot and $projects_list might be set in gitweb config file
1073 $projects_list ||= $projectroot;
1075 evaluate_query_params();
1076 evaluate_path_info();
1077 evaluate_and_validate_params();
1078 evaluate_git_dir();
1080 configure_gitweb_features();
1082 dispatch();
1085 our $is_last_request = sub { 1 };
1086 our ($pre_dispatch_hook, $post_dispatch_hook, $pre_listen_hook);
1087 our $CGI = 'CGI';
1088 our $cgi;
1089 sub configure_as_fcgi {
1090 require CGI::Fast;
1091 our $CGI = 'CGI::Fast';
1093 my $request_number = 0;
1094 # let each child service 100 requests
1095 our $is_last_request = sub { ++$request_number > 100 };
1097 sub evaluate_argv {
1098 my $script_name = $ENV{'SCRIPT_NAME'} || $ENV{'SCRIPT_FILENAME'} || __FILE__;
1099 configure_as_fcgi()
1100 if $script_name =~ /\.fcgi$/;
1102 return unless (@ARGV);
1104 require Getopt::Long;
1105 Getopt::Long::GetOptions(
1106 'fastcgi|fcgi|f' => \&configure_as_fcgi,
1107 'nproc|n=i' => sub {
1108 my ($arg, $val) = @_;
1109 return unless eval { require FCGI::ProcManager; 1; };
1110 my $proc_manager = FCGI::ProcManager->new({
1111 n_processes => $val,
1113 our $pre_listen_hook = sub { $proc_manager->pm_manage() };
1114 our $pre_dispatch_hook = sub { $proc_manager->pm_pre_dispatch() };
1115 our $post_dispatch_hook = sub { $proc_manager->pm_post_dispatch() };
1120 sub run {
1121 evaluate_argv();
1122 evaluate_git_version();
1124 $pre_listen_hook->()
1125 if $pre_listen_hook;
1127 REQUEST:
1128 while ($cgi = $CGI->new()) {
1129 $pre_dispatch_hook->()
1130 if $pre_dispatch_hook;
1132 run_request();
1134 $post_dispatch_hook->()
1135 if $post_dispatch_hook;
1137 last REQUEST if ($is_last_request->());
1140 DONE_GITWEB:
1144 run();
1146 if (defined caller) {
1147 # wrapped in a subroutine processing requests,
1148 # e.g. mod_perl with ModPerl::Registry, or PSGI with Plack::App::WrapCGI
1149 return;
1150 } else {
1151 # pure CGI script, serving single request
1152 exit;
1155 ## ======================================================================
1156 ## action links
1158 # possible values of extra options
1159 # -full => 0|1 - use absolute/full URL ($my_uri/$my_url as base)
1160 # -replay => 1 - start from a current view (replay with modifications)
1161 # -path_info => 0|1 - don't use/use path_info URL (if possible)
1162 sub href {
1163 my %params = @_;
1164 # default is to use -absolute url() i.e. $my_uri
1165 my $href = $params{-full} ? $my_url : $my_uri;
1167 $params{'project'} = $project unless exists $params{'project'};
1169 if ($params{-replay}) {
1170 while (my ($name, $symbol) = each %cgi_param_mapping) {
1171 if (!exists $params{$name}) {
1172 $params{$name} = $input_params{$name};
1177 my $use_pathinfo = gitweb_check_feature('pathinfo');
1178 if (defined $params{'project'} &&
1179 (exists $params{-path_info} ? $params{-path_info} : $use_pathinfo)) {
1180 # try to put as many parameters as possible in PATH_INFO:
1181 # - project name
1182 # - action
1183 # - hash_parent or hash_parent_base:/file_parent
1184 # - hash or hash_base:/filename
1185 # - the snapshot_format as an appropriate suffix
1187 # When the script is the root DirectoryIndex for the domain,
1188 # $href here would be something like http://gitweb.example.com/
1189 # Thus, we strip any trailing / from $href, to spare us double
1190 # slashes in the final URL
1191 $href =~ s,/$,,;
1193 # Then add the project name, if present
1194 $href .= "/".esc_url($params{'project'});
1195 delete $params{'project'};
1197 # since we destructively absorb parameters, we keep this
1198 # boolean that remembers if we're handling a snapshot
1199 my $is_snapshot = $params{'action'} eq 'snapshot';
1201 # Summary just uses the project path URL, any other action is
1202 # added to the URL
1203 if (defined $params{'action'}) {
1204 $href .= "/".esc_url($params{'action'}) unless $params{'action'} eq 'summary';
1205 delete $params{'action'};
1208 # Next, we put hash_parent_base:/file_parent..hash_base:/file_name,
1209 # stripping nonexistent or useless pieces
1210 $href .= "/" if ($params{'hash_base'} || $params{'hash_parent_base'}
1211 || $params{'hash_parent'} || $params{'hash'});
1212 if (defined $params{'hash_base'}) {
1213 if (defined $params{'hash_parent_base'}) {
1214 $href .= esc_url($params{'hash_parent_base'});
1215 # skip the file_parent if it's the same as the file_name
1216 if (defined $params{'file_parent'}) {
1217 if (defined $params{'file_name'} && $params{'file_parent'} eq $params{'file_name'}) {
1218 delete $params{'file_parent'};
1219 } elsif ($params{'file_parent'} !~ /\.\./) {
1220 $href .= ":/".esc_url($params{'file_parent'});
1221 delete $params{'file_parent'};
1224 $href .= "..";
1225 delete $params{'hash_parent'};
1226 delete $params{'hash_parent_base'};
1227 } elsif (defined $params{'hash_parent'}) {
1228 $href .= esc_url($params{'hash_parent'}). "..";
1229 delete $params{'hash_parent'};
1232 $href .= esc_url($params{'hash_base'});
1233 if (defined $params{'file_name'} && $params{'file_name'} !~ /\.\./) {
1234 $href .= ":/".esc_url($params{'file_name'});
1235 delete $params{'file_name'};
1237 delete $params{'hash'};
1238 delete $params{'hash_base'};
1239 } elsif (defined $params{'hash'}) {
1240 $href .= esc_url($params{'hash'});
1241 delete $params{'hash'};
1244 # If the action was a snapshot, we can absorb the
1245 # snapshot_format parameter too
1246 if ($is_snapshot) {
1247 my $fmt = $params{'snapshot_format'};
1248 # snapshot_format should always be defined when href()
1249 # is called, but just in case some code forgets, we
1250 # fall back to the default
1251 $fmt ||= $snapshot_fmts[0];
1252 $href .= $known_snapshot_formats{$fmt}{'suffix'};
1253 delete $params{'snapshot_format'};
1257 # now encode the parameters explicitly
1258 my @result = ();
1259 for (my $i = 0; $i < @cgi_param_mapping; $i += 2) {
1260 my ($name, $symbol) = ($cgi_param_mapping[$i], $cgi_param_mapping[$i+1]);
1261 if (defined $params{$name}) {
1262 if (ref($params{$name}) eq "ARRAY") {
1263 foreach my $par (@{$params{$name}}) {
1264 push @result, $symbol . "=" . esc_param($par);
1266 } else {
1267 push @result, $symbol . "=" . esc_param($params{$name});
1271 $href .= "?" . join(';', @result) if scalar @result;
1273 return $href;
1277 ## ======================================================================
1278 ## validation, quoting/unquoting and escaping
1280 sub validate_action {
1281 my $input = shift || return undef;
1282 return undef unless exists $actions{$input};
1283 return $input;
1286 sub validate_project {
1287 my $input = shift || return undef;
1288 if (!validate_pathname($input) ||
1289 !(-d "$projectroot/$input") ||
1290 !check_export_ok("$projectroot/$input") ||
1291 ($strict_export && !project_in_list($input))) {
1292 return undef;
1293 } else {
1294 return $input;
1298 sub validate_pathname {
1299 my $input = shift || return undef;
1301 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
1302 # at the beginning, at the end, and between slashes.
1303 # also this catches doubled slashes
1304 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
1305 return undef;
1307 # no null characters
1308 if ($input =~ m!\0!) {
1309 return undef;
1311 return $input;
1314 sub validate_refname {
1315 my $input = shift || return undef;
1317 # textual hashes are O.K.
1318 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
1319 return $input;
1321 # it must be correct pathname
1322 $input = validate_pathname($input)
1323 or return undef;
1324 # restrictions on ref name according to git-check-ref-format
1325 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
1326 return undef;
1328 return $input;
1331 # decode sequences of octets in utf8 into Perl's internal form,
1332 # which is utf-8 with utf8 flag set if needed. gitweb writes out
1333 # in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
1334 sub to_utf8 {
1335 my $str = shift;
1336 return undef unless defined $str;
1337 if (utf8::valid($str)) {
1338 utf8::decode($str);
1339 return $str;
1340 } else {
1341 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
1345 # quote unsafe chars, but keep the slash, even when it's not
1346 # correct, but quoted slashes look too horrible in bookmarks
1347 sub esc_param {
1348 my $str = shift;
1349 return undef unless defined $str;
1350 $str =~ s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;
1351 $str =~ s/ /\+/g;
1352 return $str;
1355 # quote unsafe chars in whole URL, so some characters cannot be quoted
1356 sub esc_url {
1357 my $str = shift;
1358 return undef unless defined $str;
1359 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&= ]+)/CGI::escape($1)/eg;
1360 $str =~ s/ /\+/g;
1361 return $str;
1364 # replace invalid utf8 character with SUBSTITUTION sequence
1365 sub esc_html {
1366 my $str = shift;
1367 my %opts = @_;
1369 return undef unless defined $str;
1371 $str = to_utf8($str);
1372 $str = $cgi->escapeHTML($str);
1373 if ($opts{'-nbsp'}) {
1374 $str =~ s/ /&nbsp;/g;
1376 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
1377 return $str;
1380 # quote control characters and escape filename to HTML
1381 sub esc_path {
1382 my $str = shift;
1383 my %opts = @_;
1385 return undef unless defined $str;
1387 $str = to_utf8($str);
1388 $str = $cgi->escapeHTML($str);
1389 if ($opts{'-nbsp'}) {
1390 $str =~ s/ /&nbsp;/g;
1392 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
1393 return $str;
1396 # Make control characters "printable", using character escape codes (CEC)
1397 sub quot_cec {
1398 my $cntrl = shift;
1399 my %opts = @_;
1400 my %es = ( # character escape codes, aka escape sequences
1401 "\t" => '\t', # tab (HT)
1402 "\n" => '\n', # line feed (LF)
1403 "\r" => '\r', # carrige return (CR)
1404 "\f" => '\f', # form feed (FF)
1405 "\b" => '\b', # backspace (BS)
1406 "\a" => '\a', # alarm (bell) (BEL)
1407 "\e" => '\e', # escape (ESC)
1408 "\013" => '\v', # vertical tab (VT)
1409 "\000" => '\0', # nul character (NUL)
1411 my $chr = ( (exists $es{$cntrl})
1412 ? $es{$cntrl}
1413 : sprintf('\%2x', ord($cntrl)) );
1414 if ($opts{-nohtml}) {
1415 return $chr;
1416 } else {
1417 return "<span class=\"cntrl\">$chr</span>";
1421 # Alternatively use unicode control pictures codepoints,
1422 # Unicode "printable representation" (PR)
1423 sub quot_upr {
1424 my $cntrl = shift;
1425 my %opts = @_;
1427 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
1428 if ($opts{-nohtml}) {
1429 return $chr;
1430 } else {
1431 return "<span class=\"cntrl\">$chr</span>";
1435 # git may return quoted and escaped filenames
1436 sub unquote {
1437 my $str = shift;
1439 sub unq {
1440 my $seq = shift;
1441 my %es = ( # character escape codes, aka escape sequences
1442 't' => "\t", # tab (HT, TAB)
1443 'n' => "\n", # newline (NL)
1444 'r' => "\r", # return (CR)
1445 'f' => "\f", # form feed (FF)
1446 'b' => "\b", # backspace (BS)
1447 'a' => "\a", # alarm (bell) (BEL)
1448 'e' => "\e", # escape (ESC)
1449 'v' => "\013", # vertical tab (VT)
1452 if ($seq =~ m/^[0-7]{1,3}$/) {
1453 # octal char sequence
1454 return chr(oct($seq));
1455 } elsif (exists $es{$seq}) {
1456 # C escape sequence, aka character escape code
1457 return $es{$seq};
1459 # quoted ordinary character
1460 return $seq;
1463 if ($str =~ m/^"(.*)"$/) {
1464 # needs unquoting
1465 $str = $1;
1466 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
1468 return $str;
1471 # escape tabs (convert tabs to spaces)
1472 sub untabify {
1473 my $line = shift;
1475 while ((my $pos = index($line, "\t")) != -1) {
1476 if (my $count = (8 - ($pos % 8))) {
1477 my $spaces = ' ' x $count;
1478 $line =~ s/\t/$spaces/;
1482 return $line;
1485 sub project_in_list {
1486 my $project = shift;
1487 my @list = git_get_projects_list();
1488 return @list && scalar(grep { $_->{'path'} eq $project } @list);
1491 ## ----------------------------------------------------------------------
1492 ## HTML aware string manipulation
1494 # Try to chop given string on a word boundary between position
1495 # $len and $len+$add_len. If there is no word boundary there,
1496 # chop at $len+$add_len. Do not chop if chopped part plus ellipsis
1497 # (marking chopped part) would be longer than given string.
1498 sub chop_str {
1499 my $str = shift;
1500 my $len = shift;
1501 my $add_len = shift || 10;
1502 my $where = shift || 'right'; # 'left' | 'center' | 'right'
1504 # Make sure perl knows it is utf8 encoded so we don't
1505 # cut in the middle of a utf8 multibyte char.
1506 $str = to_utf8($str);
1508 # allow only $len chars, but don't cut a word if it would fit in $add_len
1509 # if it doesn't fit, cut it if it's still longer than the dots we would add
1510 # remove chopped character entities entirely
1512 # when chopping in the middle, distribute $len into left and right part
1513 # return early if chopping wouldn't make string shorter
1514 if ($where eq 'center') {
1515 return $str if ($len + 5 >= length($str)); # filler is length 5
1516 $len = int($len/2);
1517 } else {
1518 return $str if ($len + 4 >= length($str)); # filler is length 4
1521 # regexps: ending and beginning with word part up to $add_len
1522 my $endre = qr/.{$len}\w{0,$add_len}/;
1523 my $begre = qr/\w{0,$add_len}.{$len}/;
1525 if ($where eq 'left') {
1526 $str =~ m/^(.*?)($begre)$/;
1527 my ($lead, $body) = ($1, $2);
1528 if (length($lead) > 4) {
1529 $lead = " ...";
1531 return "$lead$body";
1533 } elsif ($where eq 'center') {
1534 $str =~ m/^($endre)(.*)$/;
1535 my ($left, $str) = ($1, $2);
1536 $str =~ m/^(.*?)($begre)$/;
1537 my ($mid, $right) = ($1, $2);
1538 if (length($mid) > 5) {
1539 $mid = " ... ";
1541 return "$left$mid$right";
1543 } else {
1544 $str =~ m/^($endre)(.*)$/;
1545 my $body = $1;
1546 my $tail = $2;
1547 if (length($tail) > 4) {
1548 $tail = "... ";
1550 return "$body$tail";
1554 # takes the same arguments as chop_str, but also wraps a <span> around the
1555 # result with a title attribute if it does get chopped. Additionally, the
1556 # string is HTML-escaped.
1557 sub chop_and_escape_str {
1558 my ($str) = @_;
1560 my $chopped = chop_str(@_);
1561 if ($chopped eq $str) {
1562 return esc_html($chopped);
1563 } else {
1564 $str =~ s/[[:cntrl:]]/?/g;
1565 return $cgi->span({-title=>$str}, esc_html($chopped));
1569 ## ----------------------------------------------------------------------
1570 ## functions returning short strings
1572 # CSS class for given age value (in seconds)
1573 sub age_class {
1574 my $age = shift;
1576 if (!defined $age) {
1577 return "noage";
1578 } elsif ($age < 60*60*2) {
1579 return "age0";
1580 } elsif ($age < 60*60*24*2) {
1581 return "age1";
1582 } else {
1583 return "age2";
1587 # convert age in seconds to "nn units ago" string
1588 sub age_string {
1589 my $age = shift;
1590 my $age_str;
1592 if ($age > 60*60*24*365*2) {
1593 $age_str = (int $age/60/60/24/365);
1594 $age_str .= " years ago";
1595 } elsif ($age > 60*60*24*(365/12)*2) {
1596 $age_str = int $age/60/60/24/(365/12);
1597 $age_str .= " months ago";
1598 } elsif ($age > 60*60*24*7*2) {
1599 $age_str = int $age/60/60/24/7;
1600 $age_str .= " weeks ago";
1601 } elsif ($age > 60*60*24*2) {
1602 $age_str = int $age/60/60/24;
1603 $age_str .= " days ago";
1604 } elsif ($age > 60*60*2) {
1605 $age_str = int $age/60/60;
1606 $age_str .= " hours ago";
1607 } elsif ($age > 60*2) {
1608 $age_str = int $age/60;
1609 $age_str .= " min ago";
1610 } elsif ($age > 2) {
1611 $age_str = int $age;
1612 $age_str .= " sec ago";
1613 } else {
1614 $age_str .= " right now";
1616 return $age_str;
1619 use constant {
1620 S_IFINVALID => 0030000,
1621 S_IFGITLINK => 0160000,
1624 # submodule/subproject, a commit object reference
1625 sub S_ISGITLINK {
1626 my $mode = shift;
1628 return (($mode & S_IFMT) == S_IFGITLINK)
1631 # convert file mode in octal to symbolic file mode string
1632 sub mode_str {
1633 my $mode = oct shift;
1635 if (S_ISGITLINK($mode)) {
1636 return 'm---------';
1637 } elsif (S_ISDIR($mode & S_IFMT)) {
1638 return 'drwxr-xr-x';
1639 } elsif (S_ISLNK($mode)) {
1640 return 'lrwxrwxrwx';
1641 } elsif (S_ISREG($mode)) {
1642 # git cares only about the executable bit
1643 if ($mode & S_IXUSR) {
1644 return '-rwxr-xr-x';
1645 } else {
1646 return '-rw-r--r--';
1648 } else {
1649 return '----------';
1653 # convert file mode in octal to file type string
1654 sub file_type {
1655 my $mode = shift;
1657 if ($mode !~ m/^[0-7]+$/) {
1658 return $mode;
1659 } else {
1660 $mode = oct $mode;
1663 if (S_ISGITLINK($mode)) {
1664 return "submodule";
1665 } elsif (S_ISDIR($mode & S_IFMT)) {
1666 return "directory";
1667 } elsif (S_ISLNK($mode)) {
1668 return "symlink";
1669 } elsif (S_ISREG($mode)) {
1670 return "file";
1671 } else {
1672 return "unknown";
1676 # convert file mode in octal to file type description string
1677 sub file_type_long {
1678 my $mode = shift;
1680 if ($mode !~ m/^[0-7]+$/) {
1681 return $mode;
1682 } else {
1683 $mode = oct $mode;
1686 if (S_ISGITLINK($mode)) {
1687 return "submodule";
1688 } elsif (S_ISDIR($mode & S_IFMT)) {
1689 return "directory";
1690 } elsif (S_ISLNK($mode)) {
1691 return "symlink";
1692 } elsif (S_ISREG($mode)) {
1693 if ($mode & S_IXUSR) {
1694 return "executable";
1695 } else {
1696 return "file";
1698 } else {
1699 return "unknown";
1704 ## ----------------------------------------------------------------------
1705 ## functions returning short HTML fragments, or transforming HTML fragments
1706 ## which don't belong to other sections
1708 # format line of commit message.
1709 sub format_log_line_html {
1710 my $line = shift;
1712 $line = esc_html($line, -nbsp=>1);
1713 $line =~ s{\b([0-9a-fA-F]{8,40})\b}{
1714 $cgi->a({-href => href(action=>"object", hash=>$1),
1715 -class => "text"}, $1);
1716 }eg;
1718 return $line;
1721 # format marker of refs pointing to given object
1723 # the destination action is chosen based on object type and current context:
1724 # - for annotated tags, we choose the tag view unless it's the current view
1725 # already, in which case we go to shortlog view
1726 # - for other refs, we keep the current view if we're in history, shortlog or
1727 # log view, and select shortlog otherwise
1728 sub format_ref_marker {
1729 my ($refs, $id) = @_;
1730 my $markers = '';
1732 if (defined $refs->{$id}) {
1733 foreach my $ref (@{$refs->{$id}}) {
1734 # this code exploits the fact that non-lightweight tags are the
1735 # only indirect objects, and that they are the only objects for which
1736 # we want to use tag instead of shortlog as action
1737 my ($type, $name) = qw();
1738 my $indirect = ($ref =~ s/\^\{\}$//);
1739 # e.g. tags/v2.6.11 or heads/next
1740 if ($ref =~ m!^(.*?)s?/(.*)$!) {
1741 $type = $1;
1742 $name = $2;
1743 } else {
1744 $type = "ref";
1745 $name = $ref;
1748 my $class = $type;
1749 $class .= " indirect" if $indirect;
1751 my $dest_action = "shortlog";
1753 if ($indirect) {
1754 $dest_action = "tag" unless $action eq "tag";
1755 } elsif ($action =~ /^(history|(short)?log)$/) {
1756 $dest_action = $action;
1759 my $dest = "";
1760 $dest .= "refs/" unless $ref =~ m!^refs/!;
1761 $dest .= $ref;
1763 my $link = $cgi->a({
1764 -href => href(
1765 action=>$dest_action,
1766 hash=>$dest
1767 )}, $name);
1769 $markers .= " <span class=\"$class\" title=\"$ref\">" .
1770 $link . "</span>";
1774 if ($markers) {
1775 return ' <span class="refs">'. $markers . '</span>';
1776 } else {
1777 return "";
1781 # format, perhaps shortened and with markers, title line
1782 sub format_subject_html {
1783 my ($long, $short, $href, $extra) = @_;
1784 $extra = '' unless defined($extra);
1786 if (length($short) < length($long)) {
1787 $long =~ s/[[:cntrl:]]/?/g;
1788 return $cgi->a({-href => $href, -class => "list subject",
1789 -title => to_utf8($long)},
1790 esc_html($short)) . $extra;
1791 } else {
1792 return $cgi->a({-href => $href, -class => "list subject"},
1793 esc_html($long)) . $extra;
1797 # Rather than recomputing the url for an email multiple times, we cache it
1798 # after the first hit. This gives a visible benefit in views where the avatar
1799 # for the same email is used repeatedly (e.g. shortlog).
1800 # The cache is shared by all avatar engines (currently gravatar only), which
1801 # are free to use it as preferred. Since only one avatar engine is used for any
1802 # given page, there's no risk for cache conflicts.
1803 our %avatar_cache = ();
1805 # Compute the picon url for a given email, by using the picon search service over at
1806 # http://www.cs.indiana.edu/picons/search.html
1807 sub picon_url {
1808 my $email = lc shift;
1809 if (!$avatar_cache{$email}) {
1810 my ($user, $domain) = split('@', $email);
1811 $avatar_cache{$email} =
1812 "http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/" .
1813 "$domain/$user/" .
1814 "users+domains+unknown/up/single";
1816 return $avatar_cache{$email};
1819 # Compute the gravatar url for a given email, if it's not in the cache already.
1820 # Gravatar stores only the part of the URL before the size, since that's the
1821 # one computationally more expensive. This also allows reuse of the cache for
1822 # different sizes (for this particular engine).
1823 sub gravatar_url {
1824 my $email = lc shift;
1825 my $size = shift;
1826 $avatar_cache{$email} ||=
1827 "http://www.gravatar.com/avatar/" .
1828 Digest::MD5::md5_hex($email) . "?s=";
1829 return $avatar_cache{$email} . $size;
1832 # Insert an avatar for the given $email at the given $size if the feature
1833 # is enabled.
1834 sub git_get_avatar {
1835 my ($email, %opts) = @_;
1836 my $pre_white = ($opts{-pad_before} ? "&nbsp;" : "");
1837 my $post_white = ($opts{-pad_after} ? "&nbsp;" : "");
1838 $opts{-size} ||= 'default';
1839 my $size = $avatar_size{$opts{-size}} || $avatar_size{'default'};
1840 my $url = "";
1841 if ($git_avatar eq 'gravatar') {
1842 $url = gravatar_url($email, $size);
1843 } elsif ($git_avatar eq 'picon') {
1844 $url = picon_url($email);
1846 # Other providers can be added by extending the if chain, defining $url
1847 # as needed. If no variant puts something in $url, we assume avatars
1848 # are completely disabled/unavailable.
1849 if ($url) {
1850 return $pre_white .
1851 "<img width=\"$size\" " .
1852 "class=\"avatar\" " .
1853 "src=\"$url\" " .
1854 "alt=\"\" " .
1855 "/>" . $post_white;
1856 } else {
1857 return "";
1861 sub format_search_author {
1862 my ($author, $searchtype, $displaytext) = @_;
1863 my $have_search = gitweb_check_feature('search');
1865 if ($have_search) {
1866 my $performed = "";
1867 if ($searchtype eq 'author') {
1868 $performed = "authored";
1869 } elsif ($searchtype eq 'committer') {
1870 $performed = "committed";
1873 return $cgi->a({-href => href(action=>"search", hash=>$hash,
1874 searchtext=>$author,
1875 searchtype=>$searchtype), class=>"list",
1876 title=>"Search for commits $performed by $author"},
1877 $displaytext);
1879 } else {
1880 return $displaytext;
1884 # format the author name of the given commit with the given tag
1885 # the author name is chopped and escaped according to the other
1886 # optional parameters (see chop_str).
1887 sub format_author_html {
1888 my $tag = shift;
1889 my $co = shift;
1890 my $author = chop_and_escape_str($co->{'author_name'}, @_);
1891 return "<$tag class=\"author\">" .
1892 format_search_author($co->{'author_name'}, "author",
1893 git_get_avatar($co->{'author_email'}, -pad_after => 1) .
1894 $author) .
1895 "</$tag>";
1898 # format git diff header line, i.e. "diff --(git|combined|cc) ..."
1899 sub format_git_diff_header_line {
1900 my $line = shift;
1901 my $diffinfo = shift;
1902 my ($from, $to) = @_;
1904 if ($diffinfo->{'nparents'}) {
1905 # combined diff
1906 $line =~ s!^(diff (.*?) )"?.*$!$1!;
1907 if ($to->{'href'}) {
1908 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1909 esc_path($to->{'file'}));
1910 } else { # file was deleted (no href)
1911 $line .= esc_path($to->{'file'});
1913 } else {
1914 # "ordinary" diff
1915 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
1916 if ($from->{'href'}) {
1917 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
1918 'a/' . esc_path($from->{'file'}));
1919 } else { # file was added (no href)
1920 $line .= 'a/' . esc_path($from->{'file'});
1922 $line .= ' ';
1923 if ($to->{'href'}) {
1924 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1925 'b/' . esc_path($to->{'file'}));
1926 } else { # file was deleted
1927 $line .= 'b/' . esc_path($to->{'file'});
1931 return "<div class=\"diff header\">$line</div>\n";
1934 # format extended diff header line, before patch itself
1935 sub format_extended_diff_header_line {
1936 my $line = shift;
1937 my $diffinfo = shift;
1938 my ($from, $to) = @_;
1940 # match <path>
1941 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
1942 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1943 esc_path($from->{'file'}));
1945 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
1946 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1947 esc_path($to->{'file'}));
1949 # match single <mode>
1950 if ($line =~ m/\s(\d{6})$/) {
1951 $line .= '<span class="info"> (' .
1952 file_type_long($1) .
1953 ')</span>';
1955 # match <hash>
1956 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
1957 # can match only for combined diff
1958 $line = 'index ';
1959 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1960 if ($from->{'href'}[$i]) {
1961 $line .= $cgi->a({-href=>$from->{'href'}[$i],
1962 -class=>"hash"},
1963 substr($diffinfo->{'from_id'}[$i],0,7));
1964 } else {
1965 $line .= '0' x 7;
1967 # separator
1968 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
1970 $line .= '..';
1971 if ($to->{'href'}) {
1972 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1973 substr($diffinfo->{'to_id'},0,7));
1974 } else {
1975 $line .= '0' x 7;
1978 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
1979 # can match only for ordinary diff
1980 my ($from_link, $to_link);
1981 if ($from->{'href'}) {
1982 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
1983 substr($diffinfo->{'from_id'},0,7));
1984 } else {
1985 $from_link = '0' x 7;
1987 if ($to->{'href'}) {
1988 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1989 substr($diffinfo->{'to_id'},0,7));
1990 } else {
1991 $to_link = '0' x 7;
1993 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
1994 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
1997 return $line . "<br/>\n";
2000 # format from-file/to-file diff header
2001 sub format_diff_from_to_header {
2002 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
2003 my $line;
2004 my $result = '';
2006 $line = $from_line;
2007 #assert($line =~ m/^---/) if DEBUG;
2008 # no extra formatting for "^--- /dev/null"
2009 if (! $diffinfo->{'nparents'}) {
2010 # ordinary (single parent) diff
2011 if ($line =~ m!^--- "?a/!) {
2012 if ($from->{'href'}) {
2013 $line = '--- a/' .
2014 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
2015 esc_path($from->{'file'}));
2016 } else {
2017 $line = '--- a/' .
2018 esc_path($from->{'file'});
2021 $result .= qq!<div class="diff from_file">$line</div>\n!;
2023 } else {
2024 # combined diff (merge commit)
2025 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2026 if ($from->{'href'}[$i]) {
2027 $line = '--- ' .
2028 $cgi->a({-href=>href(action=>"blobdiff",
2029 hash_parent=>$diffinfo->{'from_id'}[$i],
2030 hash_parent_base=>$parents[$i],
2031 file_parent=>$from->{'file'}[$i],
2032 hash=>$diffinfo->{'to_id'},
2033 hash_base=>$hash,
2034 file_name=>$to->{'file'}),
2035 -class=>"path",
2036 -title=>"diff" . ($i+1)},
2037 $i+1) .
2038 '/' .
2039 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
2040 esc_path($from->{'file'}[$i]));
2041 } else {
2042 $line = '--- /dev/null';
2044 $result .= qq!<div class="diff from_file">$line</div>\n!;
2048 $line = $to_line;
2049 #assert($line =~ m/^\+\+\+/) if DEBUG;
2050 # no extra formatting for "^+++ /dev/null"
2051 if ($line =~ m!^\+\+\+ "?b/!) {
2052 if ($to->{'href'}) {
2053 $line = '+++ b/' .
2054 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
2055 esc_path($to->{'file'}));
2056 } else {
2057 $line = '+++ b/' .
2058 esc_path($to->{'file'});
2061 $result .= qq!<div class="diff to_file">$line</div>\n!;
2063 return $result;
2066 # create note for patch simplified by combined diff
2067 sub format_diff_cc_simplified {
2068 my ($diffinfo, @parents) = @_;
2069 my $result = '';
2071 $result .= "<div class=\"diff header\">" .
2072 "diff --cc ";
2073 if (!is_deleted($diffinfo)) {
2074 $result .= $cgi->a({-href => href(action=>"blob",
2075 hash_base=>$hash,
2076 hash=>$diffinfo->{'to_id'},
2077 file_name=>$diffinfo->{'to_file'}),
2078 -class => "path"},
2079 esc_path($diffinfo->{'to_file'}));
2080 } else {
2081 $result .= esc_path($diffinfo->{'to_file'});
2083 $result .= "</div>\n" . # class="diff header"
2084 "<div class=\"diff nodifferences\">" .
2085 "Simple merge" .
2086 "</div>\n"; # class="diff nodifferences"
2088 return $result;
2091 # format patch (diff) line (not to be used for diff headers)
2092 sub format_diff_line {
2093 my $line = shift;
2094 my ($from, $to) = @_;
2095 my $diff_class = "";
2097 chomp $line;
2099 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
2100 # combined diff
2101 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
2102 if ($line =~ m/^\@{3}/) {
2103 $diff_class = " chunk_header";
2104 } elsif ($line =~ m/^\\/) {
2105 $diff_class = " incomplete";
2106 } elsif ($prefix =~ tr/+/+/) {
2107 $diff_class = " add";
2108 } elsif ($prefix =~ tr/-/-/) {
2109 $diff_class = " rem";
2111 } else {
2112 # assume ordinary diff
2113 my $char = substr($line, 0, 1);
2114 if ($char eq '+') {
2115 $diff_class = " add";
2116 } elsif ($char eq '-') {
2117 $diff_class = " rem";
2118 } elsif ($char eq '@') {
2119 $diff_class = " chunk_header";
2120 } elsif ($char eq "\\") {
2121 $diff_class = " incomplete";
2124 $line = untabify($line);
2125 if ($from && $to && $line =~ m/^\@{2} /) {
2126 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
2127 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
2129 $from_lines = 0 unless defined $from_lines;
2130 $to_lines = 0 unless defined $to_lines;
2132 if ($from->{'href'}) {
2133 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
2134 -class=>"list"}, $from_text);
2136 if ($to->{'href'}) {
2137 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
2138 -class=>"list"}, $to_text);
2140 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
2141 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
2142 return "<div class=\"diff$diff_class\">$line</div>\n";
2143 } elsif ($from && $to && $line =~ m/^\@{3}/) {
2144 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
2145 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
2147 @from_text = split(' ', $ranges);
2148 for (my $i = 0; $i < @from_text; ++$i) {
2149 ($from_start[$i], $from_nlines[$i]) =
2150 (split(',', substr($from_text[$i], 1)), 0);
2153 $to_text = pop @from_text;
2154 $to_start = pop @from_start;
2155 $to_nlines = pop @from_nlines;
2157 $line = "<span class=\"chunk_info\">$prefix ";
2158 for (my $i = 0; $i < @from_text; ++$i) {
2159 if ($from->{'href'}[$i]) {
2160 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
2161 -class=>"list"}, $from_text[$i]);
2162 } else {
2163 $line .= $from_text[$i];
2165 $line .= " ";
2167 if ($to->{'href'}) {
2168 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
2169 -class=>"list"}, $to_text);
2170 } else {
2171 $line .= $to_text;
2173 $line .= " $prefix</span>" .
2174 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
2175 return "<div class=\"diff$diff_class\">$line</div>\n";
2177 return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
2180 # Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
2181 # linked. Pass the hash of the tree/commit to snapshot.
2182 sub format_snapshot_links {
2183 my ($hash) = @_;
2184 my $num_fmts = @snapshot_fmts;
2185 if ($num_fmts > 1) {
2186 # A parenthesized list of links bearing format names.
2187 # e.g. "snapshot (_tar.gz_ _zip_)"
2188 return "snapshot (" . join(' ', map
2189 $cgi->a({
2190 -href => href(
2191 action=>"snapshot",
2192 hash=>$hash,
2193 snapshot_format=>$_
2195 }, $known_snapshot_formats{$_}{'display'})
2196 , @snapshot_fmts) . ")";
2197 } elsif ($num_fmts == 1) {
2198 # A single "snapshot" link whose tooltip bears the format name.
2199 # i.e. "_snapshot_"
2200 my ($fmt) = @snapshot_fmts;
2201 return
2202 $cgi->a({
2203 -href => href(
2204 action=>"snapshot",
2205 hash=>$hash,
2206 snapshot_format=>$fmt
2208 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
2209 }, "snapshot");
2210 } else { # $num_fmts == 0
2211 return undef;
2215 ## ......................................................................
2216 ## functions returning values to be passed, perhaps after some
2217 ## transformation, to other functions; e.g. returning arguments to href()
2219 # returns hash to be passed to href to generate gitweb URL
2220 # in -title key it returns description of link
2221 sub get_feed_info {
2222 my $format = shift || 'Atom';
2223 my %res = (action => lc($format));
2225 # feed links are possible only for project views
2226 return unless (defined $project);
2227 # some views should link to OPML, or to generic project feed,
2228 # or don't have specific feed yet (so they should use generic)
2229 return if ($action =~ /^(?:tags|heads|forks|tag|search)$/x);
2231 my $branch;
2232 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
2233 # from tag links; this also makes possible to detect branch links
2234 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
2235 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
2236 $branch = $1;
2238 # find log type for feed description (title)
2239 my $type = 'log';
2240 if (defined $file_name) {
2241 $type = "history of $file_name";
2242 $type .= "/" if ($action eq 'tree');
2243 $type .= " on '$branch'" if (defined $branch);
2244 } else {
2245 $type = "log of $branch" if (defined $branch);
2248 $res{-title} = $type;
2249 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
2250 $res{'file_name'} = $file_name;
2252 return %res;
2255 ## ----------------------------------------------------------------------
2256 ## git utility subroutines, invoking git commands
2258 # returns path to the core git executable and the --git-dir parameter as list
2259 sub git_cmd {
2260 $number_of_git_cmds++;
2261 return $GIT, '--git-dir='.$git_dir;
2264 # quote the given arguments for passing them to the shell
2265 # quote_command("command", "arg 1", "arg with ' and ! characters")
2266 # => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
2267 # Try to avoid using this function wherever possible.
2268 sub quote_command {
2269 return join(' ',
2270 map { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ );
2273 # get HEAD ref of given project as hash
2274 sub git_get_head_hash {
2275 return git_get_full_hash(shift, 'HEAD');
2278 sub git_get_full_hash {
2279 return git_get_hash(@_);
2282 sub git_get_short_hash {
2283 return git_get_hash(@_, '--short=7');
2286 sub git_get_hash {
2287 my ($project, $hash, @options) = @_;
2288 my $o_git_dir = $git_dir;
2289 my $retval = undef;
2290 $git_dir = "$projectroot/$project";
2291 if (open my $fd, '-|', git_cmd(), 'rev-parse',
2292 '--verify', '-q', @options, $hash) {
2293 $retval = <$fd>;
2294 chomp $retval if defined $retval;
2295 close $fd;
2297 if (defined $o_git_dir) {
2298 $git_dir = $o_git_dir;
2300 return $retval;
2303 # get type of given object
2304 sub git_get_type {
2305 my $hash = shift;
2307 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
2308 my $type = <$fd>;
2309 close $fd or return;
2310 chomp $type;
2311 return $type;
2314 # repository configuration
2315 our $config_file = '';
2316 our %config;
2318 # store multiple values for single key as anonymous array reference
2319 # single values stored directly in the hash, not as [ <value> ]
2320 sub hash_set_multi {
2321 my ($hash, $key, $value) = @_;
2323 if (!exists $hash->{$key}) {
2324 $hash->{$key} = $value;
2325 } elsif (!ref $hash->{$key}) {
2326 $hash->{$key} = [ $hash->{$key}, $value ];
2327 } else {
2328 push @{$hash->{$key}}, $value;
2332 # return hash of git project configuration
2333 # optionally limited to some section, e.g. 'gitweb'
2334 sub git_parse_project_config {
2335 my $section_regexp = shift;
2336 my %config;
2338 local $/ = "\0";
2340 open my $fh, "-|", git_cmd(), "config", '-z', '-l',
2341 or return;
2343 while (my $keyval = <$fh>) {
2344 chomp $keyval;
2345 my ($key, $value) = split(/\n/, $keyval, 2);
2347 hash_set_multi(\%config, $key, $value)
2348 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
2350 close $fh;
2352 return %config;
2355 # convert config value to boolean: 'true' or 'false'
2356 # no value, number > 0, 'true' and 'yes' values are true
2357 # rest of values are treated as false (never as error)
2358 sub config_to_bool {
2359 my $val = shift;
2361 return 1 if !defined $val; # section.key
2363 # strip leading and trailing whitespace
2364 $val =~ s/^\s+//;
2365 $val =~ s/\s+$//;
2367 return (($val =~ /^\d+$/ && $val) || # section.key = 1
2368 ($val =~ /^(?:true|yes)$/i)); # section.key = true
2371 # convert config value to simple decimal number
2372 # an optional value suffix of 'k', 'm', or 'g' will cause the value
2373 # to be multiplied by 1024, 1048576, or 1073741824
2374 sub config_to_int {
2375 my $val = shift;
2377 # strip leading and trailing whitespace
2378 $val =~ s/^\s+//;
2379 $val =~ s/\s+$//;
2381 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
2382 $unit = lc($unit);
2383 # unknown unit is treated as 1
2384 return $num * ($unit eq 'g' ? 1073741824 :
2385 $unit eq 'm' ? 1048576 :
2386 $unit eq 'k' ? 1024 : 1);
2388 return $val;
2391 # convert config value to array reference, if needed
2392 sub config_to_multi {
2393 my $val = shift;
2395 return ref($val) ? $val : (defined($val) ? [ $val ] : []);
2398 sub git_get_project_config {
2399 my ($key, $type) = @_;
2401 return unless defined $git_dir;
2403 # key sanity check
2404 return unless ($key);
2405 $key =~ s/^gitweb\.//;
2406 return if ($key =~ m/\W/);
2408 # type sanity check
2409 if (defined $type) {
2410 $type =~ s/^--//;
2411 $type = undef
2412 unless ($type eq 'bool' || $type eq 'int');
2415 # get config
2416 if (!defined $config_file ||
2417 $config_file ne "$git_dir/config") {
2418 %config = git_parse_project_config('gitweb');
2419 $config_file = "$git_dir/config";
2422 # check if config variable (key) exists
2423 return unless exists $config{"gitweb.$key"};
2425 # ensure given type
2426 if (!defined $type) {
2427 return $config{"gitweb.$key"};
2428 } elsif ($type eq 'bool') {
2429 # backward compatibility: 'git config --bool' returns true/false
2430 return config_to_bool($config{"gitweb.$key"}) ? 'true' : 'false';
2431 } elsif ($type eq 'int') {
2432 return config_to_int($config{"gitweb.$key"});
2434 return $config{"gitweb.$key"};
2437 # get hash of given path at given ref
2438 sub git_get_hash_by_path {
2439 my $base = shift;
2440 my $path = shift || return undef;
2441 my $type = shift;
2443 $path =~ s,/+$,,;
2445 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
2446 or die_error(500, "Open git-ls-tree failed");
2447 my $line = <$fd>;
2448 close $fd or return undef;
2450 if (!defined $line) {
2451 # there is no tree or hash given by $path at $base
2452 return undef;
2455 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2456 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
2457 if (defined $type && $type ne $2) {
2458 # type doesn't match
2459 return undef;
2461 return $3;
2464 # get path of entry with given hash at given tree-ish (ref)
2465 # used to get 'from' filename for combined diff (merge commit) for renames
2466 sub git_get_path_by_hash {
2467 my $base = shift || return;
2468 my $hash = shift || return;
2470 local $/ = "\0";
2472 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
2473 or return undef;
2474 while (my $line = <$fd>) {
2475 chomp $line;
2477 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
2478 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
2479 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
2480 close $fd;
2481 return $1;
2484 close $fd;
2485 return undef;
2488 ## ......................................................................
2489 ## git utility functions, directly accessing git repository
2491 sub git_get_project_description {
2492 my $path = shift;
2494 $git_dir = "$projectroot/$path";
2495 open my $fd, '<', "$git_dir/description"
2496 or return git_get_project_config('description');
2497 my $descr = <$fd>;
2498 close $fd;
2499 if (defined $descr) {
2500 chomp $descr;
2502 return $descr;
2505 sub git_get_project_ctags {
2506 my $path = shift;
2507 my $ctags = {};
2509 $git_dir = "$projectroot/$path";
2510 opendir my $dh, "$git_dir/ctags"
2511 or return $ctags;
2512 foreach (grep { -f $_ } map { "$git_dir/ctags/$_" } readdir($dh)) {
2513 open my $ct, '<', $_ or next;
2514 my $val = <$ct>;
2515 chomp $val;
2516 close $ct;
2517 my $ctag = $_; $ctag =~ s#.*/##;
2518 $ctags->{$ctag} = $val;
2520 closedir $dh;
2521 $ctags;
2524 sub git_populate_project_tagcloud {
2525 my $ctags = shift;
2527 # First, merge different-cased tags; tags vote on casing
2528 my %ctags_lc;
2529 foreach (keys %$ctags) {
2530 $ctags_lc{lc $_}->{count} += $ctags->{$_};
2531 if (not $ctags_lc{lc $_}->{topcount}
2532 or $ctags_lc{lc $_}->{topcount} < $ctags->{$_}) {
2533 $ctags_lc{lc $_}->{topcount} = $ctags->{$_};
2534 $ctags_lc{lc $_}->{topname} = $_;
2538 my $cloud;
2539 if (eval { require HTML::TagCloud; 1; }) {
2540 $cloud = HTML::TagCloud->new;
2541 foreach (sort keys %ctags_lc) {
2542 # Pad the title with spaces so that the cloud looks
2543 # less crammed.
2544 my $title = $ctags_lc{$_}->{topname};
2545 $title =~ s/ /&nbsp;/g;
2546 $title =~ s/^/&nbsp;/g;
2547 $title =~ s/$/&nbsp;/g;
2548 $cloud->add($title, $home_link."?by_tag=".$_, $ctags_lc{$_}->{count});
2550 } else {
2551 $cloud = \%ctags_lc;
2553 $cloud;
2556 sub git_show_project_tagcloud {
2557 my ($cloud, $count) = @_;
2558 print STDERR ref($cloud)."..\n";
2559 if (ref $cloud eq 'HTML::TagCloud') {
2560 return $cloud->html_and_css($count);
2561 } else {
2562 my @tags = sort { $cloud->{$a}->{count} <=> $cloud->{$b}->{count} } keys %$cloud;
2563 return '<p align="center">' . join (', ', map {
2564 "<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"
2565 } splice(@tags, 0, $count)) . '</p>';
2569 sub git_get_project_url_list {
2570 my $path = shift;
2572 $git_dir = "$projectroot/$path";
2573 open my $fd, '<', "$git_dir/cloneurl"
2574 or return wantarray ?
2575 @{ config_to_multi(git_get_project_config('url')) } :
2576 config_to_multi(git_get_project_config('url'));
2577 my @git_project_url_list = map { chomp; $_ } <$fd>;
2578 close $fd;
2580 return wantarray ? @git_project_url_list : \@git_project_url_list;
2583 sub git_get_projects_list {
2584 my ($filter) = @_;
2585 my @list;
2587 $filter ||= '';
2588 $filter =~ s/\.git$//;
2590 my $check_forks = gitweb_check_feature('forks');
2592 if (-d $projects_list) {
2593 # search in directory
2594 my $dir = $projects_list . ($filter ? "/$filter" : '');
2595 # remove the trailing "/"
2596 $dir =~ s!/+$!!;
2597 my $pfxlen = length("$dir");
2598 my $pfxdepth = ($dir =~ tr!/!!);
2600 File::Find::find({
2601 follow_fast => 1, # follow symbolic links
2602 follow_skip => 2, # ignore duplicates
2603 dangling_symlinks => 0, # ignore dangling symlinks, silently
2604 wanted => sub {
2605 # global variables
2606 our $project_maxdepth;
2607 our $projectroot;
2608 # skip project-list toplevel, if we get it.
2609 return if (m!^[/.]$!);
2610 # only directories can be git repositories
2611 return unless (-d $_);
2612 # don't traverse too deep (Find is super slow on os x)
2613 if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
2614 $File::Find::prune = 1;
2615 return;
2618 my $subdir = substr($File::Find::name, $pfxlen + 1);
2619 # we check related file in $projectroot
2620 my $path = ($filter ? "$filter/" : '') . $subdir;
2621 if (check_export_ok("$projectroot/$path")) {
2622 push @list, { path => $path };
2623 $File::Find::prune = 1;
2626 }, "$dir");
2628 } elsif (-f $projects_list) {
2629 # read from file(url-encoded):
2630 # 'git%2Fgit.git Linus+Torvalds'
2631 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2632 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2633 my %paths;
2634 open my $fd, '<', $projects_list or return;
2635 PROJECT:
2636 while (my $line = <$fd>) {
2637 chomp $line;
2638 my ($path, $owner) = split ' ', $line;
2639 $path = unescape($path);
2640 $owner = unescape($owner);
2641 if (!defined $path) {
2642 next;
2644 if ($filter ne '') {
2645 # looking for forks;
2646 my $pfx = substr($path, 0, length($filter));
2647 if ($pfx ne $filter) {
2648 next PROJECT;
2650 my $sfx = substr($path, length($filter));
2651 if ($sfx !~ /^\/.*\.git$/) {
2652 next PROJECT;
2654 } elsif ($check_forks) {
2655 PATH:
2656 foreach my $filter (keys %paths) {
2657 # looking for forks;
2658 my $pfx = substr($path, 0, length($filter));
2659 if ($pfx ne $filter) {
2660 next PATH;
2662 my $sfx = substr($path, length($filter));
2663 if ($sfx !~ /^\/.*\.git$/) {
2664 next PATH;
2666 # is a fork, don't include it in
2667 # the list
2668 next PROJECT;
2671 if (check_export_ok("$projectroot/$path")) {
2672 my $pr = {
2673 path => $path,
2674 owner => to_utf8($owner),
2676 push @list, $pr;
2677 (my $forks_path = $path) =~ s/\.git$//;
2678 $paths{$forks_path}++;
2681 close $fd;
2683 return @list;
2686 our $gitweb_project_owner = undef;
2687 sub git_get_project_list_from_file {
2689 return if (defined $gitweb_project_owner);
2691 $gitweb_project_owner = {};
2692 # read from file (url-encoded):
2693 # 'git%2Fgit.git Linus+Torvalds'
2694 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2695 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2696 if (-f $projects_list) {
2697 open(my $fd, '<', $projects_list);
2698 while (my $line = <$fd>) {
2699 chomp $line;
2700 my ($pr, $ow) = split ' ', $line;
2701 $pr = unescape($pr);
2702 $ow = unescape($ow);
2703 $gitweb_project_owner->{$pr} = to_utf8($ow);
2705 close $fd;
2709 sub git_get_project_owner {
2710 my $project = shift;
2711 my $owner;
2713 return undef unless $project;
2714 $git_dir = "$projectroot/$project";
2716 if (!defined $gitweb_project_owner) {
2717 git_get_project_list_from_file();
2720 if (exists $gitweb_project_owner->{$project}) {
2721 $owner = $gitweb_project_owner->{$project};
2723 if (!defined $owner){
2724 $owner = git_get_project_config('owner');
2726 if (!defined $owner) {
2727 $owner = get_file_owner("$git_dir");
2730 return $owner;
2733 sub git_get_last_activity {
2734 my ($path) = @_;
2735 my $fd;
2737 $git_dir = "$projectroot/$path";
2738 open($fd, "-|", git_cmd(), 'for-each-ref',
2739 '--format=%(committer)',
2740 '--sort=-committerdate',
2741 '--count=1',
2742 'refs/heads') or return;
2743 my $most_recent = <$fd>;
2744 close $fd or return;
2745 if (defined $most_recent &&
2746 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
2747 my $timestamp = $1;
2748 my $age = time - $timestamp;
2749 return ($age, age_string($age));
2751 return (undef, undef);
2754 sub git_get_references {
2755 my $type = shift || "";
2756 my %refs;
2757 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
2758 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
2759 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
2760 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
2761 or return;
2763 while (my $line = <$fd>) {
2764 chomp $line;
2765 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {
2766 if (defined $refs{$1}) {
2767 push @{$refs{$1}}, $2;
2768 } else {
2769 $refs{$1} = [ $2 ];
2773 close $fd or return;
2774 return \%refs;
2777 sub git_get_rev_name_tags {
2778 my $hash = shift || return undef;
2780 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
2781 or return;
2782 my $name_rev = <$fd>;
2783 close $fd;
2785 if ($name_rev =~ m|^$hash tags/(.*)$|) {
2786 return $1;
2787 } else {
2788 # catches also '$hash undefined' output
2789 return undef;
2793 ## ----------------------------------------------------------------------
2794 ## parse to hash functions
2796 sub parse_date {
2797 my $epoch = shift;
2798 my $tz = shift || "-0000";
2800 my %date;
2801 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
2802 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
2803 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
2804 $date{'hour'} = $hour;
2805 $date{'minute'} = $min;
2806 $date{'mday'} = $mday;
2807 $date{'day'} = $days[$wday];
2808 $date{'month'} = $months[$mon];
2809 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
2810 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
2811 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
2812 $mday, $months[$mon], $hour ,$min;
2813 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
2814 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
2816 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
2817 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
2818 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
2819 $date{'hour_local'} = $hour;
2820 $date{'minute_local'} = $min;
2821 $date{'tz_local'} = $tz;
2822 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
2823 1900+$year, $mon+1, $mday,
2824 $hour, $min, $sec, $tz);
2825 return %date;
2828 sub parse_tag {
2829 my $tag_id = shift;
2830 my %tag;
2831 my @comment;
2833 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
2834 $tag{'id'} = $tag_id;
2835 while (my $line = <$fd>) {
2836 chomp $line;
2837 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
2838 $tag{'object'} = $1;
2839 } elsif ($line =~ m/^type (.+)$/) {
2840 $tag{'type'} = $1;
2841 } elsif ($line =~ m/^tag (.+)$/) {
2842 $tag{'name'} = $1;
2843 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
2844 $tag{'author'} = $1;
2845 $tag{'author_epoch'} = $2;
2846 $tag{'author_tz'} = $3;
2847 if ($tag{'author'} =~ m/^([^<]+) <([^>]*)>/) {
2848 $tag{'author_name'} = $1;
2849 $tag{'author_email'} = $2;
2850 } else {
2851 $tag{'author_name'} = $tag{'author'};
2853 } elsif ($line =~ m/--BEGIN/) {
2854 push @comment, $line;
2855 last;
2856 } elsif ($line eq "") {
2857 last;
2860 push @comment, <$fd>;
2861 $tag{'comment'} = \@comment;
2862 close $fd or return;
2863 if (!defined $tag{'name'}) {
2864 return
2866 return %tag
2869 sub parse_commit_text {
2870 my ($commit_text, $withparents) = @_;
2871 my @commit_lines = split '\n', $commit_text;
2872 my %co;
2874 pop @commit_lines; # Remove '\0'
2876 if (! @commit_lines) {
2877 return;
2880 my $header = shift @commit_lines;
2881 if ($header !~ m/^[0-9a-fA-F]{40}/) {
2882 return;
2884 ($co{'id'}, my @parents) = split ' ', $header;
2885 while (my $line = shift @commit_lines) {
2886 last if $line eq "\n";
2887 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
2888 $co{'tree'} = $1;
2889 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
2890 push @parents, $1;
2891 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
2892 $co{'author'} = to_utf8($1);
2893 $co{'author_epoch'} = $2;
2894 $co{'author_tz'} = $3;
2895 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
2896 $co{'author_name'} = $1;
2897 $co{'author_email'} = $2;
2898 } else {
2899 $co{'author_name'} = $co{'author'};
2901 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
2902 $co{'committer'} = to_utf8($1);
2903 $co{'committer_epoch'} = $2;
2904 $co{'committer_tz'} = $3;
2905 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
2906 $co{'committer_name'} = $1;
2907 $co{'committer_email'} = $2;
2908 } else {
2909 $co{'committer_name'} = $co{'committer'};
2913 if (!defined $co{'tree'}) {
2914 return;
2916 $co{'parents'} = \@parents;
2917 $co{'parent'} = $parents[0];
2919 foreach my $title (@commit_lines) {
2920 $title =~ s/^ //;
2921 if ($title ne "") {
2922 $co{'title'} = chop_str($title, 80, 5);
2923 # remove leading stuff of merges to make the interesting part visible
2924 if (length($title) > 50) {
2925 $title =~ s/^Automatic //;
2926 $title =~ s/^merge (of|with) /Merge ... /i;
2927 if (length($title) > 50) {
2928 $title =~ s/(http|rsync):\/\///;
2930 if (length($title) > 50) {
2931 $title =~ s/(master|www|rsync)\.//;
2933 if (length($title) > 50) {
2934 $title =~ s/kernel.org:?//;
2936 if (length($title) > 50) {
2937 $title =~ s/\/pub\/scm//;
2940 $co{'title_short'} = chop_str($title, 50, 5);
2941 last;
2944 if (! defined $co{'title'} || $co{'title'} eq "") {
2945 $co{'title'} = $co{'title_short'} = '(no commit message)';
2947 # remove added spaces
2948 foreach my $line (@commit_lines) {
2949 $line =~ s/^ //;
2951 $co{'comment'} = \@commit_lines;
2953 my $age = time - $co{'committer_epoch'};
2954 $co{'age'} = $age;
2955 $co{'age_string'} = age_string($age);
2956 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
2957 if ($age > 60*60*24*7*2) {
2958 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2959 $co{'age_string_age'} = $co{'age_string'};
2960 } else {
2961 $co{'age_string_date'} = $co{'age_string'};
2962 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2964 return %co;
2967 sub parse_commit {
2968 my ($commit_id) = @_;
2969 my %co;
2971 local $/ = "\0";
2973 open my $fd, "-|", git_cmd(), "rev-list",
2974 "--parents",
2975 "--header",
2976 "--max-count=1",
2977 $commit_id,
2978 "--",
2979 or die_error(500, "Open git-rev-list failed");
2980 %co = parse_commit_text(<$fd>, 1);
2981 close $fd;
2983 return %co;
2986 sub parse_commits {
2987 my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
2988 my @cos;
2990 $maxcount ||= 1;
2991 $skip ||= 0;
2993 local $/ = "\0";
2995 open my $fd, "-|", git_cmd(), "rev-list",
2996 "--header",
2997 @args,
2998 ("--max-count=" . $maxcount),
2999 ("--skip=" . $skip),
3000 @extra_options,
3001 $commit_id,
3002 "--",
3003 ($filename ? ($filename) : ())
3004 or die_error(500, "Open git-rev-list failed");
3005 while (my $line = <$fd>) {
3006 my %co = parse_commit_text($line);
3007 push @cos, \%co;
3009 close $fd;
3011 return wantarray ? @cos : \@cos;
3014 # parse line of git-diff-tree "raw" output
3015 sub parse_difftree_raw_line {
3016 my $line = shift;
3017 my %res;
3019 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
3020 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
3021 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
3022 $res{'from_mode'} = $1;
3023 $res{'to_mode'} = $2;
3024 $res{'from_id'} = $3;
3025 $res{'to_id'} = $4;
3026 $res{'status'} = $5;
3027 $res{'similarity'} = $6;
3028 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
3029 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
3030 } else {
3031 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote($7);
3034 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
3035 # combined diff (for merge commit)
3036 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
3037 $res{'nparents'} = length($1);
3038 $res{'from_mode'} = [ split(' ', $2) ];
3039 $res{'to_mode'} = pop @{$res{'from_mode'}};
3040 $res{'from_id'} = [ split(' ', $3) ];
3041 $res{'to_id'} = pop @{$res{'from_id'}};
3042 $res{'status'} = [ split('', $4) ];
3043 $res{'to_file'} = unquote($5);
3045 # 'c512b523472485aef4fff9e57b229d9d243c967f'
3046 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
3047 $res{'commit'} = $1;
3050 return wantarray ? %res : \%res;
3053 # wrapper: return parsed line of git-diff-tree "raw" output
3054 # (the argument might be raw line, or parsed info)
3055 sub parsed_difftree_line {
3056 my $line_or_ref = shift;
3058 if (ref($line_or_ref) eq "HASH") {
3059 # pre-parsed (or generated by hand)
3060 return $line_or_ref;
3061 } else {
3062 return parse_difftree_raw_line($line_or_ref);
3066 # parse line of git-ls-tree output
3067 sub parse_ls_tree_line {
3068 my $line = shift;
3069 my %opts = @_;
3070 my %res;
3072 if ($opts{'-l'}) {
3073 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'
3074 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;
3076 $res{'mode'} = $1;
3077 $res{'type'} = $2;
3078 $res{'hash'} = $3;
3079 $res{'size'} = $4;
3080 if ($opts{'-z'}) {
3081 $res{'name'} = $5;
3082 } else {
3083 $res{'name'} = unquote($5);
3085 } else {
3086 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
3087 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
3089 $res{'mode'} = $1;
3090 $res{'type'} = $2;
3091 $res{'hash'} = $3;
3092 if ($opts{'-z'}) {
3093 $res{'name'} = $4;
3094 } else {
3095 $res{'name'} = unquote($4);
3099 return wantarray ? %res : \%res;
3102 # generates _two_ hashes, references to which are passed as 2 and 3 argument
3103 sub parse_from_to_diffinfo {
3104 my ($diffinfo, $from, $to, @parents) = @_;
3106 if ($diffinfo->{'nparents'}) {
3107 # combined diff
3108 $from->{'file'} = [];
3109 $from->{'href'} = [];
3110 fill_from_file_info($diffinfo, @parents)
3111 unless exists $diffinfo->{'from_file'};
3112 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
3113 $from->{'file'}[$i] =
3114 defined $diffinfo->{'from_file'}[$i] ?
3115 $diffinfo->{'from_file'}[$i] :
3116 $diffinfo->{'to_file'};
3117 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
3118 $from->{'href'}[$i] = href(action=>"blob",
3119 hash_base=>$parents[$i],
3120 hash=>$diffinfo->{'from_id'}[$i],
3121 file_name=>$from->{'file'}[$i]);
3122 } else {
3123 $from->{'href'}[$i] = undef;
3126 } else {
3127 # ordinary (not combined) diff
3128 $from->{'file'} = $diffinfo->{'from_file'};
3129 if ($diffinfo->{'status'} ne "A") { # not new (added) file
3130 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
3131 hash=>$diffinfo->{'from_id'},
3132 file_name=>$from->{'file'});
3133 } else {
3134 delete $from->{'href'};
3138 $to->{'file'} = $diffinfo->{'to_file'};
3139 if (!is_deleted($diffinfo)) { # file exists in result
3140 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
3141 hash=>$diffinfo->{'to_id'},
3142 file_name=>$to->{'file'});
3143 } else {
3144 delete $to->{'href'};
3148 ## ......................................................................
3149 ## parse to array of hashes functions
3151 sub git_get_heads_list {
3152 my $limit = shift;
3153 my @headslist;
3155 open my $fd, '-|', git_cmd(), 'for-each-ref',
3156 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
3157 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
3158 'refs/heads'
3159 or return;
3160 while (my $line = <$fd>) {
3161 my %ref_item;
3163 chomp $line;
3164 my ($refinfo, $committerinfo) = split(/\0/, $line);
3165 my ($hash, $name, $title) = split(' ', $refinfo, 3);
3166 my ($committer, $epoch, $tz) =
3167 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
3168 $ref_item{'fullname'} = $name;
3169 $name =~ s!^refs/heads/!!;
3171 $ref_item{'name'} = $name;
3172 $ref_item{'id'} = $hash;
3173 $ref_item{'title'} = $title || '(no commit message)';
3174 $ref_item{'epoch'} = $epoch;
3175 if ($epoch) {
3176 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
3177 } else {
3178 $ref_item{'age'} = "unknown";
3181 push @headslist, \%ref_item;
3183 close $fd;
3185 return wantarray ? @headslist : \@headslist;
3188 sub git_get_tags_list {
3189 my $limit = shift;
3190 my @tagslist;
3192 open my $fd, '-|', git_cmd(), 'for-each-ref',
3193 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
3194 '--format=%(objectname) %(objecttype) %(refname) '.
3195 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
3196 'refs/tags'
3197 or return;
3198 while (my $line = <$fd>) {
3199 my %ref_item;
3201 chomp $line;
3202 my ($refinfo, $creatorinfo) = split(/\0/, $line);
3203 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
3204 my ($creator, $epoch, $tz) =
3205 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
3206 $ref_item{'fullname'} = $name;
3207 $name =~ s!^refs/tags/!!;
3209 $ref_item{'type'} = $type;
3210 $ref_item{'id'} = $id;
3211 $ref_item{'name'} = $name;
3212 if ($type eq "tag") {
3213 $ref_item{'subject'} = $title;
3214 $ref_item{'reftype'} = $reftype;
3215 $ref_item{'refid'} = $refid;
3216 } else {
3217 $ref_item{'reftype'} = $type;
3218 $ref_item{'refid'} = $id;
3221 if ($type eq "tag" || $type eq "commit") {
3222 $ref_item{'epoch'} = $epoch;
3223 if ($epoch) {
3224 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
3225 } else {
3226 $ref_item{'age'} = "unknown";
3230 push @tagslist, \%ref_item;
3232 close $fd;
3234 return wantarray ? @tagslist : \@tagslist;
3237 ## ----------------------------------------------------------------------
3238 ## filesystem-related functions
3240 sub get_file_owner {
3241 my $path = shift;
3243 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
3244 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
3245 if (!defined $gcos) {
3246 return undef;
3248 my $owner = $gcos;
3249 $owner =~ s/[,;].*$//;
3250 return to_utf8($owner);
3253 # assume that file exists
3254 sub insert_file {
3255 my $filename = shift;
3257 open my $fd, '<', $filename;
3258 print map { to_utf8($_) } <$fd>;
3259 close $fd;
3262 ## ......................................................................
3263 ## mimetype related functions
3265 sub mimetype_guess_file {
3266 my $filename = shift;
3267 my $mimemap = shift;
3268 -r $mimemap or return undef;
3270 my %mimemap;
3271 open(my $mh, '<', $mimemap) or return undef;
3272 while (<$mh>) {
3273 next if m/^#/; # skip comments
3274 my ($mimetype, $exts) = split(/\t+/);
3275 if (defined $exts) {
3276 my @exts = split(/\s+/, $exts);
3277 foreach my $ext (@exts) {
3278 $mimemap{$ext} = $mimetype;
3282 close($mh);
3284 $filename =~ /\.([^.]*)$/;
3285 return $mimemap{$1};
3288 sub mimetype_guess {
3289 my $filename = shift;
3290 my $mime;
3291 $filename =~ /\./ or return undef;
3293 if ($mimetypes_file) {
3294 my $file = $mimetypes_file;
3295 if ($file !~ m!^/!) { # if it is relative path
3296 # it is relative to project
3297 $file = "$projectroot/$project/$file";
3299 $mime = mimetype_guess_file($filename, $file);
3301 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
3302 return $mime;
3305 sub blob_mimetype {
3306 my $fd = shift;
3307 my $filename = shift;
3309 if ($filename) {
3310 my $mime = mimetype_guess($filename);
3311 $mime and return $mime;
3314 # just in case
3315 return $default_blob_plain_mimetype unless $fd;
3317 if (-T $fd) {
3318 return 'text/plain';
3319 } elsif (! $filename) {
3320 return 'application/octet-stream';
3321 } elsif ($filename =~ m/\.png$/i) {
3322 return 'image/png';
3323 } elsif ($filename =~ m/\.gif$/i) {
3324 return 'image/gif';
3325 } elsif ($filename =~ m/\.jpe?g$/i) {
3326 return 'image/jpeg';
3327 } else {
3328 return 'application/octet-stream';
3332 sub blob_contenttype {
3333 my ($fd, $file_name, $type) = @_;
3335 $type ||= blob_mimetype($fd, $file_name);
3336 if ($type eq 'text/plain' && defined $default_text_plain_charset) {
3337 $type .= "; charset=$default_text_plain_charset";
3340 return $type;
3343 # guess file syntax for syntax highlighting; return undef if no highlighting
3344 # the name of syntax can (in the future) depend on syntax highlighter used
3345 sub guess_file_syntax {
3346 my ($highlight, $mimetype, $file_name) = @_;
3347 return undef unless ($highlight && defined $file_name);
3348 my $basename = basename($file_name, '.in');
3349 return $highlight_basename{$basename}
3350 if exists $highlight_basename{$basename};
3352 $basename =~ /\.([^.]*)$/;
3353 my $ext = $1 or return undef;
3354 return $highlight_ext{$ext}
3355 if exists $highlight_ext{$ext};
3357 return undef;
3360 # run highlighter and return FD of its output,
3361 # or return original FD if no highlighting
3362 sub run_highlighter {
3363 my ($fd, $highlight, $syntax) = @_;
3364 return $fd unless ($highlight && defined $syntax);
3366 close $fd
3367 or die_error(404, "Reading blob failed");
3368 open $fd, quote_command(git_cmd(), "cat-file", "blob", $hash)." | ".
3369 quote_command($highlight_bin).
3370 " --xhtml --fragment --syntax $syntax |"
3371 or die_error(500, "Couldn't open file or run syntax highlighter");
3372 return $fd;
3375 ## ======================================================================
3376 ## functions printing HTML: header, footer, error page
3378 sub get_page_title {
3379 my $title = to_utf8($site_name);
3381 return $title unless (defined $project);
3382 $title .= " - " . to_utf8($project);
3384 return $title unless (defined $action);
3385 $title .= "/$action"; # $action is US-ASCII (7bit ASCII)
3387 return $title unless (defined $file_name);
3388 $title .= " - " . esc_path($file_name);
3389 if ($action eq "tree" && $file_name !~ m|/$|) {
3390 $title .= "/";
3393 return $title;
3396 sub git_header_html {
3397 my $status = shift || "200 OK";
3398 my $expires = shift;
3399 my %opts = @_;
3401 my $title = get_page_title();
3402 my $content_type;
3403 # require explicit support from the UA if we are to send the page as
3404 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
3405 # we have to do this because MSIE sometimes globs '*/*', pretending to
3406 # support xhtml+xml but choking when it gets what it asked for.
3407 if (defined $cgi->http('HTTP_ACCEPT') &&
3408 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
3409 $cgi->Accept('application/xhtml+xml') != 0) {
3410 $content_type = 'application/xhtml+xml';
3411 } else {
3412 $content_type = 'text/html';
3414 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
3415 -status=> $status, -expires => $expires)
3416 unless ($opts{'-no_http_header'});
3417 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
3418 print <<EOF;
3419 <?xml version="1.0" encoding="utf-8"?>
3420 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3421 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
3422 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
3423 <!-- git core binaries version $git_version -->
3424 <head>
3425 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
3426 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
3427 <meta name="robots" content="index, nofollow"/>
3428 <title>$title</title>
3430 # the stylesheet, favicon etc urls won't work correctly with path_info
3431 # unless we set the appropriate base URL
3432 if ($ENV{'PATH_INFO'}) {
3433 print "<base href=\"".esc_url($base_url)."\" />\n";
3435 # print out each stylesheet that exist, providing backwards capability
3436 # for those people who defined $stylesheet in a config file
3437 if (defined $stylesheet) {
3438 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
3439 } else {
3440 foreach my $stylesheet (@stylesheets) {
3441 next unless $stylesheet;
3442 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
3445 if (defined $project) {
3446 my %href_params = get_feed_info();
3447 if (!exists $href_params{'-title'}) {
3448 $href_params{'-title'} = 'log';
3451 foreach my $format qw(RSS Atom) {
3452 my $type = lc($format);
3453 my %link_attr = (
3454 '-rel' => 'alternate',
3455 '-title' => "$project - $href_params{'-title'} - $format feed",
3456 '-type' => "application/$type+xml"
3459 $href_params{'action'} = $type;
3460 $link_attr{'-href'} = href(%href_params);
3461 print "<link ".
3462 "rel=\"$link_attr{'-rel'}\" ".
3463 "title=\"$link_attr{'-title'}\" ".
3464 "href=\"$link_attr{'-href'}\" ".
3465 "type=\"$link_attr{'-type'}\" ".
3466 "/>\n";
3468 $href_params{'extra_options'} = '--no-merges';
3469 $link_attr{'-href'} = href(%href_params);
3470 $link_attr{'-title'} .= ' (no merges)';
3471 print "<link ".
3472 "rel=\"$link_attr{'-rel'}\" ".
3473 "title=\"$link_attr{'-title'}\" ".
3474 "href=\"$link_attr{'-href'}\" ".
3475 "type=\"$link_attr{'-type'}\" ".
3476 "/>\n";
3479 } else {
3480 printf('<link rel="alternate" title="%s projects list" '.
3481 'href="%s" type="text/plain; charset=utf-8" />'."\n",
3482 $site_name, href(project=>undef, action=>"project_index"));
3483 printf('<link rel="alternate" title="%s projects feeds" '.
3484 'href="%s" type="text/x-opml" />'."\n",
3485 $site_name, href(project=>undef, action=>"opml"));
3487 if (defined $favicon) {
3488 print qq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);
3491 print "</head>\n" .
3492 "<body>\n";
3494 if (defined $site_header && -f $site_header) {
3495 insert_file($site_header);
3498 print "<div class=\"page_header\">\n" .
3499 $cgi->a({-href => esc_url($logo_url),
3500 -title => $logo_label},
3501 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
3502 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
3503 if (defined $project) {
3504 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
3505 if (defined $action) {
3506 print " / $action";
3508 print "\n";
3510 print "</div>\n";
3512 my $have_search = gitweb_check_feature('search');
3513 if (defined $project && $have_search) {
3514 if (!defined $searchtext) {
3515 $searchtext = "";
3517 my $search_hash;
3518 if (defined $hash_base) {
3519 $search_hash = $hash_base;
3520 } elsif (defined $hash) {
3521 $search_hash = $hash;
3522 } else {
3523 $search_hash = "HEAD";
3525 my $action = $my_uri;
3526 my $use_pathinfo = gitweb_check_feature('pathinfo');
3527 if ($use_pathinfo) {
3528 $action .= "/".esc_url($project);
3530 print $cgi->startform(-method => "get", -action => $action) .
3531 "<div class=\"search\">\n" .
3532 (!$use_pathinfo &&
3533 $cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) . "\n") .
3534 $cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) . "\n" .
3535 $cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) . "\n" .
3536 $cgi->popup_menu(-name => 'st', -default => 'commit',
3537 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
3538 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
3539 " search:\n",
3540 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
3541 "<span title=\"Extended regular expression\">" .
3542 $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
3543 -checked => $search_use_regexp) .
3544 "</span>" .
3545 "</div>" .
3546 $cgi->end_form() . "\n";
3550 sub git_footer_html {
3551 my $feed_class = 'rss_logo';
3553 print "<div class=\"page_footer\">\n";
3554 if (defined $project) {
3555 my $descr = git_get_project_description($project);
3556 if (defined $descr) {
3557 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
3560 my %href_params = get_feed_info();
3561 if (!%href_params) {
3562 $feed_class .= ' generic';
3564 $href_params{'-title'} ||= 'log';
3566 foreach my $format qw(RSS Atom) {
3567 $href_params{'action'} = lc($format);
3568 print $cgi->a({-href => href(%href_params),
3569 -title => "$href_params{'-title'} $format feed",
3570 -class => $feed_class}, $format)."\n";
3573 } else {
3574 print $cgi->a({-href => href(project=>undef, action=>"opml"),
3575 -class => $feed_class}, "OPML") . " ";
3576 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
3577 -class => $feed_class}, "TXT") . "\n";
3579 print "</div>\n"; # class="page_footer"
3581 if (defined $t0 && gitweb_check_feature('timed')) {
3582 print "<div id=\"generating_info\">\n";
3583 print 'This page took '.
3584 '<span id="generating_time" class="time_span">'.
3585 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).
3586 ' seconds </span>'.
3587 ' and '.
3588 '<span id="generating_cmd">'.
3589 $number_of_git_cmds.
3590 '</span> git commands '.
3591 " to generate.\n";
3592 print "</div>\n"; # class="page_footer"
3595 if (defined $site_footer && -f $site_footer) {
3596 insert_file($site_footer);
3599 print qq!<script type="text/javascript" src="$javascript"></script>\n!;
3600 if (defined $action &&
3601 $action eq 'blame_incremental') {
3602 print qq!<script type="text/javascript">\n!.
3603 qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.
3604 qq! "!. href() .qq!");\n!.
3605 qq!</script>\n!;
3606 } elsif (gitweb_check_feature('javascript-actions')) {
3607 print qq!<script type="text/javascript">\n!.
3608 qq!window.onload = fixLinks;\n!.
3609 qq!</script>\n!;
3612 print "</body>\n" .
3613 "</html>";
3616 # die_error(<http_status_code>, <error_message>[, <detailed_html_description>])
3617 # Example: die_error(404, 'Hash not found')
3618 # By convention, use the following status codes (as defined in RFC 2616):
3619 # 400: Invalid or missing CGI parameters, or
3620 # requested object exists but has wrong type.
3621 # 403: Requested feature (like "pickaxe" or "snapshot") not enabled on
3622 # this server or project.
3623 # 404: Requested object/revision/project doesn't exist.
3624 # 500: The server isn't configured properly, or
3625 # an internal error occurred (e.g. failed assertions caused by bugs), or
3626 # an unknown error occurred (e.g. the git binary died unexpectedly).
3627 # 503: The server is currently unavailable (because it is overloaded,
3628 # or down for maintenance). Generally, this is a temporary state.
3629 sub die_error {
3630 my $status = shift || 500;
3631 my $error = esc_html(shift) || "Internal Server Error";
3632 my $extra = shift;
3633 my %opts = @_;
3635 my %http_responses = (
3636 400 => '400 Bad Request',
3637 403 => '403 Forbidden',
3638 404 => '404 Not Found',
3639 500 => '500 Internal Server Error',
3640 503 => '503 Service Unavailable',
3642 git_header_html($http_responses{$status}, undef, %opts);
3643 print <<EOF;
3644 <div class="page_body">
3645 <br /><br />
3646 $status - $error
3647 <br />
3649 if (defined $extra) {
3650 print "<hr />\n" .
3651 "$extra\n";
3653 print "</div>\n";
3655 git_footer_html();
3656 goto DONE_GITWEB
3657 unless ($opts{'-error_handler'});
3660 ## ----------------------------------------------------------------------
3661 ## functions printing or outputting HTML: navigation
3663 sub git_print_page_nav {
3664 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
3665 $extra = '' if !defined $extra; # pager or formats
3667 my @navs = qw(summary shortlog log commit commitdiff tree);
3668 if ($suppress) {
3669 @navs = grep { $_ ne $suppress } @navs;
3672 my %arg = map { $_ => {action=>$_} } @navs;
3673 if (defined $head) {
3674 for (qw(commit commitdiff)) {
3675 $arg{$_}{'hash'} = $head;
3677 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
3678 for (qw(shortlog log)) {
3679 $arg{$_}{'hash'} = $head;
3684 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
3685 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
3687 my @actions = gitweb_get_feature('actions');
3688 my %repl = (
3689 '%' => '%',
3690 'n' => $project, # project name
3691 'f' => $git_dir, # project path within filesystem
3692 'h' => $treehead || '', # current hash ('h' parameter)
3693 'b' => $treebase || '', # hash base ('hb' parameter)
3695 while (@actions) {
3696 my ($label, $link, $pos) = splice(@actions,0,3);
3697 # insert
3698 @navs = map { $_ eq $pos ? ($_, $label) : $_ } @navs;
3699 # munch munch
3700 $link =~ s/%([%nfhb])/$repl{$1}/g;
3701 $arg{$label}{'_href'} = $link;
3704 print "<div class=\"page_nav\">\n" .
3705 (join " | ",
3706 map { $_ eq $current ?
3707 $_ : $cgi->a({-href => ($arg{$_}{_href} ? $arg{$_}{_href} : href(%{$arg{$_}}))}, "$_")
3708 } @navs);
3709 print "<br/>\n$extra<br/>\n" .
3710 "</div>\n";
3713 sub format_paging_nav {
3714 my ($action, $page, $has_next_link) = @_;
3715 my $paging_nav;
3718 if ($page > 0) {
3719 $paging_nav .=
3720 $cgi->a({-href => href(-replay=>1, page=>undef)}, "first") .
3721 " &sdot; " .
3722 $cgi->a({-href => href(-replay=>1, page=>$page-1),
3723 -accesskey => "p", -title => "Alt-p"}, "prev");
3724 } else {
3725 $paging_nav .= "first &sdot; prev";
3728 if ($has_next_link) {
3729 $paging_nav .= " &sdot; " .
3730 $cgi->a({-href => href(-replay=>1, page=>$page+1),
3731 -accesskey => "n", -title => "Alt-n"}, "next");
3732 } else {
3733 $paging_nav .= " &sdot; next";
3736 return $paging_nav;
3739 ## ......................................................................
3740 ## functions printing or outputting HTML: div
3742 sub git_print_header_div {
3743 my ($action, $title, $hash, $hash_base) = @_;
3744 my %args = ();
3746 $args{'action'} = $action;
3747 $args{'hash'} = $hash if $hash;
3748 $args{'hash_base'} = $hash_base if $hash_base;
3750 print "<div class=\"header\">\n" .
3751 $cgi->a({-href => href(%args), -class => "title"},
3752 $title ? $title : $action) .
3753 "\n</div>\n";
3756 sub print_local_time {
3757 print format_local_time(@_);
3760 sub format_local_time {
3761 my $localtime = '';
3762 my %date = @_;
3763 if ($date{'hour_local'} < 6) {
3764 $localtime .= sprintf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
3765 $date{'hour_local'}, $date{'minute_local'}, $date{'tz_local'});
3766 } else {
3767 $localtime .= sprintf(" (%02d:%02d %s)",
3768 $date{'hour_local'}, $date{'minute_local'}, $date{'tz_local'});
3771 return $localtime;
3774 # Outputs the author name and date in long form
3775 sub git_print_authorship {
3776 my $co = shift;
3777 my %opts = @_;
3778 my $tag = $opts{-tag} || 'div';
3779 my $author = $co->{'author_name'};
3781 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
3782 print "<$tag class=\"author_date\">" .
3783 format_search_author($author, "author", esc_html($author)) .
3784 " [$ad{'rfc2822'}";
3785 print_local_time(%ad) if ($opts{-localtime});
3786 print "]" . git_get_avatar($co->{'author_email'}, -pad_before => 1)
3787 . "</$tag>\n";
3790 # Outputs table rows containing the full author or committer information,
3791 # in the format expected for 'commit' view (& similar).
3792 # Parameters are a commit hash reference, followed by the list of people
3793 # to output information for. If the list is empty it defaults to both
3794 # author and committer.
3795 sub git_print_authorship_rows {
3796 my $co = shift;
3797 # too bad we can't use @people = @_ || ('author', 'committer')
3798 my @people = @_;
3799 @people = ('author', 'committer') unless @people;
3800 foreach my $who (@people) {
3801 my %wd = parse_date($co->{"${who}_epoch"}, $co->{"${who}_tz"});
3802 print "<tr><td>$who</td><td>" .
3803 format_search_author($co->{"${who}_name"}, $who,
3804 esc_html($co->{"${who}_name"})) . " " .
3805 format_search_author($co->{"${who}_email"}, $who,
3806 esc_html("<" . $co->{"${who}_email"} . ">")) .
3807 "</td><td rowspan=\"2\">" .
3808 git_get_avatar($co->{"${who}_email"}, -size => 'double') .
3809 "</td></tr>\n" .
3810 "<tr>" .
3811 "<td></td><td> $wd{'rfc2822'}";
3812 print_local_time(%wd);
3813 print "</td>" .
3814 "</tr>\n";
3818 sub git_print_page_path {
3819 my $name = shift;
3820 my $type = shift;
3821 my $hb = shift;
3824 print "<div class=\"page_path\">";
3825 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
3826 -title => 'tree root'}, to_utf8("[$project]"));
3827 print " / ";
3828 if (defined $name) {
3829 my @dirname = split '/', $name;
3830 my $basename = pop @dirname;
3831 my $fullname = '';
3833 foreach my $dir (@dirname) {
3834 $fullname .= ($fullname ? '/' : '') . $dir;
3835 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
3836 hash_base=>$hb),
3837 -title => $fullname}, esc_path($dir));
3838 print " / ";
3840 if (defined $type && $type eq 'blob') {
3841 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
3842 hash_base=>$hb),
3843 -title => $name}, esc_path($basename));
3844 } elsif (defined $type && $type eq 'tree') {
3845 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
3846 hash_base=>$hb),
3847 -title => $name}, esc_path($basename));
3848 print " / ";
3849 } else {
3850 print esc_path($basename);
3853 print "<br/></div>\n";
3856 sub git_print_log {
3857 my $log = shift;
3858 my %opts = @_;
3860 if ($opts{'-remove_title'}) {
3861 # remove title, i.e. first line of log
3862 shift @$log;
3864 # remove leading empty lines
3865 while (defined $log->[0] && $log->[0] eq "") {
3866 shift @$log;
3869 # print log
3870 my $signoff = 0;
3871 my $empty = 0;
3872 foreach my $line (@$log) {
3873 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
3874 $signoff = 1;
3875 $empty = 0;
3876 if (! $opts{'-remove_signoff'}) {
3877 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
3878 next;
3879 } else {
3880 # remove signoff lines
3881 next;
3883 } else {
3884 $signoff = 0;
3887 # print only one empty line
3888 # do not print empty line after signoff
3889 if ($line eq "") {
3890 next if ($empty || $signoff);
3891 $empty = 1;
3892 } else {
3893 $empty = 0;
3896 print format_log_line_html($line) . "<br/>\n";
3899 if ($opts{'-final_empty_line'}) {
3900 # end with single empty line
3901 print "<br/>\n" unless $empty;
3905 # return link target (what link points to)
3906 sub git_get_link_target {
3907 my $hash = shift;
3908 my $link_target;
3910 # read link
3911 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
3912 or return;
3914 local $/ = undef;
3915 $link_target = <$fd>;
3917 close $fd
3918 or return;
3920 return $link_target;
3923 # given link target, and the directory (basedir) the link is in,
3924 # return target of link relative to top directory (top tree);
3925 # return undef if it is not possible (including absolute links).
3926 sub normalize_link_target {
3927 my ($link_target, $basedir) = @_;
3929 # absolute symlinks (beginning with '/') cannot be normalized
3930 return if (substr($link_target, 0, 1) eq '/');
3932 # normalize link target to path from top (root) tree (dir)
3933 my $path;
3934 if ($basedir) {
3935 $path = $basedir . '/' . $link_target;
3936 } else {
3937 # we are in top (root) tree (dir)
3938 $path = $link_target;
3941 # remove //, /./, and /../
3942 my @path_parts;
3943 foreach my $part (split('/', $path)) {
3944 # discard '.' and ''
3945 next if (!$part || $part eq '.');
3946 # handle '..'
3947 if ($part eq '..') {
3948 if (@path_parts) {
3949 pop @path_parts;
3950 } else {
3951 # link leads outside repository (outside top dir)
3952 return;
3954 } else {
3955 push @path_parts, $part;
3958 $path = join('/', @path_parts);
3960 return $path;
3963 # print tree entry (row of git_tree), but without encompassing <tr> element
3964 sub git_print_tree_entry {
3965 my ($t, $basedir, $hash_base, $have_blame) = @_;
3967 my %base_key = ();
3968 $base_key{'hash_base'} = $hash_base if defined $hash_base;
3970 # The format of a table row is: mode list link. Where mode is
3971 # the mode of the entry, list is the name of the entry, an href,
3972 # and link is the action links of the entry.
3974 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
3975 if (exists $t->{'size'}) {
3976 print "<td class=\"size\">$t->{'size'}</td>\n";
3978 if ($t->{'type'} eq "blob") {
3979 print "<td class=\"list\">" .
3980 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
3981 file_name=>"$basedir$t->{'name'}", %base_key),
3982 -class => "list"}, esc_path($t->{'name'}));
3983 if (S_ISLNK(oct $t->{'mode'})) {
3984 my $link_target = git_get_link_target($t->{'hash'});
3985 if ($link_target) {
3986 my $norm_target = normalize_link_target($link_target, $basedir);
3987 if (defined $norm_target) {
3988 print " -> " .
3989 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
3990 file_name=>$norm_target),
3991 -title => $norm_target}, esc_path($link_target));
3992 } else {
3993 print " -> " . esc_path($link_target);
3997 print "</td>\n";
3998 print "<td class=\"link\">";
3999 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
4000 file_name=>"$basedir$t->{'name'}", %base_key)},
4001 "blob");
4002 if ($have_blame) {
4003 print " | " .
4004 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
4005 file_name=>"$basedir$t->{'name'}", %base_key)},
4006 "blame");
4008 if (defined $hash_base) {
4009 print " | " .
4010 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
4011 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
4012 "history");
4014 print " | " .
4015 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
4016 file_name=>"$basedir$t->{'name'}")},
4017 "raw");
4018 print "</td>\n";
4020 } elsif ($t->{'type'} eq "tree") {
4021 print "<td class=\"list\">";
4022 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
4023 file_name=>"$basedir$t->{'name'}",
4024 %base_key)},
4025 esc_path($t->{'name'}));
4026 print "</td>\n";
4027 print "<td class=\"link\">";
4028 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
4029 file_name=>"$basedir$t->{'name'}",
4030 %base_key)},
4031 "tree");
4032 if (defined $hash_base) {
4033 print " | " .
4034 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
4035 file_name=>"$basedir$t->{'name'}")},
4036 "history");
4038 print "</td>\n";
4039 } else {
4040 # unknown object: we can only present history for it
4041 # (this includes 'commit' object, i.e. submodule support)
4042 print "<td class=\"list\">" .
4043 esc_path($t->{'name'}) .
4044 "</td>\n";
4045 print "<td class=\"link\">";
4046 if (defined $hash_base) {
4047 print $cgi->a({-href => href(action=>"history",
4048 hash_base=>$hash_base,
4049 file_name=>"$basedir$t->{'name'}")},
4050 "history");
4052 print "</td>\n";
4056 ## ......................................................................
4057 ## functions printing large fragments of HTML
4059 # get pre-image filenames for merge (combined) diff
4060 sub fill_from_file_info {
4061 my ($diff, @parents) = @_;
4063 $diff->{'from_file'} = [ ];
4064 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
4065 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
4066 if ($diff->{'status'}[$i] eq 'R' ||
4067 $diff->{'status'}[$i] eq 'C') {
4068 $diff->{'from_file'}[$i] =
4069 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
4073 return $diff;
4076 # is current raw difftree line of file deletion
4077 sub is_deleted {
4078 my $diffinfo = shift;
4080 return $diffinfo->{'to_id'} eq ('0' x 40);
4083 # does patch correspond to [previous] difftree raw line
4084 # $diffinfo - hashref of parsed raw diff format
4085 # $patchinfo - hashref of parsed patch diff format
4086 # (the same keys as in $diffinfo)
4087 sub is_patch_split {
4088 my ($diffinfo, $patchinfo) = @_;
4090 return defined $diffinfo && defined $patchinfo
4091 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
4095 sub git_difftree_body {
4096 my ($difftree, $hash, @parents) = @_;
4097 my ($parent) = $parents[0];
4098 my $have_blame = gitweb_check_feature('blame');
4099 print "<div class=\"list_head\">\n";
4100 if ($#{$difftree} > 10) {
4101 print(($#{$difftree} + 1) . " files changed:\n");
4103 print "</div>\n";
4105 print "<table class=\"" .
4106 (@parents > 1 ? "combined " : "") .
4107 "diff_tree\">\n";
4109 # header only for combined diff in 'commitdiff' view
4110 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
4111 if ($has_header) {
4112 # table header
4113 print "<thead><tr>\n" .
4114 "<th></th><th></th>\n"; # filename, patchN link
4115 for (my $i = 0; $i < @parents; $i++) {
4116 my $par = $parents[$i];
4117 print "<th>" .
4118 $cgi->a({-href => href(action=>"commitdiff",
4119 hash=>$hash, hash_parent=>$par),
4120 -title => 'commitdiff to parent number ' .
4121 ($i+1) . ': ' . substr($par,0,7)},
4122 $i+1) .
4123 "&nbsp;</th>\n";
4125 print "</tr></thead>\n<tbody>\n";
4128 my $alternate = 1;
4129 my $patchno = 0;
4130 foreach my $line (@{$difftree}) {
4131 my $diff = parsed_difftree_line($line);
4133 if ($alternate) {
4134 print "<tr class=\"dark\">\n";
4135 } else {
4136 print "<tr class=\"light\">\n";
4138 $alternate ^= 1;
4140 if (exists $diff->{'nparents'}) { # combined diff
4142 fill_from_file_info($diff, @parents)
4143 unless exists $diff->{'from_file'};
4145 if (!is_deleted($diff)) {
4146 # file exists in the result (child) commit
4147 print "<td>" .
4148 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4149 file_name=>$diff->{'to_file'},
4150 hash_base=>$hash),
4151 -class => "list"}, esc_path($diff->{'to_file'})) .
4152 "</td>\n";
4153 } else {
4154 print "<td>" .
4155 esc_path($diff->{'to_file'}) .
4156 "</td>\n";
4159 if ($action eq 'commitdiff') {
4160 # link to patch
4161 $patchno++;
4162 print "<td class=\"link\">" .
4163 $cgi->a({-href => "#patch$patchno"}, "patch") .
4164 " | " .
4165 "</td>\n";
4168 my $has_history = 0;
4169 my $not_deleted = 0;
4170 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
4171 my $hash_parent = $parents[$i];
4172 my $from_hash = $diff->{'from_id'}[$i];
4173 my $from_path = $diff->{'from_file'}[$i];
4174 my $status = $diff->{'status'}[$i];
4176 $has_history ||= ($status ne 'A');
4177 $not_deleted ||= ($status ne 'D');
4179 if ($status eq 'A') {
4180 print "<td class=\"link\" align=\"right\"> | </td>\n";
4181 } elsif ($status eq 'D') {
4182 print "<td class=\"link\">" .
4183 $cgi->a({-href => href(action=>"blob",
4184 hash_base=>$hash,
4185 hash=>$from_hash,
4186 file_name=>$from_path)},
4187 "blob" . ($i+1)) .
4188 " | </td>\n";
4189 } else {
4190 if ($diff->{'to_id'} eq $from_hash) {
4191 print "<td class=\"link nochange\">";
4192 } else {
4193 print "<td class=\"link\">";
4195 print $cgi->a({-href => href(action=>"blobdiff",
4196 hash=>$diff->{'to_id'},
4197 hash_parent=>$from_hash,
4198 hash_base=>$hash,
4199 hash_parent_base=>$hash_parent,
4200 file_name=>$diff->{'to_file'},
4201 file_parent=>$from_path)},
4202 "diff" . ($i+1)) .
4203 " | </td>\n";
4207 print "<td class=\"link\">";
4208 if ($not_deleted) {
4209 print $cgi->a({-href => href(action=>"blob",
4210 hash=>$diff->{'to_id'},
4211 file_name=>$diff->{'to_file'},
4212 hash_base=>$hash)},
4213 "blob");
4214 print " | " if ($has_history);
4216 if ($has_history) {
4217 print $cgi->a({-href => href(action=>"history",
4218 file_name=>$diff->{'to_file'},
4219 hash_base=>$hash)},
4220 "history");
4222 print "</td>\n";
4224 print "</tr>\n";
4225 next; # instead of 'else' clause, to avoid extra indent
4227 # else ordinary diff
4229 my ($to_mode_oct, $to_mode_str, $to_file_type);
4230 my ($from_mode_oct, $from_mode_str, $from_file_type);
4231 if ($diff->{'to_mode'} ne ('0' x 6)) {
4232 $to_mode_oct = oct $diff->{'to_mode'};
4233 if (S_ISREG($to_mode_oct)) { # only for regular file
4234 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
4236 $to_file_type = file_type($diff->{'to_mode'});
4238 if ($diff->{'from_mode'} ne ('0' x 6)) {
4239 $from_mode_oct = oct $diff->{'from_mode'};
4240 if (S_ISREG($to_mode_oct)) { # only for regular file
4241 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
4243 $from_file_type = file_type($diff->{'from_mode'});
4246 if ($diff->{'status'} eq "A") { # created
4247 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
4248 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
4249 $mode_chng .= "]</span>";
4250 print "<td>";
4251 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4252 hash_base=>$hash, file_name=>$diff->{'file'}),
4253 -class => "list"}, esc_path($diff->{'file'}));
4254 print "</td>\n";
4255 print "<td>$mode_chng</td>\n";
4256 print "<td class=\"link\">";
4257 if ($action eq 'commitdiff') {
4258 # link to patch
4259 $patchno++;
4260 print $cgi->a({-href => "#patch$patchno"}, "patch");
4261 print " | ";
4263 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4264 hash_base=>$hash, file_name=>$diff->{'file'})},
4265 "blob");
4266 print "</td>\n";
4268 } elsif ($diff->{'status'} eq "D") { # deleted
4269 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
4270 print "<td>";
4271 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
4272 hash_base=>$parent, file_name=>$diff->{'file'}),
4273 -class => "list"}, esc_path($diff->{'file'}));
4274 print "</td>\n";
4275 print "<td>$mode_chng</td>\n";
4276 print "<td class=\"link\">";
4277 if ($action eq 'commitdiff') {
4278 # link to patch
4279 $patchno++;
4280 print $cgi->a({-href => "#patch$patchno"}, "patch");
4281 print " | ";
4283 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
4284 hash_base=>$parent, file_name=>$diff->{'file'})},
4285 "blob") . " | ";
4286 if ($have_blame) {
4287 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
4288 file_name=>$diff->{'file'})},
4289 "blame") . " | ";
4291 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
4292 file_name=>$diff->{'file'})},
4293 "history");
4294 print "</td>\n";
4296 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
4297 my $mode_chnge = "";
4298 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
4299 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
4300 if ($from_file_type ne $to_file_type) {
4301 $mode_chnge .= " from $from_file_type to $to_file_type";
4303 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
4304 if ($from_mode_str && $to_mode_str) {
4305 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
4306 } elsif ($to_mode_str) {
4307 $mode_chnge .= " mode: $to_mode_str";
4310 $mode_chnge .= "]</span>\n";
4312 print "<td>";
4313 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4314 hash_base=>$hash, file_name=>$diff->{'file'}),
4315 -class => "list"}, esc_path($diff->{'file'}));
4316 print "</td>\n";
4317 print "<td>$mode_chnge</td>\n";
4318 print "<td class=\"link\">";
4319 if ($action eq 'commitdiff') {
4320 # link to patch
4321 $patchno++;
4322 print $cgi->a({-href => "#patch$patchno"}, "patch") .
4323 " | ";
4324 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
4325 # "commit" view and modified file (not onlu mode changed)
4326 print $cgi->a({-href => href(action=>"blobdiff",
4327 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
4328 hash_base=>$hash, hash_parent_base=>$parent,
4329 file_name=>$diff->{'file'})},
4330 "diff") .
4331 " | ";
4333 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4334 hash_base=>$hash, file_name=>$diff->{'file'})},
4335 "blob") . " | ";
4336 if ($have_blame) {
4337 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
4338 file_name=>$diff->{'file'})},
4339 "blame") . " | ";
4341 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
4342 file_name=>$diff->{'file'})},
4343 "history");
4344 print "</td>\n";
4346 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
4347 my %status_name = ('R' => 'moved', 'C' => 'copied');
4348 my $nstatus = $status_name{$diff->{'status'}};
4349 my $mode_chng = "";
4350 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
4351 # mode also for directories, so we cannot use $to_mode_str
4352 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
4354 print "<td>" .
4355 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
4356 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
4357 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
4358 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
4359 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
4360 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
4361 -class => "list"}, esc_path($diff->{'from_file'})) .
4362 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
4363 "<td class=\"link\">";
4364 if ($action eq 'commitdiff') {
4365 # link to patch
4366 $patchno++;
4367 print $cgi->a({-href => "#patch$patchno"}, "patch") .
4368 " | ";
4369 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
4370 # "commit" view and modified file (not only pure rename or copy)
4371 print $cgi->a({-href => href(action=>"blobdiff",
4372 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
4373 hash_base=>$hash, hash_parent_base=>$parent,
4374 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
4375 "diff") .
4376 " | ";
4378 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4379 hash_base=>$parent, file_name=>$diff->{'to_file'})},
4380 "blob") . " | ";
4381 if ($have_blame) {
4382 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
4383 file_name=>$diff->{'to_file'})},
4384 "blame") . " | ";
4386 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
4387 file_name=>$diff->{'to_file'})},
4388 "history");
4389 print "</td>\n";
4391 } # we should not encounter Unmerged (U) or Unknown (X) status
4392 print "</tr>\n";
4394 print "</tbody>" if $has_header;
4395 print "</table>\n";
4398 sub git_patchset_body {
4399 my ($fd, $difftree, $hash, @hash_parents) = @_;
4400 my ($hash_parent) = $hash_parents[0];
4402 my $is_combined = (@hash_parents > 1);
4403 my $patch_idx = 0;
4404 my $patch_number = 0;
4405 my $patch_line;
4406 my $diffinfo;
4407 my $to_name;
4408 my (%from, %to);
4410 print "<div class=\"patchset\">\n";
4412 # skip to first patch
4413 while ($patch_line = <$fd>) {
4414 chomp $patch_line;
4416 last if ($patch_line =~ m/^diff /);
4419 PATCH:
4420 while ($patch_line) {
4422 # parse "git diff" header line
4423 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
4424 # $1 is from_name, which we do not use
4425 $to_name = unquote($2);
4426 $to_name =~ s!^b/!!;
4427 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
4428 # $1 is 'cc' or 'combined', which we do not use
4429 $to_name = unquote($2);
4430 } else {
4431 $to_name = undef;
4434 # check if current patch belong to current raw line
4435 # and parse raw git-diff line if needed
4436 if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
4437 # this is continuation of a split patch
4438 print "<div class=\"patch cont\">\n";
4439 } else {
4440 # advance raw git-diff output if needed
4441 $patch_idx++ if defined $diffinfo;
4443 # read and prepare patch information
4444 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
4446 # compact combined diff output can have some patches skipped
4447 # find which patch (using pathname of result) we are at now;
4448 if ($is_combined) {
4449 while ($to_name ne $diffinfo->{'to_file'}) {
4450 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
4451 format_diff_cc_simplified($diffinfo, @hash_parents) .
4452 "</div>\n"; # class="patch"
4454 $patch_idx++;
4455 $patch_number++;
4457 last if $patch_idx > $#$difftree;
4458 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
4462 # modifies %from, %to hashes
4463 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
4465 # this is first patch for raw difftree line with $patch_idx index
4466 # we index @$difftree array from 0, but number patches from 1
4467 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
4470 # git diff header
4471 #assert($patch_line =~ m/^diff /) if DEBUG;
4472 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
4473 $patch_number++;
4474 # print "git diff" header
4475 print format_git_diff_header_line($patch_line, $diffinfo,
4476 \%from, \%to);
4478 # print extended diff header
4479 print "<div class=\"diff extended_header\">\n";
4480 EXTENDED_HEADER:
4481 while ($patch_line = <$fd>) {
4482 chomp $patch_line;
4484 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
4486 print format_extended_diff_header_line($patch_line, $diffinfo,
4487 \%from, \%to);
4489 print "</div>\n"; # class="diff extended_header"
4491 # from-file/to-file diff header
4492 if (! $patch_line) {
4493 print "</div>\n"; # class="patch"
4494 last PATCH;
4496 next PATCH if ($patch_line =~ m/^diff /);
4497 #assert($patch_line =~ m/^---/) if DEBUG;
4499 my $last_patch_line = $patch_line;
4500 $patch_line = <$fd>;
4501 chomp $patch_line;
4502 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
4504 print format_diff_from_to_header($last_patch_line, $patch_line,
4505 $diffinfo, \%from, \%to,
4506 @hash_parents);
4508 # the patch itself
4509 LINE:
4510 while ($patch_line = <$fd>) {
4511 chomp $patch_line;
4513 next PATCH if ($patch_line =~ m/^diff /);
4515 print format_diff_line($patch_line, \%from, \%to);
4518 } continue {
4519 print "</div>\n"; # class="patch"
4522 # for compact combined (--cc) format, with chunk and patch simplification
4523 # the patchset might be empty, but there might be unprocessed raw lines
4524 for (++$patch_idx if $patch_number > 0;
4525 $patch_idx < @$difftree;
4526 ++$patch_idx) {
4527 # read and prepare patch information
4528 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
4530 # generate anchor for "patch" links in difftree / whatchanged part
4531 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
4532 format_diff_cc_simplified($diffinfo, @hash_parents) .
4533 "</div>\n"; # class="patch"
4535 $patch_number++;
4538 if ($patch_number == 0) {
4539 if (@hash_parents > 1) {
4540 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
4541 } else {
4542 print "<div class=\"diff nodifferences\">No differences found</div>\n";
4546 print "</div>\n"; # class="patchset"
4549 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4551 # fills project list info (age, description, owner, forks) for each
4552 # project in the list, removing invalid projects from returned list
4553 # NOTE: modifies $projlist, but does not remove entries from it
4554 sub fill_project_list_info {
4555 my ($projlist, $check_forks) = @_;
4556 my @projects;
4558 my $show_ctags = gitweb_check_feature('ctags');
4559 PROJECT:
4560 foreach my $pr (@$projlist) {
4561 my (@activity) = git_get_last_activity($pr->{'path'});
4562 unless (@activity) {
4563 next PROJECT;
4565 ($pr->{'age'}, $pr->{'age_string'}) = @activity;
4566 if (!defined $pr->{'descr'}) {
4567 my $descr = git_get_project_description($pr->{'path'}) || "";
4568 $descr = to_utf8($descr);
4569 $pr->{'descr_long'} = $descr;
4570 $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
4572 if (!defined $pr->{'owner'}) {
4573 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
4575 if ($check_forks) {
4576 my $pname = $pr->{'path'};
4577 if (($pname =~ s/\.git$//) &&
4578 ($pname !~ /\/$/) &&
4579 (-d "$projectroot/$pname")) {
4580 $pr->{'forks'} = "-d $projectroot/$pname";
4581 } else {
4582 $pr->{'forks'} = 0;
4585 $show_ctags and $pr->{'ctags'} = git_get_project_ctags($pr->{'path'});
4586 push @projects, $pr;
4589 return @projects;
4592 # print 'sort by' <th> element, generating 'sort by $name' replay link
4593 # if that order is not selected
4594 sub print_sort_th {
4595 print format_sort_th(@_);
4598 sub format_sort_th {
4599 my ($name, $order, $header) = @_;
4600 my $sort_th = "";
4601 $header ||= ucfirst($name);
4603 if ($order eq $name) {
4604 $sort_th .= "<th>$header</th>\n";
4605 } else {
4606 $sort_th .= "<th>" .
4607 $cgi->a({-href => href(-replay=>1, order=>$name),
4608 -class => "header"}, $header) .
4609 "</th>\n";
4612 return $sort_th;
4615 sub git_project_list_body {
4616 # actually uses global variable $project
4617 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
4619 my $check_forks = gitweb_check_feature('forks');
4620 my @projects = fill_project_list_info($projlist, $check_forks);
4622 $order ||= $default_projects_order;
4623 $from = 0 unless defined $from;
4624 $to = $#projects if (!defined $to || $#projects < $to);
4626 my %order_info = (
4627 project => { key => 'path', type => 'str' },
4628 descr => { key => 'descr_long', type => 'str' },
4629 owner => { key => 'owner', type => 'str' },
4630 age => { key => 'age', type => 'num' }
4632 my $oi = $order_info{$order};
4633 if ($oi->{'type'} eq 'str') {
4634 @projects = sort {$a->{$oi->{'key'}} cmp $b->{$oi->{'key'}}} @projects;
4635 } else {
4636 @projects = sort {$a->{$oi->{'key'}} <=> $b->{$oi->{'key'}}} @projects;
4639 my $show_ctags = gitweb_check_feature('ctags');
4640 if ($show_ctags) {
4641 my %ctags;
4642 foreach my $p (@projects) {
4643 foreach my $ct (keys %{$p->{'ctags'}}) {
4644 $ctags{$ct} += $p->{'ctags'}->{$ct};
4647 my $cloud = git_populate_project_tagcloud(\%ctags);
4648 print git_show_project_tagcloud($cloud, 64);
4651 print "<table class=\"project_list\">\n";
4652 unless ($no_header) {
4653 print "<tr>\n";
4654 if ($check_forks) {
4655 print "<th></th>\n";
4657 print_sort_th('project', $order, 'Project');
4658 print_sort_th('descr', $order, 'Description');
4659 print_sort_th('owner', $order, 'Owner');
4660 print_sort_th('age', $order, 'Last Change');
4661 print "<th></th>\n" . # for links
4662 "</tr>\n";
4664 my $alternate = 1;
4665 my $tagfilter = $cgi->param('by_tag');
4666 for (my $i = $from; $i <= $to; $i++) {
4667 my $pr = $projects[$i];
4669 next if $tagfilter and $show_ctags and not grep { lc $_ eq lc $tagfilter } keys %{$pr->{'ctags'}};
4670 next if $searchtext and not $pr->{'path'} =~ /$searchtext/
4671 and not $pr->{'descr_long'} =~ /$searchtext/;
4672 # Weed out forks or non-matching entries of search
4673 if ($check_forks) {
4674 my $forkbase = $project; $forkbase ||= ''; $forkbase =~ s#\.git$#/#;
4675 $forkbase="^$forkbase" if $forkbase;
4676 next if not $searchtext and not $tagfilter and $show_ctags
4677 and $pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe
4680 if ($alternate) {
4681 print "<tr class=\"dark\">\n";
4682 } else {
4683 print "<tr class=\"light\">\n";
4685 $alternate ^= 1;
4686 if ($check_forks) {
4687 print "<td>";
4688 if ($pr->{'forks'}) {
4689 print "<!-- $pr->{'forks'} -->\n";
4690 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
4692 print "</td>\n";
4694 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
4695 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
4696 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
4697 -class => "list", -title => $pr->{'descr_long'}},
4698 esc_html($pr->{'descr'})) . "</td>\n" .
4699 "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n";
4700 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
4701 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
4702 "<td class=\"link\">" .
4703 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
4704 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
4705 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
4706 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
4707 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
4708 "</td>\n" .
4709 "</tr>\n";
4711 if (defined $extra) {
4712 print "<tr>\n";
4713 if ($check_forks) {
4714 print "<td></td>\n";
4716 print "<td colspan=\"5\">$extra</td>\n" .
4717 "</tr>\n";
4719 print "</table>\n";
4722 sub git_log_body {
4723 # uses global variable $project
4724 my ($commitlist, $from, $to, $refs, $extra) = @_;
4726 $from = 0 unless defined $from;
4727 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
4729 for (my $i = 0; $i <= $to; $i++) {
4730 my %co = %{$commitlist->[$i]};
4731 next if !%co;
4732 my $commit = $co{'id'};
4733 my $ref = format_ref_marker($refs, $commit);
4734 my %ad = parse_date($co{'author_epoch'});
4735 git_print_header_div('commit',
4736 "<span class=\"age\">$co{'age_string'}</span>" .
4737 esc_html($co{'title'}) . $ref,
4738 $commit);
4739 print "<div class=\"title_text\">\n" .
4740 "<div class=\"log_link\">\n" .
4741 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
4742 " | " .
4743 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
4744 " | " .
4745 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
4746 "<br/>\n" .
4747 "</div>\n";
4748 git_print_authorship(\%co, -tag => 'span');
4749 print "<br/>\n</div>\n";
4751 print "<div class=\"log_body\">\n";
4752 git_print_log($co{'comment'}, -final_empty_line=> 1);
4753 print "</div>\n";
4755 if ($extra) {
4756 print "<div class=\"page_nav\">\n";
4757 print "$extra\n";
4758 print "</div>\n";
4762 sub git_shortlog_body {
4763 # uses global variable $project
4764 my ($commitlist, $from, $to, $refs, $extra) = @_;
4766 $from = 0 unless defined $from;
4767 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
4769 print "<table class=\"shortlog\">\n";
4770 my $alternate = 1;
4771 for (my $i = $from; $i <= $to; $i++) {
4772 my %co = %{$commitlist->[$i]};
4773 my $commit = $co{'id'};
4774 my $ref = format_ref_marker($refs, $commit);
4775 if ($alternate) {
4776 print "<tr class=\"dark\">\n";
4777 } else {
4778 print "<tr class=\"light\">\n";
4780 $alternate ^= 1;
4781 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
4782 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4783 format_author_html('td', \%co, 10) . "<td>";
4784 print format_subject_html($co{'title'}, $co{'title_short'},
4785 href(action=>"commit", hash=>$commit), $ref);
4786 print "</td>\n" .
4787 "<td class=\"link\">" .
4788 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
4789 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
4790 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
4791 my $snapshot_links = format_snapshot_links($commit);
4792 if (defined $snapshot_links) {
4793 print " | " . $snapshot_links;
4795 print "</td>\n" .
4796 "</tr>\n";
4798 if (defined $extra) {
4799 print "<tr>\n" .
4800 "<td colspan=\"4\">$extra</td>\n" .
4801 "</tr>\n";
4803 print "</table>\n";
4806 sub git_history_body {
4807 # Warning: assumes constant type (blob or tree) during history
4808 my ($commitlist, $from, $to, $refs, $extra,
4809 $file_name, $file_hash, $ftype) = @_;
4811 $from = 0 unless defined $from;
4812 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
4814 print "<table class=\"history\">\n";
4815 my $alternate = 1;
4816 for (my $i = $from; $i <= $to; $i++) {
4817 my %co = %{$commitlist->[$i]};
4818 if (!%co) {
4819 next;
4821 my $commit = $co{'id'};
4823 my $ref = format_ref_marker($refs, $commit);
4825 if ($alternate) {
4826 print "<tr class=\"dark\">\n";
4827 } else {
4828 print "<tr class=\"light\">\n";
4830 $alternate ^= 1;
4831 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4832 # shortlog: format_author_html('td', \%co, 10)
4833 format_author_html('td', \%co, 15, 3) . "<td>";
4834 # originally git_history used chop_str($co{'title'}, 50)
4835 print format_subject_html($co{'title'}, $co{'title_short'},
4836 href(action=>"commit", hash=>$commit), $ref);
4837 print "</td>\n" .
4838 "<td class=\"link\">" .
4839 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
4840 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
4842 if ($ftype eq 'blob') {
4843 my $blob_current = $file_hash;
4844 my $blob_parent = git_get_hash_by_path($commit, $file_name);
4845 if (defined $blob_current && defined $blob_parent &&
4846 $blob_current ne $blob_parent) {
4847 print " | " .
4848 $cgi->a({-href => href(action=>"blobdiff",
4849 hash=>$blob_current, hash_parent=>$blob_parent,
4850 hash_base=>$hash_base, hash_parent_base=>$commit,
4851 file_name=>$file_name)},
4852 "diff to current");
4855 print "</td>\n" .
4856 "</tr>\n";
4858 if (defined $extra) {
4859 print "<tr>\n" .
4860 "<td colspan=\"4\">$extra</td>\n" .
4861 "</tr>\n";
4863 print "</table>\n";
4866 sub git_tags_body {
4867 # uses global variable $project
4868 my ($taglist, $from, $to, $extra) = @_;
4869 $from = 0 unless defined $from;
4870 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
4872 print "<table class=\"tags\">\n";
4873 my $alternate = 1;
4874 for (my $i = $from; $i <= $to; $i++) {
4875 my $entry = $taglist->[$i];
4876 my %tag = %$entry;
4877 my $comment = $tag{'subject'};
4878 my $comment_short;
4879 if (defined $comment) {
4880 $comment_short = chop_str($comment, 30, 5);
4882 if ($alternate) {
4883 print "<tr class=\"dark\">\n";
4884 } else {
4885 print "<tr class=\"light\">\n";
4887 $alternate ^= 1;
4888 if (defined $tag{'age'}) {
4889 print "<td><i>$tag{'age'}</i></td>\n";
4890 } else {
4891 print "<td></td>\n";
4893 print "<td>" .
4894 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
4895 -class => "list name"}, esc_html($tag{'name'})) .
4896 "</td>\n" .
4897 "<td>";
4898 if (defined $comment) {
4899 print format_subject_html($comment, $comment_short,
4900 href(action=>"tag", hash=>$tag{'id'}));
4902 print "</td>\n" .
4903 "<td class=\"selflink\">";
4904 if ($tag{'type'} eq "tag") {
4905 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
4906 } else {
4907 print "&nbsp;";
4909 print "</td>\n" .
4910 "<td class=\"link\">" . " | " .
4911 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
4912 if ($tag{'reftype'} eq "commit") {
4913 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})}, "shortlog") .
4914 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})}, "log");
4915 } elsif ($tag{'reftype'} eq "blob") {
4916 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
4918 print "</td>\n" .
4919 "</tr>";
4921 if (defined $extra) {
4922 print "<tr>\n" .
4923 "<td colspan=\"5\">$extra</td>\n" .
4924 "</tr>\n";
4926 print "</table>\n";
4929 sub git_heads_body {
4930 # uses global variable $project
4931 my ($headlist, $head, $from, $to, $extra) = @_;
4932 $from = 0 unless defined $from;
4933 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
4935 print "<table class=\"heads\">\n";
4936 my $alternate = 1;
4937 for (my $i = $from; $i <= $to; $i++) {
4938 my $entry = $headlist->[$i];
4939 my %ref = %$entry;
4940 my $curr = $ref{'id'} eq $head;
4941 if ($alternate) {
4942 print "<tr class=\"dark\">\n";
4943 } else {
4944 print "<tr class=\"light\">\n";
4946 $alternate ^= 1;
4947 print "<td><i>$ref{'age'}</i></td>\n" .
4948 ($curr ? "<td class=\"current_head\">" : "<td>") .
4949 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),
4950 -class => "list name"},esc_html($ref{'name'})) .
4951 "</td>\n" .
4952 "<td class=\"link\">" .
4953 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})}, "shortlog") . " | " .
4954 $cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})}, "log") . " | " .
4955 $cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})}, "tree") .
4956 "</td>\n" .
4957 "</tr>";
4959 if (defined $extra) {
4960 print "<tr>\n" .
4961 "<td colspan=\"3\">$extra</td>\n" .
4962 "</tr>\n";
4964 print "</table>\n";
4967 sub git_search_grep_body {
4968 my ($commitlist, $from, $to, $extra) = @_;
4969 $from = 0 unless defined $from;
4970 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
4972 print "<table class=\"commit_search\">\n";
4973 my $alternate = 1;
4974 for (my $i = $from; $i <= $to; $i++) {
4975 my %co = %{$commitlist->[$i]};
4976 if (!%co) {
4977 next;
4979 my $commit = $co{'id'};
4980 if ($alternate) {
4981 print "<tr class=\"dark\">\n";
4982 } else {
4983 print "<tr class=\"light\">\n";
4985 $alternate ^= 1;
4986 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4987 format_author_html('td', \%co, 15, 5) .
4988 "<td>" .
4989 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
4990 -class => "list subject"},
4991 chop_and_escape_str($co{'title'}, 50) . "<br/>");
4992 my $comment = $co{'comment'};
4993 foreach my $line (@$comment) {
4994 if ($line =~ m/^(.*?)($search_regexp)(.*)$/i) {
4995 my ($lead, $match, $trail) = ($1, $2, $3);
4996 $match = chop_str($match, 70, 5, 'center');
4997 my $contextlen = int((80 - length($match))/2);
4998 $contextlen = 30 if ($contextlen > 30);
4999 $lead = chop_str($lead, $contextlen, 10, 'left');
5000 $trail = chop_str($trail, $contextlen, 10, 'right');
5002 $lead = esc_html($lead);
5003 $match = esc_html($match);
5004 $trail = esc_html($trail);
5006 print "$lead<span class=\"match\">$match</span>$trail<br />";
5009 print "</td>\n" .
5010 "<td class=\"link\">" .
5011 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5012 " | " .
5013 $cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})}, "commitdiff") .
5014 " | " .
5015 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5016 print "</td>\n" .
5017 "</tr>\n";
5019 if (defined $extra) {
5020 print "<tr>\n" .
5021 "<td colspan=\"3\">$extra</td>\n" .
5022 "</tr>\n";
5024 print "</table>\n";
5027 ## ======================================================================
5028 ## ======================================================================
5029 ## actions
5031 sub git_project_list {
5032 my $order = $input_params{'order'};
5033 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
5034 die_error(400, "Unknown order parameter");
5037 my @list = git_get_projects_list();
5038 if (!@list) {
5039 die_error(404, "No projects found");
5042 git_header_html();
5043 if (defined $home_text && -f $home_text) {
5044 print "<div class=\"index_include\">\n";
5045 insert_file($home_text);
5046 print "</div>\n";
5048 print $cgi->startform(-method => "get") .
5049 "<p class=\"projsearch\">Search:\n" .
5050 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
5051 "</p>" .
5052 $cgi->end_form() . "\n";
5053 git_project_list_body(\@list, $order);
5054 git_footer_html();
5057 sub git_forks {
5058 my $order = $input_params{'order'};
5059 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
5060 die_error(400, "Unknown order parameter");
5063 my @list = git_get_projects_list($project);
5064 if (!@list) {
5065 die_error(404, "No forks found");
5068 git_header_html();
5069 git_print_page_nav('','');
5070 git_print_header_div('summary', "$project forks");
5071 git_project_list_body(\@list, $order);
5072 git_footer_html();
5075 sub git_project_index {
5076 my @projects = git_get_projects_list($project);
5078 print $cgi->header(
5079 -type => 'text/plain',
5080 -charset => 'utf-8',
5081 -content_disposition => 'inline; filename="index.aux"');
5083 foreach my $pr (@projects) {
5084 if (!exists $pr->{'owner'}) {
5085 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
5088 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
5089 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
5090 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
5091 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
5092 $path =~ s/ /\+/g;
5093 $owner =~ s/ /\+/g;
5095 print "$path $owner\n";
5099 sub git_summary {
5100 my $descr = git_get_project_description($project) || "none";
5101 my %co = parse_commit("HEAD");
5102 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
5103 my $head = $co{'id'};
5105 my $owner = git_get_project_owner($project);
5107 my $refs = git_get_references();
5108 # These get_*_list functions return one more to allow us to see if
5109 # there are more ...
5110 my @taglist = git_get_tags_list(16);
5111 my @headlist = git_get_heads_list(16);
5112 my @forklist;
5113 my $check_forks = gitweb_check_feature('forks');
5115 if ($check_forks) {
5116 @forklist = git_get_projects_list($project);
5119 git_header_html();
5120 git_print_page_nav('summary','', $head);
5122 print "<div class=\"title\">&nbsp;</div>\n";
5123 print "<table class=\"projects_list\">\n" .
5124 "<tr id=\"metadata_desc\"><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
5125 "<tr id=\"metadata_owner\"><td>owner</td><td>" . esc_html($owner) . "</td></tr>\n";
5126 if (defined $cd{'rfc2822'}) {
5127 print "<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
5130 # use per project git URL list in $projectroot/$project/cloneurl
5131 # or make project git URL from git base URL and project name
5132 my $url_tag = "URL";
5133 my @url_list = git_get_project_url_list($project);
5134 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
5135 foreach my $git_url (@url_list) {
5136 next unless $git_url;
5137 print "<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";
5138 $url_tag = "";
5141 # Tag cloud
5142 my $show_ctags = gitweb_check_feature('ctags');
5143 if ($show_ctags) {
5144 my $ctags = git_get_project_ctags($project);
5145 my $cloud = git_populate_project_tagcloud($ctags);
5146 print "<tr id=\"metadata_ctags\"><td>Content tags:<br />";
5147 print "</td>\n<td>" unless %$ctags;
5148 print "<form action=\"$show_ctags\" method=\"post\"><input type=\"hidden\" name=\"p\" value=\"$project\" />Add: <input type=\"text\" name=\"t\" size=\"8\" /></form>";
5149 print "</td>\n<td>" if %$ctags;
5150 print git_show_project_tagcloud($cloud, 48);
5151 print "</td></tr>";
5154 print "</table>\n";
5156 # If XSS prevention is on, we don't include README.html.
5157 # TODO: Allow a readme in some safe format.
5158 if (!$prevent_xss && -s "$projectroot/$project/README.html") {
5159 print "<div class=\"title\">readme</div>\n" .
5160 "<div class=\"readme\">\n";
5161 insert_file("$projectroot/$project/README.html");
5162 print "\n</div>\n"; # class="readme"
5165 # we need to request one more than 16 (0..15) to check if
5166 # those 16 are all
5167 my @commitlist = $head ? parse_commits($head, 17) : ();
5168 if (@commitlist) {
5169 git_print_header_div('shortlog');
5170 git_shortlog_body(\@commitlist, 0, 15, $refs,
5171 $#commitlist <= 15 ? undef :
5172 $cgi->a({-href => href(action=>"shortlog")}, "..."));
5175 if (@taglist) {
5176 git_print_header_div('tags');
5177 git_tags_body(\@taglist, 0, 15,
5178 $#taglist <= 15 ? undef :
5179 $cgi->a({-href => href(action=>"tags")}, "..."));
5182 if (@headlist) {
5183 git_print_header_div('heads');
5184 git_heads_body(\@headlist, $head, 0, 15,
5185 $#headlist <= 15 ? undef :
5186 $cgi->a({-href => href(action=>"heads")}, "..."));
5189 if (@forklist) {
5190 git_print_header_div('forks');
5191 git_project_list_body(\@forklist, 'age', 0, 15,
5192 $#forklist <= 15 ? undef :
5193 $cgi->a({-href => href(action=>"forks")}, "..."),
5194 'no_header');
5197 git_footer_html();
5200 sub git_tag {
5201 my %tag = parse_tag($hash);
5203 if (! %tag) {
5204 die_error(404, "Unknown tag object");
5207 my $head = git_get_head_hash($project);
5208 git_header_html();
5209 git_print_page_nav('','', $head,undef,$head);
5210 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
5211 print "<div class=\"title_text\">\n" .
5212 "<table class=\"object_header\">\n" .
5213 "<tr>\n" .
5214 "<td>object</td>\n" .
5215 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
5216 $tag{'object'}) . "</td>\n" .
5217 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
5218 $tag{'type'}) . "</td>\n" .
5219 "</tr>\n";
5220 if (defined($tag{'author'})) {
5221 git_print_authorship_rows(\%tag, 'author');
5223 print "</table>\n\n" .
5224 "</div>\n";
5225 print "<div class=\"page_body\">";
5226 my $comment = $tag{'comment'};
5227 foreach my $line (@$comment) {
5228 chomp $line;
5229 print esc_html($line, -nbsp=>1) . "<br/>\n";
5231 print "</div>\n";
5232 git_footer_html();
5235 sub git_blame_common {
5236 my $format = shift || 'porcelain';
5237 if ($format eq 'porcelain' && $cgi->param('js')) {
5238 $format = 'incremental';
5239 $action = 'blame_incremental'; # for page title etc
5242 # permissions
5243 gitweb_check_feature('blame')
5244 or die_error(403, "Blame view not allowed");
5246 # error checking
5247 die_error(400, "No file name given") unless $file_name;
5248 $hash_base ||= git_get_head_hash($project);
5249 die_error(404, "Couldn't find base commit") unless $hash_base;
5250 my %co = parse_commit($hash_base)
5251 or die_error(404, "Commit not found");
5252 my $ftype = "blob";
5253 if (!defined $hash) {
5254 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
5255 or die_error(404, "Error looking up file");
5256 } else {
5257 $ftype = git_get_type($hash);
5258 if ($ftype !~ "blob") {
5259 die_error(400, "Object is not a blob");
5263 my $fd;
5264 if ($format eq 'incremental') {
5265 # get file contents (as base)
5266 open $fd, "-|", git_cmd(), 'cat-file', 'blob', $hash
5267 or die_error(500, "Open git-cat-file failed");
5268 } elsif ($format eq 'data') {
5269 # run git-blame --incremental
5270 open $fd, "-|", git_cmd(), "blame", "--incremental",
5271 $hash_base, "--", $file_name
5272 or die_error(500, "Open git-blame --incremental failed");
5273 } else {
5274 # run git-blame --porcelain
5275 open $fd, "-|", git_cmd(), "blame", '-p',
5276 $hash_base, '--', $file_name
5277 or die_error(500, "Open git-blame --porcelain failed");
5280 # incremental blame data returns early
5281 if ($format eq 'data') {
5282 print $cgi->header(
5283 -type=>"text/plain", -charset => "utf-8",
5284 -status=> "200 OK");
5285 local $| = 1; # output autoflush
5286 print while <$fd>;
5287 close $fd
5288 or print "ERROR $!\n";
5290 print 'END';
5291 if (defined $t0 && gitweb_check_feature('timed')) {
5292 print ' '.
5293 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).
5294 ' '.$number_of_git_cmds;
5296 print "\n";
5298 return;
5301 # page header
5302 git_header_html();
5303 my $formats_nav =
5304 $cgi->a({-href => href(action=>"blob", -replay=>1)},
5305 "blob") .
5306 " | ";
5307 if ($format eq 'incremental') {
5308 $formats_nav .=
5309 $cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},
5310 "blame") . " (non-incremental)";
5311 } else {
5312 $formats_nav .=
5313 $cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},
5314 "blame") . " (incremental)";
5316 $formats_nav .=
5317 " | " .
5318 $cgi->a({-href => href(action=>"history", -replay=>1)},
5319 "history") .
5320 " | " .
5321 $cgi->a({-href => href(action=>$action, file_name=>$file_name)},
5322 "HEAD");
5323 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
5324 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5325 git_print_page_path($file_name, $ftype, $hash_base);
5327 # page body
5328 if ($format eq 'incremental') {
5329 print "<noscript>\n<div class=\"error\"><center><b>\n".
5330 "This page requires JavaScript to run.\n Use ".
5331 $cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},
5332 'this page').
5333 " instead.\n".
5334 "</b></center></div>\n</noscript>\n";
5336 print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;
5339 print qq!<div class="page_body">\n!;
5340 print qq!<div id="progress_info">... / ...</div>\n!
5341 if ($format eq 'incremental');
5342 print qq!<table id="blame_table" class="blame" width="100%">\n!.
5343 #qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.
5344 qq!<thead>\n!.
5345 qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.
5346 qq!</thead>\n!.
5347 qq!<tbody>\n!;
5349 my @rev_color = qw(light dark);
5350 my $num_colors = scalar(@rev_color);
5351 my $current_color = 0;
5353 if ($format eq 'incremental') {
5354 my $color_class = $rev_color[$current_color];
5356 #contents of a file
5357 my $linenr = 0;
5358 LINE:
5359 while (my $line = <$fd>) {
5360 chomp $line;
5361 $linenr++;
5363 print qq!<tr id="l$linenr" class="$color_class">!.
5364 qq!<td class="sha1"><a href=""> </a></td>!.
5365 qq!<td class="linenr">!.
5366 qq!<a class="linenr" href="">$linenr</a></td>!;
5367 print qq!<td class="pre">! . esc_html($line) . "</td>\n";
5368 print qq!</tr>\n!;
5371 } else { # porcelain, i.e. ordinary blame
5372 my %metainfo = (); # saves information about commits
5374 # blame data
5375 LINE:
5376 while (my $line = <$fd>) {
5377 chomp $line;
5378 # the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]
5379 # no <lines in group> for subsequent lines in group of lines
5380 my ($full_rev, $orig_lineno, $lineno, $group_size) =
5381 ($line =~ /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);
5382 if (!exists $metainfo{$full_rev}) {
5383 $metainfo{$full_rev} = { 'nprevious' => 0 };
5385 my $meta = $metainfo{$full_rev};
5386 my $data;
5387 while ($data = <$fd>) {
5388 chomp $data;
5389 last if ($data =~ s/^\t//); # contents of line
5390 if ($data =~ /^(\S+)(?: (.*))?$/) {
5391 $meta->{$1} = $2 unless exists $meta->{$1};
5393 if ($data =~ /^previous /) {
5394 $meta->{'nprevious'}++;
5397 my $short_rev = substr($full_rev, 0, 8);
5398 my $author = $meta->{'author'};
5399 my %date =
5400 parse_date($meta->{'author-time'}, $meta->{'author-tz'});
5401 my $date = $date{'iso-tz'};
5402 if ($group_size) {
5403 $current_color = ($current_color + 1) % $num_colors;
5405 my $tr_class = $rev_color[$current_color];
5406 $tr_class .= ' boundary' if (exists $meta->{'boundary'});
5407 $tr_class .= ' no-previous' if ($meta->{'nprevious'} == 0);
5408 $tr_class .= ' multiple-previous' if ($meta->{'nprevious'} > 1);
5409 print "<tr id=\"l$lineno\" class=\"$tr_class\">\n";
5410 if ($group_size) {
5411 print "<td class=\"sha1\"";
5412 print " title=\"". esc_html($author) . ", $date\"";
5413 print " rowspan=\"$group_size\"" if ($group_size > 1);
5414 print ">";
5415 print $cgi->a({-href => href(action=>"commit",
5416 hash=>$full_rev,
5417 file_name=>$file_name)},
5418 esc_html($short_rev));
5419 if ($group_size >= 2) {
5420 my @author_initials = ($author =~ /\b([[:upper:]])\B/g);
5421 if (@author_initials) {
5422 print "<br />" .
5423 esc_html(join('', @author_initials));
5424 # or join('.', ...)
5427 print "</td>\n";
5429 # 'previous' <sha1 of parent commit> <filename at commit>
5430 if (exists $meta->{'previous'} &&
5431 $meta->{'previous'} =~ /^([a-fA-F0-9]{40}) (.*)$/) {
5432 $meta->{'parent'} = $1;
5433 $meta->{'file_parent'} = unquote($2);
5435 my $linenr_commit =
5436 exists($meta->{'parent'}) ?
5437 $meta->{'parent'} : $full_rev;
5438 my $linenr_filename =
5439 exists($meta->{'file_parent'}) ?
5440 $meta->{'file_parent'} : unquote($meta->{'filename'});
5441 my $blamed = href(action => 'blame',
5442 file_name => $linenr_filename,
5443 hash_base => $linenr_commit);
5444 print "<td class=\"linenr\">";
5445 print $cgi->a({ -href => "$blamed#l$orig_lineno",
5446 -class => "linenr" },
5447 esc_html($lineno));
5448 print "</td>";
5449 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
5450 print "</tr>\n";
5451 } # end while
5455 # footer
5456 print "</tbody>\n".
5457 "</table>\n"; # class="blame"
5458 print "</div>\n"; # class="blame_body"
5459 close $fd
5460 or print "Reading blob failed\n";
5462 git_footer_html();
5465 sub git_blame {
5466 git_blame_common();
5469 sub git_blame_incremental {
5470 git_blame_common('incremental');
5473 sub git_blame_data {
5474 git_blame_common('data');
5477 sub git_tags {
5478 my $head = git_get_head_hash($project);
5479 git_header_html();
5480 git_print_page_nav('','', $head,undef,$head);
5481 git_print_header_div('summary', $project);
5483 my @tagslist = git_get_tags_list();
5484 if (@tagslist) {
5485 git_tags_body(\@tagslist);
5487 git_footer_html();
5490 sub git_heads {
5491 my $head = git_get_head_hash($project);
5492 git_header_html();
5493 git_print_page_nav('','', $head,undef,$head);
5494 git_print_header_div('summary', $project);
5496 my @headslist = git_get_heads_list();
5497 if (@headslist) {
5498 git_heads_body(\@headslist, $head);
5500 git_footer_html();
5503 sub git_blob_plain {
5504 my $type = shift;
5505 my $expires;
5507 if (!defined $hash) {
5508 if (defined $file_name) {
5509 my $base = $hash_base || git_get_head_hash($project);
5510 $hash = git_get_hash_by_path($base, $file_name, "blob")
5511 or die_error(404, "Cannot find file");
5512 } else {
5513 die_error(400, "No file name defined");
5515 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5516 # blobs defined by non-textual hash id's can be cached
5517 $expires = "+1d";
5520 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
5521 or die_error(500, "Open git-cat-file blob '$hash' failed");
5523 # content-type (can include charset)
5524 $type = blob_contenttype($fd, $file_name, $type);
5526 # "save as" filename, even when no $file_name is given
5527 my $save_as = "$hash";
5528 if (defined $file_name) {
5529 $save_as = $file_name;
5530 } elsif ($type =~ m/^text\//) {
5531 $save_as .= '.txt';
5534 # With XSS prevention on, blobs of all types except a few known safe
5535 # ones are served with "Content-Disposition: attachment" to make sure
5536 # they don't run in our security domain. For certain image types,
5537 # blob view writes an <img> tag referring to blob_plain view, and we
5538 # want to be sure not to break that by serving the image as an
5539 # attachment (though Firefox 3 doesn't seem to care).
5540 my $sandbox = $prevent_xss &&
5541 $type !~ m!^(?:text/plain|image/(?:gif|png|jpeg))$!;
5543 print $cgi->header(
5544 -type => $type,
5545 -expires => $expires,
5546 -content_disposition =>
5547 ($sandbox ? 'attachment' : 'inline')
5548 . '; filename="' . $save_as . '"');
5549 local $/ = undef;
5550 binmode STDOUT, ':raw';
5551 print <$fd>;
5552 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
5553 close $fd;
5556 sub git_blob {
5557 my $expires;
5559 if (!defined $hash) {
5560 if (defined $file_name) {
5561 my $base = $hash_base || git_get_head_hash($project);
5562 $hash = git_get_hash_by_path($base, $file_name, "blob")
5563 or die_error(404, "Cannot find file");
5564 } else {
5565 die_error(400, "No file name defined");
5567 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5568 # blobs defined by non-textual hash id's can be cached
5569 $expires = "+1d";
5572 my $have_blame = gitweb_check_feature('blame');
5573 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
5574 or die_error(500, "Couldn't cat $file_name, $hash");
5575 my $mimetype = blob_mimetype($fd, $file_name);
5576 # use 'blob_plain' (aka 'raw') view for files that cannot be displayed
5577 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {
5578 close $fd;
5579 return git_blob_plain($mimetype);
5581 # we can have blame only for text/* mimetype
5582 $have_blame &&= ($mimetype =~ m!^text/!);
5584 my $highlight = gitweb_check_feature('highlight');
5585 my $syntax = guess_file_syntax($highlight, $mimetype, $file_name);
5586 $fd = run_highlighter($fd, $highlight, $syntax)
5587 if $syntax;
5589 git_header_html(undef, $expires);
5590 my $formats_nav = '';
5591 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
5592 if (defined $file_name) {
5593 if ($have_blame) {
5594 $formats_nav .=
5595 $cgi->a({-href => href(action=>"blame", -replay=>1)},
5596 "blame") .
5597 " | ";
5599 $formats_nav .=
5600 $cgi->a({-href => href(action=>"history", -replay=>1)},
5601 "history") .
5602 " | " .
5603 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
5604 "raw") .
5605 " | " .
5606 $cgi->a({-href => href(action=>"blob",
5607 hash_base=>"HEAD", file_name=>$file_name)},
5608 "HEAD");
5609 } else {
5610 $formats_nav .=
5611 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
5612 "raw");
5614 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
5615 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5616 } else {
5617 print "<div class=\"page_nav\">\n" .
5618 "<br/><br/></div>\n" .
5619 "<div class=\"title\">$hash</div>\n";
5621 git_print_page_path($file_name, "blob", $hash_base);
5622 print "<div class=\"page_body\">\n";
5623 if ($mimetype =~ m!^image/!) {
5624 print qq!<img type="$mimetype"!;
5625 if ($file_name) {
5626 print qq! alt="$file_name" title="$file_name"!;
5628 print qq! src="! .
5629 href(action=>"blob_plain", hash=>$hash,
5630 hash_base=>$hash_base, file_name=>$file_name) .
5631 qq!" />\n!;
5632 } else {
5633 my $nr;
5634 while (my $line = <$fd>) {
5635 chomp $line;
5636 $nr++;
5637 $line = untabify($line);
5638 printf qq!<div class="pre"><a id="l%i" href="%s#l%i" class="linenr">%4i</a> %s</div>\n!,
5639 $nr, href(-replay => 1), $nr, $nr, $syntax ? $line : esc_html($line, -nbsp=>1);
5642 close $fd
5643 or print "Reading blob failed.\n";
5644 print "</div>";
5645 git_footer_html();
5648 sub git_tree {
5649 if (!defined $hash_base) {
5650 $hash_base = "HEAD";
5652 if (!defined $hash) {
5653 if (defined $file_name) {
5654 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
5655 } else {
5656 $hash = $hash_base;
5659 die_error(404, "No such tree") unless defined($hash);
5661 my $show_sizes = gitweb_check_feature('show-sizes');
5662 my $have_blame = gitweb_check_feature('blame');
5664 my @entries = ();
5666 local $/ = "\0";
5667 open my $fd, "-|", git_cmd(), "ls-tree", '-z',
5668 ($show_sizes ? '-l' : ()), @extra_options, $hash
5669 or die_error(500, "Open git-ls-tree failed");
5670 @entries = map { chomp; $_ } <$fd>;
5671 close $fd
5672 or die_error(404, "Reading tree failed");
5675 my $refs = git_get_references();
5676 my $ref = format_ref_marker($refs, $hash_base);
5677 git_header_html();
5678 my $basedir = '';
5679 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
5680 my @views_nav = ();
5681 if (defined $file_name) {
5682 push @views_nav,
5683 $cgi->a({-href => href(action=>"history", -replay=>1)},
5684 "history"),
5685 $cgi->a({-href => href(action=>"tree",
5686 hash_base=>"HEAD", file_name=>$file_name)},
5687 "HEAD"),
5689 my $snapshot_links = format_snapshot_links($hash);
5690 if (defined $snapshot_links) {
5691 # FIXME: Should be available when we have no hash base as well.
5692 push @views_nav, $snapshot_links;
5694 git_print_page_nav('tree','', $hash_base, undef, undef,
5695 join(' | ', @views_nav));
5696 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
5697 } else {
5698 undef $hash_base;
5699 print "<div class=\"page_nav\">\n";
5700 print "<br/><br/></div>\n";
5701 print "<div class=\"title\">$hash</div>\n";
5703 if (defined $file_name) {
5704 $basedir = $file_name;
5705 if ($basedir ne '' && substr($basedir, -1) ne '/') {
5706 $basedir .= '/';
5708 git_print_page_path($file_name, 'tree', $hash_base);
5710 print "<div class=\"page_body\">\n";
5711 print "<table class=\"tree\">\n";
5712 my $alternate = 1;
5713 # '..' (top directory) link if possible
5714 if (defined $hash_base &&
5715 defined $file_name && $file_name =~ m![^/]+$!) {
5716 if ($alternate) {
5717 print "<tr class=\"dark\">\n";
5718 } else {
5719 print "<tr class=\"light\">\n";
5721 $alternate ^= 1;
5723 my $up = $file_name;
5724 $up =~ s!/?[^/]+$!!;
5725 undef $up unless $up;
5726 # based on git_print_tree_entry
5727 print '<td class="mode">' . mode_str('040000') . "</td>\n";
5728 print '<td class="size">&nbsp;</td>'."\n" if $show_sizes;
5729 print '<td class="list">';
5730 print $cgi->a({-href => href(action=>"tree",
5731 hash_base=>$hash_base,
5732 file_name=>$up)},
5733 "..");
5734 print "</td>\n";
5735 print "<td class=\"link\"></td>\n";
5737 print "</tr>\n";
5739 foreach my $line (@entries) {
5740 my %t = parse_ls_tree_line($line, -z => 1, -l => $show_sizes);
5742 if ($alternate) {
5743 print "<tr class=\"dark\">\n";
5744 } else {
5745 print "<tr class=\"light\">\n";
5747 $alternate ^= 1;
5749 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
5751 print "</tr>\n";
5753 print "</table>\n" .
5754 "</div>";
5755 git_footer_html();
5758 sub snapshot_name {
5759 my ($project, $hash) = @_;
5761 # path/to/project.git -> project
5762 # path/to/project/.git -> project
5763 my $name = to_utf8($project);
5764 $name =~ s,([^/])/*\.git$,$1,;
5765 $name = basename($name);
5766 # sanitize name
5767 $name =~ s/[[:cntrl:]]/?/g;
5769 my $ver = $hash;
5770 if ($hash =~ /^[0-9a-fA-F]+$/) {
5771 # shorten SHA-1 hash
5772 my $full_hash = git_get_full_hash($project, $hash);
5773 if ($full_hash =~ /^$hash/ && length($hash) > 7) {
5774 $ver = git_get_short_hash($project, $hash);
5776 } elsif ($hash =~ m!^refs/tags/(.*)$!) {
5777 # tags don't need shortened SHA-1 hash
5778 $ver = $1;
5779 } else {
5780 # branches and other need shortened SHA-1 hash
5781 if ($hash =~ m!^refs/(?:heads|remotes)/(.*)$!) {
5782 $ver = $1;
5784 $ver .= '-' . git_get_short_hash($project, $hash);
5786 # in case of hierarchical branch names
5787 $ver =~ s!/!.!g;
5789 # name = project-version_string
5790 $name = "$name-$ver";
5792 return wantarray ? ($name, $name) : $name;
5795 sub git_snapshot {
5796 my $format = $input_params{'snapshot_format'};
5797 if (!@snapshot_fmts) {
5798 die_error(403, "Snapshots not allowed");
5800 # default to first supported snapshot format
5801 $format ||= $snapshot_fmts[0];
5802 if ($format !~ m/^[a-z0-9]+$/) {
5803 die_error(400, "Invalid snapshot format parameter");
5804 } elsif (!exists($known_snapshot_formats{$format})) {
5805 die_error(400, "Unknown snapshot format");
5806 } elsif ($known_snapshot_formats{$format}{'disabled'}) {
5807 die_error(403, "Snapshot format not allowed");
5808 } elsif (!grep($_ eq $format, @snapshot_fmts)) {
5809 die_error(403, "Unsupported snapshot format");
5812 my $type = git_get_type("$hash^{}");
5813 if (!$type) {
5814 die_error(404, 'Object does not exist');
5815 } elsif ($type eq 'blob') {
5816 die_error(400, 'Object is not a tree-ish');
5819 my ($name, $prefix) = snapshot_name($project, $hash);
5820 my $filename = "$name$known_snapshot_formats{$format}{'suffix'}";
5821 my $cmd = quote_command(
5822 git_cmd(), 'archive',
5823 "--format=$known_snapshot_formats{$format}{'format'}",
5824 "--prefix=$prefix/", $hash);
5825 if (exists $known_snapshot_formats{$format}{'compressor'}) {
5826 $cmd .= ' | ' . quote_command(@{$known_snapshot_formats{$format}{'compressor'}});
5829 $filename =~ s/(["\\])/\\$1/g;
5830 print $cgi->header(
5831 -type => $known_snapshot_formats{$format}{'type'},
5832 -content_disposition => 'inline; filename="' . $filename . '"',
5833 -status => '200 OK');
5835 open my $fd, "-|", $cmd
5836 or die_error(500, "Execute git-archive failed");
5837 binmode STDOUT, ':raw';
5838 print <$fd>;
5839 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
5840 close $fd;
5843 sub git_log_generic {
5844 my ($fmt_name, $body_subr, $base, $parent, $file_name, $file_hash) = @_;
5846 my $head = git_get_head_hash($project);
5847 if (!defined $base) {
5848 $base = $head;
5850 if (!defined $page) {
5851 $page = 0;
5853 my $refs = git_get_references();
5855 my $commit_hash = $base;
5856 if (defined $parent) {
5857 $commit_hash = "$parent..$base";
5859 my @commitlist =
5860 parse_commits($commit_hash, 101, (100 * $page),
5861 defined $file_name ? ($file_name, "--full-history") : ());
5863 my $ftype;
5864 if (!defined $file_hash && defined $file_name) {
5865 # some commits could have deleted file in question,
5866 # and not have it in tree, but one of them has to have it
5867 for (my $i = 0; $i < @commitlist; $i++) {
5868 $file_hash = git_get_hash_by_path($commitlist[$i]{'id'}, $file_name);
5869 last if defined $file_hash;
5872 if (defined $file_hash) {
5873 $ftype = git_get_type($file_hash);
5875 if (defined $file_name && !defined $ftype) {
5876 die_error(500, "Unknown type of object");
5878 my %co;
5879 if (defined $file_name) {
5880 %co = parse_commit($base)
5881 or die_error(404, "Unknown commit object");
5885 my $paging_nav = format_paging_nav($fmt_name, $page, $#commitlist >= 100);
5886 my $next_link = '';
5887 if ($#commitlist >= 100) {
5888 $next_link =
5889 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5890 -accesskey => "n", -title => "Alt-n"}, "next");
5892 my $patch_max = gitweb_get_feature('patches');
5893 if ($patch_max && !defined $file_name) {
5894 if ($patch_max < 0 || @commitlist <= $patch_max) {
5895 $paging_nav .= " &sdot; " .
5896 $cgi->a({-href => href(action=>"patches", -replay=>1)},
5897 "patches");
5901 git_header_html();
5902 git_print_page_nav($fmt_name,'', $hash,$hash,$hash, $paging_nav);
5903 if (defined $file_name) {
5904 git_print_header_div('commit', esc_html($co{'title'}), $base);
5905 } else {
5906 git_print_header_div('summary', $project)
5908 git_print_page_path($file_name, $ftype, $hash_base)
5909 if (defined $file_name);
5911 $body_subr->(\@commitlist, 0, 99, $refs, $next_link,
5912 $file_name, $file_hash, $ftype);
5914 git_footer_html();
5917 sub git_log {
5918 git_log_generic('log', \&git_log_body,
5919 $hash, $hash_parent);
5922 sub git_commit {
5923 $hash ||= $hash_base || "HEAD";
5924 my %co = parse_commit($hash)
5925 or die_error(404, "Unknown commit object");
5927 my $parent = $co{'parent'};
5928 my $parents = $co{'parents'}; # listref
5930 # we need to prepare $formats_nav before any parameter munging
5931 my $formats_nav;
5932 if (!defined $parent) {
5933 # --root commitdiff
5934 $formats_nav .= '(initial)';
5935 } elsif (@$parents == 1) {
5936 # single parent commit
5937 $formats_nav .=
5938 '(parent: ' .
5939 $cgi->a({-href => href(action=>"commit",
5940 hash=>$parent)},
5941 esc_html(substr($parent, 0, 7))) .
5942 ')';
5943 } else {
5944 # merge commit
5945 $formats_nav .=
5946 '(merge: ' .
5947 join(' ', map {
5948 $cgi->a({-href => href(action=>"commit",
5949 hash=>$_)},
5950 esc_html(substr($_, 0, 7)));
5951 } @$parents ) .
5952 ')';
5954 if (gitweb_check_feature('patches') && @$parents <= 1) {
5955 $formats_nav .= " | " .
5956 $cgi->a({-href => href(action=>"patch", -replay=>1)},
5957 "patch");
5960 if (!defined $parent) {
5961 $parent = "--root";
5963 my @difftree;
5964 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
5965 @diff_opts,
5966 (@$parents <= 1 ? $parent : '-c'),
5967 $hash, "--"
5968 or die_error(500, "Open git-diff-tree failed");
5969 @difftree = map { chomp; $_ } <$fd>;
5970 close $fd or die_error(404, "Reading git-diff-tree failed");
5972 # non-textual hash id's can be cached
5973 my $expires;
5974 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5975 $expires = "+1d";
5977 my $refs = git_get_references();
5978 my $ref = format_ref_marker($refs, $co{'id'});
5980 git_header_html(undef, $expires);
5981 git_print_page_nav('commit', '',
5982 $hash, $co{'tree'}, $hash,
5983 $formats_nav);
5985 if (defined $co{'parent'}) {
5986 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
5987 } else {
5988 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
5990 print "<div class=\"title_text\">\n" .
5991 "<table class=\"object_header\">\n";
5992 git_print_authorship_rows(\%co);
5993 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
5994 print "<tr>" .
5995 "<td>tree</td>" .
5996 "<td class=\"sha1\">" .
5997 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
5998 class => "list"}, $co{'tree'}) .
5999 "</td>" .
6000 "<td class=\"link\">" .
6001 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
6002 "tree");
6003 my $snapshot_links = format_snapshot_links($hash);
6004 if (defined $snapshot_links) {
6005 print " | " . $snapshot_links;
6007 print "</td>" .
6008 "</tr>\n";
6010 foreach my $par (@$parents) {
6011 print "<tr>" .
6012 "<td>parent</td>" .
6013 "<td class=\"sha1\">" .
6014 $cgi->a({-href => href(action=>"commit", hash=>$par),
6015 class => "list"}, $par) .
6016 "</td>" .
6017 "<td class=\"link\">" .
6018 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
6019 " | " .
6020 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
6021 "</td>" .
6022 "</tr>\n";
6024 print "</table>".
6025 "</div>\n";
6027 print "<div class=\"page_body\">\n";
6028 git_print_log($co{'comment'});
6029 print "</div>\n";
6031 git_difftree_body(\@difftree, $hash, @$parents);
6033 git_footer_html();
6036 sub git_object {
6037 # object is defined by:
6038 # - hash or hash_base alone
6039 # - hash_base and file_name
6040 my $type;
6042 # - hash or hash_base alone
6043 if ($hash || ($hash_base && !defined $file_name)) {
6044 my $object_id = $hash || $hash_base;
6046 open my $fd, "-|", quote_command(
6047 git_cmd(), 'cat-file', '-t', $object_id) . ' 2> /dev/null'
6048 or die_error(404, "Object does not exist");
6049 $type = <$fd>;
6050 chomp $type;
6051 close $fd
6052 or die_error(404, "Object does not exist");
6054 # - hash_base and file_name
6055 } elsif ($hash_base && defined $file_name) {
6056 $file_name =~ s,/+$,,;
6058 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
6059 or die_error(404, "Base object does not exist");
6061 # here errors should not hapen
6062 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
6063 or die_error(500, "Open git-ls-tree failed");
6064 my $line = <$fd>;
6065 close $fd;
6067 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
6068 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
6069 die_error(404, "File or directory for given base does not exist");
6071 $type = $2;
6072 $hash = $3;
6073 } else {
6074 die_error(400, "Not enough information to find object");
6077 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
6078 hash=>$hash, hash_base=>$hash_base,
6079 file_name=>$file_name),
6080 -status => '302 Found');
6083 sub git_blobdiff {
6084 my $format = shift || 'html';
6086 my $fd;
6087 my @difftree;
6088 my %diffinfo;
6089 my $expires;
6091 # preparing $fd and %diffinfo for git_patchset_body
6092 # new style URI
6093 if (defined $hash_base && defined $hash_parent_base) {
6094 if (defined $file_name) {
6095 # read raw output
6096 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6097 $hash_parent_base, $hash_base,
6098 "--", (defined $file_parent ? $file_parent : ()), $file_name
6099 or die_error(500, "Open git-diff-tree failed");
6100 @difftree = map { chomp; $_ } <$fd>;
6101 close $fd
6102 or die_error(404, "Reading git-diff-tree failed");
6103 @difftree
6104 or die_error(404, "Blob diff not found");
6106 } elsif (defined $hash &&
6107 $hash =~ /[0-9a-fA-F]{40}/) {
6108 # try to find filename from $hash
6110 # read filtered raw output
6111 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6112 $hash_parent_base, $hash_base, "--"
6113 or die_error(500, "Open git-diff-tree failed");
6114 @difftree =
6115 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
6116 # $hash == to_id
6117 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
6118 map { chomp; $_ } <$fd>;
6119 close $fd
6120 or die_error(404, "Reading git-diff-tree failed");
6121 @difftree
6122 or die_error(404, "Blob diff not found");
6124 } else {
6125 die_error(400, "Missing one of the blob diff parameters");
6128 if (@difftree > 1) {
6129 die_error(400, "Ambiguous blob diff specification");
6132 %diffinfo = parse_difftree_raw_line($difftree[0]);
6133 $file_parent ||= $diffinfo{'from_file'} || $file_name;
6134 $file_name ||= $diffinfo{'to_file'};
6136 $hash_parent ||= $diffinfo{'from_id'};
6137 $hash ||= $diffinfo{'to_id'};
6139 # non-textual hash id's can be cached
6140 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
6141 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
6142 $expires = '+1d';
6145 # open patch output
6146 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6147 '-p', ($format eq 'html' ? "--full-index" : ()),
6148 $hash_parent_base, $hash_base,
6149 "--", (defined $file_parent ? $file_parent : ()), $file_name
6150 or die_error(500, "Open git-diff-tree failed");
6153 # old/legacy style URI -- not generated anymore since 1.4.3.
6154 if (!%diffinfo) {
6155 die_error('404 Not Found', "Missing one of the blob diff parameters")
6158 # header
6159 if ($format eq 'html') {
6160 my $formats_nav =
6161 $cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},
6162 "raw");
6163 git_header_html(undef, $expires);
6164 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
6165 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
6166 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
6167 } else {
6168 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
6169 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
6171 if (defined $file_name) {
6172 git_print_page_path($file_name, "blob", $hash_base);
6173 } else {
6174 print "<div class=\"page_path\"></div>\n";
6177 } elsif ($format eq 'plain') {
6178 print $cgi->header(
6179 -type => 'text/plain',
6180 -charset => 'utf-8',
6181 -expires => $expires,
6182 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
6184 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
6186 } else {
6187 die_error(400, "Unknown blobdiff format");
6190 # patch
6191 if ($format eq 'html') {
6192 print "<div class=\"page_body\">\n";
6194 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
6195 close $fd;
6197 print "</div>\n"; # class="page_body"
6198 git_footer_html();
6200 } else {
6201 while (my $line = <$fd>) {
6202 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
6203 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
6205 print $line;
6207 last if $line =~ m!^\+\+\+!;
6209 local $/ = undef;
6210 print <$fd>;
6211 close $fd;
6215 sub git_blobdiff_plain {
6216 git_blobdiff('plain');
6219 sub git_commitdiff {
6220 my %params = @_;
6221 my $format = $params{-format} || 'html';
6223 my ($patch_max) = gitweb_get_feature('patches');
6224 if ($format eq 'patch') {
6225 die_error(403, "Patch view not allowed") unless $patch_max;
6228 $hash ||= $hash_base || "HEAD";
6229 my %co = parse_commit($hash)
6230 or die_error(404, "Unknown commit object");
6232 # choose format for commitdiff for merge
6233 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
6234 $hash_parent = '--cc';
6236 # we need to prepare $formats_nav before almost any parameter munging
6237 my $formats_nav;
6238 if ($format eq 'html') {
6239 $formats_nav =
6240 $cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
6241 "raw");
6242 if ($patch_max && @{$co{'parents'}} <= 1) {
6243 $formats_nav .= " | " .
6244 $cgi->a({-href => href(action=>"patch", -replay=>1)},
6245 "patch");
6248 if (defined $hash_parent &&
6249 $hash_parent ne '-c' && $hash_parent ne '--cc') {
6250 # commitdiff with two commits given
6251 my $hash_parent_short = $hash_parent;
6252 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
6253 $hash_parent_short = substr($hash_parent, 0, 7);
6255 $formats_nav .=
6256 ' (from';
6257 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
6258 if ($co{'parents'}[$i] eq $hash_parent) {
6259 $formats_nav .= ' parent ' . ($i+1);
6260 last;
6263 $formats_nav .= ': ' .
6264 $cgi->a({-href => href(action=>"commitdiff",
6265 hash=>$hash_parent)},
6266 esc_html($hash_parent_short)) .
6267 ')';
6268 } elsif (!$co{'parent'}) {
6269 # --root commitdiff
6270 $formats_nav .= ' (initial)';
6271 } elsif (scalar @{$co{'parents'}} == 1) {
6272 # single parent commit
6273 $formats_nav .=
6274 ' (parent: ' .
6275 $cgi->a({-href => href(action=>"commitdiff",
6276 hash=>$co{'parent'})},
6277 esc_html(substr($co{'parent'}, 0, 7))) .
6278 ')';
6279 } else {
6280 # merge commit
6281 if ($hash_parent eq '--cc') {
6282 $formats_nav .= ' | ' .
6283 $cgi->a({-href => href(action=>"commitdiff",
6284 hash=>$hash, hash_parent=>'-c')},
6285 'combined');
6286 } else { # $hash_parent eq '-c'
6287 $formats_nav .= ' | ' .
6288 $cgi->a({-href => href(action=>"commitdiff",
6289 hash=>$hash, hash_parent=>'--cc')},
6290 'compact');
6292 $formats_nav .=
6293 ' (merge: ' .
6294 join(' ', map {
6295 $cgi->a({-href => href(action=>"commitdiff",
6296 hash=>$_)},
6297 esc_html(substr($_, 0, 7)));
6298 } @{$co{'parents'}} ) .
6299 ')';
6303 my $hash_parent_param = $hash_parent;
6304 if (!defined $hash_parent_param) {
6305 # --cc for multiple parents, --root for parentless
6306 $hash_parent_param =
6307 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
6310 # read commitdiff
6311 my $fd;
6312 my @difftree;
6313 if ($format eq 'html') {
6314 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6315 "--no-commit-id", "--patch-with-raw", "--full-index",
6316 $hash_parent_param, $hash, "--"
6317 or die_error(500, "Open git-diff-tree failed");
6319 while (my $line = <$fd>) {
6320 chomp $line;
6321 # empty line ends raw part of diff-tree output
6322 last unless $line;
6323 push @difftree, scalar parse_difftree_raw_line($line);
6326 } elsif ($format eq 'plain') {
6327 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6328 '-p', $hash_parent_param, $hash, "--"
6329 or die_error(500, "Open git-diff-tree failed");
6330 } elsif ($format eq 'patch') {
6331 # For commit ranges, we limit the output to the number of
6332 # patches specified in the 'patches' feature.
6333 # For single commits, we limit the output to a single patch,
6334 # diverging from the git-format-patch default.
6335 my @commit_spec = ();
6336 if ($hash_parent) {
6337 if ($patch_max > 0) {
6338 push @commit_spec, "-$patch_max";
6340 push @commit_spec, '-n', "$hash_parent..$hash";
6341 } else {
6342 if ($params{-single}) {
6343 push @commit_spec, '-1';
6344 } else {
6345 if ($patch_max > 0) {
6346 push @commit_spec, "-$patch_max";
6348 push @commit_spec, "-n";
6350 push @commit_spec, '--root', $hash;
6352 open $fd, "-|", git_cmd(), "format-patch", @diff_opts,
6353 '--encoding=utf8', '--stdout', @commit_spec
6354 or die_error(500, "Open git-format-patch failed");
6355 } else {
6356 die_error(400, "Unknown commitdiff format");
6359 # non-textual hash id's can be cached
6360 my $expires;
6361 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6362 $expires = "+1d";
6365 # write commit message
6366 if ($format eq 'html') {
6367 my $refs = git_get_references();
6368 my $ref = format_ref_marker($refs, $co{'id'});
6370 git_header_html(undef, $expires);
6371 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
6372 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
6373 print "<div class=\"title_text\">\n" .
6374 "<table class=\"object_header\">\n";
6375 git_print_authorship_rows(\%co);
6376 print "</table>".
6377 "</div>\n";
6378 print "<div class=\"page_body\">\n";
6379 if (@{$co{'comment'}} > 1) {
6380 print "<div class=\"log\">\n";
6381 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
6382 print "</div>\n"; # class="log"
6385 } elsif ($format eq 'plain') {
6386 my $refs = git_get_references("tags");
6387 my $tagname = git_get_rev_name_tags($hash);
6388 my $filename = basename($project) . "-$hash.patch";
6390 print $cgi->header(
6391 -type => 'text/plain',
6392 -charset => 'utf-8',
6393 -expires => $expires,
6394 -content_disposition => 'inline; filename="' . "$filename" . '"');
6395 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
6396 print "From: " . to_utf8($co{'author'}) . "\n";
6397 print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
6398 print "Subject: " . to_utf8($co{'title'}) . "\n";
6400 print "X-Git-Tag: $tagname\n" if $tagname;
6401 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
6403 foreach my $line (@{$co{'comment'}}) {
6404 print to_utf8($line) . "\n";
6406 print "---\n\n";
6407 } elsif ($format eq 'patch') {
6408 my $filename = basename($project) . "-$hash.patch";
6410 print $cgi->header(
6411 -type => 'text/plain',
6412 -charset => 'utf-8',
6413 -expires => $expires,
6414 -content_disposition => 'inline; filename="' . "$filename" . '"');
6417 # write patch
6418 if ($format eq 'html') {
6419 my $use_parents = !defined $hash_parent ||
6420 $hash_parent eq '-c' || $hash_parent eq '--cc';
6421 git_difftree_body(\@difftree, $hash,
6422 $use_parents ? @{$co{'parents'}} : $hash_parent);
6423 print "<br/>\n";
6425 git_patchset_body($fd, \@difftree, $hash,
6426 $use_parents ? @{$co{'parents'}} : $hash_parent);
6427 close $fd;
6428 print "</div>\n"; # class="page_body"
6429 git_footer_html();
6431 } elsif ($format eq 'plain') {
6432 local $/ = undef;
6433 print <$fd>;
6434 close $fd
6435 or print "Reading git-diff-tree failed\n";
6436 } elsif ($format eq 'patch') {
6437 local $/ = undef;
6438 print <$fd>;
6439 close $fd
6440 or print "Reading git-format-patch failed\n";
6444 sub git_commitdiff_plain {
6445 git_commitdiff(-format => 'plain');
6448 # format-patch-style patches
6449 sub git_patch {
6450 git_commitdiff(-format => 'patch', -single => 1);
6453 sub git_patches {
6454 git_commitdiff(-format => 'patch');
6457 sub git_history {
6458 git_log_generic('history', \&git_history_body,
6459 $hash_base, $hash_parent_base,
6460 $file_name, $hash);
6463 sub git_search {
6464 gitweb_check_feature('search') or die_error(403, "Search is disabled");
6465 if (!defined $searchtext) {
6466 die_error(400, "Text field is empty");
6468 if (!defined $hash) {
6469 $hash = git_get_head_hash($project);
6471 my %co = parse_commit($hash);
6472 if (!%co) {
6473 die_error(404, "Unknown commit object");
6475 if (!defined $page) {
6476 $page = 0;
6479 $searchtype ||= 'commit';
6480 if ($searchtype eq 'pickaxe') {
6481 # pickaxe may take all resources of your box and run for several minutes
6482 # with every query - so decide by yourself how public you make this feature
6483 gitweb_check_feature('pickaxe')
6484 or die_error(403, "Pickaxe is disabled");
6486 if ($searchtype eq 'grep') {
6487 gitweb_check_feature('grep')
6488 or die_error(403, "Grep is disabled");
6491 git_header_html();
6493 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
6494 my $greptype;
6495 if ($searchtype eq 'commit') {
6496 $greptype = "--grep=";
6497 } elsif ($searchtype eq 'author') {
6498 $greptype = "--author=";
6499 } elsif ($searchtype eq 'committer') {
6500 $greptype = "--committer=";
6502 $greptype .= $searchtext;
6503 my @commitlist = parse_commits($hash, 101, (100 * $page), undef,
6504 $greptype, '--regexp-ignore-case',
6505 $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
6507 my $paging_nav = '';
6508 if ($page > 0) {
6509 $paging_nav .=
6510 $cgi->a({-href => href(action=>"search", hash=>$hash,
6511 searchtext=>$searchtext,
6512 searchtype=>$searchtype)},
6513 "first");
6514 $paging_nav .= " &sdot; " .
6515 $cgi->a({-href => href(-replay=>1, page=>$page-1),
6516 -accesskey => "p", -title => "Alt-p"}, "prev");
6517 } else {
6518 $paging_nav .= "first";
6519 $paging_nav .= " &sdot; prev";
6521 my $next_link = '';
6522 if ($#commitlist >= 100) {
6523 $next_link =
6524 $cgi->a({-href => href(-replay=>1, page=>$page+1),
6525 -accesskey => "n", -title => "Alt-n"}, "next");
6526 $paging_nav .= " &sdot; $next_link";
6527 } else {
6528 $paging_nav .= " &sdot; next";
6531 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
6532 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6533 if ($page == 0 && !@commitlist) {
6534 print "<p>No match.</p>\n";
6535 } else {
6536 git_search_grep_body(\@commitlist, 0, 99, $next_link);
6540 if ($searchtype eq 'pickaxe') {
6541 git_print_page_nav('','', $hash,$co{'tree'},$hash);
6542 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6544 print "<table class=\"pickaxe search\">\n";
6545 my $alternate = 1;
6546 local $/ = "\n";
6547 open my $fd, '-|', git_cmd(), '--no-pager', 'log', @diff_opts,
6548 '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
6549 ($search_use_regexp ? '--pickaxe-regex' : ());
6550 undef %co;
6551 my @files;
6552 while (my $line = <$fd>) {
6553 chomp $line;
6554 next unless $line;
6556 my %set = parse_difftree_raw_line($line);
6557 if (defined $set{'commit'}) {
6558 # finish previous commit
6559 if (%co) {
6560 print "</td>\n" .
6561 "<td class=\"link\">" .
6562 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
6563 " | " .
6564 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
6565 print "</td>\n" .
6566 "</tr>\n";
6569 if ($alternate) {
6570 print "<tr class=\"dark\">\n";
6571 } else {
6572 print "<tr class=\"light\">\n";
6574 $alternate ^= 1;
6575 %co = parse_commit($set{'commit'});
6576 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
6577 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
6578 "<td><i>$author</i></td>\n" .
6579 "<td>" .
6580 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
6581 -class => "list subject"},
6582 chop_and_escape_str($co{'title'}, 50) . "<br/>");
6583 } elsif (defined $set{'to_id'}) {
6584 next if ($set{'to_id'} =~ m/^0{40}$/);
6586 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
6587 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),
6588 -class => "list"},
6589 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
6590 "<br/>\n";
6593 close $fd;
6595 # finish last commit (warning: repetition!)
6596 if (%co) {
6597 print "</td>\n" .
6598 "<td class=\"link\">" .
6599 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
6600 " | " .
6601 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
6602 print "</td>\n" .
6603 "</tr>\n";
6606 print "</table>\n";
6609 if ($searchtype eq 'grep') {
6610 git_print_page_nav('','', $hash,$co{'tree'},$hash);
6611 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6613 print "<table class=\"grep_search\">\n";
6614 my $alternate = 1;
6615 my $matches = 0;
6616 local $/ = "\n";
6617 open my $fd, "-|", git_cmd(), 'grep', '-n',
6618 $search_use_regexp ? ('-E', '-i') : '-F',
6619 $searchtext, $co{'tree'};
6620 my $lastfile = '';
6621 while (my $line = <$fd>) {
6622 chomp $line;
6623 my ($file, $lno, $ltext, $binary);
6624 last if ($matches++ > 1000);
6625 if ($line =~ /^Binary file (.+) matches$/) {
6626 $file = $1;
6627 $binary = 1;
6628 } else {
6629 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
6631 if ($file ne $lastfile) {
6632 $lastfile and print "</td></tr>\n";
6633 if ($alternate++) {
6634 print "<tr class=\"dark\">\n";
6635 } else {
6636 print "<tr class=\"light\">\n";
6638 print "<td class=\"list\">".
6639 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
6640 file_name=>"$file"),
6641 -class => "list"}, esc_path($file));
6642 print "</td><td>\n";
6643 $lastfile = $file;
6645 if ($binary) {
6646 print "<div class=\"binary\">Binary file</div>\n";
6647 } else {
6648 $ltext = untabify($ltext);
6649 if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
6650 $ltext = esc_html($1, -nbsp=>1);
6651 $ltext .= '<span class="match">';
6652 $ltext .= esc_html($2, -nbsp=>1);
6653 $ltext .= '</span>';
6654 $ltext .= esc_html($3, -nbsp=>1);
6655 } else {
6656 $ltext = esc_html($ltext, -nbsp=>1);
6658 print "<div class=\"pre\">" .
6659 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
6660 file_name=>"$file").'#l'.$lno,
6661 -class => "linenr"}, sprintf('%4i', $lno))
6662 . ' ' . $ltext . "</div>\n";
6665 if ($lastfile) {
6666 print "</td></tr>\n";
6667 if ($matches > 1000) {
6668 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
6670 } else {
6671 print "<div class=\"diff nodifferences\">No matches found</div>\n";
6673 close $fd;
6675 print "</table>\n";
6677 git_footer_html();
6680 sub git_search_help {
6681 git_header_html();
6682 git_print_page_nav('','', $hash,$hash,$hash);
6683 print <<EOT;
6684 <p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without
6685 regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,
6686 the pattern entered is recognized as the POSIX extended
6687 <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case
6688 insensitive).</p>
6689 <dl>
6690 <dt><b>commit</b></dt>
6691 <dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
6693 my $have_grep = gitweb_check_feature('grep');
6694 if ($have_grep) {
6695 print <<EOT;
6696 <dt><b>grep</b></dt>
6697 <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
6698 a different one) are searched for the given pattern. On large trees, this search can take
6699 a while and put some strain on the server, so please use it with some consideration. Note that
6700 due to git-grep peculiarity, currently if regexp mode is turned off, the matches are
6701 case-sensitive.</dd>
6704 print <<EOT;
6705 <dt><b>author</b></dt>
6706 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>
6707 <dt><b>committer</b></dt>
6708 <dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>
6710 my $have_pickaxe = gitweb_check_feature('pickaxe');
6711 if ($have_pickaxe) {
6712 print <<EOT;
6713 <dt><b>pickaxe</b></dt>
6714 <dd>All commits that caused the string to appear or disappear from any file (changes that
6715 added, removed or "modified" the string) will be listed. This search can take a while and
6716 takes a lot of strain on the server, so please use it wisely. Note that since you may be
6717 interested even in changes just changing the case as well, this search is case sensitive.</dd>
6720 print "</dl>\n";
6721 git_footer_html();
6724 sub git_shortlog {
6725 git_log_generic('shortlog', \&git_shortlog_body,
6726 $hash, $hash_parent);
6729 ## ......................................................................
6730 ## feeds (RSS, Atom; OPML)
6732 sub git_feed {
6733 my $format = shift || 'atom';
6734 my $have_blame = gitweb_check_feature('blame');
6736 # Atom: http://www.atomenabled.org/developers/syndication/
6737 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
6738 if ($format ne 'rss' && $format ne 'atom') {
6739 die_error(400, "Unknown web feed format");
6742 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
6743 my $head = $hash || 'HEAD';
6744 my @commitlist = parse_commits($head, 150, 0, $file_name);
6746 my %latest_commit;
6747 my %latest_date;
6748 my $content_type = "application/$format+xml";
6749 if (defined $cgi->http('HTTP_ACCEPT') &&
6750 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
6751 # browser (feed reader) prefers text/xml
6752 $content_type = 'text/xml';
6754 if (defined($commitlist[0])) {
6755 %latest_commit = %{$commitlist[0]};
6756 my $latest_epoch = $latest_commit{'committer_epoch'};
6757 %latest_date = parse_date($latest_epoch);
6758 my $if_modified = $cgi->http('IF_MODIFIED_SINCE');
6759 if (defined $if_modified) {
6760 my $since;
6761 if (eval { require HTTP::Date; 1; }) {
6762 $since = HTTP::Date::str2time($if_modified);
6763 } elsif (eval { require Time::ParseDate; 1; }) {
6764 $since = Time::ParseDate::parsedate($if_modified, GMT => 1);
6766 if (defined $since && $latest_epoch <= $since) {
6767 print $cgi->header(
6768 -type => $content_type,
6769 -charset => 'utf-8',
6770 -last_modified => $latest_date{'rfc2822'},
6771 -status => '304 Not Modified');
6772 return;
6775 print $cgi->header(
6776 -type => $content_type,
6777 -charset => 'utf-8',
6778 -last_modified => $latest_date{'rfc2822'});
6779 } else {
6780 print $cgi->header(
6781 -type => $content_type,
6782 -charset => 'utf-8');
6785 # Optimization: skip generating the body if client asks only
6786 # for Last-Modified date.
6787 return if ($cgi->request_method() eq 'HEAD');
6789 # header variables
6790 my $title = "$site_name - $project/$action";
6791 my $feed_type = 'log';
6792 if (defined $hash) {
6793 $title .= " - '$hash'";
6794 $feed_type = 'branch log';
6795 if (defined $file_name) {
6796 $title .= " :: $file_name";
6797 $feed_type = 'history';
6799 } elsif (defined $file_name) {
6800 $title .= " - $file_name";
6801 $feed_type = 'history';
6803 $title .= " $feed_type";
6804 my $descr = git_get_project_description($project);
6805 if (defined $descr) {
6806 $descr = esc_html($descr);
6807 } else {
6808 $descr = "$project " .
6809 ($format eq 'rss' ? 'RSS' : 'Atom') .
6810 " feed";
6812 my $owner = git_get_project_owner($project);
6813 $owner = esc_html($owner);
6815 #header
6816 my $alt_url;
6817 if (defined $file_name) {
6818 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
6819 } elsif (defined $hash) {
6820 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
6821 } else {
6822 $alt_url = href(-full=>1, action=>"summary");
6824 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
6825 if ($format eq 'rss') {
6826 print <<XML;
6827 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
6828 <channel>
6830 print "<title>$title</title>\n" .
6831 "<link>$alt_url</link>\n" .
6832 "<description>$descr</description>\n" .
6833 "<language>en</language>\n" .
6834 # project owner is responsible for 'editorial' content
6835 "<managingEditor>$owner</managingEditor>\n";
6836 if (defined $logo || defined $favicon) {
6837 # prefer the logo to the favicon, since RSS
6838 # doesn't allow both
6839 my $img = esc_url($logo || $favicon);
6840 print "<image>\n" .
6841 "<url>$img</url>\n" .
6842 "<title>$title</title>\n" .
6843 "<link>$alt_url</link>\n" .
6844 "</image>\n";
6846 if (%latest_date) {
6847 print "<pubDate>$latest_date{'rfc2822'}</pubDate>\n";
6848 print "<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";
6850 print "<generator>gitweb v.$version/$git_version</generator>\n";
6851 } elsif ($format eq 'atom') {
6852 print <<XML;
6853 <feed xmlns="http://www.w3.org/2005/Atom">
6855 print "<title>$title</title>\n" .
6856 "<subtitle>$descr</subtitle>\n" .
6857 '<link rel="alternate" type="text/html" href="' .
6858 $alt_url . '" />' . "\n" .
6859 '<link rel="self" type="' . $content_type . '" href="' .
6860 $cgi->self_url() . '" />' . "\n" .
6861 "<id>" . href(-full=>1) . "</id>\n" .
6862 # use project owner for feed author
6863 "<author><name>$owner</name></author>\n";
6864 if (defined $favicon) {
6865 print "<icon>" . esc_url($favicon) . "</icon>\n";
6867 if (defined $logo_url) {
6868 # not twice as wide as tall: 72 x 27 pixels
6869 print "<logo>" . esc_url($logo) . "</logo>\n";
6871 if (! %latest_date) {
6872 # dummy date to keep the feed valid until commits trickle in:
6873 print "<updated>1970-01-01T00:00:00Z</updated>\n";
6874 } else {
6875 print "<updated>$latest_date{'iso-8601'}</updated>\n";
6877 print "<generator version='$version/$git_version'>gitweb</generator>\n";
6880 # contents
6881 for (my $i = 0; $i <= $#commitlist; $i++) {
6882 my %co = %{$commitlist[$i]};
6883 my $commit = $co{'id'};
6884 # we read 150, we always show 30 and the ones more recent than 48 hours
6885 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
6886 last;
6888 my %cd = parse_date($co{'author_epoch'});
6890 # get list of changed files
6891 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6892 $co{'parent'} || "--root",
6893 $co{'id'}, "--", (defined $file_name ? $file_name : ())
6894 or next;
6895 my @difftree = map { chomp; $_ } <$fd>;
6896 close $fd
6897 or next;
6899 # print element (entry, item)
6900 my $co_url = href(-full=>1, action=>"commitdiff", hash=>$commit);
6901 if ($format eq 'rss') {
6902 print "<item>\n" .
6903 "<title>" . esc_html($co{'title'}) . "</title>\n" .
6904 "<author>" . esc_html($co{'author'}) . "</author>\n" .
6905 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
6906 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
6907 "<link>$co_url</link>\n" .
6908 "<description>" . esc_html($co{'title'}) . "</description>\n" .
6909 "<content:encoded>" .
6910 "<![CDATA[\n";
6911 } elsif ($format eq 'atom') {
6912 print "<entry>\n" .
6913 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
6914 "<updated>$cd{'iso-8601'}</updated>\n" .
6915 "<author>\n" .
6916 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
6917 if ($co{'author_email'}) {
6918 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
6920 print "</author>\n" .
6921 # use committer for contributor
6922 "<contributor>\n" .
6923 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
6924 if ($co{'committer_email'}) {
6925 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
6927 print "</contributor>\n" .
6928 "<published>$cd{'iso-8601'}</published>\n" .
6929 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
6930 "<id>$co_url</id>\n" .
6931 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
6932 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
6934 my $comment = $co{'comment'};
6935 print "<pre>\n";
6936 foreach my $line (@$comment) {
6937 $line = esc_html($line);
6938 print "$line\n";
6940 print "</pre><ul>\n";
6941 foreach my $difftree_line (@difftree) {
6942 my %difftree = parse_difftree_raw_line($difftree_line);
6943 next if !$difftree{'from_id'};
6945 my $file = $difftree{'file'} || $difftree{'to_file'};
6947 print "<li>" .
6948 "[" .
6949 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
6950 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
6951 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
6952 file_name=>$file, file_parent=>$difftree{'from_file'}),
6953 -title => "diff"}, 'D');
6954 if ($have_blame) {
6955 print $cgi->a({-href => href(-full=>1, action=>"blame",
6956 file_name=>$file, hash_base=>$commit),
6957 -title => "blame"}, 'B');
6959 # if this is not a feed of a file history
6960 if (!defined $file_name || $file_name ne $file) {
6961 print $cgi->a({-href => href(-full=>1, action=>"history",
6962 file_name=>$file, hash=>$commit),
6963 -title => "history"}, 'H');
6965 $file = esc_path($file);
6966 print "] ".
6967 "$file</li>\n";
6969 if ($format eq 'rss') {
6970 print "</ul>]]>\n" .
6971 "</content:encoded>\n" .
6972 "</item>\n";
6973 } elsif ($format eq 'atom') {
6974 print "</ul>\n</div>\n" .
6975 "</content>\n" .
6976 "</entry>\n";
6980 # end of feed
6981 if ($format eq 'rss') {
6982 print "</channel>\n</rss>\n";
6983 } elsif ($format eq 'atom') {
6984 print "</feed>\n";
6988 sub git_rss {
6989 git_feed('rss');
6992 sub git_atom {
6993 git_feed('atom');
6996 sub git_opml {
6997 my @list = git_get_projects_list();
6999 print $cgi->header(
7000 -type => 'text/xml',
7001 -charset => 'utf-8',
7002 -content_disposition => 'inline; filename="opml.xml"');
7004 print <<XML;
7005 <?xml version="1.0" encoding="utf-8"?>
7006 <opml version="1.0">
7007 <head>
7008 <title>$site_name OPML Export</title>
7009 </head>
7010 <body>
7011 <outline text="git RSS feeds">
7014 foreach my $pr (@list) {
7015 my %proj = %$pr;
7016 my $head = git_get_head_hash($proj{'path'});
7017 if (!defined $head) {
7018 next;
7020 $git_dir = "$projectroot/$proj{'path'}";
7021 my %co = parse_commit($head);
7022 if (!%co) {
7023 next;
7026 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
7027 my $rss = href('project' => $proj{'path'}, 'action' => 'rss', -full => 1);
7028 my $html = href('project' => $proj{'path'}, 'action' => 'summary', -full => 1);
7029 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
7031 print <<XML;
7032 </outline>
7033 </body>
7034 </opml>