i18n: reset: mark parseopt strings for translation
[git.git] / perl / Git / SVN / Ra.pm
blob23ff43e86b4f08fb38b8d9ca90a6c77319ae8f63
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 $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 $RA = bless $self, $class;
131 sub check_path {
132 my ($self, $path, $r) = @_;
133 my $cache = $self->{cache}->{check_path};
134 if ($r == $cache->{r} && exists $cache->{data}->{$path}) {
135 return $cache->{data}->{$path};
137 my $pool = SVN::Pool->new;
138 my $t = $self->SUPER::check_path($path, $r, $pool);
139 $pool->clear;
140 if ($r != $cache->{r}) {
141 %{$cache->{data}} = ();
142 $cache->{r} = $r;
144 $cache->{data}->{$path} = $t;
147 sub get_dir {
148 my ($self, $dir, $r) = @_;
149 my $cache = $self->{cache}->{get_dir};
150 if ($r == $cache->{r}) {
151 if (my $x = $cache->{data}->{$dir}) {
152 return wantarray ? @$x : $x->[0];
155 my $pool = SVN::Pool->new;
156 my ($d, undef, $props) = $self->SUPER::get_dir($dir, $r, $pool);
157 my %dirents = map { $_ => { kind => $d->{$_}->kind } } keys %$d;
158 $pool->clear;
159 if ($r != $cache->{r}) {
160 %{$cache->{data}} = ();
161 $cache->{r} = $r;
163 $cache->{data}->{$dir} = [ \%dirents, $r, $props ];
164 wantarray ? (\%dirents, $r, $props) : \%dirents;
167 sub DESTROY {
168 # do not call the real DESTROY since we store ourselves in $RA
171 # get_log(paths, start, end, limit,
172 # discover_changed_paths, strict_node_history, receiver)
173 sub get_log {
174 my ($self, @args) = @_;
175 my $pool = SVN::Pool->new;
177 # svn_log_changed_path_t objects passed to get_log are likely to be
178 # overwritten even if only the refs are copied to an external variable,
179 # so we should dup the structures in their entirety. Using an
180 # externally passed pool (instead of our temporary and quickly cleared
181 # pool in Git::SVN::Ra) does not help matters at all...
182 my $receiver = pop @args;
183 my $prefix = "/".$self->{svn_path};
184 $prefix =~ s#/+($)##;
185 my $prefix_regex = qr#^\Q$prefix\E#;
186 push(@args, sub {
187 my ($paths) = $_[0];
188 return &$receiver(@_) unless $paths;
189 $_[0] = ();
190 foreach my $p (keys %$paths) {
191 my $i = $paths->{$p};
192 # Make path relative to our url, not repos_root
193 $p =~ s/$prefix_regex//;
194 my %s = map { $_ => $i->$_; }
195 qw/copyfrom_path copyfrom_rev action/;
196 if ($s{'copyfrom_path'}) {
197 $s{'copyfrom_path'} =~ s/$prefix_regex//;
199 $_[0]{$p} = \%s;
201 &$receiver(@_);
205 # the limit parameter was not supported in SVN 1.1.x, so we
206 # drop it. Therefore, the receiver callback passed to it
207 # is made aware of this limitation by being wrapped if
208 # the limit passed to is being wrapped.
209 if (::compare_svn_version('1.2.0') <= 0) {
210 my $limit = splice(@args, 3, 1);
211 if ($limit > 0) {
212 my $receiver = pop @args;
213 push(@args, sub { &$receiver(@_) if (--$limit >= 0) });
216 my $ret = $self->SUPER::get_log(@args, $pool);
217 $pool->clear;
218 $ret;
221 sub trees_match {
222 my ($self, $url1, $rev1, $url2, $rev2) = @_;
223 my $ctx = SVN::Client->new(auth => _auth_providers);
224 my $out = IO::File->new_tmpfile;
226 # older SVN (1.1.x) doesn't take $pool as the last parameter for
227 # $ctx->diff(), so we'll create a default one
228 my $pool = SVN::Pool->new_default_sub;
230 $ra_invalid = 1; # this will open a new SVN::Ra connection to $url1
231 $ctx->diff([], $url1, $rev1, $url2, $rev2, 1, 1, 0, $out, $out);
232 $out->flush;
233 my $ret = (($out->stat)[7] == 0);
234 close $out or croak $!;
236 $ret;
239 sub get_commit_editor {
240 my ($self, $log, $cb, $pool) = @_;
242 my @lock = (::compare_svn_version('1.2.0') >= 0) ? (undef, 0) : ();
243 $self->SUPER::get_commit_editor($log, $cb, @lock, $pool);
246 sub gs_do_update {
247 my ($self, $rev_a, $rev_b, $gs, $editor) = @_;
248 my $new = ($rev_a == $rev_b);
249 my $path = $gs->{path};
251 if ($new && -e $gs->{index}) {
252 unlink $gs->{index} or die
253 "Couldn't unlink index: $gs->{index}: $!\n";
255 my $pool = SVN::Pool->new;
256 $editor->set_path_strip($path);
257 my (@pc) = split m#/#, $path;
258 my $reporter = $self->do_update($rev_b, (@pc ? shift @pc : ''),
259 1, $editor, $pool);
260 my @lock = (::compare_svn_version('1.2.0') >= 0) ? (undef) : ();
262 # Since we can't rely on svn_ra_reparent being available, we'll
263 # just have to do some magic with set_path to make it so
264 # we only want a partial path.
265 my $sp = '';
266 my $final = join('/', @pc);
267 while (@pc) {
268 $reporter->set_path($sp, $rev_b, 0, @lock, $pool);
269 $sp .= '/' if length $sp;
270 $sp .= shift @pc;
272 die "BUG: '$sp' != '$final'\n" if ($sp ne $final);
274 $reporter->set_path($sp, $rev_a, $new, @lock, $pool);
276 $reporter->finish_report($pool);
277 $pool->clear;
278 $editor->{git_commit_ok};
281 # this requires SVN 1.4.3 or later (do_switch didn't work before 1.4.3, and
282 # svn_ra_reparent didn't work before 1.4)
283 sub gs_do_switch {
284 my ($self, $rev_a, $rev_b, $gs, $url_b, $editor) = @_;
285 my $path = $gs->{path};
286 my $pool = SVN::Pool->new;
288 my $full_url = $self->{url};
289 my $old_url = $full_url;
290 $full_url .= '/' . $path if length $path;
291 my ($ra, $reparented);
293 if ($old_url =~ m#^svn(\+ssh)?://# ||
294 ($full_url =~ m#^https?://# &&
295 escape_url($full_url) ne $full_url)) {
296 $_[0] = undef;
297 $self = undef;
298 $RA = undef;
299 $ra = Git::SVN::Ra->new($full_url);
300 $ra_invalid = 1;
301 } elsif ($old_url ne $full_url) {
302 SVN::_Ra::svn_ra_reparent($self->{session}, $full_url, $pool);
303 $self->{url} = $full_url;
304 $reparented = 1;
307 $ra ||= $self;
308 $url_b = escape_url($url_b);
309 my $reporter = $ra->do_switch($rev_b, '', 1, $url_b, $editor, $pool);
310 my @lock = (::compare_svn_version('1.2.0') >= 0) ? (undef) : ();
311 $reporter->set_path('', $rev_a, 0, @lock, $pool);
312 $reporter->finish_report($pool);
314 if ($reparented) {
315 SVN::_Ra::svn_ra_reparent($self->{session}, $old_url, $pool);
316 $self->{url} = $old_url;
319 $pool->clear;
320 $editor->{git_commit_ok};
323 sub longest_common_path {
324 my ($gsv, $globs) = @_;
325 my %common;
326 my $common_max = scalar @$gsv;
328 foreach my $gs (@$gsv) {
329 my @tmp = split m#/#, $gs->{path};
330 my $p = '';
331 foreach (@tmp) {
332 $p .= length($p) ? "/$_" : $_;
333 $common{$p} ||= 0;
334 $common{$p}++;
337 $globs ||= [];
338 $common_max += scalar @$globs;
339 foreach my $glob (@$globs) {
340 my @tmp = split m#/#, $glob->{path}->{left};
341 my $p = '';
342 foreach (@tmp) {
343 $p .= length($p) ? "/$_" : $_;
344 $common{$p} ||= 0;
345 $common{$p}++;
349 my $longest_path = '';
350 foreach (sort {length $b <=> length $a} keys %common) {
351 if ($common{$_} == $common_max) {
352 $longest_path = $_;
353 last;
356 $longest_path;
359 sub gs_fetch_loop_common {
360 my ($self, $base, $head, $gsv, $globs) = @_;
361 return if ($base > $head);
362 my $inc = $_log_window_size;
363 my ($min, $max) = ($base, $head < $base + $inc ? $head : $base + $inc);
364 my $longest_path = longest_common_path($gsv, $globs);
365 my $ra_url = $self->{url};
366 my $find_trailing_edge;
367 while (1) {
368 my %revs;
369 my $err;
370 my $err_handler = $SVN::Error::handler;
371 $SVN::Error::handler = sub {
372 ($err) = @_;
373 skip_unknown_revs($err);
375 sub _cb {
376 my ($paths, $r, $author, $date, $log) = @_;
377 [ $paths,
378 { author => $author, date => $date, log => $log } ];
380 $self->get_log([$longest_path], $min, $max, 0, 1, 1,
381 sub { $revs{$_[1]} = _cb(@_) });
382 if ($err) {
383 print "Checked through r$max\r";
384 } else {
385 $find_trailing_edge = 1;
387 if ($err and $find_trailing_edge) {
388 print STDERR "Path '$longest_path' ",
389 "was probably deleted:\n",
390 $err->expanded_message,
391 "\nWill attempt to follow ",
392 "revisions r$min .. r$max ",
393 "committed before the deletion\n";
394 my $hi = $max;
395 while (--$hi >= $min) {
396 my $ok;
397 $self->get_log([$longest_path], $min, $hi,
398 0, 1, 1, sub {
399 $ok = $_[1];
400 $revs{$_[1]} = _cb(@_) });
401 if ($ok) {
402 print STDERR "r$min .. r$ok OK\n";
403 last;
406 $find_trailing_edge = 0;
408 $SVN::Error::handler = $err_handler;
410 my %exists = map { $_->{path} => $_ } @$gsv;
411 foreach my $r (sort {$a <=> $b} keys %revs) {
412 my ($paths, $logged) = @{$revs{$r}};
414 foreach my $gs ($self->match_globs(\%exists, $paths,
415 $globs, $r)) {
416 if ($gs->rev_map_max >= $r) {
417 next;
419 next unless $gs->match_paths($paths, $r);
420 $gs->{logged_rev_props} = $logged;
421 if (my $last_commit = $gs->last_commit) {
422 $gs->assert_index_clean($last_commit);
424 my $log_entry = $gs->do_fetch($paths, $r);
425 if ($log_entry) {
426 $gs->do_git_commit($log_entry);
428 $Git::SVN::INDEX_FILES{$gs->{index}} = 1;
430 foreach my $g (@$globs) {
431 my $k = "svn-remote.$g->{remote}." .
432 "$g->{t}-maxRev";
433 Git::SVN::tmp_config($k, $r);
435 if ($ra_invalid) {
436 $_[0] = undef;
437 $self = undef;
438 $RA = undef;
439 $self = Git::SVN::Ra->new($ra_url);
440 $ra_invalid = undef;
443 # pre-fill the .rev_db since it'll eventually get filled in
444 # with '0' x40 if something new gets committed
445 foreach my $gs (@$gsv) {
446 next if $gs->rev_map_max >= $max;
447 next if defined $gs->rev_map_get($max);
448 $gs->rev_map_set($max, 0 x40);
450 foreach my $g (@$globs) {
451 my $k = "svn-remote.$g->{remote}.$g->{t}-maxRev";
452 Git::SVN::tmp_config($k, $max);
454 last if $max >= $head;
455 $min = $max + 1;
456 $max += $inc;
457 $max = $head if ($max > $head);
459 Git::SVN::gc();
462 sub get_dir_globbed {
463 my ($self, $left, $depth, $r) = @_;
465 my @x = eval { $self->get_dir($left, $r) };
466 return unless scalar @x == 3;
467 my $dirents = $x[0];
468 my @finalents;
469 foreach my $de (keys %$dirents) {
470 next if $dirents->{$de}->{kind} != $SVN::Node::dir;
471 if ($depth > 1) {
472 my @args = ("$left/$de", $depth - 1, $r);
473 foreach my $dir ($self->get_dir_globbed(@args)) {
474 push @finalents, "$de/$dir";
476 } else {
477 push @finalents, $de;
480 @finalents;
483 # return value: 0 -- don't ignore, 1 -- ignore
484 sub is_ref_ignored {
485 my ($g, $p) = @_;
486 my $refname = $g->{ref}->full_path($p);
487 return 1 if defined($g->{ignore_refs_regex}) &&
488 $refname =~ m!$g->{ignore_refs_regex}!;
489 return 0 unless defined($_ignore_refs_regex);
490 return 1 if $refname =~ m!$_ignore_refs_regex!o;
491 return 0;
494 sub match_globs {
495 my ($self, $exists, $paths, $globs, $r) = @_;
497 sub get_dir_check {
498 my ($self, $exists, $g, $r) = @_;
500 my @dirs = $self->get_dir_globbed($g->{path}->{left},
501 $g->{path}->{depth},
502 $r);
504 foreach my $de (@dirs) {
505 my $p = $g->{path}->full_path($de);
506 next if $exists->{$p};
507 next if (length $g->{path}->{right} &&
508 ($self->check_path($p, $r) !=
509 $SVN::Node::dir));
510 next unless $p =~ /$g->{path}->{regex}/;
511 $exists->{$p} = Git::SVN->init($self->{url}, $p, undef,
512 $g->{ref}->full_path($de), 1);
515 foreach my $g (@$globs) {
516 if (my $path = $paths->{"/$g->{path}->{left}"}) {
517 if ($path->{action} =~ /^[AR]$/) {
518 get_dir_check($self, $exists, $g, $r);
521 foreach (keys %$paths) {
522 if (/$g->{path}->{left_regex}/ &&
523 !/$g->{path}->{regex}/) {
524 next if $paths->{$_}->{action} !~ /^[AR]$/;
525 get_dir_check($self, $exists, $g, $r);
527 next unless /$g->{path}->{regex}/;
528 my $p = $1;
529 my $pathname = $g->{path}->full_path($p);
530 next if is_ref_ignored($g, $p);
531 next if $exists->{$pathname};
532 next if ($self->check_path($pathname, $r) !=
533 $SVN::Node::dir);
534 $exists->{$pathname} = Git::SVN->init(
535 $self->{url}, $pathname, undef,
536 $g->{ref}->full_path($p), 1);
538 my $c = '';
539 foreach (split m#/#, $g->{path}->{left}) {
540 $c .= "/$_";
541 next unless ($paths->{$c} &&
542 ($paths->{$c}->{action} =~ /^[AR]$/));
543 get_dir_check($self, $exists, $g, $r);
546 values %$exists;
549 sub minimize_url {
550 my ($self) = @_;
551 return $self->{url} if ($self->{url} eq $self->{repos_root});
552 my $url = $self->{repos_root};
553 my @components = split(m!/!, $self->{svn_path});
554 my $c = '';
555 do {
556 $url .= "/$c" if length $c;
557 eval {
558 my $ra = (ref $self)->new($url);
559 my $latest = $ra->get_latest_revnum;
560 $ra->get_log("", $latest, 0, 1, 0, 1, sub {});
562 } while ($@ && ($c = shift @components));
563 $url;
566 sub can_do_switch {
567 my $self = shift;
568 unless (defined $can_do_switch) {
569 my $pool = SVN::Pool->new;
570 my $rep = eval {
571 $self->do_switch(1, '', 0, $self->{url},
572 SVN::Delta::Editor->new, $pool);
574 if ($@) {
575 $can_do_switch = 0;
576 } else {
577 $rep->abort_report($pool);
578 $can_do_switch = 1;
580 $pool->clear;
582 $can_do_switch;
585 sub skip_unknown_revs {
586 my ($err) = @_;
587 my $errno = $err->apr_err();
588 # Maybe the branch we're tracking didn't
589 # exist when the repo started, so it's
590 # not an error if it doesn't, just continue
592 # Wonderfully consistent library, eh?
593 # 160013 - svn:// and file://
594 # 175002 - http(s)://
595 # 175007 - http(s):// (this repo required authorization, too...)
596 # More codes may be discovered later...
597 if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
598 my $err_key = $err->expanded_message;
599 # revision numbers change every time, filter them out
600 $err_key =~ s/\d+/\0/g;
601 $err_key = "$errno\0$err_key";
602 unless ($ignored_err{$err_key}) {
603 warn "W: Ignoring error from SVN, path probably ",
604 "does not exist: ($errno): ",
605 $err->expanded_message,"\n";
606 warn "W: Do not be alarmed at the above message ",
607 "git-svn is just searching aggressively for ",
608 "old history.\n",
609 "This may take a while on large repositories\n";
610 $ignored_err{$err_key} = 1;
612 return;
614 die "Error from SVN, ($errno): ", $err->expanded_message,"\n";
618 __END__
620 Git::SVN::Ra - Subversion remote access functions for git-svn
622 =head1 SYNOPSIS
624 use Git::SVN::Ra;
626 my $ra = Git::SVN::Ra->new($branchurl);
627 my ($dirents, $fetched_revnum, $props) =
628 $ra->get_dir('.', $SVN::Core::INVALID_REVNUM);
630 =head1 DESCRIPTION
632 This is a wrapper around the L<SVN::Ra> module for use by B<git-svn>.
633 It fills in some default parameters (such as the authentication
634 scheme), smooths over incompatibilities between libsvn versions, adds
635 caching, and implements some functions specific to B<git-svn>.
637 Do not use it unless you are developing git-svn. The interface will
638 change as git-svn evolves.
640 =head1 DEPENDENCIES
642 Subversion perl bindings,
643 L<Git::SVN>.
645 C<Git::SVN::Ra> has not been tested using callers other than
646 B<git-svn> itself.
648 =head1 SEE ALSO
650 L<SVN::Ra>.
652 =head1 INCOMPATIBILITIES
654 None reported.
656 =head1 BUGS
658 None.