git-svn: canonicalize earlier
[git/mingw/j6t.git] / perl / Git / SVN / Ra.pm
blobc88b2b91daad10ab565745e5659c0f110da6dbec
1 package Git::SVN::Ra;
2 use vars qw/@ISA $config_dir $_ignore_refs_regex $_log_window_size/;
3 use strict;
4 use warnings;
5 use SVN::Client;
6 use Git::SVN::Utils qw(
7 canonicalize_url
8 );
10 use SVN::Ra;
11 BEGIN {
12 @ISA = qw(SVN::Ra);
15 my ($ra_invalid, $can_do_switch, %ignored_err, $RA);
17 BEGIN {
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];
33 sub _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)
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;
66 \@rv;
70 sub new {
71 my ($class, $url) = @_;
72 $url = canonicalize_url($url);
73 return $RA if ($RA && $RA->url eq $url);
75 ::_req_svn();
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'};
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"));
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;
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;
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 => {} } };
118 return $RA;
121 sub url {
122 my $self = shift;
124 if (@_) {
125 my $url = shift;
126 $self->{url} = canonicalize_url($url);
127 return;
130 return $self->{url};
133 sub 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};
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;
146 $cache->{data}->{$path} = $t;
149 sub 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];
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;
165 $cache->{data}->{$dir} = [ \%dirents, $r, $props ];
166 wantarray ? (\%dirents, $r, $props) : \%dirents;
169 sub DESTROY {
170 # do not call the real DESTROY since we store ourselves in $RA
173 # get_log(paths, start, end, limit,
174 # discover_changed_paths, strict_node_history, receiver)
175 sub get_log {
176 my ($self, @args) = @_;
177 my $pool = SVN::Pool->new;
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//;
201 $_[0]{$p} = \%s;
203 &$receiver(@_);
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) });
218 my $ret = $self->SUPER::get_log(@args, $pool);
219 $pool->clear;
220 $ret;
223 sub 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;
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;
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 $!;
238 $ret;
241 sub get_commit_editor {
242 my ($self, $log, $cb, $pool) = @_;
244 my @lock = (::compare_svn_version('1.2.0') >= 0) ? (undef, 0) : ();
245 $self->SUPER::get_commit_editor($log, $cb, @lock, $pool);
248 sub gs_do_update {
249 my ($self, $rev_a, $rev_b, $gs, $editor) = @_;
250 my $new = ($rev_a == $rev_b);
251 my $path = $gs->path;
253 if ($new && -e $gs->{index}) {
254 unlink $gs->{index} or die
255 "Couldn't unlink index: $gs->{index}: $!\n";
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) : ();
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;
274 die "BUG: '$sp' != '$final'\n" if ($sp ne $final);
276 $reporter->set_path($sp, $rev_a, $new, @lock, $pool);
278 $reporter->finish_report($pool);
279 $pool->clear;
280 $editor->{git_commit_ok};
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)
285 sub 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;
290 my $full_url = $self->url;
291 my $old_url = $full_url;
292 $full_url .= '/' . $path if length $path;
293 my ($ra, $reparented);
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;
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);
316 if ($reparented) {
317 SVN::_Ra::svn_ra_reparent($self->{session}, $old_url, $pool);
318 $self->url($old_url);
321 $pool->clear;
322 $editor->{git_commit_ok};
325 sub longest_common_path {
326 my ($gsv, $globs) = @_;
327 my %common;
328 my $common_max = scalar @$gsv;
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}++;
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}++;
351 my $longest_path = '';
352 foreach (sort {length $b <=> length $a} keys %common) {
353 if ($common{$_} == $common_max) {
354 $longest_path = $_;
355 last;
358 $longest_path;
361 sub 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);
377 sub _cb {
378 my ($paths, $r, $author, $date, $log) = @_;
379 [ $paths,
380 { author => $author, date => $date, log => $log } ];
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;
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;
408 $find_trailing_edge = 0;
410 $SVN::Error::handler = $err_handler;
412 my %exists = map { $_->{path} => $_ } @$gsv;
413 foreach my $r (sort {$a <=> $b} keys %revs) {
414 my ($paths, $logged) = @{$revs{$r}};
416 foreach my $gs ($self->match_globs(\%exists, $paths,
417 $globs, $r)) {
418 if ($gs->rev_map_max >= $r) {
419 next;
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);
426 my $log_entry = $gs->do_fetch($paths, $r);
427 if ($log_entry) {
428 $gs->do_git_commit($log_entry);
430 $Git::SVN::INDEX_FILES{$gs->{index}} = 1;
432 foreach my $g (@$globs) {
433 my $k = "svn-remote.$g->{remote}." .
434 "$g->{t}-maxRev";
435 Git::SVN::tmp_config($k, $r);
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;
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);
452 foreach my $g (@$globs) {
453 my $k = "svn-remote.$g->{remote}.$g->{t}-maxRev";
454 Git::SVN::tmp_config($k, $max);
456 last if $max >= $head;
457 $min = $max + 1;
458 $max += $inc;
459 $max = $head if ($max > $head);
461 Git::SVN::gc();
464 sub get_dir_globbed {
465 my ($self, $left, $depth, $r) = @_;
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";
478 } else {
479 push @finalents, $de;
482 @finalents;
485 # return value: 0 -- don't ignore, 1 -- ignore
486 sub 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;
496 sub match_globs {
497 my ($self, $exists, $paths, $globs, $r) = @_;
499 sub get_dir_check {
500 my ($self, $exists, $g, $r) = @_;
502 my @dirs = $self->get_dir_globbed($g->{path}->{left},
503 $g->{path}->{depth},
504 $r);
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);
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);
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);
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);
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);
548 values %$exists;
551 sub 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 {});
564 } while ($@ && ($c = shift @components));
566 return canonicalize_url($url);
569 sub 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);
577 if ($@) {
578 $can_do_switch = 0;
579 } else {
580 $rep->abort_report($pool);
581 $can_do_switch = 1;
583 $pool->clear;
585 $can_do_switch;
588 sub 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
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;
615 return;
617 die "Error from SVN, ($errno): ", $err->expanded_message,"\n";
621 __END__
623 Git::SVN::Ra - Subversion remote access functions for git-svn
625 =head1 SYNOPSIS
627 use Git::SVN::Ra;
629 my $ra = Git::SVN::Ra->new($branchurl);
630 my ($dirents, $fetched_revnum, $props) =
631 $ra->get_dir('.', $SVN::Core::INVALID_REVNUM);
633 =head1 DESCRIPTION
635 This is a wrapper around the L<SVN::Ra> module for use by B<git-svn>.
636 It fills in some default parameters (such as the authentication
637 scheme), smooths over incompatibilities between libsvn versions, adds
638 caching, and implements some functions specific to B<git-svn>.
640 Do not use it unless you are developing git-svn. The interface will
641 change as git-svn evolves.
643 =head1 DEPENDENCIES
645 Subversion perl bindings,
646 L<Git::SVN>.
648 C<Git::SVN::Ra> has not been tested using callers other than
649 B<git-svn> itself.
651 =head1 SEE ALSO
653 L<SVN::Ra>.
655 =head1 INCOMPATIBILITIES
657 None reported.
659 =head1 BUGS
661 None.