Git 2.3.8
[git/mjg.git] / perl / Git / SVN / Ra.pm
blob32e2f19ea41b2dfed735cf1fa5e1fa49f3fed915
1 package Git::SVN::Ra;
2 use vars qw/@ISA $config_dir $_ignore_refs_regex $_log_window_size/;
3 use strict;
4 use warnings;
5 use Memoize;
6 use SVN::Client;
7 use Git::SVN::Utils qw(
8 canonicalize_url
9 canonicalize_path
10 add_path_to_url
13 use SVN::Ra;
14 BEGIN {
15 @ISA = qw(SVN::Ra);
18 my ($ra_invalid, $can_do_switch, %ignored_err, $RA);
20 BEGIN {
21 # enforce temporary pool usage for some simple functions
22 no strict 'refs';
23 for my $f (qw/rev_proplist get_latest_revnum get_uuid get_repos_root
24 get_file/) {
25 my $SUPER = "SUPER::$f";
26 *$f = sub {
27 my $self = shift;
28 my $pool = SVN::Pool->new;
29 my @ret = $self->$SUPER(@_,$pool);
30 $pool->clear;
31 wantarray ? @ret : $ret[0];
36 # serf has a bug that leads to a coredump upon termination if the
37 # remote access object is left around (not fixed yet in serf 1.3.1).
38 # Explicitly free it to work around the issue.
39 END {
40 $RA = undef;
41 $ra_invalid = 1;
44 sub _auth_providers () {
45 my @rv = (
46 SVN::Client::get_simple_provider(),
47 SVN::Client::get_ssl_server_trust_file_provider(),
48 SVN::Client::get_simple_prompt_provider(
49 \&Git::SVN::Prompt::simple, 2),
50 SVN::Client::get_ssl_client_cert_file_provider(),
51 SVN::Client::get_ssl_client_cert_prompt_provider(
52 \&Git::SVN::Prompt::ssl_client_cert, 2),
53 SVN::Client::get_ssl_client_cert_pw_file_provider(),
54 SVN::Client::get_ssl_client_cert_pw_prompt_provider(
55 \&Git::SVN::Prompt::ssl_client_cert_pw, 2),
56 SVN::Client::get_username_provider(),
57 SVN::Client::get_ssl_server_trust_prompt_provider(
58 \&Git::SVN::Prompt::ssl_server_trust),
59 SVN::Client::get_username_prompt_provider(
60 \&Git::SVN::Prompt::username, 2)
63 # earlier 1.6.x versions would segfault, and <= 1.5.x didn't have
64 # this function
65 if (::compare_svn_version('1.6.15') >= 0) {
66 my $config = SVN::Core::config_get_config($config_dir);
67 my ($p, @a);
68 # config_get_config returns all config files from
69 # ~/.subversion, auth_get_platform_specific_client_providers
70 # just wants the config "file".
71 @a = ($config->{'config'}, undef);
72 $p = SVN::Core::auth_get_platform_specific_client_providers(@a);
73 # Insert the return value from
74 # auth_get_platform_specific_providers
75 unshift @rv, @$p;
77 \@rv;
80 sub prepare_config_once {
81 SVN::_Core::svn_config_ensure($config_dir, undef);
82 my ($baton, $callbacks) = SVN::Core::auth_open_helper(_auth_providers);
83 my $config = SVN::Core::config_get_config($config_dir);
84 my $dont_store_passwords = 1;
85 my $conf_t = $config->{'config'};
87 no warnings 'once';
88 # The usage of $SVN::_Core::SVN_CONFIG_* variables
89 # produces warnings that variables are used only once.
90 # I had not found the better way to shut them up, so
91 # the warnings of type 'once' are disabled in this block.
92 if (SVN::_Core::svn_config_get_bool($conf_t,
93 $SVN::_Core::SVN_CONFIG_SECTION_AUTH,
94 $SVN::_Core::SVN_CONFIG_OPTION_STORE_PASSWORDS,
95 1) == 0) {
96 SVN::_Core::svn_auth_set_parameter($baton,
97 $SVN::_Core::SVN_AUTH_PARAM_DONT_STORE_PASSWORDS,
98 bless (\$dont_store_passwords, "_p_void"));
100 if (SVN::_Core::svn_config_get_bool($conf_t,
101 $SVN::_Core::SVN_CONFIG_SECTION_AUTH,
102 $SVN::_Core::SVN_CONFIG_OPTION_STORE_AUTH_CREDS,
103 1) == 0) {
104 $Git::SVN::Prompt::_no_auth_cache = 1;
107 return ($config, $baton, $callbacks);
108 } # no warnings 'once'
110 INIT {
111 Memoize::memoize '_auth_providers';
112 Memoize::memoize 'prepare_config_once';
115 sub new {
116 my ($class, $url) = @_;
117 $url = canonicalize_url($url);
118 return $RA if ($RA && $RA->url eq $url);
120 ::_req_svn();
122 $RA = undef;
123 my ($config, $baton, $callbacks) = prepare_config_once();
124 my $self = SVN::Ra->new(url => $url, auth => $baton,
125 config => $config,
126 pool => SVN::Pool->new,
127 auth_provider_callbacks => $callbacks);
128 $RA = bless $self, $class;
130 # Make sure its canonicalized
131 $self->url($url);
132 $self->{svn_path} = $url;
133 $self->{repos_root} = $self->get_repos_root;
134 $self->{svn_path} =~ s#^\Q$self->{repos_root}\E(/|$)##;
135 $self->{cache} = { check_path => { r => 0, data => {} },
136 get_dir => { r => 0, data => {} } };
138 return $RA;
141 sub url {
142 my $self = shift;
144 if (@_) {
145 my $url = shift;
146 $self->{url} = canonicalize_url($url);
147 return;
150 return $self->{url};
153 sub check_path {
154 my ($self, $path, $r) = @_;
155 my $cache = $self->{cache}->{check_path};
156 if ($r == $cache->{r} && exists $cache->{data}->{$path}) {
157 return $cache->{data}->{$path};
159 my $pool = SVN::Pool->new;
160 my $t = $self->SUPER::check_path($path, $r, $pool);
161 $pool->clear;
162 if ($r != $cache->{r}) {
163 %{$cache->{data}} = ();
164 $cache->{r} = $r;
166 $cache->{data}->{$path} = $t;
169 sub get_dir {
170 my ($self, $dir, $r) = @_;
171 my $cache = $self->{cache}->{get_dir};
172 if ($r == $cache->{r}) {
173 if (my $x = $cache->{data}->{$dir}) {
174 return wantarray ? @$x : $x->[0];
177 my $pool = SVN::Pool->new;
178 my ($d, undef, $props);
180 if (::compare_svn_version('1.4.0') >= 0) {
181 # n.b. in addition to being potentially more efficient,
182 # this works around what appears to be a bug in some
183 # SVN 1.8 versions
184 my $kind = 1; # SVN_DIRENT_KIND
185 ($d, undef, $props) = $self->get_dir2($dir, $r, $kind, $pool);
186 } else {
187 ($d, undef, $props) = $self->SUPER::get_dir($dir, $r, $pool);
189 my %dirents = map { $_ => { kind => $d->{$_}->kind } } keys %$d;
190 $pool->clear;
191 if ($r != $cache->{r}) {
192 %{$cache->{data}} = ();
193 $cache->{r} = $r;
195 $cache->{data}->{$dir} = [ \%dirents, $r, $props ];
196 wantarray ? (\%dirents, $r, $props) : \%dirents;
199 # get_log(paths, start, end, limit,
200 # discover_changed_paths, strict_node_history, receiver)
201 sub get_log {
202 my ($self, @args) = @_;
203 my $pool = SVN::Pool->new;
205 # svn_log_changed_path_t objects passed to get_log are likely to be
206 # overwritten even if only the refs are copied to an external variable,
207 # so we should dup the structures in their entirety. Using an
208 # externally passed pool (instead of our temporary and quickly cleared
209 # pool in Git::SVN::Ra) does not help matters at all...
210 my $receiver = pop @args;
211 my $prefix = "/".$self->{svn_path};
212 $prefix =~ s#/+($)##;
213 my $prefix_regex = qr#^\Q$prefix\E#;
214 push(@args, sub {
215 my ($paths) = $_[0];
216 return &$receiver(@_) unless $paths;
217 $_[0] = ();
218 foreach my $p (keys %$paths) {
219 my $i = $paths->{$p};
220 # Make path relative to our url, not repos_root
221 $p =~ s/$prefix_regex//;
222 my %s = map { $_ => $i->$_; }
223 qw/copyfrom_path copyfrom_rev action/;
224 if ($s{'copyfrom_path'}) {
225 $s{'copyfrom_path'} =~ s/$prefix_regex//;
226 $s{'copyfrom_path'} = canonicalize_path($s{'copyfrom_path'});
228 $_[0]{$p} = \%s;
230 &$receiver(@_);
234 # the limit parameter was not supported in SVN 1.1.x, so we
235 # drop it. Therefore, the receiver callback passed to it
236 # is made aware of this limitation by being wrapped if
237 # the limit passed to is being wrapped.
238 if (::compare_svn_version('1.2.0') <= 0) {
239 my $limit = splice(@args, 3, 1);
240 if ($limit > 0) {
241 my $receiver = pop @args;
242 push(@args, sub { &$receiver(@_) if (--$limit >= 0) });
245 my $ret = $self->SUPER::get_log(@args, $pool);
246 $pool->clear;
247 $ret;
250 sub trees_match {
251 my ($self, $url1, $rev1, $url2, $rev2) = @_;
252 my $ctx = SVN::Client->new(auth => _auth_providers);
253 my $out = IO::File->new_tmpfile;
255 # older SVN (1.1.x) doesn't take $pool as the last parameter for
256 # $ctx->diff(), so we'll create a default one
257 my $pool = SVN::Pool->new_default_sub;
259 $ra_invalid = 1; # this will open a new SVN::Ra connection to $url1
260 $ctx->diff([], $url1, $rev1, $url2, $rev2, 1, 1, 0, $out, $out);
261 $out->flush;
262 my $ret = (($out->stat)[7] == 0);
263 close $out or croak $!;
265 $ret;
268 sub get_commit_editor {
269 my ($self, $log, $cb, $pool) = @_;
271 my @lock = (::compare_svn_version('1.2.0') >= 0) ? (undef, 0) : ();
272 $self->SUPER::get_commit_editor($log, $cb, @lock, $pool);
275 sub gs_do_update {
276 my ($self, $rev_a, $rev_b, $gs, $editor) = @_;
277 my $new = ($rev_a == $rev_b);
278 my $path = $gs->path;
280 if ($new && -e $gs->{index}) {
281 unlink $gs->{index} or die
282 "Couldn't unlink index: $gs->{index}: $!\n";
284 my $pool = SVN::Pool->new;
285 $editor->set_path_strip($path);
286 my (@pc) = split m#/#, $path;
287 my $reporter = $self->do_update($rev_b, (@pc ? shift @pc : ''),
288 1, $editor, $pool);
289 my @lock = (::compare_svn_version('1.2.0') >= 0) ? (undef) : ();
291 # Since we can't rely on svn_ra_reparent being available, we'll
292 # just have to do some magic with set_path to make it so
293 # we only want a partial path.
294 my $sp = '';
295 my $final = join('/', @pc);
296 while (@pc) {
297 $reporter->set_path($sp, $rev_b, 0, @lock, $pool);
298 $sp .= '/' if length $sp;
299 $sp .= shift @pc;
301 die "BUG: '$sp' != '$final'\n" if ($sp ne $final);
303 $reporter->set_path($sp, $rev_a, $new, @lock, $pool);
305 $reporter->finish_report($pool);
306 $pool->clear;
307 $editor->{git_commit_ok};
310 # this requires SVN 1.4.3 or later (do_switch didn't work before 1.4.3, and
311 # svn_ra_reparent didn't work before 1.4)
312 sub gs_do_switch {
313 my ($self, $rev_a, $rev_b, $gs, $url_b, $editor) = @_;
314 my $path = $gs->path;
315 my $pool = SVN::Pool->new;
317 my $old_url = $self->url;
318 my $full_url = add_path_to_url( $self->url, $path );
319 my ($ra, $reparented);
321 if ($old_url =~ m#^svn(\+\w+)?://# ||
322 ($full_url =~ m#^https?://# &&
323 canonicalize_url($full_url) ne $full_url)) {
324 $_[0] = undef;
325 $self = undef;
326 $RA = undef;
327 $ra = Git::SVN::Ra->new($full_url);
328 $ra_invalid = 1;
329 } elsif ($old_url ne $full_url) {
330 SVN::_Ra::svn_ra_reparent(
331 $self->{session},
332 canonicalize_url($full_url),
333 $pool
335 $self->url($full_url);
336 $reparented = 1;
339 $ra ||= $self;
340 $url_b = canonicalize_url($url_b);
341 my $reporter = $ra->do_switch($rev_b, '', 1, $url_b, $editor, $pool);
342 my @lock = (::compare_svn_version('1.2.0') >= 0) ? (undef) : ();
343 $reporter->set_path('', $rev_a, 0, @lock, $pool);
344 $reporter->finish_report($pool);
346 if ($reparented) {
347 SVN::_Ra::svn_ra_reparent($self->{session}, $old_url, $pool);
348 $self->url($old_url);
351 $pool->clear;
352 $editor->{git_commit_ok};
355 sub longest_common_path {
356 my ($gsv, $globs) = @_;
357 my %common;
358 my $common_max = scalar @$gsv;
360 foreach my $gs (@$gsv) {
361 my @tmp = split m#/#, $gs->path;
362 my $p = '';
363 foreach (@tmp) {
364 $p .= length($p) ? "/$_" : $_;
365 $common{$p} ||= 0;
366 $common{$p}++;
369 $globs ||= [];
370 $common_max += scalar @$globs;
371 foreach my $glob (@$globs) {
372 my @tmp = split m#/#, $glob->{path}->{left};
373 my $p = '';
374 foreach (@tmp) {
375 $p .= length($p) ? "/$_" : $_;
376 $common{$p} ||= 0;
377 $common{$p}++;
381 my $longest_path = '';
382 foreach (sort {length $b <=> length $a} keys %common) {
383 if ($common{$_} == $common_max) {
384 $longest_path = $_;
385 last;
388 $longest_path;
391 sub gs_fetch_loop_common {
392 my ($self, $base, $head, $gsv, $globs) = @_;
393 return if ($base > $head);
394 # Make sure the cat_blob open2 FileHandle is created before calling
395 # SVN::Pool::new_default so that it does not incorrectly end up in the pool.
396 $::_repository->_open_cat_blob_if_needed;
397 my $gpool = SVN::Pool->new_default;
398 my $ra_url = $self->url;
399 my $reload_ra = sub {
400 $_[0] = undef;
401 $self = undef;
402 $RA = undef;
403 $gpool->clear;
404 $self = Git::SVN::Ra->new($ra_url);
405 $ra_invalid = undef;
407 my $inc = $_log_window_size;
408 my ($min, $max) = ($base, $head < $base + $inc ? $head : $base + $inc);
409 my $longest_path = longest_common_path($gsv, $globs);
410 my $find_trailing_edge;
411 while (1) {
412 my %revs;
413 my $err;
414 my $err_handler = $SVN::Error::handler;
415 $SVN::Error::handler = sub {
416 ($err) = @_;
417 skip_unknown_revs($err);
419 sub _cb {
420 my ($paths, $r, $author, $date, $log) = @_;
421 [ $paths,
422 { author => $author, date => $date, log => $log } ];
424 $self->get_log([$longest_path], $min, $max, 0, 1, 1,
425 sub { $revs{$_[1]} = _cb(@_) });
426 if ($err) {
427 print "Checked through r$max\r";
428 } else {
429 $find_trailing_edge = 1;
431 if ($err and $find_trailing_edge) {
432 print STDERR "Path '$longest_path' ",
433 "was probably deleted:\n",
434 $err->expanded_message,
435 "\nWill attempt to follow ",
436 "revisions r$min .. r$max ",
437 "committed before the deletion\n";
438 my $hi = $max;
439 while (--$hi >= $min) {
440 my $ok;
441 $self->get_log([$longest_path], $min, $hi,
442 0, 1, 1, sub {
443 $ok = $_[1];
444 $revs{$_[1]} = _cb(@_) });
445 if ($ok) {
446 print STDERR "r$min .. r$ok OK\n";
447 last;
450 $find_trailing_edge = 0;
452 $SVN::Error::handler = $err_handler;
454 my %exists = map { $_->path => $_ } @$gsv;
455 foreach my $r (sort {$a <=> $b} keys %revs) {
456 my ($paths, $logged) = @{delete $revs{$r}};
458 foreach my $gs ($self->match_globs(\%exists, $paths,
459 $globs, $r)) {
460 if ($gs->rev_map_max >= $r) {
461 next;
463 next unless $gs->match_paths($paths, $r);
464 $gs->{logged_rev_props} = $logged;
465 if (my $last_commit = $gs->last_commit) {
466 $gs->assert_index_clean($last_commit);
468 my $log_entry = $gs->do_fetch($paths, $r);
469 if ($log_entry) {
470 $gs->do_git_commit($log_entry);
472 $Git::SVN::INDEX_FILES{$gs->{index}} = 1;
474 foreach my $g (@$globs) {
475 my $k = "svn-remote.$g->{remote}." .
476 "$g->{t}-maxRev";
477 Git::SVN::tmp_config($k, $r);
479 $reload_ra->() if $ra_invalid;
481 # pre-fill the .rev_db since it'll eventually get filled in
482 # with '0' x40 if something new gets committed
483 foreach my $gs (@$gsv) {
484 next if $gs->rev_map_max >= $max;
485 next if defined $gs->rev_map_get($max);
486 $gs->rev_map_set($max, 0 x40);
488 foreach my $g (@$globs) {
489 my $k = "svn-remote.$g->{remote}.$g->{t}-maxRev";
490 Git::SVN::tmp_config($k, $max);
492 last if $max >= $head;
493 $min = $max + 1;
494 $max += $inc;
495 $max = $head if ($max > $head);
497 $reload_ra->();
499 Git::SVN::gc();
502 sub get_dir_globbed {
503 my ($self, $left, $depth, $r) = @_;
505 my @x = eval { $self->get_dir($left, $r) };
506 return unless scalar @x == 3;
507 my $dirents = $x[0];
508 my @finalents;
509 foreach my $de (keys %$dirents) {
510 next if $dirents->{$de}->{kind} != $SVN::Node::dir;
511 if ($depth > 1) {
512 my @args = ("$left/$de", $depth - 1, $r);
513 foreach my $dir ($self->get_dir_globbed(@args)) {
514 push @finalents, "$de/$dir";
516 } else {
517 push @finalents, $de;
520 @finalents;
523 # return value: 0 -- don't ignore, 1 -- ignore
524 sub is_ref_ignored {
525 my ($g, $p) = @_;
526 my $refname = $g->{ref}->full_path($p);
527 return 1 if defined($g->{ignore_refs_regex}) &&
528 $refname =~ m!$g->{ignore_refs_regex}!;
529 return 0 unless defined($_ignore_refs_regex);
530 return 1 if $refname =~ m!$_ignore_refs_regex!o;
531 return 0;
534 sub match_globs {
535 my ($self, $exists, $paths, $globs, $r) = @_;
537 sub get_dir_check {
538 my ($self, $exists, $g, $r) = @_;
540 my @dirs = $self->get_dir_globbed($g->{path}->{left},
541 $g->{path}->{depth},
542 $r);
544 foreach my $de (@dirs) {
545 my $p = $g->{path}->full_path($de);
546 next if $exists->{$p};
547 next if (length $g->{path}->{right} &&
548 ($self->check_path($p, $r) !=
549 $SVN::Node::dir));
550 next unless $p =~ /$g->{path}->{regex}/;
551 $exists->{$p} = Git::SVN->init($self->url, $p, undef,
552 $g->{ref}->full_path($de), 1);
555 foreach my $g (@$globs) {
556 if (my $path = $paths->{"/$g->{path}->{left}"}) {
557 if ($path->{action} =~ /^[AR]$/) {
558 get_dir_check($self, $exists, $g, $r);
561 foreach (keys %$paths) {
562 if (/$g->{path}->{left_regex}/ &&
563 !/$g->{path}->{regex}/) {
564 next if $paths->{$_}->{action} !~ /^[AR]$/;
565 get_dir_check($self, $exists, $g, $r);
567 next unless /$g->{path}->{regex}/;
568 my $p = $1;
569 my $pathname = $g->{path}->full_path($p);
570 next if is_ref_ignored($g, $p);
571 next if $exists->{$pathname};
572 next if ($self->check_path($pathname, $r) !=
573 $SVN::Node::dir);
574 $exists->{$pathname} = Git::SVN->init(
575 $self->url, $pathname, undef,
576 $g->{ref}->full_path($p), 1);
578 my $c = '';
579 foreach (split m#/#, $g->{path}->{left}) {
580 $c .= "/$_";
581 next unless ($paths->{$c} &&
582 ($paths->{$c}->{action} =~ /^[AR]$/));
583 get_dir_check($self, $exists, $g, $r);
586 values %$exists;
589 sub minimize_url {
590 my ($self) = @_;
591 return $self->url if ($self->url eq $self->{repos_root});
592 my $url = $self->{repos_root};
593 my @components = split(m!/!, $self->{svn_path});
594 my $c = '';
595 do {
596 $url = add_path_to_url($url, $c);
597 eval {
598 my $ra = (ref $self)->new($url);
599 my $latest = $ra->get_latest_revnum;
600 $ra->get_log("", $latest, 0, 1, 0, 1, sub {});
602 } while ($@ && ($c = shift @components));
604 return canonicalize_url($url);
607 sub can_do_switch {
608 my $self = shift;
609 unless (defined $can_do_switch) {
610 my $pool = SVN::Pool->new;
611 my $rep = eval {
612 $self->do_switch(1, '', 0, $self->url,
613 SVN::Delta::Editor->new, $pool);
615 if ($@) {
616 $can_do_switch = 0;
617 } else {
618 $rep->abort_report($pool);
619 $can_do_switch = 1;
621 $pool->clear;
623 $can_do_switch;
626 sub skip_unknown_revs {
627 my ($err) = @_;
628 my $errno = $err->apr_err();
629 # Maybe the branch we're tracking didn't
630 # exist when the repo started, so it's
631 # not an error if it doesn't, just continue
633 # Wonderfully consistent library, eh?
634 # 160013 - svn:// and file://
635 # 175002 - http(s)://
636 # 175007 - http(s):// (this repo required authorization, too...)
637 # More codes may be discovered later...
638 if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
639 my $err_key = $err->expanded_message;
640 # revision numbers change every time, filter them out
641 $err_key =~ s/\d+/\0/g;
642 $err_key = "$errno\0$err_key";
643 unless ($ignored_err{$err_key}) {
644 warn "W: Ignoring error from SVN, path probably ",
645 "does not exist: ($errno): ",
646 $err->expanded_message,"\n";
647 warn "W: Do not be alarmed at the above message ",
648 "git-svn is just searching aggressively for ",
649 "old history.\n",
650 "This may take a while on large repositories\n";
651 $ignored_err{$err_key} = 1;
653 return;
655 die "Error from SVN, ($errno): ", $err->expanded_message,"\n";
659 __END__
661 =head1 NAME
663 Git::SVN::Ra - Subversion remote access functions for git-svn
665 =head1 SYNOPSIS
667 use Git::SVN::Ra;
669 my $ra = Git::SVN::Ra->new($branchurl);
670 my ($dirents, $fetched_revnum, $props) =
671 $ra->get_dir('.', $SVN::Core::INVALID_REVNUM);
673 =head1 DESCRIPTION
675 This is a wrapper around the L<SVN::Ra> module for use by B<git-svn>.
676 It fills in some default parameters (such as the authentication
677 scheme), smooths over incompatibilities between libsvn versions, adds
678 caching, and implements some functions specific to B<git-svn>.
680 Do not use it unless you are developing git-svn. The interface will
681 change as git-svn evolves.
683 =head1 DEPENDENCIES
685 Subversion perl bindings,
686 L<Git::SVN>.
688 C<Git::SVN::Ra> has not been tested using callers other than
689 B<git-svn> itself.
691 =head1 SEE ALSO
693 L<SVN::Ra>.
695 =head1 INCOMPATIBILITIES
697 None reported.
699 =head1 BUGS
701 None.