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);
9
10use SVN::Ra;
11BEGIN {
12 @ISA = qw(SVN::Ra);
13}
14
15my ($ra_invalid, $can_do_switch, %ignored_err, $RA);
16
17BEGIN {
18 # enforce temporary pool usage for some simple functions
19 no strict 'refs';
20 for my $f (qw/rev_proplist get_latest_revnum get_uuid get_repos_root
21 get_file/) {
22 my $SUPER = "SUPER::$f";
23 *$f = sub {
24 my $self = shift;
25 my $pool = SVN::Pool->new;
26 my @ret = $self->$SUPER(@_,$pool);
27 $pool->clear;
28 wantarray ? @ret : $ret[0];
29 };
30 }
31}
32
33sub _auth_providers () {
34 my @rv = (
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)
50 );
51
52 # earlier 1.6.x versions would segfault, and <= 1.5.x didn't have
53 # this function
54 if (::compare_svn_version('1.6.15') >= 0) {
55 my $config = SVN::Core::config_get_config($config_dir);
56 my ($p, @a);
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
64 unshift @rv, @$p;
65 }
66 \@rv;
67}
68
69
70sub new {
71 my ($class, $url) = @_;
72 $url = canonicalize_url($url);
73 return $RA if ($RA && $RA->url eq $url);
74
75 ::_req_svn();
76
77 SVN::_Core::svn_config_ensure($config_dir, undef);
78 my ($baton, $callbacks) = SVN::Core::auth_open_helper(_auth_providers);
79 my $config = SVN::Core::config_get_config($config_dir);
80 $RA = undef;
81 my $dont_store_passwords = 1;
82 my $conf_t = ${$config}{'config'};
83 {
84 no warnings 'once';
85 # The usage of $SVN::_Core::SVN_CONFIG_* variables
86 # produces warnings that variables are used only once.
87 # I had not found the better way to shut them up, so
88 # the warnings of type 'once' are disabled in this block.
89 if (SVN::_Core::svn_config_get_bool($conf_t,
90 $SVN::_Core::SVN_CONFIG_SECTION_AUTH,
91 $SVN::_Core::SVN_CONFIG_OPTION_STORE_PASSWORDS,
92 1) == 0) {
93 SVN::_Core::svn_auth_set_parameter($baton,
94 $SVN::_Core::SVN_AUTH_PARAM_DONT_STORE_PASSWORDS,
95 bless (\$dont_store_passwords, "_p_void"));
96 }
97 if (SVN::_Core::svn_config_get_bool($conf_t,
98 $SVN::_Core::SVN_CONFIG_SECTION_AUTH,
99 $SVN::_Core::SVN_CONFIG_OPTION_STORE_AUTH_CREDS,
100 1) == 0) {
101 $Git::SVN::Prompt::_no_auth_cache = 1;
102 }
103 } # no warnings 'once'
104 my $self = SVN::Ra->new(url => $url, auth => $baton,
105 config => $config,
106 pool => SVN::Pool->new,
107 auth_provider_callbacks => $callbacks);
108 $RA = bless $self, $class;
109
110 # Make sure its canonicalized
111 $self->url($url);
112 $self->{svn_path} = $url;
113 $self->{repos_root} = $self->get_repos_root;
114 $self->{svn_path} =~ s#^\Q$self->{repos_root}\E(/|$)##;
115 $self->{cache} = { check_path => { r => 0, data => {} },
116 get_dir => { r => 0, data => {} } };
117
118 return $RA;
119}
120
121sub url {
122 my $self = shift;
123
124 if (@_) {
125 my $url = shift;
126 $self->{url} = canonicalize_url($url);
127 return;
128 }
129
130 return $self->{url};
131}
132
133sub check_path {
134 my ($self, $path, $r) = @_;
135 my $cache = $self->{cache}->{check_path};
136 if ($r == $cache->{r} && exists $cache->{data}->{$path}) {
137 return $cache->{data}->{$path};
138 }
139 my $pool = SVN::Pool->new;
140 my $t = $self->SUPER::check_path($path, $r, $pool);
141 $pool->clear;
142 if ($r != $cache->{r}) {
143 %{$cache->{data}} = ();
144 $cache->{r} = $r;
145 }
146 $cache->{data}->{$path} = $t;
147}
148
149sub get_dir {
150 my ($self, $dir, $r) = @_;
151 my $cache = $self->{cache}->{get_dir};
152 if ($r == $cache->{r}) {
153 if (my $x = $cache->{data}->{$dir}) {
154 return wantarray ? @$x : $x->[0];
155 }
156 }
157 my $pool = SVN::Pool->new;
158 my ($d, undef, $props) = $self->SUPER::get_dir($dir, $r, $pool);
159 my %dirents = map { $_ => { kind => $d->{$_}->kind } } keys %$d;
160 $pool->clear;
161 if ($r != $cache->{r}) {
162 %{$cache->{data}} = ();
163 $cache->{r} = $r;
164 }
165 $cache->{data}->{$dir} = [ \%dirents, $r, $props ];
166 wantarray ? (\%dirents, $r, $props) : \%dirents;
167}
168
169sub DESTROY {
170 # do not call the real DESTROY since we store ourselves in $RA
171}
172
173# get_log(paths, start, end, limit,
174# discover_changed_paths, strict_node_history, receiver)
175sub get_log {
176 my ($self, @args) = @_;
177 my $pool = SVN::Pool->new;
178
179 # svn_log_changed_path_t objects passed to get_log are likely to be
180 # overwritten even if only the refs are copied to an external variable,
181 # so we should dup the structures in their entirety. Using an
182 # externally passed pool (instead of our temporary and quickly cleared
183 # pool in Git::SVN::Ra) does not help matters at all...
184 my $receiver = pop @args;
185 my $prefix = "/".$self->{svn_path};
186 $prefix =~ s#/+($)##;
187 my $prefix_regex = qr#^\Q$prefix\E#;
188 push(@args, sub {
189 my ($paths) = $_[0];
190 return &$receiver(@_) unless $paths;
191 $_[0] = ();
192 foreach my $p (keys %$paths) {
193 my $i = $paths->{$p};
194 # Make path relative to our url, not repos_root
195 $p =~ s/$prefix_regex//;
196 my %s = map { $_ => $i->$_; }
197 qw/copyfrom_path copyfrom_rev action/;
198 if ($s{'copyfrom_path'}) {
199 $s{'copyfrom_path'} =~ s/$prefix_regex//;
200 }
201 $_[0]{$p} = \%s;
202 }
203 &$receiver(@_);
204 });
205
206
207 # the limit parameter was not supported in SVN 1.1.x, so we
208 # drop it. Therefore, the receiver callback passed to it
209 # is made aware of this limitation by being wrapped if
210 # the limit passed to is being wrapped.
211 if (::compare_svn_version('1.2.0') <= 0) {
212 my $limit = splice(@args, 3, 1);
213 if ($limit > 0) {
214 my $receiver = pop @args;
215 push(@args, sub { &$receiver(@_) if (--$limit >= 0) });
216 }
217 }
218 my $ret = $self->SUPER::get_log(@args, $pool);
219 $pool->clear;
220 $ret;
221}
222
223sub trees_match {
224 my ($self, $url1, $rev1, $url2, $rev2) = @_;
225 my $ctx = SVN::Client->new(auth => _auth_providers);
226 my $out = IO::File->new_tmpfile;
227
228 # older SVN (1.1.x) doesn't take $pool as the last parameter for
229 # $ctx->diff(), so we'll create a default one
230 my $pool = SVN::Pool->new_default_sub;
231
232 $ra_invalid = 1; # this will open a new SVN::Ra connection to $url1
233 $ctx->diff([], $url1, $rev1, $url2, $rev2, 1, 1, 0, $out, $out);
234 $out->flush;
235 my $ret = (($out->stat)[7] == 0);
236 close $out or croak $!;
237
238 $ret;
239}
240
241sub get_commit_editor {
242 my ($self, $log, $cb, $pool) = @_;
243
244 my @lock = (::compare_svn_version('1.2.0') >= 0) ? (undef, 0) : ();
245 $self->SUPER::get_commit_editor($log, $cb, @lock, $pool);
246}
247
248sub gs_do_update {
249 my ($self, $rev_a, $rev_b, $gs, $editor) = @_;
250 my $new = ($rev_a == $rev_b);
251 my $path = $gs->path;
252
253 if ($new && -e $gs->{index}) {
254 unlink $gs->{index} or die
255 "Couldn't unlink index: $gs->{index}: $!\n";
256 }
257 my $pool = SVN::Pool->new;
258 $editor->set_path_strip($path);
259 my (@pc) = split m#/#, $path;
260 my $reporter = $self->do_update($rev_b, (@pc ? shift @pc : ''),
261 1, $editor, $pool);
262 my @lock = (::compare_svn_version('1.2.0') >= 0) ? (undef) : ();
263
264 # Since we can't rely on svn_ra_reparent being available, we'll
265 # just have to do some magic with set_path to make it so
266 # we only want a partial path.
267 my $sp = '';
268 my $final = join('/', @pc);
269 while (@pc) {
270 $reporter->set_path($sp, $rev_b, 0, @lock, $pool);
271 $sp .= '/' if length $sp;
272 $sp .= shift @pc;
273 }
274 die "BUG: '$sp' != '$final'\n" if ($sp ne $final);
275
276 $reporter->set_path($sp, $rev_a, $new, @lock, $pool);
277
278 $reporter->finish_report($pool);
279 $pool->clear;
280 $editor->{git_commit_ok};
281}
282
283# this requires SVN 1.4.3 or later (do_switch didn't work before 1.4.3, and
284# svn_ra_reparent didn't work before 1.4)
285sub gs_do_switch {
286 my ($self, $rev_a, $rev_b, $gs, $url_b, $editor) = @_;
287 my $path = $gs->path;
288 my $pool = SVN::Pool->new;
289
290 my $full_url = $self->url;
291 my $old_url = $full_url;
292 $full_url .= '/' . $path if length $path;
293 my ($ra, $reparented);
294
295 if ($old_url =~ m#^svn(\+ssh)?://# ||
296 ($full_url =~ m#^https?://# &&
297 canonicalize_url($full_url) ne $full_url)) {
298 $_[0] = undef;
299 $self = undef;
300 $RA = undef;
301 $ra = Git::SVN::Ra->new($full_url);
302 $ra_invalid = 1;
303 } elsif ($old_url ne $full_url) {
304 SVN::_Ra::svn_ra_reparent($self->{session}, $full_url, $pool);
305 $self->url($full_url);
306 $reparented = 1;
307 }
308
309 $ra ||= $self;
310 $url_b = canonicalize_url($url_b);
311 my $reporter = $ra->do_switch($rev_b, '', 1, $url_b, $editor, $pool);
312 my @lock = (::compare_svn_version('1.2.0') >= 0) ? (undef) : ();
313 $reporter->set_path('', $rev_a, 0, @lock, $pool);
314 $reporter->finish_report($pool);
315
316 if ($reparented) {
317 SVN::_Ra::svn_ra_reparent($self->{session}, $old_url, $pool);
318 $self->url($old_url);
319 }
320
321 $pool->clear;
322 $editor->{git_commit_ok};
323}
324
325sub longest_common_path {
326 my ($gsv, $globs) = @_;
327 my %common;
328 my $common_max = scalar @$gsv;
329
330 foreach my $gs (@$gsv) {
331 my @tmp = split m#/#, $gs->path;
332 my $p = '';
333 foreach (@tmp) {
334 $p .= length($p) ? "/$_" : $_;
335 $common{$p} ||= 0;
336 $common{$p}++;
337 }
338 }
339 $globs ||= [];
340 $common_max += scalar @$globs;
341 foreach my $glob (@$globs) {
342 my @tmp = split m#/#, $glob->{path}->{left};
343 my $p = '';
344 foreach (@tmp) {
345 $p .= length($p) ? "/$_" : $_;
346 $common{$p} ||= 0;
347 $common{$p}++;
348 }
349 }
350
351 my $longest_path = '';
352 foreach (sort {length $b <=> length $a} keys %common) {
353 if ($common{$_} == $common_max) {
354 $longest_path = $_;
355 last;
356 }
357 }
358 $longest_path;
359}
360
361sub gs_fetch_loop_common {
362 my ($self, $base, $head, $gsv, $globs) = @_;
363 return if ($base > $head);
364 my $inc = $_log_window_size;
365 my ($min, $max) = ($base, $head < $base + $inc ? $head : $base + $inc);
366 my $longest_path = longest_common_path($gsv, $globs);
367 my $ra_url = $self->url;
368 my $find_trailing_edge;
369 while (1) {
370 my %revs;
371 my $err;
372 my $err_handler = $SVN::Error::handler;
373 $SVN::Error::handler = sub {
374 ($err) = @_;
375 skip_unknown_revs($err);
376 };
377 sub _cb {
378 my ($paths, $r, $author, $date, $log) = @_;
379 [ $paths,
380 { author => $author, date => $date, log => $log } ];
381 }
382 $self->get_log([$longest_path], $min, $max, 0, 1, 1,
383 sub { $revs{$_[1]} = _cb(@_) });
384 if ($err) {
385 print "Checked through r$max\r";
386 } else {
387 $find_trailing_edge = 1;
388 }
389 if ($err and $find_trailing_edge) {
390 print STDERR "Path '$longest_path' ",
391 "was probably deleted:\n",
392 $err->expanded_message,
393 "\nWill attempt to follow ",
394 "revisions r$min .. r$max ",
395 "committed before the deletion\n";
396 my $hi = $max;
397 while (--$hi >= $min) {
398 my $ok;
399 $self->get_log([$longest_path], $min, $hi,
400 0, 1, 1, sub {
401 $ok = $_[1];
402 $revs{$_[1]} = _cb(@_) });
403 if ($ok) {
404 print STDERR "r$min .. r$ok OK\n";
405 last;
406 }
407 }
408 $find_trailing_edge = 0;
409 }
410 $SVN::Error::handler = $err_handler;
411
412 my %exists = map { $_->{path} => $_ } @$gsv;
413 foreach my $r (sort {$a <=> $b} keys %revs) {
414 my ($paths, $logged) = @{$revs{$r}};
415
416 foreach my $gs ($self->match_globs(\%exists, $paths,
417 $globs, $r)) {
418 if ($gs->rev_map_max >= $r) {
419 next;
420 }
421 next unless $gs->match_paths($paths, $r);
422 $gs->{logged_rev_props} = $logged;
423 if (my $last_commit = $gs->last_commit) {
424 $gs->assert_index_clean($last_commit);
425 }
426 my $log_entry = $gs->do_fetch($paths, $r);
427 if ($log_entry) {
428 $gs->do_git_commit($log_entry);
429 }
430 $Git::SVN::INDEX_FILES{$gs->{index}} = 1;
431 }
432 foreach my $g (@$globs) {
433 my $k = "svn-remote.$g->{remote}." .
434 "$g->{t}-maxRev";
435 Git::SVN::tmp_config($k, $r);
436 }
437 if ($ra_invalid) {
438 $_[0] = undef;
439 $self = undef;
440 $RA = undef;
441 $self = Git::SVN::Ra->new($ra_url);
442 $ra_invalid = undef;
443 }
444 }
445 # pre-fill the .rev_db since it'll eventually get filled in
446 # with '0' x40 if something new gets committed
447 foreach my $gs (@$gsv) {
448 next if $gs->rev_map_max >= $max;
449 next if defined $gs->rev_map_get($max);
450 $gs->rev_map_set($max, 0 x40);
451 }
452 foreach my $g (@$globs) {
453 my $k = "svn-remote.$g->{remote}.$g->{t}-maxRev";
454 Git::SVN::tmp_config($k, $max);
455 }
456 last if $max >= $head;
457 $min = $max + 1;
458 $max += $inc;
459 $max = $head if ($max > $head);
460 }
461 Git::SVN::gc();
462}
463
464sub get_dir_globbed {
465 my ($self, $left, $depth, $r) = @_;
466
467 my @x = eval { $self->get_dir($left, $r) };
468 return unless scalar @x == 3;
469 my $dirents = $x[0];
470 my @finalents;
471 foreach my $de (keys %$dirents) {
472 next if $dirents->{$de}->{kind} != $SVN::Node::dir;
473 if ($depth > 1) {
474 my @args = ("$left/$de", $depth - 1, $r);
475 foreach my $dir ($self->get_dir_globbed(@args)) {
476 push @finalents, "$de/$dir";
477 }
478 } else {
479 push @finalents, $de;
480 }
481 }
482 @finalents;
483}
484
485# return value: 0 -- don't ignore, 1 -- ignore
486sub is_ref_ignored {
487 my ($g, $p) = @_;
488 my $refname = $g->{ref}->full_path($p);
489 return 1 if defined($g->{ignore_refs_regex}) &&
490 $refname =~ m!$g->{ignore_refs_regex}!;
491 return 0 unless defined($_ignore_refs_regex);
492 return 1 if $refname =~ m!$_ignore_refs_regex!o;
493 return 0;
494}
495
496sub match_globs {
497 my ($self, $exists, $paths, $globs, $r) = @_;
498
499 sub get_dir_check {
500 my ($self, $exists, $g, $r) = @_;
501
502 my @dirs = $self->get_dir_globbed($g->{path}->{left},
503 $g->{path}->{depth},
504 $r);
505
506 foreach my $de (@dirs) {
507 my $p = $g->{path}->full_path($de);
508 next if $exists->{$p};
509 next if (length $g->{path}->{right} &&
510 ($self->check_path($p, $r) !=
511 $SVN::Node::dir));
512 next unless $p =~ /$g->{path}->{regex}/;
513 $exists->{$p} = Git::SVN->init($self->url, $p, undef,
514 $g->{ref}->full_path($de), 1);
515 }
516 }
517 foreach my $g (@$globs) {
518 if (my $path = $paths->{"/$g->{path}->{left}"}) {
519 if ($path->{action} =~ /^[AR]$/) {
520 get_dir_check($self, $exists, $g, $r);
521 }
522 }
523 foreach (keys %$paths) {
524 if (/$g->{path}->{left_regex}/ &&
525 !/$g->{path}->{regex}/) {
526 next if $paths->{$_}->{action} !~ /^[AR]$/;
527 get_dir_check($self, $exists, $g, $r);
528 }
529 next unless /$g->{path}->{regex}/;
530 my $p = $1;
531 my $pathname = $g->{path}->full_path($p);
532 next if is_ref_ignored($g, $p);
533 next if $exists->{$pathname};
534 next if ($self->check_path($pathname, $r) !=
535 $SVN::Node::dir);
536 $exists->{$pathname} = Git::SVN->init(
537 $self->url, $pathname, undef,
538 $g->{ref}->full_path($p), 1);
539 }
540 my $c = '';
541 foreach (split m#/#, $g->{path}->{left}) {
542 $c .= "/$_";
543 next unless ($paths->{$c} &&
544 ($paths->{$c}->{action} =~ /^[AR]$/));
545 get_dir_check($self, $exists, $g, $r);
546 }
547 }
548 values %$exists;
549}
550
551sub minimize_url {
552 my ($self) = @_;
553 return $self->url if ($self->url eq $self->{repos_root});
554 my $url = $self->{repos_root};
555 my @components = split(m!/!, $self->{svn_path});
556 my $c = '';
557 do {
558 $url .= "/$c" if length $c;
559 eval {
560 my $ra = (ref $self)->new($url);
561 my $latest = $ra->get_latest_revnum;
562 $ra->get_log("", $latest, 0, 1, 0, 1, sub {});
563 };
564 } while ($@ && ($c = shift @components));
565
566 return canonicalize_url($url);
567}
568
569sub can_do_switch {
570 my $self = shift;
571 unless (defined $can_do_switch) {
572 my $pool = SVN::Pool->new;
573 my $rep = eval {
574 $self->do_switch(1, '', 0, $self->url,
575 SVN::Delta::Editor->new, $pool);
576 };
577 if ($@) {
578 $can_do_switch = 0;
579 } else {
580 $rep->abort_report($pool);
581 $can_do_switch = 1;
582 }
583 $pool->clear;
584 }
585 $can_do_switch;
586}
587
588sub skip_unknown_revs {
589 my ($err) = @_;
590 my $errno = $err->apr_err();
591 # Maybe the branch we're tracking didn't
592 # exist when the repo started, so it's
593 # not an error if it doesn't, just continue
594 #
595 # Wonderfully consistent library, eh?
596 # 160013 - svn:// and file://
597 # 175002 - http(s)://
598 # 175007 - http(s):// (this repo required authorization, too...)
599 # More codes may be discovered later...
600 if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
601 my $err_key = $err->expanded_message;
602 # revision numbers change every time, filter them out
603 $err_key =~ s/\d+/\0/g;
604 $err_key = "$errno\0$err_key";
605 unless ($ignored_err{$err_key}) {
606 warn "W: Ignoring error from SVN, path probably ",
607 "does not exist: ($errno): ",
608 $err->expanded_message,"\n";
609 warn "W: Do not be alarmed at the above message ",
610 "git-svn is just searching aggressively for ",
611 "old history.\n",
612 "This may take a while on large repositories\n";
613 $ignored_err{$err_key} = 1;
614 }
615 return;
616 }
617 die "Error from SVN, ($errno): ", $err->expanded_message,"\n";
618}
619
6201;
621__END__
622
623Git::SVN::Ra - Subversion remote access functions for git-svn
624
625=head1 SYNOPSIS
626
627 use Git::SVN::Ra;
628
629 my $ra = Git::SVN::Ra->new($branchurl);
630 my ($dirents, $fetched_revnum, $props) =
631 $ra->get_dir('.', $SVN::Core::INVALID_REVNUM);
632
633=head1 DESCRIPTION
634
635This is a wrapper around the L<SVN::Ra> module for use by B<git-svn>.
636It fills in some default parameters (such as the authentication
637scheme), smooths over incompatibilities between libsvn versions, adds
638caching, and implements some functions specific to B<git-svn>.
639
640Do not use it unless you are developing git-svn. The interface will
641change as git-svn evolves.
642
643=head1 DEPENDENCIES
644
645Subversion perl bindings,
646L<Git::SVN>.
647
648C<Git::SVN::Ra> has not been tested using callers other than
649B<git-svn> itself.
650
651=head1 SEE ALSO
652
653L<SVN::Ra>.
654
655=head1 INCOMPATIBILITIES
656
657None reported.
658
659=head1 BUGS
660
661None.