2 use vars qw
/@ISA $config_dir $_ignore_refs_regex $_log_window_size/;
6 use Git
::SVN
::Utils
qw(
15 my ($ra_invalid, $can_do_switch, %ignored_err, $RA);
18 # enforce temporary pool usage for some simple functions
20 for my $f (qw
/rev_proplist get_latest_revnum get_uuid get_repos_root
22 my $SUPER = "SUPER::$f";
25 my $pool = SVN
::Pool
->new;
26 my @ret = $self->$SUPER(@_,$pool);
28 wantarray ?
@ret : $ret[0];
33 sub _auth_providers
() {
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
54 if (::compare_svn_version
('1.6.15') >= 0) {
55 my $config = SVN
::Core
::config_get_config
($config_dir);
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
71 my ($class, $url) = @_;
72 $url = canonicalize_url
($url);
73 return $RA if ($RA && $RA->url eq $url);
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);
81 my $dont_store_passwords = 1;
82 my $conf_t = ${$config}{'config'};
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
,
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
,
101 $Git::SVN
::Prompt
::_no_auth_cache
= 1;
103 } # no warnings 'once'
104 my $self = SVN
::Ra
->new(url
=> $url, auth
=> $baton,
106 pool
=> SVN
::Pool
->new,
107 auth_provider_callbacks
=> $callbacks);
108 $RA = bless $self, $class;
110 # Make sure its canonicalized
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
=> {} } };
126 $self->{url
} = canonicalize_url
($url);
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);
142 if ($r != $cache->{r
}) {
143 %{$cache->{data
}} = ();
146 $cache->{data
}->{$path} = $t;
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;
161 if ($r != $cache->{r
}) {
162 %{$cache->{data
}} = ();
165 $cache->{data
}->{$dir} = [ \
%dirents, $r, $props ];
166 wantarray ?
(\
%dirents, $r, $props) : \
%dirents;
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)
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#;
190 return &$receiver(@_) unless $paths;
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//;
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);
214 my $receiver = pop @args;
215 push(@args, sub { &$receiver(@_) if (--$limit >= 0) });
218 my $ret = $self->SUPER::get_log
(@args, $pool);
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);
235 my $ret = (($out->stat)[7] == 0);
236 close $out or croak
$!;
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);
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 : ''),
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.
268 my $final = join('/', @pc);
270 $reporter->set_path($sp, $rev_b, 0, @lock, $pool);
271 $sp .= '/' if length $sp;
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);
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)
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)) {
301 $ra = Git
::SVN
::Ra
->new($full_url);
303 } elsif ($old_url ne $full_url) {
304 SVN
::_Ra
::svn_ra_reparent
($self->{session
}, $full_url, $pool);
305 $self->url($full_url);
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);
317 SVN
::_Ra
::svn_ra_reparent
($self->{session
}, $old_url, $pool);
318 $self->url($old_url);
322 $editor->{git_commit_ok
};
325 sub longest_common_path
{
326 my ($gsv, $globs) = @_;
328 my $common_max = scalar @
$gsv;
330 foreach my $gs (@
$gsv) {
331 my @tmp = split m
#/#, $gs->path;
334 $p .= length($p) ?
"/$_" : $_;
340 $common_max += scalar @
$globs;
341 foreach my $glob (@
$globs) {
342 my @tmp = split m
#/#, $glob->{path}->{left};
345 $p .= length($p) ?
"/$_" : $_;
351 my $longest_path = '';
352 foreach (sort {length $b <=> length $a} keys %common) {
353 if ($common{$_} == $common_max) {
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;
372 my $err_handler = $SVN::Error
::handler
;
373 $SVN::Error
::handler
= sub {
375 skip_unknown_revs
($err);
378 my ($paths, $r, $author, $date, $log) = @_;
380 { author
=> $author, date
=> $date, log => $log } ];
382 $self->get_log([$longest_path], $min, $max, 0, 1, 1,
383 sub { $revs{$_[1]} = _cb
(@_) });
385 print "Checked through r$max\r";
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";
397 while (--$hi >= $min) {
399 $self->get_log([$longest_path], $min, $hi,
402 $revs{$_[1]} = _cb
(@_) });
404 print STDERR
"r$min .. r$ok OK\n";
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,
418 if ($gs->rev_map_max >= $r) {
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);
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}." .
435 Git
::SVN
::tmp_config
($k, $r);
441 $self = Git
::SVN
::Ra
->new($ra_url);
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;
459 $max = $head if ($max > $head);
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;
471 foreach my $de (keys %$dirents) {
472 next if $dirents->{$de}->{kind
} != $SVN::Node
::dir
;
474 my @args = ("$left/$de", $depth - 1, $r);
475 foreach my $dir ($self->get_dir_globbed(@args)) {
476 push @finalents, "$de/$dir";
479 push @finalents, $de;
485 # return value: 0 -- don't ignore, 1 -- ignore
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;
497 my ($self, $exists, $paths, $globs, $r) = @_;
500 my ($self, $exists, $g, $r) = @_;
502 my @dirs = $self->get_dir_globbed($g->{path
}->{left
},
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) !=
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}/;
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) !=
536 $exists->{$pathname} = Git
::SVN
->init(
537 $self->url, $pathname, undef,
538 $g->{ref}->full_path($p), 1);
541 foreach (split m
#/#, $g->{path}->{left}) {
543 next unless ($paths->{$c} &&
544 ($paths->{$c}->{action
} =~ /^[AR]$/));
545 get_dir_check
($self, $exists, $g, $r);
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
});
558 $url .= "/$c" if length $c;
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);
571 unless (defined $can_do_switch) {
572 my $pool = SVN
::Pool
->new;
574 $self->do_switch(1, '', 0, $self->url,
575 SVN
::Delta
::Editor
->new, $pool);
580 $rep->abort_report($pool);
588 sub skip_unknown_revs
{
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 ",
612 "This may take a while on large repositories\n";
613 $ignored_err{$err_key} = 1;
617 die "Error from SVN, ($errno): ", $err->expanded_message,"\n";
623 Git
::SVN
::Ra
- Subversion remote access functions
for git
-svn
629 my $ra = Git::SVN::Ra->new($branchurl);
630 my ($dirents, $fetched_revnum, $props) =
631 $ra->get_dir('.', $SVN::Core::INVALID_REVNUM);
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.
645 Subversion perl bindings,
648 C<Git::SVN::Ra> has not been tested using callers other than
655 =head1 INCOMPATIBILITIES