2 use vars qw
/@ISA $config_dir $_ignore_refs_regex $_log_window_size/;
6 use Git
::SVN
::Utils
qw(
17 my ($ra_invalid, $can_do_switch, %ignored_err, $RA);
20 # enforce temporary pool usage for some simple functions
22 for my $f (qw
/rev_proplist get_latest_revnum get_uuid get_repos_root
24 my $SUPER = "SUPER::$f";
27 my $pool = SVN
::Pool
->new;
28 my @ret = $self->$SUPER(@_,$pool);
30 wantarray ?
@ret : $ret[0];
35 # serf has a bug that leads to a coredump upon termination if the
36 # remote access object is left around (not fixed yet in serf 1.3.1).
37 # Explicitly free it to work around the issue.
43 sub _auth_providers
() {
45 SVN
::Client
::get_simple_provider
(),
46 SVN
::Client
::get_ssl_server_trust_file_provider
(),
47 SVN
::Client
::get_simple_prompt_provider
(
48 \
&Git
::SVN
::Prompt
::simple
, 2),
49 SVN
::Client
::get_ssl_client_cert_file_provider
(),
50 SVN
::Client
::get_ssl_client_cert_prompt_provider
(
51 \
&Git
::SVN
::Prompt
::ssl_client_cert
, 2),
52 SVN
::Client
::get_ssl_client_cert_pw_file_provider
(),
53 SVN
::Client
::get_ssl_client_cert_pw_prompt_provider
(
54 \
&Git
::SVN
::Prompt
::ssl_client_cert_pw
, 2),
55 SVN
::Client
::get_username_provider
(),
56 SVN
::Client
::get_ssl_server_trust_prompt_provider
(
57 \
&Git
::SVN
::Prompt
::ssl_server_trust
),
58 SVN
::Client
::get_username_prompt_provider
(
59 \
&Git
::SVN
::Prompt
::username
, 2)
62 # earlier 1.6.x versions would segfault, and <= 1.5.x didn't have
64 if (::compare_svn_version
('1.6.15') >= 0) {
65 my $config = SVN
::Core
::config_get_config
($config_dir);
67 # config_get_config returns all config files from
68 # ~/.subversion, auth_get_platform_specific_client_providers
69 # just wants the config "file".
70 @a = ($config->{'config'}, undef);
71 $p = SVN
::Core
::auth_get_platform_specific_client_providers
(@a);
72 # Insert the return value from
73 # auth_get_platform_specific_providers
81 my ($class, $url) = @_;
82 $url = canonicalize_url
($url);
83 return $RA if ($RA && $RA->url eq $url);
87 SVN
::_Core
::svn_config_ensure
($config_dir, undef);
88 my ($baton, $callbacks) = SVN
::Core
::auth_open_helper
(_auth_providers
);
89 my $config = SVN
::Core
::config_get_config
($config_dir);
91 my $dont_store_passwords = 1;
92 my $conf_t = ${$config}{'config'};
95 # The usage of $SVN::_Core::SVN_CONFIG_* variables
96 # produces warnings that variables are used only once.
97 # I had not found the better way to shut them up, so
98 # the warnings of type 'once' are disabled in this block.
99 if (SVN
::_Core
::svn_config_get_bool
($conf_t,
100 $SVN::_Core
::SVN_CONFIG_SECTION_AUTH
,
101 $SVN::_Core
::SVN_CONFIG_OPTION_STORE_PASSWORDS
,
103 SVN
::_Core
::svn_auth_set_parameter
($baton,
104 $SVN::_Core
::SVN_AUTH_PARAM_DONT_STORE_PASSWORDS
,
105 bless (\
$dont_store_passwords, "_p_void"));
107 if (SVN
::_Core
::svn_config_get_bool
($conf_t,
108 $SVN::_Core
::SVN_CONFIG_SECTION_AUTH
,
109 $SVN::_Core
::SVN_CONFIG_OPTION_STORE_AUTH_CREDS
,
111 $Git::SVN
::Prompt
::_no_auth_cache
= 1;
113 } # no warnings 'once'
115 my $self = SVN
::Ra
->new(url
=> $url, auth
=> $baton,
117 pool
=> SVN
::Pool
->new,
118 auth_provider_callbacks
=> $callbacks);
119 $RA = bless $self, $class;
121 # Make sure its canonicalized
123 $self->{svn_path
} = $url;
124 $self->{repos_root
} = $self->get_repos_root;
125 $self->{svn_path
} =~ s
#^\Q$self->{repos_root}\E(/|$)##;
126 $self->{cache
} = { check_path
=> { r
=> 0, data
=> {} },
127 get_dir
=> { r
=> 0, data
=> {} } };
137 $self->{url
} = canonicalize_url
($url);
145 my ($self, $path, $r) = @_;
146 my $cache = $self->{cache
}->{check_path
};
147 if ($r == $cache->{r
} && exists $cache->{data
}->{$path}) {
148 return $cache->{data
}->{$path};
150 my $pool = SVN
::Pool
->new;
151 my $t = $self->SUPER::check_path
($path, $r, $pool);
153 if ($r != $cache->{r
}) {
154 %{$cache->{data
}} = ();
157 $cache->{data
}->{$path} = $t;
161 my ($self, $dir, $r) = @_;
162 my $cache = $self->{cache
}->{get_dir
};
163 if ($r == $cache->{r
}) {
164 if (my $x = $cache->{data
}->{$dir}) {
165 return wantarray ? @
$x : $x->[0];
168 my $pool = SVN
::Pool
->new;
169 my ($d, undef, $props) = $self->SUPER::get_dir
($dir, $r, $pool);
170 my %dirents = map { $_ => { kind
=> $d->{$_}->kind } } keys %$d;
172 if ($r != $cache->{r
}) {
173 %{$cache->{data
}} = ();
176 $cache->{data
}->{$dir} = [ \
%dirents, $r, $props ];
177 wantarray ?
(\
%dirents, $r, $props) : \
%dirents;
180 # get_log(paths, start, end, limit,
181 # discover_changed_paths, strict_node_history, receiver)
183 my ($self, @args) = @_;
184 my $pool = SVN
::Pool
->new;
186 # svn_log_changed_path_t objects passed to get_log are likely to be
187 # overwritten even if only the refs are copied to an external variable,
188 # so we should dup the structures in their entirety. Using an
189 # externally passed pool (instead of our temporary and quickly cleared
190 # pool in Git::SVN::Ra) does not help matters at all...
191 my $receiver = pop @args;
192 my $prefix = "/".$self->{svn_path
};
193 $prefix =~ s
#/+($)##;
194 my $prefix_regex = qr
#^\Q$prefix\E#;
197 return &$receiver(@_) unless $paths;
199 foreach my $p (keys %$paths) {
200 my $i = $paths->{$p};
201 # Make path relative to our url, not repos_root
202 $p =~ s/$prefix_regex//;
203 my %s = map { $_ => $i->$_; }
204 qw
/copyfrom_path copyfrom_rev action/;
205 if ($s{'copyfrom_path'}) {
206 $s{'copyfrom_path'} =~ s/$prefix_regex//;
207 $s{'copyfrom_path'} = canonicalize_path
($s{'copyfrom_path'});
215 # the limit parameter was not supported in SVN 1.1.x, so we
216 # drop it. Therefore, the receiver callback passed to it
217 # is made aware of this limitation by being wrapped if
218 # the limit passed to is being wrapped.
219 if (::compare_svn_version
('1.2.0') <= 0) {
220 my $limit = splice(@args, 3, 1);
222 my $receiver = pop @args;
223 push(@args, sub { &$receiver(@_) if (--$limit >= 0) });
226 my $ret = $self->SUPER::get_log
(@args, $pool);
232 my ($self, $url1, $rev1, $url2, $rev2) = @_;
233 my $ctx = SVN
::Client
->new(auth
=> _auth_providers
);
234 my $out = IO
::File
->new_tmpfile;
236 # older SVN (1.1.x) doesn't take $pool as the last parameter for
237 # $ctx->diff(), so we'll create a default one
238 my $pool = SVN
::Pool
->new_default_sub;
240 $ra_invalid = 1; # this will open a new SVN::Ra connection to $url1
241 $ctx->diff([], $url1, $rev1, $url2, $rev2, 1, 1, 0, $out, $out);
243 my $ret = (($out->stat)[7] == 0);
244 close $out or croak
$!;
249 sub get_commit_editor
{
250 my ($self, $log, $cb, $pool) = @_;
252 my @lock = (::compare_svn_version
('1.2.0') >= 0) ?
(undef, 0) : ();
253 $self->SUPER::get_commit_editor
($log, $cb, @lock, $pool);
257 my ($self, $rev_a, $rev_b, $gs, $editor) = @_;
258 my $new = ($rev_a == $rev_b);
259 my $path = $gs->path;
261 if ($new && -e
$gs->{index}) {
262 unlink $gs->{index} or die
263 "Couldn't unlink index: $gs->{index}: $!\n";
265 my $pool = SVN
::Pool
->new;
266 $editor->set_path_strip($path);
267 my (@pc) = split m
#/#, $path;
268 my $reporter = $self->do_update($rev_b, (@pc ?
shift @pc : ''),
270 my @lock = (::compare_svn_version
('1.2.0') >= 0) ?
(undef) : ();
272 # Since we can't rely on svn_ra_reparent being available, we'll
273 # just have to do some magic with set_path to make it so
274 # we only want a partial path.
276 my $final = join('/', @pc);
278 $reporter->set_path($sp, $rev_b, 0, @lock, $pool);
279 $sp .= '/' if length $sp;
282 die "BUG: '$sp' != '$final'\n" if ($sp ne $final);
284 $reporter->set_path($sp, $rev_a, $new, @lock, $pool);
286 $reporter->finish_report($pool);
288 $editor->{git_commit_ok
};
291 # this requires SVN 1.4.3 or later (do_switch didn't work before 1.4.3, and
292 # svn_ra_reparent didn't work before 1.4)
294 my ($self, $rev_a, $rev_b, $gs, $url_b, $editor) = @_;
295 my $path = $gs->path;
296 my $pool = SVN
::Pool
->new;
298 my $old_url = $self->url;
299 my $full_url = add_path_to_url
( $self->url, $path );
300 my ($ra, $reparented);
302 if ($old_url =~ m
#^svn(\+\w+)?://# ||
303 ($full_url =~ m
#^https?://# &&
304 canonicalize_url
($full_url) ne $full_url)) {
308 $ra = Git
::SVN
::Ra
->new($full_url);
310 } elsif ($old_url ne $full_url) {
311 SVN
::_Ra
::svn_ra_reparent
(
313 canonicalize_url
($full_url),
316 $self->url($full_url);
321 $url_b = canonicalize_url
($url_b);
322 my $reporter = $ra->do_switch($rev_b, '', 1, $url_b, $editor, $pool);
323 my @lock = (::compare_svn_version
('1.2.0') >= 0) ?
(undef) : ();
324 $reporter->set_path('', $rev_a, 0, @lock, $pool);
325 $reporter->finish_report($pool);
328 SVN
::_Ra
::svn_ra_reparent
($self->{session
}, $old_url, $pool);
329 $self->url($old_url);
333 $editor->{git_commit_ok
};
336 sub longest_common_path
{
337 my ($gsv, $globs) = @_;
339 my $common_max = scalar @
$gsv;
341 foreach my $gs (@
$gsv) {
342 my @tmp = split m
#/#, $gs->path;
345 $p .= length($p) ?
"/$_" : $_;
351 $common_max += scalar @
$globs;
352 foreach my $glob (@
$globs) {
353 my @tmp = split m
#/#, $glob->{path}->{left};
356 $p .= length($p) ?
"/$_" : $_;
362 my $longest_path = '';
363 foreach (sort {length $b <=> length $a} keys %common) {
364 if ($common{$_} == $common_max) {
372 sub gs_fetch_loop_common
{
373 my ($self, $base, $head, $gsv, $globs) = @_;
374 return if ($base > $head);
375 my $gpool = SVN
::Pool
->new_default;
376 my $ra_url = $self->url;
377 my $reload_ra = sub {
382 $self = Git
::SVN
::Ra
->new($ra_url);
385 my $inc = $_log_window_size;
386 my ($min, $max) = ($base, $head < $base + $inc ?
$head : $base + $inc);
387 my $longest_path = longest_common_path
($gsv, $globs);
388 my $find_trailing_edge;
392 my $err_handler = $SVN::Error
::handler
;
393 $SVN::Error
::handler
= sub {
395 skip_unknown_revs
($err);
398 my ($paths, $r, $author, $date, $log) = @_;
400 { author
=> $author, date
=> $date, log => $log } ];
402 $self->get_log([$longest_path], $min, $max, 0, 1, 1,
403 sub { $revs{$_[1]} = _cb
(@_) });
405 print "Checked through r$max\r";
407 $find_trailing_edge = 1;
409 if ($err and $find_trailing_edge) {
410 print STDERR
"Path '$longest_path' ",
411 "was probably deleted:\n",
412 $err->expanded_message,
413 "\nWill attempt to follow ",
414 "revisions r$min .. r$max ",
415 "committed before the deletion\n";
417 while (--$hi >= $min) {
419 $self->get_log([$longest_path], $min, $hi,
422 $revs{$_[1]} = _cb
(@_) });
424 print STDERR
"r$min .. r$ok OK\n";
428 $find_trailing_edge = 0;
430 $SVN::Error
::handler
= $err_handler;
432 my %exists = map { $_->path => $_ } @
$gsv;
433 foreach my $r (sort {$a <=> $b} keys %revs) {
434 my ($paths, $logged) = @
{$revs{$r}};
436 foreach my $gs ($self->match_globs(\
%exists, $paths,
438 if ($gs->rev_map_max >= $r) {
441 next unless $gs->match_paths($paths, $r);
442 $gs->{logged_rev_props
} = $logged;
443 if (my $last_commit = $gs->last_commit) {
444 $gs->assert_index_clean($last_commit);
446 my $log_entry = $gs->do_fetch($paths, $r);
448 $gs->do_git_commit($log_entry);
450 $Git::SVN
::INDEX_FILES
{$gs->{index}} = 1;
452 foreach my $g (@
$globs) {
453 my $k = "svn-remote.$g->{remote}." .
455 Git
::SVN
::tmp_config
($k, $r);
457 $reload_ra->() if $ra_invalid;
459 # pre-fill the .rev_db since it'll eventually get filled in
460 # with '0' x40 if something new gets committed
461 foreach my $gs (@
$gsv) {
462 next if $gs->rev_map_max >= $max;
463 next if defined $gs->rev_map_get($max);
464 $gs->rev_map_set($max, 0 x40
);
466 foreach my $g (@
$globs) {
467 my $k = "svn-remote.$g->{remote}.$g->{t}-maxRev";
468 Git
::SVN
::tmp_config
($k, $max);
470 last if $max >= $head;
473 $max = $head if ($max > $head);
480 sub get_dir_globbed
{
481 my ($self, $left, $depth, $r) = @_;
483 my @x = eval { $self->get_dir($left, $r) };
484 return unless scalar @x == 3;
487 foreach my $de (keys %$dirents) {
488 next if $dirents->{$de}->{kind
} != $SVN::Node
::dir
;
490 my @args = ("$left/$de", $depth - 1, $r);
491 foreach my $dir ($self->get_dir_globbed(@args)) {
492 push @finalents, "$de/$dir";
495 push @finalents, $de;
501 # return value: 0 -- don't ignore, 1 -- ignore
504 my $refname = $g->{ref}->full_path($p);
505 return 1 if defined($g->{ignore_refs_regex
}) &&
506 $refname =~ m!$g->{ignore_refs_regex}!;
507 return 0 unless defined($_ignore_refs_regex);
508 return 1 if $refname =~ m!$_ignore_refs_regex!o;
513 my ($self, $exists, $paths, $globs, $r) = @_;
516 my ($self, $exists, $g, $r) = @_;
518 my @dirs = $self->get_dir_globbed($g->{path
}->{left
},
522 foreach my $de (@dirs) {
523 my $p = $g->{path
}->full_path($de);
524 next if $exists->{$p};
525 next if (length $g->{path
}->{right
} &&
526 ($self->check_path($p, $r) !=
528 next unless $p =~ /$g->{path}->{regex}/;
529 $exists->{$p} = Git
::SVN
->init($self->url, $p, undef,
530 $g->{ref}->full_path($de), 1);
533 foreach my $g (@
$globs) {
534 if (my $path = $paths->{"/$g->{path}->{left}"}) {
535 if ($path->{action
} =~ /^[AR]$/) {
536 get_dir_check
($self, $exists, $g, $r);
539 foreach (keys %$paths) {
540 if (/$g->{path}->{left_regex}/ &&
541 !/$g->{path}->{regex}/) {
542 next if $paths->{$_}->{action
} !~ /^[AR]$/;
543 get_dir_check
($self, $exists, $g, $r);
545 next unless /$g->{path}->{regex}/;
547 my $pathname = $g->{path
}->full_path($p);
548 next if is_ref_ignored
($g, $p);
549 next if $exists->{$pathname};
550 next if ($self->check_path($pathname, $r) !=
552 $exists->{$pathname} = Git
::SVN
->init(
553 $self->url, $pathname, undef,
554 $g->{ref}->full_path($p), 1);
557 foreach (split m
#/#, $g->{path}->{left}) {
559 next unless ($paths->{$c} &&
560 ($paths->{$c}->{action
} =~ /^[AR]$/));
561 get_dir_check
($self, $exists, $g, $r);
569 return $self->url if ($self->url eq $self->{repos_root
});
570 my $url = $self->{repos_root
};
571 my @components = split(m!/!, $self->{svn_path
});
574 $url = add_path_to_url
($url, $c);
576 my $ra = (ref $self)->new($url);
577 my $latest = $ra->get_latest_revnum;
578 $ra->get_log("", $latest, 0, 1, 0, 1, sub {});
580 } while ($@
&& ($c = shift @components));
582 return canonicalize_url
($url);
587 unless (defined $can_do_switch) {
588 my $pool = SVN
::Pool
->new;
590 $self->do_switch(1, '', 0, $self->url,
591 SVN
::Delta
::Editor
->new, $pool);
596 $rep->abort_report($pool);
604 sub skip_unknown_revs
{
606 my $errno = $err->apr_err();
607 # Maybe the branch we're tracking didn't
608 # exist when the repo started, so it's
609 # not an error if it doesn't, just continue
611 # Wonderfully consistent library, eh?
612 # 160013 - svn:// and file://
613 # 175002 - http(s)://
614 # 175007 - http(s):// (this repo required authorization, too...)
615 # More codes may be discovered later...
616 if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
617 my $err_key = $err->expanded_message;
618 # revision numbers change every time, filter them out
619 $err_key =~ s/\d+/\0/g;
620 $err_key = "$errno\0$err_key";
621 unless ($ignored_err{$err_key}) {
622 warn "W: Ignoring error from SVN, path probably ",
623 "does not exist: ($errno): ",
624 $err->expanded_message,"\n";
625 warn "W: Do not be alarmed at the above message ",
626 "git-svn is just searching aggressively for ",
628 "This may take a while on large repositories\n";
629 $ignored_err{$err_key} = 1;
633 die "Error from SVN, ($errno): ", $err->expanded_message,"\n";
641 Git::SVN::Ra - Subversion remote access functions for git-svn
647 my $ra = Git::SVN::Ra->new($branchurl);
648 my ($dirents, $fetched_revnum, $props) =
649 $ra->get_dir('.', $SVN::Core::INVALID_REVNUM);
653 This is a wrapper around the L<SVN::Ra> module for use by B<git-svn>.
654 It fills in some default parameters (such as the authentication
655 scheme), smooths over incompatibilities between libsvn versions, adds
656 caching, and implements some functions specific to B<git-svn>.
658 Do not use it unless you are developing git-svn. The interface will
659 change as git-svn evolves.
663 Subversion perl bindings,
666 C<Git::SVN::Ra> has not been tested using callers other than
673 =head1 INCOMPATIBILITIES