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');
instead.\n";
}
my $gs = Git::SVN->new;
- $gs->fetch;
+ $gs->fetch(parse_revision_argument());
if ($gs->{last_commit} && !verify_ref('refs/heads/master^0')) {
command_noisy(qw(update-ref refs/heads/master),
$gs->{last_commit});
if ($_dry_run) {
print "diff-tree $d~1 $d\n";
} else {
- my $log = get_commit_entry($d)->{log};
- my $ra = $gs->ra;
- my $pool = SVN::Pool->new;
my %ed_opts = ( r => $last_rev,
- ra => $ra->dup,
- svn_path => $ra->{svn_path} );
- my $ed = SVN::Git::Editor->new(\%ed_opts,
- $ra->get_commit_editor($log,
- sub { print "Committed r$_[0]\n";
- $last_rev = $_[0]; }),
- $pool);
- my $mods = $ed->apply_diff("$d~1", $d);
- if (@$mods == 0) {
+ log => get_commit_entry($d)->{log},
+ ra => $gs->ra,
+ tree_a => "$d~1",
+ tree_b => $d,
+ editor_cb => sub {
+ print "Committed r$_[0]\n";
+ $last_rev = $_[0]; },
+ svn_path => $gs->{path} );
+ if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
print "No changes\n$d~1 == $d\n";
}
}
}
sub cmd_multi_fetch {
- my @gs;
- foreach (command(qw/config -l/)) {
- next unless m!^svn-remote\.(.+)\.fetch=
- \s*(.*)\s*:\s*refs/remotes/(.+)\s*$!x;
- my ($repo_id, $path, $ref_id) = ($1, $2, $3);
- push @gs, Git::SVN->new($ref_id, $repo_id, $path);
- }
- foreach (@gs) {
- $_->fetch;
+ my $remotes = Git::SVN::read_all_remotes();
+ foreach my $repo_id (sort keys %$remotes) {
+ my $url = $remotes->{$repo_id}->{url} or next;
+ my $fetch = $remotes->{$repo_id}->{fetch} or next;
+ Git::SVN::fetch_all($repo_id, $url, $fetch);
}
}
my $usage = "Usage: $0 commit-diff -r<revision> ".
"<tree-ish> <tree-ish> [<URL>]\n";
fatal($usage) if (!defined $ta || !defined $tb);
+ my $svn_path;
if (!defined $url) {
my $gs = eval { Git::SVN->new };
if (!$gs) {
"the command-line\n", $usage);
}
$url = $gs->{url};
+ $svn_path = $gs->{path};
}
unless (defined $_revision) {
fatal("-r|--revision is a required argument\n", $usage);
$_message ||= get_commit_entry($tb)->{log};
}
my $ra ||= Git::SVN::Ra->new($url);
+ $svn_path ||= $ra->{svn_path};
my $r = $_revision;
if ($r eq 'HEAD') {
$r = $ra->get_latest_revnum;
} elsif ($r !~ /^\d+$/) {
die "revision argument: $r not understood by git-svn\n";
}
- my $pool = SVN::Pool->new;
my %ed_opts = ( r => $r,
- ra => $ra->dup,
- svn_path => $ra->{svn_path} );
- my $ed = SVN::Git::Editor->new(\%ed_opts,
- $ra->get_commit_editor($_message,
- sub { print "Committed r$_[0]\n" }),
- $pool);
- my $mods = $ed->apply_diff($ta, $tb);
- if (@$mods == 0) {
+ log => $_message,
+ ra => $ra,
+ tree_a => $ta,
+ tree_b => $tb,
+ editor_cb => sub { print "Committed r$_[0]\n" },
+ svn_path => $svn_path );
+ if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
print "No changes\n$ta == $tb\n";
}
- $pool->clear;
}
########################### utility functions #########################
+sub parse_revision_argument {
+ if (!defined $_revision || $_revision eq 'BASE:HEAD') {
+ return (undef, undef);
+ }
+ return ($1, $2) if ($_revision =~ /^(\d+):(\d+)$/);
+ return ($_revision, $_revision) if ($_revision =~ /^\d+$/);
+ return (undef, $1) if ($_revision =~ /^BASE:(\d+)$/);
+ return ($1, undef) if ($_revision =~ /^(\d+):HEAD$/);
+ die "revision argument: $_revision not understood by git-svn\n",
+ "Try using the command-line svn client instead\n";
+}
+
sub complete_svn_url {
my ($url, $path) = @_;
$path =~ s#/+$##;
command(qw/cat-file commit/, shift)))[-1]);
}
-sub get_commit_time {
- my $cmt = shift;
- my $fh = command_output_pipe(qw/rev-list --pretty=raw -n1/, $cmt);
- while (<$fh>) {
- /^committer\s(?:.+) (\d+) ([\-\+]?\d+)$/ or next;
- my ($s, $tz) = ($1, $2);
- if ($tz =~ s/^\+//) {
- $s += tz_to_s_offset($tz);
- } elsif ($tz =~ s/^\-//) {
- $s -= tz_to_s_offset($tz);
- }
- close $fh;
- return $s;
- }
- die "Can't get commit time for commit: $cmt\n";
-}
-
-sub tz_to_s_offset {
- my ($tz) = @_;
- $tz =~ s/(\d\d)$//;
- return ($1 * 60) + ($tz * 3600);
-}
-
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;
svn:entry:committed-date/;
}
+sub fetch_all {
+ my ($repo_id, $url, $fetch) = @_;
+ my @gs;
+ my $ra = Git::SVN::Ra->new($url);
+ my $head = $ra->get_latest_revnum;
+ my $base = $head;
+ my $new_remote;
+ foreach my $p (sort keys %$fetch) {
+ my $gs = Git::SVN->new($fetch->{$p}, $repo_id, $p);
+ my $lr = $gs->last_rev;
+ if (defined $lr) {
+ $base = $lr if ($lr < $base);
+ } else {
+ $new_remote = 1;
+ }
+ push @gs, $gs;
+ }
+ $base = 0 if $new_remote;
+ return if (++$base > $head);
+ $ra->gs_fetch_loop_common($base, $head, @gs);
+}
+
sub read_all_remotes {
my $r = {};
foreach (grep { s/^svn-remote\.// } command(qw/config -l/)) {
$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 last_rev { ($_[0]->last_rev_commit)[0] }
+sub last_commit { ($_[0]->last_rev_commit)[1] }
+
# returns the newest SVN revision number and newest commit SHA1
sub last_rev_commit {
my ($self) = @_;
return ($rev, $c);
}
-sub parse_revision {
- my ($self, $base) = @_;
- my $head = $self->ra->get_latest_revnum;
- if (!defined $::_revision || $::_revision eq 'BASE:HEAD') {
- return ($base + 1, $head) if (defined $base);
- return (0, $head);
- }
- return ($1, $2) if ($::_revision =~ /^(\d+):(\d+)$/);
- return ($::_revision, $::_revision) if ($::_revision =~ /^\d+$/);
- if ($::_revision =~ /^BASE:(\d+)$/) {
- return ($base + 1, $1) if (defined $base);
- return (0, $head);
- }
- return ($1, $head) if ($::_revision =~ /^(\d+):HEAD$/);
- die "revision argument: $::_revision not understood by git-svn\n",
- "Try using the command-line svn client instead\n";
+sub get_fetch_range {
+ my ($self, $min, $max) = @_;
+ $max ||= $self->ra->get_latest_revnum;
+ $min ||= $self->last_rev || 0;
+ (++$min, $max);
}
sub tmp_index_do {
}
sub get_commit_parents {
- my ($self, $log_entry, @parents) = @_;
+ my ($self, $log_entry) = @_;
my (%seen, @ret, @tmp);
- # commit parents can be conditionally bound to a particular
- # svn revision via: "svn_revno=commit_sha1", filter them out here:
- foreach my $p (@parents) {
- next unless defined $p;
- if ($p =~ /^(\d+)=($::sha1_short)$/o) {
- push @tmp, $2 if $1 == $log_entry->{revision};
- } else {
- push @tmp, $p if $p =~ /^$::sha1_short$/o;
+ # legacy support for 'set-tree'; this is only used by set_tree_cb:
+ if (my $ip = $self->{inject_parents}) {
+ if (my $commit = delete $ip->{$log_entry->{revision}}) {
+ push @tmp, $commit;
}
}
if (my $cur = ::verify_ref($self->refname.'^0')) {
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, @parents) = @_;
+ 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";
die "Tree is not a valid sha1: $tree\n" if $tree !~ /^$::sha1$/o;
my @exec = ('git-commit-tree', $tree);
- foreach ($self->get_commit_parents($log_entry, @parents)) {
+ foreach ($self->get_commit_parents($log_entry)) {
push @exec, '-p', $_;
}
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) {
+ 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;
# look for a parent from another branch:
my @b_path_components = split m#/#, $self->rel_path;
unshift(@a_path_components, pop(@b_path_components));
}
goto not_found unless defined $i;
- my $branch_from = $i->copyfrom_path or goto not_found;
+ my $branch_from = $i->{copyfrom_path} or goto not_found;
if (@a_path_components) {
print STDERR "branch_from: $branch_from => ";
$branch_from .= '/'.join('/', @a_path_components);
print STDERR $branch_from, "\n";
}
- my $r = $i->copyfrom_rev;
+ my $r = $i->{copyfrom_rev};
my $repos_root = $self->ra->{repos_root};
my $url = $self->ra->{url};
my $new_url = $repos_root . $branch_from;
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)) {
- $gs->ra->get_log([$gs->{path}], 0, $r, 0, 1, 1, sub {
- my ($paths, $rev) = @_;
- my $log_entry = eval { $gs->do_fetch($paths, $rev) };
- $gs->do_git_commit($log_entry) if $log_entry;
- });
+ if ($_follow_parent && (!defined $r0 || !defined $parent)) {
+ $gs->fetch(0, $r);
($r0, $parent) = $gs->last_rev_commit;
}
if (defined $r0 && defined $parent && $gs->revisions_eq($r0, $r)) {
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:
print STDERR "Branch parent for path: '/",
- $self->rel_path, "' not found\n";
+ $self->rel_path, "' @ r$rev not found:\n";
return undef unless $paths;
+ print STDERR "Changed paths:\n";
foreach my $x (sort keys %$paths) {
my $p = $paths->{$x};
- print STDERR ' ', $p->action, ' ', $x;
- if (my $cp_from = $p->copyfrom_path) {
- print STDERR "(from $cp_from:", $p->copyfrom_rev, ')';
+ print STDERR "\t$p->{action}\t$x";
+ if ($p->{copyfrom_path}) {
+ print STDERR "(from $p->{copyfrom_path}: ",
+ "$p->{copyfrom_rev})";
}
print STDERR "\n";
}
+ print STDERR '-'x72, "\n";
return undef;
}
}
$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);
}
close $un or croak $!;
- delete $rp->{'svn:date'}; # this is the only revprop for r0
- return undef if ($ed->{nr} == 0 && scalar @$untracked == 0 &&
- scalar keys %$rp == 0);
-
$log_entry{date} = parse_svn_date($log_entry{date});
$log_entry{author} = check_author($log_entry{author});
$log_entry{log} .= "\n";
}
sub fetch {
- my ($self, @parents) = @_;
+ my ($self, $min_rev, $max_rev, @parents) = @_;
my ($last_rev, $last_commit) = $self->last_rev_commit;
- my ($base, $head) = $self->parse_revision($last_rev);
+ my ($base, $head) = $self->get_fetch_range($min_rev, $max_rev);
return if ($base > $head);
- if (defined $last_commit) {
- $self->assert_index_clean($last_commit);
- }
- my $inc = 1000;
- my ($min, $max) = ($base, $head < $base + $inc ? $head : $base + $inc);
- my $err_handler = $SVN::Error::handler;
- $SVN::Error::handler = \&skip_unknown_revs;
- while (1) {
- my @revs;
- $self->ra->get_log([$self->{path}], $min, $max, 0, 1, 1, sub {
- my ($paths, $rev, $author, $date, $log) = @_;
- push @revs, [ $paths, $rev ] });
- foreach (@revs) {
- if (my $log_entry = $self->do_fetch(@$_)) {
- $self->do_git_commit($log_entry, @parents);
- }
- }
- last if $max >= $head;
- $min = $max + 1;
- $max += $inc;
- $max = $head if ($max > $head);
- }
- $SVN::Error::handler = $err_handler;
+ $self->ra->gs_fetch_loop_common($base, $head, $self);
}
sub set_tree_cb {
$log_entry->{author} = $author;
$self->do_git_commit($log_entry, "$rev=$tree");
} else {
- $self->fetch("$rev=$tree");
+ $self->{inject_parents} = { $rev => $tree };
+ $self->fetch(undef, undef);
}
}
unless ($self->{last_rev}) {
fatal("Must have an existing revision to commit\n");
}
- my $pool = SVN::Pool->new;
- my $ed = SVN::Git::Editor->new({ r => $self->{last_rev},
- ra => $self->ra->dup,
- svn_path => $self->ra->{svn_path}
- },
- $self->ra->get_commit_editor(
- $log_entry->{log}, sub {
- $self->set_tree_cb($log_entry,
- $tree, @_);
- }),
- $pool);
- my $mods = $ed->apply_diff($self->{last_commit}, $tree);
- if (@$mods == 0) {
+ my %ed_opts = ( r => $self->{last_rev},
+ log => $log_entry->{log},
+ ra => $self->ra,
+ tree_a => $self->{last_commit},
+ tree_b => $tree,
+ editor_cb => sub {
+ $self->set_tree_cb($log_entry, $tree, @_) },
+ svn_path => $self->{path} );
+ if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
print "No changes\nr$self->{last_rev} = $tree\n";
}
- $pool->clear;
}
-sub skip_unknown_revs {
- my ($err) = @_;
- my $errno = $err->apr_err();
- # Maybe the branch we're tracking didn't
- # exist when the repo started, so it's
- # not an error if it doesn't, just continue
- #
- # Wonderfully consistent library, eh?
- # 160013 - svn:// and file://
- # 175002 - http(s)://
- # 175007 - http(s):// (this repo required authorization, too...)
- # More codes may be discovered later...
- if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
- return;
+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";
}
- croak "Error from SVN, ($errno): ", $err->expanded_message,"\n";
+ command_close_pipe($rev_list, $ctx);
+ print "Done rebuilding $self->{db_path}\n";
}
# rev_db:
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 = shift;
- my $git_svn = shift;
- my $self = SVN::Delta::Editor->new(@_);
+ my ($class, $opts) = @_;
+ foreach (qw/svn_path r ra tree_a tree_b log editor_cb/) {
+ die "$_ required!\n" unless (defined $opts->{$_});
+ }
+
+ my $pool = SVN::Pool->new;
+ my $mods = generate_diff($opts->{tree_a}, $opts->{tree_b});
+ my $types = check_diff_paths($opts->{ra}, $opts->{svn_path},
+ $opts->{r}, $mods);
+
+ # $opts->{ra} functions should not be used after this:
+ my @ce = $opts->{ra}->get_commit_editor($opts->{log},
+ $opts->{editor_cb}, $pool);
+ my $self = SVN::Delta::Editor->new(@ce, $pool);
bless $self, $class;
- foreach (qw/svn_path r ra/) {
- die "$_ required!\n" unless (defined $git_svn->{$_});
- $self->{$_} = $git_svn->{$_};
+ foreach (qw/svn_path r tree_a tree_b/) {
+ $self->{$_} = $opts->{$_};
}
- $self->{pool} = SVN::Pool->new;
+ $self->{url} = $opts->{ra}->{url};
+ $self->{mods} = $mods;
+ $self->{types} = $types;
+ $self->{pool} = $pool;
$self->{bat} = { '' => $self->open_root($self->{r}, $self->{pool}) };
$self->{rm} = { };
- require Digest::MD5;
+ $self->{path_prefix} = length $self->{svn_path} ?
+ "$self->{svn_path}/" : '';
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";
+ } else {
+ push @diff_tree, '-C';
+ }
+ 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";
+ my $state = 'meta';
+ my @mods;
+ while (<$diff_fh>) {
+ chomp $_; # this gets rid of the trailing "\0"
+ if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s
+ $::sha1\s($::sha1)\s
+ ([MTCRAD])\d*$/xo) {
+ push @mods, { mode_a => $1, mode_b => $2,
+ sha1_b => $3, chg => $4 };
+ if ($4 =~ /^(?:C|R)$/) {
+ $state = 'file_a';
+ } else {
+ $state = 'file_b';
+ }
+ } elsif ($state eq 'file_a') {
+ my $x = $mods[$#mods] or croak "Empty array\n";
+ if ($x->{chg} !~ /^(?:C|R)$/) {
+ croak "Error parsing $_, $x->{chg}\n";
+ }
+ $x->{file_a} = $_;
+ $state = 'file_b';
+ } elsif ($state eq 'file_b') {
+ my $x = $mods[$#mods] or croak "Empty array\n";
+ if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) {
+ croak "Error parsing $_, $x->{chg}\n";
+ }
+ if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) {
+ croak "Error parsing $_, $x->{chg}\n";
+ }
+ $x->{file_b} = $_;
+ $state = 'meta';
+ } else {
+ croak "Error parsing $_\n";
+ }
+ }
+ command_close_pipe($diff_fh, $ctx);
+ \@mods;
+}
+
+sub check_diff_paths {
+ my ($ra, $pfx, $rev, $mods) = @_;
+ my %types;
+ $pfx .= '/' if length $pfx;
+
+ sub type_diff_paths {
+ my ($ra, $types, $path, $rev) = @_;
+ my @p = split m#/+#, $path;
+ my $c = shift @p;
+ unless (defined $types->{$c}) {
+ $types->{$c} = $ra->check_path($c, $rev);
+ }
+ while (@p) {
+ $c .= '/' . shift @p;
+ next if defined $types->{$c};
+ $types->{$c} = $ra->check_path($c, $rev);
+ }
+ }
+
+ foreach my $m (@$mods) {
+ foreach my $f (qw/file_a file_b/) {
+ next unless defined $m->{$f};
+ my ($dir) = ($m->{$f} =~ m#^(.*?)/?(?:[^/]+)$#);
+ if (length $pfx.$dir && ! defined $types{$dir}) {
+ type_diff_paths($ra, \%types, $pfx.$dir, $rev);
+ }
+ }
+ }
+ \%types;
+}
+
sub split_path {
return ($_[0] =~ m#^(.*?)/?([^/]+)$#);
}
sub repo_path {
- (defined $_[1] && length $_[1]) ? $_[1] : ''
+ my ($self, $path) = @_;
+ $self->{path_prefix}.(defined $path ? $path : '');
}
sub url_path {
my ($self, $path) = @_;
- $self->{ra}->{url} . '/' . $self->repo_path($path);
+ $self->{url} . '/' . $self->repo_path($path);
}
sub rmdirs {
- my ($self, $tree_b) = @_;
+ my ($self) = @_;
my $rm = $self->{rm};
delete $rm->{''}; # we never delete the url we're tracking
return unless %$rm;
delete $rm->{''}; # we never delete the url we're tracking
return unless %$rm;
- my ($fh, $ctx) = command_output_pipe(
- qw/ls-tree --name-only -r -z/, $tree_b);
+ my ($fh, $ctx) = command_output_pipe(qw/ls-tree --name-only -r -z/,
+ $self->{tree_b});
local $/ = "\0";
while (<$fh>) {
chomp;
sub open_or_add_dir {
my ($self, $full_path, $baton) = @_;
- my $t = $self->{ra}->check_path($full_path, $self->{r});
+ my $t = $self->{types}->{$full_path};
+ if (!defined $t) {
+ die "$full_path not known in r$self->{r} or we have a bug!\n";
+ }
if ($t == $SVN::Node::none) {
return $self->add_directory($full_path, $baton,
undef, -1, $self->{pool});
sub ensure_path {
my ($self, $path) = @_;
my $bat = $self->{bat};
- $path = $self->repo_path($path);
- return $bat->{''} unless (length $path);
- my @p = split m#/+#, $path;
+ my $repo_path = $self->repo_path($path);
+ return $bat->{''} unless (length $repo_path);
+ my @p = split m#/+#, $repo_path;
my $c = shift @p;
$bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
while (@p) {
sub abort_edit {
my ($self) = @_;
$self->SUPER::abort_edit($self->{pool});
+}
+
+sub DESTROY {
+ my $self = shift;
+ $self->SUPER::DESTROY(@_);
$self->{pool}->clear;
}
# this drives the editor
sub apply_diff {
- my ($self, $tree_a, $tree_b) = @_;
- my @diff_tree = qw(diff-tree -z -r);
- 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, $tree_a, $tree_b;
- my ($diff_fh, $ctx) = command_output_pipe(@diff_tree);
- my $nl = $/;
- local $/ = "\0";
- my $state = 'meta';
- my @mods;
- while (<$diff_fh>) {
- chomp $_; # this gets rid of the trailing "\0"
- if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s
- $::sha1\s($::sha1)\s
- ([MTCRAD])\d*$/xo) {
- push @mods, { mode_a => $1, mode_b => $2,
- sha1_b => $3, chg => $4 };
- if ($4 =~ /^(?:C|R)$/) {
- $state = 'file_a';
- } else {
- $state = 'file_b';
- }
- } elsif ($state eq 'file_a') {
- my $x = $mods[$#mods] or croak "Empty array\n";
- if ($x->{chg} !~ /^(?:C|R)$/) {
- croak "Error parsing $_, $x->{chg}\n";
- }
- $x->{file_a} = $_;
- $state = 'file_b';
- } elsif ($state eq 'file_b') {
- my $x = $mods[$#mods] or croak "Empty array\n";
- if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) {
- croak "Error parsing $_, $x->{chg}\n";
- }
- if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) {
- croak "Error parsing $_, $x->{chg}\n";
- }
- $x->{file_b} = $_;
- $state = 'meta';
- } else {
- croak "Error parsing $_\n";
- }
- }
- command_close_pipe($diff_fh, $ctx);
- $/ = $nl;
-
+ my ($self) = @_;
+ my $mods = $self->{mods};
my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 );
- foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @mods) {
+ foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
my $f = $m->{chg};
if (defined $o{$f}) {
$self->$f($m);
fatal("Invalid change type: $f\n");
}
}
- $self->rmdirs($tree_b) if $::_rmdir;
- if (@mods == 0) {
+ $self->rmdirs if $_rmdir;
+ if (@$mods == 0) {
$self->abort_edit;
} else {
$self->close_edit;
}
- \@mods;
+ return scalar @$mods;
}
package Git::SVN::Ra;
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
-}
-
-sub dup {
- my ($self) = @_;
- my $dup = SVN::Ra->new(pool => SVN::Pool->new,
- map { $_ => $self->{$_} } qw/config url
- auth auth_provider_callbacks repos_root svn_path/);
- bless $dup, ref $self;
+ # do not call the real DESTROY since we store ourselves in $RA
}
sub get_log {
my ($self, @args) = @_;
my $pool = SVN::Pool->new;
- $args[4]-- if $args[4] && ! $::_follow_parent;
splice(@args, 3, 1) if ($SVN::Core::VERSION le '1.2.0');
my $ret = $self->SUPER::get_log(@args, $pool);
$pool->clear;
}
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};
}
+sub gs_fetch_loop_common {
+ my ($self, $base, $head, @gs) = @_;
+ my $inc = 1000;
+ my ($min, $max) = ($base, $head < $base + $inc ? $head : $base + $inc);
+ foreach my $gs (@gs) {
+ if (my $last_commit = $gs->last_commit) {
+ $gs->assert_index_clean($last_commit);
+ }
+ }
+ while (1) {
+ 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);
+ }
+ }
+ }
+ last if $max >= $head;
+ $min = $max + 1;
+ $max += $inc;
+ $max = $head if ($max > $head);
+ }
+}
+
sub minimize_url {
my ($self) = @_;
return $self->{url} if ($self->{url} eq $self->{repos_root});
$can_do_switch;
}
+sub skip_unknown_revs {
+ my ($err) = @_;
+ my $errno = $err->apr_err();
+ # Maybe the branch we're tracking didn't
+ # exist when the repo started, so it's
+ # not an error if it doesn't, just continue
+ #
+ # Wonderfully consistent library, eh?
+ # 160013 - svn:// and file://
+ # 175002 - http(s)://
+ # 175007 - http(s):// (this repo required authorization, too...)
+ # More codes may be discovered later...
+ if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
+ return;
+ }
+ die "Error from SVN, ($errno): ", $err->expanded_message,"\n";
+}
+
+# svn_log_changed_path_t objects passed to get_log are likely to be
+# overwritten even if only the refs are copied to an external variable,
+# so we should dup the structures in their entirety. Using an externally
+# passed pool (instead of our temporary and quickly cleared pool in
+# Git::SVN::Ra) does not help matters at all...
+sub dup_changed_paths {
+ my ($paths) = @_;
+ return undef unless $paths;
+ my %ret;
+ foreach my $p (keys %$paths) {
+ my $i = $paths->{$p};
+ my %s = map { $_ => $i->$_ }
+ qw/copyfrom_path copyfrom_rev action/;
+ $ret{$p} = \%s;
+ }
+ \%ret;
+}
+
package Git::SVN::Log;
use strict;
use warnings;
exec $pager or ::fatal "Can't run pager: $! ($pager)\n";
}
+sub tz_to_s_offset {
+ my ($tz) = @_;
+ $tz =~ s/(\d\d)$//;
+ return ($1 * 60) + ($tz * 3600);
+}
+
sub get_author_info {
my ($dest, $author, $t, $tz) = @_;
$author =~ s/(?:^\s*|\s*$)//g;
$dest->{a} = $au;
# Date::Parse isn't in the standard Perl distro :(
if ($tz =~ s/^\+//) {
- $t += ::tz_to_s_offset($tz);
+ $t += tz_to_s_offset($tz);
} elsif ($tz =~ s/^\-//) {
- $t -= ::tz_to_s_offset($tz);
+ $t -= tz_to_s_offset($tz);
}
$dest->{t_utc} = $t;
}
author => 'committer name'
};
+
+# this is generated by generate_diff();
@mods = array of diff-index line hashes, each element represents one line
of diff-index output