use warnings;
use strict;
use vars qw/ $AUTHOR $VERSION
- $SVN_URL
- $GIT_SVN_INDEX $GIT_SVN
- $GIT_DIR $GIT_SVN_DIR $REVDB
- $_follow_parent $sha1 $sha1_short $_revision
- $_cp_remote $_upgrade $_rmdir $_q $_cp_similarity
- $_find_copies_harder $_l $_authors %users/;
+ $sha1 $sha1_short $_revision
+ $_q $_authors %users/;
$AUTHOR = 'Eric Wong <normalperson@yhbt.net>';
$VERSION = '@@GIT_VERSION@@';
$Git::SVN::default_repo_id = 'git-svn';
$Git::SVN::default_ref_id = $ENV{GIT_SVN_ID} || 'git-svn';
-my $LC_ALL = $ENV{LC_ALL};
$Git::SVN::Log::TZ = $ENV{TZ};
-# make sure the svn binary gives consistent output between locales and TZs:
$ENV{TZ} = 'UTC';
-$ENV{LC_ALL} = 'C';
$| = 1; # unbuffer STDOUT
sub fatal (@) { print STDERR @_; exit 1 }
$sha1_short = qr/[a-f\d]{4,40}/;
my ($_stdin, $_help, $_edit,
$_repack, $_repack_nr, $_repack_flags,
- $_message, $_file, $_no_metadata,
+ $_message, $_file,
$_template, $_shared,
- $_version, $_upgrade,
+ $_version,
$_merge, $_strategy, $_dry_run,
$_prefix);
my %remote_opts = ( 'username=s' => \$Git::SVN::Prompt::_username,
'config-dir=s' => \$Git::SVN::Ra::config_dir,
'no-auth-cache' => \$Git::SVN::Prompt::_no_auth_cache );
-my %fc_opts = ( 'follow-parent|follow' => \$_follow_parent,
+my %fc_opts = ( 'follow-parent|follow' => \$Git::SVN::_follow_parent,
'authors-file|A=s' => \$_authors,
'repack:i' => \$_repack,
- 'no-metadata' => \$_no_metadata,
+ 'no-metadata' => \$Git::SVN::_no_metadata,
'quiet|q' => \$_q,
'repack-flags|repack-args|repack-opts=s' => \$_repack_flags,
%remote_opts );
'branches|b=s' => \$_branches );
my %init_opts = ( 'template=s' => \$_template, 'shared' => \$_shared );
my %cmt_opts = ( 'edit|e' => \$_edit,
- 'rmdir' => \$_rmdir,
- 'find-copies-harder' => \$_find_copies_harder,
- 'l=i' => \$_l,
- 'copy-similarity|C=i'=> \$_cp_similarity
+ 'rmdir' => \$SVN::Git::Editor::_rmdir,
+ 'find-copies-harder' => \$SVN::Git::Editor::_find_copies_harder,
+ 'l=i' => \$SVN::Git::Editor::_rename_limit,
+ 'copy-similarity|C=i'=> \$SVN::Git::Editor::_cp_similarity
);
my %cmd = (
{ 'stdin|' => \$_stdin, %cmt_opts, %fc_opts, } ],
'show-ignore' => [ \&cmd_show_ignore, "Show svn:ignore listings",
{ 'revision|r=i' => \$_revision } ],
- rebuild => [ \&cmd_rebuild, "Rebuild git-svn metadata (after git clone)",
- { 'copy-remote|remote=s' => \$_cp_remote,
- 'upgrade' => \$_upgrade } ],
'multi-init' => [ \&cmd_multi_init,
'Initialize multiple trees (like git-svnimport)',
{ %multi_opts, %init_opts, %remote_opts,
my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
read_repo_config(\%opts);
-my $rv = GetOptions(%opts, 'help|H|h' => \$_help,
- 'version|V' => \$_version,
- 'minimize-connections' =>
- \$Git::SVN::Migration::_minimize,
- 'id|i=s' => \$Git::SVN::default_ref_id);
+my $rv = GetOptions(%opts, 'help|H|h' => \$_help, 'version|V' => \$_version,
+ 'minimize-connections' => \$Git::SVN::Migration::_minimize,
+ 'id|i=s' => \$Git::SVN::default_ref_id,
+ 'svn-remote|remote|R=s' => \$Git::SVN::default_repo_id);
exit 1 if (!$rv && $cmd ne 'log');
usage(0) if $_help;
version() if $_version;
usage(1) unless defined $cmd;
load_authors() if $_authors;
-unless ($cmd =~ /^(?:init|rebuild|multi-init|commit-diff)$/) {
+unless ($cmd =~ /^(?:init|multi-init|commit-diff)$/) {
Git::SVN::Migration::migration_check();
}
eval {
exit 0;
}
-sub cmd_rebuild {
- my $url = shift;
- my $gs = $url ? Git::SVN->init($url)
- : eval { Git::SVN->new };
- $gs ||= Git::SVN->_new;
- if (!verify_ref($gs->refname.'^0')) {
- $gs->copy_remote_ref;
- }
-
- my ($rev_list, $ctx) = command_output_pipe("rev-list", $gs->refname);
- my $latest;
- my $svn_uuid;
- while (<$rev_list>) {
- chomp;
- my $c = $_;
- fatal "Non-SHA1: $c\n" unless $c =~ /^$sha1$/o;
- my ($url, $rev, $uuid) = cmt_metadata($c);
-
- # ignore merges (from set-tree)
- next if (!defined $rev || !$uuid);
-
- # if we merged or otherwise started elsewhere, this is
- # how we break out of it
- if ((defined $svn_uuid && ($uuid ne $svn_uuid)) ||
- ($gs->{url} && $url && ($url ne $gs->{url}))) {
- next;
- }
-
- unless (defined $latest) {
- if (!$gs->{url} && !$url) {
- fatal "SVN repository location required\n";
- }
- $gs = Git::SVN->init($url);
- $latest = $rev;
- }
- $gs->rev_db_set($rev, $c);
- print "r$rev = $c\n";
- }
- command_close_pipe($rev_list, $ctx);
-}
-
sub do_git_init_db {
unless (-d $ENV{GIT_DIR}) {
my @init_db = ('init');
package Git::SVN;
use strict;
use warnings;
-use vars qw/$default_repo_id $default_ref_id/;
+use vars qw/$default_repo_id $default_ref_id $_no_metadata $_follow_parent/;
use Carp qw/croak/;
use File::Path qw/mkpath/;
use IPC::Open3;
$self->{url} = command_oneline('config', '--get',
"svn-remote.$repo_id.url") or
die "Failed to read \"svn-remote.$repo_id.url\" in config\n";
+ if (-z $self->{db_path} && ::verify_ref($self->refname.'^0')) {
+ $self->rebuild;
+ }
$self;
}
sub ra {
my ($self) = shift;
- $self->{ra} ||= Git::SVN::Ra->new($self->{url});
+ Git::SVN::Ra->new($self->{url});
}
sub rel_path {
$url;
}
-sub copy_remote_ref {
- my ($self) = @_;
- my $origin = $::_cp_remote ? $::_cp_remote : 'origin';
- my $ref = $self->refname;
- if (command('ls-remote', $origin, $ref)) {
- command_noisy('fetch', $origin, "$ref:$ref");
- } elsif ($::_cp_remote && !$::_upgrade) {
- die "Unable to find remote reference: $ref on $origin\n";
- }
-}
-
sub traverse_ignore {
my ($self, $fh, $path, $r) = @_;
$path =~ s#^/+##g;
- my ($dirent, undef, $props) = $self->ra->get_dir($path, $r);
+ my $ra = $self->ra;
+ my ($dirent, undef, $props) = $ra->get_dir($path, $r);
my $p = $path;
- $p =~ s#^\Q$self->{ra}->{svn_path}\E/##;
+ $p =~ s#^\Q$ra->{svn_path}\E/##;
print $fh length $p ? "\n# $p\n" : "\n# /\n";
if (my $s = $props->{'svn:ignore'}) {
$s =~ s/[\r\n]+/\n/g;
sub full_url {
my ($self) = @_;
- $self->ra->{url} . (length $self->{path} ? '/' . $self->{path} : '');
+ $self->{url} . (length $self->{path} ? '/' . $self->{path} : '');
}
sub do_git_commit {
my ($self, $log_entry) = @_;
+ my $lr = $self->last_rev;
+ if (defined $lr && $lr >= $log_entry->{revision}) {
+ die "Last fetched revision of ", $self->refname,
+ " was r$lr, but we are about to fetch: ",
+ "r$log_entry->{revision}!\n";
+ }
if (my $c = $self->rev_db_get($log_entry->{revision})) {
croak "$log_entry->{revision} = $c already exists! ",
"Why are we refetching it?\n";
defined(my $pid = open3(my $msg_fh, my $out_fh, '>&STDERR', @exec))
or croak $!;
print $msg_fh $log_entry->{log} or croak $!;
- print $msg_fh "\ngit-svn-id: ", $self->full_url, '@',
- $log_entry->{revision}, ' ',
- $self->ra->uuid, "\n" or croak $!;
+ unless ($_no_metadata) {
+ print $msg_fh "\ngit-svn-id: ", $self->full_url, '@',
+ $log_entry->{revision}, ' ',
+ $self->ra->uuid, "\n" or croak $!;
+ }
$msg_fh->flush == 0 or croak $!;
close $msg_fh or croak $!;
chomp(my $commit = do { local $/; <$out_fh> });
$self->{last_rev} = $log_entry->{revision};
$self->{last_commit} = $commit;
- print "r$log_entry->{revision} = $commit\n";
+ print "r$log_entry->{revision} = $commit ($self->{ref_id})\n";
return $commit;
}
sub find_parent_branch {
my ($self, $paths, $rev) = @_;
- return undef unless $::_follow_parent;
+ return undef unless $_follow_parent;
unless (defined $paths) {
- $self->ra->get_log([$self->{path}], $rev, $rev, 0, 1, 1,
- sub { $paths = dup_changed_paths($_[0]) });
+ my $err_handler = $SVN::Error::handler;
+ $SVN::Error::handler = \&Git::SVN::Ra::skip_unknown_revs;
+ $self->ra->get_log([$self->{path}], $rev, $rev, 0, 1, 1, sub {
+ $paths =
+ Git::SVN::Ra::dup_changed_paths($_[0]) });
+ $SVN::Error::handler = $err_handler;
}
return undef unless defined $paths;
last if $gs;
}
unless ($gs) {
- my $ref_id = $branch_from;
- $ref_id .= "\@$r" if find_ref($ref_id);
+ my $ref_id = $self->{ref_id};
+ $ref_id =~ s/\@\d+$//;
+ $ref_id .= "\@$r";
# just grow a tail if we're not unique enough :x
$ref_id .= '-' while find_ref($ref_id);
+ print STDERR "Initializing parent: $ref_id\n";
$gs = Git::SVN->init($new_url, '', $ref_id, $ref_id);
}
my ($r0, $parent) = $gs->find_rev_before($r, 1);
- if ($::_follow_parent && (!defined $r0 || !defined $parent)) {
+ if ($_follow_parent && (!defined $r0 || !defined $parent)) {
$gs->fetch(0, $r);
($r0, $parent) = $gs->last_rev_commit;
}
if ($self->ra->can_do_switch) {
print STDERR "Following parent with do_switch\n";
# do_switch works with svn/trunk >= r22312, but that
- # is not included with SVN 1.4.2 (the latest version
+ # is not included with SVN 1.4.3 (the latest version
# at the moment), so we can't rely on it
$self->{last_commit} = $parent;
$ed = SVN::Git::Fetcher->new($self);
- $gs->ra->gs_do_switch($r0, $rev, $gs->{path}, 1,
+ $gs->ra->gs_do_switch($r0, $rev, $gs,
$self->full_url, $ed)
or die "SVN connection failed somewhere...\n";
} else {
print STDERR "Following parent with do_update\n";
$ed = SVN::Git::Fetcher->new($self);
- $self->ra->gs_do_update($rev, $rev, $self->{path},
- 1, $ed)
+ $self->ra->gs_do_update($rev, $rev, $self, $ed)
or die "SVN connection failed somewhere...\n";
}
+ print STDERR "Successfully followed parent\n";
return $self->make_log_entry($rev, [$parent], $ed);
}
not_found:
}
$ed = SVN::Git::Fetcher->new($self);
}
- unless ($self->ra->gs_do_update($last_rev, $rev,
- $self->{path}, 1, $ed)) {
+ unless ($self->ra->gs_do_update($last_rev, $rev, $self, $ed)) {
die "SVN connection failed somewhere...\n";
}
$self->make_log_entry($rev, \@parents, $ed);
my ($self, $rev, $parents, $ed) = @_;
my $untracked = $self->get_untracked($ed);
- return undef if ($ed->{nr} == 0 && scalar @$untracked == 0);
-
open my $un, '>>', "$self->{dir}/unhandled.log" or croak $!;
print $un "r$rev\n" or croak $!;
print $un $_, "\n" foreach @$untracked;
}
}
+sub rebuild {
+ my ($self) = @_;
+ print "Rebuilding $self->{db_path} ...\n";
+ my ($rev_list, $ctx) = command_output_pipe("rev-list", $self->refname);
+ my $latest;
+ my $full_url = $self->full_url;
+ my $svn_uuid;
+ while (<$rev_list>) {
+ chomp;
+ my $c = $_;
+ die "Non-SHA1: $c\n" unless $c =~ /^$::sha1$/o;
+ my ($url, $rev, $uuid) = ::cmt_metadata($c);
+
+ # ignore merges (from set-tree)
+ next if (!defined $rev || !$uuid);
+
+ # if we merged or otherwise started elsewhere, this is
+ # how we break out of it
+ if ((defined $svn_uuid && ($uuid ne $svn_uuid)) ||
+ ($full_url && $url && ($url ne $full_url))) {
+ next;
+ }
+ $latest ||= $rev;
+ $svn_uuid ||= $uuid;
+
+ $self->rev_db_set($rev, $c);
+ print "r$rev = $c\n";
+ }
+ command_close_pipe($rev_list, $ctx);
+ print "Done rebuilding $self->{db_path}\n";
+}
+
# rev_db:
# Tie::File seems to be prone to offset errors if revisions get sparse,
# it's not that fast, either. Tie::File is also not in Perl 5.6. So
package main;
-sub uri_encode {
- my ($f) = @_;
- $f =~ s#([^a-zA-Z0-9\*!\:_\./\-])#uc sprintf("%%%02x",ord($1))#eg;
- $f
-}
-
-sub uri_decode {
- my ($f) = @_;
- $f =~ tr/+/ /;
- $f =~ s/%([A-F0-9]{2})/chr hex($1)/ge;
- $f
-}
-
{
my $kill_stupid_warnings = $SVN::Node::none.$SVN::Node::file.
$SVN::Node::dir.$SVN::Node::unknown.
use warnings;
use Carp qw/croak/;
use IO::File qw//;
+use Digest::MD5;
# file baton members: path, mode_a, mode_b, pool, fh, blob, base
sub new {
$self->{absent_dir} = {};
$self->{absent_file} = {};
$self->{gii} = $git_svn->tmp_index_do(sub { Git::IndexInfo->new });
- require Digest::MD5;
$self;
}
sub git_path {
my ($self, $path) = @_;
- $path =~ s!$self->{path_strip}!! if $self->{path_strip};
+ if ($self->{path_strip}) {
+ $path =~ s!$self->{path_strip}!! or
+ die "Failed to strip path '$path' ($self->{path_strip})\n";
+ }
$path;
}
my ($self, $path, $rev, $pb) = @_;
my $gpath = $self->git_path($path);
+ return undef if ($gpath eq '');
+
# remove entire directories.
if (command('ls-tree', $self->{c}, '--', $gpath) =~ /^040000 tree/) {
my ($ls, $ctx) = command_output_pipe(qw/ls-tree
}
package SVN::Git::Editor;
-use vars qw/@ISA/;
+use vars qw/@ISA $_rmdir $_cp_similarity $_find_copies_harder $_rename_limit/;
use strict;
use warnings;
use Carp qw/croak/;
use IO::File;
+use Digest::MD5;
sub new {
my ($class, $opts) = @_;
$self->{rm} = { };
$self->{path_prefix} = length $self->{svn_path} ?
"$self->{svn_path}/" : '';
- require Digest::MD5;
return $self;
}
sub generate_diff {
my ($tree_a, $tree_b) = @_;
my @diff_tree = qw(diff-tree -z -r);
- if ($::_cp_similarity) {
- push @diff_tree, "-C$::_cp_similarity";
+ if ($_cp_similarity) {
+ push @diff_tree, "-C$_cp_similarity";
} else {
push @diff_tree, '-C';
}
- push @diff_tree, '--find-copies-harder' if $::_find_copies_harder;
- push @diff_tree, "-l$::_l" if defined $::_l;
+ push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
+ push @diff_tree, "-l$_rename_limit" if defined $_rename_limit;
push @diff_tree, $tree_a, $tree_b;
my ($diff_fh, $ctx) = command_output_pipe(@diff_tree);
local $/ = "\0";
fatal("Invalid change type: $f\n");
}
}
- $self->rmdirs if $::_rmdir;
+ $self->rmdirs if $_rmdir;
if (@$mods == 0) {
$self->abort_edit;
} else {
use strict;
use warnings;
my ($can_do_switch);
-my %RA;
+my $RA;
BEGIN {
# enforce temporary pool usage for some simple functions
sub new {
my ($class, $url) = @_;
$url =~ s!/+$!!;
- return $RA{$url} if $RA{$url};
+ return $RA if ($RA && $RA->{url} eq $url);
SVN::_Core::svn_config_ensure($config_dir, undef);
my ($baton, $callbacks) = SVN::Core::auth_open_helper([
$self->{svn_path} = $url;
$self->{repos_root} = $self->get_repos_root;
$self->{svn_path} =~ s#^\Q$self->{repos_root}\E/*##;
- $RA{$url} = bless $self, $class;
+ $RA = bless $self, $class;
}
sub DESTROY {
- # do not call the real DESTROY since we store ourselves in %RA
+ # do not call the real DESTROY since we store ourselves in $RA
}
sub get_log {
}
sub gs_do_update {
- my ($self, $rev_a, $rev_b, $path, $recurse, $editor) = @_;
+ my ($self, $rev_a, $rev_b, $gs, $editor) = @_;
+ my $new = ($rev_a == $rev_b);
+ my $path = $gs->{path};
+
+ my $ta = $self->check_path($path, $rev_a);
+ my $tb = $new ? $ta : $self->check_path($path, $rev_b);
+ return 1 if ($tb != $SVN::Node::dir && $ta != $SVN::Node::dir);
+ if ($ta == $SVN::Node::none) {
+ $rev_a = $rev_b;
+ $new = 1;
+ }
+
my $pool = SVN::Pool->new;
$editor->set_path_strip($path);
- my $reporter = $self->do_update($rev_b, $path, $recurse,
- $editor, $pool);
+ my (@pc) = split m#/#, $path;
+ my $reporter = $self->do_update($rev_b, (@pc ? shift @pc : ''),
+ 1, $editor, $pool);
my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : ();
- my $new = ($rev_a == $rev_b);
- $reporter->set_path('', $rev_a, $new, @lock, $pool);
+
+ # Since we can't rely on svn_ra_reparent being available, we'll
+ # just have to do some magic with set_path to make it so
+ # we only want a partial path.
+ my $sp = '';
+ my $final = join('/', @pc);
+ while (@pc) {
+ $reporter->set_path($sp, $rev_b, 0, @lock, $pool);
+ $sp .= '/' if length $sp;
+ $sp .= shift @pc;
+ }
+ die "BUG: '$sp' != '$final'\n" if ($sp ne $final);
+
+ $reporter->set_path($sp, $rev_a, $new, @lock, $pool);
+
$reporter->finish_report($pool);
$pool->clear;
$editor->{git_commit_ok};
}
+# this requires SVN 1.4.3 or later (do_switch didn't work before 1.4.3, and
+# svn_ra_reparent didn't work before 1.4)
sub gs_do_switch {
- my ($self, $rev_a, $rev_b, $path, $recurse, $url_b, $editor) = @_;
+ my ($self, $rev_a, $rev_b, $gs, $url_b, $editor) = @_;
+ my $path = $gs->{path};
my $pool = SVN::Pool->new;
- $editor->set_path_strip($path);
- my $reporter = $self->do_switch($rev_b, $path, $recurse,
- $url_b, $editor, $pool);
+
+ my $full_url = $self->{url};
+ my $old_url = $full_url;
+ $full_url .= "/$path" if length $path;
+ my ($ra, $reparented);
+ if ($old_url ne $full_url) {
+ if ($old_url !~ m#^svn(\+ssh)?://#) {
+ SVN::_Ra::svn_ra_reparent($self->{session}, $full_url,
+ $pool);
+ $self->{url} = $full_url;
+ $reparented = 1;
+ } else {
+ $ra = Git::SVN::Ra->new($full_url);
+ }
+ }
+ $ra ||= $self;
+ my $reporter = $ra->do_switch($rev_b, '', 1, $url_b, $editor, $pool);
my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : ();
$reporter->set_path('', $rev_a, 0, @lock, $pool);
$reporter->finish_report($pool);
+
+ if ($reparented) {
+ SVN::_Ra::svn_ra_reparent($self->{session}, $old_url, $pool);
+ $self->{url} = $old_url;
+ }
+
$pool->clear;
$editor->{git_commit_ok};
}
my ($self, $base, $head, @gs) = @_;
my $inc = 1000;
my ($min, $max) = ($base, $head < $base + $inc ? $head : $base + $inc);
- my $err_handler = $SVN::Error::handler;
- my $err;
- $SVN::Error::handler = sub { ($err) = @_; skip_unknown_revs($err); };
- my @paths = @gs == 1 ? ($gs[0]->{path}) : ('');
foreach my $gs (@gs) {
if (my $last_commit = $gs->last_commit) {
$gs->assert_index_clean($last_commit);
}
- $gs->{path_regex} = qr/^\/\Q$gs->{path}\E\/?/;
}
while (1) {
- my @revs;
- $self->get_log(\@paths, $min, $max, 0, 1, 1,
- sub { push @revs, [ dup_changed_paths($_[0]), $_[1] ]; });
- if (! @revs && $err && $max >= $head) {
- print STDERR "Branch probably deleted:\n ",
- $err->expanded_message,
- "\nWill attempt to follow revisions ",
- "r$min .. r$max ",
- "committed before the deletion\n";
- @revs = map { [ undef, $_ ] } ($min .. $max);
- }
- foreach (@revs) {
- my ($paths, $r) = @$_;
- foreach my $gs (@gs) {
- if ($paths) {
- grep /$gs->{path_regex}/, keys %$paths
- or next;
+ my %revs;
+ my $err;
+ my $err_handler = $SVN::Error::handler;
+ $SVN::Error::handler = sub {
+ ($err) = @_;
+ skip_unknown_revs($err);
+ };
+ foreach my $gs (@gs) {
+ $self->get_log([$gs->{path}], $min, $max, 0, 1, 1, sub
+ { my ($paths, $rev) = @_;
+ push @{$revs{$rev}},
+ [ $gs,
+ dup_changed_paths($paths) ] });
+
+ next unless ($err && $max >= $head);
+
+ print STDERR "Path '$gs->{path}' ",
+ "was probably deleted:\n",
+ $err->expanded_message,
+ "\nWill attempt to follow ",
+ "revisions r$min .. r$max ",
+ "committed before the deletion\n";
+ my $hi = $max;
+ while (--$hi >= $min) {
+ my $ok;
+ $self->get_log([$gs->{path}], $min, $hi,
+ 0, 1, 1, sub {
+ my ($paths, $rev) = @_;
+ $ok = $rev;
+ push @{$revs{$rev}}, [ $gs,
+ dup_changed_paths($_[0])]});
+ if ($ok) {
+ print STDERR "r$min .. r$ok OK\n";
+ last;
}
+ }
+ }
+ $SVN::Error::handler = $err_handler;
+ foreach my $r (sort {$a <=> $b} keys %revs) {
+ foreach (@{$revs{$r}}) {
+ my ($gs, $paths) = @$_;
+ my $lr = $gs->last_rev;
+ next if defined $lr && $lr >= $r;
next if defined $gs->rev_db_get($r);
if (my $log_entry = $gs->do_fetch($paths, $r)) {
$gs->do_git_commit($log_entry);
$max += $inc;
$max = $head if ($max > $head);
}
- $SVN::Error::handler = $err_handler;
}
sub minimize_url {