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;
181 # do not call the real DESTROY since we store ourselves in $RA
184 # get_log(paths, start, end, limit,
185 # discover_changed_paths, strict_node_history, receiver)
187 my ($self, @args) = @_;
188 my $pool = SVN::Pool->new;
190 # svn_log_changed_path_t objects passed to get_log are likely to be
191 # overwritten even if only the refs are copied to an external variable,
192 # so we should dup the structures in their entirety. Using an
193 # externally passed pool (instead of our temporary and quickly cleared
194 # pool in Git::SVN::Ra) does not help matters at all...
195 my $receiver = pop @args;
196 my $prefix = "/".$self->{svn_path};
197 $prefix =~ s#/+($)##;
198 my $prefix_regex = qr#^\Q$prefix\E#;
201 return &$receiver(@_) unless $paths;
203 foreach my $p (keys %$paths) {
204 my $i = $paths->{$p};
205 # Make path relative to our url, not repos_root
206 $p =~ s/$prefix_regex//;
207 my %s = map { $_ => $i->$_; }
208 qw/copyfrom_path copyfrom_rev action/;
209 if ($s{'copyfrom_path'}) {
210 $s{'copyfrom_path'} =~ s/$prefix_regex//;
211 $s{'copyfrom_path'} = canonicalize_path($s{'copyfrom_path'});
219 # the limit parameter was not supported in SVN 1.1.x, so we
220 # drop it. Therefore, the receiver callback passed to it
221 # is made aware of this limitation by being wrapped if
222 # the limit passed to is being wrapped.
223 if (::compare_svn_version('1.2.0') <= 0) {
224 my $limit = splice(@args, 3, 1);
226 my $receiver = pop @args;
227 push(@args, sub { &$receiver(@_) if (--$limit >= 0) });
230 my $ret = $self->SUPER::get_log(@args, $pool);
236 my ($self, $url1, $rev1, $url2, $rev2) = @_;
237 my $ctx = SVN::Client->new(auth => _auth_providers);
238 my $out = IO::File->new_tmpfile;
240 # older SVN (1.1.x) doesn't take $pool as the last parameter for
241 # $ctx->diff(), so we'll create a default one
242 my $pool = SVN::Pool->new_default_sub;
244 $ra_invalid = 1; # this will open a new SVN::Ra connection to $url1
245 $ctx->diff([], $url1, $rev1, $url2, $rev2, 1, 1, 0, $out, $out);
247 my $ret = (($out->stat)[7] == 0);
248 close $out or croak $!;
253 sub get_commit_editor {
254 my ($self, $log, $cb, $pool) = @_;
256 my @lock = (::compare_svn_version('1.2.0') >= 0) ? (undef, 0) : ();
257 $self->SUPER::get_commit_editor($log, $cb, @lock, $pool);
261 my ($self, $rev_a, $rev_b, $gs, $editor) = @_;
262 my $new = ($rev_a == $rev_b);
263 my $path = $gs->path;
265 if ($new && -e $gs->{index}) {
266 unlink $gs->{index} or die
267 "Couldn't unlink index: $gs->{index}: $!\n";
269 my $pool = SVN::Pool->new;
270 $editor->set_path_strip($path);
271 my (@pc) = split m#/#, $path;
272 my $reporter = $self->do_update($rev_b, (@pc ? shift @pc : ''),
274 my @lock = (::compare_svn_version('1.2.0') >= 0) ? (undef) : ();
276 # Since we can't rely on svn_ra_reparent being available, we'll
277 # just have to do some magic with set_path to make it so
278 # we only want a partial path.
280 my $final = join('/', @pc);
282 $reporter->set_path($sp, $rev_b, 0, @lock, $pool);
283 $sp .= '/' if length $sp;
286 die "BUG: '$sp' != '$final'\n" if ($sp ne $final);
288 $reporter->set_path($sp, $rev_a, $new, @lock, $pool);
290 $reporter->finish_report($pool);
292 $editor->{git_commit_ok};
295 # this requires SVN 1.4.3 or later (do_switch didn't work before 1.4.3, and
296 # svn_ra_reparent didn't work before 1.4)
298 my ($self, $rev_a, $rev_b, $gs, $url_b, $editor) = @_;
299 my $path = $gs->path;
300 my $pool = SVN::Pool->new;
302 my $old_url = $self->url;
303 my $full_url = add_path_to_url( $self->url, $path );
304 my ($ra, $reparented);
306 if ($old_url =~ m#^svn(\+\w+)?://# ||
307 ($full_url =~ m#^https?://# &&
308 canonicalize_url($full_url) ne $full_url)) {
312 $ra = Git::SVN::Ra->new($full_url);
314 } elsif ($old_url ne $full_url) {
315 SVN::_Ra::svn_ra_reparent(
317 canonicalize_url($full_url),
320 $self->url($full_url);
325 $url_b = canonicalize_url($url_b);
326 my $reporter = $ra->do_switch($rev_b, '', 1, $url_b, $editor, $pool);
327 my @lock = (::compare_svn_version('1.2.0') >= 0) ? (undef) : ();
328 $reporter->set_path('', $rev_a, 0, @lock, $pool);
329 $reporter->finish_report($pool);
332 SVN::_Ra::svn_ra_reparent($self->{session}, $old_url, $pool);
333 $self->url($old_url);
337 $editor->{git_commit_ok};
340 sub longest_common_path {
341 my ($gsv, $globs) = @_;
343 my $common_max = scalar @$gsv;
345 foreach my $gs (@$gsv) {
346 my @tmp = split m#/#, $gs->path;
349 $p .= length($p) ? "/$_" : $_;
355 $common_max += scalar @$globs;
356 foreach my $glob (@$globs) {
357 my @tmp = split m#/#, $glob->{path}->{left};
360 $p .= length($p) ? "/$_" : $_;
366 my $longest_path = '';
367 foreach (sort {length $b <=> length $a} keys %common) {
368 if ($common{$_} == $common_max) {
376 sub gs_fetch_loop_common {
377 my ($self, $base, $head, $gsv, $globs) = @_;
378 return if ($base > $head);
379 my $inc = $_log_window_size;
380 my ($min, $max) = ($base, $head < $base + $inc ? $head : $base + $inc);
381 my $longest_path = longest_common_path($gsv, $globs);
382 my $ra_url = $self->url;
383 my $find_trailing_edge;
387 my $err_handler = $SVN::Error::handler;
388 $SVN::Error::handler = sub {
390 skip_unknown_revs($err);
393 my ($paths, $r, $author, $date, $log) = @_;
395 { author => $author, date => $date, log => $log } ];
397 $self->get_log([$longest_path], $min, $max, 0, 1, 1,
398 sub { $revs{$_[1]} = _cb(@_) });
400 print "Checked through r$max\r";
402 $find_trailing_edge = 1;
404 if ($err and $find_trailing_edge) {
405 print STDERR "Path '$longest_path' ",
406 "was probably deleted:\n",
407 $err->expanded_message,
408 "\nWill attempt to follow ",
409 "revisions r$min .. r$max ",
410 "committed before the deletion\n";
412 while (--$hi >= $min) {
414 $self->get_log([$longest_path], $min, $hi,
417 $revs{$_[1]} = _cb(@_) });
419 print STDERR "r$min .. r$ok OK\n";
423 $find_trailing_edge = 0;
425 $SVN::Error::handler = $err_handler;
427 my %exists = map { $_->path => $_ } @$gsv;
428 foreach my $r (sort {$a <=> $b} keys %revs) {
429 my ($paths, $logged) = @{$revs{$r}};
431 foreach my $gs ($self->match_globs(\%exists, $paths,
433 if ($gs->rev_map_max >= $r) {
436 next unless $gs->match_paths($paths, $r);
437 $gs->{logged_rev_props} = $logged;
438 if (my $last_commit = $gs->last_commit) {
439 $gs->assert_index_clean($last_commit);
441 my $log_entry = $gs->do_fetch($paths, $r);
443 $gs->do_git_commit($log_entry);
445 $Git::SVN::INDEX_FILES{$gs->{index}} = 1;
447 foreach my $g (@$globs) {
448 my $k = "svn-remote.$g->{remote}." .
450 Git::SVN::tmp_config($k, $r);
456 $self = Git::SVN::Ra->new($ra_url);
460 # pre-fill the .rev_db since it'll eventually get filled in
461 # with '0' x40 if something new gets committed
462 foreach my $gs (@$gsv) {
463 next if $gs->rev_map_max >= $max;
464 next if defined $gs->rev_map_get($max);
465 $gs->rev_map_set($max, 0 x40);
467 foreach my $g (@$globs) {
468 my $k = "svn-remote.$g->{remote}.$g->{t}-maxRev";
469 Git::SVN::tmp_config($k, $max);
471 last if $max >= $head;
474 $max = $head if ($max > $head);
479 sub get_dir_globbed {
480 my ($self, $left, $depth, $r) = @_;
482 my @x = eval { $self->get_dir($left, $r) };
483 return unless scalar @x == 3;
486 foreach my $de (keys %$dirents) {
487 next if $dirents->{$de}->{kind} != $SVN::Node::dir;
489 my @args = ("$left/$de", $depth - 1, $r);
490 foreach my $dir ($self->get_dir_globbed(@args)) {
491 push @finalents, "$de/$dir";
494 push @finalents, $de;
500 # return value: 0 -- don't ignore, 1 -- ignore
503 my $refname = $g->{ref}->full_path($p);
504 return 1 if defined($g->{ignore_refs_regex}) &&
505 $refname =~ m!$g->{ignore_refs_regex}!;
506 return 0 unless defined($_ignore_refs_regex);
507 return 1 if $refname =~ m!$_ignore_refs_regex!o;
512 my ($self, $exists, $paths, $globs, $r) = @_;
515 my ($self, $exists, $g, $r) = @_;
517 my @dirs = $self->get_dir_globbed($g->{path}->{left},
521 foreach my $de (@dirs) {
522 my $p = $g->{path}->full_path($de);
523 next if $exists->{$p};
524 next if (length $g->{path}->{right} &&
525 ($self->check_path($p, $r) !=
527 next unless $p =~ /$g->{path}->{regex}/;
528 $exists->{$p} = Git::SVN->init($self->url, $p, undef,
529 $g->{ref}->full_path($de), 1);
532 foreach my $g (@$globs) {
533 if (my $path = $paths->{"/$g->{path}->{left}"}) {
534 if ($path->{action} =~ /^[AR]$/) {
535 get_dir_check($self, $exists, $g, $r);
538 foreach (keys %$paths) {
539 if (/$g->{path}->{left_regex}/ &&
540 !/$g->{path}->{regex}/) {
541 next if $paths->{$_}->{action} !~ /^[AR]$/;
542 get_dir_check($self, $exists, $g, $r);
544 next unless /$g->{path}->{regex}/;
546 my $pathname = $g->{path}->full_path($p);
547 next if is_ref_ignored($g, $p);
548 next if $exists->{$pathname};
549 next if ($self->check_path($pathname, $r) !=
551 $exists->{$pathname} = Git::SVN->init(
552 $self->url, $pathname, undef,
553 $g->{ref}->full_path($p), 1);
556 foreach (split m#/#, $g->{path}->{left}) {
558 next unless ($paths->{$c} &&
559 ($paths->{$c}->{action} =~ /^[AR]$/));
560 get_dir_check($self, $exists, $g, $r);
568 return $self->url if ($self->url eq $self->{repos_root});
569 my $url = $self->{repos_root};
570 my @components = split(m!/!, $self->{svn_path});
573 $url = add_path_to_url($url, $c);
575 my $ra = (ref $self)->new($url);
576 my $latest = $ra->get_latest_revnum;
577 $ra->get_log("", $latest, 0, 1, 0, 1, sub {});
579 } while ($@ && ($c = shift @components));
581 return canonicalize_url($url);
586 unless (defined $can_do_switch) {
587 my $pool = SVN::Pool->new;
589 $self->do_switch(1, '', 0, $self->url,
590 SVN::Delta::Editor->new, $pool);
595 $rep->abort_report($pool);
603 sub skip_unknown_revs {
605 my $errno = $err->apr_err();
606 # Maybe the branch we're tracking didn't
607 # exist when the repo started, so it's
608 # not an error if it doesn't, just continue
610 # Wonderfully consistent library, eh?
611 # 160013 - svn:// and file://
612 # 175002 - http(s)://
613 # 175007 - http(s):// (this repo required authorization, too...)
614 # More codes may be discovered later...
615 if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
616 my $err_key = $err->expanded_message;
617 # revision numbers change every time, filter them out
618 $err_key =~ s/\d+/\0/g;
619 $err_key = "$errno\0$err_key";
620 unless ($ignored_err{$err_key}) {
621 warn "W: Ignoring error from SVN, path probably ",
622 "does not exist: ($errno): ",
623 $err->expanded_message,"\n";
624 warn "W: Do not be alarmed at the above message ",
625 "git-svn is just searching aggressively for ",
627 "This may take a while on large repositories\n";
628 $ignored_err{$err_key} = 1;
632 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