gitweb: use new Git::Repo API, and add optional caching
[git/gitweb-caching.git] / gitweb / gitweb.perl
blob410fe0db96c159ec9114b3931e63beab626664d3
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);
15 use Encode;
16 use Fcntl ':mode';
17 use File::Find qw();
18 use File::Basename qw(basename);
20 use Git::RepoRoot;
22 binmode STDOUT, ':utf8';
24 BEGIN {
25 CGI->compile() if $ENV{'MOD_PERL'};
28 our $cgi = new CGI;
29 our $version = "++GIT_VERSION++";
30 our $my_url = $cgi->url();
31 our $my_uri = $cgi->url(-absolute => 1);
33 # core git executable to use
34 # this can just be "git" if your webserver has a sensible PATH
35 our $GIT = "++GIT_BINDIR++/git";
37 # absolute fs-path which will be prepended to the project path
38 #our $projectroot = "/pub/scm";
39 our $projectroot = "++GITWEB_PROJECTROOT++";
41 # fs traversing limit for getting project list
42 # the number is relative to the projectroot
43 our $project_maxdepth = "++GITWEB_PROJECT_MAXDEPTH++";
45 # target of the home link on top of all pages
46 our $home_link = $my_uri || "/";
48 # string of the home link on top of all pages
49 our $home_link_str = "++GITWEB_HOME_LINK_STR++";
51 # name of your site or organization to appear in page titles
52 # replace this with something more descriptive for clearer bookmarks
53 our $site_name = "++GITWEB_SITENAME++"
54 || ($ENV{'SERVER_NAME'} || "Untitled") . " Git";
56 # filename of html text to include at top of each page
57 our $site_header = "++GITWEB_SITE_HEADER++";
58 # html text to include at home page
59 our $home_text = "++GITWEB_HOMETEXT++";
60 # filename of html text to include at bottom of each page
61 our $site_footer = "++GITWEB_SITE_FOOTER++";
63 # URI of stylesheets
64 our @stylesheets = ("++GITWEB_CSS++");
65 # URI of a single stylesheet, which can be overridden in GITWEB_CONFIG.
66 our $stylesheet = undef;
67 # URI of GIT logo (72x27 size)
68 our $logo = "++GITWEB_LOGO++";
69 # URI of GIT favicon, assumed to be image/png type
70 our $favicon = "++GITWEB_FAVICON++";
72 # URI and label (title) of GIT logo link
73 #our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
74 #our $logo_label = "git documentation";
75 our $logo_url = "http://git.or.cz/";
76 our $logo_label = "git homepage";
78 # source of projects list
79 our $projects_list = "++GITWEB_LIST++";
81 # the width (in characters) of the projects list "Description" column
82 our $projects_list_description_width = 25;
84 # default order of projects list
85 # valid values are none, project, descr, owner, and age
86 our $default_projects_order = "project";
88 # show repository only if this file exists
89 # (only effective if this variable evaluates to true)
90 our $export_ok = "++GITWEB_EXPORT_OK++";
92 # only allow viewing of repositories also shown on the overview page
93 our $strict_export = "++GITWEB_STRICT_EXPORT++";
95 # list of git base URLs used for URL to where fetch project from,
96 # i.e. full URL is "$git_base_url/$project"
97 our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
99 # default blob_plain mimetype and default charset for text/plain blob
100 our $default_blob_plain_mimetype = 'text/plain';
101 our $default_text_plain_charset = undef;
103 # file to use for guessing MIME types before trying /etc/mime.types
104 # (relative to the current git repository)
105 our $mimetypes_file = undef;
107 # assume this charset if line contains non-UTF-8 characters;
108 # it should be valid encoding (see Encoding::Supported(3pm) for list),
109 # for which encoding all byte sequences are valid, for example
110 # 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
111 # could be even 'utf-8' for the old behavior)
112 our $fallback_encoding = 'latin1';
114 # rename detection options for git-diff and git-diff-tree
115 # - default is '-M', with the cost proportional to
116 # (number of removed files) * (number of new files).
117 # - more costly is '-C' (which implies '-M'), with the cost proportional to
118 # (number of changed files + number of removed files) * (number of new files)
119 # - even more costly is '-C', '--find-copies-harder' with cost
120 # (number of files in the original tree) * (number of new files)
121 # - one might want to include '-B' option, e.g. '-B', '-M'
122 our @diff_opts = ('-M'); # taken from git_commit
124 # information about snapshot formats that gitweb is capable of serving
125 our %known_snapshot_formats = (
126 # name => {
127 # 'display' => display name,
128 # 'type' => mime type,
129 # 'suffix' => filename suffix,
130 # 'format' => --format for git-archive,
131 # 'compressor' => [compressor command and arguments]
132 # (array reference, optional)}
134 'tgz' => {
135 'display' => 'tar.gz',
136 'type' => 'application/x-gzip',
137 'suffix' => '.tar.gz',
138 'format' => 'tar',
139 'compressor' => ['gzip']},
141 'tbz2' => {
142 'display' => 'tar.bz2',
143 'type' => 'application/x-bzip2',
144 'suffix' => '.tar.bz2',
145 'format' => 'tar',
146 'compressor' => ['bzip2']},
148 'zip' => {
149 'display' => 'zip',
150 'type' => 'application/x-zip',
151 'suffix' => '.zip',
152 'format' => 'zip'},
155 # Aliases so we understand old gitweb.snapshot values in repository
156 # configuration.
157 our %known_snapshot_format_aliases = (
158 'gzip' => 'tgz',
159 'bzip2' => 'tbz2',
161 # backward compatibility: legacy gitweb config support
162 'x-gzip' => undef, 'gz' => undef,
163 'x-bzip2' => undef, 'bz2' => undef,
164 'x-zip' => undef, '' => undef,
167 # Cache::Cache object to cache data from the repository, or undef for
168 # no cache. You would typically use a Cache::Memcached instance here.
169 our $cache = undef;
171 # Expiration time in seconds for transient cache entries, or undef for
172 # no expiration. (Only used if $cache is defined.)
174 # Transient cache entries (like get_sha1('HEAD')) are automatically
175 # invalidated when an mtime of either the repository's root directory
176 # or of the refs directory or any subdirectory changes. This
177 # mechanism *should* detect changes to the repository reliably if you
178 # only use git or rsync to write to it, and hence this expiration time
179 # can be set very high. (This does not default to non-expiring
180 # [undef] just in case a change goes undetected for some reason.) You
181 # might want to set this to a lower time (e.g. a few minutes) if
182 # developers change files in the refs directories on your server by
183 # non-standard means (i.e. manually).
185 # You can usually go with the default here.
186 our $cache_transient_expiration_time = 60 * 60 * 24;
188 # Directory on disk to hold potentially large cache items (in
189 # particular, snapshots, diffs and blobs), or undef for no cache.
190 # Cache files will be created in this directory, but they will not be
191 # expired; you should periodically delete old files yourself. Setting
192 # $large_cache_root but not $cache is possible, but usually not
193 # sensible.
194 our $large_cache_root = undef;
196 # Extra cache key component to use. This should stringify to a string
197 # without null bytes; it is used as a means to discard all cache
198 # entries whenever this key changes. Since gitweb already uses its
199 # own version number as a cache key component, you don't normally need
200 # this unless you're doing development work on gitweb (in which case
201 # you might set it to gitweb's mtime, (stat 'gitweb.cgi')[9]).
202 our $cache_key = undef;
204 # Display information in the footer of each page (currently only cache
205 # statistics): 0 = none, 1 = short, 2 = long.
206 our $page_info = 0;
208 # You define site-wide feature defaults here; override them with
209 # $GITWEB_CONFIG as necessary.
210 our %feature = (
211 # feature => {
212 # 'sub' => feature-sub (subroutine),
213 # 'override' => allow-override (boolean),
214 # 'default' => [ default options...] (array reference)}
216 # if feature is overridable (it means that allow-override has true value),
217 # then feature-sub will be called with default options as parameters;
218 # return value of feature-sub indicates if to enable specified feature
220 # if there is no 'sub' key (no feature-sub), then feature cannot be
221 # overriden
223 # use gitweb_check_feature(<feature>) to check if <feature> is enabled
225 # Enable the 'blame' blob view, showing the last commit that modified
226 # each line in the file. This can be very CPU-intensive.
228 # To enable system wide have in $GITWEB_CONFIG
229 # $feature{'blame'}{'default'} = [1];
230 # To have project specific config enable override in $GITWEB_CONFIG
231 # $feature{'blame'}{'override'} = 1;
232 # and in project config gitweb.blame = 0|1;
233 'blame' => {
234 'sub' => \&feature_blame,
235 'override' => 0,
236 'default' => [0]},
238 # Enable the 'snapshot' link, providing a compressed archive of any
239 # tree. This can potentially generate high traffic if you have large
240 # project.
242 # Value is a list of formats defined in %known_snapshot_formats that
243 # you wish to offer.
244 # To disable system wide have in $GITWEB_CONFIG
245 # $feature{'snapshot'}{'default'} = [];
246 # To have project specific config enable override in $GITWEB_CONFIG
247 # $feature{'snapshot'}{'override'} = 1;
248 # and in project config, a comma-separated list of formats or "none"
249 # to disable. Example: gitweb.snapshot = tbz2,zip;
250 'snapshot' => {
251 'sub' => \&feature_snapshot,
252 'override' => 0,
253 'default' => ['tgz']},
255 # Enable text search, which will list the commits which match author,
256 # committer or commit text to a given string. Enabled by default.
257 # Project specific override is not supported.
258 'search' => {
259 'override' => 0,
260 'default' => [1]},
262 # Enable grep search, which will list the files in currently selected
263 # tree containing the given string. Enabled by default. This can be
264 # potentially CPU-intensive, of course.
266 # To enable system wide have in $GITWEB_CONFIG
267 # $feature{'grep'}{'default'} = [1];
268 # To have project specific config enable override in $GITWEB_CONFIG
269 # $feature{'grep'}{'override'} = 1;
270 # and in project config gitweb.grep = 0|1;
271 'grep' => {
272 'override' => 0,
273 'default' => [1]},
275 # Enable the pickaxe search, which will list the commits that modified
276 # a given string in a file. This can be practical and quite faster
277 # alternative to 'blame', but still potentially CPU-intensive.
279 # To enable system wide have in $GITWEB_CONFIG
280 # $feature{'pickaxe'}{'default'} = [1];
281 # To have project specific config enable override in $GITWEB_CONFIG
282 # $feature{'pickaxe'}{'override'} = 1;
283 # and in project config gitweb.pickaxe = 0|1;
284 'pickaxe' => {
285 'sub' => \&feature_pickaxe,
286 'override' => 0,
287 'default' => [1]},
289 # Make gitweb use an alternative format of the URLs which can be
290 # more readable and natural-looking: project name is embedded
291 # directly in the path and the query string contains other
292 # auxiliary information. All gitweb installations recognize
293 # URL in either format; this configures in which formats gitweb
294 # generates links.
296 # To enable system wide have in $GITWEB_CONFIG
297 # $feature{'pathinfo'}{'default'} = [1];
298 # Project specific override is not supported.
300 # Note that you will need to change the default location of CSS,
301 # favicon, logo and possibly other files to an absolute URL. Also,
302 # if gitweb.cgi serves as your indexfile, you will need to force
303 # $my_uri to contain the script name in your $GITWEB_CONFIG.
304 'pathinfo' => {
305 'override' => 0,
306 'default' => [0]},
308 # Make gitweb consider projects in project root subdirectories
309 # to be forks of existing projects. Given project $projname.git,
310 # projects matching $projname/*.git will not be shown in the main
311 # projects list, instead a '+' mark will be added to $projname
312 # there and a 'forks' view will be enabled for the project, listing
313 # all the forks. If project list is taken from a file, forks have
314 # to be listed after the main project.
316 # To enable system wide have in $GITWEB_CONFIG
317 # $feature{'forks'}{'default'} = [1];
318 # Project specific override is not supported.
319 'forks' => {
320 'override' => 0,
321 'default' => [0]},
324 sub gitweb_check_feature {
325 my ($name) = @_;
326 return unless exists $feature{$name};
327 my ($sub, $override, @defaults) = (
328 $feature{$name}{'sub'},
329 $feature{$name}{'override'},
330 @{$feature{$name}{'default'}});
331 if (!$override) { return @defaults; }
332 if (!defined $sub) {
333 warn "feature $name is not overrideable";
334 return @defaults;
336 return $sub->(@defaults);
339 sub feature_blame {
340 my ($val) = git_get_project_config('blame', '--bool');
342 if ($val eq 'true') {
343 return 1;
344 } elsif ($val eq 'false') {
345 return 0;
348 return $_[0];
351 sub feature_snapshot {
352 my (@fmts) = @_;
354 my ($val) = git_get_project_config('snapshot');
356 if ($val) {
357 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
360 return @fmts;
363 sub feature_grep {
364 my ($val) = git_get_project_config('grep', '--bool');
366 if ($val eq 'true') {
367 return (1);
368 } elsif ($val eq 'false') {
369 return (0);
372 return ($_[0]);
375 sub feature_pickaxe {
376 my ($val) = git_get_project_config('pickaxe', '--bool');
378 if ($val eq 'true') {
379 return (1);
380 } elsif ($val eq 'false') {
381 return (0);
384 return ($_[0]);
387 # checking HEAD file with -e is fragile if the repository was
388 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
389 # and then pruned.
390 sub check_head_link {
391 my ($dir) = @_;
392 my $headfile = "$dir/HEAD";
393 return ((-e $headfile) ||
394 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
397 sub check_export_ok {
398 my ($dir) = @_;
399 return (check_head_link($dir) &&
400 (!$export_ok || -e "$dir/$export_ok"));
403 # process alternate names for backward compatibility
404 # filter out unsupported (unknown) snapshot formats
405 sub filter_snapshot_fmts {
406 my @fmts = @_;
408 @fmts = map {
409 exists $known_snapshot_format_aliases{$_} ?
410 $known_snapshot_format_aliases{$_} : $_} @fmts;
411 @fmts = grep(exists $known_snapshot_formats{$_}, @fmts);
415 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
416 if (-e $GITWEB_CONFIG) {
417 do $GITWEB_CONFIG;
418 } else {
419 our $GITWEB_CONFIG_SYSTEM = $ENV{'GITWEB_CONFIG_SYSTEM'} || "++GITWEB_CONFIG_SYSTEM++";
420 do $GITWEB_CONFIG_SYSTEM if -e $GITWEB_CONFIG_SYSTEM;
423 # version of the core git binary
424 our $git_version = qx("$GIT" --version) =~ m/git version (.*)$/ ? $1 : "unknown";
427 # ======================================================================
428 # caching layer
430 package CachedRepo;
432 use Digest::MD5 qw(md5_hex);
433 use List::Util qw(max);
435 use base qw(Git::Repo);
437 # Global statistics, collected across repositories.
438 # Hits, misses, sets, and failed_sets are counters, and get_list is an
439 # arrayref of keys, where a key is an arrayref of key items.
440 our %cache_statistics = (
441 hits => 0, misses => 0, sets => 0, failed_sets => 0, get_list => []);
442 our %large_cache_statistics = (
443 hits => 0, misses => 0, sets => 0, failed_sets => 0, get_list => []);
445 # Options: like Git::Repo->new, and the following:
446 # cache: a Cache::Cache conforming cache instance
447 # transient_expiration_time: expiration time in seconds for transient
448 # cache entries (like get_hash('HEAD')), or undef; do not set to
449 # 30 days or more, since it makes Cache::Memcached hiccup
450 sub new {
451 my ($class, %opts) = @_;
452 my $cache = delete $opts{cache};
453 my $large_cache_root = delete $opts{large_cache_root};
454 my $transient_expiration_time = delete $opts{transient_expiration_time};
455 my $self = $class->SUPER::new(%opts);
456 $self->{cache} = $cache;
457 $self->{large_cache_root} = $large_cache_root;
458 $self->{transient_expiration_time} = $transient_expiration_time;
459 return $self;
462 sub cache { shift->{cache} }
463 sub large_cache_root { shift->{large_cache_root} }
464 sub transient_expiration_time { shift->{transient_expiration_time} }
466 # Fast function to generate a unique (short, hashed) key for the cache
467 # to use. None of the parameters *should* contain null bytes. Example:
468 # $repo->get_key('get_sha1', 'HEAD:file1') eq '05dd723732a45fcac864787ec1897cc1'
469 sub get_key {
470 my $self = shift;
471 # Some caches (like Cache::FileCache) hash keys themselves,
472 # but Cache::Memcached does not like long keys, so we need to
473 # hash them. MD5 is fine here, since (as of July 2008) there
474 # are only collision attacks, but no practical preimage
475 # attacks on MD5. Constructing two colliding keys doesn't
476 # seem to pose much of a threat for the cache. Digest::SHA1
477 # is only in core as of Perl 5.9, so we cannot use it here.
478 return md5_hex(join "\0", $self->_key_items(@_));
481 # Return a list of strings that can be used to generate a key.
482 sub _key_items {
483 my $self = shift;
484 return map { defined $_ ? " $_" : '' }
485 ('gitweb', $version, $cache_key, 'project', $self->repo_dir, @_);
488 # Convenience function: cache_set(\@key, $value, $expire);
489 # $expire is boolean and indicates whether an expiry time should be set.
490 sub cache_set {
491 my ($self, $key, $value, $expire) = @_;
492 return unless $self->cache;
493 my $expiration_token = $expire ? $self->get_last_modification() : undef;
494 my $ok = $self->cache->set(
495 $self->get_key($expiration_token, @$key), $value,
496 $expire ? $self->transient_expiration_time : ());
497 $ok ? $cache_statistics{sets}++ : $cache_statistics{failed_sets}++;
498 return $ok;
501 # Convenience function: cache_get(\@key, $expire)
502 # $expire must be the same that has been used for cache_set or the
503 # lookup will fail.
504 sub cache_get {
505 my ($self, $key, $expire) = @_;
506 return unless $self->cache;
507 my $expiration_token = $expire ? $self->get_last_modification() : undef;
508 my $val = $self->cache->get($self->get_key($expiration_token, @$key));
509 defined $val ? $cache_statistics{hits}++ : $cache_statistics{misses}++;
510 push @{$cache_statistics{get_list}},
511 [$self->repo_dir, $expire ? 1 : 0, @$key];
512 return $val;
515 # Return the output of the given git command as a string.
516 # Valid options are:
517 # cmd: An arrayref of arguments to pass to git (mandatory).
518 # max_exit_code: Die if the exit code of the git binary is greater
519 # than this (default: 0).
520 # cache: If 1, the output is cached but expires when the repo is
521 # modified; if 2, it is cached indefinitely.
522 sub cmd_output {
523 my ($self, %opts) = @_;
524 my $key = ['cmd', $opts{max_exit_code}, @{$opts{cmd}}];
525 my $output;
526 unless ($opts{cache} && defined($output = $self->cache_get(
527 $key, $opts{cache} == 1))) {
528 my @cmd = ($self->_git_cmd, @{$opts{cmd}});
529 my $cmd = join ' ', @cmd;
530 open my $fh, '-|', @cmd or die "cannot open pipe: $cmd";
531 local $/;
532 $output = <$fh>;
533 if (!close $fh) {
534 die "error closing pipe ($!): $cmd" if $!;
535 my $exit_code = $? >> 8;
536 die "Command failed with exit code $exit_code: $cmd"
537 if $exit_code > ($opts{max_exit_code} || 0);
539 $self->cache_set($key, $output, $opts{cache} == 1) if $opts{cache};
541 return $output;
544 # progressive_cmd_output(%opts)
546 # Return a coderef that returns a chunk of the output of the given
547 # command each time it is called, or undef when the output is
548 # exhausted. For the output to be cached, it must be read until the
549 # coderef returns undef, otherwise it will leave a stale temporary
550 # file in the cache.
552 # Options:
553 # cmd: an arrayref or string of arguments to git; if it's a string, it will be
554 # passed to the shell
555 # max_exit_code: die if the command exits with a higher exit code (default: 0)
556 # separator: like $/; if undef, read the output in chunks of arbitrary size
557 # cache: if true, cache the output of the command (without expiration)
559 # Example:
560 # my $diff_read = $repo->progressive_cmd_output(
561 # cmd => ['diff', $from_sha1, $to_sha1], separator => "\n", cache => 1]);
562 # while (my $line = $diff_read->()) {
563 # chomp $line;
564 # ...
566 my $_file_seq = 0;
567 sub progressive_cmd_output {
568 die 'must pass an odd number of arguments' unless @_ % 2;
569 my ($self, %opts) = @_;
570 local $/ = defined $opts{separator} ? $opts{separator} : \32768;
571 my (@cmd, $cmd_str);
572 if (ref($opts{cmd}) eq 'ARRAY') {
573 @cmd = ($self->_git_cmd, @{$opts{cmd}});
574 $cmd_str = join " ", @cmd; # only used for diagnostics
575 } else {
576 $cmd_str = main::quote_command($self->_git_cmd) .
577 " $opts{cmd}"; # this will be passed to the shell
579 # We read from $fh, whether it's a pipe or a cache file. If
580 # it's a pipe, we also progressively cache it to
581 # $tmp_cache_file, and at the end move $tmp_cache_file_name to
582 # $cache_file_name. This avoids having partially written
583 # cache entries.
584 my ($fh, $cache_file_name, $tmp_cache_file_name, $tmp_cache_file);
585 if ($opts{cache} && $self->large_cache_root) {
586 my @key_items = ('cmd', $opts{max_exit_code},
587 ref($opts{cmd}) eq 'ARRAY' ?
588 (@{$opts{cmd}}) : (undef, $opts{cmd}));
589 push @{$large_cache_statistics{get_list}},
590 [$self->repo_dir, @key_items];
591 $cache_file_name = File::Spec->catfile(
592 $self->large_cache_root, $self->get_key(@key_items));
593 $tmp_cache_file_name = File::Spec->catfile(
594 $self->large_cache_root,
595 join('.', $$, $_file_seq++, 'tmp'));
597 unless ($cache_file_name && open $fh, '<', $cache_file_name) {
598 # Not in cache -- open pipe, and open cache file to write to.
599 if (@cmd) {
600 open $fh, '-|', @cmd;
601 } else {
602 open $fh, '-|', $cmd_str;
604 die "cannot open pipe: $cmd_str" unless $fh;
605 if ($tmp_cache_file_name) {
606 open $tmp_cache_file, '>', $tmp_cache_file_name
607 or $tmp_cache_file = undef;
609 # Increment failed_sets; it will be decremented upon
610 # successful finalization of the cache entry.
611 $large_cache_statistics{failed_sets}++;
612 # Record uncached calls as misses.
613 $large_cache_statistics{misses}++;
614 } else {
615 $large_cache_statistics{hits}++;
617 my $read = sub {
618 return undef unless $fh; # already closed
619 my $output = <$fh>;
620 if ($output) {
621 # Write to cache and return.
622 if ($tmp_cache_file && ! print $tmp_cache_file $output) {
623 # Writing to cache failed; clean up
624 # and stop caching this pipe.
625 close $tmp_cache_file;
626 $tmp_cache_file = undef;
627 unlink $tmp_cache_file_name;
629 return $output;
631 # End of output; close and finalize cache.
632 if (close $fh) {
633 # We sometimes get mysterious "Bad file
634 # descriptor" errors here, but reading from
635 # the pipe worked fine, so let's not die.
636 #die "error closing pipe ($!): $cmd_str" if $!;
637 my $exit_code = $? >> 8;
638 die "Command died with exit code $exit_code: $cmd_str"
639 if $exit_code > ($opts{max_exit_code} || 0);
641 $fh = undef;
642 if ($tmp_cache_file && close $tmp_cache_file) {
643 # Cache file written OK, move it in place.
644 if (rename $tmp_cache_file_name, $cache_file_name) {
645 $large_cache_statistics{failed_sets}--;
646 $large_cache_statistics{sets}++;
649 return undef;
651 # We can also provide a &close function here in case it
652 # becomes necessary to close pipes prematurely.
653 return $read;
656 # The following methods override the base class (Git::Repo) methods to
657 # add caching.
659 sub get_sha1 {
660 my ($self, $object_id) = @_;
661 my $expire = ($object_id !~ /^[0-9a-f]{40}(?![0-9a-f])/);
662 my $triple = $self->cache_get(['SHA1', $object_id], $expire);
663 unless (defined $triple) {
664 $triple = [$self->SUPER::get_sha1($object_id)];
665 # Do not cache failed lookups -- missing SHA1s would
666 # be permanently cached, but a subsequent push to the
667 # repository might add those missing SHA1s to the
668 # repository.
669 return unless $triple->[0];
670 $self->cache_set(['SHA1', $object_id], $triple, $expire);
672 return wantarray ? @$triple : $triple->[0];
675 sub get_object {
676 my ($self, $sha1) = @_;
677 my $type_content = $self->cache_get(['cat-file', $sha1], 0);
678 unless (defined $type_content) {
679 $type_content = [$self->SUPER::get_object($sha1)];
680 die 'unexpected empty return value' unless @$type_content;
681 $self->cache_set(['cat-file', $sha1], $type_content, 0);
683 return wantarray ? @$type_content : $type_content->[1];
686 # get_commit and get_tag only return empty Commit and Tag objects,
687 # which when loaded (lazily), happen to call get_object and thus are
688 # cached as well.
690 sub name_rev {
691 my ($self, $sha1, $tags_only) = @_;
692 my $name = $self->cache_get(['name-rev', $sha1, $tags_only], 1);
693 unless (defined $name) {
694 # || '' is to cache failed lookups (name_rev doesn't
695 # ever return empty names).
696 $name = $self->SUPER::name_rev($sha1, $tags_only) || '';
697 $self->cache_set(['name-rev', $sha1, $tags_only], $name, 1);
699 return $name || undef;
702 # Return the seconds since epoch when the repository was last touched.
703 sub get_last_modification {
704 my $self = shift;
705 return $self->{last_modification} if $self->{last_modification};
706 # Hashref mapping absolute paths of directories to mtimes. We
707 # rely on the fact here that every time git updates a file, it
708 # creates it under a different name and then moves it in
709 # place, thus causing the mtime of the containing directory to
710 # be updated. Hence it's enough to just stat the directories.
711 my $mtimes = $self->cache_get(['mtimes'], 0);
712 if ($mtimes) {
713 CHECK_CACHE: {
714 # Check if the cache response is up to date.
715 while (my ($dir, $mtime) = each %$mtimes) {
716 last CHECK_CACHE if (stat $dir)[9] != $mtime;
718 $self->{last_modification} = max(values %$mtimes);
719 return max(values %$mtimes);
722 # Either mtimes are not in cache, or at least one directory
723 # has been updated. Traverse the whole ref tree and record
724 # all directory mtimes -- this is a bit slower than the
725 # up-to-date-ness check above since we end up stat'ing all
726 # files in the refs directory.
727 my $time = time;
728 $mtimes = { $self->repo_dir => (stat $self->repo_dir)[9] };
729 my $cacheable = 1;
730 # Traverse refs/ directory and record the mtimes of all
731 # directories. Implementation note: This stats *all* files in
732 # the tree; this can become quite inefficient for repositories
733 # with many refs (say, >100) that are pushed to frequently.
734 # There are two possible solutions:
735 # 1. If we dropped the requirement of following symlinks, it
736 # would in theory be possible to exploit the nlinks==2
737 # feature of many Unix file systems. However, File::Find
738 # doesn't allow us to only traverse directories.
739 # 2. We could traverse only those directories whose mtimes have
740 # changed since the last time we checked (which we got from
741 # the cache); this makes the traversal code slightly more
742 # complicated, but should solve the worst efficiency issues.
743 File::Find::find( {
744 wanted => sub {
745 my $time = time; # get time first
746 # No way to avoid stat'ing unconditionally
747 # with File::Find.
748 my @stat = stat($_);
749 if (Fcntl::S_ISDIR($stat[2])) {
750 # Record the directory's mtime.
751 $mtimes->{$File::Find::name} = $stat[9];
752 # Mtimes have a 1-second granularity,
753 # so if the directory has *just* been
754 # modified, we might miss subsequent
755 # modifictions in the same second if
756 # we cached it.
757 $cacheable = 0 if $stat[9] >= $time;
760 # The refs/ tree can contain symlinks -- e.g. as of
761 # Aug 08 repo.or.cz uses a 'forkee' symlink, which
762 # links to another repository's refs/ directory.
763 follow => 1,
764 follow_skip => 2, # ignore duplicates
765 }, File::Spec->catfile($self->repo_dir, 'refs'));
766 $self->cache_set(['mtimes'], $mtimes, 0) if $cacheable;
767 $self->{last_modification} = max(values %$mtimes);
768 return max(values %$mtimes);
771 package main;
774 our $repo_root = Git::RepoRoot->new(root_dir => $projectroot,
775 git_binary => $GIT,
776 cache => $cache,
777 large_cache_root => $large_cache_root,
778 transient_expiration_time =>
779 $cache_transient_expiration_time,
780 repo_class => 'CachedRepo'
783 $projects_list ||= $projectroot;
785 # ======================================================================
786 # input validation and dispatch
787 our $action = $cgi->param('a');
788 if (defined $action) {
789 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
790 die_error(400, "Invalid action parameter");
794 # parameters which are pathnames
795 our $project = $cgi->param('p');
796 our $repo = $repo_root->repo(repo_dir => $project) if $project;
797 if (defined $project) {
798 if (!validate_pathname($project) ||
799 !(-d "$projectroot/$project") ||
800 !check_head_link("$projectroot/$project") ||
801 ($export_ok && !(-e "$projectroot/$project/$export_ok")) ||
802 ($strict_export && !project_in_list($project))) {
803 undef $project;
804 die_error(404, "No such project");
808 our $file_name = $cgi->param('f');
809 if (defined $file_name) {
810 if (!validate_pathname($file_name)) {
811 die_error(400, "Invalid file parameter");
815 our $file_parent = $cgi->param('fp');
816 if (defined $file_parent) {
817 if (!validate_pathname($file_parent)) {
818 die_error(400, "Invalid file parent parameter");
822 # parameters which are refnames
823 our $hash = $cgi->param('h');
824 if (defined $hash) {
825 if (!validate_refname($hash)) {
826 die_error(400, "Invalid hash parameter");
830 our $hash_parent = $cgi->param('hp');
831 if (defined $hash_parent) {
832 if (!validate_refname($hash_parent)) {
833 die_error(400, "Invalid hash parent parameter");
837 our $hash_base = $cgi->param('hb');
838 if (defined $hash_base) {
839 if (!validate_refname($hash_base)) {
840 die_error(400, "Invalid hash base parameter");
844 my %allowed_options = (
845 "--no-merges" => [ qw(rss atom log shortlog history) ],
848 our @extra_options = $cgi->param('opt');
849 if (defined @extra_options) {
850 foreach my $opt (@extra_options) {
851 if (not exists $allowed_options{$opt}) {
852 die_error(400, "Invalid option parameter");
854 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
855 die_error(400, "Invalid option parameter for this action");
860 our $hash_parent_base = $cgi->param('hpb');
861 if (defined $hash_parent_base) {
862 if (!validate_refname($hash_parent_base)) {
863 die_error(400, "Invalid hash parent base parameter");
867 # other parameters
868 our $page = $cgi->param('pg');
869 if (defined $page) {
870 if ($page =~ m/[^0-9]/) {
871 die_error(400, "Invalid page parameter");
875 our $searchtype = $cgi->param('st');
876 if (defined $searchtype) {
877 if ($searchtype =~ m/[^a-z]/) {
878 die_error(400, "Invalid searchtype parameter");
882 our $search_use_regexp = $cgi->param('sr');
884 our $searchtext = $cgi->param('s');
885 our $search_regexp;
886 if (defined $searchtext) {
887 if (length($searchtext) < 2) {
888 die_error(403, "At least two characters are required for search parameter");
890 $search_regexp = $search_use_regexp ? $searchtext : quotemeta $searchtext;
893 # now read PATH_INFO and use it as alternative to parameters
894 sub evaluate_path_info {
895 return if defined $project;
896 my $path_info = $ENV{"PATH_INFO"};
897 return if !$path_info;
898 $path_info =~ s,^/+,,;
899 return if !$path_info;
900 # find which part of PATH_INFO is project
901 $project = $path_info;
902 $project =~ s,/+$,,;
903 while ($project && !check_head_link("$projectroot/$project")) {
904 $project =~ s,/*[^/]*$,,;
906 # validate project
907 $project = validate_pathname($project);
908 if (!$project ||
909 ($export_ok && !-e "$projectroot/$project/$export_ok") ||
910 ($strict_export && !project_in_list($project))) {
911 undef $project;
912 return;
914 $repo = $repo_root->repo(repo_dir => $project);
915 # do not change any parameters if an action is given using the query string
916 return if $action;
917 $path_info =~ s,^\Q$project\E/*,,;
918 my ($refname, $pathname) = split(/:/, $path_info, 2);
919 if (defined $pathname) {
920 # we got "project.git/branch:filename" or "project.git/branch:dir/"
921 # we could use git_get_type(branch:pathname) here
922 $pathname =~ s,^/+,,;
923 if (!$pathname || substr($pathname, -1) eq "/") {
924 $action ||= "tree";
925 $pathname =~ s,/$,,;
926 } else {
927 $action ||= "blob_plain";
929 $hash_base ||= validate_refname($refname);
930 $file_name ||= validate_pathname($pathname);
931 } elsif (defined $refname) {
932 # we got "project.git/branch"
933 $action ||= "shortlog";
934 $hash ||= validate_refname($refname);
937 evaluate_path_info();
939 # dispatch
940 my %actions = (
941 "blame" => \&git_blame,
942 "blobdiff" => \&git_blobdiff,
943 "blobdiff_plain" => \&git_blobdiff_plain,
944 "blob" => \&git_blob,
945 "blob_plain" => \&git_blob_plain,
946 "commitdiff" => \&git_commitdiff,
947 "commitdiff_plain" => \&git_commitdiff_plain,
948 "commit" => \&git_commit,
949 "forks" => \&git_forks,
950 "heads" => \&git_heads,
951 "history" => \&git_history,
952 "log" => \&git_log,
953 "rss" => \&git_rss,
954 "atom" => \&git_atom,
955 "search" => \&git_search,
956 "search_help" => \&git_search_help,
957 "shortlog" => \&git_shortlog,
958 "summary" => \&git_summary,
959 "tag" => \&git_tag,
960 "tags" => \&git_tags,
961 "tree" => \&git_tree,
962 "snapshot" => \&git_snapshot,
963 "object" => \&git_object,
964 # those below don't need $project
965 "opml" => \&git_opml,
966 "project_list" => \&git_project_list,
967 "project_index" => \&git_project_index,
970 if (!defined $action) {
971 if (defined $hash) {
972 $action = git_get_type($hash);
973 } elsif (defined $hash_base && defined $file_name) {
974 $action = git_get_type("$hash_base:$file_name");
975 } elsif (defined $project) {
976 $action = 'summary';
977 } else {
978 $action = 'project_list';
981 if (!defined($actions{$action})) {
982 die_error(400, "Unknown action");
984 if ($action !~ m/^(opml|project_list|project_index)$/ &&
985 !$project) {
986 die_error(400, "Project needed");
988 $actions{$action}->();
989 exit;
991 ## ======================================================================
992 ## action links
994 sub href (%) {
995 my %params = @_;
996 # default is to use -absolute url() i.e. $my_uri
997 my $href = $params{-full} ? $my_url : $my_uri;
999 # If you touch this, check the search form for updating, too.
1001 my @mapping = (
1002 project => "p",
1003 action => "a",
1004 file_name => "f",
1005 file_parent => "fp",
1006 hash => "h",
1007 hash_parent => "hp",
1008 hash_base => "hb",
1009 hash_parent_base => "hpb",
1010 page => "pg",
1011 order => "o",
1012 searchtext => "s",
1013 searchtype => "st",
1014 snapshot_format => "sf",
1015 extra_options => "opt",
1016 search_use_regexp => "sr",
1018 my %mapping = @mapping;
1020 $params{'project'} = $project unless exists $params{'project'};
1022 if ($params{-replay}) {
1023 while (my ($name, $symbol) = each %mapping) {
1024 if (!exists $params{$name}) {
1025 # to allow for multivalued params we use arrayref form
1026 $params{$name} = [ $cgi->param($symbol) ];
1031 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
1032 if ($use_pathinfo) {
1033 # use PATH_INFO for project name
1034 $href .= "/".esc_url($params{'project'}) if defined $params{'project'};
1035 delete $params{'project'};
1037 # Summary just uses the project path URL
1038 if (defined $params{'action'} && $params{'action'} eq 'summary') {
1039 delete $params{'action'};
1043 # now encode the parameters explicitly
1044 my @result = ();
1045 for (my $i = 0; $i < @mapping; $i += 2) {
1046 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
1047 if (defined $params{$name}) {
1048 if (ref($params{$name}) eq "ARRAY") {
1049 foreach my $par (@{$params{$name}}) {
1050 push @result, $symbol . "=" . esc_param($par);
1052 } else {
1053 push @result, $symbol . "=" . esc_param($params{$name});
1057 $href .= "?" . join(';', @result) if scalar @result;
1059 return $href;
1063 ## ======================================================================
1064 ## validation, quoting/unquoting and escaping
1066 sub validate_pathname {
1067 my $input = shift || return undef;
1069 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
1070 # at the beginning, at the end, and between slashes.
1071 # also this catches doubled slashes
1072 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
1073 return undef;
1075 # no null characters
1076 if ($input =~ m!\0!) {
1077 return undef;
1079 return $input;
1082 sub validate_refname {
1083 my $input = shift || return undef;
1085 # textual hashes are O.K.
1086 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
1087 return $input;
1089 # it must be correct pathname
1090 $input = validate_pathname($input)
1091 or return undef;
1092 # restrictions on ref name according to git-check-ref-format
1093 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
1094 return undef;
1096 return $input;
1099 # decode sequences of octets in utf8 into Perl's internal form,
1100 # which is utf-8 with utf8 flag set if needed. gitweb writes out
1101 # in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
1102 sub to_utf8 {
1103 my $str = shift;
1104 if (utf8::valid($str)) {
1105 utf8::decode($str);
1106 return $str;
1107 } else {
1108 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
1112 # quote unsafe chars, but keep the slash, even when it's not
1113 # correct, but quoted slashes look too horrible in bookmarks
1114 sub esc_param {
1115 my $str = shift;
1116 $str =~ s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X", ord($1))/eg;
1117 $str =~ s/\+/%2B/g;
1118 $str =~ s/ /\+/g;
1119 return $str;
1122 # quote unsafe chars in whole URL, so some charactrs cannot be quoted
1123 sub esc_url {
1124 my $str = shift;
1125 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
1126 $str =~ s/\+/%2B/g;
1127 $str =~ s/ /\+/g;
1128 return $str;
1131 # replace invalid utf8 character with SUBSTITUTION sequence
1132 sub esc_html ($;%) {
1133 my $str = shift;
1134 my %opts = @_;
1136 $str = to_utf8($str);
1137 $str = $cgi->escapeHTML($str);
1138 if ($opts{'-nbsp'}) {
1139 $str =~ s/ /&nbsp;/g;
1141 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
1142 return $str;
1145 # quote control characters and escape filename to HTML
1146 sub esc_path {
1147 my $str = shift;
1148 my %opts = @_;
1150 $str = to_utf8($str);
1151 $str = $cgi->escapeHTML($str);
1152 if ($opts{'-nbsp'}) {
1153 $str =~ s/ /&nbsp;/g;
1155 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
1156 return $str;
1159 # Make control characters "printable", using character escape codes (CEC)
1160 sub quot_cec {
1161 my $cntrl = shift;
1162 my %opts = @_;
1163 my %es = ( # character escape codes, aka escape sequences
1164 "\t" => '\t', # tab (HT)
1165 "\n" => '\n', # line feed (LF)
1166 "\r" => '\r', # carrige return (CR)
1167 "\f" => '\f', # form feed (FF)
1168 "\b" => '\b', # backspace (BS)
1169 "\a" => '\a', # alarm (bell) (BEL)
1170 "\e" => '\e', # escape (ESC)
1171 "\013" => '\v', # vertical tab (VT)
1172 "\000" => '\0', # nul character (NUL)
1174 my $chr = ( (exists $es{$cntrl})
1175 ? $es{$cntrl}
1176 : sprintf('\%03o', ord($cntrl)) );
1177 if ($opts{-nohtml}) {
1178 return $chr;
1179 } else {
1180 return "<span class=\"cntrl\">$chr</span>";
1184 # Alternatively use unicode control pictures codepoints,
1185 # Unicode "printable representation" (PR)
1186 sub quot_upr {
1187 my $cntrl = shift;
1188 my %opts = @_;
1190 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
1191 if ($opts{-nohtml}) {
1192 return $chr;
1193 } else {
1194 return "<span class=\"cntrl\">$chr</span>";
1198 # git may return quoted and escaped filenames
1199 sub unquote {
1200 my $str = shift;
1202 sub unq {
1203 my $seq = shift;
1204 my %es = ( # character escape codes, aka escape sequences
1205 't' => "\t", # tab (HT, TAB)
1206 'n' => "\n", # newline (NL)
1207 'r' => "\r", # return (CR)
1208 'f' => "\f", # form feed (FF)
1209 'b' => "\b", # backspace (BS)
1210 'a' => "\a", # alarm (bell) (BEL)
1211 'e' => "\e", # escape (ESC)
1212 'v' => "\013", # vertical tab (VT)
1215 if ($seq =~ m/^[0-7]{1,3}$/) {
1216 # octal char sequence
1217 return chr(oct($seq));
1218 } elsif (exists $es{$seq}) {
1219 # C escape sequence, aka character escape code
1220 return $es{$seq};
1222 # quoted ordinary character
1223 return $seq;
1226 if ($str =~ m/^"(.*)"$/) {
1227 # needs unquoting
1228 $str = $1;
1229 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
1231 return $str;
1234 # escape tabs (convert tabs to spaces)
1235 sub untabify {
1236 my $line = shift;
1238 while ((my $pos = index($line, "\t")) != -1) {
1239 if (my $count = (8 - ($pos % 8))) {
1240 my $spaces = ' ' x $count;
1241 $line =~ s/\t/$spaces/;
1245 return $line;
1248 sub project_in_list {
1249 my $project = shift;
1250 my @list = git_get_projects_list();
1251 return @list && scalar(grep { $_->{'path'} eq $project } @list);
1254 ## ----------------------------------------------------------------------
1255 ## HTML aware string manipulation
1257 # Try to chop given string on a word boundary between position
1258 # $len and $len+$add_len. If there is no word boundary there,
1259 # chop at $len+$add_len. Do not chop if chopped part plus ellipsis
1260 # (marking chopped part) would be longer than given string.
1261 sub chop_str {
1262 my $str = shift;
1263 my $len = shift;
1264 my $add_len = shift || 10;
1265 my $where = shift || 'right'; # 'left' | 'center' | 'right'
1267 # Make sure perl knows it is utf8 encoded so we don't
1268 # cut in the middle of a utf8 multibyte char.
1269 $str = to_utf8($str);
1271 # allow only $len chars, but don't cut a word if it would fit in $add_len
1272 # if it doesn't fit, cut it if it's still longer than the dots we would add
1273 # remove chopped character entities entirely
1275 # when chopping in the middle, distribute $len into left and right part
1276 # return early if chopping wouldn't make string shorter
1277 if ($where eq 'center') {
1278 return $str if ($len + 5 >= length($str)); # filler is length 5
1279 $len = int($len/2);
1280 } else {
1281 return $str if ($len + 4 >= length($str)); # filler is length 4
1284 # regexps: ending and beginning with word part up to $add_len
1285 my $endre = qr/.{$len}\w{0,$add_len}/;
1286 my $begre = qr/\w{0,$add_len}.{$len}/;
1288 if ($where eq 'left') {
1289 $str =~ m/^(.*?)($begre)$/;
1290 my ($lead, $body) = ($1, $2);
1291 if (length($lead) > 4) {
1292 $body =~ s/^[^;]*;// if ($lead =~ m/&[^;]*$/);
1293 $lead = " ...";
1295 return "$lead$body";
1297 } elsif ($where eq 'center') {
1298 $str =~ m/^($endre)(.*)$/;
1299 my ($left, $str) = ($1, $2);
1300 $str =~ m/^(.*?)($begre)$/;
1301 my ($mid, $right) = ($1, $2);
1302 if (length($mid) > 5) {
1303 $left =~ s/&[^;]*$//;
1304 $right =~ s/^[^;]*;// if ($mid =~ m/&[^;]*$/);
1305 $mid = " ... ";
1307 return "$left$mid$right";
1309 } else {
1310 $str =~ m/^($endre)(.*)$/;
1311 my $body = $1;
1312 my $tail = $2;
1313 if (length($tail) > 4) {
1314 $body =~ s/&[^;]*$//;
1315 $tail = "... ";
1317 return "$body$tail";
1321 # takes the same arguments as chop_str, but also wraps a <span> around the
1322 # result with a title attribute if it does get chopped. Additionally, the
1323 # string is HTML-escaped.
1324 sub chop_and_escape_str {
1325 my ($str) = @_;
1327 my $chopped = chop_str(@_);
1328 if ($chopped eq $str) {
1329 return esc_html($chopped);
1330 } else {
1331 $str =~ s/([[:cntrl:]])/?/g;
1332 return $cgi->span({-title=>$str}, esc_html($chopped));
1336 ## ----------------------------------------------------------------------
1337 ## functions returning short strings
1339 # CSS class for given age value (in seconds)
1340 sub age_class {
1341 my $age = shift;
1343 if (!defined $age) {
1344 return "noage";
1345 } elsif ($age < 60*60*2) {
1346 return "age0";
1347 } elsif ($age < 60*60*24*2) {
1348 return "age1";
1349 } else {
1350 return "age2";
1354 # convert age in seconds to "nn units ago" string
1355 sub age_string {
1356 my $age = shift;
1357 my $age_str;
1359 if ($age > 60*60*24*365*2) {
1360 $age_str = (int $age/60/60/24/365);
1361 $age_str .= " years ago";
1362 } elsif ($age > 60*60*24*(365/12)*2) {
1363 $age_str = int $age/60/60/24/(365/12);
1364 $age_str .= " months ago";
1365 } elsif ($age > 60*60*24*7*2) {
1366 $age_str = int $age/60/60/24/7;
1367 $age_str .= " weeks ago";
1368 } elsif ($age > 60*60*24*2) {
1369 $age_str = int $age/60/60/24;
1370 $age_str .= " days ago";
1371 } elsif ($age > 60*60*2) {
1372 $age_str = int $age/60/60;
1373 $age_str .= " hours ago";
1374 } elsif ($age > 60*2) {
1375 $age_str = int $age/60;
1376 $age_str .= " min ago";
1377 } elsif ($age > 2) {
1378 $age_str = int $age;
1379 $age_str .= " sec ago";
1380 } else {
1381 $age_str .= " right now";
1383 return $age_str;
1386 use constant {
1387 S_IFINVALID => 0030000,
1388 S_IFGITLINK => 0160000,
1391 # submodule/subproject, a commit object reference
1392 sub S_ISGITLINK($) {
1393 my $mode = shift;
1395 return (($mode & S_IFMT) == S_IFGITLINK)
1398 # convert file mode in octal to symbolic file mode string
1399 sub mode_str {
1400 my $mode = oct shift;
1402 if (S_ISGITLINK($mode)) {
1403 return 'm---------';
1404 } elsif (S_ISDIR($mode & S_IFMT)) {
1405 return 'drwxr-xr-x';
1406 } elsif (S_ISLNK($mode)) {
1407 return 'lrwxrwxrwx';
1408 } elsif (S_ISREG($mode)) {
1409 # git cares only about the executable bit
1410 if ($mode & S_IXUSR) {
1411 return '-rwxr-xr-x';
1412 } else {
1413 return '-rw-r--r--';
1415 } else {
1416 return '----------';
1420 # convert file mode in octal to file type string
1421 sub file_type {
1422 my $mode = shift;
1424 if ($mode !~ m/^[0-7]+$/) {
1425 return $mode;
1426 } else {
1427 $mode = oct $mode;
1430 if (S_ISGITLINK($mode)) {
1431 return "submodule";
1432 } elsif (S_ISDIR($mode & S_IFMT)) {
1433 return "directory";
1434 } elsif (S_ISLNK($mode)) {
1435 return "symlink";
1436 } elsif (S_ISREG($mode)) {
1437 return "file";
1438 } else {
1439 return "unknown";
1443 # convert file mode in octal to file type description string
1444 sub file_type_long {
1445 my $mode = shift;
1447 if ($mode !~ m/^[0-7]+$/) {
1448 return $mode;
1449 } else {
1450 $mode = oct $mode;
1453 if (S_ISGITLINK($mode)) {
1454 return "submodule";
1455 } elsif (S_ISDIR($mode & S_IFMT)) {
1456 return "directory";
1457 } elsif (S_ISLNK($mode)) {
1458 return "symlink";
1459 } elsif (S_ISREG($mode)) {
1460 if ($mode & S_IXUSR) {
1461 return "executable";
1462 } else {
1463 return "file";
1465 } else {
1466 return "unknown";
1471 ## ----------------------------------------------------------------------
1472 ## functions returning short HTML fragments, or transforming HTML fragments
1473 ## which don't belong to other sections
1475 # format line of commit message.
1476 sub format_log_line_html {
1477 my $line = shift;
1479 $line = esc_html($line, -nbsp=>1);
1480 if ($line =~ m/([0-9a-fA-F]{8,40})/) {
1481 my $hash_text = $1;
1482 my $link =
1483 $cgi->a({-href => href(action=>"object", hash=>$hash_text),
1484 -class => "text"}, $hash_text);
1485 $line =~ s/$hash_text/$link/;
1487 return $line;
1490 # format marker of refs pointing to given object
1491 sub format_ref_marker {
1492 my ($refs, $id) = @_;
1493 my $markers = '';
1495 if (defined $refs->{$id}) {
1496 foreach my $ref (@{$refs->{$id}}) {
1497 my ($type, $name) = qw();
1498 # e.g. tags/v2.6.11 or heads/next
1499 if ($ref =~ m!^(.*?)s?/(.*)$!) {
1500 $type = $1;
1501 $name = $2;
1502 } else {
1503 $type = "ref";
1504 $name = $ref;
1507 $markers .= " <span class=\"$type\" title=\"$ref\">" .
1508 esc_html($name) . "</span>";
1512 if ($markers) {
1513 return ' <span class="refs">'. $markers . '</span>';
1514 } else {
1515 return "";
1519 # format, perhaps shortened and with markers, title line
1520 sub format_subject_html {
1521 my ($long, $short, $href, $extra) = @_;
1522 $extra = '' unless defined($extra);
1524 if (length($short) < length($long)) {
1525 return $cgi->a({-href => $href, -class => "list subject",
1526 -title => to_utf8($long)},
1527 esc_html($short) . $extra);
1528 } else {
1529 return $cgi->a({-href => $href, -class => "list subject"},
1530 esc_html($long) . $extra);
1534 # format git diff header line, i.e. "diff --(git|combined|cc) ..."
1535 sub format_git_diff_header_line {
1536 my $line = shift;
1537 my $diffinfo = shift;
1538 my ($from, $to) = @_;
1540 if ($diffinfo->{'nparents'}) {
1541 # combined diff
1542 $line =~ s!^(diff (.*?) )"?.*$!$1!;
1543 if ($to->{'href'}) {
1544 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1545 esc_path($to->{'file'}));
1546 } else { # file was deleted (no href)
1547 $line .= esc_path($to->{'file'});
1549 } else {
1550 # "ordinary" diff
1551 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
1552 if ($from->{'href'}) {
1553 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
1554 'a/' . esc_path($from->{'file'}));
1555 } else { # file was added (no href)
1556 $line .= 'a/' . esc_path($from->{'file'});
1558 $line .= ' ';
1559 if ($to->{'href'}) {
1560 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1561 'b/' . esc_path($to->{'file'}));
1562 } else { # file was deleted
1563 $line .= 'b/' . esc_path($to->{'file'});
1567 return "<div class=\"diff header\">$line</div>\n";
1570 # format extended diff header line, before patch itself
1571 sub format_extended_diff_header_line {
1572 my $line = shift;
1573 my $diffinfo = shift;
1574 my ($from, $to) = @_;
1576 # match <path>
1577 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
1578 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1579 esc_path($from->{'file'}));
1581 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
1582 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1583 esc_path($to->{'file'}));
1585 # match single <mode>
1586 if ($line =~ m/\s(\d{6})$/) {
1587 $line .= '<span class="info"> (' .
1588 file_type_long($1) .
1589 ')</span>';
1591 # match <hash>
1592 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
1593 # can match only for combined diff
1594 $line = 'index ';
1595 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1596 if ($from->{'href'}[$i]) {
1597 $line .= $cgi->a({-href=>$from->{'href'}[$i],
1598 -class=>"hash"},
1599 substr($diffinfo->{'from_id'}[$i],0,7));
1600 } else {
1601 $line .= '0' x 7;
1603 # separator
1604 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
1606 $line .= '..';
1607 if ($to->{'href'}) {
1608 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1609 substr($diffinfo->{'to_id'},0,7));
1610 } else {
1611 $line .= '0' x 7;
1614 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
1615 # can match only for ordinary diff
1616 my ($from_link, $to_link);
1617 if ($from->{'href'}) {
1618 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
1619 substr($diffinfo->{'from_id'},0,7));
1620 } else {
1621 $from_link = '0' x 7;
1623 if ($to->{'href'}) {
1624 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1625 substr($diffinfo->{'to_id'},0,7));
1626 } else {
1627 $to_link = '0' x 7;
1629 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
1630 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
1633 return $line . "<br/>\n";
1636 # format from-file/to-file diff header
1637 sub format_diff_from_to_header {
1638 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
1639 my $line;
1640 my $result = '';
1642 $line = $from_line;
1643 #assert($line =~ m/^---/) if DEBUG;
1644 # no extra formatting for "^--- /dev/null"
1645 if (! $diffinfo->{'nparents'}) {
1646 # ordinary (single parent) diff
1647 if ($line =~ m!^--- "?a/!) {
1648 if ($from->{'href'}) {
1649 $line = '--- a/' .
1650 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1651 esc_path($from->{'file'}));
1652 } else {
1653 $line = '--- a/' .
1654 esc_path($from->{'file'});
1657 $result .= qq!<div class="diff from_file">$line</div>\n!;
1659 } else {
1660 # combined diff (merge commit)
1661 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1662 if ($from->{'href'}[$i]) {
1663 $line = '--- ' .
1664 $cgi->a({-href=>href(action=>"blobdiff",
1665 hash_parent=>$diffinfo->{'from_id'}[$i],
1666 hash_parent_base=>$parents[$i],
1667 file_parent=>$from->{'file'}[$i],
1668 hash=>$diffinfo->{'to_id'},
1669 hash_base=>$hash,
1670 file_name=>$to->{'file'}),
1671 -class=>"path",
1672 -title=>"diff" . ($i+1)},
1673 $i+1) .
1674 '/' .
1675 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
1676 esc_path($from->{'file'}[$i]));
1677 } else {
1678 $line = '--- /dev/null';
1680 $result .= qq!<div class="diff from_file">$line</div>\n!;
1684 $line = $to_line;
1685 #assert($line =~ m/^\+\+\+/) if DEBUG;
1686 # no extra formatting for "^+++ /dev/null"
1687 if ($line =~ m!^\+\+\+ "?b/!) {
1688 if ($to->{'href'}) {
1689 $line = '+++ b/' .
1690 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1691 esc_path($to->{'file'}));
1692 } else {
1693 $line = '+++ b/' .
1694 esc_path($to->{'file'});
1697 $result .= qq!<div class="diff to_file">$line</div>\n!;
1699 return $result;
1702 # create note for patch simplified by combined diff
1703 sub format_diff_cc_simplified {
1704 my ($diffinfo, @parents) = @_;
1705 my $result = '';
1707 $result .= "<div class=\"diff header\">" .
1708 "diff --cc ";
1709 if (!is_deleted($diffinfo)) {
1710 $result .= $cgi->a(
1711 {-href => href(action=>"blob",
1712 $hash && git_get_type($hash) eq 'commit' ?
1713 (hash_base=>$hash) : (),
1714 hash=>$diffinfo->{'to_id'},
1715 file_name=>$diffinfo->{'to_file'}),
1716 -class => "path"},
1717 esc_path($diffinfo->{'to_file'}));
1718 } else {
1719 $result .= esc_path($diffinfo->{'to_file'});
1721 $result .= "</div>\n" . # class="diff header"
1722 "<div class=\"diff nodifferences\">" .
1723 "Simple merge" .
1724 "</div>\n"; # class="diff nodifferences"
1726 return $result;
1729 # format patch (diff) line (not to be used for diff headers)
1730 sub format_diff_line {
1731 my $line = shift;
1732 my ($from, $to) = @_;
1733 my $diff_class = "";
1735 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
1736 # combined diff
1737 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
1738 if ($line =~ m/^\@{3}/) {
1739 $diff_class = " chunk_header";
1740 } elsif ($line =~ m/^\\/) {
1741 $diff_class = " incomplete";
1742 } elsif ($prefix =~ tr/+/+/) {
1743 $diff_class = " add";
1744 } elsif ($prefix =~ tr/-/-/) {
1745 $diff_class = " rem";
1747 } else {
1748 # assume ordinary diff
1749 my $char = substr($line, 0, 1);
1750 if ($char eq '+') {
1751 $diff_class = " add";
1752 } elsif ($char eq '-') {
1753 $diff_class = " rem";
1754 } elsif ($char eq '@') {
1755 $diff_class = " chunk_header";
1756 } elsif ($char eq "\\") {
1757 $diff_class = " incomplete";
1760 $line = untabify($line);
1761 if ($from && $to && $line =~ m/^\@{2} /) {
1762 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
1763 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
1765 $from_lines = 0 unless defined $from_lines;
1766 $to_lines = 0 unless defined $to_lines;
1768 if ($from->{'href'}) {
1769 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
1770 -class=>"list"}, $from_text);
1772 if ($to->{'href'}) {
1773 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
1774 -class=>"list"}, $to_text);
1776 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
1777 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1778 return "<div class=\"diff$diff_class\">$line</div>\n";
1779 } elsif ($from && $to && $line =~ m/^\@{3}/) {
1780 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
1781 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
1783 @from_text = split(' ', $ranges);
1784 for (my $i = 0; $i < @from_text; ++$i) {
1785 ($from_start[$i], $from_nlines[$i]) =
1786 (split(',', substr($from_text[$i], 1)), 0);
1789 $to_text = pop @from_text;
1790 $to_start = pop @from_start;
1791 $to_nlines = pop @from_nlines;
1793 $line = "<span class=\"chunk_info\">$prefix ";
1794 for (my $i = 0; $i < @from_text; ++$i) {
1795 if ($from->{'href'}[$i]) {
1796 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
1797 -class=>"list"}, $from_text[$i]);
1798 } else {
1799 $line .= $from_text[$i];
1801 $line .= " ";
1803 if ($to->{'href'}) {
1804 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
1805 -class=>"list"}, $to_text);
1806 } else {
1807 $line .= $to_text;
1809 $line .= " $prefix</span>" .
1810 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1811 return "<div class=\"diff$diff_class\">$line</div>\n";
1813 return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
1816 # Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
1817 # linked. Pass the hash of the tree/commit to snapshot.
1818 sub format_snapshot_links {
1819 my ($hash) = @_;
1820 my @snapshot_fmts = gitweb_check_feature('snapshot');
1821 @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
1822 my $num_fmts = @snapshot_fmts;
1823 if ($num_fmts > 1) {
1824 # A parenthesized list of links bearing format names.
1825 # e.g. "snapshot (_tar.gz_ _zip_)"
1826 return "snapshot (" . join(' ', map
1827 $cgi->a({
1828 -href => href(
1829 action=>"snapshot",
1830 hash=>$hash,
1831 snapshot_format=>$_
1833 }, $known_snapshot_formats{$_}{'display'})
1834 , @snapshot_fmts) . ")";
1835 } elsif ($num_fmts == 1) {
1836 # A single "snapshot" link whose tooltip bears the format name.
1837 # i.e. "_snapshot_"
1838 my ($fmt) = @snapshot_fmts;
1839 return
1840 $cgi->a({
1841 -href => href(
1842 action=>"snapshot",
1843 hash=>$hash,
1844 snapshot_format=>$fmt
1846 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
1847 }, "snapshot");
1848 } else { # $num_fmts == 0
1849 return undef;
1853 ## ......................................................................
1854 ## functions returning values to be passed, perhaps after some
1855 ## transformation, to other functions; e.g. returning arguments to href()
1857 # returns hash to be passed to href to generate gitweb URL
1858 # in -title key it returns description of link
1859 sub get_feed_info {
1860 my $format = shift || 'Atom';
1861 my %res = (action => lc($format));
1863 # feed links are possible only for project views
1864 return unless (defined $project);
1865 # some views should link to OPML, or to generic project feed,
1866 # or don't have specific feed yet (so they should use generic)
1867 return if ($action =~ /^(?:tags|heads|forks|tag|search)$/x);
1869 my $branch;
1870 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
1871 # from tag links; this also makes possible to detect branch links
1872 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
1873 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
1874 $branch = $1;
1876 # find log type for feed description (title)
1877 my $type = 'log';
1878 if (defined $file_name) {
1879 $type = "history of $file_name";
1880 $type .= "/" if ($action eq 'tree');
1881 $type .= " on '$branch'" if (defined $branch);
1882 } else {
1883 $type = "log of $branch" if (defined $branch);
1886 $res{-title} = $type;
1887 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
1888 $res{'file_name'} = $file_name;
1890 return %res;
1893 ## ----------------------------------------------------------------------
1894 ## git utility subroutines, invoking git commands
1896 # quote the given arguments for passing them to the shell
1897 # quote_command("command", "arg 1", "arg with ' and ! characters")
1898 # => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
1899 # Try to avoid using this function wherever possible.
1900 sub quote_command {
1901 return join(' ',
1902 map( { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ ));
1905 # git_get_sha1_or_die ( EXTENDED_OBJECT_IDENTIFER [, TYPE] )
1907 # Look up the object referred to by C<EXTENDED_OBJECT_IDENTIFER> and
1908 # return its SHA1 hash in scalar context or its ($hash, $type, $size)
1909 # in list context. Return an error page to the browser if the object
1910 # couldn't be found.
1912 # If C<TYPE> is given, resolve tag and commit objects if necessary and
1913 # die unless the object found has the right type. The $type return
1914 # value is guaranteed to equal C<TYPE>.
1915 sub git_get_sha1_or_die {
1916 my ($object_id, $want_type) = @_;
1917 # This method shouldn't be used for checking missing
1918 # parameters, since it cannot generate proper error messages.
1919 # Hence we die with 500.
1920 die_error(500, 'No object given') unless $object_id;
1921 my ($hash, $type, $size) = $repo->get_sha1($object_id);
1922 unless ($hash) {
1923 my $human_type = ucfirst($want_type || 'object');
1924 die_error(404, "$human_type not found: '$object_id'");
1926 if ($want_type && $want_type ne $type) {
1927 if ($type eq 'tag') {
1928 return git_get_sha1_or_die(
1929 $repo->get_tag($hash)->object, $want_type);
1930 } elsif ($type eq 'commit' && $want_type eq 'tree') {
1931 return git_get_sha1_or_die(
1932 $repo->get_commit($hash)->tree, $want_type);
1933 } else {
1934 # $object_id and $type can be off due to recursion,
1935 # but fixing it complicates the code too much.
1936 die_error(400, "Expected a $want_type object, but " .
1937 "'$object_id' is a $type object");
1940 return wantarray ? ($hash, $type, $size) : $hash;
1943 # get HEAD ref hash of current project or die if no HEAD ref was found
1944 sub git_get_head_hash {
1945 # Need to have the global $project variable defined.
1946 die_error(400, 'no project given') unless $project;
1947 my $sha1 = $repo->get_sha1('HEAD')
1948 or die_error(500, "HEAD ref not found for project '$project'");
1949 return $sha1;
1952 # get type of given object
1953 sub git_get_type {
1954 my ($sha1, $type, $size) = $repo->get_sha1(shift);
1955 return $type;
1958 # repository configuration
1959 our $config_file = '';
1960 our %config;
1962 # store multiple values for single key as anonymous array reference
1963 # single values stored directly in the hash, not as [ <value> ]
1964 sub hash_set_multi {
1965 my ($hash, $key, $value) = @_;
1967 if (!exists $hash->{$key}) {
1968 $hash->{$key} = $value;
1969 } elsif (!ref $hash->{$key}) {
1970 $hash->{$key} = [ $hash->{$key}, $value ];
1971 } else {
1972 push @{$hash->{$key}}, $value;
1976 # return hash of git project configuration
1977 # optionally limited to some section, e.g. 'gitweb'
1978 sub git_parse_project_config {
1979 my $section_regexp = shift;
1980 my %config;
1982 return unless $repo;
1983 for my $keyval (split "\0", $repo->cmd_output(
1984 cmd => [qw(config -z -l)], cache => 1)) {
1985 my ($key, $value) = split(/\n/, $keyval, 2);
1987 hash_set_multi(\%config, $key, $value)
1988 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
1990 return %config;
1993 # convert config value to boolean, 'true' or 'false'
1994 # no value, number > 0, 'true' and 'yes' values are true
1995 # rest of values are treated as false (never as error)
1996 sub config_to_bool {
1997 my $val = shift;
1999 # strip leading and trailing whitespace
2000 $val =~ s/^\s+//;
2001 $val =~ s/\s+$//;
2003 return (!defined $val || # section.key
2004 ($val =~ /^\d+$/ && $val) || # section.key = 1
2005 ($val =~ /^(?:true|yes)$/i)); # section.key = true
2008 # convert config value to simple decimal number
2009 # an optional value suffix of 'k', 'm', or 'g' will cause the value
2010 # to be multiplied by 1024, 1048576, or 1073741824
2011 sub config_to_int {
2012 my $val = shift;
2014 # strip leading and trailing whitespace
2015 $val =~ s/^\s+//;
2016 $val =~ s/\s+$//;
2018 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
2019 $unit = lc($unit);
2020 # unknown unit is treated as 1
2021 return $num * ($unit eq 'g' ? 1073741824 :
2022 $unit eq 'm' ? 1048576 :
2023 $unit eq 'k' ? 1024 : 1);
2025 return $val;
2028 # convert config value to array reference, if needed
2029 sub config_to_multi {
2030 my $val = shift;
2032 return ref($val) ? $val : (defined($val) ? [ $val ] : []);
2035 sub git_get_project_config {
2036 my ($key, $type) = @_;
2038 # key sanity check
2039 return unless ($key);
2040 $key =~ s/^gitweb\.//;
2041 return if ($key =~ m/\W/);
2043 # type sanity check
2044 if (defined $type) {
2045 $type =~ s/^--//;
2046 $type = undef
2047 unless ($type eq 'bool' || $type eq 'int');
2050 # get config
2051 if (!defined $config_file ||
2052 $config_file ne "$projectroot/$project/config") {
2053 %config = git_parse_project_config('gitweb');
2054 $config_file = "$projectroot/$project/config";
2057 # ensure given type
2058 if (!defined $type) {
2059 return $config{"gitweb.$key"};
2060 } elsif ($type eq 'bool') {
2061 # backward compatibility: 'git config --bool' returns true/false
2062 return config_to_bool($config{"gitweb.$key"}) ? 'true' : 'false';
2063 } elsif ($type eq 'int') {
2064 return config_to_int($config{"gitweb.$key"});
2066 return $config{"gitweb.$key"};
2069 # Return the SHA1 of the blob or tree at the path in the given commit,
2070 # or return undef if it does not exist.
2071 sub git_get_sha1_by_path {
2072 my ($base, $path, $type) = @_;
2073 $path =~ s,/+$,,;
2074 return $repo->get_sha1("$base:$path", $type);
2077 # Get path of entry with given hash at given tree-ish (ref); used to
2078 # get 'from' filename for combined diff (merge commit) for renames.
2079 # Note that this does not resolve tag or commit objects in the $hash
2080 # parameter, you must pass a tree or blob object.
2081 sub git_get_path_by_hash {
2082 my $base = shift || return;
2083 my $hash = shift || return;
2085 # This subroutine could be extracted into the Git::Tree API
2086 # once it exists.
2087 my $tree = git_get_sha1_or_die($base, 'tree');
2088 my ($file_sha1, $file_type) = $repo->get_sha1($hash);
2089 die_error(404, "object not found: '$hash'") unless $file_sha1;
2090 die_error(400, "'$hash' is a $file_type object, not a tree or blob object")
2091 unless $file_type eq 'blob' || $file_type eq 'tree';
2093 # The ls-tree output can be quite large, so use
2094 # progressive_cmd_output.
2095 my $ls_tree_read = $repo->progressive_cmd_output(
2096 cmd => [qw(ls-tree -r -t), $tree], separator => "\n",
2097 cache => 1);
2098 while (my $line = $ls_tree_read->()) {
2099 if ($line =~ /^[0-9]+ [a-z]+ $file_sha1\t(.+)$/) {
2100 while ($ls_tree_read->()) { } # cache it
2101 # TODO: needs unquoting
2102 return $1;
2105 return undef;
2108 ## ......................................................................
2109 ## git utility functions, directly accessing git repository
2111 # The following subroutines locally change the global $project
2112 # variable as a side-effect so that their calls to
2113 # git_get_project_config work.
2115 sub git_get_project_description {
2116 local $project = shift;
2118 open my $fd, "$projectroot/$project/description"
2119 or return git_get_project_config('description');
2120 my $descr = <$fd>;
2121 close $fd;
2122 if (defined $descr) {
2123 chomp $descr;
2125 return $descr;
2128 sub git_get_project_url_list {
2129 local $project = shift;
2131 open my $fd, "$projectroot/$project/cloneurl"
2132 or return wantarray ?
2133 @{ config_to_multi(git_get_project_config('url')) } :
2134 config_to_multi(git_get_project_config('url'));
2135 my @git_project_url_list = map { chomp; $_ } <$fd>;
2136 close $fd;
2138 return wantarray ? @git_project_url_list : \@git_project_url_list;
2141 sub git_get_projects_list {
2142 my ($filter) = @_;
2143 my @list;
2145 $filter ||= '';
2146 $filter =~ s/\.git$//;
2148 my ($check_forks) = gitweb_check_feature('forks');
2150 if (-d $projects_list) {
2151 # search in directory
2152 my $dir = $projects_list . ($filter ? "/$filter" : '');
2153 # remove the trailing "/"
2154 $dir =~ s!/+$!!;
2155 my $pfxlen = length("$dir");
2156 my $pfxdepth = ($dir =~ tr!/!!);
2158 File::Find::find({
2159 follow_fast => 1, # follow symbolic links
2160 follow_skip => 2, # ignore duplicates
2161 dangling_symlinks => 0, # ignore dangling symlinks, silently
2162 wanted => sub {
2163 # skip project-list toplevel, if we get it.
2164 return if (m!^[/.]$!);
2165 # only directories can be git repositories
2166 return unless (-d $_);
2167 # don't traverse too deep (Find is super slow on os x)
2168 if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
2169 $File::Find::prune = 1;
2170 return;
2173 my $subdir = substr($File::Find::name, $pfxlen + 1);
2174 # we check related file in $projectroot
2175 if ($check_forks and $subdir =~ m#/.#) {
2176 $File::Find::prune = 1;
2177 } elsif (check_export_ok("$projectroot/$filter/$subdir")) {
2178 push @list, { path => ($filter ? "$filter/" : '') . $subdir };
2179 $File::Find::prune = 1;
2182 }, "$dir");
2184 } elsif (-f $projects_list) {
2185 # read from file(url-encoded):
2186 # 'git%2Fgit.git Linus+Torvalds'
2187 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2188 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2189 my %paths;
2190 open my ($fd), $projects_list or return;
2191 PROJECT:
2192 while (my $line = <$fd>) {
2193 chomp $line;
2194 my ($path, $owner) = split ' ', $line;
2195 $path = unescape($path);
2196 $owner = unescape($owner);
2197 if (!defined $path) {
2198 next;
2200 if ($filter ne '') {
2201 # looking for forks;
2202 my $pfx = substr($path, 0, length($filter));
2203 if ($pfx ne $filter) {
2204 next PROJECT;
2206 my $sfx = substr($path, length($filter));
2207 if ($sfx !~ /^\/.*\.git$/) {
2208 next PROJECT;
2210 } elsif ($check_forks) {
2211 PATH:
2212 foreach my $filter (keys %paths) {
2213 # looking for forks;
2214 my $pfx = substr($path, 0, length($filter));
2215 if ($pfx ne $filter) {
2216 next PATH;
2218 my $sfx = substr($path, length($filter));
2219 if ($sfx !~ /^\/.*\.git$/) {
2220 next PATH;
2222 # is a fork, don't include it in
2223 # the list
2224 next PROJECT;
2227 if (check_export_ok("$projectroot/$path")) {
2228 my $pr = {
2229 path => $path,
2230 owner => to_utf8($owner),
2232 push @list, $pr;
2233 (my $forks_path = $path) =~ s/\.git$//;
2234 $paths{$forks_path}++;
2237 close $fd;
2239 return @list;
2242 our $gitweb_project_owner = undef;
2243 sub git_get_project_list_from_file {
2245 return if (defined $gitweb_project_owner);
2247 $gitweb_project_owner = {};
2248 # read from file (url-encoded):
2249 # 'git%2Fgit.git Linus+Torvalds'
2250 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2251 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2252 if (-f $projects_list) {
2253 open (my $fd , $projects_list);
2254 while (my $line = <$fd>) {
2255 chomp $line;
2256 my ($pr, $ow) = split ' ', $line;
2257 $pr = unescape($pr);
2258 $ow = unescape($ow);
2259 $gitweb_project_owner->{$pr} = to_utf8($ow);
2261 close $fd;
2265 sub git_get_project_owner {
2266 local $project = shift;
2267 my $owner;
2269 return undef unless $project;
2271 if (!defined $gitweb_project_owner) {
2272 git_get_project_list_from_file();
2275 if (exists $gitweb_project_owner->{$project}) {
2276 $owner = $gitweb_project_owner->{$project};
2278 if (!defined $owner){
2279 $owner = git_get_project_config('owner');
2281 if (!defined $owner) {
2282 $owner = get_file_owner("$projectroot/$project");
2285 return $owner;
2288 sub git_get_last_activity {
2289 my $path = shift;
2291 chomp(my $most_recent = $repo_root->repo(repo_dir => $path)->cmd_output(
2292 cmd => [ qw(for-each-ref --count=1 --format=%(committer)),
2293 qw(--sort=-committerdate refs/heads) ],
2294 cache => 1, max_exit_code => 255)) or return;
2295 $most_recent =~ / (\d+) [-+][01]\d\d\d$/ or return;
2296 my $timestamp = $1;
2297 my $age = time - $timestamp;
2298 return ($age, age_string($age));
2301 # Return a hashref from SHA1s to arrayrefs of ref names. Example:
2302 # { '7e51...' => ['tags/tag-object'], # tag SHA1
2303 # '51ba...' => ['tags/tag-object'], # referenced commit SHA1
2304 # '3c4a...' => ['heads/master', 'tags/another-tag'] }
2305 sub git_get_references {
2306 my $type = shift || "";
2307 my %refs;
2308 # This is not implementable in terms of a simple for-each-refs
2309 # call (e.g. extractable to Git::Repo->get_refs) because it
2310 # doesn't dereference, and we cannot dereference a lot of
2311 # SHA1s ourselves as long as there is no implementation that
2312 # uses Cache::Memcached->get_multi. Hence, we use
2313 # cmd_output.
2314 my @lines = split "\n", $repo->cmd_output(
2315 cmd => ['show-ref', '--dereference',
2316 ($type ? ("--", "refs/$type") : ())],
2317 max_exit_code => 1, # exits with status 1 on empty repos
2318 cache => 1);
2319 for my $line (@lines) {
2320 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
2321 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
2322 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type/?[^^]+)!) {
2323 if (defined $refs{$1}) {
2324 push @{$refs{$1}}, $2;
2325 } else {
2326 $refs{$1} = [ $2 ];
2330 return \%refs;
2333 sub git_get_rev_name_tags {
2334 my $hash = shift or die_error();
2336 my $name = $repo->name_rev($hash);
2337 $name =~ s!^tags/!! if $name;
2338 return $name;
2341 ## ----------------------------------------------------------------------
2342 ## parse to hash functions
2344 sub parse_date {
2345 my $epoch = shift;
2346 my $tz = shift || "-0000";
2348 my %date;
2349 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
2350 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
2351 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
2352 $date{'hour'} = $hour;
2353 $date{'minute'} = $min;
2354 $date{'mday'} = $mday;
2355 $date{'day'} = $days[$wday];
2356 $date{'month'} = $months[$mon];
2357 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
2358 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
2359 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
2360 $mday, $months[$mon], $hour ,$min;
2361 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
2362 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
2364 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
2365 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
2366 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
2367 $date{'hour_local'} = $hour;
2368 $date{'minute_local'} = $min;
2369 $date{'tz_local'} = $tz;
2370 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
2371 1900+$year, $mon+1, $mday,
2372 $hour, $min, $sec, $tz);
2373 return %date;
2376 sub parse_tag {
2377 my $sha1 = shift;
2378 my %tag;
2379 my @comment;
2381 my ($raw_header, $raw_comment) = split "\n\n", $repo->get_object($sha1), 2;
2382 for my $line (split "\n", $raw_header) {
2383 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
2384 $tag{'object'} = $1;
2385 } elsif ($line =~ m/^type (.+)$/) {
2386 $tag{'type'} = $1;
2387 } elsif ($line =~ m/^tag (.+)$/) {
2388 $tag{'name'} = $1;
2389 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
2390 $tag{'author'} = $1;
2391 $tag{'epoch'} = $2;
2392 $tag{'tz'} = $3;
2395 $tag{'comment'} = [split "\n", $raw_comment];
2396 return %tag
2399 sub parse_commit_text {
2400 my ($commit_text, $withparents) = @_;
2401 my @commit_lines = split '\n', $commit_text;
2402 my %co;
2404 if (! @commit_lines) {
2405 return;
2408 my $header = shift @commit_lines;
2409 if ($header !~ m/^[0-9a-fA-F]{40}/) {
2410 return;
2412 ($co{'id'}, my @parents) = split ' ', $header;
2413 while (my $line = shift @commit_lines) {
2414 last if $line eq "\n";
2415 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
2416 $co{'tree'} = $1;
2417 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
2418 push @parents, $1;
2419 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
2420 $co{'author'} = $1;
2421 $co{'author_epoch'} = $2;
2422 $co{'author_tz'} = $3;
2423 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
2424 $co{'author_name'} = $1;
2425 $co{'author_email'} = $2;
2426 } else {
2427 $co{'author_name'} = $co{'author'};
2429 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
2430 $co{'committer'} = $1;
2431 $co{'committer_epoch'} = $2;
2432 $co{'committer_tz'} = $3;
2433 $co{'committer_name'} = $co{'committer'};
2434 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
2435 $co{'committer_name'} = $1;
2436 $co{'committer_email'} = $2;
2437 } else {
2438 $co{'committer_name'} = $co{'committer'};
2442 if (!defined $co{'tree'}) {
2443 return;
2445 $co{'parents'} = \@parents;
2446 $co{'parent'} = $parents[0];
2448 foreach my $title (@commit_lines) {
2449 $title =~ s/^ //;
2450 if ($title ne "") {
2451 $co{'title'} = chop_str($title, 80, 5);
2452 # remove leading stuff of merges to make the interesting part visible
2453 if (length($title) > 50) {
2454 $title =~ s/^Automatic //;
2455 $title =~ s/^merge (of|with) /Merge ... /i;
2456 if (length($title) > 50) {
2457 $title =~ s/(http|rsync):\/\///;
2459 if (length($title) > 50) {
2460 $title =~ s/(master|www|rsync)\.//;
2462 if (length($title) > 50) {
2463 $title =~ s/kernel.org:?//;
2465 if (length($title) > 50) {
2466 $title =~ s/\/pub\/scm//;
2469 $co{'title_short'} = chop_str($title, 50, 5);
2470 last;
2473 if ($co{'title'} eq "") {
2474 $co{'title'} = $co{'title_short'} = '(no commit message)';
2476 # remove added spaces
2477 foreach my $line (@commit_lines) {
2478 $line =~ s/^ //;
2480 $co{'comment'} = \@commit_lines;
2482 my $age = time - $co{'committer_epoch'};
2483 $co{'age'} = $age;
2484 $co{'age_string'} = age_string($age);
2485 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
2486 if ($age > 60*60*24*7*2) {
2487 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2488 $co{'age_string_age'} = $co{'age_string'};
2489 } else {
2490 $co{'age_string_date'} = $co{'age_string'};
2491 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2493 return %co;
2496 sub parse_commit {
2497 my ($commit_id) = @_;
2498 # This currently supports arbitrary object names, so we
2499 # resolve it here.
2500 my $sha1 = git_get_sha1_or_die($commit_id, 'commit');
2502 # This formats commits slightly differently than the raw
2503 # cat-file, so we cannot use get_object here. This function
2504 # should be replaced by $repo->get_commit anyway.
2505 my $commit_text = $repo->cmd_output(
2506 cmd => [qw(rev-list --parents --header --max-count=1), $sha1, '--'],
2507 cache => 2);
2508 $commit_text =~ s/\x00$//;
2509 return parse_commit_text($commit_text, 1);
2512 sub parse_commits {
2513 my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
2514 # This currently supports arbitrary object names, so we
2515 # resolve it here.
2516 my $sha1 = git_get_sha1_or_die($commit_id);
2517 git_get_sha1_or_die("$sha1:$filename") if $filename; # check existence
2519 $maxcount ||= 1;
2520 $skip ||= 0;
2522 # TODO: Integrate this into Git::Repo, and get rid of @args
2523 # and @extra_options.
2524 my @raw_commits = split "\0", $repo->cmd_output(
2525 cmd => ['rev-list', '--header', @args, "--max-count=$maxcount",
2526 "--skip=$skip", @extra_options, $sha1, "--",
2527 $filename || ()],
2528 cache => 2);
2529 my @cos = map { { parse_commit_text($_) } } @raw_commits;
2530 return wantarray ? @cos : \@cos;
2533 # parse line of git-diff-tree "raw" output
2534 sub parse_difftree_raw_line {
2535 my $line = shift;
2536 my %res;
2538 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
2539 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
2540 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
2541 $res{'from_mode'} = $1;
2542 $res{'to_mode'} = $2;
2543 $res{'from_id'} = $3;
2544 $res{'to_id'} = $4;
2545 $res{'status'} = $5;
2546 $res{'similarity'} = $6;
2547 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
2548 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
2549 } else {
2550 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote($7);
2553 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
2554 # combined diff (for merge commit)
2555 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
2556 $res{'nparents'} = length($1);
2557 $res{'from_mode'} = [ split(' ', $2) ];
2558 $res{'to_mode'} = pop @{$res{'from_mode'}};
2559 $res{'from_id'} = [ split(' ', $3) ];
2560 $res{'to_id'} = pop @{$res{'from_id'}};
2561 $res{'status'} = [ split('', $4) ];
2562 $res{'to_file'} = unquote($5);
2564 # 'c512b523472485aef4fff9e57b229d9d243c967f'
2565 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
2566 $res{'commit'} = $1;
2569 return wantarray ? %res : \%res;
2572 # wrapper: return parsed line of git-diff-tree "raw" output
2573 # (the argument might be raw line, or parsed info)
2574 sub parsed_difftree_line {
2575 my $line_or_ref = shift;
2577 if (ref($line_or_ref) eq "HASH") {
2578 # pre-parsed (or generated by hand)
2579 return $line_or_ref;
2580 } else {
2581 return parse_difftree_raw_line($line_or_ref);
2585 # parse line of git-ls-tree output
2586 sub parse_ls_tree_line ($;%) {
2587 my $line = shift;
2588 my %opts = @_;
2589 my %res;
2591 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2592 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
2594 $res{'mode'} = $1;
2595 $res{'type'} = $2;
2596 $res{'hash'} = $3;
2597 if ($opts{'-z'}) {
2598 $res{'name'} = $4;
2599 } else {
2600 $res{'name'} = unquote($4);
2603 return wantarray ? %res : \%res;
2606 # generates _two_ hashes, references to which are passed as 2 and 3 argument
2607 sub parse_from_to_diffinfo {
2608 my ($diffinfo, $from, $to, @parents) = @_;
2610 if ($diffinfo->{'nparents'}) {
2611 # combined diff
2612 $from->{'file'} = [];
2613 $from->{'href'} = [];
2614 fill_from_file_info($diffinfo, @parents)
2615 unless exists $diffinfo->{'from_file'};
2616 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2617 $from->{'file'}[$i] =
2618 defined $diffinfo->{'from_file'}[$i] ?
2619 $diffinfo->{'from_file'}[$i] :
2620 $diffinfo->{'to_file'};
2621 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
2622 $from->{'href'}[$i] = href(action=>"blob",
2623 hash_base=>$parents[$i],
2624 hash=>$diffinfo->{'from_id'}[$i],
2625 file_name=>$from->{'file'}[$i]);
2626 } else {
2627 $from->{'href'}[$i] = undef;
2630 } else {
2631 # ordinary (not combined) diff
2632 $from->{'file'} = $diffinfo->{'from_file'};
2633 if ($diffinfo->{'status'} ne "A") { # not new (added) file
2634 $from->{'href'} = href(action=>"blob",
2635 $hash_parent && git_get_type($hash_parent) eq 'commit' ?
2636 (hash_base=>$hash_parent) : (),
2637 hash=>$diffinfo->{'from_id'},
2638 file_name=>$from->{'file'});
2639 } else {
2640 delete $from->{'href'};
2644 $to->{'file'} = $diffinfo->{'to_file'};
2645 if (!is_deleted($diffinfo)) { # file exists in result
2646 $to->{'href'} = href(action=>"blob",
2647 $hash && git_get_type($hash) eq 'commit' ?
2648 (hash_base=>$hash) : (),
2649 hash=>$diffinfo->{'to_id'},
2650 file_name=>$to->{'file'});
2651 } else {
2652 delete $to->{'href'};
2656 ## ......................................................................
2657 ## parse to array of hashes functions
2659 sub git_get_heads_list {
2660 my $limit = shift;
2661 my @headslist;
2663 my @lines = split "\n", $repo->cmd_output(
2664 cmd => ['for-each-ref', '--sort=-committerdate',
2665 $limit ? '--count='.($limit+1) : (),
2666 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
2667 'refs/heads'],
2668 cache => 1);
2669 for my $line (@lines) {
2670 my %ref_item;
2672 my ($refinfo, $committerinfo) = split(/\0/, $line);
2673 my ($hash, $name, $title) = split(' ', $refinfo, 3);
2674 my ($committer, $epoch, $tz) =
2675 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
2676 $ref_item{'fullname'} = $name;
2677 $name =~ s!^refs/heads/!!;
2679 $ref_item{'name'} = $name;
2680 $ref_item{'id'} = $hash;
2681 $ref_item{'title'} = $title || '(no commit message)';
2682 $ref_item{'epoch'} = $epoch;
2683 if ($epoch) {
2684 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2685 } else {
2686 $ref_item{'age'} = "unknown";
2689 push @headslist, \%ref_item;
2692 return wantarray ? @headslist : \@headslist;
2695 sub git_get_tags_list {
2696 my $limit = shift;
2697 my @tagslist;
2699 my @lines = split "\n", $repo->cmd_output(
2700 cmd => ['for-each-ref', '--sort=-creatordate',
2701 ($limit ? '--count='.($limit+1) : ()),
2702 '--format=%(objectname) %(objecttype) %(refname) '.
2703 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
2704 'refs/tags'],
2705 cache => 1);
2706 for my $line (@lines) {
2707 my %ref_item;
2709 my ($refinfo, $creatorinfo) = split(/\0/, $line);
2710 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
2711 my ($creator, $epoch, $tz) =
2712 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
2713 $ref_item{'fullname'} = $name;
2714 $name =~ s!^refs/tags/!!;
2716 $ref_item{'type'} = $type;
2717 $ref_item{'id'} = $id;
2718 $ref_item{'name'} = $name;
2719 if ($type eq "tag") {
2720 $ref_item{'subject'} = $title;
2721 $ref_item{'reftype'} = $reftype;
2722 $ref_item{'refid'} = $refid;
2723 } else {
2724 $ref_item{'reftype'} = $type;
2725 $ref_item{'refid'} = $id;
2728 if ($type eq "tag" || $type eq "commit") {
2729 $ref_item{'epoch'} = $epoch;
2730 if ($epoch) {
2731 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2732 } else {
2733 $ref_item{'age'} = "unknown";
2737 push @tagslist, \%ref_item;
2740 return wantarray ? @tagslist : \@tagslist;
2743 ## ----------------------------------------------------------------------
2744 ## filesystem-related functions
2746 sub get_file_owner {
2747 my $path = shift;
2749 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
2750 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
2751 if (!defined $gcos) {
2752 return undef;
2754 my $owner = $gcos;
2755 $owner =~ s/[,;].*$//;
2756 return to_utf8($owner);
2759 ## ......................................................................
2760 ## mimetype related functions
2762 sub mimetype_guess_file {
2763 my $filename = shift;
2764 my $mimemap = shift;
2765 -r $mimemap or return undef;
2767 my %mimemap;
2768 open(MIME, $mimemap) or return undef;
2769 while (<MIME>) {
2770 next if m/^#/; # skip comments
2771 my ($mime, $exts) = split(/\t+/);
2772 if (defined $exts) {
2773 my @exts = split(/\s+/, $exts);
2774 foreach my $ext (@exts) {
2775 $mimemap{$ext} = $mime;
2779 close(MIME);
2781 $filename =~ /\.([^.]*)$/;
2782 return $mimemap{$1};
2785 sub mimetype_guess {
2786 my $filename = shift;
2787 my $mime;
2788 $filename =~ /\./ or return undef;
2790 if ($mimetypes_file) {
2791 my $file = $mimetypes_file;
2792 if ($file !~ m!^/!) { # if it is relative path
2793 # it is relative to project
2794 $file = "$projectroot/$project/$file";
2796 $mime = mimetype_guess_file($filename, $file);
2798 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
2799 return $mime;
2802 # Replacement for (heuristic) -T operator. (perldoc -f -T)
2803 sub is_ascii_text {
2804 my $text = shift;
2805 return 1 if length($text) == 0;
2806 return ((grep { ord($_) > 127 } split('', $text)) / length $text) <= 0.3;
2809 # Determine the MIME type of a blob based on its file name ($filename)
2810 # and its first n bytes ($snippet).
2811 sub blob_mimetype {
2812 my $snippet = shift;
2813 my $filename = shift;
2815 if ($filename) {
2816 my $mime = mimetype_guess($filename);
2817 $mime and return $mime;
2820 if (is_ascii_text($snippet)) {
2821 return 'text/plain';
2822 } elsif (! $filename) {
2823 return 'application/octet-stream';
2824 } elsif ($filename =~ m/\.png$/i) {
2825 return 'image/png';
2826 } elsif ($filename =~ m/\.gif$/i) {
2827 return 'image/gif';
2828 } elsif ($filename =~ m/\.jpe?g$/i) {
2829 return 'image/jpeg';
2830 } else {
2831 return 'application/octet-stream';
2835 sub blob_contenttype {
2836 my ($snippet, $file_name, $type) = @_;
2838 $type ||= blob_mimetype($snippet, $file_name);
2839 if ($type eq 'text/plain' && defined $default_text_plain_charset) {
2840 $type .= "; charset=$default_text_plain_charset";
2843 return $type;
2846 ## ======================================================================
2847 ## functions printing HTML: header, footer, error page
2849 sub git_header_html {
2850 my $status = shift || "200 OK";
2851 my $expires = shift || 'now';
2853 my $title = "$site_name";
2854 if (defined $project) {
2855 $title .= " - " . to_utf8($project);
2856 if (defined $action) {
2857 $title .= "/$action";
2858 if (defined $file_name) {
2859 $title .= " - " . esc_path($file_name);
2860 if ($action eq "tree" && $file_name !~ m|/$|) {
2861 $title .= "/";
2866 my $content_type;
2867 # require explicit support from the UA if we are to send the page as
2868 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
2869 # we have to do this because MSIE sometimes globs '*/*', pretending to
2870 # support xhtml+xml but choking when it gets what it asked for.
2871 if (defined $cgi->http('HTTP_ACCEPT') &&
2872 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
2873 $cgi->Accept('application/xhtml+xml') != 0) {
2874 $content_type = 'application/xhtml+xml';
2875 } else {
2876 $content_type = 'text/html';
2878 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
2879 -status=> $status, -expires => $expires);
2880 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
2881 print <<EOF;
2882 <?xml version="1.0" encoding="utf-8"?>
2883 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2884 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
2885 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
2886 <!-- git core binaries version $git_version -->
2887 <head>
2888 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
2889 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
2890 <meta name="robots" content="index, nofollow"/>
2891 <title>$title</title>
2893 # print out each stylesheet that exist
2894 if (defined $stylesheet) {
2895 #provides backwards capability for those people who define style sheet in a config file
2896 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2897 } else {
2898 foreach my $stylesheet (@stylesheets) {
2899 next unless $stylesheet;
2900 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2903 if (defined $project) {
2904 my %href_params = get_feed_info();
2905 if (!exists $href_params{'-title'}) {
2906 $href_params{'-title'} = 'log';
2909 foreach my $format qw(RSS Atom) {
2910 my $type = lc($format);
2911 my %link_attr = (
2912 '-rel' => 'alternate',
2913 '-title' => "$project - $href_params{'-title'} - $format feed",
2914 '-type' => "application/$type+xml"
2917 $href_params{'action'} = $type;
2918 $link_attr{'-href'} = href(%href_params);
2919 print "<link ".
2920 "rel=\"$link_attr{'-rel'}\" ".
2921 "title=\"$link_attr{'-title'}\" ".
2922 "href=\"$link_attr{'-href'}\" ".
2923 "type=\"$link_attr{'-type'}\" ".
2924 "/>\n";
2926 $href_params{'extra_options'} = '--no-merges';
2927 $link_attr{'-href'} = href(%href_params);
2928 $link_attr{'-title'} .= ' (no merges)';
2929 print "<link ".
2930 "rel=\"$link_attr{'-rel'}\" ".
2931 "title=\"$link_attr{'-title'}\" ".
2932 "href=\"$link_attr{'-href'}\" ".
2933 "type=\"$link_attr{'-type'}\" ".
2934 "/>\n";
2937 } else {
2938 printf('<link rel="alternate" title="%s projects list" '.
2939 'href="%s" type="text/plain; charset=utf-8" />'."\n",
2940 $site_name, href(project=>undef, action=>"project_index"));
2941 printf('<link rel="alternate" title="%s projects feeds" '.
2942 'href="%s" type="text/x-opml" />'."\n",
2943 $site_name, href(project=>undef, action=>"opml"));
2945 if (defined $favicon) {
2946 print qq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);
2949 print "</head>\n" .
2950 "<body>\n";
2952 if ($site_header && -f $site_header) {
2953 open (my $fd, $site_header);
2954 print <$fd>;
2955 close $fd;
2958 print "<div class=\"page_header\">\n" .
2959 $cgi->a({-href => esc_url($logo_url),
2960 -title => $logo_label},
2961 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
2962 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
2963 if (defined $project) {
2964 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
2965 if (defined $action) {
2966 print " / $action";
2968 print "\n";
2970 print "</div>\n";
2972 if (defined $project && gitweb_check_feature('search') &&
2973 $repo->get_sha1('HEAD')) {
2974 if (!defined $searchtext) {
2975 $searchtext = "";
2977 my $search_hash;
2978 if (defined $hash_base) {
2979 $search_hash = $hash_base;
2980 } elsif (defined $hash) {
2981 $search_hash = $hash;
2982 } else {
2983 $search_hash = "HEAD";
2985 my $action = $my_uri;
2986 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
2987 if ($use_pathinfo) {
2988 $action .= "/".esc_url($project);
2990 print $cgi->startform(-method => "get", -action => $action) .
2991 "<div class=\"search\">\n" .
2992 (!$use_pathinfo &&
2993 $cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) . "\n") .
2994 $cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) . "\n" .
2995 $cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) . "\n" .
2996 $cgi->popup_menu(-name => 'st', -default => 'commit',
2997 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
2998 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
2999 " search:\n",
3000 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
3001 "<span title=\"Extended regular expression\">" .
3002 $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
3003 -checked => $search_use_regexp) .
3004 "</span>" .
3005 "</div>" .
3006 $cgi->end_form() . "\n";
3010 sub git_footer_html {
3011 my $feed_class = 'rss_logo';
3013 print "<div class=\"page_footer\">\n";
3014 if (defined $project) {
3015 my $descr = git_get_project_description($project);
3016 if (defined $descr) {
3017 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
3020 my %href_params = get_feed_info();
3021 if (!%href_params) {
3022 $feed_class .= ' generic';
3024 $href_params{'-title'} ||= 'log';
3026 foreach my $format qw(RSS Atom) {
3027 $href_params{'action'} = lc($format);
3028 print $cgi->a({-href => href(%href_params),
3029 -title => "$href_params{'-title'} $format feed",
3030 -class => $feed_class}, $format)."\n";
3033 } else {
3034 print $cgi->a({-href => href(project=>undef, action=>"opml"),
3035 -class => $feed_class}, "OPML") . " ";
3036 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
3037 -class => $feed_class}, "TXT") . "\n";
3039 print "</div>\n"; # class="page_footer"
3041 if ($page_info) {
3042 print "<div class=\"page_info\">\n";
3043 my $print_stats = sub {
3044 my ($name, $cache_exists) = (shift, shift);
3045 my %s = @_; # statistics hash
3046 if ($cache_exists) {
3047 my $gets = $s{hits} + $s{misses};
3048 print "<p>" . ucfirst($name) . ": " .
3049 "<b>$gets</b> gets " .
3050 "(<b>$s{hits}</b> hits + ".
3051 "<b>$s{misses}</b> misses); " .
3052 "<b>$s{sets}</b> sets, " .
3053 "<b>$s{failed_sets}</b> failed sets.</p>\n";
3054 if ($page_info == 2 && @{$s{get_list}}) {
3055 print "<pre class=\"cache_list\">";
3056 print join("\n",
3057 map(esc_html(join ',',
3058 map(defined $_ ? " $_" : '',
3059 @$_)),
3060 @{$s{get_list}}));
3061 print "</pre>\n";
3063 } else {
3064 print "<p><i>No $name.</i></p>\n";
3067 $print_stats->('main cache', $cache,
3068 %CachedRepo::cache_statistics);
3069 $print_stats->('large cache', $large_cache_root,
3070 %CachedRepo::large_cache_statistics);
3071 print "</div>\n"; # class="page_info"
3074 if (-f $site_footer) {
3075 open (my $fd, $site_footer);
3076 print <$fd>;
3077 close $fd;
3080 print "</body>\n" .
3081 "</html>";
3084 # die_error(<http_status_code>, <error_message>)
3085 # Example: die_error(404, 'Hash not found')
3086 # By convention, use the following status codes (as defined in RFC 2616):
3087 # 400: Invalid or missing CGI parameters, or
3088 # requested object exists but has wrong type.
3089 # 403: Requested feature (like "pickaxe" or "snapshot") not enabled on
3090 # this server or project.
3091 # 404: Requested object/revision/project doesn't exist.
3092 # 500: The server isn't configured properly, or
3093 # an internal error occurred (e.g. failed assertions caused by bugs), or
3094 # an unknown error occurred (e.g. the git binary died unexpectedly).
3095 sub die_error {
3096 my $status = shift || 500;
3097 my $error = shift || "Internal server error";
3099 my %http_responses = (400 => '400 Bad Request',
3100 403 => '403 Forbidden',
3101 404 => '404 Not Found',
3102 500 => '500 Internal Server Error');
3103 git_header_html($http_responses{$status});
3104 print <<EOF;
3105 <div class="page_body">
3106 <br /><br />
3107 $status - $error
3108 <br />
3109 </div>
3111 git_footer_html();
3112 exit;
3115 ## ----------------------------------------------------------------------
3116 ## functions printing or outputting HTML: navigation
3118 sub git_print_page_nav {
3119 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
3120 $extra = '' if !defined $extra; # pager or formats
3122 return unless $repo->get_sha1('HEAD'); # no navigation for empty repos
3124 my @navs = qw(summary shortlog log commit commitdiff tree);
3125 if ($suppress) {
3126 @navs = grep { $_ ne $suppress } @navs;
3129 my %arg = map { $_ => {action=>$_} } @navs;
3130 if (defined $head) {
3131 for (qw(commit commitdiff)) {
3132 $arg{$_}{'hash'} = $head;
3134 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
3135 for (qw(shortlog log)) {
3136 $arg{$_}{'hash'} = $head;
3140 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
3141 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
3143 print "<div class=\"page_nav\">\n" .
3144 (join " | ",
3145 map { $_ eq $current ?
3146 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
3147 } @navs);
3148 print "<br/>\n$extra<br/>\n" .
3149 "</div>\n";
3152 sub format_paging_nav {
3153 my ($action, $hash, $head, $page, $has_next_link) = @_;
3154 my $paging_nav;
3157 if ($hash ne $head || $page) {
3158 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
3159 } else {
3160 $paging_nav .= "HEAD";
3163 if ($page > 0) {
3164 $paging_nav .= " &sdot; " .
3165 $cgi->a({-href => href(-replay=>1, page=>$page-1),
3166 -accesskey => "p", -title => "Alt-p"}, "prev");
3167 } else {
3168 $paging_nav .= " &sdot; prev";
3171 if ($has_next_link) {
3172 $paging_nav .= " &sdot; " .
3173 $cgi->a({-href => href(-replay=>1, page=>$page+1),
3174 -accesskey => "n", -title => "Alt-n"}, "next");
3175 } else {
3176 $paging_nav .= " &sdot; next";
3179 return $paging_nav;
3182 ## ......................................................................
3183 ## functions printing or outputting HTML: div
3185 sub git_print_header_div {
3186 my ($action, $title, $hash, $hash_base) = @_;
3187 my %args = ();
3189 $args{'action'} = $action;
3190 $args{'hash'} = $hash if $hash;
3191 $args{'hash_base'} = $hash_base if $hash_base;
3193 print "<div class=\"header\">\n" .
3194 $cgi->a({-href => href(%args), -class => "title"},
3195 $title ? $title : $action) .
3196 "\n</div>\n";
3199 #sub git_print_authorship (\%) {
3200 sub git_print_authorship {
3201 my $co = shift;
3203 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
3204 print "<div class=\"author_date\">" .
3205 esc_html($co->{'author_name'}) .
3206 " [$ad{'rfc2822'}";
3207 if ($ad{'hour_local'} < 6) {
3208 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
3209 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3210 } else {
3211 printf(" (%02d:%02d %s)",
3212 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3214 print "]</div>\n";
3217 sub git_print_page_path {
3218 my $name = shift;
3219 my $type = shift;
3220 my $hb = shift;
3223 print "<div class=\"page_path\">";
3224 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
3225 -title => 'tree root'}, to_utf8("[$project]"));
3226 print " / ";
3227 if (defined $name) {
3228 my @dirname = split '/', $name;
3229 my $basename = pop @dirname;
3230 my $fullname = '';
3232 foreach my $dir (@dirname) {
3233 $fullname .= ($fullname ? '/' : '') . $dir;
3234 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
3235 hash_base=>$hb),
3236 -title => $fullname}, esc_path($dir));
3237 print " / ";
3239 if (defined $type && $type eq 'blob') {
3240 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
3241 hash_base=>$hb),
3242 -title => $name}, esc_path($basename));
3243 } elsif (defined $type && $type eq 'tree') {
3244 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
3245 hash_base=>$hb),
3246 -title => $name}, esc_path($basename));
3247 print " / ";
3248 } else {
3249 print esc_path($basename);
3252 print "<br/></div>\n";
3255 # sub git_print_log (\@;%) {
3256 sub git_print_log ($;%) {
3257 my $log = shift;
3258 my %opts = @_;
3260 if ($opts{'-remove_title'}) {
3261 # remove title, i.e. first line of log
3262 shift @$log;
3264 # remove leading empty lines
3265 while (defined $log->[0] && $log->[0] eq "") {
3266 shift @$log;
3269 # print log
3270 my $signoff = 0;
3271 my $empty = 0;
3272 foreach my $line (@$log) {
3273 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
3274 $signoff = 1;
3275 $empty = 0;
3276 if (! $opts{'-remove_signoff'}) {
3277 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
3278 next;
3279 } else {
3280 # remove signoff lines
3281 next;
3283 } else {
3284 $signoff = 0;
3287 # print only one empty line
3288 # do not print empty line after signoff
3289 if ($line eq "") {
3290 next if ($empty || $signoff);
3291 $empty = 1;
3292 } else {
3293 $empty = 0;
3296 print format_log_line_html($line) . "<br/>\n";
3299 if ($opts{'-final_empty_line'}) {
3300 # end with single empty line
3301 print "<br/>\n" unless $empty;
3305 # given link target, and the directory (basedir) the link is in,
3306 # return target of link relative to top directory (top tree);
3307 # return undef if it is not possible (including absolute links).
3308 sub normalize_link_target {
3309 my ($link_target, $basedir, $hash_base) = @_;
3311 # we can normalize symlink target only if $hash_base is provided
3312 return unless $hash_base;
3314 # absolute symlinks (beginning with '/') cannot be normalized
3315 return if (substr($link_target, 0, 1) eq '/');
3317 # normalize link target to path from top (root) tree (dir)
3318 my $path;
3319 if ($basedir) {
3320 $path = $basedir . '/' . $link_target;
3321 } else {
3322 # we are in top (root) tree (dir)
3323 $path = $link_target;
3326 # remove //, /./, and /../
3327 my @path_parts;
3328 foreach my $part (split('/', $path)) {
3329 # discard '.' and ''
3330 next if (!$part || $part eq '.');
3331 # handle '..'
3332 if ($part eq '..') {
3333 if (@path_parts) {
3334 pop @path_parts;
3335 } else {
3336 # link leads outside repository (outside top dir)
3337 return;
3339 } else {
3340 push @path_parts, $part;
3343 $path = join('/', @path_parts);
3345 return $path;
3348 # print tree entry (row of git_tree), but without encompassing <tr> element
3349 sub git_print_tree_entry {
3350 my ($t, $basedir, $hash_base, $have_blame) = @_;
3352 my %base_key = ();
3353 $base_key{'hash_base'} = $hash_base if defined $hash_base;
3355 # The format of a table row is: mode list link. Where mode is
3356 # the mode of the entry, list is the name of the entry, an href,
3357 # and link is the action links of the entry.
3359 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
3360 if ($t->{'type'} eq "blob") {
3361 print "<td class=\"list\">" .
3362 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
3363 file_name=>"$basedir$t->{'name'}", %base_key),
3364 -class => "list"}, esc_path($t->{'name'}));
3365 if (S_ISLNK(oct $t->{'mode'})) {
3366 my $link_target = $repo->get_object($t->{'hash'});
3367 if ($link_target) {
3368 my $norm_target = normalize_link_target($link_target, $basedir, $hash_base);
3369 if (defined $norm_target) {
3370 print " -> " .
3371 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
3372 file_name=>$norm_target),
3373 -title => $norm_target}, esc_path($link_target));
3374 } else {
3375 print " -> " . esc_path($link_target);
3379 print "</td>\n";
3380 print "<td class=\"link\">";
3381 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
3382 file_name=>"$basedir$t->{'name'}", %base_key)},
3383 "blob");
3384 if ($have_blame) {
3385 print " | " .
3386 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
3387 file_name=>"$basedir$t->{'name'}", %base_key)},
3388 "blame");
3390 if (defined $hash_base) {
3391 print " | " .
3392 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3393 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
3394 "history");
3396 print " | " .
3397 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
3398 file_name=>"$basedir$t->{'name'}")},
3399 "raw");
3400 print "</td>\n";
3402 } elsif ($t->{'type'} eq "tree") {
3403 print "<td class=\"list\">";
3404 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
3405 file_name=>"$basedir$t->{'name'}", %base_key)},
3406 esc_path($t->{'name'}));
3407 print "</td>\n";
3408 print "<td class=\"link\">";
3409 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
3410 file_name=>"$basedir$t->{'name'}", %base_key)},
3411 "tree");
3412 if (defined $hash_base) {
3413 print " | " .
3414 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3415 file_name=>"$basedir$t->{'name'}")},
3416 "history");
3418 print "</td>\n";
3419 } else {
3420 # unknown object: we can only present history for it
3421 # (this includes 'commit' object, i.e. submodule support)
3422 print "<td class=\"list\">" .
3423 esc_path($t->{'name'}) .
3424 "</td>\n";
3425 print "<td class=\"link\">";
3426 if (defined $hash_base) {
3427 print $cgi->a({-href => href(action=>"history",
3428 hash_base=>$hash_base,
3429 file_name=>"$basedir$t->{'name'}")},
3430 "history");
3432 print "</td>\n";
3436 ## ......................................................................
3437 ## functions printing large fragments of HTML
3439 # get pre-image filenames for merge (combined) diff
3440 sub fill_from_file_info {
3441 my ($diff, @parents) = @_;
3443 $diff->{'from_file'} = [ ];
3444 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
3445 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
3446 if ($diff->{'status'}[$i] eq 'R' ||
3447 $diff->{'status'}[$i] eq 'C') {
3448 $diff->{'from_file'}[$i] =
3449 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
3453 return $diff;
3456 # is current raw difftree line of file deletion
3457 sub is_deleted {
3458 my $diffinfo = shift;
3460 return $diffinfo->{'to_id'} eq ('0' x 40);
3463 # does patch correspond to [previous] difftree raw line
3464 # $diffinfo - hashref of parsed raw diff format
3465 # $patchinfo - hashref of parsed patch diff format
3466 # (the same keys as in $diffinfo)
3467 sub is_patch_split {
3468 my ($diffinfo, $patchinfo) = @_;
3470 return defined $diffinfo && defined $patchinfo
3471 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
3475 sub git_difftree_body {
3476 my ($difftree, $hash, @parents) = @_;
3477 my ($parent) = $parents[0];
3478 my ($have_blame) = gitweb_check_feature('blame');
3479 print "<div class=\"list_head\">\n";
3480 if ($#{$difftree} > 10) {
3481 print(($#{$difftree} + 1) . " files changed:\n");
3483 print "</div>\n";
3485 print "<table class=\"" .
3486 (@parents > 1 ? "combined " : "") .
3487 "diff_tree\">\n";
3489 # header only for combined diff in 'commitdiff' view
3490 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
3491 if ($has_header) {
3492 # table header
3493 print "<thead><tr>\n" .
3494 "<th></th><th></th>\n"; # filename, patchN link
3495 for (my $i = 0; $i < @parents; $i++) {
3496 my $par = $parents[$i];
3497 print "<th>" .
3498 $cgi->a({-href => href(action=>"commitdiff",
3499 hash=>$hash, hash_parent=>$par),
3500 -title => 'commitdiff to parent number ' .
3501 ($i+1) . ': ' . substr($par,0,7)},
3502 $i+1) .
3503 "&nbsp;</th>\n";
3505 print "</tr></thead>\n<tbody>\n";
3508 my $alternate = 1;
3509 my $patchno = 0;
3510 foreach my $line (@{$difftree}) {
3511 my $diff = parsed_difftree_line($line);
3513 if ($alternate) {
3514 print "<tr class=\"dark\">\n";
3515 } else {
3516 print "<tr class=\"light\">\n";
3518 $alternate ^= 1;
3520 if (exists $diff->{'nparents'}) { # combined diff
3522 fill_from_file_info($diff, @parents)
3523 unless exists $diff->{'from_file'};
3525 if (!is_deleted($diff)) {
3526 # file exists in the result (child) commit
3527 print "<td>" .
3528 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3529 file_name=>$diff->{'to_file'},
3530 hash_base=>$hash),
3531 -class => "list"}, esc_path($diff->{'to_file'})) .
3532 "</td>\n";
3533 } else {
3534 print "<td>" .
3535 esc_path($diff->{'to_file'}) .
3536 "</td>\n";
3539 if ($action eq 'commitdiff') {
3540 # link to patch
3541 $patchno++;
3542 print "<td class=\"link\">" .
3543 $cgi->a({-href => "#patch$patchno"}, "patch") .
3544 " | " .
3545 "</td>\n";
3548 my $has_history = 0;
3549 my $not_deleted = 0;
3550 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
3551 my $hash_parent = $parents[$i];
3552 my $from_hash = $diff->{'from_id'}[$i];
3553 my $from_path = $diff->{'from_file'}[$i];
3554 my $status = $diff->{'status'}[$i];
3556 $has_history ||= ($status ne 'A');
3557 $not_deleted ||= ($status ne 'D');
3559 if ($status eq 'A') {
3560 print "<td class=\"link\" align=\"right\"> | </td>\n";
3561 } elsif ($status eq 'D') {
3562 print "<td class=\"link\">" .
3563 $cgi->a({-href => href(action=>"blob",
3564 hash_base=>$hash,
3565 hash=>$from_hash,
3566 file_name=>$from_path)},
3567 "blob" . ($i+1)) .
3568 " | </td>\n";
3569 } else {
3570 if ($diff->{'to_id'} eq $from_hash) {
3571 print "<td class=\"link nochange\">";
3572 } else {
3573 print "<td class=\"link\">";
3575 print $cgi->a({-href => href(action=>"blobdiff",
3576 hash=>$diff->{'to_id'},
3577 hash_parent=>$from_hash,
3578 hash_base=>$hash,
3579 hash_parent_base=>$hash_parent,
3580 file_name=>$diff->{'to_file'},
3581 file_parent=>$from_path)},
3582 "diff" . ($i+1)) .
3583 " | </td>\n";
3587 print "<td class=\"link\">";
3588 if ($not_deleted) {
3589 print $cgi->a({-href => href(action=>"blob",
3590 hash=>$diff->{'to_id'},
3591 file_name=>$diff->{'to_file'},
3592 hash_base=>$hash)},
3593 "blob");
3594 print " | " if ($has_history);
3596 if ($has_history) {
3597 print $cgi->a({-href => href(action=>"history",
3598 file_name=>$diff->{'to_file'},
3599 hash_base=>$hash)},
3600 "history");
3602 print "</td>\n";
3604 print "</tr>\n";
3605 next; # instead of 'else' clause, to avoid extra indent
3607 # else ordinary diff
3609 my ($to_mode_oct, $to_mode_str, $to_file_type);
3610 my ($from_mode_oct, $from_mode_str, $from_file_type);
3611 if ($diff->{'to_mode'} ne ('0' x 6)) {
3612 $to_mode_oct = oct $diff->{'to_mode'};
3613 if (S_ISREG($to_mode_oct)) { # only for regular file
3614 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
3616 $to_file_type = file_type($diff->{'to_mode'});
3618 if ($diff->{'from_mode'} ne ('0' x 6)) {
3619 $from_mode_oct = oct $diff->{'from_mode'};
3620 if (S_ISREG($to_mode_oct)) { # only for regular file
3621 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
3623 $from_file_type = file_type($diff->{'from_mode'});
3626 if ($diff->{'status'} eq "A") { # created
3627 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
3628 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
3629 $mode_chng .= "]</span>";
3630 print "<td>";
3631 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3632 hash_base=>$hash, file_name=>$diff->{'file'}),
3633 -class => "list"}, esc_path($diff->{'file'}));
3634 print "</td>\n";
3635 print "<td>$mode_chng</td>\n";
3636 print "<td class=\"link\">";
3637 if ($action eq 'commitdiff') {
3638 # link to patch
3639 $patchno++;
3640 print $cgi->a({-href => "#patch$patchno"}, "patch");
3641 print " | ";
3643 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3644 hash_base=>$hash, file_name=>$diff->{'file'})},
3645 "blob");
3646 print "</td>\n";
3648 } elsif ($diff->{'status'} eq "D") { # deleted
3649 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
3650 print "<td>";
3651 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3652 hash_base=>$parent, file_name=>$diff->{'file'}),
3653 -class => "list"}, esc_path($diff->{'file'}));
3654 print "</td>\n";
3655 print "<td>$mode_chng</td>\n";
3656 print "<td class=\"link\">";
3657 if ($action eq 'commitdiff') {
3658 # link to patch
3659 $patchno++;
3660 print $cgi->a({-href => "#patch$patchno"}, "patch");
3661 print " | ";
3663 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3664 hash_base=>$parent, file_name=>$diff->{'file'})},
3665 "blob") . " | ";
3666 if ($have_blame) {
3667 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
3668 file_name=>$diff->{'file'})},
3669 "blame") . " | ";
3671 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
3672 file_name=>$diff->{'file'})},
3673 "history");
3674 print "</td>\n";
3676 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
3677 my $mode_chnge = "";
3678 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3679 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
3680 if ($from_file_type ne $to_file_type) {
3681 $mode_chnge .= " from $from_file_type to $to_file_type";
3683 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
3684 if ($from_mode_str && $to_mode_str) {
3685 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
3686 } elsif ($to_mode_str) {
3687 $mode_chnge .= " mode: $to_mode_str";
3690 $mode_chnge .= "]</span>\n";
3692 print "<td>";
3693 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3694 hash_base=>$hash, file_name=>$diff->{'file'}),
3695 -class => "list"}, esc_path($diff->{'file'}));
3696 print "</td>\n";
3697 print "<td>$mode_chnge</td>\n";
3698 print "<td class=\"link\">";
3699 if ($action eq 'commitdiff') {
3700 # link to patch
3701 $patchno++;
3702 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3703 " | ";
3704 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
3705 # "commit" view and modified file (not onlu mode changed)
3706 print $cgi->a({-href => href(action=>"blobdiff",
3707 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
3708 hash_base=>$hash, hash_parent_base=>$parent,
3709 file_name=>$diff->{'file'})},
3710 "diff") .
3711 " | ";
3713 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3714 hash_base=>$hash, file_name=>$diff->{'file'})},
3715 "blob") . " | ";
3716 if ($have_blame) {
3717 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
3718 file_name=>$diff->{'file'})},
3719 "blame") . " | ";
3721 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
3722 file_name=>$diff->{'file'})},
3723 "history");
3724 print "</td>\n";
3726 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
3727 my %status_name = ('R' => 'moved', 'C' => 'copied');
3728 my $nstatus = $status_name{$diff->{'status'}};
3729 my $mode_chng = "";
3730 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3731 # mode also for directories, so we cannot use $to_mode_str
3732 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
3734 print "<td>" .
3735 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
3736 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
3737 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
3738 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
3739 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
3740 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
3741 -class => "list"}, esc_path($diff->{'from_file'})) .
3742 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
3743 "<td class=\"link\">";
3744 if ($action eq 'commitdiff') {
3745 # link to patch
3746 $patchno++;
3747 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3748 " | ";
3749 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
3750 # "commit" view and modified file (not only pure rename or copy)
3751 print $cgi->a({-href => href(action=>"blobdiff",
3752 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
3753 hash_base=>$hash, hash_parent_base=>$parent,
3754 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
3755 "diff") .
3756 " | ";
3758 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3759 hash_base=>$parent, file_name=>$diff->{'to_file'})},
3760 "blob") . " | ";
3761 if ($have_blame) {
3762 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
3763 file_name=>$diff->{'to_file'})},
3764 "blame") . " | ";
3766 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
3767 file_name=>$diff->{'to_file'})},
3768 "history");
3769 print "</td>\n";
3771 } # we should not encounter Unmerged (U) or Unknown (X) status
3772 print "</tr>\n";
3774 print "</tbody>" if $has_header;
3775 print "</table>\n";
3778 sub git_patchset_body {
3779 my ($read, $difftree, $hash, @hash_parents) = @_;
3780 my ($hash_parent) = $hash_parents[0];
3782 my $is_combined = (@hash_parents > 1);
3783 my $patch_idx = 0;
3784 my $patch_number = 0;
3785 my $patch_line;
3786 my $diffinfo;
3787 my $to_name;
3788 my (%from, %to);
3790 print "<div class=\"patchset\">\n";
3792 # skip to first patch
3793 while ($patch_line = $read->()) {
3794 chomp $patch_line;
3796 last if ($patch_line =~ m/^diff /);
3799 PATCH:
3800 while ($patch_line) {
3802 # parse "git diff" header line
3803 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
3804 # $1 is from_name, which we do not use
3805 $to_name = unquote($2);
3806 $to_name =~ s!^b/!!;
3807 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
3808 # $1 is 'cc' or 'combined', which we do not use
3809 $to_name = unquote($2);
3810 } else {
3811 $to_name = undef;
3814 # check if current patch belong to current raw line
3815 # and parse raw git-diff line if needed
3816 if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
3817 # this is continuation of a split patch
3818 print "<div class=\"patch cont\">\n";
3819 } else {
3820 # advance raw git-diff output if needed
3821 $patch_idx++