@matheusbsbs / matheus-bagliano / commits / cf52b8f063
Fork 0

git-svn: fix several corner-case and rare bugs with 'commit'

None of these were really show-stoppers (or even triggered) on most of the trees I've tracked. * Node change prevention for identically named nodes. This is a limitation of SVN, but we find the error and exit before it's passed to SVN so we don't dirty our working tree when our commit fails. git-svn will exit with an error code 1 if any of the following conditions are found: 1. a directory is removed and a file of the same name of the removed directory is created 1a. a file has its parent directory removed and the file is takes the name of the removed parent directory:: baz/zzz => baz 2. a file is removed and a directory of the same name of the removed file is created. 2a. a file is moved into a deeper directory that shares the previous name of the file:: dir/$file => dir/file/$file Since SVN cannot handle these cases, the user will have to manually split the commit into several parts. * --rmdir now handles nested/deep removals. If dir/a/b/c/d/e/file is removed, and everything else is in the dir/ hierarchy is otherwise empty, then dir/ will be deleted when file is deleted from svn and --rmdir specified. * Always assert that we have written the tree we want to write on commits. This helped me find several bugs in the symlink handling code (which as been fixed). * Several symlink handling fixes. We now refuse to set permissions on symlinks. We also always unlink a file if we're going to overwrite it. * Apply changes in a pre-determined order, so we always have rename from locations handy before we delete them. Signed-off-by: Eric Wong <normalperson@yhbt.net> Signed-off-by: Junio C Hamano <junkio@cox.net>

