git-svn: make (multi-)fetch safer but slower
[gitweb.git] / git-svn.perl
index e771bf5692d068d92d032b9745c886707b102a3c..efc55156639316259a246e50d8ae7a51439b8155 100755 (executable)
@@ -4,12 +4,8 @@
 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 }
@@ -60,19 +53,19 @@ BEGIN
 $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 );
@@ -83,10 +76,10 @@ BEGIN
                '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 = (
@@ -106,9 +99,6 @@ BEGIN
                        { '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,
@@ -155,18 +145,17 @@ BEGIN
 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 {
@@ -211,47 +200,6 @@ sub version {
        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');
@@ -349,25 +297,16 @@ sub cmd_dcommit {
                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 $mods = generate_diff("$d~1", $d);
-                       my $types = check_diff_paths($ra,
-                                                    $gs->{path},
-                                                    $last_rev,
-                                                    $mods);
                        my %ed_opts = ( r => $last_rev,
-                                       mods => $mods,
-                                       url => $ra->{url},
-                                       types => $types,
+                                       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} );
-                       my $ed = SVN::Git::Editor->new(\%ed_opts,
-                                        $ra->get_commit_editor($log,
-                                        sub { print "Committed r$_[0]\n";
-                                              $last_rev = $_[0]; }),
-                                        $pool);
-                       if (!$ed->apply_diff($mods, $d)) {
+                       if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
                                print "No changes\n$d~1 == $d\n";
                        }
                }
@@ -425,15 +364,11 @@ sub cmd_multi_init {
 }
 
 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);
        }
 }
 
@@ -474,19 +409,16 @@ sub cmd_commit_diff {
        } elsif ($r !~ /^\d+$/) {
                die "revision argument: $r not understood by git-svn\n";
        }
-       my $pool = SVN::Pool->new;
-       my $mods = generate_diff($ta, $tb);
-       my $types = check_diff_paths($ra, $svn_path, $r, $mods);
-       my %ed_opts = ( r => $r, url => $ra->{url}, svn_path => $svn_path,
-                       mods => $mods, types => $types );
-       my $ed = SVN::Git::Editor->new(\%ed_opts,
-                                      $ra->get_commit_editor($_message,
-                                        sub { print "Committed r$_[0]\n" }),
-                                      $pool);
-       if (!$ed->apply_diff($mods, $tb)) {
+       my %ed_opts = ( r => $r,
+                       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 #########################
@@ -691,119 +623,10 @@ sub cmt_metadata {
                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);
-}
-
-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$_l" if defined $_l;
-       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);
-                       }
-               }
-       }
-       use Data::Dumper;
-       warn Dumper \%types;
-       warn Dumper $mods;
-       \%types;
-}
-
 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;
