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