Gitweb: make line number toggling work for Firefox and Safari
[git/dscho.git] / gitweb / gitweb.perl
blobec0f17b463ecacde7be98b512c6edd0c44d16028
1 #!/usr/bin/perl
3 # gitweb - simple web interface to track changes in git repositories
5 # (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org>
6 # (C) 2005, Christian Gierke
8 # This program is licensed under the GPLv2
10 use strict;
11 use warnings;
12 use CGI qw(:standard :escapeHTML -nosticky);
13 use CGI::Util qw(unescape);
14 use CGI::Carp qw(fatalsToBrowser set_message);
15 use Encode;
16 use Fcntl ':mode';
17 use File::Find qw();
18 use File::Basename qw(basename);
19 binmode STDOUT, ':utf8';
21 our $t0;
22 if (eval { require Time::HiRes; 1; }) {
23 $t0 = [Time::HiRes::gettimeofday()];
25 our $number_of_git_cmds = 0;
27 BEGIN {
28 CGI->compile() if $ENV{'MOD_PERL'};
31 our $version = "++GIT_VERSION++";
33 our ($my_url, $my_uri, $base_url, $path_info, $home_link);
34 sub evaluate_uri {
35 our $cgi;
37 our $my_url = $cgi->url();
38 our $my_uri = $cgi->url(-absolute => 1);
40 # Base URL for relative URLs in gitweb ($logo, $favicon, ...),
41 # needed and used only for URLs with nonempty PATH_INFO
42 our $base_url = $my_url;
44 # When the script is used as DirectoryIndex, the URL does not contain the name
45 # of the script file itself, and $cgi->url() fails to strip PATH_INFO, so we
46 # have to do it ourselves. We make $path_info global because it's also used
47 # later on.
49 # Another issue with the script being the DirectoryIndex is that the resulting
50 # $my_url data is not the full script URL: this is good, because we want
51 # generated links to keep implying the script name if it wasn't explicitly
52 # indicated in the URL we're handling, but it means that $my_url cannot be used
53 # as base URL.
54 # Therefore, if we needed to strip PATH_INFO, then we know that we have
55 # to build the base URL ourselves:
56 our $path_info = $ENV{"PATH_INFO"};
57 if ($path_info) {
58 if ($my_url =~ s,\Q$path_info\E$,, &&
59 $my_uri =~ s,\Q$path_info\E$,, &&
60 defined $ENV{'SCRIPT_NAME'}) {
61 $base_url = $cgi->url(-base => 1) . $ENV{'SCRIPT_NAME'};
65 # target of the home link on top of all pages
66 our $home_link = $my_uri || "/";
69 # core git executable to use
70 # this can just be "git" if your webserver has a sensible PATH
71 our $GIT = "++GIT_BINDIR++/git";
73 # absolute fs-path which will be prepended to the project path
74 #our $projectroot = "/pub/scm";
75 our $projectroot = "++GITWEB_PROJECTROOT++";
77 # fs traversing limit for getting project list
78 # the number is relative to the projectroot
79 our $project_maxdepth = "++GITWEB_PROJECT_MAXDEPTH++";
81 # string of the home link on top of all pages
82 our $home_link_str = "++GITWEB_HOME_LINK_STR++";
84 # name of your site or organization to appear in page titles
85 # replace this with something more descriptive for clearer bookmarks
86 our $site_name = "++GITWEB_SITENAME++"
87 || ($ENV{'SERVER_NAME'} || "Untitled") . " Git";
89 # filename of html text to include at top of each page
90 our $site_header = "++GITWEB_SITE_HEADER++";
91 # html text to include at home page
92 our $home_text = "++GITWEB_HOMETEXT++";
93 # filename of html text to include at bottom of each page
94 our $site_footer = "++GITWEB_SITE_FOOTER++";
96 # URI of stylesheets
97 our @stylesheets = ("++GITWEB_CSS++");
98 # URI of a single stylesheet, which can be overridden in GITWEB_CONFIG.
99 our $stylesheet = undef;
100 # URI of GIT logo (72x27 size)
101 our $logo = "++GITWEB_LOGO++";
102 # URI of GIT favicon, assumed to be image/png type
103 our $favicon = "++GITWEB_FAVICON++";
104 # URI of gitweb.js (JavaScript code for gitweb)
105 our $javascript = "++GITWEB_JS++";
107 # URI and label (title) of GIT logo link
108 #our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
109 #our $logo_label = "git documentation";
110 our $logo_url = "http://git-scm.com/";
111 our $logo_label = "git homepage";
113 # source of projects list
114 our $projects_list = "++GITWEB_LIST++";
116 # the width (in characters) of the projects list "Description" column
117 our $projects_list_description_width = 25;
119 # default order of projects list
120 # valid values are none, project, descr, owner, and age
121 our $default_projects_order = "project";
123 # show repository only if this file exists
124 # (only effective if this variable evaluates to true)
125 our $export_ok = "++GITWEB_EXPORT_OK++";
127 # show repository only if this subroutine returns true
128 # when given the path to the project, for example:
129 # sub { return -e "$_[0]/git-daemon-export-ok"; }
130 our $export_auth_hook = undef;
132 # only allow viewing of repositories also shown on the overview page
133 our $strict_export = "++GITWEB_STRICT_EXPORT++";
135 # list of git base URLs used for URL to where fetch project from,
136 # i.e. full URL is "$git_base_url/$project"
137 our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
139 # default blob_plain mimetype and default charset for text/plain blob
140 our $default_blob_plain_mimetype = 'text/plain';
141 our $default_text_plain_charset = undef;
143 # file to use for guessing MIME types before trying /etc/mime.types
144 # (relative to the current git repository)
145 our $mimetypes_file = undef;
147 # assume this charset if line contains non-UTF-8 characters;
148 # it should be valid encoding (see Encoding::Supported(3pm) for list),
149 # for which encoding all byte sequences are valid, for example
150 # 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
151 # could be even 'utf-8' for the old behavior)
152 our $fallback_encoding = 'latin1';
154 # rename detection options for git-diff and git-diff-tree
155 # - default is '-M', with the cost proportional to
156 # (number of removed files) * (number of new files).
157 # - more costly is '-C' (which implies '-M'), with the cost proportional to
158 # (number of changed files + number of removed files) * (number of new files)
159 # - even more costly is '-C', '--find-copies-harder' with cost
160 # (number of files in the original tree) * (number of new files)
161 # - one might want to include '-B' option, e.g. '-B', '-M'
162 our @diff_opts = ('-M'); # taken from git_commit
164 # Disables features that would allow repository owners to inject script into
165 # the gitweb domain.
166 our $prevent_xss = 0;
168 # information about snapshot formats that gitweb is capable of serving
169 our %known_snapshot_formats = (
170 # name => {
171 # 'display' => display name,
172 # 'type' => mime type,
173 # 'suffix' => filename suffix,
174 # 'format' => --format for git-archive,
175 # 'compressor' => [compressor command and arguments]
176 # (array reference, optional)
177 # 'disabled' => boolean (optional)}
179 'tgz' => {
180 'display' => 'tar.gz',
181 'type' => 'application/x-gzip',
182 'suffix' => '.tar.gz',
183 'format' => 'tar',
184 'compressor' => ['gzip']},
186 'tbz2' => {
187 'display' => 'tar.bz2',
188 'type' => 'application/x-bzip2',
189 'suffix' => '.tar.bz2',
190 'format' => 'tar',
191 'compressor' => ['bzip2']},
193 'txz' => {
194 'display' => 'tar.xz',
195 'type' => 'application/x-xz',
196 'suffix' => '.tar.xz',
197 'format' => 'tar',
198 'compressor' => ['xz'],
199 'disabled' => 1},
201 'zip' => {
202 'display' => 'zip',
203 'type' => 'application/x-zip',
204 'suffix' => '.zip',
205 'format' => 'zip'},
208 # Aliases so we understand old gitweb.snapshot values in repository
209 # configuration.
210 our %known_snapshot_format_aliases = (
211 'gzip' => 'tgz',
212 'bzip2' => 'tbz2',
213 'xz' => 'txz',
215 # backward compatibility: legacy gitweb config support
216 'x-gzip' => undef, 'gz' => undef,
217 'x-bzip2' => undef, 'bz2' => undef,
218 'x-zip' => undef, '' => undef,
221 # Pixel sizes for icons and avatars. If the default font sizes or lineheights
222 # are changed, it may be appropriate to change these values too via
223 # $GITWEB_CONFIG.
224 our %avatar_size = (
225 'default' => 16,
226 'double' => 32
229 # Used to set the maximum load that we will still respond to gitweb queries.
230 # If server load exceed this value then return "503 server busy" error.
231 # If gitweb cannot determined server load, it is taken to be 0.
232 # Leave it undefined (or set to 'undef') to turn off load checking.
233 our $maxload = 300;
235 # configuration for 'highlight' (http://www.andre-simon.de/)
236 # match by basename
237 our %highlight_basename = (
238 #'Program' => 'py',
239 #'Library' => 'py',
240 'SConstruct' => 'py', # SCons equivalent of Makefile
241 'Makefile' => 'make',
243 # match by extension
244 our %highlight_ext = (
245 # main extensions, defining name of syntax;
246 # see files in /usr/share/highlight/langDefs/ directory
247 map { $_ => $_ }
248 qw(py c cpp rb java css php sh pl js tex bib xml awk bat ini spec tcl),
249 # alternate extensions, see /etc/highlight/filetypes.conf
250 'h' => 'c',
251 map { $_ => 'cpp' } qw(cxx c++ cc),
252 map { $_ => 'php' } qw(php3 php4),
253 map { $_ => 'pl' } qw(perl pm), # perhaps also 'cgi'
254 'mak' => 'make',
255 map { $_ => 'xml' } qw(xhtml html htm),
258 # You define site-wide feature defaults here; override them with
259 # $GITWEB_CONFIG as necessary.
260 our %feature = (
261 # feature => {
262 # 'sub' => feature-sub (subroutine),
263 # 'override' => allow-override (boolean),
264 # 'default' => [ default options...] (array reference)}
266 # if feature is overridable (it means that allow-override has true value),
267 # then feature-sub will be called with default options as parameters;
268 # return value of feature-sub indicates if to enable specified feature
270 # if there is no 'sub' key (no feature-sub), then feature cannot be
271 # overridden
273 # use gitweb_get_feature(<feature>) to retrieve the <feature> value
274 # (an array) or gitweb_check_feature(<feature>) to check if <feature>
275 # is enabled
277 # Enable the 'blame' blob view, showing the last commit that modified
278 # each line in the file. This can be very CPU-intensive.
280 # To enable system wide have in $GITWEB_CONFIG
281 # $feature{'blame'}{'default'} = [1];
282 # To have project specific config enable override in $GITWEB_CONFIG
283 # $feature{'blame'}{'override'} = 1;
284 # and in project config gitweb.blame = 0|1;
285 'blame' => {
286 'sub' => sub { feature_bool('blame', @_) },
287 'override' => 0,
288 'default' => [0]},
290 # Enable the 'snapshot' link, providing a compressed archive of any
291 # tree. This can potentially generate high traffic if you have large
292 # project.
294 # Value is a list of formats defined in %known_snapshot_formats that
295 # you wish to offer.
296 # To disable system wide have in $GITWEB_CONFIG
297 # $feature{'snapshot'}{'default'} = [];
298 # To have project specific config enable override in $GITWEB_CONFIG
299 # $feature{'snapshot'}{'override'} = 1;
300 # and in project config, a comma-separated list of formats or "none"
301 # to disable. Example: gitweb.snapshot = tbz2,zip;
302 'snapshot' => {
303 'sub' => \&feature_snapshot,
304 'override' => 0,
305 'default' => ['tgz']},
307 # Enable text search, which will list the commits which match author,
308 # committer or commit text to a given string. Enabled by default.
309 # Project specific override is not supported.
310 'search' => {
311 'override' => 0,
312 'default' => [1]},
314 # Enable grep search, which will list the files in currently selected
315 # tree containing the given string. Enabled by default. This can be
316 # potentially CPU-intensive, of course.
318 # To enable system wide have in $GITWEB_CONFIG
319 # $feature{'grep'}{'default'} = [1];
320 # To have project specific config enable override in $GITWEB_CONFIG
321 # $feature{'grep'}{'override'} = 1;
322 # and in project config gitweb.grep = 0|1;
323 'grep' => {
324 'sub' => sub { feature_bool('grep', @_) },
325 'override' => 0,
326 'default' => [1]},
328 # Enable the pickaxe search, which will list the commits that modified
329 # a given string in a file. This can be practical and quite faster
330 # alternative to 'blame', but still potentially CPU-intensive.
332 # To enable system wide have in $GITWEB_CONFIG
333 # $feature{'pickaxe'}{'default'} = [1];
334 # To have project specific config enable override in $GITWEB_CONFIG
335 # $feature{'pickaxe'}{'override'} = 1;
336 # and in project config gitweb.pickaxe = 0|1;
337 'pickaxe' => {
338 'sub' => sub { feature_bool('pickaxe', @_) },
339 'override' => 0,
340 'default' => [1]},
342 # Enable showing size of blobs in a 'tree' view, in a separate
343 # column, similar to what 'ls -l' does. This cost a bit of IO.
345 # To disable system wide have in $GITWEB_CONFIG
346 # $feature{'show-sizes'}{'default'} = [0];
347 # To have project specific config enable override in $GITWEB_CONFIG
348 # $feature{'show-sizes'}{'override'} = 1;
349 # and in project config gitweb.showsizes = 0|1;
350 'show-sizes' => {
351 'sub' => sub { feature_bool('showsizes', @_) },
352 'override' => 0,
353 'default' => [1]},
355 # Make gitweb use an alternative format of the URLs which can be
356 # more readable and natural-looking: project name is embedded
357 # directly in the path and the query string contains other
358 # auxiliary information. All gitweb installations recognize
359 # URL in either format; this configures in which formats gitweb
360 # generates links.
362 # To enable system wide have in $GITWEB_CONFIG
363 # $feature{'pathinfo'}{'default'} = [1];
364 # Project specific override is not supported.
366 # Note that you will need to change the default location of CSS,
367 # favicon, logo and possibly other files to an absolute URL. Also,
368 # if gitweb.cgi serves as your indexfile, you will need to force
369 # $my_uri to contain the script name in your $GITWEB_CONFIG.
370 'pathinfo' => {
371 'override' => 0,
372 'default' => [0]},
374 # Make gitweb consider projects in project root subdirectories
375 # to be forks of existing projects. Given project $projname.git,
376 # projects matching $projname/*.git will not be shown in the main
377 # projects list, instead a '+' mark will be added to $projname
378 # there and a 'forks' view will be enabled for the project, listing
379 # all the forks. If project list is taken from a file, forks have
380 # to be listed after the main project.
382 # To enable system wide have in $GITWEB_CONFIG
383 # $feature{'forks'}{'default'} = [1];
384 # Project specific override is not supported.
385 'forks' => {
386 'override' => 0,
387 'default' => [0]},
389 # Insert custom links to the action bar of all project pages.
390 # This enables you mainly to link to third-party scripts integrating
391 # into gitweb; e.g. git-browser for graphical history representation
392 # or custom web-based repository administration interface.
394 # The 'default' value consists of a list of triplets in the form
395 # (label, link, position) where position is the label after which
396 # to insert the link and link is a format string where %n expands
397 # to the project name, %f to the project path within the filesystem,
398 # %h to the current hash (h gitweb parameter) and %b to the current
399 # hash base (hb gitweb parameter); %% expands to %.
401 # To enable system wide have in $GITWEB_CONFIG e.g.
402 # $feature{'actions'}{'default'} = [('graphiclog',
403 # '/git-browser/by-commit.html?r=%n', 'summary')];
404 # Project specific override is not supported.
405 'actions' => {
406 'override' => 0,
407 'default' => []},
409 # Allow gitweb scan project content tags described in ctags/
410 # of project repository, and display the popular Web 2.0-ish
411 # "tag cloud" near the project list. Note that this is something
412 # COMPLETELY different from the normal Git tags.
414 # gitweb by itself can show existing tags, but it does not handle
415 # tagging itself; you need an external application for that.
416 # For an example script, check Girocco's cgi/tagproj.cgi.
417 # You may want to install the HTML::TagCloud Perl module to get
418 # a pretty tag cloud instead of just a list of tags.
420 # To enable system wide have in $GITWEB_CONFIG
421 # $feature{'ctags'}{'default'} = ['path_to_tag_script'];
422 # Project specific override is not supported.
423 'ctags' => {
424 'override' => 0,
425 'default' => [0]},
427 # The maximum number of patches in a patchset generated in patch
428 # view. Set this to 0 or undef to disable patch view, or to a
429 # negative number to remove any limit.
431 # To disable system wide have in $GITWEB_CONFIG
432 # $feature{'patches'}{'default'} = [0];
433 # To have project specific config enable override in $GITWEB_CONFIG
434 # $feature{'patches'}{'override'} = 1;
435 # and in project config gitweb.patches = 0|n;
436 # where n is the maximum number of patches allowed in a patchset.
437 'patches' => {
438 'sub' => \&feature_patches,
439 'override' => 0,
440 'default' => [16]},
442 # Avatar support. When this feature is enabled, views such as
443 # shortlog or commit will display an avatar associated with
444 # the email of the committer(s) and/or author(s).
446 # Currently available providers are gravatar and picon.
447 # If an unknown provider is specified, the feature is disabled.
449 # Gravatar depends on Digest::MD5.
450 # Picon currently relies on the indiana.edu database.
452 # To enable system wide have in $GITWEB_CONFIG
453 # $feature{'avatar'}{'default'} = ['<provider>'];
454 # where <provider> is either gravatar or picon.
455 # To have project specific config enable override in $GITWEB_CONFIG
456 # $feature{'avatar'}{'override'} = 1;
457 # and in project config gitweb.avatar = <provider>;
458 'avatar' => {
459 'sub' => \&feature_avatar,
460 'override' => 0,
461 'default' => ['']},
463 # Enable displaying how much time and how many git commands
464 # it took to generate and display page. Disabled by default.
465 # Project specific override is not supported.
466 'timed' => {
467 'override' => 0,
468 'default' => [0]},
470 # Enable turning some links into links to actions which require
471 # JavaScript to run (like 'blame_incremental'). Not enabled by
472 # default. Project specific override is currently not supported.
473 'javascript-actions' => {
474 'override' => 0,
475 'default' => [0]},
477 # Syntax highlighting support. This is based on Daniel Svensson's
478 # and Sham Chukoury's work in gitweb-xmms2.git.
479 # It requires the 'highlight' program present in $PATH,
480 # and therefore is disabled by default.
482 # To enable system wide have in $GITWEB_CONFIG
483 # $feature{'highlight'}{'default'} = [1];
485 'highlight' => {
486 'sub' => sub { feature_bool('highlight', @_) },
487 'override' => 0,
488 'default' => [0]},
491 sub gitweb_get_feature {
492 my ($name) = @_;
493 return unless exists $feature{$name};
494 my ($sub, $override, @defaults) = (
495 $feature{$name}{'sub'},
496 $feature{$name}{'override'},
497 @{$feature{$name}{'default'}});
498 # project specific override is possible only if we have project
499 our $git_dir; # global variable, declared later
500 if (!$override || !defined $git_dir) {
501 return @defaults;
503 if (!defined $sub) {
504 warn "feature $name is not overridable";
505 return @defaults;
507 return $sub->(@defaults);
510 # A wrapper to check if a given feature is enabled.
511 # With this, you can say
513 # my $bool_feat = gitweb_check_feature('bool_feat');
514 # gitweb_check_feature('bool_feat') or somecode;
516 # instead of
518 # my ($bool_feat) = gitweb_get_feature('bool_feat');
519 # (gitweb_get_feature('bool_feat'))[0] or somecode;
521 sub gitweb_check_feature {
522 return (gitweb_get_feature(@_))[0];
526 sub feature_bool {
527 my $key = shift;
528 my ($val) = git_get_project_config($key, '--bool');
530 if (!defined $val) {
531 return ($_[0]);
532 } elsif ($val eq 'true') {
533 return (1);
534 } elsif ($val eq 'false') {
535 return (0);
539 sub feature_snapshot {
540 my (@fmts) = @_;
542 my ($val) = git_get_project_config('snapshot');
544 if ($val) {
545 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
548 return @fmts;
551 sub feature_patches {
552 my @val = (git_get_project_config('patches', '--int'));
554 if (@val) {
555 return @val;
558 return ($_[0]);
561 sub feature_avatar {
562 my @val = (git_get_project_config('avatar'));
564 return @val ? @val : @_;
567 # checking HEAD file with -e is fragile if the repository was
568 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
569 # and then pruned.
570 sub check_head_link {
571 my ($dir) = @_;
572 my $headfile = "$dir/HEAD";
573 return ((-e $headfile) ||
574 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
577 sub check_export_ok {
578 my ($dir) = @_;
579 return (check_head_link($dir) &&
580 (!$export_ok || -e "$dir/$export_ok") &&
581 (!$export_auth_hook || $export_auth_hook->($dir)));
584 # process alternate names for backward compatibility
585 # filter out unsupported (unknown) snapshot formats
586 sub filter_snapshot_fmts {
587 my @fmts = @_;
589 @fmts = map {
590 exists $known_snapshot_format_aliases{$_} ?
591 $known_snapshot_format_aliases{$_} : $_} @fmts;
592 @fmts = grep {
593 exists $known_snapshot_formats{$_} &&
594 !$known_snapshot_formats{$_}{'disabled'}} @fmts;
597 our ($GITWEB_CONFIG, $GITWEB_CONFIG_SYSTEM);
598 sub evaluate_gitweb_config {
599 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
600 our $GITWEB_CONFIG_SYSTEM = $ENV{'GITWEB_CONFIG_SYSTEM'} || "++GITWEB_CONFIG_SYSTEM++";
601 # die if there are errors parsing config file
602 if (-e $GITWEB_CONFIG) {
603 do $GITWEB_CONFIG;
604 die $@ if $@;
605 } elsif (-e $GITWEB_CONFIG_SYSTEM) {
606 do $GITWEB_CONFIG_SYSTEM;
607 die $@ if $@;
611 # Get loadavg of system, to compare against $maxload.
612 # Currently it requires '/proc/loadavg' present to get loadavg;
613 # if it is not present it returns 0, which means no load checking.
614 sub get_loadavg {
615 if( -e '/proc/loadavg' ){
616 open my $fd, '<', '/proc/loadavg'
617 or return 0;
618 my @load = split(/\s+/, scalar <$fd>);
619 close $fd;
621 # The first three columns measure CPU and IO utilization of the last one,
622 # five, and 10 minute periods. The fourth column shows the number of
623 # currently running processes and the total number of processes in the m/n
624 # format. The last column displays the last process ID used.
625 return $load[0] || 0;
627 # additional checks for load average should go here for things that don't export
628 # /proc/loadavg
630 return 0;
633 # version of the core git binary
634 our $git_version;
635 sub evaluate_git_version {
636 our $git_version = qx("$GIT" --version) =~ m/git version (.*)$/ ? $1 : "unknown";
637 $number_of_git_cmds++;
640 sub check_loadavg {
641 if (defined $maxload && get_loadavg() > $maxload) {
642 die_error(503, "The load average on the server is too high");
646 # ======================================================================
647 # input validation and dispatch
649 # input parameters can be collected from a variety of sources (presently, CGI
650 # and PATH_INFO), so we define an %input_params hash that collects them all
651 # together during validation: this allows subsequent uses (e.g. href()) to be
652 # agnostic of the parameter origin
654 our %input_params = ();
656 # input parameters are stored with the long parameter name as key. This will
657 # also be used in the href subroutine to convert parameters to their CGI
658 # equivalent, and since the href() usage is the most frequent one, we store
659 # the name -> CGI key mapping here, instead of the reverse.
661 # XXX: Warning: If you touch this, check the search form for updating,
662 # too.
664 our @cgi_param_mapping = (
665 project => "p",
666 action => "a",
667 file_name => "f",
668 file_parent => "fp",
669 hash => "h",
670 hash_parent => "hp",
671 hash_base => "hb",
672 hash_parent_base => "hpb",
673 page => "pg",
674 order => "o",
675 searchtext => "s",
676 searchtype => "st",
677 snapshot_format => "sf",
678 extra_options => "opt",
679 search_use_regexp => "sr",
680 # this must be last entry (for manipulation from JavaScript)
681 javascript => "js"
683 our %cgi_param_mapping = @cgi_param_mapping;
685 # we will also need to know the possible actions, for validation
686 our %actions = (
687 "blame" => \&git_blame,
688 "blame_incremental" => \&git_blame_incremental,
689 "blame_data" => \&git_blame_data,
690 "blobdiff" => \&git_blobdiff,
691 "blobdiff_plain" => \&git_blobdiff_plain,
692 "blob" => \&git_blob,
693 "blob_plain" => \&git_blob_plain,
694 "commitdiff" => \&git_commitdiff,
695 "commitdiff_plain" => \&git_commitdiff_plain,
696 "commit" => \&git_commit,
697 "forks" => \&git_forks,
698 "heads" => \&git_heads,
699 "history" => \&git_history,
700 "log" => \&git_log,
701 "patch" => \&git_patch,
702 "patches" => \&git_patches,
703 "rss" => \&git_rss,
704 "atom" => \&git_atom,
705 "search" => \&git_search,
706 "search_help" => \&git_search_help,
707 "shortlog" => \&git_shortlog,
708 "summary" => \&git_summary,
709 "tag" => \&git_tag,
710 "tags" => \&git_tags,
711 "tree" => \&git_tree,
712 "snapshot" => \&git_snapshot,
713 "object" => \&git_object,
714 # those below don't need $project
715 "opml" => \&git_opml,
716 "project_list" => \&git_project_list,
717 "project_index" => \&git_project_index,
720 # finally, we have the hash of allowed extra_options for the commands that
721 # allow them
722 our %allowed_options = (
723 "--no-merges" => [ qw(rss atom log shortlog history) ],
726 # fill %input_params with the CGI parameters. All values except for 'opt'
727 # should be single values, but opt can be an array. We should probably
728 # build an array of parameters that can be multi-valued, but since for the time
729 # being it's only this one, we just single it out
730 sub evaluate_query_params {
731 our $cgi;
733 while (my ($name, $symbol) = each %cgi_param_mapping) {
734 if ($symbol eq 'opt') {
735 $input_params{$name} = [ $cgi->param($symbol) ];
736 } else {
737 $input_params{$name} = $cgi->param($symbol);
742 # now read PATH_INFO and update the parameter list for missing parameters
743 sub evaluate_path_info {
744 return if defined $input_params{'project'};
745 return if !$path_info;
746 $path_info =~ s,^/+,,;
747 return if !$path_info;
749 # find which part of PATH_INFO is project
750 my $project = $path_info;
751 $project =~ s,/+$,,;
752 while ($project && !check_head_link("$projectroot/$project")) {
753 $project =~ s,/*[^/]*$,,;
755 return unless $project;
756 $input_params{'project'} = $project;
758 # do not change any parameters if an action is given using the query string
759 return if $input_params{'action'};
760 $path_info =~ s,^\Q$project\E/*,,;
762 # next, check if we have an action
763 my $action = $path_info;
764 $action =~ s,/.*$,,;
765 if (exists $actions{$action}) {
766 $path_info =~ s,^$action/*,,;
767 $input_params{'action'} = $action;
770 # list of actions that want hash_base instead of hash, but can have no
771 # pathname (f) parameter
772 my @wants_base = (
773 'tree',
774 'history',
777 # we want to catch
778 # [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name]
779 my ($parentrefname, $parentpathname, $refname, $pathname) =
780 ($path_info =~ /^(?:(.+?)(?::(.+))?\.\.)?(.+?)(?::(.+))?$/);
782 # first, analyze the 'current' part
783 if (defined $pathname) {
784 # we got "branch:filename" or "branch:dir/"
785 # we could use git_get_type(branch:pathname), but:
786 # - it needs $git_dir
787 # - it does a git() call
788 # - the convention of terminating directories with a slash
789 # makes it superfluous
790 # - embedding the action in the PATH_INFO would make it even
791 # more superfluous
792 $pathname =~ s,^/+,,;
793 if (!$pathname || substr($pathname, -1) eq "/") {
794 $input_params{'action'} ||= "tree";
795 $pathname =~ s,/$,,;
796 } else {
797 # the default action depends on whether we had parent info
798 # or not
799 if ($parentrefname) {
800 $input_params{'action'} ||= "blobdiff_plain";
801 } else {
802 $input_params{'action'} ||= "blob_plain";
805 $input_params{'hash_base'} ||= $refname;
806 $input_params{'file_name'} ||= $pathname;
807 } elsif (defined $refname) {
808 # we got "branch". In this case we have to choose if we have to
809 # set hash or hash_base.
811 # Most of the actions without a pathname only want hash to be
812 # set, except for the ones specified in @wants_base that want
813 # hash_base instead. It should also be noted that hand-crafted
814 # links having 'history' as an action and no pathname or hash
815 # set will fail, but that happens regardless of PATH_INFO.
816 $input_params{'action'} ||= "shortlog";
817 if (grep { $_ eq $input_params{'action'} } @wants_base) {
818 $input_params{'hash_base'} ||= $refname;
819 } else {
820 $input_params{'hash'} ||= $refname;
824 # next, handle the 'parent' part, if present
825 if (defined $parentrefname) {
826 # a missing pathspec defaults to the 'current' filename, allowing e.g.
827 # someproject/blobdiff/oldrev..newrev:/filename
828 if ($parentpathname) {
829 $parentpathname =~ s,^/+,,;
830 $parentpathname =~ s,/$,,;
831 $input_params{'file_parent'} ||= $parentpathname;
832 } else {
833 $input_params{'file_parent'} ||= $input_params{'file_name'};
835 # we assume that hash_parent_base is wanted if a path was specified,
836 # or if the action wants hash_base instead of hash
837 if (defined $input_params{'file_parent'} ||
838 grep { $_ eq $input_params{'action'} } @wants_base) {
839 $input_params{'hash_parent_base'} ||= $parentrefname;
840 } else {
841 $input_params{'hash_parent'} ||= $parentrefname;
845 # for the snapshot action, we allow URLs in the form
846 # $project/snapshot/$hash.ext
847 # where .ext determines the snapshot and gets removed from the
848 # passed $refname to provide the $hash.
850 # To be able to tell that $refname includes the format extension, we
851 # require the following two conditions to be satisfied:
852 # - the hash input parameter MUST have been set from the $refname part
853 # of the URL (i.e. they must be equal)
854 # - the snapshot format MUST NOT have been defined already (e.g. from
855 # CGI parameter sf)
856 # It's also useless to try any matching unless $refname has a dot,
857 # so we check for that too
858 if (defined $input_params{'action'} &&
859 $input_params{'action'} eq 'snapshot' &&
860 defined $refname && index($refname, '.') != -1 &&
861 $refname eq $input_params{'hash'} &&
862 !defined $input_params{'snapshot_format'}) {
863 # We loop over the known snapshot formats, checking for
864 # extensions. Allowed extensions are both the defined suffix
865 # (which includes the initial dot already) and the snapshot
866 # format key itself, with a prepended dot
867 while (my ($fmt, $opt) = each %known_snapshot_formats) {
868 my $hash = $refname;
869 unless ($hash =~ s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) {
870 next;
872 my $sfx = $1;
873 # a valid suffix was found, so set the snapshot format
874 # and reset the hash parameter
875 $input_params{'snapshot_format'} = $fmt;
876 $input_params{'hash'} = $hash;
877 # we also set the format suffix to the one requested
878 # in the URL: this way a request for e.g. .tgz returns
879 # a .tgz instead of a .tar.gz
880 $known_snapshot_formats{$fmt}{'suffix'} = $sfx;
881 last;
886 our ($action, $project, $file_name, $file_parent, $hash, $hash_parent, $hash_base,
887 $hash_parent_base, @extra_options, $page, $searchtype, $search_use_regexp,
888 $searchtext, $search_regexp);
889 sub evaluate_and_validate_params {
890 our $action = $input_params{'action'};
891 if (defined $action) {
892 if (!validate_action($action)) {
893 die_error(400, "Invalid action parameter");
897 # parameters which are pathnames
898 our $project = $input_params{'project'};
899 if (defined $project) {
900 if (!validate_project($project)) {
901 undef $project;
902 die_error(404, "No such project");
906 our $file_name = $input_params{'file_name'};
907 if (defined $file_name) {
908 if (!validate_pathname($file_name)) {
909 die_error(400, "Invalid file parameter");
913 our $file_parent = $input_params{'file_parent'};
914 if (defined $file_parent) {
915 if (!validate_pathname($file_parent)) {
916 die_error(400, "Invalid file parent parameter");
920 # parameters which are refnames
921 our $hash = $input_params{'hash'};
922 if (defined $hash) {
923 if (!validate_refname($hash)) {
924 die_error(400, "Invalid hash parameter");
928 our $hash_parent = $input_params{'hash_parent'};
929 if (defined $hash_parent) {
930 if (!validate_refname($hash_parent)) {
931 die_error(400, "Invalid hash parent parameter");
935 our $hash_base = $input_params{'hash_base'};
936 if (defined $hash_base) {
937 if (!validate_refname($hash_base)) {
938 die_error(400, "Invalid hash base parameter");
942 our @extra_options = @{$input_params{'extra_options'}};
943 # @extra_options is always defined, since it can only be (currently) set from
944 # CGI, and $cgi->param() returns the empty array in array context if the param
945 # is not set
946 foreach my $opt (@extra_options) {
947 if (not exists $allowed_options{$opt}) {
948 die_error(400, "Invalid option parameter");
950 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
951 die_error(400, "Invalid option parameter for this action");
955 our $hash_parent_base = $input_params{'hash_parent_base'};
956 if (defined $hash_parent_base) {
957 if (!validate_refname($hash_parent_base)) {
958 die_error(400, "Invalid hash parent base parameter");
962 # other parameters
963 our $page = $input_params{'page'};
964 if (defined $page) {
965 if ($page =~ m/[^0-9]/) {
966 die_error(400, "Invalid page parameter");
970 our $searchtype = $input_params{'searchtype'};
971 if (defined $searchtype) {
972 if ($searchtype =~ m/[^a-z]/) {
973 die_error(400, "Invalid searchtype parameter");
977 our $search_use_regexp = $input_params{'search_use_regexp'};
979 our $searchtext = $input_params{'searchtext'};
980 our $search_regexp;
981 if (defined $searchtext) {
982 if (length($searchtext) < 2) {
983 die_error(403, "At least two characters are required for search parameter");
985 $search_regexp = $search_use_regexp ? $searchtext : quotemeta $searchtext;
989 # path to the current git repository
990 our $git_dir;
991 sub evaluate_git_dir {
992 our $git_dir = "$projectroot/$project" if $project;
995 our (@snapshot_fmts, $git_avatar);
996 sub configure_gitweb_features {
997 # list of supported snapshot formats
998 our @snapshot_fmts = gitweb_get_feature('snapshot');
999 @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
1001 # check that the avatar feature is set to a known provider name,
1002 # and for each provider check if the dependencies are satisfied.
1003 # if the provider name is invalid or the dependencies are not met,
1004 # reset $git_avatar to the empty string.
1005 our ($git_avatar) = gitweb_get_feature('avatar');
1006 if ($git_avatar eq 'gravatar') {
1007 $git_avatar = '' unless (eval { require Digest::MD5; 1; });
1008 } elsif ($git_avatar eq 'picon') {
1009 # no dependencies
1010 } else {
1011 $git_avatar = '';
1015 # custom error handler: 'die <message>' is Internal Server Error
1016 sub handle_errors_html {
1017 my $msg = shift; # it is already HTML escaped
1019 # to avoid infinite loop where error occurs in die_error,
1020 # change handler to default handler, disabling handle_errors_html
1021 set_message("Error occured when inside die_error:\n$msg");
1023 # you cannot jump out of die_error when called as error handler;
1024 # the subroutine set via CGI::Carp::set_message is called _after_
1025 # HTTP headers are already written, so it cannot write them itself
1026 die_error(undef, undef, $msg, -error_handler => 1, -no_http_header => 1);
1028 set_message(\&handle_errors_html);
1030 # dispatch
1031 sub dispatch {
1032 if (!defined $action) {
1033 if (defined $hash) {
1034 $action = git_get_type($hash);
1035 } elsif (defined $hash_base && defined $file_name) {
1036 $action = git_get_type("$hash_base:$file_name");
1037 } elsif (defined $project) {
1038 $action = 'summary';
1039 } else {
1040 $action = 'project_list';
1043 if (!defined($actions{$action})) {
1044 die_error(400, "Unknown action");
1046 if ($action !~ m/^(?:opml|project_list|project_index)$/ &&
1047 !$project) {
1048 die_error(400, "Project needed");
1050 $actions{$action}->();
1053 sub reset_timer {
1054 our $t0 = [Time::HiRes::gettimeofday()]
1055 if defined $t0;
1056 our $number_of_git_cmds = 0;
1059 sub run_request {
1060 reset_timer();
1062 evaluate_uri();
1063 evaluate_gitweb_config();
1064 check_loadavg();
1066 # $projectroot and $projects_list might be set in gitweb config file
1067 $projects_list ||= $projectroot;
1069 evaluate_query_params();
1070 evaluate_path_info();
1071 evaluate_and_validate_params();
1072 evaluate_git_dir();
1074 configure_gitweb_features();
1076 dispatch();
1079 our $is_last_request = sub { 1 };
1080 our ($pre_dispatch_hook, $post_dispatch_hook, $pre_listen_hook);
1081 our $CGI = 'CGI';
1082 our $cgi;
1083 sub configure_as_fcgi {
1084 require CGI::Fast;
1085 our $CGI = 'CGI::Fast';
1087 my $request_number = 0;
1088 # let each child service 100 requests
1089 our $is_last_request = sub { ++$request_number > 100 };
1091 sub evaluate_argv {
1092 my $script_name = $ENV{'SCRIPT_NAME'} || $ENV{'SCRIPT_FILENAME'} || __FILE__;
1093 configure_as_fcgi()
1094 if $script_name =~ /\.fcgi$/;
1096 return unless (@ARGV);
1098 require Getopt::Long;
1099 Getopt::Long::GetOptions(
1100 'fastcgi|fcgi|f' => \&configure_as_fcgi,
1101 'nproc|n=i' => sub {
1102 my ($arg, $val) = @_;
1103 return unless eval { require FCGI::ProcManager; 1; };
1104 my $proc_manager = FCGI::ProcManager->new({
1105 n_processes => $val,
1107 our $pre_listen_hook = sub { $proc_manager->pm_manage() };
1108 our $pre_dispatch_hook = sub { $proc_manager->pm_pre_dispatch() };
1109 our $post_dispatch_hook = sub { $proc_manager->pm_post_dispatch() };
1114 sub run {
1115 evaluate_argv();
1116 evaluate_git_version();
1118 $pre_listen_hook->()
1119 if $pre_listen_hook;
1121 REQUEST:
1122 while ($cgi = $CGI->new()) {
1123 $pre_dispatch_hook->()
1124 if $pre_dispatch_hook;
1126 run_request();
1128 $post_dispatch_hook->()
1129 if $post_dispatch_hook;
1131 last REQUEST if ($is_last_request->());
1134 DONE_GITWEB:
1138 run();
1140 if (defined caller) {
1141 # wrapped in a subroutine processing requests,
1142 # e.g. mod_perl with ModPerl::Registry, or PSGI with Plack::App::WrapCGI
1143 return;
1144 } else {
1145 # pure CGI script, serving single request
1146 exit;
1149 ## ======================================================================
1150 ## action links
1152 # possible values of extra options
1153 # -full => 0|1 - use absolute/full URL ($my_uri/$my_url as base)
1154 # -replay => 1 - start from a current view (replay with modifications)
1155 # -path_info => 0|1 - don't use/use path_info URL (if possible)
1156 sub href {
1157 my %params = @_;
1158 # default is to use -absolute url() i.e. $my_uri
1159 my $href = $params{-full} ? $my_url : $my_uri;
1161 $params{'project'} = $project unless exists $params{'project'};
1163 if ($params{-replay}) {
1164 while (my ($name, $symbol) = each %cgi_param_mapping) {
1165 if (!exists $params{$name}) {
1166 $params{$name} = $input_params{$name};
1171 my $use_pathinfo = gitweb_check_feature('pathinfo');
1172 if (defined $params{'project'} &&
1173 (exists $params{-path_info} ? $params{-path_info} : $use_pathinfo)) {
1174 # try to put as many parameters as possible in PATH_INFO:
1175 # - project name
1176 # - action
1177 # - hash_parent or hash_parent_base:/file_parent
1178 # - hash or hash_base:/filename
1179 # - the snapshot_format as an appropriate suffix
1181 # When the script is the root DirectoryIndex for the domain,
1182 # $href here would be something like http://gitweb.example.com/
1183 # Thus, we strip any trailing / from $href, to spare us double
1184 # slashes in the final URL
1185 $href =~ s,/$,,;
1187 # Then add the project name, if present
1188 $href .= "/".esc_url($params{'project'});
1189 delete $params{'project'};
1191 # since we destructively absorb parameters, we keep this
1192 # boolean that remembers if we're handling a snapshot
1193 my $is_snapshot = $params{'action'} eq 'snapshot';
1195 # Summary just uses the project path URL, any other action is
1196 # added to the URL
1197 if (defined $params{'action'}) {
1198 $href .= "/".esc_url($params{'action'}) unless $params{'action'} eq 'summary';
1199 delete $params{'action'};
1202 # Next, we put hash_parent_base:/file_parent..hash_base:/file_name,
1203 # stripping nonexistent or useless pieces
1204 $href .= "/" if ($params{'hash_base'} || $params{'hash_parent_base'}
1205 || $params{'hash_parent'} || $params{'hash'});
1206 if (defined $params{'hash_base'}) {
1207 if (defined $params{'hash_parent_base'}) {
1208 $href .= esc_url($params{'hash_parent_base'});
1209 # skip the file_parent if it's the same as the file_name
1210 if (defined $params{'file_parent'}) {
1211 if (defined $params{'file_name'} && $params{'file_parent'} eq $params{'file_name'}) {
1212 delete $params{'file_parent'};
1213 } elsif ($params{'file_parent'} !~ /\.\./) {
1214 $href .= ":/".esc_url($params{'file_parent'});
1215 delete $params{'file_parent'};
1218 $href .= "..";
1219 delete $params{'hash_parent'};
1220 delete $params{'hash_parent_base'};
1221 } elsif (defined $params{'hash_parent'}) {
1222 $href .= esc_url($params{'hash_parent'}). "..";
1223 delete $params{'hash_parent'};
1226 $href .= esc_url($params{'hash_base'});
1227 if (defined $params{'file_name'} && $params{'file_name'} !~ /\.\./) {
1228 $href .= ":/".esc_url($params{'file_name'});
1229 delete $params{'file_name'};
1231 delete $params{'hash'};
1232 delete $params{'hash_base'};
1233 } elsif (defined $params{'hash'}) {
1234 $href .= esc_url($params{'hash'});
1235 delete $params{'hash'};
1238 # If the action was a snapshot, we can absorb the
1239 # snapshot_format parameter too
1240 if ($is_snapshot) {
1241 my $fmt = $params{'snapshot_format'};
1242 # snapshot_format should always be defined when href()
1243 # is called, but just in case some code forgets, we
1244 # fall back to the default
1245 $fmt ||= $snapshot_fmts[0];
1246 $href .= $known_snapshot_formats{$fmt}{'suffix'};
1247 delete $params{'snapshot_format'};
1251 # now encode the parameters explicitly
1252 my @result = ();
1253 for (my $i = 0; $i < @cgi_param_mapping; $i += 2) {
1254 my ($name, $symbol) = ($cgi_param_mapping[$i], $cgi_param_mapping[$i+1]);
1255 if (defined $params{$name}) {
1256 if (ref($params{$name}) eq "ARRAY") {
1257 foreach my $par (@{$params{$name}}) {
1258 push @result, $symbol . "=" . esc_param($par);
1260 } else {
1261 push @result, $symbol . "=" . esc_param($params{$name});
1265 $href .= "?" . join(';', @result) if scalar @result;
1267 return $href;
1271 ## ======================================================================
1272 ## validation, quoting/unquoting and escaping
1274 sub validate_action {
1275 my $input = shift || return undef;
1276 return undef unless exists $actions{$input};
1277 return $input;
1280 sub validate_project {
1281 my $input = shift || return undef;
1282 if (!validate_pathname($input) ||
1283 !(-d "$projectroot/$input") ||
1284 !check_export_ok("$projectroot/$input") ||
1285 ($strict_export && !project_in_list($input))) {
1286 return undef;
1287 } else {
1288 return $input;
1292 sub validate_pathname {
1293 my $input = shift || return undef;
1295 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
1296 # at the beginning, at the end, and between slashes.
1297 # also this catches doubled slashes
1298 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
1299 return undef;
1301 # no null characters
1302 if ($input =~ m!\0!) {
1303 return undef;
1305 return $input;
1308 sub validate_refname {
1309 my $input = shift || return undef;
1311 # textual hashes are O.K.
1312 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
1313 return $input;
1315 # it must be correct pathname
1316 $input = validate_pathname($input)
1317 or return undef;
1318 # restrictions on ref name according to git-check-ref-format
1319 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
1320 return undef;
1322 return $input;
1325 # decode sequences of octets in utf8 into Perl's internal form,
1326 # which is utf-8 with utf8 flag set if needed. gitweb writes out
1327 # in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
1328 sub to_utf8 {
1329 my $str = shift;
1330 return undef unless defined $str;
1331 if (utf8::valid($str)) {
1332 utf8::decode($str);
1333 return $str;
1334 } else {
1335 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
1339 # quote unsafe chars, but keep the slash, even when it's not
1340 # correct, but quoted slashes look too horrible in bookmarks
1341 sub esc_param {
1342 my $str = shift;
1343 return undef unless defined $str;
1344 $str =~ s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;
1345 $str =~ s/ /\+/g;
1346 return $str;
1349 # quote unsafe chars in whole URL, so some characters cannot be quoted
1350 sub esc_url {
1351 my $str = shift;
1352 return undef unless defined $str;
1353 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&= ]+)/CGI::escape($1)/eg;
1354 $str =~ s/ /\+/g;
1355 return $str;
1358 # replace invalid utf8 character with SUBSTITUTION sequence
1359 sub esc_html {
1360 my $str = shift;
1361 my %opts = @_;
1363 return undef unless defined $str;
1365 $str = to_utf8($str);
1366 $str = $cgi->escapeHTML($str);
1367 if ($opts{'-nbsp'}) {
1368 $str =~ s/ /&nbsp;/g;
1370 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
1371 return $str;
1374 # quote control characters and escape filename to HTML
1375 sub esc_path {
1376 my $str = shift;
1377 my %opts = @_;
1379 return undef unless defined $str;
1381 $str = to_utf8($str);
1382 $str = $cgi->escapeHTML($str);
1383 if ($opts{'-nbsp'}) {
1384 $str =~ s/ /&nbsp;/g;
1386 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
1387 return $str;
1390 # Make control characters "printable", using character escape codes (CEC)
1391 sub quot_cec {
1392 my $cntrl = shift;
1393 my %opts = @_;
1394 my %es = ( # character escape codes, aka escape sequences
1395 "\t" => '\t', # tab (HT)
1396 "\n" => '\n', # line feed (LF)
1397 "\r" => '\r', # carrige return (CR)
1398 "\f" => '\f', # form feed (FF)
1399 "\b" => '\b', # backspace (BS)
1400 "\a" => '\a', # alarm (bell) (BEL)
1401 "\e" => '\e', # escape (ESC)
1402 "\013" => '\v', # vertical tab (VT)
1403 "\000" => '\0', # nul character (NUL)
1405 my $chr = ( (exists $es{$cntrl})
1406 ? $es{$cntrl}
1407 : sprintf('\%2x', ord($cntrl)) );
1408 if ($opts{-nohtml}) {
1409 return $chr;
1410 } else {
1411 return "<span class=\"cntrl\">$chr</span>";
1415 # Alternatively use unicode control pictures codepoints,
1416 # Unicode "printable representation" (PR)
1417 sub quot_upr {
1418 my $cntrl = shift;
1419 my %opts = @_;
1421 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
1422 if ($opts{-nohtml}) {
1423 return $chr;
1424 } else {
1425 return "<span class=\"cntrl\">$chr</span>";
1429 # git may return quoted and escaped filenames
1430 sub unquote {
1431 my $str = shift;
1433 sub unq {
1434 my $seq = shift;
1435 my %es = ( # character escape codes, aka escape sequences
1436 't' => "\t", # tab (HT, TAB)
1437 'n' => "\n", # newline (NL)
1438 'r' => "\r", # return (CR)
1439 'f' => "\f", # form feed (FF)
1440 'b' => "\b", # backspace (BS)
1441 'a' => "\a", # alarm (bell) (BEL)
1442 'e' => "\e", # escape (ESC)
1443 'v' => "\013", # vertical tab (VT)
1446 if ($seq =~ m/^[0-7]{1,3}$/) {
1447 # octal char sequence
1448 return chr(oct($seq));
1449 } elsif (exists $es{$seq}) {
1450 # C escape sequence, aka character escape code
1451 return $es{$seq};
1453 # quoted ordinary character
1454 return $seq;
1457 if ($str =~ m/^"(.*)"$/) {
1458 # needs unquoting
1459 $str = $1;
1460 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
1462 return $str;
1465 # escape tabs (convert tabs to spaces)
1466 sub untabify {
1467 my $line = shift;
1469 while ((my $pos = index($line, "\t")) != -1) {
1470 if (my $count = (8 - ($pos % 8))) {
1471 my $spaces = ' ' x $count;
1472 $line =~ s/\t/$spaces/;
1476 return $line;
1479 sub project_in_list {
1480 my $project = shift;
1481 my @list = git_get_projects_list();
1482 return @list && scalar(grep { $_->{'path'} eq $project } @list);
1485 ## ----------------------------------------------------------------------
1486 ## HTML aware string manipulation
1488 # Try to chop given string on a word boundary between position
1489 # $len and $len+$add_len. If there is no word boundary there,
1490 # chop at $len+$add_len. Do not chop if chopped part plus ellipsis
1491 # (marking chopped part) would be longer than given string.
1492 sub chop_str {
1493 my $str = shift;
1494 my $len = shift;
1495 my $add_len = shift || 10;
1496 my $where = shift || 'right'; # 'left' | 'center' | 'right'
1498 # Make sure perl knows it is utf8 encoded so we don't
1499 # cut in the middle of a utf8 multibyte char.
1500 $str = to_utf8($str);
1502 # allow only $len chars, but don't cut a word if it would fit in $add_len
1503 # if it doesn't fit, cut it if it's still longer than the dots we would add
1504 # remove chopped character entities entirely
1506 # when chopping in the middle, distribute $len into left and right part
1507 # return early if chopping wouldn't make string shorter
1508 if ($where eq 'center') {
1509 return $str if ($len + 5 >= length($str)); # filler is length 5
1510 $len = int($len/2);
1511 } else {
1512 return $str if ($len + 4 >= length($str)); # filler is length 4
1515 # regexps: ending and beginning with word part up to $add_len
1516 my $endre = qr/.{$len}\w{0,$add_len}/;
1517 my $begre = qr/\w{0,$add_len}.{$len}/;
1519 if ($where eq 'left') {
1520 $str =~ m/^(.*?)($begre)$/;
1521 my ($lead, $body) = ($1, $2);
1522 if (length($lead) > 4) {
1523 $lead = " ...";
1525 return "$lead$body";
1527 } elsif ($where eq 'center') {
1528 $str =~ m/^($endre)(.*)$/;
1529 my ($left, $str) = ($1, $2);
1530 $str =~ m/^(.*?)($begre)$/;
1531 my ($mid, $right) = ($1, $2);
1532 if (length($mid) > 5) {
1533 $mid = " ... ";
1535 return "$left$mid$right";
1537 } else {
1538 $str =~ m/^($endre)(.*)$/;
1539 my $body = $1;
1540 my $tail = $2;
1541 if (length($tail) > 4) {
1542 $tail = "... ";
1544 return "$body$tail";
1548 # takes the same arguments as chop_str, but also wraps a <span> around the
1549 # result with a title attribute if it does get chopped. Additionally, the
1550 # string is HTML-escaped.
1551 sub chop_and_escape_str {
1552 my ($str) = @_;
1554 my $chopped = chop_str(@_);
1555 if ($chopped eq $str) {
1556 return esc_html($chopped);
1557 } else {
1558 $str =~ s/[[:cntrl:]]/?/g;
1559 return $cgi->span({-title=>$str}, esc_html($chopped));
1563 ## ----------------------------------------------------------------------
1564 ## functions returning short strings
1566 # CSS class for given age value (in seconds)
1567 sub age_class {
1568 my $age = shift;
1570 if (!defined $age) {
1571 return "noage";
1572 } elsif ($age < 60*60*2) {
1573 return "age0";
1574 } elsif ($age < 60*60*24*2) {
1575 return "age1";
1576 } else {
1577 return "age2";
1581 # convert age in seconds to "nn units ago" string
1582 sub age_string {
1583 my $age = shift;
1584 my $age_str;
1586 if ($age > 60*60*24*365*2) {
1587 $age_str = (int $age/60/60/24/365);
1588 $age_str .= " years ago";
1589 } elsif ($age > 60*60*24*(365/12)*2) {
1590 $age_str = int $age/60/60/24/(365/12);
1591 $age_str .= " months ago";
1592 } elsif ($age > 60*60*24*7*2) {
1593 $age_str = int $age/60/60/24/7;
1594 $age_str .= " weeks ago";
1595 } elsif ($age > 60*60*24*2) {
1596 $age_str = int $age/60/60/24;
1597 $age_str .= " days ago";
1598 } elsif ($age > 60*60*2) {
1599 $age_str = int $age/60/60;
1600 $age_str .= " hours ago";
1601 } elsif ($age > 60*2) {
1602 $age_str = int $age/60;
1603 $age_str .= " min ago";
1604 } elsif ($age > 2) {
1605 $age_str = int $age;
1606 $age_str .= " sec ago";
1607 } else {
1608 $age_str .= " right now";
1610 return $age_str;
1613 use constant {
1614 S_IFINVALID => 0030000,
1615 S_IFGITLINK => 0160000,
1618 # submodule/subproject, a commit object reference
1619 sub S_ISGITLINK {
1620 my $mode = shift;
1622 return (($mode & S_IFMT) == S_IFGITLINK)
1625 # convert file mode in octal to symbolic file mode string
1626 sub mode_str {
1627 my $mode = oct shift;
1629 if (S_ISGITLINK($mode)) {
1630 return 'm---------';
1631 } elsif (S_ISDIR($mode & S_IFMT)) {
1632 return 'drwxr-xr-x';
1633 } elsif (S_ISLNK($mode)) {
1634 return 'lrwxrwxrwx';
1635 } elsif (S_ISREG($mode)) {
1636 # git cares only about the executable bit
1637 if ($mode & S_IXUSR) {
1638 return '-rwxr-xr-x';
1639 } else {
1640 return '-rw-r--r--';
1642 } else {
1643 return '----------';
1647 # convert file mode in octal to file type string
1648 sub file_type {
1649 my $mode = shift;
1651 if ($mode !~ m/^[0-7]+$/) {
1652 return $mode;
1653 } else {
1654 $mode = oct $mode;
1657 if (S_ISGITLINK($mode)) {
1658 return "submodule";
1659 } elsif (S_ISDIR($mode & S_IFMT)) {
1660 return "directory";
1661 } elsif (S_ISLNK($mode)) {
1662 return "symlink";
1663 } elsif (S_ISREG($mode)) {
1664 return "file";
1665 } else {
1666 return "unknown";
1670 # convert file mode in octal to file type description string
1671 sub file_type_long {
1672 my $mode = shift;
1674 if ($mode !~ m/^[0-7]+$/) {
1675 return $mode;
1676 } else {
1677 $mode = oct $mode;
1680 if (S_ISGITLINK($mode)) {
1681 return "submodule";
1682 } elsif (S_ISDIR($mode & S_IFMT)) {
1683 return "directory";
1684 } elsif (S_ISLNK($mode)) {
1685 return "symlink";
1686 } elsif (S_ISREG($mode)) {
1687 if ($mode & S_IXUSR) {
1688 return "executable";
1689 } else {
1690 return "file";
1692 } else {
1693 return "unknown";
1698 ## ----------------------------------------------------------------------
1699 ## functions returning short HTML fragments, or transforming HTML fragments
1700 ## which don't belong to other sections
1702 # format line of commit message.
1703 sub format_log_line_html {
1704 my $line = shift;
1706 $line = esc_html($line, -nbsp=>1);
1707 $line =~ s{\b([0-9a-fA-F]{8,40})\b}{
1708 $cgi->a({-href => href(action=>"object", hash=>$1),
1709 -class => "text"}, $1);
1710 }eg;
1712 return $line;
1715 # format marker of refs pointing to given object
1717 # the destination action is chosen based on object type and current context:
1718 # - for annotated tags, we choose the tag view unless it's the current view
1719 # already, in which case we go to shortlog view
1720 # - for other refs, we keep the current view if we're in history, shortlog or
1721 # log view, and select shortlog otherwise
1722 sub format_ref_marker {
1723 my ($refs, $id) = @_;
1724 my $markers = '';
1726 if (defined $refs->{$id}) {
1727 foreach my $ref (@{$refs->{$id}}) {
1728 # this code exploits the fact that non-lightweight tags are the
1729 # only indirect objects, and that they are the only objects for which
1730 # we want to use tag instead of shortlog as action
1731 my ($type, $name) = qw();
1732 my $indirect = ($ref =~ s/\^\{\}$//);
1733 # e.g. tags/v2.6.11 or heads/next
1734 if ($ref =~ m!^(.*?)s?/(.*)$!) {
1735 $type = $1;
1736 $name = $2;
1737 } else {
1738 $type = "ref";
1739 $name = $ref;
1742 my $class = $type;
1743 $class .= " indirect" if $indirect;
1745 my $dest_action = "shortlog";
1747 if ($indirect) {
1748 $dest_action = "tag" unless $action eq "tag";
1749 } elsif ($action =~ /^(history|(short)?log)$/) {
1750 $dest_action = $action;
1753 my $dest = "";
1754 $dest .= "refs/" unless $ref =~ m!^refs/!;
1755 $dest .= $ref;
1757 my $link = $cgi->a({
1758 -href => href(
1759 action=>$dest_action,
1760 hash=>$dest
1761 )}, $name);
1763 $markers .= " <span class=\"$class\" title=\"$ref\">" .
1764 $link . "</span>";
1768 if ($markers) {
1769 return ' <span class="refs">'. $markers . '</span>';
1770 } else {
1771 return "";
1775 # format, perhaps shortened and with markers, title line
1776 sub format_subject_html {
1777 my ($long, $short, $href, $extra) = @_;
1778 $extra = '' unless defined($extra);
1780 if (length($short) < length($long)) {
1781 $long =~ s/[[:cntrl:]]/?/g;
1782 return $cgi->a({-href => $href, -class => "list subject",
1783 -title => to_utf8($long)},
1784 esc_html($short)) . $extra;
1785 } else {
1786 return $cgi->a({-href => $href, -class => "list subject"},
1787 esc_html($long)) . $extra;
1791 # Rather than recomputing the url for an email multiple times, we cache it
1792 # after the first hit. This gives a visible benefit in views where the avatar
1793 # for the same email is used repeatedly (e.g. shortlog).
1794 # The cache is shared by all avatar engines (currently gravatar only), which
1795 # are free to use it as preferred. Since only one avatar engine is used for any
1796 # given page, there's no risk for cache conflicts.
1797 our %avatar_cache = ();
1799 # Compute the picon url for a given email, by using the picon search service over at
1800 # http://www.cs.indiana.edu/picons/search.html
1801 sub picon_url {
1802 my $email = lc shift;
1803 if (!$avatar_cache{$email}) {
1804 my ($user, $domain) = split('@', $email);
1805 $avatar_cache{$email} =
1806 "http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/" .
1807 "$domain/$user/" .
1808 "users+domains+unknown/up/single";
1810 return $avatar_cache{$email};
1813 # Compute the gravatar url for a given email, if it's not in the cache already.
1814 # Gravatar stores only the part of the URL before the size, since that's the
1815 # one computationally more expensive. This also allows reuse of the cache for
1816 # different sizes (for this particular engine).
1817 sub gravatar_url {
1818 my $email = lc shift;
1819 my $size = shift;
1820 $avatar_cache{$email} ||=
1821 "http://www.gravatar.com/avatar/" .
1822 Digest::MD5::md5_hex($email) . "?s=";
1823 return $avatar_cache{$email} . $size;
1826 # Insert an avatar for the given $email at the given $size if the feature
1827 # is enabled.
1828 sub git_get_avatar {
1829 my ($email, %opts) = @_;
1830 my $pre_white = ($opts{-pad_before} ? "&nbsp;" : "");
1831 my $post_white = ($opts{-pad_after} ? "&nbsp;" : "");
1832 $opts{-size} ||= 'default';
1833 my $size = $avatar_size{$opts{-size}} || $avatar_size{'default'};
1834 my $url = "";
1835 if ($git_avatar eq 'gravatar') {
1836 $url = gravatar_url($email, $size);
1837 } elsif ($git_avatar eq 'picon') {
1838 $url = picon_url($email);
1840 # Other providers can be added by extending the if chain, defining $url
1841 # as needed. If no variant puts something in $url, we assume avatars
1842 # are completely disabled/unavailable.
1843 if ($url) {
1844 return $pre_white .
1845 "<img width=\"$size\" " .
1846 "class=\"avatar\" " .
1847 "src=\"$url\" " .
1848 "alt=\"\" " .
1849 "/>" . $post_white;
1850 } else {
1851 return "";
1855 sub format_search_author {
1856 my ($author, $searchtype, $displaytext) = @_;
1857 my $have_search = gitweb_check_feature('search');
1859 if ($have_search) {
1860 my $performed = "";
1861 if ($searchtype eq 'author') {
1862 $performed = "authored";
1863 } elsif ($searchtype eq 'committer') {
1864 $performed = "committed";
1867 return $cgi->a({-href => href(action=>"search", hash=>$hash,
1868 searchtext=>$author,
1869 searchtype=>$searchtype), class=>"list",
1870 title=>"Search for commits $performed by $author"},
1871 $displaytext);
1873 } else {
1874 return $displaytext;
1878 # format the author name of the given commit with the given tag
1879 # the author name is chopped and escaped according to the other
1880 # optional parameters (see chop_str).
1881 sub format_author_html {
1882 my $tag = shift;
1883 my $co = shift;
1884 my $author = chop_and_escape_str($co->{'author_name'}, @_);
1885 return "<$tag class=\"author\">" .
1886 format_search_author($co->{'author_name'}, "author",
1887 git_get_avatar($co->{'author_email'}, -pad_after => 1) .
1888 $author) .
1889 "</$tag>";
1892 # format git diff header line, i.e. "diff --(git|combined|cc) ..."
1893 sub format_git_diff_header_line {
1894 my $line = shift;
1895 my $diffinfo = shift;
1896 my ($from, $to) = @_;
1898 if ($diffinfo->{'nparents'}) {
1899 # combined diff
1900 $line =~ s!^(diff (.*?) )"?.*$!$1!;
1901 if ($to->{'href'}) {
1902 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1903 esc_path($to->{'file'}));
1904 } else { # file was deleted (no href)
1905 $line .= esc_path($to->{'file'});
1907 } else {
1908 # "ordinary" diff
1909 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
1910 if ($from->{'href'}) {
1911 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
1912 'a/' . esc_path($from->{'file'}));
1913 } else { # file was added (no href)
1914 $line .= 'a/' . esc_path($from->{'file'});
1916 $line .= ' ';
1917 if ($to->{'href'}) {
1918 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1919 'b/' . esc_path($to->{'file'}));
1920 } else { # file was deleted
1921 $line .= 'b/' . esc_path($to->{'file'});
1925 return "<div class=\"diff header\">$line</div>\n";
1928 # format extended diff header line, before patch itself
1929 sub format_extended_diff_header_line {
1930 my $line = shift;
1931 my $diffinfo = shift;
1932 my ($from, $to) = @_;
1934 # match <path>
1935 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
1936 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1937 esc_path($from->{'file'}));
1939 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
1940 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1941 esc_path($to->{'file'}));
1943 # match single <mode>
1944 if ($line =~ m/\s(\d{6})$/) {
1945 $line .= '<span class="info"> (' .
1946 file_type_long($1) .
1947 ')</span>';
1949 # match <hash>
1950 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
1951 # can match only for combined diff
1952 $line = 'index ';
1953 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1954 if ($from->{'href'}[$i]) {
1955 $line .= $cgi->a({-href=>$from->{'href'}[$i],
1956 -class=>"hash"},
1957 substr($diffinfo->{'from_id'}[$i],0,7));
1958 } else {
1959 $line .= '0' x 7;
1961 # separator
1962 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
1964 $line .= '..';
1965 if ($to->{'href'}) {
1966 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1967 substr($diffinfo->{'to_id'},0,7));
1968 } else {
1969 $line .= '0' x 7;
1972 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
1973 # can match only for ordinary diff
1974 my ($from_link, $to_link);
1975 if ($from->{'href'}) {
1976 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
1977 substr($diffinfo->{'from_id'},0,7));
1978 } else {
1979 $from_link = '0' x 7;
1981 if ($to->{'href'}) {
1982 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1983 substr($diffinfo->{'to_id'},0,7));
1984 } else {
1985 $to_link = '0' x 7;
1987 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
1988 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
1991 return $line . "<br/>\n";
1994 # format from-file/to-file diff header
1995 sub format_diff_from_to_header {
1996 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
1997 my $line;
1998 my $result = '';
2000 $line = $from_line;
2001 #assert($line =~ m/^---/) if DEBUG;
2002 # no extra formatting for "^--- /dev/null"
2003 if (! $diffinfo->{'nparents'}) {
2004 # ordinary (single parent) diff
2005 if ($line =~ m!^--- "?a/!) {
2006 if ($from->{'href'}) {
2007 $line = '--- a/' .
2008 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
2009 esc_path($from->{'file'}));
2010 } else {
2011 $line = '--- a/' .
2012 esc_path($from->{'file'});
2015 $result .= qq!<div class="diff from_file">$line</div>\n!;
2017 } else {
2018 # combined diff (merge commit)
2019 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2020 if ($from->{'href'}[$i]) {
2021 $line = '--- ' .
2022 $cgi->a({-href=>href(action=>"blobdiff",
2023 hash_parent=>$diffinfo->{'from_id'}[$i],
2024 hash_parent_base=>$parents[$i],
2025 file_parent=>$from->{'file'}[$i],
2026 hash=>$diffinfo->{'to_id'},
2027 hash_base=>$hash,
2028 file_name=>$to->{'file'}),
2029 -class=>"path",
2030 -title=>"diff" . ($i+1)},
2031 $i+1) .
2032 '/' .
2033 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
2034 esc_path($from->{'file'}[$i]));
2035 } else {
2036 $line = '--- /dev/null';
2038 $result .= qq!<div class="diff from_file">$line</div>\n!;
2042 $line = $to_line;
2043 #assert($line =~ m/^\+\+\+/) if DEBUG;
2044 # no extra formatting for "^+++ /dev/null"
2045 if ($line =~ m!^\+\+\+ "?b/!) {
2046 if ($to->{'href'}) {
2047 $line = '+++ b/' .
2048 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
2049 esc_path($to->{'file'}));
2050 } else {
2051 $line = '+++ b/' .
2052 esc_path($to->{'file'});
2055 $result .= qq!<div class="diff to_file">$line</div>\n!;
2057 return $result;
2060 # create note for patch simplified by combined diff
2061 sub format_diff_cc_simplified {
2062 my ($diffinfo, @parents) = @_;
2063 my $result = '';
2065 $result .= "<div class=\"diff header\">" .
2066 "diff --cc ";
2067 if (!is_deleted($diffinfo)) {
2068 $result .= $cgi->a({-href => href(action=>"blob",
2069 hash_base=>$hash,
2070 hash=>$diffinfo->{'to_id'},
2071 file_name=>$diffinfo->{'to_file'}),
2072 -class => "path"},
2073 esc_path($diffinfo->{'to_file'}));
2074 } else {
2075 $result .= esc_path($diffinfo->{'to_file'});
2077 $result .= "</div>\n" . # class="diff header"
2078 "<div class=\"diff nodifferences\">" .
2079 "Simple merge" .
2080 "</div>\n"; # class="diff nodifferences"
2082 return $result;
2085 # format patch (diff) line (not to be used for diff headers)
2086 sub format_diff_line {
2087 my $line = shift;
2088 my ($from, $to) = @_;
2089 my $diff_class = "";
2091 chomp $line;
2093 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
2094 # combined diff
2095 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
2096 if ($line =~ m/^\@{3}/) {
2097 $diff_class = " chunk_header";
2098 } elsif ($line =~ m/^\\/) {
2099 $diff_class = " incomplete";
2100 } elsif ($prefix =~ tr/+/+/) {
2101 $diff_class = " add";
2102 } elsif ($prefix =~ tr/-/-/) {
2103 $diff_class = " rem";
2105 } else {
2106 # assume ordinary diff
2107 my $char = substr($line, 0, 1);
2108 if ($char eq '+') {
2109 $diff_class = " add";
2110 } elsif ($char eq '-') {
2111 $diff_class = " rem";
2112 } elsif ($char eq '@') {
2113 $diff_class = " chunk_header";
2114 } elsif ($char eq "\\") {
2115 $diff_class = " incomplete";
2118 $line = untabify($line);
2119 if ($from && $to && $line =~ m/^\@{2} /) {
2120 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
2121 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
2123 $from_lines = 0 unless defined $from_lines;
2124 $to_lines = 0 unless defined $to_lines;
2126 if ($from->{'href'}) {
2127 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
2128 -class=>"list"}, $from_text);
2130 if ($to->{'href'}) {
2131 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
2132 -class=>"list"}, $to_text);
2134 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
2135 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
2136 return "<div class=\"diff$diff_class\">$line</div>\n";
2137 } elsif ($from && $to && $line =~ m/^\@{3}/) {
2138 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
2139 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
2141 @from_text = split(' ', $ranges);
2142 for (my $i = 0; $i < @from_text; ++$i) {
2143 ($from_start[$i], $from_nlines[$i]) =
2144 (split(',', substr($from_text[$i], 1)), 0);
2147 $to_text = pop @from_text;
2148 $to_start = pop @from_start;
2149 $to_nlines = pop @from_nlines;
2151 $line = "<span class=\"chunk_info\">$prefix ";
2152 for (my $i = 0; $i < @from_text; ++$i) {
2153 if ($from->{'href'}[$i]) {
2154 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
2155 -class=>"list"}, $from_text[$i]);
2156 } else {
2157 $line .= $from_text[$i];
2159 $line .= " ";
2161 if ($to->{'href'}) {
2162 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
2163 -class=>"list"}, $to_text);
2164 } else {
2165 $line .= $to_text;
2167 $line .= " $prefix</span>" .
2168 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
2169 return "<div class=\"diff$diff_class\">$line</div>\n";
2171 return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
2174 # Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
2175 # linked. Pass the hash of the tree/commit to snapshot.
2176 sub format_snapshot_links {
2177 my ($hash) = @_;
2178 my $num_fmts = @snapshot_fmts;
2179 if ($num_fmts > 1) {
2180 # A parenthesized list of links bearing format names.
2181 # e.g. "snapshot (_tar.gz_ _zip_)"
2182 return "snapshot (" . join(' ', map
2183 $cgi->a({
2184 -href => href(
2185 action=>"snapshot",
2186 hash=>$hash,
2187 snapshot_format=>$_
2189 }, $known_snapshot_formats{$_}{'display'})
2190 , @snapshot_fmts) . ")";
2191 } elsif ($num_fmts == 1) {
2192 # A single "snapshot" link whose tooltip bears the format name.
2193 # i.e. "_snapshot_"
2194 my ($fmt) = @snapshot_fmts;
2195 return
2196 $cgi->a({
2197 -href => href(
2198 action=>"snapshot",
2199 hash=>$hash,
2200 snapshot_format=>$fmt
2202 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
2203 }, "snapshot");
2204 } else { # $num_fmts == 0
2205 return undef;
2209 ## ......................................................................
2210 ## functions returning values to be passed, perhaps after some
2211 ## transformation, to other functions; e.g. returning arguments to href()
2213 # returns hash to be passed to href to generate gitweb URL
2214 # in -title key it returns description of link
2215 sub get_feed_info {
2216 my $format = shift || 'Atom';
2217 my %res = (action => lc($format));
2219 # feed links are possible only for project views
2220 return unless (defined $project);
2221 # some views should link to OPML, or to generic project feed,
2222 # or don't have specific feed yet (so they should use generic)
2223 return if ($action =~ /^(?:tags|heads|forks|tag|search)$/x);
2225 my $branch;
2226 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
2227 # from tag links; this also makes possible to detect branch links
2228 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
2229 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
2230 $branch = $1;
2232 # find log type for feed description (title)
2233 my $type = 'log';
2234 if (defined $file_name) {
2235 $type = "history of $file_name";
2236 $type .= "/" if ($action eq 'tree');
2237 $type .= " on '$branch'" if (defined $branch);
2238 } else {
2239 $type = "log of $branch" if (defined $branch);
2242 $res{-title} = $type;
2243 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
2244 $res{'file_name'} = $file_name;
2246 return %res;
2249 ## ----------------------------------------------------------------------
2250 ## git utility subroutines, invoking git commands
2252 # returns path to the core git executable and the --git-dir parameter as list
2253 sub git_cmd {
2254 $number_of_git_cmds++;
2255 return $GIT, '--git-dir='.$git_dir;
2258 # quote the given arguments for passing them to the shell
2259 # quote_command("command", "arg 1", "arg with ' and ! characters")
2260 # => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
2261 # Try to avoid using this function wherever possible.
2262 sub quote_command {
2263 return join(' ',
2264 map { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ );
2267 # get HEAD ref of given project as hash
2268 sub git_get_head_hash {
2269 return git_get_full_hash(shift, 'HEAD');
2272 sub git_get_full_hash {
2273 return git_get_hash(@_);
2276 sub git_get_short_hash {
2277 return git_get_hash(@_, '--short=7');
2280 sub git_get_hash {
2281 my ($project, $hash, @options) = @_;
2282 my $o_git_dir = $git_dir;
2283 my $retval = undef;
2284 $git_dir = "$projectroot/$project";
2285 if (open my $fd, '-|', git_cmd(), 'rev-parse',
2286 '--verify', '-q', @options, $hash) {
2287 $retval = <$fd>;
2288 chomp $retval if defined $retval;
2289 close $fd;
2291 if (defined $o_git_dir) {
2292 $git_dir = $o_git_dir;
2294 return $retval;
2297 # get type of given object
2298 sub git_get_type {
2299 my $hash = shift;
2301 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
2302 my $type = <$fd>;
2303 close $fd or return;
2304 chomp $type;
2305 return $type;
2308 # repository configuration
2309 our $config_file = '';
2310 our %config;
2312 # store multiple values for single key as anonymous array reference
2313 # single values stored directly in the hash, not as [ <value> ]
2314 sub hash_set_multi {
2315 my ($hash, $key, $value) = @_;
2317 if (!exists $hash->{$key}) {
2318 $hash->{$key} = $value;
2319 } elsif (!ref $hash->{$key}) {
2320 $hash->{$key} = [ $hash->{$key}, $value ];
2321 } else {
2322 push @{$hash->{$key}}, $value;
2326 # return hash of git project configuration
2327 # optionally limited to some section, e.g. 'gitweb'
2328 sub git_parse_project_config {
2329 my $section_regexp = shift;
2330 my %config;
2332 local $/ = "\0";
2334 open my $fh, "-|", git_cmd(), "config", '-z', '-l',
2335 or return;
2337 while (my $keyval = <$fh>) {
2338 chomp $keyval;
2339 my ($key, $value) = split(/\n/, $keyval, 2);
2341 hash_set_multi(\%config, $key, $value)
2342 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
2344 close $fh;
2346 return %config;
2349 # convert config value to boolean: 'true' or 'false'
2350 # no value, number > 0, 'true' and 'yes' values are true
2351 # rest of values are treated as false (never as error)
2352 sub config_to_bool {
2353 my $val = shift;
2355 return 1 if !defined $val; # section.key
2357 # strip leading and trailing whitespace
2358 $val =~ s/^\s+//;
2359 $val =~ s/\s+$//;
2361 return (($val =~ /^\d+$/ && $val) || # section.key = 1
2362 ($val =~ /^(?:true|yes)$/i)); # section.key = true
2365 # convert config value to simple decimal number
2366 # an optional value suffix of 'k', 'm', or 'g' will cause the value
2367 # to be multiplied by 1024, 1048576, or 1073741824
2368 sub config_to_int {
2369 my $val = shift;
2371 # strip leading and trailing whitespace
2372 $val =~ s/^\s+//;
2373 $val =~ s/\s+$//;
2375 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
2376 $unit = lc($unit);
2377 # unknown unit is treated as 1
2378 return $num * ($unit eq 'g' ? 1073741824 :
2379 $unit eq 'm' ? 1048576 :
2380 $unit eq 'k' ? 1024 : 1);
2382 return $val;
2385 # convert config value to array reference, if needed
2386 sub config_to_multi {
2387 my $val = shift;
2389 return ref($val) ? $val : (defined($val) ? [ $val ] : []);
2392 sub git_get_project_config {
2393 my ($key, $type) = @_;
2395 return unless defined $git_dir;
2397 # key sanity check
2398 return unless ($key);
2399 $key =~ s/^gitweb\.//;
2400 return if ($key =~ m/\W/);
2402 # type sanity check
2403 if (defined $type) {
2404 $type =~ s/^--//;
2405 $type = undef
2406 unless ($type eq 'bool' || $type eq 'int');
2409 # get config
2410 if (!defined $config_file ||
2411 $config_file ne "$git_dir/config") {
2412 %config = git_parse_project_config('gitweb');
2413 $config_file = "$git_dir/config";
2416 # check if config variable (key) exists
2417 return unless exists $config{"gitweb.$key"};
2419 # ensure given type
2420 if (!defined $type) {
2421 return $config{"gitweb.$key"};
2422 } elsif ($type eq 'bool') {
2423 # backward compatibility: 'git config --bool' returns true/false
2424 return config_to_bool($config{"gitweb.$key"}) ? 'true' : 'false';
2425 } elsif ($type eq 'int') {
2426 return config_to_int($config{"gitweb.$key"});
2428 return $config{"gitweb.$key"};
2431 # get hash of given path at given ref
2432 sub git_get_hash_by_path {
2433 my $base = shift;
2434 my $path = shift || return undef;
2435 my $type = shift;
2437 $path =~ s,/+$,,;
2439 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
2440 or die_error(500, "Open git-ls-tree failed");
2441 my $line = <$fd>;
2442 close $fd or return undef;
2444 if (!defined $line) {
2445 # there is no tree or hash given by $path at $base
2446 return undef;
2449 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2450 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
2451 if (defined $type && $type ne $2) {
2452 # type doesn't match
2453 return undef;
2455 return $3;
2458 # get path of entry with given hash at given tree-ish (ref)
2459 # used to get 'from' filename for combined diff (merge commit) for renames
2460 sub git_get_path_by_hash {
2461 my $base = shift || return;
2462 my $hash = shift || return;
2464 local $/ = "\0";
2466 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
2467 or return undef;
2468 while (my $line = <$fd>) {
2469 chomp $line;
2471 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
2472 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
2473 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
2474 close $fd;
2475 return $1;
2478 close $fd;
2479 return undef;
2482 ## ......................................................................
2483 ## git utility functions, directly accessing git repository
2485 sub git_get_project_description {
2486 my $path = shift;
2488 $git_dir = "$projectroot/$path";
2489 open my $fd, '<', "$git_dir/description"
2490 or return git_get_project_config('description');
2491 my $descr = <$fd>;
2492 close $fd;
2493 if (defined $descr) {
2494 chomp $descr;
2496 return $descr;
2499 sub git_get_project_ctags {
2500 my $path = shift;
2501 my $ctags = {};
2503 $git_dir = "$projectroot/$path";
2504 opendir my $dh, "$git_dir/ctags"
2505 or return $ctags;
2506 foreach (grep { -f $_ } map { "$git_dir/ctags/$_" } readdir($dh)) {
2507 open my $ct, '<', $_ or next;
2508 my $val = <$ct>;
2509 chomp $val;
2510 close $ct;
2511 my $ctag = $_; $ctag =~ s#.*/##;
2512 $ctags->{$ctag} = $val;
2514 closedir $dh;
2515 $ctags;
2518 sub git_populate_project_tagcloud {
2519 my $ctags = shift;
2521 # First, merge different-cased tags; tags vote on casing
2522 my %ctags_lc;
2523 foreach (keys %$ctags) {
2524 $ctags_lc{lc $_}->{count} += $ctags->{$_};
2525 if (not $ctags_lc{lc $_}->{topcount}
2526 or $ctags_lc{lc $_}->{topcount} < $ctags->{$_}) {
2527 $ctags_lc{lc $_}->{topcount} = $ctags->{$_};
2528 $ctags_lc{lc $_}->{topname} = $_;
2532 my $cloud;
2533 if (eval { require HTML::TagCloud; 1; }) {
2534 $cloud = HTML::TagCloud->new;
2535 foreach (sort keys %ctags_lc) {
2536 # Pad the title with spaces so that the cloud looks
2537 # less crammed.
2538 my $title = $ctags_lc{$_}->{topname};
2539 $title =~ s/ /&nbsp;/g;
2540 $title =~ s/^/&nbsp;/g;
2541 $title =~ s/$/&nbsp;/g;
2542 $cloud->add($title, $home_link."?by_tag=".$_, $ctags_lc{$_}->{count});
2544 } else {
2545 $cloud = \%ctags_lc;
2547 $cloud;
2550 sub git_show_project_tagcloud {
2551 my ($cloud, $count) = @_;
2552 print STDERR ref($cloud)."..\n";
2553 if (ref $cloud eq 'HTML::TagCloud') {
2554 return $cloud->html_and_css($count);
2555 } else {
2556 my @tags = sort { $cloud->{$a}->{count} <=> $cloud->{$b}->{count} } keys %$cloud;
2557 return '<p align="center">' . join (', ', map {
2558 "<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"
2559 } splice(@tags, 0, $count)) . '</p>';
2563 sub git_get_project_url_list {
2564 my $path = shift;
2566 $git_dir = "$projectroot/$path";
2567 open my $fd, '<', "$git_dir/cloneurl"
2568 or return wantarray ?
2569 @{ config_to_multi(git_get_project_config('url')) } :
2570 config_to_multi(git_get_project_config('url'));
2571 my @git_project_url_list = map { chomp; $_ } <$fd>;
2572 close $fd;
2574 return wantarray ? @git_project_url_list : \@git_project_url_list;
2577 sub git_get_projects_list {
2578 my ($filter) = @_;
2579 my @list;
2581 $filter ||= '';
2582 $filter =~ s/\.git$//;
2584 my $check_forks = gitweb_check_feature('forks');
2586 if (-d $projects_list) {
2587 # search in directory
2588 my $dir = $projects_list . ($filter ? "/$filter" : '');
2589 # remove the trailing "/"
2590 $dir =~ s!/+$!!;
2591 my $pfxlen = length("$dir");
2592 my $pfxdepth = ($dir =~ tr!/!!);
2594 File::Find::find({
2595 follow_fast => 1, # follow symbolic links
2596 follow_skip => 2, # ignore duplicates
2597 dangling_symlinks => 0, # ignore dangling symlinks, silently
2598 wanted => sub {
2599 # global variables
2600 our $project_maxdepth;
2601 our $projectroot;
2602 # skip project-list toplevel, if we get it.
2603 return if (m!^[/.]$!);
2604 # only directories can be git repositories
2605 return unless (-d $_);
2606 # don't traverse too deep (Find is super slow on os x)
2607 if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
2608 $File::Find::prune = 1;
2609 return;
2612 my $subdir = substr($File::Find::name, $pfxlen + 1);
2613 # we check related file in $projectroot
2614 my $path = ($filter ? "$filter/" : '') . $subdir;
2615 if (check_export_ok("$projectroot/$path")) {
2616 push @list, { path => $path };
2617 $File::Find::prune = 1;
2620 }, "$dir");
2622 } elsif (-f $projects_list) {
2623 # read from file(url-encoded):
2624 # 'git%2Fgit.git Linus+Torvalds'
2625 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2626 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2627 my %paths;
2628 open my $fd, '<', $projects_list or return;
2629 PROJECT:
2630 while (my $line = <$fd>) {
2631 chomp $line;
2632 my ($path, $owner) = split ' ', $line;
2633 $path = unescape($path);
2634 $owner = unescape($owner);
2635 if (!defined $path) {
2636 next;
2638 if ($filter ne '') {
2639 # looking for forks;
2640 my $pfx = substr($path, 0, length($filter));
2641 if ($pfx ne $filter) {
2642 next PROJECT;
2644 my $sfx = substr($path, length($filter));
2645 if ($sfx !~ /^\/.*\.git$/) {
2646 next PROJECT;
2648 } elsif ($check_forks) {
2649 PATH:
2650 foreach my $filter (keys %paths) {
2651 # looking for forks;
2652 my $pfx = substr($path, 0, length($filter));
2653 if ($pfx ne $filter) {
2654 next PATH;
2656 my $sfx = substr($path, length($filter));
2657 if ($sfx !~ /^\/.*\.git$/) {
2658 next PATH;
2660 # is a fork, don't include it in
2661 # the list
2662 next PROJECT;
2665 if (check_export_ok("$projectroot/$path")) {
2666 my $pr = {
2667 path => $path,
2668 owner => to_utf8($owner),
2670 push @list, $pr;
2671 (my $forks_path = $path) =~ s/\.git$//;
2672 $paths{$forks_path}++;
2675 close $fd;
2677 return @list;
2680 our $gitweb_project_owner = undef;
2681 sub git_get_project_list_from_file {
2683 return if (defined $gitweb_project_owner);
2685 $gitweb_project_owner = {};
2686 # read from file (url-encoded):
2687 # 'git%2Fgit.git Linus+Torvalds'
2688 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2689 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2690 if (-f $projects_list) {
2691 open(my $fd, '<', $projects_list);
2692 while (my $line = <$fd>) {
2693 chomp $line;
2694 my ($pr, $ow) = split ' ', $line;
2695 $pr = unescape($pr);
2696 $ow = unescape($ow);
2697 $gitweb_project_owner->{$pr} = to_utf8($ow);
2699 close $fd;
2703 sub git_get_project_owner {
2704 my $project = shift;
2705 my $owner;
2707 return undef unless $project;
2708 $git_dir = "$projectroot/$project";
2710 if (!defined $gitweb_project_owner) {
2711 git_get_project_list_from_file();
2714 if (exists $gitweb_project_owner->{$project}) {
2715 $owner = $gitweb_project_owner->{$project};
2717 if (!defined $owner){
2718 $owner = git_get_project_config('owner');
2720 if (!defined $owner) {
2721 $owner = get_file_owner("$git_dir");
2724 return $owner;
2727 sub git_get_last_activity {
2728 my ($path) = @_;
2729 my $fd;
2731 $git_dir = "$projectroot/$path";
2732 open($fd, "-|", git_cmd(), 'for-each-ref',
2733 '--format=%(committer)',
2734 '--sort=-committerdate',
2735 '--count=1',
2736 'refs/heads') or return;
2737 my $most_recent = <$fd>;
2738 close $fd or return;
2739 if (defined $most_recent &&
2740 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
2741 my $timestamp = $1;
2742 my $age = time - $timestamp;
2743 return ($age, age_string($age));
2745 return (undef, undef);
2748 sub git_get_references {
2749 my $type = shift || "";
2750 my %refs;
2751 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
2752 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
2753 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
2754 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
2755 or return;
2757 while (my $line = <$fd>) {
2758 chomp $line;
2759 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {
2760 if (defined $refs{$1}) {
2761 push @{$refs{$1}}, $2;
2762 } else {
2763 $refs{$1} = [ $2 ];
2767 close $fd or return;
2768 return \%refs;
2771 sub git_get_rev_name_tags {
2772 my $hash = shift || return undef;
2774 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
2775 or return;
2776 my $name_rev = <$fd>;
2777 close $fd;
2779 if ($name_rev =~ m|^$hash tags/(.*)$|) {
2780 return $1;
2781 } else {
2782 # catches also '$hash undefined' output
2783 return undef;
2787 ## ----------------------------------------------------------------------
2788 ## parse to hash functions
2790 sub parse_date {
2791 my $epoch = shift;
2792 my $tz = shift || "-0000";
2794 my %date;
2795 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
2796 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
2797 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
2798 $date{'hour'} = $hour;
2799 $date{'minute'} = $min;
2800 $date{'mday'} = $mday;
2801 $date{'day'} = $days[$wday];
2802 $date{'month'} = $months[$mon];
2803 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
2804 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
2805 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
2806 $mday, $months[$mon], $hour ,$min;
2807 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
2808 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
2810 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
2811 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
2812 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
2813 $date{'hour_local'} = $hour;
2814 $date{'minute_local'} = $min;
2815 $date{'tz_local'} = $tz;
2816 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
2817 1900+$year, $mon+1, $mday,
2818 $hour, $min, $sec, $tz);
2819 return %date;
2822 sub parse_tag {
2823 my $tag_id = shift;
2824 my %tag;
2825 my @comment;
2827 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
2828 $tag{'id'} = $tag_id;
2829 while (my $line = <$fd>) {
2830 chomp $line;
2831 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
2832 $tag{'object'} = $1;
2833 } elsif ($line =~ m/^type (.+)$/) {
2834 $tag{'type'} = $1;
2835 } elsif ($line =~ m/^tag (.+)$/) {
2836 $tag{'name'} = $1;
2837 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
2838 $tag{'author'} = $1;
2839 $tag{'author_epoch'} = $2;
2840 $tag{'author_tz'} = $3;
2841 if ($tag{'author'} =~ m/^([^<]+) <([^>]*)>/) {
2842 $tag{'author_name'} = $1;
2843 $tag{'author_email'} = $2;
2844 } else {
2845 $tag{'author_name'} = $tag{'author'};
2847 } elsif ($line =~ m/--BEGIN/) {
2848 push @comment, $line;
2849 last;
2850 } elsif ($line eq "") {
2851 last;
2854 push @comment, <$fd>;
2855 $tag{'comment'} = \@comment;
2856 close $fd or return;
2857 if (!defined $tag{'name'}) {
2858 return
2860 return %tag
2863 sub parse_commit_text {
2864 my ($commit_text, $withparents) = @_;
2865 my @commit_lines = split '\n', $commit_text;
2866 my %co;
2868 pop @commit_lines; # Remove '\0'
2870 if (! @commit_lines) {
2871 return;
2874 my $header = shift @commit_lines;
2875 if ($header !~ m/^[0-9a-fA-F]{40}/) {
2876 return;
2878 ($co{'id'}, my @parents) = split ' ', $header;
2879 while (my $line = shift @commit_lines) {
2880 last if $line eq "\n";
2881 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
2882 $co{'tree'} = $1;
2883 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
2884 push @parents, $1;
2885 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
2886 $co{'author'} = to_utf8($1);
2887 $co{'author_epoch'} = $2;
2888 $co{'author_tz'} = $3;
2889 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
2890 $co{'author_name'} = $1;
2891 $co{'author_email'} = $2;
2892 } else {
2893 $co{'author_name'} = $co{'author'};
2895 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
2896 $co{'committer'} = to_utf8($1);
2897 $co{'committer_epoch'} = $2;
2898 $co{'committer_tz'} = $3;
2899 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
2900 $co{'committer_name'} = $1;
2901 $co{'committer_email'} = $2;
2902 } else {
2903 $co{'committer_name'} = $co{'committer'};
2907 if (!defined $co{'tree'}) {
2908 return;
2910 $co{'parents'} = \@parents;
2911 $co{'parent'} = $parents[0];
2913 foreach my $title (@commit_lines) {
2914 $title =~ s/^ //;
2915 if ($title ne "") {
2916 $co{'title'} = chop_str($title, 80, 5);
2917 # remove leading stuff of merges to make the interesting part visible
2918 if (length($title) > 50) {
2919 $title =~ s/^Automatic //;
2920 $title =~ s/^merge (of|with) /Merge ... /i;
2921 if (length($title) > 50) {
2922 $title =~ s/(http|rsync):\/\///;
2924 if (length($title) > 50) {
2925 $title =~ s/(master|www|rsync)\.//;
2927 if (length($title) > 50) {
2928 $title =~ s/kernel.org:?//;
2930 if (length($title) > 50) {
2931 $title =~ s/\/pub\/scm//;
2934 $co{'title_short'} = chop_str($title, 50, 5);
2935 last;
2938 if (! defined $co{'title'} || $co{'title'} eq "") {
2939 $co{'title'} = $co{'title_short'} = '(no commit message)';
2941 # remove added spaces
2942 foreach my $line (@commit_lines) {
2943 $line =~ s/^ //;
2945 $co{'comment'} = \@commit_lines;
2947 my $age = time - $co{'committer_epoch'};
2948 $co{'age'} = $age;
2949 $co{'age_string'} = age_string($age);
2950 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
2951 if ($age > 60*60*24*7*2) {
2952 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2953 $co{'age_string_age'} = $co{'age_string'};
2954 } else {
2955 $co{'age_string_date'} = $co{'age_string'};
2956 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2958 return %co;
2961 sub parse_commit {
2962 my ($commit_id) = @_;
2963 my %co;
2965 local $/ = "\0";
2967 open my $fd, "-|", git_cmd(), "rev-list",
2968 "--parents",
2969 "--header",
2970 "--max-count=1",
2971 $commit_id,
2972 "--",
2973 or die_error(500, "Open git-rev-list failed");
2974 %co = parse_commit_text(<$fd>, 1);
2975 close $fd;
2977 return %co;
2980 sub parse_commits {
2981 my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
2982 my @cos;
2984 $maxcount ||= 1;
2985 $skip ||= 0;
2987 local $/ = "\0";
2989 open my $fd, "-|", git_cmd(), "rev-list",
2990 "--header",
2991 @args,
2992 ("--max-count=" . $maxcount),
2993 ("--skip=" . $skip),
2994 @extra_options,
2995 $commit_id,
2996 "--",
2997 ($filename ? ($filename) : ())
2998 or die_error(500, "Open git-rev-list failed");
2999 while (my $line = <$fd>) {
3000 my %co = parse_commit_text($line);
3001 push @cos, \%co;
3003 close $fd;
3005 return wantarray ? @cos : \@cos;
3008 # parse line of git-diff-tree "raw" output
3009 sub parse_difftree_raw_line {
3010 my $line = shift;
3011 my %res;
3013 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
3014 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
3015 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
3016 $res{'from_mode'} = $1;
3017 $res{'to_mode'} = $2;
3018 $res{'from_id'} = $3;
3019 $res{'to_id'} = $4;
3020 $res{'status'} = $5;
3021 $res{'similarity'} = $6;
3022 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
3023 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
3024 } else {
3025 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote($7);
3028 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
3029 # combined diff (for merge commit)
3030 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
3031 $res{'nparents'} = length($1);
3032 $res{'from_mode'} = [ split(' ', $2) ];
3033 $res{'to_mode'} = pop @{$res{'from_mode'}};
3034 $res{'from_id'} = [ split(' ', $3) ];
3035 $res{'to_id'} = pop @{$res{'from_id'}};
3036 $res{'status'} = [ split('', $4) ];
3037 $res{'to_file'} = unquote($5);
3039 # 'c512b523472485aef4fff9e57b229d9d243c967f'
3040 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
3041 $res{'commit'} = $1;
3044 return wantarray ? %res : \%res;
3047 # wrapper: return parsed line of git-diff-tree "raw" output
3048 # (the argument might be raw line, or parsed info)
3049 sub parsed_difftree_line {
3050 my $line_or_ref = shift;
3052 if (ref($line_or_ref) eq "HASH") {
3053 # pre-parsed (or generated by hand)
3054 return $line_or_ref;
3055 } else {
3056 return parse_difftree_raw_line($line_or_ref);
3060 # parse line of git-ls-tree output
3061 sub parse_ls_tree_line {
3062 my $line = shift;
3063 my %opts = @_;
3064 my %res;
3066 if ($opts{'-l'}) {
3067 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'
3068 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;
3070 $res{'mode'} = $1;
3071 $res{'type'} = $2;
3072 $res{'hash'} = $3;
3073 $res{'size'} = $4;
3074 if ($opts{'-z'}) {
3075 $res{'name'} = $5;
3076 } else {
3077 $res{'name'} = unquote($5);
3079 } else {
3080 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
3081 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
3083 $res{'mode'} = $1;
3084 $res{'type'} = $2;
3085 $res{'hash'} = $3;
3086 if ($opts{'-z'}) {
3087 $res{'name'} = $4;
3088 } else {
3089 $res{'name'} = unquote($4);
3093 return wantarray ? %res : \%res;
3096 # generates _two_ hashes, references to which are passed as 2 and 3 argument
3097 sub parse_from_to_diffinfo {
3098 my ($diffinfo, $from, $to, @parents) = @_;
3100 if ($diffinfo->{'nparents'}) {
3101 # combined diff
3102 $from->{'file'} = [];
3103 $from->{'href'} = [];
3104 fill_from_file_info($diffinfo, @parents)
3105 unless exists $diffinfo->{'from_file'};
3106 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
3107 $from->{'file'}[$i] =
3108 defined $diffinfo->{'from_file'}[$i] ?
3109 $diffinfo->{'from_file'}[$i] :
3110 $diffinfo->{'to_file'};
3111 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
3112 $from->{'href'}[$i] = href(action=>"blob",
3113 hash_base=>$parents[$i],
3114 hash=>$diffinfo->{'from_id'}[$i],
3115 file_name=>$from->{'file'}[$i]);
3116 } else {
3117 $from->{'href'}[$i] = undef;
3120 } else {
3121 # ordinary (not combined) diff
3122 $from->{'file'} = $diffinfo->{'from_file'};
3123 if ($diffinfo->{'status'} ne "A") { # not new (added) file
3124 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
3125 hash=>$diffinfo->{'from_id'},
3126 file_name=>$from->{'file'});
3127 } else {
3128 delete $from->{'href'};
3132 $to->{'file'} = $diffinfo->{'to_file'};
3133 if (!is_deleted($diffinfo)) { # file exists in result
3134 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
3135 hash=>$diffinfo->{'to_id'},
3136 file_name=>$to->{'file'});
3137 } else {
3138 delete $to->{'href'};
3142 ## ......................................................................
3143 ## parse to array of hashes functions
3145 sub git_get_heads_list {
3146 my $limit = shift;
3147 my @headslist;
3149 open my $fd, '-|', git_cmd(), 'for-each-ref',
3150 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
3151 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
3152 'refs/heads'
3153 or return;
3154 while (my $line = <$fd>) {
3155 my %ref_item;
3157 chomp $line;
3158 my ($refinfo, $committerinfo) = split(/\0/, $line);
3159 my ($hash, $name, $title) = split(' ', $refinfo, 3);
3160 my ($committer, $epoch, $tz) =
3161 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
3162 $ref_item{'fullname'} = $name;
3163 $name =~ s!^refs/heads/!!;
3165 $ref_item{'name'} = $name;
3166 $ref_item{'id'} = $hash;
3167 $ref_item{'title'} = $title || '(no commit message)';
3168 $ref_item{'epoch'} = $epoch;
3169 if ($epoch) {
3170 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
3171 } else {
3172 $ref_item{'age'} = "unknown";
3175 push @headslist, \%ref_item;
3177 close $fd;
3179 return wantarray ? @headslist : \@headslist;
3182 sub git_get_tags_list {
3183 my $limit = shift;
3184 my @tagslist;
3186 open my $fd, '-|', git_cmd(), 'for-each-ref',
3187 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
3188 '--format=%(objectname) %(objecttype) %(refname) '.
3189 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
3190 'refs/tags'
3191 or return;
3192 while (my $line = <$fd>) {
3193 my %ref_item;
3195 chomp $line;
3196 my ($refinfo, $creatorinfo) = split(/\0/, $line);
3197 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
3198 my ($creator, $epoch, $tz) =
3199 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
3200 $ref_item{'fullname'} = $name;
3201 $name =~ s!^refs/tags/!!;
3203 $ref_item{'type'} = $type;
3204 $ref_item{'id'} = $id;
3205 $ref_item{'name'} = $name;
3206 if ($type eq "tag") {
3207 $ref_item{'subject'} = $title;
3208 $ref_item{'reftype'} = $reftype;
3209 $ref_item{'refid'} = $refid;
3210 } else {
3211 $ref_item{'reftype'} = $type;
3212 $ref_item{'refid'} = $id;
3215 if ($type eq "tag" || $type eq "commit") {
3216 $ref_item{'epoch'} = $epoch;
3217 if ($epoch) {
3218 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
3219 } else {
3220 $ref_item{'age'} = "unknown";
3224 push @tagslist, \%ref_item;
3226 close $fd;
3228 return wantarray ? @tagslist : \@tagslist;
3231 ## ----------------------------------------------------------------------
3232 ## filesystem-related functions
3234 sub get_file_owner {
3235 my $path = shift;
3237 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
3238 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
3239 if (!defined $gcos) {
3240 return undef;
3242 my $owner = $gcos;
3243 $owner =~ s/[,;].*$//;
3244 return to_utf8($owner);
3247 # assume that file exists
3248 sub insert_file {
3249 my $filename = shift;
3251 open my $fd, '<', $filename;
3252 print map { to_utf8($_) } <$fd>;
3253 close $fd;
3256 ## ......................................................................
3257 ## mimetype related functions
3259 sub mimetype_guess_file {
3260 my $filename = shift;
3261 my $mimemap = shift;
3262 -r $mimemap or return undef;
3264 my %mimemap;
3265 open(my $mh, '<', $mimemap) or return undef;
3266 while (<$mh>) {
3267 next if m/^#/; # skip comments
3268 my ($mimetype, $exts) = split(/\t+/);
3269 if (defined $exts) {
3270 my @exts = split(/\s+/, $exts);
3271 foreach my $ext (@exts) {
3272 $mimemap{$ext} = $mimetype;
3276 close($mh);
3278 $filename =~ /\.([^.]*)$/;
3279 return $mimemap{$1};
3282 sub mimetype_guess {
3283 my $filename = shift;
3284 my $mime;
3285 $filename =~ /\./ or return undef;
3287 if ($mimetypes_file) {
3288 my $file = $mimetypes_file;
3289 if ($file !~ m!^/!) { # if it is relative path
3290 # it is relative to project
3291 $file = "$projectroot/$project/$file";
3293 $mime = mimetype_guess_file($filename, $file);
3295 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
3296 return $mime;
3299 sub blob_mimetype {
3300 my $fd = shift;
3301 my $filename = shift;
3303 if ($filename) {
3304 my $mime = mimetype_guess($filename);
3305 $mime and return $mime;
3308 # just in case
3309 return $default_blob_plain_mimetype unless $fd;
3311 if (-T $fd) {
3312 return 'text/plain';
3313 } elsif (! $filename) {
3314 return 'application/octet-stream';
3315 } elsif ($filename =~ m/\.png$/i) {
3316 return 'image/png';
3317 } elsif ($filename =~ m/\.gif$/i) {
3318 return 'image/gif';
3319 } elsif ($filename =~ m/\.jpe?g$/i) {
3320 return 'image/jpeg';
3321 } else {
3322 return 'application/octet-stream';
3326 sub blob_contenttype {
3327 my ($fd, $file_name, $type) = @_;
3329 $type ||= blob_mimetype($fd, $file_name);
3330 if ($type eq 'text/plain' && defined $default_text_plain_charset) {
3331 $type .= "; charset=$default_text_plain_charset";
3334 return $type;
3337 # guess file syntax for syntax highlighting; return undef if no highlighting
3338 # the name of syntax can (in the future) depend on syntax highlighter used
3339 sub guess_file_syntax {
3340 my ($highlight, $mimetype, $file_name) = @_;
3341 return undef unless ($highlight && defined $file_name);
3342 my $basename = basename($file_name, '.in');
3343 return $highlight_basename{$basename}
3344 if exists $highlight_basename{$basename};
3346 $basename =~ /\.([^.]*)$/;
3347 my $ext = $1 or return undef;
3348 return $highlight_ext{$ext}
3349 if exists $highlight_ext{$ext};
3351 return undef;
3354 # run highlighter and return FD of its output,
3355 # or return original FD if no highlighting
3356 sub run_highlighter {
3357 my ($fd, $highlight, $syntax) = @_;
3358 return $fd unless ($highlight && defined $syntax);
3360 close $fd
3361 or die_error(404, "Reading blob failed");
3362 open $fd, quote_command(git_cmd(), "cat-file", "blob", $hash)." | ".
3363 "highlight --xhtml --fragment --syntax $syntax |"
3364 or die_error(500, "Couldn't open file or run syntax highlighter");
3365 return $fd;
3368 ## ======================================================================
3369 ## functions printing HTML: header, footer, error page
3371 sub get_page_title {
3372 my $title = to_utf8($site_name);
3374 return $title unless (defined $project);
3375 $title .= " - " . to_utf8($project);
3377 return $title unless (defined $action);
3378 $title .= "/$action"; # $action is US-ASCII (7bit ASCII)
3380 return $title unless (defined $file_name);
3381 $title .= " - " . esc_path($file_name);
3382 if ($action eq "tree" && $file_name !~ m|/$|) {
3383 $title .= "/";
3386 return $title;
3389 sub git_header_html {
3390 my $status = shift || "200 OK";
3391 my $expires = shift;
3392 my %opts = @_;
3394 my $title = get_page_title();
3395 my $content_type;
3396 # require explicit support from the UA if we are to send the page as
3397 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
3398 # we have to do this because MSIE sometimes globs '*/*', pretending to
3399 # support xhtml+xml but choking when it gets what it asked for.
3400 if (defined $cgi->http('HTTP_ACCEPT') &&
3401 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
3402 $cgi->Accept('application/xhtml+xml') != 0) {
3403 $content_type = 'application/xhtml+xml';
3404 } else {
3405 $content_type = 'text/html';
3407 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
3408 -status=> $status, -expires => $expires)
3409 unless ($opts{'-no_http_header'});
3410 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
3411 print <<EOF;
3412 <?xml version="1.0" encoding="utf-8"?>
3413 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3414 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
3415 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
3416 <!-- git core binaries version $git_version -->
3417 <head>
3418 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
3419 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
3420 <meta name="robots" content="index, nofollow"/>
3421 <title>$title</title>
3423 # the stylesheet, favicon etc urls won't work correctly with path_info
3424 # unless we set the appropriate base URL
3425 if ($ENV{'PATH_INFO'}) {
3426 print "<base href=\"".esc_url($base_url)."\" />\n";
3428 # print out each stylesheet that exist, providing backwards capability
3429 # for those people who defined $stylesheet in a config file
3430 if (defined $stylesheet) {
3431 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
3432 } else {
3433 foreach my $stylesheet (@stylesheets) {
3434 next unless $stylesheet;
3435 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
3438 if (defined $project) {
3439 my %href_params = get_feed_info();
3440 if (!exists $href_params{'-title'}) {
3441 $href_params{'-title'} = 'log';
3444 foreach my $format qw(RSS Atom) {
3445 my $type = lc($format);
3446 my %link_attr = (
3447 '-rel' => 'alternate',
3448 '-title' => "$project - $href_params{'-title'} - $format feed",
3449 '-type' => "application/$type+xml"
3452 $href_params{'action'} = $type;
3453 $link_attr{'-href'} = href(%href_params);
3454 print "<link ".
3455 "rel=\"$link_attr{'-rel'}\" ".
3456 "title=\"$link_attr{'-title'}\" ".
3457 "href=\"$link_attr{'-href'}\" ".
3458 "type=\"$link_attr{'-type'}\" ".
3459 "/>\n";
3461 $href_params{'extra_options'} = '--no-merges';
3462 $link_attr{'-href'} = href(%href_params);
3463 $link_attr{'-title'} .= ' (no merges)';
3464 print "<link ".
3465 "rel=\"$link_attr{'-rel'}\" ".
3466 "title=\"$link_attr{'-title'}\" ".
3467 "href=\"$link_attr{'-href'}\" ".
3468 "type=\"$link_attr{'-type'}\" ".
3469 "/>\n";
3472 } else {
3473 printf('<link rel="alternate" title="%s projects list" '.
3474 'href="%s" type="text/plain; charset=utf-8" />'."\n",
3475 $site_name, href(project=>undef, action=>"project_index"));
3476 printf('<link rel="alternate" title="%s projects feeds" '.
3477 'href="%s" type="text/x-opml" />'."\n",
3478 $site_name, href(project=>undef, action=>"opml"));
3480 if (defined $favicon) {
3481 print qq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);
3484 print "</head>\n" .
3485 "<body>\n";
3487 if (defined $site_header && -f $site_header) {
3488 insert_file($site_header);
3491 print "<div class=\"page_header\">\n" .
3492 $cgi->a({-href => esc_url($logo_url),
3493 -title => $logo_label},
3494 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
3495 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
3496 if (defined $project) {
3497 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
3498 if (defined $action) {
3499 print " / $action";
3501 print "\n";
3503 print "</div>\n";
3505 my $have_search = gitweb_check_feature('search');
3506 if (defined $project && $have_search) {
3507 if (!defined $searchtext) {
3508 $searchtext = "";
3510 my $search_hash;
3511 if (defined $hash_base) {
3512 $search_hash = $hash_base;
3513 } elsif (defined $hash) {
3514 $search_hash = $hash;
3515 } else {
3516 $search_hash = "HEAD";
3518 my $action = $my_uri;
3519 my $use_pathinfo = gitweb_check_feature('pathinfo');
3520 if ($use_pathinfo) {
3521 $action .= "/".esc_url($project);
3523 print $cgi->startform(-method => "get", -action => $action) .
3524 "<div class=\"search\">\n" .
3525 (!$use_pathinfo &&
3526 $cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) . "\n") .
3527 $cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) . "\n" .
3528 $cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) . "\n" .
3529 $cgi->popup_menu(-name => 'st', -default => 'commit',
3530 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
3531 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
3532 " search:\n",
3533 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
3534 "<span title=\"Extended regular expression\">" .
3535 $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
3536 -checked => $search_use_regexp) .
3537 "</span>" .
3538 "</div>" .
3539 $cgi->end_form() . "\n";
3543 sub git_footer_html {
3544 my $feed_class = 'rss_logo';
3546 print "<div class=\"page_footer\">\n";
3547 if (defined $project) {
3548 my $descr = git_get_project_description($project);
3549 if (defined $descr) {
3550 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
3553 my %href_params = get_feed_info();
3554 if (!%href_params) {
3555 $feed_class .= ' generic';
3557 $href_params{'-title'} ||= 'log';
3559 foreach my $format qw(RSS Atom) {
3560 $href_params{'action'} = lc($format);
3561 print $cgi->a({-href => href(%href_params),
3562 -title => "$href_params{'-title'} $format feed",
3563 -class => $feed_class}, $format)."\n";
3566 } else {
3567 print $cgi->a({-href => href(project=>undef, action=>"opml"),
3568 -class => $feed_class}, "OPML") . " ";
3569 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
3570 -class => $feed_class}, "TXT") . "\n";
3572 print "</div>\n"; # class="page_footer"
3574 if (defined $t0 && gitweb_check_feature('timed')) {
3575 print "<div id=\"generating_info\">\n";
3576 print 'This page took '.
3577 '<span id="generating_time" class="time_span">'.
3578 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).
3579 ' seconds </span>'.
3580 ' and '.
3581 '<span id="generating_cmd">'.
3582 $number_of_git_cmds.
3583 '</span> git commands '.
3584 " to generate.\n";
3585 print "</div>\n"; # class="page_footer"
3588 if (defined $site_footer && -f $site_footer) {
3589 insert_file($site_footer);
3592 print qq!<script type="text/javascript" src="$javascript"></script>\n!;
3593 if (defined $action &&
3594 $action eq 'blame_incremental') {
3595 print qq!<script type="text/javascript">\n!.
3596 qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.
3597 qq! "!. href() .qq!");\n!.
3598 qq!</script>\n!;
3599 } elsif (gitweb_check_feature('javascript-actions')) {
3600 print qq!<script type="text/javascript">\n!.
3601 qq!window.onload = fixLinks;\n!.
3602 qq!</script>\n!;
3605 print "</body>\n" .
3606 "</html>";
3609 # die_error(<http_status_code>, <error_message>[, <detailed_html_description>])
3610 # Example: die_error(404, 'Hash not found')
3611 # By convention, use the following status codes (as defined in RFC 2616):
3612 # 400: Invalid or missing CGI parameters, or
3613 # requested object exists but has wrong type.
3614 # 403: Requested feature (like "pickaxe" or "snapshot") not enabled on
3615 # this server or project.
3616 # 404: Requested object/revision/project doesn't exist.
3617 # 500: The server isn't configured properly, or
3618 # an internal error occurred (e.g. failed assertions caused by bugs), or
3619 # an unknown error occurred (e.g. the git binary died unexpectedly).
3620 # 503: The server is currently unavailable (because it is overloaded,
3621 # or down for maintenance). Generally, this is a temporary state.
3622 sub die_error {
3623 my $status = shift || 500;
3624 my $error = esc_html(shift) || "Internal Server Error";
3625 my $extra = shift;
3626 my %opts = @_;
3628 my %http_responses = (
3629 400 => '400 Bad Request',
3630 403 => '403 Forbidden',
3631 404 => '404 Not Found',
3632 500 => '500 Internal Server Error',
3633 503 => '503 Service Unavailable',
3635 git_header_html($http_responses{$status}, undef, %opts);
3636 print <<EOF;
3637 <div class="page_body">
3638 <br /><br />
3639 $status - $error
3640 <br />
3642 if (defined $extra) {
3643 print "<hr />\n" .
3644 "$extra\n";
3646 print "</div>\n";
3648 git_footer_html();
3649 goto DONE_GITWEB
3650 unless ($opts{'-error_handler'});
3653 ## ----------------------------------------------------------------------
3654 ## functions printing or outputting HTML: navigation
3656 sub git_print_page_nav {
3657 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
3658 $extra = '' if !defined $extra; # pager or formats
3660 my @navs = qw(summary shortlog log commit commitdiff tree);
3661 if ($suppress) {
3662 @navs = grep { $_ ne $suppress } @navs;
3665 my %arg = map { $_ => {action=>$_} } @navs;
3666 if (defined $head) {
3667 for (qw(commit commitdiff)) {
3668 $arg{$_}{'hash'} = $head;
3670 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
3671 for (qw(shortlog log)) {
3672 $arg{$_}{'hash'} = $head;
3677 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
3678 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
3680 my @actions = gitweb_get_feature('actions');
3681 my %repl = (
3682 '%' => '%',
3683 'n' => $project, # project name
3684 'f' => $git_dir, # project path within filesystem
3685 'h' => $treehead || '', # current hash ('h' parameter)
3686 'b' => $treebase || '', # hash base ('hb' parameter)
3688 while (@actions) {
3689 my ($label, $link, $pos) = splice(@actions,0,3);
3690 # insert
3691 @navs = map { $_ eq $pos ? ($_, $label) : $_ } @navs;
3692 # munch munch
3693 $link =~ s/%([%nfhb])/$repl{$1}/g;
3694 $arg{$label}{'_href'} = $link;
3697 print "<div class=\"page_nav\">\n" .
3698 (join " | ",
3699 map { $_ eq $current ?
3700 $_ : $cgi->a({-href => ($arg{$_}{_href} ? $arg{$_}{_href} : href(%{$arg{$_}}))}, "$_")
3701 } @navs);
3702 print "<br/>\n$extra<br/>\n" .
3703 "</div>\n";
3706 sub format_paging_nav {
3707 my ($action, $page, $has_next_link) = @_;
3708 my $paging_nav;
3711 if ($page > 0) {
3712 $paging_nav .=
3713 $cgi->a({-href => href(-replay=>1, page=>undef)}, "first") .
3714 " &sdot; " .
3715 $cgi->a({-href => href(-replay=>1, page=>$page-1),
3716 -accesskey => "p", -title => "Alt-p"}, "prev");
3717 } else {
3718 $paging_nav .= "first &sdot; prev";
3721 if ($has_next_link) {
3722 $paging_nav .= " &sdot; " .
3723 $cgi->a({-href => href(-replay=>1, page=>$page+1),
3724 -accesskey => "n", -title => "Alt-n"}, "next");
3725 } else {
3726 $paging_nav .= " &sdot; next";
3729 return $paging_nav;
3732 ## ......................................................................
3733 ## functions printing or outputting HTML: div
3735 sub git_print_header_div {
3736 my ($action, $title, $hash, $hash_base) = @_;
3737 my %args = ();
3739 $args{'action'} = $action;
3740 $args{'hash'} = $hash if $hash;
3741 $args{'hash_base'} = $hash_base if $hash_base;
3743 print "<div class=\"header\">\n" .
3744 $cgi->a({-href => href(%args), -class => "title"},
3745 $title ? $title : $action) .
3746 "\n</div>\n";
3749 sub print_local_time {
3750 print format_local_time(@_);
3753 sub format_local_time {
3754 my $localtime = '';
3755 my %date = @_;
3756 if ($date{'hour_local'} < 6) {
3757 $localtime .= sprintf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
3758 $date{'hour_local'}, $date{'minute_local'}, $date{'tz_local'});
3759 } else {
3760 $localtime .= sprintf(" (%02d:%02d %s)",
3761 $date{'hour_local'}, $date{'minute_local'}, $date{'tz_local'});
3764 return $localtime;
3767 # Outputs the author name and date in long form
3768 sub git_print_authorship {
3769 my $co = shift;
3770 my %opts = @_;
3771 my $tag = $opts{-tag} || 'div';
3772 my $author = $co->{'author_name'};
3774 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
3775 print "<$tag class=\"author_date\">" .
3776 format_search_author($author, "author", esc_html($author)) .
3777 " [$ad{'rfc2822'}";
3778 print_local_time(%ad) if ($opts{-localtime});
3779 print "]" . git_get_avatar($co->{'author_email'}, -pad_before => 1)
3780 . "</$tag>\n";
3783 # Outputs table rows containing the full author or committer information,
3784 # in the format expected for 'commit' view (& similar).
3785 # Parameters are a commit hash reference, followed by the list of people
3786 # to output information for. If the list is empty it defaults to both
3787 # author and committer.
3788 sub git_print_authorship_rows {
3789 my $co = shift;
3790 # too bad we can't use @people = @_ || ('author', 'committer')
3791 my @people = @_;
3792 @people = ('author', 'committer') unless @people;
3793 foreach my $who (@people) {
3794 my %wd = parse_date($co->{"${who}_epoch"}, $co->{"${who}_tz"});
3795 print "<tr><td>$who</td><td>" .
3796 format_search_author($co->{"${who}_name"}, $who,
3797 esc_html($co->{"${who}_name"})) . " " .
3798 format_search_author($co->{"${who}_email"}, $who,
3799 esc_html("<" . $co->{"${who}_email"} . ">")) .
3800 "</td><td rowspan=\"2\">" .
3801 git_get_avatar($co->{"${who}_email"}, -size => 'double') .
3802 "</td></tr>\n" .
3803 "<tr>" .
3804 "<td></td><td> $wd{'rfc2822'}";
3805 print_local_time(%wd);
3806 print "</td>" .
3807 "</tr>\n";
3811 sub git_print_page_path {
3812 my $name = shift;
3813 my $type = shift;
3814 my $hb = shift;
3817 print "<div class=\"page_path\">";
3818 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
3819 -title => 'tree root'}, to_utf8("[$project]"));
3820 print " / ";
3821 if (defined $name) {
3822 my @dirname = split '/', $name;
3823 my $basename = pop @dirname;
3824 my $fullname = '';
3826 foreach my $dir (@dirname) {
3827 $fullname .= ($fullname ? '/' : '') . $dir;
3828 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
3829 hash_base=>$hb),
3830 -title => $fullname}, esc_path($dir));
3831 print " / ";
3833 if (defined $type && $type eq 'blob') {
3834 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
3835 hash_base=>$hb),
3836 -title => $name}, esc_path($basename));
3837 print '&nbsp;&nbsp;&nbsp;&nbsp;
3838 <a id="lineNoToggle" href="#" onclick="toggleLineNumbers();"></a>
3839 <script>
3840 function toggleLineNumbers() {
3841 e = document.getElementById("lineNoStyle");
3842 e2 = document.getElementById("lineNoToggle");
3843 if (e2.innerHTML == "[Hide line numbers]") {
3844 e.innerHTML = ".linenr { display:none; }";
3845 e2.innerHTML = "[Show line numbers]";
3847 else {
3848 e.innerHTML = "";
3849 e2.innerHTML = "[Hide line numbers]";
3852 var style = document.createElement("style");
3853 style.setAttribute("id", "lineNoStyle");
3854 document.getElementsByTagName("head")[0].appendChild(style);
3855 toggleLineNumbers();
3856 </script>
3858 } elsif (defined $type && $type eq 'tree') {
3859 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
3860 hash_base=>$hb),
3861 -title => $name}, esc_path($basename));
3862 print " / ";
3863 } else {
3864 print esc_path($basename);
3867 print "<br/></div>\n";
3870 sub git_print_log {
3871 my $log = shift;
3872 my %opts = @_;
3874 if ($opts{'-remove_title'}) {
3875 # remove title, i.e. first line of log
3876 shift @$log;
3878 # remove leading empty lines
3879 while (defined $log->[0] && $log->[0] eq "") {
3880 shift @$log;
3883 # print log
3884 my $signoff = 0;
3885 my $empty = 0;
3886 foreach my $line (@$log) {
3887 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
3888 $signoff = 1;
3889 $empty = 0;
3890 if (! $opts{'-remove_signoff'}) {
3891 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
3892 next;
3893 } else {
3894 # remove signoff lines
3895 next;
3897 } else {
3898 $signoff = 0;
3901 # print only one empty line
3902 # do not print empty line after signoff
3903 if ($line eq "") {
3904 next if ($empty || $signoff);
3905 $empty = 1;
3906 } else {
3907 $empty = 0;
3910 print format_log_line_html($line) . "<br/>\n";
3913 if ($opts{'-final_empty_line'}) {
3914 # end with single empty line
3915 print "<br/>\n" unless $empty;
3919 # return link target (what link points to)
3920 sub git_get_link_target {
3921 my $hash = shift;
3922 my $link_target;
3924 # read link
3925 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
3926 or return;
3928 local $/ = undef;
3929 $link_target = <$fd>;
3931 close $fd
3932 or return;
3934 return $link_target;
3937 # given link target, and the directory (basedir) the link is in,
3938 # return target of link relative to top directory (top tree);
3939 # return undef if it is not possible (including absolute links).
3940 sub normalize_link_target {
3941 my ($link_target, $basedir) = @_;
3943 # absolute symlinks (beginning with '/') cannot be normalized
3944 return if (substr($link_target, 0, 1) eq '/');
3946 # normalize link target to path from top (root) tree (dir)
3947 my $path;
3948 if ($basedir) {
3949 $path = $basedir . '/' . $link_target;
3950 } else {
3951 # we are in top (root) tree (dir)
3952 $path = $link_target;
3955 # remove //, /./, and /../
3956 my @path_parts;
3957 foreach my $part (split('/', $path)) {
3958 # discard '.' and ''
3959 next if (!$part || $part eq '.');
3960 # handle '..'
3961 if ($part eq '..') {
3962 if (@path_parts) {
3963 pop @path_parts;
3964 } else {
3965 # link leads outside repository (outside top dir)
3966 return;
3968 } else {
3969 push @path_parts, $part;
3972 $path = join('/', @path_parts);
3974 return $path;
3977 # print tree entry (row of git_tree), but without encompassing <tr> element
3978 sub git_print_tree_entry {
3979 my ($t, $basedir, $hash_base, $have_blame) = @_;
3981 my %base_key = ();
3982 $base_key{'hash_base'} = $hash_base if defined $hash_base;
3984 # The format of a table row is: mode list link. Where mode is
3985 # the mode of the entry, list is the name of the entry, an href,
3986 # and link is the action links of the entry.
3988 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
3989 if (exists $t->{'size'}) {
3990 print "<td class=\"size\">$t->{'size'}</td>\n";
3992 if ($t->{'type'} eq "blob") {
3993 print "<td class=\"list\">" .
3994 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
3995 file_name=>"$basedir$t->{'name'}", %base_key),
3996 -class => "list"}, esc_path($t->{'name'}));
3997 if (S_ISLNK(oct $t->{'mode'})) {
3998 my $link_target = git_get_link_target($t->{'hash'});
3999 if ($link_target) {
4000 my $norm_target = normalize_link_target($link_target, $basedir);
4001 if (defined $norm_target) {
4002 print " -> " .
4003 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
4004 file_name=>$norm_target),
4005 -title => $norm_target}, esc_path($link_target));
4006 } else {
4007 print " -> " . esc_path($link_target);
4011 print "</td>\n";
4012 print "<td class=\"link\">";
4013 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
4014 file_name=>"$basedir$t->{'name'}", %base_key)},
4015 "blob");
4016 if ($have_blame) {
4017 print " | " .
4018 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
4019 file_name=>"$basedir$t->{'name'}", %base_key)},
4020 "blame");
4022 if (defined $hash_base) {
4023 print " | " .
4024 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
4025 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
4026 "history");
4028 print " | " .
4029 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
4030 file_name=>"$basedir$t->{'name'}")},
4031 "raw");
4032 print "</td>\n";
4034 } elsif ($t->{'type'} eq "tree") {
4035 print "<td class=\"list\">";
4036 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
4037 file_name=>"$basedir$t->{'name'}",
4038 %base_key)},
4039 esc_path($t->{'name'}));
4040 print "</td>\n";
4041 print "<td class=\"link\">";
4042 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
4043 file_name=>"$basedir$t->{'name'}",
4044 %base_key)},
4045 "tree");
4046 if (defined $hash_base) {
4047 print " | " .
4048 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
4049 file_name=>"$basedir$t->{'name'}")},
4050 "history");
4052 print "</td>\n";
4053 } else {
4054 # unknown object: we can only present history for it
4055 # (this includes 'commit' object, i.e. submodule support)
4056 print "<td class=\"list\">" .
4057 esc_path($t->{'name'}) .
4058 "</td>\n";
4059 print "<td class=\"link\">";
4060 if (defined $hash_base) {
4061 print $cgi->a({-href => href(action=>"history",
4062 hash_base=>$hash_base,
4063 file_name=>"$basedir$t->{'name'}")},
4064 "history");
4066 print "</td>\n";
4070 ## ......................................................................
4071 ## functions printing large fragments of HTML
4073 # get pre-image filenames for merge (combined) diff
4074 sub fill_from_file_info {
4075 my ($diff, @parents) = @_;
4077 $diff->{'from_file'} = [ ];
4078 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
4079 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
4080 if ($diff->{'status'}[$i] eq 'R' ||
4081 $diff->{'status'}[$i] eq 'C') {
4082 $diff->{'from_file'}[$i] =
4083 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
4087 return $diff;
4090 # is current raw difftree line of file deletion
4091 sub is_deleted {
4092 my $diffinfo = shift;
4094 return $diffinfo->{'to_id'} eq ('0' x 40);
4097 # does patch correspond to [previous] difftree raw line
4098 # $diffinfo - hashref of parsed raw diff format
4099 # $patchinfo - hashref of parsed patch diff format
4100 # (the same keys as in $diffinfo)
4101 sub is_patch_split {
4102 my ($diffinfo, $patchinfo) = @_;
4104 return defined $diffinfo && defined $patchinfo
4105 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
4109 sub git_difftree_body {
4110 my ($difftree, $hash, @parents) = @_;
4111 my ($parent) = $parents[0];
4112 my $have_blame = gitweb_check_feature('blame');
4113 print "<div class=\"list_head\">\n";
4114 if ($#{$difftree} > 10) {
4115 print(($#{$difftree} + 1) . " files changed:\n");
4117 print "</div>\n";
4119 print "<table class=\"" .
4120 (@parents > 1 ? "combined " : "") .
4121 "diff_tree\">\n";
4123 # header only for combined diff in 'commitdiff' view
4124 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
4125 if ($has_header) {
4126 # table header
4127 print "<thead><tr>\n" .
4128 "<th></th><th></th>\n"; # filename, patchN link
4129 for (my $i = 0; $i < @parents; $i++) {
4130 my $par = $parents[$i];
4131 print "<th>" .
4132 $cgi->a({-href => href(action=>"commitdiff",
4133 hash=>$hash, hash_parent=>$par),
4134 -title => 'commitdiff to parent number ' .
4135 ($i+1) . ': ' . substr($par,0,7)},
4136 $i+1) .
4137 "&nbsp;</th>\n";
4139 print "</tr></thead>\n<tbody>\n";
4142 my $alternate = 1;
4143 my $patchno = 0;
4144 foreach my $line (@{$difftree}) {
4145 my $diff = parsed_difftree_line($line);
4147 if ($alternate) {
4148 print "<tr class=\"dark\">\n";
4149 } else {
4150 print "<tr class=\"light\">\n";
4152 $alternate ^= 1;
4154 if (exists $diff->{'nparents'}) { # combined diff
4156 fill_from_file_info($diff, @parents)
4157 unless exists $diff->{'from_file'};
4159 if (!is_deleted($diff)) {
4160 # file exists in the result (child) commit
4161 print "<td>" .
4162 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4163 file_name=>$diff->{'to_file'},
4164 hash_base=>$hash),
4165 -class => "list"}, esc_path($diff->{'to_file'})) .
4166 "</td>\n";
4167 } else {
4168 print "<td>" .
4169 esc_path($diff->{'to_file'}) .
4170 "</td>\n";
4173 if ($action eq 'commitdiff') {
4174 # link to patch
4175 $patchno++;
4176 print "<td class=\"link\">" .
4177 $cgi->a({-href => "#patch$patchno"}, "patch") .
4178 " | " .
4179 "</td>\n";
4182 my $has_history = 0;
4183 my $not_deleted = 0;
4184 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
4185 my $hash_parent = $parents[$i];
4186 my $from_hash = $diff->{'from_id'}[$i];
4187 my $from_path = $diff->{'from_file'}[$i];
4188 my $status = $diff->{'status'}[$i];
4190 $has_history ||= ($status ne 'A');
4191 $not_deleted ||= ($status ne 'D');
4193 if ($status eq 'A') {
4194 print "<td class=\"link\" align=\"right\"> | </td>\n";
4195 } elsif ($status eq 'D') {
4196 print "<td class=\"link\">" .
4197 $cgi->a({-href => href(action=>"blob",
4198 hash_base=>$hash,
4199 hash=>$from_hash,
4200 file_name=>$from_path)},
4201 "blob" . ($i+1)) .
4202 " | </td>\n";
4203 } else {
4204 if ($diff->{'to_id'} eq $from_hash) {
4205 print "<td class=\"link nochange\">";
4206 } else {
4207 print "<td class=\"link\">";
4209 print $cgi->a({-href => href(action=>"blobdiff",
4210 hash=>$diff->{'to_id'},
4211 hash_parent=>$from_hash,
4212 hash_base=>$hash,
4213 hash_parent_base=>$hash_parent,
4214 file_name=>$diff->{'to_file'},
4215 file_parent=>$from_path)},
4216 "diff" . ($i+1)) .
4217 " | </td>\n";
4221 print "<td class=\"link\">";
4222 if ($not_deleted) {
4223 print $cgi->a({-href => href(action=>"blob",
4224 hash=>$diff->{'to_id'},
4225 file_name=>$diff->{'to_file'},
4226 hash_base=>$hash)},
4227 "blob");
4228 print " | " if ($has_history);
4230 if ($has_history) {
4231 print $cgi->a({-href => href(action=>"history",
4232 file_name=>$diff->{'to_file'},
4233 hash_base=>$hash)},
4234 "history");
4236 print "</td>\n";
4238 print "</tr>\n";
4239 next; # instead of 'else' clause, to avoid extra indent
4241 # else ordinary diff
4243 my ($to_mode_oct, $to_mode_str, $to_file_type);
4244 my ($from_mode_oct, $from_mode_str, $from_file_type);
4245 if ($diff->{'to_mode'} ne ('0' x 6)) {
4246 $to_mode_oct = oct $diff->{'to_mode'};
4247 if (S_ISREG($to_mode_oct)) { # only for regular file
4248 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
4250 $to_file_type = file_type($diff->{'to_mode'});
4252 if ($diff->{'from_mode'} ne ('0' x 6)) {
4253 $from_mode_oct = oct $diff->{'from_mode'};
4254 if (S_ISREG($to_mode_oct)) { # only for regular file
4255 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
4257 $from_file_type = file_type($diff->{'from_mode'});
4260 if ($diff->{'status'} eq "A") { # created
4261 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
4262 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
4263 $mode_chng .= "]</span>";
4264 print "<td>";
4265 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4266 hash_base=>$hash, file_name=>$diff->{'file'}),
4267 -class => "list"}, esc_path($diff->{'file'}));
4268 print "</td>\n";
4269 print "<td>$mode_chng</td>\n";
4270 print "<td class=\"link\">";
4271 if ($action eq 'commitdiff') {
4272 # link to patch
4273 $patchno++;
4274 print $cgi->a({-href => "#patch$patchno"}, "patch");
4275 print " | ";
4277 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4278 hash_base=>$hash, file_name=>$diff->{'file'})},
4279 "blob");
4280 print "</td>\n";
4282 } elsif ($diff->{'status'} eq "D") { # deleted
4283 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
4284 print "<td>";
4285 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
4286 hash_base=>$parent, file_name=>$diff->{'file'}),
4287 -class => "list"}, esc_path($diff->{'file'}));
4288 print "</td>\n";
4289 print "<td>$mode_chng</td>\n";
4290 print "<td class=\"link\">";
4291 if ($action eq 'commitdiff') {
4292 # link to patch
4293 $patchno++;
4294 print $cgi->a({-href => "#patch$patchno"}, "patch");
4295 print " | ";
4297 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
4298 hash_base=>$parent, file_name=>$diff->{'file'})},
4299 "blob") . " | ";
4300 if ($have_blame) {
4301 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
4302 file_name=>$diff->{'file'})},
4303 "blame") . " | ";
4305 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
4306 file_name=>$diff->{'file'})},
4307 "history");
4308 print "</td>\n";
4310 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
4311 my $mode_chnge = "";
4312 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
4313 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
4314 if ($from_file_type ne $to_file_type) {
4315 $mode_chnge .= " from $from_file_type to $to_file_type";
4317 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
4318 if ($from_mode_str && $to_mode_str) {
4319 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
4320 } elsif ($to_mode_str) {
4321 $mode_chnge .= " mode: $to_mode_str";
4324 $mode_chnge .= "]</span>\n";
4326 print "<td>";
4327 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4328 hash_base=>$hash, file_name=>$diff->{'file'}),
4329 -class => "list"}, esc_path($diff->{'file'}));
4330 print "</td>\n";
4331 print "<td>$mode_chnge</td>\n";
4332 print "<td class=\"link\">";
4333 if ($action eq 'commitdiff') {
4334 # link to patch
4335 $patchno++;
4336 print $cgi->a({-href => "#patch$patchno"}, "patch") .
4337 " | ";
4338 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
4339 # "commit" view and modified file (not onlu mode changed)
4340 print $cgi->a({-href => href(action=>"blobdiff",
4341 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
4342 hash_base=>$hash, hash_parent_base=>$parent,
4343 file_name=>$diff->{'file'})},
4344 "diff") .
4345 " | ";
4347 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4348 hash_base=>$hash, file_name=>$diff->{'file'})},
4349 "blob") . " | ";
4350 if ($have_blame) {
4351 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
4352 file_name=>$diff->{'file'})},
4353 "blame") . " | ";
4355 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
4356 file_name=>$diff->{'file'})},
4357 "history");
4358 print "</td>\n";
4360 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
4361 my %status_name = ('R' => 'moved', 'C' => 'copied');
4362 my $nstatus = $status_name{$diff->{'status'}};
4363 my $mode_chng = "";
4364 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
4365 # mode also for directories, so we cannot use $to_mode_str
4366 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
4368 print "<td>" .
4369 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
4370 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
4371 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
4372 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
4373 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
4374 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
4375 -class => "list"}, esc_path($diff->{'from_file'})) .
4376 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
4377 "<td class=\"link\">";
4378 if ($action eq 'commitdiff') {
4379 # link to patch
4380 $patchno++;
4381 print $cgi->a({-href => "#patch$patchno"}, "patch") .
4382 " | ";
4383 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
4384 # "commit" view and modified file (not only pure rename or copy)
4385 print $cgi->a({-href => href(action=>"blobdiff",
4386 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
4387 hash_base=>$hash, hash_parent_base=>$parent,
4388 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
4389 "diff") .
4390 " | ";
4392 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4393 hash_base=>$parent, file_name=>$diff->{'to_file'})},
4394 "blob") . " | ";
4395 if ($have_blame) {
4396 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
4397 file_name=>$diff->{'to_file'})},
4398 "blame") . " | ";
4400 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
4401 file_name=>$diff->{'to_file'})},
4402 "history");
4403 print "</td>\n";
4405 } # we should not encounter Unmerged (U) or Unknown (X) status
4406 print "</tr>\n";
4408 print "</tbody>" if $has_header;
4409 print "</table>\n";
4412 sub git_patchset_body {
4413 my ($fd, $difftree, $hash, @hash_parents) = @_;
4414 my ($hash_parent) = $hash_parents[0];
4416 my $is_combined = (@hash_parents > 1);
4417 my $patch_idx = 0;
4418 my $patch_number = 0;
4419 my $patch_line;
4420 my $diffinfo;
4421 my $to_name;
4422 my (%from, %to);
4424 print "<div class=\"patchset\">\n";
4426 # skip to first patch
4427 while ($patch_line = <$fd>) {
4428 chomp $patch_line;
4430 last if ($patch_line =~ m/^diff /);
4433 PATCH:
4434 while ($patch_line) {
4436 # parse "git diff" header line
4437 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
4438 # $1 is from_name, which we do not use
4439 $to_name = unquote($2);
4440 $to_name =~ s!^b/!!;
4441 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
4442 # $1 is 'cc' or 'combined', which we do not use
4443 $to_name = unquote($2);
4444 } else {
4445 $to_name = undef;
4448 # check if current patch belong to current raw line
4449 # and parse raw git-diff line if needed
4450 if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
4451 # this is continuation of a split patch
4452 print "<div class=\"patch cont\">\n";
4453 } else {
4454 # advance raw git-diff output if needed
4455 $patch_idx++ if defined $diffinfo;
4457 # read and prepare patch information
4458 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
4460 # compact combined diff output can have some patches skipped
4461 # find which patch (using pathname of result) we are at now;
4462 if ($is_combined) {
4463 while ($to_name ne $diffinfo->{'to_file'}) {
4464 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
4465 format_diff_cc_simplified($diffinfo, @hash_parents) .
4466 "</div>\n"; # class="patch"
4468 $patch_idx++;
4469 $patch_number++;
4471 last if $patch_idx > $#$difftree;
4472 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
4476 # modifies %from, %to hashes
4477 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
4479 # this is first patch for raw difftree line with $patch_idx index
4480 # we index @$difftree array from 0, but number patches from 1
4481 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
4484 # git diff header
4485 #assert($patch_line =~ m/^diff /) if DEBUG;
4486 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
4487 $patch_number++;
4488 # print "git diff" header
4489 print format_git_diff_header_line($patch_line, $diffinfo,
4490 \%from, \%to);
4492 # print extended diff header
4493 print "<div class=\"diff extended_header\">\n";
4494 EXTENDED_HEADER:
4495 while ($patch_line = <$fd>) {
4496 chomp $patch_line;
4498 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
4500 print format_extended_diff_header_line($patch_line, $diffinfo,
4501 \%from, \%to);
4503 print "</div>\n"; # class="diff extended_header"
4505 # from-file/to-file diff header
4506 if (! $patch_line) {
4507 print "</div>\n"; # class="patch"
4508 last PATCH;
4510 next PATCH if ($patch_line =~ m/^diff /);
4511 #assert($patch_line =~ m/^---/) if DEBUG;
4513 my $last_patch_line = $patch_line;
4514 $patch_line = <$fd>;
4515 chomp $patch_line;
4516 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
4518 print format_diff_from_to_header($last_patch_line, $patch_line,
4519 $diffinfo, \%from, \%to,
4520 @hash_parents);
4522 # the patch itself
4523 LINE:
4524 while ($patch_line = <$fd>) {
4525 chomp $patch_line;
4527 next PATCH if ($patch_line =~ m/^diff /);
4529 print format_diff_line($patch_line, \%from, \%to);
4532 } continue {
4533 print "</div>\n"; # class="patch"
4536 # for compact combined (--cc) format, with chunk and patch simplification
4537 # the patchset might be empty, but there might be unprocessed raw lines
4538 for (++$patch_idx if $patch_number > 0;
4539 $patch_idx < @$difftree;
4540 ++$patch_idx) {
4541 # read and prepare patch information
4542 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
4544 # generate anchor for "patch" links in difftree / whatchanged part
4545 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
4546 format_diff_cc_simplified($diffinfo, @hash_parents) .
4547 "</div>\n"; # class="patch"
4549 $patch_number++;
4552 if ($patch_number == 0) {
4553 if (@hash_parents > 1) {
4554 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
4555 } else {
4556 print "<div class=\"diff nodifferences\">No differences found</div>\n";
4560 print "</div>\n"; # class="patchset"
4563 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4565 # fills project list info (age, description, owner, forks) for each
4566 # project in the list, removing invalid projects from returned list
4567 # NOTE: modifies $projlist, but does not remove entries from it
4568 sub fill_project_list_info {
4569 my ($projlist, $check_forks) = @_;
4570 my @projects;
4572 my $show_ctags = gitweb_check_feature('ctags');
4573 PROJECT:
4574 foreach my $pr (@$projlist) {
4575 my (@activity) = git_get_last_activity($pr->{'path'});
4576 unless (@activity) {
4577 next PROJECT;
4579 ($pr->{'age'}, $pr->{'age_string'}) = @activity;
4580 if (!defined $pr->{'descr'}) {
4581 my $descr = git_get_project_description($pr->{'path'}) || "";
4582 $descr = to_utf8($descr);
4583 $pr->{'descr_long'} = $descr;
4584 $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
4586 if (!defined $pr->{'owner'}) {
4587 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
4589 if ($check_forks) {
4590 my $pname = $pr->{'path'};
4591 if (($pname =~ s/\.git$//) &&
4592 ($pname !~ /\/$/) &&
4593 (-d "$projectroot/$pname")) {
4594 $pr->{'forks'} = "-d $projectroot/$pname";
4595 } else {
4596 $pr->{'forks'} = 0;
4599 $show_ctags and $pr->{'ctags'} = git_get_project_ctags($pr->{'path'});
4600 push @projects, $pr;
4603 return @projects;
4606 # print 'sort by' <th> element, generating 'sort by $name' replay link
4607 # if that order is not selected
4608 sub print_sort_th {
4609 print format_sort_th(@_);
4612 sub format_sort_th {
4613 my ($name, $order, $header) = @_;
4614 my $sort_th = "";
4615 $header ||= ucfirst($name);
4617 if ($order eq $name) {
4618 $sort_th .= "<th>$header</th>\n";
4619 } else {
4620 $sort_th .= "<th>" .
4621 $cgi->a({-href => href(-replay=>1, order=>$name),
4622 -class => "header"}, $header) .
4623 "</th>\n";
4626 return $sort_th;
4629 sub git_project_list_body {
4630 # actually uses global variable $project
4631 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
4633 my $check_forks = gitweb_check_feature('forks');
4634 my @projects = fill_project_list_info($projlist, $check_forks);
4636 $order ||= $default_projects_order;
4637 $from = 0 unless defined $from;
4638 $to = $#projects if (!defined $to || $#projects < $to);
4640 my %order_info = (
4641 project => { key => 'path', type => 'str' },
4642 descr => { key => 'descr_long', type => 'str' },
4643 owner => { key => 'owner', type => 'str' },
4644 age => { key => 'age', type => 'num' }
4646 my $oi = $order_info{$order};
4647 if ($oi->{'type'} eq 'str') {
4648 @projects = sort {$a->{$oi->{'key'}} cmp $b->{$oi->{'key'}}} @projects;
4649 } else {
4650 @projects = sort {$a->{$oi->{'key'}} <=> $b->{$oi->{'key'}}} @projects;
4653 my $show_ctags = gitweb_check_feature('ctags');
4654 if ($show_ctags) {
4655 my %ctags;
4656 foreach my $p (@projects) {
4657 foreach my $ct (keys %{$p->{'ctags'}}) {
4658 $ctags{$ct} += $p->{'ctags'}->{$ct};
4661 my $cloud = git_populate_project_tagcloud(\%ctags);
4662 print git_show_project_tagcloud($cloud, 64);
4665 print "<table class=\"project_list\">\n";
4666 unless ($no_header) {
4667 print "<tr>\n";
4668 if ($check_forks) {
4669 print "<th></th>\n";
4671 print_sort_th('project', $order, 'Project');
4672 print_sort_th('descr', $order, 'Description');
4673 print_sort_th('owner', $order, 'Owner');
4674 print_sort_th('age', $order, 'Last Change');
4675 print "<th></th>\n" . # for links
4676 "</tr>\n";
4678 my $alternate = 1;
4679 my $tagfilter = $cgi->param('by_tag');
4680 for (my $i = $from; $i <= $to; $i++) {
4681 my $pr = $projects[$i];
4683 next if $tagfilter and $show_ctags and not grep { lc $_ eq lc $tagfilter } keys %{$pr->{'ctags'}};
4684 next if $searchtext and not $pr->{'path'} =~ /$searchtext/
4685 and not $pr->{'descr_long'} =~ /$searchtext/;
4686 # Weed out forks or non-matching entries of search
4687 if ($check_forks) {
4688 my $forkbase = $project; $forkbase ||= ''; $forkbase =~ s#\.git$#/#;
4689 $forkbase="^$forkbase" if $forkbase;
4690 next if not $searchtext and not $tagfilter and $show_ctags
4691 and $pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe
4694 if ($alternate) {
4695 print "<tr class=\"dark\">\n";
4696 } else {
4697 print "<tr class=\"light\">\n";
4699 $alternate ^= 1;
4700 if ($check_forks) {
4701 print "<td>";
4702 if ($pr->{'forks'}) {
4703 print "<!-- $pr->{'forks'} -->\n";
4704 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
4706 print "</td>\n";
4708 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
4709 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
4710 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
4711 -class => "list", -title => $pr->{'descr_long'}},
4712 esc_html($pr->{'descr'})) . "</td>\n" .
4713 "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n";
4714 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
4715 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
4716 "<td class=\"link\">" .
4717 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
4718 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
4719 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
4720 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
4721 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
4722 "</td>\n" .
4723 "</tr>\n";
4725 if (defined $extra) {
4726 print "<tr>\n";
4727 if ($check_forks) {
4728 print "<td></td>\n";
4730 print "<td colspan=\"5\">$extra</td>\n" .
4731 "</tr>\n";
4733 print "</table>\n";
4736 sub git_log_body {
4737 # uses global variable $project
4738 my ($commitlist, $from, $to, $refs, $extra) = @_;
4740 $from = 0 unless defined $from;
4741 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
4743 for (my $i = 0; $i <= $to; $i++) {
4744 my %co = %{$commitlist->[$i]};
4745 next if !%co;
4746 my $commit = $co{'id'};
4747 my $ref = format_ref_marker($refs, $commit);
4748 my %ad = parse_date($co{'author_epoch'});
4749 git_print_header_div('commit',
4750 "<span class=\"age\">$co{'age_string'}</span>" .
4751 esc_html($co{'title'}) . $ref,
4752 $commit);
4753 print "<div class=\"title_text\">\n" .
4754 "<div class=\"log_link\">\n" .
4755 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
4756 " | " .
4757 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
4758 " | " .
4759 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
4760 "<br/>\n" .
4761 "</div>\n";
4762 git_print_authorship(\%co, -tag => 'span');
4763 print "<br/>\n</div>\n";
4765 print "<div class=\"log_body\">\n";
4766 git_print_log($co{'comment'}, -final_empty_line=> 1);
4767 print "</div>\n";
4769 if ($extra) {
4770 print "<div class=\"page_nav\">\n";
4771 print "$extra\n";
4772 print "</div>\n";
4776 sub git_shortlog_body {
4777 # uses global variable $project
4778 my ($commitlist, $from, $to, $refs, $extra) = @_;
4780 $from = 0 unless defined $from;
4781 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
4783 print "<table class=\"shortlog\">\n";
4784 my $alternate = 1;
4785 for (my $i = $from; $i <= $to; $i++) {
4786 my %co = %{$commitlist->[$i]};
4787 my $commit = $co{'id'};
4788 my $ref = format_ref_marker($refs, $commit);
4789 if ($alternate) {
4790 print "<tr class=\"dark\">\n";
4791 } else {
4792 print "<tr class=\"light\">\n";
4794 $alternate ^= 1;
4795 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
4796 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4797 format_author_html('td', \%co, 10) . "<td>";
4798 print format_subject_html($co{'title'}, $co{'title_short'},
4799 href(action=>"commit", hash=>$commit), $ref);
4800 print "</td>\n" .
4801 "<td class=\"link\">" .
4802 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
4803 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
4804 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
4805 my $snapshot_links = format_snapshot_links($commit);
4806 if (defined $snapshot_links) {
4807 print " | " . $snapshot_links;
4809 print "</td>\n" .
4810 "</tr>\n";
4812 if (defined $extra) {
4813 print "<tr>\n" .
4814 "<td colspan=\"4\">$extra</td>\n" .
4815 "</tr>\n";
4817 print "</table>\n";
4820 sub git_history_body {
4821 # Warning: assumes constant type (blob or tree) during history
4822 my ($commitlist, $from, $to, $refs, $extra,
4823 $file_name, $file_hash, $ftype) = @_;
4825 $from = 0 unless defined $from;
4826 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
4828 print "<table class=\"history\">\n";
4829 my $alternate = 1;
4830 for (my $i = $from; $i <= $to; $i++) {
4831 my %co = %{$commitlist->[$i]};
4832 if (!%co) {
4833 next;
4835 my $commit = $co{'id'};
4837 my $ref = format_ref_marker($refs, $commit);
4839 if ($alternate) {
4840 print "<tr class=\"dark\">\n";
4841 } else {
4842 print "<tr class=\"light\">\n";
4844 $alternate ^= 1;
4845 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4846 # shortlog: format_author_html('td', \%co, 10)
4847 format_author_html('td', \%co, 15, 3) . "<td>";
4848 # originally git_history used chop_str($co{'title'}, 50)
4849 print format_subject_html($co{'title'}, $co{'title_short'},
4850 href(action=>"commit", hash=>$commit), $ref);
4851 print "</td>\n" .
4852 "<td class=\"link\">" .
4853 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
4854 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
4856 if ($ftype eq 'blob') {
4857 my $blob_current = $file_hash;
4858 my $blob_parent = git_get_hash_by_path($commit, $file_name);
4859 if (defined $blob_current && defined $blob_parent &&
4860 $blob_current ne $blob_parent) {
4861 print " | " .
4862 $cgi->a({-href => href(action=>"blobdiff",
4863 hash=>$blob_current, hash_parent=>$blob_parent,
4864 hash_base=>$hash_base, hash_parent_base=>$commit,
4865 file_name=>$file_name)},
4866 "diff to current");
4869 print "</td>\n" .
4870 "</tr>\n";
4872 if (defined $extra) {
4873 print "<tr>\n" .
4874 "<td colspan=\"4\">$extra</td>\n" .
4875 "</tr>\n";
4877 print "</table>\n";
4880 sub git_tags_body {
4881 # uses global variable $project
4882 my ($taglist, $from, $to, $extra) = @_;
4883 $from = 0 unless defined $from;
4884 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
4886 print "<table class=\"tags\">\n";
4887 my $alternate = 1;
4888 for (my $i = $from; $i <= $to; $i++) {
4889 my $entry = $taglist->[$i];
4890 my %tag = %$entry;
4891 my $comment = $tag{'subject'};
4892 my $comment_short;
4893 if (defined $comment) {
4894 $comment_short = chop_str($comment, 30, 5);
4896 if ($alternate) {
4897 print "<tr class=\"dark\">\n";
4898 } else {
4899 print "<tr class=\"light\">\n";
4901 $alternate ^= 1;
4902 if (defined $tag{'age'}) {
4903 print "<td><i>$tag{'age'}</i></td>\n";
4904 } else {
4905 print "<td></td>\n";
4907 print "<td>" .
4908 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
4909 -class => "list name"}, esc_html($tag{'name'})) .
4910 "</td>\n" .
4911 "<td>";
4912 if (defined $comment) {
4913 print format_subject_html($comment, $comment_short,
4914 href(action=>"tag", hash=>$tag{'id'}));
4916 print "</td>\n" .
4917 "<td class=\"selflink\">";
4918 if ($tag{'type'} eq "tag") {
4919 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
4920 } else {
4921 print "&nbsp;";
4923 print "</td>\n" .
4924 "<td class=\"link\">" . " | " .
4925 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
4926 if ($tag{'reftype'} eq "commit") {
4927 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})}, "shortlog") .
4928 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})}, "log");
4929 } elsif ($tag{'reftype'} eq "blob") {
4930 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
4932 print "</td>\n" .
4933 "</tr>";
4935 if (defined $extra) {
4936 print "<tr>\n" .
4937 "<td colspan=\"5\">$extra</td>\n" .
4938 "</tr>\n";
4940 print "</table>\n";
4943 sub git_heads_body {
4944 # uses global variable $project
4945 my ($headlist, $head, $from, $to, $extra) = @_;
4946 $from = 0 unless defined $from;
4947 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
4949 print "<table class=\"heads\">\n";
4950 my $alternate = 1;
4951 for (my $i = $from; $i <= $to; $i++) {
4952 my $entry = $headlist->[$i];
4953 my %ref = %$entry;
4954 my $curr = $ref{'id'} eq $head;
4955 if ($alternate) {
4956 print "<tr class=\"dark\">\n";
4957 } else {
4958 print "<tr class=\"light\">\n";
4960 $alternate ^= 1;
4961 print "<td><i>$ref{'age'}</i></td>\n" .
4962 ($curr ? "<td class=\"current_head\">" : "<td>") .
4963 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),
4964 -class => "list name"},esc_html($ref{'name'})) .
4965 "</td>\n" .
4966 "<td class=\"link\">" .
4967 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})}, "shortlog") . " | " .
4968 $cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})}, "log") . " | " .
4969 $cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})}, "tree") .
4970 "</td>\n" .
4971 "</tr>";
4973 if (defined $extra) {
4974 print "<tr>\n" .
4975 "<td colspan=\"3\">$extra</td>\n" .
4976 "</tr>\n";
4978 print "</table>\n";
4981 sub git_search_grep_body {
4982 my ($commitlist, $from, $to, $extra) = @_;
4983 $from = 0 unless defined $from;
4984 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
4986 print "<table class=\"commit_search\">\n";
4987 my $alternate = 1;
4988 for (my $i = $from; $i <= $to; $i++) {
4989 my %co = %{$commitlist->[$i]};
4990 if (!%co) {
4991 next;
4993 my $commit = $co{'id'};
4994 if ($alternate) {
4995 print "<tr class=\"dark\">\n";
4996 } else {
4997 print "<tr class=\"light\">\n";
4999 $alternate ^= 1;
5000 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5001 format_author_html('td', \%co, 15, 5) .
5002 "<td>" .
5003 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
5004 -class => "list subject"},
5005 chop_and_escape_str($co{'title'}, 50) . "<br/>");
5006 my $comment = $co{'comment'};
5007 foreach my $line (@$comment) {
5008 if ($line =~ m/^(.*?)($search_regexp)(.*)$/i) {
5009 my ($lead, $match, $trail) = ($1, $2, $3);
5010 $match = chop_str($match, 70, 5, 'center');
5011 my $contextlen = int((80 - length($match))/2);
5012 $contextlen = 30 if ($contextlen > 30);
5013 $lead = chop_str($lead, $contextlen, 10, 'left');
5014 $trail = chop_str($trail, $contextlen, 10, 'right');
5016 $lead = esc_html($lead);
5017 $match = esc_html($match);
5018 $trail = esc_html($trail);
5020 print "$lead<span class=\"match\">$match</span>$trail<br />";
5023 print "</td>\n" .
5024 "<td class=\"link\">" .
5025 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5026 " | " .
5027 $cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})}, "commitdiff") .
5028 " | " .
5029 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5030 print "</td>\n" .
5031 "</tr>\n";
5033 if (defined $extra) {
5034 print "<tr>\n" .
5035 "<td colspan=\"3\">$extra</td>\n" .
5036 "</tr>\n";
5038 print "</table>\n";
5041 ## ======================================================================
5042 ## ======================================================================
5043 ## actions
5045 sub git_project_list {
5046 my $order = $input_params{'order'};
5047 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
5048 die_error(400, "Unknown order parameter");
5051 my @list = git_get_projects_list();
5052 if (!@list) {
5053 die_error(404, "No projects found");
5056 git_header_html();
5057 if (defined $home_text && -f $home_text) {
5058 print "<div class=\"index_include\">\n";
5059 insert_file($home_text);
5060 print "</div>\n";
5062 print $cgi->startform(-method => "get") .
5063 "<p class=\"projsearch\">Search:\n" .
5064 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
5065 "</p>" .
5066 $cgi->end_form() . "\n";
5067 git_project_list_body(\@list, $order);
5068 git_footer_html();
5071 sub git_forks {
5072 my $order = $input_params{'order'};
5073 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
5074 die_error(400, "Unknown order parameter");
5077 my @list = git_get_projects_list($project);
5078 if (!@list) {
5079 die_error(404, "No forks found");
5082 git_header_html();
5083 git_print_page_nav('','');
5084 git_print_header_div('summary', "$project forks");
5085 git_project_list_body(\@list, $order);
5086 git_footer_html();
5089 sub git_project_index {
5090 my @projects = git_get_projects_list($project);
5092 print $cgi->header(
5093 -type => 'text/plain',
5094 -charset => 'utf-8',
5095 -content_disposition => 'inline; filename="index.aux"');
5097 foreach my $pr (@projects) {
5098 if (!exists $pr->{'owner'}) {
5099 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
5102 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
5103 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
5104 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
5105 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
5106 $path =~ s/ /\+/g;
5107 $owner =~ s/ /\+/g;
5109 print "$path $owner\n";
5113 sub git_summary {
5114 my $descr = git_get_project_description($project) || "none";
5115 my %co = parse_commit("HEAD");
5116 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
5117 my $head = $co{'id'};
5119 my $owner = git_get_project_owner($project);
5121 my $refs = git_get_references();
5122 # These get_*_list functions return one more to allow us to see if
5123 # there are more ...
5124 my @taglist = git_get_tags_list(16);
5125 my @headlist = git_get_heads_list(16);
5126 my @forklist;
5127 my $check_forks = gitweb_check_feature('forks');
5129 if ($check_forks) {
5130 @forklist = git_get_projects_list($project);
5133 git_header_html();
5134 git_print_page_nav('summary','', $head);
5136 print "<div class=\"title\">&nbsp;</div>\n";
5137 print "<table class=\"projects_list\">\n" .
5138 "<tr id=\"metadata_desc\"><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
5139 "<tr id=\"metadata_owner\"><td>owner</td><td>" . esc_html($owner) . "</td></tr>\n";
5140 if (defined $cd{'rfc2822'}) {
5141 print "<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
5144 # use per project git URL list in $projectroot/$project/cloneurl
5145 # or make project git URL from git base URL and project name
5146 my $url_tag = "URL";
5147 my @url_list = git_get_project_url_list($project);
5148 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
5149 foreach my $git_url (@url_list) {
5150 next unless $git_url;
5151 print "<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";
5152 $url_tag = "";
5155 # Tag cloud
5156 my $show_ctags = gitweb_check_feature('ctags');
5157 if ($show_ctags) {
5158 my $ctags = git_get_project_ctags($project);
5159 my $cloud = git_populate_project_tagcloud($ctags);
5160 print "<tr id=\"metadata_ctags\"><td>Content tags:<br />";
5161 print "</td>\n<td>" unless %$ctags;
5162 print "<form action=\"$show_ctags\" method=\"post\"><input type=\"hidden\" name=\"p\" value=\"$project\" />Add: <input type=\"text\" name=\"t\" size=\"8\" /></form>";
5163 print "</td>\n<td>" if %$ctags;
5164 print git_show_project_tagcloud($cloud, 48);
5165 print "</td></tr>";
5168 print "</table>\n";
5170 # If XSS prevention is on, we don't include README.html.
5171 # TODO: Allow a readme in some safe format.
5172 if (!$prevent_xss && -s "$projectroot/$project/README.html") {
5173 print "<div class=\"title\">readme</div>\n" .
5174 "<div class=\"readme\">\n";
5175 insert_file("$projectroot/$project/README.html");
5176 print "\n</div>\n"; # class="readme"
5179 # we need to request one more than 16 (0..15) to check if
5180 # those 16 are all
5181 my @commitlist = $head ? parse_commits($head, 17) : ();
5182 if (@commitlist) {
5183 git_print_header_div('shortlog');
5184 git_shortlog_body(\@commitlist, 0, 15, $refs,
5185 $#commitlist <= 15 ? undef :
5186 $cgi->a({-href => href(action=>"shortlog")}, "..."));
5189 if (@taglist) {
5190 git_print_header_div('tags');
5191 git_tags_body(\@taglist, 0, 15,
5192 $#taglist <= 15 ? undef :
5193 $cgi->a({-href => href(action=>"tags")}, "..."));
5196 if (@headlist) {
5197 git_print_header_div('heads');
5198 git_heads_body(\@headlist, $head, 0, 15,
5199 $#headlist <= 15 ? undef :
5200 $cgi->a({-href => href(action=>"heads")}, "..."));
5203 if (@forklist) {
5204 git_print_header_div('forks');
5205 git_project_list_body(\@forklist, 'age', 0, 15,
5206 $#forklist <= 15 ? undef :
5207 $cgi->a({-href => href(action=>"forks")}, "..."),
5208 'no_header');
5211 git_footer_html();
5214 sub git_tag {
5215 my %tag = parse_tag($hash);
5217 if (! %tag) {
5218 die_error(404, "Unknown tag object");
5221 my $head = git_get_head_hash($project);
5222 git_header_html();
5223 git_print_page_nav('','', $head,undef,$head);
5224 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
5225 print "<div class=\"title_text\">\n" .
5226 "<table class=\"object_header\">\n" .
5227 "<tr>\n" .
5228 "<td>object</td>\n" .
5229 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
5230 $tag{'object'}) . "</td>\n" .
5231 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
5232 $tag{'type'}) . "</td>\n" .
5233 "</tr>\n";
5234 if (defined($tag{'author'})) {
5235 git_print_authorship_rows(\%tag, 'author');
5237 print "</table>\n\n" .
5238 "</div>\n";
5239 print "<div class=\"page_body\">";
5240 my $comment = $tag{'comment'};
5241 foreach my $line (@$comment) {
5242 chomp $line;
5243 print esc_html($line, -nbsp=>1) . "<br/>\n";
5245 print "</div>\n";
5246 git_footer_html();
5249 sub git_blame_common {
5250 my $format = shift || 'porcelain';
5251 if ($format eq 'porcelain' && $cgi->param('js')) {
5252 $format = 'incremental';
5253 $action = 'blame_incremental'; # for page title etc
5256 # permissions
5257 gitweb_check_feature('blame')
5258 or die_error(403, "Blame view not allowed");
5260 # error checking
5261 die_error(400, "No file name given") unless $file_name;
5262 $hash_base ||= git_get_head_hash($project);
5263 die_error(404, "Couldn't find base commit") unless $hash_base;
5264 my %co = parse_commit($hash_base)
5265 or die_error(404, "Commit not found");
5266 my $ftype = "blob";
5267 if (!defined $hash) {
5268 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
5269 or die_error(404, "Error looking up file");
5270 } else {
5271 $ftype = git_get_type($hash);
5272 if ($ftype !~ "blob") {
5273 die_error(400, "Object is not a blob");
5277 my $fd;
5278 if ($format eq 'incremental') {
5279 # get file contents (as base)
5280 open $fd, "-|", git_cmd(), 'cat-file', 'blob', $hash
5281 or die_error(500, "Open git-cat-file failed");
5282 } elsif ($format eq 'data') {
5283 # run git-blame --incremental
5284 open $fd, "-|", git_cmd(), "blame", "--incremental",
5285 $hash_base, "--", $file_name
5286 or die_error(500, "Open git-blame --incremental failed");
5287 } else {
5288 # run git-blame --porcelain
5289 open $fd, "-|", git_cmd(), "blame", '-p',
5290 $hash_base, '--', $file_name
5291 or die_error(500, "Open git-blame --porcelain failed");
5294 # incremental blame data returns early
5295 if ($format eq 'data') {
5296 print $cgi->header(
5297 -type=>"text/plain", -charset => "utf-8",
5298 -status=> "200 OK");
5299 local $| = 1; # output autoflush
5300 print while <$fd>;
5301 close $fd
5302 or print "ERROR $!\n";
5304 print 'END';
5305 if (defined $t0 && gitweb_check_feature('timed')) {
5306 print ' '.
5307 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).
5308 ' '.$number_of_git_cmds;
5310 print "\n";
5312 return;
5315 # page header
5316 git_header_html();
5317 my $formats_nav =
5318 $cgi->a({-href => href(action=>"blob", -replay=>1)},
5319 "blob") .
5320 " | ";
5321 if ($format eq 'incremental') {
5322 $formats_nav .=
5323 $cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},
5324 "blame") . " (non-incremental)";
5325 } else {
5326 $formats_nav .=
5327 $cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},
5328 "blame") . " (incremental)";
5330 $formats_nav .=
5331 " | " .
5332 $cgi->a({-href => href(action=>"history", -replay=>1)},
5333 "history") .
5334 " | " .
5335 $cgi->a({-href => href(action=>$action, file_name=>$file_name)},
5336 "HEAD");
5337 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
5338 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5339 git_print_page_path($file_name, $ftype, $hash_base);
5341 # page body
5342 if ($format eq 'incremental') {
5343 print "<noscript>\n<div class=\"error\"><center><b>\n".
5344 "This page requires JavaScript to run.\n Use ".
5345 $cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},
5346 'this page').
5347 " instead.\n".
5348 "</b></center></div>\n</noscript>\n";
5350 print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;
5353 print qq!<div class="page_body">\n!;
5354 print qq!<div id="progress_info">... / ...</div>\n!
5355 if ($format eq 'incremental');
5356 print qq!<table id="blame_table" class="blame" width="100%">\n!.
5357 #qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.
5358 qq!<thead>\n!.
5359 qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.
5360 qq!</thead>\n!.
5361 qq!<tbody>\n!;
5363 my @rev_color = qw(light dark);
5364 my $num_colors = scalar(@rev_color);
5365 my $current_color = 0;
5367 if ($format eq 'incremental') {
5368 my $color_class = $rev_color[$current_color];
5370 #contents of a file
5371 my $linenr = 0;
5372 LINE:
5373 while (my $line = <$fd>) {
5374 chomp $line;
5375 $linenr++;
5377 print qq!<tr id="l$linenr" class="$color_class">!.
5378 qq!<td class="sha1"><a href=""> </a></td>!.
5379 qq!<td class="linenr">!.
5380 qq!<a class="linenr" href="">$linenr</a></td>!;
5381 print qq!<td class="pre">! . esc_html($line) . "</td>\n";
5382 print qq!</tr>\n!;
5385 } else { # porcelain, i.e. ordinary blame
5386 my %metainfo = (); # saves information about commits
5388 # blame data
5389 LINE:
5390 while (my $line = <$fd>) {
5391 chomp $line;
5392 # the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]
5393 # no <lines in group> for subsequent lines in group of lines
5394 my ($full_rev, $orig_lineno, $lineno, $group_size) =
5395 ($line =~ /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);
5396 if (!exists $metainfo{$full_rev}) {
5397 $metainfo{$full_rev} = { 'nprevious' => 0 };
5399 my $meta = $metainfo{$full_rev};
5400 my $data;
5401 while ($data = <$fd>) {
5402 chomp $data;
5403 last if ($data =~ s/^\t//); # contents of line
5404 if ($data =~ /^(\S+)(?: (.*))?$/) {
5405 $meta->{$1} = $2 unless exists $meta->{$1};
5407 if ($data =~ /^previous /) {
5408 $meta->{'nprevious'}++;
5411 my $short_rev = substr($full_rev, 0, 8);
5412 my $author = $meta->{'author'};
5413 my %date =
5414 parse_date($meta->{'author-time'}, $meta->{'author-tz'});
5415 my $date = $date{'iso-tz'};
5416 if ($group_size) {
5417 $current_color = ($current_color + 1) % $num_colors;
5419 my $tr_class = $rev_color[$current_color];
5420 $tr_class .= ' boundary' if (exists $meta->{'boundary'});
5421 $tr_class .= ' no-previous' if ($meta->{'nprevious'} == 0);
5422 $tr_class .= ' multiple-previous' if ($meta->{'nprevious'} > 1);
5423 print "<tr id=\"l$lineno\" class=\"$tr_class\">\n";
5424 if ($group_size) {
5425 print "<td class=\"sha1\"";
5426 print " title=\"". esc_html($author) . ", $date\"";
5427 print " rowspan=\"$group_size\"" if ($group_size > 1);
5428 print ">";
5429 print $cgi->a({-href => href(action=>"commit",
5430 hash=>$full_rev,
5431 file_name=>$file_name)},
5432 esc_html($short_rev));
5433 if ($group_size >= 2) {
5434 my @author_initials = ($author =~ /\b([[:upper:]])\B/g);
5435 if (@author_initials) {
5436 print "<br />" .
5437 esc_html(join('', @author_initials));
5438 # or join('.', ...)
5441 print "</td>\n";
5443 # 'previous' <sha1 of parent commit> <filename at commit>
5444 if (exists $meta->{'previous'} &&
5445 $meta->{'previous'} =~ /^([a-fA-F0-9]{40}) (.*)$/) {
5446 $meta->{'parent'} = $1;
5447 $meta->{'file_parent'} = unquote($2);
5449 my $linenr_commit =
5450 exists($meta->{'parent'}) ?
5451 $meta->{'parent'} : $full_rev;
5452 my $linenr_filename =
5453 exists($meta->{'file_parent'}) ?
5454 $meta->{'file_parent'} : unquote($meta->{'filename'});
5455 my $blamed = href(action => 'blame',
5456 file_name => $linenr_filename,
5457 hash_base => $linenr_commit);
5458 print "<td class=\"linenr\">";
5459 print $cgi->a({ -href => "$blamed#l$orig_lineno",
5460 -class => "linenr" },
5461 esc_html($lineno));
5462 print "</td>";
5463 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
5464 print "</tr>\n";
5465 } # end while
5469 # footer
5470 print "</tbody>\n".
5471 "</table>\n"; # class="blame"
5472 print "</div>\n"; # class="blame_body"
5473 close $fd
5474 or print "Reading blob failed\n";
5476 git_footer_html();
5479 sub git_blame {
5480 git_blame_common();
5483 sub git_blame_incremental {
5484 git_blame_common('incremental');
5487 sub git_blame_data {
5488 git_blame_common('data');
5491 sub git_tags {
5492 my $head = git_get_head_hash($project);
5493 git_header_html();
5494 git_print_page_nav('','', $head,undef,$head);
5495 git_print_header_div('summary', $project);
5497 my @tagslist = git_get_tags_list();
5498 if (@tagslist) {
5499 git_tags_body(\@tagslist);
5501 git_footer_html();
5504 sub git_heads {
5505 my $head = git_get_head_hash($project);
5506 git_header_html();
5507 git_print_page_nav('','', $head,undef,$head);
5508 git_print_header_div('summary', $project);
5510 my @headslist = git_get_heads_list();
5511 if (@headslist) {
5512 git_heads_body(\@headslist, $head);
5514 git_footer_html();
5517 sub git_blob_plain {
5518 my $type = shift;
5519 my $expires;
5521 if (!defined $hash) {
5522 if (defined $file_name) {
5523 my $base = $hash_base || git_get_head_hash($project);
5524 $hash = git_get_hash_by_path($base, $file_name, "blob")
5525 or die_error(404, "Cannot find file");
5526 } else {
5527 die_error(400, "No file name defined");
5529 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5530 # blobs defined by non-textual hash id's can be cached
5531 $expires = "+1d";
5534 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
5535 or die_error(500, "Open git-cat-file blob '$hash' failed");
5537 # content-type (can include charset)
5538 $type = blob_contenttype($fd, $file_name, $type);
5540 # "save as" filename, even when no $file_name is given
5541 my $save_as = "$hash";
5542 if (defined $file_name) {
5543 $save_as = $file_name;
5544 } elsif ($type =~ m/^text\//) {
5545 $save_as .= '.txt';
5548 # With XSS prevention on, blobs of all types except a few known safe
5549 # ones are served with "Content-Disposition: attachment" to make sure
5550 # they don't run in our security domain. For certain image types,
5551 # blob view writes an <img> tag referring to blob_plain view, and we
5552 # want to be sure not to break that by serving the image as an
5553 # attachment (though Firefox 3 doesn't seem to care).
5554 my $sandbox = $prevent_xss &&
5555 $type !~ m!^(?:text/plain|image/(?:gif|png|jpeg))$!;
5557 print $cgi->header(
5558 -type => $type,
5559 -expires => $expires,
5560 -content_disposition =>
5561 ($sandbox ? 'attachment' : 'inline')
5562 . '; filename="' . $save_as . '"');
5563 local $/ = undef;
5564 binmode STDOUT, ':raw';
5565 print <$fd>;
5566 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
5567 close $fd;
5570 sub git_blob {
5571 my $expires;
5573 if (!defined $hash) {
5574 if (defined $file_name) {
5575 my $base = $hash_base || git_get_head_hash($project);
5576 $hash = git_get_hash_by_path($base, $file_name, "blob")
5577 or die_error(404, "Cannot find file");
5578 } else {
5579 die_error(400, "No file name defined");
5581 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5582 # blobs defined by non-textual hash id's can be cached
5583 $expires = "+1d";
5586 my $have_blame = gitweb_check_feature('blame');
5587 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
5588 or die_error(500, "Couldn't cat $file_name, $hash");
5589 my $mimetype = blob_mimetype($fd, $file_name);
5590 # use 'blob_plain' (aka 'raw') view for files that cannot be displayed
5591 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {
5592 close $fd;
5593 return git_blob_plain($mimetype);
5595 # we can have blame only for text/* mimetype
5596 $have_blame &&= ($mimetype =~ m!^text/!);
5598 my $highlight = gitweb_check_feature('highlight');
5599 my $syntax = guess_file_syntax($highlight, $mimetype, $file_name);
5600 $fd = run_highlighter($fd, $highlight, $syntax)
5601 if $syntax;
5603 git_header_html(undef, $expires);
5604 my $formats_nav = '';
5605 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
5606 if (defined $file_name) {
5607 if ($have_blame) {
5608 $formats_nav .=
5609 $cgi->a({-href => href(action=>"blame", -replay=>1)},
5610 "blame") .
5611 " | ";
5613 $formats_nav .=
5614 $cgi->a({-href => href(action=>"history", -replay=>1)},
5615 "history") .
5616 " | " .
5617 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
5618 "raw") .
5619 " | " .
5620 $cgi->a({-href => href(action=>"blob",
5621 hash_base=>"HEAD", file_name=>$file_name)},
5622 "HEAD");
5623 } else {
5624 $formats_nav .=
5625 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
5626 "raw");
5628 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
5629 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5630 } else {
5631 print "<div class=\"page_nav\">\n" .
5632 "<br/><br/></div>\n" .
5633 "<div class=\"title\">$hash</div>\n";
5635 git_print_page_path($file_name, "blob", $hash_base);
5636 print "<div class=\"page_body\">\n";
5637 if ($mimetype =~ m!^image/!) {
5638 print qq!<img type="$mimetype"!;
5639 if ($file_name) {
5640 print qq! alt="$file_name" title="$file_name"!;
5642 print qq! src="! .
5643 href(action=>"blob_plain", hash=>$hash,
5644 hash_base=>$hash_base, file_name=>$file_name) .
5645 qq!" />\n!;
5646 } else {
5647 my $nr;
5648 while (my $line = <$fd>) {
5649 chomp $line;
5650 $nr++;
5651 $line = untabify($line);
5652 printf qq!<div class="pre"><a id="l%i" href="%s#l%i" class="linenr">%4i</a> %s</div>\n!,
5653 $nr, href(-replay => 1), $nr, $nr, $syntax ? $line : esc_html($line, -nbsp=>1);
5656 close $fd
5657 or print "Reading blob failed.\n";
5658 print "</div>";
5659 git_footer_html();
5662 sub git_tree {
5663 if (!defined $hash_base) {
5664 $hash_base = "HEAD";
5666 if (!defined $hash) {
5667 if (defined $file_name) {
5668 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
5669 } else {
5670 $hash = $hash_base;
5673 die_error(404, "No such tree") unless defined($hash);
5675 my $show_sizes = gitweb_check_feature('show-sizes');
5676 my $have_blame = gitweb_check_feature('blame');
5678 my @entries = ();
5680 local $/ = "\0";
5681 open my $fd, "-|", git_cmd(), "ls-tree", '-z',
5682 ($show_sizes ? '-l' : ()), @extra_options, $hash
5683 or die_error(500, "Open git-ls-tree failed");
5684 @entries = map { chomp; $_ } <$fd>;
5685 close $fd
5686 or die_error(404, "Reading tree failed");
5689 my $refs = git_get_references();
5690 my $ref = format_ref_marker($refs, $hash_base);
5691 git_header_html();
5692 my $basedir = '';
5693 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
5694 my @views_nav = ();
5695 if (defined $file_name) {
5696 push @views_nav,
5697 $cgi->a({-href => href(action=>"history", -replay=>1)},
5698 "history"),
5699 $cgi->a({-href => href(action=>"tree",
5700 hash_base=>"HEAD", file_name=>$file_name)},
5701 "HEAD"),
5703 my $snapshot_links = format_snapshot_links($hash);
5704 if (defined $snapshot_links) {
5705 # FIXME: Should be available when we have no hash base as well.
5706 push @views_nav, $snapshot_links;
5708 git_print_page_nav('tree','', $hash_base, undef, undef,
5709 join(' | ', @views_nav));
5710 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
5711 } else {
5712 undef $hash_base;
5713 print "<div class=\"page_nav\">\n";
5714 print "<br/><br/></div>\n";
5715 print "<div class=\"title\">$hash</div>\n";
5717 if (defined $file_name) {
5718 $basedir = $file_name;
5719 if ($basedir ne '' && substr($basedir, -1) ne '/') {
5720 $basedir .= '/';
5722 git_print_page_path($file_name, 'tree', $hash_base);
5724 print "<div class=\"page_body\">\n";
5725 print "<table class=\"tree\">\n";
5726 my $alternate = 1;
5727 # '..' (top directory) link if possible
5728 if (defined $hash_base &&
5729 defined $file_name && $file_name =~ m![^/]+$!) {
5730 if ($alternate) {
5731 print "<tr class=\"dark\">\n";
5732 } else {
5733 print "<tr class=\"light\">\n";
5735 $alternate ^= 1;
5737 my $up = $file_name;
5738 $up =~ s!/?[^/]+$!!;
5739 undef $up unless $up;
5740 # based on git_print_tree_entry
5741 print '<td class="mode">' . mode_str('040000') . "</td>\n";
5742 print '<td class="size">&nbsp;</td>'."\n" if $show_sizes;
5743 print '<td class="list">';
5744 print $cgi->a({-href => href(action=>"tree",
5745 hash_base=>$hash_base,
5746 file_name=>$up)},
5747 "..");
5748 print "</td>\n";
5749 print "<td class=\"link\"></td>\n";
5751 print "</tr>\n";
5753 foreach my $line (@entries) {
5754 my %t = parse_ls_tree_line($line, -z => 1, -l => $show_sizes);
5756 if ($alternate) {
5757 print "<tr class=\"dark\">\n";
5758 } else {
5759 print "<tr class=\"light\">\n";
5761 $alternate ^= 1;
5763 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
5765 print "</tr>\n";
5767 print "</table>\n" .
5768 "</div>";
5769 git_footer_html();
5772 sub snapshot_name {
5773 my ($project, $hash) = @_;
5775 # path/to/project.git -> project
5776 # path/to/project/.git -> project
5777 my $name = to_utf8($project);
5778 $name =~ s,([^/])/*\.git$,$1,;
5779 $name = basename($name);
5780 # sanitize name
5781 $name =~ s/[[:cntrl:]]/?/g;
5783 my $ver = $hash;
5784 if ($hash =~ /^[0-9a-fA-F]+$/) {
5785 # shorten SHA-1 hash
5786 my $full_hash = git_get_full_hash($project, $hash);
5787 if ($full_hash =~ /^$hash/ && length($hash) > 7) {
5788 $ver = git_get_short_hash($project, $hash);
5790 } elsif ($hash =~ m!^refs/tags/(.*)$!) {
5791 # tags don't need shortened SHA-1 hash
5792 $ver = $1;
5793 } else {
5794 # branches and other need shortened SHA-1 hash
5795 if ($hash =~ m!^refs/(?:heads|remotes)/(.*)$!) {
5796 $ver = $1;
5798 $ver .= '-' . git_get_short_hash($project, $hash);
5800 # in case of hierarchical branch names
5801 $ver =~ s!/!.!g;
5803 # name = project-version_string
5804 $name = "$name-$ver";
5806 return wantarray ? ($name, $name) : $name;
5809 sub git_snapshot {
5810 my $format = $input_params{'snapshot_format'};
5811 if (!@snapshot_fmts) {
5812 die_error(403, "Snapshots not allowed");
5814 # default to first supported snapshot format
5815 $format ||= $snapshot_fmts[0];
5816 if ($format !~ m/^[a-z0-9]+$/) {
5817 die_error(400, "Invalid snapshot format parameter");
5818 } elsif (!exists($known_snapshot_formats{$format})) {
5819 die_error(400, "Unknown snapshot format");
5820 } elsif ($known_snapshot_formats{$format}{'disabled'}) {
5821 die_error(403, "Snapshot format not allowed");
5822 } elsif (!grep($_ eq $format, @snapshot_fmts)) {
5823 die_error(403, "Unsupported snapshot format");
5826 my $type = git_get_type("$hash^{}");
5827 if (!$type) {
5828 die_error(404, 'Object does not exist');
5829 } elsif ($type eq 'blob') {
5830 die_error(400, 'Object is not a tree-ish');
5833 my ($name, $prefix) = snapshot_name($project, $hash);
5834 my $filename = "$name$known_snapshot_formats{$format}{'suffix'}";
5835 my $cmd = quote_command(
5836 git_cmd(), 'archive',
5837 "--format=$known_snapshot_formats{$format}{'format'}",
5838 "--prefix=$prefix/", $hash);
5839 if (exists $known_snapshot_formats{$format}{'compressor'}) {
5840 $cmd .= ' | ' . quote_command(@{$known_snapshot_formats{$format}{'compressor'}});
5843 $filename =~ s/(["\\])/\\$1/g;
5844 print $cgi->header(
5845 -type => $known_snapshot_formats{$format}{'type'},
5846 -content_disposition => 'inline; filename="' . $filename . '"',
5847 -status => '200 OK');
5849 open my $fd, "-|", $cmd
5850 or die_error(500, "Execute git-archive failed");
5851 binmode STDOUT, ':raw';
5852 print <$fd>;
5853 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
5854 close $fd;
5857 sub git_log_generic {
5858 my ($fmt_name, $body_subr, $base, $parent, $file_name, $file_hash) = @_;
5860 my $head = git_get_head_hash($project);
5861 if (!defined $base) {
5862 $base = $head;
5864 if (!defined $page) {
5865 $page = 0;
5867 my $refs = git_get_references();
5869 my $commit_hash = $base;
5870 if (defined $parent) {
5871 $commit_hash = "$parent..$base";
5873 my @commitlist =
5874 parse_commits($commit_hash, 101, (100 * $page),
5875 defined $file_name ? ($file_name, "--full-history") : ());
5877 my $ftype;
5878 if (!defined $file_hash && defined $file_name) {
5879 # some commits could have deleted file in question,
5880 # and not have it in tree, but one of them has to have it
5881 for (my $i = 0; $i < @commitlist; $i++) {
5882 $file_hash = git_get_hash_by_path($commitlist[$i]{'id'}, $file_name);
5883 last if defined $file_hash;
5886 if (defined $file_hash) {
5887 $ftype = git_get_type($file_hash);
5889 if (defined $file_name && !defined $ftype) {
5890 die_error(500, "Unknown type of object");
5892 my %co;
5893 if (defined $file_name) {
5894 %co = parse_commit($base)
5895 or die_error(404, "Unknown commit object");
5899 my $paging_nav = format_paging_nav($fmt_name, $page, $#commitlist >= 100);
5900 my $next_link = '';
5901 if ($#commitlist >= 100) {
5902 $next_link =
5903 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5904 -accesskey => "n", -title => "Alt-n"}, "next");
5906 my $patch_max = gitweb_get_feature('patches');
5907 if ($patch_max && !defined $file_name) {
5908 if ($patch_max < 0 || @commitlist <= $patch_max) {
5909 $paging_nav .= " &sdot; " .
5910 $cgi->a({-href => href(action=>"patches", -replay=>1)},
5911 "patches");
5915 git_header_html();
5916 git_print_page_nav($fmt_name,'', $hash,$hash,$hash, $paging_nav);
5917 if (defined $file_name) {
5918 git_print_header_div('commit', esc_html($co{'title'}), $base);
5919 } else {
5920 git_print_header_div('summary', $project)
5922 git_print_page_path($file_name, $ftype, $hash_base)
5923 if (defined $file_name);
5925 $body_subr->(\@commitlist, 0, 99, $refs, $next_link,
5926 $file_name, $file_hash, $ftype);
5928 git_footer_html();
5931 sub git_log {
5932 git_log_generic('log', \&git_log_body,
5933 $hash, $hash_parent);
5936 sub git_commit {
5937 $hash ||= $hash_base || "HEAD";
5938 my %co = parse_commit($hash)
5939 or die_error(404, "Unknown commit object");
5941 my $parent = $co{'parent'};
5942 my $parents = $co{'parents'}; # listref
5944 # we need to prepare $formats_nav before any parameter munging
5945 my $formats_nav;
5946 if (!defined $parent) {
5947 # --root commitdiff
5948 $formats_nav .= '(initial)';
5949 } elsif (@$parents == 1) {
5950 # single parent commit
5951 $formats_nav .=
5952 '(parent: ' .
5953 $cgi->a({-href => href(action=>"commit",
5954 hash=>$parent)},
5955 esc_html(substr($parent, 0, 7))) .
5956 ')';
5957 } else {
5958 # merge commit
5959 $formats_nav .=
5960 '(merge: ' .
5961 join(' ', map {
5962 $cgi->a({-href => href(action=>"commit",
5963 hash=>$_)},
5964 esc_html(substr($_, 0, 7)));
5965 } @$parents ) .
5966 ')';
5968 if (gitweb_check_feature('patches') && @$parents <= 1) {
5969 $formats_nav .= " | " .
5970 $cgi->a({-href => href(action=>"patch", -replay=>1)},
5971 "patch");
5974 if (!defined $parent) {
5975 $parent = "--root";
5977 my @difftree;
5978 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
5979 @diff_opts,
5980 (@$parents <= 1 ? $parent : '-c'),
5981 $hash, "--"
5982 or die_error(500, "Open git-diff-tree failed");
5983 @difftree = map { chomp; $_ } <$fd>;
5984 close $fd or die_error(404, "Reading git-diff-tree failed");
5986 # non-textual hash id's can be cached
5987 my $expires;
5988 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5989 $expires = "+1d";
5991 my $refs = git_get_references();
5992 my $ref = format_ref_marker($refs, $co{'id'});
5994 git_header_html(undef, $expires);
5995 git_print_page_nav('commit', '',
5996 $hash, $co{'tree'}, $hash,
5997 $formats_nav);
5999 if (defined $co{'parent'}) {
6000 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
6001 } else {
6002 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
6004 print "<div class=\"title_text\">\n" .
6005 "<table class=\"object_header\">\n";
6006 git_print_authorship_rows(\%co);
6007 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
6008 print "<tr>" .
6009 "<td>tree</td>" .
6010 "<td class=\"sha1\">" .
6011 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
6012 class => "list"}, $co{'tree'}) .
6013 "</td>" .
6014 "<td class=\"link\">" .
6015 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
6016 "tree");
6017 my $snapshot_links = format_snapshot_links($hash);
6018 if (defined $snapshot_links) {
6019 print " | " . $snapshot_links;
6021 print "</td>" .
6022 "</tr>\n";
6024 foreach my $par (@$parents) {
6025 print "<tr>" .
6026 "<td>parent</td>" .
6027 "<td class=\"sha1\">" .
6028 $cgi->a({-href => href(action=>"commit", hash=>$par),
6029 class => "list"}, $par) .
6030 "</td>" .
6031 "<td class=\"link\">" .
6032 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
6033 " | " .
6034 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
6035 "</td>" .
6036 "</tr>\n";
6038 print "</table>".
6039 "</div>\n";
6041 print "<div class=\"page_body\">\n";
6042 git_print_log($co{'comment'});
6043 print "</div>\n";
6045 git_difftree_body(\@difftree, $hash, @$parents);
6047 git_footer_html();
6050 sub git_object {
6051 # object is defined by:
6052 # - hash or hash_base alone
6053 # - hash_base and file_name
6054 my $type;
6056 # - hash or hash_base alone
6057 if ($hash || ($hash_base && !defined $file_name)) {
6058 my $object_id = $hash || $hash_base;
6060 open my $fd, "-|", quote_command(
6061 git_cmd(), 'cat-file', '-t', $object_id) . ' 2> /dev/null'
6062 or die_error(404, "Object does not exist");
6063 $type = <$fd>;
6064 chomp $type;
6065 close $fd
6066 or die_error(404, "Object does not exist");
6068 # - hash_base and file_name
6069 } elsif ($hash_base && defined $file_name) {
6070 $file_name =~ s,/+$,,;
6072 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
6073 or die_error(404, "Base object does not exist");
6075 # here errors should not hapen
6076 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
6077 or die_error(500, "Open git-ls-tree failed");
6078 my $line = <$fd>;
6079 close $fd;
6081 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
6082 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
6083 die_error(404, "File or directory for given base does not exist");
6085 $type = $2;
6086 $hash = $3;
6087 } else {
6088 die_error(400, "Not enough information to find object");
6091 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
6092 hash=>$hash, hash_base=>$hash_base,
6093 file_name=>$file_name),
6094 -status => '302 Found');
6097 sub git_blobdiff {
6098 my $format = shift || 'html';
6100 my $fd;
6101 my @difftree;
6102 my %diffinfo;
6103 my $expires;
6105 # preparing $fd and %diffinfo for git_patchset_body
6106 # new style URI
6107 if (defined $hash_base && defined $hash_parent_base) {
6108 if (defined $file_name) {
6109 # read raw output
6110 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6111 $hash_parent_base, $hash_base,
6112 "--", (defined $file_parent ? $file_parent : ()), $file_name
6113 or die_error(500, "Open git-diff-tree failed");
6114 @difftree = map { chomp; $_ } <$fd>;
6115 close $fd
6116 or die_error(404, "Reading git-diff-tree failed");
6117 @difftree
6118 or die_error(404, "Blob diff not found");
6120 } elsif (defined $hash &&
6121 $hash =~ /[0-9a-fA-F]{40}/) {
6122 # try to find filename from $hash
6124 # read filtered raw output
6125 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6126 $hash_parent_base, $hash_base, "--"
6127 or die_error(500, "Open git-diff-tree failed");
6128 @difftree =
6129 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
6130 # $hash == to_id
6131 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
6132 map { chomp; $_ } <$fd>;
6133 close $fd
6134 or die_error(404, "Reading git-diff-tree failed");
6135 @difftree
6136 or die_error(404, "Blob diff not found");
6138 } else {
6139 die_error(400, "Missing one of the blob diff parameters");
6142 if (@difftree > 1) {
6143 die_error(400, "Ambiguous blob diff specification");
6146 %diffinfo = parse_difftree_raw_line($difftree[0]);
6147 $file_parent ||= $diffinfo{'from_file'} || $file_name;
6148 $file_name ||= $diffinfo{'to_file'};
6150 $hash_parent ||= $diffinfo{'from_id'};
6151 $hash ||= $diffinfo{'to_id'};
6153 # non-textual hash id's can be cached
6154 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
6155 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
6156 $expires = '+1d';
6159 # open patch output
6160 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6161 '-p', ($format eq 'html' ? "--full-index" : ()),
6162 $hash_parent_base, $hash_base,
6163 "--", (defined $file_parent ? $file_parent : ()), $file_name
6164 or die_error(500, "Open git-diff-tree failed");
6167 # old/legacy style URI -- not generated anymore since 1.4.3.
6168 if (!%diffinfo) {
6169 die_error('404 Not Found', "Missing one of the blob diff parameters")
6172 # header
6173 if ($format eq 'html') {
6174 my $formats_nav =
6175 $cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},
6176 "raw");
6177 git_header_html(undef, $expires);
6178 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
6179 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
6180 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
6181 } else {
6182 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
6183 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
6185 if (defined $file_name) {
6186 git_print_page_path($file_name, "blob", $hash_base);
6187 } else {
6188 print "<div class=\"page_path\"></div>\n";
6191 } elsif ($format eq 'plain') {
6192 print $cgi->header(
6193 -type => 'text/plain',
6194 -charset => 'utf-8',
6195 -expires => $expires,
6196 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
6198 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
6200 } else {
6201 die_error(400, "Unknown blobdiff format");
6204 # patch
6205 if ($format eq 'html') {
6206 print "<div class=\"page_body\">\n";
6208 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
6209 close $fd;
6211 print "</div>\n"; # class="page_body"
6212 git_footer_html();
6214 } else {
6215 while (my $line = <$fd>) {
6216 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
6217 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
6219 print $line;
6221 last if $line =~ m!^\+\+\+!;
6223 local $/ = undef;
6224 print <$fd>;
6225 close $fd;
6229 sub git_blobdiff_plain {
6230 git_blobdiff('plain');
6233 sub git_commitdiff {
6234 my %params = @_;
6235 my $format = $params{-format} || 'html';
6237 my ($patch_max) = gitweb_get_feature('patches');
6238 if ($format eq 'patch') {
6239 die_error(403, "Patch view not allowed") unless $patch_max;
6242 $hash ||= $hash_base || "HEAD";
6243 my %co = parse_commit($hash)
6244 or die_error(404, "Unknown commit object");
6246 # choose format for commitdiff for merge
6247 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
6248 $hash_parent = '--cc';
6250 # we need to prepare $formats_nav before almost any parameter munging
6251 my $formats_nav;
6252 if ($format eq 'html') {
6253 $formats_nav =
6254 $cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
6255 "raw");
6256 if ($patch_max && @{$co{'parents'}} <= 1) {
6257 $formats_nav .= " | " .
6258 $cgi->a({-href => href(action=>"patch", -replay=>1)},
6259 "patch");
6262 if (defined $hash_parent &&
6263 $hash_parent ne '-c' && $hash_parent ne '--cc') {
6264 # commitdiff with two commits given
6265 my $hash_parent_short = $hash_parent;
6266 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
6267 $hash_parent_short = substr($hash_parent, 0, 7);
6269 $formats_nav .=
6270 ' (from';
6271 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
6272 if ($co{'parents'}[$i] eq $hash_parent) {
6273 $formats_nav .= ' parent ' . ($i+1);
6274 last;
6277 $formats_nav .= ': ' .
6278 $cgi->a({-href => href(action=>"commitdiff",
6279 hash=>$hash_parent)},
6280 esc_html($hash_parent_short)) .
6281 ')';
6282 } elsif (!$co{'parent'}) {
6283 # --root commitdiff
6284 $formats_nav .= ' (initial)';
6285 } elsif (scalar @{$co{'parents'}} == 1) {
6286 # single parent commit
6287 $formats_nav .=
6288 ' (parent: ' .
6289 $cgi->a({-href => href(action=>"commitdiff",
6290 hash=>$co{'parent'})},
6291 esc_html(substr($co{'parent'}, 0, 7))) .
6292 ')';
6293 } else {
6294 # merge commit
6295 if ($hash_parent eq '--cc') {
6296 $formats_nav .= ' | ' .
6297 $cgi->a({-href => href(action=>"commitdiff",
6298 hash=>$hash, hash_parent=>'-c')},
6299 'combined');
6300 } else { # $hash_parent eq '-c'
6301 $formats_nav .= ' | ' .
6302 $cgi->a({-href => href(action=>"commitdiff",
6303 hash=>$hash, hash_parent=>'--cc')},
6304 'compact');
6306 $formats_nav .=
6307 ' (merge: ' .
6308 join(' ', map {
6309 $cgi->a({-href => href(action=>"commitdiff",
6310 hash=>$_)},
6311 esc_html(substr($_, 0, 7)));
6312 } @{$co{'parents'}} ) .
6313 ')';
6317 my $hash_parent_param = $hash_parent;
6318 if (!defined $hash_parent_param) {
6319 # --cc for multiple parents, --root for parentless
6320 $hash_parent_param =
6321 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
6324 # read commitdiff
6325 my $fd;
6326 my @difftree;
6327 if ($format eq 'html') {
6328 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6329 "--no-commit-id", "--patch-with-raw", "--full-index",
6330 $hash_parent_param, $hash, "--"
6331 or die_error(500, "Open git-diff-tree failed");
6333 while (my $line = <$fd>) {
6334 chomp $line;
6335 # empty line ends raw part of diff-tree output
6336 last unless $line;
6337 push @difftree, scalar parse_difftree_raw_line($line);
6340 } elsif ($format eq 'plain') {
6341 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6342 '-p', $hash_parent_param, $hash, "--"
6343 or die_error(500, "Open git-diff-tree failed");
6344 } elsif ($format eq 'patch') {
6345 # For commit ranges, we limit the output to the number of
6346 # patches specified in the 'patches' feature.
6347 # For single commits, we limit the output to a single patch,
6348 # diverging from the git-format-patch default.
6349 my @commit_spec = ();
6350 if ($hash_parent) {
6351 if ($patch_max > 0) {
6352 push @commit_spec, "-$patch_max";
6354 push @commit_spec, '-n', "$hash_parent..$hash";
6355 } else {
6356 if ($params{-single}) {
6357 push @commit_spec, '-1';
6358 } else {
6359 if ($patch_max > 0) {
6360 push @commit_spec, "-$patch_max";
6362 push @commit_spec, "-n";
6364 push @commit_spec, '--root', $hash;
6366 open $fd, "-|", git_cmd(), "format-patch", @diff_opts,
6367 '--encoding=utf8', '--stdout', @commit_spec
6368 or die_error(500, "Open git-format-patch failed");
6369 } else {
6370 die_error(400, "Unknown commitdiff format");
6373 # non-textual hash id's can be cached
6374 my $expires;
6375 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6376 $expires = "+1d";
6379 # write commit message
6380 if ($format eq 'html') {
6381 my $refs = git_get_references();
6382 my $ref = format_ref_marker($refs, $co{'id'});
6384 git_header_html(undef, $expires);
6385 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
6386 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
6387 print "<div class=\"title_text\">\n" .
6388 "<table class=\"object_header\">\n";
6389 git_print_authorship_rows(\%co);
6390 print "</table>".
6391 "</div>\n";
6392 print "<div class=\"page_body\">\n";
6393 if (@{$co{'comment'}} > 1) {
6394 print "<div class=\"log\">\n";
6395 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
6396 print "</div>\n"; # class="log"
6399 } elsif ($format eq 'plain') {
6400 my $refs = git_get_references("tags");
6401 my $tagname = git_get_rev_name_tags($hash);
6402 my $filename = basename($project) . "-$hash.patch";
6404 print $cgi->header(
6405 -type => 'text/plain',
6406 -charset => 'utf-8',
6407 -expires => $expires,
6408 -content_disposition => 'inline; filename="' . "$filename" . '"');
6409 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
6410 print "From: " . to_utf8($co{'author'}) . "\n";
6411 print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
6412 print "Subject: " . to_utf8($co{'title'}) . "\n";
6414 print "X-Git-Tag: $tagname\n" if $tagname;
6415 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
6417 foreach my $line (@{$co{'comment'}}) {
6418 print to_utf8($line) . "\n";
6420 print "---\n\n";
6421 } elsif ($format eq 'patch') {
6422 my $filename = basename($project) . "-$hash.patch";
6424 print $cgi->header(
6425 -type => 'text/plain',
6426 -charset => 'utf-8',
6427 -expires => $expires,
6428 -content_disposition => 'inline; filename="' . "$filename" . '"');
6431 # write patch
6432 if ($format eq 'html') {
6433 my $use_parents = !defined $hash_parent ||
6434 $hash_parent eq '-c' || $hash_parent eq '--cc';
6435 git_difftree_body(\@difftree, $hash,
6436 $use_parents ? @{$co{'parents'}} : $hash_parent);
6437 print "<br/>\n";
6439 git_patchset_body($fd, \@difftree, $hash,
6440 $use_parents ? @{$co{'parents'}} : $hash_parent);
6441 close $fd;
6442 print "</div>\n"; # class="page_body"
6443 git_footer_html();
6445 } elsif ($format eq 'plain') {
6446 local $/ = undef;
6447 print <$fd>;
6448 close $fd
6449 or print "Reading git-diff-tree failed\n";
6450 } elsif ($format eq 'patch') {
6451 local $/ = undef;
6452 print <$fd>;
6453 close $fd
6454 or print "Reading git-format-patch failed\n";
6458 sub git_commitdiff_plain {
6459 git_commitdiff(-format => 'plain');
6462 # format-patch-style patches
6463 sub git_patch {
6464 git_commitdiff(-format => 'patch', -single => 1);
6467 sub git_patches {
6468 git_commitdiff(-format => 'patch');
6471 sub git_history {
6472 git_log_generic('history', \&git_history_body,
6473 $hash_base, $hash_parent_base,
6474 $file_name, $hash);
6477 sub git_search {
6478 gitweb_check_feature('search') or die_error(403, "Search is disabled");
6479 if (!defined $searchtext) {
6480 die_error(400, "Text field is empty");
6482 if (!defined $hash) {
6483 $hash = git_get_head_hash($project);
6485 my %co = parse_commit($hash);
6486 if (!%co) {
6487 die_error(404, "Unknown commit object");
6489 if (!defined $page) {
6490 $page = 0;
6493 $searchtype ||= 'commit';
6494 if ($searchtype eq 'pickaxe') {
6495 # pickaxe may take all resources of your box and run for several minutes
6496 # with every query - so decide by yourself how public you make this feature
6497 gitweb_check_feature('pickaxe')
6498 or die_error(403, "Pickaxe is disabled");
6500 if ($searchtype eq 'grep') {
6501 gitweb_check_feature('grep')
6502 or die_error(403, "Grep is disabled");
6505 git_header_html();
6507 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
6508 my $greptype;
6509 if ($searchtype eq 'commit') {
6510 $greptype = "--grep=";
6511 } elsif ($searchtype eq 'author') {
6512 $greptype = "--author=";
6513 } elsif ($searchtype eq 'committer') {
6514 $greptype = "--committer=";
6516 $greptype .= $searchtext;
6517 my @commitlist = parse_commits($hash, 101, (100 * $page), undef,
6518 $greptype, '--regexp-ignore-case',
6519 $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
6521 my $paging_nav = '';
6522 if ($page > 0) {
6523 $paging_nav .=
6524 $cgi->a({-href => href(action=>"search", hash=>$hash,
6525 searchtext=>$searchtext,
6526 searchtype=>$searchtype)},
6527 "first");
6528 $paging_nav .= " &sdot; " .
6529 $cgi->a({-href => href(-replay=>1, page=>$page-1),
6530 -accesskey => "p", -title => "Alt-p"}, "prev");
6531 } else {
6532 $paging_nav .= "first";
6533 $paging_nav .= " &sdot; prev";
6535 my $next_link = '';
6536 if ($#commitlist >= 100) {
6537 $next_link =
6538 $cgi->a({-href => href(-replay=>1, page=>$page+1),
6539 -accesskey => "n", -title => "Alt-n"}, "next");
6540 $paging_nav .= " &sdot; $next_link";
6541 } else {
6542 $paging_nav .= " &sdot; next";
6545 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
6546 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6547 if ($page == 0 && !@commitlist) {
6548 print "<p>No match.</p>\n";
6549 } else {
6550 git_search_grep_body(\@commitlist, 0, 99, $next_link);
6554 if ($searchtype eq 'pickaxe') {
6555 git_print_page_nav('','', $hash,$co{'tree'},$hash);
6556 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6558 print "<table class=\"pickaxe search\">\n";
6559 my $alternate = 1;
6560 local $/ = "\n";
6561 open my $fd, '-|', git_cmd(), '--no-pager', 'log', @diff_opts,
6562 '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
6563 ($search_use_regexp ? '--pickaxe-regex' : ());
6564 undef %co;
6565 my @files;
6566 while (my $line = <$fd>) {
6567 chomp $line;
6568 next unless $line;
6570 my %set = parse_difftree_raw_line($line);
6571 if (defined $set{'commit'}) {
6572 # finish previous commit
6573 if (%co) {
6574 print "</td>\n" .
6575 "<td class=\"link\">" .
6576 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
6577 " | " .
6578 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
6579 print "</td>\n" .
6580 "</tr>\n";
6583 if ($alternate) {
6584 print "<tr class=\"dark\">\n";
6585 } else {
6586 print "<tr class=\"light\">\n";
6588 $alternate ^= 1;
6589 %co = parse_commit($set{'commit'});
6590 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
6591 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
6592 "<td><i>$author</i></td>\n" .
6593 "<td>" .
6594 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
6595 -class => "list subject"},
6596 chop_and_escape_str($co{'title'}, 50) . "<br/>");
6597 } elsif (defined $set{'to_id'}) {
6598 next if ($set{'to_id'} =~ m/^0{40}$/);
6600 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
6601 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),
6602 -class => "list"},
6603 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
6604 "<br/>\n";
6607 close $fd;
6609 # finish last commit (warning: repetition!)
6610 if (%co) {
6611 print "</td>\n" .
6612 "<td class=\"link\">" .
6613 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
6614 " | " .
6615 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
6616 print "</td>\n" .
6617 "</tr>\n";
6620 print "</table>\n";
6623 if ($searchtype eq 'grep') {
6624 git_print_page_nav('','', $hash,$co{'tree'},$hash);
6625 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6627 print "<table class=\"grep_search\">\n";
6628 my $alternate = 1;
6629 my $matches = 0;
6630 local $/ = "\n";
6631 open my $fd, "-|", git_cmd(), 'grep', '-n',
6632 $search_use_regexp ? ('-E', '-i') : '-F',
6633 $searchtext, $co{'tree'};
6634 my $lastfile = '';
6635 while (my $line = <$fd>) {
6636 chomp $line;
6637 my ($file, $lno, $ltext, $binary);
6638 last if ($matches++ > 1000);
6639 if ($line =~ /^Binary file (.+) matches$/) {
6640 $file = $1;
6641 $binary = 1;
6642 } else {
6643 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
6645 if ($file ne $lastfile) {
6646 $lastfile and print "</td></tr>\n";
6647 if ($alternate++) {
6648 print "<tr class=\"dark\">\n";
6649 } else {
6650 print "<tr class=\"light\">\n";
6652 print "<td class=\"list\">".
6653 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
6654 file_name=>"$file"),
6655 -class => "list"}, esc_path($file));
6656 print "</td><td>\n";
6657 $lastfile = $file;
6659 if ($binary) {
6660 print "<div class=\"binary\">Binary file</div>\n";
6661 } else {
6662 $ltext = untabify($ltext);
6663 if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
6664 $ltext = esc_html($1, -nbsp=>1);
6665 $ltext .= '<span class="match">';
6666 $ltext .= esc_html($2, -nbsp=>1);
6667 $ltext .= '</span>';
6668 $ltext .= esc_html($3, -nbsp=>1);
6669 } else {
6670 $ltext = esc_html($ltext, -nbsp=>1);
6672 print "<div class=\"pre\">" .
6673 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
6674 file_name=>"$file").'#l'.$lno,
6675 -class => "linenr"}, sprintf('%4i', $lno))
6676 . ' ' . $ltext . "</div>\n";
6679 if ($lastfile) {
6680 print "</td></tr>\n";
6681 if ($matches > 1000) {
6682 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
6684 } else {
6685 print "<div class=\"diff nodifferences\">No matches found</div>\n";
6687 close $fd;
6689 print "</table>\n";
6691 git_footer_html();
6694 sub git_search_help {
6695 git_header_html();
6696 git_print_page_nav('','', $hash,$hash,$hash);
6697 print <<EOT;
6698 <p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without
6699 regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,
6700 the pattern entered is recognized as the POSIX extended
6701 <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case
6702 insensitive).</p>
6703 <dl>
6704 <dt><b>commit</b></dt>
6705 <dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
6707 my $have_grep = gitweb_check_feature('grep');
6708 if ($have_grep) {
6709 print <<EOT;
6710 <dt><b>grep</b></dt>
6711 <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
6712 a different one) are searched for the given pattern. On large trees, this search can take
6713 a while and put some strain on the server, so please use it with some consideration. Note that
6714 due to git-grep peculiarity, currently if regexp mode is turned off, the matches are
6715 case-sensitive.</dd>
6718 print <<EOT;
6719 <dt><b>author</b></dt>
6720 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>
6721 <dt><b>committer</b></dt>
6722 <dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>
6724 my $have_pickaxe = gitweb_check_feature('pickaxe');
6725 if ($have_pickaxe) {
6726 print <<EOT;
6727 <dt><b>pickaxe</b></dt>
6728 <dd>All commits that caused the string to appear or disappear from any file (changes that
6729 added, removed or "modified" the string) will be listed. This search can take a while and
6730 takes a lot of strain on the server, so please use it wisely. Note that since you may be
6731 interested even in changes just changing the case as well, this search is case sensitive.</dd>
6734 print "</dl>\n";
6735 git_footer_html();
6738 sub git_shortlog {
6739 git_log_generic('shortlog', \&git_shortlog_body,
6740 $hash, $hash_parent);
6743 ## ......................................................................
6744 ## feeds (RSS, Atom; OPML)
6746 sub git_feed {
6747 my $format = shift || 'atom';
6748 my $have_blame = gitweb_check_feature('blame');
6750 # Atom: http://www.atomenabled.org/developers/syndication/
6751 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
6752 if ($format ne 'rss' && $format ne 'atom') {
6753 die_error(400, "Unknown web feed format");
6756 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
6757 my $head = $hash || 'HEAD';
6758 my @commitlist = parse_commits($head, 150, 0, $file_name);
6760 my %latest_commit;
6761 my %latest_date;
6762 my $content_type = "application/$format+xml";
6763 if (defined $cgi->http('HTTP_ACCEPT') &&
6764 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
6765 # browser (feed reader) prefers text/xml
6766 $content_type = 'text/xml';
6768 if (defined($commitlist[0])) {
6769 %latest_commit = %{$commitlist[0]};
6770 my $latest_epoch = $latest_commit{'committer_epoch'};
6771 %latest_date = parse_date($latest_epoch);
6772 my $if_modified = $cgi->http('IF_MODIFIED_SINCE');
6773 if (defined $if_modified) {
6774 my $since;
6775 if (eval { require HTTP::Date; 1; }) {
6776 $since = HTTP::Date::str2time($if_modified);
6777 } elsif (eval { require Time::ParseDate; 1; }) {
6778 $since = Time::ParseDate::parsedate($if_modified, GMT => 1);
6780 if (defined $since && $latest_epoch <= $since) {
6781 print $cgi->header(
6782 -type => $content_type,
6783 -charset => 'utf-8',
6784 -last_modified => $latest_date{'rfc2822'},
6785 -status => '304 Not Modified');
6786 return;
6789 print $cgi->header(
6790 -type => $content_type,
6791 -charset => 'utf-8',
6792 -last_modified => $latest_date{'rfc2822'});
6793 } else {
6794 print $cgi->header(
6795 -type => $content_type,
6796 -charset => 'utf-8');
6799 # Optimization: skip generating the body if client asks only
6800 # for Last-Modified date.
6801 return if ($cgi->request_method() eq 'HEAD');
6803 # header variables
6804 my $title = "$site_name - $project/$action";
6805 my $feed_type = 'log';
6806 if (defined $hash) {
6807 $title .= " - '$hash'";
6808 $feed_type = 'branch log';
6809 if (defined $file_name) {
6810 $title .= " :: $file_name";
6811 $feed_type = 'history';
6813 } elsif (defined $file_name) {
6814 $title .= " - $file_name";
6815 $feed_type = 'history';
6817 $title .= " $feed_type";
6818 my $descr = git_get_project_description($project);
6819 if (defined $descr) {
6820 $descr = esc_html($descr);
6821 } else {
6822 $descr = "$project " .
6823 ($format eq 'rss' ? 'RSS' : 'Atom') .
6824 " feed";
6826 my $owner = git_get_project_owner($project);
6827 $owner = esc_html($owner);
6829 #header
6830 my $alt_url;
6831 if (defined $file_name) {
6832 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
6833 } elsif (defined $hash) {
6834 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
6835 } else {
6836 $alt_url = href(-full=>1, action=>"summary");
6838 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
6839 if ($format eq 'rss') {
6840 print <<XML;
6841 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
6842 <channel>
6844 print "<title>$title</title>\n" .
6845 "<link>$alt_url</link>\n" .
6846 "<description>$descr</description>\n" .
6847 "<language>en</language>\n" .
6848 # project owner is responsible for 'editorial' content
6849 "<managingEditor>$owner</managingEditor>\n";
6850 if (defined $logo || defined $favicon) {
6851 # prefer the logo to the favicon, since RSS
6852 # doesn't allow both
6853 my $img = esc_url($logo || $favicon);
6854 print "<image>\n" .
6855 "<url>$img</url>\n" .
6856 "<title>$title</title>\n" .
6857 "<link>$alt_url</link>\n" .
6858 "</image>\n";
6860 if (%latest_date) {
6861 print "<pubDate>$latest_date{'rfc2822'}</pubDate>\n";
6862 print "<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";
6864 print "<generator>gitweb v.$version/$git_version</generator>\n";
6865 } elsif ($format eq 'atom') {
6866 print <<XML;
6867 <feed xmlns="http://www.w3.org/2005/Atom">
6869 print "<title>$title</title>\n" .
6870 "<subtitle>$descr</subtitle>\n" .
6871 '<link rel="alternate" type="text/html" href="' .
6872 $alt_url . '" />' . "\n" .
6873 '<link rel="self" type="' . $content_type . '" href="' .
6874 $cgi->self_url() . '" />' . "\n" .
6875 "<id>" . href(-full=>1) . "</id>\n" .
6876 # use project owner for feed author
6877 "<author><name>$owner</name></author>\n";
6878 if (defined $favicon) {
6879 print "<icon>" . esc_url($favicon) . "</icon>\n";
6881 if (defined $logo_url) {
6882 # not twice as wide as tall: 72 x 27 pixels
6883 print "<logo>" . esc_url($logo) . "</logo>\n";
6885 if (! %latest_date) {
6886 # dummy date to keep the feed valid until commits trickle in:
6887 print "<updated>1970-01-01T00:00:00Z</updated>\n";
6888 } else {
6889 print "<updated>$latest_date{'iso-8601'}</updated>\n";
6891 print "<generator version='$version/$git_version'>gitweb</generator>\n";
6894 # contents
6895 for (my $i = 0; $i <= $#commitlist; $i++) {
6896 my %co = %{$commitlist[$i]};
6897 my $commit = $co{'id'};
6898 # we read 150, we always show 30 and the ones more recent than 48 hours
6899 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
6900 last;
6902 my %cd = parse_date($co{'author_epoch'});
6904 # get list of changed files
6905 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6906 $co{'parent'} || "--root",
6907 $co{'id'}, "--", (defined $file_name ? $file_name : ())
6908 or next;
6909 my @difftree = map { chomp; $_ } <$fd>;
6910 close $fd
6911 or next;
6913 # print element (entry, item)
6914 my $co_url = href(-full=>1, action=>"commitdiff", hash=>$commit);
6915 if ($format eq 'rss') {
6916 print "<item>\n" .
6917 "<title>" . esc_html($co{'title'}) . "</title>\n" .
6918 "<author>" . esc_html($co{'author'}) . "</author>\n" .
6919 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
6920 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
6921 "<link>$co_url</link>\n" .
6922 "<description>" . esc_html($co{'title'}) . "</description>\n" .
6923 "<content:encoded>" .
6924 "<![CDATA[\n";
6925 } elsif ($format eq 'atom') {
6926 print "<entry>\n" .
6927 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
6928 "<updated>$cd{'iso-8601'}</updated>\n" .
6929 "<author>\n" .
6930 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
6931 if ($co{'author_email'}) {
6932 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
6934 print "</author>\n" .
6935 # use committer for contributor
6936 "<contributor>\n" .
6937 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
6938 if ($co{'committer_email'}) {
6939 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
6941 print "</contributor>\n" .
6942 "<published>$cd{'iso-8601'}</published>\n" .
6943 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
6944 "<id>$co_url</id>\n" .
6945 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
6946 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
6948 my $comment = $co{'comment'};
6949 print "<pre>\n";
6950 foreach my $line (@$comment) {
6951 $line = esc_html($line);
6952 print "$line\n";
6954 print "</pre><ul>\n";
6955 foreach my $difftree_line (@difftree) {
6956 my %difftree = parse_difftree_raw_line($difftree_line);
6957 next if !$difftree{'from_id'};
6959 my $file = $difftree{'file'} || $difftree{'to_file'};
6961 print "<li>" .
6962 "[" .
6963 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
6964 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
6965 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
6966 file_name=>$file, file_parent=>$difftree{'from_file'}),
6967 -title => "diff"}, 'D');
6968 if ($have_blame) {
6969 print $cgi->a({-href => href(-full=>1, action=>"blame",
6970 file_name=>$file, hash_base=>$commit),
6971 -title => "blame"}, 'B');
6973 # if this is not a feed of a file history
6974 if (!defined $file_name || $file_name ne $file) {
6975 print $cgi->a({-href => href(-full=>1, action=>"history",
6976 file_name=>$file, hash=>$commit),
6977 -title => "history"}, 'H');
6979 $file = esc_path($file);
6980 print "] ".
6981 "$file</li>\n";
6983 if ($format eq 'rss') {
6984 print "</ul>]]>\n" .
6985 "</content:encoded>\n" .
6986 "</item>\n";
6987 } elsif ($format eq 'atom') {
6988 print "</ul>\n</div>\n" .
6989 "</content>\n" .
6990 "</entry>\n";
6994 # end of feed
6995 if ($format eq 'rss') {
6996 print "</channel>\n</rss>\n";
6997 } elsif ($format eq 'atom') {
6998 print "</feed>\n";
7002 sub git_rss {
7003 git_feed('rss');
7006 sub git_atom {
7007 git_feed('atom');
7010 sub git_opml {
7011 my @list = git_get_projects_list();
7013 print $cgi->header(
7014 -type => 'text/xml',
7015 -charset => 'utf-8',
7016 -content_disposition => 'inline; filename="opml.xml"');
7018 print <<XML;
7019 <?xml version="1.0" encoding="utf-8"?>
7020 <opml version="1.0">
7021 <head>
7022 <title>$site_name OPML Export</title>
7023 </head>
7024 <body>
7025 <outline text="git RSS feeds">
7028 foreach my $pr (@list) {
7029 my %proj = %$pr;
7030 my $head = git_get_head_hash($proj{'path'});
7031 if (!defined $head) {
7032 next;
7034 $git_dir = "$projectroot/$proj{'path'}";
7035 my %co = parse_commit($head);
7036 if (!%co) {
7037 next;
7040 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
7041 my $rss = href('project' => $proj{'path'}, 'action' => 'rss', -full => 1);
7042 my $html = href('project' => $proj{'path'}, 'action' => 'summary', -full => 1);
7043 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
7045 print <<XML;
7046 </outline>
7047 </body>
7048 </opml>