Merge branch 'gb/gitweb-remote-heads'
[git/dscho.git] / gitweb / gitweb.perl
bloba1a3797b6d4fcd1a282f42ae7f63a76bb471ea90
1 #!/usr/bin/perl
3 # gitweb - simple web interface to track changes in git repositories
5 # (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org>
6 # (C) 2005, Christian Gierke
8 # This program is licensed under the GPLv2
10 use 5.008;
11 use strict;
12 use warnings;
13 use CGI qw(:standard :escapeHTML -nosticky);
14 use CGI::Util qw(unescape);
15 use CGI::Carp qw(fatalsToBrowser set_message);
16 use Encode;
17 use Fcntl ':mode';
18 use File::Find qw();
19 use File::Basename qw(basename);
20 binmode STDOUT, ':utf8';
22 our $t0;
23 if (eval { require Time::HiRes; 1; }) {
24 $t0 = [Time::HiRes::gettimeofday()];
26 our $number_of_git_cmds = 0;
28 BEGIN {
29 CGI->compile() if $ENV{'MOD_PERL'};
32 our $version = "++GIT_VERSION++";
34 our ($my_url, $my_uri, $base_url, $path_info, $home_link);
35 sub evaluate_uri {
36 our $cgi;
38 our $my_url = $cgi->url();
39 our $my_uri = $cgi->url(-absolute => 1);
41 # Base URL for relative URLs in gitweb ($logo, $favicon, ...),
42 # needed and used only for URLs with nonempty PATH_INFO
43 our $base_url = $my_url;
45 # When the script is used as DirectoryIndex, the URL does not contain the name
46 # of the script file itself, and $cgi->url() fails to strip PATH_INFO, so we
47 # have to do it ourselves. We make $path_info global because it's also used
48 # later on.
50 # Another issue with the script being the DirectoryIndex is that the resulting
51 # $my_url data is not the full script URL: this is good, because we want
52 # generated links to keep implying the script name if it wasn't explicitly
53 # indicated in the URL we're handling, but it means that $my_url cannot be used
54 # as base URL.
55 # Therefore, if we needed to strip PATH_INFO, then we know that we have
56 # to build the base URL ourselves:
57 our $path_info = $ENV{"PATH_INFO"};
58 if ($path_info) {
59 if ($my_url =~ s,\Q$path_info\E$,, &&
60 $my_uri =~ s,\Q$path_info\E$,, &&
61 defined $ENV{'SCRIPT_NAME'}) {
62 $base_url = $cgi->url(-base => 1) . $ENV{'SCRIPT_NAME'};
66 # target of the home link on top of all pages
67 our $home_link = $my_uri || "/";
70 # core git executable to use
71 # this can just be "git" if your webserver has a sensible PATH
72 our $GIT = "++GIT_BINDIR++/git";
74 # absolute fs-path which will be prepended to the project path
75 #our $projectroot = "/pub/scm";
76 our $projectroot = "++GITWEB_PROJECTROOT++";
78 # fs traversing limit for getting project list
79 # the number is relative to the projectroot
80 our $project_maxdepth = "++GITWEB_PROJECT_MAXDEPTH++";
82 # string of the home link on top of all pages
83 our $home_link_str = "++GITWEB_HOME_LINK_STR++";
85 # name of your site or organization to appear in page titles
86 # replace this with something more descriptive for clearer bookmarks
87 our $site_name = "++GITWEB_SITENAME++"
88 || ($ENV{'SERVER_NAME'} || "Untitled") . " Git";
90 # filename of html text to include at top of each page
91 our $site_header = "++GITWEB_SITE_HEADER++";
92 # html text to include at home page
93 our $home_text = "++GITWEB_HOMETEXT++";
94 # filename of html text to include at bottom of each page
95 our $site_footer = "++GITWEB_SITE_FOOTER++";
97 # URI of stylesheets
98 our @stylesheets = ("++GITWEB_CSS++");
99 # URI of a single stylesheet, which can be overridden in GITWEB_CONFIG.
100 our $stylesheet = undef;
101 # URI of GIT logo (72x27 size)
102 our $logo = "++GITWEB_LOGO++";
103 # URI of GIT favicon, assumed to be image/png type
104 our $favicon = "++GITWEB_FAVICON++";
105 # URI of gitweb.js (JavaScript code for gitweb)
106 our $javascript = "++GITWEB_JS++";
108 # URI and label (title) of GIT logo link
109 #our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
110 #our $logo_label = "git documentation";
111 our $logo_url = "http://git-scm.com/";
112 our $logo_label = "git homepage";
114 # source of projects list
115 our $projects_list = "++GITWEB_LIST++";
117 # the width (in characters) of the projects list "Description" column
118 our $projects_list_description_width = 25;
120 # default order of projects list
121 # valid values are none, project, descr, owner, and age
122 our $default_projects_order = "project";
124 # show repository only if this file exists
125 # (only effective if this variable evaluates to true)
126 our $export_ok = "++GITWEB_EXPORT_OK++";
128 # show repository only if this subroutine returns true
129 # when given the path to the project, for example:
130 # sub { return -e "$_[0]/git-daemon-export-ok"; }
131 our $export_auth_hook = undef;
133 # only allow viewing of repositories also shown on the overview page
134 our $strict_export = "++GITWEB_STRICT_EXPORT++";
136 # list of git base URLs used for URL to where fetch project from,
137 # i.e. full URL is "$git_base_url/$project"
138 our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
140 # default blob_plain mimetype and default charset for text/plain blob
141 our $default_blob_plain_mimetype = 'text/plain';
142 our $default_text_plain_charset = undef;
144 # file to use for guessing MIME types before trying /etc/mime.types
145 # (relative to the current git repository)
146 our $mimetypes_file = undef;
148 # assume this charset if line contains non-UTF-8 characters;
149 # it should be valid encoding (see Encoding::Supported(3pm) for list),
150 # for which encoding all byte sequences are valid, for example
151 # 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
152 # could be even 'utf-8' for the old behavior)
153 our $fallback_encoding = 'latin1';
155 # rename detection options for git-diff and git-diff-tree
156 # - default is '-M', with the cost proportional to
157 # (number of removed files) * (number of new files).
158 # - more costly is '-C' (which implies '-M'), with the cost proportional to
159 # (number of changed files + number of removed files) * (number of new files)
160 # - even more costly is '-C', '--find-copies-harder' with cost
161 # (number of files in the original tree) * (number of new files)
162 # - one might want to include '-B' option, e.g. '-B', '-M'
163 our @diff_opts = ('-M'); # taken from git_commit
165 # Disables features that would allow repository owners to inject script into
166 # the gitweb domain.
167 our $prevent_xss = 0;
169 # Path to the highlight executable to use (must be the one from
170 # http://www.andre-simon.de due to assumptions about parameters and output).
171 # Useful if highlight is not installed on your webserver's PATH.
172 # [Default: highlight]
173 our $highlight_bin = "++HIGHLIGHT_BIN++";
175 # information about snapshot formats that gitweb is capable of serving
176 our %known_snapshot_formats = (
177 # name => {
178 # 'display' => display name,
179 # 'type' => mime type,
180 # 'suffix' => filename suffix,
181 # 'format' => --format for git-archive,
182 # 'compressor' => [compressor command and arguments]
183 # (array reference, optional)
184 # 'disabled' => boolean (optional)}
186 'tgz' => {
187 'display' => 'tar.gz',
188 'type' => 'application/x-gzip',
189 'suffix' => '.tar.gz',
190 'format' => 'tar',
191 'compressor' => ['gzip']},
193 'tbz2' => {
194 'display' => 'tar.bz2',
195 'type' => 'application/x-bzip2',
196 'suffix' => '.tar.bz2',
197 'format' => 'tar',
198 'compressor' => ['bzip2']},
200 'txz' => {
201 'display' => 'tar.xz',
202 'type' => 'application/x-xz',
203 'suffix' => '.tar.xz',
204 'format' => 'tar',
205 'compressor' => ['xz'],
206 'disabled' => 1},
208 'zip' => {
209 'display' => 'zip',
210 'type' => 'application/x-zip',
211 'suffix' => '.zip',
212 'format' => 'zip'},
215 # Aliases so we understand old gitweb.snapshot values in repository
216 # configuration.
217 our %known_snapshot_format_aliases = (
218 'gzip' => 'tgz',
219 'bzip2' => 'tbz2',
220 'xz' => 'txz',
222 # backward compatibility: legacy gitweb config support
223 'x-gzip' => undef, 'gz' => undef,
224 'x-bzip2' => undef, 'bz2' => undef,
225 'x-zip' => undef, '' => undef,
228 # Pixel sizes for icons and avatars. If the default font sizes or lineheights
229 # are changed, it may be appropriate to change these values too via
230 # $GITWEB_CONFIG.
231 our %avatar_size = (
232 'default' => 16,
233 'double' => 32
236 # Used to set the maximum load that we will still respond to gitweb queries.
237 # If server load exceed this value then return "503 server busy" error.
238 # If gitweb cannot determined server load, it is taken to be 0.
239 # Leave it undefined (or set to 'undef') to turn off load checking.
240 our $maxload = 300;
242 # configuration for 'highlight' (http://www.andre-simon.de/)
243 # match by basename
244 our %highlight_basename = (
245 #'Program' => 'py',
246 #'Library' => 'py',
247 'SConstruct' => 'py', # SCons equivalent of Makefile
248 'Makefile' => 'make',
250 # match by extension
251 our %highlight_ext = (
252 # main extensions, defining name of syntax;
253 # see files in /usr/share/highlight/langDefs/ directory
254 map { $_ => $_ }
255 qw(py c cpp rb java css php sh pl js tex bib xml awk bat ini spec tcl),
256 # alternate extensions, see /etc/highlight/filetypes.conf
257 'h' => 'c',
258 map { $_ => 'cpp' } qw(cxx c++ cc),
259 map { $_ => 'php' } qw(php3 php4),
260 map { $_ => 'pl' } qw(perl pm), # perhaps also 'cgi'
261 'mak' => 'make',
262 map { $_ => 'xml' } qw(xhtml html htm),
265 # You define site-wide feature defaults here; override them with
266 # $GITWEB_CONFIG as necessary.
267 our %feature = (
268 # feature => {
269 # 'sub' => feature-sub (subroutine),
270 # 'override' => allow-override (boolean),
271 # 'default' => [ default options...] (array reference)}
273 # if feature is overridable (it means that allow-override has true value),
274 # then feature-sub will be called with default options as parameters;
275 # return value of feature-sub indicates if to enable specified feature
277 # if there is no 'sub' key (no feature-sub), then feature cannot be
278 # overridden
280 # use gitweb_get_feature(<feature>) to retrieve the <feature> value
281 # (an array) or gitweb_check_feature(<feature>) to check if <feature>
282 # is enabled
284 # Enable the 'blame' blob view, showing the last commit that modified
285 # each line in the file. This can be very CPU-intensive.
287 # To enable system wide have in $GITWEB_CONFIG
288 # $feature{'blame'}{'default'} = [1];
289 # To have project specific config enable override in $GITWEB_CONFIG
290 # $feature{'blame'}{'override'} = 1;
291 # and in project config gitweb.blame = 0|1;
292 'blame' => {
293 'sub' => sub { feature_bool('blame', @_) },
294 'override' => 0,
295 'default' => [0]},
297 # Enable the 'snapshot' link, providing a compressed archive of any
298 # tree. This can potentially generate high traffic if you have large
299 # project.
301 # Value is a list of formats defined in %known_snapshot_formats that
302 # you wish to offer.
303 # To disable system wide have in $GITWEB_CONFIG
304 # $feature{'snapshot'}{'default'} = [];
305 # To have project specific config enable override in $GITWEB_CONFIG
306 # $feature{'snapshot'}{'override'} = 1;
307 # and in project config, a comma-separated list of formats or "none"
308 # to disable. Example: gitweb.snapshot = tbz2,zip;
309 'snapshot' => {
310 'sub' => \&feature_snapshot,
311 'override' => 0,
312 'default' => ['tgz']},
314 # Enable text search, which will list the commits which match author,
315 # committer or commit text to a given string. Enabled by default.
316 # Project specific override is not supported.
317 'search' => {
318 'override' => 0,
319 'default' => [1]},
321 # Enable grep search, which will list the files in currently selected
322 # tree containing the given string. Enabled by default. This can be
323 # potentially CPU-intensive, of course.
325 # To enable system wide have in $GITWEB_CONFIG
326 # $feature{'grep'}{'default'} = [1];
327 # To have project specific config enable override in $GITWEB_CONFIG
328 # $feature{'grep'}{'override'} = 1;
329 # and in project config gitweb.grep = 0|1;
330 'grep' => {
331 'sub' => sub { feature_bool('grep', @_) },
332 'override' => 0,
333 'default' => [1]},
335 # Enable the pickaxe search, which will list the commits that modified
336 # a given string in a file. This can be practical and quite faster
337 # alternative to 'blame', but still potentially CPU-intensive.
339 # To enable system wide have in $GITWEB_CONFIG
340 # $feature{'pickaxe'}{'default'} = [1];
341 # To have project specific config enable override in $GITWEB_CONFIG
342 # $feature{'pickaxe'}{'override'} = 1;
343 # and in project config gitweb.pickaxe = 0|1;
344 'pickaxe' => {
345 'sub' => sub { feature_bool('pickaxe', @_) },
346 'override' => 0,
347 'default' => [1]},
349 # Enable showing size of blobs in a 'tree' view, in a separate
350 # column, similar to what 'ls -l' does. This cost a bit of IO.
352 # To disable system wide have in $GITWEB_CONFIG
353 # $feature{'show-sizes'}{'default'} = [0];
354 # To have project specific config enable override in $GITWEB_CONFIG
355 # $feature{'show-sizes'}{'override'} = 1;
356 # and in project config gitweb.showsizes = 0|1;
357 'show-sizes' => {
358 'sub' => sub { feature_bool('showsizes', @_) },
359 'override' => 0,
360 'default' => [1]},
362 # Make gitweb use an alternative format of the URLs which can be
363 # more readable and natural-looking: project name is embedded
364 # directly in the path and the query string contains other
365 # auxiliary information. All gitweb installations recognize
366 # URL in either format; this configures in which formats gitweb
367 # generates links.
369 # To enable system wide have in $GITWEB_CONFIG
370 # $feature{'pathinfo'}{'default'} = [1];
371 # Project specific override is not supported.
373 # Note that you will need to change the default location of CSS,
374 # favicon, logo and possibly other files to an absolute URL. Also,
375 # if gitweb.cgi serves as your indexfile, you will need to force
376 # $my_uri to contain the script name in your $GITWEB_CONFIG.
377 'pathinfo' => {
378 'override' => 0,
379 'default' => [0]},
381 # Make gitweb consider projects in project root subdirectories
382 # to be forks of existing projects. Given project $projname.git,
383 # projects matching $projname/*.git will not be shown in the main
384 # projects list, instead a '+' mark will be added to $projname
385 # there and a 'forks' view will be enabled for the project, listing
386 # all the forks. If project list is taken from a file, forks have
387 # to be listed after the main project.
389 # To enable system wide have in $GITWEB_CONFIG
390 # $feature{'forks'}{'default'} = [1];
391 # Project specific override is not supported.
392 'forks' => {
393 'override' => 0,
394 'default' => [0]},
396 # Insert custom links to the action bar of all project pages.
397 # This enables you mainly to link to third-party scripts integrating
398 # into gitweb; e.g. git-browser for graphical history representation
399 # or custom web-based repository administration interface.
401 # The 'default' value consists of a list of triplets in the form
402 # (label, link, position) where position is the label after which
403 # to insert the link and link is a format string where %n expands
404 # to the project name, %f to the project path within the filesystem,
405 # %h to the current hash (h gitweb parameter) and %b to the current
406 # hash base (hb gitweb parameter); %% expands to %.
408 # To enable system wide have in $GITWEB_CONFIG e.g.
409 # $feature{'actions'}{'default'} = [('graphiclog',
410 # '/git-browser/by-commit.html?r=%n', 'summary')];
411 # Project specific override is not supported.
412 'actions' => {
413 'override' => 0,
414 'default' => []},
416 # Allow gitweb scan project content tags described in ctags/
417 # of project repository, and display the popular Web 2.0-ish
418 # "tag cloud" near the project list. Note that this is something
419 # COMPLETELY different from the normal Git tags.
421 # gitweb by itself can show existing tags, but it does not handle
422 # tagging itself; you need an external application for that.
423 # For an example script, check Girocco's cgi/tagproj.cgi.
424 # You may want to install the HTML::TagCloud Perl module to get
425 # a pretty tag cloud instead of just a list of tags.
427 # To enable system wide have in $GITWEB_CONFIG
428 # $feature{'ctags'}{'default'} = ['path_to_tag_script'];
429 # Project specific override is not supported.
430 'ctags' => {
431 'override' => 0,
432 'default' => [0]},
434 # The maximum number of patches in a patchset generated in patch
435 # view. Set this to 0 or undef to disable patch view, or to a
436 # negative number to remove any limit.
438 # To disable system wide have in $GITWEB_CONFIG
439 # $feature{'patches'}{'default'} = [0];
440 # To have project specific config enable override in $GITWEB_CONFIG
441 # $feature{'patches'}{'override'} = 1;
442 # and in project config gitweb.patches = 0|n;
443 # where n is the maximum number of patches allowed in a patchset.
444 'patches' => {
445 'sub' => \&feature_patches,
446 'override' => 0,
447 'default' => [16]},
449 # Avatar support. When this feature is enabled, views such as
450 # shortlog or commit will display an avatar associated with
451 # the email of the committer(s) and/or author(s).
453 # Currently available providers are gravatar and picon.
454 # If an unknown provider is specified, the feature is disabled.
456 # Gravatar depends on Digest::MD5.
457 # Picon currently relies on the indiana.edu database.
459 # To enable system wide have in $GITWEB_CONFIG
460 # $feature{'avatar'}{'default'} = ['<provider>'];
461 # where <provider> is either gravatar or picon.
462 # To have project specific config enable override in $GITWEB_CONFIG
463 # $feature{'avatar'}{'override'} = 1;
464 # and in project config gitweb.avatar = <provider>;
465 'avatar' => {
466 'sub' => \&feature_avatar,
467 'override' => 0,
468 'default' => ['']},
470 # Enable displaying how much time and how many git commands
471 # it took to generate and display page. Disabled by default.
472 # Project specific override is not supported.
473 'timed' => {
474 'override' => 0,
475 'default' => [0]},
477 # Enable turning some links into links to actions which require
478 # JavaScript to run (like 'blame_incremental'). Not enabled by
479 # default. Project specific override is currently not supported.
480 'javascript-actions' => {
481 'override' => 0,
482 'default' => [0]},
484 # Syntax highlighting support. This is based on Daniel Svensson's
485 # and Sham Chukoury's work in gitweb-xmms2.git.
486 # It requires the 'highlight' program present in $PATH,
487 # and therefore is disabled by default.
489 # To enable system wide have in $GITWEB_CONFIG
490 # $feature{'highlight'}{'default'} = [1];
492 'highlight' => {
493 'sub' => sub { feature_bool('highlight', @_) },
494 'override' => 0,
495 'default' => [0]},
497 # Enable displaying of remote heads in the heads list
499 # To enable system wide have in $GITWEB_CONFIG
500 # $feature{'remote_heads'}{'default'} = [1];
501 # To have project specific config enable override in $GITWEB_CONFIG
502 # $feature{'remote_heads'}{'override'} = 1;
503 # and in project config gitweb.remote_heads = 0|1;
504 'remote_heads' => {
505 'sub' => sub { feature_bool('remote_heads', @_) },
506 'override' => 0,
507 'default' => [0]},
510 sub gitweb_get_feature {
511 my ($name) = @_;
512 return unless exists $feature{$name};
513 my ($sub, $override, @defaults) = (
514 $feature{$name}{'sub'},
515 $feature{$name}{'override'},
516 @{$feature{$name}{'default'}});
517 # project specific override is possible only if we have project
518 our $git_dir; # global variable, declared later
519 if (!$override || !defined $git_dir) {
520 return @defaults;
522 if (!defined $sub) {
523 warn "feature $name is not overridable";
524 return @defaults;
526 return $sub->(@defaults);
529 # A wrapper to check if a given feature is enabled.
530 # With this, you can say
532 # my $bool_feat = gitweb_check_feature('bool_feat');
533 # gitweb_check_feature('bool_feat') or somecode;
535 # instead of
537 # my ($bool_feat) = gitweb_get_feature('bool_feat');
538 # (gitweb_get_feature('bool_feat'))[0] or somecode;
540 sub gitweb_check_feature {
541 return (gitweb_get_feature(@_))[0];
545 sub feature_bool {
546 my $key = shift;
547 my ($val) = git_get_project_config($key, '--bool');
549 if (!defined $val) {
550 return ($_[0]);
551 } elsif ($val eq 'true') {
552 return (1);
553 } elsif ($val eq 'false') {
554 return (0);
558 sub feature_snapshot {
559 my (@fmts) = @_;
561 my ($val) = git_get_project_config('snapshot');
563 if ($val) {
564 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
567 return @fmts;
570 sub feature_patches {
571 my @val = (git_get_project_config('patches', '--int'));
573 if (@val) {
574 return @val;
577 return ($_[0]);
580 sub feature_avatar {
581 my @val = (git_get_project_config('avatar'));
583 return @val ? @val : @_;
586 # checking HEAD file with -e is fragile if the repository was
587 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
588 # and then pruned.
589 sub check_head_link {
590 my ($dir) = @_;
591 my $headfile = "$dir/HEAD";
592 return ((-e $headfile) ||
593 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
596 sub check_export_ok {
597 my ($dir) = @_;
598 return (check_head_link($dir) &&
599 (!$export_ok || -e "$dir/$export_ok") &&
600 (!$export_auth_hook || $export_auth_hook->($dir)));
603 # process alternate names for backward compatibility
604 # filter out unsupported (unknown) snapshot formats
605 sub filter_snapshot_fmts {
606 my @fmts = @_;
608 @fmts = map {
609 exists $known_snapshot_format_aliases{$_} ?
610 $known_snapshot_format_aliases{$_} : $_} @fmts;
611 @fmts = grep {
612 exists $known_snapshot_formats{$_} &&
613 !$known_snapshot_formats{$_}{'disabled'}} @fmts;
616 our ($GITWEB_CONFIG, $GITWEB_CONFIG_SYSTEM);
617 sub evaluate_gitweb_config {
618 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
619 our $GITWEB_CONFIG_SYSTEM = $ENV{'GITWEB_CONFIG_SYSTEM'} || "++GITWEB_CONFIG_SYSTEM++";
620 # die if there are errors parsing config file
621 if (-e $GITWEB_CONFIG) {
622 do $GITWEB_CONFIG;
623 die $@ if $@;
624 } elsif (-e $GITWEB_CONFIG_SYSTEM) {
625 do $GITWEB_CONFIG_SYSTEM;
626 die $@ if $@;
630 # Get loadavg of system, to compare against $maxload.
631 # Currently it requires '/proc/loadavg' present to get loadavg;
632 # if it is not present it returns 0, which means no load checking.
633 sub get_loadavg {
634 if( -e '/proc/loadavg' ){
635 open my $fd, '<', '/proc/loadavg'
636 or return 0;
637 my @load = split(/\s+/, scalar <$fd>);
638 close $fd;
640 # The first three columns measure CPU and IO utilization of the last one,
641 # five, and 10 minute periods. The fourth column shows the number of
642 # currently running processes and the total number of processes in the m/n
643 # format. The last column displays the last process ID used.
644 return $load[0] || 0;
646 # additional checks for load average should go here for things that don't export
647 # /proc/loadavg
649 return 0;
652 # version of the core git binary
653 our $git_version;
654 sub evaluate_git_version {
655 our $git_version = qx("$GIT" --version) =~ m/git version (.*)$/ ? $1 : "unknown";
656 $number_of_git_cmds++;
659 sub check_loadavg {
660 if (defined $maxload && get_loadavg() > $maxload) {
661 die_error(503, "The load average on the server is too high");
665 # ======================================================================
666 # input validation and dispatch
668 # input parameters can be collected from a variety of sources (presently, CGI
669 # and PATH_INFO), so we define an %input_params hash that collects them all
670 # together during validation: this allows subsequent uses (e.g. href()) to be
671 # agnostic of the parameter origin
673 our %input_params = ();
675 # input parameters are stored with the long parameter name as key. This will
676 # also be used in the href subroutine to convert parameters to their CGI
677 # equivalent, and since the href() usage is the most frequent one, we store
678 # the name -> CGI key mapping here, instead of the reverse.
680 # XXX: Warning: If you touch this, check the search form for updating,
681 # too.
683 our @cgi_param_mapping = (
684 project => "p",
685 action => "a",
686 file_name => "f",
687 file_parent => "fp",
688 hash => "h",
689 hash_parent => "hp",
690 hash_base => "hb",
691 hash_parent_base => "hpb",
692 page => "pg",
693 order => "o",
694 searchtext => "s",
695 searchtype => "st",
696 snapshot_format => "sf",
697 extra_options => "opt",
698 search_use_regexp => "sr",
699 # this must be last entry (for manipulation from JavaScript)
700 javascript => "js"
702 our %cgi_param_mapping = @cgi_param_mapping;
704 # we will also need to know the possible actions, for validation
705 our %actions = (
706 "blame" => \&git_blame,
707 "blame_incremental" => \&git_blame_incremental,
708 "blame_data" => \&git_blame_data,
709 "blobdiff" => \&git_blobdiff,
710 "blobdiff_plain" => \&git_blobdiff_plain,
711 "blob" => \&git_blob,
712 "blob_plain" => \&git_blob_plain,
713 "commitdiff" => \&git_commitdiff,
714 "commitdiff_plain" => \&git_commitdiff_plain,
715 "commit" => \&git_commit,
716 "forks" => \&git_forks,
717 "heads" => \&git_heads,
718 "history" => \&git_history,
719 "log" => \&git_log,
720 "patch" => \&git_patch,
721 "patches" => \&git_patches,
722 "remotes" => \&git_remotes,
723 "rss" => \&git_rss,
724 "atom" => \&git_atom,
725 "search" => \&git_search,
726 "search_help" => \&git_search_help,
727 "shortlog" => \&git_shortlog,
728 "summary" => \&git_summary,
729 "tag" => \&git_tag,
730 "tags" => \&git_tags,
731 "tree" => \&git_tree,
732 "snapshot" => \&git_snapshot,
733 "object" => \&git_object,
734 # those below don't need $project
735 "opml" => \&git_opml,
736 "project_list" => \&git_project_list,
737 "project_index" => \&git_project_index,
740 # finally, we have the hash of allowed extra_options for the commands that
741 # allow them
742 our %allowed_options = (
743 "--no-merges" => [ qw(rss atom log shortlog history) ],
746 # fill %input_params with the CGI parameters. All values except for 'opt'
747 # should be single values, but opt can be an array. We should probably
748 # build an array of parameters that can be multi-valued, but since for the time
749 # being it's only this one, we just single it out
750 sub evaluate_query_params {
751 our $cgi;
753 while (my ($name, $symbol) = each %cgi_param_mapping) {
754 if ($symbol eq 'opt') {
755 $input_params{$name} = [ $cgi->param($symbol) ];
756 } else {
757 $input_params{$name} = $cgi->param($symbol);
762 # now read PATH_INFO and update the parameter list for missing parameters
763 sub evaluate_path_info {
764 return if defined $input_params{'project'};
765 return if !$path_info;
766 $path_info =~ s,^/+,,;
767 return if !$path_info;
769 # find which part of PATH_INFO is project
770 my $project = $path_info;
771 $project =~ s,/+$,,;
772 while ($project && !check_head_link("$projectroot/$project")) {
773 $project =~ s,/*[^/]*$,,;
775 return unless $project;
776 $input_params{'project'} = $project;
778 # do not change any parameters if an action is given using the query string
779 return if $input_params{'action'};
780 $path_info =~ s,^\Q$project\E/*,,;
782 # next, check if we have an action
783 my $action = $path_info;
784 $action =~ s,/.*$,,;
785 if (exists $actions{$action}) {
786 $path_info =~ s,^$action/*,,;
787 $input_params{'action'} = $action;
790 # list of actions that want hash_base instead of hash, but can have no
791 # pathname (f) parameter
792 my @wants_base = (
793 'tree',
794 'history',
797 # we want to catch, among others
798 # [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name]
799 my ($parentrefname, $parentpathname, $refname, $pathname) =
800 ($path_info =~ /^(?:(.+?)(?::(.+))?\.\.)?([^:]+?)?(?::(.+))?$/);
802 # first, analyze the 'current' part
803 if (defined $pathname) {
804 # we got "branch:filename" or "branch:dir/"
805 # we could use git_get_type(branch:pathname), but:
806 # - it needs $git_dir
807 # - it does a git() call
808 # - the convention of terminating directories with a slash
809 # makes it superfluous
810 # - embedding the action in the PATH_INFO would make it even
811 # more superfluous
812 $pathname =~ s,^/+,,;
813 if (!$pathname || substr($pathname, -1) eq "/") {
814 $input_params{'action'} ||= "tree";
815 $pathname =~ s,/$,,;
816 } else {
817 # the default action depends on whether we had parent info
818 # or not
819 if ($parentrefname) {
820 $input_params{'action'} ||= "blobdiff_plain";
821 } else {
822 $input_params{'action'} ||= "blob_plain";
825 $input_params{'hash_base'} ||= $refname;
826 $input_params{'file_name'} ||= $pathname;
827 } elsif (defined $refname) {
828 # we got "branch". In this case we have to choose if we have to
829 # set hash or hash_base.
831 # Most of the actions without a pathname only want hash to be
832 # set, except for the ones specified in @wants_base that want
833 # hash_base instead. It should also be noted that hand-crafted
834 # links having 'history' as an action and no pathname or hash
835 # set will fail, but that happens regardless of PATH_INFO.
836 if (defined $parentrefname) {
837 # if there is parent let the default be 'shortlog' action
838 # (for http://git.example.com/repo.git/A..B links); if there
839 # is no parent, dispatch will detect type of object and set
840 # action appropriately if required (if action is not set)
841 $input_params{'action'} ||= "shortlog";
843 if ($input_params{'action'} &&
844 grep { $_ eq $input_params{'action'} } @wants_base) {
845 $input_params{'hash_base'} ||= $refname;
846 } else {
847 $input_params{'hash'} ||= $refname;
851 # next, handle the 'parent' part, if present
852 if (defined $parentrefname) {
853 # a missing pathspec defaults to the 'current' filename, allowing e.g.
854 # someproject/blobdiff/oldrev..newrev:/filename
855 if ($parentpathname) {
856 $parentpathname =~ s,^/+,,;
857 $parentpathname =~ s,/$,,;
858 $input_params{'file_parent'} ||= $parentpathname;
859 } else {
860 $input_params{'file_parent'} ||= $input_params{'file_name'};
862 # we assume that hash_parent_base is wanted if a path was specified,
863 # or if the action wants hash_base instead of hash
864 if (defined $input_params{'file_parent'} ||
865 grep { $_ eq $input_params{'action'} } @wants_base) {
866 $input_params{'hash_parent_base'} ||= $parentrefname;
867 } else {
868 $input_params{'hash_parent'} ||= $parentrefname;
872 # for the snapshot action, we allow URLs in the form
873 # $project/snapshot/$hash.ext
874 # where .ext determines the snapshot and gets removed from the
875 # passed $refname to provide the $hash.
877 # To be able to tell that $refname includes the format extension, we
878 # require the following two conditions to be satisfied:
879 # - the hash input parameter MUST have been set from the $refname part
880 # of the URL (i.e. they must be equal)
881 # - the snapshot format MUST NOT have been defined already (e.g. from
882 # CGI parameter sf)
883 # It's also useless to try any matching unless $refname has a dot,
884 # so we check for that too
885 if (defined $input_params{'action'} &&
886 $input_params{'action'} eq 'snapshot' &&
887 defined $refname && index($refname, '.') != -1 &&
888 $refname eq $input_params{'hash'} &&
889 !defined $input_params{'snapshot_format'}) {
890 # We loop over the known snapshot formats, checking for
891 # extensions. Allowed extensions are both the defined suffix
892 # (which includes the initial dot already) and the snapshot
893 # format key itself, with a prepended dot
894 while (my ($fmt, $opt) = each %known_snapshot_formats) {
895 my $hash = $refname;
896 unless ($hash =~ s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) {
897 next;
899 my $sfx = $1;
900 # a valid suffix was found, so set the snapshot format
901 # and reset the hash parameter
902 $input_params{'snapshot_format'} = $fmt;
903 $input_params{'hash'} = $hash;
904 # we also set the format suffix to the one requested
905 # in the URL: this way a request for e.g. .tgz returns
906 # a .tgz instead of a .tar.gz
907 $known_snapshot_formats{$fmt}{'suffix'} = $sfx;
908 last;
913 our ($action, $project, $file_name, $file_parent, $hash, $hash_parent, $hash_base,
914 $hash_parent_base, @extra_options, $page, $searchtype, $search_use_regexp,
915 $searchtext, $search_regexp);
916 sub evaluate_and_validate_params {
917 our $action = $input_params{'action'};
918 if (defined $action) {
919 if (!validate_action($action)) {
920 die_error(400, "Invalid action parameter");
924 # parameters which are pathnames
925 our $project = $input_params{'project'};
926 if (defined $project) {
927 if (!validate_project($project)) {
928 undef $project;
929 die_error(404, "No such project");
933 our $file_name = $input_params{'file_name'};
934 if (defined $file_name) {
935 if (!validate_pathname($file_name)) {
936 die_error(400, "Invalid file parameter");
940 our $file_parent = $input_params{'file_parent'};
941 if (defined $file_parent) {
942 if (!validate_pathname($file_parent)) {
943 die_error(400, "Invalid file parent parameter");
947 # parameters which are refnames
948 our $hash = $input_params{'hash'};
949 if (defined $hash) {
950 if (!validate_refname($hash)) {
951 die_error(400, "Invalid hash parameter");
955 our $hash_parent = $input_params{'hash_parent'};
956 if (defined $hash_parent) {
957 if (!validate_refname($hash_parent)) {
958 die_error(400, "Invalid hash parent parameter");
962 our $hash_base = $input_params{'hash_base'};
963 if (defined $hash_base) {
964 if (!validate_refname($hash_base)) {
965 die_error(400, "Invalid hash base parameter");
969 our @extra_options = @{$input_params{'extra_options'}};
970 # @extra_options is always defined, since it can only be (currently) set from
971 # CGI, and $cgi->param() returns the empty array in array context if the param
972 # is not set
973 foreach my $opt (@extra_options) {
974 if (not exists $allowed_options{$opt}) {
975 die_error(400, "Invalid option parameter");
977 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
978 die_error(400, "Invalid option parameter for this action");
982 our $hash_parent_base = $input_params{'hash_parent_base'};
983 if (defined $hash_parent_base) {
984 if (!validate_refname($hash_parent_base)) {
985 die_error(400, "Invalid hash parent base parameter");
989 # other parameters
990 our $page = $input_params{'page'};
991 if (defined $page) {
992 if ($page =~ m/[^0-9]/) {
993 die_error(400, "Invalid page parameter");
997 our $searchtype = $input_params{'searchtype'};
998 if (defined $searchtype) {
999 if ($searchtype =~ m/[^a-z]/) {
1000 die_error(400, "Invalid searchtype parameter");
1004 our $search_use_regexp = $input_params{'search_use_regexp'};
1006 our $searchtext = $input_params{'searchtext'};
1007 our $search_regexp;
1008 if (defined $searchtext) {
1009 if (length($searchtext) < 2) {
1010 die_error(403, "At least two characters are required for search parameter");
1012 $search_regexp = $search_use_regexp ? $searchtext : quotemeta $searchtext;
1016 # path to the current git repository
1017 our $git_dir;
1018 sub evaluate_git_dir {
1019 our $git_dir = "$projectroot/$project" if $project;
1022 our (@snapshot_fmts, $git_avatar);
1023 sub configure_gitweb_features {
1024 # list of supported snapshot formats
1025 our @snapshot_fmts = gitweb_get_feature('snapshot');
1026 @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
1028 # check that the avatar feature is set to a known provider name,
1029 # and for each provider check if the dependencies are satisfied.
1030 # if the provider name is invalid or the dependencies are not met,
1031 # reset $git_avatar to the empty string.
1032 our ($git_avatar) = gitweb_get_feature('avatar');
1033 if ($git_avatar eq 'gravatar') {
1034 $git_avatar = '' unless (eval { require Digest::MD5; 1; });
1035 } elsif ($git_avatar eq 'picon') {
1036 # no dependencies
1037 } else {
1038 $git_avatar = '';
1042 # custom error handler: 'die <message>' is Internal Server Error
1043 sub handle_errors_html {
1044 my $msg = shift; # it is already HTML escaped
1046 # to avoid infinite loop where error occurs in die_error,
1047 # change handler to default handler, disabling handle_errors_html
1048 set_message("Error occured when inside die_error:\n$msg");
1050 # you cannot jump out of die_error when called as error handler;
1051 # the subroutine set via CGI::Carp::set_message is called _after_
1052 # HTTP headers are already written, so it cannot write them itself
1053 die_error(undef, undef, $msg, -error_handler => 1, -no_http_header => 1);
1055 set_message(\&handle_errors_html);
1057 # dispatch
1058 sub dispatch {
1059 if (!defined $action) {
1060 if (defined $hash) {
1061 $action = git_get_type($hash);
1062 } elsif (defined $hash_base && defined $file_name) {
1063 $action = git_get_type("$hash_base:$file_name");
1064 } elsif (defined $project) {
1065 $action = 'summary';
1066 } else {
1067 $action = 'project_list';
1070 if (!defined($actions{$action})) {
1071 die_error(400, "Unknown action");
1073 if ($action !~ m/^(?:opml|project_list|project_index)$/ &&
1074 !$project) {
1075 die_error(400, "Project needed");
1077 $actions{$action}->();
1080 sub reset_timer {
1081 our $t0 = [Time::HiRes::gettimeofday()]
1082 if defined $t0;
1083 our $number_of_git_cmds = 0;
1086 sub run_request {
1087 reset_timer();
1089 evaluate_uri();
1090 evaluate_gitweb_config();
1091 evaluate_git_version();
1092 check_loadavg();
1094 # $projectroot and $projects_list might be set in gitweb config file
1095 $projects_list ||= $projectroot;
1097 evaluate_query_params();
1098 evaluate_path_info();
1099 evaluate_and_validate_params();
1100 evaluate_git_dir();
1102 configure_gitweb_features();
1104 dispatch();
1107 our $is_last_request = sub { 1 };
1108 our ($pre_dispatch_hook, $post_dispatch_hook, $pre_listen_hook);
1109 our $CGI = 'CGI';
1110 our $cgi;
1111 sub configure_as_fcgi {
1112 require CGI::Fast;
1113 our $CGI = 'CGI::Fast';
1115 my $request_number = 0;
1116 # let each child service 100 requests
1117 our $is_last_request = sub { ++$request_number > 100 };
1119 sub evaluate_argv {
1120 my $script_name = $ENV{'SCRIPT_NAME'} || $ENV{'SCRIPT_FILENAME'} || __FILE__;
1121 configure_as_fcgi()
1122 if $script_name =~ /\.fcgi$/;
1124 return unless (@ARGV);
1126 require Getopt::Long;
1127 Getopt::Long::GetOptions(
1128 'fastcgi|fcgi|f' => \&configure_as_fcgi,
1129 'nproc|n=i' => sub {
1130 my ($arg, $val) = @_;
1131 return unless eval { require FCGI::ProcManager; 1; };
1132 my $proc_manager = FCGI::ProcManager->new({
1133 n_processes => $val,
1135 our $pre_listen_hook = sub { $proc_manager->pm_manage() };
1136 our $pre_dispatch_hook = sub { $proc_manager->pm_pre_dispatch() };
1137 our $post_dispatch_hook = sub { $proc_manager->pm_post_dispatch() };
1142 sub run {
1143 evaluate_argv();
1145 $pre_listen_hook->()
1146 if $pre_listen_hook;
1148 REQUEST:
1149 while ($cgi = $CGI->new()) {
1150 $pre_dispatch_hook->()
1151 if $pre_dispatch_hook;
1153 run_request();
1155 $post_dispatch_hook->()
1156 if $post_dispatch_hook;
1158 last REQUEST if ($is_last_request->());
1161 DONE_GITWEB:
1165 run();
1167 if (defined caller) {
1168 # wrapped in a subroutine processing requests,
1169 # e.g. mod_perl with ModPerl::Registry, or PSGI with Plack::App::WrapCGI
1170 return;
1171 } else {
1172 # pure CGI script, serving single request
1173 exit;
1176 ## ======================================================================
1177 ## action links
1179 # possible values of extra options
1180 # -full => 0|1 - use absolute/full URL ($my_uri/$my_url as base)
1181 # -replay => 1 - start from a current view (replay with modifications)
1182 # -path_info => 0|1 - don't use/use path_info URL (if possible)
1183 sub href {
1184 my %params = @_;
1185 # default is to use -absolute url() i.e. $my_uri
1186 my $href = $params{-full} ? $my_url : $my_uri;
1188 $params{'project'} = $project unless exists $params{'project'};
1190 if ($params{-replay}) {
1191 while (my ($name, $symbol) = each %cgi_param_mapping) {
1192 if (!exists $params{$name}) {
1193 $params{$name} = $input_params{$name};
1198 my $use_pathinfo = gitweb_check_feature('pathinfo');
1199 if (defined $params{'project'} &&
1200 (exists $params{-path_info} ? $params{-path_info} : $use_pathinfo)) {
1201 # try to put as many parameters as possible in PATH_INFO:
1202 # - project name
1203 # - action
1204 # - hash_parent or hash_parent_base:/file_parent
1205 # - hash or hash_base:/filename
1206 # - the snapshot_format as an appropriate suffix
1208 # When the script is the root DirectoryIndex for the domain,
1209 # $href here would be something like http://gitweb.example.com/
1210 # Thus, we strip any trailing / from $href, to spare us double
1211 # slashes in the final URL
1212 $href =~ s,/$,,;
1214 # Then add the project name, if present
1215 $href .= "/".esc_url($params{'project'});
1216 delete $params{'project'};
1218 # since we destructively absorb parameters, we keep this
1219 # boolean that remembers if we're handling a snapshot
1220 my $is_snapshot = $params{'action'} eq 'snapshot';
1222 # Summary just uses the project path URL, any other action is
1223 # added to the URL
1224 if (defined $params{'action'}) {
1225 $href .= "/".esc_url($params{'action'}) unless $params{'action'} eq 'summary';
1226 delete $params{'action'};
1229 # Next, we put hash_parent_base:/file_parent..hash_base:/file_name,
1230 # stripping nonexistent or useless pieces
1231 $href .= "/" if ($params{'hash_base'} || $params{'hash_parent_base'}
1232 || $params{'hash_parent'} || $params{'hash'});
1233 if (defined $params{'hash_base'}) {
1234 if (defined $params{'hash_parent_base'}) {
1235 $href .= esc_url($params{'hash_parent_base'});
1236 # skip the file_parent if it's the same as the file_name
1237 if (defined $params{'file_parent'}) {
1238 if (defined $params{'file_name'} && $params{'file_parent'} eq $params{'file_name'}) {
1239 delete $params{'file_parent'};
1240 } elsif ($params{'file_parent'} !~ /\.\./) {
1241 $href .= ":/".esc_url($params{'file_parent'});
1242 delete $params{'file_parent'};
1245 $href .= "..";
1246 delete $params{'hash_parent'};
1247 delete $params{'hash_parent_base'};
1248 } elsif (defined $params{'hash_parent'}) {
1249 $href .= esc_url($params{'hash_parent'}). "..";
1250 delete $params{'hash_parent'};
1253 $href .= esc_url($params{'hash_base'});
1254 if (defined $params{'file_name'} && $params{'file_name'} !~ /\.\./) {
1255 $href .= ":/".esc_url($params{'file_name'});
1256 delete $params{'file_name'};
1258 delete $params{'hash'};
1259 delete $params{'hash_base'};
1260 } elsif (defined $params{'hash'}) {
1261 $href .= esc_url($params{'hash'});
1262 delete $params{'hash'};
1265 # If the action was a snapshot, we can absorb the
1266 # snapshot_format parameter too
1267 if ($is_snapshot) {
1268 my $fmt = $params{'snapshot_format'};
1269 # snapshot_format should always be defined when href()
1270 # is called, but just in case some code forgets, we
1271 # fall back to the default
1272 $fmt ||= $snapshot_fmts[0];
1273 $href .= $known_snapshot_formats{$fmt}{'suffix'};
1274 delete $params{'snapshot_format'};
1278 # now encode the parameters explicitly
1279 my @result = ();
1280 for (my $i = 0; $i < @cgi_param_mapping; $i += 2) {
1281 my ($name, $symbol) = ($cgi_param_mapping[$i], $cgi_param_mapping[$i+1]);
1282 if (defined $params{$name}) {
1283 if (ref($params{$name}) eq "ARRAY") {
1284 foreach my $par (@{$params{$name}}) {
1285 push @result, $symbol . "=" . esc_param($par);
1287 } else {
1288 push @result, $symbol . "=" . esc_param($params{$name});
1292 $href .= "?" . join(';', @result) if scalar @result;
1294 return $href;
1298 ## ======================================================================
1299 ## validation, quoting/unquoting and escaping
1301 sub validate_action {
1302 my $input = shift || return undef;
1303 return undef unless exists $actions{$input};
1304 return $input;
1307 sub validate_project {
1308 my $input = shift || return undef;
1309 if (!validate_pathname($input) ||
1310 !(-d "$projectroot/$input") ||
1311 !check_export_ok("$projectroot/$input") ||
1312 ($strict_export && !project_in_list($input))) {
1313 return undef;
1314 } else {
1315 return $input;
1319 sub validate_pathname {
1320 my $input = shift || return undef;
1322 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
1323 # at the beginning, at the end, and between slashes.
1324 # also this catches doubled slashes
1325 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
1326 return undef;
1328 # no null characters
1329 if ($input =~ m!\0!) {
1330 return undef;
1332 return $input;
1335 sub validate_refname {
1336 my $input = shift || return undef;
1338 # textual hashes are O.K.
1339 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
1340 return $input;
1342 # it must be correct pathname
1343 $input = validate_pathname($input)
1344 or return undef;
1345 # restrictions on ref name according to git-check-ref-format
1346 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
1347 return undef;
1349 return $input;
1352 # decode sequences of octets in utf8 into Perl's internal form,
1353 # which is utf-8 with utf8 flag set if needed. gitweb writes out
1354 # in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
1355 sub to_utf8 {
1356 my $str = shift;
1357 return undef unless defined $str;
1358 if (utf8::valid($str)) {
1359 utf8::decode($str);
1360 return $str;
1361 } else {
1362 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
1366 # quote unsafe chars, but keep the slash, even when it's not
1367 # correct, but quoted slashes look too horrible in bookmarks
1368 sub esc_param {
1369 my $str = shift;
1370 return undef unless defined $str;
1371 $str =~ s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;
1372 $str =~ s/ /\+/g;
1373 return $str;
1376 # quote unsafe chars in whole URL, so some characters cannot be quoted
1377 sub esc_url {
1378 my $str = shift;
1379 return undef unless defined $str;
1380 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&= ]+)/CGI::escape($1)/eg;
1381 $str =~ s/ /\+/g;
1382 return $str;
1385 # replace invalid utf8 character with SUBSTITUTION sequence
1386 sub esc_html {
1387 my $str = shift;
1388 my %opts = @_;
1390 return undef unless defined $str;
1392 $str = to_utf8($str);
1393 $str = $cgi->escapeHTML($str);
1394 if ($opts{'-nbsp'}) {
1395 $str =~ s/ /&nbsp;/g;
1397 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
1398 return $str;
1401 # quote control characters and escape filename to HTML
1402 sub esc_path {
1403 my $str = shift;
1404 my %opts = @_;
1406 return undef unless defined $str;
1408 $str = to_utf8($str);
1409 $str = $cgi->escapeHTML($str);
1410 if ($opts{'-nbsp'}) {
1411 $str =~ s/ /&nbsp;/g;
1413 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
1414 return $str;
1417 # Make control characters "printable", using character escape codes (CEC)
1418 sub quot_cec {
1419 my $cntrl = shift;
1420 my %opts = @_;
1421 my %es = ( # character escape codes, aka escape sequences
1422 "\t" => '\t', # tab (HT)
1423 "\n" => '\n', # line feed (LF)
1424 "\r" => '\r', # carrige return (CR)
1425 "\f" => '\f', # form feed (FF)
1426 "\b" => '\b', # backspace (BS)
1427 "\a" => '\a', # alarm (bell) (BEL)
1428 "\e" => '\e', # escape (ESC)
1429 "\013" => '\v', # vertical tab (VT)
1430 "\000" => '\0', # nul character (NUL)
1432 my $chr = ( (exists $es{$cntrl})
1433 ? $es{$cntrl}
1434 : sprintf('\%2x', ord($cntrl)) );
1435 if ($opts{-nohtml}) {
1436 return $chr;
1437 } else {
1438 return "<span class=\"cntrl\">$chr</span>";
1442 # Alternatively use unicode control pictures codepoints,
1443 # Unicode "printable representation" (PR)
1444 sub quot_upr {
1445 my $cntrl = shift;
1446 my %opts = @_;
1448 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
1449 if ($opts{-nohtml}) {
1450 return $chr;
1451 } else {
1452 return "<span class=\"cntrl\">$chr</span>";
1456 # git may return quoted and escaped filenames
1457 sub unquote {
1458 my $str = shift;
1460 sub unq {
1461 my $seq = shift;
1462 my %es = ( # character escape codes, aka escape sequences
1463 't' => "\t", # tab (HT, TAB)
1464 'n' => "\n", # newline (NL)
1465 'r' => "\r", # return (CR)
1466 'f' => "\f", # form feed (FF)
1467 'b' => "\b", # backspace (BS)
1468 'a' => "\a", # alarm (bell) (BEL)
1469 'e' => "\e", # escape (ESC)
1470 'v' => "\013", # vertical tab (VT)
1473 if ($seq =~ m/^[0-7]{1,3}$/) {
1474 # octal char sequence
1475 return chr(oct($seq));
1476 } elsif (exists $es{$seq}) {
1477 # C escape sequence, aka character escape code
1478 return $es{$seq};
1480 # quoted ordinary character
1481 return $seq;
1484 if ($str =~ m/^"(.*)"$/) {
1485 # needs unquoting
1486 $str = $1;
1487 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
1489 return $str;
1492 # escape tabs (convert tabs to spaces)
1493 sub untabify {
1494 my $line = shift;
1496 while ((my $pos = index($line, "\t")) != -1) {
1497 if (my $count = (8 - ($pos % 8))) {
1498 my $spaces = ' ' x $count;
1499 $line =~ s/\t/$spaces/;
1503 return $line;
1506 sub project_in_list {
1507 my $project = shift;
1508 my @list = git_get_projects_list();
1509 return @list && scalar(grep { $_->{'path'} eq $project } @list);
1512 ## ----------------------------------------------------------------------
1513 ## HTML aware string manipulation
1515 # Try to chop given string on a word boundary between position
1516 # $len and $len+$add_len. If there is no word boundary there,
1517 # chop at $len+$add_len. Do not chop if chopped part plus ellipsis
1518 # (marking chopped part) would be longer than given string.
1519 sub chop_str {
1520 my $str = shift;
1521 my $len = shift;
1522 my $add_len = shift || 10;
1523 my $where = shift || 'right'; # 'left' | 'center' | 'right'
1525 # Make sure perl knows it is utf8 encoded so we don't
1526 # cut in the middle of a utf8 multibyte char.
1527 $str = to_utf8($str);
1529 # allow only $len chars, but don't cut a word if it would fit in $add_len
1530 # if it doesn't fit, cut it if it's still longer than the dots we would add
1531 # remove chopped character entities entirely
1533 # when chopping in the middle, distribute $len into left and right part
1534 # return early if chopping wouldn't make string shorter
1535 if ($where eq 'center') {
1536 return $str if ($len + 5 >= length($str)); # filler is length 5
1537 $len = int($len/2);
1538 } else {
1539 return $str if ($len + 4 >= length($str)); # filler is length 4
1542 # regexps: ending and beginning with word part up to $add_len
1543 my $endre = qr/.{$len}\w{0,$add_len}/;
1544 my $begre = qr/\w{0,$add_len}.{$len}/;
1546 if ($where eq 'left') {
1547 $str =~ m/^(.*?)($begre)$/;
1548 my ($lead, $body) = ($1, $2);
1549 if (length($lead) > 4) {
1550 $lead = " ...";
1552 return "$lead$body";
1554 } elsif ($where eq 'center') {
1555 $str =~ m/^($endre)(.*)$/;
1556 my ($left, $str) = ($1, $2);
1557 $str =~ m/^(.*?)($begre)$/;
1558 my ($mid, $right) = ($1, $2);
1559 if (length($mid) > 5) {
1560 $mid = " ... ";
1562 return "$left$mid$right";
1564 } else {
1565 $str =~ m/^($endre)(.*)$/;
1566 my $body = $1;
1567 my $tail = $2;
1568 if (length($tail) > 4) {
1569 $tail = "... ";
1571 return "$body$tail";
1575 # takes the same arguments as chop_str, but also wraps a <span> around the
1576 # result with a title attribute if it does get chopped. Additionally, the
1577 # string is HTML-escaped.
1578 sub chop_and_escape_str {
1579 my ($str) = @_;
1581 my $chopped = chop_str(@_);
1582 if ($chopped eq $str) {
1583 return esc_html($chopped);
1584 } else {
1585 $str =~ s/[[:cntrl:]]/?/g;
1586 return $cgi->span({-title=>$str}, esc_html($chopped));
1590 ## ----------------------------------------------------------------------
1591 ## functions returning short strings
1593 # CSS class for given age value (in seconds)
1594 sub age_class {
1595 my $age = shift;
1597 if (!defined $age) {
1598 return "noage";
1599 } elsif ($age < 60*60*2) {
1600 return "age0";
1601 } elsif ($age < 60*60*24*2) {
1602 return "age1";
1603 } else {
1604 return "age2";
1608 # convert age in seconds to "nn units ago" string
1609 sub age_string {
1610 my $age = shift;
1611 my $age_str;
1613 if ($age > 60*60*24*365*2) {
1614 $age_str = (int $age/60/60/24/365);
1615 $age_str .= " years ago";
1616 } elsif ($age > 60*60*24*(365/12)*2) {
1617 $age_str = int $age/60/60/24/(365/12);
1618 $age_str .= " months ago";
1619 } elsif ($age > 60*60*24*7*2) {
1620 $age_str = int $age/60/60/24/7;
1621 $age_str .= " weeks ago";
1622 } elsif ($age > 60*60*24*2) {
1623 $age_str = int $age/60/60/24;
1624 $age_str .= " days ago";
1625 } elsif ($age > 60*60*2) {
1626 $age_str = int $age/60/60;
1627 $age_str .= " hours ago";
1628 } elsif ($age > 60*2) {
1629 $age_str = int $age/60;
1630 $age_str .= " min ago";
1631 } elsif ($age > 2) {
1632 $age_str = int $age;
1633 $age_str .= " sec ago";
1634 } else {
1635 $age_str .= " right now";
1637 return $age_str;
1640 use constant {
1641 S_IFINVALID => 0030000,
1642 S_IFGITLINK => 0160000,
1645 # submodule/subproject, a commit object reference
1646 sub S_ISGITLINK {
1647 my $mode = shift;
1649 return (($mode & S_IFMT) == S_IFGITLINK)
1652 # convert file mode in octal to symbolic file mode string
1653 sub mode_str {
1654 my $mode = oct shift;
1656 if (S_ISGITLINK($mode)) {
1657 return 'm---------';
1658 } elsif (S_ISDIR($mode & S_IFMT)) {
1659 return 'drwxr-xr-x';
1660 } elsif (S_ISLNK($mode)) {
1661 return 'lrwxrwxrwx';
1662 } elsif (S_ISREG($mode)) {
1663 # git cares only about the executable bit
1664 if ($mode & S_IXUSR) {
1665 return '-rwxr-xr-x';
1666 } else {
1667 return '-rw-r--r--';
1669 } else {
1670 return '----------';
1674 # convert file mode in octal to file type string
1675 sub file_type {
1676 my $mode = shift;
1678 if ($mode !~ m/^[0-7]+$/) {
1679 return $mode;
1680 } else {
1681 $mode = oct $mode;
1684 if (S_ISGITLINK($mode)) {
1685 return "submodule";
1686 } elsif (S_ISDIR($mode & S_IFMT)) {
1687 return "directory";
1688 } elsif (S_ISLNK($mode)) {
1689 return "symlink";
1690 } elsif (S_ISREG($mode)) {
1691 return "file";
1692 } else {
1693 return "unknown";
1697 # convert file mode in octal to file type description string
1698 sub file_type_long {
1699 my $mode = shift;
1701 if ($mode !~ m/^[0-7]+$/) {
1702 return $mode;
1703 } else {
1704 $mode = oct $mode;
1707 if (S_ISGITLINK($mode)) {
1708 return "submodule";
1709 } elsif (S_ISDIR($mode & S_IFMT)) {
1710 return "directory";
1711 } elsif (S_ISLNK($mode)) {
1712 return "symlink";
1713 } elsif (S_ISREG($mode)) {
1714 if ($mode & S_IXUSR) {
1715 return "executable";
1716 } else {
1717 return "file";
1719 } else {
1720 return "unknown";
1725 ## ----------------------------------------------------------------------
1726 ## functions returning short HTML fragments, or transforming HTML fragments
1727 ## which don't belong to other sections
1729 # format line of commit message.
1730 sub format_log_line_html {
1731 my $line = shift;
1733 $line = esc_html($line, -nbsp=>1);
1734 $line =~ s{\b([0-9a-fA-F]{8,40})\b}{
1735 $cgi->a({-href => href(action=>"object", hash=>$1),
1736 -class => "text"}, $1);
1737 }eg;
1739 return $line;
1742 # format marker of refs pointing to given object
1744 # the destination action is chosen based on object type and current context:
1745 # - for annotated tags, we choose the tag view unless it's the current view
1746 # already, in which case we go to shortlog view
1747 # - for other refs, we keep the current view if we're in history, shortlog or
1748 # log view, and select shortlog otherwise
1749 sub format_ref_marker {
1750 my ($refs, $id) = @_;
1751 my $markers = '';
1753 if (defined $refs->{$id}) {
1754 foreach my $ref (@{$refs->{$id}}) {
1755 # this code exploits the fact that non-lightweight tags are the
1756 # only indirect objects, and that they are the only objects for which
1757 # we want to use tag instead of shortlog as action
1758 my ($type, $name) = qw();
1759 my $indirect = ($ref =~ s/\^\{\}$//);
1760 # e.g. tags/v2.6.11 or heads/next
1761 if ($ref =~ m!^(.*?)s?/(.*)$!) {
1762 $type = $1;
1763 $name = $2;
1764 } else {
1765 $type = "ref";
1766 $name = $ref;
1769 my $class = $type;
1770 $class .= " indirect" if $indirect;
1772 my $dest_action = "shortlog";
1774 if ($indirect) {
1775 $dest_action = "tag" unless $action eq "tag";
1776 } elsif ($action =~ /^(history|(short)?log)$/) {
1777 $dest_action = $action;
1780 my $dest = "";
1781 $dest .= "refs/" unless $ref =~ m!^refs/!;
1782 $dest .= $ref;
1784 my $link = $cgi->a({
1785 -href => href(
1786 action=>$dest_action,
1787 hash=>$dest
1788 )}, $name);
1790 $markers .= " <span class=\"$class\" title=\"$ref\">" .
1791 $link . "</span>";
1795 if ($markers) {
1796 return ' <span class="refs">'. $markers . '</span>';
1797 } else {
1798 return "";
1802 # format, perhaps shortened and with markers, title line
1803 sub format_subject_html {
1804 my ($long, $short, $href, $extra) = @_;
1805 $extra = '' unless defined($extra);
1807 if (length($short) < length($long)) {
1808 $long =~ s/[[:cntrl:]]/?/g;
1809 return $cgi->a({-href => $href, -class => "list subject",
1810 -title => to_utf8($long)},
1811 esc_html($short)) . $extra;
1812 } else {
1813 return $cgi->a({-href => $href, -class => "list subject"},
1814 esc_html($long)) . $extra;
1818 # Rather than recomputing the url for an email multiple times, we cache it
1819 # after the first hit. This gives a visible benefit in views where the avatar
1820 # for the same email is used repeatedly (e.g. shortlog).
1821 # The cache is shared by all avatar engines (currently gravatar only), which
1822 # are free to use it as preferred. Since only one avatar engine is used for any
1823 # given page, there's no risk for cache conflicts.
1824 our %avatar_cache = ();
1826 # Compute the picon url for a given email, by using the picon search service over at
1827 # http://www.cs.indiana.edu/picons/search.html
1828 sub picon_url {
1829 my $email = lc shift;
1830 if (!$avatar_cache{$email}) {
1831 my ($user, $domain) = split('@', $email);
1832 $avatar_cache{$email} =
1833 "http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/" .
1834 "$domain/$user/" .
1835 "users+domains+unknown/up/single";
1837 return $avatar_cache{$email};
1840 # Compute the gravatar url for a given email, if it's not in the cache already.
1841 # Gravatar stores only the part of the URL before the size, since that's the
1842 # one computationally more expensive. This also allows reuse of the cache for
1843 # different sizes (for this particular engine).
1844 sub gravatar_url {
1845 my $email = lc shift;
1846 my $size = shift;
1847 $avatar_cache{$email} ||=
1848 "http://www.gravatar.com/avatar/" .
1849 Digest::MD5::md5_hex($email) . "?s=";
1850 return $avatar_cache{$email} . $size;
1853 # Insert an avatar for the given $email at the given $size if the feature
1854 # is enabled.
1855 sub git_get_avatar {
1856 my ($email, %opts) = @_;
1857 my $pre_white = ($opts{-pad_before} ? "&nbsp;" : "");
1858 my $post_white = ($opts{-pad_after} ? "&nbsp;" : "");
1859 $opts{-size} ||= 'default';
1860 my $size = $avatar_size{$opts{-size}} || $avatar_size{'default'};
1861 my $url = "";
1862 if ($git_avatar eq 'gravatar') {
1863 $url = gravatar_url($email, $size);
1864 } elsif ($git_avatar eq 'picon') {
1865 $url = picon_url($email);
1867 # Other providers can be added by extending the if chain, defining $url
1868 # as needed. If no variant puts something in $url, we assume avatars
1869 # are completely disabled/unavailable.
1870 if ($url) {
1871 return $pre_white .
1872 "<img width=\"$size\" " .
1873 "class=\"avatar\" " .
1874 "src=\"$url\" " .
1875 "alt=\"\" " .
1876 "/>" . $post_white;
1877 } else {
1878 return "";
1882 sub format_search_author {
1883 my ($author, $searchtype, $displaytext) = @_;
1884 my $have_search = gitweb_check_feature('search');
1886 if ($have_search) {
1887 my $performed = "";
1888 if ($searchtype eq 'author') {
1889 $performed = "authored";
1890 } elsif ($searchtype eq 'committer') {
1891 $performed = "committed";
1894 return $cgi->a({-href => href(action=>"search", hash=>$hash,
1895 searchtext=>$author,
1896 searchtype=>$searchtype), class=>"list",
1897 title=>"Search for commits $performed by $author"},
1898 $displaytext);
1900 } else {
1901 return $displaytext;
1905 # format the author name of the given commit with the given tag
1906 # the author name is chopped and escaped according to the other
1907 # optional parameters (see chop_str).
1908 sub format_author_html {
1909 my $tag = shift;
1910 my $co = shift;
1911 my $author = chop_and_escape_str($co->{'author_name'}, @_);
1912 return "<$tag class=\"author\">" .
1913 format_search_author($co->{'author_name'}, "author",
1914 git_get_avatar($co->{'author_email'}, -pad_after => 1) .
1915 $author) .
1916 "</$tag>";
1919 # format git diff header line, i.e. "diff --(git|combined|cc) ..."
1920 sub format_git_diff_header_line {
1921 my $line = shift;
1922 my $diffinfo = shift;
1923 my ($from, $to) = @_;
1925 if ($diffinfo->{'nparents'}) {
1926 # combined diff
1927 $line =~ s!^(diff (.*?) )"?.*$!$1!;
1928 if ($to->{'href'}) {
1929 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1930 esc_path($to->{'file'}));
1931 } else { # file was deleted (no href)
1932 $line .= esc_path($to->{'file'});
1934 } else {
1935 # "ordinary" diff
1936 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
1937 if ($from->{'href'}) {
1938 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
1939 'a/' . esc_path($from->{'file'}));
1940 } else { # file was added (no href)
1941 $line .= 'a/' . esc_path($from->{'file'});
1943 $line .= ' ';
1944 if ($to->{'href'}) {
1945 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1946 'b/' . esc_path($to->{'file'}));
1947 } else { # file was deleted
1948 $line .= 'b/' . esc_path($to->{'file'});
1952 return "<div class=\"diff header\">$line</div>\n";
1955 # format extended diff header line, before patch itself
1956 sub format_extended_diff_header_line {
1957 my $line = shift;
1958 my $diffinfo = shift;
1959 my ($from, $to) = @_;
1961 # match <path>
1962 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
1963 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1964 esc_path($from->{'file'}));
1966 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
1967 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1968 esc_path($to->{'file'}));
1970 # match single <mode>
1971 if ($line =~ m/\s(\d{6})$/) {
1972 $line .= '<span class="info"> (' .
1973 file_type_long($1) .
1974 ')</span>';
1976 # match <hash>
1977 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
1978 # can match only for combined diff
1979 $line = 'index ';
1980 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1981 if ($from->{'href'}[$i]) {
1982 $line .= $cgi->a({-href=>$from->{'href'}[$i],
1983 -class=>"hash"},
1984 substr($diffinfo->{'from_id'}[$i],0,7));
1985 } else {
1986 $line .= '0' x 7;
1988 # separator
1989 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
1991 $line .= '..';
1992 if ($to->{'href'}) {
1993 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1994 substr($diffinfo->{'to_id'},0,7));
1995 } else {
1996 $line .= '0' x 7;
1999 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
2000 # can match only for ordinary diff
2001 my ($from_link, $to_link);
2002 if ($from->{'href'}) {
2003 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
2004 substr($diffinfo->{'from_id'},0,7));
2005 } else {
2006 $from_link = '0' x 7;
2008 if ($to->{'href'}) {
2009 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
2010 substr($diffinfo->{'to_id'},0,7));
2011 } else {
2012 $to_link = '0' x 7;
2014 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
2015 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
2018 return $line . "<br/>\n";
2021 # format from-file/to-file diff header
2022 sub format_diff_from_to_header {
2023 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
2024 my $line;
2025 my $result = '';
2027 $line = $from_line;
2028 #assert($line =~ m/^---/) if DEBUG;
2029 # no extra formatting for "^--- /dev/null"
2030 if (! $diffinfo->{'nparents'}) {
2031 # ordinary (single parent) diff
2032 if ($line =~ m!^--- "?a/!) {
2033 if ($from->{'href'}) {
2034 $line = '--- a/' .
2035 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
2036 esc_path($from->{'file'}));
2037 } else {
2038 $line = '--- a/' .
2039 esc_path($from->{'file'});
2042 $result .= qq!<div class="diff from_file">$line</div>\n!;
2044 } else {
2045 # combined diff (merge commit)
2046 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2047 if ($from->{'href'}[$i]) {
2048 $line = '--- ' .
2049 $cgi->a({-href=>href(action=>"blobdiff",
2050 hash_parent=>$diffinfo->{'from_id'}[$i],
2051 hash_parent_base=>$parents[$i],
2052 file_parent=>$from->{'file'}[$i],
2053 hash=>$diffinfo->{'to_id'},
2054 hash_base=>$hash,
2055 file_name=>$to->{'file'}),
2056 -class=>"path",
2057 -title=>"diff" . ($i+1)},
2058 $i+1) .
2059 '/' .
2060 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
2061 esc_path($from->{'file'}[$i]));
2062 } else {
2063 $line = '--- /dev/null';
2065 $result .= qq!<div class="diff from_file">$line</div>\n!;
2069 $line = $to_line;
2070 #assert($line =~ m/^\+\+\+/) if DEBUG;
2071 # no extra formatting for "^+++ /dev/null"
2072 if ($line =~ m!^\+\+\+ "?b/!) {
2073 if ($to->{'href'}) {
2074 $line = '+++ b/' .
2075 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
2076 esc_path($to->{'file'}));
2077 } else {
2078 $line = '+++ b/' .
2079 esc_path($to->{'file'});
2082 $result .= qq!<div class="diff to_file">$line</div>\n!;
2084 return $result;
2087 # create note for patch simplified by combined diff
2088 sub format_diff_cc_simplified {
2089 my ($diffinfo, @parents) = @_;
2090 my $result = '';
2092 $result .= "<div class=\"diff header\">" .
2093 "diff --cc ";
2094 if (!is_deleted($diffinfo)) {
2095 $result .= $cgi->a({-href => href(action=>"blob",
2096 hash_base=>$hash,
2097 hash=>$diffinfo->{'to_id'},
2098 file_name=>$diffinfo->{'to_file'}),
2099 -class => "path"},
2100 esc_path($diffinfo->{'to_file'}));
2101 } else {
2102 $result .= esc_path($diffinfo->{'to_file'});
2104 $result .= "</div>\n" . # class="diff header"
2105 "<div class=\"diff nodifferences\">" .
2106 "Simple merge" .
2107 "</div>\n"; # class="diff nodifferences"
2109 return $result;
2112 # format patch (diff) line (not to be used for diff headers)
2113 sub format_diff_line {
2114 my $line = shift;
2115 my ($from, $to) = @_;
2116 my $diff_class = "";
2118 chomp $line;
2120 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
2121 # combined diff
2122 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
2123 if ($line =~ m/^\@{3}/) {
2124 $diff_class = " chunk_header";
2125 } elsif ($line =~ m/^\\/) {
2126 $diff_class = " incomplete";
2127 } elsif ($prefix =~ tr/+/+/) {
2128 $diff_class = " add";
2129 } elsif ($prefix =~ tr/-/-/) {
2130 $diff_class = " rem";
2132 } else {
2133 # assume ordinary diff
2134 my $char = substr($line, 0, 1);
2135 if ($char eq '+') {
2136 $diff_class = " add";
2137 } elsif ($char eq '-') {
2138 $diff_class = " rem";
2139 } elsif ($char eq '@') {
2140 $diff_class = " chunk_header";
2141 } elsif ($char eq "\\") {
2142 $diff_class = " incomplete";
2145 $line = untabify($line);
2146 if ($from && $to && $line =~ m/^\@{2} /) {
2147 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
2148 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
2150 $from_lines = 0 unless defined $from_lines;
2151 $to_lines = 0 unless defined $to_lines;
2153 if ($from->{'href'}) {
2154 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
2155 -class=>"list"}, $from_text);
2157 if ($to->{'href'}) {
2158 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
2159 -class=>"list"}, $to_text);
2161 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
2162 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
2163 return "<div class=\"diff$diff_class\">$line</div>\n";
2164 } elsif ($from && $to && $line =~ m/^\@{3}/) {
2165 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
2166 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
2168 @from_text = split(' ', $ranges);
2169 for (my $i = 0; $i < @from_text; ++$i) {
2170 ($from_start[$i], $from_nlines[$i]) =
2171 (split(',', substr($from_text[$i], 1)), 0);
2174 $to_text = pop @from_text;
2175 $to_start = pop @from_start;
2176 $to_nlines = pop @from_nlines;
2178 $line = "<span class=\"chunk_info\">$prefix ";
2179 for (my $i = 0; $i < @from_text; ++$i) {
2180 if ($from->{'href'}[$i]) {
2181 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
2182 -class=>"list"}, $from_text[$i]);
2183 } else {
2184 $line .= $from_text[$i];
2186 $line .= " ";
2188 if ($to->{'href'}) {
2189 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
2190 -class=>"list"}, $to_text);
2191 } else {
2192 $line .= $to_text;
2194 $line .= " $prefix</span>" .
2195 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
2196 return "<div class=\"diff$diff_class\">$line</div>\n";
2198 return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
2201 # Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
2202 # linked. Pass the hash of the tree/commit to snapshot.
2203 sub format_snapshot_links {
2204 my ($hash) = @_;
2205 my $num_fmts = @snapshot_fmts;
2206 if ($num_fmts > 1) {
2207 # A parenthesized list of links bearing format names.
2208 # e.g. "snapshot (_tar.gz_ _zip_)"
2209 return "snapshot (" . join(' ', map
2210 $cgi->a({
2211 -href => href(
2212 action=>"snapshot",
2213 hash=>$hash,
2214 snapshot_format=>$_
2216 }, $known_snapshot_formats{$_}{'display'})
2217 , @snapshot_fmts) . ")";
2218 } elsif ($num_fmts == 1) {
2219 # A single "snapshot" link whose tooltip bears the format name.
2220 # i.e. "_snapshot_"
2221 my ($fmt) = @snapshot_fmts;
2222 return
2223 $cgi->a({
2224 -href => href(
2225 action=>"snapshot",
2226 hash=>$hash,
2227 snapshot_format=>$fmt
2229 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
2230 }, "snapshot");
2231 } else { # $num_fmts == 0
2232 return undef;
2236 ## ......................................................................
2237 ## functions returning values to be passed, perhaps after some
2238 ## transformation, to other functions; e.g. returning arguments to href()
2240 # returns hash to be passed to href to generate gitweb URL
2241 # in -title key it returns description of link
2242 sub get_feed_info {
2243 my $format = shift || 'Atom';
2244 my %res = (action => lc($format));
2246 # feed links are possible only for project views
2247 return unless (defined $project);
2248 # some views should link to OPML, or to generic project feed,
2249 # or don't have specific feed yet (so they should use generic)
2250 return if ($action =~ /^(?:tags|heads|forks|tag|search)$/x);
2252 my $branch;
2253 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
2254 # from tag links; this also makes possible to detect branch links
2255 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
2256 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
2257 $branch = $1;
2259 # find log type for feed description (title)
2260 my $type = 'log';
2261 if (defined $file_name) {
2262 $type = "history of $file_name";
2263 $type .= "/" if ($action eq 'tree');
2264 $type .= " on '$branch'" if (defined $branch);
2265 } else {
2266 $type = "log of $branch" if (defined $branch);
2269 $res{-title} = $type;
2270 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
2271 $res{'file_name'} = $file_name;
2273 return %res;
2276 ## ----------------------------------------------------------------------
2277 ## git utility subroutines, invoking git commands
2279 # returns path to the core git executable and the --git-dir parameter as list
2280 sub git_cmd {
2281 $number_of_git_cmds++;
2282 return $GIT, '--git-dir='.$git_dir;
2285 # quote the given arguments for passing them to the shell
2286 # quote_command("command", "arg 1", "arg with ' and ! characters")
2287 # => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
2288 # Try to avoid using this function wherever possible.
2289 sub quote_command {
2290 return join(' ',
2291 map { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ );
2294 # get HEAD ref of given project as hash
2295 sub git_get_head_hash {
2296 return git_get_full_hash(shift, 'HEAD');
2299 sub git_get_full_hash {
2300 return git_get_hash(@_);
2303 sub git_get_short_hash {
2304 return git_get_hash(@_, '--short=7');
2307 sub git_get_hash {
2308 my ($project, $hash, @options) = @_;
2309 my $o_git_dir = $git_dir;
2310 my $retval = undef;
2311 $git_dir = "$projectroot/$project";
2312 if (open my $fd, '-|', git_cmd(), 'rev-parse',
2313 '--verify', '-q', @options, $hash) {
2314 $retval = <$fd>;
2315 chomp $retval if defined $retval;
2316 close $fd;
2318 if (defined $o_git_dir) {
2319 $git_dir = $o_git_dir;
2321 return $retval;
2324 # get type of given object
2325 sub git_get_type {
2326 my $hash = shift;
2328 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
2329 my $type = <$fd>;
2330 close $fd or return;
2331 chomp $type;
2332 return $type;
2335 # repository configuration
2336 our $config_file = '';
2337 our %config;
2339 # store multiple values for single key as anonymous array reference
2340 # single values stored directly in the hash, not as [ <value> ]
2341 sub hash_set_multi {
2342 my ($hash, $key, $value) = @_;
2344 if (!exists $hash->{$key}) {
2345 $hash->{$key} = $value;
2346 } elsif (!ref $hash->{$key}) {
2347 $hash->{$key} = [ $hash->{$key}, $value ];
2348 } else {
2349 push @{$hash->{$key}}, $value;
2353 # return hash of git project configuration
2354 # optionally limited to some section, e.g. 'gitweb'
2355 sub git_parse_project_config {
2356 my $section_regexp = shift;
2357 my %config;
2359 local $/ = "\0";
2361 open my $fh, "-|", git_cmd(), "config", '-z', '-l',
2362 or return;
2364 while (my $keyval = <$fh>) {
2365 chomp $keyval;
2366 my ($key, $value) = split(/\n/, $keyval, 2);
2368 hash_set_multi(\%config, $key, $value)
2369 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
2371 close $fh;
2373 return %config;
2376 # convert config value to boolean: 'true' or 'false'
2377 # no value, number > 0, 'true' and 'yes' values are true
2378 # rest of values are treated as false (never as error)
2379 sub config_to_bool {
2380 my $val = shift;
2382 return 1 if !defined $val; # section.key
2384 # strip leading and trailing whitespace
2385 $val =~ s/^\s+//;
2386 $val =~ s/\s+$//;
2388 return (($val =~ /^\d+$/ && $val) || # section.key = 1
2389 ($val =~ /^(?:true|yes)$/i)); # section.key = true
2392 # convert config value to simple decimal number
2393 # an optional value suffix of 'k', 'm', or 'g' will cause the value
2394 # to be multiplied by 1024, 1048576, or 1073741824
2395 sub config_to_int {
2396 my $val = shift;
2398 # strip leading and trailing whitespace
2399 $val =~ s/^\s+//;
2400 $val =~ s/\s+$//;
2402 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
2403 $unit = lc($unit);
2404 # unknown unit is treated as 1
2405 return $num * ($unit eq 'g' ? 1073741824 :
2406 $unit eq 'm' ? 1048576 :
2407 $unit eq 'k' ? 1024 : 1);
2409 return $val;
2412 # convert config value to array reference, if needed
2413 sub config_to_multi {
2414 my $val = shift;
2416 return ref($val) ? $val : (defined($val) ? [ $val ] : []);
2419 sub git_get_project_config {
2420 my ($key, $type) = @_;
2422 return unless defined $git_dir;
2424 # key sanity check
2425 return unless ($key);
2426 $key =~ s/^gitweb\.//;
2427 return if ($key =~ m/\W/);
2429 # type sanity check
2430 if (defined $type) {
2431 $type =~ s/^--//;
2432 $type = undef
2433 unless ($type eq 'bool' || $type eq 'int');
2436 # get config
2437 if (!defined $config_file ||
2438 $config_file ne "$git_dir/config") {
2439 %config = git_parse_project_config('gitweb');
2440 $config_file = "$git_dir/config";
2443 # check if config variable (key) exists
2444 return unless exists $config{"gitweb.$key"};
2446 # ensure given type
2447 if (!defined $type) {
2448 return $config{"gitweb.$key"};
2449 } elsif ($type eq 'bool') {
2450 # backward compatibility: 'git config --bool' returns true/false
2451 return config_to_bool($config{"gitweb.$key"}) ? 'true' : 'false';
2452 } elsif ($type eq 'int') {
2453 return config_to_int($config{"gitweb.$key"});
2455 return $config{"gitweb.$key"};
2458 # get hash of given path at given ref
2459 sub git_get_hash_by_path {
2460 my $base = shift;
2461 my $path = shift || return undef;
2462 my $type = shift;
2464 $path =~ s,/+$,,;
2466 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
2467 or die_error(500, "Open git-ls-tree failed");
2468 my $line = <$fd>;
2469 close $fd or return undef;
2471 if (!defined $line) {
2472 # there is no tree or hash given by $path at $base
2473 return undef;
2476 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2477 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
2478 if (defined $type && $type ne $2) {
2479 # type doesn't match
2480 return undef;
2482 return $3;
2485 # get path of entry with given hash at given tree-ish (ref)
2486 # used to get 'from' filename for combined diff (merge commit) for renames
2487 sub git_get_path_by_hash {
2488 my $base = shift || return;
2489 my $hash = shift || return;
2491 local $/ = "\0";
2493 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
2494 or return undef;
2495 while (my $line = <$fd>) {
2496 chomp $line;
2498 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
2499 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
2500 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
2501 close $fd;
2502 return $1;
2505 close $fd;
2506 return undef;
2509 ## ......................................................................
2510 ## git utility functions, directly accessing git repository
2512 sub git_get_project_description {
2513 my $path = shift;
2515 $git_dir = "$projectroot/$path";
2516 open my $fd, '<', "$git_dir/description"
2517 or return git_get_project_config('description');
2518 my $descr = <$fd>;
2519 close $fd;
2520 if (defined $descr) {
2521 chomp $descr;
2523 return $descr;
2526 sub git_get_project_ctags {
2527 my $path = shift;
2528 my $ctags = {};
2530 $git_dir = "$projectroot/$path";
2531 opendir my $dh, "$git_dir/ctags"
2532 or return $ctags;
2533 foreach (grep { -f $_ } map { "$git_dir/ctags/$_" } readdir($dh)) {
2534 open my $ct, '<', $_ or next;
2535 my $val = <$ct>;
2536 chomp $val;
2537 close $ct;
2538 my $ctag = $_; $ctag =~ s#.*/##;
2539 $ctags->{$ctag} = $val;
2541 closedir $dh;
2542 $ctags;
2545 sub git_populate_project_tagcloud {
2546 my $ctags = shift;
2548 # First, merge different-cased tags; tags vote on casing
2549 my %ctags_lc;
2550 foreach (keys %$ctags) {
2551 $ctags_lc{lc $_}->{count} += $ctags->{$_};
2552 if (not $ctags_lc{lc $_}->{topcount}
2553 or $ctags_lc{lc $_}->{topcount} < $ctags->{$_}) {
2554 $ctags_lc{lc $_}->{topcount} = $ctags->{$_};
2555 $ctags_lc{lc $_}->{topname} = $_;
2559 my $cloud;
2560 if (eval { require HTML::TagCloud; 1; }) {
2561 $cloud = HTML::TagCloud->new;
2562 foreach (sort keys %ctags_lc) {
2563 # Pad the title with spaces so that the cloud looks
2564 # less crammed.
2565 my $title = $ctags_lc{$_}->{topname};
2566 $title =~ s/ /&nbsp;/g;
2567 $title =~ s/^/&nbsp;/g;
2568 $title =~ s/$/&nbsp;/g;
2569 $cloud->add($title, $home_link."?by_tag=".$_, $ctags_lc{$_}->{count});
2571 } else {
2572 $cloud = \%ctags_lc;
2574 $cloud;
2577 sub git_show_project_tagcloud {
2578 my ($cloud, $count) = @_;
2579 print STDERR ref($cloud)."..\n";
2580 if (ref $cloud eq 'HTML::TagCloud') {
2581 return $cloud->html_and_css($count);
2582 } else {
2583 my @tags = sort { $cloud->{$a}->{count} <=> $cloud->{$b}->{count} } keys %$cloud;
2584 return '<p align="center">' . join (', ', map {
2585 "<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"
2586 } splice(@tags, 0, $count)) . '</p>';
2590 sub git_get_project_url_list {
2591 my $path = shift;
2593 $git_dir = "$projectroot/$path";
2594 open my $fd, '<', "$git_dir/cloneurl"
2595 or return wantarray ?
2596 @{ config_to_multi(git_get_project_config('url')) } :
2597 config_to_multi(git_get_project_config('url'));
2598 my @git_project_url_list = map { chomp; $_ } <$fd>;
2599 close $fd;
2601 return wantarray ? @git_project_url_list : \@git_project_url_list;
2604 sub git_get_projects_list {
2605 my ($filter) = @_;
2606 my @list;
2608 $filter ||= '';
2609 $filter =~ s/\.git$//;
2611 my $check_forks = gitweb_check_feature('forks');
2613 if (-d $projects_list) {
2614 # search in directory
2615 my $dir = $projects_list . ($filter ? "/$filter" : '');
2616 # remove the trailing "/"
2617 $dir =~ s!/+$!!;
2618 my $pfxlen = length("$dir");
2619 my $pfxdepth = ($dir =~ tr!/!!);
2621 File::Find::find({
2622 follow_fast => 1, # follow symbolic links
2623 follow_skip => 2, # ignore duplicates
2624 dangling_symlinks => 0, # ignore dangling symlinks, silently
2625 wanted => sub {
2626 # global variables
2627 our $project_maxdepth;
2628 our $projectroot;
2629 # skip project-list toplevel, if we get it.
2630 return if (m!^[/.]$!);
2631 # only directories can be git repositories
2632 return unless (-d $_);
2633 # don't traverse too deep (Find is super slow on os x)
2634 if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
2635 $File::Find::prune = 1;
2636 return;
2639 my $subdir = substr($File::Find::name, $pfxlen + 1);
2640 # we check related file in $projectroot
2641 my $path = ($filter ? "$filter/" : '') . $subdir;
2642 if (check_export_ok("$projectroot/$path")) {
2643 push @list, { path => $path };
2644 $File::Find::prune = 1;
2647 }, "$dir");
2649 } elsif (-f $projects_list) {
2650 # read from file(url-encoded):
2651 # 'git%2Fgit.git Linus+Torvalds'
2652 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2653 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2654 my %paths;
2655 open my $fd, '<', $projects_list or return;
2656 PROJECT:
2657 while (my $line = <$fd>) {
2658 chomp $line;
2659 my ($path, $owner) = split ' ', $line;
2660 $path = unescape($path);
2661 $owner = unescape($owner);
2662 if (!defined $path) {
2663 next;
2665 if ($filter ne '') {
2666 # looking for forks;
2667 my $pfx = substr($path, 0, length($filter));
2668 if ($pfx ne $filter) {
2669 next PROJECT;
2671 my $sfx = substr($path, length($filter));
2672 if ($sfx !~ /^\/.*\.git$/) {
2673 next PROJECT;
2675 } elsif ($check_forks) {
2676 PATH:
2677 foreach my $filter (keys %paths) {
2678 # looking for forks;
2679 my $pfx = substr($path, 0, length($filter));
2680 if ($pfx ne $filter) {
2681 next PATH;
2683 my $sfx = substr($path, length($filter));
2684 if ($sfx !~ /^\/.*\.git$/) {
2685 next PATH;
2687 # is a fork, don't include it in
2688 # the list
2689 next PROJECT;
2692 if (check_export_ok("$projectroot/$path")) {
2693 my $pr = {
2694 path => $path,
2695 owner => to_utf8($owner),
2697 push @list, $pr;
2698 (my $forks_path = $path) =~ s/\.git$//;
2699 $paths{$forks_path}++;
2702 close $fd;
2704 return @list;
2707 our $gitweb_project_owner = undef;
2708 sub git_get_project_list_from_file {
2710 return if (defined $gitweb_project_owner);
2712 $gitweb_project_owner = {};
2713 # read from file (url-encoded):
2714 # 'git%2Fgit.git Linus+Torvalds'
2715 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2716 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2717 if (-f $projects_list) {
2718 open(my $fd, '<', $projects_list);
2719 while (my $line = <$fd>) {
2720 chomp $line;
2721 my ($pr, $ow) = split ' ', $line;
2722 $pr = unescape($pr);
2723 $ow = unescape($ow);
2724 $gitweb_project_owner->{$pr} = to_utf8($ow);
2726 close $fd;
2730 sub git_get_project_owner {
2731 my $project = shift;
2732 my $owner;
2734 return undef unless $project;
2735 $git_dir = "$projectroot/$project";
2737 if (!defined $gitweb_project_owner) {
2738 git_get_project_list_from_file();
2741 if (exists $gitweb_project_owner->{$project}) {
2742 $owner = $gitweb_project_owner->{$project};
2744 if (!defined $owner){
2745 $owner = git_get_project_config('owner');
2747 if (!defined $owner) {
2748 $owner = get_file_owner("$git_dir");
2751 return $owner;
2754 sub git_get_last_activity {
2755 my ($path) = @_;
2756 my $fd;
2758 $git_dir = "$projectroot/$path";
2759 open($fd, "-|", git_cmd(), 'for-each-ref',
2760 '--format=%(committer)',
2761 '--sort=-committerdate',
2762 '--count=1',
2763 'refs/heads') or return;
2764 my $most_recent = <$fd>;
2765 close $fd or return;
2766 if (defined $most_recent &&
2767 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
2768 my $timestamp = $1;
2769 my $age = time - $timestamp;
2770 return ($age, age_string($age));
2772 return (undef, undef);
2775 # Implementation note: when a single remote is wanted, we cannot use 'git
2776 # remote show -n' because that command always work (assuming it's a remote URL
2777 # if it's not defined), and we cannot use 'git remote show' because that would
2778 # try to make a network roundtrip. So the only way to find if that particular
2779 # remote is defined is to walk the list provided by 'git remote -v' and stop if
2780 # and when we find what we want.
2781 sub git_get_remotes_list {
2782 my $wanted = shift;
2783 my %remotes = ();
2785 open my $fd, '-|' , git_cmd(), 'remote', '-v';
2786 return unless $fd;
2787 while (my $remote = <$fd>) {
2788 chomp $remote;
2789 $remote =~ s!\t(.*?)\s+\((\w+)\)$!!;
2790 next if $wanted and not $remote eq $wanted;
2791 my ($url, $key) = ($1, $2);
2793 $remotes{$remote} ||= { 'heads' => () };
2794 $remotes{$remote}{$key} = $url;
2796 close $fd or return;
2797 return wantarray ? %remotes : \%remotes;
2800 # Takes a hash of remotes as first parameter and fills it by adding the
2801 # available remote heads for each of the indicated remotes.
2802 sub fill_remote_heads {
2803 my $remotes = shift;
2804 my @heads = map { "remotes/$_" } keys %$remotes;
2805 my @remoteheads = git_get_heads_list(undef, @heads);
2806 foreach my $remote (keys %$remotes) {
2807 $remotes->{$remote}{'heads'} = [ grep {
2808 $_->{'name'} =~ s!^$remote/!!
2809 } @remoteheads ];
2813 sub git_get_references {
2814 my $type = shift || "";
2815 my %refs;
2816 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
2817 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
2818 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
2819 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
2820 or return;
2822 while (my $line = <$fd>) {
2823 chomp $line;
2824 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {
2825 if (defined $refs{$1}) {
2826 push @{$refs{$1}}, $2;
2827 } else {
2828 $refs{$1} = [ $2 ];
2832 close $fd or return;
2833 return \%refs;
2836 sub git_get_rev_name_tags {
2837 my $hash = shift || return undef;
2839 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
2840 or return;
2841 my $name_rev = <$fd>;
2842 close $fd;
2844 if ($name_rev =~ m|^$hash tags/(.*)$|) {
2845 return $1;
2846 } else {
2847 # catches also '$hash undefined' output
2848 return undef;
2852 ## ----------------------------------------------------------------------
2853 ## parse to hash functions
2855 sub parse_date {
2856 my $epoch = shift;
2857 my $tz = shift || "-0000";
2859 my %date;
2860 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
2861 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
2862 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
2863 $date{'hour'} = $hour;
2864 $date{'minute'} = $min;
2865 $date{'mday'} = $mday;
2866 $date{'day'} = $days[$wday];
2867 $date{'month'} = $months[$mon];
2868 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
2869 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
2870 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
2871 $mday, $months[$mon], $hour ,$min;
2872 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
2873 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
2875 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
2876 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
2877 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
2878 $date{'hour_local'} = $hour;
2879 $date{'minute_local'} = $min;
2880 $date{'tz_local'} = $tz;
2881 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
2882 1900+$year, $mon+1, $mday,
2883 $hour, $min, $sec, $tz);
2884 return %date;
2887 sub parse_tag {
2888 my $tag_id = shift;
2889 my %tag;
2890 my @comment;
2892 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
2893 $tag{'id'} = $tag_id;
2894 while (my $line = <$fd>) {
2895 chomp $line;
2896 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
2897 $tag{'object'} = $1;
2898 } elsif ($line =~ m/^type (.+)$/) {
2899 $tag{'type'} = $1;
2900 } elsif ($line =~ m/^tag (.+)$/) {
2901 $tag{'name'} = $1;
2902 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
2903 $tag{'author'} = $1;
2904 $tag{'author_epoch'} = $2;
2905 $tag{'author_tz'} = $3;
2906 if ($tag{'author'} =~ m/^([^<]+) <([^>]*)>/) {
2907 $tag{'author_name'} = $1;
2908 $tag{'author_email'} = $2;
2909 } else {
2910 $tag{'author_name'} = $tag{'author'};
2912 } elsif ($line =~ m/--BEGIN/) {
2913 push @comment, $line;
2914 last;
2915 } elsif ($line eq "") {
2916 last;
2919 push @comment, <$fd>;
2920 $tag{'comment'} = \@comment;
2921 close $fd or return;
2922 if (!defined $tag{'name'}) {
2923 return
2925 return %tag
2928 sub parse_commit_text {
2929 my ($commit_text, $withparents) = @_;
2930 my @commit_lines = split '\n', $commit_text;
2931 my %co;
2933 pop @commit_lines; # Remove '\0'
2935 if (! @commit_lines) {
2936 return;
2939 my $header = shift @commit_lines;
2940 if ($header !~ m/^[0-9a-fA-F]{40}/) {
2941 return;
2943 ($co{'id'}, my @parents) = split ' ', $header;
2944 while (my $line = shift @commit_lines) {
2945 last if $line eq "\n";
2946 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
2947 $co{'tree'} = $1;
2948 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
2949 push @parents, $1;
2950 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
2951 $co{'author'} = to_utf8($1);
2952 $co{'author_epoch'} = $2;
2953 $co{'author_tz'} = $3;
2954 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
2955 $co{'author_name'} = $1;
2956 $co{'author_email'} = $2;
2957 } else {
2958 $co{'author_name'} = $co{'author'};
2960 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
2961 $co{'committer'} = to_utf8($1);
2962 $co{'committer_epoch'} = $2;
2963 $co{'committer_tz'} = $3;
2964 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
2965 $co{'committer_name'} = $1;
2966 $co{'committer_email'} = $2;
2967 } else {
2968 $co{'committer_name'} = $co{'committer'};
2972 if (!defined $co{'tree'}) {
2973 return;
2975 $co{'parents'} = \@parents;
2976 $co{'parent'} = $parents[0];
2978 foreach my $title (@commit_lines) {
2979 $title =~ s/^ //;
2980 if ($title ne "") {
2981 $co{'title'} = chop_str($title, 80, 5);
2982 # remove leading stuff of merges to make the interesting part visible
2983 if (length($title) > 50) {
2984 $title =~ s/^Automatic //;
2985 $title =~ s/^merge (of|with) /Merge ... /i;
2986 if (length($title) > 50) {
2987 $title =~ s/(http|rsync):\/\///;
2989 if (length($title) > 50) {
2990 $title =~ s/(master|www|rsync)\.//;
2992 if (length($title) > 50) {
2993 $title =~ s/kernel.org:?//;
2995 if (length($title) > 50) {
2996 $title =~ s/\/pub\/scm//;
2999 $co{'title_short'} = chop_str($title, 50, 5);
3000 last;
3003 if (! defined $co{'title'} || $co{'title'} eq "") {
3004 $co{'title'} = $co{'title_short'} = '(no commit message)';
3006 # remove added spaces
3007 foreach my $line (@commit_lines) {
3008 $line =~ s/^ //;
3010 $co{'comment'} = \@commit_lines;
3012 my $age = time - $co{'committer_epoch'};
3013 $co{'age'} = $age;
3014 $co{'age_string'} = age_string($age);
3015 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
3016 if ($age > 60*60*24*7*2) {
3017 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
3018 $co{'age_string_age'} = $co{'age_string'};
3019 } else {
3020 $co{'age_string_date'} = $co{'age_string'};
3021 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
3023 return %co;
3026 sub parse_commit {
3027 my ($commit_id) = @_;
3028 my %co;
3030 local $/ = "\0";
3032 open my $fd, "-|", git_cmd(), "rev-list",
3033 "--parents",
3034 "--header",
3035 "--max-count=1",
3036 $commit_id,
3037 "--",
3038 or die_error(500, "Open git-rev-list failed");
3039 %co = parse_commit_text(<$fd>, 1);
3040 close $fd;
3042 return %co;
3045 sub parse_commits {
3046 my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
3047 my @cos;
3049 $maxcount ||= 1;
3050 $skip ||= 0;
3052 local $/ = "\0";
3054 open my $fd, "-|", git_cmd(), "rev-list",
3055 "--header",
3056 @args,
3057 ("--max-count=" . $maxcount),
3058 ("--skip=" . $skip),
3059 @extra_options,
3060 $commit_id,
3061 "--",
3062 ($filename ? ($filename) : ())
3063 or die_error(500, "Open git-rev-list failed");
3064 while (my $line = <$fd>) {
3065 my %co = parse_commit_text($line);
3066 push @cos, \%co;
3068 close $fd;
3070 return wantarray ? @cos : \@cos;
3073 # parse line of git-diff-tree "raw" output
3074 sub parse_difftree_raw_line {
3075 my $line = shift;
3076 my %res;
3078 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
3079 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
3080 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
3081 $res{'from_mode'} = $1;
3082 $res{'to_mode'} = $2;
3083 $res{'from_id'} = $3;
3084 $res{'to_id'} = $4;
3085 $res{'status'} = $5;
3086 $res{'similarity'} = $6;
3087 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
3088 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
3089 } else {
3090 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote($7);
3093 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
3094 # combined diff (for merge commit)
3095 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
3096 $res{'nparents'} = length($1);
3097 $res{'from_mode'} = [ split(' ', $2) ];
3098 $res{'to_mode'} = pop @{$res{'from_mode'}};
3099 $res{'from_id'} = [ split(' ', $3) ];
3100 $res{'to_id'} = pop @{$res{'from_id'}};
3101 $res{'status'} = [ split('', $4) ];
3102 $res{'to_file'} = unquote($5);
3104 # 'c512b523472485aef4fff9e57b229d9d243c967f'
3105 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
3106 $res{'commit'} = $1;
3109 return wantarray ? %res : \%res;
3112 # wrapper: return parsed line of git-diff-tree "raw" output
3113 # (the argument might be raw line, or parsed info)
3114 sub parsed_difftree_line {
3115 my $line_or_ref = shift;
3117 if (ref($line_or_ref) eq "HASH") {
3118 # pre-parsed (or generated by hand)
3119 return $line_or_ref;
3120 } else {
3121 return parse_difftree_raw_line($line_or_ref);
3125 # parse line of git-ls-tree output
3126 sub parse_ls_tree_line {
3127 my $line = shift;
3128 my %opts = @_;
3129 my %res;
3131 if ($opts{'-l'}) {
3132 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'
3133 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;
3135 $res{'mode'} = $1;
3136 $res{'type'} = $2;
3137 $res{'hash'} = $3;
3138 $res{'size'} = $4;
3139 if ($opts{'-z'}) {
3140 $res{'name'} = $5;
3141 } else {
3142 $res{'name'} = unquote($5);
3144 } else {
3145 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
3146 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
3148 $res{'mode'} = $1;
3149 $res{'type'} = $2;
3150 $res{'hash'} = $3;
3151 if ($opts{'-z'}) {
3152 $res{'name'} = $4;
3153 } else {
3154 $res{'name'} = unquote($4);
3158 return wantarray ? %res : \%res;
3161 # generates _two_ hashes, references to which are passed as 2 and 3 argument
3162 sub parse_from_to_diffinfo {
3163 my ($diffinfo, $from, $to, @parents) = @_;
3165 if ($diffinfo->{'nparents'}) {
3166 # combined diff
3167 $from->{'file'} = [];
3168 $from->{'href'} = [];
3169 fill_from_file_info($diffinfo, @parents)
3170 unless exists $diffinfo->{'from_file'};
3171 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
3172 $from->{'file'}[$i] =
3173 defined $diffinfo->{'from_file'}[$i] ?
3174 $diffinfo->{'from_file'}[$i] :
3175 $diffinfo->{'to_file'};
3176 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
3177 $from->{'href'}[$i] = href(action=>"blob",
3178 hash_base=>$parents[$i],
3179 hash=>$diffinfo->{'from_id'}[$i],
3180 file_name=>$from->{'file'}[$i]);
3181 } else {
3182 $from->{'href'}[$i] = undef;
3185 } else {
3186 # ordinary (not combined) diff
3187 $from->{'file'} = $diffinfo->{'from_file'};
3188 if ($diffinfo->{'status'} ne "A") { # not new (added) file
3189 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
3190 hash=>$diffinfo->{'from_id'},
3191 file_name=>$from->{'file'});
3192 } else {
3193 delete $from->{'href'};
3197 $to->{'file'} = $diffinfo->{'to_file'};
3198 if (!is_deleted($diffinfo)) { # file exists in result
3199 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
3200 hash=>$diffinfo->{'to_id'},
3201 file_name=>$to->{'file'});
3202 } else {
3203 delete $to->{'href'};
3207 ## ......................................................................
3208 ## parse to array of hashes functions
3210 sub git_get_heads_list {
3211 my ($limit, @classes) = @_;
3212 @classes = ('heads') unless @classes;
3213 my @patterns = map { "refs/$_" } @classes;
3214 my @headslist;
3216 open my $fd, '-|', git_cmd(), 'for-each-ref',
3217 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
3218 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
3219 @patterns
3220 or return;
3221 while (my $line = <$fd>) {
3222 my %ref_item;
3224 chomp $line;
3225 my ($refinfo, $committerinfo) = split(/\0/, $line);
3226 my ($hash, $name, $title) = split(' ', $refinfo, 3);
3227 my ($committer, $epoch, $tz) =
3228 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
3229 $ref_item{'fullname'} = $name;
3230 $name =~ s!^refs/(?:head|remote)s/!!;
3232 $ref_item{'name'} = $name;
3233 $ref_item{'id'} = $hash;
3234 $ref_item{'title'} = $title || '(no commit message)';
3235 $ref_item{'epoch'} = $epoch;
3236 if ($epoch) {
3237 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
3238 } else {
3239 $ref_item{'age'} = "unknown";
3242 push @headslist, \%ref_item;
3244 close $fd;
3246 return wantarray ? @headslist : \@headslist;
3249 sub git_get_tags_list {
3250 my $limit = shift;
3251 my @tagslist;
3253 open my $fd, '-|', git_cmd(), 'for-each-ref',
3254 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
3255 '--format=%(objectname) %(objecttype) %(refname) '.
3256 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
3257 'refs/tags'
3258 or return;
3259 while (my $line = <$fd>) {
3260 my %ref_item;
3262 chomp $line;
3263 my ($refinfo, $creatorinfo) = split(/\0/, $line);
3264 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
3265 my ($creator, $epoch, $tz) =
3266 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
3267 $ref_item{'fullname'} = $name;
3268 $name =~ s!^refs/tags/!!;
3270 $ref_item{'type'} = $type;
3271 $ref_item{'id'} = $id;
3272 $ref_item{'name'} = $name;
3273 if ($type eq "tag") {
3274 $ref_item{'subject'} = $title;
3275 $ref_item{'reftype'} = $reftype;
3276 $ref_item{'refid'} = $refid;
3277 } else {
3278 $ref_item{'reftype'} = $type;
3279 $ref_item{'refid'} = $id;
3282 if ($type eq "tag" || $type eq "commit") {
3283 $ref_item{'epoch'} = $epoch;
3284 if ($epoch) {
3285 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
3286 } else {
3287 $ref_item{'age'} = "unknown";
3291 push @tagslist, \%ref_item;
3293 close $fd;
3295 return wantarray ? @tagslist : \@tagslist;
3298 ## ----------------------------------------------------------------------
3299 ## filesystem-related functions
3301 sub get_file_owner {
3302 my $path = shift;
3304 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
3305 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
3306 if (!defined $gcos) {
3307 return undef;
3309 my $owner = $gcos;
3310 $owner =~ s/[,;].*$//;
3311 return to_utf8($owner);
3314 # assume that file exists
3315 sub insert_file {
3316 my $filename = shift;
3318 open my $fd, '<', $filename;
3319 print map { to_utf8($_) } <$fd>;
3320 close $fd;
3323 ## ......................................................................
3324 ## mimetype related functions
3326 sub mimetype_guess_file {
3327 my $filename = shift;
3328 my $mimemap = shift;
3329 -r $mimemap or return undef;
3331 my %mimemap;
3332 open(my $mh, '<', $mimemap) or return undef;
3333 while (<$mh>) {
3334 next if m/^#/; # skip comments
3335 my ($mimetype, $exts) = split(/\t+/);
3336 if (defined $exts) {
3337 my @exts = split(/\s+/, $exts);
3338 foreach my $ext (@exts) {
3339 $mimemap{$ext} = $mimetype;
3343 close($mh);
3345 $filename =~ /\.([^.]*)$/;
3346 return $mimemap{$1};
3349 sub mimetype_guess {
3350 my $filename = shift;
3351 my $mime;
3352 $filename =~ /\./ or return undef;
3354 if ($mimetypes_file) {
3355 my $file = $mimetypes_file;
3356 if ($file !~ m!^/!) { # if it is relative path
3357 # it is relative to project
3358 $file = "$projectroot/$project/$file";
3360 $mime = mimetype_guess_file($filename, $file);
3362 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
3363 return $mime;
3366 sub blob_mimetype {
3367 my $fd = shift;
3368 my $filename = shift;
3370 if ($filename) {
3371 my $mime = mimetype_guess($filename);
3372 $mime and return $mime;
3375 # just in case
3376 return $default_blob_plain_mimetype unless $fd;
3378 if (-T $fd) {
3379 return 'text/plain';
3380 } elsif (! $filename) {
3381 return 'application/octet-stream';
3382 } elsif ($filename =~ m/\.png$/i) {
3383 return 'image/png';
3384 } elsif ($filename =~ m/\.gif$/i) {
3385 return 'image/gif';
3386 } elsif ($filename =~ m/\.jpe?g$/i) {
3387 return 'image/jpeg';
3388 } else {
3389 return 'application/octet-stream';
3393 sub blob_contenttype {
3394 my ($fd, $file_name, $type) = @_;
3396 $type ||= blob_mimetype($fd, $file_name);
3397 if ($type eq 'text/plain' && defined $default_text_plain_charset) {
3398 $type .= "; charset=$default_text_plain_charset";
3401 return $type;
3404 # guess file syntax for syntax highlighting; return undef if no highlighting
3405 # the name of syntax can (in the future) depend on syntax highlighter used
3406 sub guess_file_syntax {
3407 my ($highlight, $mimetype, $file_name) = @_;
3408 return undef unless ($highlight && defined $file_name);
3409 my $basename = basename($file_name, '.in');
3410 return $highlight_basename{$basename}
3411 if exists $highlight_basename{$basename};
3413 $basename =~ /\.([^.]*)$/;
3414 my $ext = $1 or return undef;
3415 return $highlight_ext{$ext}
3416 if exists $highlight_ext{$ext};
3418 return undef;
3421 # run highlighter and return FD of its output,
3422 # or return original FD if no highlighting
3423 sub run_highlighter {
3424 my ($fd, $highlight, $syntax) = @_;
3425 return $fd unless ($highlight && defined $syntax);
3427 close $fd
3428 or die_error(404, "Reading blob failed");
3429 open $fd, quote_command(git_cmd(), "cat-file", "blob", $hash)." | ".
3430 quote_command($highlight_bin).
3431 " --xhtml --fragment --syntax $syntax |"
3432 or die_error(500, "Couldn't open file or run syntax highlighter");
3433 return $fd;
3436 ## ======================================================================
3437 ## functions printing HTML: header, footer, error page
3439 sub get_page_title {
3440 my $title = to_utf8($site_name);
3442 return $title unless (defined $project);
3443 $title .= " - " . to_utf8($project);
3445 return $title unless (defined $action);
3446 $title .= "/$action"; # $action is US-ASCII (7bit ASCII)
3448 return $title unless (defined $file_name);
3449 $title .= " - " . esc_path($file_name);
3450 if ($action eq "tree" && $file_name !~ m|/$|) {
3451 $title .= "/";
3454 return $title;
3457 sub git_header_html {
3458 my $status = shift || "200 OK";
3459 my $expires = shift;
3460 my %opts = @_;
3462 my $title = get_page_title();
3463 my $content_type;
3464 # require explicit support from the UA if we are to send the page as
3465 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
3466 # we have to do this because MSIE sometimes globs '*/*', pretending to
3467 # support xhtml+xml but choking when it gets what it asked for.
3468 if (defined $cgi->http('HTTP_ACCEPT') &&
3469 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
3470 $cgi->Accept('application/xhtml+xml') != 0) {
3471 $content_type = 'application/xhtml+xml';
3472 } else {
3473 $content_type = 'text/html';
3475 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
3476 -status=> $status, -expires => $expires)
3477 unless ($opts{'-no_http_header'});
3478 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
3479 print <<EOF;
3480 <?xml version="1.0" encoding="utf-8"?>
3481 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3482 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
3483 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
3484 <!-- git core binaries version $git_version -->
3485 <head>
3486 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
3487 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
3488 <meta name="robots" content="index, nofollow"/>
3489 <title>$title</title>
3491 # the stylesheet, favicon etc urls won't work correctly with path_info
3492 # unless we set the appropriate base URL
3493 if ($ENV{'PATH_INFO'}) {
3494 print "<base href=\"".esc_url($base_url)."\" />\n";
3496 # print out each stylesheet that exist, providing backwards capability
3497 # for those people who defined $stylesheet in a config file
3498 if (defined $stylesheet) {
3499 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
3500 } else {
3501 foreach my $stylesheet (@stylesheets) {
3502 next unless $stylesheet;
3503 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
3506 if (defined $project) {
3507 my %href_params = get_feed_info();
3508 if (!exists $href_params{'-title'}) {
3509 $href_params{'-title'} = 'log';
3512 foreach my $format qw(RSS Atom) {
3513 my $type = lc($format);
3514 my %link_attr = (
3515 '-rel' => 'alternate',
3516 '-title' => "$project - $href_params{'-title'} - $format feed",
3517 '-type' => "application/$type+xml"
3520 $href_params{'action'} = $type;
3521 $link_attr{'-href'} = href(%href_params);
3522 print "<link ".
3523 "rel=\"$link_attr{'-rel'}\" ".
3524 "title=\"$link_attr{'-title'}\" ".
3525 "href=\"$link_attr{'-href'}\" ".
3526 "type=\"$link_attr{'-type'}\" ".
3527 "/>\n";
3529 $href_params{'extra_options'} = '--no-merges';
3530 $link_attr{'-href'} = href(%href_params);
3531 $link_attr{'-title'} .= ' (no merges)';
3532 print "<link ".
3533 "rel=\"$link_attr{'-rel'}\" ".
3534 "title=\"$link_attr{'-title'}\" ".
3535 "href=\"$link_attr{'-href'}\" ".
3536 "type=\"$link_attr{'-type'}\" ".
3537 "/>\n";
3540 } else {
3541 printf('<link rel="alternate" title="%s projects list" '.
3542 'href="%s" type="text/plain; charset=utf-8" />'."\n",
3543 $site_name, href(project=>undef, action=>"project_index"));
3544 printf('<link rel="alternate" title="%s projects feeds" '.
3545 'href="%s" type="text/x-opml" />'."\n",
3546 $site_name, href(project=>undef, action=>"opml"));
3548 if (defined $favicon) {
3549 print qq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);
3552 print "</head>\n" .
3553 "<body>\n";
3555 if (defined $site_header && -f $site_header) {
3556 insert_file($site_header);
3559 print "<div class=\"page_header\">\n" .
3560 $cgi->a({-href => esc_url($logo_url),
3561 -title => $logo_label},
3562 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
3563 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
3564 if (defined $project) {
3565 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
3566 if (defined $action) {
3567 my $action_print = $action ;
3568 if (defined $opts{-action_extra}) {
3569 $action_print = $cgi->a({-href => href(action=>$action)},
3570 $action);
3572 print " / $action_print";
3574 if (defined $opts{-action_extra}) {
3575 print " / $opts{-action_extra}";
3577 print "\n";
3579 print "</div>\n";
3581 my $have_search = gitweb_check_feature('search');
3582 if (defined $project && $have_search) {
3583 if (!defined $searchtext) {
3584 $searchtext = "";
3586 my $search_hash;
3587 if (defined $hash_base) {
3588 $search_hash = $hash_base;
3589 } elsif (defined $hash) {
3590 $search_hash = $hash;
3591 } else {
3592 $search_hash = "HEAD";
3594 my $action = $my_uri;
3595 my $use_pathinfo = gitweb_check_feature('pathinfo');
3596 if ($use_pathinfo) {
3597 $action .= "/".esc_url($project);
3599 print $cgi->startform(-method => "get", -action => $action) .
3600 "<div class=\"search\">\n" .
3601 (!$use_pathinfo &&
3602 $cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) . "\n") .
3603 $cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) . "\n" .
3604 $cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) . "\n" .
3605 $cgi->popup_menu(-name => 'st', -default => 'commit',
3606 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
3607 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
3608 " search:\n",
3609 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
3610 "<span title=\"Extended regular expression\">" .
3611 $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
3612 -checked => $search_use_regexp) .
3613 "</span>" .
3614 "</div>" .
3615 $cgi->end_form() . "\n";
3619 sub git_footer_html {
3620 my $feed_class = 'rss_logo';
3622 print "<div class=\"page_footer\">\n";
3623 if (defined $project) {
3624 my $descr = git_get_project_description($project);
3625 if (defined $descr) {
3626 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
3629 my %href_params = get_feed_info();
3630 if (!%href_params) {
3631 $feed_class .= ' generic';
3633 $href_params{'-title'} ||= 'log';
3635 foreach my $format qw(RSS Atom) {
3636 $href_params{'action'} = lc($format);
3637 print $cgi->a({-href => href(%href_params),
3638 -title => "$href_params{'-title'} $format feed",
3639 -class => $feed_class}, $format)."\n";
3642 } else {
3643 print $cgi->a({-href => href(project=>undef, action=>"opml"),
3644 -class => $feed_class}, "OPML") . " ";
3645 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
3646 -class => $feed_class}, "TXT") . "\n";
3648 print "</div>\n"; # class="page_footer"
3650 if (defined $t0 && gitweb_check_feature('timed')) {
3651 print "<div id=\"generating_info\">\n";
3652 print 'This page took '.
3653 '<span id="generating_time" class="time_span">'.
3654 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).
3655 ' seconds </span>'.
3656 ' and '.
3657 '<span id="generating_cmd">'.
3658 $number_of_git_cmds.
3659 '</span> git commands '.
3660 " to generate.\n";
3661 print "</div>\n"; # class="page_footer"
3664 if (defined $site_footer && -f $site_footer) {
3665 insert_file($site_footer);
3668 print qq!<script type="text/javascript" src="$javascript"></script>\n!;
3669 if (defined $action &&
3670 $action eq 'blame_incremental') {
3671 print qq!<script type="text/javascript">\n!.
3672 qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.
3673 qq! "!. href() .qq!");\n!.
3674 qq!</script>\n!;
3675 } elsif (gitweb_check_feature('javascript-actions')) {
3676 print qq!<script type="text/javascript">\n!.
3677 qq!window.onload = fixLinks;\n!.
3678 qq!</script>\n!;
3681 print "</body>\n" .
3682 "</html>";
3685 # die_error(<http_status_code>, <error_message>[, <detailed_html_description>])
3686 # Example: die_error(404, 'Hash not found')
3687 # By convention, use the following status codes (as defined in RFC 2616):
3688 # 400: Invalid or missing CGI parameters, or
3689 # requested object exists but has wrong type.
3690 # 403: Requested feature (like "pickaxe" or "snapshot") not enabled on
3691 # this server or project.
3692 # 404: Requested object/revision/project doesn't exist.
3693 # 500: The server isn't configured properly, or
3694 # an internal error occurred (e.g. failed assertions caused by bugs), or
3695 # an unknown error occurred (e.g. the git binary died unexpectedly).
3696 # 503: The server is currently unavailable (because it is overloaded,
3697 # or down for maintenance). Generally, this is a temporary state.
3698 sub die_error {
3699 my $status = shift || 500;
3700 my $error = esc_html(shift) || "Internal Server Error";
3701 my $extra = shift;
3702 my %opts = @_;
3704 my %http_responses = (
3705 400 => '400 Bad Request',
3706 403 => '403 Forbidden',
3707 404 => '404 Not Found',
3708 500 => '500 Internal Server Error',
3709 503 => '503 Service Unavailable',
3711 git_header_html($http_responses{$status}, undef, %opts);
3712 print <<EOF;
3713 <div class="page_body">
3714 <br /><br />
3715 $status - $error
3716 <br />
3718 if (defined $extra) {
3719 print "<hr />\n" .
3720 "$extra\n";
3722 print "</div>\n";
3724 git_footer_html();
3725 goto DONE_GITWEB
3726 unless ($opts{'-error_handler'});
3729 ## ----------------------------------------------------------------------
3730 ## functions printing or outputting HTML: navigation
3732 sub git_print_page_nav {
3733 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
3734 $extra = '' if !defined $extra; # pager or formats
3736 my @navs = qw(summary shortlog log commit commitdiff tree);
3737 if ($suppress) {
3738 @navs = grep { $_ ne $suppress } @navs;
3741 my %arg = map { $_ => {action=>$_} } @navs;
3742 if (defined $head) {
3743 for (qw(commit commitdiff)) {
3744 $arg{$_}{'hash'} = $head;
3746 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
3747 for (qw(shortlog log)) {
3748 $arg{$_}{'hash'} = $head;
3753 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
3754 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
3756 my @actions = gitweb_get_feature('actions');
3757 my %repl = (
3758 '%' => '%',
3759 'n' => $project, # project name
3760 'f' => $git_dir, # project path within filesystem
3761 'h' => $treehead || '', # current hash ('h' parameter)
3762 'b' => $treebase || '', # hash base ('hb' parameter)
3764 while (@actions) {
3765 my ($label, $link, $pos) = splice(@actions,0,3);
3766 # insert
3767 @navs = map { $_ eq $pos ? ($_, $label) : $_ } @navs;
3768 # munch munch
3769 $link =~ s/%([%nfhb])/$repl{$1}/g;
3770 $arg{$label}{'_href'} = $link;
3773 print "<div class=\"page_nav\">\n" .
3774 (join " | ",
3775 map { $_ eq $current ?
3776 $_ : $cgi->a({-href => ($arg{$_}{_href} ? $arg{$_}{_href} : href(%{$arg{$_}}))}, "$_")
3777 } @navs);
3778 print "<br/>\n$extra<br/>\n" .
3779 "</div>\n";
3782 # returns a submenu for the nagivation of the refs views (tags, heads,
3783 # remotes) with the current view disabled and the remotes view only
3784 # available if the feature is enabled
3785 sub format_ref_views {
3786 my ($current) = @_;
3787 my @ref_views = qw{tags heads};
3788 push @ref_views, 'remotes' if gitweb_check_feature('remote_heads');
3789 return join " | ", map {
3790 $_ eq $current ? $_ :
3791 $cgi->a({-href => href(action=>$_)}, $_)
3792 } @ref_views
3795 sub format_paging_nav {
3796 my ($action, $page, $has_next_link) = @_;
3797 my $paging_nav;
3800 if ($page > 0) {
3801 $paging_nav .=
3802 $cgi->a({-href => href(-replay=>1, page=>undef)}, "first") .
3803 " &sdot; " .
3804 $cgi->a({-href => href(-replay=>1, page=>$page-1),
3805 -accesskey => "p", -title => "Alt-p"}, "prev");
3806 } else {
3807 $paging_nav .= "first &sdot; prev";
3810 if ($has_next_link) {
3811 $paging_nav .= " &sdot; " .
3812 $cgi->a({-href => href(-replay=>1, page=>$page+1),
3813 -accesskey => "n", -title => "Alt-n"}, "next");
3814 } else {
3815 $paging_nav .= " &sdot; next";
3818 return $paging_nav;
3821 ## ......................................................................
3822 ## functions printing or outputting HTML: div
3824 sub git_print_header_div {
3825 my ($action, $title, $hash, $hash_base) = @_;
3826 my %args = ();
3828 $args{'action'} = $action;
3829 $args{'hash'} = $hash if $hash;
3830 $args{'hash_base'} = $hash_base if $hash_base;
3832 print "<div class=\"header\">\n" .
3833 $cgi->a({-href => href(%args), -class => "title"},
3834 $title ? $title : $action) .
3835 "\n</div>\n";
3838 sub format_repo_url {
3839 my ($name, $url) = @_;
3840 return "<tr class=\"metadata_url\"><td>$name</td><td>$url</td></tr>\n";
3843 # Group output by placing it in a DIV element and adding a header.
3844 # Options for start_div() can be provided by passing a hash reference as the
3845 # first parameter to the function.
3846 # Options to git_print_header_div() can be provided by passing an array
3847 # reference. This must follow the options to start_div if they are present.
3848 # The content can be a scalar, which is output as-is, a scalar reference, which
3849 # is output after html escaping, an IO handle passed either as *handle or
3850 # *handle{IO}, or a function reference. In the latter case all following
3851 # parameters will be taken as argument to the content function call.
3852 sub git_print_section {
3853 my ($div_args, $header_args, $content);
3854 my $arg = shift;
3855 if (ref($arg) eq 'HASH') {
3856 $div_args = $arg;
3857 $arg = shift;
3859 if (ref($arg) eq 'ARRAY') {
3860 $header_args = $arg;
3861 $arg = shift;
3863 $content = $arg;
3865 print $cgi->start_div($div_args);
3866 git_print_header_div(@$header_args);
3868 if (ref($content) eq 'CODE') {
3869 $content->(@_);
3870 } elsif (ref($content) eq 'SCALAR') {
3871 print esc_html($$content);
3872 } elsif (ref($content) eq 'GLOB' or ref($content) eq 'IO::Handle') {
3873 print <$content>;
3874 } elsif (!ref($content) && defined($content)) {
3875 print $content;
3878 print $cgi->end_div;
3881 sub print_local_time {
3882 print format_local_time(@_);
3885 sub format_local_time {
3886 my $localtime = '';
3887 my %date = @_;
3888 if ($date{'hour_local'} < 6) {
3889 $localtime .= sprintf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
3890 $date{'hour_local'}, $date{'minute_local'}, $date{'tz_local'});
3891 } else {
3892 $localtime .= sprintf(" (%02d:%02d %s)",
3893 $date{'hour_local'}, $date{'minute_local'}, $date{'tz_local'});
3896 return $localtime;
3899 # Outputs the author name and date in long form
3900 sub git_print_authorship {
3901 my $co = shift;
3902 my %opts = @_;
3903 my $tag = $opts{-tag} || 'div';
3904 my $author = $co->{'author_name'};
3906 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
3907 print "<$tag class=\"author_date\">" .
3908 format_search_author($author, "author", esc_html($author)) .
3909 " [$ad{'rfc2822'}";
3910 print_local_time(%ad) if ($opts{-localtime});
3911 print "]" . git_get_avatar($co->{'author_email'}, -pad_before => 1)
3912 . "</$tag>\n";
3915 # Outputs table rows containing the full author or committer information,
3916 # in the format expected for 'commit' view (& similar).
3917 # Parameters are a commit hash reference, followed by the list of people
3918 # to output information for. If the list is empty it defaults to both
3919 # author and committer.
3920 sub git_print_authorship_rows {
3921 my $co = shift;
3922 # too bad we can't use @people = @_ || ('author', 'committer')
3923 my @people = @_;
3924 @people = ('author', 'committer') unless @people;
3925 foreach my $who (@people) {
3926 my %wd = parse_date($co->{"${who}_epoch"}, $co->{"${who}_tz"});
3927 print "<tr><td>$who</td><td>" .
3928 format_search_author($co->{"${who}_name"}, $who,
3929 esc_html($co->{"${who}_name"})) . " " .
3930 format_search_author($co->{"${who}_email"}, $who,
3931 esc_html("<" . $co->{"${who}_email"} . ">")) .
3932 "</td><td rowspan=\"2\">" .
3933 git_get_avatar($co->{"${who}_email"}, -size => 'double') .
3934 "</td></tr>\n" .
3935 "<tr>" .
3936 "<td></td><td> $wd{'rfc2822'}";
3937 print_local_time(%wd);
3938 print "</td>" .
3939 "</tr>\n";
3943 sub git_print_page_path {
3944 my $name = shift;
3945 my $type = shift;
3946 my $hb = shift;
3949 print "<div class=\"page_path\">";
3950 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
3951 -title => 'tree root'}, to_utf8("[$project]"));
3952 print " / ";
3953 if (defined $name) {
3954 my @dirname = split '/', $name;
3955 my $basename = pop @dirname;
3956 my $fullname = '';
3958 foreach my $dir (@dirname) {
3959 $fullname .= ($fullname ? '/' : '') . $dir;
3960 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
3961 hash_base=>$hb),
3962 -title => $fullname}, esc_path($dir));
3963 print " / ";
3965 if (defined $type && $type eq 'blob') {
3966 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
3967 hash_base=>$hb),
3968 -title => $name}, esc_path($basename));
3969 } elsif (defined $type && $type eq 'tree') {
3970 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
3971 hash_base=>$hb),
3972 -title => $name}, esc_path($basename));
3973 print " / ";
3974 } else {
3975 print esc_path($basename);
3978 print "<br/></div>\n";
3981 sub git_print_log {
3982 my $log = shift;
3983 my %opts = @_;
3985 if ($opts{'-remove_title'}) {
3986 # remove title, i.e. first line of log
3987 shift @$log;
3989 # remove leading empty lines
3990 while (defined $log->[0] && $log->[0] eq "") {
3991 shift @$log;
3994 # print log
3995 my $signoff = 0;
3996 my $empty = 0;
3997 foreach my $line (@$log) {
3998 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
3999 $signoff = 1;
4000 $empty = 0;
4001 if (! $opts{'-remove_signoff'}) {
4002 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
4003 next;
4004 } else {
4005 # remove signoff lines
4006 next;
4008 } else {
4009 $signoff = 0;
4012 # print only one empty line
4013 # do not print empty line after signoff
4014 if ($line eq "") {
4015 next if ($empty || $signoff);
4016 $empty = 1;
4017 } else {
4018 $empty = 0;
4021 print format_log_line_html($line) . "<br/>\n";
4024 if ($opts{'-final_empty_line'}) {
4025 # end with single empty line
4026 print "<br/>\n" unless $empty;
4030 # return link target (what link points to)
4031 sub git_get_link_target {
4032 my $hash = shift;
4033 my $link_target;
4035 # read link
4036 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4037 or return;
4039 local $/ = undef;
4040 $link_target = <$fd>;
4042 close $fd
4043 or return;
4045 return $link_target;
4048 # given link target, and the directory (basedir) the link is in,
4049 # return target of link relative to top directory (top tree);
4050 # return undef if it is not possible (including absolute links).
4051 sub normalize_link_target {
4052 my ($link_target, $basedir) = @_;
4054 # absolute symlinks (beginning with '/') cannot be normalized
4055 return if (substr($link_target, 0, 1) eq '/');
4057 # normalize link target to path from top (root) tree (dir)
4058 my $path;
4059 if ($basedir) {
4060 $path = $basedir . '/' . $link_target;
4061 } else {
4062 # we are in top (root) tree (dir)
4063 $path = $link_target;
4066 # remove //, /./, and /../
4067 my @path_parts;
4068 foreach my $part (split('/', $path)) {
4069 # discard '.' and ''
4070 next if (!$part || $part eq '.');
4071 # handle '..'
4072 if ($part eq '..') {
4073 if (@path_parts) {
4074 pop @path_parts;
4075 } else {
4076 # link leads outside repository (outside top dir)
4077 return;
4079 } else {
4080 push @path_parts, $part;
4083 $path = join('/', @path_parts);
4085 return $path;
4088 # print tree entry (row of git_tree), but without encompassing <tr> element
4089 sub git_print_tree_entry {
4090 my ($t, $basedir, $hash_base, $have_blame) = @_;
4092 my %base_key = ();
4093 $base_key{'hash_base'} = $hash_base if defined $hash_base;
4095 # The format of a table row is: mode list link. Where mode is
4096 # the mode of the entry, list is the name of the entry, an href,
4097 # and link is the action links of the entry.
4099 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
4100 if (exists $t->{'size'}) {
4101 print "<td class=\"size\">$t->{'size'}</td>\n";
4103 if ($t->{'type'} eq "blob") {
4104 print "<td class=\"list\">" .
4105 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
4106 file_name=>"$basedir$t->{'name'}", %base_key),
4107 -class => "list"}, esc_path($t->{'name'}));
4108 if (S_ISLNK(oct $t->{'mode'})) {
4109 my $link_target = git_get_link_target($t->{'hash'});
4110 if ($link_target) {
4111 my $norm_target = normalize_link_target($link_target, $basedir);
4112 if (defined $norm_target) {
4113 print " -> " .
4114 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
4115 file_name=>$norm_target),
4116 -title => $norm_target}, esc_path($link_target));
4117 } else {
4118 print " -> " . esc_path($link_target);
4122 print "</td>\n";
4123 print "<td class=\"link\">";
4124 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
4125 file_name=>"$basedir$t->{'name'}", %base_key)},
4126 "blob");
4127 if ($have_blame) {
4128 print " | " .
4129 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
4130 file_name=>"$basedir$t->{'name'}", %base_key)},
4131 "blame");
4133 if (defined $hash_base) {
4134 print " | " .
4135 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
4136 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
4137 "history");
4139 print " | " .
4140 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
4141 file_name=>"$basedir$t->{'name'}")},
4142 "raw");
4143 print "</td>\n";
4145 } elsif ($t->{'type'} eq "tree") {
4146 print "<td class=\"list\">";
4147 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
4148 file_name=>"$basedir$t->{'name'}",
4149 %base_key)},
4150 esc_path($t->{'name'}));
4151 print "</td>\n";
4152 print "<td class=\"link\">";
4153 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
4154 file_name=>"$basedir$t->{'name'}",
4155 %base_key)},
4156 "tree");
4157 if (defined $hash_base) {
4158 print " | " .
4159 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
4160 file_name=>"$basedir$t->{'name'}")},
4161 "history");
4163 print "</td>\n";
4164 } else {
4165 # unknown object: we can only present history for it
4166 # (this includes 'commit' object, i.e. submodule support)
4167 print "<td class=\"list\">" .
4168 esc_path($t->{'name'}) .
4169 "</td>\n";
4170 print "<td class=\"link\">";
4171 if (defined $hash_base) {
4172 print $cgi->a({-href => href(action=>"history",
4173 hash_base=>$hash_base,
4174 file_name=>"$basedir$t->{'name'}")},
4175 "history");
4177 print "</td>\n";
4181 ## ......................................................................
4182 ## functions printing large fragments of HTML
4184 # get pre-image filenames for merge (combined) diff
4185 sub fill_from_file_info {
4186 my ($diff, @parents) = @_;
4188 $diff->{'from_file'} = [ ];
4189 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
4190 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
4191 if ($diff->{'status'}[$i] eq 'R' ||
4192 $diff->{'status'}[$i] eq 'C') {
4193 $diff->{'from_file'}[$i] =
4194 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
4198 return $diff;
4201 # is current raw difftree line of file deletion
4202 sub is_deleted {
4203 my $diffinfo = shift;
4205 return $diffinfo->{'to_id'} eq ('0' x 40);
4208 # does patch correspond to [previous] difftree raw line
4209 # $diffinfo - hashref of parsed raw diff format
4210 # $patchinfo - hashref of parsed patch diff format
4211 # (the same keys as in $diffinfo)
4212 sub is_patch_split {
4213 my ($diffinfo, $patchinfo) = @_;
4215 return defined $diffinfo && defined $patchinfo
4216 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
4220 sub git_difftree_body {
4221 my ($difftree, $hash, @parents) = @_;
4222 my ($parent) = $parents[0];
4223 my $have_blame = gitweb_check_feature('blame');
4224 print "<div class=\"list_head\">\n";
4225 if ($#{$difftree} > 10) {
4226 print(($#{$difftree} + 1) . " files changed:\n");
4228 print "</div>\n";
4230 print "<table class=\"" .
4231 (@parents > 1 ? "combined " : "") .
4232 "diff_tree\">\n";
4234 # header only for combined diff in 'commitdiff' view
4235 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
4236 if ($has_header) {
4237 # table header
4238 print "<thead><tr>\n" .
4239 "<th></th><th></th>\n"; # filename, patchN link
4240 for (my $i = 0; $i < @parents; $i++) {
4241 my $par = $parents[$i];
4242 print "<th>" .
4243 $cgi->a({-href => href(action=>"commitdiff",
4244 hash=>$hash, hash_parent=>$par),
4245 -title => 'commitdiff to parent number ' .
4246 ($i+1) . ': ' . substr($par,0,7)},
4247 $i+1) .
4248 "&nbsp;</th>\n";
4250 print "</tr></thead>\n<tbody>\n";
4253 my $alternate = 1;
4254 my $patchno = 0;
4255 foreach my $line (@{$difftree}) {
4256 my $diff = parsed_difftree_line($line);
4258 if ($alternate) {
4259 print "<tr class=\"dark\">\n";
4260 } else {
4261 print "<tr class=\"light\">\n";
4263 $alternate ^= 1;
4265 if (exists $diff->{'nparents'}) { # combined diff
4267 fill_from_file_info($diff, @parents)
4268 unless exists $diff->{'from_file'};
4270 if (!is_deleted($diff)) {
4271 # file exists in the result (child) commit
4272 print "<td>" .
4273 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4274 file_name=>$diff->{'to_file'},
4275 hash_base=>$hash),
4276 -class => "list"}, esc_path($diff->{'to_file'})) .
4277 "</td>\n";
4278 } else {
4279 print "<td>" .
4280 esc_path($diff->{'to_file'}) .
4281 "</td>\n";
4284 if ($action eq 'commitdiff') {
4285 # link to patch
4286 $patchno++;
4287 print "<td class=\"link\">" .
4288 $cgi->a({-href => "#patch$patchno"}, "patch") .
4289 " | " .
4290 "</td>\n";
4293 my $has_history = 0;
4294 my $not_deleted = 0;
4295 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
4296 my $hash_parent = $parents[$i];
4297 my $from_hash = $diff->{'from_id'}[$i];
4298 my $from_path = $diff->{'from_file'}[$i];
4299 my $status = $diff->{'status'}[$i];
4301 $has_history ||= ($status ne 'A');
4302 $not_deleted ||= ($status ne 'D');
4304 if ($status eq 'A') {
4305 print "<td class=\"link\" align=\"right\"> | </td>\n";
4306 } elsif ($status eq 'D') {
4307 print "<td class=\"link\">" .
4308 $cgi->a({-href => href(action=>"blob",
4309 hash_base=>$hash,
4310 hash=>$from_hash,
4311 file_name=>$from_path)},
4312 "blob" . ($i+1)) .
4313 " | </td>\n";
4314 } else {
4315 if ($diff->{'to_id'} eq $from_hash) {
4316 print "<td class=\"link nochange\">";
4317 } else {
4318 print "<td class=\"link\">";
4320 print $cgi->a({-href => href(action=>"blobdiff",
4321 hash=>$diff->{'to_id'},
4322 hash_parent=>$from_hash,
4323 hash_base=>$hash,
4324 hash_parent_base=>$hash_parent,
4325 file_name=>$diff->{'to_file'},
4326 file_parent=>$from_path)},
4327 "diff" . ($i+1)) .
4328 " | </td>\n";
4332 print "<td class=\"link\">";
4333 if ($not_deleted) {
4334 print $cgi->a({-href => href(action=>"blob",
4335 hash=>$diff->{'to_id'},
4336 file_name=>$diff->{'to_file'},
4337 hash_base=>$hash)},
4338 "blob");
4339 print " | " if ($has_history);
4341 if ($has_history) {
4342 print $cgi->a({-href => href(action=>"history",
4343 file_name=>$diff->{'to_file'},
4344 hash_base=>$hash)},
4345 "history");
4347 print "</td>\n";
4349 print "</tr>\n";
4350 next; # instead of 'else' clause, to avoid extra indent
4352 # else ordinary diff
4354 my ($to_mode_oct, $to_mode_str, $to_file_type);
4355 my ($from_mode_oct, $from_mode_str, $from_file_type);
4356 if ($diff->{'to_mode'} ne ('0' x 6)) {
4357 $to_mode_oct = oct $diff->{'to_mode'};
4358 if (S_ISREG($to_mode_oct)) { # only for regular file
4359 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
4361 $to_file_type = file_type($diff->{'to_mode'});
4363 if ($diff->{'from_mode'} ne ('0' x 6)) {
4364 $from_mode_oct = oct $diff->{'from_mode'};
4365 if (S_ISREG($to_mode_oct)) { # only for regular file
4366 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
4368 $from_file_type = file_type($diff->{'from_mode'});
4371 if ($diff->{'status'} eq "A") { # created
4372 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
4373 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
4374 $mode_chng .= "]</span>";
4375 print "<td>";
4376 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4377 hash_base=>$hash, file_name=>$diff->{'file'}),
4378 -class => "list"}, esc_path($diff->{'file'}));
4379 print "</td>\n";
4380 print "<td>$mode_chng</td>\n";
4381 print "<td class=\"link\">";
4382 if ($action eq 'commitdiff') {
4383 # link to patch
4384 $patchno++;
4385 print $cgi->a({-href => "#patch$patchno"}, "patch");
4386 print " | ";
4388 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4389 hash_base=>$hash, file_name=>$diff->{'file'})},
4390 "blob");
4391 print "</td>\n";
4393 } elsif ($diff->{'status'} eq "D") { # deleted
4394 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
4395 print "<td>";
4396 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
4397 hash_base=>$parent, file_name=>$diff->{'file'}),
4398 -class => "list"}, esc_path($diff->{'file'}));
4399 print "</td>\n";
4400 print "<td>$mode_chng</td>\n";
4401 print "<td class=\"link\">";
4402 if ($action eq 'commitdiff') {
4403 # link to patch
4404 $patchno++;
4405 print $cgi->a({-href => "#patch$patchno"}, "patch");
4406 print " | ";
4408 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
4409 hash_base=>$parent, file_name=>$diff->{'file'})},
4410 "blob") . " | ";
4411 if ($have_blame) {
4412 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
4413 file_name=>$diff->{'file'})},
4414 "blame") . " | ";
4416 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
4417 file_name=>$diff->{'file'})},
4418 "history");
4419 print "</td>\n";
4421 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
4422 my $mode_chnge = "";
4423 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
4424 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
4425 if ($from_file_type ne $to_file_type) {
4426 $mode_chnge .= " from $from_file_type to $to_file_type";
4428 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
4429 if ($from_mode_str && $to_mode_str) {
4430 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
4431 } elsif ($to_mode_str) {
4432 $mode_chnge .= " mode: $to_mode_str";
4435 $mode_chnge .= "]</span>\n";
4437 print "<td>";
4438 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4439 hash_base=>$hash, file_name=>$diff->{'file'}),
4440 -class => "list"}, esc_path($diff->{'file'}));
4441 print "</td>\n";
4442 print "<td>$mode_chnge</td>\n";
4443 print "<td class=\"link\">";
4444 if ($action eq 'commitdiff') {
4445 # link to patch
4446 $patchno++;
4447 print $cgi->a({-href => "#patch$patchno"}, "patch") .
4448 " | ";
4449 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
4450 # "commit" view and modified file (not onlu mode changed)
4451 print $cgi->a({-href => href(action=>"blobdiff",
4452 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
4453 hash_base=>$hash, hash_parent_base=>$parent,
4454 file_name=>$diff->{'file'})},
4455 "diff") .
4456 " | ";
4458 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4459 hash_base=>$hash, file_name=>$diff->{'file'})},
4460 "blob") . " | ";
4461 if ($have_blame) {
4462 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
4463 file_name=>$diff->{'file'})},
4464 "blame") . " | ";
4466 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
4467 file_name=>$diff->{'file'})},
4468 "history");
4469 print "</td>\n";
4471 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
4472 my %status_name = ('R' => 'moved', 'C' => 'copied');
4473 my $nstatus = $status_name{$diff->{'status'}};
4474 my $mode_chng = "";
4475 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
4476 # mode also for directories, so we cannot use $to_mode_str
4477 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
4479 print "<td>" .
4480 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
4481 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
4482 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
4483 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
4484 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
4485 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
4486 -class => "list"}, esc_path($diff->{'from_file'})) .
4487 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
4488 "<td class=\"link\">";
4489 if ($action eq 'commitdiff') {
4490 # link to patch
4491 $patchno++;
4492 print $cgi->a({-href => "#patch$patchno"}, "patch") .
4493 " | ";
4494 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
4495 # "commit" view and modified file (not only pure rename or copy)
4496 print $cgi->a({-href => href(action=>"blobdiff",
4497 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
4498 hash_base=>$hash, hash_parent_base=>$parent,
4499 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
4500 "diff") .
4501 " | ";
4503 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4504 hash_base=>$parent, file_name=>$diff->{'to_file'})},
4505 "blob") . " | ";
4506 if ($have_blame) {
4507 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
4508 file_name=>$diff->{'to_file'})},
4509 "blame") . " | ";
4511 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
4512 file_name=>$diff->{'to_file'})},
4513 "history");
4514 print "</td>\n";
4516 } # we should not encounter Unmerged (U) or Unknown (X) status
4517 print "</tr>\n";
4519 print "</tbody>" if $has_header;
4520 print "</table>\n";
4523 sub git_patchset_body {
4524 my ($fd, $difftree, $hash, @hash_parents) = @_;
4525 my ($hash_parent) = $hash_parents[0];
4527 my $is_combined = (@hash_parents > 1);
4528 my $patch_idx = 0;
4529 my $patch_number = 0;
4530 my $patch_line;
4531 my $diffinfo;
4532 my $to_name;
4533 my (%from, %to);
4535 print "<div class=\"patchset\">\n";
4537 # skip to first patch
4538 while ($patch_line = <$fd>) {
4539 chomp $patch_line;
4541 last if ($patch_line =~ m/^diff /);
4544 PATCH:
4545 while ($patch_line) {
4547 # parse "git diff" header line
4548 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
4549 # $1 is from_name, which we do not use
4550 $to_name = unquote($2);
4551 $to_name =~ s!^b/!!;
4552 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
4553 # $1 is 'cc' or 'combined', which we do not use
4554 $to_name = unquote($2);
4555 } else {
4556 $to_name = undef;
4559 # check if current patch belong to current raw line
4560 # and parse raw git-diff line if needed
4561 if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
4562 # this is continuation of a split patch
4563 print "<div class=\"patch cont\">\n";
4564 } else {
4565 # advance raw git-diff output if needed
4566 $patch_idx++ if defined $diffinfo;
4568 # read and prepare patch information
4569 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
4571 # compact combined diff output can have some patches skipped
4572 # find which patch (using pathname of result) we are at now;
4573 if ($is_combined) {
4574 while ($to_name ne $diffinfo->{'to_file'}) {
4575 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
4576 format_diff_cc_simplified($diffinfo, @hash_parents) .
4577 "</div>\n"; # class="patch"
4579 $patch_idx++;
4580 $patch_number++;
4582 last if $patch_idx > $#$difftree;
4583 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
4587 # modifies %from, %to hashes
4588 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
4590 # this is first patch for raw difftree line with $patch_idx index
4591 # we index @$difftree array from 0, but number patches from 1
4592 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
4595 # git diff header
4596 #assert($patch_line =~ m/^diff /) if DEBUG;
4597 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
4598 $patch_number++;
4599 # print "git diff" header
4600 print format_git_diff_header_line($patch_line, $diffinfo,
4601 \%from, \%to);
4603 # print extended diff header
4604 print "<div class=\"diff extended_header\">\n";
4605 EXTENDED_HEADER:
4606 while ($patch_line = <$fd>) {
4607 chomp $patch_line;
4609 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
4611 print format_extended_diff_header_line($patch_line, $diffinfo,
4612 \%from, \%to);
4614 print "</div>\n"; # class="diff extended_header"
4616 # from-file/to-file diff header
4617 if (! $patch_line) {
4618 print "</div>\n"; # class="patch"
4619 last PATCH;
4621 next PATCH if ($patch_line =~ m/^diff /);
4622 #assert($patch_line =~ m/^---/) if DEBUG;
4624 my $last_patch_line = $patch_line;
4625 $patch_line = <$fd>;
4626 chomp $patch_line;
4627 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
4629 print format_diff_from_to_header($last_patch_line, $patch_line,
4630 $diffinfo, \%from, \%to,
4631 @hash_parents);
4633 # the patch itself
4634 LINE:
4635 while ($patch_line = <$fd>) {
4636 chomp $patch_line;
4638 next PATCH if ($patch_line =~ m/^diff /);
4640 print format_diff_line($patch_line, \%from, \%to);
4643 } continue {
4644 print "</div>\n"; # class="patch"
4647 # for compact combined (--cc) format, with chunk and patch simplification
4648 # the patchset might be empty, but there might be unprocessed raw lines
4649 for (++$patch_idx if $patch_number > 0;
4650 $patch_idx < @$difftree;
4651 ++$patch_idx) {
4652 # read and prepare patch information
4653 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
4655 # generate anchor for "patch" links in difftree / whatchanged part
4656 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
4657 format_diff_cc_simplified($diffinfo, @hash_parents) .
4658 "</div>\n"; # class="patch"
4660 $patch_number++;
4663 if ($patch_number == 0) {
4664 if (@hash_parents > 1) {
4665 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
4666 } else {
4667 print "<div class=\"diff nodifferences\">No differences found</div>\n";
4671 print "</div>\n"; # class="patchset"
4674 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4676 # fills project list info (age, description, owner, forks) for each
4677 # project in the list, removing invalid projects from returned list
4678 # NOTE: modifies $projlist, but does not remove entries from it
4679 sub fill_project_list_info {
4680 my ($projlist, $check_forks) = @_;
4681 my @projects;
4683 my $show_ctags = gitweb_check_feature('ctags');
4684 PROJECT:
4685 foreach my $pr (@$projlist) {
4686 my (@activity) = git_get_last_activity($pr->{'path'});
4687 unless (@activity) {
4688 next PROJECT;
4690 ($pr->{'age'}, $pr->{'age_string'}) = @activity;
4691 if (!defined $pr->{'descr'}) {
4692 my $descr = git_get_project_description($pr->{'path'}) || "";
4693 $descr = to_utf8($descr);
4694 $pr->{'descr_long'} = $descr;
4695 $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
4697 if (!defined $pr->{'owner'}) {
4698 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
4700 if ($check_forks) {
4701 my $pname = $pr->{'path'};
4702 if (($pname =~ s/\.git$//) &&
4703 ($pname !~ /\/$/) &&
4704 (-d "$projectroot/$pname")) {
4705 $pr->{'forks'} = "-d $projectroot/$pname";
4706 } else {
4707 $pr->{'forks'} = 0;
4710 $show_ctags and $pr->{'ctags'} = git_get_project_ctags($pr->{'path'});
4711 push @projects, $pr;
4714 return @projects;
4717 # print 'sort by' <th> element, generating 'sort by $name' replay link
4718 # if that order is not selected
4719 sub print_sort_th {
4720 print format_sort_th(@_);
4723 sub format_sort_th {
4724 my ($name, $order, $header) = @_;
4725 my $sort_th = "";
4726 $header ||= ucfirst($name);
4728 if ($order eq $name) {
4729 $sort_th .= "<th>$header</th>\n";
4730 } else {
4731 $sort_th .= "<th>" .
4732 $cgi->a({-href => href(-replay=>1, order=>$name),
4733 -class => "header"}, $header) .
4734 "</th>\n";
4737 return $sort_th;
4740 sub git_project_list_body {
4741 # actually uses global variable $project
4742 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
4744 my $check_forks = gitweb_check_feature('forks');
4745 my @projects = fill_project_list_info($projlist, $check_forks);
4747 $order ||= $default_projects_order;
4748 $from = 0 unless defined $from;
4749 $to = $#projects if (!defined $to || $#projects < $to);
4751 my %order_info = (
4752 project => { key => 'path', type => 'str' },
4753 descr => { key => 'descr_long', type => 'str' },
4754 owner => { key => 'owner', type => 'str' },
4755 age => { key => 'age', type => 'num' }
4757 my $oi = $order_info{$order};
4758 if ($oi->{'type'} eq 'str') {
4759 @projects = sort {$a->{$oi->{'key'}} cmp $b->{$oi->{'key'}}} @projects;
4760 } else {
4761 @projects = sort {$a->{$oi->{'key'}} <=> $b->{$oi->{'key'}}} @projects;
4764 my $show_ctags = gitweb_check_feature('ctags');
4765 if ($show_ctags) {
4766 my %ctags;
4767 foreach my $p (@projects) {
4768 foreach my $ct (keys %{$p->{'ctags'}}) {
4769 $ctags{$ct} += $p->{'ctags'}->{$ct};
4772 my $cloud = git_populate_project_tagcloud(\%ctags);
4773 print git_show_project_tagcloud($cloud, 64);
4776 print "<table class=\"project_list\">\n";
4777 unless ($no_header) {
4778 print "<tr>\n";
4779 if ($check_forks) {
4780 print "<th></th>\n";
4782 print_sort_th('project', $order, 'Project');
4783 print_sort_th('descr', $order, 'Description');
4784 print_sort_th('owner', $order, 'Owner');
4785 print_sort_th('age', $order, 'Last Change');
4786 print "<th></th>\n" . # for links
4787 "</tr>\n";
4789 my $alternate = 1;
4790 my $tagfilter = $cgi->param('by_tag');
4791 for (my $i = $from; $i <= $to; $i++) {
4792 my $pr = $projects[$i];
4794 next if $tagfilter and $show_ctags and not grep { lc $_ eq lc $tagfilter } keys %{$pr->{'ctags'}};
4795 next if $searchtext and not $pr->{'path'} =~ /$searchtext/
4796 and not $pr->{'descr_long'} =~ /$searchtext/;
4797 # Weed out forks or non-matching entries of search
4798 if ($check_forks) {
4799 my $forkbase = $project; $forkbase ||= ''; $forkbase =~ s#\.git$#/#;
4800 $forkbase="^$forkbase" if $forkbase;
4801 next if not $searchtext and not $tagfilter and $show_ctags
4802 and $pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe
4805 if ($alternate) {
4806 print "<tr class=\"dark\">\n";
4807 } else {
4808 print "<tr class=\"light\">\n";
4810 $alternate ^= 1;
4811 if ($check_forks) {
4812 print "<td>";
4813 if ($pr->{'forks'}) {
4814 print "<!-- $pr->{'forks'} -->\n";
4815 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
4817 print "</td>\n";
4819 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
4820 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
4821 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
4822 -class => "list", -title => $pr->{'descr_long'}},
4823 esc_html($pr->{'descr'})) . "</td>\n" .
4824 "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n";
4825 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
4826 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
4827 "<td class=\"link\">" .
4828 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
4829 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
4830 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
4831 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
4832 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
4833 "</td>\n" .
4834 "</tr>\n";
4836 if (defined $extra) {
4837 print "<tr>\n";
4838 if ($check_forks) {
4839 print "<td></td>\n";
4841 print "<td colspan=\"5\">$extra</td>\n" .
4842 "</tr>\n";
4844 print "</table>\n";
4847 sub git_log_body {
4848 # uses global variable $project
4849 my ($commitlist, $from, $to, $refs, $extra) = @_;
4851 $from = 0 unless defined $from;
4852 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
4854 for (my $i = 0; $i <= $to; $i++) {
4855 my %co = %{$commitlist->[$i]};
4856 next if !%co;
4857 my $commit = $co{'id'};
4858 my $ref = format_ref_marker($refs, $commit);
4859 my %ad = parse_date($co{'author_epoch'});
4860 git_print_header_div('commit',
4861 "<span class=\"age\">$co{'age_string'}</span>" .
4862 esc_html($co{'title'}) . $ref,
4863 $commit);
4864 print "<div class=\"title_text\">\n" .
4865 "<div class=\"log_link\">\n" .
4866 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
4867 " | " .
4868 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
4869 " | " .
4870 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
4871 "<br/>\n" .
4872 "</div>\n";
4873 git_print_authorship(\%co, -tag => 'span');
4874 print "<br/>\n</div>\n";
4876 print "<div class=\"log_body\">\n";
4877 git_print_log($co{'comment'}, -final_empty_line=> 1);
4878 print "</div>\n";
4880 if ($extra) {
4881 print "<div class=\"page_nav\">\n";
4882 print "$extra\n";
4883 print "</div>\n";
4887 sub git_shortlog_body {
4888 # uses global variable $project
4889 my ($commitlist, $from, $to, $refs, $extra) = @_;
4891 $from = 0 unless defined $from;
4892 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
4894 print "<table class=\"shortlog\">\n";
4895 my $alternate = 1;
4896 for (my $i = $from; $i <= $to; $i++) {
4897 my %co = %{$commitlist->[$i]};
4898 my $commit = $co{'id'};
4899 my $ref = format_ref_marker($refs, $commit);
4900 if ($alternate) {
4901 print "<tr class=\"dark\">\n";
4902 } else {
4903 print "<tr class=\"light\">\n";
4905 $alternate ^= 1;
4906 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
4907 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4908 format_author_html('td', \%co, 10) . "<td>";
4909 print format_subject_html($co{'title'}, $co{'title_short'},
4910 href(action=>"commit", hash=>$commit), $ref);
4911 print "</td>\n" .
4912 "<td class=\"link\">" .
4913 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
4914 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
4915 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
4916 my $snapshot_links = format_snapshot_links($commit);
4917 if (defined $snapshot_links) {
4918 print " | " . $snapshot_links;
4920 print "</td>\n" .
4921 "</tr>\n";
4923 if (defined $extra) {
4924 print "<tr>\n" .
4925 "<td colspan=\"4\">$extra</td>\n" .
4926 "</tr>\n";
4928 print "</table>\n";
4931 sub git_history_body {
4932 # Warning: assumes constant type (blob or tree) during history
4933 my ($commitlist, $from, $to, $refs, $extra,
4934 $file_name, $file_hash, $ftype) = @_;
4936 $from = 0 unless defined $from;
4937 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
4939 print "<table class=\"history\">\n";
4940 my $alternate = 1;
4941 for (my $i = $from; $i <= $to; $i++) {
4942 my %co = %{$commitlist->[$i]};
4943 if (!%co) {
4944 next;
4946 my $commit = $co{'id'};
4948 my $ref = format_ref_marker($refs, $commit);
4950 if ($alternate) {
4951 print "<tr class=\"dark\">\n";
4952 } else {
4953 print "<tr class=\"light\">\n";
4955 $alternate ^= 1;
4956 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4957 # shortlog: format_author_html('td', \%co, 10)
4958 format_author_html('td', \%co, 15, 3) . "<td>";
4959 # originally git_history used chop_str($co{'title'}, 50)
4960 print format_subject_html($co{'title'}, $co{'title_short'},
4961 href(action=>"commit", hash=>$commit), $ref);
4962 print "</td>\n" .
4963 "<td class=\"link\">" .
4964 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
4965 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
4967 if ($ftype eq 'blob') {
4968 my $blob_current = $file_hash;
4969 my $blob_parent = git_get_hash_by_path($commit, $file_name);
4970 if (defined $blob_current && defined $blob_parent &&
4971 $blob_current ne $blob_parent) {
4972 print " | " .
4973 $cgi->a({-href => href(action=>"blobdiff",
4974 hash=>$blob_current, hash_parent=>$blob_parent,
4975 hash_base=>$hash_base, hash_parent_base=>$commit,
4976 file_name=>$file_name)},
4977 "diff to current");
4980 print "</td>\n" .
4981 "</tr>\n";
4983 if (defined $extra) {
4984 print "<tr>\n" .
4985 "<td colspan=\"4\">$extra</td>\n" .
4986 "</tr>\n";
4988 print "</table>\n";
4991 sub git_tags_body {
4992 # uses global variable $project
4993 my ($taglist, $from, $to, $extra) = @_;
4994 $from = 0 unless defined $from;
4995 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
4997 print "<table class=\"tags\">\n";
4998 my $alternate = 1;
4999 for (my $i = $from; $i <= $to; $i++) {
5000 my $entry = $taglist->[$i];
5001 my %tag = %$entry;
5002 my $comment = $tag{'subject'};
5003 my $comment_short;
5004 if (defined $comment) {
5005 $comment_short = chop_str($comment, 30, 5);
5007 if ($alternate) {
5008 print "<tr class=\"dark\">\n";
5009 } else {
5010 print "<tr class=\"light\">\n";
5012 $alternate ^= 1;
5013 if (defined $tag{'age'}) {
5014 print "<td><i>$tag{'age'}</i></td>\n";
5015 } else {
5016 print "<td></td>\n";
5018 print "<td>" .
5019 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
5020 -class => "list name"}, esc_html($tag{'name'})) .
5021 "</td>\n" .
5022 "<td>";
5023 if (defined $comment) {
5024 print format_subject_html($comment, $comment_short,
5025 href(action=>"tag", hash=>$tag{'id'}));
5027 print "</td>\n" .
5028 "<td class=\"selflink\">";
5029 if ($tag{'type'} eq "tag") {
5030 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
5031 } else {
5032 print "&nbsp;";
5034 print "</td>\n" .
5035 "<td class=\"link\">" . " | " .
5036 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
5037 if ($tag{'reftype'} eq "commit") {
5038 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})}, "shortlog") .
5039 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})}, "log");
5040 } elsif ($tag{'reftype'} eq "blob") {
5041 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
5043 print "</td>\n" .
5044 "</tr>";
5046 if (defined $extra) {
5047 print "<tr>\n" .
5048 "<td colspan=\"5\">$extra</td>\n" .
5049 "</tr>\n";
5051 print "</table>\n";
5054 sub git_heads_body {
5055 # uses global variable $project
5056 my ($headlist, $head, $from, $to, $extra) = @_;
5057 $from = 0 unless defined $from;
5058 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
5060 print "<table class=\"heads\">\n";
5061 my $alternate = 1;
5062 for (my $i = $from; $i <= $to; $i++) {
5063 my $entry = $headlist->[$i];
5064 my %ref = %$entry;
5065 my $curr = $ref{'id'} eq $head;
5066 if ($alternate) {
5067 print "<tr class=\"dark\">\n";
5068 } else {
5069 print "<tr class=\"light\">\n";
5071 $alternate ^= 1;
5072 print "<td><i>$ref{'age'}</i></td>\n" .
5073 ($curr ? "<td class=\"current_head\">" : "<td>") .
5074 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),
5075 -class => "list name"},esc_html($ref{'name'})) .
5076 "</td>\n" .
5077 "<td class=\"link\">" .
5078 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})}, "shortlog") . " | " .
5079 $cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})}, "log") . " | " .
5080 $cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'fullname'})}, "tree") .
5081 "</td>\n" .
5082 "</tr>";
5084 if (defined $extra) {
5085 print "<tr>\n" .
5086 "<td colspan=\"3\">$extra</td>\n" .
5087 "</tr>\n";
5089 print "</table>\n";
5092 # Display a single remote block
5093 sub git_remote_block {
5094 my ($remote, $rdata, $limit, $head) = @_;
5096 my $heads = $rdata->{'heads'};
5097 my $fetch = $rdata->{'fetch'};
5098 my $push = $rdata->{'push'};
5100 my $urls_table = "<table class=\"projects_list\">\n" ;
5102 if (defined $fetch) {
5103 if ($fetch eq $push) {
5104 $urls_table .= format_repo_url("URL", $fetch);
5105 } else {
5106 $urls_table .= format_repo_url("Fetch URL", $fetch);
5107 $urls_table .= format_repo_url("Push URL", $push) if defined $push;
5109 } elsif (defined $push) {
5110 $urls_table .= format_repo_url("Push URL", $push);
5111 } else {
5112 $urls_table .= format_repo_url("", "No remote URL");
5115 $urls_table .= "</table>\n";
5117 my $dots;
5118 if (defined $limit && $limit < @$heads) {
5119 $dots = $cgi->a({-href => href(action=>"remotes", hash=>$remote)}, "...");
5122 print $urls_table;
5123 git_heads_body($heads, $head, 0, $limit, $dots);
5126 # Display a list of remote names with the respective fetch and push URLs
5127 sub git_remotes_list {
5128 my ($remotedata, $limit) = @_;
5129 print "<table class=\"heads\">\n";
5130 my $alternate = 1;
5131 my @remotes = sort keys %$remotedata;
5133 my $limited = $limit && $limit < @remotes;
5135 $#remotes = $limit - 1 if $limited;
5137 while (my $remote = shift @remotes) {
5138 my $rdata = $remotedata->{$remote};
5139 my $fetch = $rdata->{'fetch'};
5140 my $push = $rdata->{'push'};
5141 if ($alternate) {
5142 print "<tr class=\"dark\">\n";
5143 } else {
5144 print "<tr class=\"light\">\n";
5146 $alternate ^= 1;
5147 print "<td>" .
5148 $cgi->a({-href=> href(action=>'remotes', hash=>$remote),
5149 -class=> "list name"},esc_html($remote)) .
5150 "</td>";
5151 print "<td class=\"link\">" .
5152 (defined $fetch ? $cgi->a({-href=> $fetch}, "fetch") : "fetch") .
5153 " | " .
5154 (defined $push ? $cgi->a({-href=> $push}, "push") : "push") .
5155 "</td>";
5157 print "</tr>\n";
5160 if ($limited) {
5161 print "<tr>\n" .
5162 "<td colspan=\"3\">" .
5163 $cgi->a({-href => href(action=>"remotes")}, "...") .
5164 "</td>\n" . "</tr>\n";
5167 print "</table>";
5170 # Display remote heads grouped by remote, unless there are too many
5171 # remotes, in which case we only display the remote names
5172 sub git_remotes_body {
5173 my ($remotedata, $limit, $head) = @_;
5174 if ($limit and $limit < keys %$remotedata) {
5175 git_remotes_list($remotedata, $limit);
5176 } else {
5177 fill_remote_heads($remotedata);
5178 while (my ($remote, $rdata) = each %$remotedata) {
5179 git_print_section({-class=>"remote", -id=>$remote},
5180 ["remotes", $remote, $remote], sub {
5181 git_remote_block($remote, $rdata, $limit, $head);
5187 sub git_search_grep_body {
5188 my ($commitlist, $from, $to, $extra) = @_;
5189 $from = 0 unless defined $from;
5190 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
5192 print "<table class=\"commit_search\">\n";
5193 my $alternate = 1;
5194 for (my $i = $from; $i <= $to; $i++) {
5195 my %co = %{$commitlist->[$i]};
5196 if (!%co) {
5197 next;
5199 my $commit = $co{'id'};
5200 if ($alternate) {
5201 print "<tr class=\"dark\">\n";
5202 } else {
5203 print "<tr class=\"light\">\n";
5205 $alternate ^= 1;
5206 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5207 format_author_html('td', \%co, 15, 5) .
5208 "<td>" .
5209 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
5210 -class => "list subject"},
5211 chop_and_escape_str($co{'title'}, 50) . "<br/>");
5212 my $comment = $co{'comment'};
5213 foreach my $line (@$comment) {
5214 if ($line =~ m/^(.*?)($search_regexp)(.*)$/i) {
5215 my ($lead, $match, $trail) = ($1, $2, $3);
5216 $match = chop_str($match, 70, 5, 'center');
5217 my $contextlen = int((80 - length($match))/2);
5218 $contextlen = 30 if ($contextlen > 30);
5219 $lead = chop_str($lead, $contextlen, 10, 'left');
5220 $trail = chop_str($trail, $contextlen, 10, 'right');
5222 $lead = esc_html($lead);
5223 $match = esc_html($match);
5224 $trail = esc_html($trail);
5226 print "$lead<span class=\"match\">$match</span>$trail<br />";
5229 print "</td>\n" .
5230 "<td class=\"link\">" .
5231 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5232 " | " .
5233 $cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})}, "commitdiff") .
5234 " | " .
5235 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5236 print "</td>\n" .
5237 "</tr>\n";
5239 if (defined $extra) {
5240 print "<tr>\n" .
5241 "<td colspan=\"3\">$extra</td>\n" .
5242 "</tr>\n";
5244 print "</table>\n";
5247 ## ======================================================================
5248 ## ======================================================================
5249 ## actions
5251 sub git_project_list {
5252 my $order = $input_params{'order'};
5253 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
5254 die_error(400, "Unknown order parameter");
5257 my @list = git_get_projects_list();
5258 if (!@list) {
5259 die_error(404, "No projects found");
5262 git_header_html();
5263 if (defined $home_text && -f $home_text) {
5264 print "<div class=\"index_include\">\n";
5265 insert_file($home_text);
5266 print "</div>\n";
5268 print $cgi->startform(-method => "get") .
5269 "<p class=\"projsearch\">Search:\n" .
5270 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
5271 "</p>" .
5272 $cgi->end_form() . "\n";
5273 git_project_list_body(\@list, $order);
5274 git_footer_html();
5277 sub git_forks {
5278 my $order = $input_params{'order'};
5279 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
5280 die_error(400, "Unknown order parameter");
5283 my @list = git_get_projects_list($project);
5284 if (!@list) {
5285 die_error(404, "No forks found");
5288 git_header_html();
5289 git_print_page_nav('','');
5290 git_print_header_div('summary', "$project forks");
5291 git_project_list_body(\@list, $order);
5292 git_footer_html();
5295 sub git_project_index {
5296 my @projects = git_get_projects_list($project);
5298 print $cgi->header(
5299 -type => 'text/plain',
5300 -charset => 'utf-8',
5301 -content_disposition => 'inline; filename="index.aux"');
5303 foreach my $pr (@projects) {
5304 if (!exists $pr->{'owner'}) {
5305 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
5308 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
5309 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
5310 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
5311 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
5312 $path =~ s/ /\+/g;
5313 $owner =~ s/ /\+/g;
5315 print "$path $owner\n";
5319 sub git_summary {
5320 my $descr = git_get_project_description($project) || "none";
5321 my %co = parse_commit("HEAD");
5322 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
5323 my $head = $co{'id'};
5324 my $remote_heads = gitweb_check_feature('remote_heads');
5326 my $owner = git_get_project_owner($project);
5328 my $refs = git_get_references();
5329 # These get_*_list functions return one more to allow us to see if
5330 # there are more ...
5331 my @taglist = git_get_tags_list(16);
5332 my @headlist = git_get_heads_list(16);
5333 my %remotedata = $remote_heads ? git_get_remotes_list() : ();
5334 my @forklist;
5335 my $check_forks = gitweb_check_feature('forks');
5337 if ($check_forks) {
5338 @forklist = git_get_projects_list($project);
5341 git_header_html();
5342 git_print_page_nav('summary','', $head);
5344 print "<div class=\"title\">&nbsp;</div>\n";
5345 print "<table class=\"projects_list\">\n" .
5346 "<tr id=\"metadata_desc\"><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
5347 "<tr id=\"metadata_owner\"><td>owner</td><td>" . esc_html($owner) . "</td></tr>\n";
5348 if (defined $cd{'rfc2822'}) {
5349 print "<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
5352 # use per project git URL list in $projectroot/$project/cloneurl
5353 # or make project git URL from git base URL and project name
5354 my $url_tag = "URL";
5355 my @url_list = git_get_project_url_list($project);
5356 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
5357 foreach my $git_url (@url_list) {
5358 next unless $git_url;
5359 print format_repo_url($url_tag, $git_url);
5360 $url_tag = "";
5363 # Tag cloud
5364 my $show_ctags = gitweb_check_feature('ctags');
5365 if ($show_ctags) {
5366 my $ctags = git_get_project_ctags($project);
5367 my $cloud = git_populate_project_tagcloud($ctags);
5368 print "<tr id=\"metadata_ctags\"><td>Content tags:<br />";
5369 print "</td>\n<td>" unless %$ctags;
5370 print "<form action=\"$show_ctags\" method=\"post\"><input type=\"hidden\" name=\"p\" value=\"$project\" />Add: <input type=\"text\" name=\"t\" size=\"8\" /></form>";
5371 print "</td>\n<td>" if %$ctags;
5372 print git_show_project_tagcloud($cloud, 48);
5373 print "</td></tr>";
5376 print "</table>\n";
5378 # If XSS prevention is on, we don't include README.html.
5379 # TODO: Allow a readme in some safe format.
5380 if (!$prevent_xss && -s "$projectroot/$project/README.html") {
5381 print "<div class=\"title\">readme</div>\n" .
5382 "<div class=\"readme\">\n";
5383 insert_file("$projectroot/$project/README.html");
5384 print "\n</div>\n"; # class="readme"
5387 # we need to request one more than 16 (0..15) to check if
5388 # those 16 are all
5389 my @commitlist = $head ? parse_commits($head, 17) : ();
5390 if (@commitlist) {
5391 git_print_header_div('shortlog');
5392 git_shortlog_body(\@commitlist, 0, 15, $refs,
5393 $#commitlist <= 15 ? undef :
5394 $cgi->a({-href => href(action=>"shortlog")}, "..."));
5397 if (@taglist) {
5398 git_print_header_div('tags');
5399 git_tags_body(\@taglist, 0, 15,
5400 $#taglist <= 15 ? undef :
5401 $cgi->a({-href => href(action=>"tags")}, "..."));
5404 if (@headlist) {
5405 git_print_header_div('heads');
5406 git_heads_body(\@headlist, $head, 0, 15,
5407 $#headlist <= 15 ? undef :
5408 $cgi->a({-href => href(action=>"heads")}, "..."));
5411 if (%remotedata) {
5412 git_print_header_div('remotes');
5413 git_remotes_body(\%remotedata, 15, $head);
5416 if (@forklist) {
5417 git_print_header_div('forks');
5418 git_project_list_body(\@forklist, 'age', 0, 15,
5419 $#forklist <= 15 ? undef :
5420 $cgi->a({-href => href(action=>"forks")}, "..."),
5421 'no_header');
5424 git_footer_html();
5427 sub git_tag {
5428 my %tag = parse_tag($hash);
5430 if (! %tag) {
5431 die_error(404, "Unknown tag object");
5434 my $head = git_get_head_hash($project);
5435 git_header_html();
5436 git_print_page_nav('','', $head,undef,$head);
5437 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
5438 print "<div class=\"title_text\">\n" .
5439 "<table class=\"object_header\">\n" .
5440 "<tr>\n" .
5441 "<td>object</td>\n" .
5442 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
5443 $tag{'object'}) . "</td>\n" .
5444 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
5445 $tag{'type'}) . "</td>\n" .
5446 "</tr>\n";
5447 if (defined($tag{'author'})) {
5448 git_print_authorship_rows(\%tag, 'author');
5450 print "</table>\n\n" .
5451 "</div>\n";
5452 print "<div class=\"page_body\">";
5453 my $comment = $tag{'comment'};
5454 foreach my $line (@$comment) {
5455 chomp $line;
5456 print esc_html($line, -nbsp=>1) . "<br/>\n";
5458 print "</div>\n";
5459 git_footer_html();
5462 sub git_blame_common {
5463 my $format = shift || 'porcelain';
5464 if ($format eq 'porcelain' && $cgi->param('js')) {
5465 $format = 'incremental';
5466 $action = 'blame_incremental'; # for page title etc
5469 # permissions
5470 gitweb_check_feature('blame')
5471 or die_error(403, "Blame view not allowed");
5473 # error checking
5474 die_error(400, "No file name given") unless $file_name;
5475 $hash_base ||= git_get_head_hash($project);
5476 die_error(404, "Couldn't find base commit") unless $hash_base;
5477 my %co = parse_commit($hash_base)
5478 or die_error(404, "Commit not found");
5479 my $ftype = "blob";
5480 if (!defined $hash) {
5481 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
5482 or die_error(404, "Error looking up file");
5483 } else {
5484 $ftype = git_get_type($hash);
5485 if ($ftype !~ "blob") {
5486 die_error(400, "Object is not a blob");
5490 my $fd;
5491 if ($format eq 'incremental') {
5492 # get file contents (as base)
5493 open $fd, "-|", git_cmd(), 'cat-file', 'blob', $hash
5494 or die_error(500, "Open git-cat-file failed");
5495 } elsif ($format eq 'data') {
5496 # run git-blame --incremental
5497 open $fd, "-|", git_cmd(), "blame", "--incremental",
5498 $hash_base, "--", $file_name
5499 or die_error(500, "Open git-blame --incremental failed");
5500 } else {
5501 # run git-blame --porcelain
5502 open $fd, "-|", git_cmd(), "blame", '-p',
5503 $hash_base, '--', $file_name
5504 or die_error(500, "Open git-blame --porcelain failed");
5507 # incremental blame data returns early
5508 if ($format eq 'data') {
5509 print $cgi->header(
5510 -type=>"text/plain", -charset => "utf-8",
5511 -status=> "200 OK");
5512 local $| = 1; # output autoflush
5513 print while <$fd>;
5514 close $fd
5515 or print "ERROR $!\n";
5517 print 'END';
5518 if (defined $t0 && gitweb_check_feature('timed')) {
5519 print ' '.
5520 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).
5521 ' '.$number_of_git_cmds;
5523 print "\n";
5525 return;
5528 # page header
5529 git_header_html();
5530 my $formats_nav =
5531 $cgi->a({-href => href(action=>"blob", -replay=>1)},
5532 "blob") .
5533 " | ";
5534 if ($format eq 'incremental') {
5535 $formats_nav .=
5536 $cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},
5537 "blame") . " (non-incremental)";
5538 } else {
5539 $formats_nav .=
5540 $cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},
5541 "blame") . " (incremental)";
5543 $formats_nav .=
5544 " | " .
5545 $cgi->a({-href => href(action=>"history", -replay=>1)},
5546 "history") .
5547 " | " .
5548 $cgi->a({-href => href(action=>$action, file_name=>$file_name)},
5549 "HEAD");
5550 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
5551 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5552 git_print_page_path($file_name, $ftype, $hash_base);
5554 # page body
5555 if ($format eq 'incremental') {
5556 print "<noscript>\n<div class=\"error\"><center><b>\n".
5557 "This page requires JavaScript to run.\n Use ".
5558 $cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},
5559 'this page').
5560 " instead.\n".
5561 "</b></center></div>\n</noscript>\n";
5563 print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;
5566 print qq!<div class="page_body">\n!;
5567 print qq!<div id="progress_info">... / ...</div>\n!
5568 if ($format eq 'incremental');
5569 print qq!<table id="blame_table" class="blame" width="100%">\n!.
5570 #qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.
5571 qq!<thead>\n!.
5572 qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.
5573 qq!</thead>\n!.
5574 qq!<tbody>\n!;
5576 my @rev_color = qw(light dark);
5577 my $num_colors = scalar(@rev_color);
5578 my $current_color = 0;
5580 if ($format eq 'incremental') {
5581 my $color_class = $rev_color[$current_color];
5583 #contents of a file
5584 my $linenr = 0;
5585 LINE:
5586 while (my $line = <$fd>) {
5587 chomp $line;
5588 $linenr++;
5590 print qq!<tr id="l$linenr" class="$color_class">!.
5591 qq!<td class="sha1"><a href=""> </a></td>!.
5592 qq!<td class="linenr">!.
5593 qq!<a class="linenr" href="">$linenr</a></td>!;
5594 print qq!<td class="pre">! . esc_html($line) . "</td>\n";
5595 print qq!</tr>\n!;
5598 } else { # porcelain, i.e. ordinary blame
5599 my %metainfo = (); # saves information about commits
5601 # blame data
5602 LINE:
5603 while (my $line = <$fd>) {
5604 chomp $line;
5605 # the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]
5606 # no <lines in group> for subsequent lines in group of lines
5607 my ($full_rev, $orig_lineno, $lineno, $group_size) =
5608 ($line =~ /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);
5609 if (!exists $metainfo{$full_rev}) {
5610 $metainfo{$full_rev} = { 'nprevious' => 0 };
5612 my $meta = $metainfo{$full_rev};
5613 my $data;
5614 while ($data = <$fd>) {
5615 chomp $data;
5616 last if ($data =~ s/^\t//); # contents of line
5617 if ($data =~ /^(\S+)(?: (.*))?$/) {
5618 $meta->{$1} = $2 unless exists $meta->{$1};
5620 if ($data =~ /^previous /) {
5621 $meta->{'nprevious'}++;
5624 my $short_rev = substr($full_rev, 0, 8);
5625 my $author = $meta->{'author'};
5626 my %date =
5627 parse_date($meta->{'author-time'}, $meta->{'author-tz'});
5628 my $date = $date{'iso-tz'};
5629 if ($group_size) {
5630 $current_color = ($current_color + 1) % $num_colors;
5632 my $tr_class = $rev_color[$current_color];
5633 $tr_class .= ' boundary' if (exists $meta->{'boundary'});
5634 $tr_class .= ' no-previous' if ($meta->{'nprevious'} == 0);
5635 $tr_class .= ' multiple-previous' if ($meta->{'nprevious'} > 1);
5636 print "<tr id=\"l$lineno\" class=\"$tr_class\">\n";
5637 if ($group_size) {
5638 print "<td class=\"sha1\"";
5639 print " title=\"". esc_html($author) . ", $date\"";
5640 print " rowspan=\"$group_size\"" if ($group_size > 1);
5641 print ">";
5642 print $cgi->a({-href => href(action=>"commit",
5643 hash=>$full_rev,
5644 file_name=>$file_name)},
5645 esc_html($short_rev));
5646 if ($group_size >= 2) {
5647 my @author_initials = ($author =~ /\b([[:upper:]])\B/g);
5648 if (@author_initials) {
5649 print "<br />" .
5650 esc_html(join('', @author_initials));
5651 # or join('.', ...)
5654 print "</td>\n";
5656 # 'previous' <sha1 of parent commit> <filename at commit>
5657 if (exists $meta->{'previous'} &&
5658 $meta->{'previous'} =~ /^([a-fA-F0-9]{40}) (.*)$/) {
5659 $meta->{'parent'} = $1;
5660 $meta->{'file_parent'} = unquote($2);
5662 my $linenr_commit =
5663 exists($meta->{'parent'}) ?
5664 $meta->{'parent'} : $full_rev;
5665 my $linenr_filename =
5666 exists($meta->{'file_parent'}) ?
5667 $meta->{'file_parent'} : unquote($meta->{'filename'});
5668 my $blamed = href(action => 'blame',
5669 file_name => $linenr_filename,
5670 hash_base => $linenr_commit);
5671 print "<td class=\"linenr\">";
5672 print $cgi->a({ -href => "$blamed#l$orig_lineno",
5673 -class => "linenr" },
5674 esc_html($lineno));
5675 print "</td>";
5676 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
5677 print "</tr>\n";
5678 } # end while
5682 # footer
5683 print "</tbody>\n".
5684 "</table>\n"; # class="blame"
5685 print "</div>\n"; # class="blame_body"
5686 close $fd
5687 or print "Reading blob failed\n";
5689 git_footer_html();
5692 sub git_blame {
5693 git_blame_common();
5696 sub git_blame_incremental {
5697 git_blame_common('incremental');
5700 sub git_blame_data {
5701 git_blame_common('data');
5704 sub git_tags {
5705 my $head = git_get_head_hash($project);
5706 git_header_html();
5707 git_print_page_nav('','', $head,undef,$head,format_ref_views('tags'));
5708 git_print_header_div('summary', $project);
5710 my @tagslist = git_get_tags_list();
5711 if (@tagslist) {
5712 git_tags_body(\@tagslist);
5714 git_footer_html();
5717 sub git_heads {
5718 my $head = git_get_head_hash($project);
5719 git_header_html();
5720 git_print_page_nav('','', $head,undef,$head,format_ref_views('heads'));
5721 git_print_header_div('summary', $project);
5723 my @headslist = git_get_heads_list();
5724 if (@headslist) {
5725 git_heads_body(\@headslist, $head);
5727 git_footer_html();
5730 # used both for single remote view and for list of all the remotes
5731 sub git_remotes {
5732 gitweb_check_feature('remote_heads')
5733 or die_error(403, "Remote heads view is disabled");
5735 my $head = git_get_head_hash($project);
5736 my $remote = $input_params{'hash'};
5738 my $remotedata = git_get_remotes_list($remote);
5739 die_error(500, "Unable to get remote information") unless defined $remotedata;
5741 unless (%$remotedata) {
5742 die_error(404, defined $remote ?
5743 "Remote $remote not found" :
5744 "No remotes found");
5747 git_header_html(undef, undef, -action_extra => $remote);
5748 git_print_page_nav('', '', $head, undef, $head,
5749 format_ref_views($remote ? '' : 'remotes'));
5751 fill_remote_heads($remotedata);
5752 if (defined $remote) {
5753 git_print_header_div('remotes', "$remote remote for $project");
5754 git_remote_block($remote, $remotedata->{$remote}, undef, $head);
5755 } else {
5756 git_print_header_div('summary', "$project remotes");
5757 git_remotes_body($remotedata, undef, $head);
5760 git_footer_html();
5763 sub git_blob_plain {
5764 my $type = shift;
5765 my $expires;
5767 if (!defined $hash) {
5768 if (defined $file_name) {
5769 my $base = $hash_base || git_get_head_hash($project);
5770 $hash = git_get_hash_by_path($base, $file_name, "blob")
5771 or die_error(404, "Cannot find file");
5772 } else {
5773 die_error(400, "No file name defined");
5775 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5776 # blobs defined by non-textual hash id's can be cached
5777 $expires = "+1d";
5780 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
5781 or die_error(500, "Open git-cat-file blob '$hash' failed");
5783 # content-type (can include charset)
5784 $type = blob_contenttype($fd, $file_name, $type);
5786 # "save as" filename, even when no $file_name is given
5787 my $save_as = "$hash";
5788 if (defined $file_name) {
5789 $save_as = $file_name;
5790 } elsif ($type =~ m/^text\//) {
5791 $save_as .= '.txt';
5794 # With XSS prevention on, blobs of all types except a few known safe
5795 # ones are served with "Content-Disposition: attachment" to make sure
5796 # they don't run in our security domain. For certain image types,
5797 # blob view writes an <img> tag referring to blob_plain view, and we
5798 # want to be sure not to break that by serving the image as an
5799 # attachment (though Firefox 3 doesn't seem to care).
5800 my $sandbox = $prevent_xss &&
5801 $type !~ m!^(?:text/plain|image/(?:gif|png|jpeg))$!;
5803 print $cgi->header(
5804 -type => $type,
5805 -expires => $expires,
5806 -content_disposition =>
5807 ($sandbox ? 'attachment' : 'inline')
5808 . '; filename="' . $save_as . '"');
5809 local $/ = undef;
5810 binmode STDOUT, ':raw';
5811 print <$fd>;
5812 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
5813 close $fd;
5816 sub git_blob {
5817 my $expires;
5819 if (!defined $hash) {
5820 if (defined $file_name) {
5821 my $base = $hash_base || git_get_head_hash($project);
5822 $hash = git_get_hash_by_path($base, $file_name, "blob")
5823 or die_error(404, "Cannot find file");
5824 } else {
5825 die_error(400, "No file name defined");
5827 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5828 # blobs defined by non-textual hash id's can be cached
5829 $expires = "+1d";
5832 my $have_blame = gitweb_check_feature('blame');
5833 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
5834 or die_error(500, "Couldn't cat $file_name, $hash");
5835 my $mimetype = blob_mimetype($fd, $file_name);
5836 # use 'blob_plain' (aka 'raw') view for files that cannot be displayed
5837 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {
5838 close $fd;
5839 return git_blob_plain($mimetype);
5841 # we can have blame only for text/* mimetype
5842 $have_blame &&= ($mimetype =~ m!^text/!);
5844 my $highlight = gitweb_check_feature('highlight');
5845 my $syntax = guess_file_syntax($highlight, $mimetype, $file_name);
5846 $fd = run_highlighter($fd, $highlight, $syntax)
5847 if $syntax;
5849 git_header_html(undef, $expires);
5850 my $formats_nav = '';
5851 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
5852 if (defined $file_name) {
5853 if ($have_blame) {
5854 $formats_nav .=
5855 $cgi->a({-href => href(action=>"blame", -replay=>1)},
5856 "blame") .
5857 " | ";
5859 $formats_nav .=
5860 $cgi->a({-href => href(action=>"history", -replay=>1)},
5861 "history") .
5862 " | " .
5863 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
5864 "raw") .
5865 " | " .
5866 $cgi->a({-href => href(action=>"blob",
5867 hash_base=>"HEAD", file_name=>$file_name)},
5868 "HEAD");
5869 } else {
5870 $formats_nav .=
5871 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
5872 "raw");
5874 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
5875 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5876 } else {
5877 print "<div class=\"page_nav\">\n" .
5878 "<br/><br/></div>\n" .
5879 "<div class=\"title\">$hash</div>\n";
5881 git_print_page_path($file_name, "blob", $hash_base);
5882 print "<div class=\"page_body\">\n";
5883 if ($mimetype =~ m!^image/!) {
5884 print qq!<img type="$mimetype"!;
5885 if ($file_name) {
5886 print qq! alt="$file_name" title="$file_name"!;
5888 print qq! src="! .
5889 href(action=>"blob_plain", hash=>$hash,
5890 hash_base=>$hash_base, file_name=>$file_name) .
5891 qq!" />\n!;
5892 } else {
5893 my $nr;
5894 while (my $line = <$fd>) {
5895 chomp $line;
5896 $nr++;
5897 $line = untabify($line);
5898 printf qq!<div class="pre"><a id="l%i" href="%s#l%i" class="linenr">%4i</a> %s</div>\n!,
5899 $nr, href(-replay => 1), $nr, $nr, $syntax ? $line : esc_html($line, -nbsp=>1);
5902 close $fd
5903 or print "Reading blob failed.\n";
5904 print "</div>";
5905 git_footer_html();
5908 sub git_tree {
5909 if (!defined $hash_base) {
5910 $hash_base = "HEAD";
5912 if (!defined $hash) {
5913 if (defined $file_name) {
5914 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
5915 } else {
5916 $hash = $hash_base;
5919 die_error(404, "No such tree") unless defined($hash);
5921 my $show_sizes = gitweb_check_feature('show-sizes');
5922 my $have_blame = gitweb_check_feature('blame');
5924 my @entries = ();
5926 local $/ = "\0";
5927 open my $fd, "-|", git_cmd(), "ls-tree", '-z',
5928 ($show_sizes ? '-l' : ()), @extra_options, $hash
5929 or die_error(500, "Open git-ls-tree failed");
5930 @entries = map { chomp; $_ } <$fd>;
5931 close $fd
5932 or die_error(404, "Reading tree failed");
5935 my $refs = git_get_references();
5936 my $ref = format_ref_marker($refs, $hash_base);
5937 git_header_html();
5938 my $basedir = '';
5939 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
5940 my @views_nav = ();
5941 if (defined $file_name) {
5942 push @views_nav,
5943 $cgi->a({-href => href(action=>"history", -replay=>1)},
5944 "history"),
5945 $cgi->a({-href => href(action=>"tree",
5946 hash_base=>"HEAD", file_name=>$file_name)},
5947 "HEAD"),
5949 my $snapshot_links = format_snapshot_links($hash);
5950 if (defined $snapshot_links) {
5951 # FIXME: Should be available when we have no hash base as well.
5952 push @views_nav, $snapshot_links;
5954 git_print_page_nav('tree','', $hash_base, undef, undef,
5955 join(' | ', @views_nav));
5956 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
5957 } else {
5958 undef $hash_base;
5959 print "<div class=\"page_nav\">\n";
5960 print "<br/><br/></div>\n";
5961 print "<div class=\"title\">$hash</div>\n";
5963 if (defined $file_name) {
5964 $basedir = $file_name;
5965 if ($basedir ne '' && substr($basedir, -1) ne '/') {
5966 $basedir .= '/';
5968 git_print_page_path($file_name, 'tree', $hash_base);
5970 print "<div class=\"page_body\">\n";
5971 print "<table class=\"tree\">\n";
5972 my $alternate = 1;
5973 # '..' (top directory) link if possible
5974 if (defined $hash_base &&
5975 defined $file_name && $file_name =~ m![^/]+$!) {
5976 if ($alternate) {
5977 print "<tr class=\"dark\">\n";
5978 } else {
5979 print "<tr class=\"light\">\n";
5981 $alternate ^= 1;
5983 my $up = $file_name;
5984 $up =~ s!/?[^/]+$!!;
5985 undef $up unless $up;
5986 # based on git_print_tree_entry
5987 print '<td class="mode">' . mode_str('040000') . "</td>\n";
5988 print '<td class="size">&nbsp;</td>'."\n" if $show_sizes;
5989 print '<td class="list">';
5990 print $cgi->a({-href => href(action=>"tree",
5991 hash_base=>$hash_base,
5992 file_name=>$up)},
5993 "..");
5994 print "</td>\n";
5995 print "<td class=\"link\"></td>\n";
5997 print "</tr>\n";
5999 foreach my $line (@entries) {
6000 my %t = parse_ls_tree_line($line, -z => 1, -l => $show_sizes);
6002 if ($alternate) {
6003 print "<tr class=\"dark\">\n";
6004 } else {
6005 print "<tr class=\"light\">\n";
6007 $alternate ^= 1;
6009 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
6011 print "</tr>\n";
6013 print "</table>\n" .
6014 "</div>";
6015 git_footer_html();
6018 sub snapshot_name {
6019 my ($project, $hash) = @_;
6021 # path/to/project.git -> project
6022 # path/to/project/.git -> project
6023 my $name = to_utf8($project);
6024 $name =~ s,([^/])/*\.git$,$1,;
6025 $name = basename($name);
6026 # sanitize name
6027 $name =~ s/[[:cntrl:]]/?/g;
6029 my $ver = $hash;
6030 if ($hash =~ /^[0-9a-fA-F]+$/) {
6031 # shorten SHA-1 hash
6032 my $full_hash = git_get_full_hash($project, $hash);
6033 if ($full_hash =~ /^$hash/ && length($hash) > 7) {
6034 $ver = git_get_short_hash($project, $hash);
6036 } elsif ($hash =~ m!^refs/tags/(.*)$!) {
6037 # tags don't need shortened SHA-1 hash
6038 $ver = $1;
6039 } else {
6040 # branches and other need shortened SHA-1 hash
6041 if ($hash =~ m!^refs/(?:heads|remotes)/(.*)$!) {
6042 $ver = $1;
6044 $ver .= '-' . git_get_short_hash($project, $hash);
6046 # in case of hierarchical branch names
6047 $ver =~ s!/!.!g;
6049 # name = project-version_string
6050 $name = "$name-$ver";
6052 return wantarray ? ($name, $name) : $name;
6055 sub git_snapshot {
6056 my $format = $input_params{'snapshot_format'};
6057 if (!@snapshot_fmts) {
6058 die_error(403, "Snapshots not allowed");
6060 # default to first supported snapshot format
6061 $format ||= $snapshot_fmts[0];
6062 if ($format !~ m/^[a-z0-9]+$/) {
6063 die_error(400, "Invalid snapshot format parameter");
6064 } elsif (!exists($known_snapshot_formats{$format})) {
6065 die_error(400, "Unknown snapshot format");
6066 } elsif ($known_snapshot_formats{$format}{'disabled'}) {
6067 die_error(403, "Snapshot format not allowed");
6068 } elsif (!grep($_ eq $format, @snapshot_fmts)) {
6069 die_error(403, "Unsupported snapshot format");
6072 my $type = git_get_type("$hash^{}");
6073 if (!$type) {
6074 die_error(404, 'Object does not exist');
6075 } elsif ($type eq 'blob') {
6076 die_error(400, 'Object is not a tree-ish');
6079 my ($name, $prefix) = snapshot_name($project, $hash);
6080 my $filename = "$name$known_snapshot_formats{$format}{'suffix'}";
6081 my $cmd = quote_command(
6082 git_cmd(), 'archive',
6083 "--format=$known_snapshot_formats{$format}{'format'}",
6084 "--prefix=$prefix/", $hash);
6085 if (exists $known_snapshot_formats{$format}{'compressor'}) {
6086 $cmd .= ' | ' . quote_command(@{$known_snapshot_formats{$format}{'compressor'}});
6089 $filename =~ s/(["\\])/\\$1/g;
6090 print $cgi->header(
6091 -type => $known_snapshot_formats{$format}{'type'},
6092 -content_disposition => 'inline; filename="' . $filename . '"',
6093 -status => '200 OK');
6095 open my $fd, "-|", $cmd
6096 or die_error(500, "Execute git-archive failed");
6097 binmode STDOUT, ':raw';
6098 print <$fd>;
6099 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
6100 close $fd;
6103 sub git_log_generic {
6104 my ($fmt_name, $body_subr, $base, $parent, $file_name, $file_hash) = @_;
6106 my $head = git_get_head_hash($project);
6107 if (!defined $base) {
6108 $base = $head;
6110 if (!defined $page) {
6111 $page = 0;
6113 my $refs = git_get_references();
6115 my $commit_hash = $base;
6116 if (defined $parent) {
6117 $commit_hash = "$parent..$base";
6119 my @commitlist =
6120 parse_commits($commit_hash, 101, (100 * $page),
6121 defined $file_name ? ($file_name, "--full-history") : ());
6123 my $ftype;
6124 if (!defined $file_hash && defined $file_name) {
6125 # some commits could have deleted file in question,
6126 # and not have it in tree, but one of them has to have it
6127 for (my $i = 0; $i < @commitlist; $i++) {
6128 $file_hash = git_get_hash_by_path($commitlist[$i]{'id'}, $file_name);
6129 last if defined $file_hash;
6132 if (defined $file_hash) {
6133 $ftype = git_get_type($file_hash);
6135 if (defined $file_name && !defined $ftype) {
6136 die_error(500, "Unknown type of object");
6138 my %co;
6139 if (defined $file_name) {
6140 %co = parse_commit($base)
6141 or die_error(404, "Unknown commit object");
6145 my $paging_nav = format_paging_nav($fmt_name, $page, $#commitlist >= 100);
6146 my $next_link = '';
6147 if ($#commitlist >= 100) {
6148 $next_link =
6149 $cgi->a({-href => href(-replay=>1, page=>$page+1),
6150 -accesskey => "n", -title => "Alt-n"}, "next");
6152 my $patch_max = gitweb_get_feature('patches');
6153 if ($patch_max && !defined $file_name) {
6154 if ($patch_max < 0 || @commitlist <= $patch_max) {
6155 $paging_nav .= " &sdot; " .
6156 $cgi->a({-href => href(action=>"patches", -replay=>1)},
6157 "patches");
6161 git_header_html();
6162 git_print_page_nav($fmt_name,'', $hash,$hash,$hash, $paging_nav);
6163 if (defined $file_name) {
6164 git_print_header_div('commit', esc_html($co{'title'}), $base);
6165 } else {
6166 git_print_header_div('summary', $project)
6168 git_print_page_path($file_name, $ftype, $hash_base)
6169 if (defined $file_name);
6171 $body_subr->(\@commitlist, 0, 99, $refs, $next_link,
6172 $file_name, $file_hash, $ftype);
6174 git_footer_html();
6177 sub git_log {
6178 git_log_generic('log', \&git_log_body,
6179 $hash, $hash_parent);
6182 sub git_commit {
6183 $hash ||= $hash_base || "HEAD";
6184 my %co = parse_commit($hash)
6185 or die_error(404, "Unknown commit object");
6187 my $parent = $co{'parent'};
6188 my $parents = $co{'parents'}; # listref
6190 # we need to prepare $formats_nav before any parameter munging
6191 my $formats_nav;
6192 if (!defined $parent) {
6193 # --root commitdiff
6194 $formats_nav .= '(initial)';
6195 } elsif (@$parents == 1) {
6196 # single parent commit
6197 $formats_nav .=
6198 '(parent: ' .
6199 $cgi->a({-href => href(action=>"commit",
6200 hash=>$parent)},
6201 esc_html(substr($parent, 0, 7))) .
6202 ')';
6203 } else {
6204 # merge commit
6205 $formats_nav .=
6206 '(merge: ' .
6207 join(' ', map {
6208 $cgi->a({-href => href(action=>"commit",
6209 hash=>$_)},
6210 esc_html(substr($_, 0, 7)));
6211 } @$parents ) .
6212 ')';
6214 if (gitweb_check_feature('patches') && @$parents <= 1) {
6215 $formats_nav .= " | " .
6216 $cgi->a({-href => href(action=>"patch", -replay=>1)},
6217 "patch");
6220 if (!defined $parent) {
6221 $parent = "--root";
6223 my @difftree;
6224 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
6225 @diff_opts,
6226 (@$parents <= 1 ? $parent : '-c'),
6227 $hash, "--"
6228 or die_error(500, "Open git-diff-tree failed");
6229 @difftree = map { chomp; $_ } <$fd>;
6230 close $fd or die_error(404, "Reading git-diff-tree failed");
6232 # non-textual hash id's can be cached
6233 my $expires;
6234 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6235 $expires = "+1d";
6237 my $refs = git_get_references();
6238 my $ref = format_ref_marker($refs, $co{'id'});
6240 git_header_html(undef, $expires);
6241 git_print_page_nav('commit', '',
6242 $hash, $co{'tree'}, $hash,
6243 $formats_nav);
6245 if (defined $co{'parent'}) {
6246 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
6247 } else {
6248 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
6250 print "<div class=\"title_text\">\n" .
6251 "<table class=\"object_header\">\n";
6252 git_print_authorship_rows(\%co);
6253 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
6254 print "<tr>" .
6255 "<td>tree</td>" .
6256 "<td class=\"sha1\">" .
6257 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
6258 class => "list"}, $co{'tree'}) .
6259 "</td>" .
6260 "<td class=\"link\">" .
6261 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
6262 "tree");
6263 my $snapshot_links = format_snapshot_links($hash);
6264 if (defined $snapshot_links) {
6265 print " | " . $snapshot_links;
6267 print "</td>" .
6268 "</tr>\n";
6270 foreach my $par (@$parents) {
6271 print "<tr>" .
6272 "<td>parent</td>" .
6273 "<td class=\"sha1\">" .
6274 $cgi->a({-href => href(action=>"commit", hash=>$par),
6275 class => "list"}, $par) .
6276 "</td>" .
6277 "<td class=\"link\">" .
6278 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
6279 " | " .
6280 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
6281 "</td>" .
6282 "</tr>\n";
6284 print "</table>".
6285 "</div>\n";
6287 print "<div class=\"page_body\">\n";
6288 git_print_log($co{'comment'});
6289 print "</div>\n";
6291 git_difftree_body(\@difftree, $hash, @$parents);
6293 git_footer_html();
6296 sub git_object {
6297 # object is defined by:
6298 # - hash or hash_base alone
6299 # - hash_base and file_name
6300 my $type;
6302 # - hash or hash_base alone
6303 if ($hash || ($hash_base && !defined $file_name)) {
6304 my $object_id = $hash || $hash_base;
6306 open my $fd, "-|", quote_command(
6307 git_cmd(), 'cat-file', '-t', $object_id) . ' 2> /dev/null'
6308 or die_error(404, "Object does not exist");
6309 $type = <$fd>;
6310 chomp $type;
6311 close $fd
6312 or die_error(404, "Object does not exist");
6314 # - hash_base and file_name
6315 } elsif ($hash_base && defined $file_name) {
6316 $file_name =~ s,/+$,,;
6318 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
6319 or die_error(404, "Base object does not exist");
6321 # here errors should not hapen
6322 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
6323 or die_error(500, "Open git-ls-tree failed");
6324 my $line = <$fd>;
6325 close $fd;
6327 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
6328 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
6329 die_error(404, "File or directory for given base does not exist");
6331 $type = $2;
6332 $hash = $3;
6333 } else {
6334 die_error(400, "Not enough information to find object");
6337 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
6338 hash=>$hash, hash_base=>$hash_base,
6339 file_name=>$file_name),
6340 -status => '302 Found');
6343 sub git_blobdiff {
6344 my $format = shift || 'html';
6346 my $fd;
6347 my @difftree;
6348 my %diffinfo;
6349 my $expires;
6351 # preparing $fd and %diffinfo for git_patchset_body
6352 # new style URI
6353 if (defined $hash_base && defined $hash_parent_base) {
6354 if (defined $file_name) {
6355 # read raw output
6356 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6357 $hash_parent_base, $hash_base,
6358 "--", (defined $file_parent ? $file_parent : ()), $file_name
6359 or die_error(500, "Open git-diff-tree failed");
6360 @difftree = map { chomp; $_ } <$fd>;
6361 close $fd
6362 or die_error(404, "Reading git-diff-tree failed");
6363 @difftree
6364 or die_error(404, "Blob diff not found");
6366 } elsif (defined $hash &&
6367 $hash =~ /[0-9a-fA-F]{40}/) {
6368 # try to find filename from $hash
6370 # read filtered raw output
6371 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6372 $hash_parent_base, $hash_base, "--"
6373 or die_error(500, "Open git-diff-tree failed");
6374 @difftree =
6375 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
6376 # $hash == to_id
6377 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
6378 map { chomp; $_ } <$fd>;
6379 close $fd
6380 or die_error(404, "Reading git-diff-tree failed");
6381 @difftree
6382 or die_error(404, "Blob diff not found");
6384 } else {
6385 die_error(400, "Missing one of the blob diff parameters");
6388 if (@difftree > 1) {
6389 die_error(400, "Ambiguous blob diff specification");
6392 %diffinfo = parse_difftree_raw_line($difftree[0]);
6393 $file_parent ||= $diffinfo{'from_file'} || $file_name;
6394 $file_name ||= $diffinfo{'to_file'};
6396 $hash_parent ||= $diffinfo{'from_id'};
6397 $hash ||= $diffinfo{'to_id'};
6399 # non-textual hash id's can be cached
6400 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
6401 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
6402 $expires = '+1d';
6405 # open patch output
6406 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6407 '-p', ($format eq 'html' ? "--full-index" : ()),
6408 $hash_parent_base, $hash_base,
6409 "--", (defined $file_parent ? $file_parent : ()), $file_name
6410 or die_error(500, "Open git-diff-tree failed");
6413 # old/legacy style URI -- not generated anymore since 1.4.3.
6414 if (!%diffinfo) {
6415 die_error('404 Not Found', "Missing one of the blob diff parameters")
6418 # header
6419 if ($format eq 'html') {
6420 my $formats_nav =
6421 $cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},
6422 "raw");
6423 git_header_html(undef, $expires);
6424 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
6425 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
6426 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
6427 } else {
6428 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
6429 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
6431 if (defined $file_name) {
6432 git_print_page_path($file_name, "blob", $hash_base);
6433 } else {
6434 print "<div class=\"page_path\"></div>\n";
6437 } elsif ($format eq 'plain') {
6438 print $cgi->header(
6439 -type => 'text/plain',
6440 -charset => 'utf-8',
6441 -expires => $expires,
6442 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
6444 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
6446 } else {
6447 die_error(400, "Unknown blobdiff format");
6450 # patch
6451 if ($format eq 'html') {
6452 print "<div class=\"page_body\">\n";
6454 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
6455 close $fd;
6457 print "</div>\n"; # class="page_body"
6458 git_footer_html();
6460 } else {
6461 while (my $line = <$fd>) {
6462 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
6463 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
6465 print $line;
6467 last if $line =~ m!^\+\+\+!;
6469 local $/ = undef;
6470 print <$fd>;
6471 close $fd;
6475 sub git_blobdiff_plain {
6476 git_blobdiff('plain');
6479 sub git_commitdiff {
6480 my %params = @_;
6481 my $format = $params{-format} || 'html';
6483 my ($patch_max) = gitweb_get_feature('patches');
6484 if ($format eq 'patch') {
6485 die_error(403, "Patch view not allowed") unless $patch_max;
6488 $hash ||= $hash_base || "HEAD";
6489 my %co = parse_commit($hash)
6490 or die_error(404, "Unknown commit object");
6492 # choose format for commitdiff for merge
6493 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
6494 $hash_parent = '--cc';
6496 # we need to prepare $formats_nav before almost any parameter munging
6497 my $formats_nav;
6498 if ($format eq 'html') {
6499 $formats_nav =
6500 $cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
6501 "raw");
6502 if ($patch_max && @{$co{'parents'}} <= 1) {
6503 $formats_nav .= " | " .
6504 $cgi->a({-href => href(action=>"patch", -replay=>1)},
6505 "patch");
6508 if (defined $hash_parent &&
6509 $hash_parent ne '-c' && $hash_parent ne '--cc') {
6510 # commitdiff with two commits given
6511 my $hash_parent_short = $hash_parent;
6512 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
6513 $hash_parent_short = substr($hash_parent, 0, 7);
6515 $formats_nav .=
6516 ' (from';
6517 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
6518 if ($co{'parents'}[$i] eq $hash_parent) {
6519 $formats_nav .= ' parent ' . ($i+1);
6520 last;
6523 $formats_nav .= ': ' .
6524 $cgi->a({-href => href(action=>"commitdiff",
6525 hash=>$hash_parent)},
6526 esc_html($hash_parent_short)) .
6527 ')';
6528 } elsif (!$co{'parent'}) {
6529 # --root commitdiff
6530 $formats_nav .= ' (initial)';
6531 } elsif (scalar @{$co{'parents'}} == 1) {
6532 # single parent commit
6533 $formats_nav .=
6534 ' (parent: ' .
6535 $cgi->a({-href => href(action=>"commitdiff",
6536 hash=>$co{'parent'})},
6537 esc_html(substr($co{'parent'}, 0, 7))) .
6538 ')';
6539 } else {
6540 # merge commit
6541 if ($hash_parent eq '--cc') {
6542 $formats_nav .= ' | ' .
6543 $cgi->a({-href => href(action=>"commitdiff",
6544 hash=>$hash, hash_parent=>'-c')},
6545 'combined');
6546 } else { # $hash_parent eq '-c'
6547 $formats_nav .= ' | ' .
6548 $cgi->a({-href => href(action=>"commitdiff",
6549 hash=>$hash, hash_parent=>'--cc')},
6550 'compact');
6552 $formats_nav .=
6553 ' (merge: ' .
6554 join(' ', map {
6555 $cgi->a({-href => href(action=>"commitdiff",
6556 hash=>$_)},
6557 esc_html(substr($_, 0, 7)));
6558 } @{$co{'parents'}} ) .
6559 ')';
6563 my $hash_parent_param = $hash_parent;
6564 if (!defined $hash_parent_param) {
6565 # --cc for multiple parents, --root for parentless
6566 $hash_parent_param =
6567 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
6570 # read commitdiff
6571 my $fd;
6572 my @difftree;
6573 if ($format eq 'html') {
6574 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6575 "--no-commit-id", "--patch-with-raw", "--full-index",
6576 $hash_parent_param, $hash, "--"
6577 or die_error(500, "Open git-diff-tree failed");
6579 while (my $line = <$fd>) {
6580 chomp $line;
6581 # empty line ends raw part of diff-tree output
6582 last unless $line;
6583 push @difftree, scalar parse_difftree_raw_line($line);
6586 } elsif ($format eq 'plain') {
6587 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6588 '-p', $hash_parent_param, $hash, "--"
6589 or die_error(500, "Open git-diff-tree failed");
6590 } elsif ($format eq 'patch') {
6591 # For commit ranges, we limit the output to the number of
6592 # patches specified in the 'patches' feature.
6593 # For single commits, we limit the output to a single patch,
6594 # diverging from the git-format-patch default.
6595 my @commit_spec = ();
6596 if ($hash_parent) {
6597 if ($patch_max > 0) {
6598 push @commit_spec, "-$patch_max";
6600 push @commit_spec, '-n', "$hash_parent..$hash";
6601 } else {
6602 if ($params{-single}) {
6603 push @commit_spec, '-1';
6604 } else {
6605 if ($patch_max > 0) {
6606 push @commit_spec, "-$patch_max";
6608 push @commit_spec, "-n";
6610 push @commit_spec, '--root', $hash;
6612 open $fd, "-|", git_cmd(), "format-patch", @diff_opts,
6613 '--encoding=utf8', '--stdout', @commit_spec
6614 or die_error(500, "Open git-format-patch failed");
6615 } else {
6616 die_error(400, "Unknown commitdiff format");
6619 # non-textual hash id's can be cached
6620 my $expires;
6621 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6622 $expires = "+1d";
6625 # write commit message
6626 if ($format eq 'html') {
6627 my $refs = git_get_references();
6628 my $ref = format_ref_marker($refs, $co{'id'});
6630 git_header_html(undef, $expires);
6631 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
6632 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
6633 print "<div class=\"title_text\">\n" .
6634 "<table class=\"object_header\">\n";
6635 git_print_authorship_rows(\%co);
6636 print "</table>".
6637 "</div>\n";
6638 print "<div class=\"page_body\">\n";
6639 if (@{$co{'comment'}} > 1) {
6640 print "<div class=\"log\">\n";
6641 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
6642 print "</div>\n"; # class="log"
6645 } elsif ($format eq 'plain') {
6646 my $refs = git_get_references("tags");
6647 my $tagname = git_get_rev_name_tags($hash);
6648 my $filename = basename($project) . "-$hash.patch";
6650 print $cgi->header(
6651 -type => 'text/plain',
6652 -charset => 'utf-8',
6653 -expires => $expires,
6654 -content_disposition => 'inline; filename="' . "$filename" . '"');
6655 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
6656 print "From: " . to_utf8($co{'author'}) . "\n";
6657 print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
6658 print "Subject: " . to_utf8($co{'title'}) . "\n";
6660 print "X-Git-Tag: $tagname\n" if $tagname;
6661 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
6663 foreach my $line (@{$co{'comment'}}) {
6664 print to_utf8($line) . "\n";
6666 print "---\n\n";
6667 } elsif ($format eq 'patch') {
6668 my $filename = basename($project) . "-$hash.patch";
6670 print $cgi->header(
6671 -type => 'text/plain',
6672 -charset => 'utf-8',
6673 -expires => $expires,
6674 -content_disposition => 'inline; filename="' . "$filename" . '"');
6677 # write patch
6678 if ($format eq 'html') {
6679 my $use_parents = !defined $hash_parent ||
6680 $hash_parent eq '-c' || $hash_parent eq '--cc';
6681 git_difftree_body(\@difftree, $hash,
6682 $use_parents ? @{$co{'parents'}} : $hash_parent);
6683 print "<br/>\n";
6685 git_patchset_body($fd, \@difftree, $hash,
6686 $use_parents ? @{$co{'parents'}} : $hash_parent);
6687 close $fd;
6688 print "</div>\n"; # class="page_body"
6689 git_footer_html();
6691 } elsif ($format eq 'plain') {
6692 local $/ = undef;
6693 print <$fd>;
6694 close $fd
6695 or print "Reading git-diff-tree failed\n";
6696 } elsif ($format eq 'patch') {
6697 local $/ = undef;
6698 print <$fd>;
6699 close $fd
6700 or print "Reading git-format-patch failed\n";
6704 sub git_commitdiff_plain {
6705 git_commitdiff(-format => 'plain');
6708 # format-patch-style patches
6709 sub git_patch {
6710 git_commitdiff(-format => 'patch', -single => 1);
6713 sub git_patches {
6714 git_commitdiff(-format => 'patch');
6717 sub git_history {
6718 git_log_generic('history', \&git_history_body,
6719 $hash_base, $hash_parent_base,
6720 $file_name, $hash);
6723 sub git_search {
6724 gitweb_check_feature('search') or die_error(403, "Search is disabled");
6725 if (!defined $searchtext) {
6726 die_error(400, "Text field is empty");
6728 if (!defined $hash) {
6729 $hash = git_get_head_hash($project);
6731 my %co = parse_commit($hash);
6732 if (!%co) {
6733 die_error(404, "Unknown commit object");
6735 if (!defined $page) {
6736 $page = 0;
6739 $searchtype ||= 'commit';
6740 if ($searchtype eq 'pickaxe') {
6741 # pickaxe may take all resources of your box and run for several minutes
6742 # with every query - so decide by yourself how public you make this feature
6743 gitweb_check_feature('pickaxe')
6744 or die_error(403, "Pickaxe is disabled");
6746 if ($searchtype eq 'grep') {
6747 gitweb_check_feature('grep')
6748 or die_error(403, "Grep is disabled");
6751 git_header_html();
6753 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
6754 my $greptype;
6755 if ($searchtype eq 'commit') {
6756 $greptype = "--grep=";
6757 } elsif ($searchtype eq 'author') {
6758 $greptype = "--author=";
6759 } elsif ($searchtype eq 'committer') {
6760 $greptype = "--committer=";
6762 $greptype .= $searchtext;
6763 my @commitlist = parse_commits($hash, 101, (100 * $page), undef,
6764 $greptype, '--regexp-ignore-case',
6765 $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
6767 my $paging_nav = '';
6768 if ($page > 0) {
6769 $paging_nav .=
6770 $cgi->a({-href => href(action=>"search", hash=>$hash,
6771 searchtext=>$searchtext,
6772 searchtype=>$searchtype)},
6773 "first");
6774 $paging_nav .= " &sdot; " .
6775 $cgi->a({-href => href(-replay=>1, page=>$page-1),
6776 -accesskey => "p", -title => "Alt-p"}, "prev");
6777 } else {
6778 $paging_nav .= "first";
6779 $paging_nav .= " &sdot; prev";
6781 my $next_link = '';
6782 if ($#commitlist >= 100) {
6783 $next_link =
6784 $cgi->a({-href => href(-replay=>1, page=>$page+1),
6785 -accesskey => "n", -title => "Alt-n"}, "next");
6786 $paging_nav .= " &sdot; $next_link";
6787 } else {
6788 $paging_nav .= " &sdot; next";
6791 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
6792 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6793 if ($page == 0 && !@commitlist) {
6794 print "<p>No match.</p>\n";
6795 } else {
6796 git_search_grep_body(\@commitlist, 0, 99, $next_link);
6800 if ($searchtype eq 'pickaxe') {
6801 git_print_page_nav('','', $hash,$co{'tree'},$hash);
6802 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6804 print "<table class=\"pickaxe search\">\n";
6805 my $alternate = 1;
6806 local $/ = "\n";
6807 open my $fd, '-|', git_cmd(), '--no-pager', 'log', @diff_opts,
6808 '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
6809 ($search_use_regexp ? '--pickaxe-regex' : ());
6810 undef %co;
6811 my @files;
6812 while (my $line = <$fd>) {
6813 chomp $line;
6814 next unless $line;
6816 my %set = parse_difftree_raw_line($line);
6817 if (defined $set{'commit'}) {
6818 # finish previous commit
6819 if (%co) {
6820 print "</td>\n" .
6821 "<td class=\"link\">" .
6822 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
6823 " | " .
6824 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
6825 print "</td>\n" .
6826 "</tr>\n";
6829 if ($alternate) {
6830 print "<tr class=\"dark\">\n";
6831 } else {
6832 print "<tr class=\"light\">\n";
6834 $alternate ^= 1;
6835 %co = parse_commit($set{'commit'});
6836 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
6837 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
6838 "<td><i>$author</i></td>\n" .
6839 "<td>" .
6840 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
6841 -class => "list subject"},
6842 chop_and_escape_str($co{'title'}, 50) . "<br/>");
6843 } elsif (defined $set{'to_id'}) {
6844 next if ($set{'to_id'} =~ m/^0{40}$/);
6846 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
6847 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),
6848 -class => "list"},
6849 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
6850 "<br/>\n";
6853 close $fd;
6855 # finish last commit (warning: repetition!)
6856 if (%co) {
6857 print "</td>\n" .
6858 "<td class=\"link\">" .
6859 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
6860 " | " .
6861 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
6862 print "</td>\n" .
6863 "</tr>\n";
6866 print "</table>\n";
6869 if ($searchtype eq 'grep') {
6870 git_print_page_nav('','', $hash,$co{'tree'},$hash);
6871 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6873 print "<table class=\"grep_search\">\n";
6874 my $alternate = 1;
6875 my $matches = 0;
6876 local $/ = "\n";
6877 open my $fd, "-|", git_cmd(), 'grep', '-n',
6878 $search_use_regexp ? ('-E', '-i') : '-F',
6879 $searchtext, $co{'tree'};
6880 my $lastfile = '';
6881 while (my $line = <$fd>) {
6882 chomp $line;
6883 my ($file, $lno, $ltext, $binary);
6884 last if ($matches++ > 1000);
6885 if ($line =~ /^Binary file (.+) matches$/) {
6886 $file = $1;
6887 $binary = 1;
6888 } else {
6889 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
6891 if ($file ne $lastfile) {
6892 $lastfile and print "</td></tr>\n";
6893 if ($alternate++) {
6894 print "<tr class=\"dark\">\n";
6895 } else {
6896 print "<tr class=\"light\">\n";
6898 print "<td class=\"list\">".
6899 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
6900 file_name=>"$file"),
6901 -class => "list"}, esc_path($file));
6902 print "</td><td>\n";
6903 $lastfile = $file;
6905 if ($binary) {
6906 print "<div class=\"binary\">Binary file</div>\n";
6907 } else {
6908 $ltext = untabify($ltext);
6909 if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
6910 $ltext = esc_html($1, -nbsp=>1);
6911 $ltext .= '<span class="match">';
6912 $ltext .= esc_html($2, -nbsp=>1);
6913 $ltext .= '</span>';
6914 $ltext .= esc_html($3, -nbsp=>1);
6915 } else {
6916 $ltext = esc_html($ltext, -nbsp=>1);
6918 print "<div class=\"pre\">" .
6919 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
6920 file_name=>"$file").'#l'.$lno,
6921 -class => "linenr"}, sprintf('%4i', $lno))
6922 . ' ' . $ltext . "</div>\n";
6925 if ($lastfile) {
6926 print "</td></tr>\n";
6927 if ($matches > 1000) {
6928 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
6930 } else {
6931 print "<div class=\"diff nodifferences\">No matches found</div>\n";
6933 close $fd;
6935 print "</table>\n";
6937 git_footer_html();
6940 sub git_search_help {
6941 git_header_html();
6942 git_print_page_nav('','', $hash,$hash,$hash);
6943 print <<EOT;
6944 <p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without
6945 regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,
6946 the pattern entered is recognized as the POSIX extended
6947 <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case
6948 insensitive).</p>
6949 <dl>
6950 <dt><b>commit</b></dt>
6951 <dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
6953 my $have_grep = gitweb_check_feature('grep');
6954 if ($have_grep) {
6955 print <<EOT;
6956 <dt><b>grep</b></dt>
6957 <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
6958 a different one) are searched for the given pattern. On large trees, this search can take
6959 a while and put some strain on the server, so please use it with some consideration. Note that
6960 due to git-grep peculiarity, currently if regexp mode is turned off, the matches are
6961 case-sensitive.</dd>
6964 print <<EOT;
6965 <dt><b>author</b></dt>
6966 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>
6967 <dt><b>committer</b></dt>
6968 <dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>
6970 my $have_pickaxe = gitweb_check_feature('pickaxe');
6971 if ($have_pickaxe) {
6972 print <<EOT;
6973 <dt><b>pickaxe</b></dt>
6974 <dd>All commits that caused the string to appear or disappear from any file (changes that
6975 added, removed or "modified" the string) will be listed. This search can take a while and
6976 takes a lot of strain on the server, so please use it wisely. Note that since you may be
6977 interested even in changes just changing the case as well, this search is case sensitive.</dd>
6980 print "</dl>\n";
6981 git_footer_html();
6984 sub git_shortlog {
6985 git_log_generic('shortlog', \&git_shortlog_body,
6986 $hash, $hash_parent);
6989 ## ......................................................................
6990 ## feeds (RSS, Atom; OPML)
6992 sub git_feed {
6993 my $format = shift || 'atom';
6994 my $have_blame = gitweb_check_feature('blame');
6996 # Atom: http://www.atomenabled.org/developers/syndication/
6997 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
6998 if ($format ne 'rss' && $format ne 'atom') {
6999 die_error(400, "Unknown web feed format");
7002 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
7003 my $head = $hash || 'HEAD';
7004 my @commitlist = parse_commits($head, 150, 0, $file_name);
7006 my %latest_commit;
7007 my %latest_date;
7008 my $content_type = "application/$format+xml";
7009 if (defined $cgi->http('HTTP_ACCEPT') &&
7010 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
7011 # browser (feed reader) prefers text/xml
7012 $content_type = 'text/xml';
7014 if (defined($commitlist[0])) {
7015 %latest_commit = %{$commitlist[0]};
7016 my $latest_epoch = $latest_commit{'committer_epoch'};
7017 %latest_date = parse_date($latest_epoch);
7018 my $if_modified = $cgi->http('IF_MODIFIED_SINCE');
7019 if (defined $if_modified) {
7020 my $since;
7021 if (eval { require HTTP::Date; 1; }) {
7022 $since = HTTP::Date::str2time($if_modified);
7023 } elsif (eval { require Time::ParseDate; 1; }) {
7024 $since = Time::ParseDate::parsedate($if_modified, GMT => 1);
7026 if (defined $since && $latest_epoch <= $since) {
7027 print $cgi->header(
7028 -type => $content_type,
7029 -charset => 'utf-8',
7030 -last_modified => $latest_date{'rfc2822'},
7031 -status => '304 Not Modified');
7032 return;
7035 print $cgi->header(
7036 -type => $content_type,
7037 -charset => 'utf-8',
7038 -last_modified => $latest_date{'rfc2822'});
7039 } else {
7040 print $cgi->header(
7041 -type => $content_type,
7042 -charset => 'utf-8');
7045 # Optimization: skip generating the body if client asks only
7046 # for Last-Modified date.
7047 return if ($cgi->request_method() eq 'HEAD');
7049 # header variables
7050 my $title = "$site_name - $project/$action";
7051 my $feed_type = 'log';
7052 if (defined $hash) {
7053 $title .= " - '$hash'";
7054 $feed_type = 'branch log';
7055 if (defined $file_name) {
7056 $title .= " :: $file_name";
7057 $feed_type = 'history';
7059 } elsif (defined $file_name) {
7060 $title .= " - $file_name";
7061 $feed_type = 'history';
7063 $title .= " $feed_type";
7064 my $descr = git_get_project_description($project);
7065 if (defined $descr) {
7066 $descr = esc_html($descr);
7067 } else {
7068 $descr = "$project " .
7069 ($format eq 'rss' ? 'RSS' : 'Atom') .
7070 " feed";
7072 my $owner = git_get_project_owner($project);
7073 $owner = esc_html($owner);
7075 #header
7076 my $alt_url;
7077 if (defined $file_name) {
7078 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
7079 } elsif (defined $hash) {
7080 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
7081 } else {
7082 $alt_url = href(-full=>1, action=>"summary");
7084 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
7085 if ($format eq 'rss') {
7086 print <<XML;
7087 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
7088 <channel>
7090 print "<title>$title</title>\n" .
7091 "<link>$alt_url</link>\n" .
7092 "<description>$descr</description>\n" .
7093 "<language>en</language>\n" .
7094 # project owner is responsible for 'editorial' content
7095 "<managingEditor>$owner</managingEditor>\n";
7096 if (defined $logo || defined $favicon) {
7097 # prefer the logo to the favicon, since RSS
7098 # doesn't allow both
7099 my $img = esc_url($logo || $favicon);
7100 print "<image>\n" .
7101 "<url>$img</url>\n" .
7102 "<title>$title</title>\n" .
7103 "<link>$alt_url</link>\n" .
7104 "</image>\n";
7106 if (%latest_date) {
7107 print "<pubDate>$latest_date{'rfc2822'}</pubDate>\n";
7108 print "<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";
7110 print "<generator>gitweb v.$version/$git_version</generator>\n";
7111 } elsif ($format eq 'atom') {
7112 print <<XML;
7113 <feed xmlns="http://www.w3.org/2005/Atom">
7115 print "<title>$title</title>\n" .
7116 "<subtitle>$descr</subtitle>\n" .
7117 '<link rel="alternate" type="text/html" href="' .
7118 $alt_url . '" />' . "\n" .
7119 '<link rel="self" type="' . $content_type . '" href="' .
7120 $cgi->self_url() . '" />' . "\n" .
7121 "<id>" . href(-full=>1) . "</id>\n" .
7122 # use project owner for feed author
7123 "<author><name>$owner</name></author>\n";
7124 if (defined $favicon) {
7125 print "<icon>" . esc_url($favicon) . "</icon>\n";
7127 if (defined $logo_url) {
7128 # not twice as wide as tall: 72 x 27 pixels
7129 print "<logo>" . esc_url($logo) . "</logo>\n";
7131 if (! %latest_date) {
7132 # dummy date to keep the feed valid until commits trickle in:
7133 print "<updated>1970-01-01T00:00:00Z</updated>\n";
7134 } else {
7135 print "<updated>$latest_date{'iso-8601'}</updated>\n";
7137 print "<generator version='$version/$git_version'>gitweb</generator>\n";
7140 # contents
7141 for (my $i = 0; $i <= $#commitlist; $i++) {
7142 my %co = %{$commitlist[$i]};
7143 my $commit = $co{'id'};
7144 # we read 150, we always show 30 and the ones more recent than 48 hours
7145 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
7146 last;
7148 my %cd = parse_date($co{'author_epoch'});
7150 # get list of changed files
7151 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
7152 $co{'parent'} || "--root",
7153 $co{'id'}, "--", (defined $file_name ? $file_name : ())
7154 or next;
7155 my @difftree = map { chomp; $_ } <$fd>;
7156 close $fd
7157 or next;
7159 # print element (entry, item)
7160 my $co_url = href(-full=>1, action=>"commitdiff", hash=>$commit);
7161 if ($format eq 'rss') {
7162 print "<item>\n" .
7163 "<title>" . esc_html($co{'title'}) . "</title>\n" .
7164 "<author>" . esc_html($co{'author'}) . "</author>\n" .
7165 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
7166 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
7167 "<link>$co_url</link>\n" .
7168 "<description>" . esc_html($co{'title'}) . "</description>\n" .
7169 "<content:encoded>" .
7170 "<![CDATA[\n";
7171 } elsif ($format eq 'atom') {
7172 print "<entry>\n" .
7173 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
7174 "<updated>$cd{'iso-8601'}</updated>\n" .
7175 "<author>\n" .
7176 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
7177 if ($co{'author_email'}) {
7178 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
7180 print "</author>\n" .
7181 # use committer for contributor
7182 "<contributor>\n" .
7183 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
7184 if ($co{'committer_email'}) {
7185 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
7187 print "</contributor>\n" .
7188 "<published>$cd{'iso-8601'}</published>\n" .
7189 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
7190 "<id>$co_url</id>\n" .
7191 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
7192 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
7194 my $comment = $co{'comment'};
7195 print "<pre>\n";
7196 foreach my $line (@$comment) {
7197 $line = esc_html($line);
7198 print "$line\n";
7200 print "</pre><ul>\n";
7201 foreach my $difftree_line (@difftree) {
7202 my %difftree = parse_difftree_raw_line($difftree_line);
7203 next if !$difftree{'from_id'};
7205 my $file = $difftree{'file'} || $difftree{'to_file'};
7207 print "<li>" .
7208 "[" .
7209 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
7210 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
7211 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
7212 file_name=>$file, file_parent=>$difftree{'from_file'}),
7213 -title => "diff"}, 'D');
7214 if ($have_blame) {
7215 print $cgi->a({-href => href(-full=>1, action=>"blame",
7216 file_name=>$file, hash_base=>$commit),
7217 -title => "blame"}, 'B');
7219 # if this is not a feed of a file history
7220 if (!defined $file_name || $file_name ne $file) {
7221 print $cgi->a({-href => href(-full=>1, action=>"history",
7222 file_name=>$file, hash=>$commit),
7223 -title => "history"}, 'H');
7225 $file = esc_path($file);
7226 print "] ".
7227 "$file</li>\n";
7229 if ($format eq 'rss') {
7230 print "</ul>]]>\n" .
7231 "</content:encoded>\n" .
7232 "</item>\n";
7233 } elsif ($format eq 'atom') {
7234 print "</ul>\n</div>\n" .
7235 "</content>\n" .
7236 "</entry>\n";
7240 # end of feed
7241 if ($format eq 'rss') {
7242 print "</channel>\n</rss>\n";
7243 } elsif ($format eq 'atom') {
7244 print "</feed>\n";
7248 sub git_rss {
7249 git_feed('rss');
7252 sub git_atom {
7253 git_feed('atom');
7256 sub git_opml {
7257 my @list = git_get_projects_list();
7259 print $cgi->header(
7260 -type => 'text/xml',
7261 -charset => 'utf-8',
7262 -content_disposition => 'inline; filename="opml.xml"');
7264 print <<XML;
7265 <?xml version="1.0" encoding="utf-8"?>
7266 <opml version="1.0">
7267 <head>
7268 <title>$site_name OPML Export</title>
7269 </head>
7270 <body>
7271 <outline text="git RSS feeds">
7274 foreach my $pr (@list) {
7275 my %proj = %$pr;
7276 my $head = git_get_head_hash($proj{'path'});
7277 if (!defined $head) {
7278 next;
7280 $git_dir = "$projectroot/$proj{'path'}";
7281 my %co = parse_commit($head);
7282 if (!%co) {
7283 next;
7286 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
7287 my $rss = href('project' => $proj{'path'}, 'action' => 'rss', -full => 1);
7288 my $html = href('project' => $proj{'path'}, 'action' => 'summary', -full => 1);
7289 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
7291 print <<XML;
7292 </outline>
7293 </body>
7294 </opml>