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