2 use vars qw
/@ISA $config_dir $_ignore_refs_regex $_log_window_size/;
6 use Git
::SVN
::Utils
qw(
15 my ($ra_invalid, $can_do_switch, %ignored_err, $RA);
18 # enforce temporary pool usage for some simple functions
20 for my $f (qw
/rev_proplist get_latest_revnum get_uuid get_repos_root
22 my $SUPER = "SUPER::$f";
25 my $pool = SVN
::Pool
->new;
26 my @ret = $self->$SUPER(@_,$pool);
28 wantarray ?
@ret : $ret[0];
33 sub _auth_providers
() {
35 SVN
::Client
::get_simple_provider
(),
36 SVN
::Client
::get_ssl_server_trust_file_provider
(),
37 SVN
::Client
::get_simple_prompt_provider
(
38 \
&Git
::SVN
::Prompt
::simple
, 2),
39 SVN
::Client
::get_ssl_client_cert_file_provider
(),
40 SVN
::Client
::get_ssl_client_cert_prompt_provider
(
41 \
&Git
::SVN
::Prompt
::ssl_client_cert
, 2),
42 SVN
::Client
::get_ssl_client_cert_pw_file_provider
(),
43 SVN
::Client
::get_ssl_client_cert_pw_prompt_provider
(
44 \
&Git
::SVN
::Prompt
::ssl_client_cert_pw
, 2),
45 SVN
::Client
::get_username_provider
(),
46 SVN
::Client
::get_ssl_server_trust_prompt_provider
(
47 \
&Git
::SVN
::Prompt
::ssl_server_trust
),
48 SVN
::Client
::get_username_prompt_provider
(
49 \
&Git
::SVN
::Prompt
::username
, 2)
52 # earlier 1.6.x versions would segfault, and <= 1.5.x didn't have
54 if (::compare_svn_version
('1.6.15') >= 0) {
55 my $config = SVN
::Core
::config_get_config
($config_dir);
57 # config_get_config returns all config files from
58 # ~/.subversion, auth_get_platform_specific_client_providers
59 # just wants the config "file".
60 @a = ($config->{'config'}, undef);
61 $p = SVN
::Core
::auth_get_platform_specific_client_providers
(@a);
62 # Insert the return value from
63 # auth_get_platform_specific_providers
72 foreach (split m{/}, $uri) {
73 s/([^~\w.%+-]|%(?![a-fA-F0-9]{2}))/sprintf("%%%02X",ord($1))/eg;
81 if ($url =~ m
#^(https?)://([^/]+)(.*)$#) {
82 my ($scheme, $domain, $uri) = ($1, $2, escape_uri_only
($3));
83 $url = "$scheme://$domain$uri";
89 my ($class, $url) = @_;
91 return $RA if ($RA && $RA->url eq $url);
95 SVN
::_Core
::svn_config_ensure
($config_dir, undef);
96 my ($baton, $callbacks) = SVN
::Core
::auth_open_helper
(_auth_providers
);
97 my $config = SVN
::Core
::config_get_config
($config_dir);
99 my $dont_store_passwords = 1;
100 my $conf_t = ${$config}{'config'};
103 # The usage of $SVN::_Core::SVN_CONFIG_* variables
104 # produces warnings that variables are used only once.
105 # I had not found the better way to shut them up, so
106 # the warnings of type 'once' are disabled in this block.
107 if (SVN
::_Core
::svn_config_get_bool
($conf_t,
108 $SVN::_Core
::SVN_CONFIG_SECTION_AUTH
,
109 $SVN::_Core
::SVN_CONFIG_OPTION_STORE_PASSWORDS
,
111 SVN
::_Core
::svn_auth_set_parameter
($baton,
112 $SVN::_Core
::SVN_AUTH_PARAM_DONT_STORE_PASSWORDS
,
113 bless (\
$dont_store_passwords, "_p_void"));
115 if (SVN
::_Core
::svn_config_get_bool
($conf_t,
116 $SVN::_Core
::SVN_CONFIG_SECTION_AUTH
,
117 $SVN::_Core
::SVN_CONFIG_OPTION_STORE_AUTH_CREDS
,
119 $Git::SVN
::Prompt
::_no_auth_cache
= 1;
121 } # no warnings 'once'
122 my $self = SVN
::Ra
->new(url
=> escape_url
($url), auth
=> $baton,
124 pool
=> SVN
::Pool
->new,
125 auth_provider_callbacks
=> $callbacks);
126 $RA = bless $self, $class;
128 # Make sure its canonicalized
130 $self->{svn_path
} = $url;
131 $self->{repos_root
} = $self->get_repos_root;
132 $self->{svn_path
} =~ s
#^\Q$self->{repos_root}\E(/|$)##;
133 $self->{cache
} = { check_path
=> { r
=> 0, data
=> {} },
134 get_dir
=> { r
=> 0, data
=> {} } };
144 $self->{url
} = canonicalize_url
($url);
152 my ($self, $path, $r) = @_;
153 my $cache = $self->{cache
}->{check_path
};
154 if ($r == $cache->{r
} && exists $cache->{data
}->{$path}) {
155 return $cache->{data
}->{$path};
157 my $pool = SVN
::Pool
->new;
158 my $t = $self->SUPER::check_path
($path, $r, $pool);
160 if ($r != $cache->{r
}) {
161 %{$cache->{data
}} = ();
164 $cache->{data
}->{$path} = $t;
168 my ($self, $dir, $r) = @_;
169 my $cache = $self->{cache
}->{get_dir
};
170 if ($r == $cache->{r
}) {
171 if (my $x = $cache->{data
}->{$dir}) {
172 return wantarray ? @
$x : $x->[0];
175 my $pool = SVN
::Pool
->new;
176 my ($d, undef, $props) = $self->SUPER::get_dir
($dir, $r, $pool);
177 my %dirents = map { $_ => { kind
=> $d->{$_}->kind } } keys %$d;
179 if ($r != $cache->{r
}) {
180 %{$cache->{data
}} = ();
183 $cache->{data
}->{$dir} = [ \
%dirents, $r, $props ];
184 wantarray ?
(\
%dirents, $r, $props) : \
%dirents;
188 # do not call the real DESTROY since we store ourselves in $RA
191 # get_log(paths, start, end, limit,
192 # discover_changed_paths, strict_node_history, receiver)
194 my ($self, @args) = @_;
195 my $pool = SVN
::Pool
->new;
197 # svn_log_changed_path_t objects passed to get_log are likely to be
198 # overwritten even if only the refs are copied to an external variable,
199 # so we should dup the structures in their entirety. Using an
200 # externally passed pool (instead of our temporary and quickly cleared
201 # pool in Git::SVN::Ra) does not help matters at all...
202 my $receiver = pop @args;
203 my $prefix = "/".$self->{svn_path
};
204 $prefix =~ s
#/+($)##;
205 my $prefix_regex = qr
#^\Q$prefix\E#;
208 return &$receiver(@_) unless $paths;
210 foreach my $p (keys %$paths) {
211 my $i = $paths->{$p};
212 # Make path relative to our url, not repos_root
213 $p =~ s/$prefix_regex//;
214 my %s = map { $_ => $i->$_; }
215 qw
/copyfrom_path copyfrom_rev action/;
216 if ($s{'copyfrom_path'}) {
217 $s{'copyfrom_path'} =~ s/$prefix_regex//;
225 # the limit parameter was not supported in SVN 1.1.x, so we
226 # drop it. Therefore, the receiver callback passed to it
227 # is made aware of this limitation by being wrapped if
228 # the limit passed to is being wrapped.
229 if (::compare_svn_version
('1.2.0') <= 0) {
230 my $limit = splice(@args, 3, 1);
232 my $receiver = pop @args;
233 push(@args, sub { &$receiver(@_) if (--$limit >= 0) });
236 my $ret = $self->SUPER::get_log
(@args, $pool);
242 my ($self, $url1, $rev1, $url2, $rev2) = @_;
243 my $ctx = SVN
::Client
->new(auth
=> _auth_providers
);
244 my $out = IO
::File
->new_tmpfile;
246 # older SVN (1.1.x) doesn't take $pool as the last parameter for
247 # $ctx->diff(), so we'll create a default one
248 my $pool = SVN
::Pool
->new_default_sub;
250 $ra_invalid = 1; # this will open a new SVN::Ra connection to $url1
251 $ctx->diff([], $url1, $rev1, $url2, $rev2, 1, 1, 0, $out, $out);
253 my $ret = (($out->stat)[7] == 0);
254 close $out or croak
$!;
259 sub get_commit_editor
{
260 my ($self, $log, $cb, $pool) = @_;
262 my @lock = (::compare_svn_version
('1.2.0') >= 0) ?
(undef, 0) : ();
263 $self->SUPER::get_commit_editor
($log, $cb, @lock, $pool);
267 my ($self, $rev_a, $rev_b, $gs, $editor) = @_;
268 my $new = ($rev_a == $rev_b);
269 my $path = $gs->path;
271 if ($new && -e
$gs->{index}) {
272 unlink $gs->{index} or die
273 "Couldn't unlink index: $gs->{index}: $!\n";
275 my $pool = SVN
::Pool
->new;
276 $editor->set_path_strip($path);
277 my (@pc) = split m
#/#, $path;
278 my $reporter = $self->do_update($rev_b, (@pc ?
shift @pc : ''),
280 my @lock = (::compare_svn_version
('1.2.0') >= 0) ?
(undef) : ();
282 # Since we can't rely on svn_ra_reparent being available, we'll
283 # just have to do some magic with set_path to make it so
284 # we only want a partial path.
286 my $final = join('/', @pc);
288 $reporter->set_path($sp, $rev_b, 0, @lock, $pool);
289 $sp .= '/' if length $sp;
292 die "BUG: '$sp' != '$final'\n" if ($sp ne $final);
294 $reporter->set_path($sp, $rev_a, $new, @lock, $pool);
296 $reporter->finish_report($pool);
298 $editor->{git_commit_ok
};
301 # this requires SVN 1.4.3 or later (do_switch didn't work before 1.4.3, and
302 # svn_ra_reparent didn't work before 1.4)
304 my ($self, $rev_a, $rev_b, $gs, $url_b, $editor) = @_;
305 my $path = $gs->path;
306 my $pool = SVN
::Pool
->new;
308 my $full_url = $self->url;
309 my $old_url = $full_url;
310 $full_url .= '/' . $path if length $path;
311 my ($ra, $reparented);
313 if ($old_url =~ m
#^svn(\+ssh)?://# ||
314 ($full_url =~ m
#^https?://# &&
315 escape_url
($full_url) ne $full_url)) {
319 $ra = Git
::SVN
::Ra
->new($full_url);
321 } elsif ($old_url ne $full_url) {
322 SVN
::_Ra
::svn_ra_reparent
($self->{session
}, $full_url, $pool);
323 $self->url($full_url);
328 $url_b = escape_url
($url_b);
329 my $reporter = $ra->do_switch($rev_b, '', 1, $url_b, $editor, $pool);
330 my @lock = (::compare_svn_version
('1.2.0') >= 0) ?
(undef) : ();
331 $reporter->set_path('', $rev_a, 0, @lock, $pool);
332 $reporter->finish_report($pool);
335 SVN
::_Ra
::svn_ra_reparent
($self->{session
}, $old_url, $pool);
336 $self->url($old_url);
340 $editor->{git_commit_ok
};
343 sub longest_common_path
{
344 my ($gsv, $globs) = @_;
346 my $common_max = scalar @
$gsv;
348 foreach my $gs (@
$gsv) {
349 my @tmp = split m
#/#, $gs->path;
352 $p .= length($p) ?
"/$_" : $_;
358 $common_max += scalar @
$globs;
359 foreach my $glob (@
$globs) {
360 my @tmp = split m
#/#, $glob->{path}->{left};
363 $p .= length($p) ?
"/$_" : $_;
369 my $longest_path = '';
370 foreach (sort {length $b <=> length $a} keys %common) {
371 if ($common{$_} == $common_max) {
379 sub gs_fetch_loop_common
{
380 my ($self, $base, $head, $gsv, $globs) = @_;
381 return if ($base > $head);
382 my $inc = $_log_window_size;
383 my ($min, $max) = ($base, $head < $base + $inc ?
$head : $base + $inc);
384 my $longest_path = longest_common_path
($gsv, $globs);
385 my $ra_url = $self->url;
386 my $find_trailing_edge;
390 my $err_handler = $SVN::Error
::handler
;
391 $SVN::Error
::handler
= sub {
393 skip_unknown_revs
($err);
396 my ($paths, $r, $author, $date, $log) = @_;
398 { author
=> $author, date
=> $date, log => $log } ];
400 $self->get_log([$longest_path], $min, $max, 0, 1, 1,
401 sub { $revs{$_[1]} = _cb
(@_) });
403 print "Checked through r$max\r";
405 $find_trailing_edge = 1;
407 if ($err and $find_trailing_edge) {
408 print STDERR
"Path '$longest_path' ",
409 "was probably deleted:\n",
410 $err->expanded_message,
411 "\nWill attempt to follow ",
412 "revisions r$min .. r$max ",
413 "committed before the deletion\n";
415 while (--$hi >= $min) {
417 $self->get_log([$longest_path], $min, $hi,
420 $revs{$_[1]} = _cb
(@_) });
422 print STDERR
"r$min .. r$ok OK\n";
426 $find_trailing_edge = 0;
428 $SVN::Error
::handler
= $err_handler;
430 my %exists = map { $_->{path
} => $_ } @
$gsv;
431 foreach my $r (sort {$a <=> $b} keys %revs) {
432 my ($paths, $logged) = @
{$revs{$r}};
434 foreach my $gs ($self->match_globs(\
%exists, $paths,
436 if ($gs->rev_map_max >= $r) {
439 next unless $gs->match_paths($paths, $r);
440 $gs->{logged_rev_props
} = $logged;
441 if (my $last_commit = $gs->last_commit) {
442 $gs->assert_index_clean($last_commit);
444 my $log_entry = $gs->do_fetch($paths, $r);
446 $gs->do_git_commit($log_entry);
448 $Git::SVN
::INDEX_FILES
{$gs->{index}} = 1;
450 foreach my $g (@
$globs) {
451 my $k = "svn-remote.$g->{remote}." .
453 Git
::SVN
::tmp_config
($k, $r);
459 $self = Git
::SVN
::Ra
->new($ra_url);
463 # pre-fill the .rev_db since it'll eventually get filled in
464 # with '0' x40 if something new gets committed
465 foreach my $gs (@
$gsv) {
466 next if $gs->rev_map_max >= $max;
467 next if defined $gs->rev_map_get($max);
468 $gs->rev_map_set($max, 0 x40
);
470 foreach my $g (@
$globs) {
471 my $k = "svn-remote.$g->{remote}.$g->{t}-maxRev";
472 Git
::SVN
::tmp_config
($k, $max);
474 last if $max >= $head;
477 $max = $head if ($max > $head);
482 sub get_dir_globbed
{
483 my ($self, $left, $depth, $r) = @_;
485 my @x = eval { $self->get_dir($left, $r) };
486 return unless scalar @x == 3;
489 foreach my $de (keys %$dirents) {
490 next if $dirents->{$de}->{kind
} != $SVN::Node
::dir
;
492 my @args = ("$left/$de", $depth - 1, $r);
493 foreach my $dir ($self->get_dir_globbed(@args)) {
494 push @finalents, "$de/$dir";
497 push @finalents, $de;
503 # return value: 0 -- don't ignore, 1 -- ignore
506 my $refname = $g->{ref}->full_path($p);
507 return 1 if defined($g->{ignore_refs_regex
}) &&
508 $refname =~ m!$g->{ignore_refs_regex}!;
509 return 0 unless defined($_ignore_refs_regex);
510 return 1 if $refname =~ m!$_ignore_refs_regex!o;
515 my ($self, $exists, $paths, $globs, $r) = @_;
518 my ($self, $exists, $g, $r) = @_;
520 my @dirs = $self->get_dir_globbed($g->{path
}->{left
},
524 foreach my $de (@dirs) {
525 my $p = $g->{path
}->full_path($de);
526 next if $exists->{$p};
527 next if (length $g->{path
}->{right
} &&
528 ($self->check_path($p, $r) !=
530 next unless $p =~ /$g->{path}->{regex}/;
531 $exists->{$p} = Git
::SVN
->init($self->url, $p, undef,
532 $g->{ref}->full_path($de), 1);
535 foreach my $g (@
$globs) {
536 if (my $path = $paths->{"/$g->{path}->{left}"}) {
537 if ($path->{action
} =~ /^[AR]$/) {
538 get_dir_check
($self, $exists, $g, $r);
541 foreach (keys %$paths) {
542 if (/$g->{path}->{left_regex}/ &&
543 !/$g->{path}->{regex}/) {
544 next if $paths->{$_}->{action
} !~ /^[AR]$/;
545 get_dir_check
($self, $exists, $g, $r);
547 next unless /$g->{path}->{regex}/;
549 my $pathname = $g->{path
}->full_path($p);
550 next if is_ref_ignored
($g, $p);
551 next if $exists->{$pathname};
552 next if ($self->check_path($pathname, $r) !=
554 $exists->{$pathname} = Git
::SVN
->init(
555 $self->url, $pathname, undef,
556 $g->{ref}->full_path($p), 1);
559 foreach (split m
#/#, $g->{path}->{left}) {
561 next unless ($paths->{$c} &&
562 ($paths->{$c}->{action
} =~ /^[AR]$/));
563 get_dir_check
($self, $exists, $g, $r);
571 return $self->url if ($self->url eq $self->{repos_root
});
572 my $url = $self->{repos_root
};
573 my @components = split(m!/!, $self->{svn_path
});
576 $url .= "/$c" if length $c;
578 my $ra = (ref $self)->new($url);
579 my $latest = $ra->get_latest_revnum;
580 $ra->get_log("", $latest, 0, 1, 0, 1, sub {});
582 } while ($@
&& ($c = shift @components));
588 unless (defined $can_do_switch) {
589 my $pool = SVN
::Pool
->new;
591 $self->do_switch(1, '', 0, $self->url,
592 SVN
::Delta
::Editor
->new, $pool);
597 $rep->abort_report($pool);
605 sub skip_unknown_revs
{
607 my $errno = $err->apr_err();
608 # Maybe the branch we're tracking didn't
609 # exist when the repo started, so it's
610 # not an error if it doesn't, just continue
612 # Wonderfully consistent library, eh?
613 # 160013 - svn:// and file://
614 # 175002 - http(s)://
615 # 175007 - http(s):// (this repo required authorization, too...)
616 # More codes may be discovered later...
617 if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
618 my $err_key = $err->expanded_message;
619 # revision numbers change every time, filter them out
620 $err_key =~ s/\d+/\0/g;
621 $err_key = "$errno\0$err_key";
622 unless ($ignored_err{$err_key}) {
623 warn "W: Ignoring error from SVN, path probably ",
624 "does not exist: ($errno): ",
625 $err->expanded_message,"\n";
626 warn "W: Do not be alarmed at the above message ",
627 "git-svn is just searching aggressively for ",
629 "This may take a while on large repositories\n";
630 $ignored_err{$err_key} = 1;
634 die "Error from SVN, ($errno): ", $err->expanded_message,"\n";
640 Git
::SVN
::Ra
- Subversion remote access functions
for git
-svn
646 my $ra = Git::SVN::Ra->new($branchurl);
647 my ($dirents, $fetched_revnum, $props) =
648 $ra->get_dir('.', $SVN::Core::INVALID_REVNUM);
652 This is a wrapper around the L<SVN::Ra> module for use by B<git-svn>.
653 It fills in some default parameters (such as the authentication
654 scheme), smooths over incompatibilities between libsvn versions, adds
655 caching, and implements some functions specific to B<git-svn>.
657 Do not use it unless you are developing git-svn. The interface will
658 change as git-svn evolves.
662 Subversion perl bindings,
665 C<Git::SVN::Ra> has not been tested using callers other than
672 =head1 INCOMPATIBILITIES