1package Git::SVN::Ra;
2use vars qw/@ISA $config_dir $_ignore_refs_regex $_log_window_size/;
3use strict;
4use warnings;
5use SVN::Client;
6use Git::SVN::Utils qw(
7 canonicalize_url
8 canonicalize_path
9 add_path_to_url
10);
11
12use SVN::Ra;
13BEGIN {
14 @ISA = qw(SVN::Ra);
15}
16
17my ($ra_invalid, $can_do_switch, %ignored_err, $RA);
18
19BEGIN {
20 # enforce temporary pool usage for some simple functions
21 no strict 'refs';
22 for my $f (qw/rev_proplist get_latest_revnum get_uuid get_repos_root
23 get_file/) {
24 my $SUPER = "SUPER::$f";
25 *$f = sub {
26 my $self = shift;
27 my $pool = SVN::Pool->new;
28 my @ret = $self->$SUPER(@_,$pool);
29 $pool->clear;
30 wantarray ? @ret : $ret[0];
31 };
32 }
33}
34
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.
38END {
39 $RA = undef;
40 $ra_invalid = 1;
41}
42
43sub _auth_providers () {
44 my @rv = (
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)
60 );
61
62 # earlier 1.6.x versions would segfault, and <= 1.5.x didn't have
63 # this function
64 if (::compare_svn_version('1.6.15') >= 0) {
65 my $config = SVN::Core::config_get_config($config_dir);
66 my ($p, @a);
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
74 unshift @rv, @$p;
75 }
76 \@rv;
77}
78
79
80sub new {
81 my ($class, $url) = @_;
82 $url = canonicalize_url($url);
83 return $RA if ($RA && $RA->url eq $url);
84
85 ::_req_svn();
86
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);
90 $RA = undef;
91 my $dont_store_passwords = 1;
92 my $conf_t = ${$config}{'config'};
93 {
94 no warnings 'once';
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,
102 1) == 0) {
103 SVN::_Core::svn_auth_set_parameter($baton,
104 $SVN::_Core::SVN_AUTH_PARAM_DONT_STORE_PASSWORDS,
105 bless (\$dont_store_passwords, "_p_void"));
106 }
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,
110 1) == 0) {
111 $Git::SVN::Prompt::_no_auth_cache = 1;
112 }
113 } # no warnings 'once'
114
115 my $self = SVN::Ra->new(url => $url, auth => $baton,
116 config => $config,
117 pool => SVN::Pool->new,
118 auth_provider_callbacks => $callbacks);
119 $RA = bless $self, $class;
120
121 # Make sure its canonicalized
122 $self->url($url);
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
129 return $RA;
130}
131
132sub url {
133 my $self = shift;
134
135 if (@_) {
136 my $url = shift;
137 $self->{url} = canonicalize_url($url);
138 return;
139 }
140
141 return $self->{url};
142}
143
144sub check_path {
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};
149 }
150 my $pool = SVN::Pool->new;
151 my $t = $self->SUPER::check_path($path, $r, $pool);
152 $pool->clear;
153 if ($r != $cache->{r}) {
154 %{$cache->{data}} = ();
155 $cache->{r} = $r;
156 }
157 $cache->{data}->{$path} = $t;
158}
159
160sub get_dir {
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];
166 }
167 }
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;
171 $pool->clear;
172 if ($r != $cache->{r}) {
173 %{$cache->{data}} = ();
174 $cache->{r} = $r;
175 }
176 $cache->{data}->{$dir} = [ \%dirents, $r, $props ];
177 wantarray ? (\%dirents, $r, $props) : \%dirents;
178}
179
180sub DESTROY {
181 # do not call the real DESTROY since we store ourselves in $RA
182}
183
184# get_log(paths, start, end, limit,
185# discover_changed_paths, strict_node_history, receiver)
186sub get_log {
187 my ($self, @args) = @_;
188 my $pool = SVN::Pool->new;
189
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#;
199 push(@args, sub {
200 my ($paths) = $_[0];
201 return &$receiver(@_) unless $paths;
202 $_[0] = ();
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'});
212 }
213 $_[0]{$p} = \%s;
214 }
215 &$receiver(@_);
216 });
217
218
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);
225 if ($limit > 0) {
226 my $receiver = pop @args;
227 push(@args, sub { &$receiver(@_) if (--$limit >= 0) });
228 }
229 }
230 my $ret = $self->SUPER::get_log(@args, $pool);
231 $pool->clear;
232 $ret;
233}
234
235sub trees_match {
236 my ($self, $url1, $rev1, $url2, $rev2) = @_;
237 my $ctx = SVN::Client->new(auth => _auth_providers);
238 my $out = IO::File->new_tmpfile;
239
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;
243
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);
246 $out->flush;
247 my $ret = (($out->stat)[7] == 0);
248 close $out or croak $!;
249
250 $ret;
251}
252
253sub get_commit_editor {
254 my ($self, $log, $cb, $pool) = @_;
255
256 my @lock = (::compare_svn_version('1.2.0') >= 0) ? (undef, 0) : ();
257 $self->SUPER::get_commit_editor($log, $cb, @lock, $pool);
258}
259
260sub gs_do_update {
261 my ($self, $rev_a, $rev_b, $gs, $editor) = @_;
262 my $new = ($rev_a == $rev_b);
263 my $path = $gs->path;
264
265 if ($new && -e $gs->{index}) {
266 unlink $gs->{index} or die
267 "Couldn't unlink index: $gs->{index}: $!\n";
268 }
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 : ''),
273 1, $editor, $pool);
274 my @lock = (::compare_svn_version('1.2.0') >= 0) ? (undef) : ();
275
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.
279 my $sp = '';
280 my $final = join('/', @pc);
281 while (@pc) {
282 $reporter->set_path($sp, $rev_b, 0, @lock, $pool);
283 $sp .= '/' if length $sp;
284 $sp .= shift @pc;
285 }
286 die "BUG: '$sp' != '$final'\n" if ($sp ne $final);
287
288 $reporter->set_path($sp, $rev_a, $new, @lock, $pool);
289
290 $reporter->finish_report($pool);
291 $pool->clear;
292 $editor->{git_commit_ok};
293}
294
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)
297sub gs_do_switch {
298 my ($self, $rev_a, $rev_b, $gs, $url_b, $editor) = @_;
299 my $path = $gs->path;
300 my $pool = SVN::Pool->new;
301
302 my $old_url = $self->url;
303 my $full_url = add_path_to_url( $self->url, $path );
304 my ($ra, $reparented);
305
306 if ($old_url =~ m#^svn(\+\w+)?://# ||
307 ($full_url =~ m#^https?://# &&
308 canonicalize_url($full_url) ne $full_url)) {
309 $_[0] = undef;
310 $self = undef;
311 $RA = undef;
312 $ra = Git::SVN::Ra->new($full_url);
313 $ra_invalid = 1;
314 } elsif ($old_url ne $full_url) {
315 SVN::_Ra::svn_ra_reparent(
316 $self->{session},
317 canonicalize_url($full_url),
318 $pool
319 );
320 $self->url($full_url);
321 $reparented = 1;
322 }
323
324 $ra ||= $self;
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);
330
331 if ($reparented) {
332 SVN::_Ra::svn_ra_reparent($self->{session}, $old_url, $pool);
333 $self->url($old_url);
334 }
335
336 $pool->clear;
337 $editor->{git_commit_ok};
338}
339
340sub longest_common_path {
341 my ($gsv, $globs) = @_;
342 my %common;
343 my $common_max = scalar @$gsv;
344
345 foreach my $gs (@$gsv) {
346 my @tmp = split m#/#, $gs->path;
347 my $p = '';
348 foreach (@tmp) {
349 $p .= length($p) ? "/$_" : $_;
350 $common{$p} ||= 0;
351 $common{$p}++;
352 }
353 }
354 $globs ||= [];
355 $common_max += scalar @$globs;
356 foreach my $glob (@$globs) {
357 my @tmp = split m#/#, $glob->{path}->{left};
358 my $p = '';
359 foreach (@tmp) {
360 $p .= length($p) ? "/$_" : $_;
361 $common{$p} ||= 0;
362 $common{$p}++;
363 }
364 }
365
366 my $longest_path = '';
367 foreach (sort {length $b <=> length $a} keys %common) {
368 if ($common{$_} == $common_max) {
369 $longest_path = $_;
370 last;
371 }
372 }
373 $longest_path;
374}
375
376sub 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;
384 while (1) {
385 my %revs;
386 my $err;
387 my $err_handler = $SVN::Error::handler;
388 $SVN::Error::handler = sub {
389 ($err) = @_;
390 skip_unknown_revs($err);
391 };
392 sub _cb {
393 my ($paths, $r, $author, $date, $log) = @_;
394 [ $paths,
395 { author => $author, date => $date, log => $log } ];
396 }
397 $self->get_log([$longest_path], $min, $max, 0, 1, 1,
398 sub { $revs{$_[1]} = _cb(@_) });
399 if ($err) {
400 print "Checked through r$max\r";
401 } else {
402 $find_trailing_edge = 1;
403 }
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";
411 my $hi = $max;
412 while (--$hi >= $min) {
413 my $ok;
414 $self->get_log([$longest_path], $min, $hi,
415 0, 1, 1, sub {
416 $ok = $_[1];
417 $revs{$_[1]} = _cb(@_) });
418 if ($ok) {
419 print STDERR "r$min .. r$ok OK\n";
420 last;
421 }
422 }
423 $find_trailing_edge = 0;
424 }
425 $SVN::Error::handler = $err_handler;
426
427 my %exists = map { $_->path => $_ } @$gsv;
428 foreach my $r (sort {$a <=> $b} keys %revs) {
429 my ($paths, $logged) = @{$revs{$r}};
430
431 foreach my $gs ($self->match_globs(\%exists, $paths,
432 $globs, $r)) {
433 if ($gs->rev_map_max >= $r) {
434 next;
435 }
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);
440 }
441 my $log_entry = $gs->do_fetch($paths, $r);
442 if ($log_entry) {
443 $gs->do_git_commit($log_entry);
444 }
445 $Git::SVN::INDEX_FILES{$gs->{index}} = 1;
446 }
447 foreach my $g (@$globs) {
448 my $k = "svn-remote.$g->{remote}." .
449 "$g->{t}-maxRev";
450 Git::SVN::tmp_config($k, $r);
451 }
452 if ($ra_invalid) {
453 $_[0] = undef;
454 $self = undef;
455 $RA = undef;
456 $self = Git::SVN::Ra->new($ra_url);
457 $ra_invalid = undef;
458 }
459 }
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);
466 }
467 foreach my $g (@$globs) {
468 my $k = "svn-remote.$g->{remote}.$g->{t}-maxRev";
469 Git::SVN::tmp_config($k, $max);
470 }
471 last if $max >= $head;
472 $min = $max + 1;
473 $max += $inc;
474 $max = $head if ($max > $head);
475 }
476 Git::SVN::gc();
477}
478
479sub get_dir_globbed {
480 my ($self, $left, $depth, $r) = @_;
481
482 my @x = eval { $self->get_dir($left, $r) };
483 return unless scalar @x == 3;
484 my $dirents = $x[0];
485 my @finalents;
486 foreach my $de (keys %$dirents) {
487 next if $dirents->{$de}->{kind} != $SVN::Node::dir;
488 if ($depth > 1) {
489 my @args = ("$left/$de", $depth - 1, $r);
490 foreach my $dir ($self->get_dir_globbed(@args)) {
491 push @finalents, "$de/$dir";
492 }
493 } else {
494 push @finalents, $de;
495 }
496 }
497 @finalents;
498}
499
500# return value: 0 -- don't ignore, 1 -- ignore
501sub is_ref_ignored {
502 my ($g, $p) = @_;
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;
508 return 0;
509}
510
511sub match_globs {
512 my ($self, $exists, $paths, $globs, $r) = @_;
513
514 sub get_dir_check {
515 my ($self, $exists, $g, $r) = @_;
516
517 my @dirs = $self->get_dir_globbed($g->{path}->{left},
518 $g->{path}->{depth},
519 $r);
520
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) !=
526 $SVN::Node::dir));
527 next unless $p =~ /$g->{path}->{regex}/;
528 $exists->{$p} = Git::SVN->init($self->url, $p, undef,
529 $g->{ref}->full_path($de), 1);
530 }
531 }
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);
536 }
537 }
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);
543 }
544 next unless /$g->{path}->{regex}/;
545 my $p = $1;
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) !=
550 $SVN::Node::dir);
551 $exists->{$pathname} = Git::SVN->init(
552 $self->url, $pathname, undef,
553 $g->{ref}->full_path($p), 1);
554 }
555 my $c = '';
556 foreach (split m#/#, $g->{path}->{left}) {
557 $c .= "/$_";
558 next unless ($paths->{$c} &&
559 ($paths->{$c}->{action} =~ /^[AR]$/));
560 get_dir_check($self, $exists, $g, $r);
561 }
562 }
563 values %$exists;
564}
565
566sub minimize_url {
567 my ($self) = @_;
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});
571 my $c = '';
572 do {
573 $url = add_path_to_url($url, $c);
574 eval {
575 my $ra = (ref $self)->new($url);
576 my $latest = $ra->get_latest_revnum;
577 $ra->get_log("", $latest, 0, 1, 0, 1, sub {});
578 };
579 } while ($@ && ($c = shift @components));
580
581 return canonicalize_url($url);
582}
583
584sub can_do_switch {
585 my $self = shift;
586 unless (defined $can_do_switch) {
587 my $pool = SVN::Pool->new;
588 my $rep = eval {
589 $self->do_switch(1, '', 0, $self->url,
590 SVN::Delta::Editor->new, $pool);
591 };
592 if ($@) {
593 $can_do_switch = 0;
594 } else {
595 $rep->abort_report($pool);
596 $can_do_switch = 1;
597 }
598 $pool->clear;
599 }
600 $can_do_switch;
601}
602
603sub skip_unknown_revs {
604 my ($err) = @_;
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
609 #
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 ",
626 "old history.\n",
627 "This may take a while on large repositories\n";
628 $ignored_err{$err_key} = 1;
629 }
630 return;
631 }
632 die "Error from SVN, ($errno): ", $err->expanded_message,"\n";
633}
634
6351;
636__END__
637
638=head1 NAME
639
640Git::SVN::Ra - Subversion remote access functions for git-svn
641
642=head1 SYNOPSIS
643
644 use Git::SVN::Ra;
645
646 my $ra = Git::SVN::Ra->new($branchurl);
647 my ($dirents, $fetched_revnum, $props) =
648 $ra->get_dir('.', $SVN::Core::INVALID_REVNUM);
649
650=head1 DESCRIPTION
651
652This is a wrapper around the L<SVN::Ra> module for use by B<git-svn>.
653It fills in some default parameters (such as the authentication
654scheme), smooths over incompatibilities between libsvn versions, adds
655caching, and implements some functions specific to B<git-svn>.
656
657Do not use it unless you are developing git-svn. The interface will
658change as git-svn evolves.
659
660=head1 DEPENDENCIES
661
662Subversion perl bindings,
663L<Git::SVN>.
664
665C<Git::SVN::Ra> has not been tested using callers other than
666B<git-svn> itself.
667
668=head1 SEE ALSO
669
670L<SVN::Ra>.
671
672=head1 INCOMPATIBILITIES
673
674None reported.
675
676=head1 BUGS
677
678None.