copy_dir becomes copy_path and handles both files and directories
The A (Add) and R (Replace) actions handling are unified. Signed-off-by: Yaacov Akiba Slama <ya@slamail.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Yaacov Akiba Slama committed
Nov 2, 2005 at 23:51 UTC
8168373fe7e6214205fd23e519f72c3f1bbb7080
1 file changed
+53
-37
git-svnimport.perl
+53
-37
@@ -348,21 +348,42 @@ sub split_path($$) {
348
return ($branch,$path);
349
}
350
351
-sub copy_subdir($$$$$$) {
351
+sub branch_rev($$) {
352
+
353
+ my ($srcbranch,$uptorev) = @_;
354
+
355
+ my $bbranches = $branches{$srcbranch};
356
+ my @revs = reverse sort { ($a eq 'LAST' ? 0 : $a) <=> ($b eq 'LAST' ? 0 : $b) } keys %$bbranches;
357
+ my $therev;
358
+ foreach my $arev(@revs) {
359
+ next if ($arev eq 'LAST');
360
+ if ($arev <= $uptorev) {
361
+ $therev = $arev;
362
+ last;
363
+ }
364
+ }
365
+ return $therev;
366
+}
367
+
368
+sub copy_path($$$$$$$) {
369
# Somebody copied a whole subdirectory.
370
# We need to find the index entries from the old version which the
371
# SVN log entry points to, and add them to the new place.
372
356
- my($newrev,$newbranch,$path,$oldpath,$rev,$new) = @_;
357
- my($branch,$srcpath) = split_path($rev,$oldpath);
373
+ my($newrev,$newbranch,$path,$oldpath,$rev,$node_kind,$new) = @_;
374
359
- my $gitrev = $branches{$branch}{$rev};
375
+ my($srcbranch,$srcpath) = split_path($rev,$oldpath);
376
+ my $therev = branch_rev($srcbranch, $rev);
377
+ my $gitrev = $branches{$srcbranch}{$therev};
378
unless($gitrev) {
379
print STDERR "$newrev:$newbranch: could not find $oldpath \@ $rev\n";
380
return;
381
}
364
- print "$newrev:$newbranch:$path: copying from $branch:$srcpath @ $rev\n" if $opt_v;
365
- $srcpath =~ s#/*$#/#;
382
+ print "$newrev:$newbranch:$path: copying from $srcbranch:$srcpath @ $rev\n" if $opt_v;
383
+ if ($node_kind eq $SVN::Node::dir) {
384
+ $srcpath =~ s#/*$#/#;
385
+ }
386
+
387
open my $f,"-|","git-ls-tree","-r","-z",$gitrev,$srcpath;
388
local $/ = "\0";
389
while(<$f>) {
@@ -370,9 +391,12 @@ sub copy_subdir($$$$$$) {
391
my($m,$p) = split(/\t/,$_,2);
392
my($mode,$type,$sha1) = split(/ /,$m);
393
next if $type ne "blob";
373
- $p = substr($p,length($srcpath)-1);
374
- print "... found $path$p ...\n" if $opt_v;
375
- push(@$new,[$mode,$sha1,$path.$p]);
394
+ if ($node_kind eq $SVN::Node::dir) {
395
+ $p = $path . substr($p,length($srcpath)-1);
396
+ } else {
397
+ $p = $path;
398
+ }
399
+ push(@$new,[$mode,$sha1,$p]);
400
}
401
close($f) or
402
print STDERR "$newrev:$newbranch: could not list files in $oldpath \@ $rev\n";
@@ -476,39 +500,31 @@ sub commit {
500
foreach my $path(@paths) {
501
my $action = $changed_paths->{$path};
502
479
- if ($action->[0] eq "A") {
480
- my $f = get_file($revision,$branch,$path);
481
- if($f) {
482
- push(@new,$f) if $f;
483
- } elsif($action->[1]) {
484
- copy_subdir($revision,$branch,$path,$action->[1],$action->[2],\@new);
485
- } else {
486
- my $opath = $action->[3];
487
- print STDERR "$revision: $branch: could not fetch '$opath'\n";
503
+ if ($action->[0] eq "R") {
504
+ # refer to a file/tree in an earlier commit
505
+ push(@old,$path); # remove any old stuff
506
+ }
507
+ if(($action->[0] eq "A") || ($action->[0] eq "R")) {
508
+ my $node_kind = node_kind($branch,$path,$revision);
509
+ if($action->[1]) {
510
+ copy_path($revision,$branch,$path,$action->[1],$action->[2],$node_kind,\@new);
511
+ } elsif ($node_kind eq $SVN::Node::file) {
512
+ my $f = get_file($revision,$branch,$path);
513
+ if ($f) {
514
+ push(@new,$f) if $f;
515
+ } else {
516
+ my $opath = $action->[3];
517
+ print STDERR "$revision: $branch: could not fetch '$opath'\n";
518
+ }
519
}
520
} elsif ($action->[0] eq "D") {
521
push(@old,$path);
522
} elsif ($action->[0] eq "M") {
492
- my $f = get_file($revision,$branch,$path);
493
- push(@new,$f) if $f;
494
- } elsif ($action->[0] eq "R") {
495
- # refer to a file/tree in an earlier commit
496
- push(@old,$path); # remove any old stuff
497
-
498
- # ... and add any new stuff
499
- my($b,$srcpath) = split_path($revision,$action->[1]);
500
- $srcpath =~ s#/*$#/#;
501
- open my $F,"-|","git-ls-tree","-r","-z", $branches{$b}{$action->[2]}, $srcpath;
502
- local $/ = "\0";
503
- while(<$F>) {
504
- chomp;
505
- my($m,$p) = split(/\t/,$_,2);
506
- my($mode,$type,$sha1) = split(/ /,$m);
507
- next if $type ne "blob";
508
- $p = substr($p,length($srcpath)-1);
509
- push(@new,[$mode,$sha1,$path.$p]);
523
+ my $node_kind = node_kind($branch,$path,$revision);
524
+ if ($node_kind eq $SVN::Node::file) {
525
+ my $f = get_file($revision,$branch,$path);
526
+ push(@new,$f) if $f;
527
}
511
- close($F);
528
} else {
529
die "$revision: unknown action '".$action->[0]."' for $path\n";
530
}