1828519b4a02c6f5b6eb1974c60cfc2ef2c0d0f7
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 $gpool = SVN::Pool->new_default;
380 my $ra_url = $self->url;
381 my $reload_ra = sub {
382 $_[0] = undef;
383 $self = undef;
384 $RA = undef;
385 $gpool->clear;
386 $self = Git::SVN::Ra->new($ra_url);
387 $ra_invalid = undef;
388 };
389 my $inc = $_log_window_size;
390 my ($min, $max) = ($base, $head < $base + $inc ? $head : $base + $inc);
391 my $longest_path = longest_common_path($gsv, $globs);
392 my $find_trailing_edge;
393 while (1) {
394 my %revs;
395 my $err;
396 my $err_handler = $SVN::Error::handler;
397 $SVN::Error::handler = sub {
398 ($err) = @_;
399 skip_unknown_revs($err);
400 };
401 sub _cb {
402 my ($paths, $r, $author, $date, $log) = @_;
403 [ $paths,
404 { author => $author, date => $date, log => $log } ];
405 }
406 $self->get_log([$longest_path], $min, $max, 0, 1, 1,
407 sub { $revs{$_[1]} = _cb(@_) });
408 if ($err) {
409 print "Checked through r$max\r";
410 } else {
411 $find_trailing_edge = 1;
412 }
413 if ($err and $find_trailing_edge) {
414 print STDERR "Path '$longest_path' ",
415 "was probably deleted:\n",
416 $err->expanded_message,
417 "\nWill attempt to follow ",
418 "revisions r$min .. r$max ",
419 "committed before the deletion\n";
420 my $hi = $max;
421 while (--$hi >= $min) {
422 my $ok;
423 $self->get_log([$longest_path], $min, $hi,
424 0, 1, 1, sub {
425 $ok = $_[1];
426 $revs{$_[1]} = _cb(@_) });
427 if ($ok) {
428 print STDERR "r$min .. r$ok OK\n";
429 last;
430 }
431 }
432 $find_trailing_edge = 0;
433 }
434 $SVN::Error::handler = $err_handler;
435
436 my %exists = map { $_->path => $_ } @$gsv;
437 foreach my $r (sort {$a <=> $b} keys %revs) {
438 my ($paths, $logged) = @{$revs{$r}};
439
440 foreach my $gs ($self->match_globs(\%exists, $paths,
441 $globs, $r)) {
442 if ($gs->rev_map_max >= $r) {
443 next;
444 }
445 next unless $gs->match_paths($paths, $r);
446 $gs->{logged_rev_props} = $logged;
447 if (my $last_commit = $gs->last_commit) {
448 $gs->assert_index_clean($last_commit);
449 }
450 my $log_entry = $gs->do_fetch($paths, $r);
451 if ($log_entry) {
452 $gs->do_git_commit($log_entry);
453 }
454 $Git::SVN::INDEX_FILES{$gs->{index}} = 1;
455 }
456 foreach my $g (@$globs) {
457 my $k = "svn-remote.$g->{remote}." .
458 "$g->{t}-maxRev";
459 Git::SVN::tmp_config($k, $r);
460 }
461 $reload_ra->() if $ra_invalid;
462 }
463 # pre-fill the .rev_db since it'll eventually get filled in
464 # with '0' x40 if something new gets committed
465 foreach my $gs (@$gsv) {
466 next if $gs->rev_map_max >= $max;
467 next if defined $gs->rev_map_get($max);
468 $gs->rev_map_set($max, 0 x40);
469 }
470 foreach my $g (@$globs) {
471 my $k = "svn-remote.$g->{remote}.$g->{t}-maxRev";
472 Git::SVN::tmp_config($k, $max);
473 }
474 last if $max >= $head;
475 $min = $max + 1;
476 $max += $inc;
477 $max = $head if ($max > $head);
478
479 $reload_ra->();
480 }
481 Git::SVN::gc();
482}
483
484sub get_dir_globbed {
485 my ($self, $left, $depth, $r) = @_;
486
487 my @x = eval { $self->get_dir($left, $r) };
488 return unless scalar @x == 3;
489 my $dirents = $x[0];
490 my @finalents;
491 foreach my $de (keys %$dirents) {
492 next if $dirents->{$de}->{kind} != $SVN::Node::dir;
493 if ($depth > 1) {
494 my @args = ("$left/$de", $depth - 1, $r);
495 foreach my $dir ($self->get_dir_globbed(@args)) {
496 push @finalents, "$de/$dir";
497 }
498 } else {
499 push @finalents, $de;
500 }
501 }
502 @finalents;
503}
504
505# return value: 0 -- don't ignore, 1 -- ignore
506sub is_ref_ignored {
507 my ($g, $p) = @_;
508 my $refname = $g->{ref}->full_path($p);
509 return 1 if defined($g->{ignore_refs_regex}) &&
510 $refname =~ m!$g->{ignore_refs_regex}!;
511 return 0 unless defined($_ignore_refs_regex);
512 return 1 if $refname =~ m!$_ignore_refs_regex!o;
513 return 0;
514}
515
516sub match_globs {
517 my ($self, $exists, $paths, $globs, $r) = @_;
518
519 sub get_dir_check {
520 my ($self, $exists, $g, $r) = @_;
521
522 my @dirs = $self->get_dir_globbed($g->{path}->{left},
523 $g->{path}->{depth},
524 $r);
525
526 foreach my $de (@dirs) {
527 my $p = $g->{path}->full_path($de);
528 next if $exists->{$p};
529 next if (length $g->{path}->{right} &&
530 ($self->check_path($p, $r) !=
531 $SVN::Node::dir));
532 next unless $p =~ /$g->{path}->{regex}/;
533 $exists->{$p} = Git::SVN->init($self->url, $p, undef,
534 $g->{ref}->full_path($de), 1);
535 }
536 }
537 foreach my $g (@$globs) {
538 if (my $path = $paths->{"/$g->{path}->{left}"}) {
539 if ($path->{action} =~ /^[AR]$/) {
540 get_dir_check($self, $exists, $g, $r);
541 }
542 }
543 foreach (keys %$paths) {
544 if (/$g->{path}->{left_regex}/ &&
545 !/$g->{path}->{regex}/) {
546 next if $paths->{$_}->{action} !~ /^[AR]$/;
547 get_dir_check($self, $exists, $g, $r);
548 }
549 next unless /$g->{path}->{regex}/;
550 my $p = $1;
551 my $pathname = $g->{path}->full_path($p);
552 next if is_ref_ignored($g, $p);
553 next if $exists->{$pathname};
554 next if ($self->check_path($pathname, $r) !=
555 $SVN::Node::dir);
556 $exists->{$pathname} = Git::SVN->init(
557 $self->url, $pathname, undef,
558 $g->{ref}->full_path($p), 1);
559 }
560 my $c = '';
561 foreach (split m#/#, $g->{path}->{left}) {
562 $c .= "/$_";
563 next unless ($paths->{$c} &&
564 ($paths->{$c}->{action} =~ /^[AR]$/));
565 get_dir_check($self, $exists, $g, $r);
566 }
567 }
568 values %$exists;
569}
570
571sub minimize_url {
572 my ($self) = @_;
573 return $self->url if ($self->url eq $self->{repos_root});
574 my $url = $self->{repos_root};
575 my @components = split(m!/!, $self->{svn_path});
576 my $c = '';
577 do {
578 $url = add_path_to_url($url, $c);
579 eval {
580 my $ra = (ref $self)->new($url);
581 my $latest = $ra->get_latest_revnum;
582 $ra->get_log("", $latest, 0, 1, 0, 1, sub {});
583 };
584 } while ($@ && ($c = shift @components));
585
586 return canonicalize_url($url);
587}
588
589sub can_do_switch {
590 my $self = shift;
591 unless (defined $can_do_switch) {
592 my $pool = SVN::Pool->new;
593 my $rep = eval {
594 $self->do_switch(1, '', 0, $self->url,
595 SVN::Delta::Editor->new, $pool);
596 };
597 if ($@) {
598 $can_do_switch = 0;
599 } else {
600 $rep->abort_report($pool);
601 $can_do_switch = 1;
602 }
603 $pool->clear;
604 }
605 $can_do_switch;
606}
607
608sub skip_unknown_revs {
609 my ($err) = @_;
610 my $errno = $err->apr_err();
611 # Maybe the branch we're tracking didn't
612 # exist when the repo started, so it's
613 # not an error if it doesn't, just continue
614 #
615 # Wonderfully consistent library, eh?
616 # 160013 - svn:// and file://
617 # 175002 - http(s)://
618 # 175007 - http(s):// (this repo required authorization, too...)
619 # More codes may be discovered later...
620 if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
621 my $err_key = $err->expanded_message;
622 # revision numbers change every time, filter them out
623 $err_key =~ s/\d+/\0/g;
624 $err_key = "$errno\0$err_key";
625 unless ($ignored_err{$err_key}) {
626 warn "W: Ignoring error from SVN, path probably ",
627 "does not exist: ($errno): ",
628 $err->expanded_message,"\n";
629 warn "W: Do not be alarmed at the above message ",
630 "git-svn is just searching aggressively for ",
631 "old history.\n",
632 "This may take a while on large repositories\n";
633 $ignored_err{$err_key} = 1;
634 }
635 return;
636 }
637 die "Error from SVN, ($errno): ", $err->expanded_message,"\n";
638}
639
6401;
641__END__
642
643=head1 NAME
644
645Git::SVN::Ra - Subversion remote access functions for git-svn
646
647=head1 SYNOPSIS
648
649 use Git::SVN::Ra;
650
651 my $ra = Git::SVN::Ra->new($branchurl);
652 my ($dirents, $fetched_revnum, $props) =
653 $ra->get_dir('.', $SVN::Core::INVALID_REVNUM);
654
655=head1 DESCRIPTION
656
657This is a wrapper around the L<SVN::Ra> module for use by B<git-svn>.
658It fills in some default parameters (such as the authentication
659scheme), smooths over incompatibilities between libsvn versions, adds
660caching, and implements some functions specific to B<git-svn>.
661
662Do not use it unless you are developing git-svn. The interface will
663change as git-svn evolves.
664
665=head1 DEPENDENCIES
666
667Subversion perl bindings,
668L<Git::SVN>.
669
670C<Git::SVN::Ra> has not been tested using callers other than
671B<git-svn> itself.
672
673=head1 SEE ALSO
674
675L<SVN::Ra>.
676
677=head1 INCOMPATIBILITIES
678
679None reported.
680
681=head1 BUGS
682
683None.