@matheusbsbs / matheus-bagliano / commits / 0090a6185a
Fork 0

svn import: copy directories

Import SVN-copied and -referenced directories correctly. Signed-Off-By: Matthias Urlichs <smurf@smurf.noris.de>

Matthias Urlichs committed Oct 11, 2005 at 19:42 UTC 0090a6185af18a199d5bc12781adf411177f1e1a
1 file changed +56 -13
git-svnimport.perl
+56 -13
@@ -265,11 +265,11 @@ sub get_file($$$) {
265 my $svnpath;
266 $path = "" if $path eq "/"; # this should not happen, but ...
267 if($branch eq "/") {
268 - $svnpath = "/$trunk_name/$path";
268 + $svnpath = "$trunk_name/$path";
269 } elsif($branch =~ m#^/#) {
270 - $svnpath = "/$tag_name$branch/$path";
270 + $svnpath = "$tag_name$branch/$path";
271 } else {
272 - $svnpath = "/$branch_name/$branch/$path";
272 + $svnpath = "$branch_name/$branch/$path";
273 }
274
275 # now get it
@@ -280,7 +280,7 @@ sub get_file($$$) {
280 # /svn/!svn/bc/2/django/trunk/django-docs/build.py
281 my $url=$svn_url->clone();
282 $url->path($url->path."/!svn/bc/$rev/$svn_dir$svnpath");
283 - print "Fetching $url...\n" if $opt_v;
283 + print "... $path...\n" if $opt_v;
284 $req = HTTP::Request->new(GET => $url);
285 $res = $lwp_ua->request($req);
286 if ($res->is_success) {
@@ -294,7 +294,7 @@ sub get_file($$$) {
294 die $res->status_line." at $url\n";
295 }
296 } else {
297 - $name = $svn->file($svnpath,$rev);
297 + $name = $svn->file("/$svnpath",$rev);
298 return undef unless defined $name;
299 }
300
@@ -326,6 +326,36 @@ sub split_path($$) {
326 return ($branch,$path);
327 }
328
329 +sub copy_subdir($$$$$$) {
330 + # Somebody copied a whole subdirectory.
331 + # We need to find the index entries from the old version which the
332 + # SVN log entry points to, and add them to the new place.
333 +
334 + my($newrev,$newbranch,$path,$oldpath,$rev,$new) = @_;
335 + my($branch,$srcpath) = split_path($rev,$oldpath);
336 +
337 + my $gitrev = $branches{$branch}{$rev};
338 + unless($gitrev) {
339 + print STDERR "$newrev:$newbranch: could not find $oldpath \@ $rev\n";
340 + return;
341 + }
342 + print "$newrev:$newbranch:$path: copying from $branch:$srcpath @ $rev\n" if $opt_v;
343 + $srcpath =~ s#/*$#/#;
344 + open my $f,"-|","git-ls-tree","-r","-z",$gitrev,$srcpath;
345 + local $/ = "\0";
346 + while(<$f>) {
347 + chomp;
348 + my($m,$p) = split(/\t/,$_,2);
349 + my($mode,$type,$sha1) = split(/ /,$m);
350 + next if $type ne "blob";
351 + $p = substr($p,length($srcpath)-1);
352 + print "... found $path$p ...\n" if $opt_v;
353 + push(@$new,[$mode,$sha1,$path.$p]);
354 + }
355 + close($f) or
356 + print STDERR "$newrev:$newbranch: could not list files in $oldpath \@ $rev\n";
357 +}
358 +
359 sub commit {
360 my($branch, $changed_paths, $revision, $author, $date, $message) = @_;
361 my($author_name,$author_email,$dest);
@@ -420,10 +450,20 @@ sub commit {
450 if($tag and not %$changed_paths) {
451 $cid = $rev;
452 } else {
423 - while(my($path,$action) = each %$changed_paths) {
453 + my @paths = sort keys %$changed_paths;
454 + foreach my $path(@paths) {
455 + my $action = $changed_paths->{$path};
456 +
457 if ($action->[0] eq "A") {
458 my $f = get_file($revision,$branch,$path);
426 - push(@new,$f) if $f;
459 + if($f) {
460 + push(@new,$f) if $f;
461 + } elsif($action->[1]) {
462 + copy_subdir($revision,$branch,$path,$action->[1],$action->[2],\@new);
463 + } else {
464 + my $opath = $action->[3];
465 + print STDERR "$revision: $branch: could not fetch '$opath'\n";
466 + }
467 } elsif ($action->[0] eq "D") {
468 push(@old,$path);
469 } elsif ($action->[0] eq "M") {
@@ -434,16 +474,19 @@ sub commit {
474 push(@old,$path); # remove any old stuff
475
476 # ... and add any new stuff
437 - my($b,$p) = split_path($revision,$action->[1]);
438 - open my $F,"-|","git-ls-tree","-r","-z", $branches{$b}{$action->[2]}, $p;
439 - local $/ = '\0';
477 + my($b,$srcpath) = split_path($revision,$action->[1]);
478 + $srcpath =~ s#/*$#/#;
479 + open my $F,"-|","git-ls-tree","-r","-z", $branches{$b}{$action->[2]}, $srcpath;
480 + local $/ = "\0";
481 while(<$F>) {
482 chomp;
483 my($m,$p) = split(/\t/,$_,2);
484 my($mode,$type,$sha1) = split(/ /,$m);
485 next if $type ne "blob";
445 - push(@new,[$mode,$sha1,$p]);
486 + $p = substr($p,length($srcpath)-1);
487 + push(@new,[$mode,$sha1,$path.$p]);
488 }
489 + close($F);
490 } else {
491 die "$revision: unknown action '".$action->[0]."' for $path\n";
492 }
@@ -452,7 +495,7 @@ sub commit {
495 if(@old) {
496 open my $F, "-|", "git-ls-files", "-z", @old or die $!;
497 @old = ();
455 - local $/ = '\0';
498 + local $/ = "\0";
499 while(<$F>) {
500 chomp;
501 push(@old,$_);
@@ -609,7 +652,7 @@ sub _commit_all {
652 ($changed_paths, $revision, $author, $date, $message, $pool) = @_;
653 my %p;
654 while(my($path,$action) = each %$changed_paths) {
612 - $p{$path} = [ $action->action,$action->copyfrom_path, $action->copyfrom_rev ];
655 + $p{$path} = [ $action->action,$action->copyfrom_path, $action->copyfrom_rev, $path ];
656 }
657 $changed_paths = \%p;
658 }