git-svnimport.perlon commit Handle a revision that only creates a new tag correctly. (a16db4f)
   1#!/usr/bin/perl -w
   2
   3# This tool is copyright (c) 2005, Matthias Urlichs.
   4# It is released under the Gnu Public License, version 2.
   5#
   6# The basic idea is to pull and analyze SVN changes.
   7#
   8# Checking out the files is done by a single long-running SVN connection.
   9#
  10# The head revision is on branch "origin" by default.
  11# You can change that with the '-o' option.
  12
  13require 5.008; # for shell-safe open("-|",LIST)
  14use strict;
  15use warnings;
  16use Getopt::Std;
  17use File::Spec;
  18use File::Temp qw(tempfile);
  19use File::Path qw(mkpath);
  20use File::Basename qw(basename dirname);
  21use Time::Local;
  22use IO::Pipe;
  23use POSIX qw(strftime dup2);
  24use IPC::Open2;
  25use SVN::Core;
  26use SVN::Ra;
  27
  28die "Need CVN:Core 1.2.1 or better" if $SVN::Core::VERSION lt "1.2.1";
  29
  30$SIG{'PIPE'}="IGNORE";
  31$ENV{'TZ'}="UTC";
  32
  33our($opt_h,$opt_o,$opt_v,$opt_u,$opt_C,$opt_i,$opt_m,$opt_M,$opt_t,$opt_T,$opt_b,$opt_s,$opt_l,$opt_d,$opt_D);
  34
  35sub usage() {
  36        print STDERR <<END;
  37Usage: ${\basename $0}     # fetch/update GIT from CVS
  38       [-o branch-for-HEAD] [-h] [-v] [-l max_num_changes]
  39       [-C GIT_repository] [-t tagname] [-T trunkname] [-b branchname]
  40       [-d|-D] [-i] [-u] [-s start_chg] [-m] [-M regex] [SVN_URL]
  41END
  42        exit(1);
  43}
  44
  45getopts("b:C:dDhil:mM:o:s:t:T:uv") or usage();
  46usage if $opt_h;
  47
  48my $tag_name = $opt_t || "tags";
  49my $trunk_name = $opt_T || "trunk";
  50my $branch_name = $opt_b || "branches";
  51
  52@ARGV == 1 or @ARGV == 2 or usage();
  53
  54$opt_o ||= "origin";
  55$opt_s ||= 1;
  56$opt_l = 100 unless defined $opt_l;
  57my $git_tree = $opt_C;
  58$git_tree ||= ".";
  59
  60my $svn_url = $ARGV[0];
  61my $svn_dir = $ARGV[1];
  62
  63our @mergerx = ();
  64if ($opt_m) {
  65        @mergerx = ( qr/\W(?:from|of|merge|merging|merged) (\w+)/i );
  66}
  67if ($opt_M) {
  68        push (@mergerx, qr/$opt_M/);
  69}
  70
  71select(STDERR); $|=1; select(STDOUT);
  72
  73
  74package SVNconn;
  75# Basic SVN connection.
  76# We're only interested in connecting and downloading, so ...
  77
  78use File::Spec;
  79use File::Temp qw(tempfile);
  80use POSIX qw(strftime dup2);
  81
  82sub new {
  83        my($what,$repo) = @_;
  84        $what=ref($what) if ref($what);
  85
  86        my $self = {};
  87        $self->{'buffer'} = "";
  88        bless($self,$what);
  89
  90        $repo =~ s#/+$##;
  91        $self->{'fullrep'} = $repo;
  92        $self->conn();
  93
  94        return $self;
  95}
  96
  97sub conn {
  98        my $self = shift;
  99        my $repo = $self->{'fullrep'};
 100        my $s = SVN::Ra->new($repo);
 101
 102        die "SVN connection to $repo: $!\n" unless defined $s;
 103        $self->{'svn'} = $s;
 104        $self->{'repo'} = $repo;
 105        $self->{'maxrev'} = $s->get_latest_revnum();
 106}
 107
 108sub file {
 109        my($self,$path,$rev) = @_;
 110
 111        my ($fh, $name) = tempfile('gitsvn.XXXXXX',
 112                    DIR => File::Spec->tmpdir(), UNLINK => 1);
 113
 114        print "... $rev $path ...\n" if $opt_v;
 115        my $pool = SVN::Pool->new();
 116        eval { $self->{'svn'}->get_file($path,$rev,$fh,$pool); };
 117        $pool->clear;
 118        if($@) {
 119                return undef if $@ =~ /Attempted to get checksum/;
 120                die $@;
 121        }
 122        close ($fh);
 123
 124        return $name;
 125}
 126
 127package main;
 128use URI;
 129
 130my $svn = $svn_url;
 131$svn .= "/$svn_dir" if defined $svn_dir;
 132$svn = SVNconn->new($svn);
 133
 134my $lwp_ua;
 135if($opt_d or $opt_D) {
 136        $svn_url = URI->new($svn_url)->canonical;
 137        if($opt_D) {
 138                $svn_dir =~ s#/*$#/#;
 139        } else {
 140                $svn_dir = "";
 141        }
 142        if ($svn_url->scheme eq "http") {
 143                use LWP::UserAgent;
 144                $lwp_ua = LWP::UserAgent->new(keep_alive => 1, requests_redirectable => []);
 145        } else {
 146                print STDERR "Warning: not HTTP; turning off direct file access\n";
 147                $opt_d=0;
 148        }
 149}
 150
 151sub pdate($) {
 152        my($d) = @_;
 153        $d =~ m#(\d\d\d\d)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)#
 154                or die "Unparseable date: $d\n";
 155        my $y=$1; $y-=1900 if $y>1900;
 156        return timegm($6||0,$5,$4,$3,$2-1,$y);
 157}
 158
 159sub getwd() {
 160        my $pwd = `pwd`;
 161        chomp $pwd;
 162        return $pwd;
 163}
 164
 165
 166sub get_headref($$) {
 167    my $name    = shift;
 168    my $git_dir = shift;
 169    my $sha;
 170
 171    if (open(C,"$git_dir/refs/heads/$name")) {
 172        chomp($sha = <C>);
 173        close(C);
 174        length($sha) == 40
 175            or die "Cannot get head id for $name ($sha): $!\n";
 176    }
 177    return $sha;
 178}
 179
 180
 181-d $git_tree
 182        or mkdir($git_tree,0777)
 183        or die "Could not create $git_tree: $!";
 184chdir($git_tree);
 185
 186my $orig_branch = "";
 187my $forward_master = 0;
 188my %branches;
 189
 190my $git_dir = $ENV{"GIT_DIR"} || ".git";
 191$git_dir = getwd()."/".$git_dir unless $git_dir =~ m#^/#;
 192$ENV{"GIT_DIR"} = $git_dir;
 193my $orig_git_index;
 194$orig_git_index = $ENV{GIT_INDEX_FILE} if exists $ENV{GIT_INDEX_FILE};
 195my ($git_ih, $git_index) = tempfile('gitXXXXXX', SUFFIX => '.idx',
 196                                    DIR => File::Spec->tmpdir());
 197close ($git_ih);
 198$ENV{GIT_INDEX_FILE} = $git_index;
 199my $maxnum = 0;
 200my $last_rev = "";
 201my $last_branch;
 202my $current_rev = $opt_s-1;
 203unless(-d $git_dir) {
 204        system("git-init-db");
 205        die "Cannot init the GIT db at $git_tree: $?\n" if $?;
 206        system("git-read-tree");
 207        die "Cannot init an empty tree: $?\n" if $?;
 208
 209        $last_branch = $opt_o;
 210        $orig_branch = "";
 211} else {
 212        -f "$git_dir/refs/heads/$opt_o"
 213                or die "Branch '$opt_o' does not exist.\n".
 214                       "Either use the correct '-o branch' option,\n".
 215                       "or import to a new repository.\n";
 216
 217        -f "$git_dir/svn2git"
 218                or die "'$git_dir/svn2git' does not exist.\n".
 219                       "You need that file for incremental imports.\n";
 220        $last_branch = basename(readlink("$git_dir/HEAD"));
 221        unless($last_branch) {
 222                warn "Cannot read the last branch name: $! -- assuming 'master'\n";
 223                $last_branch = "master";
 224        }
 225        $orig_branch = $last_branch;
 226        $last_rev = get_headref($orig_branch, $git_dir);
 227        if (-f "$git_dir/SVN2GIT_HEAD") {
 228                die <<EOM;
 229SVN2GIT_HEAD exists.
 230Make sure your working directory corresponds to HEAD and remove SVN2GIT_HEAD.
 231You may need to run
 232
 233    git-read-tree -m -u SVN2GIT_HEAD HEAD
 234EOM
 235        }
 236        system('cp', "$git_dir/HEAD", "$git_dir/SVN2GIT_HEAD");
 237
 238        $forward_master =
 239            $opt_o ne 'master' && -f "$git_dir/refs/heads/master" &&
 240            system('cmp', '-s', "$git_dir/refs/heads/master",
 241                                "$git_dir/refs/heads/$opt_o") == 0;
 242
 243        # populate index
 244        system('git-read-tree', $last_rev);
 245        die "read-tree failed: $?\n" if $?;
 246
 247        # Get the last import timestamps
 248        open my $B,"<", "$git_dir/svn2git";
 249        while(<$B>) {
 250                chomp;
 251                my($num,$branch,$ref) = split;
 252                $branches{$branch}{$num} = $ref;
 253                $branches{$branch}{"LAST"} = $ref;
 254                $current_rev = $num if $current_rev < $num;
 255        }
 256        close($B);
 257}
 258-d $git_dir
 259        or die "Could not create git subdir ($git_dir).\n";
 260
 261open BRANCHES,">>", "$git_dir/svn2git";
 262
 263sub node_kind($$$) {
 264        my ($branch, $path, $revision) = @_;
 265        my $pool=SVN::Pool->new;
 266        my $kind = $svn->{'svn'}->check_path(revert_split_path($branch,$path),$revision,$pool);
 267        $pool->clear;
 268        return $kind;
 269}
 270
 271sub revert_split_path($$) {
 272        my($branch,$path) = @_;
 273
 274        my $svnpath;
 275        $path = "" if $path eq "/"; # this should not happen, but ...
 276        if($branch eq "/") {
 277                $svnpath = "$trunk_name/$path";
 278        } elsif($branch =~ m#^/#) {
 279                $svnpath = "$tag_name$branch/$path";
 280        } else {
 281                $svnpath = "$branch_name/$branch/$path";
 282        }
 283
 284        return $svnpath
 285}
 286
 287sub get_file($$$) {
 288        my($rev,$branch,$path) = @_;
 289
 290        my $svnpath = revert_split_path($branch,$path);
 291
 292        # now get it
 293        my $name;
 294        if($opt_d) {
 295                my($req,$res);
 296
 297                # /svn/!svn/bc/2/django/trunk/django-docs/build.py
 298                my $url=$svn_url->clone();
 299                $url->path($url->path."/!svn/bc/$rev/$svn_dir$svnpath");
 300                print "... $path...\n" if $opt_v;
 301                $req = HTTP::Request->new(GET => $url);
 302                $res = $lwp_ua->request($req);
 303                if ($res->is_success) {
 304                        my $fh;
 305                        ($fh, $name) = tempfile('gitsvn.XXXXXX',
 306                        DIR => File::Spec->tmpdir(), UNLINK => 1);
 307                        print $fh $res->content;
 308                        close($fh) or die "Could not write $name: $!\n";
 309                } else {
 310                        return undef if $res->code == 301; # directory?
 311                        die $res->status_line." at $url\n";
 312                }
 313        } else {
 314                $name = $svn->file("/$svnpath",$rev);
 315                return undef unless defined $name;
 316        }
 317
 318        open my $F, '-|', "git-hash-object", "-w", $name
 319                or die "Cannot create object: $!\n";
 320        my $sha = <$F>;
 321        chomp $sha;
 322        close $F;
 323        unlink $name;
 324        my $mode = "0644"; # SV does not seem to store any file modes
 325        return [$mode, $sha, $path];
 326}
 327
 328sub split_path($$) {
 329        my($rev,$path) = @_;
 330        my $branch;
 331
 332        if($path =~ s#^/\Q$tag_name\E/([^/]+)/?##) {
 333                $branch = "/$1";
 334        } elsif($path =~ s#^/\Q$trunk_name\E/?##) {
 335                $branch = "/";
 336        } elsif($path =~ s#^/\Q$branch_name\E/([^/]+)/?##) {
 337                $branch = $1;
 338        } else {
 339                my %no_error = (
 340                        "/" => 1,
 341                        "/$tag_name" => 1,
 342                        "/$branch_name" => 1
 343                );
 344                print STDERR "$rev: Unrecognized path: $path\n" unless (defined $no_error{$path});
 345                return ()
 346        }
 347        $path = "/" if $path eq "";
 348        return ($branch,$path);
 349}
 350
 351sub branch_rev($$) {
 352
 353        my ($srcbranch,$uptorev) = @_;
 354
 355        my $bbranches = $branches{$srcbranch};
 356        my @revs = reverse sort { ($a eq 'LAST' ? 0 : $a) <=> ($b eq 'LAST' ? 0 : $b) } keys %$bbranches;
 357        my $therev;
 358        foreach my $arev(@revs) {
 359                next if  ($arev eq 'LAST');
 360                if ($arev <= $uptorev) {
 361                        $therev = $arev;
 362                        last;
 363                }
 364        }
 365        return $therev;
 366}
 367
 368sub copy_path($$$$$$$$) {
 369        # Somebody copied a whole subdirectory.
 370        # We need to find the index entries from the old version which the
 371        # SVN log entry points to, and add them to the new place.
 372
 373        my($newrev,$newbranch,$path,$oldpath,$rev,$node_kind,$new,$parents) = @_;
 374
 375        my($srcbranch,$srcpath) = split_path($rev,$oldpath);
 376        my $therev = branch_rev($srcbranch, $rev);
 377        my $gitrev = $branches{$srcbranch}{$therev};
 378        unless($gitrev) {
 379                print STDERR "$newrev:$newbranch: could not find $oldpath \@ $rev\n";
 380                return;
 381        }
 382        if ($srcbranch ne $newbranch) {
 383                push(@$parents, $branches{$srcbranch}{'LAST'});
 384        }
 385        print "$newrev:$newbranch:$path: copying from $srcbranch:$srcpath @ $rev\n" if $opt_v;
 386        if ($node_kind eq $SVN::Node::dir) {
 387                        $srcpath =~ s#/*$#/#;
 388        }
 389        
 390        open my $f,"-|","git-ls-tree","-r","-z",$gitrev,$srcpath;
 391        local $/ = "\0";
 392        while(<$f>) {
 393                chomp;
 394                my($m,$p) = split(/\t/,$_,2);
 395                my($mode,$type,$sha1) = split(/ /,$m);
 396                next if $type ne "blob";
 397                if ($node_kind eq $SVN::Node::dir) {
 398                        $p = $path . substr($p,length($srcpath)-1);
 399                } else {
 400                        $p = $path;
 401                }
 402                push(@$new,[$mode,$sha1,$p]);   
 403        }
 404        close($f) or
 405                print STDERR "$newrev:$newbranch: could not list files in $oldpath \@ $rev\n";
 406}
 407
 408sub commit {
 409        my($branch, $changed_paths, $revision, $author, $date, $message) = @_;
 410        my($author_name,$author_email,$dest);
 411        my(@old,@new,@parents);
 412
 413        if (not defined $author) {
 414                $author_name = $author_email = "unknown";
 415        } elsif ($author =~ /^(.*?)\s+<(.*)>$/) {
 416                ($author_name, $author_email) = ($1, $2);
 417        } else {
 418                $author =~ s/^<(.*)>$/$1/;
 419                $author_name = $author_email = $author;
 420        }
 421        $date = pdate($date);
 422
 423        my $tag;
 424        my $parent;
 425        if($branch eq "/") { # trunk
 426                $parent = $opt_o;
 427        } elsif($branch =~ m#^/(.+)#) { # tag
 428                $tag = 1;
 429                $parent = $1;
 430        } else { # "normal" branch
 431                # nothing to do
 432                $parent = $branch;
 433        }
 434        $dest = $parent;
 435
 436        my $prev = $changed_paths->{"/"};
 437        if($prev and $prev->[0] eq "A") {
 438                delete $changed_paths->{"/"};
 439                my $oldpath = $prev->[1];
 440                my $rev;
 441                if(defined $oldpath) {
 442                        my $p;
 443                        ($parent,$p) = split_path($revision,$oldpath);
 444                        if($parent eq "/") {
 445                                $parent = $opt_o;
 446                        } else {
 447                                $parent =~ s#^/##; # if it's a tag
 448                        }
 449                } else {
 450                        $parent = undef;
 451                }
 452        }
 453
 454        my $rev;
 455        if($revision > $opt_s and defined $parent) {
 456                open(H,"git-rev-parse --verify $parent |");
 457                $rev = <H>;
 458                close(H) or do {
 459                        print STDERR "$revision: cannot find commit '$parent'!\n";
 460                        return;
 461                };
 462                chop $rev;
 463                if(length($rev) != 40) {
 464                        print STDERR "$revision: cannot find commit '$parent'!\n";
 465                        return;
 466                }
 467                $rev = $branches{($parent eq $opt_o) ? "/" : $parent}{"LAST"};
 468                if($revision != $opt_s and not $rev) {
 469                        print STDERR "$revision: do not know ancestor for '$parent'!\n";
 470                        return;
 471                }
 472        } else {
 473                $rev = undef;
 474        }
 475
 476#       if($prev and $prev->[0] eq "A") {
 477#               if(not $tag) {
 478#                       unless(open(H,"> $git_dir/refs/heads/$branch")) {
 479#                               print STDERR "$revision: Could not create branch $branch: $!\n";
 480#                               $state=11;
 481#                               next;
 482#                       }
 483#                       print H "$rev\n"
 484#                               or die "Could not write branch $branch: $!";
 485#                       close(H)
 486#                               or die "Could not write branch $branch: $!";
 487#               }
 488#       }
 489        if(not defined $rev) {
 490                unlink($git_index);
 491        } elsif ($rev ne $last_rev) {
 492                print "Switching from $last_rev to $rev ($branch)\n" if $opt_v;
 493                system("git-read-tree", $rev);
 494                die "read-tree failed for $rev: $?\n" if $?;
 495                $last_rev = $rev;
 496        }
 497
 498        push (@parents, $rev) if defined $rev;
 499
 500        my $cid;
 501        if($tag and not %$changed_paths) {
 502                $cid = $rev;
 503        } else {
 504                my @paths = sort keys %$changed_paths;
 505                foreach my $path(@paths) {
 506                        my $action = $changed_paths->{$path};
 507
 508                        if ($action->[0] eq "R") {
 509                                # refer to a file/tree in an earlier commit
 510                                push(@old,$path); # remove any old stuff
 511                        }
 512                        if(($action->[0] eq "A") || ($action->[0] eq "R")) {
 513                                my $node_kind = node_kind($branch,$path,$revision);
 514                                if($action->[1]) {
 515                                        copy_path($revision,$branch,$path,$action->[1],$action->[2],$node_kind,\@new,\@parents);
 516                                } elsif ($node_kind eq $SVN::Node::file) {
 517                                        my $f = get_file($revision,$branch,$path);
 518                                        if ($f) {
 519                                                push(@new,$f) if $f;
 520                                        } else {
 521                                                my $opath = $action->[3];
 522                                                print STDERR "$revision: $branch: could not fetch '$opath'\n";
 523                                        }
 524                                }
 525                        } elsif ($action->[0] eq "D") {
 526                                push(@old,$path);
 527                        } elsif ($action->[0] eq "M") {
 528                                my $node_kind = node_kind($branch,$path,$revision);
 529                                if ($node_kind eq $SVN::Node::file) {
 530                                        my $f = get_file($revision,$branch,$path);
 531                                        push(@new,$f) if $f;
 532                                }
 533                        } else {
 534                                die "$revision: unknown action '".$action->[0]."' for $path\n";
 535                        }
 536                }
 537
 538                if(@old) {
 539                        open my $F, "-|", "git-ls-files", "-z", @old or die $!;
 540                        @old = ();
 541                        local $/ = "\0";
 542                        while(<$F>) {
 543                                chomp;
 544                                push(@old,$_);
 545                        }
 546                        close($F);
 547
 548                        while(@old) {
 549                                my @o2;
 550                                if(@old > 55) {
 551                                        @o2 = splice(@old,0,50);
 552                                } else {
 553                                        @o2 = @old;
 554                                        @old = ();
 555                                }
 556                                system("git-update-index","--force-remove","--",@o2);
 557                                die "Cannot remove files: $?\n" if $?;
 558                        }
 559                }
 560                while(@new) {
 561                        my @n2;
 562                        if(@new > 12) {
 563                                @n2 = splice(@new,0,10);
 564                        } else {
 565                                @n2 = @new;
 566                                @new = ();
 567                        }
 568                        system("git-update-index","--add",
 569                                (map { ('--cacheinfo', @$_) } @n2));
 570                        die "Cannot add files: $?\n" if $?;
 571                }
 572
 573                my $pid = open(C,"-|");
 574                die "Cannot fork: $!" unless defined $pid;
 575                unless($pid) {
 576                        exec("git-write-tree");
 577                        die "Cannot exec git-write-tree: $!\n";
 578                }
 579                chomp(my $tree = <C>);
 580                length($tree) == 40
 581                        or die "Cannot get tree id ($tree): $!\n";
 582                close(C)
 583                        or die "Error running git-write-tree: $?\n";
 584                print "Tree ID $tree\n" if $opt_v;
 585
 586                my $pr = IO::Pipe->new() or die "Cannot open pipe: $!\n";
 587                my $pw = IO::Pipe->new() or die "Cannot open pipe: $!\n";
 588                $pid = fork();
 589                die "Fork: $!\n" unless defined $pid;
 590                unless($pid) {
 591                        $pr->writer();
 592                        $pw->reader();
 593                        open(OUT,">&STDOUT");
 594                        dup2($pw->fileno(),0);
 595                        dup2($pr->fileno(),1);
 596                        $pr->close();
 597                        $pw->close();
 598
 599                        my @par = ();
 600
 601                        # loose detection of merges
 602                        # based on the commit msg
 603                        foreach my $rx (@mergerx) {
 604                                if ($message =~ $rx) {
 605                                        my $mparent = $1;
 606                                        if ($mparent eq 'HEAD') { $mparent = $opt_o };
 607                                        if ( -e "$git_dir/refs/heads/$mparent") {
 608                                                $mparent = get_headref($mparent, $git_dir);
 609                                                push (@parents, $mparent);
 610                                                print OUT "Merge parent branch: $mparent\n" if $opt_v;
 611                                        }
 612                                }
 613                        }
 614                        my %seen_parents = ();
 615                        my @unique_parents = grep { ! $seen_parents{$_} ++ } @parents;
 616                        foreach my $bparent (@unique_parents) {
 617                                push @par, '-p', $bparent;
 618                                print OUT "Merge parent branch: $bparent\n" if $opt_v;
 619                        }
 620
 621                        exec("env",
 622                                "GIT_AUTHOR_NAME=$author_name",
 623                                "GIT_AUTHOR_EMAIL=$author_email",
 624                                "GIT_AUTHOR_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
 625                                "GIT_COMMITTER_NAME=$author_name",
 626                                "GIT_COMMITTER_EMAIL=$author_email",
 627                                "GIT_COMMITTER_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
 628                                "git-commit-tree", $tree,@par);
 629                        die "Cannot exec git-commit-tree: $!\n";
 630                }
 631                $pw->writer();
 632                $pr->reader();
 633
 634                $message =~ s/[\s\n]+\z//;
 635
 636                print $pw "$message\n"
 637                        or die "Error writing to git-commit-tree: $!\n";
 638                $pw->close();
 639
 640                print "Committed change $revision:$branch ".strftime("%Y-%m-%d %H:%M:%S",gmtime($date)).")\n" if $opt_v;
 641                chomp($cid = <$pr>);
 642                length($cid) == 40
 643                        or die "Cannot get commit id ($cid): $!\n";
 644                print "Commit ID $cid\n" if $opt_v;
 645                $pr->close();
 646
 647                waitpid($pid,0);
 648                die "Error running git-commit-tree: $?\n" if $?;
 649        }
 650
 651        if (not defined $cid) {
 652                $cid = $branches{"/"}{"LAST"};
 653        }
 654
 655        if(not defined $dest) {
 656                print "... no known parent\n" if $opt_v;
 657        } elsif(not $tag) {
 658                print "Writing to refs/heads/$dest\n" if $opt_v;
 659                open(C,">$git_dir/refs/heads/$dest") and
 660                print C ("$cid\n") and
 661                close(C)
 662                        or die "Cannot write branch $dest for update: $!\n";
 663        }
 664
 665        if($tag) {
 666                my($in, $out) = ('','');
 667                $last_rev = "-" if %$changed_paths;
 668                # the tag was 'complex', i.e. did not refer to a "real" revision
 669
 670                $dest =~ tr/_/\./ if $opt_u;
 671                $branch = $dest;
 672
 673                my $pid = open2($in, $out, 'git-mktag');
 674                print $out ("object $cid\n".
 675                    "type commit\n".
 676                    "tag $dest\n".
 677                    "tagger $author_name <$author_email>\n") and
 678                close($out)
 679                    or die "Cannot create tag object $dest: $!\n";
 680
 681                my $tagobj = <$in>;
 682                chomp $tagobj;
 683
 684                if ( !close($in) or waitpid($pid, 0) != $pid or
 685                                $? != 0 or $tagobj !~ /^[0123456789abcdef]{40}$/ ) {
 686                        die "Cannot create tag object $dest: $!\n";
 687                }
 688
 689                open(C,">$git_dir/refs/tags/$dest") and
 690                print C ("$tagobj\n") and
 691                close(C)
 692                        or die "Cannot create tag $branch: $!\n";
 693
 694                print "Created tag '$dest' on '$branch'\n" if $opt_v;
 695        }
 696        $branches{$branch}{"LAST"} = $cid;
 697        $branches{$branch}{$revision} = $cid;
 698        $last_rev = $cid;
 699        print BRANCHES "$revision $branch $cid\n";
 700        print "DONE: $revision $dest $cid\n" if $opt_v;
 701}
 702
 703my ($changed_paths, $revision, $author, $date, $message, $pool) = @_;
 704sub _commit_all {
 705        ($changed_paths, $revision, $author, $date, $message, $pool) = @_;
 706        my %p;
 707        while(my($path,$action) = each %$changed_paths) {
 708                $p{$path} = [ $action->action,$action->copyfrom_path, $action->copyfrom_rev, $path ];
 709        }
 710        $changed_paths = \%p;
 711}
 712
 713sub commit_all {
 714        my %done;
 715        my @col;
 716        my $pref;
 717        my $branch;
 718
 719        while(my($path,$action) = each %$changed_paths) {
 720                ($branch,$path) = split_path($revision,$path);
 721                next if not defined $branch;
 722                $done{$branch}{$path} = $action;
 723        }
 724        while(($branch,$changed_paths) = each %done) {
 725                commit($branch, $changed_paths, $revision, $author, $date, $message);
 726        }
 727}
 728
 729while(++$current_rev <= $svn->{'maxrev'}) {
 730        my $pool=SVN::Pool->new;
 731        $svn->{'svn'}->get_log("/",$current_rev,$current_rev,1,1,1,\&_commit_all,$pool);
 732        $pool->clear;
 733        commit_all();
 734        if($opt_l and not --$opt_l) {
 735                print STDERR "Stopping, because there is a memory leak (in the SVN library).\n";
 736                print STDERR "Please repeat this command; it will continue safely\n";
 737                last;
 738        }
 739}
 740
 741
 742unlink($git_index);
 743
 744if (defined $orig_git_index) {
 745        $ENV{GIT_INDEX_FILE} = $orig_git_index;
 746} else {
 747        delete $ENV{GIT_INDEX_FILE};
 748}
 749
 750# Now switch back to the branch we were in before all of this happened
 751if($orig_branch) {
 752        print "DONE\n" if $opt_v and (not defined $opt_l or $opt_l > 0);
 753        system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master")
 754                if $forward_master;
 755        unless ($opt_i) {
 756                system('git-read-tree', '-m', '-u', 'SVN2GIT_HEAD', 'HEAD');
 757                die "read-tree failed: $?\n" if $?;
 758        }
 759} else {
 760        $orig_branch = "master";
 761        print "DONE; creating $orig_branch branch\n" if $opt_v and (not defined $opt_l or $opt_l > 0);
 762        system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master")
 763                unless -f "$git_dir/refs/heads/master";
 764        unlink("$git_dir/HEAD");
 765        symlink("refs/heads/$orig_branch","$git_dir/HEAD");
 766        unless ($opt_i) {
 767                system('git checkout');
 768                die "checkout failed: $?\n" if $?;
 769        }
 770}
 771unlink("$git_dir/SVN2GIT_HEAD");
 772close(BRANCHES);