2 use vars qw
/@ISA $config_dir $_ignore_refs_regex $_log_window_size/;
11 my ($ra_invalid, $can_do_switch, %ignored_err, $RA);
14 # enforce temporary pool usage for some simple functions
16 for my $f (qw
/rev_proplist get_latest_revnum get_uuid get_repos_root
18 my $SUPER = "SUPER::$f";
21 my $pool = SVN
::Pool
->new;
22 my @ret = $self->$SUPER(@_,$pool);
24 wantarray ?
@ret : $ret[0];
29 sub _auth_providers
() {
31 SVN
::Client
::get_simple_provider
(),
32 SVN
::Client
::get_ssl_server_trust_file_provider
(),
33 SVN
::Client
::get_simple_prompt_provider
(
34 \
&Git
::SVN
::Prompt
::simple
, 2),
35 SVN
::Client
::get_ssl_client_cert_file_provider
(),
36 SVN
::Client
::get_ssl_client_cert_prompt_provider
(
37 \
&Git
::SVN
::Prompt
::ssl_client_cert
, 2),
38 SVN
::Client
::get_ssl_client_cert_pw_file_provider
(),
39 SVN
::Client
::get_ssl_client_cert_pw_prompt_provider
(
40 \
&Git
::SVN
::Prompt
::ssl_client_cert_pw
, 2),
41 SVN
::Client
::get_username_provider
(),
42 SVN
::Client
::get_ssl_server_trust_prompt_provider
(
43 \
&Git
::SVN
::Prompt
::ssl_server_trust
),
44 SVN
::Client
::get_username_prompt_provider
(
45 \
&Git
::SVN
::Prompt
::username
, 2)
48 # earlier 1.6.x versions would segfault, and <= 1.5.x didn't have
50 if (::compare_svn_version
('1.6.15') >= 0) {
51 my $config = SVN
::Core
::config_get_config
($config_dir);
53 # config_get_config returns all config files from
54 # ~/.subversion, auth_get_platform_specific_client_providers
55 # just wants the config "file".
56 @a = ($config->{'config'}, undef);
57 $p = SVN
::Core
::auth_get_platform_specific_client_providers
(@a);
58 # Insert the return value from
59 # auth_get_platform_specific_providers
68 foreach (split m{/}, $uri) {
69 s/([^~\w.%+-]|%(?![a-fA-F0-9]{2}))/sprintf("%%%02X",ord($1))/eg;
77 if ($url =~ m
#^(https?)://([^/]+)(.*)$#) {
78 my ($scheme, $domain, $uri) = ($1, $2, escape_uri_only
($3));
79 $url = "$scheme://$domain$uri";
85 my ($class, $url) = @_;
87 return $RA if ($RA && $RA->url eq $url);
91 SVN
::_Core
::svn_config_ensure
($config_dir, undef);
92 my ($baton, $callbacks) = SVN
::Core
::auth_open_helper
(_auth_providers
);
93 my $config = SVN
::Core
::config_get_config
($config_dir);
95 my $dont_store_passwords = 1;
96 my $conf_t = ${$config}{'config'};
99 # The usage of $SVN::_Core::SVN_CONFIG_* variables
100 # produces warnings that variables are used only once.
101 # I had not found the better way to shut them up, so
102 # the warnings of type 'once' are disabled in this block.
103 if (SVN
::_Core
::svn_config_get_bool
($conf_t,
104 $SVN::_Core
::SVN_CONFIG_SECTION_AUTH
,
105 $SVN::_Core
::SVN_CONFIG_OPTION_STORE_PASSWORDS
,
107 SVN
::_Core
::svn_auth_set_parameter
($baton,
108 $SVN::_Core
::SVN_AUTH_PARAM_DONT_STORE_PASSWORDS
,
109 bless (\
$dont_store_passwords, "_p_void"));
111 if (SVN
::_Core
::svn_config_get_bool
($conf_t,
112 $SVN::_Core
::SVN_CONFIG_SECTION_AUTH
,
113 $SVN::_Core
::SVN_CONFIG_OPTION_STORE_AUTH_CREDS
,
115 $Git::SVN
::Prompt
::_no_auth_cache
= 1;
117 } # no warnings 'once'
118 my $self = SVN
::Ra
->new(url
=> escape_url
($url), auth
=> $baton,
120 pool
=> SVN
::Pool
->new,
121 auth_provider_callbacks
=> $callbacks);
122 $RA = bless $self, $class;
124 # Make sure its canonicalized
126 $self->{svn_path
} = $url;
127 $self->{repos_root
} = $self->get_repos_root;
128 $self->{svn_path
} =~ s
#^\Q$self->{repos_root}\E(/|$)##;
129 $self->{cache
} = { check_path
=> { r
=> 0, data
=> {} },
130 get_dir
=> { r
=> 0, data
=> {} } };
148 my ($self, $path, $r) = @_;
149 my $cache = $self->{cache
}->{check_path
};
150 if ($r == $cache->{r
} && exists $cache->{data
}->{$path}) {
151 return $cache->{data
}->{$path};
153 my $pool = SVN
::Pool
->new;
154 my $t = $self->SUPER::check_path
($path, $r, $pool);
156 if ($r != $cache->{r
}) {
157 %{$cache->{data
}} = ();
160 $cache->{data
}->{$path} = $t;
164 my ($self, $dir, $r) = @_;
165 my $cache = $self->{cache
}->{get_dir
};
166 if ($r == $cache->{r
}) {
167 if (my $x = $cache->{data
}->{$dir}) {
168 return wantarray ? @
$x : $x->[0];
171 my $pool = SVN
::Pool
->new;
172 my ($d, undef, $props) = $self->SUPER::get_dir
($dir, $r, $pool);
173 my %dirents = map { $_ => { kind
=> $d->{$_}->kind } } keys %$d;
175 if ($r != $cache->{r
}) {
176 %{$cache->{data
}} = ();
179 $cache->{data
}->{$dir} = [ \
%dirents, $r, $props ];
180 wantarray ?
(\
%dirents, $r, $props) : \
%dirents;
184 # do not call the real DESTROY since we store ourselves in $RA
187 # get_log(paths, start, end, limit,
188 # discover_changed_paths, strict_node_history, receiver)
190 my ($self, @args) = @_;
191 my $pool = SVN
::Pool
->new;
193 # svn_log_changed_path_t objects passed to get_log are likely to be
194 # overwritten even if only the refs are copied to an external variable,
195 # so we should dup the structures in their entirety. Using an
196 # externally passed pool (instead of our temporary and quickly cleared
197 # pool in Git::SVN::Ra) does not help matters at all...
198 my $receiver = pop @args;
199 my $prefix = "/".$self->{svn_path
};
200 $prefix =~ s
#/+($)##;
201 my $prefix_regex = qr
#^\Q$prefix\E#;
204 return &$receiver(@_) unless $paths;
206 foreach my $p (keys %$paths) {
207 my $i = $paths->{$p};
208 # Make path relative to our url, not repos_root
209 $p =~ s/$prefix_regex//;
210 my %s = map { $_ => $i->$_; }
211 qw
/copyfrom_path copyfrom_rev action/;
212 if ($s{'copyfrom_path'}) {
213 $s{'copyfrom_path'} =~ s/$prefix_regex//;
221 # the limit parameter was not supported in SVN 1.1.x, so we
222 # drop it. Therefore, the receiver callback passed to it
223 # is made aware of this limitation by being wrapped if
224 # the limit passed to is being wrapped.
225 if (::compare_svn_version
('1.2.0') <= 0) {
226 my $limit = splice(@args, 3, 1);
228 my $receiver = pop @args;
229 push(@args, sub { &$receiver(@_) if (--$limit >= 0) });
232 my $ret = $self->SUPER::get_log
(@args, $pool);
238 my ($self, $url1, $rev1, $url2, $rev2) = @_;
239 my $ctx = SVN
::Client
->new(auth
=> _auth_providers
);
240 my $out = IO
::File
->new_tmpfile;
242 # older SVN (1.1.x) doesn't take $pool as the last parameter for
243 # $ctx->diff(), so we'll create a default one
244 my $pool = SVN
::Pool
->new_default_sub;
246 $ra_invalid = 1; # this will open a new SVN::Ra connection to $url1
247 $ctx->diff([], $url1, $rev1, $url2, $rev2, 1, 1, 0, $out, $out);
249 my $ret = (($out->stat)[7] == 0);
250 close $out or croak
$!;
255 sub get_commit_editor
{
256 my ($self, $log, $cb, $pool) = @_;
258 my @lock = (::compare_svn_version
('1.2.0') >= 0) ?
(undef, 0) : ();
259 $self->SUPER::get_commit_editor
($log, $cb, @lock, $pool);
263 my ($self, $rev_a, $rev_b, $gs, $editor) = @_;
264 my $new = ($rev_a == $rev_b);
265 my $path = $gs->{path
};
267 if ($new && -e
$gs->{index}) {
268 unlink $gs->{index} or die
269 "Couldn't unlink index: $gs->{index}: $!\n";
271 my $pool = SVN
::Pool
->new;
272 $editor->set_path_strip($path);
273 my (@pc) = split m
#/#, $path;
274 my $reporter = $self->do_update($rev_b, (@pc ?
shift @pc : ''),
276 my @lock = (::compare_svn_version
('1.2.0') >= 0) ?
(undef) : ();
278 # Since we can't rely on svn_ra_reparent being available, we'll
279 # just have to do some magic with set_path to make it so
280 # we only want a partial path.
282 my $final = join('/', @pc);
284 $reporter->set_path($sp, $rev_b, 0, @lock, $pool);
285 $sp .= '/' if length $sp;
288 die "BUG: '$sp' != '$final'\n" if ($sp ne $final);
290 $reporter->set_path($sp, $rev_a, $new, @lock, $pool);
292 $reporter->finish_report($pool);
294 $editor->{git_commit_ok
};
297 # this requires SVN 1.4.3 or later (do_switch didn't work before 1.4.3, and
298 # svn_ra_reparent didn't work before 1.4)
300 my ($self, $rev_a, $rev_b, $gs, $url_b, $editor) = @_;
301 my $path = $gs->{path
};
302 my $pool = SVN
::Pool
->new;
304 my $full_url = $self->url;
305 my $old_url = $full_url;
306 $full_url .= '/' . $path if length $path;
307 my ($ra, $reparented);
309 if ($old_url =~ m
#^svn(\+ssh)?://# ||
310 ($full_url =~ m
#^https?://# &&
311 escape_url
($full_url) ne $full_url)) {
315 $ra = Git
::SVN
::Ra
->new($full_url);
317 } elsif ($old_url ne $full_url) {
318 SVN
::_Ra
::svn_ra_reparent
($self->{session
}, $full_url, $pool);
319 $self->url($full_url);
324 $url_b = escape_url
($url_b);
325 my $reporter = $ra->do_switch($rev_b, '', 1, $url_b, $editor, $pool);
326 my @lock = (::compare_svn_version
('1.2.0') >= 0) ?
(undef) : ();
327 $reporter->set_path('', $rev_a, 0, @lock, $pool);
328 $reporter->finish_report($pool);
331 SVN
::_Ra
::svn_ra_reparent
($self->{session
}, $old_url, $pool);
332 $self->url($old_url);
336 $editor->{git_commit_ok
};
339 sub longest_common_path
{
340 my ($gsv, $globs) = @_;
342 my $common_max = scalar @
$gsv;
344 foreach my $gs (@
$gsv) {
345 my @tmp = split m
#/#, $gs->{path};
348 $p .= length($p) ?
"/$_" : $_;
354 $common_max += scalar @
$globs;
355 foreach my $glob (@
$globs) {
356 my @tmp = split m
#/#, $glob->{path}->{left};
359 $p .= length($p) ?
"/$_" : $_;
365 my $longest_path = '';
366 foreach (sort {length $b <=> length $a} keys %common) {
367 if ($common{$_} == $common_max) {
375 sub gs_fetch_loop_common
{
376 my ($self, $base, $head, $gsv, $globs) = @_;
377 return if ($base > $head);
378 my $inc = $_log_window_size;
379 my ($min, $max) = ($base, $head < $base + $inc ?
$head : $base + $inc);
380 my $longest_path = longest_common_path
($gsv, $globs);
381 my $ra_url = $self->url;
382 my $find_trailing_edge;
386 my $err_handler = $SVN::Error
::handler
;
387 $SVN::Error
::handler
= sub {
389 skip_unknown_revs
($err);
392 my ($paths, $r, $author, $date, $log) = @_;
394 { author
=> $author, date
=> $date, log => $log } ];
396 $self->get_log([$longest_path], $min, $max, 0, 1, 1,
397 sub { $revs{$_[1]} = _cb
(@_) });
399 print "Checked through r$max\r";
401 $find_trailing_edge = 1;
403 if ($err and $find_trailing_edge) {
404 print STDERR
"Path '$longest_path' ",
405 "was probably deleted:\n",
406 $err->expanded_message,
407 "\nWill attempt to follow ",
408 "revisions r$min .. r$max ",
409 "committed before the deletion\n";
411 while (--$hi >= $min) {
413 $self->get_log([$longest_path], $min, $hi,
416 $revs{$_[1]} = _cb
(@_) });
418 print STDERR
"r$min .. r$ok OK\n";
422 $find_trailing_edge = 0;
424 $SVN::Error
::handler
= $err_handler;
426 my %exists = map { $_->{path
} => $_ } @
$gsv;
427 foreach my $r (sort {$a <=> $b} keys %revs) {
428 my ($paths, $logged) = @
{$revs{$r}};
430 foreach my $gs ($self->match_globs(\
%exists, $paths,
432 if ($gs->rev_map_max >= $r) {
435 next unless $gs->match_paths($paths, $r);
436 $gs->{logged_rev_props
} = $logged;
437 if (my $last_commit = $gs->last_commit) {
438 $gs->assert_index_clean($last_commit);
440 my $log_entry = $gs->do_fetch($paths, $r);
442 $gs->do_git_commit($log_entry);
444 $Git::SVN
::INDEX_FILES
{$gs->{index}} = 1;
446 foreach my $g (@
$globs) {
447 my $k = "svn-remote.$g->{remote}." .
449 Git
::SVN
::tmp_config
($k, $r);
455 $self = Git
::SVN
::Ra
->new($ra_url);
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);
478 sub get_dir_globbed
{
479 my ($self, $left, $depth, $r) = @_;
481 my @x = eval { $self->get_dir($left, $r) };
482 return unless scalar @x == 3;
485 foreach my $de (keys %$dirents) {
486 next if $dirents->{$de}->{kind
} != $SVN::Node
::dir
;
488 my @args = ("$left/$de", $depth - 1, $r);
489 foreach my $dir ($self->get_dir_globbed(@args)) {
490 push @finalents, "$de/$dir";
493 push @finalents, $de;
499 # return value: 0 -- don't ignore, 1 -- ignore
502 my $refname = $g->{ref}->full_path($p);
503 return 1 if defined($g->{ignore_refs_regex
}) &&
504 $refname =~ m!$g->{ignore_refs_regex}!;
505 return 0 unless defined($_ignore_refs_regex);
506 return 1 if $refname =~ m!$_ignore_refs_regex!o;
511 my ($self, $exists, $paths, $globs, $r) = @_;
514 my ($self, $exists, $g, $r) = @_;
516 my @dirs = $self->get_dir_globbed($g->{path
}->{left
},
520 foreach my $de (@dirs) {
521 my $p = $g->{path
}->full_path($de);
522 next if $exists->{$p};
523 next if (length $g->{path
}->{right
} &&
524 ($self->check_path($p, $r) !=
526 next unless $p =~ /$g->{path}->{regex}/;
527 $exists->{$p} = Git
::SVN
->init($self->url, $p, undef,
528 $g->{ref}->full_path($de), 1);
531 foreach my $g (@
$globs) {
532 if (my $path = $paths->{"/$g->{path}->{left}"}) {
533 if ($path->{action
} =~ /^[AR]$/) {
534 get_dir_check
($self, $exists, $g, $r);
537 foreach (keys %$paths) {
538 if (/$g->{path}->{left_regex}/ &&
539 !/$g->{path}->{regex}/) {
540 next if $paths->{$_}->{action
} !~ /^[AR]$/;
541 get_dir_check
($self, $exists, $g, $r);
543 next unless /$g->{path}->{regex}/;
545 my $pathname = $g->{path
}->full_path($p);
546 next if is_ref_ignored
($g, $p);
547 next if $exists->{$pathname};
548 next if ($self->check_path($pathname, $r) !=
550 $exists->{$pathname} = Git
::SVN
->init(
551 $self->url, $pathname, undef,
552 $g->{ref}->full_path($p), 1);
555 foreach (split m
#/#, $g->{path}->{left}) {
557 next unless ($paths->{$c} &&
558 ($paths->{$c}->{action
} =~ /^[AR]$/));
559 get_dir_check
($self, $exists, $g, $r);
567 return $self->url if ($self->url eq $self->{repos_root
});
568 my $url = $self->{repos_root
};
569 my @components = split(m!/!, $self->{svn_path
});
572 $url .= "/$c" if length $c;
574 my $ra = (ref $self)->new($url);
575 my $latest = $ra->get_latest_revnum;
576 $ra->get_log("", $latest, 0, 1, 0, 1, sub {});
578 } while ($@
&& ($c = shift @components));
584 unless (defined $can_do_switch) {
585 my $pool = SVN
::Pool
->new;
587 $self->do_switch(1, '', 0, $self->url,
588 SVN
::Delta
::Editor
->new, $pool);
593 $rep->abort_report($pool);
601 sub skip_unknown_revs
{
603 my $errno = $err->apr_err();
604 # Maybe the branch we're tracking didn't
605 # exist when the repo started, so it's
606 # not an error if it doesn't, just continue
608 # Wonderfully consistent library, eh?
609 # 160013 - svn:// and file://
610 # 175002 - http(s)://
611 # 175007 - http(s):// (this repo required authorization, too...)
612 # More codes may be discovered later...
613 if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
614 my $err_key = $err->expanded_message;
615 # revision numbers change every time, filter them out
616 $err_key =~ s/\d+/\0/g;
617 $err_key = "$errno\0$err_key";
618 unless ($ignored_err{$err_key}) {
619 warn "W: Ignoring error from SVN, path probably ",
620 "does not exist: ($errno): ",
621 $err->expanded_message,"\n";
622 warn "W: Do not be alarmed at the above message ",
623 "git-svn is just searching aggressively for ",
625 "This may take a while on large repositories\n";
626 $ignored_err{$err_key} = 1;
630 die "Error from SVN, ($errno): ", $err->expanded_message,"\n";
636 Git
::SVN
::Ra
- Subversion remote access functions
for git
-svn
642 my $ra = Git::SVN::Ra->new($branchurl);
643 my ($dirents, $fetched_revnum, $props) =
644 $ra->get_dir('.', $SVN::Core::INVALID_REVNUM);
648 This is a wrapper around the L<SVN::Ra> module for use by B<git-svn>.
649 It fills in some default parameters (such as the authentication
650 scheme), smooths over incompatibilities between libsvn versions, adds
651 caching, and implements some functions specific to B<git-svn>.
653 Do not use it unless you are developing git-svn. The interface will
654 change as git-svn evolves.
658 Subversion perl bindings,
661 C<Git::SVN::Ra> has not been tested using callers other than
668 =head1 INCOMPATIBILITIES