| 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 Memoize; | ||
| 6 | use Git::SVN::Utils qw( | ||
| 7 | canonicalize_url | ||
| 8 | canonicalize_path | ||
| 9 | add_path_to_url | ||
| 10 | ); | ||
| 11 | |||
| 12 | use SVN::Ra; | ||
| 13 | BEGIN { | ||
| 14 | @ISA = qw(SVN::Ra); | ||
| 15 | } | ||
| 16 | |||
| 17 | my ($ra_invalid, $can_do_switch, %ignored_err, $RA); | ||
| 18 | |||
| 19 | BEGIN { | ||
| 20 | # enforce temporary pool usage for some simple functions | ||
| 21 | no strict 'refs'; | ||
| 22 | for my $f (qw/rev_proplist get_latest_revnum get_uuid get_repos_root | ||
| 23 | get_file/) { | ||
| 24 | my $SUPER = "SUPER::$f"; | ||
| 25 | *$f = sub { | ||
| 26 | my $self = shift; | ||
| 27 | my $pool = SVN::Pool->new; | ||
| 28 | my @ret = $self->$SUPER(@_,$pool); | ||
| 29 | $pool->clear; | ||
| 30 | wantarray ? @ret : $ret[0]; | ||
| 31 | }; | ||
| 32 | } | ||
| 33 | } | ||
| 34 | |||
| 35 | # serf has a bug that leads to a coredump upon termination if the | ||
| 36 | # remote access object is left around (not fixed yet in serf 1.3.1). | ||
| 37 | # Explicitly free it to work around the issue. | ||
| 38 | END { | ||
| 39 | $RA = undef; | ||
| 40 | $ra_invalid = 1; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub _auth_providers () { | ||
| 44 | require SVN::Client; | ||
| 45 | my @rv = ( | ||
| 46 | SVN::Client::get_simple_provider(), | ||
| 47 | SVN::Client::get_ssl_server_trust_file_provider(), | ||
| 48 | SVN::Client::get_simple_prompt_provider( | ||
| 49 | \&Git::SVN::Prompt::simple, 2), | ||
| 50 | SVN::Client::get_ssl_client_cert_file_provider(), | ||
| 51 | SVN::Client::get_ssl_client_cert_prompt_provider( | ||
| 52 | \&Git::SVN::Prompt::ssl_client_cert, 2), | ||
| 53 | SVN::Client::get_ssl_client_cert_pw_file_provider(), | ||
| 54 | SVN::Client::get_ssl_client_cert_pw_prompt_provider( | ||
| 55 | \&Git::SVN::Prompt::ssl_client_cert_pw, 2), | ||
| 56 | SVN::Client::get_username_provider(), | ||
| 57 | SVN::Client::get_ssl_server_trust_prompt_provider( | ||
| 58 | \&Git::SVN::Prompt::ssl_server_trust), | ||
| 59 | SVN::Client::get_username_prompt_provider( | ||
| 60 | \&Git::SVN::Prompt::username, 2) | ||
| 61 | ); | ||
| 62 | |||
| 63 | # earlier 1.6.x versions would segfault, and <= 1.5.x didn't have | ||
| 64 | # this function | ||
| 65 | if (::compare_svn_version('1.6.15') >= 0) { | ||
| 66 | my $config = SVN::Core::config_get_config($config_dir); | ||
| 67 | my ($p, @a); | ||
| 68 | # config_get_config returns all config files from | ||
| 69 | # ~/.subversion, auth_get_platform_specific_client_providers | ||
| 70 | # just wants the config "file". | ||
| 71 | @a = ($config->{'config'}, undef); | ||
| 72 | $p = SVN::Core::auth_get_platform_specific_client_providers(@a); | ||
| 73 | # Insert the return value from | ||
| 74 | # auth_get_platform_specific_providers | ||
| 75 | unshift @rv, @$p; | ||
| 76 | } | ||
| 77 | \@rv; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub prepare_config_once { | ||
| 81 | SVN::_Core::svn_config_ensure($config_dir, undef); | ||
| 82 | my ($baton, $callbacks) = SVN::Core::auth_open_helper(_auth_providers); | ||
| 83 | my $config = SVN::Core::config_get_config($config_dir); | ||
| 84 | my $dont_store_passwords = 1; | ||
| 85 | my $conf_t = $config->{'config'}; | ||
| 86 | |||
| 87 | no warnings 'once'; | ||
| 88 | # The usage of $SVN::_Core::SVN_CONFIG_* variables | ||
| 89 | # produces warnings that variables are used only once. | ||
| 90 | # I had not found the better way to shut them up, so | ||
| 91 | # the warnings of type 'once' are disabled in this block. | ||
| 92 | if (SVN::_Core::svn_config_get_bool($conf_t, | ||
| 93 | $SVN::_Core::SVN_CONFIG_SECTION_AUTH, | ||
| 94 | $SVN::_Core::SVN_CONFIG_OPTION_STORE_PASSWORDS, | ||
| 95 | 1) == 0) { | ||
| 96 | SVN::_Core::svn_auth_set_parameter($baton, | ||
| 97 | $SVN::_Core::SVN_AUTH_PARAM_DONT_STORE_PASSWORDS, | ||
| 98 | bless (\$dont_store_passwords, "_p_void")); | ||
| 99 | } | ||
| 100 | if (SVN::_Core::svn_config_get_bool($conf_t, | ||
| 101 | $SVN::_Core::SVN_CONFIG_SECTION_AUTH, | ||
| 102 | $SVN::_Core::SVN_CONFIG_OPTION_STORE_AUTH_CREDS, | ||
| 103 | 1) == 0) { | ||
| 104 | $Git::SVN::Prompt::_no_auth_cache = 1; | ||
| 105 | } | ||
| 106 | |||
| 107 | return ($config, $baton, $callbacks); | ||
| 108 | } # no warnings 'once' | ||
| 109 | |||
| 110 | INIT { | ||
| 111 | Memoize::memoize '_auth_providers'; | ||
| 112 | Memoize::memoize 'prepare_config_once'; | ||
| 113 | } | ||
| 114 | |||
| 115 | sub new { | ||
| 116 | my ($class, $url) = @_; | ||
| 117 | $url = canonicalize_url($url); | ||
| 118 | return $RA if ($RA && $RA->url eq $url); | ||
| 119 | |||
| 120 | ::_req_svn(); | ||
| 121 | |||
| 122 | $RA = undef; | ||
| 123 | my ($config, $baton, $callbacks) = prepare_config_once(); | ||
| 124 | my $self = SVN::Ra->new(url => $url, auth => $baton, | ||
| 125 | config => $config, | ||
| 126 | pool => SVN::Pool->new, | ||
| 127 | auth_provider_callbacks => $callbacks); | ||
| 128 | $RA = bless $self, $class; | ||
| 129 | |||
| 130 | # Make sure its canonicalized | ||
| 131 | $self->url($url); | ||
| 132 | $self->{svn_path} = $url; | ||
| 133 | $self->{repos_root} = $self->get_repos_root; | ||
| 134 | $self->{svn_path} =~ s#^\Q$self->{repos_root}\E(/|$)##; | ||
| 135 | $self->{cache} = { check_path => { r => 0, data => {} }, | ||
| 136 | get_dir => { r => 0, data => {} } }; | ||
| 137 | |||
| 138 | return $RA; | ||
| 139 | } | ||
| 140 | |||
| 141 | sub url { | ||
| 142 | my $self = shift; | ||
| 143 | |||
| 144 | if (@_) { | ||
| 145 | my $url = shift; | ||
| 146 | $self->{url} = canonicalize_url($url); | ||
| 147 | return; | ||
| 148 | } | ||
| 149 | |||
| 150 | return $self->{url}; | ||
| 151 | } | ||
| 152 | |||
| 153 | sub check_path { | ||
| 154 | my ($self, $path, $r) = @_; | ||
| 155 | my $cache = $self->{cache}->{check_path}; | ||
| 156 | if ($r == $cache->{r} && exists $cache->{data}->{$path}) { | ||
| 157 | return $cache->{data}->{$path}; | ||
| 158 | } | ||
| 159 | my $pool = SVN::Pool->new; | ||
| 160 | my $t = $self->SUPER::check_path($path, $r, $pool); | ||
| 161 | $pool->clear; | ||
| 162 | if ($r != $cache->{r}) { | ||
| 163 | %{$cache->{data}} = (); | ||
| 164 | $cache->{r} = $r; | ||
| 165 | } | ||
| 166 | $cache->{data}->{$path} = $t; | ||
| 167 | } | ||
| 168 | |||
| 169 | sub get_dir { | ||
| 170 | my ($self, $dir, $r) = @_; | ||
| 171 | my $cache = $self->{cache}->{get_dir}; | ||
| 172 | if ($r == $cache->{r}) { | ||
| 173 | if (my $x = $cache->{data}->{$dir}) { | ||
| 174 | return wantarray ? @$x : $x->[0]; | ||
| 175 | } | ||
| 176 | } | ||
| 177 | my $pool = SVN::Pool->new; | ||
| 178 | my ($d, undef, $props); | ||
| 179 | |||
| 180 | if (::compare_svn_version('1.4.0') >= 0) { | ||
| 181 | # n.b. in addition to being potentially more efficient, | ||
| 182 | # this works around what appears to be a bug in some | ||
| 183 | # SVN 1.8 versions | ||
| 184 | my $kind = 1; # SVN_DIRENT_KIND | ||
| 185 | ($d, undef, $props) = $self->get_dir2($dir, $r, $kind, $pool); | ||
| 186 | } else { | ||
| 187 | ($d, undef, $props) = $self->SUPER::get_dir($dir, $r, $pool); | ||
| 188 | } | ||
| 189 | my %dirents = map { $_ => { kind => $d->{$_}->kind } } keys %$d; | ||
| 190 | $pool->clear; | ||
| 191 | if ($r != $cache->{r}) { | ||
| 192 | %{$cache->{data}} = (); | ||
| 193 | $cache->{r} = $r; | ||
| 194 | } | ||
| 195 | $cache->{data}->{$dir} = [ \%dirents, $r, $props ]; | ||
| 196 | wantarray ? (\%dirents, $r, $props) : \%dirents; | ||
| 197 | } | ||
| 198 | |||
| 199 | # get_log(paths, start, end, limit, | ||
| 200 | # discover_changed_paths, strict_node_history, receiver) | ||
| 201 | sub get_log { | ||
| 202 | my ($self, @args) = @_; | ||
| 203 | my $pool = SVN::Pool->new; | ||
| 204 | |||
| 205 | # svn_log_changed_path_t objects passed to get_log are likely to be | ||
| 206 | # overwritten even if only the refs are copied to an external variable, | ||
| 207 | # so we should dup the structures in their entirety. Using an | ||
| 208 | # externally passed pool (instead of our temporary and quickly cleared | ||
| 209 | # pool in Git::SVN::Ra) does not help matters at all... | ||
| 210 | my $receiver = pop @args; | ||
| 211 | my $prefix = "/".$self->{svn_path}; | ||
| 212 | $prefix =~ s#/+($)##; | ||
| 213 | my $prefix_regex = qr#^\Q$prefix\E#; | ||
| 214 | push(@args, sub { | ||
| 215 | my ($paths) = $_[0]; | ||
| 216 | return &$receiver(@_) unless $paths; | ||
| 217 | $_[0] = (); | ||
| 218 | foreach my $p (keys %$paths) { | ||
| 219 | my $i = $paths->{$p}; | ||
| 220 | # Make path relative to our url, not repos_root | ||
| 221 | $p =~ s/$prefix_regex//; | ||
| 222 | my %s = map { $_ => $i->$_; } | ||
| 223 | qw/copyfrom_path copyfrom_rev action/; | ||
| 224 | if ($s{'copyfrom_path'}) { | ||
| 225 | $s{'copyfrom_path'} =~ s/$prefix_regex//; | ||
| 226 | $s{'copyfrom_path'} = canonicalize_path($s{'copyfrom_path'}); | ||
| 227 | } | ||
| 228 | $_[0]{$p} = \%s; | ||
| 229 | } | ||
| 230 | &$receiver(@_); | ||
| 231 | }); | ||
| 232 | |||
| 233 | |||
| 234 | # the limit parameter was not supported in SVN 1.1.x, so we | ||
| 235 | # drop it. Therefore, the receiver callback passed to it | ||
| 236 | # is made aware of this limitation by being wrapped if | ||
| 237 | # the limit passed to is being wrapped. | ||
| 238 | if (::compare_svn_version('1.2.0') <= 0) { | ||
| 239 | my $limit = splice(@args, 3, 1); | ||
| 240 | if ($limit > 0) { | ||
| 241 | my $receiver = pop @args; | ||
| 242 | push(@args, sub { &$receiver(@_) if (--$limit >= 0) }); | ||
| 243 | } | ||
| 244 | } | ||
| 245 | my $ret = $self->SUPER::get_log(@args, $pool); | ||
| 246 | $pool->clear; | ||
| 247 | $ret; | ||
| 248 | } | ||
| 249 | |||
| 250 | # uncommon, only for ancient SVN (<= 1.4.2) | ||
| 251 | sub trees_match { | ||
| 252 | require IO::File; | ||
| 253 | require SVN::Client; | ||
| 254 | my ($self, $url1, $rev1, $url2, $rev2) = @_; | ||
| 255 | my $ctx = SVN::Client->new(auth => _auth_providers); | ||
| 256 | my $out = IO::File->new_tmpfile; | ||
| 257 | |||
| 258 | # older SVN (1.1.x) doesn't take $pool as the last parameter for | ||
| 259 | # $ctx->diff(), so we'll create a default one | ||
| 260 | my $pool = SVN::Pool->new_default_sub; | ||
| 261 | |||
| 262 | $ra_invalid = 1; # this will open a new SVN::Ra connection to $url1 | ||
| 263 | $ctx->diff([], $url1, $rev1, $url2, $rev2, 1, 1, 0, $out, $out); | ||
| 264 | $out->flush; | ||
| 265 | my $ret = (($out->stat)[7] == 0); | ||
| 266 | close $out or croak $!; | ||
| 267 | |||
| 268 | $ret; | ||
| 269 | } | ||
| 270 | |||
| 271 | sub get_commit_editor { | ||
| 272 | my ($self, $log, $cb, $pool) = @_; | ||
| 273 | |||
| 274 | my @lock = (::compare_svn_version('1.2.0') >= 0) ? (undef, 0) : (); | ||
| 275 | $self->SUPER::get_commit_editor($log, $cb, @lock, $pool); | ||
| 276 | } | ||
| 277 | |||
| 278 | sub gs_do_update { | ||
| 279 | my ($self, $rev_a, $rev_b, $gs, $editor) = @_; | ||
| 280 | my $new = ($rev_a == $rev_b); | ||
| 281 | my $path = $gs->path; | ||
| 282 | |||
| 283 | if ($new && -e $gs->{index}) { | ||
| 284 | unlink $gs->{index} or die | ||
| 285 | "Couldn't unlink index: $gs->{index}: $!\n"; | ||
| 286 | } | ||
| 287 | my $pool = SVN::Pool->new; | ||
| 288 | $editor->set_path_strip($path); | ||
| 289 | my (@pc) = split m#/#, $path; | ||
| 290 | my $reporter = $self->do_update($rev_b, (@pc ? shift @pc : ''), | ||
| 291 | 1, $editor, $pool); | ||
| 292 | my @lock = (::compare_svn_version('1.2.0') >= 0) ? (undef) : (); | ||
| 293 | |||
| 294 | # Since we can't rely on svn_ra_reparent being available, we'll | ||
| 295 | # just have to do some magic with set_path to make it so | ||
| 296 | # we only want a partial path. | ||
| 297 | my $sp = ''; | ||
| 298 | my $final = join('/', @pc); | ||
| 299 | while (@pc) { | ||
| 300 | $reporter->set_path($sp, $rev_b, 0, @lock, $pool); | ||
| 301 | $sp .= '/' if length $sp; | ||
| 302 | $sp .= shift @pc; | ||
| 303 | } | ||
| 304 | die "BUG: '$sp' != '$final'\n" if ($sp ne $final); | ||
| 305 | |||
| 306 | $reporter->set_path($sp, $rev_a, $new, @lock, $pool); | ||
| 307 | |||
| 308 | $reporter->finish_report($pool); | ||
| 309 | $pool->clear; | ||
| 310 | $editor->{git_commit_ok}; | ||
| 311 | } | ||
| 312 | |||
| 313 | # this requires SVN 1.4.3 or later (do_switch didn't work before 1.4.3, and | ||
| 314 | # svn_ra_reparent didn't work before 1.4) | ||
| 315 | sub gs_do_switch { | ||
| 316 | my ($self, $rev_a, $rev_b, $gs, $url_b, $editor) = @_; | ||
| 317 | my $path = $gs->path; | ||
| 318 | my $pool = SVN::Pool->new; | ||
| 319 | |||
| 320 | my $old_url = $self->url; | ||
| 321 | my $full_url = add_path_to_url( $self->url, $path ); | ||
| 322 | my ($ra, $reparented); | ||
| 323 | |||
| 324 | if ($old_url =~ m#^svn(\+\w+)?://# || | ||
| 325 | ($full_url =~ m#^https?://# && | ||
| 326 | canonicalize_url($full_url) ne $full_url)) { | ||
| 327 | $_[0] = undef; | ||
| 328 | $self = undef; | ||
| 329 | $RA = undef; | ||
| 330 | $ra = Git::SVN::Ra->new($full_url); | ||
| 331 | $ra_invalid = 1; | ||
| 332 | } elsif ($old_url ne $full_url) { | ||
| 333 | SVN::_Ra::svn_ra_reparent( | ||
| 334 | $self->{session}, | ||
| 335 | canonicalize_url($full_url), | ||
| 336 | $pool | ||
| 337 | ); | ||
| 338 | $self->url($full_url); | ||
| 339 | $reparented = 1; | ||
| 340 | } | ||
| 341 | |||
| 342 | $ra ||= $self; | ||
| 343 | $url_b = canonicalize_url($url_b); | ||
| 344 | my $reporter = $ra->do_switch($rev_b, '', 1, $url_b, $editor, $pool); | ||
| 345 | my @lock = (::compare_svn_version('1.2.0') >= 0) ? (undef) : (); | ||
| 346 | $reporter->set_path('', $rev_a, 0, @lock, $pool); | ||
| 347 | $reporter->finish_report($pool); | ||
| 348 | |||
| 349 | if ($reparented) { | ||
| 350 | SVN::_Ra::svn_ra_reparent($self->{session}, $old_url, $pool); | ||
| 351 | $self->url($old_url); | ||
| 352 | } | ||
| 353 | |||
| 354 | $pool->clear; | ||
| 355 | $editor->{git_commit_ok}; | ||
| 356 | } | ||
| 357 | |||
| 358 | sub longest_common_path { | ||
| 359 | my ($gsv, $globs) = @_; | ||
| 360 | my %common; | ||
| 361 | my $common_max = scalar @$gsv; | ||
| 362 | |||
| 363 | foreach my $gs (@$gsv) { | ||
| 364 | my @tmp = split m#/#, $gs->path; | ||
| 365 | my $p = ''; | ||
| 366 | foreach (@tmp) { | ||
| 367 | $p .= length($p) ? "/$_" : $_; | ||
| 368 | $common{$p} ||= 0; | ||
| 369 | $common{$p}++; | ||
| 370 | } | ||
| 371 | } | ||
| 372 | $globs ||= []; | ||
| 373 | $common_max += scalar @$globs; | ||
| 374 | foreach my $glob (@$globs) { | ||
| 375 | my @tmp = split m#/#, $glob->{path}->{left}; | ||
| 376 | my $p = ''; | ||
| 377 | foreach (@tmp) { | ||
| 378 | $p .= length($p) ? "/$_" : $_; | ||
| 379 | $common{$p} ||= 0; | ||
| 380 | $common{$p}++; | ||
| 381 | } | ||
| 382 | } | ||
| 383 | |||
| 384 | my $longest_path = ''; | ||
| 385 | foreach (sort {length $b <=> length $a} keys %common) { | ||
| 386 | if ($common{$_} == $common_max) { | ||
| 387 | $longest_path = $_; | ||
| 388 | last; | ||
| 389 | } | ||
| 390 | } | ||
| 391 | $longest_path; | ||
| 392 | } | ||
| 393 | |||
| 394 | sub gs_fetch_loop_common { | ||
| 395 | my ($self, $base, $head, $gsv, $globs) = @_; | ||
| 396 | return if ($base > $head); | ||
| 397 | # Make sure the cat_blob open2 FileHandle is created before calling | ||
| 398 | # SVN::Pool::new_default so that it does not incorrectly end up in the pool. | ||
| 399 | $::_repository->_open_cat_blob_if_needed; | ||
| 400 | my $gpool = SVN::Pool->new_default; | ||
| 401 | my $ra_url = $self->url; | ||
| 402 | my $reload_ra = sub { | ||
| 403 | $_[0] = undef; | ||
| 404 | $self = undef; | ||
| 405 | $RA = undef; | ||
| 406 | $gpool->clear; | ||
| 407 | $self = Git::SVN::Ra->new($ra_url); | ||
| 408 | $ra_invalid = undef; | ||
| 409 | }; | ||
| 410 | my $inc = $_log_window_size; | ||
| 411 | my ($min, $max) = ($base, $head < $base + $inc ? $head : $base + $inc); | ||
| 412 | my $longest_path = longest_common_path($gsv, $globs); | ||
| 413 | my $find_trailing_edge; | ||
| 414 | while (1) { | ||
| 415 | my %revs; | ||
| 416 | my $err; | ||
| 417 | my $err_handler = $SVN::Error::handler; | ||
| 418 | $SVN::Error::handler = sub { | ||
| 419 | ($err) = @_; | ||
| 420 | skip_unknown_revs($err); | ||
| 421 | }; | ||
| 422 | sub _cb { | ||
| 423 | my ($paths, $r, $author, $date, $log) = @_; | ||
| 424 | [ $paths, | ||
| 425 | { author => $author, date => $date, log => $log } ]; | ||
| 426 | } | ||
| 427 | $self->get_log([$longest_path], $min, $max, 0, 1, 1, | ||
| 428 | sub { $revs{$_[1]} = _cb(@_) }); | ||
| 429 | if ($err) { | ||
| 430 | print "Checked through r$max\r"; | ||
| 431 | } else { | ||
| 432 | $find_trailing_edge = 1; | ||
| 433 | } | ||
| 434 | if ($err and $find_trailing_edge) { | ||
| 435 | print STDERR "Path '$longest_path' ", | ||
| 436 | "was probably deleted:\n", | ||
| 437 | $err->expanded_message, | ||
| 438 | "\nWill attempt to follow ", | ||
| 439 | "revisions r$min .. r$max ", | ||
| 440 | "committed before the deletion\n"; | ||
| 441 | my $hi = $max; | ||
| 442 | while (--$hi >= $min) { | ||
| 443 | my $ok; | ||
| 444 | $self->get_log([$longest_path], $min, $hi, | ||
| 445 | 0, 1, 1, sub { | ||
| 446 | $ok = $_[1]; | ||
| 447 | $revs{$_[1]} = _cb(@_) }); | ||
| 448 | if ($ok) { | ||
| 449 | print STDERR "r$min .. r$ok OK\n"; | ||
| 450 | last; | ||
| 451 | } | ||
| 452 | } | ||
| 453 | $find_trailing_edge = 0; | ||
| 454 | } | ||
| 455 | $SVN::Error::handler = $err_handler; | ||
| 456 | |||
| 457 | my %exists = map { $_->path => $_ } @$gsv; | ||
| 458 | foreach my $r (sort {$a <=> $b} keys %revs) { | ||
| 459 | my ($paths, $logged) = @{delete $revs{$r}}; | ||
| 460 | |||
| 461 | foreach my $gs ($self->match_globs(\%exists, $paths, | ||
| 462 | $globs, $r)) { | ||
| 463 | if ($gs->rev_map_max >= $r) { | ||
| 464 | next; | ||
| 465 | } | ||
| 466 | next unless $gs->match_paths($paths, $r); | ||
| 467 | $gs->{logged_rev_props} = $logged; | ||
| 468 | if (my $last_commit = $gs->last_commit) { | ||
| 469 | $gs->assert_index_clean($last_commit); | ||
| 470 | } | ||
| 471 | my $log_entry = $gs->do_fetch($paths, $r); | ||
| 472 | if ($log_entry) { | ||
| 473 | $gs->do_git_commit($log_entry); | ||
| 474 | } | ||
| 475 | $Git::SVN::INDEX_FILES{$gs->{index}} = 1; | ||
| 476 | } | ||
| 477 | foreach my $g (@$globs) { | ||
| 478 | my $k = "svn-remote.$g->{remote}." . | ||
| 479 | "$g->{t}-maxRev"; | ||
| 480 | Git::SVN::tmp_config($k, $r); | ||
| 481 | } | ||
| 482 | $reload_ra->() if $ra_invalid; | ||
| 483 | } | ||
| 484 | # pre-fill the .rev_db since it'll eventually get filled in | ||
| 485 | # with '0' x40 if something new gets committed | ||
| 486 | foreach my $gs (@$gsv) { | ||
| 487 | next if $gs->rev_map_max >= $max; | ||
| 488 | next if defined $gs->rev_map_get($max); | ||
| 489 | $gs->rev_map_set($max, 0 x40); | ||
| 490 | } | ||
| 491 | foreach my $g (@$globs) { | ||
| 492 | my $k = "svn-remote.$g->{remote}.$g->{t}-maxRev"; | ||
| 493 | Git::SVN::tmp_config($k, $max); | ||
| 494 | } | ||
| 495 | last if $max >= $head; | ||
| 496 | $min = $max + 1; | ||
| 497 | $max += $inc; | ||
| 498 | $max = $head if ($max > $head); | ||
| 499 | |||
| 500 | $reload_ra->(); | ||
| 501 | } | ||
| 502 | Git::SVN::gc(); | ||
| 503 | } | ||
| 504 | |||
| 505 | sub get_dir_globbed { | ||
| 506 | my ($self, $left, $depth, $r) = @_; | ||
| 507 | |||
| 508 | my @x = eval { $self->get_dir($left, $r) }; | ||
| 509 | return unless scalar @x == 3; | ||
| 510 | my $dirents = $x[0]; | ||
| 511 | my @finalents; | ||
| 512 | foreach my $de (keys %$dirents) { | ||
| 513 | next if $dirents->{$de}->{kind} != $SVN::Node::dir; | ||
| 514 | if ($depth > 1) { | ||
| 515 | my @args = ("$left/$de", $depth - 1, $r); | ||
| 516 | foreach my $dir ($self->get_dir_globbed(@args)) { | ||
| 517 | push @finalents, "$de/$dir"; | ||
| 518 | } | ||
| 519 | } else { | ||
| 520 | push @finalents, $de; | ||
| 521 | } | ||
| 522 | } | ||
| 523 | @finalents; | ||
| 524 | } | ||
| 525 | |||
| 526 | # return value: 0 -- don't ignore, 1 -- ignore | ||
| 527 | sub is_ref_ignored { | ||
| 528 | my ($g, $p) = @_; | ||
| 529 | my $refname = $g->{ref}->full_path($p); | ||
| 530 | return 1 if defined($g->{ignore_refs_regex}) && | ||
| 531 | $refname =~ m!$g->{ignore_refs_regex}!; | ||
| 532 | return 0 unless defined($_ignore_refs_regex); | ||
| 533 | return 1 if $refname =~ m!$_ignore_refs_regex!o; | ||
| 534 | return 0; | ||
| 535 | } | ||
| 536 | |||
| 537 | sub match_globs { | ||
| 538 | my ($self, $exists, $paths, $globs, $r) = @_; | ||
| 539 | |||
| 540 | sub get_dir_check { | ||
| 541 | my ($self, $exists, $g, $r) = @_; | ||
| 542 | |||
| 543 | my @dirs = $self->get_dir_globbed($g->{path}->{left}, | ||
| 544 | $g->{path}->{depth}, | ||
| 545 | $r); | ||
| 546 | |||
| 547 | foreach my $de (@dirs) { | ||
| 548 | my $p = $g->{path}->full_path($de); | ||
| 549 | next if $exists->{$p}; | ||
| 550 | next if (length $g->{path}->{right} && | ||
| 551 | ($self->check_path($p, $r) != | ||
| 552 | $SVN::Node::dir)); | ||
| 553 | next unless $p =~ /$g->{path}->{regex}/; | ||
| 554 | $exists->{$p} = Git::SVN->init($self->url, $p, undef, | ||
| 555 | $g->{ref}->full_path($de), 1); | ||
| 556 | } | ||
| 557 | } | ||
| 558 | foreach my $g (@$globs) { | ||
| 559 | if (my $path = $paths->{"/$g->{path}->{left}"}) { | ||
| 560 | if ($path->{action} =~ /^[AR]$/) { | ||
| 561 | get_dir_check($self, $exists, $g, $r); | ||
| 562 | } | ||
| 563 | } | ||
| 564 | foreach (keys %$paths) { | ||
| 565 | if (/$g->{path}->{left_regex}/ && | ||
| 566 | !/$g->{path}->{regex}/) { | ||
| 567 | next if $paths->{$_}->{action} !~ /^[AR]$/; | ||
| 568 | get_dir_check($self, $exists, $g, $r); | ||
| 569 | } | ||
| 570 | next unless /$g->{path}->{regex}/; | ||
| 571 | my $p = $1; | ||
| 572 | my $pathname = $g->{path}->full_path($p); | ||
| 573 | next if is_ref_ignored($g, $p); | ||
| 574 | next if $exists->{$pathname}; | ||
| 575 | next if ($self->check_path($pathname, $r) != | ||
| 576 | $SVN::Node::dir); | ||
| 577 | $exists->{$pathname} = Git::SVN->init( | ||
| 578 | $self->url, $pathname, undef, | ||
| 579 | $g->{ref}->full_path($p), 1); | ||
| 580 | } | ||
| 581 | my $c = ''; | ||
| 582 | foreach (split m#/#, $g->{path}->{left}) { | ||
| 583 | $c .= "/$_"; | ||
| 584 | next unless ($paths->{$c} && | ||
| 585 | ($paths->{$c}->{action} =~ /^[AR]$/)); | ||
| 586 | get_dir_check($self, $exists, $g, $r); | ||
| 587 | } | ||
| 588 | } | ||
| 589 | values %$exists; | ||
| 590 | } | ||
| 591 | |||
| 592 | sub minimize_url { | ||
| 593 | my ($self) = @_; | ||
| 594 | return $self->url if ($self->url eq $self->{repos_root}); | ||
| 595 | my $url = $self->{repos_root}; | ||
| 596 | my @components = split(m!/!, $self->{svn_path}); | ||
| 597 | my $c = ''; | ||
| 598 | do { | ||
| 599 | $url = add_path_to_url($url, $c); | ||
| 600 | eval { | ||
| 601 | my $ra = (ref $self)->new($url); | ||
| 602 | my $latest = $ra->get_latest_revnum; | ||
| 603 | $ra->get_log("", $latest, 0, 1, 0, 1, sub {}); | ||
| 604 | }; | ||
| 605 | } while ($@ && ($c = shift @components)); | ||
| 606 | |||
| 607 | return canonicalize_url($url); | ||
| 608 | } | ||
| 609 | |||
| 610 | sub can_do_switch { | ||
| 611 | my $self = shift; | ||
| 612 | unless (defined $can_do_switch) { | ||
| 613 | my $pool = SVN::Pool->new; | ||
| 614 | my $rep = eval { | ||
| 615 | $self->do_switch(1, '', 0, $self->url, | ||
| 616 | SVN::Delta::Editor->new, $pool); | ||
| 617 | }; | ||
| 618 | if ($@) { | ||
| 619 | $can_do_switch = 0; | ||
| 620 | } else { | ||
| 621 | $rep->abort_report($pool); | ||
| 622 | $can_do_switch = 1; | ||
| 623 | } | ||
| 624 | $pool->clear; | ||
| 625 | } | ||
| 626 | $can_do_switch; | ||
| 627 | } | ||
| 628 | |||
| 629 | sub skip_unknown_revs { | ||
| 630 | my ($err) = @_; | ||
| 631 | my $errno = $err->apr_err(); | ||
| 632 | # Maybe the branch we're tracking didn't | ||
| 633 | # exist when the repo started, so it's | ||
| 634 | # not an error if it doesn't, just continue | ||
| 635 | # | ||
| 636 | # Wonderfully consistent library, eh? | ||
| 637 | # 160013 - svn:// and file:// | ||
| 638 | # 175002 - http(s):// | ||
| 639 | # 175007 - http(s):// (this repo required authorization, too...) | ||
| 640 | # More codes may be discovered later... | ||
| 641 | if ($errno == 175007 || $errno == 175002 || $errno == 160013) { | ||
| 642 | my $err_key = $err->expanded_message; | ||
| 643 | # revision numbers change every time, filter them out | ||
| 644 | $err_key =~ s/\d+/\0/g; | ||
| 645 | $err_key = "$errno\0$err_key"; | ||
| 646 | unless ($ignored_err{$err_key}) { | ||
| 647 | warn "W: Ignoring error from SVN, path probably ", | ||
| 648 | "does not exist: ($errno): ", | ||
| 649 | $err->expanded_message,"\n"; | ||
| 650 | warn "W: Do not be alarmed at the above message ", | ||
| 651 | "git-svn is just searching aggressively for ", | ||
| 652 | "old history.\n", | ||
| 653 | "This may take a while on large repositories\n"; | ||
| 654 | $ignored_err{$err_key} = 1; | ||
| 655 | } | ||
| 656 | return; | ||
| 657 | } | ||
| 658 | die "Error from SVN, ($errno): ", $err->expanded_message,"\n"; | ||
| 659 | } | ||
| 660 | |||
| 661 | 1; | ||
| 662 | __END__ |