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