Code

f31fcf84ed3150a1d53676cd1e01ba276b122cba
[git.git] / git-svnimport.perl
1 #!/usr/bin/perl -w
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.
13 use strict;
14 use warnings;
15 use Getopt::Std;
16 use File::Copy;
17 use File::Spec;
18 use File::Temp qw(tempfile);
19 use File::Path qw(mkpath);
20 use File::Basename qw(basename dirname);
21 use Time::Local;
22 use IO::Pipe;
23 use POSIX qw(strftime dup2);
24 use IPC::Open2;
25 use SVN::Core;
26 use SVN::Ra;
28 die "Need SVN:Core 1.2.1 or better" if $SVN::Core::VERSION lt "1.2.1";
30 $SIG{'PIPE'}="IGNORE";
31 $ENV{'TZ'}="UTC";
33 our($opt_h,$opt_o,$opt_v,$opt_u,$opt_C,$opt_i,$opt_m,$opt_M,$opt_t,$opt_T,
34     $opt_b,$opt_r,$opt_I,$opt_A,$opt_s,$opt_l,$opt_d,$opt_D,$opt_S,$opt_F,
35     $opt_P,$opt_R);
37 sub usage() {
38         print STDERR <<END;
39 Usage: ${\basename $0}     # fetch/update GIT from SVN
40        [-o branch-for-HEAD] [-h] [-v] [-l max_rev] [-R repack_each_revs]
41        [-C GIT_repository] [-t tagname] [-T trunkname] [-b branchname]
42        [-d|-D] [-i] [-u] [-r] [-I ignorefilename] [-s start_chg]
43        [-m] [-M regex] [-A author_file] [-S] [-F] [-P project_name] [SVN_URL]
44 END
45         exit(1);
46 }
48 getopts("A:b:C:dDFhiI:l:mM:o:rs:t:T:SP:R:uv") or usage();
49 usage if $opt_h;
51 my $tag_name = $opt_t || "tags";
52 my $trunk_name = $opt_T || "trunk";
53 my $branch_name = $opt_b || "branches";
54 my $project_name = $opt_P || "";
55 $project_name = "/" . $project_name if ($project_name);
56 my $repack_after = $opt_R || 1000;
58 @ARGV == 1 or @ARGV == 2 or usage();
60 $opt_o ||= "origin";
61 $opt_s ||= 1;
62 my $git_tree = $opt_C;
63 $git_tree ||= ".";
65 my $svn_url = $ARGV[0];
66 my $svn_dir = $ARGV[1];
68 our @mergerx = ();
69 if ($opt_m) {
70         my $branch_esc = quotemeta ($branch_name);
71         my $trunk_esc  = quotemeta ($trunk_name);
72         @mergerx =
73         (
74                 qr!\b(?:merg(?:ed?|ing))\b.*?\b((?:(?<=$branch_esc/)[\w\.\-]+)|(?:$trunk_esc))\b!i,
75                 qr!\b(?:from|of)\W+((?:(?<=$branch_esc/)[\w\.\-]+)|(?:$trunk_esc))\b!i,
76                 qr!\b(?:from|of)\W+(?:the )?([\w\.\-]+)[-\s]branch\b!i
77         );
78 }
79 if ($opt_M) {
80         unshift (@mergerx, qr/$opt_M/);
81 }
83 # Absolutize filename now, since we will have chdir'ed by the time we
84 # get around to opening it.
85 $opt_A = File::Spec->rel2abs($opt_A) if $opt_A;
87 our %users = ();
88 our $users_file = undef;
89 sub read_users($) {
90         $users_file = File::Spec->rel2abs(@_);
91         die "Cannot open $users_file\n" unless -f $users_file;
92         open(my $authors,$users_file);
93         while(<$authors>) {
94                 chomp;
95                 next unless /^(\S+?)\s*=\s*(.+?)\s*<(.+)>\s*$/;
96                 (my $user,my $name,my $email) = ($1,$2,$3);
97                 $users{$user} = [$name,$email];
98         }
99         close($authors);
102 select(STDERR); $|=1; select(STDOUT);
105 package SVNconn;
106 # Basic SVN connection.
107 # We're only interested in connecting and downloading, so ...
109 use File::Spec;
110 use File::Temp qw(tempfile);
111 use POSIX qw(strftime dup2);
112 use Fcntl qw(SEEK_SET);
114 sub new {
115         my($what,$repo) = @_;
116         $what=ref($what) if ref($what);
118         my $self = {};
119         $self->{'buffer'} = "";
120         bless($self,$what);
122         $repo =~ s#/+$##;
123         $self->{'fullrep'} = $repo;
124         $self->conn();
126         return $self;
129 sub conn {
130         my $self = shift;
131         my $repo = $self->{'fullrep'};
132         my $auth = SVN::Core::auth_open ([SVN::Client::get_simple_provider,
133                           SVN::Client::get_ssl_server_trust_file_provider,
134                           SVN::Client::get_username_provider]);
135         my $s = SVN::Ra->new(url => $repo, auth => $auth);
136         die "SVN connection to $repo: $!\n" unless defined $s;
137         $self->{'svn'} = $s;
138         $self->{'repo'} = $repo;
139         $self->{'maxrev'} = $s->get_latest_revnum();
142 sub file {
143         my($self,$path,$rev) = @_;
145         my ($fh, $name) = tempfile('gitsvn.XXXXXX',
146                     DIR => File::Spec->tmpdir(), UNLINK => 1);
148         print "... $rev $path ...\n" if $opt_v;
149         my (undef, $properties);
150         my $pool = SVN::Pool->new();
151         eval { (undef, $properties)
152                    = $self->{'svn'}->get_file($path,$rev,$fh,$pool); };
153         $pool->clear;
154         if($@) {
155                 return undef if $@ =~ /Attempted to get checksum/;
156                 die $@;
157         }
158         my $mode;
159         if (exists $properties->{'svn:executable'}) {
160                 $mode = '100755';
161         } elsif (exists $properties->{'svn:special'}) {
162                 my ($special_content, $filesize);
163                 $filesize = tell $fh;
164                 seek $fh, 0, SEEK_SET;
165                 read $fh, $special_content, $filesize;
166                 if ($special_content =~ s/^link //) {
167                         $mode = '120000';
168                         seek $fh, 0, SEEK_SET;
169                         truncate $fh, 0;
170                         print $fh $special_content;
171                 } else {
172                         die "unexpected svn:special file encountered";
173                 }
174         } else {
175                 $mode = '100644';
176         }
177         close ($fh);
179         return ($name, $mode);
182 sub ignore {
183         my($self,$path,$rev) = @_;
185         print "... $rev $path ...\n" if $opt_v;
186         my (undef,undef,$properties)
187             = $self->{'svn'}->get_dir($path,$rev,undef);
188         if (exists $properties->{'svn:ignore'}) {
189                 my ($fh, $name) = tempfile('gitsvn.XXXXXX',
190                                            DIR => File::Spec->tmpdir(),
191                                            UNLINK => 1);
192                 print $fh $properties->{'svn:ignore'};
193                 close($fh);
194                 return $name;
195         } else {
196                 return undef;
197         }
200 sub dir_list {
201         my($self,$path,$rev) = @_;
202         my ($dirents,undef,$properties)
203             = $self->{'svn'}->get_dir($path,$rev,undef);
204         return $dirents;
207 package main;
208 use URI;
210 our $svn = $svn_url;
211 $svn .= "/$svn_dir" if defined $svn_dir;
212 my $svn2 = SVNconn->new($svn);
213 $svn = SVNconn->new($svn);
215 my $lwp_ua;
216 if($opt_d or $opt_D) {
217         $svn_url = URI->new($svn_url)->canonical;
218         if($opt_D) {
219                 $svn_dir =~ s#/*$#/#;
220         } else {
221                 $svn_dir = "";
222         }
223         if ($svn_url->scheme eq "http") {
224                 use LWP::UserAgent;
225                 $lwp_ua = LWP::UserAgent->new(keep_alive => 1, requests_redirectable => []);
226         } else {
227                 print STDERR "Warning: not HTTP; turning off direct file access\n";
228                 $opt_d=0;
229         }
232 sub pdate($) {
233         my($d) = @_;
234         $d =~ m#(\d\d\d\d)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)#
235                 or die "Unparseable date: $d\n";
236         my $y=$1; $y-=1900 if $y>1900;
237         return timegm($6||0,$5,$4,$3,$2-1,$y);
240 sub getwd() {
241         my $pwd = `pwd`;
242         chomp $pwd;
243         return $pwd;
247 sub get_headref($$) {
248     my $name    = shift;
249     my $git_dir = shift;
250     my $sha;
252     if (open(C,"$git_dir/refs/heads/$name")) {
253         chomp($sha = <C>);
254         close(C);
255         length($sha) == 40
256             or die "Cannot get head id for $name ($sha): $!\n";
257     }
258     return $sha;
262 -d $git_tree
263         or mkdir($git_tree,0777)
264         or die "Could not create $git_tree: $!";
265 chdir($git_tree);
267 my $orig_branch = "";
268 my $forward_master = 0;
269 my %branches;
271 my $git_dir = $ENV{"GIT_DIR"} || ".git";
272 $git_dir = getwd()."/".$git_dir unless $git_dir =~ m#^/#;
273 $ENV{"GIT_DIR"} = $git_dir;
274 my $orig_git_index;
275 $orig_git_index = $ENV{GIT_INDEX_FILE} if exists $ENV{GIT_INDEX_FILE};
276 my ($git_ih, $git_index) = tempfile('gitXXXXXX', SUFFIX => '.idx',
277                                     DIR => File::Spec->tmpdir());
278 close ($git_ih);
279 $ENV{GIT_INDEX_FILE} = $git_index;
280 my $maxnum = 0;
281 my $last_rev = "";
282 my $last_branch;
283 my $current_rev = $opt_s || 1;
284 unless(-d $git_dir) {
285         system("git-init-db");
286         die "Cannot init the GIT db at $git_tree: $?\n" if $?;
287         system("git-read-tree");
288         die "Cannot init an empty tree: $?\n" if $?;
290         $last_branch = $opt_o;
291         $orig_branch = "";
292 } else {
293         -f "$git_dir/refs/heads/$opt_o"
294                 or die "Branch '$opt_o' does not exist.\n".
295                        "Either use the correct '-o branch' option,\n".
296                        "or import to a new repository.\n";
298         -f "$git_dir/svn2git"
299                 or die "'$git_dir/svn2git' does not exist.\n".
300                        "You need that file for incremental imports.\n";
301         open(F, "git-symbolic-ref HEAD |") or
302                 die "Cannot run git-symbolic-ref: $!\n";
303         chomp ($last_branch = <F>);
304         $last_branch = basename($last_branch);
305         close(F);
306         unless($last_branch) {
307                 warn "Cannot read the last branch name: $! -- assuming 'master'\n";
308                 $last_branch = "master";
309         }
310         $orig_branch = $last_branch;
311         $last_rev = get_headref($orig_branch, $git_dir);
312         if (-f "$git_dir/SVN2GIT_HEAD") {
313                 die <<EOM;
314 SVN2GIT_HEAD exists.
315 Make sure your working directory corresponds to HEAD and remove SVN2GIT_HEAD.
316 You may need to run
318     git-read-tree -m -u SVN2GIT_HEAD HEAD
319 EOM
320         }
321         system('cp', "$git_dir/HEAD", "$git_dir/SVN2GIT_HEAD");
323         $forward_master =
324             $opt_o ne 'master' && -f "$git_dir/refs/heads/master" &&
325             system('cmp', '-s', "$git_dir/refs/heads/master",
326                                 "$git_dir/refs/heads/$opt_o") == 0;
328         # populate index
329         system('git-read-tree', $last_rev);
330         die "read-tree failed: $?\n" if $?;
332         # Get the last import timestamps
333         open my $B,"<", "$git_dir/svn2git";
334         while(<$B>) {
335                 chomp;
336                 my($num,$branch,$ref) = split;
337                 $branches{$branch}{$num} = $ref;
338                 $branches{$branch}{"LAST"} = $ref;
339                 $current_rev = $num+1 if $current_rev <= $num;
340         }
341         close($B);
343 -d $git_dir
344         or die "Could not create git subdir ($git_dir).\n";
346 my $default_authors = "$git_dir/svn-authors";
347 if ($opt_A) {
348         read_users($opt_A);
349         copy($opt_A,$default_authors) or die "Copy failed: $!";
350 } else {
351         read_users($default_authors) if -f $default_authors;
354 open BRANCHES,">>", "$git_dir/svn2git";
356 sub node_kind($$) {
357         my ($svnpath, $revision) = @_;
358         my $pool=SVN::Pool->new;
359         my $kind = $svn->{'svn'}->check_path($svnpath,$revision,$pool);
360         $pool->clear;
361         return $kind;
364 sub get_file($$$) {
365         my($svnpath,$rev,$path) = @_;
367         # now get it
368         my ($name,$mode);
369         if($opt_d) {
370                 my($req,$res);
372                 # /svn/!svn/bc/2/django/trunk/django-docs/build.py
373                 my $url=$svn_url->clone();
374                 $url->path($url->path."/!svn/bc/$rev/$svn_dir$svnpath");
375                 print "... $path...\n" if $opt_v;
376                 $req = HTTP::Request->new(GET => $url);
377                 $res = $lwp_ua->request($req);
378                 if ($res->is_success) {
379                         my $fh;
380                         ($fh, $name) = tempfile('gitsvn.XXXXXX',
381                         DIR => File::Spec->tmpdir(), UNLINK => 1);
382                         print $fh $res->content;
383                         close($fh) or die "Could not write $name: $!\n";
384                 } else {
385                         return undef if $res->code == 301; # directory?
386                         die $res->status_line." at $url\n";
387                 }
388                 $mode = '0644'; # can't obtain mode via direct http request?
389         } else {
390                 ($name,$mode) = $svn->file("$svnpath",$rev);
391                 return undef unless defined $name;
392         }
394         my $pid = open(my $F, '-|');
395         die $! unless defined $pid;
396         if (!$pid) {
397             exec("git-hash-object", "-w", $name)
398                 or die "Cannot create object: $!\n";
399         }
400         my $sha = <$F>;
401         chomp $sha;
402         close $F;
403         unlink $name;
404         return [$mode, $sha, $path];
407 sub get_ignore($$$$$) {
408         my($new,$old,$rev,$path,$svnpath) = @_;
410         return unless $opt_I;
411         my $name = $svn->ignore("$svnpath",$rev);
412         if ($path eq '/') {
413                 $path = $opt_I;
414         } else {
415                 $path = File::Spec->catfile($path,$opt_I);
416         }
417         if (defined $name) {
418                 my $pid = open(my $F, '-|');
419                 die $! unless defined $pid;
420                 if (!$pid) {
421                         exec("git-hash-object", "-w", $name)
422                             or die "Cannot create object: $!\n";
423                 }
424                 my $sha = <$F>;
425                 chomp $sha;
426                 close $F;
427                 unlink $name;
428                 push(@$new,['0644',$sha,$path]);
429         } elsif (defined $old) {
430                 push(@$old,$path);
431         }
434 sub project_path($$)
436         my ($path, $project) = @_;
438         $path = "/".$path unless ($path =~ m#^\/#) ;
439         return $1 if ($path =~ m#^$project\/(.*)$#);
441         $path =~ s#\.#\\\.#g;
442         $path =~ s#\+#\\\+#g;
443         return "/" if ($project =~ m#^$path.*$#);
445         return undef;
448 sub split_path($$) {
449         my($rev,$path) = @_;
450         my $branch;
452         if($path =~ s#^/\Q$tag_name\E/([^/]+)/?##) {
453                 $branch = "/$1";
454         } elsif($path =~ s#^/\Q$trunk_name\E/?##) {
455                 $branch = "/";
456         } elsif($path =~ s#^/\Q$branch_name\E/([^/]+)/?##) {
457                 $branch = $1;
458         } else {
459                 my %no_error = (
460                         "/" => 1,
461                         "/$tag_name" => 1,
462                         "/$branch_name" => 1
463                 );
464                 print STDERR "$rev: Unrecognized path: $path\n" unless (defined $no_error{$path});
465                 return ()
466         }
467         if ($path eq "") {
468                 $path = "/";
469         } elsif ($project_name) {
470                 $path = project_path($path, $project_name);
471         }
472         return ($branch,$path);
475 sub branch_rev($$) {
477         my ($srcbranch,$uptorev) = @_;
479         my $bbranches = $branches{$srcbranch};
480         my @revs = reverse sort { ($a eq 'LAST' ? 0 : $a) <=> ($b eq 'LAST' ? 0 : $b) } keys %$bbranches;
481         my $therev;
482         foreach my $arev(@revs) {
483                 next if  ($arev eq 'LAST');
484                 if ($arev <= $uptorev) {
485                         $therev = $arev;
486                         last;
487                 }
488         }
489         return $therev;
492 sub expand_svndir($$$);
494 sub expand_svndir($$$)
496         my ($svnpath, $rev, $path) = @_;
497         my @list;
498         get_ignore(\@list, undef, $rev, $path, $svnpath);
499         my $dirents = $svn->dir_list($svnpath, $rev);
500         foreach my $p(keys %$dirents) {
501                 my $kind = node_kind($svnpath.'/'.$p, $rev);
502                 if ($kind eq $SVN::Node::file) {
503                         my $f = get_file($svnpath.'/'.$p, $rev, $path.'/'.$p);
504                         push(@list, $f) if $f;
505                 } elsif ($kind eq $SVN::Node::dir) {
506                         push(@list,
507                              expand_svndir($svnpath.'/'.$p, $rev, $path.'/'.$p));
508                 }
509         }
510         return @list;
513 sub copy_path($$$$$$$$) {
514         # Somebody copied a whole subdirectory.
515         # We need to find the index entries from the old version which the
516         # SVN log entry points to, and add them to the new place.
518         my($newrev,$newbranch,$path,$oldpath,$rev,$node_kind,$new,$parents) = @_;
520         my($srcbranch,$srcpath) = split_path($rev,$oldpath);
521         unless(defined $srcbranch && defined $srcpath) {
522                 print "Path not found when copying from $oldpath @ $rev.\n".
523                         "Will try to copy from original SVN location...\n"
524                         if $opt_v;
525                 push (@$new, expand_svndir($oldpath, $rev, $path));
526                 return;
527         }
528         my $therev = branch_rev($srcbranch, $rev);
529         my $gitrev = $branches{$srcbranch}{$therev};
530         unless($gitrev) {
531                 print STDERR "$newrev:$newbranch: could not find $oldpath \@ $rev\n";
532                 return;
533         }
534         if ($srcbranch ne $newbranch) {
535                 push(@$parents, $branches{$srcbranch}{'LAST'});
536         }
537         print "$newrev:$newbranch:$path: copying from $srcbranch:$srcpath @ $rev\n" if $opt_v;
538         if ($node_kind eq $SVN::Node::dir) {
539                 $srcpath =~ s#/*$#/#;
540         }
541         
542         my $pid = open my $f,'-|';
543         die $! unless defined $pid;
544         if (!$pid) {
545                 exec("git-ls-tree","-r","-z",$gitrev,$srcpath)
546                         or die $!;
547         }
548         local $/ = "\0";
549         while(<$f>) {
550                 chomp;
551                 my($m,$p) = split(/\t/,$_,2);
552                 my($mode,$type,$sha1) = split(/ /,$m);
553                 next if $type ne "blob";
554                 if ($node_kind eq $SVN::Node::dir) {
555                         $p = $path . substr($p,length($srcpath)-1);
556                 } else {
557                         $p = $path;
558                 }
559                 push(@$new,[$mode,$sha1,$p]);   
560         }
561         close($f) or
562                 print STDERR "$newrev:$newbranch: could not list files in $oldpath \@ $rev\n";
565 sub commit {
566         my($branch, $changed_paths, $revision, $author, $date, $message) = @_;
567         my($committer_name,$committer_email,$dest);
568         my($author_name,$author_email);
569         my(@old,@new,@parents);
571         if (not defined $author or $author eq "") {
572                 $committer_name = $committer_email = "unknown";
573         } elsif (defined $users_file) {
574                 die "User $author is not listed in $users_file\n"
575                     unless exists $users{$author};
576                 ($committer_name,$committer_email) = @{$users{$author}};
577         } elsif ($author =~ /^(.*?)\s+<(.*)>$/) {
578                 ($committer_name, $committer_email) = ($1, $2);
579         } else {
580                 $author =~ s/^<(.*)>$/$1/;
581                 $committer_name = $committer_email = $author;
582         }
584         if ($opt_F && $message =~ /From:\s+(.*?)\s+<(.*)>\s*\n/) {
585                 ($author_name, $author_email) = ($1, $2);
586                 print "Author from From: $1 <$2>\n" if ($opt_v);;
587         } elsif ($opt_S && $message =~ /Signed-off-by:\s+(.*?)\s+<(.*)>\s*\n/) {
588                 ($author_name, $author_email) = ($1, $2);
589                 print "Author from Signed-off-by: $1 <$2>\n" if ($opt_v);;
590         } else {
591                 $author_name = $committer_name;
592                 $author_email = $committer_email;
593         }
595         $date = pdate($date);
597         my $tag;
598         my $parent;
599         if($branch eq "/") { # trunk
600                 $parent = $opt_o;
601         } elsif($branch =~ m#^/(.+)#) { # tag
602                 $tag = 1;
603                 $parent = $1;
604         } else { # "normal" branch
605                 # nothing to do
606                 $parent = $branch;
607         }
608         $dest = $parent;
610         my $prev = $changed_paths->{"/"};
611         if($prev and $prev->[0] eq "A") {
612                 delete $changed_paths->{"/"};
613                 my $oldpath = $prev->[1];
614                 my $rev;
615                 if(defined $oldpath) {
616                         my $p;
617                         ($parent,$p) = split_path($revision,$oldpath);
618                         if(defined $parent) {
619                                 if($parent eq "/") {
620                                         $parent = $opt_o;
621                                 } else {
622                                         $parent =~ s#^/##; # if it's a tag
623                                 }
624                         }
625                 } else {
626                         $parent = undef;
627                 }
628         }
630         my $rev;
631         if($revision > $opt_s and defined $parent) {
632                 open(H,"git-rev-parse --verify $parent |");
633                 $rev = <H>;
634                 close(H) or do {
635                         print STDERR "$revision: cannot find commit '$parent'!\n";
636                         return;
637                 };
638                 chop $rev;
639                 if(length($rev) != 40) {
640                         print STDERR "$revision: cannot find commit '$parent'!\n";
641                         return;
642                 }
643                 $rev = $branches{($parent eq $opt_o) ? "/" : $parent}{"LAST"};
644                 if($revision != $opt_s and not $rev) {
645                         print STDERR "$revision: do not know ancestor for '$parent'!\n";
646                         return;
647                 }
648         } else {
649                 $rev = undef;
650         }
652 #       if($prev and $prev->[0] eq "A") {
653 #               if(not $tag) {
654 #                       unless(open(H,"> $git_dir/refs/heads/$branch")) {
655 #                               print STDERR "$revision: Could not create branch $branch: $!\n";
656 #                               $state=11;
657 #                               next;
658 #                       }
659 #                       print H "$rev\n"
660 #                               or die "Could not write branch $branch: $!";
661 #                       close(H)
662 #                               or die "Could not write branch $branch: $!";
663 #               }
664 #       }
665         if(not defined $rev) {
666                 unlink($git_index);
667         } elsif ($rev ne $last_rev) {
668                 print "Switching from $last_rev to $rev ($branch)\n" if $opt_v;
669                 system("git-read-tree", $rev);
670                 die "read-tree failed for $rev: $?\n" if $?;
671                 $last_rev = $rev;
672         }
674         push (@parents, $rev) if defined $rev;
676         my $cid;
677         if($tag and not %$changed_paths) {
678                 $cid = $rev;
679         } else {
680                 my @paths = sort keys %$changed_paths;
681                 foreach my $path(@paths) {
682                         my $action = $changed_paths->{$path};
684                         if ($action->[0] eq "R") {
685                                 # refer to a file/tree in an earlier commit
686                                 push(@old,$path); # remove any old stuff
687                         }
688                         if(($action->[0] eq "A") || ($action->[0] eq "R")) {
689                                 my $node_kind = node_kind($action->[3], $revision);
690                                 if ($node_kind eq $SVN::Node::file) {
691                                         my $f = get_file($action->[3],
692                                                          $revision, $path);
693                                         if ($f) {
694                                                 push(@new,$f) if $f;
695                                         } else {
696                                                 my $opath = $action->[3];
697                                                 print STDERR "$revision: $branch: could not fetch '$opath'\n";
698                                         }
699                                 } elsif ($node_kind eq $SVN::Node::dir) {
700                                         if($action->[1]) {
701                                                 copy_path($revision, $branch,
702                                                           $path, $action->[1],
703                                                           $action->[2], $node_kind,
704                                                           \@new, \@parents);
705                                         } else {
706                                                 get_ignore(\@new, \@old, $revision,
707                                                            $path, $action->[3]);
708                                         }
709                                 }
710                         } elsif ($action->[0] eq "D") {
711                                 push(@old,$path);
712                         } elsif ($action->[0] eq "M") {
713                                 my $node_kind = node_kind($action->[3], $revision);
714                                 if ($node_kind eq $SVN::Node::file) {
715                                         my $f = get_file($action->[3],
716                                                          $revision, $path);
717                                         push(@new,$f) if $f;
718                                 } elsif ($node_kind eq $SVN::Node::dir) {
719                                         get_ignore(\@new, \@old, $revision,
720                                                    $path, $action->[3]);
721                                 }
722                         } else {
723                                 die "$revision: unknown action '".$action->[0]."' for $path\n";
724                         }
725                 }
727                 while(@old) {
728                         my @o1;
729                         if(@old > 55) {
730                                 @o1 = splice(@old,0,50);
731                         } else {
732                                 @o1 = @old;
733                                 @old = ();
734                         }
735                         my $pid = open my $F, "-|";
736                         die "$!" unless defined $pid;
737                         if (!$pid) {
738                                 exec("git-ls-files", "-z", @o1) or die $!;
739                         }
740                         @o1 = ();
741                         local $/ = "\0";
742                         while(<$F>) {
743                                 chomp;
744                                 push(@o1,$_);
745                         }
746                         close($F);
748                         while(@o1) {
749                                 my @o2;
750                                 if(@o1 > 55) {
751                                         @o2 = splice(@o1,0,50);
752                                 } else {
753                                         @o2 = @o1;
754                                         @o1 = ();
755                                 }
756                                 system("git-update-index","--force-remove","--",@o2);
757                                 die "Cannot remove files: $?\n" if $?;
758                         }
759                 }
760                 while(@new) {
761                         my @n2;
762                         if(@new > 12) {
763                                 @n2 = splice(@new,0,10);
764                         } else {
765                                 @n2 = @new;
766                                 @new = ();
767                         }
768                         system("git-update-index","--add",
769                                 (map { ('--cacheinfo', @$_) } @n2));
770                         die "Cannot add files: $?\n" if $?;
771                 }
773                 my $pid = open(C,"-|");
774                 die "Cannot fork: $!" unless defined $pid;
775                 unless($pid) {
776                         exec("git-write-tree");
777                         die "Cannot exec git-write-tree: $!\n";
778                 }
779                 chomp(my $tree = <C>);
780                 length($tree) == 40
781                         or die "Cannot get tree id ($tree): $!\n";
782                 close(C)
783                         or die "Error running git-write-tree: $?\n";
784                 print "Tree ID $tree\n" if $opt_v;
786                 my $pr = IO::Pipe->new() or die "Cannot open pipe: $!\n";
787                 my $pw = IO::Pipe->new() or die "Cannot open pipe: $!\n";
788                 $pid = fork();
789                 die "Fork: $!\n" unless defined $pid;
790                 unless($pid) {
791                         $pr->writer();
792                         $pw->reader();
793                         open(OUT,">&STDOUT");
794                         dup2($pw->fileno(),0);
795                         dup2($pr->fileno(),1);
796                         $pr->close();
797                         $pw->close();
799                         my @par = ();
801                         # loose detection of merges
802                         # based on the commit msg
803                         foreach my $rx (@mergerx) {
804                                 if ($message =~ $rx) {
805                                         my $mparent = $1;
806                                         if ($mparent eq 'HEAD') { $mparent = $opt_o };
807                                         if ( -e "$git_dir/refs/heads/$mparent") {
808                                                 $mparent = get_headref($mparent, $git_dir);
809                                                 push (@parents, $mparent);
810                                                 print OUT "Merge parent branch: $mparent\n" if $opt_v;
811                                         }
812                                 }
813                         }
814                         my %seen_parents = ();
815                         my @unique_parents = grep { ! $seen_parents{$_} ++ } @parents;
816                         foreach my $bparent (@unique_parents) {
817                                 push @par, '-p', $bparent;
818                                 print OUT "Merge parent branch: $bparent\n" if $opt_v;
819                         }
821                         exec("env",
822                                 "GIT_AUTHOR_NAME=$author_name",
823                                 "GIT_AUTHOR_EMAIL=$author_email",
824                                 "GIT_AUTHOR_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
825                                 "GIT_COMMITTER_NAME=$committer_name",
826                                 "GIT_COMMITTER_EMAIL=$committer_email",
827                                 "GIT_COMMITTER_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
828                                 "git-commit-tree", $tree,@par);
829                         die "Cannot exec git-commit-tree: $!\n";
830                 }
831                 $pw->writer();
832                 $pr->reader();
834                 $message =~ s/[\s\n]+\z//;
835                 $message = "r$revision: $message" if $opt_r;
837                 print $pw "$message\n"
838                         or die "Error writing to git-commit-tree: $!\n";
839                 $pw->close();
841                 print "Committed change $revision:$branch ".strftime("%Y-%m-%d %H:%M:%S",gmtime($date)).")\n" if $opt_v;
842                 chomp($cid = <$pr>);
843                 length($cid) == 40
844                         or die "Cannot get commit id ($cid): $!\n";
845                 print "Commit ID $cid\n" if $opt_v;
846                 $pr->close();
848                 waitpid($pid,0);
849                 die "Error running git-commit-tree: $?\n" if $?;
850         }
852         if (not defined $cid) {
853                 $cid = $branches{"/"}{"LAST"};
854         }
856         if(not defined $dest) {
857                 print "... no known parent\n" if $opt_v;
858         } elsif(not $tag) {
859                 print "Writing to refs/heads/$dest\n" if $opt_v;
860                 open(C,">$git_dir/refs/heads/$dest") and
861                 print C ("$cid\n") and
862                 close(C)
863                         or die "Cannot write branch $dest for update: $!\n";
864         }
866         if($tag) {
867                 my($in, $out) = ('','');
868                 $last_rev = "-" if %$changed_paths;
869                 # the tag was 'complex', i.e. did not refer to a "real" revision
871                 $dest =~ tr/_/\./ if $opt_u;
872                 $branch = $dest;
874                 my $pid = open2($in, $out, 'git-mktag');
875                 print $out ("object $cid\n".
876                     "type commit\n".
877                     "tag $dest\n".
878                     "tagger $committer_name <$committer_email> 0 +0000\n") and
879                 close($out)
880                     or die "Cannot create tag object $dest: $!\n";
882                 my $tagobj = <$in>;
883                 chomp $tagobj;
885                 if ( !close($in) or waitpid($pid, 0) != $pid or
886                                 $? != 0 or $tagobj !~ /^[0123456789abcdef]{40}$/ ) {
887                         die "Cannot create tag object $dest: $!\n";
888                 }
890                 open(C,">$git_dir/refs/tags/$dest") and
891                 print C ("$tagobj\n") and
892                 close(C)
893                         or die "Cannot create tag $branch: $!\n";
895                 print "Created tag '$dest' on '$branch'\n" if $opt_v;
896         }
897         $branches{$branch}{"LAST"} = $cid;
898         $branches{$branch}{$revision} = $cid;
899         $last_rev = $cid;
900         print BRANCHES "$revision $branch $cid\n";
901         print "DONE: $revision $dest $cid\n" if $opt_v;
904 sub commit_all {
905         # Recursive use of the SVN connection does not work
906         local $svn = $svn2;
908         my ($changed_paths, $revision, $author, $date, $message, $pool) = @_;
909         my %p;
910         while(my($path,$action) = each %$changed_paths) {
911                 $p{$path} = [ $action->action,$action->copyfrom_path, $action->copyfrom_rev, $path ];
912         }
913         $changed_paths = \%p;
915         my %done;
916         my @col;
917         my $pref;
918         my $branch;
920         while(my($path,$action) = each %$changed_paths) {
921                 ($branch,$path) = split_path($revision,$path);
922                 next if not defined $branch;
923                 next if not defined $path;
924                 $done{$branch}{$path} = $action;
925         }
926         while(($branch,$changed_paths) = each %done) {
927                 commit($branch, $changed_paths, $revision, $author, $date, $message);
928         }
931 $opt_l = $svn->{'maxrev'} if not defined $opt_l or $opt_l > $svn->{'maxrev'};
933 if ($opt_l < $current_rev) {
934     print "Up to date: no new revisions to fetch!\n" if $opt_v;
935     unlink("$git_dir/SVN2GIT_HEAD");
936     exit;
939 print "Processing from $current_rev to $opt_l ...\n" if $opt_v;
941 my $from_rev;
942 my $to_rev = $current_rev;
944 while ($to_rev < $opt_l) {
945         $from_rev = $to_rev;
946         $to_rev = $from_rev + $repack_after;
947         $to_rev = $opt_l if $opt_l < $to_rev;
948         print "Fetching from $from_rev to $to_rev ...\n" if $opt_v;
949         my $pool=SVN::Pool->new;
950         $svn->{'svn'}->get_log("/",$from_rev,$to_rev,0,1,1,\&commit_all,$pool);
951         $pool->clear;
952         my $pid = fork();
953         die "Fork: $!\n" unless defined $pid;
954         unless($pid) {
955                 exec("git-repack", "-d")
956                         or die "Cannot repack: $!\n";
957         }
958         waitpid($pid, 0);
962 unlink($git_index);
964 if (defined $orig_git_index) {
965         $ENV{GIT_INDEX_FILE} = $orig_git_index;
966 } else {
967         delete $ENV{GIT_INDEX_FILE};
970 # Now switch back to the branch we were in before all of this happened
971 if($orig_branch) {
972         print "DONE\n" if $opt_v and (not defined $opt_l or $opt_l > 0);
973         system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master")
974                 if $forward_master;
975         unless ($opt_i) {
976                 system('git-read-tree', '-m', '-u', 'SVN2GIT_HEAD', 'HEAD');
977                 die "read-tree failed: $?\n" if $?;
978         }
979 } else {
980         $orig_branch = "master";
981         print "DONE; creating $orig_branch branch\n" if $opt_v and (not defined $opt_l or $opt_l > 0);
982         system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master")
983                 unless -f "$git_dir/refs/heads/master";
984         system('git-update-ref', 'HEAD', "$orig_branch");
985         unless ($opt_i) {
986                 system('git checkout');
987                 die "checkout failed: $?\n" if $?;
988         }
990 unlink("$git_dir/SVN2GIT_HEAD");
991 close(BRANCHES);