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);
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 => {} } };
128 $RA = bless $self, $class;
132 my ($self, $path, $r) = @_;
133 my $cache = $self->{cache}->{check_path};
134 if ($r == $cache->{r} && exists $cache->{data}->{$path}) {
135 return $cache->{data}->{$path};
137 my $pool = SVN::Pool->new;
138 my $t = $self->SUPER::check_path($path, $r, $pool);
140 if ($r != $cache->{r}) {
141 %{$cache->{data}} = ();
144 $cache->{data}->{$path} = $t;
148 my ($self, $dir, $r) = @_;
149 my $cache = $self->{cache}->{get_dir};
150 if ($r == $cache->{r}) {
151 if (my $x = $cache->{data}->{$dir}) {
152 return wantarray ? @$x : $x->[0];
155 my $pool = SVN::Pool->new;
156 my ($d, undef, $props) = $self->SUPER::get_dir($dir, $r, $pool);
157 my %dirents = map { $_ => { kind => $d->{$_}->kind } } keys %$d;
159 if ($r != $cache->{r}) {
160 %{$cache->{data}} = ();
163 $cache->{data}->{$dir} = [ \%dirents, $r, $props ];
164 wantarray ? (\%dirents, $r, $props) : \%dirents;
168 # do not call the real DESTROY since we store ourselves in $RA
171 # get_log(paths, start, end, limit,
172 # discover_changed_paths, strict_node_history, receiver)
174 my ($self, @args) = @_;
175 my $pool = SVN::Pool->new;
177 # svn_log_changed_path_t objects passed to get_log are likely to be
178 # overwritten even if only the refs are copied to an external variable,
179 # so we should dup the structures in their entirety. Using an
180 # externally passed pool (instead of our temporary and quickly cleared
181 # pool in Git::SVN::Ra) does not help matters at all...
182 my $receiver = pop @args;
183 my $prefix = "/".$self->{svn_path};
184 $prefix =~ s#/+($)##;
185 my $prefix_regex = qr#^\Q$prefix\E#;
188 return &$receiver(@_) unless $paths;
190 foreach my $p (keys %$paths) {
191 my $i = $paths->{$p};
192 # Make path relative to our url, not repos_root
193 $p =~ s/$prefix_regex//;
194 my %s = map { $_ => $i->$_; }
195 qw/copyfrom_path copyfrom_rev action/;
196 if ($s{'copyfrom_path'}) {
197 $s{'copyfrom_path'} =~ s/$prefix_regex//;
205 # the limit parameter was not supported in SVN 1.1.x, so we
206 # drop it. Therefore, the receiver callback passed to it
207 # is made aware of this limitation by being wrapped if
208 # the limit passed to is being wrapped.
209 if (::compare_svn_version('1.2.0') <= 0) {
210 my $limit = splice(@args, 3, 1);
212 my $receiver = pop @args;
213 push(@args, sub { &$receiver(@_) if (--$limit >= 0) });
216 my $ret = $self->SUPER::get_log(@args, $pool);
222 my ($self, $url1, $rev1, $url2, $rev2) = @_;
223 my $ctx = SVN::Client->new(auth => _auth_providers);
224 my $out = IO::File->new_tmpfile;
226 # older SVN (1.1.x) doesn't take $pool as the last parameter for
227 # $ctx->diff(), so we'll create a default one
228 my $pool = SVN::Pool->new_default_sub;
230 $ra_invalid = 1; # this will open a new SVN::Ra connection to $url1
231 $ctx->diff([], $url1, $rev1, $url2, $rev2, 1, 1, 0, $out, $out);
233 my $ret = (($out->stat)[7] == 0);
234 close $out or croak $!;
239 sub get_commit_editor {
240 my ($self, $log, $cb, $pool) = @_;
242 my @lock = (::compare_svn_version('1.2.0') >= 0) ? (undef, 0) : ();
243 $self->SUPER::get_commit_editor($log, $cb, @lock, $pool);
247 my ($self, $rev_a, $rev_b, $gs, $editor) = @_;
248 my $new = ($rev_a == $rev_b);
249 my $path = $gs->{path};
251 if ($new && -e $gs->{index}) {
252 unlink $gs->{index} or die
253 "Couldn't unlink index: $gs->{index}: $!\n";
255 my $pool = SVN::Pool->new;
256 $editor->set_path_strip($path);
257 my (@pc) = split m#/#, $path;
258 my $reporter = $self->do_update($rev_b, (@pc ? shift @pc : ''),
260 my @lock = (::compare_svn_version('1.2.0') >= 0) ? (undef) : ();
262 # Since we can't rely on svn_ra_reparent being available, we'll
263 # just have to do some magic with set_path to make it so
264 # we only want a partial path.
266 my $final = join('/', @pc);
268 $reporter->set_path($sp, $rev_b, 0, @lock, $pool);
269 $sp .= '/' if length $sp;
272 die "BUG: '$sp' != '$final'\n" if ($sp ne $final);
274 $reporter->set_path($sp, $rev_a, $new, @lock, $pool);
276 $reporter->finish_report($pool);
278 $editor->{git_commit_ok};
281 # this requires SVN 1.4.3 or later (do_switch didn't work before 1.4.3, and
282 # svn_ra_reparent didn't work before 1.4)
284 my ($self, $rev_a, $rev_b, $gs, $url_b, $editor) = @_;
285 my $path = $gs->{path};
286 my $pool = SVN::Pool->new;
288 my $full_url = $self->{url};
289 my $old_url = $full_url;
290 $full_url .= '/' . $path if length $path;
291 my ($ra, $reparented);
293 if ($old_url =~ m#^svn(\+ssh)?://# ||
294 ($full_url =~ m#^https?://# &&
295 escape_url($full_url) ne $full_url)) {
299 $ra = Git::SVN::Ra->new($full_url);
301 } elsif ($old_url ne $full_url) {
302 SVN::_Ra::svn_ra_reparent($self->{session}, $full_url, $pool);
303 $self->{url} = $full_url;
308 $url_b = escape_url($url_b);
309 my $reporter = $ra->do_switch($rev_b, '', 1, $url_b, $editor, $pool);
310 my @lock = (::compare_svn_version('1.2.0') >= 0) ? (undef) : ();
311 $reporter->set_path('', $rev_a, 0, @lock, $pool);
312 $reporter->finish_report($pool);
315 SVN::_Ra::svn_ra_reparent($self->{session}, $old_url, $pool);
316 $self->{url} = $old_url;
320 $editor->{git_commit_ok};
323 sub longest_common_path {
324 my ($gsv, $globs) = @_;
326 my $common_max = scalar @$gsv;
328 foreach my $gs (@$gsv) {
329 my @tmp = split m#/#, $gs->{path};
332 $p .= length($p) ? "/$_" : $_;
338 $common_max += scalar @$globs;
339 foreach my $glob (@$globs) {
340 my @tmp = split m#/#, $glob->{path}->{left};
343 $p .= length($p) ? "/$_" : $_;
349 my $longest_path = '';
350 foreach (sort {length $b <=> length $a} keys %common) {
351 if ($common{$_} == $common_max) {
359 sub gs_fetch_loop_common {
360 my ($self, $base, $head, $gsv, $globs) = @_;
361 return if ($base > $head);
362 my $inc = $_log_window_size;
363 my ($min, $max) = ($base, $head < $base + $inc ? $head : $base + $inc);
364 my $longest_path = longest_common_path($gsv, $globs);
365 my $ra_url = $self->{url};
366 my $find_trailing_edge;
370 my $err_handler = $SVN::Error::handler;
371 $SVN::Error::handler = sub {
373 skip_unknown_revs($err);
376 my ($paths, $r, $author, $date, $log) = @_;
378 { author => $author, date => $date, log => $log } ];
380 $self->get_log([$longest_path], $min, $max, 0, 1, 1,
381 sub { $revs{$_[1]} = _cb(@_) });
383 print "Checked through r$max\r";
385 $find_trailing_edge = 1;
387 if ($err and $find_trailing_edge) {
388 print STDERR "Path '$longest_path' ",
389 "was probably deleted:\n",
390 $err->expanded_message,
391 "\nWill attempt to follow ",
392 "revisions r$min .. r$max ",
393 "committed before the deletion\n";
395 while (--$hi >= $min) {
397 $self->get_log([$longest_path], $min, $hi,
400 $revs{$_[1]} = _cb(@_) });
402 print STDERR "r$min .. r$ok OK\n";
406 $find_trailing_edge = 0;
408 $SVN::Error::handler = $err_handler;
410 my %exists = map { $_->{path} => $_ } @$gsv;
411 foreach my $r (sort {$a <=> $b} keys %revs) {
412 my ($paths, $logged) = @{$revs{$r}};
414 foreach my $gs ($self->match_globs(\%exists, $paths,
416 if ($gs->rev_map_max >= $r) {
419 next unless $gs->match_paths($paths, $r);
420 $gs->{logged_rev_props} = $logged;
421 if (my $last_commit = $gs->last_commit) {
422 $gs->assert_index_clean($last_commit);
424 my $log_entry = $gs->do_fetch($paths, $r);
426 $gs->do_git_commit($log_entry);
428 $Git::SVN::INDEX_FILES{$gs->{index}} = 1;
430 foreach my $g (@$globs) {
431 my $k = "svn-remote.$g->{remote}." .
433 Git::SVN::tmp_config($k, $r);
439 $self = Git::SVN::Ra->new($ra_url);
443 # pre-fill the .rev_db since it'll eventually get filled in
444 # with '0' x40 if something new gets committed
445 foreach my $gs (@$gsv) {
446 next if $gs->rev_map_max >= $max;
447 next if defined $gs->rev_map_get($max);
448 $gs->rev_map_set($max, 0 x40);
450 foreach my $g (@$globs) {
451 my $k = "svn-remote.$g->{remote}.$g->{t}-maxRev";
452 Git::SVN::tmp_config($k, $max);
454 last if $max >= $head;
457 $max = $head if ($max > $head);
462 sub get_dir_globbed {
463 my ($self, $left, $depth, $r) = @_;
465 my @x = eval { $self->get_dir($left, $r) };
466 return unless scalar @x == 3;
469 foreach my $de (keys %$dirents) {
470 next if $dirents->{$de}->{kind} != $SVN::Node::dir;
472 my @args = ("$left/$de", $depth - 1, $r);
473 foreach my $dir ($self->get_dir_globbed(@args)) {
474 push @finalents, "$de/$dir";
477 push @finalents, $de;
483 # return value: 0 -- don't ignore, 1 -- ignore
486 my $refname = $g->{ref}->full_path($p);
487 return 1 if defined($g->{ignore_refs_regex}) &&
488 $refname =~ m!$g->{ignore_refs_regex}!;
489 return 0 unless defined($_ignore_refs_regex);
490 return 1 if $refname =~ m!$_ignore_refs_regex!o;
495 my ($self, $exists, $paths, $globs, $r) = @_;
498 my ($self, $exists, $g, $r) = @_;
500 my @dirs = $self->get_dir_globbed($g->{path}->{left},
504 foreach my $de (@dirs) {
505 my $p = $g->{path}->full_path($de);
506 next if $exists->{$p};
507 next if (length $g->{path}->{right} &&
508 ($self->check_path($p, $r) !=
510 next unless $p =~ /$g->{path}->{regex}/;
511 $exists->{$p} = Git::SVN->init($self->{url}, $p, undef,
512 $g->{ref}->full_path($de), 1);
515 foreach my $g (@$globs) {
516 if (my $path = $paths->{"/$g->{path}->{left}"}) {
517 if ($path->{action} =~ /^[AR]$/) {
518 get_dir_check($self, $exists, $g, $r);
521 foreach (keys %$paths) {
522 if (/$g->{path}->{left_regex}/ &&
523 !/$g->{path}->{regex}/) {
524 next if $paths->{$_}->{action} !~ /^[AR]$/;
525 get_dir_check($self, $exists, $g, $r);
527 next unless /$g->{path}->{regex}/;
529 my $pathname = $g->{path}->full_path($p);
530 next if is_ref_ignored($g, $p);
531 next if $exists->{$pathname};
532 next if ($self->check_path($pathname, $r) !=
534 $exists->{$pathname} = Git::SVN->init(
535 $self->{url}, $pathname, undef,
536 $g->{ref}->full_path($p), 1);
539 foreach (split m#/#, $g->{path}->{left}) {
541 next unless ($paths->{$c} &&
542 ($paths->{$c}->{action} =~ /^[AR]$/));
543 get_dir_check($self, $exists, $g, $r);
551 return $self->{url} if ($self->{url} eq $self->{repos_root});
552 my $url = $self->{repos_root};
553 my @components = split(m!/!, $self->{svn_path});
556 $url .= "/$c" if length $c;
558 my $ra = (ref $self)->new($url);
559 my $latest = $ra->get_latest_revnum;
560 $ra->get_log("", $latest, 0, 1, 0, 1, sub {});
562 } while ($@ && ($c = shift @components));
568 unless (defined $can_do_switch) {
569 my $pool = SVN::Pool->new;
571 $self->do_switch(1, '', 0, $self->{url},
572 SVN::Delta::Editor->new, $pool);
577 $rep->abort_report($pool);
585 sub skip_unknown_revs {
587 my $errno = $err->apr_err();
588 # Maybe the branch we're tracking didn't
589 # exist when the repo started, so it's
590 # not an error if it doesn't, just continue
592 # Wonderfully consistent library, eh?
593 # 160013 - svn:// and file://
594 # 175002 - http(s)://
595 # 175007 - http(s):// (this repo required authorization, too...)
596 # More codes may be discovered later...
597 if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
598 my $err_key = $err->expanded_message;
599 # revision numbers change every time, filter them out
600 $err_key =~ s/\d+/\0/g;
601 $err_key = "$errno\0$err_key";
602 unless ($ignored_err{$err_key}) {
603 warn "W: Ignoring error from SVN, path probably ",
604 "does not exist: ($errno): ",
605 $err->expanded_message,"\n";
606 warn "W: Do not be alarmed at the above message ",
607 "git-svn is just searching aggressively for ",
609 "This may take a while on large repositories\n";
610 $ignored_err{$err_key} = 1;
614 die "Error from SVN, ($errno): ", $err->expanded_message,"\n";
620 Git::SVN::Ra - Subversion remote access functions for git-svn
626 my $ra = Git::SVN::Ra->new($branchurl);
627 my ($dirents, $fetched_revnum, $props) =
628 $ra->get_dir('.', $SVN::Core::INVALID_REVNUM);
632 This is a wrapper around the L<SVN::Ra> module for use by B<git-svn>.
633 It fills in some default parameters (such as the authentication
634 scheme), smooths over incompatibilities between libsvn versions, adds
635 caching, and implements some functions specific to B<git-svn>.
637 Do not use it unless you are developing git-svn. The interface will
638 change as git-svn evolves.
642 Subversion perl bindings,
645 C<Git::SVN::Ra> has not been tested using callers other than
652 =head1 INCOMPATIBILITIES