Eric Wong committed Feb 20, 2006 at 10:57 UTC cf52b8f06389189bd32565c5c6adad75ac8a1a62
1 file changed +200 -60
contrib/git-svn/git-svn
+200 -60
@@ -238,7 +238,11 @@ sub commit {
238 my $svn_current_rev = svn_info('.')->{'Last Changed Rev'};
239 foreach my $c (@revs) {
240 print "Committing $c\n";
241 - svn_checkout_tree($svn_current_rev, $c);
241 + my $mods = svn_checkout_tree($svn_current_rev, $c);
242 + if (scalar @$mods == 0) {
243 + print "Skipping, no changes detected\n";
244 + next;
245 + }
246 $svn_current_rev = svn_commit_tree($svn_current_rev, $c);
247 }
248 print "Done committing ",scalar @revs," revisions to SVN\n";
@@ -267,9 +271,9 @@ sub setup_git_svn {
271 }
272
273 sub assert_svn_wc_clean {
270 - my ($svn_rev, $commit) = @_;
274 + my ($svn_rev, $treeish) = @_;
275 croak "$svn_rev is not an integer!\n" unless ($svn_rev =~ /^\d+$/);
272 - croak "$commit is not a sha1!\n" unless ($commit =~ /^$sha1$/o);
276 + croak "$treeish is not a sha1!\n" unless ($treeish =~ /^$sha1$/o);
277 my $svn_info = svn_info('.');
278 if ($svn_rev != $svn_info->{'Last Changed Rev'}) {
279 croak "Expected r$svn_rev, got r",
@@ -282,12 +286,42 @@ sub assert_svn_wc_clean {
286 print STDERR $_ foreach @status;
287 croak;
288 }
285 - my ($tree_a) = grep(/^tree $sha1$/o,`git-cat-file commit $commit`);
286 - $tree_a =~ s/^tree //;
287 - chomp $tree_a;
288 - chomp(my $tree_b = `GIT_INDEX_FILE=$GIT_SVN_INDEX git-write-tree`);
289 - if ($tree_a ne $tree_b) {
290 - croak "$svn_rev != $commit, $tree_a != $tree_b\n";
289 + assert_tree($treeish);
290 +}
291 +
292 +sub assert_tree {
293 + my ($treeish) = @_;
294 + croak "Not a sha1: $treeish\n" unless $treeish =~ /^$sha1$/o;
295 + chomp(my $type = `git-cat-file -t $treeish`);
296 + my $expected;
297 + while ($type eq 'tag') {
298 + chomp(($treeish, $type) = `git-cat-file tag $treeish`);
299 + }
300 + if ($type eq 'commit') {
301 + $expected = (grep /^tree /,`git-cat-file commit $treeish`)[0];
302 + ($expected) = ($expected =~ /^tree ($sha1)$/);
303 + die "Unable to get tree from $treeish\n" unless $expected;
304 + } elsif ($type eq 'tree') {
305 + $expected = $treeish;
306 + } else {
307 + die "$treeish is a $type, expected tree, tag or commit\n";
308 + }
309 +
310 + my $old_index = $ENV{GIT_INDEX_FILE};
311 + my $tmpindex = $GIT_SVN_INDEX.'.assert-tmp';
312 + if (-e $tmpindex) {
313 + unlink $tmpindex or croak $!;
314 + }
315 + $ENV{GIT_INDEX_FILE} = $tmpindex;
316 + git_addremove();
317 + chomp(my $tree = `git-write-tree`);
318 + if ($old_index) {
319 + $ENV{GIT_INDEX_FILE} = $old_index;
320 + } else {
321 + delete $ENV{GIT_INDEX_FILE};
322 + }
323 + if ($tree ne $expected) {
324 + croak "Tree mismatch, Got: $tree, Expected: $expected\n";
325 }
326 }
327
@@ -298,7 +332,6 @@ sub parse_diff_tree {
332 my @mods;
333 while (<$diff_fh>) {
334 chomp $_; # this gets rid of the trailing "\0"
301 - print $_,"\n";
335 if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s
336 $sha1\s($sha1)\s([MTCRAD])\d*$/xo) {
337 push @mods, { mode_a => $1, mode_b => $2,
@@ -309,36 +342,44 @@ sub parse_diff_tree {
342 $state = 'file_b';
343 }
344 } elsif ($state eq 'file_a') {
312 - my $x = $mods[$#mods] or croak __LINE__,": Empty array\n";
345 + my $x = $mods[$#mods] or croak "Empty array\n";
346 if ($x->{chg} !~ /^(?:C|R)$/) {
314 - croak __LINE__,": Error parsing $_, $x->{chg}\n";
347 + croak "Error parsing $_, $x->{chg}\n";
348 }
349 $x->{file_a} = $_;
350 $state = 'file_b';
351 } elsif ($state eq 'file_b') {
319 - my $x = $mods[$#mods] or croak __LINE__,": Empty array\n";
352 + my $x = $mods[$#mods] or croak "Empty array\n";
353 if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) {
321 - croak __LINE__,": Error parsing $_, $x->{chg}\n";
354 + croak "Error parsing $_, $x->{chg}\n";
355 }
356 if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) {
324 - croak __LINE__,": Error parsing $_, $x->{chg}\n";
357 + croak "Error parsing $_, $x->{chg}\n";
358 }
359 $x->{file_b} = $_;
360 $state = 'meta';
361 } else {
329 - croak __LINE__,": Error parsing $_\n";
362 + croak "Error parsing $_\n";
363 }
364 }
365 close $diff_fh or croak $!;
366 +
367 return \@mods;
368 }
369
370 sub svn_check_prop_executable {
371 my $m = shift;
338 - if ($m->{mode_b} =~ /755$/ && $m->{mode_a} !~ /755$/) {
339 - sys(qw(svn propset svn:executable 1), $m->{file_b});
372 + return if -l $m->{file_b};
373 + if ($m->{mode_b} =~ /755$/) {
374 + chmod((0755 &~ umask),$m->{file_b}) or croak $!;
375 + if ($m->{mode_a} !~ /755$/) {
376 + sys(qw(svn propset svn:executable 1), $m->{file_b});
377 + }
378 + -x $m->{file_b} or croak "$m->{file_b} is not executable!\n";
379 } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) {
380 sys(qw(svn propdel svn:executable), $m->{file_b});
381 + chmod((0644 &~ umask),$m->{file_b}) or croak $!;
382 + -x $m->{file_b} and croak "$m->{file_b} is executable!\n";
383 }
384 }
385
@@ -349,84 +390,166 @@ sub svn_ensure_parent_path {
390 sys(qw(svn add -N), $dir_b) unless (-d "$dir_b/.svn");
391 }
392
393 +sub precommit_check {
394 + my $mods = shift;
395 + my (%rm_file, %rmdir_check, %added_check);
396 +
397 + my %o = ( D => 0, R => 1, C => 2, A => 3, M => 3, T => 3 );
398 + foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
399 + if ($m->{chg} eq 'R') {
400 + if (-d $m->{file_b}) {
401 + err_dir_to_file("$m->{file_a} => $m->{file_b}");
402 + }
403 + # dir/$file => dir/file/$file
404 + my $dirname = dirname($m->{file_b});
405 + while ($dirname ne File::Spec->curdir) {
406 + if ($dirname ne $m->{file_a}) {
407 + $dirname = dirname($dirname);
408 + next;
409 + }
410 + err_file_to_dir("$m->{file_a} => $m->{file_b}");
411 + }
412 + # baz/zzz => baz (baz is a file)
413 + $dirname = dirname($m->{file_a});
414 + while ($dirname ne File::Spec->curdir) {
415 + if ($dirname ne $m->{file_b}) {
416 + $dirname = dirname($dirname);
417 + next;
418 + }
419 + err_dir_to_file("$m->{file_a} => $m->{file_b}");
420 + }
421 + }
422 + if ($m->{chg} =~ /^(D|R)$/) {
423 + my $t = $1 eq 'D' ? 'file_b' : 'file_a';
424 + $rm_file{ $m->{$t} } = 1;
425 + my $dirname = dirname( $m->{$t} );
426 + my $basename = basename( $m->{$t} );
427 + $rmdir_check{$dirname}->{$basename} = 1;
428 + } elsif ($m->{chg} =~ /^(?:A|C)$/) {
429 + if (-d $m->{file_b}) {
430 + err_dir_to_file($m->{file_b});
431 + }
432 + my $dirname = dirname( $m->{file_b} );
433 + my $basename = basename( $m->{file_b} );
434 + $added_check{$dirname}->{$basename} = 1;
435 + while ($dirname ne File::Spec->curdir) {
436 + if ($rm_file{$dirname}) {
437 + err_file_to_dir($m->{file_b});
438 + }
439 + $dirname = dirname $dirname;
440 + }
441 + }
442 + }
443 + return (\%rmdir_check, \%added_check);
444 +
445 + sub err_dir_to_file {
446 + my $file = shift;
447 + print STDERR "Node change from directory to file ",
448 + "is not supported by Subversion: ",$file,"\n";
449 + exit 1;
450 + }
451 + sub err_file_to_dir {
452 + my $file = shift;
453 + print STDERR "Node change from file to directory ",
454 + "is not supported by Subversion: ",$file,"\n";
455 + exit 1;
456 + }
457 +}
458 +
459 sub svn_checkout_tree {
353 - my ($svn_rev, $commit) = @_;
460 + my ($svn_rev, $treeish) = @_;
461 my $from = file_to_s("$REV_DIR/$svn_rev");
462 assert_svn_wc_clean($svn_rev,$from);
356 - print "diff-tree '$from' '$commit'\n";
463 + print "diff-tree '$from' '$treeish'\n";
464 my $pid = open my $diff_fh, '-|';
465 defined $pid or croak $!;
466 if ($pid == 0) {
467 my @diff_tree = qw(git-diff-tree -z -r -C);
468 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
469 push @diff_tree, "-l$_l" if defined $_l;
363 - exec(@diff_tree, $from, $commit) or croak $!;
470 + exec(@diff_tree, $from, $treeish) or croak $!;
471 }
472 my $mods = parse_diff_tree($diff_fh);
473 unless (@$mods) {
474 # git can do empty commits, SVN doesn't allow it...
368 - return $svn_rev;
475 + return $mods;
476 }
370 - my %rm;
371 - foreach my $m (@$mods) {
477 + my ($rm, $add) = precommit_check($mods);
478 +
479 + my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 );
480 + foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
481 if ($m->{chg} eq 'C') {
482 svn_ensure_parent_path( $m->{file_b} );
483 sys(qw(svn cp), $m->{file_a}, $m->{file_b});
375 - blob_to_file( $m->{sha1_b}, $m->{file_b});
484 + apply_mod_line_blob($m);
485 svn_check_prop_executable($m);
486 } elsif ($m->{chg} eq 'D') {
378 - $rm{dirname $m->{file_b}}->{basename $m->{file_b}} = 1;
487 sys(qw(svn rm --force), $m->{file_b});
488 } elsif ($m->{chg} eq 'R') {
489 svn_ensure_parent_path( $m->{file_b} );
490 sys(qw(svn mv --force), $m->{file_a}, $m->{file_b});
383 - blob_to_file( $m->{sha1_b}, $m->{file_b});
491 + apply_mod_line_blob($m);
492 svn_check_prop_executable($m);
385 - $rm{dirname $m->{file_a}}->{basename $m->{file_a}} = 1;
493 } elsif ($m->{chg} eq 'M') {
387 - if ($m->{mode_b} =~ /^120/ && $m->{mode_a} =~ /^120/) {
388 - unlink $m->{file_b} or croak $!;
389 - blob_to_symlink($m->{sha1_b}, $m->{file_b});
390 - } else {
391 - blob_to_file($m->{sha1_b}, $m->{file_b});
392 - }
494 + apply_mod_line_blob($m);
495 svn_check_prop_executable($m);
496 } elsif ($m->{chg} eq 'T') {
497 sys(qw(svn rm --force),$m->{file_b});
396 - if ($m->{mode_b} =~ /^120/ && $m->{mode_a} =~ /^100/) {
397 - blob_to_symlink($m->{sha1_b}, $m->{file_b});
398 - } else {
399 - blob_to_file($m->{sha1_b}, $m->{file_b});
400 - }
401 - svn_check_prop_executable($m);
498 + apply_mod_line_blob($m);
499 sys(qw(svn add --force), $m->{file_b});
500 + svn_check_prop_executable($m);
501 } elsif ($m->{chg} eq 'A') {
502 svn_ensure_parent_path( $m->{file_b} );
405 - blob_to_file( $m->{sha1_b}, $m->{file_b});
406 - if ($m->{mode_b} =~ /755$/) {
407 - chmod 0755, $m->{file_b};
408 - }
503 + apply_mod_line_blob($m);
504 sys(qw(svn add --force), $m->{file_b});
505 + svn_check_prop_executable($m);
506 } else {
507 croak "Invalid chg: $m->{chg}\n";
508 }
509 }
414 - if ($_rmdir) {
415 - my $old_index = $ENV{GIT_INDEX_FILE};
416 - $ENV{GIT_INDEX_FILE} = $GIT_SVN_INDEX;
417 - foreach my $dir (keys %rm) {
418 - my $files = $rm{$dir};
419 - my @files;
420 - foreach (safe_qx('svn','ls',$dir)) {
421 - chomp;
422 - push @files, $_ unless $files->{$_};
423 - }
424 - sys(qw(svn rm),$dir) unless @files;
425 - }
426 - if ($old_index) {
427 - $ENV{GIT_INDEX_FILE} = $old_index;
428 - } else {
429 - delete $ENV{GIT_INDEX_FILE};
510 +
511 + assert_tree($treeish);
512 + if ($_rmdir) { # remove empty directories
513 + handle_rmdir($rm, $add);
514 + }
515 + assert_tree($treeish);
516 + return $mods;
517 +}
518 +
519 +# svn ls doesn't work with respect to the current working tree, but what's
520 +# in the repository. There's not even an option for it... *sigh*
521 +# (added files don't show up and removed files remain in the ls listing)
522 +sub svn_ls_current {
523 + my ($dir, $rm, $add) = @_;
524 + chomp(my @ls = safe_qx('svn','ls',$dir));
525 + my @ret = ();
526 + foreach (@ls) {
527 + s#/$##; # trailing slashes are evil
528 + push @ret, $_ unless $rm->{$dir}->{$_};
529 + }
530 + if (exists $add->{$dir}) {
531 + push @ret, keys %{$add->{$dir}};
532 + }
533 + return \@ret;
534 +}
535 +
536 +sub handle_rmdir {
537 + my ($rm, $add) = @_;
538 +
539 + foreach my $dir (sort {length $b <=> length $a} keys %$rm) {
540 + my $ls = svn_ls_current($dir, $rm, $add);
541 + next if (scalar @$ls);
542 + sys(qw(svn rm --force),$dir);
543 +
544 + my $dn = dirname $dir;
545 + $rm->{ $dn }->{ basename $dir } = 1;
546 + $ls = svn_ls_current($dn, $rm, $add);
547 + while (scalar @$ls == 0 && $dn ne File::Spec->curdir) {
548 + sys(qw(svn rm --force),$dn);
549 + $dir = basename $dn;
550 + $dn = dirname $dn;
551 + $rm->{ $dn }->{ $dir } = 1;
552 + $ls = svn_ls_current($dn, $rm, $add);
553 }
554 }
555 }
@@ -692,10 +815,23 @@ sub git_commit {
815 return $commit;
816 }
817
818 +sub apply_mod_line_blob {
819 + my $m = shift;
820 + if ($m->{mode_b} =~ /^120/) {
821 + blob_to_symlink($m->{sha1_b}, $m->{file_b});
822 + } else {
823 + blob_to_file($m->{sha1_b}, $m->{file_b});
824 + }
825 +}
826 +
827 sub blob_to_symlink {
828 my ($blob, $link) = @_;
829 defined $link or croak "\$link not defined!\n";
830 croak "Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
831 + if (-l $link || -f _) {
832 + unlink $link or croak $!;
833 + }
834 +
835 my $dest = `git-cat-file blob $blob`; # no newline, so no chomp
836 symlink $dest, $link or croak $!;
837 }
@@ -704,6 +840,10 @@ sub blob_to_file {
840 my ($blob, $file) = @_;
841 defined $file or croak "\$file not defined!\n";
842 croak "Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
843 + if (-l $file || -f _) {
844 + unlink $file or croak $!;
845 + }
846 +
847 open my $blob_fh, '>', $file or croak "$!: $file\n";
848 my $pid = fork;
849 defined $pid or croak $!;