@matheusbsbs / matheus-bagliano / commits / 373274f978
Fork 0

git-svn: do our best to ensure that our ref and rev_db are consistent

Defer any signals that cause termination while they are updating; and put the update-ref call as close to the rename() as possible. Also, make things extra-safe (but slower) for people using --no-metadata since they can't rely on .rev_db being rebuilt if it's clobbered (well, I'm calling update-ref with the -m flag for reflogs, we don't yet have a way to rebuild .rev_db from reflogs. Signed-off-by: Eric Wong <normalperson@yhbt.net>

Eric Wong committed Jan 31, 2007 at 13:54 UTC 373274f978a48b62549f20059bff630d85533533
1 file changed +36 -4
git-svn.perl
+36 -4
@@ -631,6 +631,7 @@ use vars qw/$default_repo_id $default_ref_id $_no_metadata $_follow_parent
631 $_repack $_repack_flags/;
632 use Carp qw/croak/;
633 use File::Path qw/mkpath/;
634 +use File::Copy qw/copy/;
635 use IPC::Open3;
636
637 my $_repack_nr;
@@ -645,6 +646,9 @@ BEGIN {
646 svn:entry:committed-date/;
647 }
648
649 +my %LOCKFILES;
650 +END { unlink keys %LOCKFILES if %LOCKFILES }
651 +
652 sub fetch_all {
653 my ($repo_id, $url, $fetch) = @_;
654 my @gs;
@@ -1030,8 +1034,7 @@ sub do_git_commit {
1034 die "Failed to commit, invalid sha1: $commit\n";
1035 }
1036
1033 - command_noisy('update-ref',$self->refname, $commit);
1034 - $self->rev_db_set($log_entry->{revision}, $commit);
1037 + $self->rev_db_set($log_entry->{revision}, $commit, 1);
1038
1039 $self->{last_rev} = $log_entry->{revision};
1040 $self->{last_commit} = $commit;
@@ -1353,11 +1356,28 @@ sub rebuild {
1356 # to a revision: (41 * rev) is the byte offset.
1357 # A record of 40 0s denotes an empty revision.
1358 # And yes, it's still pretty fast (faster than Tie::File).
1359 +# These files are disposable unless --no-metadata is set
1360
1361 sub rev_db_set {
1358 - my ($self, $rev, $commit) = @_;
1362 + my ($self, $rev, $commit, $update_ref) = @_;
1363 length $commit == 40 or croak "arg3 must be a full SHA1 hexsum\n";
1360 - open my $fh, '+<', $self->{db_path} or croak $!;
1364 + my ($db, $db_lock) = ($self->{db_path}, "$self->{db_path}.lock");
1365 + my $sig;
1366 + if ($update_ref) {
1367 + $SIG{INT} = $SIG{HUP} = $SIG{TERM} = $SIG{ALRM} = $SIG{PIPE} =
1368 + $SIG{USR1} = $SIG{USR2} = sub { $sig = $_[0] };
1369 + }
1370 + $LOCKFILES{$db_lock} = 1;
1371 + if ($_no_metadata) {
1372 + copy($db, $db_lock) or die "rev_db_set(@_): ",
1373 + "Failed to copy: ",
1374 + "$db => $db_lock ($!)\n";
1375 + } else {
1376 + rename $db, $db_lock or die "rev_db_set(@_): ",
1377 + "Failed to rename: ",
1378 + "$db => $db_lock ($!)\n";
1379 + }
1380 + open my $fh, '+<', $db_lock or croak $!;
1381 my $offset = $rev * 41;
1382 # assume that append is the common case:
1383 seek $fh, 0, 2 or croak $!;
@@ -1370,6 +1390,18 @@ sub rev_db_set {
1390 seek $fh, $offset, 0 or croak $!;
1391 print $fh $commit,"\n" or croak $!;
1392 close $fh or croak $!;
1393 + if ($update_ref) {
1394 + command_noisy('update-ref', '-m', "r$rev",
1395 + $self->refname, $commit);
1396 + }
1397 + rename $db_lock, $db or die "rev_db_set(@_): ", "Failed to rename: ",
1398 + "$db_lock => $db ($!)\n";
1399 + delete $LOCKFILES{$db_lock};
1400 + if ($update_ref) {
1401 + $SIG{INT} = $SIG{HUP} = $SIG{TERM} = $SIG{ALRM} = $SIG{PIPE} =
1402 + $SIG{USR1} = $SIG{USR2} = 'DEFAULT';
1403 + kill $sig, $$ if defined $sig;
1404 + }
1405 }
1406
1407 sub rev_db_get {