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