@@ -819,6 +642,28 @@ BEGIN
                                        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/)) {
@@ -966,6 +811,9 @@ sub new {
        $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;
 }
 
@@ -973,7 +821,7 @@ sub new {
 
 sub ra {
        my ($self) = shift;
-       $self->{ra} ||= Git::SVN::Ra->new($self->{url});
+       Git::SVN::Ra->new($self->{url});
 }
 
 sub rel_path {
@@ -986,23 +834,13 @@ 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;
@@ -1102,16 +940,12 @@ sub assert_index_clean {
 }
 
 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')) {
@@ -1134,11 +968,17 @@ sub get_commit_parents {
 
 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";
@@ -1158,15 +998,17 @@ sub do_git_commit {
        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> });
@@ -1182,7 +1024,7 @@ sub do_git_commit {
 
        $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;
 }
 
@@ -1198,10 +1040,14 @@ sub revisions_eq {
 
 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;
 
@@ -1242,14 +1088,16 @@ sub find_parent_branch {
                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;
        }
@@ -1260,20 +1108,20 @@ sub find_parent_branch {
                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:
@@ -1310,8 +1158,7 @@ sub do_fetch {
                }
                $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);
@@ -1382,8 +1229,6 @@ sub make_log_entry {
        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;
@@ -1412,40 +1257,7 @@ sub fetch {
        my ($last_rev, $last_commit) = $self->last_rev_commit;
        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;
-       my $err;
-       $SVN::Error::handler = sub { ($err) = @_; skip_unknown_revs($err); } ;
-       while (1) {
-               my @revs;
-               $self->ra->get_log([$self->{path}], $min, $max, 0, 1, 1,
-                   sub {
-                       my ($paths, $rev) = @_;
-                       push @revs, [ dup_changed_paths($paths), $rev ];
-                       });
-               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) {
-                       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 {
@@ -1456,7 +1268,8 @@ sub set_tree_cb {
                $log_entry->{author} = $author;
                $self->do_git_commit($log_entry, "$rev=$tree");
        } else {
-               $self->fetch(undef, undef, "$rev=$tree");
+               $self->{inject_parents} = { $rev => $tree };
+               $self->fetch(undef, undef);
        }
 }
 
@@ -1466,60 +1279,49 @@ sub set_tree {
        unless ($self->{last_rev}) {
                fatal("Must have an existing revision to commit\n");
        }
-       my $pool = SVN::Pool->new;
-       my $mods = ::generate_diff($self->{last_commit}, $tree);
-       my $types = ::check_diff_paths($self->ra, $self->{path},
-                                    $self->{last_rev}, $mods);
-       my %ed_opts = ( r => $self->{last_rev}, url => $self->ra->{url},
-                       svn_path => $self->{path},
-                       mods => $mods, types => $types );
-       my $ed = SVN::Git::Editor->new(\%ed_opts,
-                                      $self->ra->get_commit_editor(
-                                        $log_entry->{log}, sub {
-                                          $self->set_tree_cb($log_entry,
-                                                             $tree, @_);
-                                      }),
-                                      $pool);
-       if (!$ed->apply_diff($mods, $tree)) {
+       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;
-       }
-       croak "Error from SVN, ($errno): ", $err->expanded_message,"\n";
-}
+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);
 
-# 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;
+               # 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";
        }
-       \%ret;
+       command_close_pipe($rev_list, $ctx);
+       print "Done rebuilding $self->{db_path}\n";
 }
 
 # rev_db:
@@ -1737,19 +1539,6 @@ sub _read_password {
 
 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.
@@ -1768,6 +1557,7 @@ package SVN::Git::Fetcher;
 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 {
@@ -1781,7 +1571,6 @@ sub new {
        $self->{absent_dir} = {};
        $self->{absent_file} = {};
        $self->{gii} = $git_svn->tmp_index_do(sub { Git::IndexInfo->new });
-       require Digest::MD5;
        $self;
 }
 
@@ -1801,7 +1590,10 @@ sub open_directory {
 
 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;
 }
 
@@ -1809,6 +1601,8 @@ sub delete_entry {
        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
@@ -1981,30 +1775,126 @@ sub close_edit {
 }
 
 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 mods url types r/) {
-               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} = { };
        $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";
+       } 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#^(.*?)/?([^/]+)$#);
 }
@@ -2020,7 +1910,7 @@ sub url_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;
@@ -2038,8 +1928,8 @@ sub rmdirs {
        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;
@@ -2221,12 +2111,18 @@ sub close_edit {
 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, $mods, $tree_b) = @_;
+       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) {
                my $f = $m->{chg};
@@ -2236,7 +2132,7 @@ sub apply_diff {
                        fatal("Invalid change type: $f\n");
                }
        }
-       $self->rmdirs($tree_b) if $::_rmdir;
+       $self->rmdirs if $_rmdir;
        if (@$mods == 0) {
                $self->abort_edit;
        } else {
@@ -2250,7 +2146,7 @@ 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
@@ -2270,7 +2166,7 @@ BEGIN
 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([
@@ -2296,11 +2192,11 @@ sub new {
        $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 {
@@ -2324,32 +2220,146 @@ sub uuid {
 }
 
 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});
@@ -2382,6 +2392,42 @@ sub can_do_switch {
        $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;
@@ -2499,6 +2545,12 @@ sub run_pager {
        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;
@@ -2515,9 +2567,9 @@ sub get_author_info {
        $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;
 }